Overpass API regression suite coverage (Nova Scotia) / overpass_api/dispatch/scripting_core.cc

overpass_api/dispatch/scripting_core.cc

124 of 142 instrumented lines run by the suite (87.3%) at 8edf11b0419c (this file on GitHub).

runnever runcolumns: line, executions, queries (click for the queries that run the line)

1/** Copyright 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018 Roland Olbricht et al.
2 *
3 * This file is part of Overpass_API.
4 *
5 * Overpass_API is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as
7 * published by the Free Software Foundation, either version 3 of the
8 * License, or (at your option) any later version.
9 *
10 * Overpass_API is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with Overpass_API. If not, see <http://www.gnu.org/licenses/>.
17 */
19#include "resource_manager.h"
20#include "scripting_core.h"
21#include "../core/parsed_query.h"
22#include "../core/settings.h"
23#include "../frontend/console_output.h"
24#include "../frontend/map_ql_parser.h"
25#include "../frontend/user_interface.h"
26#include "../statements/area_query.h"
27#include "../statements/coord_query.h"
28#include "../statements/id_query.h"
29#include "../statements/make_area.h"
30#include "../statements/map_to_area.h"
31#include "../statements/osm_script.h"
32#include "../statements/query.h"
33#include "../statements/statement.h"
34#include "../statements/statement_dump.h"
35#include "../../expat/expat_justparse_interface.h"
36#include "../../template_db/dispatcher.h"
37#include "../../template_db/types.h"
39#include <errno.h>
40#include <fcntl.h>
41#include <sys/mman.h>
42#include <sys/select.h>
43#include <sys/stat.h>
44#include <sys/types.h>
45#include <unistd.h>
47#include <cstdlib>
48#include <cstring>
49#include <ctime>
50#include <fstream>
51#include <sstream>
52#include <string>
53#include <vector>
56namespace
58 std::vector< Statement* > statement_stack_;
59 std::vector< Statement_Dump* > statement_dump_stack_;
60 std::vector< std::string > text_stack;
61 Script_Parser xml_parser;
63 template< class TStatement >
64 std::vector< TStatement* >& statement_stack();
66 template< >
672888 q std::vector< Statement* >& statement_stack< Statement >() { return statement_stack_; }
69 template< >
709908 q std::vector< Statement_Dump* >& statement_stack< Statement_Dump >() { return statement_dump_stack_; }
74int determine_area_level(Error_Output* error_output, int area_level)
76595595 q if ((area_level < 2) && (Make_Area_Statement::is_used()))
770 {
780 if (error_output)
790 {
800 error_output->runtime_error
810 ("Specify --rules to execute a rule. make-area can only appear in rules.");
820 throw Exit_Error();
830 }
840 }
85595595 q if ((area_level == 0) &&
86595595 q (Coord_Query_Statement::is_used() || Area_Query_Statement::is_used() ||
87595595 q Map_To_Area_Statement::is_used() ||
88595595 q Query_Statement::area_query_exists() || Id_Query_Statement::area_query_exists()))
89102102 q area_level = 1;
91595595 q return area_level;
95Statement::Factory* stmt_factory_global = 0;
96Statement_Dump::Factory* stmt_dump_factory_global = 0;
98Statement::Factory* get_factory(Statement::Factory*)
100508 q return stmt_factory_global;
103Statement_Dump::Factory* get_factory(Statement_Dump::Factory*)
1041508 q{
1051508 q return stmt_dump_factory_global;
1061508 q}
108template< class TStatement >
109void start(const char *el, const char **attr)
1102008 q{
1112008 q TStatement* statement(get_factory((typename TStatement::Factory*)(0))->create_statement
1122008 q (el, xml_parser.current_line_number(), convert_c_pairs(attr)));
1142008 q statement_stack< TStatement >().push_back(statement);
1152008 q text_stack.push_back(xml_parser.get_parsed_text());
1162008 q xml_parser.reset_parsed_text();
1172008 q}
119template< class TStatement >
120void end(const char *el)
1211967 q{
1221967 q if (statement_stack< TStatement >().size() > 1)
1231687 q {
1241687 q TStatement* statement(statement_stack< TStatement >().back());
1261687 q if (statement)
1271687 q {
1281687 q statement->add_final_text(xml_parser.get_parsed_text());
1291687 q xml_parser.reset_parsed_text();
130/* statement->set_endpos(get_tag_end());*/
1311687 q }
1331687 q statement_stack< TStatement >().pop_back();
1341687 q if (statement_stack< TStatement >().back() && statement)
1351687 q statement_stack< TStatement >().back()->add_statement(statement, text_stack.back());
1361687 q text_stack.pop_back();
1371687 q }
138287 q else if ((statement_stack< TStatement >().size() == 1) &&
139287 q (statement_stack< TStatement >().front()))
140287 q statement_stack< TStatement >().front()->add_final_text(xml_parser.get_parsed_text());
1411967 q}
143bool parse_and_validate
144 (Statement::Factory& stmt_factory, Parsed_Query& parsed_query,
145 const std::string& xml_raw, Error_Output* error_output, Debug_Level debug_level)
1472448612 q if (error_output && error_output->display_encoding_errors())
1480 return false;
1502448612 q unsigned int pos(0);
1512448612 q while (pos < xml_raw.size() && isspace(xml_raw[pos]))
1520 ++pos;
1542448612 q if (pos < xml_raw.size() && xml_raw[pos] == '<')
155328 q {
156328 q try
157328 q {
158328 q if (debug_level == parser_dump_xml || debug_level == parser_dump_compact_map_ql
159328 q || debug_level == parser_dump_pretty_map_ql || debug_level == parser_dump_bbox_map_ql)
160248 q {
161248 q Statement_Dump::Factory stmt_dump_factory(stmt_factory);
162248 q stmt_dump_factory_global = &stmt_dump_factory;
163248 q xml_parser.parse(xml_raw, start< Statement_Dump >, end< Statement_Dump >);
165248 q for (std::vector< Statement_Dump* >::const_iterator it =
166248 q statement_stack< Statement_Dump >().begin();
167458 q it != statement_stack< Statement_Dump >().end(); ++it)
168217 q {
169217 q if (debug_level == parser_dump_xml)
17077 q std::cout<<(*it)->dump_xml();
171147 q else if (debug_level == parser_dump_compact_map_ql)
17277 q std::cout<<(*it)->dump_compact_map_ql(stmt_factory);
17377 q else if (debug_level == parser_dump_bbox_map_ql)
1740 std::cout<<(*it)->dump_bbox_map_ql(stmt_factory);
17577 q else if (debug_level == parser_dump_pretty_map_ql)
17677 q std::cout<<(*it)->dump_pretty_map_ql(stmt_factory);
177217 q }
178248 q for (std::vector< Statement_Dump* >::iterator it = statement_stack< Statement_Dump >().begin();
179458 q it != statement_stack< Statement_Dump >().end(); ++it)
180217 q delete *it;
181248 q }
18288 q else
18488 q stmt_factory_global = &stmt_factory;
18588 q xml_parser.parse(xml_raw, start< Statement >, end< Statement >);
18688 q Osm_Script_Statement* root =
18788 q get_statement_stack()->empty() ? 0 :
18888 q dynamic_cast< Osm_Script_Statement* >(get_statement_stack()->front());
18988 q if (root)
19077 q root->set_factory(&stmt_factory);
19188 q stmt_factory_global = 0;
193328 q }
194328 q catch(Parse_Error parse_error)
195328 q {
19641 q if (error_output)
19741 q error_output->add_parse_error(parse_error.message,
19841 q xml_parser.current_line_number());
200328 q catch(File_Error e)
201328 q {
2020 std::ostringstream temp;
2030 temp<<"open: "<<e.error_number<<' '<<strerror(e.error_number)<<' '<<e.filename<<' '<<e.origin;
2040 if (error_output)
2050 error_output->runtime_error(temp.str());
2070 return false;
2080 }
209328 q }
2102416604 q else
2112416604 q {
2122416604 q if (debug_level == parser_execute)
214604604 q parse_and_validate_map_ql(stmt_factory, xml_raw, error_output, parsed_query);
215604604 q Osm_Script_Statement* root =
216604604 q dynamic_cast< Osm_Script_Statement* >(get_statement_stack()->front());
217604604 q if (root)
218604604 q root->set_factory(&stmt_factory);
2201812604 q else if (debug_level == parser_dump_xml)
221604604 q parse_and_dump_xml_from_map_ql(stmt_factory, xml_raw, error_output, parsed_query);
2221208604 q else if (debug_level == parser_dump_compact_map_ql)
223604604 q parse_and_dump_compact_from_map_ql(stmt_factory, xml_raw, error_output, parsed_query);
224604604 q else if (debug_level == parser_dump_bbox_map_ql)
2250 parse_and_dump_bbox_from_map_ql(stmt_factory, xml_raw, error_output, parsed_query);
226604604 q else if (debug_level == parser_dump_pretty_map_ql)
227604604 q parse_and_dump_pretty_from_map_ql(stmt_factory, xml_raw, error_output, parsed_query);
2282416604 q }
2302448612 q if ((error_output) && (error_output->display_parse_errors()))
2328423 q return false;
2342364597 q if ((error_output) && (error_output->display_static_errors()))
23662 q return false;
2392358597 q return true;
242std::vector< Statement* >* get_statement_stack()
2444197611 q return &statement_stack_;