forked from lifeissweetgood/_dash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdash.c
362 lines (302 loc) · 9.41 KB
/
dash.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
#define _GNU_SOURCE /* for asprintf */
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
#include <errno.h>
#include <signal.h>
#define MAX_CMD_LEN 1000
#define MAX_CMD_ARGS 100
#define ERROR(x) if(x) { printf("%s: %d: ERROR %s\n", \
__func__, __LINE__, x); exit(1); }
#define ERROR_CLEANUP(x) if(x) { printf("%s: %d: ERROR %s\n", \
__func__, __LINE__, x); \
rc = -__LINE__; goto cleanup; }
#define ASSERT(x) if(x) { printf("ASSERT: %d: Assert failed!\n", __LINE__); \
rc = -__LINE__; goto cleanup; }
static bool got_sigint = false;
/**
* Grabs commands and args and formats them to pass to child process
* No escape characters - " " is the only delimiter.
*/
static void parseUserInput(const char *userInputStr, char **storeArgs)
{
unsigned int i;
unsigned int arg = 0;
unsigned int string_start = 0;
unsigned int cur_string_len = 0;
unsigned int last_was_space = 1;
for (i = 0; i <= strlen(userInputStr); i++) {
if ((userInputStr[i] == ' ') || (userInputStr[i] == '\0')) {
if (!last_was_space) {
storeArgs[arg] = strndup(&userInputStr[string_start], cur_string_len + 1);
if (storeArgs[arg] == NULL)
ERROR("Failed malloc!\n");
storeArgs[arg][cur_string_len] = '\0';
printf("storeArgs[%d] = %s\n", arg, storeArgs[arg]);
arg += 1;
cur_string_len = 0;
string_start = i + 1;
} else { /* multiple spaces in a row */
string_start += 1;
}
last_was_space = 1;
} else {
last_was_space = 0;
cur_string_len += 1;
}
}
}
/**
* Removes new line char from user input string.
*
*/
void removeNewLine(char *oldInputStr, char *newInputStr)
{
int i = 0;
for(; oldInputStr[i] != '\n'; i++)
newInputStr[i] = oldInputStr[i];
newInputStr[i] = '\0'; // Terminate string properly
}
/*
* Displays error message for failed exec call.
*
*/
int printErrorMessage(char** args, int code)
{
switch(code) {
case EACCES:
printf("Permission DENIED: %s\n", args[0]);
break;
case ENOENT:
printf("Command not found: %s\n", args[0]);
break;
default:
printf("Something bad happened. Error code %d\n", code);
printf("You tried to run: %s\n", args[0]);
}
}
/**
* Termination handler for signal.
*
*/
static void termination_handler(int signum)
{
got_sigint = true;
}
/**
* Registers signal handler.
*
*/
static void registerSignalHandler() {
struct sigaction new_action;
new_action.sa_handler = termination_handler;
// new_action.sa_handler = SIG_IGN;
sigemptyset (&new_action.sa_mask);
new_action.sa_flags = 0;
sigaction(SIGINT, &new_action, NULL);
}
/**
* Wrapper for chdir function to change directories
*
* NOTE: Changes are not persistent after _dash exits
*/
int changeDir(char *cdCommand)
{
int i = 0, rc = 0;
char *cdPathToMoveTo = NULL;
char *errMsg = NULL;
if( (cdPathToMoveTo = malloc(sizeof(cdCommand))) == NULL)
ERROR("Failed malloc!\n");
// Essentially, we're copying the original command but removing the 'cd'
// part to grab the path. We're expecting input in the format of:
// cd /path/to/move/to/
//
// We're trusting the user input an awwwwful lot here (see TODO
// below)
for(; cdCommand[i] != '\0'; i++)
cdPathToMoveTo[i] = cdCommand[i+3];
cdPathToMoveTo[i+1] = '\0'; // Terminate string properly
// Call built-in function to move dirs
if(chdir(cdPathToMoveTo) < 0)
{
asprintf(&errMsg, "Can't move to '%s'!", cdPathToMoveTo);
ERROR_CLEANUP(errMsg);
}
cleanup:
free(cdPathToMoveTo);
free(errMsg);
return rc;
}
/**
* Executes the fork
*/
int execute_fork(char **cmdargv, int pipeExists)
{
char buf;
int pipefd[2];
pid_t pid;
int j;
int i = 0, cursor = 0, rc = 0, status = 0;
char **subCmdArgv = NULL;
while(cmdargv[cursor] != NULL)
{
for(j=0; cmdargv[j] != NULL; j++)
printf("CHECKINGCHECKING %d: cmdargv[%d]: %s\n", __LINE__, j, cmdargv[j]);
if(pipeExists)
{
if(pipe(pipefd) < 0)
ERROR("Can't create pipe!");
}
if((subCmdArgv = (char**) malloc(sizeof(char*) * j)) == NULL)
ERROR("Failed malloc!\n");
for(j=0; cmdargv[j] != NULL; j++)
printf("CHECKINGCHECKING %d: cmdargv[%d]: %s\n", __LINE__, j, cmdargv[j]);
// Parse out each command separated by a | (pipe)
for(i = 0; cmdargv[i+cursor] != NULL; i++)
{
if(strcmp("|", cmdargv[i+cursor]) == 0)
break;
subCmdArgv[i] = strdup(cmdargv[i+cursor]);
}
cursor += (i+1); // holds the next position in cmdargv after the |
pid = fork();
if(pid == 0) // Child Process
{
if(pipeExists)
{
dup2(STDOUT_FILENO, pipefd[1]);
if(close(pipefd[0]) < 0)
ERROR("Child Proc: Can't close read end of pipe!");
}
rc = execvp(subCmdArgv[0], subCmdArgv);
if(rc < 0)
printErrorMessage(cmdargv, errno);
if(pipeExists)
{
if(close(pipefd[1]) < 0)
ERROR("Child Proc: Can't close write end of pipe!");
}
// Child process needs to exit out or else program never exists.
exit(1);
}
else if( pid < 0) // Fork failed. Boo.
{
ERROR_CLEANUP("Fork failed\n");
}
else // Parent Process
{
if(pipeExists)
{
if(close(pipefd[1]) < 0)
ERROR_CLEANUP("Parent Proc: Can't close write end of pipe!");
while(read(pipefd[0], &buf, 1) == 1)
printf("%c", buf);
if(close(pipefd[0]) < 0)
ERROR_CLEANUP("Parent Proc: Can't close read end of pipe!");
}
}
// Wait on child process
waitpid(pid, &status, 0);
// Since we did a strdup above for each command arg, we have to free it
// before getting the next set of commands.
for(j=0; subCmdArgv[j] != NULL; j++)
{
printf("subcmdargv[%d]: %s\n", j, subCmdArgv[j]);
free(subCmdArgv[j]);
subCmdArgv[j] = NULL;
}
} // end while loop
cleanup:
free(subCmdArgv);
subCmdArgv = NULL;
return rc;
}
/**
* Main function
*
*/
int main(int argc, char* argv[])
{
int i, j, cmdargv_len;
int rc = 0;
int pipeExists = 0;
char *formattedInput = NULL;
char *userInput = NULL;
char **cmdargv = NULL ;
char *cdCmd = NULL;
char *pipeCmd = NULL;
if((userInput = malloc(sizeof(char)*MAX_CMD_LEN)) == NULL)
ERROR("Failed malloc\n");
cmdargv_len = sizeof(char*) * MAX_CMD_ARGS;
if((cmdargv = (char**) malloc(cmdargv_len)) == NULL) {
ERROR("Failed malloc\n");
} else {
memset(cmdargv, '\0', sizeof(char*) * MAX_CMD_ARGS);
}
registerSignalHandler();
while(1)
{
printf("_dash > ");
got_sigint = false;
if (fgets(userInput, MAX_CMD_LEN, stdin) == NULL) {
if (got_sigint) {
printf("\n");
continue;
} else {
// Ctrl+D
return 0;
}
}
// TODO: Sanitize user input! We're currently hoping the user is a
// benelovent, sweet human being. HA!
if( (formattedInput = malloc(sizeof(userInput))) == NULL )
ERROR("Failed malloc\n");
removeNewLine(userInput, formattedInput);
// See if user wants out.
if((strcmp("quit", formattedInput) == 0) ||
(strcmp("exit", formattedInput) == 0) ||
(strcmp("q", formattedInput) == 0))
{
printf("Quitting!\n");
goto cleanup;
}
// Check to see if user wants to change working directories
if( (cdCmd = strstr(formattedInput, "cd ")) != NULL )
{
if(changeDir(cdCmd) != 0)
ERROR("cd failed.");
free(cdCmd);
// No need to fork/exec, can just move on.
continue;
}
// Check to see if user wants to pipe commands
if( (pipeCmd = strstr(formattedInput, "|")) != NULL )
{
pipeExists = 1;
// Don't need to free pipeCmd bc freeing formattedInput will take
// care of that for us.
//free(pipeCmd);
}
parseUserInput(formattedInput, cmdargv);
for(j=0; cmdargv[j] != NULL; j++)
printf("%d: cmdargv[%d]: %s\n", __LINE__, j, cmdargv[j]);
rc = execute_fork(cmdargv, pipeExists);
ASSERT( rc != 0 );
pipeExists = 0;
}
/* Cleanup! */
cleanup:
free(formattedInput);
free(userInput);
if (cmdargv) {
for (i = 0; i < MAX_CMD_ARGS; i++) {
free(cmdargv[i]); /* free(NULL) is ok with glibc */
}
}
free(cmdargv);
printf("All finished.\n");
return 0;
}