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