diff --git a/src/execute/utils/environment_variable.c b/src/execute/utils/environment_variable.c index dd7597ff..c9476a19 100644 --- a/src/execute/utils/environment_variable.c +++ b/src/execute/utils/environment_variable.c @@ -8,17 +8,17 @@ #include #include -char *at_fun() +char *at_fun(void) { return ""; } -char *star_fun() +char *star_fun(void) { return ""; } -char *dollar_fun() +char *dollar_fun(void) { pid_t pid = getpid(); char pid_str[10]; @@ -27,22 +27,22 @@ char *dollar_fun() return get_environment_variable("$"); } -char *quest_fun() +char *quest_fun(void) { return ""; } -char *number_fun() +char *number_fun(void) { return ""; } -char *sharp_fun() +char *sharp_fun(void) { return ""; } -char *random_fun() +char *random_fun(void) { srand(time(NULL)); int random_number = rand(); @@ -54,7 +54,7 @@ char *random_fun() return get_environment_variable("RANDOM"); } -char *uid_fun() +char *uid_fun(void) { uid_t user_id = getuid(); char user_id_str[10]; @@ -63,12 +63,12 @@ char *uid_fun() return get_environment_variable("UID"); } -char *oldpwd_fun() +char *oldpwd_fun(void) { return ""; } -char *pwd_fun() +char *pwd_fun(void) { return ""; } diff --git a/src/execute/utils/environment_variable.h b/src/execute/utils/environment_variable.h index 779ceb34..c9f364d7 100644 --- a/src/execute/utils/environment_variable.h +++ b/src/execute/utils/environment_variable.h @@ -5,15 +5,15 @@ void set_variable(char *key, char *value); char *get_variable(char *key); char *get_environment_variable(char *key); -char *at_fun(); -char *dollar_fun(); -char *star_fun(); -char *quest_fun(); -char *number_fun(); -char *sharp_fun(); -char *uid_fun(); -char *random_fun(); -char *oldpwd_fun(); -char *pwd_fun(); +char *at_fun(void); +char *dollar_fun(void); +char *star_fun(void); +char *quest_fun(void); +char *number_fun(void); +char *sharp_fun(void); +char *uid_fun(void); +char *random_fun(void); +char *oldpwd_fun(void); +char *pwd_fun(void); #endif /* ! ENVIRONMENT_VARIABLE_H */ diff --git a/src/parser/parser_condition.c b/src/parser/parser_condition.c index 37614cdb..7b1ddcbb 100644 --- a/src/parser/parser_condition.c +++ b/src/parser/parser_condition.c @@ -8,18 +8,6 @@ #include "parser.h" -struct ast_node *shell_command(struct lexer *lexer) -{ - struct ast_node *current = rule_if(lexer); - if (current == NULL) - current = rule_while(lexer); - if (current == NULL) - current = rule_until(lexer); - if (current == NULL) - current = rule_for(lexer); - return current; -} - struct ast_node *rule_if(struct lexer *lexer) { struct ast_node *current = ast_node_new(AST_CONDITION); @@ -212,6 +200,68 @@ struct ast_node *rule_until(struct lexer *lexer) return NULL; } +void for_2(struct lexer *lexer, struct ast_node *current) +{ + parser_pop(lexer); + char *value = lexer_peek(lexer).data; + while ((parser_peek(lexer) == TOKEN_WORD + || parser_peek(lexer) == TOKEN_VARIABLE) + && strcmp(value, "do") != 0) + { + struct ast_node *condition = ast_node_new(AST_WORD); + condition->value = value; + ast_append(current, condition); + parser_pop(lexer); + value = lexer_peek(lexer).data; + } + free(value); +} + +void eol(struct lexer *lexer) +{ + while (parser_peek(lexer) == TOKEN_EOL) + { + parser_pop(lexer); + } +} + +void for_3(struct lexer *lexer, struct ast_node *current) +{ + char *value = lexer_peek(lexer).data; + struct ast_node *var = ast_node_new(AST_WORD); + var->value = value; + ast_append(current, var); + parser_pop(lexer); +} + +struct ast_node *for_4(struct lexer *lexer, struct ast_node *current) +{ + char *value = lexer_peek(lexer).data; + if (strcmp(value, "do") == 0) + { + free(value); + parser_pop(lexer); + struct ast_node *response = compound_list(lexer); + if (response == NULL) + { + ast_free(current); + return NULL; + } + ast_append(current, response); + value = lexer_peek(lexer).data; + + if (strcmp(value, "done") == 0) + { + free(value); + parser_pop(lexer); + return current; + } + } + free(value); + ast_free(current); + return NULL; +} + struct ast_node *rule_for(struct lexer *lexer) { struct ast_node *current = ast_node_new(AST_FOR); @@ -227,38 +277,19 @@ struct ast_node *rule_for(struct lexer *lexer) { goto ERROR; } - value = lexer_peek(lexer).data; - struct ast_node *var = ast_node_new(AST_WORD); - var->value = value; - ast_append(current, var); - parser_pop(lexer); + for_3(lexer, current); if (parser_peek(lexer) == TOKEN_SEMICOLON) { goto END; } - while (parser_peek(lexer) == TOKEN_EOL) - { - parser_pop(lexer); - } + eol(lexer); value = lexer_peek(lexer).data; if (strcmp(value, "in") == 0) { free(value); - parser_pop(lexer); - value = lexer_peek(lexer).data; - while ((parser_peek(lexer) == TOKEN_WORD - || parser_peek(lexer) == TOKEN_VARIABLE) - && strcmp(value, "do") != 0) - { - struct ast_node *condition = ast_node_new(AST_WORD); - condition->value = value; - ast_append(current, condition); - parser_pop(lexer); - value = lexer_peek(lexer).data; - } - free(value); + for_2(lexer, current); if (parser_peek(lexer) != TOKEN_SEMICOLON && parser_peek(lexer) != TOKEN_EOL) { @@ -272,29 +303,8 @@ struct ast_node *rule_for(struct lexer *lexer) } END: - while (parser_peek(lexer) == TOKEN_EOL) - { - parser_pop(lexer); - } - - value = lexer_peek(lexer).data; - if (strcmp(value, "do") == 0) - { - free(value); - parser_pop(lexer); - struct ast_node *response = compound_list(lexer); - if (response == NULL) - goto ERROR; - ast_append(current, response); - value = lexer_peek(lexer).data; - - if (strcmp(value, "done") == 0) - { - free(value); - parser_pop(lexer); - return current; - } - } + eol(lexer); + return for_4(lexer, current); } free(value); } diff --git a/src/parser/parser_functionnal.c b/src/parser/parser_functionnal.c index 28043f21..9fc2cd36 100644 --- a/src/parser/parser_functionnal.c +++ b/src/parser/parser_functionnal.c @@ -8,6 +8,18 @@ #include "parser.h" +struct ast_node *shell_command(struct lexer *lexer) +{ + struct ast_node *current = rule_if(lexer); + if (current == NULL) + current = rule_while(lexer); + if (current == NULL) + current = rule_until(lexer); + if (current == NULL) + current = rule_for(lexer); + return current; +} + struct ast_node *prefix(struct lexer *lexer) { if (parser_peek(lexer) == TOKEN_WORD_ASSIGNMENT)