#include #include #include #include #include #include #include #include "array.h" #include "process.h" #include "prompt_utils.h" #include "builtins.h" int main(void) { setvbuf(stderr, NULL, _IONBF, 0); setvbuf(stdout, NULL, _IONBF, 0); printf("Welcome! Available built-ins are:\n" "cd: `cd ` - if no path is given, return to the current dir\n" "wait: `wait pid1 ... pidN` - wait on the processes and report their exit conditions\n" "fg: `fg pid` - pulls a process from the background back in the foreground (#TODO)\n" "\n" "You can put processes in the background using the unary `&` for new processes, and using CTRL-Z (#TODO) for already running ones\n" "With `|` you can pipe the output from one process to the input of another\n" ); char const *const original_wd = get_current_dir_name(); char const *prompt = relative_path(original_wd, original_wd); bool done = false; while (!done) { char *line = NULL; size_t cap = 0; __ssize_t length = 0; printf("%s > ", prompt); if ((length = getline(&line, &cap, stdin)) < 0) { fprintf(stderr, "Failed to read from STDIN"); fflush(stderr); exit(-1); } if (strspn(line, " \n\t") == strlen(line)) { // skip empty lines - empty being just spaces or tabs continue; } line[length - 1] = '\0'; // cut the line feed process *processes = NULL; parse_line(line, &processes); if (strcmp(processes[0].argv[0], "cd") == 0) { if (arrayLen(processes) != 1) { perror("Can't chain cd with other processes"); } int ret; switch(arrayLen(processes[0].argv)) { case 3: ret = chdir(processes[0].argv[1]); break; case 2: ret = chdir(original_wd); break; default: fprintf(stderr, "usage: cd \n"); fflush(stderr); ret = -1; } if (ret) printf("[%i] ", ret); free((void *)prompt); char const *current_wd = get_current_dir_name(); prompt = relative_path(original_wd, current_wd); free((void *)current_wd); } else if (strcmp(processes[0].argv[0], "exit") == 0) { done = true; } else if (strcmp(processes[0].argv[0], "wait") == 0) { builtin_wait(processes[0]); } else { for (size_t i = 0; i < arrayLen(processes); ++i) { int ret = exec_command(processes[i]); if (ret) printf("[%i] ", ret); } } free((void *)line); line = NULL; cap = 0; free_processes(&processes); } free((void *)original_wd); free((void *)prompt); }