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