| [2446] | 1 | // This may look like C code, but it is really -*- C++ -*- | 
|---|
|  | 2 | // Classe interpreteur de commande pour piapp | 
|---|
|  | 3 | // Reza      Aout 97 , Juillet,Aout 98 | 
|---|
|  | 4 | //           Octobre 2003: de PIext -> SysTools | 
|---|
|  | 5 | // LAL-IN2P3/CNRS  DAPNIA/CEA | 
|---|
|  | 6 |  | 
|---|
|  | 7 | #ifndef COMMANDER_H_SEEN | 
|---|
|  | 8 | #define COMMANDER_H_SEEN | 
|---|
|  | 9 |  | 
|---|
|  | 10 | #include "machdefs.h" | 
|---|
|  | 11 | #include <iostream> | 
|---|
|  | 12 | #include <fstream> | 
|---|
|  | 13 | #include <string> | 
|---|
|  | 14 | #include <vector> | 
|---|
|  | 15 | #include <list> | 
|---|
|  | 16 | #include <stack> | 
|---|
|  | 17 | #include <map> | 
|---|
|  | 18 | #include <functional> | 
|---|
|  | 19 |  | 
|---|
|  | 20 | #include "pdlmgr.h" | 
|---|
|  | 21 | #include "dvlist.h" | 
|---|
|  | 22 | #include "ctimer.h" | 
|---|
|  | 23 |  | 
|---|
| [2483] | 24 | namespace SOPHYA { | 
|---|
| [2446] | 25 |  | 
|---|
| [2804] | 26 | ////// Classe definissant l'interface pour un interpreteur de commande | 
|---|
| [2446] | 27 |  | 
|---|
| [2598] | 28 | /*! | 
|---|
|  | 29 | \ingroup SysTools | 
|---|
|  | 30 | \brief  Interface definition for a generic command interpreter. | 
|---|
|  | 31 | */ | 
|---|
| [2446] | 32 | class CmdInterpreter { | 
|---|
|  | 33 | public: | 
|---|
|  | 34 | virtual               ~CmdInterpreter() {} ; | 
|---|
| [2598] | 35 | //! Returns the interpreter's name | 
|---|
| [2446] | 36 | virtual string        Name()=0; | 
|---|
| [2598] | 37 | //! Method to be called in order to interpret a line or string. | 
|---|
| [2446] | 38 | virtual int           Interpret(string& line)=0; | 
|---|
|  | 39 | }; | 
|---|
|  | 40 |  | 
|---|
|  | 41 |  | 
|---|
| [2804] | 42 | /////// Classe definissant l'interface pour un executeur de commande | 
|---|
|  | 43 |  | 
|---|
| [2598] | 44 | /*! | 
|---|
|  | 45 | \ingroup SysTools | 
|---|
|  | 46 | \brief Interface definition for command executor, to be used with Commander | 
|---|
| [2446] | 47 |  | 
|---|
| [2598] | 48 | A command is defined by a keyword and a number of argument | 
|---|
|  | 49 | */ | 
|---|
|  | 50 |  | 
|---|
| [2446] | 51 | class CmdExecutor { | 
|---|
|  | 52 | public: | 
|---|
|  | 53 | virtual       ~CmdExecutor() {} ; | 
|---|
|  | 54 | // keyw : Le mot cle associe , args: Arguments de la commande | 
|---|
| [2753] | 55 | //! command execution method for a command defined by keyword and its arguments. | 
|---|
| [2446] | 56 | virtual int   Execute(string& keyw, vector<string>& args, string& toks)=0; | 
|---|
| [2943] | 57 | //! Return true if the command \b keyw is thread compatible (can be executed in a separate thread) | 
|---|
| [2753] | 58 | virtual bool  IsThreadable(string const & keyw) { return false; } | 
|---|
| [2446] | 59 | }; | 
|---|
|  | 60 |  | 
|---|
|  | 61 |  | 
|---|
|  | 62 |  | 
|---|
|  | 63 | class CommanderBloc;    // Bloc de type foreach / for de l'interpreteur Commander | 
|---|
|  | 64 | class CommanderScript;  // Script de commandes defini ds l'interpreteur Commander | 
|---|
| [2671] | 65 | class CommandExeThr;    // Thread d'execution de commande | 
|---|
| [2446] | 66 |  | 
|---|
| [2671] | 67 | //! A simple command interpreter with c-shell like syntax and  dynamic load capability. | 
|---|
| [2446] | 68 |  | 
|---|
|  | 69 | class Commander : public CmdInterpreter  { | 
|---|
|  | 70 | public: | 
|---|
|  | 71 | static Commander*     GetInterpreter(); | 
|---|
|  | 72 |  | 
|---|
| [2955] | 73 | Commander(bool fgsigzt=true); | 
|---|
| [2446] | 74 | virtual               ~Commander(); | 
|---|
|  | 75 | virtual string        Name(); | 
|---|
|  | 76 |  | 
|---|
| [2466] | 77 | virtual void          AddHelpGroup(string& grp, string& desc); | 
|---|
| [2446] | 78 | virtual void          RegisterCommand(string& keyw, string& usage, CmdExecutor * ce, | 
|---|
| [2466] | 79 | string& grp); | 
|---|
|  | 80 | inline  void          RegisterCommand(string& keyw, string& usage, CmdExecutor * ce, | 
|---|
| [3572] | 81 | const char* grp) | 
|---|
| [2466] | 82 | { string sgrp = grp; RegisterCommand(keyw, usage, ce, sgrp); } | 
|---|
|  | 83 |  | 
|---|
| [2446] | 84 | virtual void          RegisterHelp(string& keyw, string& usage, string& grp); | 
|---|
|  | 85 |  | 
|---|
|  | 86 | virtual void          LoadModule(string& fnameso, string& name); | 
|---|
|  | 87 |  | 
|---|
|  | 88 | virtual void          AddInterpreter(CmdInterpreter * cl); | 
|---|
|  | 89 | virtual void          SelInterpreter(string& name); | 
|---|
|  | 90 |  | 
|---|
|  | 91 | virtual int           Interpret(string& line); | 
|---|
| [2671] | 92 | virtual void          StopExecution(); | 
|---|
| [2473] | 93 |  | 
|---|
| [2446] | 94 | virtual int           ExecuteCommand(string& keyw, vector<string>& args, string& toks); | 
|---|
|  | 95 | virtual int           ExecFile(string& file, vector<string>& args); | 
|---|
|  | 96 | virtual int           CShellExecute(string cmd); | 
|---|
|  | 97 | virtual string&       GetUsage(const string& kw); | 
|---|
|  | 98 |  | 
|---|
|  | 99 | inline  void          SetMaxLoopLimit(int_8 lim=0) { maxlooplimit = lim; } | 
|---|
|  | 100 | inline  int_8         GetMaxLoopLimit() { return maxlooplimit; } | 
|---|
|  | 101 |  | 
|---|
|  | 102 | string                GetCurrentPrompt() { return curprompt; } | 
|---|
|  | 103 |  | 
|---|
|  | 104 |  | 
|---|
|  | 105 | virtual void          HelptoLaTeX(string const & flnm); | 
|---|
|  | 106 |  | 
|---|
| [2943] | 107 | //! return the current selected interpreter (default : this) | 
|---|
| [2446] | 108 | inline  CmdInterpreter* CurrentInterpreter() { return(curcmdi); } | 
|---|
|  | 109 |  | 
|---|
| [2518] | 110 | // ----- Action / gestion des variables propres de l'interpreteur | 
|---|
|  | 111 | // Verifie l'existence de la variable nomme vn et retourne sa valeur ds vv | 
|---|
|  | 112 | // Retourne false si la variable n'existe pas | 
|---|
|  | 113 | virtual bool    GetVar(string const & vn, string & vv); | 
|---|
|  | 114 | virtual bool    GetVar(string const & vn, int idx, string & vv); | 
|---|
|  | 115 | virtual bool    GetVar(string const & vn, vector<string> & vv); | 
|---|
|  | 116 | virtual bool    SetVar(string const & vn, string const & vv); | 
|---|
|  | 117 | virtual bool    SetVar(string const & vn, int idx, string const & vv); | 
|---|
|  | 118 | virtual bool    SetVar(string const & vn, vector<string> const & vv); | 
|---|
|  | 119 | virtual bool    CheckVarName(string const & vn); | 
|---|
|  | 120 | virtual bool    DeleteVar(string const & vn); | 
|---|
|  | 121 | virtual void    ListVar(); | 
|---|
|  | 122 | // Variables de l'environnement application | 
|---|
|  | 123 | virtual bool    GetVarApp(string const & vn, string & vv); | 
|---|
|  | 124 | virtual bool    SetVarApp(string const & vn, string const & vv); | 
|---|
|  | 125 | virtual bool    DeleteVarApp(string const & vn); | 
|---|
|  | 126 | virtual void    ListVarApp(); | 
|---|
|  | 127 | // Variables d'environnement globales | 
|---|
|  | 128 | virtual bool    GetVarEnv(string const & vn, string & vv); | 
|---|
|  | 129 | virtual bool    SetVarEnv(string const & vn, string const & vv); | 
|---|
|  | 130 | virtual bool    DeleteVarEnv(string const & vn); | 
|---|
|  | 131 | virtual void    ListVarEnv(); | 
|---|
|  | 132 |  | 
|---|
| [2483] | 133 | //   Utilitaire pour decoupage en mot | 
|---|
| [2446] | 134 | static  int   LineToWords(string& line, string& kw, vector<string>& tokens, | 
|---|
| [2518] | 135 | vector<bool>& qottoks, string& toks, bool uq=true); | 
|---|
| [2446] | 136 | protected: | 
|---|
| [2473] | 137 | virtual int   ParseLineExecute(string& line, bool qw=true); | 
|---|
|  | 138 |  | 
|---|
|  | 139 | virtual int   ExecuteCommandLine(string & keyw, vector<string> & args, | 
|---|
|  | 140 | string & toks); | 
|---|
|  | 141 |  | 
|---|
| [2466] | 142 | virtual bool  CheckHelpGrp(string& grp, int& gid, string& desc); | 
|---|
|  | 143 | inline  bool  CheckHelpGrp(string& grp, int& gid) | 
|---|
|  | 144 | { string desc=""; return CheckHelpGrp(grp, gid, desc); } | 
|---|
|  | 145 |  | 
|---|
| [2473] | 146 | virtual int   SubstituteVars(string & s, string & s2); | 
|---|
| [2446] | 147 | int           EvaluateTest(vector<string> & args, | 
|---|
|  | 148 | string & line, bool & res); | 
|---|
|  | 149 | int           EvalRPNExpr(vector<string> & args, string & line); | 
|---|
|  | 150 |  | 
|---|
| [2518] | 151 | // variable de l'interpreteur = valeur - accepte la syntaxe de type varname[index] | 
|---|
|  | 152 | virtual bool    SetVariable(string const & vn, string const & vv); | 
|---|
| [2483] | 153 | // Acces aux variables | 
|---|
|  | 154 | virtual bool    Var2Str(string const & vn, string & vv); | 
|---|
|  | 155 | inline  bool    Var2Str(string const & vn, int idx, string & vv) | 
|---|
|  | 156 | { return GetVar(vn, idx, vv); } | 
|---|
|  | 157 | inline  bool    Var2Str(string const & vn, vector<string> & vv) | 
|---|
|  | 158 | { return GetVar(vn, vv); } | 
|---|
|  | 159 |  | 
|---|
| [2473] | 160 | virtual string  GetTmpDir(); | 
|---|
|  | 161 |  | 
|---|
|  | 162 | virtual void  SetCurrentPrompt(const char* pr); | 
|---|
| [2446] | 163 | inline void   SetCurrentPrompt(string const & pr) { SetCurrentPrompt(pr.c_str()); } | 
|---|
| [2483] | 164 | inline void   SetDefaultPrompt(string const & pr) { defprompt = pr; } | 
|---|
| [2446] | 165 |  | 
|---|
|  | 166 | virtual void  ShowMessage(const char * msg, int att); | 
|---|
|  | 167 |  | 
|---|
|  | 168 | void          PushStack(vector<string> & args); | 
|---|
|  | 169 | void          PopStack(bool psta=true); | 
|---|
|  | 170 |  | 
|---|
| [2671] | 171 | //   Gestion des threads d'execution de commandes | 
|---|
|  | 172 | void          ListThreads(); | 
|---|
| [2955] | 173 | void          StopThr(uint_8 thrid, bool fgkill=true); | 
|---|
| [2671] | 174 | void          CleanThrList(); | 
|---|
|  | 175 | void          WaitThreads(); | 
|---|
|  | 176 |  | 
|---|
|  | 177 | // ------ Attributs et variables ------ | 
|---|
| [2446] | 178 | CmdInterpreter* curcmdi; | 
|---|
|  | 179 |  | 
|---|
|  | 180 | // Gestion des variables | 
|---|
| [2483] | 181 | typedef map< string,  vector<string>, less<string> > CmdVarList; | 
|---|
|  | 182 | CmdVarList variables; | 
|---|
| [2446] | 183 |  | 
|---|
|  | 184 | // Pour enregistrer la liste de commandes et leurs executeurs et le help | 
|---|
| [2804] | 185 | /*! \cond   Pour supprimer la documentation par doxygen */ | 
|---|
|  | 186 | //  Command executor registration - For Commander internal use | 
|---|
| [2446] | 187 | struct cmdex {int group; string us; CmdExecutor * cex; } ; | 
|---|
| [2804] | 188 | //  Help text registration - For Commander internal use | 
|---|
| [2466] | 189 | struct hgrpst {int gid; string desc; } ;               // Identification+description d'un groupe de help | 
|---|
| [2804] | 190 | /*! \endcond */ | 
|---|
|  | 191 |  | 
|---|
| [2466] | 192 | typedef map<string, hgrpst, less<string> > CmdHGroup;   // Liste des groupes de commandes | 
|---|
| [2446] | 193 | CmdHGroup cmdhgrp; | 
|---|
| [2466] | 194 | int cmdgrpid;                                        // Numero de groupe courant | 
|---|
|  | 195 | typedef map<string, cmdex, less<string> > CmdExmap; | 
|---|
|  | 196 | CmdExmap cmdexmap;                                   // Liste des commandes et leurs executeurs | 
|---|
|  | 197 | CmdExmap helpexmap;                                  // Pour les helps sans commande | 
|---|
| [2671] | 198 | // Pour garder la liste des threads d'execution de commande | 
|---|
|  | 199 | list<CommandExeThr *> CmdThrExeList; | 
|---|
|  | 200 | uint_8 ThrId; | 
|---|
|  | 201 |  | 
|---|
| [2446] | 202 | // Pour garder la liste des modules | 
|---|
|  | 203 | typedef map<string, PDynLinkMgr* , less<string> > Modmap; | 
|---|
|  | 204 | Modmap modmap; | 
|---|
|  | 205 |  | 
|---|
|  | 206 | // Pour garder la liste des interpreteur | 
|---|
|  | 207 | typedef map<string, CmdInterpreter*, less<string> > InterpMap; | 
|---|
|  | 208 | InterpMap interpmap; | 
|---|
|  | 209 |  | 
|---|
|  | 210 | //  Pour stocker les scripts definis ds l'interpreteur | 
|---|
|  | 211 | typedef map<string, CommanderScript*, less<string> > ScriptList; | 
|---|
|  | 212 | ScriptList mScripts;  // Liste des scripts | 
|---|
|  | 213 | CommanderScript* curscript; // Script en cours de definition | 
|---|
|  | 214 |  | 
|---|
| [2483] | 215 | // Code de retour execution commande | 
|---|
|  | 216 | int _xstatus; | 
|---|
|  | 217 | // Valeur de retour (par l'instruction return) - | 
|---|
|  | 218 | string _retstr; | 
|---|
|  | 219 |  | 
|---|
|  | 220 | //  Pour stocker les alias definies par l'interpreteur | 
|---|
| [2446] | 221 | typedef map<string, string, less<string> > CmdStrList; | 
|---|
|  | 222 | CmdStrList mAliases;  // Liste des alias | 
|---|
|  | 223 |  | 
|---|
| [2483] | 224 | // Le stack pour les arguments des .pic et des scripts | 
|---|
| [2446] | 225 | stack< vector<string> > ArgsStack; | 
|---|
| [2483] | 226 | // Stack pour les Prompts | 
|---|
| [2446] | 227 | stack<string> PromptStack; | 
|---|
|  | 228 |  | 
|---|
| [2483] | 229 | // Gestion des blocs de commandes et tests (if) | 
|---|
| [2446] | 230 | stack< CommanderBloc * > CmdBlks;  // Bloc de commande courant (foreach, ...) | 
|---|
|  | 231 | int felevel;                    // foreach-for level | 
|---|
|  | 232 | int_8 maxlooplimit;            // Limite maximum des boucles | 
|---|
|  | 233 | stack< list<char> > TestsStack; // Stack des resultats de test | 
|---|
|  | 234 | list<char>::iterator tresit;       // Test courant | 
|---|
|  | 235 | bool curtestresult;             // Resultat courant des tests | 
|---|
|  | 236 |  | 
|---|
| [2518] | 237 | // Controle du flot d'execution | 
|---|
|  | 238 | bool fgexebrk; | 
|---|
|  | 239 |  | 
|---|
| [2483] | 240 | // Commande splitees sur plusieurs lignes | 
|---|
| [2446] | 241 | bool mulinefg;            // Bloc multi-lignes (ligne suite) | 
|---|
|  | 242 | string mulinecmd;         // Commande multi-lignes | 
|---|
| [2483] | 243 |  | 
|---|
|  | 244 | // Texte de prompt (attente de commande) | 
|---|
| [2446] | 245 | string spromptmul;        // Prompt console avant multi-ligne | 
|---|
| [2483] | 246 | string curprompt;         // Prompt courant | 
|---|
|  | 247 | string defprompt;         // Prompt par defaut | 
|---|
| [2446] | 248 |  | 
|---|
| [2483] | 249 | // Gestion d'historique, trace, timing des commandes | 
|---|
| [2446] | 250 | ofstream hist;       //  History file | 
|---|
|  | 251 | bool histon;        //  True ->  history file | 
|---|
|  | 252 | bool trace;          // Trace flag | 
|---|
|  | 253 | bool timing;         // Display CPU Time | 
|---|
|  | 254 | Timer* gltimer;      // pour Display CPU Time | 
|---|
|  | 255 |  | 
|---|
| [2483] | 256 | friend class CommanderBloc; | 
|---|
|  | 257 | friend class CommanderScript; | 
|---|
|  | 258 |  | 
|---|
| [2446] | 259 | }; | 
|---|
|  | 260 |  | 
|---|
| [2483] | 261 | } // namespace SOPHYA | 
|---|
| [2446] | 262 |  | 
|---|
|  | 263 | /* end of ifdef COMMANDER_H_SEEN */ | 
|---|
|  | 264 | #endif | 
|---|
|  | 265 |  | 
|---|