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