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