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

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

fenetre pour CxxExecutor - Reza 24/10/2000

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