My Project
interface.hh
1 /* ###
2  * IP: GHIDRA
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 // Very generic command line executor class: IfaceStatus
17 // A new class instance derived from IfaceCommand is attached to a command line via registerCom
18 // i.e.
19 // IfaceStatus stat(cin,cout);
20 // stat.registerCom(new IfcQuit(),"quit");
21 // stat.registerCom(new IfcOpenfileAppend(),"openfile","append");
22 // stat.mainloop();
23 
24 // Command line processing is started with mainloop, which prints a
25 // prompt set with setprompt, allows bash style command line editing, including
26 // command completion and history, and executes the corresponding IfaceCommand.execute callback.
27 // Command words only have to match enough to disambiguate it from other commands.
28 
29 // Custom history size can be passed in constructor to IfaceStatus.
30 // Applications should inherit from base class IfaceStatus in order
31 // to get custom data into IfaceCommand callbacks and to redefine
32 // the virtual function execute for custom error handling.
33 
34 #ifndef __INTERFACE__
35 #define __INTERFACE__
36 
37 #include "capability.hh"
38 #include <string>
39 #include <map>
40 #include <algorithm>
41 #include <fstream>
42 #include <sstream>
43 #include <cstdio>
44 
45 using namespace std;
46 
47 struct IfaceError {
48  string explain; // Explanatory string
49  IfaceError(const string &s) { explain = s; }
50 };
51 
52 struct IfaceParseError : public IfaceError {
53  IfaceParseError(const string &s) : IfaceError(s) {}
54 };
55 
57  IfaceExecutionError(const string &s) : IfaceError(s) {}
58 };
59 
60 class IfaceStatus; // Forward declaration
61 
62 class IfaceData { // Data specialized for a particular command
63 public:
64  virtual ~IfaceData(void) {}
65 };
66 
67 class IfaceCommand {
68  vector<string> com; // The command
69 public:
70  virtual ~IfaceCommand(void) {}
71  virtual void setData(IfaceStatus *root,IfaceData *data)=0;
72  virtual void execute(istream &s)=0;
73  virtual string getModule(void) const=0;
74  virtual IfaceData *createData(void)=0;
75  void addWord(const string &temp) { com.push_back(temp); }
76  void removeWord(void) { com.pop_back(); }
77  const string &getCommandWord(int4 i) const { return com[i]; }
78  void addWords(const vector<string> &wordlist);
79  int4 numWords(void) const { return com.size(); }
80  void commandString(string &res) const;
81  int4 compare(const IfaceCommand &op2) const;
82 };
83 
85 public:
86  virtual void setData(IfaceStatus *root,IfaceData *data) {}
87  virtual void execute(istream &s) {}
88  virtual string getModule(void) const { return "dummy"; }
89  virtual IfaceData *createData(void) { return (IfaceData *)0; }
90 };
91 
92 inline bool compare_ifacecommand(const IfaceCommand *a,const IfaceCommand *b) {
93  return (0>a->compare(*b));
94 }
95 
97  static vector<IfaceCapability *> thelist;
98 protected:
99  string name; // Identifying name for the capability
100 public:
101  const string &getName(void) const { return name; }
102  virtual void initialize(void);
103  virtual void registerCommands(IfaceStatus *status)=0;
104 
105  static void registerAllCommands(IfaceStatus *status);
106 };
107 
108 class IfaceStatus {
109  vector<istream *> inputstack;
110  vector<string> promptstack;
111  vector<uint4> flagstack;
112  string prompt;
113  int4 maxhistory;
114  int4 curhistory; // most recent history
115  vector<string> history;
116  bool sorted; // Are commands sorted
117  bool inerror; // -true- if last command did not succeed
118  bool errorisdone; // -true- if any error terminates the process
119  void restrict(vector<IfaceCommand *>::const_iterator &first,vector<IfaceCommand *>::const_iterator &last,vector<string> &input);
120  virtual void readLine(string &line) { getline(*sptr,line,'\n'); }
121  void saveHistory(const string &line);
122 protected:
123  istream *sptr; // Where to get input
124  vector<IfaceCommand *> comlist; // List of commands
125  map<string,IfaceData *> datamap; // Data associated with particular modules
126  int4 expandCom(vector<string> &expand,istream &s,
127  vector<IfaceCommand *>::const_iterator &first,
128  vector<IfaceCommand *>::const_iterator &last);
129 public:
130  bool done;
131  ostream *optr; // Where to put command line output
132  ostream *fileoptr; // Where to put bulk output
133 
134  IfaceStatus(const string &prmpt,istream &is,ostream &os,int4 mxhist=10);
135  virtual ~IfaceStatus(void);
136  void setErrorIsDone(bool val) { errorisdone = val; }
137  void pushScript(const string &filename,const string &newprompt);
138  void popScript(void);
139  int4 getNumInputStreamSize(void) const { return inputstack.size(); }
140  void writePrompt(void) { *optr << prompt; }
141  void registerCom(IfaceCommand *fptr, const char *nm1,
142  const char *nm2 = (const char *)0,
143  const char *nm3 = (const char *)0,
144  const char *nm4 = (const char *)0,
145  const char *nm5 = (const char *)0);
146  IfaceData *getData(const string &nm) const;
147  bool runCommand(void);
148  void getHistory(string &line,int4 i) const;
149  int4 getHistorySize(void) const { return history.size(); }
150  bool isStreamFinished(void) const { if (done||inerror) return true; return sptr->eof(); }
151  bool isInError(void) const { return inerror; }
152  void evaluateError(void);
153  static void wordsToString(string &res,const vector<string> &list);
154 };
155 
157 protected:
158  IfaceStatus *status;
159 public:
160  virtual void setData(IfaceStatus *root,IfaceData *data) { status = root; }
161  virtual string getModule(void) const { return "base"; }
162  virtual IfaceData *createData(void) { return (IfaceData *)0; }
163 };
164 
165 class IfcQuit : public IfaceBaseCommand {
166 public:
167  virtual void execute(istream &s);
168 };
169 
170 class IfcHistory : public IfaceBaseCommand {
171 public:
172  virtual void execute(istream &s);
173 };
174 
176 public:
177  virtual void execute(istream &s);
178 };
179 
181 public:
182  virtual void execute(istream &s);
183 };
184 
186 public:
187  virtual void execute(istream &s);
188 };
189 
190 class IfcEcho : public IfaceBaseCommand {
191 public:
192  virtual void execute(istream &s);
193 };
194 
195 #endif
Definition: interface.hh:62
Definition: interface.hh:185
Infrastructure for discovering code extensions to the decompiler.
Definition: interface.hh:175
Definition: interface.hh:156
Definition: interface.hh:56
Definition: interface.hh:52
Definition: interface.hh:84
Definition: interface.hh:190
Definition: interface.hh:47
Definition: interface.hh:165
Definition: interface.hh:108
Definition: interface.hh:67
Class for automatically registering extension points to the decompiler.
Definition: capability.hh:36
Definition: interface.hh:180
Definition: interface.hh:96
Definition: interface.hh:170