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