source: Sophya/trunk/SophyaLib/SysTools/commander.h@ 2790

Last change on this file since 2790 was 2753, checked in by ansari, 20 years ago

1/ Ajout methode Nop() ds ZSync - pour eviter les warnings unused variable
2/ Ajout de la methode IsThreadable() a l'interface CmdExecutor et sa prise
en compte pour l'execution en thread separe

Reza , 23 Mai 2005

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