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

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

Initialisation numero help-gid + declarations virtual pour les methodes de Commander - Reza 4 Dec 2003

File size: 6.6 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
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
[2466]75 virtual void AddHelpGroup(string& grp, string& desc);
[2446]76 virtual void RegisterCommand(string& keyw, string& usage, CmdExecutor * ce,
[2466]77 string& grp);
78 inline void RegisterCommand(string& keyw, string& usage, CmdExecutor * ce,
79 char* grp)
80 { string sgrp = grp; RegisterCommand(keyw, usage, ce, sgrp); }
81
[2446]82 virtual void RegisterHelp(string& keyw, string& usage, string& grp);
83
84 virtual void LoadModule(string& fnameso, string& name);
85
86 virtual void AddInterpreter(CmdInterpreter * cl);
87 virtual void SelInterpreter(string& name);
88
89 virtual int Interpret(string& line);
[2473]90
[2446]91 virtual int ExecuteCommand(string& keyw, vector<string>& args, string& toks);
92 virtual int ExecFile(string& file, vector<string>& args);
93 virtual int CShellExecute(string cmd);
94 virtual string& GetUsage(const string& kw);
95
96 inline void SetMaxLoopLimit(int_8 lim=0) { maxlooplimit = lim; }
97 inline int_8 GetMaxLoopLimit() { return maxlooplimit; }
98
99 string GetCurrentPrompt() { return curprompt; }
100
101
102 virtual void HelptoLaTeX(string const & flnm);
103
104 inline CmdInterpreter* CurrentInterpreter() { return(curcmdi); }
105
106
107// Utilitaire pour decoupage en mot
108 static int LineToWords(string& line, string& kw, vector<string>& tokens,
109 string& toks, bool uq=true);
110protected:
[2473]111 virtual int ParseLineExecute(string& line, bool qw=true);
112
113 virtual int ExecuteCommandLine(string & keyw, vector<string> & args,
114 string & toks);
115
[2466]116 virtual bool CheckHelpGrp(string& grp, int& gid, string& desc);
117 inline bool CheckHelpGrp(string& grp, int& gid)
118 { string desc=""; return CheckHelpGrp(grp, gid, desc); }
119
[2473]120 virtual int SubstituteVars(string & s, string & s2);
[2446]121 int EvaluateTest(vector<string> & args,
122 string & line, bool & res);
123 int EvalRPNExpr(vector<string> & args, string & line);
124
[2473]125 virtual bool GetVar(string & vn, string & vv);
126 virtual string GetVariable(string const & vn);
127 virtual bool HasVariable(string const & vn);
128 virtual bool SetVar(string const & vn, string const & vv);
129 virtual bool DeleteVar(string const & vn);
130 virtual void ListVars();
131
132 virtual string GetTmpDir();
133
134 virtual void SetCurrentPrompt(const char* pr);
[2446]135 inline void SetCurrentPrompt(string const & pr) { SetCurrentPrompt(pr.c_str()); }
136
137 virtual void ShowMessage(const char * msg, int att);
138
139 void PushStack(vector<string> & args);
140 void PopStack(bool psta=true);
141
142 CmdInterpreter* curcmdi;
143
144// Gestion des variables
145 DVList variables;
146
147// Pour enregistrer la liste de commandes et leurs executeurs et le help
148 struct cmdex {int group; string us; CmdExecutor * cex; } ;
[2466]149 struct hgrpst {int gid; string desc; } ; // Identification+description d'un groupe de help
150 typedef map<string, hgrpst, less<string> > CmdHGroup; // Liste des groupes de commandes
[2446]151 CmdHGroup cmdhgrp;
[2466]152 int cmdgrpid; // Numero de groupe courant
153 typedef map<string, cmdex, less<string> > CmdExmap;
154 CmdExmap cmdexmap; // Liste des commandes et leurs executeurs
155 CmdExmap helpexmap; // Pour les helps sans commande
[2446]156
157// Pour garder la liste des modules
158 typedef map<string, PDynLinkMgr* , less<string> > Modmap;
159 Modmap modmap;
160
161// Pour garder la liste des interpreteur
162 typedef map<string, CmdInterpreter*, less<string> > InterpMap;
163 InterpMap interpmap;
164
165// Pour stocker les scripts definis ds l'interpreteur
166 typedef map<string, CommanderScript*, less<string> > ScriptList;
167 ScriptList mScripts; // Liste des scripts
168 CommanderScript* curscript; // Script en cours de definition
169
170// Pour stocker les alias definies par l'interpreteur
171 typedef map<string, string, less<string> > CmdStrList;
172 CmdStrList mAliases; // Liste des alias
173
174// Le stack pour les arguments des .pic et des scripts
175 stack< vector<string> > ArgsStack;
176// Stack pour les Prompts
177 stack<string> PromptStack;
178
179 stack< CommanderBloc * > CmdBlks; // Bloc de commande courant (foreach, ...)
180 int felevel; // foreach-for level
181 int_8 maxlooplimit; // Limite maximum des boucles
182 stack< list<char> > TestsStack; // Stack des resultats de test
183 list<char>::iterator tresit; // Test courant
184 bool curtestresult; // Resultat courant des tests
185
186 bool mulinefg; // Bloc multi-lignes (ligne suite)
187 string mulinecmd; // Commande multi-lignes
188 string spromptmul; // Prompt console avant multi-ligne
189 string curprompt;
190
191 ofstream hist; // History file
192 bool histon; // True -> history file
193 bool trace; // Trace flag
194 bool timing; // Display CPU Time
195 Timer* gltimer; // pour Display CPU Time
196
197};
198
199
200/* end of ifdef COMMANDER_H_SEEN */
201#endif
202
Note: See TracBrowser for help on using the repository browser.