diff --git a/Makefile b/Makefile index 90062b1..c2e8000 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ -GCC=gcc -g +GCC=gcc -g -lreadline .PHONY default: build diff --git a/main.c b/main.c index 25a30e9..5a63be6 100644 --- a/main.c +++ b/main.c @@ -5,6 +5,9 @@ #include +#include +#include + int get_alias(Alias *alias, State *state, char *name); int add_alias(State* state, char *name, char *substitution); int del_alias(State *state, char *name); @@ -113,7 +116,6 @@ int del_array_element(GrowingArray* array, char* key) { } else { array->inner[i] = array->inner[i+1]; } - } } } @@ -203,7 +205,6 @@ void shutdown(State *state) { } int check_pipes(State* state, int num_args, char* args[]) { - int left = 0; int num_commands = 0; int space = 10; @@ -256,13 +257,8 @@ int check_pipes(State* state, int num_args, char* args[]) { for (int i = 0; i < num_commands; i++) { execute(in, out, pipes[i], &child); if (i != num_commands - 1) { - if (in == pipein[0]) { - in = pipeout[0]; - out = pipein[1]; - } else { - in = pipein[0]; - out = pipeout[1]; - } + in = pipeout[0]; + out = pipein[1]; } else { out = 1; in = out - 1; @@ -324,7 +320,6 @@ int check_builtins(State *state, int num_args, char *args[]) { } } return ER_FAILURE; - } if (!strcmp(args[0], "alias")) { @@ -459,11 +454,12 @@ int repl(State* state, FILE *input) { char *line; if (state->interactive) { - fprintf(state->output, "%s", state->ps1); - } - - if (readline(input, &line)) { - return ER_SUCCESS; + line = readline(state->ps1); + add_history(line); + } else { + if (get_line(input, &line)) { + return ER_SUCCESS; + } } char **sep_string; @@ -472,6 +468,7 @@ int repl(State* state, FILE *input) { split_string(&sep_string, &num_words, line, ' '); int err = check_pipes(state, num_words, sep_string); + if (err == ER_NOT_EXISTS) { expand_command(state, num_words, sep_string); } @@ -517,8 +514,8 @@ int startup(State *state, int argc, char **argv) { perror("Config file error"); return err; } + fclose(rc); } - fclose(rc); if (argc == 2) { FILE *script_text = fopen(argv[1], "r"); diff --git a/main.h b/main.h index 3d63bdc..3aa5550 100644 --- a/main.h +++ b/main.h @@ -6,7 +6,6 @@ #include #include - #ifndef MAINH #define MAINH @@ -46,8 +45,10 @@ struct State { struct GrowingArray variables; int num_aliases; FILE* code; - FILE *input; - FILE *output; + + FILE* input; + FILE* output; + bool interactive; char *ps1; }; diff --git a/run.c b/run.c index 233aa78..006b4d4 100644 --- a/run.c +++ b/run.c @@ -1,5 +1,12 @@ #include "run.h" +struct Job { + bool done; + int ret_val; + char **argv; + int argc; +}; + /* Fork and exec a command given in args taking input from in and sending * output to out. The arguments array must end in a NULL. * diff --git a/util.c b/util.c index 36e63c8..979412a 100644 --- a/util.c +++ b/util.c @@ -10,7 +10,7 @@ void *dualloc(void *thing, size_t size) { } /* Read a line into a newly created buffer */ -int readline(FILE* in, char **out) { +int get_line(FILE* in, char **out) { int size_of_buffer = INITIAL_LINE_BUFFER; char *buffer = calloc(size_of_buffer, sizeof(char)); char character; @@ -159,14 +159,14 @@ int split_string(char **sep_string[], int *num, char *string, char delim) { int print_chicken(State *state) { char *chicken[] = { - " _\n", + " _\n", " (o)\n", " / |\n", " / |==========\n", " \\ CHSH /\n", " \\______/\n", " |\n", - " _|}\n"}; + " _|\n"}; for (int i = 0; i < 8; i ++) { diff --git a/util.h b/util.h index 71bba55..d41968c 100644 --- a/util.h +++ b/util.h @@ -6,7 +6,7 @@ void *dualloc(void *thing, size_t size); -int readline(FILE* in, char **out); +int get_line(FILE* in, char **out); int split_string(char **sep_string[], int *num, char *string, char delim);