From 05d6a71cab4e8dd2fd4a72d508c3fc49b5985bf4 Mon Sep 17 00:00:00 2001 From: Jaewon Choi <jaewon.james.choi@gmail.com> Date: Sun, 15 Oct 2017 15:29:34 +0900 Subject: [PATCH] WIP: move to another machine --- include/utils.h | 2 +- src/main.c | 19 ++++++++++++++++--- src/utils.c | 12 ++++++++---- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/include/utils.h b/include/utils.h index 6076d97..5f3403d 100644 --- a/include/utils.h +++ b/include/utils.h @@ -5,7 +5,7 @@ void mysh_parse_command(const char* command, int* n_commands, - struct single_command** commands); + struct single_command (*commands)[]); void parse_single_command(const char* command, int *argc, char*** argv); diff --git a/src/main.c b/src/main.c index efb87c3..fd6bffa 100644 --- a/src/main.c +++ b/src/main.c @@ -10,13 +10,25 @@ static void release_argv(int argc, char*** argv); int main() { char buf[8096]; - int argc; - char** argv; while (1) { fgets(buf, 8096, stdin); - mysh_parse_command(buf, &argc, &argv); + struct single_command commands[512]; + int n_commands = 0; + mysh_parse_command(buf, &n_commands, &commands); + + for (int i = 0; i < n_commands; ++i) { + struct single_command* com = commands + i; + int argc = com->argc; + char** argv = com->argv; + for (int j = 0; j < argc; ++j) { + printf("%s ", argv[j]); + } + printf("\n"); + } + + /* if (strcmp(argv[0], "") == 0) { goto release_and_continue; @@ -39,6 +51,7 @@ release_and_continue: release_and_exit: release_argv(argc, &argv); break; + */ } return 0; diff --git a/src/utils.c b/src/utils.c index 1d1ad39..fe4d51e 100644 --- a/src/utils.c +++ b/src/utils.c @@ -1,15 +1,13 @@ #include "utils.h" +#define _GNU_SOURCE #include <stdlib.h> #include <string.h> void mysh_parse_command(const char* command, int* n_commands, - struct single_command** commands) + struct single_command (*commands)[]) { - const int kMaxPipes = 512; - *commands = (struct single_command*)malloc(sizeof(struct single_command) * kMaxPipes); - char buf[4096]; strcpy(buf, command); @@ -19,6 +17,12 @@ void mysh_parse_command(const char* command, int ti = 0; while (tok != NULL) { + struct single_command* com = *commands + ti; + parse_single_command(tok, &com->argc, &com->argv); + + ++ti; + + tok = strtok_r(buf, "|", &saveptr); } *n_commands = ti; -- GitLab