/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/22 17:14:49 by dkaiser #+# #+# */
-/* Updated: 2025/01/15 17:20:56 by dkaiser ### ########.fr */
+/* Updated: 2025/01/15 18:24:09 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
char *get_cmd_path(char *cmd, t_env *env, int *return_code);
int execute_cmd(t_cmd *cmd, t_env **env);
char *format_string(char *str, t_env *env);
-int set_return_code(int return_code, t_env **env);
+int set_return_code(int return_code, t_env **env);
int handle_redirections(t_redirection *redirs);
-void *error(int err_code, char *err_text, int exit_code, int *ret_code);
-
+void *error(int err_code, char *err_text, int exit_code,
+ int *ret_code);
+char *read_heredoc(char *delimiter);
#endif
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/02 13:49:31 by dkaiser #+# #+# */
-/* Updated: 2025/01/15 18:10:46 by dkaiser ### ########.fr */
+/* Updated: 2025/01/15 18:33:36 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
static void collect_and_check_redir(t_redirection *result, t_token **cur,
t_env *env);
-static void set_redir(t_redirection *redir, int type, char *specifier,
+static void set_redir(t_redirection *redir, int type, char *spec,
t_env *env);
-
-static char *read_heredoc(char *delimiter)
-{
- char *line;
- char *result;
- char *temp;
- size_t total_length;
- size_t line_length;
-
- total_length = 0;
- result = NULL;
- while (1)
- {
- line = readline("> ");
- if (!line || ft_strcmp(line, delimiter) == 0)
- {
- free(line);
- break ;
- }
- line_length = ft_strlen(line) + 1;
- temp = malloc(total_length + line_length + 1);
- if (!temp)
- {
- perror("malloc");
- return (free(result), NULL);
- }
- if (result)
- {
- ft_strcpy(temp, result);
- free(result);
- }
- else
- temp[0] = '\0';
- ft_strcat(temp, line);
- ft_strcat(temp, "\n");
- result = temp;
- total_length += line_length;
- free(line);
- }
- return (result);
-}
+static char *get_heredoc_data(t_token *cur);
t_redirection *collect_redirs(t_token **tokens, t_env *env)
{
return (result);
}
-static void set_redir(t_redirection *redir, int type, char *specifier,
- t_env *env)
+static void set_redir(t_redirection *redir, int type, char *spec, t_env *env)
{
redir->type = type;
- if (specifier != NULL)
- redir->specifier = format_string(specifier, env);
+ if (spec != NULL)
+ redir->specifier = format_string(spec, env);
else
- redir->specifier = specifier;
+ redir->specifier = spec;
}
static void collect_and_check_redir(t_redirection *result, t_token **cur,
t_env *env)
{
- char *heredoc_data;
t_token *next_token;
+ char *str;
- heredoc_data = NULL;
+ if ((*cur)->content.redir_type != INPUT_LIMITER)
+ str = ft_strdup((*cur)->next->content.string);
if ((*cur)->content.redir_type == INPUT_LIMITER)
{
- heredoc_data = read_heredoc((*cur)->next->content.string);
- if (!heredoc_data)
- {
- perror("Heredoc allocation failed");
- return ;
- }
- set_redir(&result[0], INPUT_LIMITER, heredoc_data, env);
+ set_redir(&result[0], INPUT_LIMITER, get_heredoc_data(*cur), env);
}
else if ((*cur)->content.redir_type == INPUT_FILE)
- set_redir(&result[0], INPUT_FILE,
- ft_strdup((*cur)->next->content.string), env);
+ set_redir(&result[0], INPUT_FILE, str, env);
else if ((*cur)->content.redir_type == OUTPUT_OVERRIDE)
- set_redir(&result[1], OUTPUT_OVERRIDE,
- ft_strdup((*cur)->next->content.string), env);
+ set_redir(&result[1], OUTPUT_OVERRIDE, str, env);
else if ((*cur)->content.redir_type == OUTPUT_APPEND)
- set_redir(&result[1], OUTPUT_APPEND,
- ft_strdup((*cur)->next->content.string), env);
+ set_redir(&result[1], OUTPUT_APPEND, str, env);
next_token = (*cur)->next;
free_token_and_connect(*cur);
if (next_token)
else
*cur = NULL;
}
+
+static char *get_heredoc_data(t_token *cur)
+{
+ char *heredoc_data;
+
+ if (cur->content.redir_type == INPUT_LIMITER)
+ {
+ heredoc_data = read_heredoc(cur->next->content.string);
+ if (!heredoc_data)
+ {
+ perror("Heredoc allocation failed");
+ return ;
+ }
+ set_redir(&result[0], INPUT_LIMITER, heredoc_data, env);
+ }
+ return (heredoc_data);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* read_heredoc.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2025/01/15 18:22:09 by dkaiser #+# #+# */
+/* Updated: 2025/01/15 18:22:52 by dkaiser ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "minishell.h"
+
+char *read_heredoc(char *delimiter)
+{
+ char *line;
+ char *result;
+ char *temp;
+ size_t total_length;
+ size_t line_length;
+
+ total_length = 0;
+ result = NULL;
+ while (1)
+ {
+ line = readline("> ");
+ if (!line || ft_strcmp(line, delimiter) == 0)
+ {
+ free(line);
+ break ;
+ }
+ line_length = ft_strlen(line) + 1;
+ temp = malloc(total_length + line_length + 1);
+ if (!temp)
+ {
+ perror("malloc");
+ return (free(result), NULL);
+ }
+ if (result)
+ {
+ ft_strcpy(temp, result);
+ free(result);
+ }
+ else
+ temp[0] = '\0';
+ ft_strcat(temp, line);
+ ft_strcat(temp, "\n");
+ result = temp;
+ total_length += line_length;
+ free(line);
+ }
+ return (result);
+}