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