-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnush.c
More file actions
189 lines (158 loc) · 5.15 KB
/
nush.c
File metadata and controls
189 lines (158 loc) · 5.15 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
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include "tokens.h"
#include "built_in.h"
#include "pipe.h"
#include "redirects.h"
#include "nush.h"
#include "other_operators.h"
// removes the first and last characters, used to remove parens.
char*
remove_first_and_last_chars(char* string)
{
int string_len = strlen(string);
int result_len = string_len - 2;
string++;
char* result = malloc(result_len + 1);
memcpy(result, string, result_len);
result[result_len] = 0;
return result;
}
int
execute(svec* tokens)
{
if(tokens->size == 0) {
return 1;
}
// ========== SEMICOLON ===========
int semicolon_index = get_index_semicolon(tokens);
if(semicolon_index != -1) {
return nush_semicolon(semicolon_index, tokens);
}
// ========= PARANTHESES ===========
// if there is one token, and it starts and ends with a parentheses, we need to tokenize the
// contents of the parentheses after removing the parens and then pass those tokens back into execute
char* first_token = tokens->data[0];
int starts_with_parens = strncmp(first_token, "(", 1) == 0;
if(tokens->size == 1 && starts_with_parens) {
char* inner_string = remove_first_and_last_chars(first_token);
svec* inner_tokens = tokenize_line(inner_string);
free(inner_string);
int return_code = execute(inner_tokens);
free(inner_tokens);
return return_code;
}
// ========== REDIRECT ===========
int redirect_operator_index = get_redirect_operator_index(tokens);
if(redirect_operator_index != -1) {
return exec_redirect_cmd(redirect_operator_index, tokens);
}
// ========== PIPE ===========
int pipe_index = get_pipe_index(tokens);
if(pipe_index != -1) {
return handle_and_exec_pipe(pipe_index, tokens);
}
// ========== OTHER OPERATORS ===========
int other_operator_index = get_other_operator_index(tokens);
if(other_operator_index != -1) {
return exec_other_operator_func(other_operator_index, tokens);
}
// ========== BUILT INS ===========
int built_in_cmd_code = get_built_in_cmd_code(tokens->data[0]);
if(built_in_cmd_code != -1) {
return exec_built_in_cmd(built_in_cmd_code, tokens);
}
// ========== PROGRAMS ===========
int cpid;
if((cpid = fork())) {
// parent
int status;
waitpid(cpid, &status, 0);
if (WIFEXITED(status)) {
if(status != 0) {
return status;
}
}
} else {
// child
char* cmd = tokens->data[0];
char** args = tokens->data;
execvp(cmd, args);
exit(errno);
}
return 0;
}
int
main(int argc, char* argv[])
{
if (argc == 1) {
char cmd[256];
char temp[256];
for(;;) {
printf("nush$ ");
fflush(stdout);
char* lc = fgets(temp, 256, stdin);
if(lc == 0) {
break;
}
strcpy(cmd, temp);
// handles the \ character, allowing the user to continue entering on the next line
while(temp[strlen(temp) - 2] == '\\') {
printf("> ");
// if the last two chars of command are \ and the null terminator,
// we need to remove that before we continue appending
if(cmd[strlen(cmd) - 2] == '\\') {
char new_cmd[256];
memcpy(new_cmd, cmd, strlen(cmd) - 2);
new_cmd[strlen(cmd) - 2] = 0;
strcpy(cmd, new_cmd);
}
lc = fgets(temp, 256, stdin);
if(lc == 0) {
break;
}
strncat(cmd, temp, 256 - strlen(cmd));
}
svec* tokens = tokenize_line(cmd);
execute(tokens);
free_svec(tokens);
}
} else {
FILE* file = fopen(argv[1], "r");
char cmd[256];
char temp[256];
for(;;) {
char* lc = fgets(temp, 256, file);
if(lc == 0) {
break;
}
strcpy(cmd, temp);
// handles the \ character, allowing the user to continue entering on the next line
while(temp[strlen(temp) - 2] == '\\') {
// if the last two chars of command are \ and the null terminator,
// we need to remove that before we continue appending
if(cmd[strlen(cmd) - 2] == '\\') {
char new_cmd[256];
memcpy(new_cmd, cmd, strlen(cmd) - 2);
new_cmd[strlen(cmd) - 2] = 0;
strcpy(cmd, new_cmd);
}
lc = fgets(temp, 256, file);
if(lc == 0) {
break;
}
strncat(cmd, temp, 256 - strlen(cmd));
}
svec* tokens = tokenize_line(cmd);
execute(tokens);
free_svec(tokens);
}
fclose(file);
}
return 0;
}