diff --git a/src/ast/tokeniser.c b/src/ast/tokeniser.c index 448f1c6..c2fa421 100644 --- a/src/ast/tokeniser.c +++ b/src/ast/tokeniser.c @@ -79,12 +79,50 @@ bool check_closable(token_t actual_token) return false; } +static +token_t handle_token_type(ast_ctx_t *ctx) +{ + for (size_t i = 0; i < TOKENS_LIST_SZ; i++) { + if (u_strncmp(ctx->str, TOKENS_LIST[i].str, TOKENS_LIST[i].sz) == 0) { + U_DEBUG("Token %-14s [%.*s]\n", TOKENS_LIST[i].name, + (int)TOKENS_LIST[i].sz, ctx->str); + ctx->str += TOKENS_LIST[i].sz; + return (token_t){ TOKENS_LIST[i].type, + ctx->str - TOKENS_LIST[i].sz, TOKENS_LIST[i].sz }; + } + } + return (token_t){0, NULL, 0}; +} + +static +bool compare_to_close(ast_ctx_t *ctx, token_t acutal_tok) +{ + token_type_t token = 0; + + if (!*ctx->str && !(*ctx->str + 1)) + return false; + for (size_t i = 0; i < TOKENS_LIST_SZ; i++) { + if (u_strncmp(ctx->str, TOKENS_LIST[i].str, + TOKENS_LIST[i].sz) == 0) { + token = TOKENS_LIST[i].type; + break; + } + } + if (u_strncmp(ctx->str, acutal_tok.str, acutal_tok.sz) == 0){ + if (acutal_tok.type & (T_QUOTES | T_DQUOTES) + && !isblank(*(ctx->str + 1)) && token == 0) + return false; + return true; + } + return false; +} + static void get_arg_token(ast_ctx_t *ctx, int *found_token, token_t acutal_tok) { if (check_closable(acutal_tok)){ ctx->str++; - if (u_strncmp(ctx->str, acutal_tok.str, acutal_tok.sz) == 0){ + if (compare_to_close(ctx, acutal_tok)){ *found_token = 1; ctx->str++; } @@ -101,21 +139,6 @@ void get_arg_token(ast_ctx_t *ctx, int *found_token, token_t acutal_tok) ctx->str++; } -static -token_t handle_token_type(ast_ctx_t *ctx) -{ - for (size_t i = 0; i < TOKENS_LIST_SZ; i++) { - if (u_strncmp(ctx->str, TOKENS_LIST[i].str, TOKENS_LIST[i].sz) == 0) { - U_DEBUG("Token %-14s [%.*s]\n", TOKENS_LIST[i].name, - (int)TOKENS_LIST[i].sz, ctx->str); - ctx->str += TOKENS_LIST[i].sz; - return (token_t){ TOKENS_LIST[i].type, - ctx->str - TOKENS_LIST[i].sz, TOKENS_LIST[i].sz }; - } - } - return (token_t){0, NULL, 0}; -} - void format_for_closable(ast_ctx_t *ctx, token_t *actual_token) { if (actual_token->type == T_RIGHT_PARENT) diff --git a/src/exec.h b/src/exec.h index 9435b99..0e915af 100644 --- a/src/exec.h +++ b/src/exec.h @@ -53,8 +53,8 @@ int exec_the_args(ef_t *ef, char **args); void exit_child(int sig __attribute__((unused))); int visit_loop(ef_t *ef, ast_t *node); void handle_var_case(ast_t *node, exec_ctx_t *ctx, size_t *i, args_t *args); -char *handle_magic_quotes(ast_t *node, exec_ctx_t *ctx, size_t *i, - args_t *args); +bool handle_magic_quotes(ast_t *node, exec_ctx_t *ctx, + size_t *i, args_t *args); bool handle_quotes(ast_t *node, size_t *i, exec_ctx_t *ctx, args_t *args); char *get_values(exec_ctx_t *ctx, char *key); #endif /* EXEC_H */ diff --git a/src/handle_vars.c b/src/handle_vars.c index f2a88ce..a574c4e 100644 --- a/src/handle_vars.c +++ b/src/handle_vars.c @@ -93,6 +93,26 @@ bool check_parentheses(ast_t *node, size_t *i, exec_ctx_t *ctx, args_t *args) return false; } +static +bool format_quotes(ast_t *node, char be_matched, size_t *i) +{ + char *last_quote = strchr(node->vector.tokens[*i].str, be_matched); + + if (last_quote == NULL) + return (fprintf(stderr, "Unmatched \'%c\'.\n", be_matched), false); + if (isblank(last_quote[1])){ + last_quote[0] = '\0'; + return true; + } else + node->vector.tokens[*i].str[node->vector.tokens[*i].sz - 1] = '\0'; + memmove(&last_quote[0], &last_quote[1], strlen(last_quote)); + last_quote = strchr(node->vector.tokens[*i].str, be_matched); + if (strchr(node->vector.tokens[*i].str, be_matched)) + return (fprintf(stderr, "Unmatched \'%c\'.\n", be_matched), false); + node->vector.tokens[*i].str[node->vector.tokens[*i].sz - 2] = '\0'; + return true; +} + static bool check_quotes(ast_t *node, size_t *i, exec_ctx_t *ctx, args_t *args) { @@ -100,16 +120,15 @@ bool check_quotes(ast_t *node, size_t *i, exec_ctx_t *ctx, args_t *args) if (!strchr("\'\"`", node->vector.tokens[*i].str[0])) return true; - if (strlen(node->vector.tokens[*i].str) == 1 || be_matched != - node->vector.tokens[*i].str[node->vector.tokens[*i].sz - 1]) + if (strlen(node->vector.tokens[*i].str) == 1 || + !strchr(&node->vector.tokens[*i].str[1], be_matched)) return (fprintf(stderr, "Unmatched \'%c\'.\n", be_matched), true); - node->vector.tokens[*i].str[node->vector.tokens[*i].sz - 1] = '\0'; memmove(&node->vector.tokens[*i].str[0], - &node->vector.tokens[*i].str[1], strlen(node->vector.tokens[*i].str)); - if (be_matched == '`'){ - handle_magic_quotes(node, ctx, i, args); - return false; - } + &node->vector.tokens[*i].str[1], node->vector.tokens[*i].sz); + if (be_matched == '`') + return handle_magic_quotes(node, ctx, i, args); + if (!format_quotes(node, be_matched, i)) + return true; if (be_matched == '\"') return handle_quotes(node, i, ctx, args); args->args[args->sz] = strdup(node->vector.tokens[*i].str); diff --git a/src/magic_quotes.c b/src/magic_quotes.c index 3c3d44c..11ed5a0 100644 --- a/src/magic_quotes.c +++ b/src/magic_quotes.c @@ -106,17 +106,16 @@ void get_output(int fd[2], args_t *args) put_output(fd, args, to_return, sz); } -char *handle_magic_quotes(ast_t *node, exec_ctx_t *ctx, +bool handle_magic_quotes(ast_t *node, exec_ctx_t *ctx, size_t *i, args_t *args) { - char *to_return = 0; int fd[2]; if (pipe(fd) == -1) - return NULL; + return true; exec_magic(node, ctx, i, fd); close(fd[1]); get_output(fd, args); close(fd[0]); - return to_return; + return false; }