terminado

This commit is contained in:
marcnava-42cursus
2026-02-14 15:04:25 +01:00
parent c1e622947d
commit 39e5719183
7 changed files with 206 additions and 87 deletions

View File

@@ -15,15 +15,15 @@
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 add_oldpwd, t_map_entry *oldpwd_entry);
static bool print_sorted_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};
void export_sort_entries(t_map_entry **entries, size_t count);
uint8_t builtin_export(
t_command cmd,
@@ -82,42 +82,36 @@ static uint8_t print_exported(
{
t_list *entries;
t_map_entry **sorted_entries;
t_map_entry oldpwd_entry;
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");
oldpwd_entry = (t_map_entry){"OLDPWD", NULL};
sorted_entries = (t_map_entry **)entries_to_array(entries, &count,
add_oldpwd);
!ft_hashmap_contains_key(msh->variables.environment, "OLDPWD"),
&oldpwd_entry);
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);
export_sort_entries(sorted_entries, count);
if (!print_sorted_entries(sorted_entries, count))
return (free(sorted_entries), EXIT_FAILURE);
return (free(sorted_entries), EXIT_SUCCESS);
}
static void **entries_to_array(
t_list *entries,
size_t *count,
bool add_oldpwd
bool add_oldpwd,
t_map_entry *oldpwd_entry
)
{
void **array;
t_list *node;
void **array;
t_list *node;
t_map_entry *entry;
size_t i;
size_t i;
*count = (size_t)ft_lstsize(entries) + (size_t)add_oldpwd;
array = (void **)malloc(sizeof(void *) * (*count));
@@ -133,35 +127,25 @@ static void **entries_to_array(
node = node->next;
}
if (add_oldpwd)
array[i++] = &g_oldpwd_entry;
array[i++] = oldpwd_entry;
*count = i;
return (array);
}
static void sort_entries(
static bool print_sorted_entries(
t_map_entry **entries,
size_t count
)
{
size_t i;
size_t j;
t_map_entry *tmp;
size_t i;
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++;
}
if (!export_print_declaration((char *)entries[i]->key,
(char *)entries[i]->value))
return (false);
i++;
}
return (true);
}

View File

@@ -0,0 +1,40 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* export_sort.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/14 23:55:00 by sede-san #+# #+# */
/* Updated: 2026/02/14 23:55:00 by sede-san ### ########.fr */
/* */
/* ************************************************************************** */
#include "builtins.h"
void export_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++;
}
}

View File

@@ -11,21 +11,14 @@
/* ************************************************************************** */
#include "core.h"
#include "signals_internal.h"
static int g_signal = 0;
static bool g_heredoc_mode = false;
int g_signal = 0;
static void sigint_handler(int signal)
static void sigint_handler_interactive(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();
@@ -36,8 +29,7 @@ void minishell_set_interactive_signals(void)
struct sigaction action;
ft_bzero(&action, sizeof(action));
g_heredoc_mode = false;
action.sa_handler = sigint_handler;
action.sa_handler = sigint_handler_interactive;
sigemptyset(&action.sa_mask);
sigaction(SIGINT, &action, NULL);
action.sa_handler = SIG_IGN;
@@ -49,26 +41,12 @@ 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_heredoc_signals(void)
{
struct sigaction action;
ft_bzero(&action, sizeof(action));
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);
}
bool minishell_consume_sigint(void)
{
if (g_signal != SIGINT)

View File

@@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* signals_heredoc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/14 23:40:00 by sede-san #+# #+# */
/* Updated: 2026/02/14 23:40:00 by sede-san ### ########.fr */
/* */
/* ************************************************************************** */
#include "core.h"
#include "signals_internal.h"
static void sigint_handler_heredoc(int signal)
{
g_signal = signal;
write(STDOUT_FILENO, "\n", 1);
}
void minishell_set_heredoc_signals(void)
{
struct sigaction action;
ft_bzero(&action, sizeof(action));
action.sa_handler = sigint_handler_heredoc;
sigemptyset(&action.sa_mask);
sigaction(SIGINT, &action, NULL);
action.sa_handler = SIG_IGN;
sigaction(SIGQUIT, &action, NULL);
}

View File

@@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* signals_internal.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/14 23:40:00 by sede-san #+# #+# */
/* Updated: 2026/02/14 23:40:00 by sede-san ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef SIGNALS_INTERNAL_H
# define SIGNALS_INTERNAL_H
extern int g_signal;
#endif

View File

@@ -53,28 +53,6 @@ int executor_heredoc_open_tmp(char **path_out)
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)

View File

@@ -0,0 +1,89 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* heredoc_readline.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/15 00:20:00 by sede-san #+# #+# */
/* Updated: 2026/02/15 00:20:00 by sede-san ### ########.fr */
/* */
/* ************************************************************************** */
#include "heredoc_internal.h"
#include "variables.h"
#include <errno.h>
static bool append_char(char **line, size_t *len, char c)
{
char *new_line;
size_t i;
new_line = (char *)malloc(*len + 2);
if (new_line == NULL)
return (free(*line), *line = NULL, false);
i = 0;
while (i < *len)
{
new_line[i] = (*line)[i];
i++;
}
new_line[*len] = c;
new_line[*len + 1] = '\0';
free(*line);
*line = new_line;
(*len)++;
return (true);
}
static char *read_line_stdin(void)
{
char *line;
char c;
size_t len;
ssize_t nread;
line = ft_strdup("");
if (line == NULL)
return (NULL);
len = 0;
errno = 0;
nread = read(STDIN_FILENO, &c, 1);
while (nread > 0)
{
if (c == '\n')
return (line);
if (!append_char(&line, &len, c))
return (NULL);
nread = read(STDIN_FILENO, &c, 1);
}
if (nread == -1 && errno == EINTR)
return (line);
if (len > 0)
return (line);
return (free(line), NULL);
}
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;
if (write(STDOUT_FILENO, prompt, ft_strlen(prompt)) == -1)
return (NULL);
return (read_line_stdin());
}