forked from tsiemens/eece-315-proj1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prompt.c
369 lines (319 loc) · 9.93 KB
/
prompt.c
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#include "definitions.h"
#include "fileGet.h"
#include "prompt.h"
/*
* @desc: Upon first run, this welcome screen runs so that
* the user understands they are running B5's shell.
* @input: none
* @output: none
*/
void welcome()
{
printf("\n%s********************************\n", RED);
printf("* %sThe EECE 315 B5 SHELL %s*\n", BLUE, RED);
printf("********************************\n");
printf("* %sDeveloped by: %s*\n", BLUE, RED);
printf("* %sAndrej Satara, Jessica Seto %s*\n", GREEN, RED);
printf("* %sTrevor Siemens, Rohit Singla %s*\n", GREEN, RED);
printf("********************************\n%s", ENDCLR);
return;
}
/*
* @desc: retrieves the environment variables and
* concatenates a prompt in the following format:
* loginName@machineName:CWD>>
* @params: cmdCount, keeps track of command history
* @return: pPrompt, a char* with the prompt.
*/
char* makePrompt(int cmdCount)
{
char fileBuffer[FILENAME_MAX] = { 0 };
char smallBuffer[32] = { 0 };
char largeBuffer[1024] = { 0 };
char* pPrompt; // the Prompt
char* pHost; // the machine name
char* pCWD; // the Current WOrking Directory
char* pLogin; // current Login name
char intStr[3];
sprintf(intStr,"%d",cmdCount);
pLogin = getenv("LOGNAME");
pCWD = getcwd(fileBuffer,sizeof(fileBuffer));
pCWD = homeToTilda(pCWD);
pHost = gethostname(smallBuffer,sizeof(smallBuffer)) == 0 ? smallBuffer : "B5";
// Checking the Host, CWD, and Login
if(pHost == NULL){
printf("ERROR: no HOSTNAME\n");
}
if(pCWD == NULL){
printf("getcwd() ERROR!\n");
}
if(pLogin == NULL){
printf("ERROR: no LOGIN\n");
}
if( (pLogin != NULL) && (pHost != NULL) ) {
strcat(largeBuffer, BLUE);
strcat(largeBuffer,pLogin);
strcat(largeBuffer, RED);
strcat(largeBuffer,"@");
strcat(largeBuffer, GREEN);
strcat(largeBuffer,pHost);
strcat(largeBuffer, YELLOW);
if(pCWD != NULL) {
strcat(largeBuffer,":");
strcat(largeBuffer,pCWD);
}
strcat(largeBuffer,"[");
strcat(largeBuffer,intStr);
strcat(largeBuffer,"]");
strcat(largeBuffer,"\n>>");
strcat(largeBuffer, ENDCLR);
pPrompt = largeBuffer;
} else {
printf("ERROR: cannot form prompt. set to default\n");
pPrompt = "???$";
}
if( pPrompt == NULL) {
printf("ERROR: prompt is null \n");
}
return pPrompt;
}
/*
* @desc: retrieves the $PATH variable and
* makes sure that its valid. Otherwise sets it
* to a default.
* @param: none
* @return: pPath, a char* with the full path.
*/
char* getPathEnv(){
char* pPath;
// get PATH and LOGNAME env variables
pPath = getenv("PATH");
char* defaultPath = "/bin";
if(pPath == NULL){
pPath = defaultPath; // make default
}
return pPath;
}
/* Converts all spaces outside of quotes to '"'
* EG: |This is "a string" with quotes|
* -> |This"is""a string""with"quotes|
* @param: string with spaces for delimiters. quotes optional.
* NOTE: this string is altered!
* @return: string with only spaces inside quotes.
*/
char* quoteDelimit(char* command){
if( command == NULL ){
return NULL;
}
bool inQuotes = false;
int length = strlen(command);
int i;
for(i = 0; i < length; i++){
if(!inQuotes && command[i] == ' '){
command[i] = '\"';
} else if( !inQuotes && command[i] == '\"' ){
inQuotes = true;
} else if( inQuotes && command[i] == '\"' ){
inQuotes = false;
}
}
return command;
}
/* Creates a command struct from the command line by
* breaking up the command line string into tokens.
* @params: pCommand - a char pointer to the command line
* @return: command - a struct holding the command line strings
* command->name = the name of the command
* command->argv = an array of command parameters
* command->argc = the number of arguments in argv[]
* command->isBg = a bool to specify whether a background command is called
*/
command_t* parseCommand(char * pCommand){
if (strcmp(pCommand, "\n") == CMP_OK){
return NULL;
}
int count = 0;
int strLength = 0;
int nameAlloc = 0;
int argAlloc = 0;
char * pTemp; // Temporary pointer to store the remaining string as it is being parsed
command_t * command;
command = (command_t *)malloc(sizeof(command_t)+ARGV_SIZE*sizeof(char));
// First delete the newline character from the command line string
strLength = strlen(pCommand);
if(pCommand[strLength - 1] == '\n'){
pCommand[strLength - 1] = '\0';
}
pCommand = quoteDelimit(pCommand);
pTemp = strtok(pCommand,"\"");
// Parse the command line
for(count = 0; pTemp != NULL; count++){
// The first element in the command line is the name of the command
if (count == 0){
nameAlloc = (strlen(pTemp)+1)*sizeof(char);
command->name = (char *) malloc(nameAlloc);
strcpy(command->name,pTemp);
argAlloc = (strlen(pTemp)+1)*sizeof(char);
command->argv[count] = (char *) malloc(argAlloc);
strcpy(command->argv[count],pTemp);
pTemp = strtok(NULL,"\"");
} else {
// The following elements in the command line are the command parameters
argAlloc = (strlen(pTemp)+1)*sizeof(char);
command->argv[count] = (char *) malloc(argAlloc);
strcpy(command->argv[count],pTemp);
pTemp = strtok(NULL,"\"");
}
}
command->argv[count] = NULL;
command->argc = count;
if( strcmp(command->argv[command->argc-1],"&") == CMP_OK){
command->isBg = true;
command->argv[command->argc-1] = NULL;
command->argc--;
}else{
command->isBg = false;
}
return command;
}
/* Takes the command struct and takes approproate action.
* Will either execute a shell command, program, or output an error message.
* @params: command - a struct representing the command
* @return: false is the command was note found, true otherwise.
*/
bool processCommand(command_t* command, char* pEnv[]){
char* defaultDir = getenv("HOME");
if( strcmp(command->name, "cd") == CMP_OK) {
if( command->argc == 1 ) {
changeDirectory(defaultDir);
} else {
changeDirectory(command->argv[1]);
}
} else if( strcmp(command->name,"help") == CMP_OK) {
help();
} else if( strcmp(command->name, "senv") == CMP_OK) {
char* envName = NULL;
char* envVal = NULL;
if( command->argc > 1) {
envName = command->argv[1];
}
if( command->argc > 2) {
envVal = command->argv[2];
}
setEnvVar(envName,envVal);
} else if( strcmp(command->name, "usenv") == CMP_OK) {
char* envName = NULL;
if( command->argc > 1) {
envName = command->argv[1];
}
unsetEnvVar(envName);
} else if( strcmp(command->name, "genv") == CMP_OK) {
char* envName = NULL;
if( command-> argc > 1) {
envName = command->argv[1];
}
getEnvVar(envName);
} else {
char* filePath = getFilePath(command->name);
if(filePath != NULL) {
pid_t pid_command = fork();
if(pid_command < CHILD) { // ERROR
fprintf(stderr, "Fork failure!\n");
} else if ( pid_command == CHILD) {
if(command->isBg) {
freopen("/dev/null","w",stdout);
freopen("/dev/null","w",stderr);
}
execve(filePath, command->argv, pEnv);
} else {
if(!command->isBg) {
int stat;
waitpid(pid_command, &stat, 0);
} else {
printf("Process ID: %d\n", pid_command);
}
}
} else if(strcmp(command->name, "quit") == CMP_OK ||
strcmp(command->name, "exit") == CMP_OK ||
strcmp(command->name, "q") == CMP_OK ) {
return false;
} else {
printf("%s: Command not found\n", command->name);
return false;
}
}
return true;
}
/* Prints instructions for shell specific commands
* @params: none
* @return: none
*/
void help()
{
printf("EECE 315 B5 Shell, version 1.0\n");
printf("These shell commands are defined internally:\n");
printf("cd [dir]\n");
printf("help\n");
printf("senv [env var name] [env var value]\n");
printf("usenv [env var name]\n");
printf("genv [env var name]\n");
printf("quit OR exit OR q\n");
printf("Insert '&' after a program command to run in background\n");
return;
}
/* Sets the passed enviroment variable to the passed value
* @param: envName - the name of the environment variable
* @param: envValue - the value to be set for the variable
* @return: none
*/
void setEnvVar(char* envName, char* envValue){
if( envName == NULL || envValue == NULL ){
printf("Error: senv requires arguments [environment name] [value]\n");
} else {
if( setenv(envName, envValue, 1) == ENV_ERR ){
printf("Error: Could not set %s\n", envName);
}
}
return;
}
/* Deletes the enviroment variable
* @param: envName - the name of the environment variable
* @return: none
*/
void unsetEnvVar(char* envName){
if( envName == NULL ){
printf("Error: usenv requires arguments [environment name]\n");
} else {
if( unsetenv(envName) == ENV_ERR ){
printf("Error: Could not unset %s\n", envName);
}
}
return;
}
/* Prints the value of the environment variable
* @param: envName - the name of the environment variable
* @return: none
*/
void getEnvVar(char* envName){
if( envName == NULL ){
printf("Error: genv requires arguments [environment name]\n");
} else{
printf("%s = %s\n", envName, getenv(envName));
}
return;
}
/* void changeDirectory(char* pPath)
* @desc: Changes the cwd to the speciied path.
* @input: pPath, specified char* to path.
* @output: none
*/
void changeDirectory(char* pPath)
{
int ret;
char* absPath = tildaToHome(pPath);
ret = chdir(absPath);
if(ret == NEG_ONE) {
printf("I'm sorry Dave, I cannot change to that directory.\n");
}
return;
}