[2615] | 1 | #include "sopnamsp.h"
|
---|
[2446] | 2 | #include "commander.h"
|
---|
| 3 | #include <stdio.h>
|
---|
| 4 | #include <stdlib.h>
|
---|
[3619] | 5 | #include <string.h>
|
---|
[2518] | 6 | #include <unistd.h>
|
---|
[2446] | 7 | #include <ctype.h>
|
---|
| 8 | #include <math.h>
|
---|
[2943] | 9 | #include <signal.h>
|
---|
[2446] | 10 |
|
---|
| 11 | #include "strutil.h"
|
---|
| 12 | #include "strutilxx.h"
|
---|
[2512] | 13 | #include "cexpre.h"
|
---|
| 14 | #include "rpneval.h"
|
---|
[2446] | 15 | #include "srandgen.h"
|
---|
[2671] | 16 | #include "zthread.h"
|
---|
[2446] | 17 |
|
---|
| 18 |
|
---|
[2483] | 19 | namespace SOPHYA {
|
---|
[2518] | 20 |
|
---|
[3577] | 21 | //--------------------------------------------------
|
---|
| 22 | // Pour ecrire une valeur double sous forme de string
|
---|
| 23 | // sans perte de precision, et si possible de maniere
|
---|
[3581] | 24 | // lisible (%g, sinon format adapte jusqu'a %.17e)
|
---|
| 25 | static char cval_strbuff_[80];
|
---|
| 26 | ////1er essai binaire: char * cval_dble2str(double v)
|
---|
| 27 | ////1er essai binaire: {
|
---|
| 28 | ////1er essai binaire: char* str1 = cval_strbuff_;
|
---|
| 29 | ////1er essai binaire: sprintf(str1,"%g",v);
|
---|
| 30 | ////1er essai binaire: char* str2 = cval_strbuff_+32;
|
---|
| 31 | ////1er essai binaire: sprintf(str2,"%.17e",v);
|
---|
| 32 | ////1er essai binaire: double x1 = atof(str1);
|
---|
| 33 | ////1er essai binaire: double x2 = atof(str2);
|
---|
| 34 | ////1er essai binaire: double fx = fabs(v);
|
---|
| 35 | ////1er essai binaire: if (fx>0. && fabs(x2-x1)/fx>1e-12) return str2;
|
---|
| 36 | ////1er essai binaire: else return str1;
|
---|
| 37 | ////1er essai binaire: }
|
---|
[3577] | 38 | char * cval_dble2str(double v)
|
---|
| 39 | {
|
---|
[3581] | 40 | char* strgood = cval_strbuff_; sprintf(strgood,"%g",v);
|
---|
| 41 | char* strfull = cval_strbuff_+32; sprintf(strfull,"%.17e",v);
|
---|
| 42 | char* strform = cval_strbuff_+64;
|
---|
| 43 | double x1 = atof(strfull), fx = fabs(x1);
|
---|
| 44 | if(fx>0.) {
|
---|
| 45 | for(int i=7;i<=17;i+=2) { // increment de 2 pour aller + vite
|
---|
| 46 | double x2 = atof(strgood);
|
---|
| 47 | if(fabs(x2-x1)/fx<1.e-16) break;
|
---|
| 48 | sprintf(strform,"%%.%de",i);
|
---|
| 49 | sprintf(strgood,strform,v);
|
---|
| 50 | }
|
---|
| 51 | }
|
---|
| 52 | return strgood;
|
---|
[3577] | 53 | }
|
---|
| 54 | //--------------------------------------------------
|
---|
| 55 |
|
---|
[2518] | 56 | // Differents code de retour specifiques
|
---|
[4034] | 57 | #define CMD_RETURN_RC 99900
|
---|
| 58 | #define CMD_BREAK_RC 99990
|
---|
| 59 | #define CMD_LOOPBREAK_RC 99910
|
---|
| 60 | #define CMD_BREAKALL_RC 99998
|
---|
| 61 | #define CMD_BREAKEXE_RC 99999
|
---|
[2518] | 62 |
|
---|
[2446] | 63 | // ------------------------------------------------------------
|
---|
| 64 | // Bloc de commandes (Foreach, ...)
|
---|
| 65 | // Classe CommanderBloc
|
---|
| 66 | // ------------------------------------------------------------
|
---|
| 67 | /*!
|
---|
[2598] | 68 | \internal
|
---|
[2446] | 69 | \class SOPHYA::CommanderBloc
|
---|
| 70 | \ingroup SysTools
|
---|
[2598] | 71 | Class for internal use by class Commander to handle loops
|
---|
[2446] | 72 | */
|
---|
| 73 | class CommanderBloc {
|
---|
| 74 | public:
|
---|
[2483] | 75 | enum BType { BT_None, BT_ForeachList, BT_ForeachInt, BT_ForeachFloat,
|
---|
| 76 | BT_ForeachLineInFile };
|
---|
[2446] | 77 |
|
---|
| 78 | CommanderBloc(Commander* piac, CommanderBloc* par, string& kw, vector<string>& args);
|
---|
| 79 | ~CommanderBloc();
|
---|
| 80 | inline CommanderBloc* Parent() { return(parent); }
|
---|
| 81 | inline bool CheckOK() { return blkok; }
|
---|
| 82 | inline void AddLine(string& line)
|
---|
| 83 | { lines.push_back(line); bloclineid.push_back(lines.size()); }
|
---|
| 84 | void AddLine(string& line, string& kw);
|
---|
| 85 | inline void AddBloc(CommanderBloc* blk)
|
---|
| 86 | { blocs.push_back(blk); bloclineid.push_back(-blocs.size()); }
|
---|
[2518] | 87 |
|
---|
| 88 | // Execution complete du bloc (boucle)
|
---|
| 89 | int Execute();
|
---|
| 90 | // Execution pour un element de bloc
|
---|
| 91 | int ExecuteOnce(string& lvv);
|
---|
| 92 |
|
---|
[2446] | 93 | inline int& TestLevel() { return testlevel; }
|
---|
| 94 | inline int& LoopLevel() { return looplevel; }
|
---|
| 95 | inline bool CheckBloc()
|
---|
| 96 | { return ((testlevel == 0)&&(looplevel == 0)&&(!scrdef)); }
|
---|
| 97 |
|
---|
| 98 | protected:
|
---|
| 99 | Commander* _commander;
|
---|
| 100 | CommanderBloc* parent;
|
---|
| 101 | bool blkok; // true -> block OK
|
---|
| 102 | BType typ; // foreach , integer loop, float loop, test
|
---|
| 103 | string varname;
|
---|
[2483] | 104 | string filename; // forinfile bloc
|
---|
[2446] | 105 | vector<string> strlist;
|
---|
| 106 | vector<string> lines;
|
---|
| 107 | vector<CommanderBloc *> blocs;
|
---|
| 108 | vector<int> bloclineid;
|
---|
| 109 | int i1,i2,di;
|
---|
| 110 | float f1,f2,df;
|
---|
| 111 | int testlevel; // niveau d'imbrication des if
|
---|
| 112 | int looplevel; // niveau d'imbrication des for/foreach
|
---|
| 113 | bool scrdef; // true -> commande defscript ds for/foreach
|
---|
| 114 | };
|
---|
| 115 |
|
---|
| 116 | /* --Methode-- */
|
---|
| 117 | CommanderBloc::CommanderBloc(Commander* piac, CommanderBloc* par, string& kw, vector<string>& args)
|
---|
| 118 | {
|
---|
| 119 | _commander = piac;
|
---|
| 120 | parent = par;
|
---|
| 121 | blkok = false;
|
---|
| 122 | typ = BT_None;
|
---|
| 123 | i1 = 0; i2 = -1; di = 1;
|
---|
| 124 | f1 = 0.; f2 = -1.; df = 1.;
|
---|
| 125 | testlevel = looplevel = 0;
|
---|
| 126 | scrdef = false;
|
---|
| 127 |
|
---|
| 128 | if ((args.size() < 2) || !isalpha((int)args[0][0]) ) return;
|
---|
[2483] | 129 | if ((kw != "foreach") && (kw != "for") && (kw != "forinfile")) return;
|
---|
[2518] | 130 | if (!piac->CheckVarName(args[0])) return;
|
---|
| 131 | varname = args[0];
|
---|
| 132 |
|
---|
[2446] | 133 | //if (isalpha((int)args[1][0]) ) { This is a foreach bloc with string list
|
---|
[2483] | 134 | if (kw == "forinfile") {
|
---|
| 135 | filename = args[1];
|
---|
[2518] | 136 | typ = BT_ForeachLineInFile;
|
---|
[2483] | 137 | blkok = true;
|
---|
| 138 | }
|
---|
| 139 | else if (kw == "foreach" ) { // This is a foreach bloc with string list
|
---|
[2518] | 140 | if ( (args[1] == "(") && (args[args.size()-1] == ")") ) {
|
---|
| 141 | // foreach varname ( w1 w2 w3 ... )
|
---|
[3581] | 142 | for(unsigned int kk=2; kk<args.size()-1; kk++) strlist.push_back(args[kk]);
|
---|
[2518] | 143 | }
|
---|
| 144 | else {
|
---|
| 145 | // foreach varname WordVectorName
|
---|
| 146 | if (!piac->GetVar(args[1], strlist)) return;
|
---|
| 147 | }
|
---|
| 148 | if (strlist.size() < 1) return;
|
---|
[2446] | 149 | typ = BT_ForeachList;
|
---|
| 150 | blkok = true;
|
---|
[2483] | 151 | }
|
---|
[2446] | 152 | else { // This is an integer or float loop
|
---|
| 153 | size_t l = args[1].length();
|
---|
| 154 | size_t p = args[1].find(':');
|
---|
| 155 | size_t pp = args[1].find('.');
|
---|
| 156 | bool fl = (pp < l) ? true : false; // Float loop or integer loop
|
---|
| 157 | if (p >= l) return; // Syntaxe error
|
---|
| 158 | string a1 = args[1].substr(0, p);
|
---|
| 159 | string aa = args[1].substr(p+1);
|
---|
| 160 | p = aa.find(':');
|
---|
| 161 | string a2, a3;
|
---|
| 162 | bool hasa3 = false;
|
---|
| 163 | if (p < aa.length() ) {
|
---|
| 164 | a2 = aa.substr(0,p);
|
---|
| 165 | a3 = aa.substr(p+1);
|
---|
| 166 | hasa3 = true;
|
---|
| 167 | }
|
---|
| 168 | else a2 = aa;
|
---|
| 169 | if (fl) {
|
---|
| 170 | typ = BT_ForeachFloat;
|
---|
| 171 | blkok = true;
|
---|
| 172 | f1 = atof(a1.c_str());
|
---|
| 173 | f2 = atof(a2.c_str());
|
---|
| 174 | if (hasa3) df = atof(a3.c_str());
|
---|
| 175 | else df = 1.;
|
---|
| 176 | }
|
---|
| 177 | else {
|
---|
| 178 | typ = BT_ForeachInt;
|
---|
| 179 | blkok = true;
|
---|
| 180 | i1 = atoi(a1.c_str());
|
---|
| 181 | i2 = atoi(a2.c_str());
|
---|
| 182 | if (hasa3) di = atoi(a3.c_str());
|
---|
| 183 | else di = 1;
|
---|
| 184 | }
|
---|
| 185 | }
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 | /* --Methode-- */
|
---|
| 189 | CommanderBloc::~CommanderBloc()
|
---|
| 190 | {
|
---|
[3581] | 191 | for(unsigned int k=0; k<blocs.size(); k++) delete blocs[k];
|
---|
[2446] | 192 | }
|
---|
| 193 |
|
---|
| 194 | /* --Methode-- */
|
---|
| 195 | void CommanderBloc::AddLine(string& line, string& kw)
|
---|
| 196 | {
|
---|
| 197 | AddLine(line);
|
---|
| 198 | if (kw == "if") testlevel++;
|
---|
| 199 | else if (kw == "endif") testlevel--;
|
---|
[4034] | 200 | // bug trouve par JEC en Mai 2011 forinfile manquant ds la ligne suivante
|
---|
| 201 | else if ((kw == "for") || (kw == "foreach") || (kw == "forinfile")) looplevel++;
|
---|
[2446] | 202 | else if (kw == "end") looplevel--;
|
---|
| 203 | else if (kw == "defscript") scrdef = true;
|
---|
| 204 | }
|
---|
| 205 |
|
---|
| 206 | /* --Methode-- */
|
---|
[2518] | 207 | int CommanderBloc::Execute()
|
---|
[2446] | 208 | {
|
---|
| 209 | int k=0;
|
---|
| 210 | char buff[32];
|
---|
| 211 | int rcc = 0;
|
---|
| 212 |
|
---|
| 213 | int mxloop = _commander->GetMaxLoopLimit();
|
---|
| 214 |
|
---|
[2483] | 215 | if (typ == BT_ForeachLineInFile) { // foreach line in file loop
|
---|
[2518] | 216 | ifstream is(filename.c_str());
|
---|
| 217 | string line;
|
---|
| 218 | while (!is.eof()) {
|
---|
| 219 | rcc = 0;
|
---|
[2796] | 220 | line = "";
|
---|
| 221 | getline(is,line);
|
---|
[4034] | 222 | if (is.good()) { // bug signale par JEC en mai 2011 || is.eof()) {
|
---|
[2518] | 223 | rcc = ExecuteOnce(line);
|
---|
[4034] | 224 | if ((rcc == CMD_BREAKEXE_RC) || (rcc == CMD_BREAKALL_RC)) return rcc;
|
---|
[2796] | 225 | else if (rcc == CMD_BREAK_RC) break;
|
---|
[2518] | 226 | }
|
---|
| 227 | }
|
---|
[4034] | 228 | rcc=0;
|
---|
[2483] | 229 | }
|
---|
[2518] | 230 | else if (typ == BT_ForeachList) { // foreach string loop
|
---|
[3581] | 231 | for(unsigned int k=0; k<strlist.size(); k++) {
|
---|
[2518] | 232 | rcc = ExecuteOnce(strlist[k]);
|
---|
[4034] | 233 | if ((rcc == CMD_BREAKEXE_RC) || (rcc == CMD_BREAKALL_RC)) return rcc;
|
---|
[2518] | 234 | else if (rcc == CMD_BREAK_RC) break;
|
---|
[4034] | 235 | }
|
---|
| 236 | rcc = 0;
|
---|
[2518] | 237 | }
|
---|
| 238 | else if (typ == BT_ForeachInt) { // Integer loop
|
---|
[2446] | 239 | for(int i=i1; i<i2; i+=di) {
|
---|
| 240 | k++;
|
---|
| 241 | if ((mxloop>0) && (k > mxloop)) {
|
---|
| 242 | cout << ">>> Maximum CommanderBloc loop limit ("<< mxloop << ") -> break " << endl;
|
---|
| 243 | break;
|
---|
| 244 | }
|
---|
[2518] | 245 | sprintf(buff, "%d", i);
|
---|
| 246 | string lvv = buff;
|
---|
| 247 | rcc = ExecuteOnce(lvv);
|
---|
[4034] | 248 | if ((rcc == CMD_BREAKEXE_RC) || (rcc == CMD_BREAKALL_RC)) return rcc;
|
---|
[2518] | 249 | else if (rcc == CMD_BREAK_RC) break;
|
---|
[2446] | 250 | }
|
---|
[4034] | 251 | rcc = 0;
|
---|
[2518] | 252 | }
|
---|
| 253 | else if (typ == BT_ForeachFloat) { // float loop
|
---|
| 254 | for(double f=f1; f<f2; f+=df) {
|
---|
[2446] | 255 | k++;
|
---|
| 256 | if ((mxloop>0) && (k > mxloop)) {
|
---|
| 257 | cout << ">>> Maximum CommanderBloc loop limit ("<< mxloop << ") -> break " << endl;
|
---|
| 258 | break;
|
---|
| 259 | }
|
---|
[3577] | 260 | string lvv = cval_dble2str(f);
|
---|
[2518] | 261 | rcc = ExecuteOnce(lvv);
|
---|
[4034] | 262 | if ((rcc == CMD_BREAKEXE_RC) || (rcc == CMD_BREAKALL_RC)) return rcc;
|
---|
[2518] | 263 | else if (rcc == CMD_BREAK_RC) break;
|
---|
[2446] | 264 | }
|
---|
[4034] | 265 | rcc = 0;
|
---|
[2518] | 266 | }
|
---|
| 267 | return(rcc);
|
---|
| 268 | }
|
---|
[2446] | 269 |
|
---|
[2518] | 270 | /* --Methode-- */
|
---|
| 271 | int CommanderBloc::ExecuteOnce(string& lvv)
|
---|
| 272 | {
|
---|
[3581] | 273 | unsigned int kj=0;
|
---|
[2518] | 274 | int kk=0;
|
---|
| 275 | int rcc = 0;
|
---|
| 276 | _commander->SetVar(varname, lvv);
|
---|
| 277 | for(kj=0; kj<bloclineid.size(); kj++) {
|
---|
| 278 | rcc = 0;
|
---|
| 279 | kk = bloclineid[kj];
|
---|
| 280 | if (kk > 0)
|
---|
| 281 | rcc = _commander->Interpret(lines[kk-1]);
|
---|
| 282 | else
|
---|
| 283 | rcc = blocs[-kk-1]->Execute();
|
---|
[4034] | 284 | if ((rcc == CMD_BREAKEXE_RC) || (rcc == CMD_BREAKALL_RC)) return rcc;
|
---|
[2518] | 285 | if (rcc == CMD_BREAK_RC) break;
|
---|
| 286 | }
|
---|
| 287 | return rcc;
|
---|
[2446] | 288 | }
|
---|
| 289 |
|
---|
| 290 | // ---------------------------------------------------------------
|
---|
| 291 | // Classe CommanderScript
|
---|
| 292 | // Definition et execution d'un script de Commander
|
---|
| 293 | // script : Une liste de commande Commander - Lors de l'execution,
|
---|
| 294 | // les variables-argument $# $0 $1 sont definies.
|
---|
| 295 | // ---------------------------------------------------------------
|
---|
| 296 |
|
---|
| 297 | /*!
|
---|
[2598] | 298 | \internal
|
---|
[2446] | 299 | \class SOPHYA::CommanderScript
|
---|
| 300 | \ingroup SysTools
|
---|
[2598] | 301 | Class for internal use by class Commander to handle functions
|
---|
[2446] | 302 | or scripts
|
---|
| 303 | */
|
---|
| 304 |
|
---|
| 305 | class CommanderScript {
|
---|
| 306 | public:
|
---|
| 307 | CommanderScript(Commander* piac, string const& name, string const& comm);
|
---|
| 308 | virtual ~CommanderScript();
|
---|
| 309 |
|
---|
| 310 | void AddLine(string& line, string& kw);
|
---|
| 311 | virtual int Execute(vector<string>& args);
|
---|
| 312 |
|
---|
| 313 | inline string& Name() { return mName; }
|
---|
| 314 | inline string& Comment() { return mComm; }
|
---|
| 315 | inline int& TestLevel() { return testlevel; }
|
---|
| 316 | inline int& LoopLevel() { return looplevel; }
|
---|
| 317 | inline bool CheckScript()
|
---|
| 318 | { return ((testlevel == 0)&&(looplevel == 0)&&(!scrdef)&&fgok); }
|
---|
| 319 |
|
---|
| 320 | protected:
|
---|
| 321 | Commander* _commander;
|
---|
| 322 | string mName;
|
---|
| 323 | string mComm;
|
---|
| 324 | vector<string> lines;
|
---|
| 325 | int testlevel; // niveau d'imbrication des if
|
---|
| 326 | int looplevel; // niveau d'imbrication des for/foreach
|
---|
| 327 | bool scrdef; // true -> commande defscript ds for/foreach
|
---|
| 328 | bool fgok; // Script name OK
|
---|
| 329 |
|
---|
| 330 | };
|
---|
| 331 |
|
---|
| 332 | /* --Methode-- */
|
---|
| 333 | CommanderScript::CommanderScript(Commander* piac, string const& name,
|
---|
| 334 | string const& comm)
|
---|
| 335 | {
|
---|
| 336 | _commander = piac;
|
---|
| 337 | testlevel = looplevel = 0;
|
---|
| 338 | scrdef = false;
|
---|
| 339 | mName = name;
|
---|
| 340 | if (!isalpha(name[0])) fgok = false;
|
---|
| 341 | else fgok = true;
|
---|
| 342 | mComm = comm;
|
---|
| 343 | }
|
---|
| 344 |
|
---|
| 345 | /* --Methode-- */
|
---|
| 346 | CommanderScript::~CommanderScript()
|
---|
| 347 | {
|
---|
| 348 | }
|
---|
| 349 |
|
---|
| 350 | /* --Methode-- */
|
---|
| 351 | void CommanderScript::AddLine(string& line, string& kw)
|
---|
| 352 | {
|
---|
| 353 | if (kw == "if") testlevel++;
|
---|
| 354 | else if (kw == "endif") testlevel--;
|
---|
[4034] | 355 | // bug trouve par JEC en Mai 2011 forinfile manquant ds la ligne suivante
|
---|
| 356 | else if ((kw == "for") || (kw == "foreach") || (kw == "forinfile")) looplevel++;
|
---|
[2446] | 357 | else if (kw == "end") looplevel--;
|
---|
| 358 | else if (kw == "defscript") scrdef = true;
|
---|
| 359 | lines.push_back(line);
|
---|
| 360 | }
|
---|
| 361 |
|
---|
| 362 | /* --Methode-- */
|
---|
| 363 | int CommanderScript::Execute(vector<string>& args)
|
---|
| 364 | {
|
---|
[3581] | 365 | int rcc=-2;
|
---|
[2446] | 366 | if (!CheckScript()) return(-1);
|
---|
| 367 | cout << " CommanderScript::Execute() - Executing script " << Name() << endl;
|
---|
[3581] | 368 | for(unsigned int k=0; k<lines.size(); k++) {
|
---|
[2518] | 369 | rcc = _commander->Interpret(lines[k]);
|
---|
| 370 | if ( (rcc == CMD_BREAKEXE_RC) || (rcc == CMD_RETURN_RC) ) break;
|
---|
[2446] | 371 | }
|
---|
[2518] | 372 | return(rcc);
|
---|
[2446] | 373 | }
|
---|
| 374 |
|
---|
[2671] | 375 |
|
---|
[2446] | 376 | // ------------------------------------------------------------
|
---|
[2671] | 377 | // Classe CommandExeThr
|
---|
| 378 | // ------------------------------------------------------------
|
---|
| 379 | /*!
|
---|
| 380 | \internal
|
---|
| 381 | \class SOPHYA::CommandExeThr
|
---|
| 382 | \ingroup SysTools
|
---|
| 383 | Class for internal use by class Commander for command execution in separate threads.
|
---|
[2943] | 384 | The command execution is carried in method run() which has its own exception handling bloc.
|
---|
[2671] | 385 | */
|
---|
| 386 | class CommandExeThr : public ZThread {
|
---|
| 387 | public:
|
---|
| 388 | CommandExeThr(uint_8 id, CmdExecutor * cmdex, string& keyw,
|
---|
| 389 | vector<string>& args, string& toks);
|
---|
| 390 | virtual void run();
|
---|
| 391 | inline uint_8 Id() { return _id; }
|
---|
| 392 | inline bool IfDone() { return _fgdone; }
|
---|
| 393 | inline string& Tokens() { return _toks; }
|
---|
| 394 | inline string& Keyword() { return _keyw; }
|
---|
| 395 | protected:
|
---|
| 396 | uint_8 _id;
|
---|
| 397 | CmdExecutor * _cmdex;
|
---|
| 398 | string _keyw, _toks;
|
---|
| 399 | vector<string> _args;
|
---|
| 400 | bool _fgdone;
|
---|
| 401 | };
|
---|
| 402 |
|
---|
| 403 | /* --Methode-- */
|
---|
| 404 | CommandExeThr::CommandExeThr(uint_8 id, CmdExecutor * cmdex, string& keyw,
|
---|
| 405 | vector<string>& args, string& toks)
|
---|
| 406 | {
|
---|
| 407 | _id = id;
|
---|
| 408 | _cmdex = cmdex;
|
---|
| 409 | _keyw = keyw;
|
---|
[2956] | 410 | _args = args;
|
---|
| 411 | _args.erase(_args.end()-1);
|
---|
[2671] | 412 | _toks = toks;
|
---|
| 413 | for(size_t k=_toks.size()-1; k>0; k--)
|
---|
| 414 | if (_toks[k] == '&') { _toks[k] = ' '; break; }
|
---|
| 415 | _fgdone = false;
|
---|
| 416 | }
|
---|
| 417 |
|
---|
| 418 | /* --Methode-- */
|
---|
| 419 | void CommandExeThr::run()
|
---|
| 420 | {
|
---|
[2943] | 421 | int rc = 0;
|
---|
| 422 | try {
|
---|
| 423 | rc = _cmdex->Execute(_keyw, _args, _toks);
|
---|
| 424 | }
|
---|
| 425 | catch (PThrowable& exc) {
|
---|
| 426 | cout << "CommandExeThr::run() Catched Exception Msg()= "
|
---|
| 427 | << exc.Msg() << endl;
|
---|
| 428 | rc = -77;
|
---|
| 429 | }
|
---|
| 430 | catch (std::exception& sex) {
|
---|
| 431 | cout << "CommandExeThr::run() Catched std::exception what()= "
|
---|
| 432 | << sex.what() << endl;
|
---|
| 433 | rc = -78;
|
---|
| 434 | }
|
---|
| 435 | catch (...) {
|
---|
| 436 | cout << "CommandExeThr::run() Catched unknown (...) exception " << endl;
|
---|
| 437 | rc = -79;
|
---|
| 438 | }
|
---|
[2671] | 439 | _fgdone = true;
|
---|
| 440 | setRC(rc);
|
---|
| 441 | }
|
---|
| 442 |
|
---|
| 443 | // ------------------------------------------------------------
|
---|
[4063] | 444 | // Classe Cmd_CE_VarList
|
---|
| 445 | // ------------------------------------------------------------
|
---|
| 446 | /*!
|
---|
| 447 | \internal
|
---|
| 448 | \class SOPHYA::Cmd_CE_VarList
|
---|
| 449 | \ingroup SysTools
|
---|
| 450 | This class, reserved for internal use by class Commander, enables the CExpressionEvaluator class
|
---|
| 451 | to access the interpreter variable values.
|
---|
| 452 | */
|
---|
| 453 |
|
---|
| 454 | class Cmd_CE_VarList : public CE_VarListInterface {
|
---|
| 455 | public:
|
---|
| 456 | Cmd_CE_VarList(Commander& cmd) : cmd_(cmd) { }
|
---|
| 457 | virtual void Update();
|
---|
| 458 | virtual double* GetVarPointer(std::string const& name);
|
---|
| 459 | private:
|
---|
| 460 | Commander& cmd_;
|
---|
| 461 | std::map<std::string, double> namevals_;
|
---|
| 462 | };
|
---|
| 463 |
|
---|
| 464 | /* --Methode-- */
|
---|
| 465 | void Cmd_CE_VarList::Update()
|
---|
| 466 | {
|
---|
| 467 | bool exist=false;
|
---|
| 468 | string sval;
|
---|
| 469 | std::map<std::string, double>::iterator it;
|
---|
| 470 | for(it=namevals_.begin(); it!=namevals_.end(); it++) {
|
---|
| 471 | exist=cmd_.GetVar((*it).first, sval);
|
---|
| 472 | if (!exist) throw NotFoundExc("Cmd_CE_VarList::Update() missing variable !");
|
---|
| 473 | (*it).second=atof(sval.c_str());
|
---|
| 474 | }
|
---|
| 475 | return;
|
---|
| 476 | }
|
---|
| 477 |
|
---|
| 478 | /* --Methode-- */
|
---|
| 479 | double* Cmd_CE_VarList::GetVarPointer(std::string const& name)
|
---|
| 480 | {
|
---|
| 481 | std::map<std::string, double>::iterator it=namevals_.find(name);
|
---|
| 482 | if (it!=namevals_.end()) return &((*it).second);
|
---|
| 483 | bool exist=false;
|
---|
| 484 | string sval;
|
---|
| 485 | exist=cmd_.GetVar(name, sval);
|
---|
| 486 | if (!exist) return NULL;
|
---|
| 487 | namevals_[name]=atof(sval.c_str());
|
---|
| 488 | it=namevals_.find(name);
|
---|
| 489 | if (it!=namevals_.end()) return &((*it).second);
|
---|
| 490 | return NULL;
|
---|
| 491 | }
|
---|
| 492 |
|
---|
| 493 |
|
---|
| 494 | // ------------------------------------------------------------
|
---|
[2446] | 495 | // Classe Commander
|
---|
| 496 | // ------------------------------------------------------------
|
---|
| 497 | typedef void (* DlModuleInitEndFunction) ();
|
---|
| 498 |
|
---|
| 499 | /*!
|
---|
[2598] | 500 | \class Commander
|
---|
[2446] | 501 | \ingroup SysTools
|
---|
[2598] | 502 | \brief Simple command interpreter
|
---|
| 503 |
|
---|
| 504 | This Simple command interpreter with c-shell like syntax
|
---|
| 505 | can be used to add scripting capabilities
|
---|
| 506 | to applications.
|
---|
| 507 |
|
---|
[2671] | 508 | Although the interpreter has many limitations compared to
|
---|
[2598] | 509 | c-shell, or Tcl , it provides some interesting possibilities:
|
---|
| 510 |
|
---|
| 511 | - Extended arithmetic operations (c-like and RPN)
|
---|
| 512 | - Simple and vector variables
|
---|
| 513 | - Script definition
|
---|
[2943] | 514 | - Command execution in separate threads
|
---|
[2598] | 515 | - Dynamic Load
|
---|
| 516 |
|
---|
| 517 | \sa CmdExecutor CExpressionEvaluator RPNExpressionEvaluator
|
---|
| 518 |
|
---|
| 519 | Usage example:
|
---|
| 520 | \code
|
---|
| 521 | #include "commander.h"
|
---|
| 522 | ...
|
---|
| 523 | Commander cmd;
|
---|
| 524 | char* ss[3] = {"foreach f ( AA bbb CCCC ddddd )", "echo $f" , "end"};
|
---|
| 525 | for(int k=0; k<3; k++) {
|
---|
| 526 | string line = ss[k];
|
---|
| 527 | cmd.Interpret(line);
|
---|
| 528 | }
|
---|
| 529 | \endcode
|
---|
[2446] | 530 | */
|
---|
| 531 |
|
---|
| 532 | #define _MAGENTA_ 1
|
---|
| 533 |
|
---|
| 534 | static Commander* cur_commander = NULL;
|
---|
| 535 | /* --Methode-- */
|
---|
[2955] | 536 | /*!
|
---|
| 537 | \brief Constructor. Initializes variable list and copies \c history.pic to \c hisold.pic
|
---|
| 538 | if \b fgsigstop == true , calls ZThread::ActivateExitOnSignal()
|
---|
| 539 | */
|
---|
| 540 | Commander::Commander(bool fgsigzt)
|
---|
[2446] | 541 | {
|
---|
| 542 | system("cp history.pic hisold.pic");
|
---|
| 543 | hist.open("history.pic");
|
---|
| 544 | histon = true;
|
---|
[4063] | 545 | trace = false; timing = false; varcexp = true;
|
---|
[2446] | 546 | gltimer = NULL;
|
---|
[4034] | 547 | felevel_ = 0;
|
---|
[3814] | 548 | SetMaxLoopLimit();
|
---|
[2446] | 549 | mulinecmd = "";
|
---|
| 550 | mulinefg = false;
|
---|
| 551 | spromptmul = "Cmd> ";
|
---|
| 552 | SetCurrentPrompt(spromptmul);
|
---|
[2483] | 553 | SetDefaultPrompt(spromptmul);
|
---|
[2446] | 554 | curscript = NULL;
|
---|
| 555 |
|
---|
[2483] | 556 | _xstatus = 0;
|
---|
| 557 | _retstr = "";
|
---|
| 558 |
|
---|
[2518] | 559 | // Controle du flot d'execution
|
---|
| 560 | fgexebrk = false;
|
---|
| 561 |
|
---|
[4034] | 562 | curtestresult_ = true;
|
---|
| 563 | PushStack();
|
---|
[2446] | 564 |
|
---|
[2671] | 565 | // Pour la numerotation et l'identification des threads
|
---|
| 566 | ThrId = 0;
|
---|
| 567 |
|
---|
[2473] | 568 | // Numero de help-groupe courant - Le premier groupe ajoute aura un gid = 1
|
---|
| 569 | // gid = 0 n'existe pas : c'est le groupe de toutes les commandes
|
---|
| 570 | cmdgrpid = 0;
|
---|
[2446] | 571 |
|
---|
[2466] | 572 | string grp = "Commander";
|
---|
| 573 | string gdesc = "Basic (generic) interpreter (class SOPHYA::Commander) builtin commands";
|
---|
| 574 | AddHelpGroup(grp, gdesc);
|
---|
| 575 |
|
---|
[2446] | 576 | string kw = "Commander";
|
---|
| 577 | string usage;
|
---|
| 578 | usage = ">>> (Commander) Interpreter's keywords : \n";
|
---|
[2671] | 579 | usage += " > set varname string # To set a variable, $varname \n";
|
---|
[2446] | 580 | usage += " > unset varname # clear variable definition \n";
|
---|
[2518] | 581 | usage += " > rpneval varname RPNExpression # Reverse Polish Notation evaluation \n";
|
---|
| 582 | usage += " > varname = ArithmeticExpression # C-like Expression evaluation \n";
|
---|
| 583 | usage += " > varname = 'String' # Set variable vname \n";
|
---|
| 584 | usage += " > var2words varname wordvarname [sep] # to break varname into words \n";
|
---|
| 585 | usage += " > echo string # output string \n";
|
---|
| 586 | usage += " > echo2file filename string # Append the string to the specified file \n";
|
---|
| 587 | usage += " > alias name string # define a command alias \n";
|
---|
| 588 | usage += " > foreach varname ( string-list ) # Loop \n";
|
---|
| 589 | usage += " > for varname i1:i2[:di] # Integer loop \n";
|
---|
[2446] | 590 | usage += " > for varname f1:f2[:df] # Float loop \n";
|
---|
[2518] | 591 | usage += " > forinfile varname FileName # Loop over lines in file \n";
|
---|
[2446] | 592 | usage += " > end # end loops \n";
|
---|
| 593 | usage += " > if ( test ) then # Conditional test : a == != < > <= >= b \n";
|
---|
| 594 | usage += " > else # Conditional \n";
|
---|
| 595 | usage += " > endif # End of conditional if bloc \n";
|
---|
[4034] | 596 | usage += " > break # exit from the current loop blocs \n";
|
---|
| 597 | usage += " > breakall # exit all loop blocs \n";
|
---|
[2446] | 598 | usage += " > return # Stops command execution from a file \n";
|
---|
| 599 | usage += " > defscript endscript # Command script definition \n";
|
---|
| 600 | usage += " > listvars # List of variable names and values \n";
|
---|
| 601 | usage += " > listalias # List of alias names and values \n";
|
---|
| 602 | usage += " > listcommands # List of all known commands \n";
|
---|
| 603 | usage += " > listscripts # List of all known scripts \n";
|
---|
| 604 | usage += " > clearcript # Clear a script definition \n";
|
---|
[2671] | 605 | usage += " > thrlist # List of command execution threads (& as the last character) \n";
|
---|
| 606 | usage += " > clearthrlist # Removes finished threads from the list \n";
|
---|
[2955] | 607 | usage += " > killthr Id # Try to stop a given thread (ThrId=id) by sending SIGUSR1 \n";
|
---|
| 608 | usage += " > cancelthr Id # Try to cancel a given thread (ThrId=id) \n";
|
---|
[2671] | 609 | usage += " > waitthr # Waits until all active threads have finished (join()) \n";
|
---|
[2446] | 610 | usage += " > exec filename # Execute commands from file \n";
|
---|
[2518] | 611 | usage += " > help <command_name> # <command_name> usage info \n";
|
---|
| 612 | usage += " > sleep nsec # sleep nsec seconds \n";
|
---|
| 613 | usage += " > readstdin varname # reads a line from stdin into $varname \n";
|
---|
[4063] | 614 | usage += " > timingon timingoff traceon traceoff varcexpon varcexpoff \n";
|
---|
[2446] | 615 | RegisterHelp(kw, usage, grp);
|
---|
| 616 |
|
---|
| 617 | kw = "RPNEvaluator";
|
---|
| 618 | usage = " Reverse Polish Notation (HP calculator like) expression evaluation \n";
|
---|
| 619 | usage += " >> Stack: \n";
|
---|
| 620 | usage += " ... (4) (3) z=(2) y=(1) x=(0)=Stack.Top() \n";
|
---|
| 621 | usage += " >> Examples: \n";
|
---|
| 622 | usage += " - sin(PI/6): pi 6 / sin \n";
|
---|
| 623 | usage += " - 1*2*...*5: 1 2 3 4 5 product \n";
|
---|
| 624 | usage += " - x=x+y: x = $x $y * \n";
|
---|
| 625 | usage += " >>> Stack operations : \n";
|
---|
| 626 | usage += " print x<>y pop push (duplicate x) \n";
|
---|
| 627 | usage += " >>> Constants (Cst pushed to stack): \n";
|
---|
| 628 | usage += " pi e \n";
|
---|
| 629 | usage += " >>> Arithmetic operators (x,y) --> x@y \n";
|
---|
| 630 | usage += " + - * / % ( (int)y % (int)x )\n";
|
---|
| 631 | usage += " >>> F(X): x --> F(x) \n";
|
---|
| 632 | usage += " chs sqrt sq log log10 exp \n";
|
---|
| 633 | usage += " fabs floor ceil \n";
|
---|
| 634 | usage += " cos sin tan acos asin atan deg2rad rad2deg \n";
|
---|
| 635 | usage += " >>> F(X,Y): (x,y) --> F(x,y) \n";
|
---|
| 636 | usage += " pow atan2 \n";
|
---|
| 637 | usage += " >>> F(): random number generators \n";
|
---|
| 638 | usage += " rand (flat 0..1) norand (normal/gaussian) \n";
|
---|
| 639 | usage += " >>> Stack sum/product/mean/sigma/sigma^2 \n";
|
---|
| 640 | usage += " sum product mean sigma sigma2 sigmean (y->sigma x->mean) \n";
|
---|
| 641 | RegisterHelp(kw, usage, grp);
|
---|
| 642 |
|
---|
| 643 | kw = "autoiniranf";
|
---|
| 644 | usage = "> Automatic random number generator initialisation\n";
|
---|
[3615] | 645 | usage += " by AutoInitRand(int lp) \n";
|
---|
[2446] | 646 | usage += " Usage: autoiniranf";
|
---|
| 647 | RegisterCommand(kw, usage, NULL, grp);
|
---|
| 648 |
|
---|
[2671] | 649 | kw = "CExpEvaluator";
|
---|
| 650 | usage = "> Evaluation of C-like expression (used in V = C-like-Expression) \n";
|
---|
| 651 | usage += " >>> Arithmetic operators, parenthesis ( + - * / ) \n";
|
---|
| 652 | usage += " >>> Functions : sqrt fabs floor hypot \n";
|
---|
| 653 | usage += " ... exp log log10 pow ; sinh cosh tanh \n";
|
---|
| 654 | usage += " ... sin cos tan asin acos atan atan2 \n";
|
---|
| 655 | usage += " ... rand01() randpm1() gaurand() \n";
|
---|
| 656 | usage += " >>> Constants : Pi = M_PI E = M_E \n";
|
---|
| 657 | usage += " Example: x = 5.*(2.+sin(0.3*Pi))";
|
---|
| 658 | RegisterCommand(kw, usage, NULL, grp);
|
---|
| 659 |
|
---|
[2446] | 660 | kw = "shell execute";
|
---|
| 661 | usage = "> shell command_string # Execute shell command\n";
|
---|
| 662 | usage += "> cshell command_string # Execute cshell command\n";
|
---|
| 663 | usage += "---Examples:\n";
|
---|
| 664 | usage += " > shell ls\n";
|
---|
| 665 | usage += " > cshell echo '$LD_LIBRARY_PATH'; map2cl -h; ls\n";
|
---|
| 666 | usage += " > shell myfile.csh [arg1] [arg2] [...]\n";
|
---|
| 667 | usage += " (where the first line of \"myfile.csh\" is \"#!/bin/csh\")\n";
|
---|
| 668 | RegisterCommand(kw, usage, NULL, grp);
|
---|
| 669 |
|
---|
| 670 |
|
---|
| 671 | AddInterpreter(this);
|
---|
| 672 | curcmdi = this;
|
---|
[2955] | 673 | if (fgsigzt) ZThread::ActivateExitOnSignal(SIGUSR1);
|
---|
[2446] | 674 | }
|
---|
| 675 |
|
---|
| 676 | /* --Methode-- */
|
---|
| 677 | Commander::~Commander()
|
---|
| 678 | {
|
---|
| 679 | hist.close();
|
---|
| 680 | if (gltimer) { delete gltimer; gltimer = NULL; }
|
---|
| 681 | Modmap::iterator it;
|
---|
| 682 | for(it = modmap.begin(); it != modmap.end(); it++) {
|
---|
| 683 | string name = (*it).first + "_end";
|
---|
| 684 | DlModuleInitEndFunction fend = (*it).second->GetFunction(name);
|
---|
| 685 | if (fend) fend();
|
---|
| 686 | delete (*it).second;
|
---|
| 687 | }
|
---|
| 688 |
|
---|
| 689 | for(ScriptList::iterator sit = mScripts.begin();
|
---|
| 690 | sit != mScripts.end(); sit++) delete (*sit).second;
|
---|
| 691 |
|
---|
| 692 | if (cur_commander == this) cur_commander = NULL;
|
---|
| 693 | }
|
---|
| 694 |
|
---|
| 695 | /* --Methode-- */
|
---|
| 696 | Commander* Commander::GetInterpreter()
|
---|
| 697 | {
|
---|
| 698 | return(cur_commander);
|
---|
| 699 | }
|
---|
| 700 |
|
---|
| 701 | /* --Methode-- */
|
---|
[2598] | 702 | //! Returns the string \c Commander as the interpreter's name.
|
---|
[2446] | 703 | string Commander::Name()
|
---|
| 704 | {
|
---|
| 705 | return("Commander");
|
---|
| 706 | }
|
---|
| 707 |
|
---|
| 708 | /* --Methode-- */
|
---|
[2598] | 709 | //! Add the \b grp help group with description \b desc.
|
---|
[2466] | 710 | void Commander::AddHelpGroup(string& grp, string& desc)
|
---|
[2446] | 711 | {
|
---|
[2466] | 712 | int gid;
|
---|
| 713 | CheckHelpGrp(grp, gid, desc);
|
---|
| 714 | }
|
---|
| 715 |
|
---|
| 716 | /* --Methode-- */
|
---|
[2598] | 717 | /*!
|
---|
| 718 | \brief Register a command executor associated with a given keyword.
|
---|
| 719 | \param keyw : keyword identifying the command
|
---|
| 720 | \param usage : the command help and usage information
|
---|
| 721 | \param ce : CmdExecutor pointer for this a command. The same object can be registered
|
---|
| 722 | multiple time for different commands.
|
---|
| 723 | \param grp : The help group corresponding to this command.
|
---|
| 724 | */
|
---|
[2466] | 725 | void Commander::RegisterCommand(string& keyw, string& usage, CmdExecutor * ce, string& grp)
|
---|
| 726 | {
|
---|
[2446] | 727 | if (!ce) {
|
---|
| 728 | RegisterHelp(keyw, usage, grp);
|
---|
| 729 | return;
|
---|
| 730 | }
|
---|
[2466] | 731 | int gid;
|
---|
| 732 | CheckHelpGrp(grp,gid);
|
---|
[2446] | 733 | cmdex cme;
|
---|
| 734 | cme.group = gid;
|
---|
| 735 | cme.us = usage;
|
---|
| 736 | cme.cex = ce;
|
---|
| 737 | cmdexmap[keyw] = cme;
|
---|
| 738 | }
|
---|
| 739 |
|
---|
| 740 | /* --Methode-- */
|
---|
[2598] | 741 | /*!
|
---|
| 742 | \brief Register a help text.
|
---|
| 743 | \param keyw : help keyword
|
---|
| 744 | \param usage : help text
|
---|
| 745 | \param grp : help group
|
---|
| 746 | */
|
---|
[2446] | 747 | void Commander::RegisterHelp(string& keyw, string& usage, string& grp)
|
---|
| 748 | {
|
---|
[2466] | 749 | int gid;
|
---|
| 750 | CheckHelpGrp(grp,gid);
|
---|
[2446] | 751 | cmdex cme;
|
---|
| 752 | cme.group = gid;
|
---|
| 753 | cme.us = usage;
|
---|
| 754 | cme.cex = NULL;
|
---|
| 755 | helpexmap[keyw] = cme;
|
---|
| 756 | }
|
---|
| 757 |
|
---|
| 758 | /* --Methode-- */
|
---|
[2466] | 759 | bool Commander::CheckHelpGrp(string& grp, int& gid, string& desc)
|
---|
[2446] | 760 | {
|
---|
[2466] | 761 | gid = 0;
|
---|
[2446] | 762 | CmdHGroup::iterator it = cmdhgrp.find(grp);
|
---|
| 763 | if (it == cmdhgrp.end()) {
|
---|
[2466] | 764 | cmdgrpid++; gid = cmdgrpid;
|
---|
| 765 | hgrpst hgs; hgs.gid = gid; hgs.desc = desc;
|
---|
| 766 | cmdhgrp[grp] = hgs;
|
---|
| 767 | return true;
|
---|
[2446] | 768 | }
|
---|
[2466] | 769 | else {
|
---|
| 770 | if (desc.length() > 0) (*it).second.desc = desc;
|
---|
| 771 | gid = (*it).second.gid;
|
---|
| 772 | return false;
|
---|
[2446] | 773 | }
|
---|
[2466] | 774 | }
|
---|
[2446] | 775 |
|
---|
| 776 |
|
---|
| 777 | /* --Methode-- */
|
---|
[2598] | 778 | /*!
|
---|
| 779 | \brief Dynamic loader for modules
|
---|
| 780 |
|
---|
| 781 | A module is a shared library extending the application functionalities.
|
---|
| 782 | Typically, a module adds new commands to the interpreter. Once loaded,
|
---|
| 783 | the module is activated (initialized) by calling a function with the
|
---|
| 784 | name \b modulename_init . This function should be declared extern C
|
---|
| 785 | to avoid C++ name mangling. A cleanup function \b modulename_end
|
---|
| 786 | is called by the Commander destructor.
|
---|
| 787 |
|
---|
| 788 | \param fnameso : Shared library name containing the module functions
|
---|
| 789 | and classes.
|
---|
| 790 | \param name : Module name. This string is used to form module
|
---|
| 791 | initializer and cleanup function name \c name_init \c name_end
|
---|
| 792 | */
|
---|
[2446] | 793 | void Commander::LoadModule(string& fnameso, string& name)
|
---|
| 794 | {
|
---|
| 795 | PDynLinkMgr * dynlink = new PDynLinkMgr(fnameso, false);
|
---|
| 796 | if (dynlink == NULL) {
|
---|
| 797 | cerr << "Commander/LoadModule_Error: Pb opening SO " << fnameso << endl;
|
---|
| 798 | return;
|
---|
| 799 | }
|
---|
| 800 | string fname = name + "_init";
|
---|
| 801 | DlModuleInitEndFunction finit = dynlink->GetFunction(fname);
|
---|
| 802 | if (!finit) {
|
---|
| 803 | cerr << "Commander/LoadModule_Error: Pb linking " << fname << endl;
|
---|
| 804 | return;
|
---|
| 805 | }
|
---|
| 806 | cout << "Commander/LoadModule_Info: Initialisation module" << name
|
---|
| 807 | << " " << fname << "() ..." << endl;
|
---|
| 808 | finit();
|
---|
| 809 | modmap[name] = dynlink;
|
---|
| 810 | return;
|
---|
| 811 | }
|
---|
| 812 |
|
---|
| 813 | /* --Methode-- */
|
---|
[2598] | 814 | //! Declare a new interpreter
|
---|
[2446] | 815 | void Commander::AddInterpreter(CmdInterpreter * cl)
|
---|
| 816 | {
|
---|
| 817 | if (!cl) return;
|
---|
| 818 | interpmap[cl->Name()] = cl;}
|
---|
| 819 |
|
---|
| 820 | /* --Methode-- */
|
---|
[2943] | 821 | //! Select an interpreter by its name as the current interpreter.
|
---|
[2446] | 822 | void Commander::SelInterpreter(string& name)
|
---|
| 823 | {
|
---|
| 824 | InterpMap::iterator it = interpmap.find(name);
|
---|
| 825 | if (it == interpmap.end()) return;
|
---|
| 826 | curcmdi = (*it).second;
|
---|
| 827 | }
|
---|
| 828 |
|
---|
| 829 |
|
---|
| 830 |
|
---|
| 831 | /* Fonction */
|
---|
| 832 | static string GetStringFrStdin(Commander* piac)
|
---|
| 833 | {
|
---|
| 834 | char buff[128];
|
---|
| 835 | fgets(buff, 128, stdin);
|
---|
| 836 | buff[127] = '\0';
|
---|
| 837 | return((string)buff);
|
---|
| 838 | }
|
---|
| 839 |
|
---|
| 840 | /* --Methode-- */
|
---|
[2598] | 841 | /*!
|
---|
[2671] | 842 | \brief Method which has to be invoked to interpret a given command line or string.
|
---|
[2598] | 843 | */
|
---|
[2446] | 844 | int Commander::Interpret(string& s)
|
---|
| 845 | {
|
---|
| 846 | int rc = 0;
|
---|
| 847 | ScriptList::iterator sit;
|
---|
| 848 |
|
---|
[2671] | 849 | // Si le flag d'arret d'execution a ete positionne on returne avec le code
|
---|
| 850 | // de BREAKEXECUTION
|
---|
| 851 | if (fgexebrk) {
|
---|
| 852 | cout << " ===> Commander::Interpret() - STOP Execution (CMD_BREAKEXE_RC)" << endl;
|
---|
| 853 | fgexebrk = false; return CMD_BREAKEXE_RC;
|
---|
| 854 | }
|
---|
| 855 |
|
---|
[2446] | 856 | // On saute de commandes vides
|
---|
| 857 | size_t l;
|
---|
| 858 | l = s.length();
|
---|
| 859 | if (!mulinefg && (l < 1)) return(0);
|
---|
| 860 |
|
---|
| 861 | // On enregistre les commandes
|
---|
| 862 | if (histon) hist << s << endl;
|
---|
| 863 |
|
---|
| 864 | if (s[0] == '#') return(0); // si c'est un commentaire
|
---|
| 865 |
|
---|
| 866 | // Logique de gestion des lignes suite
|
---|
| 867 | // un \ en derniere position indique la presence d'une ligne suite
|
---|
| 868 | size_t lnb = s.find_last_not_of(' ');
|
---|
[3383] | 869 | if (lnb >= l) return(0); // il n'y a que des blancs
|
---|
[2446] | 870 | if (s[lnb] == '\\' ) { // Lignes suite ...
|
---|
| 871 | mulinecmd += s.substr(0,lnb);
|
---|
| 872 | if (!mulinefg) {
|
---|
| 873 | spromptmul = GetCurrentPrompt();
|
---|
| 874 | SetCurrentPrompt("...? ");
|
---|
| 875 | mulinefg = true;
|
---|
| 876 | }
|
---|
| 877 | return(0);
|
---|
| 878 | }
|
---|
| 879 |
|
---|
| 880 | if (mulinefg) { // Il y avait des lignes suite
|
---|
| 881 | s = mulinecmd + s;
|
---|
| 882 | l = s.length();
|
---|
| 883 | mulinecmd = "";
|
---|
| 884 | mulinefg = false;
|
---|
| 885 | SetCurrentPrompt(spromptmul);
|
---|
| 886 | }
|
---|
| 887 |
|
---|
| 888 | // Removing leading blanks
|
---|
| 889 | size_t p,q;
|
---|
| 890 |
|
---|
| 891 | // On enleve le dernier caractere, si celui-ci est \n
|
---|
| 892 | if (s[l-1] == '\n') s[l-1] = '\0';
|
---|
| 893 | p=s.find_first_not_of(" \t");
|
---|
| 894 | if (p < l) s = s.substr(p);
|
---|
| 895 | // >>>> Substitution d'alias (1er mot)
|
---|
| 896 | CmdStrList::iterator it;
|
---|
| 897 | p = 0;
|
---|
| 898 | q = s.find_first_of(" \t");
|
---|
| 899 | l = s.length();
|
---|
| 900 | string w1 = (q < l) ? s.substr(p,q-p) : s.substr(p);
|
---|
| 901 | it = mAliases.find(w1);
|
---|
| 902 | if (it != mAliases.end()) {
|
---|
| 903 | s = (q < l) ? ((*it).second + s.substr(q)) : (*it).second ;
|
---|
| 904 | l = s.length();
|
---|
| 905 | p=s.find_first_not_of(" \t");
|
---|
| 906 | if (p < l) s = s.substr(p);
|
---|
| 907 | p = 0;
|
---|
| 908 | q = s.find_first_of(" ");
|
---|
| 909 | }
|
---|
| 910 |
|
---|
| 911 | // >>>> Separating keyword
|
---|
| 912 | string toks,kw;
|
---|
| 913 | if (q < l)
|
---|
| 914 | { kw = s.substr(p,q-p); toks = s.substr(q, l-q); }
|
---|
| 915 | else { kw = s.substr(p,l-p); toks = ""; }
|
---|
| 916 |
|
---|
| 917 | // les mot-cle end else endif doivent etre le seul mot de la ligne
|
---|
| 918 | if ( (kw == "end") || (kw == "else") || (kw == "endif") || (kw == "endscript") ) {
|
---|
| 919 | size_t ltk = toks.length();
|
---|
| 920 | if (toks.find_first_not_of(" \t") < ltk) {
|
---|
| 921 | cerr << "Commander::Interpret()/syntax error near end else endif endscript \n"
|
---|
| 922 | << "line: " << s << endl;
|
---|
[2483] | 923 | _xstatus = 91;
|
---|
| 924 | return(91);
|
---|
[2446] | 925 | }
|
---|
| 926 | }
|
---|
| 927 |
|
---|
| 928 | // On verifie si on est en train de definir un script
|
---|
| 929 | if (curscript) {
|
---|
| 930 | if (kw == "endscript") {
|
---|
| 931 | if (curscript->CheckScript()) {
|
---|
| 932 | sit = mScripts.find(curscript->Name());
|
---|
| 933 | if (sit != mScripts.end()) {
|
---|
| 934 | cout << "Commander::Interpret() replacing script "
|
---|
| 935 | << curscript->Name() << endl;
|
---|
| 936 | CommanderScript* scr = mScripts[curscript->Name()];
|
---|
| 937 | mScripts.erase(sit);
|
---|
| 938 | delete scr;
|
---|
| 939 | }
|
---|
| 940 | cout << "Commander::Interpret() Script " << curscript->Name()
|
---|
| 941 | << " defined successfully" << endl;
|
---|
| 942 | mScripts[curscript->Name()] = curscript;
|
---|
| 943 | SetCurrentPrompt("Cmd> ");
|
---|
| 944 | curscript = NULL;
|
---|
[2483] | 945 | _xstatus = 0;
|
---|
[2446] | 946 | return(0);
|
---|
| 947 | }
|
---|
| 948 | else {
|
---|
| 949 | cout << "Commander::Interpret() Error in Script " << curscript->Name()
|
---|
| 950 | << " definition " << endl;
|
---|
| 951 | SetCurrentPrompt("Cmd> ");
|
---|
| 952 | curscript = NULL;
|
---|
[2483] | 953 | _xstatus = 92;
|
---|
| 954 | return(92);
|
---|
[2446] | 955 | }
|
---|
| 956 | }
|
---|
| 957 | else curscript->AddLine(s, kw);
|
---|
[2483] | 958 | _xstatus = 0;
|
---|
[2446] | 959 | return(0);
|
---|
| 960 | }
|
---|
| 961 | // On verifie si nous sommes dans un bloc (for , foreach)
|
---|
| 962 | if (CmdBlks.top() != NULL) { // On est dans un bloc
|
---|
[4034] | 963 | if ( (kw == "for") || (kw == "foreach") || (kw == "forinfile") ) felevel_++;
|
---|
| 964 | else if (kw == "end") felevel_--;
|
---|
[2856] | 965 |
|
---|
| 966 | int rcbex = 0;
|
---|
[4034] | 967 | if (felevel_ == 0) { // Il faut executer le bloc
|
---|
[2446] | 968 | CommanderBloc* curb = CmdBlks.top();
|
---|
| 969 | CmdBlks.top() = curb->Parent();
|
---|
| 970 | SetCurrentPrompt("Cmd> ");
|
---|
| 971 | if (!curb->CheckBloc()) {
|
---|
| 972 | cerr << "Commander::Interpret()/syntax error - unbalenced if ... endif"
|
---|
[2671] | 973 | << " within for/foreach/forinfile bloc ! " << endl;
|
---|
[2446] | 974 | delete curb;
|
---|
[2483] | 975 | _xstatus = 93;
|
---|
| 976 | return(93);
|
---|
[2446] | 977 | }
|
---|
| 978 | // cout << " *DBG* Executing bloc " << endl;
|
---|
| 979 | bool ohv = histon;
|
---|
| 980 | histon = false;
|
---|
[4034] | 981 | if (curtestresult_) {
|
---|
[2446] | 982 | // We push also CommanderBloc and testresult on the stack
|
---|
[4034] | 983 | PushStack();
|
---|
[2856] | 984 | rcbex = curb->Execute();
|
---|
[4034] | 985 | // And then, pop CommanderBloc and TestResult from the corresponding stacks
|
---|
[2446] | 986 | PopStack(false);
|
---|
| 987 | }
|
---|
[2483] | 988 | SetCurrentPrompt(defprompt);
|
---|
[2446] | 989 | delete curb;
|
---|
| 990 | histon = ohv;
|
---|
| 991 | }
|
---|
| 992 | else CmdBlks.top()->AddLine(s, kw);
|
---|
[2856] | 993 | _xstatus = rcbex;
|
---|
| 994 | return(rcbex);
|
---|
[2446] | 995 | }
|
---|
| 996 | else if (kw == "end") {
|
---|
[2671] | 997 | cerr << "Commander::Interpret()/syntax error - end outside for/foreach/forinfile bloc \n"
|
---|
[2446] | 998 | << "line: " << s << endl;
|
---|
[2483] | 999 | _xstatus = 94;
|
---|
| 1000 | return(94);
|
---|
[2446] | 1001 | }
|
---|
| 1002 |
|
---|
| 1003 | // Sommes-nous dans un bloc de test if then else
|
---|
[4034] | 1004 | if (TestsStack.top().tstlist.size() > 0) { // Nous sommes ds un bloc if
|
---|
[2446] | 1005 | if (kw == "else") {
|
---|
[4034] | 1006 | if ((*tresit_) & 2) {
|
---|
[2446] | 1007 | cerr << "Commander::Interpret()/syntax error - multiple else in if bloc \n"
|
---|
| 1008 | << "line: " << s << endl;
|
---|
[2483] | 1009 | _xstatus = 95;
|
---|
| 1010 | return(95);
|
---|
[2446] | 1011 | }
|
---|
| 1012 | else {
|
---|
[4034] | 1013 | const char * npr = ((*tresit_)&1) ? "else-F> " : "else-T> ";
|
---|
| 1014 | if ((*tresit_)&1) curtestresult_ = false;
|
---|
[2446] | 1015 | SetCurrentPrompt(npr);
|
---|
[4034] | 1016 | (*tresit_) |= 2;
|
---|
[2483] | 1017 | _xstatus = 0;
|
---|
[2446] | 1018 | return(0);
|
---|
| 1019 | }
|
---|
| 1020 | }
|
---|
| 1021 | else if (kw == "endif") {
|
---|
[4034] | 1022 | list<char>::iterator dbit = tresit_;
|
---|
| 1023 | tresit_--;
|
---|
| 1024 | TestsStack.top().tstlist.erase(dbit);
|
---|
[2446] | 1025 | const char * npr = "Cmd> ";
|
---|
[4034] | 1026 | if (TestsStack.top().tstlist.size() > 1) {
|
---|
| 1027 | curtestresult_ = true;
|
---|
[2446] | 1028 | list<char>::iterator it;
|
---|
[4034] | 1029 | for(it=TestsStack.top().tstlist.begin(); it!=TestsStack.top().tstlist.end(); it++) {
|
---|
[2446] | 1030 | // Si on n'est pas ds le else et le if est faux
|
---|
[4034] | 1031 | if ( !((*it)&2) && !((*it)&1) ) curtestresult_ = false;
|
---|
[2446] | 1032 | // Si on est ds else et le if etait vrai !
|
---|
[4034] | 1033 | if ( ((*it)&2) && ((*it)&1) ) curtestresult_ = false;
|
---|
| 1034 | if (!curtestresult_) break;
|
---|
[2446] | 1035 | }
|
---|
| 1036 |
|
---|
[4034] | 1037 | if (!((*tresit_)&2))
|
---|
| 1038 | npr = ((*tresit_)&1) ? "if-T> " : "if-F> ";
|
---|
[2446] | 1039 | else
|
---|
[4034] | 1040 | npr = ((*tresit_)&1) ? "else-F> " : "else-T> ";
|
---|
[2446] | 1041 | }
|
---|
[4034] | 1042 | else curtestresult_ = true;
|
---|
[2446] | 1043 | SetCurrentPrompt(npr);
|
---|
[2483] | 1044 | _xstatus = 0;
|
---|
[2446] | 1045 | return(0);
|
---|
| 1046 | }
|
---|
| 1047 | }
|
---|
| 1048 | else if ((kw == "else") || (kw == "endif")) {
|
---|
| 1049 | cerr << "Commander::Interpret()/syntax error - else,endif outside if bloc \n"
|
---|
| 1050 | << "line: " << s << endl;
|
---|
[2483] | 1051 | _xstatus = 91;
|
---|
| 1052 | return(91);
|
---|
[2446] | 1053 | }
|
---|
| 1054 |
|
---|
| 1055 | bool fgcont = true;
|
---|
[4034] | 1056 | if (TestsStack.top().tstlist.size() > 0) { // Resultat de if ou else
|
---|
[2446] | 1057 | list<char>::iterator it;
|
---|
[4034] | 1058 | for(it=TestsStack.top().tstlist.begin(); it!=TestsStack.top().tstlist.end(); it++) {
|
---|
[2446] | 1059 | // Si on n'est pas ds le else et le if est faux
|
---|
| 1060 | if ( !((*it)&2) && !((*it)&1) ) fgcont = false;
|
---|
| 1061 | // Si on est ds else et le if etait vrai !
|
---|
| 1062 | if ( ((*it)&2) && ((*it)&1) ) fgcont = false;
|
---|
| 1063 | if (!fgcont) break;
|
---|
| 1064 | }
|
---|
| 1065 | }
|
---|
| 1066 |
|
---|
[2483] | 1067 | if ((!fgcont) && (kw != "if")) {
|
---|
| 1068 | _xstatus = 0;
|
---|
| 1069 | return(0);
|
---|
| 1070 | }
|
---|
[2446] | 1071 |
|
---|
| 1072 |
|
---|
| 1073 | // Les mots cles break et return peuvent de sortir de boucles/scripts/execfile
|
---|
[2518] | 1074 | if (kw == "break") return CMD_BREAK_RC;
|
---|
[4034] | 1075 | if (kw == "breakall") return CMD_BREAKALL_RC;
|
---|
[2483] | 1076 | else if (kw == "return") {
|
---|
| 1077 | _retstr = toks;
|
---|
[2518] | 1078 | return CMD_RETURN_RC;
|
---|
[2483] | 1079 | }
|
---|
[2446] | 1080 |
|
---|
| 1081 | // Nous ne sommes donc pas dans un bloc .... Substitution de variables
|
---|
| 1082 | string s2;
|
---|
| 1083 | int rcs ;
|
---|
| 1084 |
|
---|
| 1085 | rcs = SubstituteVars(s, s2);
|
---|
| 1086 | if (rcs) {
|
---|
| 1087 | cerr << "Commander::Interpret()/syntax error in SubstituteVars() \n"
|
---|
| 1088 | << "line: " << s << endl;
|
---|
[2483] | 1089 | _xstatus = 99;
|
---|
| 1090 | return(99);
|
---|
[2446] | 1091 | }
|
---|
| 1092 | // >>>> Separating keyword and tokens
|
---|
| 1093 | vector<string> tokens;
|
---|
[2518] | 1094 | vector<bool> qottoks;
|
---|
[2446] | 1095 | /* decoupage en mots */
|
---|
[2518] | 1096 | LineToWords(s2, kw, tokens, qottoks, toks, true);
|
---|
[2446] | 1097 |
|
---|
| 1098 | // Si c'est un for/foreach, on cree un nouveau bloc
|
---|
[2483] | 1099 | if ((kw == "foreach") || (kw == "for") || (kw == "forinfile") ) {
|
---|
[2446] | 1100 | // cout << " *DBG* We got a foreach... " << endl;
|
---|
| 1101 | CommanderBloc* bloc = new CommanderBloc(this, CmdBlks.top(), kw, tokens);
|
---|
| 1102 | if (!bloc->CheckOK()) {
|
---|
| 1103 | cerr << "Commander::Interpret() for/foreach syntax Error ! " << endl;
|
---|
| 1104 | delete bloc;
|
---|
[2483] | 1105 | _xstatus = 91;
|
---|
| 1106 | return(91);
|
---|
[2446] | 1107 | }
|
---|
[4034] | 1108 | felevel_++;
|
---|
[2446] | 1109 | if (CmdBlks.top()) CmdBlks.top()->AddBloc(bloc);
|
---|
| 1110 | else SetCurrentPrompt("for...> ");
|
---|
| 1111 | CmdBlks.top() = bloc;
|
---|
| 1112 | // cout << " *DBG* New Bloc created ... " << endl;
|
---|
| 1113 | return(0);
|
---|
| 1114 | }
|
---|
| 1115 | else if (kw == "if") { // Un test if
|
---|
| 1116 | bool restst = true;
|
---|
| 1117 | int rct = EvaluateTest(tokens, s, restst);
|
---|
| 1118 | if (rct) {
|
---|
| 1119 | cerr << "Commander::Interpret() if syntax Error ! " << "line: " << s << endl;
|
---|
[2483] | 1120 | _xstatus = 91;
|
---|
| 1121 | return(91);
|
---|
[2446] | 1122 | }
|
---|
| 1123 | char res_tst = (restst) ? 1 : 0;
|
---|
[4034] | 1124 | TestsStack.top().tstlist.push_back(res_tst);
|
---|
| 1125 | if (TestsStack.top().tstlist.size() == 1) tresit_ = TestsStack.top().tstlist.begin();
|
---|
| 1126 | else tresit_++;
|
---|
[2446] | 1127 | const char * npr = (restst) ? "if-T> " : "if-F> ";
|
---|
| 1128 | SetCurrentPrompt(npr);
|
---|
| 1129 | }
|
---|
| 1130 | else if ((tokens.size() > 0) && (tokens[0] == "=")) {
|
---|
[2512] | 1131 | // x = Expression
|
---|
[2518] | 1132 | if (qottoks[1]) { // decodage sous forme de chaine
|
---|
| 1133 | SetVariable(kw, tokens[1]);
|
---|
[2512] | 1134 | }
|
---|
[2518] | 1135 | else {
|
---|
| 1136 | try {
|
---|
| 1137 | double res = 0.;
|
---|
[4063] | 1138 | string sex=tokens[1];
|
---|
| 1139 | if (tokens.size() > 2)
|
---|
[3581] | 1140 | for(unsigned int js=2; js<tokens.size(); js++) sex += tokens[js];
|
---|
[4063] | 1141 | if (varcexp) { //--- Evaluation d'expression avec decodage de noms de variables
|
---|
| 1142 | Cmd_CE_VarList cevl(*this);
|
---|
| 1143 | CExpressionEvaluator cex(sex,&cevl);
|
---|
[2518] | 1144 | res = cex.Value();
|
---|
| 1145 | }
|
---|
[4063] | 1146 | else { //--- Evaluation d'expression SANS decodage des noms de variables
|
---|
| 1147 | CExpressionEvaluator cex(sex);
|
---|
[2518] | 1148 | res = cex.Value();
|
---|
| 1149 | }
|
---|
[3577] | 1150 | string vv = cval_dble2str(res);
|
---|
[2518] | 1151 | SetVariable(kw, vv);
|
---|
| 1152 | }
|
---|
| 1153 | catch (CExprException& cexerr) {
|
---|
| 1154 | cerr << "Commander::Interpret() evaluation Error : \n " << "line: " << s
|
---|
| 1155 | << " \n Msg=" << cexerr.Msg() << endl;
|
---|
| 1156 | _xstatus = 98;
|
---|
| 1157 | return(98);
|
---|
| 1158 | }
|
---|
[2446] | 1159 | }
|
---|
| 1160 | }
|
---|
| 1161 | else if (kw == "defscript") { // definition de script
|
---|
| 1162 | if (tokens.size() > 0) {
|
---|
| 1163 | if (tokens.size() < 2) tokens.push_back("");
|
---|
| 1164 | curscript = new CommanderScript(this, tokens[0], tokens[1]);
|
---|
| 1165 | SetCurrentPrompt("Script...> ");
|
---|
| 1166 | return(0);
|
---|
| 1167 | }
|
---|
| 1168 | else {
|
---|
| 1169 | cerr << "Commander::Interpret() No script name in defscript" << "line: " << s << endl;
|
---|
[2483] | 1170 | _xstatus = 91;
|
---|
| 1171 | return(91);
|
---|
[2446] | 1172 | }
|
---|
| 1173 | }
|
---|
| 1174 | else {
|
---|
| 1175 | // Si c'est le nom d'un script
|
---|
| 1176 | sit = mScripts.find(kw);
|
---|
| 1177 | if (sit != mScripts.end()) {
|
---|
| 1178 | bool ohv = histon;
|
---|
| 1179 | histon = false;
|
---|
| 1180 | tokens.insert(tokens.begin(), kw);
|
---|
| 1181 | PushStack(tokens);
|
---|
| 1182 | (*sit).second->Execute(tokens);
|
---|
| 1183 | PopStack(true);
|
---|
| 1184 | histon = ohv;
|
---|
| 1185 | }
|
---|
| 1186 | // Execution de commandes
|
---|
| 1187 | else rc = ExecuteCommandLine(kw, tokens, toks);
|
---|
[2483] | 1188 | _xstatus = rc;
|
---|
[2446] | 1189 | return(rc);
|
---|
| 1190 | }
|
---|
| 1191 | // cout << "Commander::Do() DBG KeyW= " << kw << " NbArgs= " << tokens.size() << endl;
|
---|
| 1192 | // for(int ii=0; ii<tokens.size(); ii++)
|
---|
| 1193 | // cout << "arg[ " << ii << " ] : " << tokens[ii] << endl;
|
---|
| 1194 |
|
---|
| 1195 | return(0);
|
---|
| 1196 | }
|
---|
| 1197 |
|
---|
[2943] | 1198 | //! Can be called asynchronously (from a separate thread) to break (halt) execution (in loops, scripts ...)
|
---|
[2671] | 1199 | void Commander::StopExecution()
|
---|
| 1200 | {
|
---|
| 1201 | fgexebrk = true;
|
---|
| 1202 | }
|
---|
[2446] | 1203 |
|
---|
[2671] | 1204 |
|
---|
[2446] | 1205 | /* --Methode-- */
|
---|
| 1206 | int Commander::LineToWords(string& line, string& kw, vector<string>& tokens,
|
---|
[2518] | 1207 | vector<bool>& qottoks, string& toks, bool uq)
|
---|
[2446] | 1208 | {
|
---|
| 1209 | if (line.length() < 1) return(0);
|
---|
| 1210 | int nw = 1;
|
---|
| 1211 | size_t p = line.find_first_not_of(" ");
|
---|
| 1212 | line = line.substr(p);
|
---|
| 1213 | p = 0;
|
---|
| 1214 | size_t q = line.find_first_of(" ");
|
---|
| 1215 | size_t l = line.length();
|
---|
| 1216 |
|
---|
| 1217 | if (q < l)
|
---|
| 1218 | { kw = line.substr(p,q-p); toks = line.substr(q, l-q); }
|
---|
| 1219 | else { kw = line.substr(p,l-p); toks = ""; }
|
---|
| 1220 |
|
---|
| 1221 | q = 0;
|
---|
| 1222 | while (q < l) {
|
---|
[2518] | 1223 | bool swq = false; // true -> chaine delimite par ' ou "
|
---|
[2446] | 1224 | p = toks.find_first_not_of(" \t",q+1); // au debut d'un token
|
---|
| 1225 | if (p>=l) break;
|
---|
| 1226 | if ( uq && ((toks[p] == '\'') || (toks[p] == '"')) ) {
|
---|
| 1227 | q = toks.find(toks[p],p+1);
|
---|
| 1228 | if (q>=l) {
|
---|
| 1229 | cerr << "Commander::LineToWords/Syntax Error - Unbalenced quotes " << toks[p] << '.' << endl;
|
---|
| 1230 | return(-1);
|
---|
| 1231 | }
|
---|
[2518] | 1232 | p++; swq = true;
|
---|
[2446] | 1233 | }
|
---|
| 1234 | else {
|
---|
| 1235 | q = toks.find_first_of(" \t",p); // la fin du token;
|
---|
| 1236 | }
|
---|
| 1237 | string token = toks.substr(p,q-p);
|
---|
[2518] | 1238 | tokens.push_back(token);
|
---|
| 1239 | qottoks.push_back(swq);
|
---|
| 1240 | nw++;
|
---|
[2446] | 1241 | }
|
---|
| 1242 |
|
---|
| 1243 | return(nw);
|
---|
| 1244 | }
|
---|
| 1245 |
|
---|
| 1246 | /* --Methode-- */
|
---|
| 1247 | int Commander::SubstituteVars(string & s, string & s2)
|
---|
| 1248 | // Variable substitution
|
---|
| 1249 | {
|
---|
| 1250 |
|
---|
| 1251 | int iarr = -1; // index d'element de tableau
|
---|
| 1252 | size_t p,q,q2,q3,l;
|
---|
[2997] | 1253 | bool fgvarapp = false; // Si true, VarApp en priorite
|
---|
[2446] | 1254 |
|
---|
| 1255 | s2="";
|
---|
| 1256 | p = 0;
|
---|
| 1257 | l = s.length();
|
---|
| 1258 | string vn, vv;
|
---|
| 1259 | while (p < l) {
|
---|
| 1260 | iarr = -1;
|
---|
[2997] | 1261 | fgvarapp = false;
|
---|
[2446] | 1262 | q = s.find('$',p);
|
---|
| 1263 | if (q > l) break;
|
---|
| 1264 | q2 = s.find('\'',p);
|
---|
| 1265 | if ((q2 < l) && (q2 < q)) { // On saute la chaine delimitee par ' '
|
---|
| 1266 | q2 = s.find('\'',q2+1);
|
---|
| 1267 | if (q2 >= l) {
|
---|
| 1268 | cerr << " Syntax error - Unbalenced quotes !!! " << endl;
|
---|
| 1269 | return(1);
|
---|
| 1270 | }
|
---|
| 1271 | s2 += s.substr(p, q2-p+1);
|
---|
| 1272 | p = q2+1; continue;
|
---|
| 1273 | }
|
---|
| 1274 | // cout << "DBG: " << s2 << " p= " << p << " q= " << q << " L= " << l << endl;
|
---|
| 1275 | if ((q>0) && (s[q-1] == '\\')) { // Escape character \$
|
---|
| 1276 | s2 += (s.substr(p,q-1-p) + '$') ; p = q+1;
|
---|
| 1277 | continue;
|
---|
| 1278 | }
|
---|
| 1279 | if (q >= l-1) {
|
---|
| 1280 | cerr << " Syntax error - line ending with $ !!! " << endl;
|
---|
| 1281 | return(2);
|
---|
| 1282 | }
|
---|
| 1283 | vn = "";
|
---|
| 1284 | if ( s[q+1] == '{' ) { // Variable in the form ${name}
|
---|
| 1285 | q2 = s.find('}',q+1);
|
---|
| 1286 | if (q2 >= l) {
|
---|
| 1287 | cerr << " Syntax error - Unbalenced brace {} !!! " << endl;
|
---|
| 1288 | return(3);
|
---|
| 1289 | }
|
---|
| 1290 | vn = s.substr(q+2,q2-q-2);
|
---|
| 1291 | q2++;
|
---|
[2997] | 1292 | fgvarapp = true;
|
---|
[2446] | 1293 | }
|
---|
| 1294 | else if ( s[q+1] == '(' ) { // Variable in the form $(name)
|
---|
| 1295 | q2 = s.find(')',q+1);
|
---|
| 1296 | if (q2 >= l) {
|
---|
| 1297 | cerr << " Syntax error - Unbalenced parenthesis () !!! " << endl;
|
---|
| 1298 | return(3);
|
---|
| 1299 | }
|
---|
| 1300 | vn = s.substr(q+2,q2-q-2);
|
---|
| 1301 | q2++;
|
---|
| 1302 | }
|
---|
| 1303 | else if ( s[q+1] == '[' ) { // Variable in the form $[varname] -> This is $$varname
|
---|
| 1304 | q2 = s.find(']',q+1);
|
---|
| 1305 | if (q2 >= l) {
|
---|
| 1306 | cerr << " Syntax error - Unbalenced brace [] !!! " << endl;
|
---|
| 1307 | return(4);
|
---|
| 1308 | }
|
---|
| 1309 | vn = s.substr(q+2,q2-q-2);
|
---|
[2483] | 1310 | if (!Var2Str(vn, vv)) return(5);
|
---|
[2446] | 1311 | vn = vv;
|
---|
| 1312 | q2++;
|
---|
| 1313 | }
|
---|
| 1314 | else {
|
---|
[2779] | 1315 | if (s[q+1] == '#' ) q3 = q+2; // Variable in the form $#varname
|
---|
| 1316 | else q3 = q+1;
|
---|
| 1317 | q2 = s.find_first_of(" .:+-*/,[](){}&|!$\"'<>^%=#@\\",q3);
|
---|
[2446] | 1318 | if (q2 > l) q2 = l;
|
---|
| 1319 | q3 = q2;
|
---|
| 1320 | vn = s.substr(q+1, q2-q-1);
|
---|
| 1321 | // Si variable de type $varname[index] : element de tableau
|
---|
| 1322 | if ((q2 < l) && (s[q2] == '[') ) {
|
---|
| 1323 | q3 = s.find_first_of("]",q2+1);
|
---|
| 1324 | string sia = s.substr(q2+1, q3-q2-1);
|
---|
| 1325 | if (sia.length() < 1) {
|
---|
| 1326 | cerr << " Syntax error - in $varname[index] : $"
|
---|
| 1327 | << vn << "[" << sia <<"]" << endl;
|
---|
| 1328 | return(4);
|
---|
| 1329 | }
|
---|
| 1330 | if (isalpha(sia[0])) {
|
---|
| 1331 | string sia2;
|
---|
[2483] | 1332 | if (!Var2Str(sia, sia2) || (sia2.length() < 1)) {
|
---|
[2446] | 1333 | cerr << " Syntax error - in $varname[index] : $"
|
---|
| 1334 | << vn << "[" << sia <<"]" << endl;
|
---|
| 1335 | return(4);
|
---|
| 1336 | }
|
---|
| 1337 | sia = sia2;
|
---|
| 1338 | }
|
---|
| 1339 | int rcdia = ctoi(sia.c_str(), &iarr);
|
---|
| 1340 | if (rcdia < 0) {
|
---|
| 1341 | cerr << " Syntax error - in $varname[iarr] : $"
|
---|
| 1342 | << vn << "[" << sia <<"]" << endl;
|
---|
| 1343 | return(4);
|
---|
| 1344 | }
|
---|
| 1345 | }
|
---|
| 1346 | }
|
---|
[2997] | 1347 | if (fgvarapp) {
|
---|
| 1348 | if (!GetVarApp(vn, vv))
|
---|
| 1349 | if (!Var2Str(vn, vv)) return(5);
|
---|
| 1350 | s2 += (s.substr(p, q-p) + vv);
|
---|
| 1351 | p = q2;
|
---|
| 1352 | }
|
---|
| 1353 | else if (iarr < 0) {
|
---|
[2483] | 1354 | if (!Var2Str(vn, vv)) return(5);
|
---|
[2446] | 1355 | s2 += (s.substr(p, q-p) + vv);
|
---|
| 1356 | p = q2;
|
---|
| 1357 | }
|
---|
| 1358 | else {
|
---|
[2483] | 1359 | if (! Var2Str(vn, iarr, vv) ) {
|
---|
[2446] | 1360 | cerr << " Substitution error - word index out of range in "
|
---|
| 1361 | << "$varname[iarr] : $" << vn << "[" << iarr <<"]" << endl;
|
---|
| 1362 | return(4);
|
---|
| 1363 | }
|
---|
[2483] | 1364 | else s2 += (s.substr(p, q-p) + vv);
|
---|
[2446] | 1365 | p = q3+1;
|
---|
| 1366 | }
|
---|
| 1367 | }
|
---|
| 1368 | if (p < l) s2 += s.substr(p);
|
---|
| 1369 |
|
---|
| 1370 | p = s2.find_first_not_of(" \t");
|
---|
| 1371 | if (p < l) s2 = s2.substr(p);
|
---|
| 1372 |
|
---|
| 1373 | return(0);
|
---|
| 1374 | }
|
---|
| 1375 |
|
---|
| 1376 | /* --Methode-- */
|
---|
[2483] | 1377 | bool Commander::Var2Str(string const & vn, string & vv)
|
---|
[2446] | 1378 | {
|
---|
| 1379 | if (vn.length() < 1) {
|
---|
[2518] | 1380 | cerr << " Commander::Var2Str/Error: length(varname=" << vn << ")<1 !" << endl;
|
---|
[2446] | 1381 | vv = ""; return(false);
|
---|
| 1382 | }
|
---|
| 1383 | // Variable de type $# $0 $1 ... (argument de .pic ou de script)
|
---|
| 1384 | int ka = 0;
|
---|
[2483] | 1385 | char buff[32];
|
---|
| 1386 |
|
---|
[2446] | 1387 | if (vn == "#") {
|
---|
| 1388 | if (ArgsStack.empty()) {
|
---|
[2518] | 1389 | cerr << " Commander::Var2Str/Error: ArgsStack empty ! "
|
---|
[2446] | 1390 | << " ($" << vn << ")" << endl;
|
---|
| 1391 | vv = ""; return(false);
|
---|
| 1392 | }
|
---|
| 1393 | char buff[32];
|
---|
| 1394 | long an = ArgsStack.top().size();
|
---|
[2518] | 1395 | if (an > 0) an--; // Pour se conformer a l'usage de csh : Nb args sans le $0
|
---|
[2446] | 1396 | sprintf(buff,"%ld", an);
|
---|
| 1397 | vv = buff; return(true);
|
---|
| 1398 | }
|
---|
[2518] | 1399 | else if (vn == "*") {
|
---|
| 1400 | if (ArgsStack.empty()) {
|
---|
| 1401 | cerr << " Commander::Var2Str/Error: ArgsStack empty ! "
|
---|
| 1402 | << " ($" << vn << ")" << endl;
|
---|
| 1403 | vv = ""; return(false);
|
---|
| 1404 | }
|
---|
| 1405 | vv = ArgsStack.top()[0];
|
---|
[3581] | 1406 | for(unsigned int ssk=1; ssk<ArgsStack.top().size(); ssk++) vv += ArgsStack.top()[ssk];
|
---|
[2518] | 1407 | return(true);
|
---|
| 1408 | }
|
---|
[2446] | 1409 | else if (ctoi(vn.c_str(), &ka) > 0) { // $0 $1 $2 ...
|
---|
| 1410 | if (ArgsStack.empty()) {
|
---|
[2518] | 1411 | cerr << " Commander::Var2Str/Error: ArgsStack empty ! "
|
---|
[2446] | 1412 | << " ($" << vn << ")" << endl;
|
---|
| 1413 | vv = ""; return(false);
|
---|
| 1414 | }
|
---|
[3581] | 1415 | if ( (ka < 0) || (ka >= (int)ArgsStack.top().size()) ) {
|
---|
[2518] | 1416 | cerr << " Commander::Var2Str/Error: ArgsStack index <0 or >=args.size() ! "
|
---|
[2446] | 1417 | << " ($" << vn << ")" << endl;
|
---|
| 1418 | vv = ""; return(false);
|
---|
| 1419 | }
|
---|
| 1420 | vv = ArgsStack.top()[ka]; return(true);
|
---|
| 1421 | }
|
---|
| 1422 | else if (vn[0] == '#') { // Variable de type $#vname --> size(vname)
|
---|
[2483] | 1423 | CmdVarList::iterator it = variables.find(vn.substr(1));
|
---|
| 1424 | if (it == variables.end()) {
|
---|
[2518] | 1425 | cerr << " Commander::Var2Str/Error #vname Undefined variable "
|
---|
[2446] | 1426 | << vn << " ! " << endl;
|
---|
| 1427 | vv = ""; return(false);
|
---|
| 1428 | }
|
---|
[2483] | 1429 | sprintf(buff,"%d", (int)(*it).second.size());
|
---|
[2446] | 1430 | vv = buff; return(true);
|
---|
[2483] | 1431 | }
|
---|
| 1432 | else if (vn == "status") {
|
---|
| 1433 | sprintf(buff,"%d", _xstatus);
|
---|
| 1434 | vv = buff;
|
---|
| 1435 | return true;
|
---|
| 1436 | }
|
---|
[2518] | 1437 | else if ((vn == "retstr") || (vn == "retval")) {
|
---|
[2483] | 1438 | vv = _retstr;
|
---|
| 1439 | return true;
|
---|
| 1440 | }
|
---|
| 1441 | else { // Variable de l'interpreteur, ou de l'environnement application , env. global
|
---|
| 1442 | if (GetVar(vn, vv)) return true;
|
---|
| 1443 | else if (GetVarApp(vn, vv)) return true;
|
---|
| 1444 | else if (GetVarEnv(vn, vv)) return true;
|
---|
| 1445 | else {
|
---|
[2518] | 1446 | cerr << " Commander::Var2Str/Error Undefined variable "
|
---|
[2446] | 1447 | << vn << " ! " << endl;
|
---|
[2483] | 1448 | vv = ""; return false;
|
---|
[2446] | 1449 | }
|
---|
| 1450 | }
|
---|
| 1451 |
|
---|
[2483] | 1452 | return false;
|
---|
[2446] | 1453 | }
|
---|
[2483] | 1454 |
|
---|
[2446] | 1455 | /* --Methode-- */
|
---|
[2483] | 1456 | bool Commander::SetVariable(string const & vn, string const & vv)
|
---|
[2446] | 1457 | {
|
---|
[2483] | 1458 | // On verifie si le nom est de type vname[idx]
|
---|
| 1459 | size_t p,q,l;
|
---|
| 1460 | l = vn.length();
|
---|
| 1461 | p = vn.find('[');
|
---|
| 1462 | if (p < l) {
|
---|
| 1463 | q = vn.find(']');
|
---|
| 1464 | if (q != (l-1)) {
|
---|
| 1465 | cout << "Commander::Str2Var/SetVar() - Bad varname with []: "
|
---|
| 1466 | << vn << endl;
|
---|
| 1467 | return false;
|
---|
| 1468 | }
|
---|
| 1469 | string vna = vn.substr(0, p);
|
---|
| 1470 | string sia = vn.substr(p+1, q-(p+1));
|
---|
| 1471 | if (isalpha(sia[0])) {
|
---|
| 1472 | string sia2;
|
---|
| 1473 | if (!Var2Str(sia, sia2) || (sia2.length() < 1)) {
|
---|
| 1474 | cerr << "Commander::Str2Var/SetVar() Syntax error- varname[index]:"
|
---|
| 1475 | << vn << endl;
|
---|
| 1476 | return false;
|
---|
| 1477 | }
|
---|
| 1478 | sia = sia2;
|
---|
| 1479 | }
|
---|
| 1480 | int iarr;
|
---|
| 1481 | int rcdia = ctoi(sia.c_str(), &iarr);
|
---|
| 1482 | if (rcdia < 0) {
|
---|
| 1483 | cerr << "Commander::Str2Var/SetVar() Syntax error- varname[iarr]: "
|
---|
| 1484 | << vn << endl;
|
---|
| 1485 | return false;
|
---|
| 1486 | }
|
---|
| 1487 | return SetVar(vna, iarr, vv);
|
---|
[2446] | 1488 | }
|
---|
[2483] | 1489 | else {
|
---|
| 1490 | if (vn == "status") {
|
---|
| 1491 | _xstatus = atoi(vv.c_str());
|
---|
| 1492 | return true;
|
---|
| 1493 | }
|
---|
| 1494 | else if (vn == "retstr") {
|
---|
| 1495 | _retstr = vv;
|
---|
| 1496 | return true;
|
---|
| 1497 | }
|
---|
| 1498 | else return SetVar(vn, vv);
|
---|
| 1499 | }
|
---|
[2446] | 1500 | }
|
---|
| 1501 |
|
---|
| 1502 | /* --Methode-- */
|
---|
[2483] | 1503 | bool Commander::GetVar(string const & vn, string & vv)
|
---|
[2446] | 1504 | {
|
---|
[2483] | 1505 | CmdVarList::iterator it = variables.find(vn);
|
---|
| 1506 | if (it == variables.end()) {
|
---|
| 1507 | vv = "";
|
---|
| 1508 | return false;
|
---|
| 1509 | }
|
---|
| 1510 | vv = (*it).second[0];
|
---|
| 1511 | if ((*it).second.size() > 1) {
|
---|
[3581] | 1512 | for(unsigned int k=1; k<(*it).second.size(); k++) {
|
---|
[2483] | 1513 | vv += ' '; vv += (*it).second[k];
|
---|
| 1514 | }
|
---|
| 1515 | }
|
---|
| 1516 | return true;
|
---|
| 1517 | }
|
---|
| 1518 |
|
---|
| 1519 | /* --Methode-- */
|
---|
| 1520 | bool Commander::GetVar(string const & vn, int idx, string & vv)
|
---|
| 1521 | {
|
---|
| 1522 | vv = "";
|
---|
| 1523 | CmdVarList::iterator it = variables.find(vn);
|
---|
| 1524 | if (it == variables.end()) return false;
|
---|
[3581] | 1525 | if ((idx < 0) || (idx > (int)(*it).second.size()-1))
|
---|
[2483] | 1526 | return false;
|
---|
| 1527 | vv = (*it).second[idx];
|
---|
| 1528 | return true;
|
---|
| 1529 | }
|
---|
| 1530 |
|
---|
| 1531 | /* --Methode-- */
|
---|
| 1532 | bool Commander::GetVar(string const & vn, vector<string> & vv)
|
---|
| 1533 | {
|
---|
| 1534 | vv.clear();
|
---|
| 1535 | // vv.erase(vv.begin(),vv.end());
|
---|
| 1536 | CmdVarList::iterator it = variables.find(vn);
|
---|
| 1537 | if (it == variables.end()) return false;
|
---|
| 1538 | vv = (*it).second;
|
---|
| 1539 | return true;
|
---|
| 1540 | }
|
---|
| 1541 |
|
---|
| 1542 | /* --Methode-- */
|
---|
| 1543 | bool Commander::SetVar(string const & vn, string const & val)
|
---|
| 1544 | {
|
---|
| 1545 | if ( !CheckVarName(vn) ) {
|
---|
| 1546 | cerr << "Commander::SetVar( " << vn << " ...) Bad VarName " << endl;
|
---|
[2446] | 1547 | return(false);
|
---|
| 1548 | }
|
---|
[2483] | 1549 | bool fg = false;
|
---|
| 1550 | vector<string> nouv;
|
---|
| 1551 | nouv.push_back(val);
|
---|
| 1552 | CmdVarList::iterator it = variables.find(vn);
|
---|
| 1553 | if (it == variables.end()) variables[vn] = nouv;
|
---|
| 1554 | else {
|
---|
| 1555 | (*it).second = nouv;
|
---|
| 1556 | fg = true;
|
---|
| 1557 | }
|
---|
| 1558 | return fg;
|
---|
[2446] | 1559 | }
|
---|
| 1560 |
|
---|
| 1561 | /* --Methode-- */
|
---|
[2483] | 1562 | bool Commander::SetVar(string const & vn, int idx, string const & val)
|
---|
[2446] | 1563 | {
|
---|
[2483] | 1564 | if ( !CheckVarName(vn) ) {
|
---|
| 1565 | cerr << "Commander::SetVar( " << vn << " ,idx, ...) Bad VarName " << endl;
|
---|
[2446] | 1566 | return(false);
|
---|
| 1567 | }
|
---|
[2483] | 1568 | if ((vn == "status") || (vn == "retstr")) {
|
---|
| 1569 | cerr << "Commander::SetVar(vn,idx,val) ERROR - special var status/retstr "
|
---|
| 1570 | << endl;
|
---|
| 1571 | return(false);
|
---|
| 1572 | }
|
---|
| 1573 | if (idx < 0) {
|
---|
| 1574 | cout << "Commander::SetVar(vn," << idx << ",...) Error idx < 0" << endl;
|
---|
| 1575 | return(false);
|
---|
| 1576 | }
|
---|
| 1577 | bool fg = false;
|
---|
| 1578 | CmdVarList::iterator it = variables.find(vn);
|
---|
| 1579 | if (it == variables.end()) {
|
---|
| 1580 | vector<string> nouv;
|
---|
| 1581 | for(int j=0; j<idx; j++) nouv.push_back("");
|
---|
| 1582 | nouv.push_back(val);
|
---|
| 1583 | variables[vn] = nouv;
|
---|
| 1584 | }
|
---|
| 1585 | else {
|
---|
[3581] | 1586 | if (idx >= (int)(*it).second.size())
|
---|
[2512] | 1587 | for(int j=(*it).second.size(); j<=idx; j++) (*it).second.push_back("");
|
---|
| 1588 | (*it).second[idx] = val;
|
---|
[2483] | 1589 | fg = true;
|
---|
| 1590 | }
|
---|
[2446] | 1591 | return fg;
|
---|
| 1592 | }
|
---|
| 1593 |
|
---|
| 1594 | /* --Methode-- */
|
---|
[2483] | 1595 | bool Commander::SetVar(string const & vn, vector<string> const & val)
|
---|
[2446] | 1596 | {
|
---|
[2483] | 1597 | if ( !CheckVarName(vn) ) {
|
---|
| 1598 | cerr << "Commander::SetVar( " << vn << " ...) Bad VarName " << endl;
|
---|
[2446] | 1599 | return(false);
|
---|
| 1600 | }
|
---|
[2483] | 1601 | if ((vn == "status") || (vn == "retstr")) {
|
---|
| 1602 | cerr << "Commander::SetVar(vn, vector<string>) ERROR - special var status/retstr "
|
---|
| 1603 | << endl;
|
---|
| 1604 | return(false);
|
---|
| 1605 | }
|
---|
| 1606 | bool fg = false;
|
---|
| 1607 | CmdVarList::iterator it = variables.find(vn);
|
---|
| 1608 | if (it == variables.end()) variables[vn] = val;
|
---|
| 1609 | else {
|
---|
| 1610 | (*it).second = val;
|
---|
| 1611 | fg = true;
|
---|
| 1612 | }
|
---|
| 1613 | return fg;
|
---|
[2446] | 1614 | }
|
---|
| 1615 |
|
---|
| 1616 | /* --Methode-- */
|
---|
[2483] | 1617 | bool Commander::CheckVarName(string const & vn)
|
---|
[2446] | 1618 | {
|
---|
[2483] | 1619 | size_t l,k;
|
---|
| 1620 | l = vn.length();
|
---|
| 1621 | if (l < 1) return false;
|
---|
| 1622 | if (!isalpha(vn[0])) return false;
|
---|
| 1623 | for(k=1; k<l; k++)
|
---|
| 1624 | if ((!isalnum(vn[k])) && (vn[k] != '_')) return false;
|
---|
| 1625 | return true;
|
---|
| 1626 | }
|
---|
| 1627 |
|
---|
| 1628 | /* --Methode-- */
|
---|
| 1629 | bool Commander::DeleteVar(string const & vn)
|
---|
| 1630 | {
|
---|
| 1631 | CmdVarList::iterator it = variables.find(vn);
|
---|
| 1632 | if (it == variables.end()) {
|
---|
| 1633 | cerr << "Commander::DeleteVar() Var " << vn << " undefined!" << endl;
|
---|
| 1634 | return false;
|
---|
| 1635 | }
|
---|
| 1636 | variables.erase(it);
|
---|
| 1637 | return true;
|
---|
| 1638 | }
|
---|
| 1639 |
|
---|
| 1640 | /* --Methode-- */
|
---|
| 1641 | void Commander::ListVar()
|
---|
| 1642 | {
|
---|
| 1643 | cout << " ---- Commander::ListVar() List of defined variables ---- "
|
---|
[2446] | 1644 | << endl;
|
---|
[2483] | 1645 | CmdVarList::iterator it;
|
---|
| 1646 | for(it = variables.begin(); it != variables.end(); it++) {
|
---|
| 1647 | string vn = (*it).first;
|
---|
| 1648 | int vs = (*it).second.size();
|
---|
| 1649 | cout << vn << " -> Size= " << vs << endl;
|
---|
| 1650 | }
|
---|
[2446] | 1651 | cout << "---------------------------------------------------------- "
|
---|
| 1652 | << endl;
|
---|
| 1653 | }
|
---|
| 1654 |
|
---|
| 1655 | /* --Methode-- */
|
---|
[2483] | 1656 | bool Commander::GetVarApp(string const & vn, string & vv)
|
---|
| 1657 | {
|
---|
| 1658 | vv = "";
|
---|
[2518] | 1659 | // cout << " Commander::GetVarApp() Not available ! " << endl;
|
---|
[2483] | 1660 | return false;
|
---|
| 1661 | }
|
---|
| 1662 |
|
---|
| 1663 | /* --Methode-- */
|
---|
| 1664 | bool Commander::SetVarApp(string const & vn, string const & vv)
|
---|
| 1665 | {
|
---|
[2518] | 1666 | // cout << " Commander::SetVarApp() Not available ! " << endl;
|
---|
[2483] | 1667 | return false;
|
---|
| 1668 | }
|
---|
| 1669 |
|
---|
| 1670 | /* --Methode-- */
|
---|
| 1671 | bool Commander::DeleteVarApp(string const & vn)
|
---|
| 1672 | {
|
---|
[2518] | 1673 | // cout << " Commander::DeleteVarApp() Not available ! " << endl;
|
---|
[2483] | 1674 | return false;
|
---|
| 1675 | }
|
---|
| 1676 |
|
---|
| 1677 | /* --Methode-- */
|
---|
| 1678 | void Commander::ListVarApp()
|
---|
| 1679 | {
|
---|
[2518] | 1680 | // cout << " Commander::ListVarApp() Not available ! " << endl;
|
---|
[2483] | 1681 | return;
|
---|
| 1682 | }
|
---|
| 1683 |
|
---|
| 1684 |
|
---|
| 1685 | /* --Methode-- */
|
---|
| 1686 | bool Commander::GetVarEnv(string const & vn, string & vv)
|
---|
| 1687 | {
|
---|
[2518] | 1688 | char* vev = getenv(vn.c_str());
|
---|
| 1689 | if (vev) {
|
---|
| 1690 | vv = vev;
|
---|
| 1691 | return true;
|
---|
| 1692 | }
|
---|
| 1693 | else {
|
---|
| 1694 | vv = "";
|
---|
| 1695 | return false;
|
---|
| 1696 | }
|
---|
[2483] | 1697 | }
|
---|
| 1698 |
|
---|
| 1699 | /* --Methode-- */
|
---|
| 1700 | bool Commander::SetVarEnv(string const & vn, string const & vv)
|
---|
| 1701 | {
|
---|
[2518] | 1702 | string pev = vn;
|
---|
| 1703 | pev += '=';
|
---|
| 1704 | pev += vv;
|
---|
[2887] | 1705 | // if defined(Linux) || defined(AIX)
|
---|
[2532] | 1706 | // Reza - 28/04/2004
|
---|
| 1707 | // putenv de Linux ne declare pas la variable char *string const
|
---|
| 1708 | // On ne doit meme pas utiliser une variable automatique
|
---|
| 1709 | // J'alloue donc un nouveau tableau - mais qui va le liberer ?
|
---|
[2867] | 1710 | // Idem AIX , Reza Dec 2005
|
---|
[2887] | 1711 | // Pb apparu avec g++ 4 sur darwin (Mac) - Jan 2006
|
---|
| 1712 | // Je fais copie pour tout le monde
|
---|
[2532] | 1713 | char* bev = new char[pev.size()+1];
|
---|
| 1714 | strcpy(bev, pev.c_str());
|
---|
| 1715 | if (putenv(bev) == 0) return true;
|
---|
[2887] | 1716 | // else
|
---|
| 1717 | // if (putenv(pev.c_str()) == 0) return true;
|
---|
| 1718 | // endif
|
---|
[2518] | 1719 | else return false;
|
---|
[2483] | 1720 | }
|
---|
| 1721 |
|
---|
| 1722 | /* --Methode-- */
|
---|
| 1723 | bool Commander::DeleteVarEnv(string const & vn)
|
---|
| 1724 | {
|
---|
[2518] | 1725 | // cout << " Commander::DeleteVarEnv() Not available ! " << endl;
|
---|
[2483] | 1726 | return false;
|
---|
| 1727 | }
|
---|
| 1728 |
|
---|
| 1729 | /* --Methode-- */
|
---|
| 1730 | void Commander::ListVarEnv()
|
---|
| 1731 | {
|
---|
| 1732 | cout << " Commander::ListVarEnv() Not available ! " << endl;
|
---|
| 1733 | return;
|
---|
| 1734 | }
|
---|
| 1735 |
|
---|
| 1736 |
|
---|
| 1737 | /* --Methode-- */
|
---|
[2446] | 1738 | string Commander::GetTmpDir()
|
---|
| 1739 | {
|
---|
| 1740 | return("/tmp");
|
---|
| 1741 | }
|
---|
| 1742 |
|
---|
| 1743 | /* --Methode-- */
|
---|
| 1744 | void Commander::SetCurrentPrompt(const char* pr)
|
---|
| 1745 | {
|
---|
| 1746 | curprompt = pr;
|
---|
| 1747 | }
|
---|
| 1748 |
|
---|
| 1749 | /* --Methode-- */
|
---|
| 1750 | void Commander::ShowMessage(const char * msg, int att)
|
---|
| 1751 | {
|
---|
| 1752 | cout << msg ;
|
---|
| 1753 | }
|
---|
| 1754 |
|
---|
| 1755 |
|
---|
| 1756 |
|
---|
| 1757 | /* --Methode-- */
|
---|
| 1758 | int Commander::EvaluateTest(vector<string> & args, string & line, bool & res)
|
---|
| 1759 | {
|
---|
| 1760 | res = true;
|
---|
| 1761 | if ((args.size() != 6) || (args[5] != "then") ||
|
---|
| 1762 | (args[0] != "(") || (args[4] != ")") ) return(1);
|
---|
| 1763 | if (args[2] == "==") res = (args[1] == args[3]);
|
---|
| 1764 | else if (args[2] == "!=") res = (args[1] != args[3]);
|
---|
| 1765 | else if (args[2] == "<")
|
---|
| 1766 | res = (atof(args[1].c_str()) < atof(args[3].c_str()));
|
---|
| 1767 | else if (args[2] == ">")
|
---|
| 1768 | res = (atof(args[1].c_str()) > atof(args[3].c_str()));
|
---|
| 1769 | else if (args[2] == "<=")
|
---|
| 1770 | res = (atof(args[1].c_str()) <= atof(args[3].c_str()));
|
---|
| 1771 | else if (args[2] == ">=")
|
---|
| 1772 | res = (atof(args[1].c_str()) >= atof(args[3].c_str()));
|
---|
| 1773 | else return(2);
|
---|
| 1774 | return(0);
|
---|
| 1775 | }
|
---|
| 1776 |
|
---|
| 1777 |
|
---|
| 1778 |
|
---|
| 1779 | /* --Methode-- */
|
---|
[4034] | 1780 | void Commander::PushStack(vector<string>* pargs)
|
---|
[2446] | 1781 | {
|
---|
[4034] | 1782 | // We push the argument list (args) on the stack if not NULL
|
---|
| 1783 | if (pargs != NULL) ArgsStack.push(*pargs);
|
---|
[2446] | 1784 | // We push also CommanderBloc and testresult on the stack
|
---|
| 1785 | CmdBlks.push(NULL);
|
---|
[4034] | 1786 | if (TestsStack.size()>0) {
|
---|
| 1787 | TestsStack.top().tstresit=tresit_;
|
---|
| 1788 | TestsStack.top().tstcurres=curtestresult_;
|
---|
| 1789 | }
|
---|
| 1790 | TstStatus tstat;
|
---|
| 1791 | tstat.tstcurres=curtestresult_;
|
---|
| 1792 | TestsStack.push(tstat);
|
---|
[2446] | 1793 | }
|
---|
| 1794 |
|
---|
| 1795 | /* --Methode-- */
|
---|
| 1796 | void Commander::PopStack(bool psta)
|
---|
| 1797 | {
|
---|
| 1798 | // We remove the argument list (args) from the stack
|
---|
| 1799 | if (psta) ArgsStack.pop();
|
---|
| 1800 | // And CommanderBloc and TestResult from the corresponding stacks
|
---|
| 1801 | CommanderBloc* curb = CmdBlks.top();
|
---|
| 1802 | while (curb != NULL) {
|
---|
| 1803 | CommanderBloc* parb = curb->Parent();
|
---|
| 1804 | delete curb; curb = parb;
|
---|
| 1805 | }
|
---|
| 1806 | CmdBlks.pop();
|
---|
| 1807 | TestsStack.pop();
|
---|
[4034] | 1808 | tresit_=TestsStack.top().tstresit;
|
---|
| 1809 | curtestresult_=TestsStack.top().tstcurres;
|
---|
[2446] | 1810 | }
|
---|
| 1811 |
|
---|
| 1812 | /* --Methode-- */
|
---|
| 1813 | int Commander::ExecuteCommandLine(string & kw, vector<string> & tokens, string & toks)
|
---|
| 1814 | {
|
---|
| 1815 | int rc = 0;
|
---|
| 1816 |
|
---|
| 1817 | // >>>>>>>>>>> Commande d'interpreteur
|
---|
| 1818 | if (kw == "help") {
|
---|
| 1819 | if (tokens.size() > 0) cout << GetUsage(tokens[0]) << endl;
|
---|
| 1820 | else {
|
---|
| 1821 | string kwh = "Commander";
|
---|
| 1822 | cout << GetUsage(kwh) << endl;
|
---|
| 1823 | }
|
---|
| 1824 | }
|
---|
[2518] | 1825 | else if (kw == "sleep") {
|
---|
| 1826 | if (tokens.size() < 1) {
|
---|
| 1827 | cout << "Commander::Interpret() Usage: sleep nsec " << endl;
|
---|
| 1828 | return(1);
|
---|
| 1829 | }
|
---|
| 1830 | int nsec = atoi(tokens[0].c_str());
|
---|
| 1831 | cout << "Commander::Interpret() sleep " << nsec << " seconds" << endl;
|
---|
| 1832 | sleep(nsec);
|
---|
| 1833 | }
|
---|
[2446] | 1834 |
|
---|
| 1835 | else if (kw == "set") {
|
---|
[2518] | 1836 | if (tokens.size() < 2) {
|
---|
| 1837 | cout << "Commander::Interpret() Usage: set varname value or set vecvar ( w1 w2 ... ) " << endl;
|
---|
| 1838 | return(1);
|
---|
| 1839 | }
|
---|
| 1840 |
|
---|
[2483] | 1841 | if (tokens.size() == 2)
|
---|
| 1842 | SetVariable(tokens[0], tokens[1]);
|
---|
| 1843 | else {
|
---|
[2518] | 1844 | if ( (tokens[1] != "(") || (tokens[tokens.size()-1] != ")") ) {
|
---|
| 1845 | cout << "Commander::Interpret() Usage: set vecvar ( w1 w2 ... ) " << endl;
|
---|
| 1846 | return(1);
|
---|
| 1847 | }
|
---|
| 1848 | string vname = tokens[0];
|
---|
| 1849 | vector<string>::iterator vit;
|
---|
| 1850 | vit = tokens.begin(); tokens.erase(vit);
|
---|
| 1851 | vit = tokens.begin(); tokens.erase(vit);
|
---|
| 1852 | tokens.pop_back();
|
---|
| 1853 | SetVar(vname, tokens);
|
---|
[2446] | 1854 | }
|
---|
[2483] | 1855 | return 0;
|
---|
| 1856 | }
|
---|
[2518] | 1857 | else if (kw == "var2words") {
|
---|
| 1858 | if (tokens.size() < 2) {
|
---|
| 1859 | cout << "Commander::Interpret() Usage: var2words varname wordvarname [sep]" << endl;
|
---|
| 1860 | return(1);
|
---|
[2446] | 1861 | }
|
---|
[2518] | 1862 | char sep = ' ';
|
---|
| 1863 | if (tokens.size() > 2) sep = tokens[2][0];
|
---|
| 1864 | string vv;
|
---|
| 1865 | if (!GetVar(tokens[0], vv)) {
|
---|
| 1866 | cout << "Commander::Interpret() var2words/Error No variable with name " << tokens[0] << endl;
|
---|
| 1867 | return 2;
|
---|
| 1868 | }
|
---|
| 1869 | vector<string> vs;
|
---|
| 1870 | FillVStringFrString(vv, vs, sep);
|
---|
| 1871 | SetVar(tokens[1], vs);
|
---|
| 1872 | }
|
---|
[2446] | 1873 | else if (kw == "alias") {
|
---|
| 1874 | if (tokens.size() < 2) { cout << "Commander::Interpret() Usage: alias aliasname string" << endl; return(0); }
|
---|
| 1875 | if ((tokens[0].length() < 1) || !isalpha((int)tokens[0][0]) ) {
|
---|
| 1876 | cerr << "Commander::Interpret()/Error alias name should start with alphabetic" << endl;
|
---|
[2518] | 1877 | return(1);
|
---|
[2483] | 1878 | }
|
---|
[2446] | 1879 | string xx = tokens[1];
|
---|
[3581] | 1880 | for (unsigned int kk=2; kk<tokens.size(); kk++) xx += (' ' + tokens[kk]);
|
---|
[2446] | 1881 | mAliases[tokens[0]] = xx;
|
---|
[2483] | 1882 | }
|
---|
[2446] | 1883 |
|
---|
[2483] | 1884 | else if ( (kw == "unset") || (kw == "clearvar") ) {
|
---|
| 1885 | if (tokens.size() < 1) {
|
---|
| 1886 | cout << "Commander::Interpret() Usage: unset/clearvar varname" << endl;
|
---|
[2518] | 1887 | return(1);
|
---|
[2446] | 1888 | }
|
---|
[2483] | 1889 | else DeleteVar(tokens[0]);
|
---|
| 1890 | }
|
---|
| 1891 | // Evaluation d'expression en notation polonaise inverse
|
---|
[2512] | 1892 | else if (kw == "rpneval") {
|
---|
| 1893 | try {
|
---|
| 1894 | RPNExpressionEvaluator rpn(tokens, 1);
|
---|
| 1895 | double res = rpn.Value();
|
---|
[3577] | 1896 | string vv = cval_dble2str(res);
|
---|
[2512] | 1897 | SetVariable(tokens[0],vv);
|
---|
| 1898 | return 0;
|
---|
| 1899 | }
|
---|
| 1900 | catch (RPNExprException& rpnerr) {
|
---|
| 1901 | cerr << " rpneval: Syntax error - Msg=" << rpnerr.Msg()
|
---|
| 1902 | << " \n Line=" << toks << endl;
|
---|
| 1903 | return 98;
|
---|
| 1904 | }
|
---|
[2446] | 1905 | }
|
---|
| 1906 | else if (kw == "echo") {
|
---|
[3581] | 1907 | for (unsigned int ii=0; ii<tokens.size(); ii++)
|
---|
[2446] | 1908 | cout << tokens[ii] << " " ;
|
---|
| 1909 | cout << endl;
|
---|
| 1910 | }
|
---|
| 1911 | else if (kw == "echo2file") {
|
---|
| 1912 | if (tokens.size() < 1) {
|
---|
| 1913 | cout << "Commander::Interpret() Usage: echo2file filename [string ] " << endl;
|
---|
[2518] | 1914 | return(1);
|
---|
[2446] | 1915 | }
|
---|
| 1916 | ofstream ofs(tokens[0].c_str(), ios::app);
|
---|
[3581] | 1917 | for (unsigned int ii=1; ii<tokens.size(); ii++)
|
---|
[2446] | 1918 | ofs << tokens[ii] << " " ;
|
---|
| 1919 | ofs << endl;
|
---|
| 1920 | }
|
---|
| 1921 | else if (kw == "readstdin") {
|
---|
| 1922 | if (tokens.size() < 1) { cout << "Commander::Interpret() Usage: readstdin varname" << endl; return(0); }
|
---|
| 1923 | if ((tokens[0].length() < 1) || !isalpha((int)tokens[0][0]) ) {
|
---|
| 1924 | cerr << "Commander::Interpret()/Error Variable name should start with alphabetic" << endl;
|
---|
| 1925 | return(0);
|
---|
| 1926 | }
|
---|
| 1927 | ShowMessage(">>> Reading From StdIn \n", _MAGENTA_);
|
---|
| 1928 | cout << tokens[0] << " ? " << endl;
|
---|
| 1929 | SetVar(tokens[0], GetStringFrStdin(this) );
|
---|
| 1930 | }
|
---|
| 1931 |
|
---|
[2914] | 1932 | else if (kw == "listvars" || kw == "listvar") ListVar();
|
---|
[2446] | 1933 | else if (kw == "listalias") {
|
---|
| 1934 | cout << "Commander::Interpret() Alias List , AliasName = Value \n";
|
---|
| 1935 | CmdStrList::iterator it;
|
---|
| 1936 | for(it = mAliases.begin(); it != mAliases.end(); it++)
|
---|
| 1937 | cout << (*it).first << " = " << (*it).second << "\n";
|
---|
| 1938 | cout << endl;
|
---|
| 1939 | }
|
---|
| 1940 | else if (kw == "listcommands") {
|
---|
| 1941 | cout << "---- Commander::Interpret() Command List ----- \n";
|
---|
| 1942 | CmdExmap::iterator it;
|
---|
| 1943 | int kc = 0;
|
---|
| 1944 | for(it = cmdexmap.begin(); it != cmdexmap.end(); it++) {
|
---|
| 1945 | cout << (*it).first << " ";
|
---|
| 1946 | kc++;
|
---|
| 1947 | if (kc >= 5) { cout << "\n"; kc = 0; }
|
---|
| 1948 | }
|
---|
| 1949 | cout << endl;
|
---|
| 1950 | }
|
---|
| 1951 | else if (kw == "listscripts") {
|
---|
| 1952 | cout << "---- Commander::Interpret() Script List ----- \n";
|
---|
| 1953 | for(ScriptList::iterator sit = mScripts.begin();
|
---|
| 1954 | sit != mScripts.end(); sit++)
|
---|
| 1955 | cout << " Script: " << (*sit).second->Name() << " - "
|
---|
| 1956 | << (*sit).second->Comment() << endl;
|
---|
| 1957 | }
|
---|
| 1958 | else if (kw == "clearscript") {
|
---|
| 1959 | if (tokens.size() < 1) {
|
---|
| 1960 | cout << "Commander::Interpret() Usage: clearscript scriptname" << endl;
|
---|
| 1961 | return(0);
|
---|
| 1962 | }
|
---|
| 1963 | ScriptList::iterator sit = mScripts.find(tokens[0]);
|
---|
| 1964 | if (sit == mScripts.end()) {
|
---|
| 1965 | cout << "Commander::Interpret() No script with name" << tokens[0] << endl;
|
---|
| 1966 | return(0);
|
---|
| 1967 | }
|
---|
| 1968 | else {
|
---|
| 1969 | delete (*sit).second;
|
---|
| 1970 | mScripts.erase(sit);
|
---|
| 1971 | cout << "Commander::Interpret() script " << tokens[0] << " cleared" << endl;
|
---|
| 1972 | return(0);
|
---|
| 1973 | }
|
---|
| 1974 | }
|
---|
[2671] | 1975 | //---------------------------------------------
|
---|
| 1976 | //--- Commandes de gestion des threads ------
|
---|
| 1977 | //---------------------------------------------
|
---|
| 1978 | else if (kw == "thrlist") {
|
---|
| 1979 | ListThreads();
|
---|
| 1980 | return(0);
|
---|
| 1981 | }
|
---|
[2955] | 1982 | else if ( (kw == "killthr") || (kw == "cancelthr") ) {
|
---|
| 1983 | if (tokens.size() < 1) {
|
---|
| 1984 | cout << "Commander::Interpret() Usage: killthr/cancelthr thrid" << endl;
|
---|
| 1985 | return(0);
|
---|
| 1986 | }
|
---|
[2671] | 1987 | uint_8 id = atol(tokens[0].c_str());
|
---|
[2955] | 1988 | bool fgkill = false;
|
---|
| 1989 | if (kw == "killthr") fgkill = true;
|
---|
| 1990 | StopThr(id, fgkill);
|
---|
[2671] | 1991 | return (0);
|
---|
| 1992 | }
|
---|
| 1993 | else if (kw == "waitthr") {
|
---|
| 1994 | WaitThreads();
|
---|
| 1995 | return (0);
|
---|
| 1996 | }
|
---|
| 1997 | else if (kw == "cleanthrlist") {
|
---|
| 1998 | CleanThrList();
|
---|
| 1999 | return (0);
|
---|
| 2000 | }
|
---|
| 2001 |
|
---|
[2446] | 2002 | else if (kw == "traceon") { cout << "Commander::Interpret() -> Trace ON mode " << endl; trace = true; }
|
---|
| 2003 | else if (kw == "traceoff") { cout << "Commander::Interpret() -> Trace OFF mode " << endl; trace = false; }
|
---|
| 2004 | else if (kw == "timingon") {
|
---|
| 2005 | cout << "Commander::Interpret() -> Timing ON mode " << endl;
|
---|
| 2006 | if (gltimer) delete gltimer; gltimer = new Timer("PIA-CmdInterpreter "); timing = true;
|
---|
| 2007 | }
|
---|
| 2008 | else if (kw == "timingoff") {
|
---|
| 2009 | cout << "Commander::Interpret() -> Timing OFF mode " << endl;
|
---|
| 2010 | if (gltimer) delete gltimer; gltimer = NULL; timing = false;
|
---|
| 2011 | }
|
---|
[4063] | 2012 | else if (kw == "varcexpon")
|
---|
| 2013 | { cout << "Commander::Interpret() -> Activating variable name decoding in expression evaluation " << endl; varcexp = true; }
|
---|
| 2014 | else if (kw == "varcexpoff")
|
---|
| 2015 | { cout << "Commander::Interpret() -> Deactivating variable name decoding in expression evaluation " << endl; varcexp = false; }
|
---|
[2446] | 2016 | else if (kw == "exec") {
|
---|
| 2017 | if (tokens.size() < 1) { cout << "Commander::Interpret() Usage: exec filename" << endl; return(0); }
|
---|
| 2018 | ExecFile(tokens[0], tokens);
|
---|
| 2019 | }
|
---|
| 2020 | else if (kw == "autoiniranf") {
|
---|
[3615] | 2021 | AutoInitRand(1);
|
---|
[2446] | 2022 | return(0);
|
---|
| 2023 | }
|
---|
| 2024 | else if (kw == "shell") {
|
---|
| 2025 | if (tokens.size() < 1) { cout << "Commander::Interpret() Usage: shell cmdline" << endl; return(0); }
|
---|
| 2026 | string cmd;
|
---|
[3581] | 2027 | for (unsigned int ii=0; ii<tokens.size(); ii++)
|
---|
[2446] | 2028 | cmd += (tokens[ii] + ' ');
|
---|
| 2029 | system(cmd.c_str());
|
---|
| 2030 | }
|
---|
| 2031 | else if (kw == "cshell") {
|
---|
| 2032 | if(tokens.size()<1) {cout<<"Commander::Interpret() Usage: cshell cmdline"<<endl; return(0);}
|
---|
| 2033 | string cmd="";
|
---|
[3581] | 2034 | for(unsigned int ii=0;ii<tokens.size();ii++) cmd+=(tokens[ii]+' ');
|
---|
[2446] | 2035 | CShellExecute(cmd);
|
---|
| 2036 | }
|
---|
| 2037 |
|
---|
| 2038 | // Execution d'une commande enregistree
|
---|
| 2039 | else rc = ExecuteCommand(kw, tokens, toks);
|
---|
| 2040 |
|
---|
| 2041 | if (timing) gltimer->Split();
|
---|
| 2042 | return(rc);
|
---|
| 2043 | }
|
---|
| 2044 |
|
---|
| 2045 | /* --Methode-- */
|
---|
| 2046 | int Commander::ParseLineExecute(string& line, bool qw)
|
---|
| 2047 | // Si qw == true, on decoupe entre '' ou "" ou espaces
|
---|
| 2048 | {
|
---|
| 2049 | vector<string> tokens;
|
---|
[2518] | 2050 | vector<bool> qottoks;
|
---|
[2446] | 2051 | string kw, toks;
|
---|
| 2052 | if (line.length() < 1) return(0);
|
---|
[2518] | 2053 | LineToWords(line, kw, tokens, qottoks, toks, qw);
|
---|
[2446] | 2054 | return(ExecuteCommand(kw, tokens, toks));
|
---|
| 2055 | }
|
---|
| 2056 |
|
---|
| 2057 | /* --Methode-- */
|
---|
| 2058 | int Commander::ExecuteCommand(string& keyw, vector<string>& args, string& toks)
|
---|
| 2059 | {
|
---|
| 2060 | int rc = -1;
|
---|
| 2061 | CmdExmap::iterator it = cmdexmap.find(keyw);
|
---|
| 2062 | if (it == cmdexmap.end()) cout << "No such command : " << keyw << " ! " << endl;
|
---|
| 2063 | else {
|
---|
[2671] | 2064 | if ((*it).second.cex) {
|
---|
| 2065 | // Doit-on l'executer sous forme de thread separe ?
|
---|
[2956] | 2066 | if ( (args.size()>0) && (args[args.size()-1] == "&") ) {
|
---|
| 2067 | if ((*it).second.cex->IsThreadable(keyw) ) {
|
---|
| 2068 | ThrId++;
|
---|
| 2069 | CommandExeThr * thr =
|
---|
| 2070 | new CommandExeThr(ThrId, (*it).second.cex, keyw, args, toks);
|
---|
| 2071 | CmdThrExeList.push_back(thr);
|
---|
| 2072 | cout << " Commander::ExecuteCommand() : Thread execution of command " << keyw << endl;
|
---|
| 2073 | thr->start();
|
---|
| 2074 | if (CmdThrExeList.size() > 5) CleanThrList();
|
---|
| 2075 | rc = 0;
|
---|
| 2076 | }
|
---|
| 2077 | else {
|
---|
| 2078 | args.erase(args.end()-1);
|
---|
| 2079 | for(size_t k=toks.size()-1; k>0; k--)
|
---|
| 2080 | if (toks[k] == '&') { toks[k] = ' '; break; }
|
---|
| 2081 | cout << " Commander::ExecuteCommand() : Thread execution NOT available for" << keyw << endl;
|
---|
| 2082 | rc = (*it).second.cex->Execute(keyw, args, toks);
|
---|
| 2083 | }
|
---|
[2671] | 2084 | }
|
---|
[2943] | 2085 | else rc = (*it).second.cex->Execute(keyw, args, toks);
|
---|
[2671] | 2086 | }
|
---|
[2446] | 2087 | else cout << "Dont know how to execute " << keyw << " ? " << endl;
|
---|
[2943] | 2088 | }
|
---|
[2446] | 2089 | return(rc);
|
---|
| 2090 | }
|
---|
| 2091 |
|
---|
| 2092 | /* --Methode-- */
|
---|
| 2093 | int Commander::ExecFile(string& file, vector<string>& args)
|
---|
| 2094 | {
|
---|
[2856] | 2095 | char line_buff[1024];
|
---|
[2446] | 2096 | FILE *fip;
|
---|
[2518] | 2097 | int rcc = 0;
|
---|
[2446] | 2098 | if ( (fip = fopen(file.c_str(),"r")) == NULL ) {
|
---|
| 2099 | if (file.find('.') >= file.length()) {
|
---|
| 2100 | cout << "Commander::Exec(): Error opening file " << file << endl;
|
---|
| 2101 | file += ".pic";
|
---|
| 2102 | cout << " Trying file " << file << endl;
|
---|
| 2103 | fip = fopen(file.c_str(),"r");
|
---|
| 2104 | }
|
---|
| 2105 | }
|
---|
| 2106 |
|
---|
| 2107 | if(fip == NULL) {
|
---|
| 2108 | cerr << "Commander::Exec() Error opening file " << file << endl;
|
---|
| 2109 | hist << "##! Commander::Exec() Error opening file " << file << endl;
|
---|
| 2110 | return(0);
|
---|
| 2111 | }
|
---|
| 2112 |
|
---|
| 2113 | // hist << "### Executing commands from " << file << endl;
|
---|
| 2114 | PushStack(args);
|
---|
| 2115 | if (trace) {
|
---|
| 2116 | ShowMessage("### Executing commands from ", _MAGENTA_);
|
---|
| 2117 | ShowMessage(file.c_str(), _MAGENTA_);
|
---|
| 2118 | ShowMessage("\n", _MAGENTA_);
|
---|
| 2119 | }
|
---|
| 2120 |
|
---|
| 2121 | bool ohv = histon;
|
---|
| 2122 | histon = false;
|
---|
[2856] | 2123 | while (fgets(line_buff,1023,fip) != NULL)
|
---|
[2446] | 2124 | {
|
---|
| 2125 | if (trace) ShowMessage(line_buff, _MAGENTA_);
|
---|
| 2126 | line_buff[strlen(line_buff)-1] = '\0'; /* LF/CR de la fin */
|
---|
| 2127 | string line(line_buff);
|
---|
[2518] | 2128 | rcc = Interpret(line);
|
---|
| 2129 | if ((rcc == CMD_RETURN_RC) || (rcc == CMD_BREAKEXE_RC)) break;
|
---|
[2446] | 2130 | }
|
---|
| 2131 | histon = ohv;
|
---|
| 2132 |
|
---|
| 2133 | // hist << "### End of Exec( " << file << " ) " << endl;
|
---|
| 2134 | if (trace) {
|
---|
| 2135 | ShowMessage("### End of Exec( ", _MAGENTA_);
|
---|
| 2136 | ShowMessage(file.c_str(), _MAGENTA_);
|
---|
| 2137 | ShowMessage(" ) \n", _MAGENTA_);
|
---|
| 2138 | }
|
---|
| 2139 |
|
---|
| 2140 | PopStack(true);
|
---|
| 2141 |
|
---|
| 2142 | return(0);
|
---|
| 2143 | }
|
---|
| 2144 |
|
---|
| 2145 | /* --Methode-- */
|
---|
[2671] | 2146 | void Commander::ListThreads()
|
---|
| 2147 | {
|
---|
[2955] | 2148 | cout << "--- Commander::ListThreads()- command execution threads NThread="
|
---|
| 2149 | << CmdThrExeList.size() << " ---" << endl;
|
---|
[2671] | 2150 | for(list<CommandExeThr *>::iterator tit = CmdThrExeList.begin();
|
---|
| 2151 | tit != CmdThrExeList.end(); tit++) {
|
---|
| 2152 | cout << "Id=" << (*tit)->Id();
|
---|
[2955] | 2153 | if ( (*tit)->IfRunning() ) cout << " Executing";
|
---|
| 2154 | else if ( (*tit)->IfDone() ) cout << " Finished , Rc= " << (*tit)->getRC();
|
---|
| 2155 | else cout << " Stopped/Canceled";
|
---|
| 2156 | cout << " (Cmd= " << (*tit)->Keyword() << " " << (*tit)->Tokens().substr(0,35);
|
---|
| 2157 | if ((*tit)->Tokens().length() > 35) cout << "... )" << endl;
|
---|
| 2158 | else cout << " )" << endl;
|
---|
[2671] | 2159 | }
|
---|
| 2160 | }
|
---|
| 2161 | /* --Methode-- */
|
---|
[2955] | 2162 | void Commander::StopThr(uint_8 id, bool fgkill)
|
---|
[2671] | 2163 | {
|
---|
| 2164 | for(list<CommandExeThr *>::iterator tit = CmdThrExeList.begin();
|
---|
| 2165 | tit != CmdThrExeList.end(); tit++) {
|
---|
[2955] | 2166 | if ( ((*tit)->Id() == id) && ((*tit)->IfRunning()) ) {
|
---|
| 2167 | if (fgkill) {
|
---|
| 2168 | (*tit)->kill(SIGUSR1);
|
---|
| 2169 | cout << "Commander::StopThr() Send signal SIGUSR1 to Thread Id= " << id << endl;
|
---|
| 2170 | }
|
---|
| 2171 | else {
|
---|
| 2172 | (*tit)->cancel();
|
---|
| 2173 | cout << "Commander::StopThr() Canceling Thread Id= " << id << endl;
|
---|
| 2174 | }
|
---|
[2671] | 2175 | return;
|
---|
| 2176 | }
|
---|
| 2177 | }
|
---|
[2943] | 2178 | cout << "Commander::StopThr()/Error: No active thread with Id= " << id << endl;
|
---|
[2671] | 2179 | }
|
---|
| 2180 |
|
---|
| 2181 | /* --Methode-- */
|
---|
| 2182 | void Commander::CleanThrList()
|
---|
| 2183 | {
|
---|
| 2184 | cout << "---- Commander::CleanThrList() Cleaning thrlist ----- \n";
|
---|
| 2185 | list<CommandExeThr *> thrcopie;
|
---|
[2955] | 2186 | int ncl = 0;
|
---|
[2671] | 2187 | for(list<CommandExeThr *>::iterator tit = CmdThrExeList.begin();
|
---|
| 2188 | tit != CmdThrExeList.end(); tit++) {
|
---|
[2955] | 2189 | if ( ((*tit)->IfEnded() || (*tit)->IfStopped()) && (ncl < 3) ) {
|
---|
[2671] | 2190 | cout << " Thread Id= " << (*tit)->Id() << " rc= " << (*tit)->getRC() << " Cleaned" << endl;
|
---|
| 2191 | delete (*tit);
|
---|
[2955] | 2192 | ncl++;
|
---|
[2671] | 2193 | }
|
---|
| 2194 | else thrcopie.push_back((*tit));
|
---|
| 2195 | }
|
---|
| 2196 | CmdThrExeList = thrcopie;
|
---|
| 2197 | cout << " ... " << CmdThrExeList.size() << " threads still active " << endl;
|
---|
| 2198 | }
|
---|
| 2199 |
|
---|
| 2200 | /* --Methode-- */
|
---|
| 2201 | void Commander::WaitThreads()
|
---|
| 2202 | {
|
---|
| 2203 | cout << "---- Commander::WaitThreads() Wait/Join command execution threads - NThread="
|
---|
| 2204 | << CmdThrExeList.size() << " ----- " << endl;
|
---|
| 2205 | for(list<CommandExeThr *>::iterator tit = CmdThrExeList.begin();
|
---|
| 2206 | tit != CmdThrExeList.end(); tit++) {
|
---|
| 2207 | try {
|
---|
| 2208 | if (! (*tit)->IfDone()) (*tit)->join();
|
---|
| 2209 | }
|
---|
| 2210 | catch (std::exception & e) {
|
---|
| 2211 | cout << " Commander::WaitThreads()/Exception msg= " << e.what() << endl;
|
---|
| 2212 | }
|
---|
| 2213 | cout << " Joined thread Id= " << (*tit)->Id() << " rc= " << (*tit)->getRC() << endl;
|
---|
| 2214 | delete (*tit);
|
---|
| 2215 | }
|
---|
| 2216 | CmdThrExeList.erase(CmdThrExeList.begin(), CmdThrExeList.end());
|
---|
| 2217 | }
|
---|
| 2218 |
|
---|
| 2219 | /* --Methode-- */
|
---|
[2446] | 2220 | int Commander::CShellExecute(string cmd)
|
---|
| 2221 | {
|
---|
| 2222 | if(cmd.size()<=0) return -1;
|
---|
| 2223 |
|
---|
| 2224 | string fname = GetTmpDir(); fname += "cshell_exec_pia.csh";
|
---|
| 2225 |
|
---|
| 2226 | string cmdrm = "rm -f " + fname;
|
---|
| 2227 | system(cmdrm.c_str());
|
---|
| 2228 |
|
---|
| 2229 | FILE *fip = fopen(fname.c_str(),"w");
|
---|
| 2230 | if(fip==NULL) {
|
---|
| 2231 | cout << "Commander/CShellExecute_Error: fopen("<<fname<<") failed"<<endl;
|
---|
| 2232 | return -2;
|
---|
| 2233 | }
|
---|
| 2234 | fprintf(fip,"#!/bin/csh\n\n");
|
---|
| 2235 | fprintf(fip,"%s\n",cmd.c_str());
|
---|
| 2236 | fprintf(fip,"\nexit 0\n");
|
---|
| 2237 | fclose(fip);
|
---|
| 2238 |
|
---|
| 2239 | cmd = "csh "; cmd += fname;
|
---|
| 2240 | system(cmd.c_str());
|
---|
| 2241 |
|
---|
| 2242 | system(cmdrm.c_str());
|
---|
| 2243 |
|
---|
| 2244 | return 0;
|
---|
| 2245 | }
|
---|
| 2246 |
|
---|
| 2247 | static string* videstr = NULL;
|
---|
| 2248 | /* --Methode-- */
|
---|
| 2249 | string& Commander::GetUsage(const string& kw)
|
---|
| 2250 | {
|
---|
| 2251 | bool fndok = false;
|
---|
| 2252 | CmdExmap::iterator it = cmdexmap.find(kw);
|
---|
| 2253 | if (it == cmdexmap.end()) {
|
---|
| 2254 | it = helpexmap.find(kw);
|
---|
| 2255 | if (it != helpexmap.end()) fndok = true;
|
---|
| 2256 | }
|
---|
| 2257 | else fndok = true;
|
---|
| 2258 | if (fndok) return( (*it).second.us );
|
---|
| 2259 | // Keyword pas trouve
|
---|
| 2260 | if (videstr == NULL) videstr = new string("");
|
---|
| 2261 | *videstr = "Nothing known about " + kw + " ?? ";
|
---|
| 2262 | return(*videstr);
|
---|
| 2263 |
|
---|
| 2264 | }
|
---|
| 2265 |
|
---|
| 2266 |
|
---|
| 2267 | /* Les definitions suivantes doivent se trouver ds l'en-tete du fichier LaTeX
|
---|
| 2268 | \newcommand{\piacommand}[1]{
|
---|
| 2269 | \framebox{\bf \Large #1 } \index{#1} % (Command)
|
---|
| 2270 | }
|
---|
| 2271 |
|
---|
| 2272 | \newcommand{\piahelpitem}[1]{
|
---|
| 2273 | \framebox{\bf \Large #1 } \index{#1} (Help item)
|
---|
| 2274 | }
|
---|
| 2275 |
|
---|
| 2276 | \newcommand{\myppageref}[1]{ (p. \pageref{#1} ) }
|
---|
| 2277 | */
|
---|
| 2278 |
|
---|
| 2279 | // Fonction qui remplace tout caractere non alphanumerique en Z
|
---|
| 2280 | static void check_latex_reflabel(string & prl)
|
---|
| 2281 | {
|
---|
[3581] | 2282 | for(unsigned int k=0; k<prl.length(); k++)
|
---|
[2446] | 2283 | if (! isalnum(prl[k]) ) prl[k] = 'Z';
|
---|
| 2284 | }
|
---|
[2466] | 2285 |
|
---|
[2446] | 2286 | // Fonction qui remplace _ en \_
|
---|
| 2287 | static string check_latex_underscore(string const & mot)
|
---|
| 2288 | {
|
---|
| 2289 | string rs;
|
---|
[3581] | 2290 | for(unsigned int k=0; k<mot.length(); k++) {
|
---|
[2466] | 2291 | if (mot[k] == '_') rs += "\\_";
|
---|
[2446] | 2292 | else rs += mot[k];
|
---|
| 2293 | }
|
---|
| 2294 | return rs;
|
---|
| 2295 | }
|
---|
| 2296 |
|
---|
| 2297 | /* --Methode-- */
|
---|
[2943] | 2298 | /*!
|
---|
| 2299 | \brief Produces a LaTeX file containing the registered command helps
|
---|
| 2300 | The file \b fname is created and can be inserted into a LaTeX document
|
---|
| 2301 | in order to produce the list of registered commands and corresponding description
|
---|
| 2302 | texts.
|
---|
| 2303 | The LaTeX file should contain the following definitions:
|
---|
| 2304 | \verbatim
|
---|
| 2305 | \newcommand{\piacommand}[1]{
|
---|
| 2306 | \framebox{\bf \Large #1 } \index{#1} % (Command)
|
---|
| 2307 | }
|
---|
| 2308 |
|
---|
| 2309 | \newcommand{\piahelpitem}[1]{
|
---|
| 2310 | \framebox{\bf \Large #1 } \index{#1} (Help item)
|
---|
| 2311 | }
|
---|
| 2312 |
|
---|
| 2313 | \newcommand{\myppageref}[1]{ (p. \pageref{#1} ) }
|
---|
| 2314 |
|
---|
| 2315 | \endverbatim
|
---|
| 2316 | */
|
---|
[2446] | 2317 | void Commander::HelptoLaTeX(string const & fname)
|
---|
| 2318 | {
|
---|
| 2319 | FILE *fip;
|
---|
| 2320 | if ((fip = fopen(fname.c_str(), "w")) == NULL) {
|
---|
| 2321 | cout << "Commander::HelptoLaTex_Error: fopen( " << fname << endl;
|
---|
| 2322 | return;
|
---|
| 2323 | }
|
---|
| 2324 |
|
---|
| 2325 | fputs("% ----- Liste des groupes de Help ----- \n",fip);
|
---|
| 2326 | fputs("List of {\\bf piapp} on-line Help groups: \n", fip);
|
---|
| 2327 | fputs("\\begin{itemize} \n",fip);
|
---|
| 2328 | string prl;
|
---|
| 2329 | string mol;
|
---|
| 2330 | CmdHGroup::iterator it;
|
---|
| 2331 | for(it = cmdhgrp.begin(); it != cmdhgrp.end(); it++) {
|
---|
| 2332 | if ((*it).first == "All") continue;
|
---|
| 2333 | prl = (*it).first; check_latex_reflabel(prl);
|
---|
| 2334 | mol = check_latex_underscore((*it).first);
|
---|
| 2335 | fprintf(fip,"\\item {\\bf %s } (p. \\pageref{%s}) \n",
|
---|
| 2336 | mol.c_str(), prl.c_str());
|
---|
| 2337 | }
|
---|
| 2338 |
|
---|
| 2339 | fputs("\\end{itemize} \n",fip);
|
---|
| 2340 |
|
---|
| 2341 | fputs("\\vspace*{10mm} \n",fip);
|
---|
| 2342 |
|
---|
| 2343 | CmdExmap::iterator ite;
|
---|
| 2344 | fputs("% ----- Liste de toutes les commandes et help item ----- \n",fip);
|
---|
| 2345 | fputs("\\vspace{5mm} \n",fip);
|
---|
| 2346 | // fputs("\\begin{table}[h!] \n", fip);
|
---|
| 2347 | fputs("\\begin{center} \n ", fip);
|
---|
| 2348 | fputs("\\rule{2cm}{1mm} List of {\\bf piapp} Help items \\rule{2cm}{1mm} \\\\ \n", fip);
|
---|
| 2349 | fputs("\\vspace{3mm} \n",fip);
|
---|
| 2350 | fputs("\\begin{tabular}{llllll} \n", fip);
|
---|
| 2351 | int kt = 0;
|
---|
| 2352 | for(ite = helpexmap.begin(); ite != helpexmap.end(); ite++) {
|
---|
| 2353 | prl = (*ite).first; check_latex_reflabel(prl);
|
---|
| 2354 | mol = check_latex_underscore((*ite).first);
|
---|
| 2355 | fprintf(fip,"%s & p. \\pageref{%s} ", mol.c_str(), prl.c_str() );
|
---|
| 2356 | kt++;
|
---|
| 2357 | if (kt < 3) fputs(" & ", fip);
|
---|
| 2358 | else { fputs(" \\\\ \n", fip); kt = 0; }
|
---|
| 2359 | }
|
---|
| 2360 | if (kt == 1) fputs(" & & & \\\\ \n", fip);
|
---|
| 2361 | else if (kt == 2) fputs(" & \\\\ \n", fip);
|
---|
| 2362 | fputs("\\end{tabular} \n", fip);
|
---|
| 2363 | fputs("\\end{center} \n", fip);
|
---|
| 2364 | //fputs("\\end{table} \n", fip);
|
---|
| 2365 | fputs("\\newpage \n",fip);
|
---|
| 2366 |
|
---|
| 2367 | int gid;
|
---|
| 2368 | for(it = cmdhgrp.begin(); it != cmdhgrp.end(); it++) {
|
---|
[2466] | 2369 | gid = (*it).second.gid;
|
---|
[2446] | 2370 | if (gid == 0) continue;
|
---|
| 2371 | // fputs("\\begin{table}[h!] \n",fip);
|
---|
| 2372 | fputs("\\vspace{6mm} \n",fip);
|
---|
| 2373 | fputs("\\begin{center} \n ", fip);
|
---|
[2466] | 2374 | fprintf(fip, "\\rule{2cm}{0.5mm} \\makebox[60mm]{{ \\bf %s } help group} \\rule{2cm}{0.5mm} \\\\ \n",
|
---|
[2446] | 2375 | (*it).first.c_str());
|
---|
| 2376 | fputs("\\vspace{3mm} \n",fip);
|
---|
| 2377 | fputs("\\begin{tabular}{llllll} \n", fip);
|
---|
| 2378 | kt = 0;
|
---|
| 2379 | for(ite = helpexmap.begin(); ite != helpexmap.end(); ite++) {
|
---|
| 2380 | if ((*ite).second.group != gid) continue;
|
---|
| 2381 | prl = (*ite).first; check_latex_reflabel(prl);
|
---|
| 2382 | mol = check_latex_underscore((*ite).first);
|
---|
| 2383 | fprintf(fip,"%s & p. \\pageref{%s} ", mol.c_str(), prl.c_str() );
|
---|
| 2384 | kt++;
|
---|
| 2385 | if (kt < 3) fputs(" & ", fip);
|
---|
| 2386 | else { fputs(" \\\\ \n", fip); kt = 0; }
|
---|
| 2387 | }
|
---|
| 2388 | for(ite = cmdexmap.begin(); ite != cmdexmap.end(); ite++) {
|
---|
| 2389 | if ((*ite).second.group != gid) continue;
|
---|
| 2390 | prl = (*ite).first; check_latex_reflabel(prl);
|
---|
| 2391 | mol = check_latex_underscore((*ite).first);
|
---|
| 2392 | fprintf(fip,"%s & p. \\pageref{%s} ", mol.c_str(), prl.c_str() );
|
---|
| 2393 | kt++;
|
---|
| 2394 | if (kt < 3) fputs(" & ", fip);
|
---|
| 2395 | else { fputs(" \\\\ \n", fip); kt = 0; }
|
---|
| 2396 | }
|
---|
| 2397 | if (kt == 1) fputs(" & & & \\\\ \n", fip);
|
---|
| 2398 | else if (kt == 2) fputs(" & \\\\ \n", fip);
|
---|
| 2399 | fputs("\\end{tabular} \n", fip);
|
---|
| 2400 | fputs("\\end{center} \n", fip);
|
---|
| 2401 | // fputs("\\end{table} \n",fip);
|
---|
| 2402 | // fputs("\\vspace{5mm} \n",fip);
|
---|
| 2403 | }
|
---|
| 2404 | // fputs("\\newline \n",fip);
|
---|
| 2405 |
|
---|
| 2406 | fputs("% ----- Liste des commandes dans chaque groupe ----- \n",fip);
|
---|
| 2407 | fputs("\\newpage \n",fip);
|
---|
| 2408 |
|
---|
| 2409 | for(it = cmdhgrp.begin(); it != cmdhgrp.end(); it++) {
|
---|
[2466] | 2410 | gid = (*it).second.gid;
|
---|
[2446] | 2411 | if (gid == 0) continue;
|
---|
| 2412 | prl = (*it).first; check_latex_reflabel(prl);
|
---|
| 2413 | fprintf(fip,"\\subsection{%s} \\label{%s} \n",
|
---|
| 2414 | (*it).first.c_str(), prl.c_str());
|
---|
[2466] | 2415 | if ((*it).second.desc.length() > 0)
|
---|
| 2416 | fprintf(fip,"%s \n \\\\[2mm] ", (*it).second.desc.c_str());
|
---|
[2446] | 2417 | fprintf(fip,"\\noindent \n");
|
---|
| 2418 | for(ite = helpexmap.begin(); ite != helpexmap.end(); ite++) {
|
---|
| 2419 | if ((*ite).second.group != gid) continue;
|
---|
| 2420 | prl = (*ite).first; check_latex_reflabel(prl);
|
---|
| 2421 | mol = check_latex_underscore((*ite).first);
|
---|
| 2422 | fprintf(fip,"\\piahelpitem{%s} \\label{%s} \n",
|
---|
| 2423 | mol.c_str(), prl.c_str());
|
---|
| 2424 | fputs("\\begin{verbatim} \n",fip);
|
---|
| 2425 | fprintf(fip,"%s\n", (*ite).second.us.c_str());
|
---|
| 2426 | fputs("\\end{verbatim} \n",fip);
|
---|
| 2427 | }
|
---|
| 2428 | for(ite = cmdexmap.begin(); ite != cmdexmap.end(); ite++) {
|
---|
| 2429 | if ((*ite).second.group != gid) continue;
|
---|
| 2430 | prl = (*ite).first; check_latex_reflabel(prl);
|
---|
| 2431 | mol = check_latex_underscore((*ite).first);
|
---|
| 2432 | fprintf(fip,"\\piacommand{%s} \\label{%s} \n",
|
---|
| 2433 | mol.c_str(), prl.c_str());
|
---|
| 2434 | fputs("\\begin{verbatim} \n",fip);
|
---|
| 2435 | fprintf(fip,"%s\n", (*ite).second.us.c_str());
|
---|
| 2436 | fputs("\\end{verbatim} \n",fip);
|
---|
| 2437 | }
|
---|
| 2438 | }
|
---|
| 2439 |
|
---|
| 2440 | fclose(fip);
|
---|
[2466] | 2441 | cout << " Commander::HelptoLaTeX() - LaTeX format help written to file " << fname << endl;
|
---|
| 2442 |
|
---|
[2446] | 2443 | return;
|
---|
| 2444 | }
|
---|
| 2445 |
|
---|
| 2446 |
|
---|
[2483] | 2447 | } // End of namespace SOPHYA
|
---|
[2446] | 2448 |
|
---|