-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.c
More file actions
executable file
·126 lines (122 loc) · 3.73 KB
/
shell.c
File metadata and controls
executable file
·126 lines (122 loc) · 3.73 KB
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
#include <stdio.h>
#include <string.h> // strerror, strchr, strlen, strtok_r
#include <stdlib.h> // malloc, free, exit
#include <errno.h> // errno
#include <unistd.h> // getcwd
#include "shell.h"
#include "debug.h"
/* Gets CWD and put into cwd if current size cwdBufLen is enough.
* Else allocates larger array storing CWD then returns char * to it.
* Users should not free the returned cwd. */
char *getCwdDynamicBuf() {
static int cwdBufLen = 128; // Initial buffer length will be double of this.
static char *cwd = NULL;
char *buf;
while (cwd == NULL || getcwd(cwd, cwdBufLen) == NULL) {
cwdBufLen *= 2;
DEBUG(printf("#DEBUG# cwdBufLen too short. Trying %d.\n", cwdBufLen););
if ((buf = malloc(sizeof(char) * cwdBufLen)) == NULL) {
fprintf(stderr, "Error %d: %s for cwd.\n", errno, strerror(errno));
exit(1);
}
free(cwd);
cwd = buf;
}
return cwd;
}
/* Displays cmd line prompt.
* Gets cmd line from stdin and store in cmdl.
* Max cmd line length is specified by INPUT_MAX in shell.h. */
void cmdLinePrompt(char *const cmdl) {
char buf[8], *newlinePtr;
printf("[3150 shell:%s]$ ", getCwdDynamicBuf());
if (fgets(cmdl, INPUT_MAX + 1, stdin) == NULL) { // EOF received.
DEBUG(printf("\n#DEBUG# EOF received."););
printf("\n");
exit(0);
}
if ((newlinePtr = strchr(cmdl, '\n')) != NULL)
*newlinePtr = '\0';
// When input len == INPUT_MAX, no '\n' to remove!
while (newlinePtr == NULL) {
fgets(buf, 8, stdin);
newlinePtr = strchr(buf, '\n');
} // Flush '\n' remained in stdin when input len == INPUT_MAX!
// Code reuse-able if limit on INPUT_MAX waived.
DEBUG(printf("#DEBUG# cmdl=%s=END\n", cmdl););
}
/* Tokenize the cmd line cmdl, delimiting by ' '.
* Returns an array of strings storing the tokens.
* The array has an extra NULL entry at the end
* and should be freed using freeCmdlArgv() when not needed.
* The number of tokens is stored in *cmdlArgc. */
char ***tokenizeCmdl(char *const cmdl, int **const cmdlArgc, int *const processCount) {
char ***cmdlArgv;
/** Count num of tokens separated by ' ' **/
int i = 0, j = 0, cmdlLen = strlen(cmdl);
int processNow = 0;
*processCount = 1;
for (i = 0; i < cmdlLen; i++) {
if (cmdl[i] == '|') {
(*processCount)++;
}
}
*cmdlArgc = calloc(*processCount, sizeof(int));
i = 0;
while (i < cmdlLen) {
while (cmdl[i] != '|' && i < cmdlLen) {
while (cmdl[i] == ' ' && i < cmdlLen)
i++;
if (cmdl[i] == '|')
break;
if (i < cmdlLen)
((*cmdlArgc)[processNow])++;
while (cmdl[i] != ' ' && cmdl[i] != '|' && i < cmdlLen)
i++;
}
processNow++;
i++;
}
/** Allocate arg table and register tokens into it **/
if ((cmdlArgv = malloc(sizeof(char **) * (*processCount))) == NULL) {
fprintf(stderr, "Error %d: %s for cmdlArgv.\n", errno, strerror(errno));
exit(1);
}
processNow = 0;
while (processNow < *processCount) {
j = 0;
cmdlArgv[processNow] = malloc(sizeof(char *) * ((*cmdlArgc)[processNow] + 1));
if (cmdlArgv[processNow] == NULL) {
// allocate error
exit(1);
}
while (j < (*cmdlArgc)[processNow]) {
if (processNow == 0 && j == 0) {
cmdlArgv[processNow][j] = strtok(cmdl, " |");
} else {
cmdlArgv[processNow][j] = strtok(NULL, " |");
}
j++;
}
cmdlArgv[processNow][j] = NULL;
processNow++;
}
DEBUG(
printf("#DEBUG# processCount=%d\n", *processCount);
for (i = 0; i < *processCount; i++) {
for (j = 0; cmdlArgv[i][j] != NULL; j++)
printf("#DEBUG# cmdlArgv[%d]=%s=END\n", j, cmdlArgv[i][j]);
printf("#DEBUG# cmdlArgv[%d]=%s=END\n", j, cmdlArgv[i][j]);
}
);
return cmdlArgv;
}
/* Frees and set to NULL the cmdlArgv returned by tokenizeCmdl(). */
void freeCmdlArgv(char ***cmdlArgv, int processCount) {
int i = 0;
for(;i<processCount;i++) {
free(cmdlArgv[i]);
}
free(cmdlArgv);
cmdlArgv = NULL;
}