| 1 | #include "piacmd.h"
|
|---|
| 2 | #include <stdio.h>
|
|---|
| 3 | #include <stdlib.h>
|
|---|
| 4 | #include <ctype.h>
|
|---|
| 5 | #include <math.h>
|
|---|
| 6 |
|
|---|
| 7 | #include "basexecut.h"
|
|---|
| 8 |
|
|---|
| 9 | #include "pdlmgr.h"
|
|---|
| 10 | #include "ctimer.h"
|
|---|
| 11 | #include "strutil.h"
|
|---|
| 12 | #include "strutilxx.h"
|
|---|
| 13 | // #include "dlftypes.h"
|
|---|
| 14 |
|
|---|
| 15 | #include "pistdimgapp.h"
|
|---|
| 16 | #include "nobjmgr.h"
|
|---|
| 17 | #include "piafitting.h"
|
|---|
| 18 | #include "pawexecut.h"
|
|---|
| 19 | #include "cxxexecutor.h"
|
|---|
| 20 | #include "cxxexecwin.h"
|
|---|
| 21 | #include "contmodex.h"
|
|---|
| 22 | #include "flowmodex.h"
|
|---|
| 23 |
|
|---|
| 24 | #include PISTDWDG_H
|
|---|
| 25 | #include PILIST_H
|
|---|
| 26 |
|
|---|
| 27 | // ------------------------------------------------------------
|
|---|
| 28 | // Gestion d'une fenetre d'aide interactive
|
|---|
| 29 | // Classe PIAHelpWind
|
|---|
| 30 | // ------------------------------------------------------------
|
|---|
| 31 |
|
|---|
| 32 | class PIAHelpWind : public PIWindow {
|
|---|
| 33 | public :
|
|---|
| 34 | PIAHelpWind(PIStdImgApp* par, PIACmd* piacmd);
|
|---|
| 35 | virtual ~PIAHelpWind();
|
|---|
| 36 | virtual void Show();
|
|---|
| 37 | virtual void Process(PIMessage msg, PIMsgHandler* sender, void* data=NULL);
|
|---|
| 38 | inline void AddHelpGroup(const char * hgrp, int gid)
|
|---|
| 39 | { hgrpom->AppendItem(hgrp, 20000+gid); }
|
|---|
| 40 | inline void ClearHelpList()
|
|---|
| 41 | { mNitem=0; hitemlist->DeleteAllItems(); }
|
|---|
| 42 | inline void AddHelpItem(const char * hitem)
|
|---|
| 43 | { mNitem++; hitemlist->AppendItem(hitem, 100+mNitem); }
|
|---|
| 44 | protected :
|
|---|
| 45 | PIStdImgApp* dap;
|
|---|
| 46 | PIACmd* piac;
|
|---|
| 47 | int mNitem;
|
|---|
| 48 | PIList* hitemlist;
|
|---|
| 49 | PIOptMenu* hgrpom;
|
|---|
| 50 | PIButton * mBut;
|
|---|
| 51 | PILabel * mLab;
|
|---|
| 52 | PIText* mTxt;
|
|---|
| 53 | };
|
|---|
| 54 |
|
|---|
| 55 | /* --Methode-- */
|
|---|
| 56 | PIAHelpWind::PIAHelpWind(PIStdImgApp *par, PIACmd* piacmd)
|
|---|
| 57 | : PIWindow((PIMsgHandler *)par, "Help-PIApp", PIWK_normal, 400, 300, 100, 350)
|
|---|
| 58 | {
|
|---|
| 59 | dap = par;
|
|---|
| 60 | piac = piacmd;
|
|---|
| 61 | mNitem = 0;
|
|---|
| 62 | SetMsg(77);
|
|---|
| 63 |
|
|---|
| 64 | int bsx, bsy;
|
|---|
| 65 | int tsx, tsy;
|
|---|
| 66 | int spx, spy;
|
|---|
| 67 | PIApplicationPrefCompSize(bsx, bsy);
|
|---|
| 68 | spx = bsx/6; spy = bsy/6;
|
|---|
| 69 | tsx = 10*bsx+2*spx; tsy = 7*bsy+3*spy;
|
|---|
| 70 | SetSize(tsx,tsy);
|
|---|
| 71 | hgrpom = new PIOptMenu(this, "hgrpoptmen", bsx*2.0, bsy, spx/2, spy);
|
|---|
| 72 | hgrpom->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
|
|---|
| 73 | hitemlist = new PIList(this, "hitemlist", bsx*2.0, tsy-3*spy-bsy, spx/2, 2*spy+bsy);
|
|---|
| 74 | hitemlist->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
|
|---|
| 75 | // hitemlist->SetBorderWidth(2);
|
|---|
| 76 | mTxt = new PIText(this, "helptext", true, true, bsx*8.0, 6*bsy, bsx*2.0+1.5*spx, spy);
|
|---|
| 77 | // mTxt->SetMutiLineMode(true);
|
|---|
| 78 | mTxt->SetTextEditable(false);
|
|---|
| 79 | mTxt->SetText("");
|
|---|
| 80 | mTxt->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
|
|---|
| 81 | mLab = new PILabel(this, "helpitem", bsx*4, bsy, bsx*2.5+2*spx, tsy-spy-bsy);
|
|---|
| 82 | mLab->SetBorderWidth(1);
|
|---|
| 83 | mLab->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
|
|---|
| 84 | mLab->SetLabel("");
|
|---|
| 85 | mBut = new PIButton(this, "Close", 70, bsx, bsy, tsx-bsx*1.5-spx, tsy-spy-bsy);
|
|---|
| 86 | mBut->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | /* --Methode-- */
|
|---|
| 90 | PIAHelpWind::~PIAHelpWind()
|
|---|
| 91 | {
|
|---|
| 92 | delete hgrpom;
|
|---|
| 93 | delete hitemlist;
|
|---|
| 94 | delete mTxt;
|
|---|
| 95 | delete mLab;
|
|---|
| 96 | delete mBut;
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | /* --Methode-- */
|
|---|
| 100 | void PIAHelpWind::Process(PIMessage msg, PIMsgHandler* sender, void* /*data*/)
|
|---|
| 101 | {
|
|---|
| 102 | PIMessage um = UserMsg(msg);
|
|---|
| 103 | if (((um == 77) && (ModMsg(msg) == PIMsg_Close)) || (um == 70) ) {
|
|---|
| 104 | Hide();
|
|---|
| 105 | return;
|
|---|
| 106 | }
|
|---|
| 107 | else if ( (um >= 20000) && (sender == hgrpom)) { // Selection de groupe de Help
|
|---|
| 108 | mTxt->SetText("");
|
|---|
| 109 | mLab->SetLabel("");
|
|---|
| 110 | piac->UpdateHelpList(this, um-20000);
|
|---|
| 111 | }
|
|---|
| 112 | else if ( (um > 100) && (sender == hitemlist) && (ModMsg(msg) == PIMsg_Select) ) {
|
|---|
| 113 | string s = hitemlist->GetSelectionStr();
|
|---|
| 114 | mTxt->SetText(piac->GetUsage(s));
|
|---|
| 115 | mLab->SetLabel(s);
|
|---|
| 116 | }
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | /* --Methode-- */
|
|---|
| 120 | void PIAHelpWind::Show()
|
|---|
| 121 | {
|
|---|
| 122 | hgrpom->SetValue(20000); // Groupe All
|
|---|
| 123 | mTxt->SetText("");
|
|---|
| 124 | mLab->SetLabel("");
|
|---|
| 125 | piac->UpdateHelpList(this, 0);
|
|---|
| 126 | PIWindow::Show();
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | // ------------------------------------------------------------
|
|---|
| 130 | // Bloc de commandes (Foreach, ...)
|
|---|
| 131 | // Classe PIACmdBloc
|
|---|
| 132 | // ------------------------------------------------------------
|
|---|
| 133 |
|
|---|
| 134 | class PIACmdBloc {
|
|---|
| 135 | public:
|
|---|
| 136 | enum BType { BT_None, BT_ForeachList, BT_ForeachInt, BT_ForeachFloat };
|
|---|
| 137 |
|
|---|
| 138 | PIACmdBloc(PIACmd* piac, PIACmdBloc* par, string& kw, vector<string>& args);
|
|---|
| 139 | ~PIACmdBloc();
|
|---|
| 140 | inline PIACmdBloc* Parent() { return(parent); }
|
|---|
| 141 | inline bool CheckOK() { return blkok; }
|
|---|
| 142 | inline void AddLine(string& line)
|
|---|
| 143 | { lines.push_back(line); bloclineid.push_back(lines.size()); }
|
|---|
| 144 | void AddLine(string& line, string& kw);
|
|---|
| 145 | inline void AddBloc(PIACmdBloc* blk)
|
|---|
| 146 | { blocs.push_back(blk); bloclineid.push_back(-blocs.size()); }
|
|---|
| 147 | PIACmdBloc* Execute();
|
|---|
| 148 | inline int& TestLevel() { return testlevel; }
|
|---|
| 149 | protected:
|
|---|
| 150 | PIACmd* piacmd;
|
|---|
| 151 | PIACmdBloc* parent;
|
|---|
| 152 | bool blkok; // true -> block OK
|
|---|
| 153 | BType typ; // foreach , integer loop, float loop, test
|
|---|
| 154 | string varname;
|
|---|
| 155 | vector<string> strlist;
|
|---|
| 156 | vector<string> lines;
|
|---|
| 157 | vector<PIACmdBloc *> blocs;
|
|---|
| 158 | vector<int> bloclineid;
|
|---|
| 159 | int i1,i2,di;
|
|---|
| 160 | float f1,f2,df;
|
|---|
| 161 | int testlevel; // niveau d'imbrication des if
|
|---|
| 162 | };
|
|---|
| 163 |
|
|---|
| 164 | /* --Methode-- */
|
|---|
| 165 | PIACmdBloc::PIACmdBloc(PIACmd* piac, PIACmdBloc* par, string& kw, vector<string>& args)
|
|---|
| 166 | {
|
|---|
| 167 | piacmd = piac;
|
|---|
| 168 | parent = par;
|
|---|
| 169 | blkok = false;
|
|---|
| 170 | typ = BT_None;
|
|---|
| 171 | i1 = 0; i2 = -1; di = 1;
|
|---|
| 172 | f1 = 0.; f2 = -1.; df = 1.;
|
|---|
| 173 | testlevel = 0;
|
|---|
| 174 | if ((args.size() < 2) || !isalpha((int)args[0][0]) ) return;
|
|---|
| 175 | if ((kw != "foreach") && (kw != "for")) return;
|
|---|
| 176 | varname = args[0]; // $CHECK$ Variable name should be checked
|
|---|
| 177 | //if (isalpha((int)args[1][0]) ) { This is a foreach bloc with string list
|
|---|
| 178 | if (kw == "foreach" ) { // This is a foreach bloc with string list
|
|---|
| 179 | if ((args[1] != "(") || (args[args.size()-1] != ")") ) return;
|
|---|
| 180 | for(int kk=2; kk<args.size()-1; kk++) strlist.push_back(args[kk]);
|
|---|
| 181 | typ = BT_ForeachList;
|
|---|
| 182 | blkok = true;
|
|---|
| 183 | }
|
|---|
| 184 | else { // This is an integer or float loop
|
|---|
| 185 | size_t l = args[1].length();
|
|---|
| 186 | size_t p = args[1].find(':');
|
|---|
| 187 | size_t pp = args[1].find('.');
|
|---|
| 188 | bool fl = (pp < l) ? true : false; // Float loop or integer loop
|
|---|
| 189 | if (p >= l) return; // Syntaxe error
|
|---|
| 190 | string a1 = args[1].substr(0, p);
|
|---|
| 191 | string aa = args[1].substr(p+1);
|
|---|
| 192 | p = aa.find(':');
|
|---|
| 193 | string a2, a3;
|
|---|
| 194 | bool hasa3 = false;
|
|---|
| 195 | if (p < aa.length() ) {
|
|---|
| 196 | a2 = aa.substr(0,p);
|
|---|
| 197 | a3 = aa.substr(p+1);
|
|---|
| 198 | hasa3 = true;
|
|---|
| 199 | }
|
|---|
| 200 | else a2 = aa;
|
|---|
| 201 | if (fl) {
|
|---|
| 202 | typ = BT_ForeachFloat;
|
|---|
| 203 | blkok = true;
|
|---|
| 204 | f1 = atof(a1.c_str());
|
|---|
| 205 | f2 = atof(a2.c_str());
|
|---|
| 206 | if (hasa3) df = atof(a3.c_str());
|
|---|
| 207 | else df = 1.;
|
|---|
| 208 | }
|
|---|
| 209 | else {
|
|---|
| 210 | typ = BT_ForeachInt;
|
|---|
| 211 | blkok = true;
|
|---|
| 212 | i1 = atoi(a1.c_str());
|
|---|
| 213 | i2 = atoi(a2.c_str());
|
|---|
| 214 | if (hasa3) di = atoi(a3.c_str());
|
|---|
| 215 | else di = 1;
|
|---|
| 216 | }
|
|---|
| 217 | }
|
|---|
| 218 | }
|
|---|
| 219 |
|
|---|
| 220 | /* --Methode-- */
|
|---|
| 221 | PIACmdBloc::~PIACmdBloc()
|
|---|
| 222 | {
|
|---|
| 223 | for(int k=0; k<blocs.size(); k++) delete blocs[k];
|
|---|
| 224 | }
|
|---|
| 225 |
|
|---|
| 226 | /* --Methode-- */
|
|---|
| 227 | void PIACmdBloc::AddLine(string& line, string& kw)
|
|---|
| 228 | {
|
|---|
| 229 | AddLine(line);
|
|---|
| 230 | if (kw == "if") testlevel++;
|
|---|
| 231 | else if (kw == "endif") testlevel--;
|
|---|
| 232 | }
|
|---|
| 233 |
|
|---|
| 234 | /* --Methode-- */
|
|---|
| 235 | PIACmdBloc* PIACmdBloc::Execute()
|
|---|
| 236 | {
|
|---|
| 237 | // cout << " DBG * PIACmdBloc::Execute() " << typ << " - " << bloclineid.size() <<
|
|---|
| 238 | // " I1,I2=" << i1 << " , " << i2 << " , " << di << endl;
|
|---|
| 239 | string cmd;
|
|---|
| 240 | int k=0;
|
|---|
| 241 | int kj=0;
|
|---|
| 242 | int kk=0;
|
|---|
| 243 | char buff[32];
|
|---|
| 244 | int rcc = 0;
|
|---|
| 245 |
|
|---|
| 246 | if (typ == BT_ForeachList) // foreach string loop
|
|---|
| 247 | for(k=0; k<strlist.size(); k++) {
|
|---|
| 248 | cmd = "set " + varname + " '" + strlist[k] + "'";
|
|---|
| 249 | piacmd->Interpret(cmd);
|
|---|
| 250 | for(kj=0; kj<bloclineid.size(); kj++) {
|
|---|
| 251 | kk = bloclineid[kj];
|
|---|
| 252 | if (kk > 0) {
|
|---|
| 253 | rcc = piacmd->Interpret(lines[kk-1]);
|
|---|
| 254 | if (rcc == 77766) break;
|
|---|
| 255 | }
|
|---|
| 256 | else blocs[-kk-1]->Execute();
|
|---|
| 257 | }
|
|---|
| 258 | if (rcc == 77766) break;
|
|---|
| 259 | }
|
|---|
| 260 | else if (typ == BT_ForeachInt) // Integer loop
|
|---|
| 261 | for(int i=i1; i<i2; i+=di) {
|
|---|
| 262 | k++;
|
|---|
| 263 | if (++k > 9999) {
|
|---|
| 264 | cout << ">>> Maximum PIACmdBloc loop limit (9999) -> break " << endl;
|
|---|
| 265 | break;
|
|---|
| 266 | }
|
|---|
| 267 | sprintf(buff, " %d", i);
|
|---|
| 268 | cmd = "set " + varname + buff;
|
|---|
| 269 | piacmd->Interpret(cmd);
|
|---|
| 270 | for(kj=0; kj<bloclineid.size(); kj++) {
|
|---|
| 271 | kk = bloclineid[kj];
|
|---|
| 272 | if (kk > 0) {
|
|---|
| 273 | rcc = piacmd->Interpret(lines[kk-1]);
|
|---|
| 274 | if (rcc == 77766) break;
|
|---|
| 275 | }
|
|---|
| 276 | else blocs[-kk-1]->Execute();
|
|---|
| 277 | }
|
|---|
| 278 | if (rcc == 77766) break;
|
|---|
| 279 | }
|
|---|
| 280 | else if (typ == BT_ForeachFloat) // float loop
|
|---|
| 281 | for(float f=f1; f<f2; f+=df) {
|
|---|
| 282 | k++;
|
|---|
| 283 | if (++k > 9999) {
|
|---|
| 284 | cout << ">>> Maximum PIACmdBloc loop limit (9999) -> break " << endl;
|
|---|
| 285 | break;
|
|---|
| 286 | }
|
|---|
| 287 | sprintf(buff, " %g", f);
|
|---|
| 288 | cmd = "set " + varname + buff;
|
|---|
| 289 | piacmd->Interpret(cmd);
|
|---|
| 290 | for(kj=0; kj<bloclineid.size(); kj++) {
|
|---|
| 291 | kk = bloclineid[kj];
|
|---|
| 292 | if (kk > 0) {
|
|---|
| 293 | rcc = piacmd->Interpret(lines[kk-1]);
|
|---|
| 294 | if (rcc == 77766) break;
|
|---|
| 295 | }
|
|---|
| 296 | else blocs[-kk-1]->Execute();
|
|---|
| 297 | }
|
|---|
| 298 | if (rcc == 77766) break;
|
|---|
| 299 | }
|
|---|
| 300 |
|
|---|
| 301 | return(parent);
|
|---|
| 302 | }
|
|---|
| 303 |
|
|---|
| 304 | // ------------------
|
|---|
| 305 | // Classe PIACmdScript
|
|---|
| 306 | // ------------------
|
|---|
| 307 | class PIACmdScript {
|
|---|
| 308 | public:
|
|---|
| 309 | PIACmdScript();
|
|---|
| 310 | virtual ~PIACmdScript();
|
|---|
| 311 | };
|
|---|
| 312 |
|
|---|
| 313 | // ------------------------------------------------------------
|
|---|
| 314 | // Classe PIACmd
|
|---|
| 315 | // ------------------------------------------------------------
|
|---|
| 316 |
|
|---|
| 317 | static PIACmd* curpiacmd = NULL;
|
|---|
| 318 | /* --Methode-- */
|
|---|
| 319 | PIACmd::PIACmd(NamedObjMgr* omg, PIStdImgApp* app)
|
|---|
| 320 | {
|
|---|
| 321 | mObjMgr = omg;
|
|---|
| 322 | mImgApp = app;
|
|---|
| 323 | system("cp history.pic hisold.pic");
|
|---|
| 324 | hist.open("history.pic");
|
|---|
| 325 | histon = true;
|
|---|
| 326 | trace = false; timing = false;
|
|---|
| 327 | gltimer = NULL;
|
|---|
| 328 | felevel = 0;
|
|---|
| 329 |
|
|---|
| 330 | mulinecmd = "";
|
|---|
| 331 | mulinefg = false;
|
|---|
| 332 |
|
|---|
| 333 | CmdBlks.push(NULL);
|
|---|
| 334 | list<char> xtx;
|
|---|
| 335 | TestsStack.push(xtx);
|
|---|
| 336 | curtestresult = true;
|
|---|
| 337 |
|
|---|
| 338 | cmdhgrp["All"] = 0;
|
|---|
| 339 | cmdgrpid = 1;
|
|---|
| 340 | cmdhgrp["Commands"] = 1;
|
|---|
| 341 | helpwin = new PIAHelpWind(app, this);
|
|---|
| 342 | helpwin->AddHelpGroup("All", 0);
|
|---|
| 343 | helpwin->AddHelpGroup("Commands", 1);
|
|---|
| 344 |
|
|---|
| 345 | string kw = "piacmd";
|
|---|
| 346 | string usage;
|
|---|
| 347 | usage = ">>> (piacmd) Interpreter's keywords : \n";
|
|---|
| 348 | usage += " > set varname string # To set a variable, $varname \n";
|
|---|
| 349 | usage += " > get newvarname varname # To set a newvariable, equal to $varname \n";
|
|---|
| 350 | usage += " > setol varname patt # Fills varname with object list \n";
|
|---|
| 351 | usage += " > unset varname # clear variable definition \n";
|
|---|
| 352 | usage += " > echo string # output string \n";
|
|---|
| 353 | usage += " > alias name string # define a command alias \n";
|
|---|
| 354 | usage += " > readstdin varname # reads a line from stdin into $varname \n";
|
|---|
| 355 | usage += " > foreach varname string-list # Loop \n";
|
|---|
| 356 | usage += " > for varname i1:i2[:di] # Integer loop \n";
|
|---|
| 357 | usage += " > for varname f1:f2[:df] # Float loop \n";
|
|---|
| 358 | usage += " > end # end loops \n";
|
|---|
| 359 | usage += " > if test then # Conditional test : a == != < > <= >= b \n";
|
|---|
| 360 | usage += " > else # Conditional \n";
|
|---|
| 361 | usage += " > endif # End of conditional if bloc \n";
|
|---|
| 362 | usage += " > break # Delete (clears) all test and loop blocs \n";
|
|---|
| 363 | usage += " > return # Stops command execution from a file \n";
|
|---|
| 364 | usage += " > listvars # List of variable names and values \n";
|
|---|
| 365 | usage += " > listalias # List of alias names and values \n";
|
|---|
| 366 | usage += " > listcommands # List of all known commands \n";
|
|---|
| 367 | usage += " > exec filename # Execute commands from file \n";
|
|---|
| 368 | usage += " > help <command_name> # <command_name> usage info \n";
|
|---|
| 369 | usage += " > helpwindow # Displays help window \n";
|
|---|
| 370 | usage += " > timingon timingoff traceon traceoff \n";
|
|---|
| 371 | string grp = "Commands";
|
|---|
| 372 | RegisterHelp(kw, usage, grp);
|
|---|
| 373 |
|
|---|
| 374 | kw = "shell execute";
|
|---|
| 375 | usage = "> shell command_string # Execute shell command\n";
|
|---|
| 376 | usage += "> cshell command_string # Execute cshell command\n";
|
|---|
| 377 | usage += "---Exemples:\n";
|
|---|
| 378 | usage += " > shell ls\n";
|
|---|
| 379 | usage += " > cshell echo '$LD_LIBRARY_PATH'; map2cl -h; ls\n";
|
|---|
| 380 | usage += " > shell myfile.csh [arg1] [arg2] [...]\n";
|
|---|
| 381 | usage += " (where the first line of \"myfile.csh\" is \"#!/bin/csh\")\n";
|
|---|
| 382 | RegisterHelp(kw, usage, grp);
|
|---|
| 383 |
|
|---|
| 384 | basexec = new PIABaseExecutor(this, omg, app);
|
|---|
| 385 | fitexec = new PIAFitter(this, app);
|
|---|
| 386 | pawexec = new PAWExecutor(this, app);
|
|---|
| 387 | CxxExecutor * cxxe = new CxxExecutor(this, app);
|
|---|
| 388 | cxxexec = cxxe;
|
|---|
| 389 |
|
|---|
| 390 | ContModExecutor *cntxx = new ContModExecutor(this, app);//_OP_
|
|---|
| 391 | cntexec = cntxx; //_OP_
|
|---|
| 392 | FlowModExecutor *flwxx = new FlowModExecutor(this, app);//_OP_
|
|---|
| 393 | flwexec = flwxx; //_OP_
|
|---|
| 394 |
|
|---|
| 395 | cxxoptwin = new CxxOptionWind(app, cxxe);
|
|---|
| 396 | cxxexwin = new CxxExecWind(app, cxxe);
|
|---|
| 397 |
|
|---|
| 398 | AddInterpreter(this);
|
|---|
| 399 | curcmdi = this;
|
|---|
| 400 | }
|
|---|
| 401 |
|
|---|
| 402 | /* --Methode-- */
|
|---|
| 403 | PIACmd::~PIACmd()
|
|---|
| 404 | {
|
|---|
| 405 | hist.close();
|
|---|
| 406 | if (gltimer) { delete gltimer; gltimer = NULL; }
|
|---|
| 407 | Modmap::iterator it;
|
|---|
| 408 | for(it = modmap.begin(); it != modmap.end(); it++) {
|
|---|
| 409 | string name = (*it).first + "_end";
|
|---|
| 410 | DlModuleInitEndFunction fend = (*it).second->GetFunction(name);
|
|---|
| 411 | if (fend) fend();
|
|---|
| 412 | delete (*it).second;
|
|---|
| 413 | }
|
|---|
| 414 | delete helpwin;
|
|---|
| 415 | delete cxxexwin;
|
|---|
| 416 | delete cxxoptwin;
|
|---|
| 417 | if (curpiacmd == this) curpiacmd = NULL;
|
|---|
| 418 | delete basexec;
|
|---|
| 419 | delete fitexec;
|
|---|
| 420 | delete pawexec;
|
|---|
| 421 | delete cxxexec;
|
|---|
| 422 | }
|
|---|
| 423 |
|
|---|
| 424 | /* --Methode-- */
|
|---|
| 425 | PIACmd* PIACmd::GetInterpreter()
|
|---|
| 426 | {
|
|---|
| 427 | return(curpiacmd);
|
|---|
| 428 | }
|
|---|
| 429 |
|
|---|
| 430 | /* --Methode-- */
|
|---|
| 431 | string PIACmd::Name()
|
|---|
| 432 | {
|
|---|
| 433 | return("piacmd");
|
|---|
| 434 | }
|
|---|
| 435 |
|
|---|
| 436 | /* --Methode-- */
|
|---|
| 437 | void PIACmd::RegisterCommand(string& keyw, string& usage, CmdExecutor * ce, string grp)
|
|---|
| 438 | {
|
|---|
| 439 | if (!ce) {
|
|---|
| 440 | RegisterHelp(keyw, usage, grp);
|
|---|
| 441 | return;
|
|---|
| 442 | }
|
|---|
| 443 | int gid = CheckHelpGrp(grp);
|
|---|
| 444 | cmdex cme;
|
|---|
| 445 | cme.group = gid;
|
|---|
| 446 | cme.us = usage;
|
|---|
| 447 | cme.cex = ce;
|
|---|
| 448 | cmdexmap[keyw] = cme;
|
|---|
| 449 | }
|
|---|
| 450 |
|
|---|
| 451 | /* --Methode-- */
|
|---|
| 452 | void PIACmd::RegisterHelp(string& keyw, string& usage, string& grp)
|
|---|
| 453 | {
|
|---|
| 454 | int gid = CheckHelpGrp(grp);
|
|---|
| 455 | cmdex cme;
|
|---|
| 456 | cme.group = gid;
|
|---|
| 457 | cme.us = usage;
|
|---|
| 458 | cme.cex = NULL;
|
|---|
| 459 | helpexmap[keyw] = cme;
|
|---|
| 460 | }
|
|---|
| 461 |
|
|---|
| 462 | /* --Methode-- */
|
|---|
| 463 | int PIACmd::CheckHelpGrp(string& grp)
|
|---|
| 464 | {
|
|---|
| 465 | int gid=0;
|
|---|
| 466 | CmdHGroup::iterator it = cmdhgrp.find(grp);
|
|---|
| 467 | if (it == cmdhgrp.end()) {
|
|---|
| 468 | cmdgrpid++; gid = cmdgrpid;
|
|---|
| 469 | cmdhgrp[grp] = gid;
|
|---|
| 470 | helpwin->AddHelpGroup(grp.c_str(), gid);
|
|---|
| 471 | }
|
|---|
| 472 | else gid = (*it).second;
|
|---|
| 473 | return(gid);
|
|---|
| 474 | }
|
|---|
| 475 |
|
|---|
| 476 | /* --Methode-- */
|
|---|
| 477 | void PIACmd::UpdateHelpList(PIAHelpWind* hw, int gid)
|
|---|
| 478 | {
|
|---|
| 479 | helpwin->ClearHelpList();
|
|---|
| 480 | CmdExmap::iterator it;
|
|---|
| 481 | for(it = helpexmap.begin(); it != helpexmap.end(); it++) {
|
|---|
| 482 | if ( (gid != 0) && ((*it).second.group != gid) ) continue;
|
|---|
| 483 | helpwin->AddHelpItem((*it).first.c_str());
|
|---|
| 484 | }
|
|---|
| 485 | for(it = cmdexmap.begin(); it != cmdexmap.end(); it++) {
|
|---|
| 486 | if ( (gid != 0) && ((*it).second.group != gid) ) continue;
|
|---|
| 487 | helpwin->AddHelpItem((*it).first.c_str());
|
|---|
| 488 | }
|
|---|
| 489 | }
|
|---|
| 490 |
|
|---|
| 491 | /* --Methode-- */
|
|---|
| 492 | void PIACmd::LoadModule(string& fnameso, string& name)
|
|---|
| 493 | {
|
|---|
| 494 | PDynLinkMgr * dynlink = new PDynLinkMgr(fnameso, false);
|
|---|
| 495 | if (dynlink == NULL) {
|
|---|
| 496 | cerr << "PIACmd/LoadModule_Error: Pb opening SO " << fnameso << endl;
|
|---|
| 497 | return;
|
|---|
| 498 | }
|
|---|
| 499 | string fname = name + "_init";
|
|---|
| 500 | DlModuleInitEndFunction finit = dynlink->GetFunction(fname);
|
|---|
| 501 | if (!finit) {
|
|---|
| 502 | cerr << "PIACmd/LoadModule_Error: Pb linking " << fname << endl;
|
|---|
| 503 | return;
|
|---|
| 504 | }
|
|---|
| 505 | cout << "PIACmd/LoadModule_Info: Initialisation module" << name
|
|---|
| 506 | << " " << fname << "() ..." << endl;
|
|---|
| 507 | finit();
|
|---|
| 508 | modmap[name] = dynlink;
|
|---|
| 509 | return;
|
|---|
| 510 | }
|
|---|
| 511 |
|
|---|
| 512 | /* --Methode-- */
|
|---|
| 513 | void PIACmd::AddInterpreter(CmdInterpreter * cl)
|
|---|
| 514 | {
|
|---|
| 515 | if (!cl) return;
|
|---|
| 516 | interpmap[cl->Name()] = cl;}
|
|---|
| 517 |
|
|---|
| 518 | /* --Methode-- */
|
|---|
| 519 | void PIACmd::SelInterpreter(string& name)
|
|---|
| 520 | {
|
|---|
| 521 | InterpMap::iterator it = interpmap.find(name);
|
|---|
| 522 | if (it == interpmap.end()) return;
|
|---|
| 523 | curcmdi = (*it).second;
|
|---|
| 524 | }
|
|---|
| 525 |
|
|---|
| 526 |
|
|---|
| 527 |
|
|---|
| 528 | /* Fonction */
|
|---|
| 529 | static string GetStringFrStdin(PIACmd* piac)
|
|---|
| 530 | {
|
|---|
| 531 | PIStdImgApp* piapp = piac->GetImgApp();
|
|---|
| 532 | if (piapp) {
|
|---|
| 533 | PIBaseWdg* wdg = piapp->CurrentBaseWdg();
|
|---|
| 534 | if (wdg) wdg->Refresh();
|
|---|
| 535 | #ifndef __mac__
|
|---|
| 536 | /* On vide le buffer X-Window */
|
|---|
| 537 | XSync(PIXDisplay(),False);
|
|---|
| 538 | #endif
|
|---|
| 539 | }
|
|---|
| 540 | char buff[128];
|
|---|
| 541 | fgets(buff, 128, stdin);
|
|---|
| 542 | buff[127] = '\0';
|
|---|
| 543 | return((string)buff);
|
|---|
| 544 | }
|
|---|
| 545 |
|
|---|
| 546 | /* --Methode-- */
|
|---|
| 547 | int PIACmd::Interpret(string& s)
|
|---|
| 548 | {
|
|---|
| 549 | int rc = 0;
|
|---|
| 550 | NamedObjMgr omg;
|
|---|
| 551 |
|
|---|
| 552 |
|
|---|
| 553 | // On saute de commandes vides
|
|---|
| 554 | size_t l;
|
|---|
| 555 | l = s.length();
|
|---|
| 556 | if (l < 1) return(0);
|
|---|
| 557 |
|
|---|
| 558 | // On enregistre les commandes
|
|---|
| 559 | if (histon) hist << s << endl;
|
|---|
| 560 |
|
|---|
| 561 | if (s[0] == '#') return(0); // si c'est un commentaire
|
|---|
| 562 |
|
|---|
| 563 | // Logique de gestion des lignes suite
|
|---|
| 564 | // un \ en derniere position indique la presence d'une ligne suite
|
|---|
| 565 | size_t lnb = s.find_last_not_of(' ');
|
|---|
| 566 | if (s[lnb] == '\\' ) { // Lignes suite ...
|
|---|
| 567 | mulinefg = true;
|
|---|
| 568 | mulinecmd += s.substr(0,lnb);
|
|---|
| 569 | mImgApp->GetConsole()->SetPrompt("...? ");
|
|---|
| 570 | return(0);
|
|---|
| 571 | }
|
|---|
| 572 |
|
|---|
| 573 | if (mulinefg) { // Il y avait des lignes suite
|
|---|
| 574 | s = mulinecmd + s;
|
|---|
| 575 | mulinecmd = "";
|
|---|
| 576 | mulinefg = false;
|
|---|
| 577 | const char * rprompt = (CmdBlks.empty()) ? "Cmd> " : "for...? ";
|
|---|
| 578 | mImgApp->GetConsole()->SetPrompt(rprompt);
|
|---|
| 579 | }
|
|---|
| 580 |
|
|---|
| 581 | // Removing leading blanks
|
|---|
| 582 | size_t p,q;
|
|---|
| 583 |
|
|---|
| 584 | p=s.find_first_not_of(" \t");
|
|---|
| 585 | if (p < l) s = s.substr(p);
|
|---|
| 586 |
|
|---|
| 587 | // >>>> Substitution d'alias (1er mot)
|
|---|
| 588 | CmdStrList::iterator it;
|
|---|
| 589 | p = 0;
|
|---|
| 590 | q = s.find_first_of(" \t");
|
|---|
| 591 | l = s.length();
|
|---|
| 592 | string w1 = (q < l) ? s.substr(p,q-p) : s.substr(p);
|
|---|
| 593 | it = mAliases.find(w1);
|
|---|
| 594 | if (it != mAliases.end()) {
|
|---|
| 595 | s = (q < l) ? ((*it).second + s.substr(q)) : (*it).second ;
|
|---|
| 596 | l = s.length();
|
|---|
| 597 | p=s.find_first_not_of(" \t");
|
|---|
| 598 | if (p < l) s = s.substr(p);
|
|---|
| 599 | p = 0;
|
|---|
| 600 | q = s.find_first_of(" ");
|
|---|
| 601 | }
|
|---|
| 602 |
|
|---|
| 603 | // >>>> Separating keyword
|
|---|
| 604 | string toks,kw;
|
|---|
| 605 | if (q < l)
|
|---|
| 606 | { kw = s.substr(p,q-p); toks = s.substr(q, l-q); }
|
|---|
| 607 | else { kw = s.substr(p,l-p); toks = ""; }
|
|---|
| 608 |
|
|---|
| 609 | // les mot-cle end else endif doivent etre le seul mot de la ligne
|
|---|
| 610 | if ( (kw == "end") || (kw == "else") || (kw == "endif") ) {
|
|---|
| 611 | size_t ltk = toks.length();
|
|---|
| 612 | if (toks.find_first_not_of(" \t") < ltk) {
|
|---|
| 613 | cerr << "PIACmd::Interpret()/syntax error near end else endif \n"
|
|---|
| 614 | << "line: " << s << endl;
|
|---|
| 615 | return(1);
|
|---|
| 616 | }
|
|---|
| 617 | }
|
|---|
| 618 |
|
|---|
| 619 | // On verifie si nous sommes dans un bloc (for , foreach)
|
|---|
| 620 | if (CmdBlks.top() != NULL) { // On est dans un bloc
|
|---|
| 621 | if ( (kw == "for") || (kw == "foreach")) felevel++;
|
|---|
| 622 | else if (kw == "end") felevel--;
|
|---|
| 623 | if (felevel == 0) { // Il faut executer le bloc
|
|---|
| 624 | PIACmdBloc* curb = CmdBlks.top();
|
|---|
| 625 | CmdBlks.top() = curb->Parent();
|
|---|
| 626 | mImgApp->GetConsole()->SetPrompt("Cmd> ");
|
|---|
| 627 | if (curb->TestLevel() != 0) {
|
|---|
| 628 | cerr << "PIACmd::Interpret()/syntax error - unbalenced if ... endif"
|
|---|
| 629 | << " within for/foreach bloc ! " << endl;
|
|---|
| 630 | delete curb;
|
|---|
| 631 | return(2);
|
|---|
| 632 | }
|
|---|
| 633 | // cout << " *DBG* Executing bloc " << endl;
|
|---|
| 634 | bool ohv = histon;
|
|---|
| 635 | histon = false;
|
|---|
| 636 | if (curtestresult) {
|
|---|
| 637 | // We push also PIACmdBloc and testresult on the stack
|
|---|
| 638 | CmdBlks.push(NULL);
|
|---|
| 639 | list<char> xtx;
|
|---|
| 640 | TestsStack.push(xtx);
|
|---|
| 641 | curb->Execute();
|
|---|
| 642 | // And PIACmdBloc and TestResult from the corresponding stacks
|
|---|
| 643 | PopStack(false);
|
|---|
| 644 | }
|
|---|
| 645 | delete curb;
|
|---|
| 646 | histon = ohv;
|
|---|
| 647 | }
|
|---|
| 648 | else CmdBlks.top()->AddLine(s, kw);
|
|---|
| 649 | return(0);
|
|---|
| 650 | }
|
|---|
| 651 | else if (kw == "end") {
|
|---|
| 652 | cerr << "PIACmd::Interpret()/syntax error - end outside for/foreach bloc \n"
|
|---|
| 653 | << "line: " << s << endl;
|
|---|
| 654 | return(1);
|
|---|
| 655 | }
|
|---|
| 656 |
|
|---|
| 657 | // Sommes-nous dans un bloc de test if then else
|
|---|
| 658 | if (TestsStack.top().size() > 0) { // Nous sommes ds un bloc if
|
|---|
| 659 | if (kw == "else") {
|
|---|
| 660 | if ((*tresit) & 2) {
|
|---|
| 661 | cerr << "PIACmd::Interpret()/syntax error - multiple else in if bloc \n"
|
|---|
| 662 | << "line: " << s << endl;
|
|---|
| 663 | return(1);
|
|---|
| 664 | }
|
|---|
| 665 | else {
|
|---|
| 666 | const char * npr = ((*tresit)&1) ? "else-F> " : "else-T> ";
|
|---|
| 667 | if ((*tresit)&1) curtestresult = false;
|
|---|
| 668 | mImgApp->GetConsole()->SetPrompt(npr);
|
|---|
| 669 | (*tresit) |= 2;
|
|---|
| 670 | return(0);
|
|---|
| 671 | }
|
|---|
| 672 | }
|
|---|
| 673 | else if (kw == "endif") {
|
|---|
| 674 | list<char>::iterator dbit = tresit;
|
|---|
| 675 | tresit--;
|
|---|
| 676 | TestsStack.top().erase(dbit);
|
|---|
| 677 | const char * npr = "Cmd> ";
|
|---|
| 678 | if (TestsStack.top().size() > 1) {
|
|---|
| 679 | curtestresult = true;
|
|---|
| 680 | list<char>::iterator it;
|
|---|
| 681 | for(it=TestsStack.top().begin(); it!=TestsStack.top().end(); it++) {
|
|---|
| 682 | // Si on n'est pas ds le else et le if est faux
|
|---|
| 683 | if ( !((*it)&2) && !((*it)&1) ) curtestresult = false;
|
|---|
| 684 | // Si on est ds else et le if etait vrai !
|
|---|
| 685 | if ( ((*it)&2) && ((*it)&1) ) curtestresult = false;
|
|---|
| 686 | if (!curtestresult) break;
|
|---|
| 687 | }
|
|---|
| 688 |
|
|---|
| 689 | if (!((*tresit)&2))
|
|---|
| 690 | npr = ((*tresit)&1) ? "if-T> " : "if-F> ";
|
|---|
| 691 | else
|
|---|
| 692 | npr = ((*tresit)&1) ? "else-F> " : "else-T> ";
|
|---|
| 693 | }
|
|---|
| 694 | else curtestresult = true;
|
|---|
| 695 | mImgApp->GetConsole()->SetPrompt(npr);
|
|---|
| 696 | return(0);
|
|---|
| 697 | }
|
|---|
| 698 | }
|
|---|
| 699 | else if ((kw == "else") || (kw == "endif")) {
|
|---|
| 700 | cerr << "PIACmd::Interpret()/syntax error - else,endif outside if bloc \n"
|
|---|
| 701 | << "line: " << s << endl;
|
|---|
| 702 | return(1);
|
|---|
| 703 | }
|
|---|
| 704 |
|
|---|
| 705 | bool fgcont = true;
|
|---|
| 706 | if (TestsStack.top().size() > 0) { // Resultat de if ou else
|
|---|
| 707 | list<char>::iterator it;
|
|---|
| 708 | for(it=TestsStack.top().begin(); it!=TestsStack.top().end(); it++) {
|
|---|
| 709 | // Si on n'est pas ds le else et le if est faux
|
|---|
| 710 | if ( !((*it)&2) && !((*it)&1) ) fgcont = false;
|
|---|
| 711 | // Si on est ds else et le if etait vrai !
|
|---|
| 712 | if ( ((*it)&2) && ((*it)&1) ) fgcont = false;
|
|---|
| 713 | if (!fgcont) break;
|
|---|
| 714 | }
|
|---|
| 715 | }
|
|---|
| 716 |
|
|---|
| 717 | if ((!fgcont) && (kw != "if")) return(0);
|
|---|
| 718 |
|
|---|
| 719 |
|
|---|
| 720 | // Les mots cles break et return peuvent de sortir de boucles/scripts/execfile
|
|---|
| 721 | if (kw == "break") return(77766);
|
|---|
| 722 | else if (kw == "return") return(77777);
|
|---|
| 723 |
|
|---|
| 724 | // Nous ne sommes donc pas dans un bloc .... Substitution de variables
|
|---|
| 725 | string s2;
|
|---|
| 726 | int rcs ;
|
|---|
| 727 | // Execution de code C++
|
|---|
| 728 |
|
|---|
| 729 | if (s[0] == '@') {
|
|---|
| 730 | CxxExecutor * cxxe = dynamic_cast<CxxExecutor *>(cxxexec);
|
|---|
| 731 | if (cxxe == NULL) {
|
|---|
| 732 | cerr << "PIACmd::Interpret() - BUG !!! Not a CxxExecutor " << endl;
|
|---|
| 733 | return(99);
|
|---|
| 734 | }
|
|---|
| 735 | // Sans substitution des variables $
|
|---|
| 736 | if (s[1] == '@') return(cxxe->ExecuteCXX(s.substr(2)));
|
|---|
| 737 | else { // AVEC substitution des variables $
|
|---|
| 738 | rcs = SubstituteVars(s, s2);
|
|---|
| 739 | if (rcs) return(rcs);
|
|---|
| 740 | return(cxxe->ExecuteCXX(s2.substr(1)));
|
|---|
| 741 | }
|
|---|
| 742 | }
|
|---|
| 743 |
|
|---|
| 744 |
|
|---|
| 745 | rcs = SubstituteVars(s, s2);
|
|---|
| 746 | if (rcs) return(rcs);
|
|---|
| 747 |
|
|---|
| 748 | // >>>> Separating keyword and tokens
|
|---|
| 749 | vector<string> tokens;
|
|---|
| 750 |
|
|---|
| 751 | q = s2.find(' ');
|
|---|
| 752 | l = s2.length();
|
|---|
| 753 | if (q < l)
|
|---|
| 754 | { kw = s2.substr(0,q); toks = s2.substr(q, l-q); }
|
|---|
| 755 | else { kw = s2; toks = ""; }
|
|---|
| 756 |
|
|---|
| 757 | q = 0;
|
|---|
| 758 | while (q < l) {
|
|---|
| 759 | p = toks.find_first_not_of(" \t",q+1); // au debut d'un token
|
|---|
| 760 | if (p>=l) break;
|
|---|
| 761 | if ( (toks[p] == '\'') || (toks[p] == '"') ) {
|
|---|
| 762 | q = toks.find(toks[p],p+1);
|
|---|
| 763 | if (q>=l) {
|
|---|
| 764 | cerr << "PIACmd::Interpret()/Syntax Error - Unbalenced quotes " << toks[p] << '.' << endl;
|
|---|
| 765 | return(2);
|
|---|
| 766 | }
|
|---|
| 767 | p++;
|
|---|
| 768 | }
|
|---|
| 769 | else {
|
|---|
| 770 | q = toks.find_first_of(" \t",p); // la fin du token;
|
|---|
| 771 | }
|
|---|
| 772 | string token = toks.substr(p,q-p);
|
|---|
| 773 | tokens.push_back(token);
|
|---|
| 774 | }
|
|---|
| 775 |
|
|---|
| 776 |
|
|---|
| 777 | // Si c'est un for/foreach, on cree un nouveau bloc
|
|---|
| 778 | if ((kw == "foreach") || (kw == "for")) {
|
|---|
| 779 | // cout << " *DBG* We got a foreach... " << endl;
|
|---|
| 780 | PIACmdBloc* bloc = new PIACmdBloc(this, CmdBlks.top(), kw, tokens);
|
|---|
| 781 | if (!bloc->CheckOK()) {
|
|---|
| 782 | cerr << "PIACmd::Interpret() for/foreach syntax Error ! " << endl;
|
|---|
| 783 | delete bloc;
|
|---|
| 784 | return(1);
|
|---|
| 785 | }
|
|---|
| 786 | felevel++;
|
|---|
| 787 | if (CmdBlks.top()) CmdBlks.top()->AddBloc(bloc);
|
|---|
| 788 | else mImgApp->GetConsole()->SetPrompt("for...> ");
|
|---|
| 789 | CmdBlks.top() = bloc;
|
|---|
| 790 | // cout << " *DBG* New Bloc created ... " << endl;
|
|---|
| 791 | return(0);
|
|---|
| 792 | }
|
|---|
| 793 | else if (kw == "if") { // Un test if
|
|---|
| 794 | bool restst = true;
|
|---|
| 795 | int rct = EvaluateTest(tokens, s, restst);
|
|---|
| 796 | if (rct) {
|
|---|
| 797 | cerr << "PIACmd::Interpret() if syntax Error ! " << endl;
|
|---|
| 798 | return(1);
|
|---|
| 799 | }
|
|---|
| 800 | char res_tst = (restst) ? 1 : 0;
|
|---|
| 801 | TestsStack.top().push_back(res_tst);
|
|---|
| 802 | if (TestsStack.top().size() == 1) tresit = TestsStack.top().begin();
|
|---|
| 803 | else tresit++;
|
|---|
| 804 | const char * npr = (restst) ? "if-T> " : "if-F> ";
|
|---|
| 805 | mImgApp->GetConsole()->SetPrompt(npr);
|
|---|
| 806 | }
|
|---|
| 807 | // Execution de commandes
|
|---|
| 808 | else rc = ExecuteCommandLine(kw, tokens, toks);
|
|---|
| 809 | return(rc);
|
|---|
| 810 |
|
|---|
| 811 | // cout << "PIACmd::Do() DBG KeyW= " << kw << " NbArgs= " << tokens.size() << endl;
|
|---|
| 812 | // for(int ii=0; ii<tokens.size(); ii++)
|
|---|
| 813 | // cout << "arg[ " << ii << " ] : " << tokens[ii] << endl;
|
|---|
| 814 |
|
|---|
| 815 | }
|
|---|
| 816 |
|
|---|
| 817 | /* --Methode-- */
|
|---|
| 818 | int PIACmd::SubstituteVars(string & s, string & s2)
|
|---|
| 819 | // Variable substitution
|
|---|
| 820 | {
|
|---|
| 821 | NamedObjMgr& omg = *mObjMgr;
|
|---|
| 822 |
|
|---|
| 823 | int iarr = -1; // index d'element de tableau
|
|---|
| 824 | size_t p,q,q2,q3,l;
|
|---|
| 825 |
|
|---|
| 826 | s2="";
|
|---|
| 827 | p = 0;
|
|---|
| 828 | l = s.length();
|
|---|
| 829 | string vn, vv;
|
|---|
| 830 | while (p < l) {
|
|---|
| 831 | q = s.find('$',p);
|
|---|
| 832 | if (q > l) break;
|
|---|
| 833 | q2 = s.find('\'',p);
|
|---|
| 834 | if ((q2 < l) && (q2 < q)) { // On saute la chaine delimitee par ' '
|
|---|
| 835 | q2 = s.find('\'',q2+1);
|
|---|
| 836 | if (q2 >= l) {
|
|---|
| 837 | cerr << " Syntax error - Unbalenced quotes !!! " << endl;
|
|---|
| 838 | return(1);
|
|---|
| 839 | }
|
|---|
| 840 | s2 += s.substr(p, q2-p+1);
|
|---|
| 841 | p = q2+1; continue;
|
|---|
| 842 | }
|
|---|
| 843 | // cout << "DBG: " << s2 << " p= " << p << " q= " << q << " L= " << l << endl;
|
|---|
| 844 | if ((q>0) && (s[q-1] == '\\')) { // Escape character \$
|
|---|
| 845 | s2 += (s.substr(p,q-1-p) + '$') ; p = q+1;
|
|---|
| 846 | continue;
|
|---|
| 847 | }
|
|---|
| 848 | if (q >= l-1) {
|
|---|
| 849 | cerr << " Syntax error - line ending with $ !!! " << endl;
|
|---|
| 850 | return(2);
|
|---|
| 851 | }
|
|---|
| 852 | vn = "";
|
|---|
| 853 | if ( s[q+1] == '{' ) { // Variable in the form ${name}
|
|---|
| 854 | q2 = s.find('}',q+1);
|
|---|
| 855 | if (q2 >= l) {
|
|---|
| 856 | cerr << " Syntax error - Unbalenced brace {} !!! " << endl;
|
|---|
| 857 | return(3);
|
|---|
| 858 | }
|
|---|
| 859 | vn = s.substr(q+2,q2-q-2);
|
|---|
| 860 | q2++;
|
|---|
| 861 | }
|
|---|
| 862 | else if ( s[q+1] == '(' ) { // Variable in the form $(name)
|
|---|
| 863 | q2 = s.find(')',q+1);
|
|---|
| 864 | if (q2 >= l) {
|
|---|
| 865 | cerr << " Syntax error - Unbalenced parenthesis () !!! " << endl;
|
|---|
| 866 | return(3);
|
|---|
| 867 | }
|
|---|
| 868 | vn = s.substr(q+2,q2-q-2);
|
|---|
| 869 | q2++;
|
|---|
| 870 | }
|
|---|
| 871 | else if ( s[q+1] == '[' ) { // Variable in the form $[varname] -> This is $$varname
|
|---|
| 872 | q2 = s.find(']',q+1);
|
|---|
| 873 | if (q2 >= l) {
|
|---|
| 874 | cerr << " Syntax error - Unbalenced brace [] !!! " << endl;
|
|---|
| 875 | return(4);
|
|---|
| 876 | }
|
|---|
| 877 | vn = s.substr(q+2,q2-q-2);
|
|---|
| 878 | if (!GetVar(vn, vv)) return(5);
|
|---|
| 879 | vn = vv;
|
|---|
| 880 | q2++;
|
|---|
| 881 | }
|
|---|
| 882 | else {
|
|---|
| 883 | q2 = s.find_first_of(" .:+-*/,[](){}&|!$\"'",q+1);
|
|---|
| 884 | if (q2 > l) q2 = l;
|
|---|
| 885 | q3 = q2;
|
|---|
| 886 | vn = s.substr(q+1, q2-q-1);
|
|---|
| 887 | // Si variable de type $varname[index] : element de tableau
|
|---|
| 888 | if ((q2 < l) && (s[q2] == '[') ) {
|
|---|
| 889 | q3 = s.find_first_of("]",q2+1);
|
|---|
| 890 | string sia = s.substr(q2+1, q3-q2-1);
|
|---|
| 891 | if (sia.length() < 1) {
|
|---|
| 892 | cerr << " Syntax error - in $varname[index] : $"
|
|---|
| 893 | << vn << "[" << sia <<"]" << endl;
|
|---|
| 894 | return(4);
|
|---|
| 895 | }
|
|---|
| 896 | if (isalpha(sia[0])) {
|
|---|
| 897 | string sia2;
|
|---|
| 898 | if (!GetVar(sia, sia2) || (sia2.length() < 1)) {
|
|---|
| 899 | cerr << " Syntax error - in $varname[index] : $"
|
|---|
| 900 | << vn << "[" << sia <<"]" << endl;
|
|---|
| 901 | return(4);
|
|---|
| 902 | }
|
|---|
| 903 | sia = sia2;
|
|---|
| 904 | }
|
|---|
| 905 | int rcdia = ctoi(sia.c_str(), &iarr);
|
|---|
| 906 | if (rcdia < 0) {
|
|---|
| 907 | cerr << " Syntax error - in $varname[iarr] : $"
|
|---|
| 908 | << vn << "[" << sia <<"]" << endl;
|
|---|
| 909 | return(4);
|
|---|
| 910 | }
|
|---|
| 911 | }
|
|---|
| 912 | }
|
|---|
| 913 | if (!GetVar(vn, vv)) return(5);
|
|---|
| 914 | if (iarr < 0) {
|
|---|
| 915 | s2 += (s.substr(p, q-p) + vv);
|
|---|
| 916 | p = q2;
|
|---|
| 917 | }
|
|---|
| 918 | else {
|
|---|
| 919 | vector<string> vs;
|
|---|
| 920 | FillVStringFrString(vv, vs);
|
|---|
| 921 | if (iarr >= vs.size()) {
|
|---|
| 922 | cerr << " Substitution error - word index out of range in "
|
|---|
| 923 | << "$varname[iarr] : $" << vn << "[" << iarr <<"]" << endl;
|
|---|
| 924 | return(4);
|
|---|
| 925 | }
|
|---|
| 926 | else s2 += (s.substr(p, q-p) + vs[iarr]);
|
|---|
| 927 | p = q3+1;
|
|---|
| 928 | }
|
|---|
| 929 | }
|
|---|
| 930 | if (p < l) s2 += s.substr(p);
|
|---|
| 931 |
|
|---|
| 932 | p = s2.find_first_not_of(" \t");
|
|---|
| 933 | if (p < l) s2 = s2.substr(p);
|
|---|
| 934 |
|
|---|
| 935 | return(0);
|
|---|
| 936 | }
|
|---|
| 937 |
|
|---|
| 938 | /* --Methode-- */
|
|---|
| 939 | bool PIACmd::GetVar(string & vn, string & vv)
|
|---|
| 940 | {
|
|---|
| 941 | NamedObjMgr& omg = *mObjMgr;
|
|---|
| 942 | if (vn.length() < 1) {
|
|---|
| 943 | cerr << " PIACmd::SubstituteVar/Error: length(varname=" << vn << ")<1 !" << endl;
|
|---|
| 944 | vv = ""; return(false);
|
|---|
| 945 | }
|
|---|
| 946 | // Variable de type $# $0 $1 ... (argument de .pic ou de script)
|
|---|
| 947 | int ka = 0;
|
|---|
| 948 | if (vn == "#") {
|
|---|
| 949 | if (ArgsStack.empty()) {
|
|---|
| 950 | cerr << " PIACmd::SubstituteVar/Error: ArgsStack empty ! "
|
|---|
| 951 | << " ($" << vn << ")" << endl;
|
|---|
| 952 | vv = ""; return(false);
|
|---|
| 953 | }
|
|---|
| 954 | char buff[32];
|
|---|
| 955 | long an = ArgsStack.top().size();
|
|---|
| 956 | sprintf(buff,"%ld", an);
|
|---|
| 957 | vv = buff; return(true);
|
|---|
| 958 | }
|
|---|
| 959 | else if (ctoi(vn.c_str(), &ka) > 0) { // $0 $1 $2 ...
|
|---|
| 960 | if (ArgsStack.empty()) {
|
|---|
| 961 | cerr << " PIACmd::SubstituteVar/Error: ArgsStack empty ! "
|
|---|
| 962 | << " ($" << vn << ")" << endl;
|
|---|
| 963 | vv = ""; return(false);
|
|---|
| 964 | }
|
|---|
| 965 | if ( (ka < 0) || (ka >= ArgsStack.top().size()) ) {
|
|---|
| 966 | cerr << " PIACmd::SubstituteVar/Error: Undefined variable ! "
|
|---|
| 967 | << " ($" << vn << ")" << endl;
|
|---|
| 968 | vv = ""; return(false);
|
|---|
| 969 | }
|
|---|
| 970 | vv = ArgsStack.top()[ka]; return(true);
|
|---|
| 971 | }
|
|---|
| 972 | else if (vn[0] == '#') { // Variable de type $#vname --> size(vname)
|
|---|
| 973 | vn = vn.substr(1);
|
|---|
| 974 | if (!omg.HasVar(vn) ) {
|
|---|
| 975 | cerr << " PIACmd::SubstituteVarError: Undefined variable "
|
|---|
| 976 | << vn << " ! " << endl;
|
|---|
| 977 | vv = ""; return(false);
|
|---|
| 978 | }
|
|---|
| 979 | vn = omg.GetVar(vn);
|
|---|
| 980 | vector<string> vs;
|
|---|
| 981 | FillVStringFrString(vn, vs);
|
|---|
| 982 | char buff[32];
|
|---|
| 983 | sprintf(buff,"%d", (int)vs.size());
|
|---|
| 984 | vv = buff; return(true);
|
|---|
| 985 | }
|
|---|
| 986 | else { // variable ordinaire geree par NamedObjMgr
|
|---|
| 987 | if ( (!omg.HasVar(vn)) ) {
|
|---|
| 988 | cerr << " PIACmd::SubstituteVarError: Undefined variable "
|
|---|
| 989 | << vn << " ! " << endl;
|
|---|
| 990 | vv = ""; return(false);
|
|---|
| 991 | }
|
|---|
| 992 | vv = omg.GetVar(vn); return(true);
|
|---|
| 993 | }
|
|---|
| 994 |
|
|---|
| 995 | return(false);
|
|---|
| 996 | }
|
|---|
| 997 |
|
|---|
| 998 | /* --Methode-- */
|
|---|
| 999 | int PIACmd::EvaluateTest(vector<string> & args, string & line, bool & res)
|
|---|
| 1000 | {
|
|---|
| 1001 | res = true;
|
|---|
| 1002 | if ((args.size() != 6) || (args[5] != "then") ||
|
|---|
| 1003 | (args[0] != "(") || (args[4] != ")") ) return(1);
|
|---|
| 1004 | if (args[2] == "==") res = (args[1] == args[3]);
|
|---|
| 1005 | else if (args[2] == "!=") res = (args[1] != args[3]);
|
|---|
| 1006 | else if (args[2] == "<")
|
|---|
| 1007 | res = (atof(args[1].c_str()) < atof(args[3].c_str()));
|
|---|
| 1008 | else if (args[2] == ">")
|
|---|
| 1009 | res = (atof(args[1].c_str()) > atof(args[3].c_str()));
|
|---|
| 1010 | else if (args[2] == "<=")
|
|---|
| 1011 | res = (atof(args[1].c_str()) <= atof(args[3].c_str()));
|
|---|
| 1012 | else if (args[2] == ">=")
|
|---|
| 1013 | res = (atof(args[1].c_str()) >= atof(args[3].c_str()));
|
|---|
| 1014 | else return(2);
|
|---|
| 1015 | return(0);
|
|---|
| 1016 | }
|
|---|
| 1017 |
|
|---|
| 1018 | /* --Methode-- */
|
|---|
| 1019 | void PIACmd::PushStack(vector<string>& args)
|
|---|
| 1020 | {
|
|---|
| 1021 | // We push the argument list (args) on the stack
|
|---|
| 1022 | ArgsStack.push(args);
|
|---|
| 1023 | // We push also PIACmdBloc and testresult on the stack
|
|---|
| 1024 | CmdBlks.push(NULL);
|
|---|
| 1025 | list<char> xtx;
|
|---|
| 1026 | TestsStack.push(xtx);
|
|---|
| 1027 |
|
|---|
| 1028 | }
|
|---|
| 1029 |
|
|---|
| 1030 | /* --Methode-- */
|
|---|
| 1031 | void PIACmd::PopStack(bool psta)
|
|---|
| 1032 | {
|
|---|
| 1033 | // We remove the argument list (args) from the stack
|
|---|
| 1034 | if (psta) ArgsStack.pop();
|
|---|
| 1035 | // And PIACmdBloc and TestResult from the corresponding stacks
|
|---|
| 1036 | PIACmdBloc* curb = CmdBlks.top();
|
|---|
| 1037 | while (curb != NULL) {
|
|---|
| 1038 | PIACmdBloc* parb = curb->Parent();
|
|---|
| 1039 | delete curb; curb = parb;
|
|---|
| 1040 | }
|
|---|
| 1041 | CmdBlks.pop();
|
|---|
| 1042 | TestsStack.pop();
|
|---|
| 1043 | }
|
|---|
| 1044 |
|
|---|
| 1045 | /* --Methode-- */
|
|---|
| 1046 | int PIACmd::ExecuteCommandLine(string & kw, vector<string> & tokens, string & toks)
|
|---|
| 1047 | {
|
|---|
| 1048 | int rc = 0;
|
|---|
| 1049 | NamedObjMgr omg;
|
|---|
| 1050 |
|
|---|
| 1051 | // >>>>>>>>>>> Commande d'interpreteur
|
|---|
| 1052 | if (kw == "helpwindow") ShowHelpWindow();
|
|---|
| 1053 | else if (kw == "help") {
|
|---|
| 1054 | if (tokens.size() > 0) cout << GetUsage(tokens[0]) << endl;
|
|---|
| 1055 | else {
|
|---|
| 1056 | string kwh = "piacmd";
|
|---|
| 1057 | cout << GetUsage(kwh) << endl;
|
|---|
| 1058 | }
|
|---|
| 1059 | }
|
|---|
| 1060 |
|
|---|
| 1061 | else if (kw == "set") {
|
|---|
| 1062 | if (tokens.size() < 2) { cout << "PIACmd::Interpret() Usage: set varname string" << endl; return(0); }
|
|---|
| 1063 | if ((tokens[0].length() < 1) || !isalpha((int)tokens[0][0]) ) {
|
|---|
| 1064 | cerr << "PIACmd::Interpret()/Error Variable name should start with alphabetic" << endl;
|
|---|
| 1065 | return(0);
|
|---|
| 1066 | }
|
|---|
| 1067 | // string xx = tokens[1];
|
|---|
| 1068 | // for (int kk=2; kk<tokens.size(); kk++) xx += (' ' + tokens[kk] );
|
|---|
| 1069 | omg.SetVar(tokens[0], tokens[1]);
|
|---|
| 1070 | }
|
|---|
| 1071 |
|
|---|
| 1072 | else if (kw == "getvar") {
|
|---|
| 1073 | if (tokens.size() < 2) { cout << "PIACmd::Interpret() Usage: getvar newvarname varname" << endl; return(0); }
|
|---|
| 1074 | if (!omg.HasVar(tokens[1])) {
|
|---|
| 1075 | cerr << "Error - No " << tokens[1] << " Variable " << endl;
|
|---|
| 1076 | return(0);
|
|---|
| 1077 | }
|
|---|
| 1078 | if ((tokens[0].length() < 1) || !isalpha((int)tokens[0][0]) ) {
|
|---|
| 1079 | cerr << "PIACmd::Interpret()/Error Variable name should start with alphabetic" << endl;
|
|---|
| 1080 | return(0);
|
|---|
| 1081 | }
|
|---|
| 1082 | omg.SetVar(tokens[0], omg.GetVar(tokens[1]) );
|
|---|
| 1083 | }
|
|---|
| 1084 |
|
|---|
| 1085 | else if (kw == "alias") {
|
|---|
| 1086 | if (tokens.size() < 2) { cout << "PIACmd::Interpret() Usage: alias aliasname string" << endl; return(0); }
|
|---|
| 1087 | if ((tokens[0].length() < 1) || !isalpha((int)tokens[0][0]) ) {
|
|---|
| 1088 | cerr << "PIACmd::Interpret()/Error alias name should start with alphabetic" << endl;
|
|---|
| 1089 | return(0);
|
|---|
| 1090 | }
|
|---|
| 1091 | string xx = tokens[1];
|
|---|
| 1092 | for (int kk=2; kk<tokens.size(); kk++) xx += (' ' + tokens[kk]);
|
|---|
| 1093 | mAliases[tokens[0]] = xx;
|
|---|
| 1094 | }
|
|---|
| 1095 |
|
|---|
| 1096 | else if (kw == "setol") {
|
|---|
| 1097 | if (tokens.size() < 2) { cout << "PIACmd::Interpret() Usage: setol varname objnamepattern" << endl; return(0); }
|
|---|
| 1098 | if ((tokens[0].length() < 1) || !isalpha((int)tokens[0][0]) ) {
|
|---|
| 1099 | cerr << "PIACmd::Interpret()/Error Variable name should start with alphabetic" << endl;
|
|---|
| 1100 | return(0);
|
|---|
| 1101 | }
|
|---|
| 1102 | vector<string> ol;
|
|---|
| 1103 | mObjMgr->GetObjList(tokens[1], ol);
|
|---|
| 1104 | string vol;
|
|---|
| 1105 | if (ol.size() < 1) vol = "";
|
|---|
| 1106 | else {
|
|---|
| 1107 | vol = ol[0];
|
|---|
| 1108 | for (int kk=1; kk<ol.size(); kk++) vol += (' ' + ol[kk]);
|
|---|
| 1109 | }
|
|---|
| 1110 | omg.SetVar(tokens[0], vol);
|
|---|
| 1111 | }
|
|---|
| 1112 | else if (kw == "unset") {
|
|---|
| 1113 | if (tokens.size() < 1) { cout << "PIACmd::Interpret() Usage: unset varname" << endl; return(0); }
|
|---|
| 1114 | if (omg.HasVar(tokens[0])) omg.DeleteVar(tokens[0]) ;
|
|---|
| 1115 | else cerr << "PIACmd::Interpret() No variable with name " << tokens[0] << endl;
|
|---|
| 1116 | }
|
|---|
| 1117 | else if (kw == "echo") {
|
|---|
| 1118 | for (int ii=0; ii<tokens.size(); ii++)
|
|---|
| 1119 | cout << tokens[ii] << " " ;
|
|---|
| 1120 | cout << endl;
|
|---|
| 1121 | }
|
|---|
| 1122 | else if (kw == "readstdin") {
|
|---|
| 1123 | if (tokens.size() < 1) { cout << "PIACmd::Interpret() Usage: readstdin varname" << endl; return(0); }
|
|---|
| 1124 | if ((tokens[0].length() < 1) || !isalpha((int)tokens[0][0]) ) {
|
|---|
| 1125 | cerr << "PIACmd::Interpret()/Error Variable name should start with alphabetic" << endl;
|
|---|
| 1126 | return(0);
|
|---|
| 1127 | }
|
|---|
| 1128 | mImgApp->GetConsole()->AddStr(">>> Reading From StdIn \n", PIVA_Magenta);
|
|---|
| 1129 | cout << tokens[0] << " ? " << endl;
|
|---|
| 1130 | omg.SetVar(tokens[0], GetStringFrStdin(this) );
|
|---|
| 1131 | }
|
|---|
| 1132 |
|
|---|
| 1133 | else if (kw == "listvars") {
|
|---|
| 1134 | cout << "PIACmd::Interpret() Variable List , VarName = Value \n";
|
|---|
| 1135 | DVList& varlist = omg.GetVarList();
|
|---|
| 1136 | DVList::ValList::const_iterator it;
|
|---|
| 1137 | string value;
|
|---|
| 1138 | for(it = varlist.Begin(); it != varlist.End(); it++) {
|
|---|
| 1139 | #ifdef SANS_EVOLPLANCK
|
|---|
| 1140 | MuTyV mtv = (*it).second;
|
|---|
| 1141 | value = (string)(mtv);
|
|---|
| 1142 | #else
|
|---|
| 1143 | value = (string)((*it).second.elval);
|
|---|
| 1144 | #endif
|
|---|
| 1145 | cout << (*it).first << " = " << value << "\n";
|
|---|
| 1146 | }
|
|---|
| 1147 | cout << endl;
|
|---|
| 1148 | }
|
|---|
| 1149 | else if (kw == "listalias") {
|
|---|
| 1150 | cout << "PIACmd::Interpret() Alias List , AliasName = Value \n";
|
|---|
| 1151 | CmdStrList::iterator it;
|
|---|
| 1152 | for(it = mAliases.begin(); it != mAliases.end(); it++)
|
|---|
| 1153 | cout << (*it).first << " = " << (*it).second << "\n";
|
|---|
| 1154 | cout << endl;
|
|---|
| 1155 | }
|
|---|
| 1156 | else if (kw == "listcommands") {
|
|---|
| 1157 | cout << "---- PIACmd::Interpret() Command Variable List ----- \n";
|
|---|
| 1158 | CmdExmap::iterator it;
|
|---|
| 1159 | int kc = 0;
|
|---|
| 1160 | for(it = cmdexmap.begin(); it != cmdexmap.end(); it++) {
|
|---|
| 1161 | cout << (*it).first << " ";
|
|---|
| 1162 | kc++;
|
|---|
| 1163 | if (kc >= 5) { cout << "\n"; kc = 0; }
|
|---|
| 1164 | }
|
|---|
| 1165 | cout << endl;
|
|---|
| 1166 | }
|
|---|
| 1167 | else if (kw == "traceon") { cout << "PIACmd::Interpret() -> Trace ON mode " << endl; trace = true; }
|
|---|
| 1168 | else if (kw == "traceoff") { cout << "PIACmd::Interpret() -> Trace OFF mode " << endl; trace = false; }
|
|---|
| 1169 | else if (kw == "timingon") {
|
|---|
| 1170 | cout << "PIACmd::Interpret() -> Timing ON mode " << endl;
|
|---|
| 1171 | if (gltimer) delete gltimer; gltimer = new Timer("PIA-CmdInterpreter "); timing = true;
|
|---|
| 1172 | }
|
|---|
| 1173 | else if (kw == "timingoff") {
|
|---|
| 1174 | cout << "PIACmd::Interpret() -> Timing OFF mode " << endl;
|
|---|
| 1175 | if (gltimer) delete gltimer; gltimer = NULL; timing = false;
|
|---|
| 1176 | }
|
|---|
| 1177 | else if (kw == "exec") {
|
|---|
| 1178 | if (tokens.size() < 1) { cout << "PIACmd::Interpret() Usage: exec filename" << endl; return(0); }
|
|---|
| 1179 | ExecFile(tokens[0], tokens);
|
|---|
| 1180 | }
|
|---|
| 1181 | else if (kw == "shell") {
|
|---|
| 1182 | if (tokens.size() < 1) { cout << "PIACmd::Interpret() Usage: shell cmdline" << endl; return(0); }
|
|---|
| 1183 | string cmd;
|
|---|
| 1184 | for (int ii=0; ii<tokens.size(); ii++)
|
|---|
| 1185 | cmd += (tokens[ii] + ' ');
|
|---|
| 1186 | system(cmd.c_str());
|
|---|
| 1187 | }
|
|---|
| 1188 | else if (kw == "cshell") {
|
|---|
| 1189 | if(tokens.size()<1) {cout<<"PIACmd::Interpret() Usage: cshell cmdline"<<endl; return(0);}
|
|---|
| 1190 | string cmd="";
|
|---|
| 1191 | for(int ii=0;ii<tokens.size();ii++) cmd+=(tokens[ii]+' ');
|
|---|
| 1192 | CShellExecute(cmd);
|
|---|
| 1193 | }
|
|---|
| 1194 |
|
|---|
| 1195 | // Execution d'une commande enregistree
|
|---|
| 1196 | else rc = ExecuteCommand(kw, tokens, toks);
|
|---|
| 1197 |
|
|---|
| 1198 | if (timing) gltimer->Split();
|
|---|
| 1199 | return(rc);
|
|---|
| 1200 | }
|
|---|
| 1201 |
|
|---|
| 1202 | /* --Methode-- */
|
|---|
| 1203 | int PIACmd::ParseLineExecute(string& line)
|
|---|
| 1204 | {
|
|---|
| 1205 | vector<string> tokens;
|
|---|
| 1206 |
|
|---|
| 1207 | if (line.length() < 1) return(0);
|
|---|
| 1208 |
|
|---|
| 1209 | string toks,kw;
|
|---|
| 1210 | size_t p = line.find_first_not_of(" ");
|
|---|
| 1211 | line = line.substr(p);
|
|---|
| 1212 | p = 0;
|
|---|
| 1213 | size_t q = line.find_first_of(" ");
|
|---|
| 1214 | size_t l = line.length();
|
|---|
| 1215 |
|
|---|
| 1216 | if (q < l)
|
|---|
| 1217 | { kw = line.substr(p,q-p); toks = line.substr(q, l-q); }
|
|---|
| 1218 | else { kw = line.substr(p,l-p); toks = ""; }
|
|---|
| 1219 |
|
|---|
| 1220 | q = 0;
|
|---|
| 1221 | while (q < l) {
|
|---|
| 1222 | p = toks.find_first_not_of(" ",q+1); // au debut d'un token
|
|---|
| 1223 | if (p>=l) break;
|
|---|
| 1224 | q = toks.find_first_of(" ",p); // la fin du token;
|
|---|
| 1225 | string token = toks.substr(p,q-p);
|
|---|
| 1226 | tokens.push_back(token);
|
|---|
| 1227 | }
|
|---|
| 1228 |
|
|---|
| 1229 | return(ExecuteCommand(kw, tokens, toks));
|
|---|
| 1230 | }
|
|---|
| 1231 |
|
|---|
| 1232 | /* --Methode-- */
|
|---|
| 1233 | int PIACmd::ExecuteCommand(string& keyw, vector<string>& args, string& toks)
|
|---|
| 1234 | {
|
|---|
| 1235 | int rc = -1;
|
|---|
| 1236 | CmdExmap::iterator it = cmdexmap.find(keyw);
|
|---|
| 1237 | if (it == cmdexmap.end()) cout << "No such command : " << keyw << " ! " << endl;
|
|---|
| 1238 | else {
|
|---|
| 1239 | if ((*it).second.cex) rc = (*it).second.cex->Execute(keyw, args, toks);
|
|---|
| 1240 | else cout << "Dont know how to execute " << keyw << " ? " << endl;
|
|---|
| 1241 | }
|
|---|
| 1242 | return(rc);
|
|---|
| 1243 | }
|
|---|
| 1244 |
|
|---|
| 1245 | /* --Methode-- */
|
|---|
| 1246 | int PIACmd::ExecFile(string& file, vector<string>& args)
|
|---|
| 1247 | {
|
|---|
| 1248 | char line_buff[512];
|
|---|
| 1249 | FILE *fip;
|
|---|
| 1250 |
|
|---|
| 1251 | if ( (fip = fopen(file.c_str(),"r")) == NULL ) {
|
|---|
| 1252 | if (file.find('.') >= file.length()) {
|
|---|
| 1253 | cout << "PIACmd::Exec(): Error opening file " << file << endl;
|
|---|
| 1254 | file += ".pic";
|
|---|
| 1255 | cout << " Trying file " << file << endl;
|
|---|
| 1256 | fip = fopen(file.c_str(),"r");
|
|---|
| 1257 | }
|
|---|
| 1258 | }
|
|---|
| 1259 |
|
|---|
| 1260 | if(fip == NULL) {
|
|---|
| 1261 | cerr << "PIACmd::Exec() Error opening file " << file << endl;
|
|---|
| 1262 | hist << "##! PIACmd::Exec() Error opening file " << file << endl;
|
|---|
| 1263 | return(0);
|
|---|
| 1264 | }
|
|---|
| 1265 |
|
|---|
| 1266 | // hist << "### Executing commands from " << file << endl;
|
|---|
| 1267 |
|
|---|
| 1268 | PushStack(args);
|
|---|
| 1269 | if (trace) {
|
|---|
| 1270 | mImgApp->GetConsole()->AddStr("### Executing commands from ", PIVA_Magenta);
|
|---|
| 1271 | mImgApp->GetConsole()->AddStr(file.c_str(), PIVA_Magenta);
|
|---|
| 1272 | mImgApp->GetConsole()->AddStr("\n", PIVA_Magenta);
|
|---|
| 1273 | }
|
|---|
| 1274 |
|
|---|
| 1275 | bool ohv = histon;
|
|---|
| 1276 | histon = false;
|
|---|
| 1277 | while (fgets(line_buff,511,fip) != NULL)
|
|---|
| 1278 | {
|
|---|
| 1279 | if (trace) mImgApp->GetConsole()->AddStr(line_buff, PIVA_Magenta);
|
|---|
| 1280 | line_buff[strlen(line_buff)-1] = '\0'; /* LF/CR de la fin */
|
|---|
| 1281 | string line(line_buff);
|
|---|
| 1282 | if (Interpret(line) == 77777) break;
|
|---|
| 1283 | }
|
|---|
| 1284 | histon = ohv;
|
|---|
| 1285 |
|
|---|
| 1286 | // hist << "### End of Exec( " << file << " ) " << endl;
|
|---|
| 1287 | if (trace) {
|
|---|
| 1288 | mImgApp->GetConsole()->AddStr("### End of Exec( ", PIVA_Magenta);
|
|---|
| 1289 | mImgApp->GetConsole()->AddStr(file.c_str(), PIVA_Magenta);
|
|---|
| 1290 | mImgApp->GetConsole()->AddStr(" ) \n", PIVA_Magenta);
|
|---|
| 1291 | }
|
|---|
| 1292 |
|
|---|
| 1293 | PopStack(true);
|
|---|
| 1294 |
|
|---|
| 1295 | return(0);
|
|---|
| 1296 | }
|
|---|
| 1297 |
|
|---|
| 1298 | /* --Methode-- */
|
|---|
| 1299 | int PIACmd::CShellExecute(string cmd)
|
|---|
| 1300 | {
|
|---|
| 1301 | if(cmd.size()<=0) return -1;
|
|---|
| 1302 |
|
|---|
| 1303 | NamedObjMgr omg;
|
|---|
| 1304 | string fname = omg.GetTmpDir(); fname += "cshell_exec_pia.csh";
|
|---|
| 1305 |
|
|---|
| 1306 | string cmdrm = "rm -f " + fname;
|
|---|
| 1307 | system(cmdrm.c_str());
|
|---|
| 1308 |
|
|---|
| 1309 | FILE *fip = fopen(fname.c_str(),"w");
|
|---|
| 1310 | if(fip==NULL) {
|
|---|
| 1311 | cout << "PIACmd/CShellExecute_Error: fopen("<<fname<<") failed"<<endl;
|
|---|
| 1312 | return -2;
|
|---|
| 1313 | }
|
|---|
| 1314 | fprintf(fip,"#!/bin/csh\n\n");
|
|---|
| 1315 | fprintf(fip,"%s\n",cmd.c_str());
|
|---|
| 1316 | fprintf(fip,"\nexit 0\n");
|
|---|
| 1317 | fclose(fip);
|
|---|
| 1318 |
|
|---|
| 1319 | cmd = "csh "; cmd += fname;
|
|---|
| 1320 | system(cmd.c_str());
|
|---|
| 1321 |
|
|---|
| 1322 | system(cmdrm.c_str());
|
|---|
| 1323 |
|
|---|
| 1324 | return 0;
|
|---|
| 1325 | }
|
|---|
| 1326 |
|
|---|
| 1327 | static string* videstr = NULL;
|
|---|
| 1328 | /* --Methode-- */
|
|---|
| 1329 | string& PIACmd::GetUsage(const string& kw)
|
|---|
| 1330 | {
|
|---|
| 1331 | bool fndok = false;
|
|---|
| 1332 | CmdExmap::iterator it = cmdexmap.find(kw);
|
|---|
| 1333 | if (it == cmdexmap.end()) {
|
|---|
| 1334 | it = helpexmap.find(kw);
|
|---|
| 1335 | if (it != helpexmap.end()) fndok = true;
|
|---|
| 1336 | }
|
|---|
| 1337 | else fndok = true;
|
|---|
| 1338 | if (fndok) return( (*it).second.us );
|
|---|
| 1339 | // Keyword pas trouve
|
|---|
| 1340 | if (videstr == NULL) videstr = new string("");
|
|---|
| 1341 | *videstr = "Nothing known about " + kw + " ?? ";
|
|---|
| 1342 | return(*videstr);
|
|---|
| 1343 |
|
|---|
| 1344 | }
|
|---|
| 1345 |
|
|---|
| 1346 | /* --Methode-- */
|
|---|
| 1347 | void PIACmd::ShowHelpWindow()
|
|---|
| 1348 | {
|
|---|
| 1349 | helpwin->Show();
|
|---|
| 1350 | }
|
|---|
| 1351 |
|
|---|
| 1352 | /* --Methode-- */
|
|---|
| 1353 | void PIACmd::ShowCxxOptionWindow()
|
|---|
| 1354 | {
|
|---|
| 1355 | cxxoptwin->Show();
|
|---|
| 1356 | }
|
|---|
| 1357 |
|
|---|
| 1358 | /* --Methode-- */
|
|---|
| 1359 | void PIACmd::ShowCxxExecWindow()
|
|---|
| 1360 | {
|
|---|
| 1361 | cxxexwin->Show();
|
|---|
| 1362 | }
|
|---|
| 1363 |
|
|---|
| 1364 | /* --Methode-- */
|
|---|
| 1365 | void PIACmd::HelptoLaTex(string const & fname)
|
|---|
| 1366 | {
|
|---|
| 1367 | FILE *fip;
|
|---|
| 1368 | if ((fip = fopen(fname.c_str(), "w")) == NULL) {
|
|---|
| 1369 | cout << "PIACmd::HelptoLaTex_Error: fopen( " << fname << endl;
|
|---|
| 1370 | return;
|
|---|
| 1371 | }
|
|---|
| 1372 |
|
|---|
| 1373 | fputs("% ----- Liste des groupes de Help ----- \n",fip);
|
|---|
| 1374 | fputs("List of {\\bf piapp} on-line Help groups: \n", fip);
|
|---|
| 1375 | fputs("\\begin{itemize} \n",fip);
|
|---|
| 1376 | CmdHGroup::iterator it;
|
|---|
| 1377 | for(it = cmdhgrp.begin(); it != cmdhgrp.end(); it++)
|
|---|
| 1378 | fprintf(fip,"\\item {\\bf %s } (p. \\pageref{%s}) \n",
|
|---|
| 1379 | (*it).first.c_str(), (*it).first.c_str());
|
|---|
| 1380 |
|
|---|
| 1381 | fputs("\\end{itemize} \n",fip);
|
|---|
| 1382 |
|
|---|
| 1383 | fputs("\\newpage \n",fip);
|
|---|
| 1384 |
|
|---|
| 1385 | CmdExmap::iterator ite;
|
|---|
| 1386 | fputs("% ----- Liste de toutes les commandes ----- \n",fip);
|
|---|
| 1387 | fputs("\\begin{center} \n ", fip);
|
|---|
| 1388 | fputs("\\rule{2cm}{1mm} List of {\\bf piapp} Help items \\rule{2cm}{1mm} \n", fip);
|
|---|
| 1389 | fputs("\n \n \\vspace{5mm} \n",fip);
|
|---|
| 1390 | fputs("\\begin{tabular}{llllll} \n", fip);
|
|---|
| 1391 | int kt = 0;
|
|---|
| 1392 | for(ite = helpexmap.begin(); ite != helpexmap.end(); ite++) {
|
|---|
| 1393 | fprintf(fip,"%s & p. \\pageref{%s} ", (*ite).first.c_str(), (*ite).first.c_str() );
|
|---|
| 1394 | kt++;
|
|---|
| 1395 | if (kt < 3) fputs(" & ", fip);
|
|---|
| 1396 | else { fputs(" \\\\ \n", fip); kt = 0; }
|
|---|
| 1397 | }
|
|---|
| 1398 | if (kt == 1) fputs(" & & & \\\\ \n", fip);
|
|---|
| 1399 | else if (kt == 2) fputs(" & \\\\ \n", fip);
|
|---|
| 1400 | fputs("\\end{tabular} \n", fip);
|
|---|
| 1401 | fputs("\\end{center} \n", fip);
|
|---|
| 1402 | fputs("\n \n \\vspace{1cm} \n",fip);
|
|---|
| 1403 |
|
|---|
| 1404 | fputs("\\begin{center} \n ", fip);
|
|---|
| 1405 | fputs("\\rule{2cm}{1mm} List of {\\bf piapp} Commands \\rule{2cm}{1mm} \n", fip);
|
|---|
| 1406 | fputs("\n \n \\vspace{5mm} \n",fip);
|
|---|
| 1407 | fputs("\\begin{tabular}{llllll} \n", fip);
|
|---|
| 1408 | kt = 0;
|
|---|
| 1409 | for(ite = cmdexmap.begin(); ite != cmdexmap.end(); ite++) {
|
|---|
| 1410 | fprintf(fip,"%s & p. \\pageref{%s} ", (*ite).first.c_str(), (*ite).first.c_str() );
|
|---|
| 1411 | kt++;
|
|---|
| 1412 | if (kt < 3) fputs(" & ", fip);
|
|---|
| 1413 | else { fputs(" \\\\ \n", fip); kt = 0; }
|
|---|
| 1414 | }
|
|---|
| 1415 | if (kt == 1) fputs(" & & & \\\\ \n", fip);
|
|---|
| 1416 | else if (kt == 2) fputs(" & \\\\ \n", fip);
|
|---|
| 1417 | fputs("\\end{tabular} \n", fip);
|
|---|
| 1418 | fputs("\\end{center} \n", fip);
|
|---|
| 1419 | // fputs("\\newline \n",fip);
|
|---|
| 1420 |
|
|---|
| 1421 | fputs("% ----- Liste des commandes dans chaque groupe ----- \n",fip);
|
|---|
| 1422 | fputs("\\newpage \n",fip);
|
|---|
| 1423 | int gid;
|
|---|
| 1424 | for(it = cmdhgrp.begin(); it != cmdhgrp.end(); it++) {
|
|---|
| 1425 | gid = (*it).second;
|
|---|
| 1426 | if (gid == 0) continue;
|
|---|
| 1427 | fprintf(fip,"\\subsection{%s} \\label{%s} \n",
|
|---|
| 1428 | (*it).first.c_str(), (*it).first.c_str());
|
|---|
| 1429 | for(ite = helpexmap.begin(); ite != helpexmap.end(); ite++) {
|
|---|
| 1430 | if ((*ite).second.group != gid) continue;
|
|---|
| 1431 | fprintf(fip,"{ \\Large $ \\star \\star \\star $ } Help item {\\bf \\Large %s } \\label{%s} \n",
|
|---|
| 1432 | (*ite).first.c_str(), (*ite).first.c_str());
|
|---|
| 1433 | fputs("\\begin{verbatim} \n",fip);
|
|---|
| 1434 | fprintf(fip,"%s\n", (*ite).second.us.c_str());
|
|---|
| 1435 | fputs("\\end{verbatim} \n",fip);
|
|---|
| 1436 | }
|
|---|
| 1437 | for(ite = cmdexmap.begin(); ite != cmdexmap.end(); ite++) {
|
|---|
| 1438 | if ((*ite).second.group != gid) continue;
|
|---|
| 1439 | fprintf(fip,"{ \\Large $ \\star \\star \\star $ } Command {\\bf \\Large %s } \\label{%s} \n",
|
|---|
| 1440 | (*ite).first.c_str(), (*ite).first.c_str());
|
|---|
| 1441 | fputs("\\begin{verbatim} \n",fip);
|
|---|
| 1442 | fprintf(fip,"%s\n", (*ite).second.us.c_str());
|
|---|
| 1443 | fputs("\\end{verbatim} \n",fip);
|
|---|
| 1444 | }
|
|---|
| 1445 | }
|
|---|
| 1446 |
|
|---|
| 1447 | fclose(fip);
|
|---|
| 1448 | return;
|
|---|
| 1449 | }
|
|---|
| 1450 |
|
|---|
| 1451 |
|
|---|
| 1452 |
|
|---|