Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #29

Merged
merged 4 commits into from
Sep 6, 2024
Merged

Dev #29

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# By: dmdemirk <dmdemirk@student.42london.c +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/05/30 12:57:25 by dmdemirk #+# #+# #
# Updated: 2024/06/11 15:31:58 by rocky ### ########.fr #
# Updated: 2024/09/06 11:50:43 by dmdemirk ### ########.fr #
# #
# **************************************************************************** #

Expand Down Expand Up @@ -62,6 +62,7 @@ TEST_INCLUDES = -I./inc \
MAIN_SOURCE = $(wildcard $(SRC_DIR)/*.c)
APP_SOURCES = $(wildcard $(APP_DIR)/*.c)
ENV_SOURCES = $(wildcard $(ENV_DIR)/*.c)
ERRORS_SOURCES = $(wildcard $(ERRORS_DIR)/*.c)
SHELL_VAR_SOURCES = $(wildcard $(SHELL_VAR_DIR)/*.c)
COMMON_SOURCES = $(wildcard $(COMMON_DIR)/*.c)
UTILS_SOURCES = $(wildcard $(UTILS_DIR)/*.c)
Expand All @@ -80,6 +81,7 @@ PIPE_TEST_SOURCES = $(wildcard $(TEST_DIR)/pipe/*.c)
SOURCES = $(MAIN_SOURCE) \
$(APP_SOURCES) \
$(ENV_SOURCES) \
$(ERRORS_SOURCES) \
$(SHELL_VAR_SOURCES) \
$(COMMON_SOURCES) \
$(UTILS_SOURCES) \
Expand Down Expand Up @@ -108,14 +110,14 @@ REDIRECTION_OBJECTS = $(patsubst $(REDIRECTION_DIR)/%.c, $(BUILD_DIR)/src/redi
BUILTINS_OBJECTS = $(patsubst $(BUILTINS_DIR)/%.c, $(BUILD_DIR)/src/builtins/%.o, $(BUILTINS_SOURCES))
EXECUTE_OBJECTS = $(patsubst $(EXECUTE_DIR)/%.c, $(BUILD_DIR)/src/execute/%.o, $(EXECUTE_SOURCES))
SIGNALS_OBJECTS = $(patsubst $(SIGNALS_DIR)/%.c, $(BUILD_DIR)/src/signals/%.o, $(SIGNALS_SOURCES))

MAIN_TEST_OBJECT = $(patsubst $(TEST_DIR)/%.c, $(BUILD_DIR)/src/test/%.o, $(MAIN_TEST_SOURCE))
ENV_TEST_OBJECTS = $(patsubst $(TEST_DIR)/env/%.c, $(BUILD_DIR)/src/test/env/%.o, $(ENV_TEST_SOURCES))
PIPE_TEST_OBJECTS = $(patsubst $(TEST_DIR)/pipe/%.c, $(BUILD_DIR)/src/test/pipe/%.o, $(PIPE_TEST_SOURCES))

OBJECTS = $(MAIN_OBJECT) \
$(APP_OBJECTS) \
$(ENV_OBJECTS) \
$(ERRORS_OBJECTS) \
$(SHELL_VAR_OBJECTS) \
$(EXECUTE_OBJECTS) \
$(EXIT_STATUS_OBJECTS) \
Expand Down
6 changes: 3 additions & 3 deletions inc/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: dmdemirk <dmdemirk@student.42london.c +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/07 17:20:26 by dmdemirk #+# #+# */
/* Updated: 2024/06/10 16:45:25 by dmdemirk ### ########.fr */
/* Updated: 2024/09/06 12:32:33 by dmdemirk ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -34,11 +34,11 @@ void free_env(t_env *envp);
void print_t_env(t_env *tokens);

/* shell variables */
int handle_add_set_shell_variable(t_env **shell_var, char *line);
int handle_add_set_shell_variable(t_env **shell_var, char *line);

/* shell variables utils */
void set_shell_var(t_env **shell_var, const char *key, const char *value);
void add_shell_var_node(t_env **shell_var, const char *line);
void add_shell_var_node(t_env **shell_var, const char *line);
char *get_shell_variable(t_env *shell_var, const char *key);

#endif
9 changes: 4 additions & 5 deletions inc/execute.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: dmdemirk <dmdemirk@student.42london.c +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/03 16:34:42 by dmdemirk #+# #+# */
/* Updated: 2024/07/11 16:01:50 by dmdemirk ### ########.fr */
/* Updated: 2024/09/06 12:31:40 by dmdemirk ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -28,9 +28,8 @@ void close_fds(int in, int out);
void handle_io_fd(t_ms_data *data);

/* shell variable execution */
int handle_shell_variable(t_ast *node, t_ms_data *data);
int handle_get_shell_variable(t_ms_data *data, const char *key);
void shell_variable_update(t_ms_data *data, int status);

int handle_shell_variable(t_ast *node, t_ms_data *data);
int handle_get_shell_variable(t_ms_data *data, const char *key);
void shell_variable_update(t_ms_data *data, int status);

#endif
26 changes: 19 additions & 7 deletions inc/exit_status.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* exit_status.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dmdemirk <dmdemirk@student.42london.c +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/06 12:29:24 by dmdemirk #+# #+# */
/* Updated: 2024/09/06 12:31:02 by dmdemirk ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef EXIT_STATUS_H
# define EXIT_STATUS_H

Expand All @@ -15,15 +27,15 @@
# define NOT_VALID_IDENTIFIER 263
# define INVALID_OPTION 264

#include "shell.h"
#include <errno.h>
# include "shell.h"
# include <errno.h>

/* exit status */
void exit_status_handler(t_ms_data *data, int status_code, char *err_arg);
void set_exit_status(int *exit_status, int status_code);
void exit_status_handler(t_ms_data *data, int status_code, char *err_arg);
void set_exit_status(int *exit_status, int status_code);

/* exit_status_utils */
int ft_perror(char *str);
int ft_isnumber(char *str);
int ft_perror(char *str);
int ft_isnumber(char *str);

#endif
#endif
4 changes: 2 additions & 2 deletions inc/tokens.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: rmikhayl <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/31 20:53:51 by rmikhayl #+# #+# */
/* Updated: 2024/06/26 14:39:34 by dmdemirk ### ########.fr */
/* Updated: 2024/09/06 12:29:14 by dmdemirk ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -106,7 +106,7 @@ t_ast *new_ast_node(t_token_type type);
t_ast *create_redir(t_token **tokens, t_token *tmp, t_ms_data *data);
int arg_len(t_token *current);
void set_command_args(t_ast *command_node, t_token **tokens, \
int arg_count);
int arg_count);
t_ast *manage_commands(t_token **tokens, t_ms_data *data);
t_ast *create_redir_node(t_token *token);
int is_redir_node(t_token *tokens);
Expand Down
8 changes: 5 additions & 3 deletions src/builtins/cd.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: dmdemirk <dmdemirk@student.42london.c +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/03 16:31:07 by dmdemirk #+# #+# */
/* Updated: 2024/07/11 16:06:37 by dmdemirk ### ########.fr */
/* Updated: 2024/09/06 12:28:43 by dmdemirk ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -39,9 +39,11 @@ int builtin_cd(t_ms_data *data)
if (chdir(target_dir) == -1)
{
if (errno == EACCES)
exit_status_handler(data, PERMISSION_DENIED, ft_strjoin("cd: ", target_dir));
exit_status_handler(data, PERMISSION_DENIED, \
ft_strjoin("cd: ", target_dir));
if (errno == ENOENT)
exit_status_handler(data, IS_DIRECTORY, ft_strjoin("cd: ", target_dir));
exit_status_handler(data, IS_DIRECTORY, \
ft_strjoin("cd: ", target_dir));
return (EXIT_FAILURE);
}
set_env(&data->envp, "OLDPWD", get_env(data->envp, "PWD"));
Expand Down
58 changes: 37 additions & 21 deletions src/builtins/exit.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: dmdemirk <dmdemirk@student.42london.c +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/03 16:32:42 by dmdemirk #+# #+# */
/* Updated: 2024/07/11 14:59:10 by dmdemirk ### ########.fr */
/* Updated: 2024/09/06 12:28:10 by dmdemirk ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -24,28 +24,44 @@
- Exit with the exit status
*/

int builtin_exit(t_ms_data *data)
void handle_numeric_error(t_ms_data *data, const char *arg)
{
exit_status_handler(data, NUMERIC_REQUIRED, ft_strjoin("exit: ", arg));
exit(NUMERIC_REQUIRED);
}

void handle_too_many_args_error(t_ms_data *data)
{
exit_status_handler(data, TOO_MANY_ARGS, "exit");
exit(TOO_MANY_ARGS);
}

int number;
void handle_exit(t_ms_data *data, int status)
{
ft_putendl_fd("exit", STDOUT_FILENO);
data->exit_status = status;
set_shell_var(&data->shell_variables, "?", ft_itoa(data->exit_status));
exit(status);
}

int builtin_exit(t_ms_data *data)
{
int number;

number = 0;
if (ft_isnumber(data->args[1]) == 0)
{
exit_status_handler(data, NUMERIC_REQUIRED, ft_strjoin("exit: ", data->args[1]));
exit(NUMERIC_REQUIRED);
}
else if (data->args[1] && data->args[2])
{
exit_status_handler(data, TOO_MANY_ARGS, "exit");
exit(TOO_MANY_ARGS);
}
else
{
number = ft_atoi(data->args[1]);
ft_putendl_fd("exit", STDOUT_FILENO);
data->exit_status = number;
set_shell_var(&data->shell_variables, "?", ft_itoa(data->exit_status));
exit(number);
}
if (data->args[1])
{
if (ft_isnumber(data->args[1]) == 0)
handle_numeric_error(data, data->args[1]);
else if (data->args[2])
handle_too_many_args_error(data);
else
{
number = ft_atoi(data->args[1]);
handle_exit(data, number);
}
}
else
handle_exit(data, 0);
return (0);
}
65 changes: 36 additions & 29 deletions src/builtins/export.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: dmdemirk <dmdemirk@student.42london.c +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/17 11:10:52 by dmdemirk #+# #+# */
/* Updated: 2024/06/17 15:34:56 by dmdemirk ### ########.fr */
/* Updated: 2024/09/06 13:45:43 by dmdemirk ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -23,48 +23,55 @@
- If arguments are passed, set the environment variables
*/

int builtin_export(t_ms_data *data);
static void print_env(t_env *env);
static void add_env(t_ms_data *data);
int builtin_export(t_ms_data *data);
static void print_env(t_env *env);
static void add_env(t_ms_data *data);


int builtin_export (t_ms_data *data)
{
if (ft_strcmp(data->args[1], "-p") != 0 && data->args[1][0] == '-')
{
exit_status_handler(data, INVALID_OPTION, ft_strjoin("export: ", data->args[1]));
ft_putendl_fd("export: usage: export [-p] [name[=value] ...] or export -p", STDERR_FILENO);
return (INVALID_OPTION);
}
else if (data->args[1] == NULL)
print_env(data->envp);
else if (ft_strcmp(data->args[1], "-p") == 0)
print_env(data->envp);
add_env(data);
if (data->args == NULL || data->args[1] == NULL)
{
print_env(data->envp);
return (EXIT_SUCCESS);
}
if (ft_strcmp(data->args[1], "-p") != 0 && data->args[1][0] == '-')
{
exit_status_handler(data, INVALID_OPTION, \
ft_strjoin("export: ", data->args[1]));
ft_putendl_fd("export: usage: export [-p]" \
"[name[=value] ...] or export -p", STDERR_FILENO);
return (INVALID_OPTION);
}
else if (ft_strcmp(data->args[1], "-p") == 0)
{
print_env(data->envp);
return (EXIT_SUCCESS);
}
add_env(data);
return (EXIT_SUCCESS);
}

static void print_env(t_env *env)
static void print_env(t_env *env)
{
t_env *curr_node;
t_env *curr_node;

curr_node = env;
while (curr_node)
{
if (!ft_strcmp(curr_node->value, ""))
printf("declare -x %s\n", curr_node->key);
else
printf("declare -x %s=\"%s\"\n", curr_node->key, curr_node->value);
curr_node = curr_node->next;
}
curr_node = env;
while (curr_node)
{
if (!ft_strcmp(curr_node->value, ""))
printf("declare -x %s\n", curr_node->key);
else
printf("declare -x %s=\"%s\"\n", curr_node->key, curr_node->value);
curr_node = curr_node->next;
}
}


static void add_env(t_ms_data *data)
{
int i;
char* key;
char* curr_arg;
char *key;
char *curr_arg;

i = 0;
key = NULL;
Expand Down
10 changes: 2 additions & 8 deletions src/builtins/pwd.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: dmdemirk <dmdemirk@student.42london.c +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/17 11:10:33 by dmdemirk #+# #+# */
/* Updated: 2024/06/17 11:10:42 by dmdemirk ### ########.fr */
/* Updated: 2024/09/06 12:01:53 by dmdemirk ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -28,11 +28,5 @@ int builtin_pwd(t_ms_data *data)
(void)data;
if (getcwd(cwd, sizeof(cwd)) != NULL)
ft_putendl_fd(cwd, STDOUT_FILENO);
// else
// {
// ft_putstr_fd("bash: cd: ", STDERR_FILENO);
// perror("cwd");
// return (EXIT_FAILURE);
// }
return (EXIT_SUCCESS);
}
}
10 changes: 2 additions & 8 deletions src/builtins/unset.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: dmdemirk <dmdemirk@student.42london.c +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/17 11:10:03 by dmdemirk #+# #+# */
/* Updated: 2024/06/17 11:10:25 by dmdemirk ### ########.fr */
/* Updated: 2024/09/06 12:01:39 by dmdemirk ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -34,15 +34,9 @@ int builtin_unset(t_ms_data *data)
i = 0;
while (data->args[++i])
{
key = data->args[i];
printf("key: %s\n", key);
key = data->args[i];
if (unset_env(&data->envp, key) == -1)
{
// ft_putstr_fd("bash: unset: `", STDERR_FILENO);
// ft_putstr_fd(key, STDERR_FILENO);
// ft_putendl_fd("': not a valid identifier", STDERR_FILENO);
// set_exit_status(&data->exit_status, NOT_VALID_IDENTIFIER);
// set_shell_var(&data->shell_variables, "?", ft_itoa(data->exit_status));
return (EXIT_SUCCESS);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/env/env.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: dmdemirk <dmdemirk@student.42london.c +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/07 17:20:11 by dmdemirk #+# #+# */
/* Updated: 2024/06/26 14:48:35 by dmdemirk ### ########.fr */
/* Updated: 2024/09/06 12:01:09 by dmdemirk ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -124,4 +124,4 @@ int unset_env(t_env **env, const char *key)
current = current->next;
}
return (-1);
}
}
Loading