#include "piacmd.h" #include #include #include #include #include "basexecut.h" #include "pdlmgr.h" #include "ctimer.h" // #include "dlftypes.h" #include "pistdimgapp.h" #include "nobjmgr.h" #include "piafitting.h" #include PISTDWDG_H #include PILIST_H // ------------------------------------------------------------ // Gestion d'une fenetre d'aide interactive // Classe PIAHelpWind // ------------------------------------------------------------ class PIAHelpWind : public PIWindow { public : PIAHelpWind(PIStdImgApp* par, PIACmd* piacmd); virtual ~PIAHelpWind(); virtual void Show(); virtual void Process(PIMessage msg, PIMsgHandler* sender, void* data=NULL); inline void AddHelpGroup(const char * hgrp, int gid) { hgrpom->AppendItem(hgrp, 20000+gid); } inline void ClearHelpList() { mNitem=0; hitemlist->DeleteAllItems(); } inline void AddHelpItem(const char * hitem) { mNitem++; hitemlist->AppendItem(hitem, 100+mNitem); } protected : PIStdImgApp* dap; PIACmd* piac; int mNitem; PIList* hitemlist; PIOptMenu* hgrpom; PIButton * mBut; PILabel * mLab; PIText* mTxt; }; /* --Methode-- */ PIAHelpWind::PIAHelpWind(PIStdImgApp *par, PIACmd* piacmd) : PIWindow((PIMsgHandler *)par, "Help-PIApp", PIWK_normal, 400, 300, 100, 350) { dap = par; piac = piacmd; mNitem = 0; SetMsg(77); int bsx, bsy; int tsx, tsy; int spx, spy; PIApplicationPrefCompSize(bsx, bsy); spx = bsx/6; spy = bsy/6; tsx = 10*bsx+2*spx; tsy = 7*bsy+3*spy; SetSize(tsx,tsy); hgrpom = new PIOptMenu(this, "hgrpoptmen", bsx*2.0, bsy, spx/2, spy); hgrpom->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic); hitemlist = new PIList(this, "hitemlist", bsx*2.0, tsy-3*spy-bsy, spx/2, 2*spy+bsy); hitemlist->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic); // hitemlist->SetBorderWidth(2); mTxt = new PIText(this, "helptext", true, true, bsx*8.0, 6*bsy, bsx*2.0+1.5*spx, spy); // mTxt->SetMutiLineMode(true); mTxt->SetTextEditable(false); mTxt->SetText(""); mTxt->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic); mLab = new PILabel(this, "helpitem", bsx*4, bsy, bsx*2.5+2*spx, tsy-spy-bsy); mLab->SetBorderWidth(1); mLab->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic); mLab->SetLabel(""); mBut = new PIButton(this, "Close", 70, bsx, bsy, tsx-bsx*1.5-spx, tsy-spy-bsy); mBut->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic); } /* --Methode-- */ PIAHelpWind::~PIAHelpWind() { delete hgrpom; delete hitemlist; delete mTxt; delete mLab; delete mBut; } /* --Methode-- */ void PIAHelpWind::Process(PIMessage msg, PIMsgHandler* sender, void* /*data*/) { PIMessage um = UserMsg(msg); if (((um == 77) && (ModMsg(msg) == PIMsg_Close)) || (um == 70) ) { Hide(); return; } else if ( (um >= 20000) && (sender == hgrpom)) { // Selection de groupe de Help mTxt->SetText(""); mLab->SetLabel(""); piac->UpdateHelpList(this, um-20000); } else if ( (um > 100) && (sender == hitemlist) && (ModMsg(msg) == PIMsg_Select) ) { string s = hitemlist->GetSelectionStr(); mTxt->SetText(piac->GetUsage(s)); mLab->SetLabel(s); } } /* --Methode-- */ void PIAHelpWind::Show() { hgrpom->SetValue(20000); // Groupe All mTxt->SetText(""); mLab->SetLabel(""); piac->UpdateHelpList(this, 0); PIWindow::Show(); } // ------------------------------------------------------------ // Bloc de commandes (Foreach, ...) // Classe PIACmdBloc // ------------------------------------------------------------ class PIACmdBloc { public: PIACmdBloc(PIACmd* piac, PIACmdBloc* par, vector& args); ~PIACmdBloc(); inline PIACmdBloc* Parent() { return(parent); } inline bool CheckOK() { return((typ >= 0) ? true : false); } inline void AddLine(string& line) { lines.push_back(line); bloclineid.push_back(lines.size()); } inline void AddBloc(PIACmdBloc* blk) { blocs.push_back(blk); bloclineid.push_back(-blocs.size()); } PIACmdBloc* Execute(); protected: PIACmd* piacmd; PIACmdBloc* parent; int typ; // 0 foreach , 1 integer loop, 2 float loop string varname; vector strlist; vector lines; vector blocs; vector bloclineid; int i1,i2,di; float f1,f2,df; }; /* --Methode-- */ PIACmdBloc::PIACmdBloc(PIACmd* piac, PIACmdBloc* par, vector& args) { piacmd = piac; parent = par; typ = -1; i1 = 0; i2 = -1; di = 1; f1 = 0.; f2 = -1.; df = 1.; if ((args.size() < 2) || !isalpha((int)args[0][0]) ) return; varname = args[0]; if (isalpha((int)args[1][0]) ) { // This is a foreach-integer bloc for(int kk=1; kk= l) return; // Syntaxe error string a1 = args[1].substr(0, p); string aa = args[1].substr(p+1); p = aa.find(':'); string a2, a3; bool hasa3 = false; if (p < aa.length() ) { a2 = aa.substr(0,p); a3 = aa.substr(p+1); hasa3 = true; } else a2 = aa; if (fl) { typ = 2; f1 = atof(a1.c_str()); f2 = atof(a2.c_str()); if (hasa3) df = atof(a3.c_str()); else df = 1.; } else { typ = 1; i1 = atoi(a1.c_str()); i2 = atoi(a2.c_str()); if (hasa3) di = atoi(a3.c_str()); else di = 1; } } } /* --Methode-- */ PIACmdBloc::~PIACmdBloc() { for(int k=0; kInterpret(cmd); for(kj=0; kj 0) piacmd->Interpret(lines[kk-1]); else blocs[-kk-1]->Execute(); } } else if (typ == 1) // Integer loop for(int i=i1; i 9999) { cout << ">>> Maximum PIACmdBloc loop limit (9999) -> break " << endl; break; } sprintf(buff, " %d", i); cmd = "set " + varname + buff; piacmd->Interpret(cmd); for(kj=0; kj 0) piacmd->Interpret(lines[kk-1]); else blocs[-kk-1]->Execute(); } } else if (typ == 2) // float loop for(float f=f1; f 9999) { cout << ">>> Maximum PIACmdBloc loop limit (9999) -> break " << endl; break; } sprintf(buff, " %g", f); cmd = "set " + varname + buff; piacmd->Interpret(cmd); for(kj=0; kj 0) piacmd->Interpret(lines[kk-1]); else blocs[-kk-1]->Execute(); } } return(parent); } // ------------------------------------------------------------ // Classe PIACmd // ------------------------------------------------------------ static PIACmd* curpiacmd = NULL; /* --Methode-- */ PIACmd::PIACmd(NamedObjMgr* omg, PIStdImgApp* app) { mObjMgr = omg; mImgApp = app; system("cp history.pic hisold.pic"); hist.open("history.pic"); histon = true; trace = false; timing = false; gltimer = NULL; curblk = NULL; felevel = 0; cmdhgrp["All"] = 0; cmdgrpid = 1; cmdhgrp["Commands"] = 1; helpwin = new PIAHelpWind(app, this); helpwin->AddHelpGroup("All", 0); helpwin->AddHelpGroup("Commands", 1); string kw = "piacmd"; string usage; usage = ">>> (piacmd) Interpreter's keywords : \n"; usage += " > set varname string # To set a variable, $varname \n"; usage += " > get newvarname varname # To set a newvariable, equal to $varname \n"; usage += " > setol varname patt # Fills varname with object list \n"; usage += " > unset varname # clear variable definition \n"; usage += " > echo string # output string \n"; usage += " > alias name string # define a command alias \n"; usage += " > readstdin varname # reads a line from stdin into $varname \n"; usage += " > foreach varname string-list # Loop \n"; usage += " > foreach varname i1:i2[:di] # Integer loop \n"; usage += " > foreach varname f1:f2[:df] # Float loop \n"; usage += " > end # end loops \n"; usage += " > listvars # List of variable names and values \n"; usage += " > listalias # List of alias names and values \n"; usage += " > listcommands # List of all known commands \n"; usage += " > exec filename # Execute commands from file \n"; usage += " > shell comand_string # Execute shell command \n"; usage += " > help # usage info \n"; usage += " > helpwindow # Displays help window \n"; usage += " > timingon timingoff traceon traceoff \n"; string grp = "Commands"; RegisterHelp(kw, usage, grp); basexec = new PIABaseExecutor(this, omg, app); fitexec = new PIAFitter(this, app); AddInterpreter(this); curcmdi = this; } /* --Methode-- */ PIACmd::~PIACmd() { hist.close(); if (gltimer) { delete gltimer; gltimer = NULL; } Modmap::iterator it; for(it = modmap.begin(); it != modmap.end(); it++) { string name = (*it).first + "_end"; DlModuleInitEndFunction fend = (*it).second->GetFunction(name); if (fend) fend(); delete (*it).second; } delete helpwin; if (curpiacmd == this) curpiacmd = NULL; delete basexec; delete fitexec; } /* --Methode-- */ PIACmd* PIACmd::GetInterpreter() { return(curpiacmd); } /* --Methode-- */ string PIACmd::Name() { return("piacmd"); } /* --Methode-- */ void PIACmd::RegisterCommand(string& keyw, string& usage, CmdExecutor * ce, string grp) { if (!ce) { RegisterHelp(keyw, usage, grp); return; } int gid = CheckHelpGrp(grp); cmdex cme; cme.group = gid; cme.us = usage; cme.cex = ce; cmdexmap[keyw] = cme; } /* --Methode-- */ void PIACmd::RegisterHelp(string& keyw, string& usage, string& grp) { int gid = CheckHelpGrp(grp); cmdex cme; cme.group = gid; cme.us = usage; cme.cex = NULL; helpexmap[keyw] = cme; } /* --Methode-- */ int PIACmd::CheckHelpGrp(string& grp) { int gid=0; CmdHGroup::iterator it = cmdhgrp.find(grp); if (it == cmdhgrp.end()) { cmdgrpid++; gid = cmdgrpid; cmdhgrp[grp] = gid; helpwin->AddHelpGroup(grp.c_str(), gid); } else gid = (*it).second; return(gid); } /* --Methode-- */ void PIACmd::UpdateHelpList(PIAHelpWind* hw, int gid) { helpwin->ClearHelpList(); CmdExmap::iterator it; for(it = helpexmap.begin(); it != helpexmap.end(); it++) { if ( (gid != 0) && ((*it).second.group != gid) ) continue; helpwin->AddHelpItem((*it).first.c_str()); } for(it = cmdexmap.begin(); it != cmdexmap.end(); it++) { if ( (gid != 0) && ((*it).second.group != gid) ) continue; helpwin->AddHelpItem((*it).first.c_str()); } } /* --Methode-- */ void PIACmd::LoadModule(string& fnameso, string& name) { PDynLinkMgr * dynlink = new PDynLinkMgr(fnameso, false); if (dynlink == NULL) { cerr << "PIACmd/LoadModule_Error: Pb opening SO " << fnameso << endl; return; } string fname = name + "_init"; DlModuleInitEndFunction finit = dynlink->GetFunction(fname); if (!finit) { cerr << "PIACmd/LoadModule_Error: Pb linking " << fname << endl; return; } cout << "PIACmd/LoadModule_Info: Initialisation module" << name << " " << fname << "() ..." << endl; finit(); modmap[name] = dynlink; return; } /* --Methode-- */ void PIACmd::AddInterpreter(CmdInterpreter * cl) { if (!cl) return; interpmap[cl->Name()] = cl;} /* --Methode-- */ void PIACmd::SelInterpreter(string& name) { InterpMap::iterator it = interpmap.find(name); if (it == interpmap.end()) return; curcmdi = (*it).second; } // Pour le decoupage des commandes en lignes typedef vector cmdtok; /* Fonction */ static string GetStringFrStdin(PIACmd* piac) { PIStdImgApp* piapp = piac->GetImgApp(); if (piapp) { PIBaseWdg* wdg = piapp->CurrentBaseWdg(); if (wdg) wdg->Refresh(); #ifndef __mac__ /* On vide le buffer X-Window */ XSync(PIXDisplay(),False); #endif } char buff[128]; fgets(buff, 128, stdin); buff[127] = '\0'; return((string)buff); } /* --Methode-- */ int PIACmd::Interpret(string& s) { int rc = 0; cmdtok tokens; CmdVarList::iterator it; // Removing leading blanks size_t p,q,q2,l; l = s.length(); if (l < 1) return(0); if (s[0] == '#') return(0); // si c'est un commentaire p=s.find_first_not_of(" \t"); if (p < l) s = s.substr(p); // else return(0); // On enregistre les commandes if (histon) hist << s << endl; string toks,kw; // >>>> Substitution d'alias (1er mot) p = 0; q = s.find_first_of(" \t"); l = s.length(); string w1 = (q < l) ? s.substr(p,q-p) : s.substr(p); it = mAliases.find(w1); if (it != mAliases.end()) { s = (q < l) ? ((*it).second + s.substr(q)) : (*it).second ; l = s.length(); p=s.find_first_not_of(" \t"); if (p < l) s = s.substr(p); p = 0; q = s.find_first_of(" "); } // >>>> Separating keyword if (q < l) { kw = s.substr(p,q-p); toks = s.substr(q, l-q); } else { kw = s.substr(p,l-p); toks = ""; } // On verifie si nous sommes dans un bloc if ( (curblk != NULL) && (kw != "foreach") ) { // On est dans un bloc if (kw != "end") { curblk->AddLine(s); return(0); } else { PIACmdBloc* curb = curblk; curblk = curb->Parent(); felevel--; if (curblk == NULL) { mImgApp->GetConsole()->SetPrompt("Cmd> "); // cout << " *DBG* Executing bloc " << endl; curb->Execute(); } else { char prompt[64]; sprintf(prompt, "foreach-%d? ", felevel); mImgApp->GetConsole()->SetPrompt(prompt); } return(0); } } // Nous ne sommes donc pas dans un bloc .... // >>>> Variable substitution string s2=""; p = 0; l = s.length(); string vn; while (p < l) { q = s.find('$',p); // cout << "DBG: " << s2 << " p= " << p << " q= " << q << " L= " << l << endl; if (q > l) break; if ((q>0) && (s[q-1] == '\\')) { // Escape character \$ s2 += (s.substr(p,q-1-p) + '$') ; p = q+1; continue; } if (q >= l-1) { cerr << " Syntax error !!! " << endl; return(0); } vn = ""; if ( s[q+1] == '{' ) { // Variable in the form ${name} q2 = s.find('}',q+1); if (q2 >= l) { cerr << " Syntax error !!! " << endl; return(0); } vn = s.substr(q+2,q2-q-2); q2++; } else if ( s[q+1] == '[' ) { // Variable in the form $[varname] -> This is $$varname q2 = s.find(']',q+1); if (q2 >= l) { cerr << " Syntax error !!! " << endl; return(0); } vn = s.substr(q+2,q2-q-2); it = mVars.find(vn); if ( (vn.length() < 1) || (it == mVars.end()) ) { cerr << " Error: Undefined variable " << vn << " ! " << endl; return(0); } vn = mVars[vn]; q2++; } else { q2 = s.find_first_of(" .:/,]()$",q+1); if (q2 > l) q2 = l; vn = s.substr(q+1, q2-q-1); } it = mVars.find(vn); if ( (vn.length() < 1) || (it == mVars.end()) ) { cerr << " Error: Undefined variable " << vn << " ! " << endl; return(0); } s2 += (s.substr(p, q-p) + (*it).second); p = q2; } if (p < l) s2 += s.substr(p); p = s2.find_first_not_of(" \t"); if (p < l) s2 = s2.substr(p); // >>>> Separating keyword and tokens q = s2.find(' '); l = s2.length(); if (q < l) { kw = s2.substr(0,q); toks = s2.substr(q, l-q); } else { kw = s2; toks = ""; } q = 0; while (q < l) { p = toks.find_first_not_of(" \t",q+1); // au debut d'un token if (p>=l) break; q = toks.find_first_of(" \t",p); // la fin du token; string token = toks.substr(p,q-p); tokens.push_back(token); } // Si c'est un foreach, on cree un nouveau bloc if (kw == "foreach") { // cout << " *DBG* We got a foreach... " << endl; PIACmdBloc* bloc = new PIACmdBloc(this, curblk, tokens); if (!bloc->CheckOK()) { cerr << "foreach syntax Error ! " << endl; delete bloc; return(0); } felevel++; if (curblk) curblk->AddBloc(bloc); else { char prompt[64]; sprintf(prompt, "foreach-%d> ", felevel); mImgApp->GetConsole()->SetPrompt(prompt); } curblk = bloc; // cout << " *DBG* New Bloc created ... " << endl; return(0); } // cout << "PIACmd::Do() DBG KeyW= " << kw << " NbArgs= " << tokens.size() << endl; // for(int ii=0; ii>>>>>>>>>> Commande d'interpreteur else if (kw == "helpwindow") ShowHelpWindow(); else if (kw == "help") { if (tokens.size() > 0) cout << GetUsage(tokens[0]) << endl; else { string kwh = "piacmd"; cout << GetUsage(kwh) << endl; } } else if (kw == "set") { if (tokens.size() < 2) { cout << "PIACmd::Interpret() Usage: set varname string" << endl; return(0); } if ((tokens[0].length() < 1) || !isalpha((int)tokens[0][0]) ) { cerr << "PIACmd::Interpret()/Error Variable name should start with alphabetic" << endl; return(0); } string xx = tokens[1]; for (int kk=2; kk ol; mObjMgr->GetObjList(tokens[1], ol); string vol; if (ol.size() < 1) vol = ""; else { vol = ol[0]; for (int kk=1; kkGetConsole()->AddStr(">>> Reading From StdIn \n", PIVA_Magenta); cout << tokens[0] << " ? " << endl; mVars[tokens[0]] = GetStringFrStdin(this); } else if (kw == "listvars") { cout << "PIACmd::Interpret() Variable List , VarName = Value \n"; CmdVarList::iterator it; for(it = mVars.begin(); it != mVars.end(); it++) cout << (*it).first << " = " << (*it).second << "\n"; cout << endl; } else if (kw == "listalias") { cout << "PIACmd::Interpret() Alias List , AliasName = Value \n"; CmdVarList::iterator it; for(it = mAliases.begin(); it != mAliases.end(); it++) cout << (*it).first << " = " << (*it).second << "\n"; cout << endl; } else if (kw == "listcommands") { cout << "---- PIACmd::Interpret() Command Variable List ----- \n"; CmdExmap::iterator it; int kc = 0; for(it = cmdexmap.begin(); it != cmdexmap.end(); it++) { cout << (*it).first << " "; kc++; if (kc >= 5) { cout << "\n"; kc = 0; } } cout << endl; } else if (kw == "traceon") { cout << "PIACmd::Interpret() -> Trace ON mode " << endl; trace = true; } else if (kw == "traceoff") { cout << "PIACmd::Interpret() -> Trace OFF mode " << endl; trace = false; } else if (kw == "timingon") { cout << "PIACmd::Interpret() -> Timing ON mode " << endl; if (gltimer) delete gltimer; gltimer = new Timer("PIA-CmdInterpreter "); timing = true; } else if (kw == "timingoff") { cout << "PIACmd::Interpret() -> Timing OFF mode " << endl; if (gltimer) delete gltimer; gltimer = NULL; timing = false; } else if (kw == "exec") { if (tokens.size() < 1) { cout << "PIACmd::Interpret() Usage: exec filename" << endl; return(0); } ExecFile(tokens[0], tokens); } else if (kw == "shell") { if (tokens.size() < 1) { cout << "PIACmd::Interpret() Usage: shell cmdline" << endl; return(0); } system(toks.c_str()); } // Execution d'une commande enregistree else rc = ExecuteCommand(kw, tokens); if (timing) gltimer->Split(); return(rc); } /* --Methode-- */ int PIACmd::ExecuteCommandLine(string& line) { cmdtok tokens; if (line.length() < 1) return(0); string toks,kw; size_t p = line.find_first_not_of(" "); line = line.substr(p); p = 0; size_t q = line.find_first_of(" "); size_t l = line.length(); if (q < l) { kw = line.substr(p,q-p); toks = line.substr(q, l-q); } else { kw = line.substr(p,l-p); toks = ""; } q = 0; while (q < l) { p = toks.find_first_not_of(" ",q+1); // au debut d'un token if (p>=l) break; q = toks.find_first_of(" ",p); // la fin du token; string token = toks.substr(p,q-p); tokens.push_back(token); } return(ExecuteCommand(kw, tokens)); } /* --Methode-- */ int PIACmd::ExecuteCommand(string& keyw, vector& args) { int rc = -1; CmdExmap::iterator it = cmdexmap.find(keyw); if (it == cmdexmap.end()) cout << "No such command : " << keyw << " ! " << endl; else { if ((*it).second.cex) rc = (*it).second.cex->Execute(keyw, args); else cout << "Dont know how to execute " << keyw << " ? " << endl; } return(rc); } /* --Methode-- */ int PIACmd::ExecFile(string& file, vector& args) { char line_buff[512]; FILE *fip; if ( (fip = fopen(file.c_str(),"r")) == NULL ) { if (file.find('.') >= file.length()) { cout << "PIACmd::Exec(): Error opening file " << file << endl; file += ".pic"; cout << " Trying file " << file << endl; fip = fopen(file.c_str(),"r"); } } if(fip == NULL) { cerr << "PIACmd::Exec() Error opening file " << file << endl; hist << "##! PIACmd::Exec() Error opening file " << file << endl; return(0); } // hist << "### Executing commands from " << file << endl; // Setting $0 ... $99 variables int k; CmdVarList::iterator it; char buff[32]; // First, we clear all previous values string vn="#"; it = mVars.find(vn); if (it != mVars.end()) mVars.erase(it); for(k=0; k<99; k++) { sprintf(buff,"%d",k); vn = buff; it = mVars.find(vn); if (it != mVars.end()) mVars.erase(it); } // We then set them vn="#"; sprintf(buff,"%d",(int)args.size()); mVars[vn] = buff; for(k=0; kGetConsole()->AddStr("### Executing commands from ", PIVA_Magenta); mImgApp->GetConsole()->AddStr(file.c_str(), PIVA_Magenta); mImgApp->GetConsole()->AddStr("\n", PIVA_Magenta); } histon = false; while (fgets(line_buff,511,fip) != NULL) { if (trace) mImgApp->GetConsole()->AddStr(line_buff, PIVA_Magenta); line_buff[strlen(line_buff)-1] = '\0'; /* LF/CR de la fin */ string line(line_buff); Interpret(line); } histon = true; // hist << "### End of Exec( " << file << " ) " << endl; if (trace) { mImgApp->GetConsole()->AddStr("### End of Exec( ", PIVA_Magenta); mImgApp->GetConsole()->AddStr(file.c_str(), PIVA_Magenta); mImgApp->GetConsole()->AddStr(" ) \n", PIVA_Magenta); } return(0); } static string* videstr = NULL; /* --Methode-- */ string& PIACmd::GetUsage(const string& kw) { bool fndok = false; CmdExmap::iterator it = cmdexmap.find(kw); if (it == cmdexmap.end()) { it = helpexmap.find(kw); if (it != helpexmap.end()) fndok = true; } else fndok = true; if (fndok) return( (*it).second.us ); // Keyword pas trouve if (videstr == NULL) videstr = new string(""); *videstr = "Nothing known about " + kw + " ?? "; return(*videstr); } /* --Methode-- */ void PIACmd::ShowHelpWindow() { helpwin->Show(); }