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