-
Notifications
You must be signed in to change notification settings - Fork 0
/
shellgibi.c
619 lines (539 loc) · 18 KB
/
shellgibi.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
#include <unistd.h>
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <termios.h> //termios, TCSANOW, ECHO, ICANON
#include <stdbool.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <pwd.h>
#include <ifaddrs.h>
#include <readline/readline.h>
#include <readline/history.h>
char **all_commands;
const char *sysname = "shellgibi";
enum return_codes {
SUCCESS = 0,
EXIT = 1,
UNKNOWN = 2,
};
struct command_t {
char *name;
bool background;
bool auto_complete;
int arg_count;
char **args;
char *redirects[3]; // in/out redirection
struct command_t *next; // for piping
};
/**
* Prints a command struct
* @param struct command_t *
*/
void print_command(struct command_t *command) {
int i = 0;
printf("Command: <%s>\n", command->name);
printf("\tIs Background: %s\n", command->background ? "yes" : "no");
printf("\tNeeds Auto-complete: %s\n", command->auto_complete ? "yes" : "no");
printf("\tRedirects:\n");
for (i = 0; i < 3; i++)
printf("\t\t%d: %s\n", i, command->redirects[i] ? command->redirects[i] : "N/A");
printf("\tArguments (%d):\n", command->arg_count);
for (i = 0; i < command->arg_count; ++i)
printf("\t\tArg %d: %s\n", i, command->args[i]);
if (command->next) {
printf("\tPiped to:\n");
print_command(command->next);
}
}
/**
* Release allocated memory of a command
* @param command [description]
* @return [description]
*/
int free_command(struct command_t *command) {
if (command->arg_count) {
for (int i = 0; i < command->arg_count; ++i)
free(command->args[i]);
free(command->args);
}
for (int i = 0; i < 3; ++i)
if (command->redirects[i])
free(command->redirects[i]);
if (command->next) {
free_command(command->next);
command->next = NULL;
}
free(command->name);
free(command);
return 0;
}
/**
* Show the command prompt
* @return [description]
*/
int show_prompt() {
char cwd[1024], hostname[1024];
gethostname(hostname, sizeof(hostname));
getcwd(cwd, sizeof(cwd));
printf("%s@%s:%s %s$ ", getenv("USER"), hostname, cwd, sysname);
return 0;
}
/**
* Parse a command string into a command struct
* @param buf [description]
* @param command [description]
* @return 0
*/
int parse_command(char *buf, struct command_t *command) {
const char *splitters = " \t"; // split at whitespace
int index, len;
len = strlen(buf);
while (len > 0 && strchr(splitters, buf[0]) != NULL) // trim left whitespace
{
buf++;
len--;
}
while (len > 0 && strchr(splitters, buf[len - 1]) != NULL)
buf[--len] = 0; // trim right whitespace
if (len > 0 && buf[len - 1] == '?') // auto-complete
command->auto_complete = true;
if (len > 0 && buf[len - 1] == '&') // background
command->background = true;
char *pch = strtok(buf, splitters);
command->name = (char *) malloc(strlen(pch) + 1);
if (pch == NULL)
command->name[0] = 0;
else
strcpy(command->name, pch);
command->args = (char **) malloc(sizeof(char *));
int redirect_index;
int arg_index = 0;
char temp_buf[1024], *arg;
while (1) {
// tokenize input on splitters
pch = strtok(NULL, splitters);
if (!pch)
break;
arg = temp_buf;
strcpy(arg, pch);
len = strlen(arg);
if (len == 0)
continue; // empty arg, go for next
while (len > 0 && strchr(splitters, arg[0]) != NULL) // trim left whitespace
{
arg++;
len--;
}
while (len > 0 && strchr(splitters, arg[len - 1]) != NULL)
arg[--len] = 0; // trim right whitespace
if (len == 0)
continue; // empty arg, go for next
// piping to another command
if (strcmp(arg, "|") == 0) {
struct command_t *c = malloc(sizeof(struct command_t));
int l = strlen(pch);
pch[l] = splitters[0]; // restore strtok termination
index = 1;
while (pch[index] == ' ' || pch[index] == '\t')
index++; // skip whitespaces
parse_command(pch + index, c);
pch[l] = 0; // put back strtok termination
command->next = c;
continue;
}
// background process
if (strcmp(arg, "&") == 0)
continue; // handled before
// handle input redirection
redirect_index = -1;
if (arg[0] == '<')
redirect_index = 0;
if (arg[0] == '>') {
if (len > 1 && arg[1] == '>') {
redirect_index = 2;
arg++;
len--;
} else
redirect_index = 1;
}
if (redirect_index != -1) {
command->redirects[redirect_index] = malloc(len);
strcpy(command->redirects[redirect_index], arg + 1);
continue;
}
// normal arguments
if (len > 2 &&
((arg[0] == '"' && arg[len - 1] == '"') || (arg[0] == '\'' && arg[len - 1] == '\''))) // quote wrapped arg
{
arg[--len] = 0;
arg++;
}
command->args = (char **) realloc(command->args, sizeof(char *) * (arg_index + 1));
command->args[arg_index] = (char *) malloc(len + 1);
strcpy(command->args[arg_index++], arg);
}
command->arg_count = arg_index;
return 0;
}
void prompt_backspace() {
putchar(8); // go back 1
putchar(' '); // write empty over
putchar(8); // go back 1 again
}
/**
* Prompt a command from the user
* @param buf [description]
* @param buf_size [description]
* @return [description]
*/
int prompt(struct command_t *command) {
int index = 0;
char c;
char buf[4096];
static char oldbuf[4096];
// tcgetattr gets the parameters of the current terminal
// STDIN_FILENO will tell tcgetattr that it should write the settings
// of stdin to oldt
static struct termios backup_termios, new_termios;
tcgetattr(STDIN_FILENO, &backup_termios);
new_termios = backup_termios;
// ICANON normally takes care that one line at a time will be processed
// that means it will return if it sees a "\n" or an EOF or an EOL
new_termios.c_lflag &= ~(ICANON | ECHO); // Also disable automatic echo. We manually echo each char.
// Those new settings will be set to STDIN
// TCSANOW tells tcsetattr to change attributes immediately.
tcsetattr(STDIN_FILENO, TCSANOW, &new_termios);
//FIXME: backspace is applied before printing chars
show_prompt();
int multicode_state = 0;
buf[0] = 0;
while (1) {
c = getchar();
// printf("Keycode: %u\n", c); // DEBUG: uncomment for debugging
if (c == 9) // handle tab
{
buf[index++] = '?'; // autocomplete
break;
}
if (c == 127) // handle backspace
{
if (index > 0) {
prompt_backspace();
index--;
}
continue;
}
if (c == 27 && multicode_state == 0) // handle multi-code keys
{
multicode_state = 1;
continue;
}
if (c == 91 && multicode_state == 1) {
multicode_state = 2;
continue;
}
if (c == 65 && multicode_state == 2) // up arrow
{
int i;
while (index > 0) {
prompt_backspace();
index--;
}
for (i = 0; oldbuf[i]; ++i) {
putchar(oldbuf[i]);
buf[i] = oldbuf[i];
}
index = i;
continue;
} else
multicode_state = 0;
putchar(c); // echo the character
buf[index++] = c;
if (index >= sizeof(buf) - 1)
break;
if (c == '\n') // enter key
break;
if (c == 4) // Ctrl+D
return EXIT;
}
if (index > 0 && buf[index - 1] == '\n') // trim newline from the end
index--;
buf[index++] = 0; // null terminate string
strcpy(oldbuf, buf);
parse_command(buf, command);
//print_command(command); // DEBUG: uncomment for debugging
// restore the old settings
tcsetattr(STDIN_FILENO, TCSANOW, &backup_termios);
return SUCCESS;
}
int process_command(struct command_t *command);
char **shellgibi_autocomplete(const char *text, int start, int end);
int load_all_commands();
int main() {
rl_attempted_completion_function = shellgibi_autocomplete;
rl_completer_quote_characters = "'\"";
rl_completer_word_break_characters = " ";
rl_bind_key('\t', rl_menu_complete);
while (1) {
struct command_t *command = malloc(sizeof(struct command_t));
memset(command, 0, sizeof(struct command_t)); // set all bytes to 0
int code;
code = load_all_commands();
if (code == UNKNOWN)
break;
code = prompt(command);
if (code == EXIT)
break;
code = process_command(command);
if (code == EXIT)
break;
free_command(command);
}
printf("\n");
return 0;
}
char *shellgibi_autocomplete_generator(const char *str, int start) {
static int index, length;
char *cmd_name;
if (!start) {
index = 0;
length = strlen(str);
}
while ((cmd_name = all_commands[index++])) {
if (rl_completion_quote_character) {
cmd_name = strdup(cmd_name);
if (strncmp(cmd_name, str, length) == 0) {
return cmd_name;
} else {
free(cmd_name);
}
}
}
return NULL;
}
char **shellgibi_autocomplete(const char *text, int start, int end) {
rl_attempted_completion_over = 1;
return rl_completion_matches(text, shellgibi_autocomplete_generator);
}
int load_all_commands() {
char *path_var = strdup(getenv("PATH"));
char **paths = malloc(32 * sizeof(char *));
all_commands = malloc(65536 * sizeof(char *));
//Getting all paths
int i = 0;
char *token, *tmp;
token = strtok_r(path_var, ":;", &tmp);
while (1) {
token = strtok_r(NULL, ":;", &tmp);
if (token == NULL)break;
paths[i] = (char *) malloc(1024 * sizeof(char));
strcpy(paths[i], token);
i++;
}
i--;
DIR *directory;
struct dirent *entry;
char *file_path = malloc(2048 * sizeof(char));
int counter = 0;
for (i; i > -1; i--) {
if ((directory = opendir(paths[i])) != NULL) {
while ((entry = readdir(directory)) != NULL) {
strcpy(file_path, paths[i]);
strcat(file_path, "/");
strcat(file_path, entry->d_name);
if (access(file_path, X_OK) != 0)
continue;
all_commands[counter] = malloc(1024 * sizeof(char));
strcpy(all_commands[counter], entry->d_name);
counter++;
}
closedir(directory);
} else {
return UNKNOWN;
}
}
//Full file completion for current working directory
char cwd[1024];
getcwd(cwd, sizeof(cwd));
if ((directory = opendir(cwd)) != NULL) {
while ((entry = readdir(directory)) != NULL) {
all_commands[counter] = malloc(1024 * sizeof(char));
strcpy(all_commands[counter], entry->d_name);
counter++;
}
closedir(directory);
} else {
return UNKNOWN;
}
return SUCCESS;
}
int process_command(struct command_t *command) {
int r;
if (strcmp(command->name, "") == 0)
return SUCCESS;
if (strcmp(command->name, "exit") == 0)
return EXIT;
if (strcmp(command->name, "cd") == 0) {
if (command->arg_count > 0) {
r = chdir(command->args[0]);
if (r == -1)
printf("-%s: %s: %s\n", sysname, command->name, strerror(errno));
return SUCCESS;
}
}
int num_pipes = 1;
struct command_t *tmp_cmd = malloc(sizeof(struct command_t));
memcpy(tmp_cmd, command, sizeof(struct command_t));
while (tmp_cmd->next) {
num_pipes++;
tmp_cmd = tmp_cmd->next;
}
int tmp_in = dup(0);
int tmp_out = dup(1);
int input = dup(0);
if (command->redirects[0])
input = open(command->redirects[0], O_RDONLY);
else
input = tmp_in;
int output = dup(1);
int status;
pid_t pid;
for (int i = 0; i < num_pipes; i++) {
dup2(input, 0);
close(input);
if (i == num_pipes - 1) {
if (command->redirects[1])
output = open(command->redirects[1], O_WRONLY | O_TRUNC | O_CREAT,
S_IRUSR | S_IRGRP | S_IWGRP | S_IWUSR);
else if (command->redirects[2])
output = open(command->redirects[2], O_WRONLY | O_APPEND | O_CREAT,
S_IRUSR | S_IRGRP | S_IWGRP | S_IWUSR);
else
output = tmp_out;
} else {
int fd[2];
pipe(fd);
output = fd[1];
input = fd[0];
}
dup2(output, 1);
close(output);
pid = fork();
if (pid == 0) // child
{
char path[500];
/// This shows how to do exec with environ (but is not available on MacOs)
// extern char** environ; // environment variables
// execvpe(command->name, command->args, environ); // exec+args+path+environ
/// This shows how to do exec with auto-path resolve
// add a NULL argument to the end of args, and the name to the beginning
// as required by exec
if (strcmp(command->name, "alarm") == 0) {
char cwd[1024];
getcwd(cwd, sizeof(cwd));
char *time = command->args[0];
char *music_file_name = command->args[1];
char time_array[2][5];
char *parsed_time;
int i = 0;
parsed_time = strtok(time, ".");
while (parsed_time != NULL) {
strcpy(time_array[i], parsed_time);
parsed_time = strtok(NULL, ".");
i++;
}
FILE *crontab_file;
crontab_file = fopen("crontab_file", "w");
fprintf(crontab_file, "SHELL=/bin/bash\n");
fprintf(crontab_file, "PATH=%s\n", getenv("PATH"));
fprintf(crontab_file, "%s %s * * * aplay %s\n",time_array[0], time_array[1], music_file_name);
fclose(crontab_file);
char *arguments[] = {"crontab", "crontab_file", NULL};
execvp("/usr/bin/crontab", arguments);
return SUCCESS;
} else if (strcmp(command->name, "myjobs") == 0) {
system("ps -u");
return SUCCESS;
} else if (strcmp(command->name, "pause") == 0) {
long p_pid = strtol(command->args[0], NULL, 10);
kill(p_pid, SIGSTOP);
return SUCCESS;
} else if (strcmp(command->name, "mybg") == 0) {
long p_pid = strtol(command->args[0], NULL, 10);
kill(p_pid, SIGCONT);
system("bg");
return SUCCESS;
} else if (strcmp(command->name, "myfg") == 0) {
long p_pid = strtol(command->args[0], NULL, 10);
kill(p_pid, SIGCONT);
system("fg");
return SUCCESS;
} else if (strcmp(command->name, "istforecast") == 0) {
char o[500];
strcpy(o, "./");
strcat(o, command->args[0]);
strcat(o, ".png");
char *args[] = {"wget", "wttr.in/Istanbul.png", NULL};
char *args2[] = {"mv", "./Istanbul.png", o, NULL};
execvp(args[0], args);
execvp(args2[0], args2);
return SUCCESS;
} else if (strcmp(command->name, "fib") == 0) {
int i, n, t1 = 0, t2 = 1, nextTerm;
n = atoi(command->args[0]);
for (i = 1; i <= n; ++i) {
printf("%d, ", t1);
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
}
printf("\n");
return SUCCESS;
}
// increase args size by 2
command->args = (char **) realloc(
command->args, sizeof(char *) * (command->arg_count += 2));
// shift everything forward by 1
for (int i = command->arg_count - 2; i > 0; --i)
command->args[i] = command->args[i - 1];
// set args[0] as a copy of name
command->args[0] = strdup(command->name);
// set args[arg_count-1] (last) to NULL
command->args[command->arg_count - 1] = NULL;
//execvp(command->name, command->args); // exec+args+path
/// TODO: do your own exec with path resolving using execv()
strcpy(path, "/usr/bin/");
strcat(path, command->name);
execv(path, command->args);
exit(0);
} else {
if (command->next) {
command = command->next;
continue;
}
do {
waitpid(pid, &status, WUNTRACED);
} while (!WIFEXITED(status) && !WIFSIGNALED(status)); // wait for child process to finish
dup2(input, 0);
dup2(output, 1);
close(input);
close(output);
return SUCCESS;
}
}
dup2(input, 0);
dup2(output, 1);
close(input);
close(output);
printf("-%s: %s: command not found\n", sysname, command->name);
return UNKNOWN;
}