Skip to content

Commit

Permalink
Merge pull request #101 from ErwannLesech/fix-clang
Browse files Browse the repository at this point in the history
fix<clang>: clang-tidy
  • Loading branch information
Nimu93 authored Jan 27, 2024
2 parents 3116fd0 + ab12131 commit dd594f0
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 77 deletions.
20 changes: 10 additions & 10 deletions src/execute/utils/environment_variable.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@
#include <time.h>
#include <unistd.h>

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];
Expand All @@ -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();
Expand All @@ -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];
Expand All @@ -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 "";
}
20 changes: 10 additions & 10 deletions src/execute/utils/environment_variable.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
124 changes: 67 additions & 57 deletions src/parser/parser_condition.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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)
{
Expand All @@ -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);
}
Expand Down
12 changes: 12 additions & 0 deletions src/parser/parser_functionnal.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit dd594f0

Please sign in to comment.