Ghidra Decompiler Analysis Engine
interface.hh
Go to the documentation of this file.
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  */
18 
19 #ifndef __INTERFACE__
20 #define __INTERFACE__
21 
22 #include "capability.hh"
23 #include <string>
24 #include <map>
25 #include <algorithm>
26 #include <fstream>
27 #include <sstream>
28 #include <cstdio>
29 
30 using namespace std;
31 
32 #ifdef __REMOTE_SOCKET__
33 
39 class RemoteSocket {
40  int fileDescriptor;
41  basic_filebuf<char> *inbuf;
42  basic_filebuf<char> *outbuf;
43  istream *inStream;
44  ostream *outStream;
45  bool isOpen;
46 public:
47  RemoteSocket(void);
48  ~RemoteSocket(void) { close(); }
49  bool open(const string &filename);
50  bool isSocketOpen(void);
51  istream *getInputStream(void) { return inStream; }
52  ostream *getOutputStream(void) { return outStream; }
53  void close(void);
54 };
55 
56 #endif
57 
59 struct IfaceError {
60  string explain;
61  IfaceError(const string &s) { explain = s; }
62 };
63 
68 struct IfaceParseError : public IfaceError {
69  IfaceParseError(const string &s) : IfaceError(s) {}
70 };
71 
76  IfaceExecutionError(const string &s) : IfaceError(s) {}
77 };
78 
79 class IfaceStatus; // Forward declaration
80 
85 class IfaceData {
86 public:
87  virtual ~IfaceData(void) {}
88 };
89 
97 class IfaceCommand {
98  vector<string> com;
99 public:
100  virtual ~IfaceCommand(void) {}
101 
106  virtual void setData(IfaceStatus *root,IfaceData *data)=0;
107 
111  virtual void execute(istream &s)=0;
112 
117  virtual string getModule(void) const=0;
118 
123  virtual IfaceData *createData(void)=0;
124 
128  void addWord(const string &temp) { com.push_back(temp); }
129 
130  void removeWord(void) { com.pop_back(); }
131  const string &getCommandWord(int4 i) const { return com[i]; }
132  void addWords(const vector<string> &wordlist);
133  int4 numWords(void) const { return com.size(); }
134  void commandString(string &res) const;
135  int4 compare(const IfaceCommand &op2) const;
136 };
137 
140 public:
141  virtual void setData(IfaceStatus *root,IfaceData *data) {}
142  virtual void execute(istream &s) {}
143  virtual string getModule(void) const { return "dummy"; }
144  virtual IfaceData *createData(void) { return (IfaceData *)0; }
145 };
146 
152 inline bool compare_ifacecommand(const IfaceCommand *a,const IfaceCommand *b) {
153  return (0>a->compare(*b));
154 }
155 
162  static vector<IfaceCapability *> thelist;
163 protected:
164  string name;
165 public:
166  const string &getName(void) const { return name; }
167  virtual void initialize(void);
168  virtual void registerCommands(IfaceStatus *status)=0;
169 
170  static void registerAllCommands(IfaceStatus *status);
171 };
172 
184 
189 
195 class IfaceStatus {
196  vector<string> promptstack;
197  vector<uint4> flagstack;
198  string prompt;
199  int4 maxhistory;
200  int4 curhistory;
201  vector<string> history;
202  bool sorted;
203  bool errorisdone;
204  void restrictCom(vector<IfaceCommand *>::const_iterator &first,
205  vector<IfaceCommand *>::const_iterator &last,vector<string> &input);
206 
210  virtual void readLine(string &line)=0;
211  void saveHistory(const string &line);
212 protected:
213  bool inerror;
214  vector<IfaceCommand *> comlist;
215  map<string,IfaceData *> datamap;
216  int4 expandCom(vector<string> &expand,istream &s,
217  vector<IfaceCommand *>::const_iterator &first,
218  vector<IfaceCommand *>::const_iterator &last);
219 public:
220  bool done;
221  ostream *optr;
222  ostream *fileoptr;
223 
224  IfaceStatus(const string &prmpt,ostream &os,int4 mxhist=10);
225  virtual ~IfaceStatus(void);
226  void setErrorIsDone(bool val) { errorisdone = val; }
227  virtual void pushScript(const string &filename,const string &newprompt);
228  virtual void popScript(void);
229  void reset(void);
230  int4 getNumInputStreamSize(void) const { return promptstack.size(); }
231  void writePrompt(void) { *optr << prompt; }
232  void registerCom(IfaceCommand *fptr, const char *nm1,
233  const char *nm2 = (const char *)0,
234  const char *nm3 = (const char *)0,
235  const char *nm4 = (const char *)0,
236  const char *nm5 = (const char *)0);
237  IfaceData *getData(const string &nm) const;
238  bool runCommand(void);
239  void getHistory(string &line,int4 i) const;
240  int4 getHistorySize(void) const { return history.size(); }
241  virtual bool isStreamFinished(void) const=0;
242  bool isInError(void) const { return inerror; }
243  void evaluateError(void);
244  static void wordsToString(string &res,const vector<string> &list);
245 };
246 
252 protected:
254 public:
255  virtual void setData(IfaceStatus *root,IfaceData *data) { status = root; }
256  virtual string getModule(void) const { return "base"; }
257  virtual IfaceData *createData(void) { return (IfaceData *)0; }
258 };
259 
260 class IfcQuit : public IfaceBaseCommand {
261 public:
262  virtual void execute(istream &s);
263 };
264 
265 class IfcHistory : public IfaceBaseCommand {
266 public:
267  virtual void execute(istream &s);
268 };
269 
271 public:
272  virtual void execute(istream &s);
273 };
274 
276 public:
277  virtual void execute(istream &s);
278 };
279 
281 public:
282  virtual void execute(istream &s);
283 };
284 
285 class IfcEcho : public IfaceBaseCommand {
286 public:
287  virtual void execute(istream &s);
288 };
289 
290 #endif
IfaceCommandDummy::createData
virtual IfaceData * createData(void)
Create a specialized data object for this command (and its module)
Definition: interface.hh:144
compare_ifacecommand
bool compare_ifacecommand(const IfaceCommand *a, const IfaceCommand *b)
Compare to commands as pointers.
Definition: interface.hh:152
IfaceCommandDummy::setData
virtual void setData(IfaceStatus *root, IfaceData *data)
Associate a specific data object with this command.
Definition: interface.hh:141
IfcOpenfile
Open file command to redirect bulk output to a specific file stream.
Definition: interface.hh:270
IfaceParseError
An exception describing a parsing error in a command line.
Definition: interface.hh:68
IfaceStatus::comlist
vector< IfaceCommand * > comlist
List of registered commands.
Definition: interface.hh:214
IfcQuit
Quit command to terminate processing from the given interface.
Definition: interface.hh:260
IfcClosefile
Close command, closing the current bulk output file.
Definition: interface.hh:280
IfaceCommand::compare
int4 compare(const IfaceCommand &op2) const
Order two commands by their command line strings.
Definition: interface.cc:467
IfaceCapability::name
string name
Identifying name for the capability.
Definition: interface.hh:164
IfaceBaseCommand
A root class for a basic set of commands.
Definition: interface.hh:251
execute
void execute(IfaceStatus *status, IfaceDecompData *dcp)
Execute one command for the console.
Definition: ifacedecomp.cc:3273
IfcEcho
Echo command to echo the current command line to the bulk output stream.
Definition: interface.hh:285
IfaceStatus::optr
ostream * optr
Where to put command line output.
Definition: interface.hh:221
IfaceCommand::addWord
void addWord(const string &temp)
Add a token to the command line string associated with this command.
Definition: interface.hh:128
IfaceError::explain
string explain
Explanatory string.
Definition: interface.hh:60
IfaceStatus
A generic console mode interface and command executor.
Definition: interface.hh:195
capability.hh
Infrastructure for discovering code extensions to the decompiler.
IfaceExecutionError
An exception throw during the execution of a command.
Definition: interface.hh:75
IfaceBaseCommand::getModule
virtual string getModule(void) const
Get the formal module name to which this command belongs.
Definition: interface.hh:256
IfaceStatus::fileoptr
ostream * fileoptr
Where to put bulk output.
Definition: interface.hh:222
IfaceError
An exception specific to the command line interface.
Definition: interface.hh:59
IfaceCommandDummy::getModule
virtual string getModule(void) const
Get the formal module name to which this command belongs.
Definition: interface.hh:143
IfcHistory
History command to list the most recent successful commands.
Definition: interface.hh:265
CapabilityPoint
Class for automatically registering extension points to the decompiler.
Definition: capability.hh:36
IfaceCommand
A command that can be executed from the command line.
Definition: interface.hh:97
IfaceCapability
Groups of console commands that are discovered by the loader.
Definition: interface.hh:161
IfcOpenfileAppend
Open file command directing bulk output to be appended to a specific file.
Definition: interface.hh:275
IfaceStatus::done
bool done
Set to true (by a command) to indicate processing is finished.
Definition: interface.hh:220
IfaceStatus::inerror
bool inerror
Set to true if last command did not succeed.
Definition: interface.hh:213
IfaceBaseCommand::setData
virtual void setData(IfaceStatus *root, IfaceData *data)
Associate a specific data object with this command.
Definition: interface.hh:255
IfaceCommandDummy::execute
virtual void execute(istream &s)
Definition: interface.hh:142
IfaceData
Data specialized for a particular command module.
Definition: interface.hh:85
IfaceCommandDummy
A dummy command used during parsing.
Definition: interface.hh:139
IfaceBaseCommand::createData
virtual IfaceData * createData(void)
Create a specialized data object for this command (and its module)
Definition: interface.hh:257
IfaceStatus::datamap
map< string, IfaceData * > datamap
Data associated with particular modules.
Definition: interface.hh:215
IfaceBaseCommand::status
IfaceStatus * status
The interface owning this command instance.
Definition: interface.hh:253