Compare commits
9 Commits
fix/builti
...
001709139b
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
001709139b | ||
|
|
73ed56aa16 | ||
| 5852e8618f | |||
|
|
ba40670ace | ||
|
|
dd6101edec | ||
| e2b734cf0c | |||
| 32b3bd72b5 | |||
| c95703b42b | |||
| f4cfae1107 |
9656
docs/mstest_m_failed_tests_report.txt
Normal file
9656
docs/mstest_m_failed_tests_report.txt
Normal file
File diff suppressed because it is too large
Load Diff
9178
docs/mstest_m_failed_tests_report_filtered.txt
Normal file
9178
docs/mstest_m_failed_tests_report_filtered.txt
Normal file
File diff suppressed because it is too large
Load Diff
8656
docs/mstest_m_failed_tests_report_mandatory_only.txt
Normal file
8656
docs/mstest_m_failed_tests_report_mandatory_only.txt
Normal file
File diff suppressed because it is too large
Load Diff
26
docs/mstest_m_removed_extra_tests.txt
Normal file
26
docs/mstest_m_removed_extra_tests.txt
Normal file
@@ -0,0 +1,26 @@
|
||||
Tests eliminados del reporte por ser extras (\ y/o ~):
|
||||
|
||||
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/0_compare_parsing.sh:52 | motivo: \
|
||||
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/0_compare_parsing.sh:54 | motivo: \
|
||||
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/0_compare_parsing.sh:56 | motivo: \
|
||||
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/0_compare_parsing.sh:58 | motivo: \
|
||||
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/0_compare_parsing.sh:60 | motivo: \
|
||||
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/0_compare_parsing.sh:62 | motivo: \
|
||||
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/0_compare_parsing.sh:64 | motivo: \
|
||||
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_builtins.sh:7 | motivo: ~
|
||||
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_builtins.sh:59 | motivo: \
|
||||
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_builtins.sh:61 | motivo: \
|
||||
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_builtins.sh:63 | motivo: \
|
||||
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_builtins.sh:65 | motivo: \
|
||||
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_builtins.sh:67 | motivo: \
|
||||
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_builtins.sh:69 | motivo: \
|
||||
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_builtins.sh:71 | motivo: \
|
||||
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_builtins.sh:314 | motivo: ~
|
||||
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_builtins.sh:328 | motivo: ~
|
||||
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_builtins.sh:337 | motivo: ~
|
||||
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_builtins.sh:606 | motivo: \, ~
|
||||
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_redirs.sh:208 | motivo: \
|
||||
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/2_path_check.sh:30 | motivo: ~
|
||||
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/8_syntax_errors.sh:19 | motivo: ~
|
||||
|
||||
Total casos eliminados (extras): 22
|
||||
59
docs/mstest_m_removed_extra_tests_mandatory_only.txt
Normal file
59
docs/mstest_m_removed_extra_tests_mandatory_only.txt
Normal file
@@ -0,0 +1,59 @@
|
||||
Tests eliminados del reporte por estar fuera del obligatorio:
|
||||
- \ (barra invertida no especificada)
|
||||
- ~ (tilde expansion no obligatoria)
|
||||
- $"..." (extension bash, no requerida)
|
||||
- ; (separador no obligatorio segun subject)
|
||||
- && / || (bonus)
|
||||
|
||||
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/0_compare_parsing.sh:26 | motivo: $"..." (bash extension)
|
||||
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/0_compare_parsing.sh:28 | motivo: $"..." (bash extension)
|
||||
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/0_compare_parsing.sh:32 | motivo: $"..." (bash extension)
|
||||
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/0_compare_parsing.sh:34 | motivo: $"..." (bash extension)
|
||||
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/0_compare_parsing.sh:52 | motivo: \
|
||||
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/0_compare_parsing.sh:54 | motivo: \
|
||||
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/0_compare_parsing.sh:56 | motivo: \
|
||||
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/0_compare_parsing.sh:58 | motivo: \
|
||||
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/0_compare_parsing.sh:60 | motivo: \
|
||||
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/0_compare_parsing.sh:62 | motivo: \
|
||||
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/0_compare_parsing.sh:64 | motivo: \
|
||||
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/10_parsing_hell.sh:301 | motivo: $"..." (bash extension)
|
||||
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_builtins.sh:7 | motivo: ~
|
||||
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_builtins.sh:31 | motivo: $"..." (bash extension)
|
||||
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_builtins.sh:33 | motivo: $"..." (bash extension)
|
||||
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_builtins.sh:37 | motivo: $"..." (bash extension)
|
||||
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_builtins.sh:39 | motivo: $"..." (bash extension)
|
||||
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_builtins.sh:59 | motivo: \
|
||||
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_builtins.sh:61 | motivo: \
|
||||
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_builtins.sh:63 | motivo: \
|
||||
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_builtins.sh:65 | motivo: \
|
||||
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_builtins.sh:67 | motivo: \
|
||||
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_builtins.sh:69 | motivo: \
|
||||
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_builtins.sh:71 | motivo: \
|
||||
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_builtins.sh:314 | motivo: ~
|
||||
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_builtins.sh:328 | motivo: ~
|
||||
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_builtins.sh:337 | motivo: ~
|
||||
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_builtins.sh:536 | motivo: $"..." (bash extension)
|
||||
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_builtins.sh:538 | motivo: $"..." (bash extension)
|
||||
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_builtins.sh:552 | motivo: ; (no obligatorio)
|
||||
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_builtins.sh:606 | motivo: \, ~
|
||||
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_redirs.sh:208 | motivo: \
|
||||
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_variables.sh:40 | motivo: $"..." (bash extension)
|
||||
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_variables.sh:42 | motivo: $"..." (bash extension)
|
||||
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_variables.sh:44 | motivo: $"..." (bash extension)
|
||||
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_variables.sh:48 | motivo: $"..." (bash extension)
|
||||
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_variables.sh:60 | motivo: $"..." (bash extension)
|
||||
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/1_variables.sh:62 | motivo: $"..." (bash extension)
|
||||
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/2_correction.sh:129 | motivo: $"..." (bash extension)
|
||||
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/2_correction.sh:139 | motivo: $"..." (bash extension)
|
||||
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/2_correction.sh:141 | motivo: $"..." (bash extension)
|
||||
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/2_correction.sh:143 | motivo: $"..." (bash extension)
|
||||
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/2_correction.sh:147 | motivo: $"..." (bash extension)
|
||||
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/2_correction.sh:159 | motivo: $"..." (bash extension)
|
||||
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/2_correction.sh:161 | motivo: $"..." (bash extension)
|
||||
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/2_path_check.sh:30 | motivo: ~
|
||||
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/8_syntax_errors.sh:19 | motivo: ~
|
||||
- /home/marcos/programming/minishell_sergio/tests/42_minishell_tester/cmds/mand/8_syntax_errors.sh:68 | motivo: &&/|| (bonus)
|
||||
|
||||
Total casos evaluados en reporte original: 195
|
||||
Total casos eliminados (extras): 48
|
||||
Total casos restantes (mandatory): 147
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/10/29 22:09:51 by sede-san #+# #+# */
|
||||
/* Updated: 2026/02/08 19:42:50 by sede-san ### ########.fr */
|
||||
/* Updated: 2026/02/14 01:15:34 by sede-san ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
# include "ft_args.h"
|
||||
# include "minishell.h"
|
||||
# include "core.h"
|
||||
# include "variables.h"
|
||||
|
||||
typedef uint8_t (*t_builtin_func)(t_command cmd, t_minishell *minishell);
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/10/22 19:10:13 by sede-san #+# #+# */
|
||||
/* Updated: 2026/02/13 20:06:08 by sede-san ### ########.fr */
|
||||
/* Updated: 2026/02/14 01:25:43 by sede-san ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -48,7 +48,10 @@ typedef enum e_redirection_type
|
||||
typedef struct s_redirection
|
||||
{
|
||||
t_token_type type;
|
||||
int io_number;
|
||||
char *target;
|
||||
bool heredoc_expand;
|
||||
bool heredoc_ready;
|
||||
} t_redirection;
|
||||
|
||||
/**
|
||||
@@ -63,15 +66,9 @@ typedef struct s_redirection
|
||||
typedef struct s_variables
|
||||
{
|
||||
t_hashmap *environment;
|
||||
// char **internal;
|
||||
t_hashmap *internal;
|
||||
} t_variables;
|
||||
|
||||
typedef struct s_prompt
|
||||
{
|
||||
const char *ps1; // primary prompt string
|
||||
const char *ps2; // secondary prompt string for multiline input
|
||||
} t_prompt;
|
||||
|
||||
/**
|
||||
* @brief Main minishell structure containing global state information
|
||||
*
|
||||
@@ -84,7 +81,6 @@ typedef struct s_minishell
|
||||
{
|
||||
t_variables variables;
|
||||
t_hashmap *builtins;
|
||||
t_prompt prompt;
|
||||
uint8_t exit_status;
|
||||
bool exit;
|
||||
} t_minishell;
|
||||
@@ -136,6 +132,8 @@ extern void minishell_set_execution_signals(void);
|
||||
|
||||
extern void minishell_set_child_signals(void);
|
||||
|
||||
extern void minishell_set_heredoc_signals(void);
|
||||
|
||||
extern bool minishell_consume_sigint(void);
|
||||
|
||||
/* environment.c */
|
||||
@@ -149,8 +147,7 @@ extern char **get_envp(t_minishell *msh);
|
||||
|
||||
extern void free_envp(char **envp);
|
||||
|
||||
extern char *get_env(const char *env_name, t_minishell *msh);
|
||||
|
||||
extern void unset_env(const char *env_name, t_minishell *msh);
|
||||
extern void handle_sigint_status(t_minishell *minishell);
|
||||
extern bool handle_eof(char *line, t_minishell *minishell);
|
||||
|
||||
#endif /* CORE_H */
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/02/10 22:28:01 by sede-san #+# #+# */
|
||||
/* Updated: 2026/02/10 23:24:16 by sede-san ### ########.fr */
|
||||
/* Updated: 2026/02/14 01:11:38 by sede-san ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -19,5 +19,6 @@
|
||||
|
||||
extern void syntax_error_unexpected_token(t_token *token);
|
||||
extern void malloc_error(void);
|
||||
extern void command_not_found_error(const char *command);
|
||||
|
||||
#endif /* ERRORS_H */
|
||||
@@ -53,8 +53,12 @@ extern void executor_parent_cleanup(t_list *node, t_pipeline *pl);
|
||||
extern uint8_t executor_wait_for_children(pid_t last_child_pid);
|
||||
extern void executor_cmdfree(t_command *command);
|
||||
extern bool executor_apply_redirections(const t_command *command,
|
||||
int *saved_stdin, int *saved_stdout);
|
||||
int *saved_stdin, int *saved_stdout, int *saved_stderr);
|
||||
extern void executor_restore_redirections(int saved_stdin,
|
||||
int saved_stdout);
|
||||
int saved_stdout, int saved_stderr);
|
||||
extern bool executor_prepare_heredocs(t_list *command_list,
|
||||
t_minishell *minishell, uint8_t *exit_status);
|
||||
extern char *executor_expand_heredoc_line(const char *line,
|
||||
t_minishell *minishell);
|
||||
|
||||
#endif /* EXECUTOR_H */
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/10/20 16:35:10 by sede-san #+# #+# */
|
||||
/* Updated: 2026/02/13 20:12:25 by sede-san ### ########.fr */
|
||||
/* Updated: 2026/02/14 01:13:42 by sede-san ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -42,7 +42,4 @@
|
||||
# include <term.h> // tgetent(3), tgetflag(3), tgetnum(3),
|
||||
// tgetstr(3), tgoto(3), tputs(3)
|
||||
|
||||
void handle_sigint_status(t_minishell *minishell);
|
||||
bool handle_eof(char *line, t_minishell *minishell);
|
||||
|
||||
#endif /* MINISHELL_H */
|
||||
|
||||
@@ -41,8 +41,10 @@ extern void command_clear(t_command *command);
|
||||
extern void command_add_tokens(t_command **command, t_list **tokens);
|
||||
extern bool is_pipe(t_token *token);
|
||||
extern bool is_redirection(t_token *token);
|
||||
void redirection_add(t_list **tokens, t_token *token,
|
||||
t_command **command);
|
||||
bool parser_token_is_fd_prefix(t_list *token_node,
|
||||
int *io_number);
|
||||
void redirection_add_with_fd(t_list **tokens, t_token *token,
|
||||
t_command **command, int io_number);
|
||||
void words_add(t_list **tokens, t_command **command);
|
||||
|
||||
void expand(t_list **commands, t_minishell *minishell);
|
||||
|
||||
43
include/variables.h
Normal file
43
include/variables.h
Normal file
@@ -0,0 +1,43 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* variables.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/02/13 21:45:35 by sede-san #+# #+# */
|
||||
/* Updated: 2026/02/14 01:24:49 by sede-san ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef VARIABLES_H
|
||||
# define VARIABLES_H
|
||||
|
||||
# include "minishell.h"
|
||||
# include "core.h"
|
||||
|
||||
// variables.c
|
||||
|
||||
extern char *get_var(const char *name, t_minishell *minishell);
|
||||
extern void set_var(const char *name, char *value, t_minishell *minishell);
|
||||
extern void unset_var(const char *name, t_minishell *minishell);
|
||||
|
||||
// environment.c
|
||||
|
||||
extern char *get_env(const char *name, t_minishell *minishell);
|
||||
extern void set_env(const char *name, char *value, t_minishell *minishell);
|
||||
extern void unset_env(const char *name, t_minishell *minishell);
|
||||
|
||||
extern void set_envp(char **envp, t_minishell *minishell);
|
||||
extern char **get_envp(t_minishell *minishell);
|
||||
extern void free_envp(char **envp);
|
||||
|
||||
// internal.c
|
||||
|
||||
extern char *get_int(const char *name, t_minishell *minishell);
|
||||
extern void set_int(const char *name, char *value, t_minishell *minishell);
|
||||
extern void unset_int(const char *name, t_minishell *minishell);
|
||||
|
||||
extern void set_intp(t_minishell *minishell);
|
||||
|
||||
#endif /* VARIABLES_H */
|
||||
5
src/builtins/env/env.c
vendored
5
src/builtins/env/env.c
vendored
@@ -22,8 +22,9 @@ uint8_t builtin_env(
|
||||
{
|
||||
if (cmd.argc > 1)
|
||||
{
|
||||
ft_eputendl("minishell: env: too many arguments");
|
||||
return (EXIT_FAILURE);
|
||||
ft_eprintf("minishell: env: %s: No such file or directory\n",
|
||||
cmd.argv[1]);
|
||||
return (127);
|
||||
}
|
||||
print_env(msh);
|
||||
return (EXIT_SUCCESS);
|
||||
|
||||
@@ -16,6 +16,8 @@ static bool exit_arg_is_invalid(const char *arg)
|
||||
{
|
||||
if (arg == NULL)
|
||||
return (true);
|
||||
if (arg[0] == '\0')
|
||||
return (true);
|
||||
if ((*arg == '+' || *arg == '-') && arg[1] == '\0')
|
||||
return (true);
|
||||
if (!ft_strisnum(arg))
|
||||
|
||||
@@ -6,14 +6,24 @@
|
||||
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/02/09 22:05:00 by codex #+# #+# */
|
||||
/* Updated: 2026/02/09 22:05:00 by codex ### ########.fr */
|
||||
/* Updated: 2026/02/14 02:02:14 by sede-san ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "builtins.h"
|
||||
|
||||
static uint8_t is_valid_identifier(const char *arg, size_t name_len);
|
||||
static uint8_t export_one(char *arg, t_minishell *msh);
|
||||
static uint8_t print_exported(t_minishell *msh);
|
||||
static void **entries_to_array(t_list *lst, size_t *count,
|
||||
bool add_oldpwd);
|
||||
static void sort_entries(t_map_entry **entries, size_t count);
|
||||
bool export_is_valid_identifier(const char *arg, size_t name_len);
|
||||
void export_parse_assignment(char *arg, char **eq_pos,
|
||||
size_t *name_len, bool *append);
|
||||
uint8_t export_set_assigned_value(const char *name, char *eq_pos,
|
||||
bool append, t_minishell *msh);
|
||||
bool export_print_declaration(const char *name, const char *value);
|
||||
static t_map_entry g_oldpwd_entry = {"OLDPWD", NULL};
|
||||
|
||||
uint8_t builtin_export(
|
||||
t_command cmd,
|
||||
@@ -21,36 +31,24 @@ uint8_t builtin_export(
|
||||
)
|
||||
{
|
||||
uint8_t status;
|
||||
uint8_t result;
|
||||
size_t i;
|
||||
|
||||
if (cmd.argc == 1)
|
||||
return (builtin_env(cmd, msh));
|
||||
return (print_exported(msh));
|
||||
status = EXIT_SUCCESS;
|
||||
i = 0;
|
||||
while (cmd.argv[++i] != NULL)
|
||||
if (export_one(cmd.argv[i], msh) != EXIT_SUCCESS)
|
||||
{
|
||||
result = export_one(cmd.argv[i], msh);
|
||||
if (result == 2)
|
||||
return (2);
|
||||
if (result != EXIT_SUCCESS)
|
||||
status = EXIT_FAILURE;
|
||||
}
|
||||
return (status);
|
||||
}
|
||||
|
||||
static uint8_t is_valid_identifier(
|
||||
const char *arg,
|
||||
size_t name_len
|
||||
)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
if (name_len == 0 || arg == NULL)
|
||||
return (0);
|
||||
if (!ft_isalpha(arg[0]) && arg[0] != '_')
|
||||
return (0);
|
||||
i = 0;
|
||||
while (++i < name_len)
|
||||
if (!ft_isalnum(arg[i]) && arg[i] != '_')
|
||||
return (0);
|
||||
return (1);
|
||||
}
|
||||
|
||||
static uint8_t export_one(
|
||||
char *arg,
|
||||
t_minishell *msh
|
||||
@@ -59,21 +57,111 @@ static uint8_t export_one(
|
||||
char *eq_pos;
|
||||
char *name;
|
||||
size_t name_len;
|
||||
bool append;
|
||||
|
||||
eq_pos = ft_strchr(arg, '=');
|
||||
name_len = ft_strlen(arg);
|
||||
if (eq_pos != NULL)
|
||||
name_len = (size_t)(eq_pos - arg);
|
||||
if (!is_valid_identifier(arg, name_len))
|
||||
return (ft_eprintf("minishell: export: `%s': not a valid identifier\n",
|
||||
arg), EXIT_FAILURE);
|
||||
if (arg[0] == '-' && arg[1] != '\0')
|
||||
return (ft_eprintf("minishell: export: %s: invalid option\n", arg),
|
||||
ft_eputendl("export: usage: export [name[=value] ...]"), 2);
|
||||
export_parse_assignment(arg, &eq_pos, &name_len, &append);
|
||||
if (!export_is_valid_identifier(arg, name_len))
|
||||
return (ft_eprintf(
|
||||
"minishell: export: `%s': not a valid identifier\n", arg),
|
||||
EXIT_FAILURE);
|
||||
name = ft_substr(arg, 0, name_len);
|
||||
if (name == NULL)
|
||||
return (EXIT_FAILURE);
|
||||
if (eq_pos != NULL)
|
||||
set_env(name, eq_pos + 1, msh);
|
||||
else
|
||||
set_env(name, "", msh);
|
||||
if (export_set_assigned_value(name, eq_pos, append, msh) != EXIT_SUCCESS)
|
||||
return (free(name), EXIT_FAILURE);
|
||||
free(name);
|
||||
return (EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
static uint8_t print_exported(
|
||||
t_minishell *msh
|
||||
)
|
||||
{
|
||||
t_list *entries;
|
||||
t_map_entry **sorted_entries;
|
||||
size_t count;
|
||||
size_t i;
|
||||
bool add_oldpwd;
|
||||
|
||||
entries = ft_hashmap_entries(msh->variables.environment);
|
||||
if (entries == NULL)
|
||||
return (EXIT_SUCCESS);
|
||||
add_oldpwd = !ft_hashmap_contains_key(msh->variables.environment, "OLDPWD");
|
||||
sorted_entries = (t_map_entry **)entries_to_array(entries, &count,
|
||||
add_oldpwd);
|
||||
ft_lstclear_nodes(&entries);
|
||||
if (sorted_entries == NULL)
|
||||
return (EXIT_FAILURE);
|
||||
sort_entries(sorted_entries, count);
|
||||
i = 0;
|
||||
while (i < count)
|
||||
{
|
||||
if (!export_print_declaration((char *)sorted_entries[i]->key,
|
||||
(char *)sorted_entries[i]->value))
|
||||
return (free(sorted_entries), EXIT_FAILURE);
|
||||
i++;
|
||||
}
|
||||
free(sorted_entries);
|
||||
return (EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
static void **entries_to_array(
|
||||
t_list *entries,
|
||||
size_t *count,
|
||||
bool add_oldpwd
|
||||
)
|
||||
{
|
||||
void **array;
|
||||
t_list *node;
|
||||
t_map_entry *entry;
|
||||
size_t i;
|
||||
|
||||
*count = (size_t)ft_lstsize(entries) + (size_t)add_oldpwd;
|
||||
array = (void **)malloc(sizeof(void *) * (*count));
|
||||
if (array == NULL)
|
||||
return (NULL);
|
||||
i = 0;
|
||||
node = entries;
|
||||
while (node != NULL)
|
||||
{
|
||||
entry = (t_map_entry *)node->content;
|
||||
if (entry != NULL && ft_strcmp((char *)entry->key, "_") != 0)
|
||||
array[i++] = entry;
|
||||
node = node->next;
|
||||
}
|
||||
if (add_oldpwd)
|
||||
array[i++] = &g_oldpwd_entry;
|
||||
*count = i;
|
||||
return (array);
|
||||
}
|
||||
|
||||
static void sort_entries(
|
||||
t_map_entry **entries,
|
||||
size_t count
|
||||
)
|
||||
{
|
||||
size_t i;
|
||||
size_t j;
|
||||
t_map_entry *tmp;
|
||||
|
||||
i = 0;
|
||||
while (i < count)
|
||||
{
|
||||
j = i + 1;
|
||||
while (j < count)
|
||||
{
|
||||
if (ft_strcmp((char *)entries[i]->key,
|
||||
(char *)entries[j]->key) > 0)
|
||||
{
|
||||
tmp = entries[i];
|
||||
entries[i] = entries[j];
|
||||
entries[j] = tmp;
|
||||
}
|
||||
j++;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
129
src/builtins/export/export_utils.c
Normal file
129
src/builtins/export/export_utils.c
Normal file
@@ -0,0 +1,129 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* export_utils.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/02/14 15:20:00 by sede-san #+# #+# */
|
||||
/* Updated: 2026/02/14 15:20:00 by sede-san ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "builtins.h"
|
||||
|
||||
bool export_is_valid_identifier(
|
||||
const char *arg,
|
||||
size_t name_len
|
||||
)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
if (name_len == 0 || arg == NULL)
|
||||
return (false);
|
||||
if (!ft_isalpha(arg[0]) && arg[0] != '_')
|
||||
return (false);
|
||||
i = 0;
|
||||
while (++i < name_len)
|
||||
if (!ft_isalnum(arg[i]) && arg[i] != '_')
|
||||
return (false);
|
||||
return (true);
|
||||
}
|
||||
|
||||
void export_parse_assignment(
|
||||
char *arg,
|
||||
char **eq_pos,
|
||||
size_t *name_len,
|
||||
bool *append
|
||||
)
|
||||
{
|
||||
char *plus_eq;
|
||||
|
||||
*append = false;
|
||||
*eq_pos = ft_strchr(arg, '=');
|
||||
*name_len = ft_strlen(arg);
|
||||
plus_eq = ft_strnstr(arg, "+=", *name_len);
|
||||
if (plus_eq != NULL)
|
||||
{
|
||||
*append = true;
|
||||
*name_len = (size_t)(plus_eq - arg);
|
||||
*eq_pos = plus_eq + 1;
|
||||
}
|
||||
else if (*eq_pos != NULL)
|
||||
*name_len = (size_t)(*eq_pos - arg);
|
||||
}
|
||||
|
||||
uint8_t export_set_assigned_value(
|
||||
const char *name,
|
||||
char *eq_pos,
|
||||
bool append,
|
||||
t_minishell *msh
|
||||
)
|
||||
{
|
||||
char *current;
|
||||
char *joined;
|
||||
|
||||
if (append)
|
||||
{
|
||||
current = get_var((char *)name, msh);
|
||||
if (current == NULL)
|
||||
current = "";
|
||||
joined = ft_strnjoin(2, current, eq_pos + 1);
|
||||
if (joined == NULL)
|
||||
return (EXIT_FAILURE);
|
||||
set_var(name, joined, msh);
|
||||
free(joined);
|
||||
return (EXIT_SUCCESS);
|
||||
}
|
||||
if (eq_pos != NULL)
|
||||
set_var(name, eq_pos + 1, msh);
|
||||
else
|
||||
set_var(name, "", msh);
|
||||
return (EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
char *export_escape_value(
|
||||
const char *value
|
||||
)
|
||||
{
|
||||
char *escaped;
|
||||
size_t i;
|
||||
size_t j;
|
||||
|
||||
escaped = (char *)malloc((ft_strlen(value) * 2) + 1);
|
||||
if (escaped == NULL)
|
||||
return (NULL);
|
||||
i = 0;
|
||||
j = 0;
|
||||
while (value[i] != '\0')
|
||||
{
|
||||
if (value[i] == '\\' || value[i] == '\"' || value[i] == '$')
|
||||
escaped[j++] = '\\';
|
||||
escaped[j++] = value[i++];
|
||||
}
|
||||
escaped[j] = '\0';
|
||||
return (escaped);
|
||||
}
|
||||
|
||||
bool export_print_declaration(
|
||||
const char *name,
|
||||
const char *value
|
||||
)
|
||||
{
|
||||
char *escaped;
|
||||
|
||||
ft_putstr("declare -x ");
|
||||
ft_putstr((char *)name);
|
||||
if (value != NULL)
|
||||
{
|
||||
escaped = export_escape_value(value);
|
||||
if (escaped == NULL)
|
||||
return (false);
|
||||
ft_putstr("=\"");
|
||||
ft_putstr(escaped);
|
||||
ft_putstr("\"");
|
||||
free(escaped);
|
||||
}
|
||||
ft_putchar('\n');
|
||||
return (true);
|
||||
}
|
||||
@@ -14,6 +14,7 @@
|
||||
|
||||
static uint8_t is_valid_identifier(const char *arg);
|
||||
static uint8_t unset_one(char *arg, t_minishell *msh);
|
||||
static bool is_option(const char *arg);
|
||||
|
||||
uint8_t builtin_unset(
|
||||
t_command cmd,
|
||||
@@ -21,13 +22,19 @@ uint8_t builtin_unset(
|
||||
)
|
||||
{
|
||||
uint8_t status;
|
||||
uint8_t result;
|
||||
size_t i;
|
||||
|
||||
status = EXIT_SUCCESS;
|
||||
i = 0;
|
||||
while (cmd.argv[++i] != NULL)
|
||||
if (unset_one(cmd.argv[i], msh) != EXIT_SUCCESS)
|
||||
{
|
||||
result = unset_one(cmd.argv[i], msh);
|
||||
if (result == 2)
|
||||
return (2);
|
||||
if (result != EXIT_SUCCESS)
|
||||
status = EXIT_FAILURE;
|
||||
}
|
||||
return (status);
|
||||
}
|
||||
|
||||
@@ -53,9 +60,21 @@ static uint8_t unset_one(
|
||||
t_minishell *msh
|
||||
)
|
||||
{
|
||||
if (is_option(arg))
|
||||
{
|
||||
ft_eprintf("minishell: unset: %s: invalid option\n", arg);
|
||||
ft_eputendl("unset: usage: unset [name ...]");
|
||||
return (2);
|
||||
}
|
||||
if (!is_valid_identifier(arg))
|
||||
return (ft_eprintf("minishell: unset: `%s': not a valid identifier\n",
|
||||
arg), EXIT_FAILURE);
|
||||
return (EXIT_SUCCESS);
|
||||
unset_env(arg, msh);
|
||||
return (EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
static bool is_option(
|
||||
const char *arg
|
||||
)
|
||||
{
|
||||
return (arg != NULL && arg[0] == '-' && arg[1] != '\0');
|
||||
}
|
||||
|
||||
@@ -13,11 +13,19 @@
|
||||
#include "core.h"
|
||||
|
||||
static int g_signal = 0;
|
||||
static bool g_heredoc_mode = false;
|
||||
|
||||
static void sigint_handler(int signal)
|
||||
{
|
||||
g_signal = signal;
|
||||
write(STDOUT_FILENO, "\n", 1);
|
||||
if (g_heredoc_mode)
|
||||
{
|
||||
rl_on_new_line();
|
||||
rl_replace_line("", 0);
|
||||
rl_done = 1;
|
||||
return ;
|
||||
}
|
||||
rl_on_new_line();
|
||||
rl_replace_line("", 0);
|
||||
rl_redisplay();
|
||||
@@ -28,6 +36,7 @@ void minishell_set_interactive_signals(void)
|
||||
struct sigaction action;
|
||||
|
||||
ft_bzero(&action, sizeof(action));
|
||||
g_heredoc_mode = false;
|
||||
action.sa_handler = sigint_handler;
|
||||
sigemptyset(&action.sa_mask);
|
||||
sigaction(SIGINT, &action, NULL);
|
||||
@@ -40,20 +49,23 @@ void minishell_set_execution_signals(void)
|
||||
struct sigaction action;
|
||||
|
||||
ft_bzero(&action, sizeof(action));
|
||||
g_heredoc_mode = false;
|
||||
action.sa_handler = SIG_IGN;
|
||||
sigemptyset(&action.sa_mask);
|
||||
sigaction(SIGINT, &action, NULL);
|
||||
sigaction(SIGQUIT, &action, NULL);
|
||||
}
|
||||
|
||||
void minishell_set_child_signals(void)
|
||||
void minishell_set_heredoc_signals(void)
|
||||
{
|
||||
struct sigaction action;
|
||||
|
||||
ft_bzero(&action, sizeof(action));
|
||||
action.sa_handler = SIG_DFL;
|
||||
g_heredoc_mode = true;
|
||||
action.sa_handler = sigint_handler;
|
||||
sigemptyset(&action.sa_mask);
|
||||
sigaction(SIGINT, &action, NULL);
|
||||
action.sa_handler = SIG_IGN;
|
||||
sigaction(SIGQUIT, &action, NULL);
|
||||
}
|
||||
|
||||
|
||||
24
src/core/signals_child.c
Normal file
24
src/core/signals_child.c
Normal file
@@ -0,0 +1,24 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* signals_child.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/02/14 18:20:00 by sede-san #+# #+# */
|
||||
/* Updated: 2026/02/14 18:20:00 by sede-san ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "core.h"
|
||||
|
||||
void minishell_set_child_signals(void)
|
||||
{
|
||||
struct sigaction action;
|
||||
|
||||
ft_bzero(&action, sizeof(action));
|
||||
action.sa_handler = SIG_DFL;
|
||||
sigemptyset(&action.sa_mask);
|
||||
sigaction(SIGINT, &action, NULL);
|
||||
sigaction(SIGQUIT, &action, NULL);
|
||||
}
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/02/10 22:22:06 by sede-san #+# #+# */
|
||||
/* Updated: 2026/02/12 02:55:36 by sede-san ### ########.fr */
|
||||
/* Updated: 2026/02/14 06:58:05 by sede-san ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -29,3 +29,10 @@ void malloc_error(void)
|
||||
{
|
||||
ft_eprintf("minishell: %s\n", strerror(ENOMEM));
|
||||
}
|
||||
|
||||
void command_not_found_error(
|
||||
const char *command
|
||||
)
|
||||
{
|
||||
ft_eprintf("minishell: %s: command not found\n", command);
|
||||
}
|
||||
@@ -6,12 +6,13 @@
|
||||
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/02/11 00:00:00 by sede-san #+# #+# */
|
||||
/* Updated: 2026/02/11 00:00:00 by sede-san ### ########.fr */
|
||||
/* Updated: 2026/02/14 13:12:58 by sede-san ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "executor.h"
|
||||
#include "builtins.h"
|
||||
#include "errors.h"
|
||||
#include <errno.h>
|
||||
|
||||
static uint8_t resolve_execve_status(void)
|
||||
@@ -40,9 +41,13 @@ static void handle_execve_error(
|
||||
)
|
||||
{
|
||||
uint8_t exit_status;
|
||||
size_t i;
|
||||
|
||||
exit_status = resolve_execve_status();
|
||||
free_envp(envp);
|
||||
i = 0;
|
||||
while (envp[i] != NULL)
|
||||
free(envp[i++]);
|
||||
free(envp);
|
||||
perror(command->path);
|
||||
exit(exit_status);
|
||||
}
|
||||
@@ -56,10 +61,7 @@ static void execute_external_command(
|
||||
|
||||
envp = get_envp(minishell);
|
||||
if (envp == NULL)
|
||||
{
|
||||
perror("get_envp");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
return (malloc_error(), exit(EXIT_FAILURE));
|
||||
execve(command->path, command->argv, envp);
|
||||
handle_execve_error(command, envp);
|
||||
}
|
||||
@@ -77,10 +79,7 @@ uint8_t executor_execute_command(
|
||||
free(command->path);
|
||||
command->path = executor_resolve_command_path(command, minishell);
|
||||
if (command->path == NULL)
|
||||
{
|
||||
ft_eprintf("minishell: %s: command not found\n", command->argv[0]);
|
||||
return (127);
|
||||
}
|
||||
return (command_not_found_error(command->argv[0]), 127);
|
||||
execute_external_command(command, minishell);
|
||||
return (EXIT_FAILURE);
|
||||
}
|
||||
|
||||
@@ -35,6 +35,9 @@ static void cmdfree_redirection(
|
||||
{
|
||||
if (redirection == NULL)
|
||||
return ;
|
||||
if (redirection->type == TOKEN_HEREDOC && redirection->heredoc_ready
|
||||
&& redirection->target != NULL)
|
||||
unlink(redirection->target);
|
||||
free(redirection->target);
|
||||
free(redirection);
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/02/08 19:10:47 by sede-san #+# #+# */
|
||||
/* Updated: 2026/02/11 00:00:00 by sede-san ### ########.fr */
|
||||
/* Updated: 2026/02/14 13:10:43 by sede-san ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -48,13 +48,15 @@ static void run_command_in_parent(
|
||||
t_command *command;
|
||||
int saved_stdin;
|
||||
int saved_stdout;
|
||||
int saved_stderr;
|
||||
|
||||
command = state->current_command->content;
|
||||
if (!executor_apply_redirections(command, &saved_stdin, &saved_stdout))
|
||||
if (!executor_apply_redirections(command, &saved_stdin, &saved_stdout,
|
||||
&saved_stderr))
|
||||
state->exit_status = EXIT_FAILURE;
|
||||
else
|
||||
state->exit_status = executor_execute_command(command, minishell);
|
||||
executor_restore_redirections(saved_stdin, saved_stdout);
|
||||
executor_restore_redirections(saved_stdin, saved_stdout, saved_stderr);
|
||||
}
|
||||
|
||||
static bool run_current_command(
|
||||
@@ -89,6 +91,12 @@ uint8_t execute(
|
||||
t_exec_state state;
|
||||
|
||||
init_exec_state(&state, command_list);
|
||||
if (!executor_prepare_heredocs(command_list, minishell, &state.exit_status))
|
||||
{
|
||||
minishell->exit_status = state.exit_status;
|
||||
ft_lstclear(&command_list, (void (*)(void *))executor_cmdfree);
|
||||
return (state.exit_status);
|
||||
}
|
||||
minishell_set_execution_signals();
|
||||
while (state.current_command)
|
||||
{
|
||||
|
||||
65
src/executor/heredoc.c
Normal file
65
src/executor/heredoc.c
Normal file
@@ -0,0 +1,65 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* heredoc.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/02/14 17:50:00 by sede-san #+# #+# */
|
||||
/* Updated: 2026/02/14 17:50:00 by sede-san ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "heredoc_internal.h"
|
||||
#include "errors.h"
|
||||
|
||||
static bool prepare_heredoc_redirection(t_redirection *rd,
|
||||
t_minishell *minishell, uint8_t *exit_status)
|
||||
{
|
||||
char *path;
|
||||
int fd;
|
||||
|
||||
path = NULL;
|
||||
fd = executor_heredoc_open_tmp(&path);
|
||||
if (fd == -1)
|
||||
return (perror("open"), *exit_status = EXIT_FAILURE, false);
|
||||
if (!executor_heredoc_fill_tmp(fd, rd, minishell, exit_status))
|
||||
return (executor_heredoc_discard_tmp(fd, path), false);
|
||||
return (executor_heredoc_finalize_tmp(rd, fd, path, exit_status));
|
||||
}
|
||||
|
||||
static bool process_command_heredocs(t_command *command, t_minishell *minishell,
|
||||
uint8_t *exit_status)
|
||||
{
|
||||
t_list *redir_node;
|
||||
t_redirection *rd;
|
||||
|
||||
redir_node = command->redirections;
|
||||
while (redir_node != NULL)
|
||||
{
|
||||
rd = (t_redirection *)redir_node->content;
|
||||
if (rd->type == TOKEN_HEREDOC
|
||||
&& !prepare_heredoc_redirection(rd, minishell, exit_status))
|
||||
return (false);
|
||||
redir_node = redir_node->next;
|
||||
}
|
||||
return (true);
|
||||
}
|
||||
|
||||
bool executor_prepare_heredocs(t_list *command_list, t_minishell *minishell,
|
||||
uint8_t *exit_status)
|
||||
{
|
||||
t_command *command;
|
||||
|
||||
*exit_status = EXIT_SUCCESS;
|
||||
minishell_set_heredoc_signals();
|
||||
while (command_list != NULL)
|
||||
{
|
||||
command = (t_command *)command_list->content;
|
||||
if (!process_command_heredocs(command, minishell, exit_status))
|
||||
return (minishell_set_interactive_signals(), false);
|
||||
command_list = command_list->next;
|
||||
}
|
||||
minishell_set_interactive_signals();
|
||||
return (true);
|
||||
}
|
||||
98
src/executor/heredoc_expand.c
Normal file
98
src/executor/heredoc_expand.c
Normal file
@@ -0,0 +1,98 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* heredoc_expand.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/02/14 17:30:00 by sede-san #+# #+# */
|
||||
/* Updated: 2026/02/14 17:30:00 by sede-san ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "executor.h"
|
||||
#include "variables.h"
|
||||
#include "errors.h"
|
||||
|
||||
static bool is_var_char(char c)
|
||||
{
|
||||
return (ft_isalnum(c) || c == '_');
|
||||
}
|
||||
|
||||
static bool append_text(char **result, const char *value)
|
||||
{
|
||||
char *joined;
|
||||
|
||||
joined = ft_strnjoin(2, *result, (char *)value);
|
||||
if (joined == NULL)
|
||||
{
|
||||
free(*result);
|
||||
*result = NULL;
|
||||
return (false);
|
||||
}
|
||||
free(*result);
|
||||
*result = joined;
|
||||
return (true);
|
||||
}
|
||||
|
||||
static char *expand_variable(const char *line, size_t *i, t_minishell *msh)
|
||||
{
|
||||
char *name;
|
||||
char *value;
|
||||
size_t start;
|
||||
|
||||
(*i)++;
|
||||
if (line[*i] == '?')
|
||||
return ((*i)++, ft_itoa(msh->exit_status));
|
||||
if (line[*i] == '\0' || !is_var_char(line[*i]))
|
||||
return (ft_strdup("$"));
|
||||
start = *i;
|
||||
while (line[*i] != '\0' && is_var_char(line[*i]))
|
||||
(*i)++;
|
||||
name = ft_substr(line, start, *i - start);
|
||||
if (name == NULL)
|
||||
return (NULL);
|
||||
value = get_var(name, msh);
|
||||
free(name);
|
||||
if (value == NULL)
|
||||
value = "";
|
||||
return (ft_strdup(value));
|
||||
}
|
||||
|
||||
static char *expand_literal_char(const char *line, size_t *i)
|
||||
{
|
||||
char value[2];
|
||||
|
||||
value[0] = line[*i];
|
||||
value[1] = '\0';
|
||||
(*i)++;
|
||||
return (ft_strdup(value));
|
||||
}
|
||||
|
||||
char *executor_expand_heredoc_line(
|
||||
const char *line,
|
||||
t_minishell *minishell
|
||||
)
|
||||
{
|
||||
char *result;
|
||||
char *expanded;
|
||||
size_t i;
|
||||
|
||||
result = ft_strdup("");
|
||||
if (result == NULL)
|
||||
return (malloc_error(), NULL);
|
||||
i = 0;
|
||||
while (line[i] != '\0')
|
||||
{
|
||||
if (line[i] == '$')
|
||||
expanded = expand_variable(line, &i, minishell);
|
||||
else
|
||||
expanded = expand_literal_char(line, &i);
|
||||
if (expanded == NULL)
|
||||
return (free(result), malloc_error(), NULL);
|
||||
if (!append_text(&result, expanded))
|
||||
return (free(expanded), malloc_error(), NULL);
|
||||
free(expanded);
|
||||
}
|
||||
return (result);
|
||||
}
|
||||
99
src/executor/heredoc_file.c
Normal file
99
src/executor/heredoc_file.c
Normal file
@@ -0,0 +1,99 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* heredoc_file.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/02/14 17:50:00 by sede-san #+# #+# */
|
||||
/* Updated: 2026/02/14 17:50:00 by sede-san ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "heredoc_internal.h"
|
||||
#include <errno.h>
|
||||
#include "variables.h"
|
||||
|
||||
static char *build_tmp_path(unsigned int id)
|
||||
{
|
||||
char *suffix;
|
||||
char *path;
|
||||
|
||||
suffix = ft_uitoa(id);
|
||||
if (suffix == NULL)
|
||||
return (errno = ENOMEM, NULL);
|
||||
path = ft_strnjoin(2, "/tmp/minishell_heredoc_", suffix);
|
||||
free(suffix);
|
||||
if (path == NULL)
|
||||
return (errno = ENOMEM, NULL);
|
||||
return (path);
|
||||
}
|
||||
|
||||
int executor_heredoc_open_tmp(char **path_out)
|
||||
{
|
||||
static unsigned int counter;
|
||||
char *path;
|
||||
unsigned int attempts;
|
||||
int fd;
|
||||
|
||||
attempts = 0;
|
||||
while (attempts++ < 10000)
|
||||
{
|
||||
path = build_tmp_path(counter++);
|
||||
if (path == NULL)
|
||||
return (-1);
|
||||
fd = open(path, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0600);
|
||||
if (fd != -1)
|
||||
return (*path_out = path, fd);
|
||||
free(path);
|
||||
if (errno != EEXIST)
|
||||
return (-1);
|
||||
}
|
||||
errno = EEXIST;
|
||||
return (-1);
|
||||
}
|
||||
|
||||
char *executor_heredoc_read_line(t_minishell *minishell)
|
||||
{
|
||||
char *prompt;
|
||||
char *line;
|
||||
size_t len;
|
||||
|
||||
if (!isatty(STDIN_FILENO))
|
||||
{
|
||||
line = get_next_line(STDIN_FILENO);
|
||||
if (line == NULL)
|
||||
return (NULL);
|
||||
len = ft_strlen(line);
|
||||
if (len > 0 && line[len - 1] == '\n')
|
||||
line[len - 1] = '\0';
|
||||
return (line);
|
||||
}
|
||||
prompt = get_var("PS2", minishell);
|
||||
if (prompt == NULL)
|
||||
prompt = DEFAULT_PS2;
|
||||
return (readline(prompt));
|
||||
}
|
||||
|
||||
void executor_heredoc_discard_tmp(int fd, char *path)
|
||||
{
|
||||
if (fd >= 0)
|
||||
close(fd);
|
||||
if (path != NULL)
|
||||
{
|
||||
unlink(path);
|
||||
free(path);
|
||||
}
|
||||
}
|
||||
|
||||
bool executor_heredoc_finalize_tmp(t_redirection *rd, int fd, char *path,
|
||||
uint8_t *exit_status)
|
||||
{
|
||||
if (close(fd) == -1)
|
||||
return (executor_heredoc_discard_tmp(-1, path), perror("close"),
|
||||
*exit_status = EXIT_FAILURE, false);
|
||||
free(rd->target);
|
||||
rd->target = path;
|
||||
rd->heredoc_ready = true;
|
||||
return (true);
|
||||
}
|
||||
80
src/executor/heredoc_input.c
Normal file
80
src/executor/heredoc_input.c
Normal file
@@ -0,0 +1,80 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* heredoc_input.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/02/14 17:50:00 by sede-san #+# #+# */
|
||||
/* Updated: 2026/02/14 17:50:00 by sede-san ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "heredoc_internal.h"
|
||||
#include "errors.h"
|
||||
|
||||
static bool write_all(int fd, const char *buffer, size_t len)
|
||||
{
|
||||
size_t total;
|
||||
ssize_t written;
|
||||
|
||||
total = 0;
|
||||
while (total < len)
|
||||
{
|
||||
written = write(fd, buffer + total, len - total);
|
||||
if (written <= 0)
|
||||
return (false);
|
||||
total += written;
|
||||
}
|
||||
return (true);
|
||||
}
|
||||
|
||||
static bool write_heredoc_line(int fd, const char *line)
|
||||
{
|
||||
if (!write_all(fd, line, ft_strlen(line)))
|
||||
return (false);
|
||||
return (write_all(fd, "\n", 1));
|
||||
}
|
||||
|
||||
static bool handle_heredoc_line(int fd, char *line, t_redirection *rd,
|
||||
t_minishell *minishell)
|
||||
{
|
||||
char *expanded;
|
||||
|
||||
expanded = line;
|
||||
if (rd->heredoc_expand)
|
||||
{
|
||||
expanded = executor_expand_heredoc_line(line, minishell);
|
||||
free(line);
|
||||
if (expanded == NULL)
|
||||
return (false);
|
||||
}
|
||||
if (!write_heredoc_line(fd, expanded))
|
||||
return (free(expanded), perror("write"), false);
|
||||
free(expanded);
|
||||
return (true);
|
||||
}
|
||||
|
||||
bool executor_heredoc_fill_tmp(int fd, t_redirection *rd,
|
||||
t_minishell *minishell, uint8_t *exit_status)
|
||||
{
|
||||
char *line;
|
||||
|
||||
while (1)
|
||||
{
|
||||
line = executor_heredoc_read_line(minishell);
|
||||
if (minishell_consume_sigint())
|
||||
return (free(line), *exit_status = 130, false);
|
||||
if (line == NULL)
|
||||
{
|
||||
if (!isatty(STDIN_FILENO))
|
||||
minishell->exit = true;
|
||||
return (ft_eprintf("minishell: warning: here-document delimited by "
|
||||
"end-of-file (wanted `%s')\n", rd->target), true);
|
||||
}
|
||||
if (ft_strcmp(line, rd->target) == 0)
|
||||
return (free(line), true);
|
||||
if (!handle_heredoc_line(fd, line, rd, minishell))
|
||||
return (*exit_status = EXIT_FAILURE, false);
|
||||
}
|
||||
}
|
||||
26
src/executor/heredoc_internal.h
Normal file
26
src/executor/heredoc_internal.h
Normal file
@@ -0,0 +1,26 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* heredoc_internal.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/02/14 17:50:00 by sede-san #+# #+# */
|
||||
/* Updated: 2026/02/14 17:50:00 by sede-san ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef HEREDOC_INTERNAL_H
|
||||
# define HEREDOC_INTERNAL_H
|
||||
|
||||
# include "executor.h"
|
||||
|
||||
char *executor_heredoc_read_line(t_minishell *minishell);
|
||||
int executor_heredoc_open_tmp(char **path_out);
|
||||
void executor_heredoc_discard_tmp(int fd, char *path);
|
||||
bool executor_heredoc_finalize_tmp(t_redirection *rd, int fd, char *path,
|
||||
uint8_t *exit_status);
|
||||
bool executor_heredoc_fill_tmp(int fd, t_redirection *rd,
|
||||
t_minishell *minishell, uint8_t *exit_status);
|
||||
|
||||
#endif
|
||||
@@ -6,11 +6,12 @@
|
||||
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/02/11 00:00:00 by sede-san #+# #+# */
|
||||
/* Updated: 2026/02/11 00:00:00 by sede-san ### ########.fr */
|
||||
/* Updated: 2026/02/14 13:16:15 by sede-san ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "executor.h"
|
||||
#include "variables.h"
|
||||
|
||||
static bool is_path_explicit(
|
||||
const char *command_name
|
||||
@@ -37,7 +38,7 @@ static char *resolve_path_from_env(
|
||||
return (NULL);
|
||||
command_path = NULL;
|
||||
i = -1;
|
||||
while (!command_path && path_env[++i] != NULL)
|
||||
while (command_name[0] != 0 && !command_path && path_env[++i] != NULL)
|
||||
{
|
||||
command_path = ft_strnjoin(3, path_env[i], "/", command_name);
|
||||
if (command_path != NULL && access(command_path, X_OK) != EXIT_SUCCESS)
|
||||
|
||||
@@ -53,7 +53,7 @@ void executor_child_process(
|
||||
minishell_set_child_signals();
|
||||
executor_setup_child_input(pipeline);
|
||||
executor_setup_child_output(current_command, pipeline);
|
||||
if (!executor_apply_redirections(command, NULL, NULL))
|
||||
if (!executor_apply_redirections(command, NULL, NULL, NULL))
|
||||
exit(EXIT_FAILURE);
|
||||
exit_status = executor_execute_command(command, minishell);
|
||||
exit(exit_status);
|
||||
|
||||
@@ -19,6 +19,8 @@ static int open_redirection_target(
|
||||
{
|
||||
if (redirection->type == TOKEN_REDIRECT_IN)
|
||||
return (open(redirection->target, O_RDONLY));
|
||||
if (redirection->type == TOKEN_HEREDOC)
|
||||
return (open(redirection->target, O_RDONLY));
|
||||
if (redirection->type == TOKEN_REDIRECT_OUT)
|
||||
return (open(redirection->target, O_WRONLY | O_CREAT | O_TRUNC, 0644));
|
||||
if (redirection->type == TOKEN_APPEND)
|
||||
@@ -27,73 +29,74 @@ static int open_redirection_target(
|
||||
return (-1);
|
||||
}
|
||||
|
||||
static bool backup_stream_if_needed(
|
||||
const t_redirection *redirection,
|
||||
int *saved_stdin,
|
||||
int *saved_stdout
|
||||
static bool backup_fd_if_needed(
|
||||
int io_number,
|
||||
int *saved_fd
|
||||
)
|
||||
{
|
||||
if (redirection->type == TOKEN_REDIRECT_IN && saved_stdin != NULL
|
||||
&& *saved_stdin == -1)
|
||||
{
|
||||
*saved_stdin = dup(STDIN_FILENO);
|
||||
if (*saved_stdin == -1)
|
||||
return (perror("dup"), false);
|
||||
}
|
||||
if ((redirection->type == TOKEN_REDIRECT_OUT
|
||||
|| redirection->type == TOKEN_APPEND) && saved_stdout != NULL
|
||||
&& *saved_stdout == -1)
|
||||
{
|
||||
*saved_stdout = dup(STDOUT_FILENO);
|
||||
if (*saved_stdout == -1)
|
||||
return (perror("dup"), false);
|
||||
}
|
||||
if (saved_fd == NULL || *saved_fd != -1)
|
||||
return (true);
|
||||
*saved_fd = dup(io_number);
|
||||
if (*saved_fd == -1)
|
||||
return (perror("dup"), false);
|
||||
return (true);
|
||||
}
|
||||
|
||||
static bool apply_redirection(
|
||||
const t_redirection *redirection,
|
||||
int *saved_stdin,
|
||||
int *saved_stdout,
|
||||
int *saved_stderr
|
||||
)
|
||||
{
|
||||
int fd;
|
||||
|
||||
if (redirection == NULL || redirection->target == NULL)
|
||||
return (false);
|
||||
if ((redirection->io_number == STDIN_FILENO
|
||||
&& !backup_fd_if_needed(STDIN_FILENO, saved_stdin))
|
||||
|| (redirection->io_number == STDOUT_FILENO
|
||||
&& !backup_fd_if_needed(STDOUT_FILENO, saved_stdout))
|
||||
|| (redirection->io_number == STDERR_FILENO
|
||||
&& !backup_fd_if_needed(STDERR_FILENO, saved_stderr)))
|
||||
return (false);
|
||||
fd = open_redirection_target(redirection);
|
||||
if (fd == -1)
|
||||
return (perror(redirection->target), false);
|
||||
if (dup2(fd, redirection->io_number) == -1)
|
||||
return (close(fd), perror("dup2"), false);
|
||||
return (close(fd), true);
|
||||
}
|
||||
|
||||
bool executor_apply_redirections(
|
||||
const t_command *command,
|
||||
int *saved_stdin,
|
||||
int *saved_stdout
|
||||
int *saved_stdout,
|
||||
int *saved_stderr
|
||||
)
|
||||
{
|
||||
t_list *node;
|
||||
t_redirection *redirection;
|
||||
int fd;
|
||||
t_list *node;
|
||||
|
||||
if (saved_stdin != NULL)
|
||||
*saved_stdin = -1;
|
||||
if (saved_stdout != NULL)
|
||||
*saved_stdout = -1;
|
||||
if (command == NULL || command->redirections == NULL)
|
||||
if (saved_stderr != NULL)
|
||||
*saved_stderr = -1;
|
||||
if (command == NULL)
|
||||
return (true);
|
||||
node = command->redirections;
|
||||
while (node != NULL)
|
||||
{
|
||||
redirection = (t_redirection *)node->content;
|
||||
if (redirection == NULL || redirection->target == NULL)
|
||||
return (false);
|
||||
if (!backup_stream_if_needed(redirection, saved_stdin, saved_stdout))
|
||||
return (false);
|
||||
fd = open_redirection_target(redirection);
|
||||
if (fd == -1)
|
||||
return (perror(redirection->target), false);
|
||||
if (redirection->type == TOKEN_REDIRECT_IN
|
||||
&& dup2(fd, STDIN_FILENO) == -1)
|
||||
return (close(fd), perror("dup2"), false);
|
||||
if ((redirection->type == TOKEN_REDIRECT_OUT
|
||||
|| redirection->type == TOKEN_APPEND)
|
||||
&& dup2(fd, STDOUT_FILENO) == -1)
|
||||
return (close(fd), perror("dup2"), false);
|
||||
close(fd);
|
||||
while (node != NULL
|
||||
&& apply_redirection((t_redirection *)node->content,
|
||||
saved_stdin, saved_stdout, saved_stderr))
|
||||
node = node->next;
|
||||
}
|
||||
return (true);
|
||||
return (node == NULL);
|
||||
}
|
||||
|
||||
void executor_restore_redirections(
|
||||
int saved_stdin,
|
||||
int saved_stdout
|
||||
int saved_stdout,
|
||||
int saved_stderr
|
||||
)
|
||||
{
|
||||
if (saved_stdin != -1)
|
||||
@@ -108,4 +111,10 @@ void executor_restore_redirections(
|
||||
perror("dup2");
|
||||
close(saved_stdout);
|
||||
}
|
||||
if (saved_stderr != -1)
|
||||
{
|
||||
if (dup2(saved_stderr, STDERR_FILENO) == -1)
|
||||
perror("dup2");
|
||||
close(saved_stderr);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/10/20 20:51:33 by sede-san #+# #+# */
|
||||
/* Updated: 2026/02/13 20:12:07 by sede-san ### ########.fr */
|
||||
/* Updated: 2026/02/14 12:57:00 by sede-san ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -15,29 +15,22 @@
|
||||
#include "parser.h"
|
||||
#include "executor.h"
|
||||
|
||||
static void set_prompts(
|
||||
t_minishell *minishell
|
||||
)
|
||||
{
|
||||
minishell->prompt.ps1 = DEFAULT_PS1;
|
||||
minishell->prompt.ps2 = DEFAULT_PS2;
|
||||
}
|
||||
|
||||
void minishell_init(
|
||||
t_minishell *minishell,
|
||||
char **envp
|
||||
){
|
||||
ft_bzero(minishell, sizeof(t_minishell));
|
||||
set_intp(minishell);
|
||||
set_envp(envp, minishell);
|
||||
set_builtins(minishell);
|
||||
set_prompts(minishell);
|
||||
if (minishell->variables.environment == NULL || minishell->builtins == NULL)
|
||||
minishell_clear(minishell);
|
||||
}
|
||||
|
||||
void minishell_run(
|
||||
t_minishell *minishell
|
||||
){
|
||||
)
|
||||
{
|
||||
char *line;
|
||||
t_list *commands;
|
||||
|
||||
@@ -47,7 +40,7 @@ void minishell_run(
|
||||
while (!minishell->exit)
|
||||
{
|
||||
if (isatty(STDIN_FILENO))
|
||||
line = readline(minishell->prompt.ps1);
|
||||
line = readline(get_var("PS1", minishell));
|
||||
else
|
||||
line = get_next_line(STDIN_FILENO);
|
||||
handle_sigint_status(minishell);
|
||||
@@ -67,6 +60,8 @@ void minishell_clear(
|
||||
t_minishell *minishell
|
||||
){
|
||||
rl_clear_history();
|
||||
if (minishell->variables.internal != NULL)
|
||||
ft_hashmap_clear(&minishell->variables.internal, free);
|
||||
if (minishell->variables.environment != NULL)
|
||||
ft_hashmap_clear(&minishell->variables.environment, free);
|
||||
if (minishell->builtins != NULL)
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/02/13 21:09:23 by sede-san #+# #+# */
|
||||
/* Updated: 2026/02/13 21:09:23 by sede-san ### ########.fr */
|
||||
/* Updated: 2026/02/14 13:03:15 by sede-san ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -31,6 +31,7 @@ bool handle_eof(
|
||||
return (false);
|
||||
if (isatty(STDIN_FILENO))
|
||||
ft_putendl("exit");
|
||||
free(line);
|
||||
minishell->exit = true;
|
||||
return (true);
|
||||
}
|
||||
|
||||
@@ -58,10 +58,18 @@ void command_add_tokens(
|
||||
)
|
||||
{
|
||||
t_token *token;
|
||||
int io_number;
|
||||
|
||||
token = (t_token *)(*tokens)->content;
|
||||
if (parser_token_is_fd_prefix(*tokens, &io_number))
|
||||
{
|
||||
*tokens = (*tokens)->next;
|
||||
token = (t_token *)(*tokens)->content;
|
||||
redirection_add_with_fd(tokens, token, command, io_number);
|
||||
return ;
|
||||
}
|
||||
if (is_redirection(token))
|
||||
redirection_add(tokens, token, command);
|
||||
redirection_add_with_fd(tokens, token, command, -1);
|
||||
else
|
||||
words_add(tokens, command);
|
||||
}
|
||||
|
||||
54
src/parser/parser_command_fd.c
Normal file
54
src/parser/parser_command_fd.c
Normal file
@@ -0,0 +1,54 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* parser_command_fd.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/02/14 16:40:00 by sede-san #+# #+# */
|
||||
/* Updated: 2026/02/14 16:40:00 by sede-san ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "parser.h"
|
||||
#include <limits.h>
|
||||
|
||||
static bool parse_io_number(const char *value, int *io_number)
|
||||
{
|
||||
long number;
|
||||
size_t i;
|
||||
|
||||
if (value == NULL || value[0] == '\0')
|
||||
return (false);
|
||||
number = 0;
|
||||
i = 0;
|
||||
while (value[i] != '\0')
|
||||
{
|
||||
if (!ft_isdigit(value[i]))
|
||||
return (false);
|
||||
if (number > (INT_MAX - (value[i] - '0')) / 10)
|
||||
return (false);
|
||||
number = (number * 10) + (value[i] - '0');
|
||||
i++;
|
||||
}
|
||||
*io_number = (int)number;
|
||||
return (true);
|
||||
}
|
||||
|
||||
bool parser_token_is_fd_prefix(
|
||||
t_list *token_node,
|
||||
int *io_number
|
||||
)
|
||||
{
|
||||
t_token *token;
|
||||
t_token *next_token;
|
||||
|
||||
if (token_node == NULL || token_node->next == NULL)
|
||||
return (false);
|
||||
token = (t_token *)token_node->content;
|
||||
next_token = (t_token *)token_node->next->content;
|
||||
if (token == NULL || next_token == NULL || token->type != TOKEN_WORD
|
||||
|| !is_redirection(next_token))
|
||||
return (false);
|
||||
return (parse_io_number(token->value, io_number));
|
||||
}
|
||||
@@ -6,92 +6,111 @@
|
||||
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/02/13 21:24:45 by sede-san #+# #+# */
|
||||
/* Updated: 2026/02/13 21:24:45 by sede-san ### ########.fr */
|
||||
/* Updated: 2026/02/14 06:56:00 by sede-san ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "parser.h"
|
||||
#include "errors.h"
|
||||
#include "parser_expand_internal.h"
|
||||
|
||||
static char *replace_value(char *original, char *value, int start, int end);
|
||||
static void expand_variable(char **argument, int *i, t_minishell *minishell);
|
||||
static void expand_argument(char **argument, t_minishell *minishell);
|
||||
|
||||
static char *replace_value(
|
||||
char *original,
|
||||
char *value,
|
||||
int start,
|
||||
int end
|
||||
static void command_clear_argv_expand(
|
||||
t_command *command
|
||||
)
|
||||
{
|
||||
const char *before = ft_substr(original, 0, start);
|
||||
const char *after = ft_substr(original, end, ft_strlen(original) - end);
|
||||
const char *expanded = ft_strnjoin(3, before, value, after);
|
||||
int i;
|
||||
|
||||
free((char *)before);
|
||||
free((char *)after);
|
||||
return ((char *)expanded);
|
||||
i = 0;
|
||||
while (i < command->argc)
|
||||
{
|
||||
free(command->argv[i]);
|
||||
i++;
|
||||
}
|
||||
free(command->argv);
|
||||
}
|
||||
|
||||
static void expand_variable(
|
||||
char **argument,
|
||||
int *i,
|
||||
static bool command_set_expanded_argv(
|
||||
t_command *command,
|
||||
t_list *expanded_args,
|
||||
t_minishell *minishell
|
||||
)
|
||||
{
|
||||
char *expanded;
|
||||
char *variable_name;
|
||||
char *variable_value;
|
||||
const int start = *i + 1;
|
||||
int end;
|
||||
|
||||
end = start;
|
||||
while (ft_isalnum((*argument)[end]) || (*argument)[end] == '_')
|
||||
end++;
|
||||
variable_name = ft_substr(*argument, start, end - start);
|
||||
if (variable_name == NULL)
|
||||
return (minishell->exit = true, malloc_error());
|
||||
variable_value = get_env(variable_name, minishell);
|
||||
free(variable_name);
|
||||
if (variable_value == NULL)
|
||||
variable_value = "";
|
||||
expanded = replace_value(*argument, variable_value, start - 1, end);
|
||||
if (expanded == NULL)
|
||||
return (minishell->exit = true, malloc_error());
|
||||
*i += ft_strlen(variable_value);
|
||||
free(*argument);
|
||||
*argument = expanded;
|
||||
}
|
||||
|
||||
static void expand_argument(
|
||||
char **argument,
|
||||
t_minishell *minishell
|
||||
)
|
||||
{
|
||||
bool in_single_quote;
|
||||
bool in_double_quote;
|
||||
char **argv;
|
||||
t_list *current;
|
||||
int argc;
|
||||
int i;
|
||||
|
||||
in_single_quote = false;
|
||||
in_double_quote = false;
|
||||
argc = ft_lstsize(expanded_args);
|
||||
argv = (char **)malloc(sizeof(char *) * (argc + 1));
|
||||
if (argv == NULL)
|
||||
return (ft_lstclear(&expanded_args, free),
|
||||
parser_expand_malloc_error(minishell), false);
|
||||
i = 0;
|
||||
while ((*argument)[i] != '\0')
|
||||
current = expanded_args;
|
||||
while (current != NULL)
|
||||
{
|
||||
if ((*argument)[i] == '$' && !in_single_quote)
|
||||
{
|
||||
expand_variable(argument, &i, minishell);
|
||||
if (*argument == NULL)
|
||||
return (minishell->exit = true, malloc_error());
|
||||
}
|
||||
argv[i++] = (char *)current->content;
|
||||
current = current->next;
|
||||
}
|
||||
argv[i] = NULL;
|
||||
ft_lstclear_nodes(&expanded_args);
|
||||
command_clear_argv_expand(command);
|
||||
command->argc = argc;
|
||||
command->argv = argv;
|
||||
return (true);
|
||||
}
|
||||
|
||||
static bool expand_argv(
|
||||
t_command *command,
|
||||
t_minishell *minishell
|
||||
)
|
||||
{
|
||||
int i;
|
||||
t_list *expanded_args;
|
||||
t_list *fields;
|
||||
t_list *last;
|
||||
|
||||
i = 0;
|
||||
expanded_args = NULL;
|
||||
while (i < command->argc)
|
||||
{
|
||||
fields = NULL;
|
||||
if (!parser_expand_word_fields(command->argv[i], minishell,
|
||||
true, &fields))
|
||||
return (ft_lstclear(&expanded_args, free), false);
|
||||
if (expanded_args == NULL)
|
||||
expanded_args = fields;
|
||||
else
|
||||
{
|
||||
if ((*argument)[i] == '\'' && !in_double_quote)
|
||||
in_single_quote = !in_single_quote;
|
||||
else if ((*argument)[i] == '"' && !in_single_quote)
|
||||
in_double_quote = !in_double_quote;
|
||||
i++;
|
||||
last = ft_lstlast(expanded_args);
|
||||
if (last != NULL)
|
||||
last->next = fields;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return (command_set_expanded_argv(command, expanded_args, minishell));
|
||||
}
|
||||
|
||||
static bool expand_redirections(
|
||||
t_list *redirections,
|
||||
t_minishell *minishell
|
||||
)
|
||||
{
|
||||
t_redirection *redirection;
|
||||
char *expanded;
|
||||
bool expand_vars;
|
||||
|
||||
while (redirections != NULL)
|
||||
{
|
||||
redirection = (t_redirection *)redirections->content;
|
||||
expand_vars = (redirection->type != TOKEN_HEREDOC);
|
||||
expanded = parser_expand_word(redirection->target, minishell,
|
||||
expand_vars);
|
||||
if (expanded == NULL)
|
||||
return (false);
|
||||
free(redirection->target);
|
||||
redirection->target = expanded;
|
||||
redirections = redirections->next;
|
||||
}
|
||||
return (true);
|
||||
}
|
||||
|
||||
void expand(
|
||||
@@ -99,24 +118,23 @@ void expand(
|
||||
t_minishell *minishell
|
||||
)
|
||||
{
|
||||
t_list *current_command;
|
||||
t_list *current;
|
||||
t_command *command;
|
||||
int i;
|
||||
|
||||
current_command = *commands;
|
||||
while (current_command != NULL)
|
||||
if (commands == NULL || *commands == NULL)
|
||||
return ;
|
||||
current = *commands;
|
||||
while (current != NULL)
|
||||
{
|
||||
command = (t_command *)current_command->content;
|
||||
i = 0;
|
||||
while (i < command->argc)
|
||||
command = (t_command *)current->content;
|
||||
if (!expand_argv(command, minishell)
|
||||
|| !expand_redirections(command->redirections, minishell)
|
||||
|| !expand_redirections(command->heredocs, minishell))
|
||||
{
|
||||
expand_argument(&command->argv[i], minishell);
|
||||
if (command->argv[i] == NULL)
|
||||
ft_lstclear(commands, (void (*)(void *))command_clear);
|
||||
i++;
|
||||
ft_lstclear(commands, (void (*)(void *))command_clear);
|
||||
*commands = NULL;
|
||||
return ;
|
||||
}
|
||||
if (command == NULL)
|
||||
return (ft_lstclear(commands, (void (*)(void *))command_clear));
|
||||
current_command = current_command->next;
|
||||
current = current->next;
|
||||
}
|
||||
}
|
||||
|
||||
106
src/parser/parser_expand_fields.c
Normal file
106
src/parser/parser_expand_fields.c
Normal file
@@ -0,0 +1,106 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* parser_expand_fields.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/02/14 13:15:00 by sede-san #+# #+# */
|
||||
/* Updated: 2026/02/14 13:15:00 by sede-san ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "parser_expand_internal.h"
|
||||
|
||||
static t_fields_ctx init_fields_ctx(
|
||||
t_list **fields,
|
||||
char **current,
|
||||
t_field_state *state
|
||||
)
|
||||
{
|
||||
t_fields_ctx ctx;
|
||||
|
||||
ctx.fields = fields;
|
||||
ctx.current = current;
|
||||
ctx.touched = &state->touched;
|
||||
ctx.in_single_quote = &state->in_single_quote;
|
||||
ctx.in_double_quote = &state->in_double_quote;
|
||||
return (ctx);
|
||||
}
|
||||
|
||||
static bool finish_word_fields(
|
||||
t_fields_ctx ctx
|
||||
)
|
||||
{
|
||||
if (*ctx.in_single_quote || *ctx.in_double_quote)
|
||||
{
|
||||
ft_lstclear(ctx.fields, free);
|
||||
free(*ctx.current);
|
||||
syntax_error_unexpected_token(NULL);
|
||||
ctx.minishell->exit_status = 2;
|
||||
return (false);
|
||||
}
|
||||
if ((*ctx.touched || (*ctx.current)[0] != '\0')
|
||||
&& !parser_fields_push_field(ctx))
|
||||
return (false);
|
||||
free(*ctx.current);
|
||||
return (true);
|
||||
}
|
||||
|
||||
static bool process_word_fields(
|
||||
const char *word,
|
||||
t_fields_ctx ctx
|
||||
)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
i = 0;
|
||||
while (word[i] != '\0')
|
||||
{
|
||||
if (!parser_fields_step(word, &i, ctx))
|
||||
return (false);
|
||||
}
|
||||
return (true);
|
||||
}
|
||||
|
||||
static bool set_empty_word_field(
|
||||
t_list **fields,
|
||||
t_minishell *minishell
|
||||
)
|
||||
{
|
||||
t_list *node;
|
||||
|
||||
node = ft_lstnew(ft_strdup(""));
|
||||
if (node == NULL || node->content == NULL)
|
||||
return (free(node), parser_expand_malloc_error(minishell), false);
|
||||
*fields = node;
|
||||
return (true);
|
||||
}
|
||||
|
||||
bool parser_expand_word_fields(
|
||||
const char *word,
|
||||
t_minishell *minishell,
|
||||
bool expand_vars,
|
||||
t_list **fields
|
||||
)
|
||||
{
|
||||
char *current;
|
||||
t_field_state state;
|
||||
t_fields_ctx ctx;
|
||||
|
||||
*fields = NULL;
|
||||
if (word[0] == '\0')
|
||||
return (set_empty_word_field(fields, minishell));
|
||||
current = ft_strdup("");
|
||||
if (current == NULL)
|
||||
return (parser_expand_malloc_error(minishell), false);
|
||||
state.touched = false;
|
||||
state.in_single_quote = false;
|
||||
state.in_double_quote = false;
|
||||
ctx = init_fields_ctx(fields, ¤t, &state);
|
||||
ctx.minishell = minishell;
|
||||
ctx.expand_vars = expand_vars;
|
||||
if (!process_word_fields(word, ctx))
|
||||
return (ft_lstclear(fields, free), free(current), false);
|
||||
return (finish_word_fields(ctx));
|
||||
}
|
||||
85
src/parser/parser_expand_fields_escape.c
Normal file
85
src/parser/parser_expand_fields_escape.c
Normal file
@@ -0,0 +1,85 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* parser_expand_fields_escape.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/02/14 16:40:00 by sede-san #+# #+# */
|
||||
/* Updated: 2026/02/14 16:40:00 by sede-san ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "parser_expand_internal.h"
|
||||
|
||||
static bool is_backslash_escaped_in_double_quote(
|
||||
char c
|
||||
)
|
||||
{
|
||||
return (c == '$' || c == '\"' || c == '\\');
|
||||
}
|
||||
|
||||
static char get_escaped_char(
|
||||
const char *word,
|
||||
size_t *i
|
||||
)
|
||||
{
|
||||
char value;
|
||||
|
||||
if (word[*i + 1] == '\0')
|
||||
value = word[(*i)++];
|
||||
else
|
||||
{
|
||||
value = word[*i + 1];
|
||||
*i += 2;
|
||||
}
|
||||
return (value);
|
||||
}
|
||||
|
||||
bool parser_fields_handle_backslash(
|
||||
const char *word,
|
||||
size_t *i,
|
||||
t_fields_ctx ctx,
|
||||
bool *handled
|
||||
)
|
||||
{
|
||||
char value[2];
|
||||
|
||||
*handled = false;
|
||||
if (word[*i] != '\\' || *ctx.in_single_quote)
|
||||
return (true);
|
||||
if (*ctx.in_double_quote
|
||||
&& !is_backslash_escaped_in_double_quote(word[*i + 1]))
|
||||
return (true);
|
||||
*handled = true;
|
||||
value[0] = get_escaped_char(word, i);
|
||||
value[1] = '\0';
|
||||
if (!parser_fields_append_text(ctx.current, value, ctx.minishell))
|
||||
return (false);
|
||||
*ctx.touched = true;
|
||||
return (true);
|
||||
}
|
||||
|
||||
bool parser_fields_expand_tilde(
|
||||
const char *word,
|
||||
size_t *i,
|
||||
t_fields_ctx ctx,
|
||||
bool *handled
|
||||
)
|
||||
{
|
||||
char *home;
|
||||
|
||||
*handled = false;
|
||||
if (word[*i] != '~' || *ctx.in_single_quote || *ctx.in_double_quote
|
||||
|| *i != 0 || (word[*i + 1] != '\0' && word[*i + 1] != '/'))
|
||||
return (true);
|
||||
home = get_var("HOME", ctx.minishell);
|
||||
if (home == NULL)
|
||||
return (true);
|
||||
*handled = true;
|
||||
(*i)++;
|
||||
if (!parser_fields_append_text(ctx.current, home, ctx.minishell))
|
||||
return (false);
|
||||
*ctx.touched = true;
|
||||
return (true);
|
||||
}
|
||||
113
src/parser/parser_expand_fields_step.c
Normal file
113
src/parser/parser_expand_fields_step.c
Normal file
@@ -0,0 +1,113 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* parser_expand_fields_step.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/02/14 14:12:00 by sede-san #+# #+# */
|
||||
/* Updated: 2026/02/14 14:12:00 by sede-san ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "parser_expand_internal.h"
|
||||
|
||||
static bool handle_quote_char(
|
||||
const char *word,
|
||||
size_t *i,
|
||||
t_fields_ctx ctx
|
||||
)
|
||||
{
|
||||
if (word[*i] == '\'' && !*ctx.in_double_quote)
|
||||
{
|
||||
*ctx.in_single_quote = !*ctx.in_single_quote;
|
||||
*ctx.touched = true;
|
||||
(*i)++;
|
||||
return (true);
|
||||
}
|
||||
if (word[*i] == '\"' && !*ctx.in_single_quote)
|
||||
{
|
||||
*ctx.in_double_quote = !*ctx.in_double_quote;
|
||||
*ctx.touched = true;
|
||||
(*i)++;
|
||||
return (true);
|
||||
}
|
||||
return (false);
|
||||
}
|
||||
|
||||
static bool skip_dollar_quote_prefix(
|
||||
const char *word,
|
||||
size_t *i,
|
||||
t_fields_ctx ctx
|
||||
)
|
||||
{
|
||||
if (word[*i] == '$' && !*ctx.in_single_quote && !*ctx.in_double_quote
|
||||
&& word[*i + 1] != '\0'
|
||||
&& (word[*i + 1] == '\'' || word[*i + 1] == '\"'))
|
||||
{
|
||||
(*i)++;
|
||||
return (true);
|
||||
}
|
||||
return (false);
|
||||
}
|
||||
|
||||
static bool expand_dollar_token(
|
||||
const char *word,
|
||||
size_t *i,
|
||||
t_fields_ctx ctx,
|
||||
bool *handled
|
||||
)
|
||||
{
|
||||
char *expanded;
|
||||
|
||||
*handled = false;
|
||||
if (word[*i] != '$' || *ctx.in_single_quote || !ctx.expand_vars)
|
||||
return (true);
|
||||
*handled = true;
|
||||
expanded = parser_expand_variable(word, i, ctx.minishell);
|
||||
if (expanded == NULL)
|
||||
return (false);
|
||||
if (!*ctx.in_double_quote
|
||||
&& !parser_fields_expand_unquoted_value(ctx, expanded))
|
||||
return (free(expanded), false);
|
||||
if (*ctx.in_double_quote && !parser_fields_append_text(ctx.current,
|
||||
expanded, ctx.minishell))
|
||||
return (free(expanded), false);
|
||||
if (*ctx.in_double_quote && expanded[0] != '\0')
|
||||
*ctx.touched = true;
|
||||
free(expanded);
|
||||
return (true);
|
||||
}
|
||||
|
||||
bool parser_fields_step(
|
||||
const char *word,
|
||||
size_t *i,
|
||||
t_fields_ctx ctx
|
||||
)
|
||||
{
|
||||
char value[2];
|
||||
bool handled;
|
||||
|
||||
if (handle_quote_char(word, i, ctx))
|
||||
return (true);
|
||||
if (!parser_fields_handle_backslash(word, i, ctx, &handled))
|
||||
return (false);
|
||||
if (handled)
|
||||
return (true);
|
||||
if (!parser_fields_expand_tilde(word, i, ctx, &handled))
|
||||
return (false);
|
||||
if (handled)
|
||||
return (true);
|
||||
if (skip_dollar_quote_prefix(word, i, ctx))
|
||||
return (true);
|
||||
if (!expand_dollar_token(word, i, ctx, &handled))
|
||||
return (false);
|
||||
if (handled)
|
||||
return (true);
|
||||
value[0] = word[(*i)++];
|
||||
value[1] = '\0';
|
||||
if (!parser_fields_append_text(ctx.current, value, ctx.minishell))
|
||||
return (false);
|
||||
*ctx.touched = true;
|
||||
return (true);
|
||||
}
|
||||
100
src/parser/parser_expand_fields_utils.c
Normal file
100
src/parser/parser_expand_fields_utils.c
Normal file
@@ -0,0 +1,100 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* parser_expand_fields_utils.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/02/14 14:12:00 by sede-san #+# #+# */
|
||||
/* Updated: 2026/02/14 14:12:00 by sede-san ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "parser_expand_internal.h"
|
||||
|
||||
bool parser_fields_append_text(
|
||||
char **current,
|
||||
const char *value,
|
||||
t_minishell *minishell
|
||||
)
|
||||
{
|
||||
char *joined;
|
||||
|
||||
joined = ft_strnjoin(2, *current, (char *)value);
|
||||
if (joined == NULL)
|
||||
{
|
||||
free(*current);
|
||||
*current = NULL;
|
||||
return (parser_expand_malloc_error(minishell), false);
|
||||
}
|
||||
free(*current);
|
||||
*current = joined;
|
||||
return (true);
|
||||
}
|
||||
|
||||
static bool is_ifs_char(
|
||||
char c
|
||||
)
|
||||
{
|
||||
return (c == ' ' || c == '\t' || c == '\n');
|
||||
}
|
||||
|
||||
bool parser_fields_push_field(
|
||||
t_fields_ctx ctx
|
||||
)
|
||||
{
|
||||
t_list *node;
|
||||
|
||||
node = ft_lstnew(*ctx.current);
|
||||
if (node == NULL)
|
||||
{
|
||||
free(*ctx.current);
|
||||
*ctx.current = NULL;
|
||||
return (parser_expand_malloc_error(ctx.minishell), false);
|
||||
}
|
||||
ft_lstadd_back(ctx.fields, node);
|
||||
*ctx.current = ft_strdup("");
|
||||
if (*ctx.current == NULL)
|
||||
return (ft_lstclear(ctx.fields, free),
|
||||
parser_expand_malloc_error(ctx.minishell), false);
|
||||
*ctx.touched = false;
|
||||
return (true);
|
||||
}
|
||||
|
||||
static void skip_ifs(
|
||||
const char *expanded,
|
||||
size_t *i
|
||||
)
|
||||
{
|
||||
while (expanded[*i] != '\0' && is_ifs_char(expanded[*i]))
|
||||
(*i)++;
|
||||
}
|
||||
|
||||
bool parser_fields_expand_unquoted_value(
|
||||
t_fields_ctx ctx,
|
||||
const char *expanded
|
||||
)
|
||||
{
|
||||
size_t i;
|
||||
char value[2];
|
||||
|
||||
i = 0;
|
||||
while (expanded[i] != '\0')
|
||||
{
|
||||
if (is_ifs_char(expanded[i]))
|
||||
{
|
||||
if ((*ctx.touched || (*ctx.current)[0] != '\0')
|
||||
&& !parser_fields_push_field(ctx))
|
||||
return (false);
|
||||
skip_ifs(expanded, &i);
|
||||
continue ;
|
||||
}
|
||||
value[0] = expanded[i];
|
||||
value[1] = '\0';
|
||||
if (!parser_fields_append_text(ctx.current, value, ctx.minishell))
|
||||
return (false);
|
||||
*ctx.touched = true;
|
||||
i++;
|
||||
}
|
||||
return (true);
|
||||
}
|
||||
67
src/parser/parser_expand_internal.h
Normal file
67
src/parser/parser_expand_internal.h
Normal file
@@ -0,0 +1,67 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* parser_expand_internal.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/02/14 06:56:00 by sede-san #+# #+# */
|
||||
/* Updated: 2026/02/14 06:56:00 by sede-san ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef PARSER_EXPAND_INTERNAL_H
|
||||
# define PARSER_EXPAND_INTERNAL_H
|
||||
|
||||
# include "parser.h"
|
||||
# include "errors.h"
|
||||
# include "variables.h"
|
||||
|
||||
typedef struct s_fields_ctx t_fields_ctx;
|
||||
|
||||
void parser_expand_malloc_error(t_minishell *minishell);
|
||||
char *parser_expand_variable(const char *word, size_t *i,
|
||||
t_minishell *minishell);
|
||||
char *parser_expand_word(const char *word, t_minishell *minishell,
|
||||
bool expand_vars);
|
||||
bool parser_expand_word_fields(const char *word, t_minishell *minishell,
|
||||
bool expand_vars, t_list **fields);
|
||||
bool parser_fields_append_text(char **current, const char *value,
|
||||
t_minishell *minishell);
|
||||
bool parser_fields_push_field(t_fields_ctx ctx);
|
||||
bool parser_fields_expand_unquoted_value(t_fields_ctx ctx,
|
||||
const char *expanded);
|
||||
bool parser_fields_step(const char *word, size_t *i, t_fields_ctx ctx);
|
||||
bool parser_fields_handle_backslash(const char *word, size_t *i,
|
||||
t_fields_ctx ctx, bool *handled);
|
||||
bool parser_fields_expand_tilde(const char *word, size_t *i,
|
||||
t_fields_ctx ctx, bool *handled);
|
||||
|
||||
typedef struct s_word_ctx
|
||||
{
|
||||
char **result;
|
||||
bool *in_single_quote;
|
||||
bool *in_double_quote;
|
||||
t_minishell *minishell;
|
||||
bool expand_vars;
|
||||
} t_word_ctx;
|
||||
|
||||
typedef struct s_fields_ctx
|
||||
{
|
||||
t_list **fields;
|
||||
char **current;
|
||||
bool *touched;
|
||||
bool *in_single_quote;
|
||||
bool *in_double_quote;
|
||||
t_minishell *minishell;
|
||||
bool expand_vars;
|
||||
} t_fields_ctx;
|
||||
|
||||
typedef struct s_field_state
|
||||
{
|
||||
bool touched;
|
||||
bool in_single_quote;
|
||||
bool in_double_quote;
|
||||
} t_field_state;
|
||||
|
||||
#endif
|
||||
82
src/parser/parser_expand_variable.c
Normal file
82
src/parser/parser_expand_variable.c
Normal file
@@ -0,0 +1,82 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* parser_expand_variable.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/02/14 06:56:00 by sede-san #+# #+# */
|
||||
/* Updated: 2026/02/14 06:56:00 by sede-san ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "parser_expand_internal.h"
|
||||
|
||||
void parser_expand_malloc_error(
|
||||
t_minishell *minishell
|
||||
)
|
||||
{
|
||||
minishell->exit = true;
|
||||
malloc_error();
|
||||
}
|
||||
|
||||
static bool is_var_char(
|
||||
char c
|
||||
)
|
||||
{
|
||||
return (ft_isalnum(c) || c == '_');
|
||||
}
|
||||
|
||||
static char *dup_or_error(
|
||||
const char *value,
|
||||
t_minishell *minishell
|
||||
)
|
||||
{
|
||||
char *dup;
|
||||
|
||||
dup = ft_strdup(value);
|
||||
if (dup == NULL)
|
||||
return (parser_expand_malloc_error(minishell), NULL);
|
||||
return (dup);
|
||||
}
|
||||
|
||||
static char *expand_named_variable(
|
||||
const char *word,
|
||||
size_t *i,
|
||||
t_minishell *minishell
|
||||
)
|
||||
{
|
||||
char *name;
|
||||
char *value;
|
||||
size_t start;
|
||||
|
||||
start = *i;
|
||||
while (word[*i] != '\0' && is_var_char(word[*i]))
|
||||
(*i)++;
|
||||
name = ft_substr(word, start, *i - start);
|
||||
if (name == NULL)
|
||||
return (parser_expand_malloc_error(minishell), NULL);
|
||||
value = get_var(name, minishell);
|
||||
free(name);
|
||||
if (value == NULL)
|
||||
value = "";
|
||||
return (dup_or_error(value, minishell));
|
||||
}
|
||||
|
||||
char *parser_expand_variable(const char *word, size_t *i,
|
||||
t_minishell *minishell)
|
||||
{
|
||||
char *expanded;
|
||||
|
||||
(*i)++;
|
||||
if (word[*i] == '?')
|
||||
{
|
||||
expanded = ft_itoa(minishell->exit_status);
|
||||
if (expanded == NULL)
|
||||
return (parser_expand_malloc_error(minishell), NULL);
|
||||
return ((*i)++, expanded);
|
||||
}
|
||||
if (word[*i] == '\0' || !is_var_char(word[*i]))
|
||||
return (dup_or_error("$", minishell));
|
||||
return (expand_named_variable(word, i, minishell));
|
||||
}
|
||||
104
src/parser/parser_expand_word.c
Normal file
104
src/parser/parser_expand_word.c
Normal file
@@ -0,0 +1,104 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* parser_expand_word.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/02/14 06:56:00 by sede-san #+# #+# */
|
||||
/* Updated: 2026/02/14 06:56:00 by sede-san ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "parser_expand_internal.h"
|
||||
|
||||
static bool word_toggle_quotes(
|
||||
char c,
|
||||
bool *in_single_quote,
|
||||
bool *in_double_quote
|
||||
)
|
||||
{
|
||||
if (c == '\'' && !*in_double_quote)
|
||||
return (*in_single_quote = !*in_single_quote, true);
|
||||
if (c == '\"' && !*in_single_quote)
|
||||
return (*in_double_quote = !*in_double_quote, true);
|
||||
return (false);
|
||||
}
|
||||
|
||||
static bool word_append(
|
||||
char **result,
|
||||
const char *value,
|
||||
t_minishell *minishell
|
||||
)
|
||||
{
|
||||
char *joined;
|
||||
|
||||
joined = ft_strnjoin(2, *result, (char *)value);
|
||||
if (joined == NULL)
|
||||
{
|
||||
free(*result);
|
||||
*result = NULL;
|
||||
return (parser_expand_malloc_error(minishell), false);
|
||||
}
|
||||
free(*result);
|
||||
*result = joined;
|
||||
return (true);
|
||||
}
|
||||
|
||||
static bool word_step(
|
||||
const char *word,
|
||||
size_t *i,
|
||||
t_word_ctx ctx
|
||||
)
|
||||
{
|
||||
char *expanded;
|
||||
char value[2];
|
||||
|
||||
if (word_toggle_quotes(word[*i], ctx.in_single_quote, ctx.in_double_quote))
|
||||
return ((*i)++, true);
|
||||
if (word[*i] == '$' && word[*i + 1] != '\0'
|
||||
&& !*ctx.in_single_quote && !*ctx.in_double_quote
|
||||
&& (word[*i + 1] == '\'' || word[*i + 1] == '\"'))
|
||||
return ((*i)++, true);
|
||||
if (word[*i] == '$' && !*ctx.in_single_quote && ctx.expand_vars)
|
||||
{
|
||||
expanded = parser_expand_variable(word, i, ctx.minishell);
|
||||
if (expanded == NULL || !word_append(ctx.result, expanded,
|
||||
ctx.minishell))
|
||||
return (free(expanded), false);
|
||||
return (free(expanded), true);
|
||||
}
|
||||
value[0] = word[*i];
|
||||
value[1] = '\0';
|
||||
(*i)++;
|
||||
return (word_append(ctx.result, value, ctx.minishell));
|
||||
}
|
||||
|
||||
char *parser_expand_word(
|
||||
const char *word,
|
||||
t_minishell *minishell,
|
||||
bool expand_vars
|
||||
)
|
||||
{
|
||||
char *result;
|
||||
size_t i;
|
||||
bool in_single_quote;
|
||||
bool in_double_quote;
|
||||
|
||||
result = ft_strdup("");
|
||||
if (result == NULL)
|
||||
return (parser_expand_malloc_error(minishell), NULL);
|
||||
i = 0;
|
||||
in_single_quote = false;
|
||||
in_double_quote = false;
|
||||
while (word[i] != '\0')
|
||||
{
|
||||
if (!word_step(word, &i, (t_word_ctx){&result, &in_single_quote,
|
||||
&in_double_quote, minishell, expand_vars}))
|
||||
return (NULL);
|
||||
}
|
||||
if (in_single_quote || in_double_quote)
|
||||
return (free(result), syntax_error_unexpected_token(NULL),
|
||||
minishell->exit_status = 2, NULL);
|
||||
return (result);
|
||||
}
|
||||
@@ -13,7 +13,9 @@
|
||||
#include "parser.h"
|
||||
#include "errors.h"
|
||||
|
||||
static t_redirection *redirection_new(t_list **tokens);
|
||||
static t_redirection *redirection_new(t_list **tokens, int io_number);
|
||||
static bool redirection_read_target(t_redirection *rd, t_list **tk);
|
||||
static bool has_quote_char(const char *value);
|
||||
|
||||
/**
|
||||
* @brief Creates a new redirection from a list of tokens.
|
||||
@@ -23,7 +25,8 @@ static t_redirection *redirection_new(t_list **tokens);
|
||||
* @return A new redirection or `NULL` on error.
|
||||
*/
|
||||
static t_redirection *redirection_new(
|
||||
t_list **tokens
|
||||
t_list **tokens,
|
||||
int io_number
|
||||
)
|
||||
{
|
||||
t_redirection *redirection;
|
||||
@@ -34,28 +37,62 @@ static t_redirection *redirection_new(
|
||||
return (malloc_error(), NULL);
|
||||
token = (t_token *)(*tokens)->content;
|
||||
redirection->type = token->type;
|
||||
*tokens = (*tokens)->next;
|
||||
if (*tokens == NULL)
|
||||
{
|
||||
free(redirection);
|
||||
return (syntax_error_unexpected_token(NULL), NULL);
|
||||
}
|
||||
token = (t_token *)(*tokens)->content;
|
||||
redirection->heredoc_expand = true;
|
||||
redirection->heredoc_ready = false;
|
||||
if (io_number < 0)
|
||||
redirection->io_number = STDOUT_FILENO;
|
||||
else
|
||||
redirection->io_number = io_number;
|
||||
if (io_number < 0 && (token->type == TOKEN_REDIRECT_IN
|
||||
|| token->type == TOKEN_HEREDOC))
|
||||
redirection->io_number = STDIN_FILENO;
|
||||
if (!redirection_read_target(redirection, tokens))
|
||||
return (free(redirection), NULL);
|
||||
return (redirection);
|
||||
}
|
||||
|
||||
static bool redirection_read_target(
|
||||
t_redirection *rd,
|
||||
t_list **tk
|
||||
)
|
||||
{
|
||||
t_token *token;
|
||||
|
||||
*tk = (*tk)->next;
|
||||
if (*tk == NULL)
|
||||
return (syntax_error_unexpected_token(NULL), false);
|
||||
token = (t_token *)(*tk)->content;
|
||||
if (token->type != TOKEN_WORD)
|
||||
{
|
||||
free(redirection);
|
||||
while (*tokens != NULL)
|
||||
*tokens = (*tokens)->next;
|
||||
return (syntax_error_unexpected_token(token), NULL);
|
||||
while (*tk != NULL)
|
||||
*tk = (*tk)->next;
|
||||
return (syntax_error_unexpected_token(token), false);
|
||||
}
|
||||
redirection->target = ft_strdup(token->value);
|
||||
if (redirection->target == NULL)
|
||||
if (rd->type == TOKEN_HEREDOC && has_quote_char(token->value))
|
||||
rd->heredoc_expand = false;
|
||||
rd->target = ft_strdup(token->value);
|
||||
if (rd->target == NULL)
|
||||
return (malloc_error(), false);
|
||||
*tk = (*tk)->next;
|
||||
return (true);
|
||||
}
|
||||
|
||||
static bool has_quote_char(
|
||||
const char *value
|
||||
)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
if (value == NULL)
|
||||
return (false);
|
||||
i = 0;
|
||||
while (value[i] != '\0')
|
||||
{
|
||||
free(redirection);
|
||||
return (malloc_error(), NULL);
|
||||
if (value[i] == '\'' || value[i] == '\"')
|
||||
return (true);
|
||||
i++;
|
||||
}
|
||||
*tokens = (*tokens)->next;
|
||||
return (redirection);
|
||||
return (false);
|
||||
}
|
||||
|
||||
void redirection_clear(
|
||||
@@ -69,16 +106,17 @@ void redirection_clear(
|
||||
}
|
||||
}
|
||||
|
||||
void redirection_add(
|
||||
void redirection_add_with_fd(
|
||||
t_list **tokens,
|
||||
t_token *token,
|
||||
t_command **command
|
||||
t_command **command,
|
||||
int io_number
|
||||
)
|
||||
{
|
||||
t_redirection *redirection;
|
||||
t_list *redirection_tokens;
|
||||
|
||||
redirection = redirection_new(tokens);
|
||||
redirection = redirection_new(tokens, io_number);
|
||||
if (redirection == NULL)
|
||||
{
|
||||
command_clear(*command);
|
||||
@@ -91,8 +129,6 @@ void redirection_add(
|
||||
free(redirection);
|
||||
return (malloc_error());
|
||||
}
|
||||
if (token->type == TOKEN_HEREDOC)
|
||||
ft_lstadd_back(&(*command)->heredocs, redirection_tokens);
|
||||
else
|
||||
ft_lstadd_back(&(*command)->redirections, redirection_tokens);
|
||||
(void)token;
|
||||
ft_lstadd_back(&(*command)->redirections, redirection_tokens);
|
||||
}
|
||||
|
||||
@@ -6,48 +6,63 @@
|
||||
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/02/13 21:29:44 by sede-san #+# #+# */
|
||||
/* Updated: 2026/02/13 21:29:44 by sede-san ### ########.fr */
|
||||
/* Updated: 2026/02/14 03:34:38 by sede-san ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "parser.h"
|
||||
|
||||
char **args_to_array(t_list **args, size_t argc);
|
||||
void command_clear_argv(t_command *command);
|
||||
|
||||
/**
|
||||
* @brief Converts a list of arguments into an array.
|
||||
*
|
||||
* @param args The list of arguments to convert.
|
||||
* @param argc The number of arguments in the list.
|
||||
*
|
||||
* @return An array of arguments or `NULL` on error.
|
||||
*
|
||||
* @note The `args` list is cleared after the conversion and set to NULL.
|
||||
*/
|
||||
char **args_to_array(
|
||||
static bool args_add_word(
|
||||
t_list **args,
|
||||
size_t argc
|
||||
const char *value
|
||||
)
|
||||
{
|
||||
t_list *node;
|
||||
char *dup;
|
||||
|
||||
dup = ft_strdup(value);
|
||||
if (dup == NULL)
|
||||
return (false);
|
||||
node = ft_lstnew(dup);
|
||||
if (node == NULL)
|
||||
return (free(dup), false);
|
||||
ft_lstadd_back(args, node);
|
||||
return (true);
|
||||
}
|
||||
|
||||
static bool command_append_words(
|
||||
t_command *command,
|
||||
t_list **args,
|
||||
int new_argc
|
||||
)
|
||||
{
|
||||
char **argv;
|
||||
t_list *arg;
|
||||
size_t i;
|
||||
t_list *current;
|
||||
int i;
|
||||
|
||||
argv = (char **)malloc(sizeof(char *) * (argc + 1));
|
||||
argv = (char **)malloc(sizeof(char *) * (command->argc + new_argc + 1));
|
||||
if (argv == NULL)
|
||||
return (NULL);
|
||||
return (false);
|
||||
i = 0;
|
||||
arg = *args;
|
||||
while (arg != NULL)
|
||||
while (i < command->argc)
|
||||
{
|
||||
argv[i] = (char *)arg->content;
|
||||
arg = arg->next;
|
||||
argv[i] = command->argv[i];
|
||||
i++;
|
||||
}
|
||||
current = *args;
|
||||
while (current != NULL)
|
||||
{
|
||||
argv[i++] = (char *)current->content;
|
||||
current = current->next;
|
||||
}
|
||||
argv[i] = NULL;
|
||||
free(command->argv);
|
||||
ft_lstclear_nodes(args);
|
||||
return (argv);
|
||||
command->argv = argv;
|
||||
command->argc += new_argc;
|
||||
return (true);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -65,21 +80,41 @@ void words_add(
|
||||
t_list *args;
|
||||
t_list *arg;
|
||||
t_token *token;
|
||||
t_token *next_token;
|
||||
int new_argc;
|
||||
size_t i;
|
||||
bool is_fd_prefix;
|
||||
|
||||
args = NULL;
|
||||
new_argc = 0;
|
||||
arg = *tokens;
|
||||
token = (t_token *)arg->content;
|
||||
while (arg != NULL && token->type == TOKEN_WORD)
|
||||
{
|
||||
ft_lstadd_back(&args, ft_lstnew(ft_strdup(token->value)));
|
||||
(*command)->argc++;
|
||||
is_fd_prefix = false;
|
||||
if (arg->next != NULL && token->value != NULL
|
||||
&& token->value[0] != '\0')
|
||||
{
|
||||
next_token = (t_token *)arg->next->content;
|
||||
i = 0;
|
||||
while (token->value[i] != '\0' && ft_isdigit(token->value[i]))
|
||||
i++;
|
||||
is_fd_prefix = (token->value[i] == '\0' && is_redirection(next_token));
|
||||
}
|
||||
if (is_fd_prefix)
|
||||
break ;
|
||||
if (!args_add_word(&args, token->value))
|
||||
return (ft_lstclear(&args, free), command_clear(*command),
|
||||
*command = NULL, (void)0);
|
||||
new_argc++;
|
||||
arg = arg->next;
|
||||
if (arg != NULL)
|
||||
token = (t_token *)arg->content;
|
||||
}
|
||||
*tokens = arg;
|
||||
(*command)->argv = args_to_array(&args, (*command)->argc);
|
||||
ft_lstclear_nodes(&args);
|
||||
if (!command_append_words(*command, &args, new_argc))
|
||||
return (ft_lstclear(&args, free), command_clear(*command),
|
||||
*command = NULL, (void)0);
|
||||
}
|
||||
|
||||
void command_clear_argv(
|
||||
|
||||
@@ -6,65 +6,33 @@
|
||||
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/12/01 09:12:39 by sede-san #+# #+# */
|
||||
/* Updated: 2026/02/08 19:44:15 by sede-san ### ########.fr */
|
||||
/* Updated: 2026/02/14 13:30:34 by sede-san ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
#include "core.h"
|
||||
#include "variables.h"
|
||||
#include "errors.h"
|
||||
|
||||
/**
|
||||
* @brief Parses and stores environment variables from envp array into a hashmap
|
||||
* @brief Retrieves the value of an environment variable from the shell's
|
||||
* environment hashmap.
|
||||
*
|
||||
* This function iterates through the environment variables array (envp) and
|
||||
* splits each variable string on the '=' delimiter to separate the variable
|
||||
* name from its value. Each name-value pair is then stored in the minishell's
|
||||
* environment hashmap for later retrieval.
|
||||
* This function searches for the specified environment variable name in the
|
||||
* minishell's environment variable hashmap and returns its associated value.
|
||||
*
|
||||
* @param envp Array of environment variable strings in "NAME=value" format
|
||||
* @param msh Pointer to the minishell structure containing the environment
|
||||
* hashmap
|
||||
* @param name The name of the environment variable to retrieve.
|
||||
* @param minishell Pointer to the minishell object.
|
||||
*
|
||||
* @note The function assumes envp strings are in the standard format
|
||||
* "NAME=value"
|
||||
* @return The value of the environment variable if found, NULL if not found
|
||||
*/
|
||||
void set_envp(
|
||||
char **envp,
|
||||
t_minishell *msh
|
||||
) {
|
||||
char *equal_sign;
|
||||
char *key;
|
||||
char *value;
|
||||
|
||||
if (msh == NULL || envp == NULL)
|
||||
return ;
|
||||
msh->variables.environment
|
||||
= ft_hashmap_new(32, ft_hashmap_hashstr, ft_hashmap_strcmp);
|
||||
if (msh->variables.environment == NULL)
|
||||
return ;
|
||||
while (*envp != NULL)
|
||||
{
|
||||
equal_sign = ft_strchr(*envp, '=');
|
||||
if (equal_sign == NULL)
|
||||
{
|
||||
key = ft_strdup(*envp);
|
||||
value = ft_strdup("");
|
||||
}
|
||||
else
|
||||
{
|
||||
key = ft_substr(*envp, 0, equal_sign - *envp);
|
||||
value = ft_strdup(equal_sign + 1);
|
||||
}
|
||||
if (key == NULL || value == NULL)
|
||||
{
|
||||
free(key);
|
||||
free(value);
|
||||
ft_hashmap_clear(&msh->variables.environment, free);
|
||||
return ;
|
||||
}
|
||||
ft_hashmap_put(msh->variables.environment, key, value);
|
||||
envp++;
|
||||
}
|
||||
char *get_env(
|
||||
const char *name,
|
||||
t_minishell *minishell
|
||||
)
|
||||
{
|
||||
return (ft_hashmap_get(minishell->variables.environment, name));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -75,29 +43,66 @@ void set_envp(
|
||||
* the old value is freed to prevent memory leaks. If the variable is new,
|
||||
* a duplicate of the key name is created for storage.
|
||||
*
|
||||
* @param env_name The name of the environment variable to set
|
||||
* @param env_value The value to assign to the environment variable
|
||||
* @param msh Pointer to the minishell structure containing the
|
||||
* environment hashmap
|
||||
* @param name The name of the environment variable to set
|
||||
* @param value The value to assign to the environment variable
|
||||
* @param minishell Pointer to the minishell object.
|
||||
*/
|
||||
void set_env(
|
||||
const char *env_name,
|
||||
char *env_value,
|
||||
t_minishell *msh
|
||||
) {
|
||||
t_hashmap *environment;
|
||||
const char *key;
|
||||
char *old_value;
|
||||
const char *name,
|
||||
char *value,
|
||||
t_minishell *minishell
|
||||
)
|
||||
{
|
||||
t_hashmap *environment;
|
||||
char *key;
|
||||
char *val;
|
||||
char *old_value;
|
||||
|
||||
environment = msh->variables.environment;
|
||||
key = env_name;
|
||||
if (!ft_hashmap_contains_key(environment, key))
|
||||
key = ft_strdup(env_name);
|
||||
old_value = ft_hashmap_put(environment, key, ft_strdup(env_value));
|
||||
environment = minishell->variables.environment;
|
||||
if (name != NULL && !ft_hashmap_contains_key(environment, name))
|
||||
{
|
||||
key = ft_strdup(name);
|
||||
if (key == NULL)
|
||||
return (minishell->exit = true, malloc_error());
|
||||
}
|
||||
if (value == NULL)
|
||||
val = ft_strdup("");
|
||||
else
|
||||
val = ft_strdup(value);
|
||||
if (val == NULL)
|
||||
{
|
||||
if (key != name)
|
||||
free(key);
|
||||
return (minishell->exit = true, malloc_error());
|
||||
}
|
||||
old_value = ft_hashmap_put(environment, key, val);
|
||||
if (old_value != NULL)
|
||||
free(old_value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Removes an environment variable by name.
|
||||
*
|
||||
* @param name The name of the environment variable to remove.
|
||||
* @param minishell Pointer to the minishell structure.
|
||||
*
|
||||
* @note If the environment variable exists, it will be removed from the hashmap
|
||||
* and its associated value will be freed.
|
||||
*/
|
||||
void unset_env(
|
||||
const char *name,
|
||||
t_minishell *minishell
|
||||
)
|
||||
{
|
||||
t_hashmap *environment;
|
||||
char *val;
|
||||
|
||||
environment = minishell->variables.environment;
|
||||
val = ft_hashmap_remove(environment, (void *)name);
|
||||
if (val != NULL)
|
||||
free(val);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Converts the environment variables hashmap to an envp array format.
|
||||
*
|
||||
@@ -105,7 +110,7 @@ void set_env(
|
||||
* environment hashmap and converts them into a NULL-terminated array of
|
||||
* strings in the format "KEY=VALUE".
|
||||
*
|
||||
* @param msh Pointer to the minishell structure containing the environment
|
||||
* @param minishell Pointer to the minishell structure containing the environment
|
||||
* variables hashmap.
|
||||
*
|
||||
* @return A dynamically allocated array of strings representing environment
|
||||
@@ -120,17 +125,18 @@ void set_env(
|
||||
* the NULL terminator.
|
||||
*/
|
||||
char **get_envp(
|
||||
t_minishell *msh
|
||||
) {
|
||||
t_minishell *minishell
|
||||
)
|
||||
{
|
||||
char **envp;
|
||||
t_list *env_list;
|
||||
t_list *env;
|
||||
t_map_entry *entry;
|
||||
size_t i;
|
||||
|
||||
env_list = ft_hashmap_entries(msh->variables.environment);
|
||||
env_list = ft_hashmap_entries(minishell->variables.environment);
|
||||
envp = (char **)malloc(
|
||||
(msh->variables.environment->size + 1) * sizeof(char *)
|
||||
(minishell->variables.environment->size + 1) * sizeof(char *)
|
||||
);
|
||||
if (envp != NULL)
|
||||
{
|
||||
@@ -149,44 +155,71 @@ char **get_envp(
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Frees a dynamically allocated environment variables array
|
||||
* @brief Parses and stores environment variables from envp array into a hashmap
|
||||
*
|
||||
* This function deallocates memory for an array of strings that was previously
|
||||
* allocated by `get_envp()`. It iterates through each string in the array,
|
||||
* frees the memory for individual strings, and then frees the array itself.
|
||||
* This function iterates through the environment variables array (envp) and
|
||||
* splits each variable string on the '=' delimiter to separate the variable
|
||||
* name from its value. Each name-value pair is then stored in the minishell's
|
||||
* environment hashmap for later retrieval.
|
||||
*
|
||||
* @param envp Pointer to the array of environment variable strings to be freed.
|
||||
* Each string in the array should be dynamically allocated.
|
||||
* The array must be NULL-terminated.
|
||||
* @param envp Array of environment variable strings in "NAME=value" format
|
||||
* @param minishell Pointer to the minishell structure containing the environment
|
||||
* hashmap
|
||||
*
|
||||
* @note The function assumes envp strings are in the standard format
|
||||
* "NAME=value"
|
||||
*/
|
||||
void free_envp(
|
||||
char **envp
|
||||
) {
|
||||
size_t i;
|
||||
void set_envp(
|
||||
char **envp,
|
||||
t_minishell *minishell
|
||||
)
|
||||
{
|
||||
t_hashmap **environment;
|
||||
char *equal;
|
||||
char *key;
|
||||
char *value;
|
||||
char *new_shlvl;
|
||||
int shlvl;
|
||||
size_t key_len;
|
||||
|
||||
i = -1;
|
||||
while (envp[++i] != NULL)
|
||||
free(envp[i]);
|
||||
free(envp);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Retrieves the value of an environment variable from the shell's
|
||||
* environment hashmap.
|
||||
*
|
||||
* This function searches for the specified environment variable name in the
|
||||
* minishell's environment variable hashmap and returns its associated value.
|
||||
*
|
||||
* @param env_name The name of the environment variable to retrieve (e.g.,
|
||||
* "PATH", "HOME")
|
||||
* @param msh Pointer to the minishell structure containing the environment
|
||||
* variables hashmap
|
||||
*
|
||||
* @return The value of the environment variable if found, NULL if not found
|
||||
*/
|
||||
char *get_env(
|
||||
const char *env_name,
|
||||
t_minishell *msh
|
||||
) {
|
||||
return (ft_hashmap_get(msh->variables.environment, env_name));
|
||||
if (minishell == NULL || envp == NULL)
|
||||
return ;
|
||||
environment = &minishell->variables.environment;
|
||||
*environment = ft_hashmap_new(32, ft_hashmap_hashstr, ft_hashmap_strcmp);
|
||||
if (*environment == NULL)
|
||||
return ;
|
||||
while (*envp != NULL)
|
||||
{
|
||||
equal = ft_strchr(*envp, '=');
|
||||
if (equal == NULL)
|
||||
{
|
||||
envp++;
|
||||
continue ;
|
||||
}
|
||||
key_len = (size_t)(equal - *envp);
|
||||
key = ft_substr(*envp, 0, key_len);
|
||||
value = ft_strdup(equal + 1);
|
||||
if (key == NULL || value == NULL)
|
||||
{
|
||||
free(key);
|
||||
free(value);
|
||||
minishell->exit = true;
|
||||
return (malloc_error());
|
||||
}
|
||||
set_env(key, value, minishell);
|
||||
free(key);
|
||||
free(value);
|
||||
envp++;
|
||||
}
|
||||
value = get_env("SHLVL", minishell);
|
||||
shlvl = 0;
|
||||
if (value != NULL)
|
||||
shlvl = ft_atoi(value);
|
||||
if (shlvl < 0)
|
||||
shlvl = 0;
|
||||
new_shlvl = ft_itoa(shlvl + 1);
|
||||
if (new_shlvl == NULL)
|
||||
return (minishell->exit = true, malloc_error());
|
||||
set_env("SHLVL", new_shlvl, minishell);
|
||||
free(new_shlvl);
|
||||
}
|
||||
@@ -1,41 +0,0 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* environment_unset.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/02/09 22:16:00 by codex #+# #+# */
|
||||
/* Updated: 2026/02/09 22:16:00 by codex ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
#include "core.h"
|
||||
|
||||
void unset_env(
|
||||
const char *env_name,
|
||||
t_minishell *msh
|
||||
){
|
||||
t_hashmap *new_env;
|
||||
t_list *entries;
|
||||
t_list *current;
|
||||
t_map_entry *entry;
|
||||
|
||||
new_env = ft_hashmap_new(32, ft_hashmap_hashstr, ft_hashmap_strcmp);
|
||||
if (new_env == NULL)
|
||||
return ;
|
||||
entries = ft_hashmap_entries(msh->variables.environment);
|
||||
current = entries;
|
||||
while (current != NULL)
|
||||
{
|
||||
entry = current->content;
|
||||
if (ft_strcmp(entry->key, env_name) != 0)
|
||||
ft_hashmap_put(new_env,
|
||||
ft_strdup(entry->key), ft_strdup(entry->value));
|
||||
current = current->next;
|
||||
}
|
||||
ft_lstclear_nodes(&entries);
|
||||
ft_hashmap_clear(&msh->variables.environment, free);
|
||||
msh->variables.environment = new_env;
|
||||
}
|
||||
120
src/variables/internal/internal.c
Normal file
120
src/variables/internal/internal.c
Normal file
@@ -0,0 +1,120 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* internal.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/02/13 21:29:43 by sede-san #+# #+# */
|
||||
/* Updated: 2026/02/14 01:39:36 by sede-san ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
#include "core.h"
|
||||
#include "errors.h"
|
||||
|
||||
/**
|
||||
* @brief Retrieves the value of an internal variable by name.
|
||||
*
|
||||
* @param name The name of the internal variable to retrieve.
|
||||
* @param minishell Pointer to the minishell structure.
|
||||
*
|
||||
* @return The value of the internal variable if found, or NULL if not found.
|
||||
*/
|
||||
char *get_int(
|
||||
const char *name,
|
||||
t_minishell *minishell
|
||||
)
|
||||
{
|
||||
return (ft_hashmap_get(minishell->variables.internal, name));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Stores a variable as internal.
|
||||
*
|
||||
* @param name The name of the internal variable to set.
|
||||
* @param value The value to associate with the internal variable.
|
||||
* @param minishell Pointer to the minishell structure.
|
||||
*
|
||||
* @note If the internal variable already exists, its value will be updated and
|
||||
* the old value will be freed. If it does not exist, a new internal
|
||||
* variable will be created.
|
||||
*/
|
||||
void set_int(
|
||||
const char *name,
|
||||
char *value,
|
||||
t_minishell *minishell
|
||||
)
|
||||
{
|
||||
t_hashmap *internal;
|
||||
char *key;
|
||||
char *val;
|
||||
char *old_val;
|
||||
|
||||
internal = minishell->variables.internal;
|
||||
key = (char *)name;
|
||||
if (key != NULL && !ft_hashmap_contains_key(internal, key))
|
||||
{
|
||||
key = ft_strdup(name);
|
||||
if (key == NULL)
|
||||
return (minishell->exit = true, malloc_error());
|
||||
}
|
||||
val = value;
|
||||
if (val != NULL)
|
||||
val = ft_strdup(value);
|
||||
if (val == NULL)
|
||||
{
|
||||
if (key != name)
|
||||
free(key);
|
||||
return (minishell->exit = true, malloc_error());
|
||||
}
|
||||
old_val = ft_hashmap_put(internal, key, val);
|
||||
if (old_val != NULL)
|
||||
free(old_val);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Removes an internal variable by name.
|
||||
*
|
||||
* @param name The name of the internal variable to remove.
|
||||
* @param minishell Pointer to the minishell structure.
|
||||
*
|
||||
* @note If the internal variable exists, it will be removed from the hashmap
|
||||
* and its associated value will be freed.
|
||||
*/
|
||||
void unset_int(
|
||||
const char *name,
|
||||
t_minishell *minishell
|
||||
)
|
||||
{
|
||||
char *value;
|
||||
|
||||
value = ft_hashmap_remove(minishell->variables.internal, (void *)name);
|
||||
if (value != NULL)
|
||||
free(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Initializes the internal variables hashmap with default values.
|
||||
*
|
||||
* @param minishell Pointer to the minishell structure to initialize.
|
||||
*
|
||||
* @warning This function must be called only once during the initialization of
|
||||
* the minishell.
|
||||
*/
|
||||
void set_intp(
|
||||
t_minishell *minishell
|
||||
)
|
||||
{
|
||||
if (minishell == NULL)
|
||||
return ;
|
||||
minishell->variables.internal
|
||||
= ft_hashmap_new(16, ft_hashmap_hashstr, ft_hashmap_strcmp);
|
||||
if (minishell->variables.internal == NULL)
|
||||
return ;
|
||||
set_int("?", "0", minishell);
|
||||
set_int("_", "minishell", minishell);
|
||||
set_int("PS1", DEFAULT_PS1, minishell);
|
||||
set_int("PS2", DEFAULT_PS2, minishell);
|
||||
}
|
||||
50
src/variables/variables.c
Normal file
50
src/variables/variables.c
Normal file
@@ -0,0 +1,50 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* variables.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/02/13 21:41:44 by sede-san #+# #+# */
|
||||
/* Updated: 2026/02/14 00:52:12 by sede-san ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "variables.h"
|
||||
|
||||
/**
|
||||
* @brief Retrieves the value of a variable by name.
|
||||
*
|
||||
* @param name The name of the variable to retrieve.
|
||||
* @param minishell Pointer to the minishell structure.
|
||||
*
|
||||
* @return The value of the variable if found, or NULL if not found.
|
||||
*/
|
||||
char *get_var(
|
||||
const char *name,
|
||||
t_minishell *minishell
|
||||
)
|
||||
{
|
||||
char *value;
|
||||
|
||||
value = get_int(name, minishell);
|
||||
if (value == NULL)
|
||||
value = get_env(name, minishell);
|
||||
return (value);
|
||||
}
|
||||
|
||||
void set_var(const char *name, char *value, t_minishell *minishell)
|
||||
{
|
||||
if (ft_hashmap_contains_key(minishell->variables.internal, name))
|
||||
set_int(name, value, minishell);
|
||||
set_env(name, value, minishell);
|
||||
}
|
||||
|
||||
void unset_var(
|
||||
const char *name,
|
||||
t_minishell *minishell
|
||||
)
|
||||
{
|
||||
unset_int(name, minishell);
|
||||
unset_env(name, minishell);
|
||||
}
|
||||
Reference in New Issue
Block a user