-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.c
More file actions
197 lines (160 loc) · 4.19 KB
/
shell.c
File metadata and controls
197 lines (160 loc) · 4.19 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
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
#include <stdio.h>
#include <strings.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
void shl_exit(int);
#define PIPE "|"
#define PROMPT ">"
#define MAX_LINE 1000
#define MAX_OPT_COUNT 5
#define forever while(1)
#define FAILURE 1
#define QUIT 0
typedef struct cmd {
// The command name
char* command;
// A pointer to another command to which output is piped
// Or NULL if none exist
struct cmd* pipe;
// The options to the command, including the command itself
char* options[MAX_OPT_COUNT + 1];
} Cmd;
// Used whenever there is an error during malloc
#define MEMERROR(reason) { printf("Memory error: %s\n", reason); shl_exit(FAILURE); }
/*
* Given a command, expand its options in the right place in the command
*/
void shl_parse_options(Cmd* cmd, char* buffer){
char* args = NULL;
// Explode the program options and place them in the right place
// Place the program name as the first option
int i;
args = NULL;
cmd->command = strtok_r(buffer, " \t\n", &args);
cmd->options[0] = cmd->command;
for(i = 1; i < MAX_OPT_COUNT + 1; i++) {
cmd->options[i] = strtok_r(NULL, " ", &args);
if(cmd->options[i] == NULL) break;
}
}
/*
* Parses the given string into a linked list showing each command linked
* to the command that it pipes to
*/
Cmd* shl_parse_cmd(char* rawcmd) {
char* buffer;
Cmd* first_cmd = NULL;
Cmd* last_cmd = NULL;
char* pipes = NULL;
buffer = strtok_r(rawcmd, PIPE, &pipes);
// Parse everything in between PIPE's
if(buffer != NULL) {
do {
Cmd* new_cmd = malloc(sizeof(Cmd));
if(new_cmd == NULL) MEMERROR(rawcmd);
if(first_cmd == NULL) { // Create the first element of the list
first_cmd = new_cmd;
last_cmd = first_cmd;
} else { // Append a command to the command list
last_cmd->pipe = new_cmd;
last_cmd = last_cmd->pipe;
}
last_cmd->pipe = NULL;
shl_parse_options(last_cmd, buffer);
} while((buffer = (char*)strtok_r(NULL, PIPE, &pipes)) != NULL);
}
return first_cmd;
}
/*
* Frees the resources used by the command list
*/
void shl_free_cmd(Cmd* c) {
Cmd* nextCmd;
if(c != NULL) {
do {
nextCmd = c->pipe;
free(c);
c = nextCmd;
}while(c != NULL);
}
}
/*
* Runs a given list of commands while setting up the appropriate pipes
*/
int shl_exec(Cmd* cmd_line) {
pid_t pid;
int pipefds[2];
if (cmd_line != NULL) {
pipe(pipefds);
if(pid = fork()) { // First program
if(cmd_line->pipe != NULL) {
close(1);
dup(pipefds[1]);
close(pipefds[0]);
}
if(execvp(cmd_line->command, cmd_line->options)) {
perror(cmd_line->command);
exit(errno);
}
} else { // Whatever else exists after the first program that is piped to
if(cmd_line->pipe != NULL) {
close(0);
dup(pipefds[0]);
close(pipefds[1]);
shl_exec(cmd_line->pipe);
} else exit(0);
}
}
int status;
wait(&status);
exit(status);
}
/*
* exits the shell either by quitting or some kind of failure
*/
void shl_exit(int reason){
switch(reason){
case FAILURE:
break;
case QUIT:
printf("\nQuitting...\n");
break;
}
exit(reason);
}
/*
* Read a line from the prompt.
*
* If the line says "quit", exit the program successfully.
* If the program couldn't read the input for some reason, exit abnormally.
* Otherwise, run the command line.
*
* Return the line that was read from the prompt.
*/
char* shl_prompt(){
static char buffer[MAX_LINE];
int i = 0;
bzero(buffer, MAX_LINE);
printf("%s", PROMPT);
if(!fgets(buffer, MAX_LINE, stdin)) exit(1);
// Remove trailing new line
while(buffer[i++] != '\n');
buffer[i - 1] = '\0';
// Check for quit message
if(!strcmp("quit", buffer)) shl_exit(QUIT);
return buffer;
}
/*
* Continiously reads a line, runs the command in a separate process
* then frees the used resources
*/
int main(int argc, char** argv){
forever {
Cmd* cmd_line = shl_parse_cmd(shl_prompt());
if(fork()) wait(NULL);
else shl_exec(cmd_line);
shl_free_cmd(cmd_line);
}
}