source: Sophya/trunk/SophyaPI/PIext/piacmd.cc@ 361

Last change on this file since 361 was 361, checked in by ercodmgr, 26 years ago

Deplacement des methodes d'ajustement dans nouvelle classe Reza 6/8/99

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