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