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

Last change on this file since 2446 was 2446, checked in by ansari, 22 years ago

Introduction de la classe Commander, qui devra devenir la classe de base pour PIACmd (piapp) - Reza 06/10/2003

File size: 6.0 KB
Line 
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
24// Classe definissant l'interface pour un executeur de commande
25
26// Classe definissant l'interface pour un interpreteur de commande
27
28//! Interface definition for a generic command interpreter
29class CmdInterpreter {
30public:
31 virtual ~CmdInterpreter() {} ;
32 virtual string Name()=0;
33 virtual int Interpret(string& line)=0;
34};
35
36
37//! Interface definition for command executor, to be used with SOPHYA::Commander
38
39class CmdExecutor {
40public:
41 virtual ~CmdExecutor() {} ;
42 // keyw : Le mot cle associe , args: Arguments de la commande
43 virtual int Execute(string& keyw, vector<string>& args, string& toks)=0;
44};
45
46
47
48class CommanderBloc; // Bloc de type foreach / for de l'interpreteur Commander
49class CommanderScript; // Script de commandes defini ds l'interpreteur Commander
50
51/*
52//! Interface definition for SOPHYA::Commander help manager
53class CommanderHelpMgr {
54public :
55 CommanderHelpMgr();
56 virtual ~CommanderHelpMgr();
57 virtual void AddHelpGroup(const char * hgrp, int gid);
58 virtual void AddHelpItem(const char * hitem);
59 virtual void ClearHelpList();
60 virtual void Activate();
61}
62*/
63
64
65//! A simple command interpreter with c-shell like syntax with dynamic load capability.
66
67class Commander : public CmdInterpreter {
68public:
69 static Commander* GetInterpreter();
70
71 Commander();
72 virtual ~Commander();
73 virtual string Name();
74
75 virtual void RegisterCommand(string& keyw, string& usage, CmdExecutor * ce,
76 string grp="Commands");
77 virtual void RegisterHelp(string& keyw, string& usage, string& grp);
78
79 virtual void LoadModule(string& fnameso, string& name);
80
81 virtual void AddInterpreter(CmdInterpreter * cl);
82 virtual void SelInterpreter(string& name);
83
84 virtual int Interpret(string& line);
85 virtual int ParseLineExecute(string& line, bool qw=true);
86 virtual int ExecuteCommand(string& keyw, vector<string>& args, string& toks);
87 virtual int ExecFile(string& file, vector<string>& args);
88 virtual int CShellExecute(string cmd);
89 virtual string& GetUsage(const string& kw);
90
91 inline void SetMaxLoopLimit(int_8 lim=0) { maxlooplimit = lim; }
92 inline int_8 GetMaxLoopLimit() { return maxlooplimit; }
93
94 string GetCurrentPrompt() { return curprompt; }
95
96
97 virtual void HelptoLaTeX(string const & flnm);
98
99 inline CmdInterpreter* CurrentInterpreter() { return(curcmdi); }
100
101
102// Utilitaire pour decoupage en mot
103 static int LineToWords(string& line, string& kw, vector<string>& tokens,
104 string& toks, bool uq=true);
105protected:
106 virtual int CheckHelpGrp(string& grp);
107 int ExecuteCommandLine(string & keyw, vector<string> & args,
108 string & toks);
109
110 int SubstituteVars(string & s, string & s2);
111 int EvaluateTest(vector<string> & args,
112 string & line, bool & res);
113 int EvalRPNExpr(vector<string> & args, string & line);
114
115 bool GetVar(string & vn, string & vv);
116 string GetVariable(string const & vn);
117 bool HasVariable(string const & vn);
118 bool SetVar(string const & vn, string const & vv);
119 bool DeleteVar(string const & vn);
120 void ListVars();
121 string GetTmpDir();
122 inline void SetCurrentPrompt(string const & pr) { SetCurrentPrompt(pr.c_str()); }
123 virtual void SetCurrentPrompt(const char* pr);
124
125 virtual void ShowMessage(const char * msg, int att);
126
127 void PushStack(vector<string> & args);
128 void PopStack(bool psta=true);
129
130 CmdInterpreter* curcmdi;
131
132// Gestion des variables
133 DVList variables;
134
135// Pour enregistrer la liste de commandes et leurs executeurs et le help
136 struct cmdex {int group; string us; CmdExecutor * cex; } ;
137 typedef map<string, int, less<string> > CmdHGroup; // Liste des groupes de commandes
138 CmdHGroup cmdhgrp;
139 int cmdgrpid; // Numero de groupe courant
140 typedef map<string, cmdex, less<string> > CmdExmap;
141 CmdExmap cmdexmap;
142 CmdExmap helpexmap; // Pour les helps sans commande
143
144// Pour garder la liste des modules
145 typedef map<string, PDynLinkMgr* , less<string> > Modmap;
146 Modmap modmap;
147
148// Pour garder la liste des interpreteur
149 typedef map<string, CmdInterpreter*, less<string> > InterpMap;
150 InterpMap interpmap;
151
152// Pour stocker les scripts definis ds l'interpreteur
153 typedef map<string, CommanderScript*, less<string> > ScriptList;
154 ScriptList mScripts; // Liste des scripts
155 CommanderScript* curscript; // Script en cours de definition
156
157// Pour stocker les alias definies par l'interpreteur
158 typedef map<string, string, less<string> > CmdStrList;
159 CmdStrList mAliases; // Liste des alias
160
161// Le stack pour les arguments des .pic et des scripts
162 stack< vector<string> > ArgsStack;
163// Stack pour les Prompts
164 stack<string> PromptStack;
165
166 stack< CommanderBloc * > CmdBlks; // Bloc de commande courant (foreach, ...)
167 int felevel; // foreach-for level
168 int_8 maxlooplimit; // Limite maximum des boucles
169 stack< list<char> > TestsStack; // Stack des resultats de test
170 list<char>::iterator tresit; // Test courant
171 bool curtestresult; // Resultat courant des tests
172
173 bool mulinefg; // Bloc multi-lignes (ligne suite)
174 string mulinecmd; // Commande multi-lignes
175 string spromptmul; // Prompt console avant multi-ligne
176 string curprompt;
177
178 ofstream hist; // History file
179 bool histon; // True -> history file
180 bool trace; // Trace flag
181 bool timing; // Display CPU Time
182 Timer* gltimer; // pour Display CPU Time
183
184};
185
186
187/* end of ifdef COMMANDER_H_SEEN */
188#endif
189
Note: See TracBrowser for help on using the repository browser.