/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/22 17:14:49 by dkaiser #+# #+# */
-/* Updated: 2025/01/23 23:17:00 by chuhlig ### ########.fr */
+/* Updated: 2025/01/25 15:41:33 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
int init(void);
int init_signal_handling(void);
-void repl(const char *prompt, t_env **env, int *promptflag);
+void repl(const char *prompt, t_env **env);
t_node *parse(t_token *tokens, t_env **env);
t_node *parse_cmd(t_token *tokens, t_env **env);
-t_redirection *collect_redirs(t_token **tokens, t_env *env, t_list **create_files);
+t_redirection *collect_redirs(t_token **tokens, t_env *env,
+ t_list **create_files);
void print_ast(t_node *ast);
int handle_redirections(t_redirection *redirs);
void *error(int err_code, char *err_text, int exit_code,
int *ret_code);
-void command_not_found_error(char *cmd);
+void *command_not_found_error(char *cmd, int *return_code,
+ char *cmd_path, char **split_path);
char *read_heredoc(char *delimiter);
int handle_input_redirection(t_redirection *redir);
int handle_output_redirection(t_redirection *redir);
/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/15 16:35:53 by dkaiser #+# #+# */
-/* Updated: 2025/01/20 18:12:40 by dkaiser ### ########.fr */
+/* Updated: 2025/01/25 14:58:52 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
return (NULL);
}
-void command_not_found_error(char *cmd)
+void *command_not_found_error(char *cmd, int *return_code, char *cmd_path,
+ char **split_path)
{
ft_printf("%s:", cmd);
ft_putstr_fd(" command not found", 2);
ft_printf("\n");
+ *return_code = 127;
+ free(cmd_path);
+ ft_free_split(split_path);
+ return (NULL);
}
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/17 19:19:59 by chuhlig #+# #+# */
-/* Updated: 2025/01/25 11:36:28 by chuhlig ### ########.fr */
+/* Updated: 2025/01/25 14:58:45 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
return (ft_free_split(path_start), cmd_path);
path++;
}
- *return_code = 127;
- free(cmd_path);
- ft_free_split(path_start);
- command_not_found_error(cmd);
- return (NULL);
+ return (command_not_found_error(cmd, return_code, cmd_path, path_start));
}
static char *get_simple_cmd_path(char *cmd, int *return_code)
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/22 17:14:03 by dkaiser #+# #+# */
-/* Updated: 2025/01/25 10:25:42 by chuhlig ### ########.fr */
+/* Updated: 2025/01/25 15:41:04 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
int main(int argc, char *argv[], char *envp[])
{
- t_env *env;
- static int promptflag;
+ t_env *env;
env = NULL;
- promptflag = 0;
if (!argc && !argv)
return (1);
if (init())
return (1);
getenvlst(&env, envp);
set_return_code(0, &env);
- repl("Minishell $ ", &env, &promptflag);
+ repl("Minishell $ ", &env);
free_envlst(&env);
return (0);
}
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/08 15:06:25 by dkaiser #+# #+# */
-/* Updated: 2025/01/25 11:36:09 by chuhlig ### ########.fr */
+/* Updated: 2025/01/25 15:00:55 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static char **collect_args(t_token **tokens, t_env **env);
+static void setup_vars(t_token **tokens, t_token **cur, int *i);
t_node *parse_cmd(t_token *tokens, t_env **env)
{
int i;
t_token *next;
- cur = *tokens;
- i = 0;
+ setup_vars(tokens, &cur, &i);
while (cur != NULL && ++i)
cur = cur->next;
result = malloc(sizeof(char *) * (i + 1));
if (result == NULL)
return (free_tokens(*tokens), NULL);
- cur = *tokens;
- i = 0;
+ setup_vars(tokens, &cur, &i);
while (cur != NULL && cur->type == STRING_TOKEN)
{
next = cur->next;
if (cur->previous)
free_token2(cur->previous);
- result[i] = format_string(cur->content.string, *env, ft_atoi("0"));
- i++;
+ result[i++] = format_string(cur->content.string, *env, ft_atoi("0"));
if (cur->next == NULL)
free_token2(cur);
cur = next;
result[i] = NULL;
return (result);
}
+
+static void setup_vars(t_token **tokens, t_token **cur, int *i)
+{
+ *cur = *tokens;
+ *i = 0;
+}
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/24 16:07:04 by dkaiser #+# #+# */
-/* Updated: 2025/01/25 12:41:48 by chuhlig ### ########.fr */
+/* Updated: 2025/01/25 15:39:36 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
#include "../include/minishell.h"
#include "token.h"
-void free_repl(char *input, t_node *ast)
+static void free_repl(char *input, t_node *ast)
{
free(input);
if (ast)
free_node(ast);
}
-void repl(const char *prompt, t_env **env, int *promptflag)
+void repl(const char *prompt, t_env **env)
{
char *input;
t_token *token_list;
t_node *ast;
- (*promptflag)++;
while (1)
{
input = readline(prompt);
if (input == NULL)
{
- if (*promptflag > 1)
- (*promptflag)--;
printf("exit\n");
break ;
}