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