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