/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /*************************************************************************** * macroparser.cc * * Wed Jun 4 11:57:38 CEST 2008 * Copyright 2008 Bent Bisballe Nyeng * deva@aasimon.org ****************************************************************************/ /* * This file is part of Pracro. * * Pracro is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Pracro is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Pracro; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. */ #include "debug.h" #include "macroparser.h" #include "configuration.h" #include // For assert #include // For open and friends #include #include #include #include // For vprintf and friends #include #ifndef XML // For XML #include #endif/*XML*/ #include #include #include "exception.h" void MacroParser::error(const char* fmt, ...) { PRACRO_ERR_LOG(macro, "Error in MacroParser: "); { va_list argp; va_start(argp, fmt); PRACRO_ERR_LOG_VA(macro, fmt, argp); va_end(argp); fprintf(stderr, "\n"); } { char *p; va_list argp; va_start(argp, fmt); if(vasprintf(&p, fmt, argp) != -1) { throw Exception("Error in MacroParser: " + std::string(p)); free(p); } va_end(argp); } } MacroParser::MacroParser(std::string macrofile) { state = UNDEFINED; m = NULL; current_map = NULL; current_script = NULL; file = macrofile; PRACRO_DEBUG(macro, "Using macro file: %s\n", file.c_str()); fd = open(file.c_str(), O_RDONLY); if(fd == -1) error("Could not open file %s", file.c_str()); } MacroParser::~MacroParser() { if(fd != -1) close(fd); if(m) delete m; } void MacroParser::characterData(std::string &data) { if(state == RESUME) { assert(m); // No macro present! m->resume.attributes["format"].append(data); } if(state == MAP) { assert(current_map); // No map present! current_map->attributes["lua"].append(data); } if(state == SCRIPT) { assert(current_script); // No script present! current_script->attributes["code"].append(data); } } void MacroParser::startTag(std::string name, std::map< std::string, std::string> attributes) { // Create macro and enable parsing of queries, maps and widgets if(name == "macro") { if(state != UNDEFINED) error("macro found not root tag."); state = MACRO; assert(!m); // A Macro has already been allocated, cannot create macro! m = new Macro(); m->attributes = attributes; return; } // Enable resume parsing if(name == "resume") { if(state != MACRO) error("resume found outside macro."); state = RESUME; m->resume.attributes = attributes; assert(m); // No macro is currently available, cannot create queries! return; } // Enable Query parsing if(name == "queries") { if(state != MACRO) error("queries found outside macro."); state = QUERIES; assert(m); // No macro is currently available, cannot create queries! return; } // Create Query if(name == "query") { if(state != QUERIES) error("query found outside queries."); state = QUERY; assert(m); // No macro is currently available, cannot create query! Query q; q.attributes = attributes; m->queries.push_back(q); return; } // Enable Map parsing if(name == "maps") { if(state != MACRO) error("maps found outside macro."); state = MAPS; assert(m); // No macro is currently available, cannot create maps! return; } // Create Query if(name == "map") { if(state != MAPS) error("map found outside maps."); state = MAP; assert(m); // No macro is currently available, cannot create map! Map map; map.attributes = attributes; m->maps.push_back(map); current_map = &(m->maps.back()); return; } // Enable script parsing if(name == "scripts") { if(state != MACRO) error("scripts found outside macro."); state = SCRIPTS; assert(m); // No macro is currently available, cannot create maps! return; } // Create Query if(name == "script") { if(state != SCRIPTS) error("script found outside scripts."); state = SCRIPT; assert(m); // No macro is currently available, cannot create map! Script s; s.attributes = attributes; m->scripts.push_back(s); current_script = &(m->scripts.back()); return; } // Enable widget parsing if(name == "widgets") { if(state != MACRO) error("widgets found outside macro."); state = WIDGETS; assert(m); // No macro is currently available, cannot create widgets! m->widgets.attributes = attributes; m->widgets.attributes["tagname"] = name; Widget *current = &(m->widgets); widgetstack.push_back(current); return; } // TODO: We need to parse some (maybe even all) widgets in order to // make db lookup of the previous values. if(state == WIDGETS) { assert(widgetstack.size()); // Widget stack is empty, cannot create! Widget w; w.attributes = attributes; w.attributes["tagname"] = name; Widget *parent = widgetstack.back(); parent->widgets.push_back(w); Widget *current = &(parent->widgets.back()); widgetstack.push_back(current); return; } // Handle include if(name == "include") { return; } error("Unknown/illegal tag: %s", name.c_str()); } void MacroParser::endTag(std::string name) { if(name == "macro") { state = UNDEFINED; } if(name == "resume") state = MACRO; if(name == "queries") state = MACRO; if(name == "query") state = QUERIES; if(name == "maps") state = MACRO; if(name == "map") { current_map = NULL; state = MAPS; } if(name == "scripts") state = MACRO; if(name == "script") { current_script = NULL; state = SCRIPTS; } if(name == "widgets") state = MACRO; if(state == WIDGETS) { assert(widgetstack.size()); // Widget stack is empty, cannot pop! widgetstack.pop_back(); if(widgetstack.size() == 0) state = MACRO; } } int MacroParser::readData(char *data, size_t size) { if(fd == -1) { PRACRO_ERR_LOG(macro, "Invalid file descriptor.\n"); return 0; } ssize_t r = read(fd, data, size); if(r == -1) { PRACRO_ERR_LOG(macro, "Could not read...%s\n", strerror(errno)); return 0; } return r; } void MacroParser::parseError(char *buf, size_t len, std::string error, int lineno) { PRACRO_ERR_LOG(macro, "MacroParser[%s] error at line %d: %s\n", file.c_str(), lineno, error.c_str()); PRACRO_ERR_LOG(macro, "\tBuffer %u bytes: [", len); if(fwrite(buf, len, 1, stderr) != len) {} PRACRO_ERR_LOG(macro, "]\n"); char *slineno; if(asprintf(&slineno, " at line %d\n", lineno) != -1) { throw Exception(error + slineno); free(slineno); } } Macro *MacroParser::getMacro() { return m; } #ifdef TEST_MACROPARSER #define XMLFILE "/tmp/test_macroheaderparser.xml" #include #include #include #include #include #include static char xml[] = "\n" "\n" " \n" " \n" " \n" " \n" " \n" "" ; static char xml_nonmacro[] = "\n" "\n" " \n" " \n" "" ; static char xml_fail[] = "\n" "\n" " \n" " \n" "" ; int main() { FILE *fp = fopen(XMLFILE, "w"); if(!fp) { printf("Could not write to %s\n", XMLFILE); return 1; } fprintf(fp, xml); fclose(fp); { // Test parsing of correct macro xml data. MacroParser parser(XMLFILE); try { parser.parse(); } catch(Exception &e) { printf("Failed to parse: %s\n", e.what()); return 1; } } fp = fopen(XMLFILE, "w"); if(!fp) { printf("Could not write to %s\n", XMLFILE); return 1; } fprintf(fp, xml_nonmacro); fclose(fp); // Test parsing of correct xml data, but not macro (should throw an exception). { MacroParser parser(XMLFILE); try { parser.parse(); } catch(Exception &e) { printf("Failed to parse: %s\n", e.what()); goto onandon; } return 1; } onandon: fp = fopen(XMLFILE, "w"); if(!fp) { printf("Could not write to %s\n", XMLFILE); return 1; } fprintf(fp, xml_fail); fclose(fp); // Test parsing of invalid xml data (should throw an exception). { MacroParser parser(XMLFILE); try { parser.parse(); } catch(Exception &e) { printf("Failed to parse: %s\n", e.what()); goto yetonandon; } return 1; } yetonandon: unlink(XMLFILE); return 0; } #endif/*TEST_MACROPARSER*/