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