-
Notifications
You must be signed in to change notification settings - Fork 0
/
iash.cpp
146 lines (122 loc) · 3.27 KB
/
iash.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/* See iash.h for the project description.
*
* Copyright (C) 2014 Paul Bonnen
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "iash.h"
#include "cmd/builtins.h"
#include "tools/Tokenizer.h"
#include "CommandPreprocessor.h"
#include <fstream>
using namespace std;
iash::iash (const string &appName)
: m_dispatcher(this), m_env(appName), m_iashCwd(), m_appName(appName)
{
addCommand<EchoCommand>();
addCommand<ExitCommand>();
addCommand<PwdCommand>();
addCommand<ClearCommand>();
addCommand<CatCommand>();
}
Environment* iash::getEnv ()
{
return &m_env;
}
const Directory* iash::getCwd () const
{
return &m_iashCwd;
}
void iash::addCommand (Command *cmd)
{
m_dispatcher.registerCommand(cmd);
}
int iash::runInteractive ()
{
run(cin, true);
return 0;
}
int iash::runScript (const char *fname)
{
string filename = Directory::toPlatform(m_iashCwd.resolvePath(fname));
ifstream fin (filename.c_str());
if (fin) return run(fin);
else
{
cout << m_appName << ": " << fname << ": script could not be opened";
cout << endl;
return 1;
}
}
inline int iash::exec (string &cmd)
{
UserCommand uCmd (cmd, cin, cout);
return m_dispatcher.dispatch(&uCmd);
}
inline int iash::exec (UserCommand *cmd)
{
return m_dispatcher.dispatch(cmd);
}
int iash::run (istream &cmdin, bool showPrompt)
{
CommandPreprocessor processor (this);
string raw;
int returnCode = 0;
m_exitFlag = false;
if (showPrompt) cout << m_appName << "> ";
while (!m_exitFlag && getline(cmdin, raw))
{
//Run commands
try
{
const vector<UserCommand> &commands = processor.process(raw);
for (const UserCommand &cmd : commands)
{
if (cmd.getWholeCommand().size() == 1 &&
cmd.getWholeCommand()[0].empty()) {
continue;
}
returnCode = m_dispatcher.dispatch(&cmd);
if (returnCode == 127) {
cout << "iash: command not found: ";
cout << cmd.getWholeCommand()[0] << endl;
}
//Stop running commands if exit was called
if (m_exitFlag) break;
}
}
catch (TokenizeException &toke)
{
cout << "iash: " << toke.what() << endl;
}
catch (SyntaxException &syne)
{
cout << "iash: " << syne.what() << endl;
}
catch (FileNotFoundException &fnfe)
{
cout << "iash: " << fnfe.what() << endl;
}
//Update environment
m_env.setProtected("?", to_string(returnCode));
if (m_iashCwd.getAbsPath() != m_env.getString("CWD"))
m_env.setProtected("CWD", m_iashCwd.getAbsPath());
if (showPrompt && !m_exitFlag) cout << m_appName << "> ";
}
return m_env.getInt("?");
}
void iash::exitShell()
{
m_exitFlag = true;
}