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

Last change on this file since 1268 was 1268, checked in by ercodmgr, 25 years ago

3ieme argument string& toks pour Execute et ExecuteCommand
preparation gestion TmpDir dans cxxexecutor
suppression de ExecuteCXX ds cxxexecutor et appels
possibilite d'ajouter des userfct.cc ds cxxexecutor

cmv 1/11/00

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