/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/02 13:49:31 by dkaiser #+# #+# */
-/* Updated: 2025/01/21 20:19:48 by chuhlig ### ########.fr */
+/* Updated: 2025/01/22 16:08:25 by chuhlig ### ########.fr */
/* */
/* ************************************************************************** */
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/15 16:14:31 by dkaiser #+# #+# */
-/* Updated: 2025/01/22 00:01:03 by chuhlig ### ########.fr */
+/* Updated: 2025/01/22 14:13:02 by chuhlig ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
-// char **get_split_path(t_env *env)
-// {
-// char *path;
+char **get_split_path(t_env *env)
+{
+ char *path;
-// path = env_get(env, "PATH");
-// return (ft_split(path, ':'));
-// }
+ path = env_get(env, "PATH");
+ return (ft_split(path, ':'));
+}
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/17 19:21:35 by chuhlig #+# #+# */
-/* Updated: 2025/01/21 23:52:38 by chuhlig ### ########.fr */
+/* Updated: 2025/01/22 16:29:34 by chuhlig ### ########.fr */
/* */
/* ************************************************************************** */
static void establish_pipeline(int original_stdin, int original_stdout);
static int exec_cmd(t_cmd *cmd, t_env **env, int original_std[2], int result);
+int invalid_input(char *cmd)
+{
+ ft_putstr_fd("minishell: ", STDERR_FILENO);
+ ft_putstr_fd(cmd, STDERR_FILENO);
+ ft_putstr_fd(": command not found\n", STDERR_FILENO);
+ return (127);
+}
+
int is_builtin(char *cmd)
{
- return ((ft_strcmp(cmd, "export") == 0) || (ft_strcmp(cmd, "unset") == 0)
+ if ((ft_strcmp(cmd, "export") == 0) || (ft_strcmp(cmd, "unset") == 0)
|| (ft_strcmp(cmd, "cd") == 0) || (ft_strcmp(cmd, "exit") == 0)
|| (ft_strcmp(cmd, "echo") == 0) || (ft_strcmp(cmd, "pwd") == 0)
- || (ft_strcmp(cmd, "env") == 0));
+ || (ft_strcmp(cmd, "env") == 0))
+ return (1);
+ return(invalid_input(cmd));
}
int execute_builtin(char **args, t_env **env)
cmd_path = get_cmd_path(cmd->args[i], *env, &result);
if (cmd_path != NULL)
execve(cmd_path, &(cmd->args[i]), env_to_strlst(*env));
- free(cmd_path);
+ // free(cmd_path);
exit(result);
}
waitpid(pid, &status, 0);
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/17 19:30:11 by chuhlig #+# #+# */
-/* Updated: 2025/01/21 23:25:20 by chuhlig ### ########.fr */
+/* Updated: 2025/01/22 14:31:29 by chuhlig ### ########.fr */
/* */
/* ************************************************************************** */
if (*dst != NULL)
free(*dst);
*dst = result;
+ // free(src);
+ // src = *dst;
}
static void append_var(char **dst, char *src, int *pos, t_env *env)
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/27 11:41:46 by dkaiser #+# #+# */
-/* Updated: 2025/01/22 01:53:34 by chuhlig ### ########.fr */
+/* Updated: 2025/01/22 16:00:36 by chuhlig ### ########.fr */
/* */
/* ************************************************************************** */
// panic(UNREACHABLE);
// free(node);
// }
-void free_redirections(t_redirection redirs[2])
-{
- for (int i = 0; i < 2; i++)
- {
- if (redirs[i].specifier)
- free(redirs[i].specifier);
- }
-}
-void free_node(t_node *node)
-{
- if (!node)
- return;
+// static void free_pipe_node(t_node *node)
+// {
+// free_node(node->content.pipe.left);
+// free_node(node->content.pipe.right);
+// }
+
+// static void free_cmd_node(t_node *node)
+// {
+// int i;
+
+// i = 0;
+// while (node->content.cmd.args != NULL && node->content.cmd.args[i] != NULL)
+// {
+// free(node->content.cmd.args[i]);
+// i++;
+// }
+// free(node->content.cmd.args);
+// if (node->content.cmd.redirs[0].type != 0
+// && node->content.cmd.redirs[0].specifier != NULL)
+// free(node->content.cmd.redirs[0].specifier);
+// if (node->content.cmd.redirs[1].type != 0
+// && node->content.cmd.redirs[0].specifier != NULL)
+// free(node->content.cmd.redirs[1].specifier);
+// if (node->content.cmd.create_files != NULL)
+// ft_lstclear(&node->content.cmd.create_files, free_file);
+// }
+
+// static void free_file(void *arg)
+// {
+// t_redirection *file;
+// file = (t_redirection *)arg;
+// free(file->specifier);
+// free(file);
+// }
+
+void free_node(t_node *node)
+{
if (node->type == PIPE_NODE)
- {
- free_node(node->content.pipe.left);
- free_node(node->content.pipe.right);
- }
+ free_pipe_node(node);
else if (node->type == CMD_NODE)
- {
- if (node->content.cmd.args)
- {
- for (int i = 0; node->content.cmd.args[i]; i++)
- free(node->content.cmd.args[i]);
- free(node->content.cmd.args);
- }
- free_redirections(node->content.cmd.redirs);
- // Assuming create_files is a list of dynamically allocated strings
- t_list *current = node->content.cmd.create_files;
- t_list *next;
- while (current)
- {
- next = current->next;
- free(current->content);
- free(current);
- current = next;
- }
- }
+ free_cmd_node(node);
else if (node->type == STRING_NODE)
- {
free(node->content.string);
- }
free(node);
}
-static void free_pipe_node(t_node *node)
+static void free_pipe_node(t_node *node)
{
- free_node(node->content.pipe.left);
- free_node(node->content.pipe.right);
+ free_node(node->content.pipe.left);
+ free_node(node->content.pipe.right);
}
-static void free_cmd_node(t_node *node)
+static void free_cmd_node(t_node *node)
{
- int i;
+ int i;
- i = 0;
- while (node->content.cmd.args != NULL && node->content.cmd.args[i] != NULL)
- {
- free(node->content.cmd.args[i]);
- i++;
- }
- free(node->content.cmd.args);
- if (node->content.cmd.redirs[0].type != 0
- && node->content.cmd.redirs[0].specifier != NULL)
- free(node->content.cmd.redirs[0].specifier);
- if (node->content.cmd.redirs[1].type != 0
- && node->content.cmd.redirs[0].specifier != NULL)
- free(node->content.cmd.redirs[1].specifier);
- if (node->content.cmd.create_files != NULL)
- ft_lstclear(&node->content.cmd.create_files, free_file);
+ i = 0;
+ while (node->content.cmd.args != NULL && node->content.cmd.args[i] != NULL)
+ {
+ free(node->content.cmd.args[i]);
+ i++;
+ }
+ free(node->content.cmd.args);
+ if (node->content.cmd.redirs[0].type != 0 && node->content.cmd.redirs[0].specifier != NULL)
+ free(node->content.cmd.redirs[0].specifier);
+ if (node->content.cmd.redirs[1].type != 0 && node->content.cmd.redirs[1].specifier != NULL)
+ free(node->content.cmd.redirs[1].specifier);
+ if (node->content.cmd.create_files != NULL)
+ ft_lstclear(&node->content.cmd.create_files, free_file);
}
-static void free_file(void *arg)
+static void free_file(void *arg)
{
- t_redirection *file;
+ t_redirection *file;
- file = (t_redirection *)arg;
- free(file->specifier);
- free(file);
-}
+ file = (t_redirection *)arg;
+ free(file->specifier);
+ free(file);
+}
\ No newline at end of file
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/27 14:38:57 by dkaiser #+# #+# */
-/* Updated: 2025/01/22 00:07:58 by chuhlig ### ########.fr */
+/* Updated: 2025/01/22 15:56:38 by chuhlig ### ########.fr */
/* */
/* ************************************************************************** */
token->previous->next = NULL;
if (token->next != NULL)
token->next->previous = NULL;
- // if (token->type == STRING_TOKEN && token->content.string != NULL)
- // free(token->content.string); // Ensure content is freed
+ // if (token->previous == NULL && token->next == NULL)
+ // {
+ // if (token->type == STRING_TOKEN && token->content.string != NULL)
+ // free(token->content.string); // Ensure content is freed
+ // }
free(token);//maybe free token
token = NULL;
}
token = NULL;
}
-// void free_tokens(t_token *tokens)
-// {
-// while (tokens->next != NULL)
-// {
-// tokens = tokens->next;
-// free_token(tokens->previous);
-// }
-// free_token(tokens);
-// }
-void free_tokens(t_token *tokens)
+void free_tokens(t_token *tokens)
{
- t_token *tmp;
-
- while (tokens)
- {
- tmp = tokens;
- tokens = tokens->next;
- free_token(tmp); // Ensure each token is freed
- }
+ while (tokens->next != NULL)
+ {
+ tokens = tokens->next;
+ free_token(tokens->previous);
+ }
+ free_token(tokens);
}
+// void free_tokens(t_token *tokens)
+// {
+// t_token *tmp;
+
+// while (tokens)
+// {
+// tmp = tokens;
+// tokens = tokens->next;
+// free_token(tmp); // Ensure each token is freed
+// }
+// }
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/17 19:19:59 by chuhlig #+# #+# */
-/* Updated: 2025/01/22 00:01:48 by chuhlig ### ########.fr */
+/* Updated: 2025/01/22 16:17:27 by chuhlig ### ########.fr */
/* */
/* ************************************************************************** */
return (result);
}
-void free_split_path(char **path)
+static char *find_in_path(char *cmd, t_env *env, int *return_code)
{
- char **tmp = path;
- while (*tmp)
- {
- free(*tmp);
- tmp++;
- }
- free(path);
-}
+ char *cur_path;
+ char *cmd_path;
+ char **path;
-char **get_split_path(t_env *env)
-{
- char *path_env;
- char **split_path;
- path_env = env_get(env, "PATH");
- if (!path_env)
- return (NULL);
- split_path = ft_split(path_env, ':');
- free(path_env);
- return (split_path);
+ path = get_split_path(env);
+ cmd_path = NULL;
+ while (*path)
+ {
+ if (cmd_path)
+ free(cmd_path);
+ cur_path = ft_strjoin(*path, "/");
+ if (!cur_path)
+ return (NULL);
+ cmd_path = ft_strjoin(cur_path, cmd);
+ free(cur_path);
+ if (!cmd_path)
+ return (NULL);
+ if (access(cmd_path, X_OK) != -1)
+ return (cmd_path);
+ path++;
+ }
+ *return_code = 127;
+ free(cmd_path);
+ command_not_found_error(cmd);
+ return (NULL);
}
-static char *find_in_path(char *cmd, t_env *env, int *return_code)
-{
- char *cur_path;
- char *cmd_path;
- char **path;
- char **path_start; // To keep track of the start of the path array
-
- path = get_split_path(env);
- path_start = path; // Save the start of the path array
- cmd_path = NULL;
- while (*path)
- {
- if (cmd_path)
- free(cmd_path);
- cur_path = ft_strjoin(*path, "/");
- if (!cur_path)
- {
- free_split_path(path_start); // Free the entire path array
- return (NULL);
- }
- cmd_path = ft_strjoin(cur_path, cmd);
- free(cur_path);
- if (!cmd_path)
- {
- free_split_path(path_start); // Free the entire path array
- return (NULL);
- }
- if (access(cmd_path, X_OK) != -1)
- {
- free_split_path(path_start); // Free the entire path array
- return (cmd_path);
- }
- path++;
- }
- free_split_path(path_start); // Free the entire path array
- *return_code = 127;
- command_not_found_error(cmd);
- return (NULL);
-}
+// static char *get_simple_cmd_path(char *cmd, int *return_code)
+// {
+// char *result;
+// result = ft_strdup(cmd);
+// if (!result)
+// return (NULL);
+// if (access(result, F_OK) == -1)
+// {
+// free(result);
+// return (error(ENOENT, cmd, 127, return_code));
+// }
+// if (access(result, X_OK) == -1)
+// {
+// free(result);
+// return (error(EACCES, cmd, 126, return_code));
+// }
+// if (is_directory(cmd))
+// {
+// free(result);
+// return (error(EISDIR, cmd, 126, return_code));
+// }
+// return (result);
+// }
static char *get_simple_cmd_path(char *cmd, int *return_code)
{
char *result;
result = ft_strdup(cmd);
- if (!result)
- return (NULL);
if (access(result, F_OK) == -1)
{
free(result);
return (error(ENOENT, cmd, 127, return_code));
}
- if (access(result, X_OK) == -1)
+ else if (access(result, X_OK) == -1)
{
free(result);
return (error(EACCES, cmd, 126, return_code));
}
if (is_directory(cmd))
- {
- free(result);
return (error(EISDIR, cmd, 126, return_code));
- }
return (result);
}
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/17 19:15:49 by chuhlig #+# #+# */
-/* Updated: 2025/01/21 16:35:38 by chuhlig ### ########.fr */
+/* Updated: 2025/01/22 15:20:18 by chuhlig ### ########.fr */
/* */
/* ************************************************************************** */
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/27 11:21:03 by dkaiser #+# #+# */
-/* Updated: 2025/01/20 17:59:01 by dkaiser ### ########.fr */
+/* Updated: 2025/01/22 15:15:31 by chuhlig ### ########.fr */
/* */
/* ************************************************************************** */
node->content.cmd.redirs[0] = redirs[0];
node->content.cmd.redirs[1] = redirs[1];
node->content.cmd.create_files = create_files;
- free(redirs);
+ // free(redirs);
redirs = NULL;
return (node);
}
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/24 16:07:04 by dkaiser #+# #+# */
-/* Updated: 2025/01/21 21:29:16 by chuhlig ### ########.fr */
+/* Updated: 2025/01/22 15:22:00 by chuhlig ### ########.fr */
/* */
/* ************************************************************************** */
void free_repl(char *input, t_node *ast)
{
free(input);
- free_node(ast);
+ if(ast)
+ free_node(ast);
}
void repl(const char *prompt, t_env **env, int *promptflag)