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

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

Ajout help et amelioration Help2TeX , Reza 21/10/99

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