Skip to content

Commit

Permalink
Merge branch 'main' into dimadem_fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
dimadem authored Aug 29, 2024
2 parents e0aa44b + faf0b34 commit 76bd635
Show file tree
Hide file tree
Showing 17 changed files with 144 additions and 102 deletions.
2 changes: 1 addition & 1 deletion inc/signals.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
# include "shell.h"

/* signals */
void signal_reset_prompt(int signo);
void signal_reset_prompt(int signo);
void set_signals_interactive(void);
void signal_print_newline(int signal);
void sigquit_ignore(void);
Expand Down
4 changes: 4 additions & 0 deletions inc/tokens.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ typedef struct s_loop_data
t_ast *tree;
} t_loop_data;

void final_quote_removal(int arg_count, t_ast *command_node);
char *expand_env_var(char *arg, t_ms_data *data);
void post_process_command_args(t_ast *command_node, int arg_count, \
t_ms_data *data);
void add_node(t_token **head, char *str);
void print_stack(t_token **stack);
void free_stack(t_token **stack);
Expand Down
2 changes: 1 addition & 1 deletion src/builtins/cd.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ int builtin_cd(t_ms_data *data)
char *home_dir;
char cwd[4096];

printf("builtin_cd\n");
ft_printf("builtin_cd\n");
home_dir = get_env(data->envp, "HOME");
target_dir = (char *)data->args[1];
if (!target_dir)
Expand Down
2 changes: 0 additions & 2 deletions src/builtins/export.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
*/

int builtin_export(t_ms_data *data);
// static bool check_input(t_ms_data *data);
static void add_env(t_ms_data *data);

int builtin_export(t_ms_data *data)
Expand All @@ -47,7 +46,6 @@ int builtin_export(t_ms_data *data)
}
else
add_env(data);
// if (check_input(data))
return (0);
}

Expand Down
2 changes: 1 addition & 1 deletion src/execute/execute.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ int execute_ast(t_ast *node, t_ms_data *data)
if (node->type == PIPE)
return (builtin_pipe(node, data));
else if (node->type == ENV_VAR)
printf(BLU"ENV_VAR\n"RESET);
ft_printf(BLU"ENV_VAR\n"RESET);
else if (node->type == REDIR_IN)
return (redirect_in(node, data));
else if (node->type == REDIR_OUT)
Expand Down
15 changes: 14 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ void main_loop(t_ms_data *data, t_loop_data *loop_data)
{
int status;

print_ast_root(loop_data->tree);
status = execute_ast(loop_data->tree, data);
if (status_handler(status, loop_data))
{
handle_io_fd(data);
loop_cleanup(loop_data->trimmed_input, loop_data->tokens, \
loop_data->prompt, loop_data->tree);
}
}

