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