-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommand.h
executable file
·260 lines (183 loc) · 8.1 KB
/
Command.h
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/* file: Command.h
Command Class Header File
==================================================
Wimpy Shell Project - Com Sci 342
Shea Daniels
This class is based around the data and functions
needed for storing and parsing command lines.
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! Information on constructors: !
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Command()
--------------------------------------------------
This is the basic constructor for the class.
POST: The object has been initialized.
Command(string pipe_me)
--------------------------------------------------
This is the constructor for commands that are part
of a larger piped job.
PRE: pipe_me is a string.
POST: The object has been initialized.
If the passed string pipe_me equal "pipe",
then the command will be set up to belong
to a larger PipedCommand object.
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! Information on public methods: !
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
void setCommandText(string new_cmd_text)
--------------------------------------------------
Sets the command text of this object.
PRE: new_cmd_text is a string containing the text
of a command line.
POST: new_cmd_text is now the value of this object's
command text.
string getCommandText() const
--------------------------------------------------
Returns the command text of this object.
POST: A string containing the text of the command
line is returned.
string getErrorReason() const
--------------------------------------------------
Returns the reason the command couldn't be parsed.
POST: If command couldn't be parsed, returns a string
containing the reason. If command was parsed
successfully or has not been parsed at all,
returns "none".
string getCommandName() const
--------------------------------------------------
Returns the name of the executable for the command.
POST: Returns the name of the executable. Returns
"none" if the command hasn't been parsed.
string getInputFileName() const
--------------------------------------------------
Returns the name of the file that input should
be redirected to.
POST: Returns the filename. If the command has not
been parsed or is not input redirected,
"none" is returned.
string getOutputFileName() const
--------------------------------------------------
Returns the name of the file that output should
be redirected to.
POST: Returns the filename. If the command has not
been parsed or is not output redirected,
"none" is returned.
vector<string> getArgs() const
--------------------------------------------------
Returns the vector that contains all of the command
arguments.
POST: Returns the vector. If the command has
not been parsed or the command has no
arguments, an empty vector is returned.
char ** getArgsArray()
--------------------------------------------------
Returns a pointer to an array of pointers to null
terminated character arrays containing the command
arguments. This data structure is appropriate for
use with the execv() system call.
PRE: The command has been parsed.
POST: The pointer to the dynamically allocated
array is returned.
bool isInputRedirected() const
--------------------------------------------------
Returns whether the command will have its input
redirected to a file instead of std input.
POST: Returns true if the input is redirected.
Returns false if it is not.
bool isOutputRedirected() const
--------------------------------------------------
Returns whether the command will have its output
redirected to a file instead of std out.
POST: Returns true if the output is redirected.
Returns false if it is not.
bool isBackgroundJob() const
--------------------------------------------------
Returns whether the command will execute as a
background job.
POST: Returns true if the command is a background
job. Returns false if it is not.
bool isPipedJob() const
--------------------------------------------------
Returns whether the command is part of a larger
PipedCommand object.
POST: Returns true if the command will be piped.
Returns false if it won't be.
bool parseCommandText()
--------------------------------------------------
Takes the command line text of this object and
parses it into an executable word, arguments,
and redirects based on separator chars.
PRE: The command line text of this object has
been set.
POST: A boolean is returned based on whether the
command was parsed correctly or not. If false
is returned, the reason is stored by this
object. If the command was parsed correctly
and true is returned, all of the data
will be set correctly and available using
the public "get" commands.
void resetCommand()
--------------------------------------------------
Resets the entire object to its original state,
as if it had just been constructed using the
basic constructor.
POST: This object is reset.
*/
#ifndef CMD_HEADER
#define CMD_HEADER
#include <string>
#include <vector>
#include <iostream>
using namespace std;
class Command {
public:
// constructors
Command();
Command(string pipe_me);
// set functions
void setCommandText(string new_cmd_text);
// get functions
string getCommandText() const;
string getErrorReason() const;
string getCommandName() const;
string getInputFileName() const;
string getOutputFileName() const;
vector<string> getArgs() const;
char ** getArgsArray();
// bool functions that return special command options
bool isInputRedirected() const;
bool isOutputRedirected() const;
bool isBackgroundJob() const;
bool isPipedJob() const;
// other functions
bool parseCommandText();
void resetCommand();
private:
// checkers
string checkForUnsupportedFeatures();
bool isSep(int currentPos);
// word parsing functions
int parseCmdString(int currentPos);
int parseArgString(int currentPos);
int parseInputFileString(int currentPos);
int parseOutputFileString(int currentPos);
int parseLeadingSpaces(int currentPos);
//------------------------------------------------------------
// Data
//------------------------------------------------------------
// special command option flags (input/output redirect & pipes)
bool input_redirect;
bool output_redirect;
bool background_job;
bool piped_job;
// text of entire cmd line
string command_text;
// parsed command data
string cmd_name;
string input_file;
string output_file;
string error_reason;
// space for arguments
vector<string> cmd_arguments;
};
#endif