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