summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNiklas Halle <niklas@niklashalle.net>2020-05-21 10:25:23 +0200
committerNiklas Halle <niklas@niklashalle.net>2020-05-21 10:25:23 +0200
commitddbb47ed661614d768e8ebd3b0a5084ee82b0beb (patch)
tree8fbba9b3e05f39a5fec25a1352023ab21429bdbd
parent3d0ae3aaee22bd5e768255f2f519d9e95f517552 (diff)
downloadbetriebssysteme-ddbb47ed661614d768e8ebd3b0a5084ee82b0beb.tar.gz
betriebssysteme-ddbb47ed661614d768e8ebd3b0a5084ee82b0beb.zip
?????
-rw-r--r--02_exercise/shell.c103
1 files changed, 66 insertions, 37 deletions
diff --git a/02_exercise/shell.c b/02_exercise/shell.c
index 75e77b7..84f8aff 100644
--- a/02_exercise/shell.c
+++ b/02_exercise/shell.c
@@ -2,71 +2,100 @@
#include <stdlib.h>
#include <string.h>
-#include <fcntl.h>
-#include <signal.h>
-#include <sys/stat.h>
-#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include "array.h"
-#include "errno.h"
#include <stdbool.h>
void print_prompt();
+int parse_line(char const *line, char **exe, 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(int argc, char* argv[]) {
-
- char *argument;
- char **arguments;
- if (arrayInit(argument) != 0 || arrayInit(arguments) != 0) {
- fprintf(stderr, "Failed to prepare new argument array");
- exit(-1);
- }
-
while (true) {
- char *command = NULL;
+ char *line = NULL;
size_t cap = 0;
size_t length = 0;
print_prompt();
- if ((length = getline(&command, &cap, stdin)) < 0) {
+ if ((length = getline(&line, &cap, stdin)) < 0) {
fprintf(stderr, "Failed to read from STDIN");
exit(-1);
}
- command[length - 1] = '\0'; // cut the line feed
-/*
- char c;
- int i = 0;
- while (true) {
- c = command[i++];
- if (c == ' ') {
- arrayPush(argument) = '\0';
- arrayPush(arguments) = argument;
- printf("+ %s\n", argument);
- arrayRelease(argument);
- arrayInit(argument);
- } else if (c == '\0') {
-
- } else {
- arrayPush(argument) = c;
- }
+ line[length - 1] = '\0'; // cut the line feed
+
+ char *exe = NULL;
+ char **arguments = NULL;
+ size_t argument_count;
+ parse_line(line, &exe, &arguments, &argument_count);
+
+ for (size_t h = 0; h < argument_count; ++h) {
+ char *part;
+ part = arrayPop(arguments);
+ if (part == NULL)
+ continue;
+ printf("%s\n", part);
}
-*/
- if (strstr(command, "cd") != NULL) {
+
+ exit(0);
+
+ if (strcmp(exe, "cd") == 0) {
printf("Changing dirs \n");
chdir("beispiele");
- } else if (strcmp(command, "exit\n") == 0) {
+ } else if (strcmp(exe, "exit") == 0) {
exit(0);
} else {
- exec_command(command, (char *const []) {command, NULL}, 0);
+ exec_command(exe, (char *const []) {exe, NULL}, 0);
+ }
+ free((void *)line);
+ // no need to free exe, since it is part of arguments any way
+ arrayRelease(arguments);
+ }
+}
+
+int parse_line(char const *line, char **exe, 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 array");
+ exit(-1);
+ }
+
+ bool found_exe = false;
+ char c;
+ int i = 0;
+ while (true) {
+ c = line[i++];
+ if (c == ' ' || c == '\0') {
+ arrayPush(part) = '\0';
+ arrayPush(local_parts) = part;
+ if (!found_exe) {
+ // the first is the executable
+ *exe = malloc(arrayLen(part) * sizeof(char));
+ strcpy(*exe, part);
+ found_exe = true;
+ }
+ arrayRelease(part); // TODO: am i supposed/allowed to free it here?
+ arrayInit(part);
+ if (c == '\0') {
+ arrayPush(local_parts) = NULL;
+ break;
+ }
+ } else {
+ arrayPush(part) = c;
}
- free((void *)command);
}
+
+ *part_count = arrayLen(local_parts);
+ *parts = local_parts;
+
+ return 0;
}
void print_prompt() {