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