From 21b0013f6c23c704c8e895c883e9e5916cb7de90 Mon Sep 17 00:00:00 2001 From: Manuele Conti Date: Sun, 8 Aug 2021 14:57:21 +0200 Subject: [PATCH 01/12] Start adding bdsh full pipes support --- uspace/app/bdsh/input.c | 171 +++++++++++++++++++++------------------- uspace/app/bdsh/tok.c | 40 +++++++++- uspace/app/bdsh/tok.h | 3 + 3 files changed, 134 insertions(+), 80 deletions(-) diff --git a/uspace/app/bdsh/input.c b/uspace/app/bdsh/input.c index 8918ee4b8e..7dfeed20ff 100644 --- a/uspace/app/bdsh/input.c +++ b/uspace/app/bdsh/input.c @@ -62,13 +62,12 @@ static tinput_t *tinput; /* Private helpers */ static int run_command(char **, cliuser_t *, iostate_t *); -static void print_pipe_usage(void); typedef struct { link_t alias_hup_link; alias_t *alias; } alias_hup_t; - +#if 0 static bool find_alias_hup(alias_t *alias, list_t *alias_hups) { list_foreach(*alias_hups, alias_hup_link, alias_hup_t, link) { @@ -79,7 +78,7 @@ static bool find_alias_hup(alias_t *alias, list_t *alias_hups) return false; } - +#endif /* * Tokenizes input from console, sees if the first word is a built-in, if so * invokes the built-in entry point (a[0]) passing all arguments in a[] to @@ -87,6 +86,15 @@ static bool find_alias_hup(alias_t *alias, list_t *alias_hups) */ static errno_t process_input_nohup(cliuser_t *usr, list_t *alias_hups, size_t count_executed_hups) { + char *cmd[WORD_MAX]; + size_t cmd_argc = 0; + errno_t rc = EOK; + tokenizer_t tok; + unsigned int i, pipe_count; + unsigned int pipe_pos[2]; + char *redir_from = NULL; + char *redir_to = NULL; + if (count_executed_hups >= HUBS_MAX) { cli_error(CL_EFAIL, "%s: maximal alias hubs reached\n", PACKAGE_NAME); return ELIMIT; @@ -97,14 +105,6 @@ static errno_t process_input_nohup(cliuser_t *usr, list_t *alias_hups, size_t co return ENOMEM; token_t *tokens = tokens_buf; - char *cmd[WORD_MAX]; - errno_t rc = EOK; - tokenizer_t tok; - unsigned int i, pipe_count, processed_pipes; - unsigned int pipe_pos[2]; - char *redir_from = NULL; - char *redir_to = NULL; - if (usr->line == NULL) { free(tokens_buf); return EINVAL; @@ -130,70 +130,35 @@ static errno_t process_input_nohup(cliuser_t *usr, list_t *alias_hups, size_t co tokens_length--; } - /* - * Until full support for pipes is implemented, allow for a simple case: - * [from |] command [| to ] - * - * First find the pipes and check that there are no more - */ + cmd_argc = tokens_length; for (i = 0, pipe_count = 0; i < tokens_length; i++) { - if (tokens[i].type == TOKTYPE_PIPE) { - if (pipe_count >= 2) { - print_pipe_usage(); - rc = ENOTSUP; - goto finit; - } - pipe_pos[pipe_count] = i; - pipe_count++; + switch (tokens[i].type) { + case TOKTYPE_PIPE: + pipe_pos[pipe_count++] = i; + cmd_argc = i; + redir_to = (char *)"/tmp/pipe"; + break; + + case TOKTYPE_RDIN: + redir_from = tokens[i + 1].text; + cmd_argc = i; + break; + + case TOKTYPE_RDOU: + redir_to = tokens[i + 1].text; + cmd_argc = i; + break; + + default: + break; } - } - unsigned int cmd_token_start = 0; - unsigned int cmd_token_end = tokens_length; - - processed_pipes = 0; - - /* Check if the first part (from |) is present */ - if (pipe_count > 0 && (pipe_pos[0] == 3 || pipe_pos[0] == 4) && str_cmp(tokens[0].text, "from") == 0) { - /* Ignore the first three tokens (from, file, pipe) and set from */ - redir_from = tokens[2].text; - cmd_token_start = pipe_pos[0] + 1; - processed_pipes++; - } - - /* Check if the second part (| to ) is present */ - if ((pipe_count - processed_pipes) > 0 && - (pipe_pos[processed_pipes] == tokens_length - 4 || - (pipe_pos[processed_pipes] == tokens_length - 5 && - tokens[tokens_length - 4].type == TOKTYPE_SPACE)) && - str_cmp(tokens[tokens_length - 3].text, "to") == 0) { - /* Ignore the last three tokens (pipe, to, file) and set to */ - redir_to = tokens[tokens_length - 1].text; - cmd_token_end = pipe_pos[processed_pipes]; - processed_pipes++; - } - - if (processed_pipes != pipe_count) { - print_pipe_usage(); - rc = ENOTSUP; - goto finit; } - /* Convert tokens of the command to string array */ - unsigned int cmd_pos = 0; - for (i = cmd_token_start; i < cmd_token_end; i++) { - if (tokens[i].type != TOKTYPE_SPACE) { - cmd[cmd_pos++] = tokens[i].text; - } - } - cmd[cmd_pos++] = NULL; - - if (cmd[0] == NULL) { - print_pipe_usage(); - rc = ENOTSUP; - goto finit; - } + unsigned int cmd_token_start = 0; + unsigned int cmd_token_end = cmd_argc; +#if 0 /* test if the passed cmd is an alias */ odlink_t *alias_link = odict_find_eq(&alias_dict, (void *)cmd[0], NULL); if (alias_link != NULL) { @@ -243,6 +208,7 @@ static errno_t process_input_nohup(cliuser_t *usr, list_t *alias_hups, size_t co goto finit; } } +#endif iostate_t new_iostate = { .stdin = stdin, @@ -273,6 +239,63 @@ static errno_t process_input_nohup(cliuser_t *usr, list_t *alias_hups, size_t co new_iostate.stdout = to; } + for (unsigned p = 0; p < pipe_count; p++) { + /* Convert tokens of the command to string array */ + unsigned int cmd_pos = 0; + for (i = cmd_token_start; i < cmd_token_end; i++) { + if (tokens[i].type != TOKTYPE_SPACE) { + cmd[cmd_pos++] = tokens[i].text; + } + } + cmd[cmd_pos++] = NULL; + + if (cmd[0] == NULL) { + printf("Command not found.\n"); + rc = ENOTSUP; + goto finit; + } + + if (p < pipe_count - 1) { + new_iostate.stdout = to; + } else { + new_iostate.stdin = to; + } + + if (run_command(cmd, usr, &new_iostate) == 0) { + rc = EOK; + } else { + rc = EINVAL; + } + + // Restore the Standard Input, Output and Error file descriptors + new_iostate.stdin = stdin; + new_iostate.stdout = stdout; + new_iostate.stderr = stderr; + + cmd_token_start = cmd_token_end + 1; + cmd_token_end = (p < pipe_count - 1) ? pipe_pos[p + 1] : tokens_length; + } + + unsigned int cmd_pos = 0; + for (i = cmd_token_start; i < cmd_token_end; i++) { + if (tokens[i].type != TOKTYPE_SPACE) { + cmd[cmd_pos++] = tokens[i].text; + } + } + cmd[cmd_pos++] = NULL; + + if (cmd[0] == NULL) { + printf("Command not found.\n"); + rc = ENOTSUP; + goto finit; + } + + if (pipe_count) { + fseek(to, 0, SEEK_SET); + new_iostate.stdin = to; + } + + if (run_command(cmd, usr, &new_iostate) == 0) { rc = EOK; } else { @@ -313,16 +336,6 @@ errno_t process_input(cliuser_t *usr) return rc; } -void print_pipe_usage(void) -{ - printf("Invalid syntax!\n"); - printf("Usage of redirection (pipes in the future):\n"); - printf("from filename | command ...\n"); - printf("from filename | command ... | to filename\n"); - printf("command ... | to filename\n"); - -} - int run_command(char **cmd, cliuser_t *usr, iostate_t *new_iostate) { int id = 0; diff --git a/uspace/app/bdsh/tok.c b/uspace/app/bdsh/tok.c index d9b9556b71..387f089dbc 100644 --- a/uspace/app/bdsh/tok.c +++ b/uspace/app/bdsh/tok.c @@ -137,7 +137,45 @@ errno_t tok_tokenize(tokenizer_t *tok, size_t *tokens_length) if (rc != EOK) { return rc; } - } else if (next_char == '\'') { + } else if (next_char == '<') { + if (tok_pending_chars(tok)) { + rc = tok_push_token(tok); + if (rc != EOK) { + return rc; + } + } + + tok_start_token(tok, TOKTYPE_RDIN); + + rc = tok_push_char(tok, tok_get_char(tok)); + if (rc != EOK) { + return rc; + } + tok_get_char(tok); + rc = tok_push_token(tok); + if (rc != EOK) { + return rc; + } + } else if (next_char == '>') { + if (tok_pending_chars(tok)) { + rc = tok_push_token(tok); + if (rc != EOK) { + return rc; + } + } + + tok_start_token(tok, TOKTYPE_RDOU); + + rc = tok_push_char(tok, tok_get_char(tok)); + if (rc != EOK) { + return rc; + } + tok_get_char(tok); + rc = tok_push_token(tok); + if (rc != EOK) { + return rc; + } + } else if (next_char == '\'') { /* * A string starts with a quote (') and ends again with a quote. * A literal quote is written as '' diff --git a/uspace/app/bdsh/tok.h b/uspace/app/bdsh/tok.h index b0ead897bf..f518c13e67 100644 --- a/uspace/app/bdsh/tok.h +++ b/uspace/app/bdsh/tok.h @@ -32,6 +32,9 @@ typedef enum { TOKTYPE_TEXT, TOKTYPE_PIPE, + TOKTYPE_RDIN, + TOKTYPE_RDOU, + TOKTYPE_RDAP, TOKTYPE_SPACE } token_type_t; From 932c6406cfdb756b055b8a65f21a2a6f60a512ac Mon Sep 17 00:00:00 2001 From: Manuele Conti Date: Sun, 8 Aug 2021 15:17:05 +0200 Subject: [PATCH 02/12] Add limit check about 10 pipes --- uspace/app/bdsh/input.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/uspace/app/bdsh/input.c b/uspace/app/bdsh/input.c index 7dfeed20ff..5113b2960d 100644 --- a/uspace/app/bdsh/input.c +++ b/uspace/app/bdsh/input.c @@ -55,6 +55,8 @@ #include "exec.h" #include "tok.h" +#define MAX_PIPES 10U + extern volatile unsigned int cli_quit; /** Text input field. */ @@ -91,7 +93,7 @@ static errno_t process_input_nohup(cliuser_t *usr, list_t *alias_hups, size_t co errno_t rc = EOK; tokenizer_t tok; unsigned int i, pipe_count; - unsigned int pipe_pos[2]; + unsigned int pipe_pos[MAX_PIPES]; char *redir_from = NULL; char *redir_to = NULL; @@ -152,7 +154,10 @@ static errno_t process_input_nohup(cliuser_t *usr, list_t *alias_hups, size_t co default: break; } - + if (pipe_count > MAX_PIPES) { + rc = ENOTSUP; + goto finit; + } } unsigned int cmd_token_start = 0; From 5f9a52ed111cc3ed54ecadd71a7c90851d19ef8b Mon Sep 17 00:00:00 2001 From: Manuele Conti Date: Mon, 9 Aug 2021 23:42:29 +0200 Subject: [PATCH 03/12] Remove comment out code --- uspace/app/bdsh/input.c | 52 ----------------------------------------- 1 file changed, 52 deletions(-) diff --git a/uspace/app/bdsh/input.c b/uspace/app/bdsh/input.c index 5113b2960d..016fdc25c0 100644 --- a/uspace/app/bdsh/input.c +++ b/uspace/app/bdsh/input.c @@ -163,58 +163,6 @@ static errno_t process_input_nohup(cliuser_t *usr, list_t *alias_hups, size_t co unsigned int cmd_token_start = 0; unsigned int cmd_token_end = cmd_argc; -#if 0 - /* test if the passed cmd is an alias */ - odlink_t *alias_link = odict_find_eq(&alias_dict, (void *)cmd[0], NULL); - if (alias_link != NULL) { - alias_t *data = odict_get_instance(alias_link, alias_t, odict); - /* check if the alias already has been resolved once */ - if (!find_alias_hup(data, alias_hups)) { - alias_hup_t *hup = (alias_hup_t *)calloc(1, sizeof(alias_hup_t)); - if (hup == NULL) { - cli_error(CL_EFAIL, "%s: cannot allocate alias structure\n", PACKAGE_NAME); - rc = ENOMEM; - goto finit; - } - - hup->alias = data; - list_append(&hup->alias_hup_link, alias_hups); - - char *oldLine = usr->line; - const size_t input_length = str_size(usr->line) - str_size(cmd[0]) + str_size(data->value) + 1; - usr->line = (char *)malloc(input_length); - if (usr->line == NULL) { - cli_error(CL_EFAIL, "%s: cannot allocate input structure\n", PACKAGE_NAME); - rc = ENOMEM; - goto finit; - } - - usr->line[0] = '\0'; - - unsigned int cmd_replace_index = cmd_token_start; - for (i = 0; i < tokens_length; i++) { - if (i == cmd_replace_index) { - /* if there is a pipe symbol than cmd_token_start will point at the SPACE after the pipe symbol */ - if (tokens[i].type == TOKTYPE_SPACE) { - cmd_replace_index++; - str_append(usr->line, input_length, tokens[i].text); - continue; - } - - str_append(usr->line, input_length, data->value); - } else { - str_append(usr->line, input_length, tokens[i].text); - } - } - - /* reprocess input after string replace */ - rc = process_input_nohup(usr, alias_hups, count_executed_hups + 1); - usr->line = oldLine; - goto finit; - } - } -#endif - iostate_t new_iostate = { .stdin = stdin, .stdout = stdout, From 82570ff5faaff23ad6167454f598c240d2c94549 Mon Sep 17 00:00:00 2001 From: Manuele Conti Date: Mon, 9 Aug 2021 23:44:23 +0200 Subject: [PATCH 04/12] Restore alias management --- uspace/app/bdsh/input.c | 93 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 91 insertions(+), 2 deletions(-) diff --git a/uspace/app/bdsh/input.c b/uspace/app/bdsh/input.c index 016fdc25c0..d4cd54f0ae 100644 --- a/uspace/app/bdsh/input.c +++ b/uspace/app/bdsh/input.c @@ -69,7 +69,7 @@ typedef struct { link_t alias_hup_link; alias_t *alias; } alias_hup_t; -#if 0 + static bool find_alias_hup(alias_t *alias, list_t *alias_hups) { list_foreach(*alias_hups, alias_hup_link, alias_hup_t, link) { @@ -80,7 +80,65 @@ static bool find_alias_hup(alias_t *alias, list_t *alias_hups) return false; } -#endif + +static errno_t find_alias(char **cmd, list_t *alias_hups, alias_t **data) +{ + errno_t rc = EOK; + + /* test if the passed cmd is an alias */ + odlink_t *alias_link = odict_find_eq(&alias_dict, (void *)cmd[0], NULL); + if (alias_link != NULL) { + *data = odict_get_instance(alias_link, alias_t, odict); + /* check if the alias already has been resolved once */ + if (!find_alias_hup(*data, alias_hups)) { + alias_hup_t *hup = (alias_hup_t *)calloc(1, sizeof(alias_hup_t)); + if (hup == NULL) { + cli_error(CL_EFAIL, "%s: cannot allocate alias structure\n", PACKAGE_NAME); + rc = ENOMEM; + goto exit; + } + hup->alias = *data; + list_append(&hup->alias_hup_link, alias_hups); + } + } + +exit: + return rc; +} + +static errno_t replace_alias(token_t * tokens, unsigned int tokens_start, unsigned int tokens_len, alias_t *data, char **cmd, char **line) +{ + errno_t rc = EOK; + const size_t input_length = str_size(*line) - str_size(cmd[0]) + str_size(data->value) + 1; + *line = (char *)malloc(input_length); + if (*line == NULL) { + cli_error(CL_EFAIL, "%s: cannot allocate input structure\n", PACKAGE_NAME); + rc = ENOMEM; + goto exit; + } + + *line[0] = '\0'; + + unsigned int cmd_replace_index = tokens_start; + for (unsigned int i = 0; i < tokens_len; i++) { + if (i == cmd_replace_index) { + /* if there is a pipe symbol than cmd_token_start will point at the SPACE after the pipe symbol */ + if (tokens[i].type == TOKTYPE_SPACE) { + tokens_start++; + str_append(*line, input_length, tokens[i].text); + continue; + } + + str_append(*line, input_length, data->value); + } else { + str_append(*line, input_length, tokens[i].text); + } + } + +exit: + return rc; +} + /* * Tokenizes input from console, sees if the first word is a built-in, if so * invokes the built-in entry point (a[0]) passing all arguments in a[] to @@ -208,6 +266,22 @@ static errno_t process_input_nohup(cliuser_t *usr, list_t *alias_hups, size_t co goto finit; } + alias_t *data = NULL; + rc = find_alias(cmd, alias_hups, &data); + if (rc != EOK) { + goto finit; + } + + if (data != NULL) { + rc = replace_alias(tokens, cmd_token_start, tokens_length, data, cmd, &usr->line); + if (rc == EOK) { + /* reprocess input after string replace */ + rc = process_input_nohup(usr, alias_hups, count_executed_hups + 1); + } + goto finit; + } + + if (p < pipe_count - 1) { new_iostate.stdout = to; } else { @@ -243,6 +317,21 @@ static errno_t process_input_nohup(cliuser_t *usr, list_t *alias_hups, size_t co goto finit; } + alias_t *data = NULL; + rc = find_alias(cmd, alias_hups, &data); + if (rc != EOK) { + goto finit; + } + + if (data != NULL) { + rc = replace_alias(tokens, cmd_token_start, tokens_length, data, cmd, &usr->line); + if (rc == EOK) { + /* reprocess input after string replace */ + rc = process_input_nohup(usr, alias_hups, count_executed_hups + 1); + } + goto finit; + } + if (pipe_count) { fseek(to, 0, SEEK_SET); new_iostate.stdin = to; From 3d36920ec9022ed00fbf5e2d54ce1768d27de581 Mon Sep 17 00:00:00 2001 From: Manuele Conti Date: Tue, 10 Aug 2021 15:11:39 +0200 Subject: [PATCH 05/12] Improve readability --- uspace/app/bdsh/input.c | 52 +++++------------------------------------ 1 file changed, 6 insertions(+), 46 deletions(-) diff --git a/uspace/app/bdsh/input.c b/uspace/app/bdsh/input.c index d4cd54f0ae..9e670918fb 100644 --- a/uspace/app/bdsh/input.c +++ b/uspace/app/bdsh/input.c @@ -250,7 +250,7 @@ static errno_t process_input_nohup(cliuser_t *usr, list_t *alias_hups, size_t co new_iostate.stdout = to; } - for (unsigned p = 0; p < pipe_count; p++) { + for (unsigned p = 0; p <= pipe_count; p++) { /* Convert tokens of the command to string array */ unsigned int cmd_pos = 0; for (i = cmd_token_start; i < cmd_token_end; i++) { @@ -258,7 +258,7 @@ static errno_t process_input_nohup(cliuser_t *usr, list_t *alias_hups, size_t co cmd[cmd_pos++] = tokens[i].text; } } - cmd[cmd_pos++] = NULL; + cmd[cmd_pos] = NULL; if (cmd[0] == NULL) { printf("Command not found.\n"); @@ -281,10 +281,11 @@ static errno_t process_input_nohup(cliuser_t *usr, list_t *alias_hups, size_t co goto finit; } - - if (p < pipe_count - 1) { + if (p < pipe_count) { new_iostate.stdout = to; - } else { + } + if (p && p == pipe_count) { + fseek(to, 0, SEEK_SET); new_iostate.stdin = to; } @@ -303,47 +304,6 @@ static errno_t process_input_nohup(cliuser_t *usr, list_t *alias_hups, size_t co cmd_token_end = (p < pipe_count - 1) ? pipe_pos[p + 1] : tokens_length; } - unsigned int cmd_pos = 0; - for (i = cmd_token_start; i < cmd_token_end; i++) { - if (tokens[i].type != TOKTYPE_SPACE) { - cmd[cmd_pos++] = tokens[i].text; - } - } - cmd[cmd_pos++] = NULL; - - if (cmd[0] == NULL) { - printf("Command not found.\n"); - rc = ENOTSUP; - goto finit; - } - - alias_t *data = NULL; - rc = find_alias(cmd, alias_hups, &data); - if (rc != EOK) { - goto finit; - } - - if (data != NULL) { - rc = replace_alias(tokens, cmd_token_start, tokens_length, data, cmd, &usr->line); - if (rc == EOK) { - /* reprocess input after string replace */ - rc = process_input_nohup(usr, alias_hups, count_executed_hups + 1); - } - goto finit; - } - - if (pipe_count) { - fseek(to, 0, SEEK_SET); - new_iostate.stdin = to; - } - - - if (run_command(cmd, usr, &new_iostate) == 0) { - rc = EOK; - } else { - rc = EINVAL; - } - finit_with_files: if (from != NULL) { fclose(from); From 4518991929ed9c1b9d6ed15a4280a70e380734c4 Mon Sep 17 00:00:00 2001 From: Manuele Conti Date: Tue, 10 Aug 2021 16:20:22 +0200 Subject: [PATCH 06/12] Complete alias support --- uspace/app/bdsh/input.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/uspace/app/bdsh/input.c b/uspace/app/bdsh/input.c index 9e670918fb..c5eb61857f 100644 --- a/uspace/app/bdsh/input.c +++ b/uspace/app/bdsh/input.c @@ -90,7 +90,7 @@ static errno_t find_alias(char **cmd, list_t *alias_hups, alias_t **data) if (alias_link != NULL) { *data = odict_get_instance(alias_link, alias_t, odict); /* check if the alias already has been resolved once */ - if (!find_alias_hup(*data, alias_hups)) { + if (! find_alias_hup(*data, alias_hups)) { alias_hup_t *hup = (alias_hup_t *)calloc(1, sizeof(alias_hup_t)); if (hup == NULL) { cli_error(CL_EFAIL, "%s: cannot allocate alias structure\n", PACKAGE_NAME); @@ -110,14 +110,14 @@ static errno_t replace_alias(token_t * tokens, unsigned int tokens_start, unsign { errno_t rc = EOK; const size_t input_length = str_size(*line) - str_size(cmd[0]) + str_size(data->value) + 1; - *line = (char *)malloc(input_length); - if (*line == NULL) { + char *newline = (char *)malloc(input_length); + if (newline == NULL) { cli_error(CL_EFAIL, "%s: cannot allocate input structure\n", PACKAGE_NAME); rc = ENOMEM; goto exit; } - *line[0] = '\0'; + newline[0] = '\0'; unsigned int cmd_replace_index = tokens_start; for (unsigned int i = 0; i < tokens_len; i++) { @@ -129,12 +129,13 @@ static errno_t replace_alias(token_t * tokens, unsigned int tokens_start, unsign continue; } - str_append(*line, input_length, data->value); + str_append(newline, input_length, data->value); } else { - str_append(*line, input_length, tokens[i].text); + str_append(newline, input_length, tokens[i].text); } } + *line = newline; exit: return rc; } From 87eba56159df0e5967ae526dac5badb952de96aa Mon Sep 17 00:00:00 2001 From: Manuele Conti Date: Tue, 10 Aug 2021 23:11:01 +0200 Subject: [PATCH 07/12] Fix tokenize command --- uspace/app/bdsh/input.c | 17 +++++++++++++++-- uspace/app/bdsh/tok.c | 4 ++-- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/uspace/app/bdsh/input.c b/uspace/app/bdsh/input.c index c5eb61857f..4af95aeecf 100644 --- a/uspace/app/bdsh/input.c +++ b/uspace/app/bdsh/input.c @@ -192,6 +192,8 @@ static errno_t process_input_nohup(cliuser_t *usr, list_t *alias_hups, size_t co } cmd_argc = tokens_length; + unsigned wait_from = 0; + unsigned wait_to = 0; for (i = 0, pipe_count = 0; i < tokens_length; i++) { switch (tokens[i].type) { case TOKTYPE_PIPE: @@ -201,15 +203,26 @@ static errno_t process_input_nohup(cliuser_t *usr, list_t *alias_hups, size_t co break; case TOKTYPE_RDIN: - redir_from = tokens[i + 1].text; + wait_from = 1; cmd_argc = i; break; case TOKTYPE_RDOU: - redir_to = tokens[i + 1].text; + wait_to = 1; cmd_argc = i; break; + case TOKTYPE_TEXT: + if (wait_from) { + redir_from = tokens[i].text; + wait_from = 0; + } + if (wait_to) { + redir_to = tokens[i].text; + wait_to = 0; + } + break; + default: break; } diff --git a/uspace/app/bdsh/tok.c b/uspace/app/bdsh/tok.c index 387f089dbc..9c0cc7f2dd 100644 --- a/uspace/app/bdsh/tok.c +++ b/uspace/app/bdsh/tok.c @@ -151,7 +151,7 @@ errno_t tok_tokenize(tokenizer_t *tok, size_t *tokens_length) if (rc != EOK) { return rc; } - tok_get_char(tok); + rc = tok_push_token(tok); if (rc != EOK) { return rc; @@ -170,7 +170,7 @@ errno_t tok_tokenize(tokenizer_t *tok, size_t *tokens_length) if (rc != EOK) { return rc; } - tok_get_char(tok); + rc = tok_push_token(tok); if (rc != EOK) { return rc; From 5ec0e42ef9367955d367aefb309f5719cb34c124 Mon Sep 17 00:00:00 2001 From: Manuele Conti Date: Thu, 12 Aug 2021 01:14:20 +0200 Subject: [PATCH 08/12] Add check redirect command --- uspace/app/bdsh/input.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/uspace/app/bdsh/input.c b/uspace/app/bdsh/input.c index 4af95aeecf..a2aa242ff2 100644 --- a/uspace/app/bdsh/input.c +++ b/uspace/app/bdsh/input.c @@ -226,12 +226,18 @@ static errno_t process_input_nohup(cliuser_t *usr, list_t *alias_hups, size_t co default: break; } + if (pipe_count > MAX_PIPES) { rc = ENOTSUP; goto finit; } } + if (wait_from || wait_to) { + printf("Parse error near `\\n'\n"); + goto finit; + } + unsigned int cmd_token_start = 0; unsigned int cmd_token_end = cmd_argc; From 5582f285693af599bb3e036c15b93fcde5ab5902 Mon Sep 17 00:00:00 2001 From: Manuele Conti Date: Mon, 16 Aug 2021 10:01:23 +0200 Subject: [PATCH 09/12] CStyle --- uspace/app/bdsh/input.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/uspace/app/bdsh/input.c b/uspace/app/bdsh/input.c index a2aa242ff2..1854cde6d0 100644 --- a/uspace/app/bdsh/input.c +++ b/uspace/app/bdsh/input.c @@ -106,7 +106,7 @@ static errno_t find_alias(char **cmd, list_t *alias_hups, alias_t **data) return rc; } -static errno_t replace_alias(token_t * tokens, unsigned int tokens_start, unsigned int tokens_len, alias_t *data, char **cmd, char **line) +static errno_t replace_alias(token_t *tokens, unsigned int tokens_start, unsigned int tokens_len, alias_t *data, char **cmd, char **line) { errno_t rc = EOK; const size_t input_length = str_size(*line) - str_size(cmd[0]) + str_size(data->value) + 1; From d6a982489f5ac8b795d611169cf936ce6d9a1058 Mon Sep 17 00:00:00 2001 From: Manuele Conti Date: Wed, 18 Aug 2021 21:52:53 +0200 Subject: [PATCH 10/12] Changing method to create a temporary file using tmpname function like JSvoboda suggestions. --- uspace/app/bdsh/input.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/uspace/app/bdsh/input.c b/uspace/app/bdsh/input.c index 1854cde6d0..3dfef99231 100644 --- a/uspace/app/bdsh/input.c +++ b/uspace/app/bdsh/input.c @@ -199,7 +199,7 @@ static errno_t process_input_nohup(cliuser_t *usr, list_t *alias_hups, size_t co case TOKTYPE_PIPE: pipe_pos[pipe_count++] = i; cmd_argc = i; - redir_to = (char *)"/tmp/pipe"; + redir_to = tmpnam(NULL); break; case TOKTYPE_RDIN: From b64073879eb2e573f0761f49da42da1b6da8214c Mon Sep 17 00:00:00 2001 From: Manuele Conti Date: Thu, 19 Aug 2021 00:12:33 +0200 Subject: [PATCH 11/12] Fix redirect using pipe --- uspace/app/bdsh/input.c | 59 ++++++++++++++++++++++++----------------- 1 file changed, 34 insertions(+), 25 deletions(-) diff --git a/uspace/app/bdsh/input.c b/uspace/app/bdsh/input.c index 3dfef99231..21f33780c6 100644 --- a/uspace/app/bdsh/input.c +++ b/uspace/app/bdsh/input.c @@ -199,7 +199,7 @@ static errno_t process_input_nohup(cliuser_t *usr, list_t *alias_hups, size_t co case TOKTYPE_PIPE: pipe_pos[pipe_count++] = i; cmd_argc = i; - redir_to = tmpnam(NULL); + redir_from = redir_to = tmpnam(NULL); break; case TOKTYPE_RDIN: @@ -250,25 +250,6 @@ static errno_t process_input_nohup(cliuser_t *usr, list_t *alias_hups, size_t co FILE *from = NULL; FILE *to = NULL; - if (redir_from) { - from = fopen(redir_from, "r"); - if (from == NULL) { - printf("Cannot open file %s\n", redir_from); - rc = errno; - goto finit_with_files; - } - new_iostate.stdin = from; - } - - if (redir_to) { - to = fopen(redir_to, "w"); - if (to == NULL) { - printf("Cannot open file %s\n", redir_to); - rc = errno; - goto finit_with_files; - } - new_iostate.stdout = to; - } for (unsigned p = 0; p <= pipe_count; p++) { /* Convert tokens of the command to string array */ @@ -301,12 +282,32 @@ static errno_t process_input_nohup(cliuser_t *usr, list_t *alias_hups, size_t co goto finit; } - if (p < pipe_count) { - new_iostate.stdout = to; + if (redir_to) { + if ((p < pipe_count) || (pipe_count == 0)) { + to = fopen(redir_to, "w"); + if (to == NULL) { + printf( + "Cannot open file %s redirect to\n", + redir_to); + rc = errno; + goto finit_with_files; + } + new_iostate.stdout = to; + } } - if (p && p == pipe_count) { - fseek(to, 0, SEEK_SET); - new_iostate.stdin = to; + + if (redir_from) { + if ((p && p == pipe_count) || (pipe_count == 0)) { + from = fopen(redir_from, "r"); + if (from == NULL) { + printf("Cannot open file %s redirect " + "from\n", + redir_from); + rc = errno; + goto finit_with_files; + } + new_iostate.stdin = from; + } } if (run_command(cmd, usr, &new_iostate) == 0) { @@ -315,6 +316,14 @@ static errno_t process_input_nohup(cliuser_t *usr, list_t *alias_hups, size_t co rc = EINVAL; } + if (to) { + fclose(to); + to = NULL; + } + if (from) { + fclose(from); + from = NULL; + } // Restore the Standard Input, Output and Error file descriptors new_iostate.stdin = stdin; new_iostate.stdout = stdout; From 12f5a1be9347ae8a4c87b277c612c58c71f5f5fc Mon Sep 17 00:00:00 2001 From: Manuele Conti Date: Sun, 22 Aug 2021 10:20:53 +0200 Subject: [PATCH 12/12] Remove debug code and CStyle --- uspace/app/bdsh/input.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/uspace/app/bdsh/input.c b/uspace/app/bdsh/input.c index 21f33780c6..76d0c26c83 100644 --- a/uspace/app/bdsh/input.c +++ b/uspace/app/bdsh/input.c @@ -286,9 +286,7 @@ static errno_t process_input_nohup(cliuser_t *usr, list_t *alias_hups, size_t co if ((p < pipe_count) || (pipe_count == 0)) { to = fopen(redir_to, "w"); if (to == NULL) { - printf( - "Cannot open file %s redirect to\n", - redir_to); + printf("Cannot open file %s\n", redir_to); rc = errno; goto finit_with_files; } @@ -300,9 +298,7 @@ static errno_t process_input_nohup(cliuser_t *usr, list_t *alias_hups, size_t co if ((p && p == pipe_count) || (pipe_count == 0)) { from = fopen(redir_from, "r"); if (from == NULL) { - printf("Cannot open file %s redirect " - "from\n", - redir_from); + printf("Cannot open file %s\n", redir_from); rc = errno; goto finit_with_files; }