Move input stream into IfaceTerm

This commit is contained in:
caheckman 2021-01-25 16:25:04 -05:00
parent f0474b13be
commit 8d2b737a72
4 changed files with 43 additions and 22 deletions

View file

@ -16,8 +16,9 @@
#include "ifaceterm.hh" #include "ifaceterm.hh"
IfaceTerm::IfaceTerm(const string &prmpt,istream &is,ostream &os) IfaceTerm::IfaceTerm(const string &prmpt,istream &is,ostream &os)
: IfaceStatus(prmpt,is,os) : IfaceStatus(prmpt,os)
{ {
sptr = &is;
#ifdef __TERMINAL__ #ifdef __TERMINAL__
struct termios ittypass; struct termios ittypass;
@ -229,3 +230,28 @@ void IfaceTerm::readLine(string &line)
} while(val != '\n'); } 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();
}

View file

@ -29,9 +29,14 @@ class IfaceTerm : public IfaceStatus {
int4 ifd; // Underlying file descriptor int4 ifd; // Underlying file descriptor
struct termios itty; // Original terminal settings struct termios itty; // Original terminal settings
#endif #endif
istream *sptr; // Where to get input
vector<istream *> inputstack;
int4 doCompletion(string &line,int4 cursor); int4 doCompletion(string &line,int4 cursor);
virtual void readLine(string &line); virtual void readLine(string &line);
public: public:
IfaceTerm(const string &prmpt,istream &is,ostream &os); IfaceTerm(const string &prmpt,istream &is,ostream &os);
virtual ~IfaceTerm(void); virtual ~IfaceTerm(void);
virtual void pushScript(const string &filename,const string &newprompt);
virtual void popScript(void);
virtual bool isStreamFinished(void) const;
}; };

View file

@ -114,10 +114,9 @@ bool RemoteSocket::isSocketOpen(void)
#endif #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; optr = &os;
fileoptr = optr; // Bulk out, defaults to command line output fileoptr = optr; // Bulk out, defaults to command line output
sorted = false; 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) void IfaceStatus::pushScript(const string &filename,const string &newprompt)
{ // Push new input stream on stack (with new prompt) { // 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); promptstack.push_back(prompt);
uint4 flags = 0; uint4 flags = 0;
if (errorisdone) if (errorisdone)
flags |= 1; flags |= 1;
flagstack.push_back(flags); flagstack.push_back(flags);
sptr = s;
prompt = newprompt; prompt = newprompt;
} }
void IfaceStatus::popScript(void) void IfaceStatus::popScript(void)
{ // Pop the current input stream (and current prompt) { // Pop the current input stream (and current prompt)
delete sptr;
sptr = inputstack.back();
inputstack.pop_back();
prompt = promptstack.back(); prompt = promptstack.back();
promptstack.pop_back(); promptstack.pop_back();
uint4 flags = flagstack.back(); uint4 flags = flagstack.back();
@ -162,7 +153,7 @@ void IfaceStatus::popScript(void)
void IfaceStatus::reset(void) void IfaceStatus::reset(void)
{ {
while(!inputstack.empty()) while(!promptstack.empty())
popScript(); popScript();
errorisdone = false; errorisdone = false;
done = false; done = false;
@ -228,7 +219,7 @@ IfaceStatus::~IfaceStatus(void)
((ofstream *)fileoptr)->close(); ((ofstream *)fileoptr)->close();
delete fileoptr; delete fileoptr;
} }
while(!inputstack.empty()) while(!promptstack.empty())
popScript(); popScript();
for(int4 i=0;i<comlist.size();++i) for(int4 i=0;i<comlist.size();++i)
delete comlist[i]; delete comlist[i];

View file

@ -131,8 +131,8 @@ public:
static void registerAllCommands(IfaceStatus *status); static void registerAllCommands(IfaceStatus *status);
}; };
/// \brief Current state of the console mode interface
class IfaceStatus { class IfaceStatus {
vector<istream *> inputstack;
vector<string> promptstack; vector<string> promptstack;
vector<uint4> flagstack; vector<uint4> flagstack;
string prompt; string prompt;
@ -140,13 +140,12 @@ class IfaceStatus {
int4 curhistory; // most recent history int4 curhistory; // most recent history
vector<string> history; vector<string> history;
bool sorted; // Are commands sorted bool sorted; // Are commands sorted
bool inerror; // -true- if last command did not succeed
bool errorisdone; // -true- if any error terminates the process bool errorisdone; // -true- if any error terminates the process
void restrict(vector<IfaceCommand *>::const_iterator &first,vector<IfaceCommand *>::const_iterator &last,vector<string> &input); 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); void saveHistory(const string &line);
protected: protected:
istream *sptr; // Where to get input bool inerror; // -true- if last command did not succeed
vector<IfaceCommand *> comlist; // List of commands vector<IfaceCommand *> comlist; // List of commands
map<string,IfaceData *> datamap; // Data associated with particular modules map<string,IfaceData *> datamap; // Data associated with particular modules
int4 expandCom(vector<string> &expand,istream &s, int4 expandCom(vector<string> &expand,istream &s,
@ -157,13 +156,13 @@ public:
ostream *optr; // Where to put command line output ostream *optr; // Where to put command line output
ostream *fileoptr; // Where to put bulk 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); virtual ~IfaceStatus(void);
void setErrorIsDone(bool val) { errorisdone = val; } void setErrorIsDone(bool val) { errorisdone = val; }
void pushScript(const string &filename,const string &newprompt); virtual void pushScript(const string &filename,const string &newprompt);
void popScript(void); virtual void popScript(void);
void reset(void); void reset(void);
int4 getNumInputStreamSize(void) const { return inputstack.size(); } int4 getNumInputStreamSize(void) const { return promptstack.size(); }
void writePrompt(void) { *optr << prompt; } void writePrompt(void) { *optr << prompt; }
void registerCom(IfaceCommand *fptr, const char *nm1, void registerCom(IfaceCommand *fptr, const char *nm1,
const char *nm2 = (const char *)0, const char *nm2 = (const char *)0,
@ -174,7 +173,7 @@ public:
bool runCommand(void); bool runCommand(void);
void getHistory(string &line,int4 i) const; void getHistory(string &line,int4 i) const;
int4 getHistorySize(void) const { return history.size(); } 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; } bool isInError(void) const { return inerror; }
void evaluateError(void); void evaluateError(void);
static void wordsToString(string &res,const vector<string> &list); static void wordsToString(string &res,const vector<string> &list);