[293] | 1 | #include "piacmd.h"
|
---|
[165] | 2 | #include <stdio.h>
|
---|
| 3 | #include <stdlib.h>
|
---|
[349] | 4 | #include <ctype.h>
|
---|
[165] | 5 | #include <math.h>
|
---|
| 6 |
|
---|
[293] | 7 | #include "basexecut.h"
|
---|
[165] | 8 |
|
---|
[293] | 9 | #include "pdlmgr.h"
|
---|
[165] | 10 | #include "ctimer.h"
|
---|
[293] | 11 | // #include "dlftypes.h"
|
---|
[165] | 12 |
|
---|
[293] | 13 | #include "pistdimgapp.h"
|
---|
[165] | 14 | #include "nobjmgr.h"
|
---|
[361] | 15 | #include "piafitting.h"
|
---|
[463] | 16 | #include "pawexecut.h"
|
---|
[1224] | 17 | #include "cxxexecutor.h"
|
---|
[165] | 18 |
|
---|
[293] | 19 | #include PISTDWDG_H
|
---|
| 20 | #include PILIST_H
|
---|
[165] | 21 |
|
---|
[293] | 22 | // ------------------------------------------------------------
|
---|
[349] | 23 | // Gestion d'une fenetre d'aide interactive
|
---|
| 24 | // Classe PIAHelpWind
|
---|
[293] | 25 | // ------------------------------------------------------------
|
---|
[165] | 26 |
|
---|
[293] | 27 | class PIAHelpWind : public PIWindow {
|
---|
| 28 | public :
|
---|
| 29 | PIAHelpWind(PIStdImgApp* par, PIACmd* piacmd);
|
---|
| 30 | virtual ~PIAHelpWind();
|
---|
[330] | 31 | virtual void Show();
|
---|
[293] | 32 | virtual void Process(PIMessage msg, PIMsgHandler* sender, void* data=NULL);
|
---|
[330] | 33 | inline void AddHelpGroup(const char * hgrp, int gid)
|
---|
| 34 | { hgrpom->AppendItem(hgrp, 20000+gid); }
|
---|
| 35 | inline void ClearHelpList()
|
---|
| 36 | { mNitem=0; hitemlist->DeleteAllItems(); }
|
---|
[293] | 37 | inline void AddHelpItem(const char * hitem)
|
---|
[330] | 38 | { mNitem++; hitemlist->AppendItem(hitem, 100+mNitem); }
|
---|
[293] | 39 | protected :
|
---|
| 40 | PIStdImgApp* dap;
|
---|
| 41 | PIACmd* piac;
|
---|
| 42 | int mNitem;
|
---|
| 43 | PIList* hitemlist;
|
---|
[330] | 44 | PIOptMenu* hgrpom;
|
---|
[293] | 45 | PIButton * mBut;
|
---|
| 46 | PILabel * mLab;
|
---|
| 47 | PIText* mTxt;
|
---|
| 48 | };
|
---|
[165] | 49 |
|
---|
[293] | 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;
|
---|
[330] | 57 | SetMsg(77);
|
---|
[165] | 58 |
|
---|
[293] | 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);
|
---|
[330] | 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);
|
---|
[293] | 69 | hitemlist->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
|
---|
| 70 | // hitemlist->SetBorderWidth(2);
|
---|
[324] | 71 | mTxt = new PIText(this, "helptext", true, true, bsx*8.0, 6*bsy, bsx*2.0+1.5*spx, spy);
|
---|
| 72 | // mTxt->SetMutiLineMode(true);
|
---|
[293] | 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("");
|
---|
[330] | 80 | mBut = new PIButton(this, "Close", 70, bsx, bsy, tsx-bsx*1.5-spx, tsy-spy-bsy);
|
---|
[293] | 81 | mBut->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
|
---|
| 82 | }
|
---|
[165] | 83 |
|
---|
| 84 | /* --Methode-- */
|
---|
[293] | 85 | PIAHelpWind::~PIAHelpWind()
|
---|
| 86 | {
|
---|
[330] | 87 | delete hgrpom;
|
---|
[293] | 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);
|
---|
[330] | 98 | if (((um == 77) && (ModMsg(msg) == PIMsg_Close)) || (um == 70) ) {
|
---|
[293] | 99 | Hide();
|
---|
| 100 | return;
|
---|
| 101 | }
|
---|
[330] | 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) ) {
|
---|
[293] | 108 | string s = hitemlist->GetSelectionStr();
|
---|
| 109 | mTxt->SetText(piac->GetUsage(s));
|
---|
| 110 | mLab->SetLabel(s);
|
---|
| 111 | }
|
---|
| 112 | }
|
---|
| 113 |
|
---|
[330] | 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 | }
|
---|
[293] | 123 |
|
---|
[349] | 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 |
|
---|
[293] | 267 | static PIACmd* curpiacmd = NULL;
|
---|
| 268 | /* --Methode-- */
|
---|
[165] | 269 | PIACmd::PIACmd(NamedObjMgr* omg, PIStdImgApp* app)
|
---|
| 270 | {
|
---|
[293] | 271 | mObjMgr = omg;
|
---|
[165] | 272 | mImgApp = app;
|
---|
| 273 | system("cp history.pic hisold.pic");
|
---|
| 274 | hist.open("history.pic");
|
---|
[349] | 275 | histon = true;
|
---|
[165] | 276 | trace = false; timing = false;
|
---|
| 277 | gltimer = NULL;
|
---|
[349] | 278 | curblk = NULL;
|
---|
| 279 | felevel = 0;
|
---|
[293] | 280 |
|
---|
[330] | 281 | cmdhgrp["All"] = 0;
|
---|
| 282 | cmdgrpid = 1;
|
---|
| 283 | cmdhgrp["Commands"] = 1;
|
---|
[293] | 284 | helpwin = new PIAHelpWind(app, this);
|
---|
[330] | 285 | helpwin->AddHelpGroup("All", 0);
|
---|
| 286 | helpwin->AddHelpGroup("Commands", 1);
|
---|
[293] | 287 |
|
---|
| 288 | string kw = "piacmd";
|
---|
| 289 | string usage;
|
---|
| 290 | usage = ">>> (piacmd) Interpreter's keywords : \n";
|
---|
[349] | 291 | usage += " > set varname string # To set a variable, $varname \n";
|
---|
| 292 | usage += " > get newvarname varname # To set a newvariable, equal to $varname \n";
|
---|
[333] | 293 | usage += " > setol varname patt # Fills varname with object list \n";
|
---|
[293] | 294 | usage += " > unset varname # clear variable definition \n";
|
---|
[333] | 295 | usage += " > echo string # output string \n";
|
---|
[353] | 296 | usage += " > alias name string # define a command alias \n";
|
---|
| 297 | usage += " > readstdin varname # reads a line from stdin into $varname \n";
|
---|
[349] | 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";
|
---|
[353] | 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";
|
---|
[293] | 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";
|
---|
[349] | 309 | usage += " > timingon timingoff traceon traceoff \n";
|
---|
[330] | 310 | string grp = "Commands";
|
---|
| 311 | RegisterHelp(kw, usage, grp);
|
---|
[293] | 312 |
|
---|
| 313 | basexec = new PIABaseExecutor(this, omg, app);
|
---|
[361] | 314 | fitexec = new PIAFitter(this, app);
|
---|
[463] | 315 | pawexec = new PAWExecutor(this, app);
|
---|
[1224] | 316 | cxxexec = new CxxExecutor(this, app);
|
---|
[293] | 317 | AddInterpreter(this);
|
---|
| 318 | curcmdi = this;
|
---|
[165] | 319 | }
|
---|
| 320 |
|
---|
| 321 | /* --Methode-- */
|
---|
| 322 | PIACmd::~PIACmd()
|
---|
| 323 | {
|
---|
| 324 | hist.close();
|
---|
| 325 | if (gltimer) { delete gltimer; gltimer = NULL; }
|
---|
[293] | 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;
|
---|
[361] | 335 | delete basexec;
|
---|
| 336 | delete fitexec;
|
---|
[463] | 337 | delete pawexec;
|
---|
[1224] | 338 | delete cxxexec;
|
---|
[165] | 339 | }
|
---|
| 340 |
|
---|
| 341 | /* --Methode-- */
|
---|
[293] | 342 | PIACmd* PIACmd::GetInterpreter()
|
---|
[165] | 343 | {
|
---|
[293] | 344 | return(curpiacmd);
|
---|
| 345 | }
|
---|
[165] | 346 |
|
---|
[293] | 347 | /* --Methode-- */
|
---|
| 348 | string PIACmd::Name()
|
---|
| 349 | {
|
---|
| 350 | return("piacmd");
|
---|
| 351 | }
|
---|
[165] | 352 |
|
---|
[293] | 353 | /* --Methode-- */
|
---|
[330] | 354 | void PIACmd::RegisterCommand(string& keyw, string& usage, CmdExecutor * ce, string grp)
|
---|
[293] | 355 | {
|
---|
[330] | 356 | if (!ce) {
|
---|
| 357 | RegisterHelp(keyw, usage, grp);
|
---|
| 358 | return;
|
---|
| 359 | }
|
---|
| 360 | int gid = CheckHelpGrp(grp);
|
---|
[293] | 361 | cmdex cme;
|
---|
[330] | 362 | cme.group = gid;
|
---|
[293] | 363 | cme.us = usage;
|
---|
| 364 | cme.cex = ce;
|
---|
| 365 | cmdexmap[keyw] = cme;
|
---|
| 366 | }
|
---|
| 367 |
|
---|
| 368 | /* --Methode-- */
|
---|
[330] | 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-- */
|
---|
[293] | 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;
|
---|
[165] | 415 | }
|
---|
[293] | 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;
|
---|
[165] | 421 | }
|
---|
[293] | 422 | cout << "PIACmd/LoadModule_Info: Initialisation module" << name
|
---|
| 423 | << " " << fname << "() ..." << endl;
|
---|
| 424 | finit();
|
---|
| 425 | modmap[name] = dynlink;
|
---|
| 426 | return;
|
---|
| 427 | }
|
---|
[165] | 428 |
|
---|
[293] | 429 | /* --Methode-- */
|
---|
| 430 | void PIACmd::AddInterpreter(CmdInterpreter * cl)
|
---|
| 431 | {
|
---|
| 432 | if (!cl) return;
|
---|
[349] | 433 | interpmap[cl->Name()] = cl;}
|
---|
[165] | 434 |
|
---|
| 435 | /* --Methode-- */
|
---|
[293] | 436 | void PIACmd::SelInterpreter(string& name)
|
---|
[165] | 437 | {
|
---|
[293] | 438 | InterpMap::iterator it = interpmap.find(name);
|
---|
| 439 | if (it == interpmap.end()) return;
|
---|
| 440 | curcmdi = (*it).second;
|
---|
| 441 | }
|
---|
[165] | 442 |
|
---|
[293] | 443 |
|
---|
| 444 | // Pour le decoupage des commandes en lignes
|
---|
| 445 | typedef vector<string> cmdtok;
|
---|
| 446 |
|
---|
[357] | 447 | /* Fonction */
|
---|
[368] | 448 | static string GetStringFrStdin(PIACmd* piac)
|
---|
[357] | 449 | {
|
---|
[368] | 450 | PIStdImgApp* piapp = piac->GetImgApp();
|
---|
| 451 | if (piapp) {
|
---|
| 452 | PIBaseWdg* wdg = piapp->CurrentBaseWdg();
|
---|
| 453 | if (wdg) wdg->Refresh();
|
---|
[357] | 454 | #ifndef __mac__
|
---|
[368] | 455 | /* On vide le buffer X-Window */
|
---|
[374] | 456 | XSync(PIXDisplay(),False);
|
---|
[357] | 457 | #endif
|
---|
[368] | 458 | }
|
---|
[357] | 459 | char buff[128];
|
---|
| 460 | fgets(buff, 128, stdin);
|
---|
| 461 | buff[127] = '\0';
|
---|
| 462 | return((string)buff);
|
---|
| 463 | }
|
---|
| 464 |
|
---|
[293] | 465 | /* --Methode-- */
|
---|
| 466 | int PIACmd::Interpret(string& s)
|
---|
| 467 | {
|
---|
[333] | 468 | int rc = 0;
|
---|
[165] | 469 | cmdtok tokens;
|
---|
[349] | 470 | CmdVarList::iterator it;
|
---|
[165] | 471 |
|
---|
[349] | 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);
|
---|
[165] | 480 |
|
---|
[349] | 481 | // On enregistre les commandes
|
---|
| 482 | if (histon) hist << s << endl;
|
---|
| 483 |
|
---|
| 484 |
|
---|
[165] | 485 | string toks,kw;
|
---|
| 486 |
|
---|
[349] | 487 | // >>>> Substitution d'alias (1er mot)
|
---|
[449] | 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);
|
---|
[349] | 498 | p = 0;
|
---|
| 499 | q = s.find_first_of(" ");
|
---|
| 500 | }
|
---|
| 501 |
|
---|
| 502 | // >>>> Separating keyword
|
---|
[165] | 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 |
|
---|
[349] | 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];
|
---|
[353] | 521 | sprintf(prompt, "foreach-%d? ", felevel);
|
---|
[349] | 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 |
|
---|
[165] | 598 | q = 0;
|
---|
| 599 | while (q < l) {
|
---|
[349] | 600 | p = toks.find_first_not_of(" \t",q+1); // au debut d'un token
|
---|
[165] | 601 | if (p>=l) break;
|
---|
[349] | 602 | q = toks.find_first_of(" \t",p); // la fin du token;
|
---|
[165] | 603 | string token = toks.substr(p,q-p);
|
---|
| 604 | tokens.push_back(token);
|
---|
| 605 | }
|
---|
| 606 |
|
---|
[349] | 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);
|
---|
[293] | 627 | }
|
---|
[165] | 628 |
|
---|
[349] | 629 |
|
---|
[165] | 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
|
---|
[349] | 635 | else if (kw == "helpwindow") ShowHelpWindow();
|
---|
[293] | 636 | else if (kw == "help") {
|
---|
[330] | 637 | if (tokens.size() > 0) cout << GetUsage(tokens[0]) << endl;
|
---|
[293] | 638 | else {
|
---|
[330] | 639 | string kwh = "piacmd";
|
---|
| 640 | cout << GetUsage(kwh) << endl;
|
---|
[293] | 641 | }
|
---|
[165] | 642 | }
|
---|
| 643 |
|
---|
| 644 | else if (kw == "set") {
|
---|
[293] | 645 | if (tokens.size() < 2) { cout << "PIACmd::Interpret() Usage: set varname string" << endl; return(0); }
|
---|
[349] | 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] );
|
---|
[333] | 652 | mVars[tokens[0]] = xx;
|
---|
[165] | 653 | }
|
---|
[349] | 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 |
|
---|
[333] | 680 | else if (kw == "setol") {
|
---|
| 681 | if (tokens.size() < 2) { cout << "PIACmd::Interpret() Usage: setol varname objnamepattern" << endl; return(0); }
|
---|
[349] | 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 | }
|
---|
[333] | 686 | vector<string> ol;
|
---|
[349] | 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 | }
|
---|
[333] | 694 | mVars[tokens[0]] = vol;
|
---|
| 695 | }
|
---|
[293] | 696 | else if (kw == "unset") {
|
---|
| 697 | if (tokens.size() < 1) { cout << "PIACmd::Interpret() Usage: unset varname" << endl; return(0); }
|
---|
[165] | 698 | CmdVarList::iterator it = mVars.find(tokens[0]);
|
---|
| 699 | if (it != mVars.end()) mVars.erase(it);
|
---|
[293] | 700 | else cerr << "PIACmd::Interpret() No variable with name " << tokens[0] << endl;
|
---|
[165] | 701 | }
|
---|
[333] | 702 | else if (kw == "echo") {
|
---|
| 703 | for (int kk=0; kk<tokens.size(); kk++) cout << tokens[kk] << " " ;
|
---|
| 704 | cout << endl;
|
---|
| 705 | }
|
---|
[353] | 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 | }
|
---|
[357] | 713 | mImgApp->GetConsole()->AddStr(">>> Reading From StdIn \n", PIVA_Magenta);
|
---|
| 714 | cout << tokens[0] << " ? " << endl;
|
---|
[368] | 715 | mVars[tokens[0]] = GetStringFrStdin(this);
|
---|
[353] | 716 | }
|
---|
| 717 |
|
---|
[165] | 718 | else if (kw == "listvars") {
|
---|
[293] | 719 | cout << "PIACmd::Interpret() Variable List , VarName = Value \n";
|
---|
[165] | 720 | CmdVarList::iterator it;
|
---|
| 721 | for(it = mVars.begin(); it != mVars.end(); it++)
|
---|
| 722 | cout << (*it).first << " = " << (*it).second << "\n";
|
---|
| 723 | cout << endl;
|
---|
| 724 | }
|
---|
[353] | 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") {
|
---|
[330] | 733 | cout << "---- PIACmd::Interpret() Command Variable List ----- \n";
|
---|
[293] | 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; }
|
---|
[165] | 745 | else if (kw == "timingon") {
|
---|
[293] | 746 | cout << "PIACmd::Interpret() -> Timing ON mode " << endl;
|
---|
| 747 | if (gltimer) delete gltimer; gltimer = new Timer("PIA-CmdInterpreter "); timing = true;
|
---|
| 748 | }
|
---|
[165] | 749 | else if (kw == "timingoff") {
|
---|
[293] | 750 | cout << "PIACmd::Interpret() -> Timing OFF mode " << endl;
|
---|
| 751 | if (gltimer) delete gltimer; gltimer = NULL; timing = false;
|
---|
| 752 | }
|
---|
[165] | 753 | else if (kw == "exec") {
|
---|
[293] | 754 | if (tokens.size() < 1) { cout << "PIACmd::Interpret() Usage: exec filename" << endl; return(0); }
|
---|
[349] | 755 | ExecFile(tokens[0], tokens);
|
---|
[165] | 756 | }
|
---|
| 757 | else if (kw == "shell") {
|
---|
[293] | 758 | if (tokens.size() < 1) { cout << "PIACmd::Interpret() Usage: shell cmdline" << endl; return(0); }
|
---|
[165] | 759 | system(toks.c_str());
|
---|
| 760 | }
|
---|
[293] | 761 | // Execution d'une commande enregistree
|
---|
[333] | 762 | else rc = ExecuteCommand(kw, tokens);
|
---|
[165] | 763 |
|
---|
| 764 | if (timing) gltimer->Split();
|
---|
[333] | 765 | return(rc);
|
---|
[165] | 766 | }
|
---|
| 767 |
|
---|
[333] | 768 | /* --Methode-- */
|
---|
| 769 | int PIACmd::ExecuteCommandLine(string& line)
|
---|
| 770 | {
|
---|
| 771 | cmdtok tokens;
|
---|
| 772 | if (line.length() < 1) return(0);
|
---|
[165] | 773 |
|
---|
[333] | 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 |
|
---|
[165] | 797 | /* --Methode-- */
|
---|
[333] | 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-- */
|
---|
[349] | 811 | int PIACmd::ExecFile(string& file, vector<string>& args)
|
---|
[165] | 812 | {
|
---|
[293] | 813 | char line_buff[512];
|
---|
| 814 | FILE *fip;
|
---|
[165] | 815 |
|
---|
[293] | 816 | if ( (fip = fopen(file.c_str(),"r")) == NULL ) {
|
---|
[384] | 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) {
|
---|
[293] | 826 | cerr << "PIACmd::Exec() Error opening file " << file << endl;
|
---|
| 827 | hist << "##! PIACmd::Exec() Error opening file " << file << endl;
|
---|
| 828 | return(0);
|
---|
[165] | 829 | }
|
---|
[349] | 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 |
|
---|
[293] | 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 | }
|
---|
[165] | 862 |
|
---|
[349] | 863 | histon = false;
|
---|
[293] | 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);
|
---|
[165] | 870 | }
|
---|
[357] | 871 | histon = true;
|
---|
[349] | 872 |
|
---|
| 873 | // hist << "### End of Exec( " << file << " ) " << endl;
|
---|
[293] | 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);
|
---|
[165] | 878 | }
|
---|
| 879 |
|
---|
| 880 | return(0);
|
---|
| 881 | }
|
---|
| 882 |
|
---|
[293] | 883 |
|
---|
| 884 | static string* videstr = NULL;
|
---|
| 885 | /* --Methode-- */
|
---|
| 886 | string& PIACmd::GetUsage(const string& kw)
|
---|
[165] | 887 | {
|
---|
[330] | 888 | bool fndok = false;
|
---|
[293] | 889 | CmdExmap::iterator it = cmdexmap.find(kw);
|
---|
[330] | 890 | if (it == cmdexmap.end()) {
|
---|
| 891 | it = helpexmap.find(kw);
|
---|
| 892 | if (it != helpexmap.end()) fndok = true;
|
---|
[165] | 893 | }
|
---|
[330] | 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 |
|
---|
[165] | 901 | }
|
---|
| 902 |
|
---|
[293] | 903 | /* --Methode-- */
|
---|
| 904 | void PIACmd::ShowHelpWindow()
|
---|
[165] | 905 | {
|
---|
[293] | 906 | helpwin->Show();
|
---|
[165] | 907 | }
|
---|
[357] | 908 |
|
---|
[463] | 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 | }
|
---|
[357] | 917 |
|
---|
[463] | 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++)
|
---|
[484] | 923 | fprintf(fip,"\\item {\\bf %s } (p. \\pageref{%s}) \n",
|
---|
| 924 | (*it).first.c_str(), (*it).first.c_str());
|
---|
[463] | 925 |
|
---|
| 926 | fputs("\\end{itemize} \n",fip);
|
---|
| 927 |
|
---|
[484] | 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 |
|
---|
[463] | 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;
|
---|
[484] | 972 | fprintf(fip,"\\subsection{%s} \\label{%s} \n",
|
---|
| 973 | (*it).first.c_str(), (*it).first.c_str());
|
---|
[463] | 974 | for(ite = helpexmap.begin(); ite != helpexmap.end(); ite++) {
|
---|
| 975 | if ((*ite).second.group != gid) continue;
|
---|
[484] | 976 | fprintf(fip,"{ \\Large $ \\star \\star \\star $ } Help item {\\bf \\Large %s } \\label{%s} \n",
|
---|
| 977 | (*ite).first.c_str(), (*ite).first.c_str());
|
---|
[463] | 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;
|
---|
[484] | 984 | fprintf(fip,"{ \\Large $ \\star \\star \\star $ } Command {\\bf \\Large %s } \\label{%s} \n",
|
---|
| 985 | (*ite).first.c_str(), (*ite).first.c_str());
|
---|
[463] | 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 |
|
---|