diff --git a/main.c b/main.c index da466aa..1e00f2e 100644 --- a/main.c +++ b/main.c @@ -2,6 +2,7 @@ #include "run.h" #include "main.h" #include "util.h" +#include #include #include @@ -17,6 +18,8 @@ int run_command(State *state, int argc, char *argv[]); int get_array_element(ArrayElement **ret, GrowingArray* array, char* key); int del_array_element(GrowingArray* array, char* key); +const int GLOB_DEBUG; + GrowingArray get_growing_array(size_t num_elements, size_t size_elem) { GrowingArray arr; arr.inner = calloc(num_elements, size_elem); @@ -206,8 +209,11 @@ void shutdown(State *state) { } int run_pipes(State *state, int num_args, char *args []) { - char *joined; + char *joined = 0; int err = join_string(&joined, num_args, args, " "); + if (err) { + errx(1, "no"); + } char * separated_by_pipe; int pipe_count = 0; @@ -216,6 +222,7 @@ int run_pipes(State *state, int num_args, char *args []) { pipe_count++; } } + int num_commands = pipe_count + 1; char **sep_commands = calloc(pipe_count + 1, sizeof(char *)); @@ -229,22 +236,13 @@ int run_pipes(State *state, int num_args, char *args []) { i++; } while ((piece = (char *)strtok(NULL, "|")) != 0); - if (num_commands == 1) { return ER_NOT_EXISTS; } - /*int pipein[2];*/ - /*int pipeout[2];*/ - /*memset(pipein, 0, sizeof(int) * 2);*/ - /*memset(pipeout, 0, sizeof(int) * 2);*/ - /*pipe(pipein);*/ - /*pipe(pipeout);*/ - // 0 is read, 1 is write - int *pipes = calloc(pipe_count * 2, sizeof(int)); - for (int i = 0; i < num_commands; i++) { + for (int i = 0; i < pipe_count; i++) { pipe(pipes + (2 * i)); } @@ -253,14 +251,6 @@ int run_pipes(State *state, int num_args, char *args []) { int in; int out; - /*dup2(pipes[pipe_count*2+1], 1);*/ - /*dup2(pipes[0], 0);*/ - - // read = 0 - // write = 1 - -//int split_string(char **sep_string[], int *num, char *string, char delim) { - for (int i = 0; i < num_commands; i++) { char **command; int words; @@ -279,13 +269,22 @@ int run_pipes(State *state, int num_args, char *args []) { printf("in %d out: %d exec: ", in, out); for (int i = 0; i < words; i++) { - printf("%s ", command[i]); } printf("\n"); execute(in, out, command, &child); } + int status; + while (!wait(&status)); + + free(pipes); + for (int i = 0; i < num_commands; i++) { + free(sep_commands[i]); + } + free(sep_commands); + free(joined); + return ER_SUCCESS; } @@ -389,6 +388,7 @@ int check_builtins(State *state, int num_args, char *args[]) { char **split; int num; err = split_string(&split, &num, joined, '='); + printf("%s\n", joined); if (num == 2) { char * key = split[0]; char * value = split[1]; @@ -567,6 +567,9 @@ int repl(State* state, FILE *input) { split_string(&sep_string, &num_words, line, ' '); + if (num_words == 0) + continue; + // int err = check_pipes(state, num_words, sep_string); int err = run_pipes(state, num_words, sep_string); diff --git a/run.c b/run.c index 006b4d4..d3c5744 100644 --- a/run.c +++ b/run.c @@ -1,4 +1,5 @@ #include "run.h" +#include struct Job { bool done; diff --git a/util.c b/util.c index 979412a..8f83e2a 100644 --- a/util.c +++ b/util.c @@ -65,7 +65,7 @@ int join_string(char **joined, int arr_len, char *sep_stirng[], char *delim) { length += len; - if (sep_stirng[i+1] == 0) { + if (i != (arr_len - 1) && sep_stirng[i+1] == 0) { *joined = new_string; return ER_SUCCESS; } else { @@ -75,6 +75,8 @@ int join_string(char **joined, int arr_len, char *sep_stirng[], char *delim) { length += delim_len; } } + *joined = new_string; + return ER_SUCCESS; return ER_FAILURE; } @@ -117,6 +119,7 @@ int strip_char(char *string, char del) { */ int split_string(char **sep_string[], int *num, char *string, char delim) { char **word_list = calloc(10, sizeof(char*)); + int word_list_size = 10; int word_size; char *word = calloc((word_size=INITIAL_WORD_SIZE), sizeof(char)); @@ -124,6 +127,12 @@ int split_string(char **sep_string[], int *num, char *string, char delim) { int word_length = 0; for (int i = 0; true; i++) { + + if (word_list_size - 1 == num_words) { + word_list_size += 1; + word_list = reallocarray(word_list, word_list_size, sizeof(char*)); + } + if (string[i] == '\0') { if (word_length) { word_list[num_words] = word;