Skip to content

Ghidra is a software reverse engineering (SRE) framework

Move input stream into IfaceTerm

caheckman8d2b737a722e
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/ifaceterm.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/ifaceterm.cc
index a7a105c024..8d92af273e 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/ifaceterm.cc
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/ifaceterm.cc
@@ -16,8 +16,9 @@
#include "ifaceterm.hh"
IfaceTerm::IfaceTerm(const string &prmpt,istream &is,ostream &os)
- : IfaceStatus(prmpt,is,os)
+ : IfaceStatus(prmpt,os)
{
+ sptr = &is;
#ifdef __TERMINAL__
struct termios ittypass;
@@ -229,3 +230,28 @@ void IfaceTerm::readLine(string &line)
} while(val != '\n');
}
+void IfaceTerm::pushScript(const string &filename,const string &newprompt)
+
+{
+ ifstream *s = new ifstream(filename.c_str());
+ if (!*s)
+ throw IfaceParseError("Unable to open script file");
+ inputstack.push_back(sptr);
+ sptr = s;
+ IfaceStatus::pushScript(filename,newprompt);
+}
+
+void IfaceTerm::popScript(void)
+
+{
+ delete sptr;
+ sptr = inputstack.back();
+ inputstack.pop_back();
+}
+
+bool IfaceTerm::isStreamFinished(void) const
+
+{
+ if (done||inerror) return true;
+ return sptr->eof();
+}
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/ifaceterm.hh b/Ghidra/Features/Decompiler/src/decompile/cpp/ifaceterm.hh
index 46b3dc6c15..e47c5c8e06 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/ifaceterm.hh
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/ifaceterm.hh
@@ -29,9 +29,14 @@ class IfaceTerm : public IfaceStatus {
int4 ifd; // Underlying file descriptor
struct termios itty; // Original terminal settings
#endif
+ istream *sptr; // Where to get input
+ vector<istream *> inputstack;
int4 doCompletion(string &line,int4 cursor);
virtual void readLine(string &line);
public:
IfaceTerm(const string &prmpt,istream &is,ostream &os);
virtual ~IfaceTerm(void);
+ virtual void pushScript(const string &filename,const string &newprompt);
+ virtual void popScript(void);
+ virtual bool isStreamFinished(void) const;
};
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/interface.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/interface.cc
index b90f391f6a..701713ab17 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/interface.cc
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/interface.cc
@@ -114,10 +114,9 @@ bool RemoteSocket::isSocketOpen(void)
#endif
-IfaceStatus::IfaceStatus(const string &prmpt,istream &is,ostream &os,int4 mxhist)
+IfaceStatus::IfaceStatus(const string &prmpt,ostream &os,int4 mxhist)
{
- sptr = &is;
optr = &os;
fileoptr = optr; // Bulk out, defaults to command line output
sorted = false;
@@ -132,25 +131,17 @@ IfaceStatus::IfaceStatus(const string &prmpt,istream &is,ostream &os,int4 mxhist
void IfaceStatus::pushScript(const string &filename,const string &newprompt)
{ // Push new input stream on stack (with new prompt)
- ifstream *s = new ifstream(filename.c_str());
- if (!*s)
- throw IfaceParseError("Unable to open script file");
- inputstack.push_back(sptr);
promptstack.push_back(prompt);
uint4 flags = 0;
if (errorisdone)
flags |= 1;
flagstack.push_back(flags);
- sptr = s;
prompt = newprompt;
}
void IfaceStatus::popScript(void)
{ // Pop the current input stream (and current prompt)
- delete sptr;
- sptr = inputstack.back();
- inputstack.pop_back();
prompt = promptstack.back();
promptstack.pop_back();
uint4 flags = flagstack.back();
@@ -162,7 +153,7 @@ void IfaceStatus::popScript(void)
void IfaceStatus::reset(void)
{
- while(!inputstack.empty())
+ while(!promptstack.empty())
popScript();
errorisdone = false;
done = false;
@@ -228,7 +219,7 @@ IfaceStatus::~IfaceStatus(void)
((ofstream *)fileoptr)->close();
delete fileoptr;
}
- while(!inputstack.empty())
+ while(!promptstack.empty())
popScript();
for(int4 i=0;i<comlist.size();++i)
delete comlist[i];
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/interface.hh b/Ghidra/Features/Decompiler/src/decompile/cpp/interface.hh
index 9edf4dc6fb..8afe79f6bd 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/interface.hh
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/interface.hh
@@ -131,8 +131,8 @@ public:
static void registerAllCommands(IfaceStatus *status);
};
+/// \brief Current state of the console mode interface
class IfaceStatus {
- vector<istream *> inputstack;
vector<string> promptstack;
vector<uint4> flagstack;
string prompt;
@@ -140,13 +140,12 @@ class IfaceStatus {
int4 curhistory; // most recent history
vector<string> history;
bool sorted; // Are commands sorted
- bool inerror; // -true- if last command did not succeed
bool errorisdone; // -true- if any error terminates the process
void restrict(vector<IfaceCommand *>::const_iterator &first,vector<IfaceCommand *>::const_iterator &last,vector<string> &input);
- virtual void readLine(string &line) { getline(*sptr,line,'\n'); }
+ virtual void readLine(string &line)=0;
void saveHistory(const string &line);
protected:
- istream *sptr; // Where to get input
+ bool inerror; // -true- if last command did not succeed
vector<IfaceCommand *> comlist; // List of commands
map<string,IfaceData *> datamap; // Data associated with particular modules
int4 expandCom(vector<string> &expand,istream &s,
@@ -157,13 +156,13 @@ public:
ostream *optr; // Where to put command line output
ostream *fileoptr; // Where to put bulk output
- IfaceStatus(const string &prmpt,istream &is,ostream &os,int4 mxhist=10);
+ IfaceStatus(const string &prmpt,ostream &os,int4 mxhist=10);
virtual ~IfaceStatus(void);
void setErrorIsDone(bool val) { errorisdone = val; }
- void pushScript(const string &filename,const string &newprompt);
- void popScript(void);
+ virtual void pushScript(const string &filename,const string &newprompt);
+ virtual void popScript(void);
void reset(void);
- int4 getNumInputStreamSize(void) const { return inputstack.size(); }
+ int4 getNumInputStreamSize(void) const { return promptstack.size(); }
void writePrompt(void) { *optr << prompt; }
void registerCom(IfaceCommand *fptr, const char *nm1,
const char *nm2 = (const char *)0,
@@ -174,7 +173,7 @@ public:
bool runCommand(void);
void getHistory(string &line,int4 i) const;
int4 getHistorySize(void) const { return history.size(); }
- bool isStreamFinished(void) const { if (done||inerror) return true; return sptr->eof(); }
+ virtual bool isStreamFinished(void) const=0;
bool isInError(void) const { return inerror; }
void evaluateError(void);
static void wordsToString(string &res,const vector<string> &list);