summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStefan Zabka <zabkaste@hu-berlin.de>2020-05-21 23:37:38 +0200
committerStefan Zabka <zabkaste@hu-berlin.de>2020-05-21 23:37:38 +0200
commitf959165cdece3c1bfae8abce834a2bb78db96190 (patch)
treec8f060d804a0c8aed5cbba2bd37a5ecb114dc21b
parent45b35365a5d0f895caeb6178f5f0a70cfe4464ee (diff)
downloadbetriebssysteme-f959165cdece3c1bfae8abce834a2bb78db96190.tar.gz
betriebssysteme-f959165cdece3c1bfae8abce834a2bb78db96190.zip
Process parse_line working
-rw-r--r--02_exercise/CMakeLists.txt21
-rw-r--r--02_exercise/process.c148
-rw-r--r--02_exercise/process.h30
-rw-r--r--02_exercise/process_test.c51
-rw-r--r--02_exercise/prompt_utils.c2
-rw-r--r--02_exercise/prompt_utils_test.c8
-rw-r--r--02_exercise/shell.c105
7 files changed, 270 insertions, 95 deletions
diff --git a/02_exercise/CMakeLists.txt b/02_exercise/CMakeLists.txt
index ee20a4a..ca67d1a 100644
--- a/02_exercise/CMakeLists.txt
+++ b/02_exercise/CMakeLists.txt
@@ -2,18 +2,31 @@ cmake_minimum_required(VERSION 3.5)
project(shell C)
+set(CMAKE_C_COMPILER gcc)
+set(CMAKE_C_STANDARD 11)
+set(CMAKE_C_STANDARD_REQUIRED True)
+
add_executable(prog prog.c)
add_executable(shell shell.c)
-target_link_libraries(shell PRIVATE array prompt_utils)
+target_link_libraries(shell PRIVATE array prompt_utils process)
add_compile_definitions(_GNU_SOURCE)
add_library(array array.c)
add_library(prompt_utils prompt_utils.c)
target_link_libraries(prompt_utils PRIVATE array)
-set(CMAKE_C_COMPILER gcc)
-set(CMAKE_C_STANDARD gnu11)
-set(CMAKE_C_STANDARD_REQUIRED True)
+
+add_executable(prompt_utils_test prompt_utils_test.c)
+target_link_libraries(prompt_utils_test PRIVATE prompt_utils array)
+
+add_library(process process.c)
+target_link_libraries(process PRIVATE array)
+
+add_executable(process_test process_test.c)
+target_link_libraries(process_test PRIVATE process)
+
+
+
set(CLANG_WARNINGS
-Wall
-Wextra # reasonable and standard
diff --git a/02_exercise/process.c b/02_exercise/process.c
new file mode 100644
index 0000000..9bc85be
--- /dev/null
+++ b/02_exercise/process.c
@@ -0,0 +1,148 @@
+//
+// Created by stefan on 21.05.20.
+//
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <wait.h>
+
+#include "array.h"
+#include "process.h"
+
+// Given a substring of a line tries to parse out as much information as possible
+int parse_command(char const *line, char const *end, process *p) {
+
+ 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; line + i < end; ++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;
+ }
+ }
+ if (arrayLen(part) == 0) {
+ arrayRelease(part);
+ } else {
+ arrayPush(local_parts) = part;
+ }
+
+ arrayInit(part);
+ arrayPush(part) = '\0';
+ arrayPush(local_parts) = part;
+ p->argc = arrayLen(local_parts);
+ p->argv = local_parts;
+
+ return 0;
+}
+
+int parse_line(char const *const line, process **processes) {
+ //Splits the line at | and then parses the commands
+ int ret_code = 0;
+
+ if (arrayInit(*processes) != 0) {
+ perror("Failed to initialize processes array");
+ return -1;
+ }
+ bool done = false;
+ char const *cursor = line;
+ while (!done) {
+ process p = {.in_fd = 0, .out_fd = 0, .pid = 0, .blocking = true};
+ char const *end = strchr(cursor, '|');
+ if (end == NULL) {
+ done = true;
+ end = line + strlen(line);
+ }
+ if ((ret_code = parse_command(cursor, end, &p)) != 0) {
+ return ret_code;
+ }
+
+ arrayPush(*processes) = p;
+ cursor = end + 1;
+ }
+ size_t p_len = arrayLen(*processes);
+ process * last = *processes + (p_len -1);
+
+ // Linking up all processes as we currently only have pipes for multiple commands
+
+ for (size_t i = 0; i < p_len -1 ; ++i) {
+ int fds[2];
+ if(pipe(fds) != 0) {
+ perror("Failed to create pipe");
+ return -1;
+ }
+ (*processes)[i].out_fd=fds[1];
+ (*processes)[i+1].in_fd=fds[0];
+
+ }
+ // Setting all processes to non blocking when
+ if(strcmp(last->argv[last->argc-2], "&")== 0) {
+ arrayPop(last->argv);
+ last->argc = last->argc - 1;
+
+ for (size_t i = 0; i < p_len; ++i) {
+ (*processes)[i].blocking = false;
+ }
+ }
+ return ret_code;
+}
+
+int exec_command(process p, unsigned timeout) {
+ timeout = timeout ^ timeout;
+ int pid;
+ int status;
+
+ if ((pid = fork()) == 0) {
+ if (p.in_fd != 0) {
+ dup2(p.in_fd, 0);
+ }
+ if (p.out_fd != 0) {
+ dup2(p.out_fd, 1);
+ }
+ execvp(p.argv[0], p.argv);
+ fprintf(stderr, "could not execute \"%s\"\n", p.argv[0]);
+ fflush(stderr);
+ exit(-1);
+ }
+
+ if (pid < 0) {
+ fprintf(stderr, "no fork\n");
+ exit(-2);
+ }
+
+ waitpid(pid, &status, 0);
+
+ return WEXITSTATUS(status);
+}
+
+int free_processes(process ** pr) {
+ process * processes = * pr;
+ while (!arrayIsEmpty(processes)) {
+ process p = arrayPop(processes);
+ while (!arrayIsEmpty(p.argv)) {
+ char *tmp = arrayPop(p.argv);
+ if (tmp)
+ arrayRelease(tmp);
+ }
+ arrayRelease(p.argv);
+ }
+ arrayRelease(processes);
+ *pr = NULL;
+} \ No newline at end of file
diff --git a/02_exercise/process.h b/02_exercise/process.h
new file mode 100644
index 0000000..f1cdfa0
--- /dev/null
+++ b/02_exercise/process.h
@@ -0,0 +1,30 @@
+//
+// Created by stefan on 21.05.20.
+//
+
+#ifndef SHELL_PROCESS_H
+#define SHELL_PROCESS_H
+#include <stdbool.h>
+#include <stddef.h>
+
+typedef struct {
+ char **argv;
+ size_t argc;
+ int in_fd;
+ int out_fd;
+ int pid;
+ bool blocking;
+} process;
+
+//Parses the given line and creates an array of processes at *processes
+// Expects tail -F file | grep panic & to mean that both processes should
+// run in the background
+int parse_line(char const *line, process ** processes);
+// returns the return code of the executed program
+int exec_command(process p, unsigned timeout);
+
+int free_processes(process ** processes);
+
+
+
+#endif // SHELL_PROCESS_H
diff --git a/02_exercise/process_test.c b/02_exercise/process_test.c
new file mode 100644
index 0000000..89eb2fb
--- /dev/null
+++ b/02_exercise/process_test.c
@@ -0,0 +1,51 @@
+//
+// Created by stefan on 21.05.20.
+//
+#include "process.h"
+#include "array.h"
+#include <assert.h>
+#include <string.h>
+
+
+void test_simple_case() {
+ process * processes= NULL;
+ parse_line("cat my_txt ",&processes);
+ assert(arrayLen(processes) == 1);
+ assert(arrayLen(processes[0].argv) == 3);
+ assert(strcmp(processes[0].argv[0], "cat") == 0);
+ assert(strcmp(processes[0].argv[1], "my_txt") == 0);
+ free_processes(&processes);
+}
+
+void test_detatched() {
+ process * processes= NULL;
+ parse_line("cat my_txt &",&processes);
+ assert(arrayLen(processes) == 1);
+ assert(arrayLen(processes[0].argv) == 3);
+ assert(strcmp(processes[0].argv[0], "cat") == 0);
+ assert(strcmp(processes[0].argv[1], "my_txt") == 0);
+ assert(processes[0].blocking == false);
+ free_processes(&processes);
+}
+
+
+void test_pipe() {
+ process * processes= NULL;
+ parse_line("echo my_txt | grep txt",&processes);
+ assert(arrayLen(processes) == 2);
+ assert(arrayLen(processes[0].argv) == 3);
+ assert(strcmp(processes[0].argv[0], "echo") == 0);
+ assert(strcmp(processes[0].argv[1], "my_txt") == 0);
+ assert(processes[0].out_fd != 0);
+ assert(arrayLen(processes[1].argv) == 3);
+ assert(strcmp(processes[1].argv[0], "grep") == 0);
+ assert(strcmp(processes[1].argv[1], "txt") == 0);
+ assert(processes[1].in_fd != 0);
+ free_processes(&processes);
+}
+int main() {
+ test_simple_case();
+ test_detatched();
+ test_pipe();
+ return 0;
+} \ No newline at end of file
diff --git a/02_exercise/prompt_utils.c b/02_exercise/prompt_utils.c
index d43a6b6..238f47c 100644
--- a/02_exercise/prompt_utils.c
+++ b/02_exercise/prompt_utils.c
@@ -84,7 +84,7 @@ size_t *get_seperator_indeces(char const *const text, char seperator) {
return NULL;
}
while ((current = strchr(current, seperator)) != NULL) {
- arrayPush(indeces) = current - text;
+ arrayPush(indeces) = (size_t) (current - text);
++current;
}
arrayPush(indeces) = strlen(text);
diff --git a/02_exercise/prompt_utils_test.c b/02_exercise/prompt_utils_test.c
index f648275..3819021 100644
--- a/02_exercise/prompt_utils_test.c
+++ b/02_exercise/prompt_utils_test.c
@@ -34,4 +34,10 @@ void test_relative_path() {
test_paths("/", "/Test/a/asd", "./Test/a/asd");
test_paths("/Test/c", "/Test/b", "../b");
test_paths("/B/C", "/", "../..");
-} \ No newline at end of file
+}
+
+int main(void) {
+ test_relative_path();
+ test_get_seperator_indeces();
+ return 0;
+}
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);
-}