Module: sems Branch: master Commit: a3292d8b683ba9514f64cef0cf8cdc85496e4dd6 URL: http://git.sip-router.org/cgi-bin/gitweb.cgi/sems/?a=commit;h=a3292d8b683ba9514f64cef0cf8cdc85496e4dd6
Author: Stefan Sayer <[email protected]> Committer: Stefan Sayer <[email protected]> Date: Wed Sep 8 17:57:20 2010 +0200 DSM: support for #include "script.dsm" --- apps/dsm/DSM.cpp | 9 +++--- apps/dsm/DSMStateDiagramCollection.cpp | 47 +++++++++++++++++++++++++------- apps/dsm/DSMStateDiagramCollection.h | 3 ++ doc/dsm/Readme.dsm.txt | 4 +++ doc/dsm/dsm_syntax.txt | 2 + 5 files changed, 51 insertions(+), 14 deletions(-) diff --git a/apps/dsm/DSM.cpp b/apps/dsm/DSM.cpp index 14d5ec5..9b7c91d 100644 --- a/apps/dsm/DSM.cpp +++ b/apps/dsm/DSM.cpp @@ -357,7 +357,7 @@ bool DSMFactory::loadDiags(AmConfigReader& cfg, DSMStateDiagramCollection* m_dia vector<string> diags_names = explode(LoadDiags, ","); for (vector<string>::iterator it= diags_names.begin(); it != diags_names.end(); it++) { - if (!m_diags->loadFile(DiagPath+*it+".dsm", *it, ModPath, DebugDSM, CheckDSM)) { + if (!m_diags->loadFile(DiagPath+*it+".dsm", *it, DiagPath, ModPath, DebugDSM, CheckDSM)) { ERROR("loading %s from %s\n", it->c_str(), (DiagPath+*it+".dsm").c_str()); return false; @@ -834,7 +834,8 @@ void DSMFactory::reloadDSMs(const AmArg& args, AmArg& ret) { vector<string> diags_names = explode(LoadDiags, ","); for (vector<string>::iterator it= diags_names.begin(); it != diags_names.end(); it++) { - if (!new_diags->loadFile(DiagPath+*it+".dsm", *it, ModPath, DebugDSM, CheckDSM)) { + if (!new_diags->loadFile(DiagPath+*it+".dsm", *it, DiagPath, ModPath, + DebugDSM, CheckDSM)) { ERROR("loading %s from %s\n", it->c_str(), (DiagPath+*it+".dsm").c_str()); ret.push(500); @@ -1040,7 +1041,7 @@ void DSMFactory::loadDSM(const AmArg& args, AmArg& ret) { ret.push(400); ret.push("DSM named '" + dsm_name + "' already loaded (use reloadDSMs to reload all)"); } else { - if (!MainScriptConfig.diags->loadFile(dsm_file_name, dsm_name, ModPath, DebugDSM, CheckDSM)) { + if (!MainScriptConfig.diags->loadFile(dsm_file_name, dsm_name, DiagPath, ModPath, DebugDSM, CheckDSM)) { ret.push(500); ret.push("error loading "+dsm_name+" from "+ dsm_file_name); } else { @@ -1067,7 +1068,7 @@ void DSMFactory::loadDSMWithPaths(const AmArg& args, AmArg& ret) { ret.push(400); ret.push("DSM named '" + dsm_name + "' already loaded (use reloadDSMs to reload all)"); } else { - if (!MainScriptConfig.diags->loadFile(diag_path+dsm_name+".dsm", dsm_name, mod_path, DebugDSM, CheckDSM)) { + if (!MainScriptConfig.diags->loadFile(diag_path+dsm_name+".dsm", dsm_name, diag_path, mod_path, DebugDSM, CheckDSM)) { ret.push(500); ret.push("error loading "+dsm_name+" from "+ diag_path+dsm_name+".dsm"); } else { diff --git a/apps/dsm/DSMStateDiagramCollection.cpp b/apps/dsm/DSMStateDiagramCollection.cpp index 416b89a..07f991d 100644 --- a/apps/dsm/DSMStateDiagramCollection.cpp +++ b/apps/dsm/DSMStateDiagramCollection.cpp @@ -36,12 +36,10 @@ DSMStateDiagramCollection::DSMStateDiagramCollection() { DSMStateDiagramCollection::~DSMStateDiagramCollection() { } -bool DSMStateDiagramCollection::loadFile(const string& filename, const string& name, - const string& mod_path, bool debug_dsm, bool check_dsm) { +bool DSMStateDiagramCollection::readFile(const string& filename, const string& name, + const string& load_path, string& s) { DBG("loading DSM '%s' from '%s'\n", name.c_str(), filename.c_str()); - DSMChartReader cr; - ifstream ifs(filename.c_str()); if (!ifs.good()) { ERROR("loading state diagram '%s'\n", @@ -49,23 +47,52 @@ bool DSMStateDiagramCollection::loadFile(const string& filename, const string& n return false; } - diags.push_back(DSMStateDiagram(name)); - string s; while (ifs.good() && !ifs.eof()) { string r; getline(ifs, r); // skip comments size_t fpos = r.find_first_not_of(" \t"); - if (fpos != string::npos && - r.length() > fpos+1 && - r.substr(fpos, 2) == "--") - continue; + if (fpos != string::npos) { + if (r.length() > fpos+1 && + r.substr(fpos, 2) == "--") + continue; + + if (r.length() > fpos+1 && + r.substr(fpos, 8) == "#include") { + r = r.substr(fpos+8); + r = trim(r, "'\" "); + if (r.empty()) { + ERROR("missing include file name!\n"); + return false; + } + + string include_name = r[0]=='/' ? r : load_path+"/"+r; + if (!readFile(include_name, name, load_path, s)) + return false; + continue; + } + + } s += r + "\n"; } + return true; +} + +bool DSMStateDiagramCollection::loadFile(const string& filename, const string& name, + const string& load_path, + const string& mod_path, bool debug_dsm, bool check_dsm) { + + string s; + if (!readFile(filename, name, load_path, s)) + return false; + if (debug_dsm) { DBG("dsm text\n------------------\n%s\n------------------\n", s.c_str()); } + + diags.push_back(DSMStateDiagram(name)); + DSMChartReader cr; if (!cr.decode(&diags.back(), s, mod_path, this, mods)) { ERROR("DonkeySM decode script error!\n"); return false; diff --git a/apps/dsm/DSMStateDiagramCollection.h b/apps/dsm/DSMStateDiagramCollection.h index a167ccb..90ac0b2 100644 --- a/apps/dsm/DSMStateDiagramCollection.h +++ b/apps/dsm/DSMStateDiagramCollection.h @@ -43,7 +43,10 @@ class DSMStateDiagramCollection DSMStateDiagramCollection(); ~DSMStateDiagramCollection(); + bool readFile(const string& filename, const string& name, + const string& load_path, string& s); bool loadFile(const string& filename, const string& name, + const string& load_path, const string& mod_path, bool debug_dsm, bool check_dsm); void addToEngine(DSMStateEngine* e); bool hasDiagram(const string& name); diff --git a/doc/dsm/Readme.dsm.txt b/doc/dsm/Readme.dsm.txt index 13d0816..4351007 100644 --- a/doc/dsm/Readme.dsm.txt +++ b/doc/dsm/Readme.dsm.txt @@ -53,6 +53,10 @@ A patch for fmsc 1.0.4 from the graphical FSM editor fsme (http://fsme.sf.net) is available, so DSMs can be defined in click-n-drag fashion and compiled to SEMS DSM diagrams. +DSM scripts can include other scripts by using the #include "script.dsm" +directive. That loads a script from the load path (where the current +script resides), unless an absolute path is given (e.g. +#include "/path/to/script). SystemDSMs ========== diff --git a/doc/dsm/dsm_syntax.txt b/doc/dsm/dsm_syntax.txt index e3ba30a..d32cfec 100644 --- a/doc/dsm/dsm_syntax.txt +++ b/doc/dsm/dsm_syntax.txt @@ -1,5 +1,7 @@ ============================= -- comment +#include "script.dsm" +#include "/path/to/anotherscript.dsm" import(mod_name); [initial] state name _______________________________________________ Semsdev mailing list [email protected] http://lists.iptel.org/mailman/listinfo/semsdev
