/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/26 11:39:54 by ljiriste #+# #+# */
-/* Updated: 2024/08/27 15:02:28 by ljiriste ### ########.fr */
+/* Updated: 2024/09/01 14:29:59 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef BUILTINS_H
# define BUILTINS_H
-#include "minishell_structs.h"
+# include "minishell_structs.h"
int cd(int argc, char **argv, t_execution_env *env);
int echo(int argc, char **argv);
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/02 17:40:19 by ljiriste #+# #+# */
-/* Updated: 2024/09/01 14:03:42 by lnikolov ### ########.fr */
+/* Updated: 2024/09/01 14:43:27 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
extern volatile sig_atomic_t g_last_signal;
void free_split(char **fields);
-int set_quote(char *quote, char c);
-int is_token_type(const t_parse_tree_node *node, const char *type);
+int set_quote(char *quote, char c);
+int is_token_type(const t_parse_tree_node *node, const char *type);
-int dup_pipes(const t_execution_env *env);
-int dup_redirections(const t_vec *redirections);
+int dup_pipes(const t_execution_env *env);
+void dup_redirections(const t_vec *redirections);
char **quoted_split(const char *str);
-int unquote_field(char *field);
-int add_word(t_vec *exp_str, const char *word,
- const t_execution_env *env, int enquote_result);
+int unquote_field(char *field);
+int add_word(t_vec *exp_str, const char *word,
+ const t_execution_env *env, int enquote_result);
char **expand(t_parse_tree_node *simple_command, const t_execution_env *env);
char *find_exe(const char *exe_name, const t_execution_env *env);
-int prepare_env(t_execution_env *env,
- const t_vec *redirections, t_vec *assignments);
-
-int ex_builtin(char **fields, __attribute__((unused))
- t_vec *assignments, const t_vec *redirections, t_execution_env *env);
-int ex_fields(char **fields, t_vec *assignments,
- const t_vec *redirections, t_execution_env *env);
-int ex_command(t_parse_tree_node *command, t_execution_env *env);
-int ex_pipeline(t_parse_tree_node *pipeline, t_execution_env *env, int depth);
-int ex_program(t_parse_tree_node *program, t_execution_env *env);
+int prepare_env(t_execution_env *env,
+ const t_vec *redirections, t_vec *assignments);
+
+int ex_builtin(char **fields, __attribute__((unused)) t_vec *assignments,
+ const t_vec *redirections, t_execution_env *env);
+int ex_fields(char **fields, t_vec *assignments,
+ const t_vec *redirections, t_execution_env *env);
+int ex_command(t_parse_tree_node *command, t_execution_env *env);
+int ex_pipeline(t_parse_tree_node *pipeline,
+ t_execution_env *env, int depth);
+int ex_program(t_parse_tree_node *program, t_execution_env *env);
char *get_word(const t_parse_tree_node *parent);
-int add_redirection_here(t_vec *redirections,
- t_parse_tree_node *io_here, int fd, const t_execution_env *env);
-int add_redirection_file(t_vec *redirections,
- t_parse_tree_node *io_file, int fd, t_execution_env *env);
-
-int save_redirections(t_vec *redirections,
- t_parse_tree_node *simple_command, t_execution_env *env);
-int save_assignments(t_vec *assignments, t_parse_tree_node *simple_command,
- const t_execution_env *env);
+int add_redirection_here(t_vec *redirections,
+ t_parse_tree_node *io_here, int fd, const t_execution_env *env);
+int add_redirection_file(t_vec *redirections,
+ t_parse_tree_node *io_file, int fd, t_execution_env *env);
+
+int save_redirections(t_vec *redirections,
+ t_parse_tree_node *simple_command, t_execution_env *env);
+int save_assignments(t_vec *assignments, t_parse_tree_node *simple_command,
+ const t_execution_env *env);
#endif // EXECUTION_H
/* By: lnikolov <lnikolov@student.42prague.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/02 13:22:57 by ljiriste #+# #+# */
-/* Updated: 2024/09/01 09:47:19 by ljiriste ### ########.fr */
+/* Updated: 2024/09/01 14:34:54 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
void handle_input(char **line, t_execution_env *env);
int unquote_field(char *field);
-int add_word(t_vec *exp_str, const char *word, const t_execution_env *env, int quote);
+int add_word(t_vec *exp_str, const char *word,
+ const t_execution_env *env, int quote);
int add_conformant(t_vec *expanded, t_wildcard_info *info, char quote);
int should_be_expanded(const char *word);
int expand_dir(t_vec *expanded, t_wildcard_info *info);
int execute(t_tree *parse_tree, t_execution_env *env);
int finish_token(t_vec *tokens, t_vec *current_token, char next);
char **quoted_split(const char *str);
-int categorization_step(char *line, size_t *i, t_vec *current_token, t_vec *tokens);
-int handle_quote(t_vec *current_token, char *line, char quote_char, size_t *i);
+int categorization_step(char *line, size_t *i,
+ t_vec *current_token, t_vec *tokens);
+int handle_quote(t_vec *current_token, char *line,
+ char quote_char, size_t *i);
#endif // MINISHELL_H
/* By: lnikolov <lnikolov@student.42prague.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/26 09:08:46 by ljiriste #+# #+# */
-/* Updated: 2024/09/01 09:18:49 by ljiriste ### ########.fr */
+/* Updated: 2024/09/01 14:42:11 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef MINISHELL_STRUCTS_H
# define MINISHELL_STRUCTS_H
-#include "libft.h"
+# include "libft.h"
-enum token_type
+typedef enum e_token_type
{
WORD,
ASSIGNMENT_WORD,
PIPE,
LPARA,
RPARA,
-};
+} t_token_type;
typedef struct s_vars
{
t_vec child_pids;
} t_execution_env;
-typedef struct s_wildcard_info
+typedef struct s_wildcard_info
{
const char *to_expand;
const char *current_expand_char;
int is_assignment_word(const char *str);
int cmp_var_name(const char *a, const char *b);
int set_var_value(t_vec *vars, const char *name, const char *value);
-int set_env_var_value(t_execution_env *env, const char *name, const char *value);
+int set_env_var_value(t_execution_env *env,
+ const char *name, const char *value);
ssize_t get_var_index(const t_vec *vars, const char *var_name);
const char *get_var_value(const t_vec *vars, const char *var_name);
-const char *get_env_var_value(const t_execution_env *env, const char *var_name);
+const char *get_env_var_value(const t_execution_env *env,
+ const char *var_name);
char *get_var_name(const char *line);
int add_var_line(t_vec *vec, const char *line);
-const char *type_enum_to_str(enum token_type type);
+const char *type_enum_to_str(t_token_type type);
void free_str(void *str);
void free_token(void *token);
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/02 12:57:18 by ljiriste #+# #+# */
-/* Updated: 2024/08/02 14:18:00 by ljiriste ### ########.fr */
+/* Updated: 2024/09/01 14:30:23 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
// Generated by xxd -i with small changes
// from shell_parsing_table and shell_rules
-static char g_str_table[6725] = {
+static const char g_str_table[6725] = {
0x53, 0x74, 0x61, 0x74, 0x65, 0x3b, 0x44, 0x47, 0x52, 0x45, 0x41, 0x54,
0x3b, 0x47, 0x52, 0x45, 0x41, 0x54, 0x3b, 0x4c, 0x45, 0x53, 0x53, 0x3b,
0x44, 0x4c, 0x45, 0x53, 0x53, 0x3b, 0x49, 0x4f, 0x5f, 0x4e, 0x55, 0x4d,
0x3b, 0x3b, 0x3b, 0x0a, 0x00
};
-static char g_str_rules[941] = {
+static const char g_str_rules[941] = {
0x70, 0x72, 0x6f, 0x67, 0x72, 0x61, 0x6d, 0x20, 0x2d, 0x3e, 0x20, 0x70,
0x72, 0x6f, 0x67, 0x72, 0x61, 0x6d, 0x20, 0x41, 0x4e, 0x44, 0x5f, 0x49,
0x46, 0x20, 0x70, 0x69, 0x70, 0x65, 0x6c, 0x69, 0x6e, 0x65, 0x0a, 0x70,
/* By: lnikolov <lnikolov@student.42prague.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/01 11:51:31 by lnikolov #+# #+# */
-/* Updated: 2024/09/01 13:24:55 by lnikolov ### ########.fr */
+/* Updated: 2024/09/01 14:26:22 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
return (1);
}
return (ft_vec_append(exp_str, &space) != success);
-}
\ No newline at end of file
+}
/* By: lnikolov <lnikolov@student.42prague.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/01 11:19:00 by lnikolov #+# #+# */
-/* Updated: 2024/09/01 14:00:36 by lnikolov ### ########.fr */
+/* Updated: 2024/09/01 14:28:53 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
return (1);
}
-static int save_assignments_prefix(t_vec *assignments, t_parse_tree_node *prefix,
- const t_execution_env *env)
+static int save_assignments_prefix(t_vec *assignments,
+ t_parse_tree_node *prefix, const t_execution_env *env)
{
size_t i;
t_parse_tree_node *subnode;
/* By: lnikolov <lnikolov@student.42prague.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/21 08:57:54 by ljiriste #+# #+# */
-/* Updated: 2024/09/01 13:22:49 by lnikolov ### ########.fr */
+/* Updated: 2024/09/01 14:25:34 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
}
free(fields);
return ;
-}
\ No newline at end of file
+}
/* By: lnikolov <lnikolov@student.42prague.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/01 11:53:23 by lnikolov #+# #+# */
-/* Updated: 2024/09/01 13:25:40 by lnikolov ### ########.fr */
+/* Updated: 2024/09/01 14:43:49 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
return (0);
}
-int dup_redirections(const t_vec *redirections)
+void dup_redirections(const t_vec *redirections)
{
size_t i;
const t_redirection *redir;
close(redir->from_to_fds[1]);
++i;
}
- return (0);
+ return ;
}
static int add_exported(t_vec *assignments, const t_execution_env *env)
close_fds(&env->fds_to_close);
res = dup_pipes(env);
- res = dup_redirections(redirections) || res;
+ dup_redirections(redirections);
res = add_exported(assignments, env) || res;
return (res);
-}
\ No newline at end of file
+}
/* By: lnikolov <lnikolov@student.42prague.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/01 11:53:07 by lnikolov #+# #+# */
-/* Updated: 2024/09/01 13:26:33 by lnikolov ### ########.fr */
+/* Updated: 2024/09/01 14:49:28 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
#include <fcntl.h>
-static int save_std_filenos(int fds[3])
+static void close_redirections(const t_vec *redirections)
{
- fds[0] = dup(STDIN_FILENO);
- if (fds[0] < 0)
- return (1);
- fds[1] = dup(STDOUT_FILENO);
- if (fds[1] < 0)
- {
- close(fds[0]);
- return (1);
- }
- fds[2] = dup(STDERR_FILENO);
- if (fds[2] < 0)
+ size_t i;
+ const t_redirection *redir;
+
+ i = 0;
+ while (i < redirections->size)
{
- close(fds[0]);
- close(fds[1]);
- return (1);
+ redir = ft_vec_caccess(redirections, i);
+ close(redir->from_to_fds[0]);
+ ++i;
}
- return (0);
+ return ;
}
-static int restore_std_filenos(int fds[3])
+static int restore_fds(int fds[3], const t_vec *redirections,
+ t_execution_env *env)
{
+ close_redirections(redirections);
+ close(env->stdin_fd);
+ close(env->stdout_fd);
if (dup2(fds[0], STDIN_FILENO) == -1)
return (1);
if (dup2(fds[1], STDOUT_FILENO) == -1)
return (0);
}
-static void close_pipes(const t_execution_env *env)
+static int prepare_fds(int fds[3], const t_vec *redirections,
+ t_execution_env *env)
{
- close(env->stdin_fd);
- close(env->stdout_fd);
- return ;
+ fds[0] = dup(STDIN_FILENO);
+ if (fds[0] < 0)
+ return (1);
+ fds[1] = dup(STDOUT_FILENO);
+ if (fds[1] < 0)
+ {
+ close(fds[0]);
+ return (1);
+ }
+ fds[2] = dup(STDERR_FILENO);
+ if (fds[2] < 0)
+ {
+ close(fds[0]);
+ close(fds[1]);
+ return (1);
+ }
+ dup_redirections(redirections);
+ if (!dup_pipes(env))
+ return (0);
+ restore_fds(fds, redirections, env);
+ return (1);
}
static size_t count_fields(char **fields)
return (i);
}
-static void close_redirections(const t_vec *redirections)
-{
- size_t i;
- const t_redirection *redir;
-
- i = 0;
- while (i < redirections->size)
- {
- redir = ft_vec_caccess(redirections, i);
- close(redir->from_to_fds[0]);
- ++i;
- }
- return ;
-}
-
int ex_builtin(char **fields, __attribute__((unused))
t_vec *assignments, const t_vec *redirections, t_execution_env *env)
{
int fds[3];
- save_std_filenos(fds);
- dup_pipes(env);
- dup_redirections(redirections);
+ if (prepare_fds(fds, redirections, env))
+ return (1);
if (!ft_strcmp(fields[0], "cd"))
env->builtin_ret_val = cd(count_fields(fields), fields, env);
else if (!ft_strcmp(fields[0], "echo"))
env->builtin_ret_val = execute_exit(count_fields(fields), fields, env);
else
return (1);
- close_redirections(redirections);
- close_pipes(env);
- restore_std_filenos(fds);
+ restore_fds(fds, redirections, env);
return (0);
-}
\ No newline at end of file
+}
/* By: lnikolov <lnikolov@student.42prague.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/01 11:53:01 by lnikolov #+# #+# */
-/* Updated: 2024/09/01 14:13:10 by ljiriste ### ########.fr */
+/* Updated: 2024/09/01 14:24:28 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
return (0);
}
-static int ex_simple_command(t_parse_tree_node *simple_command, t_execution_env *env)
+static int ex_simple_command(t_parse_tree_node *simple_command,
+ t_execution_env *env)
{
t_vec redirections;
t_vec assignments;
/* By: lnikolov <lnikolov@student.42prague.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/01 11:53:33 by lnikolov #+# #+# */
-/* Updated: 2024/09/01 13:31:20 by lnikolov ### ########.fr */
+/* Updated: 2024/09/01 14:28:25 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
ft_vec_free(&expanded_str, NULL);
unquote(fields);
return (fields);
-}
\ No newline at end of file
+}
/* By: lnikolov <lnikolov@student.42prague.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/01 11:51:34 by lnikolov #+# #+# */
-/* Updated: 2024/09/01 13:30:17 by lnikolov ### ########.fr */
+/* Updated: 2024/09/01 14:27:19 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
return (NULL);
}
return (fields.vec);
-}
\ No newline at end of file
+}
/* By: lnikolov <lnikolov@student.42prague.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/01 11:23:57 by lnikolov #+# #+# */
-/* Updated: 2024/09/01 14:02:22 by lnikolov ### ########.fr */
+/* Updated: 2024/09/01 14:28:16 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
free(filename);
return (ft_vec_append(redirections, &redir) != success);
}
-
/* By: lnikolov <lnikolov@student.42prague.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/01 11:48:10 by lnikolov #+# #+# */
-/* Updated: 2024/09/01 13:31:59 by lnikolov ### ########.fr */
+/* Updated: 2024/09/01 14:29:10 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
#include <fcntl.h>
-
-
static char *here_file_getline(void)
{
ft_printf("> ");
redir = (t_redirection){.from_to_fds[0] = fd,
.from_to_fds[1] = pipe_fds[0]};
return (ft_vec_append(redirections, &redir) != success);
-}
\ No newline at end of file
+}
/* By: lnikolov <lnikolov@student.42prague.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/01 11:49:27 by lnikolov #+# #+# */
-/* Updated: 2024/09/01 14:02:49 by lnikolov ### ########.fr */
+/* Updated: 2024/09/01 14:26:09 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell_structs.h"
#include "libft.h"
-
char *get_word(const t_parse_tree_node *parent)
{
char *res;
/* By: lnikolov <lnikolov@student.42prague.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/31 15:12:50 by lnikolov #+# #+# */
-/* Updated: 2024/09/01 09:44:20 by ljiriste ### ########.fr */
+/* Updated: 2024/09/01 14:42:32 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
#include <stdlib.h>
-const char *type_enum_to_str(enum token_type type)
+const char *type_enum_to_str(t_token_type type)
{
static const char *tokens[12] = {
"WORD",