-
Notifications
You must be signed in to change notification settings - Fork 0
/
msh.c
308 lines (260 loc) · 7.35 KB
/
msh.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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <pwd.h>
#include <ctype.h>
#include "tui.h"
#define TAB_SIZE 4
#define READLINE_BUFFER_INIT_SIZE 128
#define TOKENS_BUFFER_INIT_SIZE 8
#define TOKENS_DELIM " |"
static bool run = true;
static size_t buffer_alloc;
static size_t tokens_alloc;
static size_t builtin_index;
static const char *msh_builtins_name[] = {
"cd"
};
#define TOTAL_BUILTINS sizeof(msh_builtins_name)/sizeof(msh_builtins_name[0])
void buffer_print_slice(const char *buf, size_t from, size_t to) {
for(size_t i = from; i < to; i++)
printf("%c", buf[i]);
}
void buffer_clear(char *buf) {
size_t len = strlen(buf);
for (size_t i = 0; i < len; i++)
buf[i] = '\0';
}
char *buffer_read_line(char *s) {
int c;
char *aux;
size_t used_size;
used_size = 0;
while (1) {
c = getchar();
if((c == '\n') || (c == EOF))
break;
if(used_size == (buffer_alloc - 1)) {
if(!(aux = realloc(s, buffer_alloc += buffer_alloc))) {
perror("msh");
free(s);
return NULL;
}
s = aux;
}
s[used_size++] = c;
}
s[used_size] = '\0';
if (c == EOF) run = false;
return s;
}
char **buffer_split(char *b, char **t) {
char *p, **aux;
size_t tokens_count;
tokens_count = 0;
for(p = b; (p = strtok(p, TOKENS_DELIM)); p = NULL) {
if((tokens_count + 1) == tokens_alloc) {
if(!(aux = realloc(t, sizeof(char*) * (tokens_alloc *= 2)))) {
perror("msh");
for(size_t i = 0; i < tokens_count; i++)
free(t[i]);
free(t);
return NULL;
}
t = aux;
}
t[tokens_count++] = p;
}
t[tokens_count] = NULL;
return t;
}
char *editor_read_line(char *s) {
int c;
char *aux;
size_t line_len, cursor_pos;
line_len = cursor_pos = 0;
while(1) {
c = getchar();
/* Ctrl-D */
if((c == 4) || (c == EOF)) {
run = false;
buffer_clear(s);
break;
}
if(c == 3) {
buffer_clear(s);
break;
}
if((c == '\r') || (c == '\n'))
break;
/* ESC */
if(c == 27) {
getchar(); /* skip '[' */
switch (getchar()) {
case 'A': /* up */
break;
case 'B': /* down */
break;
case 'D': /* left */
if(cursor_pos > 0) {
printf("\033[1D");
cursor_pos--;
}
break;
case 'C': /* right */
if(cursor_pos < line_len) {
printf("\033[1C");
cursor_pos++;
}
break;
default:
break;
}
continue;
}
/* Backspace */
if(c == 0x7f) {
if (line_len > 0) {
line_len -= 1;
cursor_pos -= 1;
printf("\b \b");
if (cursor_pos < line_len) {
memmove(s + cursor_pos, s + cursor_pos + 1, line_len - cursor_pos + 1);
buffer_print_slice(s, cursor_pos, line_len);
putchar(' ');
printf("\033[%ldD", line_len - cursor_pos + 1);
}
}
continue;
}
/* Delete */
if(c == 0x7e) {
if ((line_len > 0) && (cursor_pos < line_len)) {
line_len -= 1;
memmove(s + cursor_pos, s + cursor_pos + 1, line_len - cursor_pos);
buffer_print_slice(s, cursor_pos, line_len);
putchar(' ');
printf("\033[%ldD", line_len - cursor_pos + 1);
}
continue;
}
if(c == '\t') {
for(size_t i = 0; i < TAB_SIZE; i++)
putchar(' ');
line_len += TAB_SIZE;
continue;
}
if(line_len == (buffer_alloc - 1)) {
if(!(aux = realloc(s, buffer_alloc += buffer_alloc))) {
perror("msh");
free(s);
return NULL;
}
s = aux;
}
/* only print printable caracters */
if (isprint(c)) {
if (cursor_pos < line_len) {
memmove(s + cursor_pos + 1, s + cursor_pos, line_len - cursor_pos);
putchar(c);
s[cursor_pos++] = c;
line_len += 1;
buffer_print_slice(s, cursor_pos, line_len);
/* move left the amount buffer_print_slice moved the cursor */
printf("\033[%ldD", line_len - cursor_pos);
} else {
putchar(c);
s[line_len++] = c;
cursor_pos++;
}
}
}
printf("\r\n");
s[line_len] = '\0';
return s;
}
bool is_builtin(const char *name) {
size_t i;
for(i = builtin_index = 0; i < TOTAL_BUILTINS; i++)
if(!strcmp(name, msh_builtins_name[i]))
return true;
return false;
}
void msh_cd(const char **path) {
if(path[1] == NULL) {
/* go ot user home */
struct passwd *pw = getpwuid(getuid());
const char *user_home_dir = pw->pw_dir;
if(chdir(user_home_dir) < 0) {
perror("cd");
}
}
else {
if(chdir(path[1]) < 0)
fprintf(stderr,
"%s: no such file or directory: %s\n",
"cd", path[0]);
}
}
static void (*msh_builtin_handler[])(const char **argv) = {
&msh_cd,
};
void msh_execute_builtin(char **argv) {
msh_builtin_handler[builtin_index]((const char **)argv);
}
void msh_execute(char **argv) {
int status;
pid_t pid;
pid = fork();
if (pid == 0) {
/* child process */
if(execvp(argv[0], argv) < 0)
fprintf(stderr, "%s: command not found\n", argv[0]);
exit(EXIT_FAILURE);
}
else if (pid < 0) {
perror("msh: could not fork");
}
else {
do {
waitpid(pid, &status, WUNTRACED);
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
}
}
void msh_loop(void) {
char *buffer, **tokens;
buffer_alloc = READLINE_BUFFER_INIT_SIZE;
if(!(buffer = calloc(buffer_alloc, sizeof(char))))
exit(EXIT_FAILURE);
tokens_alloc = TOKENS_BUFFER_INIT_SIZE;
if(!(tokens = malloc(sizeof(char*) * tokens_alloc))) {
free(buffer);
exit(EXIT_FAILURE);
}
while (run) {
tui_set_input_mode();
/* make cusor blinking vertical bar */
printf("\r\033[5 q$ ");
buffer = editor_read_line(buffer);
tui_reset_input_mode();
tokens = buffer_split(buffer, tokens);
/* skip execute if buffer is empty */
if(strlen(buffer) > 0) {
if(is_builtin(tokens[0]))
msh_execute_builtin(tokens);
else
msh_execute(tokens);
}
buffer_clear(buffer);
}
free(tokens);
free(buffer);
}
int main (void) {
msh_loop();
return 0;
}