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