diff options
author | Stefan Zabka <zabkaste@hu-berlin.de> | 2020-05-21 23:37:38 +0200 |
---|---|---|
committer | Stefan Zabka <zabkaste@hu-berlin.de> | 2020-05-21 23:37:38 +0200 |
commit | f959165cdece3c1bfae8abce834a2bb78db96190 (patch) | |
tree | c8f060d804a0c8aed5cbba2bd37a5ecb114dc21b /02_exercise/shell.c | |
parent | 45b35365a5d0f895caeb6178f5f0a70cfe4464ee (diff) | |
download | betriebssysteme-f959165cdece3c1bfae8abce834a2bb78db96190.tar.gz betriebssysteme-f959165cdece3c1bfae8abce834a2bb78db96190.zip |
Process parse_line working
Diffstat (limited to '02_exercise/shell.c')
-rw-r--r-- | 02_exercise/shell.c | 105 |
1 files changed, 16 insertions, 89 deletions
diff --git a/02_exercise/shell.c b/02_exercise/shell.c index 933d162..9653189 100644 --- a/02_exercise/shell.c +++ b/02_exercise/shell.c @@ -7,12 +7,10 @@ #include <sys/wait.h> #include "array.h" +#include "process.h" #include "prompt_utils.h" -int parse_line(char const *line, char ***parts, size_t *part_count); -// returns the return code of the executed program -int exec_command(const char *path, char *const argv[], unsigned timeout); int main(void) { chdir("."); @@ -38,17 +36,20 @@ int main(void) { line[length - 1] = '\0'; // cut the line feed - char **arguments = NULL; - size_t argument_count; - parse_line(line, &arguments, &argument_count); + process * processes = NULL; + parse_line(line, &processes); - if (strcmp(arguments[0], "cd") == 0) { - if (arrayLen(arguments) != 3) { + if (strcmp(processes[0].argv[0], "cd") == 0) { + + if(arrayLen(processes) != 1) { + perror("Can't chain cd with other processes"); + } + if (arrayLen(processes[0].argv) != 3) { fprintf(stderr, "usage: cd <path>"); goto clean; } - int ret = chdir(arguments[1]); + int ret = chdir(processes[0].argv[1]); if (ret) printf("[%i] ", ret); @@ -56,96 +57,22 @@ int main(void) { char const *current_wd = get_current_dir_name(); prompt = relative_path(original_wd, current_wd); free((void *)current_wd); - } else if (strcmp(arguments[0], "exit") == 0) { + } else if (strcmp(processes[0].argv[1], "exit") == 0) { done = true; } else { - int ret = exec_command(arguments[0], arguments, 0); + int ret; + for (size_t i = 0; i < arrayLen(processes); ++i) { + ret = exec_command(processes[i], 0); + } if (ret) printf("[%i] ", ret); } clean: free((void *)line); - while (arrayLen(arguments) > 0) { - char *tmp = arrayPop(arguments); - if (tmp) - arrayRelease(tmp); - } - arrayRelease(arguments); + free_processes(&processes); } free((void *)original_wd); free((void *)prompt); } - -int parse_line(char const *line, char ***parts, size_t *part_count) { - char *part; - char **local_parts; - - if (arrayInit(part) != 0 || arrayInit(local_parts) != 0) { - fprintf(stderr, "Failed to prepare new part / parts array whilst parsing line"); - return -1; - } - - for (size_t i = 0; i < strlen(line) + 1; ++i) { - char c = line[i]; - if (c == ' ' || c == '\0') { - if (arrayLen(part) == 0) - continue; - arrayPush(part) = '\0'; - arrayPush(local_parts) = part; - if (c == '\0') { - arrayPush(local_parts) = NULL; - break; - } else { - arrayInit(part); - } - } else { - arrayPush(part) = c; - } - } - - *part_count = arrayLen(local_parts); - *parts = local_parts; - - return 0; -} - -int exec_command(const char *path, char *const argv[], unsigned timeout) { - timeout = timeout ^ timeout; - int pid; - int pipefd[2]; - int status; - char buf[512]; - - pipe(pipefd); - if ((pid = fork()) == 0) { - close(pipefd[0]); - dup2(pipefd[1], 1); // includes close(1); - close(pipefd[1]); - execvp(path, argv); - fprintf(stderr, "could not execute \"%s\"\n", path); - fflush(stderr); - exit(-1); - } - - if (pid < 0) { - fprintf(stderr, "no fork\n"); - exit(-2); - } - - close(pipefd[1]); - - __ssize_t length = 0; - while ((length = read(pipefd[0], buf, 10)) > 0) { - buf[length] = '\0'; - fprintf(stdout, "%s", buf); - fflush(stdout); - } - - waitpid(pid, &status, 0); - - close(pipefd[0]); - - return WEXITSTATUS(status); -} |