Skip to content

Commit

Permalink
Merge pull request #89 from ErwannLesech/variable-part2.5
Browse files Browse the repository at this point in the history
fix<execute>: fix mem leaks on environment variable and fix if variab…
  • Loading branch information
Nimu93 authored Jan 22, 2024
2 parents 9b67e8d + 92a200e commit febb852
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/execute/ast_eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ int ast_eval_simple_command(struct ast_node *node)
}
return return_val;
}
char *command = node->children[0]->value;
char *command = handle_word(node->children[0]);
for (size_t i = 0; i < 3; i++)
{
if (strcmp(command, builtin[i].name) == 0)
Expand Down
23 changes: 13 additions & 10 deletions src/execute/utils/ast_variable.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,16 @@ void set_variable(char *key, char *value)
// hash_map_dump(variables);
}

char *get_environment_variable(char *key)
{
char *value = hash_map_get(variables, key);
if (value == NULL)
{
return "";
}
return value;
}

/**
* \brief Get a variable from the hash map.
* \param key The key of the variable.
Expand All @@ -86,23 +96,16 @@ void set_variable(char *key, char *value)
char *get_variable(char *key)
{
key++;
// gerer les variables envirovment
for (int i = 0; environment[i].name[0] != '\0'; i++)
{
//printf("key:%s\n", key);
if (strcmp(key, environment[i].name) == 0)
{
//printf("key:%s\n", key);
return environment[i].fun(key);
}
}
char *value = hash_map_get(variables, key);
if (value == NULL)
{
// printf("key:%s\n", key);
return "";
}
// printf("value:%s$\n", value);
// printf("key:%s\n", key);
return value;
return get_environment_variable(key);
}

/**
Expand Down
17 changes: 10 additions & 7 deletions src/execute/utils/environment_variable.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ char *star_fun()
char *dollar_fun()
{
pid_t pid = getpid();
char *pid_str = malloc(sizeof(char) * 10);
char pid_str[10];
sprintf(pid_str, "%d", pid);
return pid_str;
set_variable("$", pid_str);
return get_environment_variable("$");
}

char *quest_fun()
Expand All @@ -47,17 +48,19 @@ char *random_fun()
int random_number = rand();
random_number %= 32768;
char *random_number_str = malloc(sizeof(char) * 10);
sprintf(random_number_str, "%d", random_number);

return random_number_str;
sprintf(random_number_str, "%d", random_number);
set_variable("RANDOM", random_number_str);
free(random_number_str);
return get_environment_variable("RANDOM");
}

char *uid_fun()
{
uid_t user_id = getuid();
char *user_id_str = malloc(sizeof(char) * 10);
char user_id_str[10];
sprintf(user_id_str, "%d", user_id);
return user_id_str;
set_variable("UID", user_id_str);
return get_environment_variable("UID");
}

char *oldpwd_fun()
Expand Down
4 changes: 4 additions & 0 deletions src/execute/utils/environment_variable.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#ifndef ENVIRONMENT_VARIABLE_H
#define ENVIRONMENT_VARIABLE_H

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();
Expand Down
2 changes: 2 additions & 0 deletions src/execute/utils/loop.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,10 @@ int isDirectory(const char *path)
struct stat pathStat;
if (stat(cleanedString, &pathStat) != 0)
{
free(cleanedString);
return 0;
}
free(cleanedString);
return S_ISDIR(
pathStat.st_mode); // Vérifiez si le chemin pointe vers un répertoire
}
Expand Down
29 changes: 19 additions & 10 deletions src/parser/parser_element.c
Original file line number Diff line number Diff line change
Expand Up @@ -165,21 +165,30 @@ struct ast_node *simple_command(struct lexer *lexer)
return current;
}
}
struct ast_node *variable = parse_variable(lexer);
if (parser_peek(lexer) == TOKEN_WORD
|| parser_peek(lexer) == TOKEN_WORD_DOUBLE_QUOTE)
|| parser_peek(lexer) == TOKEN_WORD_DOUBLE_QUOTE
|| variable != NULL)
{
char *value = lexer_peek(lexer).data;
if (strcmp(value, "while") == 0 || strcmp(value, "until") == 0
|| strcmp(value, "for") == 0 || strcmp(value, "do") == 0)
if (variable != NULL)
{
ast_append(current, variable);
}
else
{
char *value = lexer_peek(lexer).data;
if (strcmp(value, "while") == 0 || strcmp(value, "until") == 0
|| strcmp(value, "for") == 0 || strcmp(value, "do") == 0)
{
free(value);
ast_free(current);
return NULL;
}
free(value);
ast_free(current);
return NULL;
struct ast_node *new = ast_node_word(lexer_peek(lexer).data);
parser_pop(lexer);
ast_append(current, new);
}
free(value);
struct ast_node *new = ast_node_word(lexer_peek(lexer).data);
parser_pop(lexer);
ast_append(current, new);
struct ast_node *curr = element(lexer);
while (curr != NULL)
{
Expand Down

0 comments on commit febb852

Please sign in to comment.