void main_loop(t_ms_data *data, t_loop_data *loop_data)
{
while (1)
{
loop_data->prompt = generate_prompt(data);
Expand All @@ -44,7 +56,8 @@ void main_loop(t_ms_data *data, t_loop_data *loop_data)
}
make_history(loop_data->input);
loop_data->trimmed_input = trim_input(loop_data->input);
input_error_checks(loop_data->trimmed_input);
if (input_error_checks(loop_data->trimmed_input))
continue ;
loop_data->tokens = tokenise(loop_data->trimmed_input);
loop_data->tree = parse_tokens(&loop_data->tokens, data);
print_ast_root(loop_data->tree);
Expand Down
40 changes: 0 additions & 40 deletions src/parser/heredoc.c

This file was deleted.

4 changes: 2 additions & 2 deletions src/parser/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: rocky <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/12 19:35:45 by rocky #+# #+# */
/* Updated: 2024/07/11 18:44:23 by dmdemirk ### ########.fr */
/* Updated: 2024/08/28 20:11:50 by rocky ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -113,4 +113,4 @@ t_ast *manage_pipe(t_token **tokens, t_ms_data *data)
*tokens = next_token;
}
return (manage_redirs(&tmp, data));
}
}
83 changes: 83 additions & 0 deletions src/parser/parser_helper_helpers.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parser_helper_helpers.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rocky <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/28 20:36:33 by rocky #+# #+# */
/* Updated: 2024/08/28 20:36:39 by rocky ### ########.fr */
/* */
/* ************************************************************************** */

#include "tokens.h"

void final_quote_removal(int arg_count, t_ast *command_node)
{
int i;
size_t len;
char *arg;
char *trimmed_arg;

i = 0;
while (i < arg_count)
{
arg = command_node->args[i];
len = ft_strlen(arg);
if ((arg[0] == '"' && arg[len - 1] == '"') || (arg[0] == '\'' \
&& arg[len - 1] == '\''))
{
trimmed_arg = ft_strndup(arg + 1, len - 2);
free(command_node->args[i]);
command_node->args[i] = trimmed_arg;
}
i++;
}
}

char *expand_env_var(char *arg, t_ms_data *data)
{
char *env_value;

if (ft_strcmp(arg, "$?") == 0)
return (ft_itoa(data->exit_status));
else if (arg[0] == '$')
{
if (arg[ft_strlen(arg) - 1] == '"')
arg[ft_strlen(arg) - 1] = '\0';
env_value = get_env(data->envp, arg + 1);
if (env_value)
return (ft_strdup(env_value));
}
return (ft_strdup(arg));
}

void post_process_command_args(t_ast *command_node, int arg_count, \
t_ms_data *data)
{
int i;
char *arg;
char *processed_arg;

i = 0;
while (i < arg_count)
{
arg = command_node->args[i];
processed_arg = NULL;
if (arg[0] == '$' || (arg[0] == '"' && arg[1] == '$'))
{
if (arg[0] == '"')
processed_arg = expand_env_var(arg + 1, data);
else
processed_arg = expand_env_var(arg, data);
}
else if (arg[0] == '\'')
processed_arg = ft_strdup(arg);
else
processed_arg = ft_strdup(arg);
free(command_node->args[i]);
command_node->args[i] = processed_arg;
i++;
}
final_quote_removal(arg_count, command_node);
}
4 changes: 2 additions & 2 deletions src/parser/parser_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
/* +:+ +:+ +:+ */
/* By: rocky <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/14 10:45:48 by rocky #+# #+# */
/* Updated: 2024/06/17 11:06:25 by rocky ### ########.fr */
/* Created: 2024/08/28 20:13:42 by rocky #+# #+# */
/* Updated: 2024/08/28 20:14:33 by rocky ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down
2 changes: 1 addition & 1 deletion src/parser/tokeniser.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ void print_tokens(t_token *tokens)
token = tokens;
while (token != NULL)
{
printf("input[%d] -> %s \n", i, token->data);
ft_printf("input[%d] -> %s \n", i, token->data);
token = token->next;
i++;
}
Expand Down
34 changes: 17 additions & 17 deletions src/test/env/env_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void test_unset_env(t_ms_data *data);

void env_tests(t_ms_data *data, char **envp)
{
printf("\nENV TESTS\n");
ft_printf("\nENV TESTS\n");
test_init_env(data, envp);
test_get_env(data, envp);
test_set_env(data, envp);
Expand All @@ -48,10 +48,10 @@ void test_init_env(t_ms_data *data, char **envp)
while (test_envp[test_envp_count])
test_envp_count++;
assert(envp_count == test_envp_count);
printf("\033[0m");
printf("\033[0;32m");
printf("init_env -> OK\n");
printf("\033[0m");
ft_printf("\033[0m");
ft_printf("\033[0;32m");
ft_printf("init_env -> OK\n");
ft_printf("\033[0m");
}

void test_get_env(t_ms_data *data, char **envp)
Expand All @@ -63,10 +63,10 @@ void test_get_env(t_ms_data *data, char **envp)
value = get_env(data->envp, "LOGNAME");
assert(value != NULL);
assert(ft_strcmp(value, getenv("LOGNAME")) == 0);
printf("\033[0m");
printf("\033[0;32m");
printf("get_env -> OK\n");
printf("\033[0m");
ft_printf("\033[0m");
ft_printf("\033[0;32m");
ft_printf("get_env -> OK\n");
ft_printf("\033[0m");
}

void test_set_env(t_ms_data *data, char **envp)
Expand All @@ -79,10 +79,10 @@ void test_set_env(t_ms_data *data, char **envp)
value = get_env(data->envp, "TEST");
assert(value != NULL);
assert(ft_strcmp(value, "test") == 0);
printf("\033[0m");
printf("\033[0;32m");
printf("set_env -> OK\n");
printf("\033[0m");
ft_printf("\033[0m");
ft_printf("\033[0;32m");
ft_printf("set_env -> OK\n");
ft_printf("\033[0m");
}

void test_unset_env(t_ms_data *data)
Expand All @@ -96,8 +96,8 @@ void test_unset_env(t_ms_data *data)
unset_env(&data->envp, "TEST");
value = get_env(data->envp, "TEST");
assert(value == NULL);
printf("\033[0m");
printf("\033[0;32m");
printf("unset_env -> OK\n");
printf("\033[0m");
ft_printf("\033[0m");
ft_printf("\033[0;32m");
ft_printf("unset_env -> OK\n");
ft_printf("\033[0m");
}
2 changes: 1 addition & 1 deletion src/test/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ int main(int argc, char **argv, char **envp)

if (argc > 1)
{
printf("Usage: %s\n", argv[0]);
ft_printf("Usage: %s\n", argv[0]);
exit(EXIT_FAILURE);
}
pipe_tests(&data);
Expand Down
10 changes: 5 additions & 5 deletions src/test/pipe/pipe_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ void pipe_tests(t_ms_data *data);

void pipe_tests(t_ms_data *data)
{
printf("\nPIPE TESTS\n");
ft_printf("\nPIPE TESTS\n");
builtin_pipe_test(data);
printf("\033[0m");
printf("\033[0;32m");
printf("pipe_test -> OK\n");
printf("\033[0m");
ft_printf("\033[0m");
ft_printf("\033[0;32m");
ft_printf("pipe_test -> OK\n");
ft_printf("\033[0m");
}

void builtin_pipe_test(t_ms_data *data)
Expand Down
2 changes: 1 addition & 1 deletion src/utils/initialise.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ void initialise(int argc, char **argv)
{
if (argc > 1)
{
printf("Usage: %s\n", argv[0]);
ft_printf("Usage: %s\n", argv[0]);
exit(EXIT_FAILURE);
}
read_history(HISTORY_PATH);
Expand Down
Loading

0 comments on commit 76bd635

Please sign in to comment.