Browse Source

fix cd, add signal ..?

dev/chlee
chlee 10 months ago
parent
commit
0f90f63ec7
11 changed files with 144 additions and 55 deletions
  1. +3
    -1
      Makefile
  2. +5
    -3
      includes/command.h
  3. +6
    -2
      includes/minishell.h
  4. +30
    -40
      srcs/command/cmd_cd.c
  5. +47
    -0
      srcs/command/cmd_cd_util.c
  6. +1
    -1
      srcs/execute_builtin.c
  7. +2
    -2
      srcs/execute_cmd.c
  8. +2
    -1
      srcs/execute_line.c
  9. +5
    -3
      srcs/main.c
  10. +2
    -2
      srcs/run_one_cmd.c
  11. +41
    -0
      srcs/signal.c

+ 3
- 1
Makefile View File

@@ -2,6 +2,7 @@ NAME = minishell
CC = gcc
CFLAGS = -Wall -Wextra -Werror -fsanitize=address
SRC_NAME = main.c \
command/cmd_cd_util.c \
command/cmd_cd.c \
command/cmd_echo.c \
command/cmd_env.c \
@@ -46,7 +47,8 @@ SRC_NAME = main.c \
ready_to_execute.c \
run_one_cmd.c \
run_many_cmd.c \
shell.c
shell.c \
signal.c
SRC_DIR = srcs/
SRC = $(addprefix $(SRC_DIR), $(SRC_NAME))



+ 5
- 3
includes/command.h View File

@@ -3,10 +3,10 @@
/* ::: :::::::: */
/* command.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ina <ina@student.42seoul.kr> +#+ +:+ +#+ */
/* By: chlee <chlee@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/01/19 21:10:25 by chlee #+# #+# */
/* Updated: 2021/01/29 13:19:46 by ina ### ########.fr */
/* Updated: 2021/01/30 03:26:46 by chlee ### ########.fr */
/* */
/* ************************************************************************** */

@@ -31,7 +31,6 @@
# include "variable.h"
# include "minishell.h"
# include "string_array.h"
# include <stdio.h>

typedef struct s_shell t_shell;
typedef struct s_cmd t_cmd;
@@ -42,6 +41,9 @@ typedef struct s_export
char **value;
} t_export;

int cd_print_not_set_error(int option);
int cd_print_default_error(char *locate, int option);

int get_export_cnt(t_list *data);
void get_export_data(t_export *export, t_list *data);
void sort_export_data(t_export *export);


+ 6
- 2
includes/minishell.h View File

@@ -3,10 +3,10 @@
/* ::: :::::::: */
/* minishell.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ina <ina@student.42seoul.kr> +#+ +:+ +#+ */
/* By: chlee <chlee@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/07 17:33:25 by ina #+# #+# */
/* Updated: 2021/01/29 21:57:07 by ina ### ########.fr */
/* Updated: 2021/01/30 04:02:38 by chlee ### ########.fr */
/* */
/* ************************************************************************** */

@@ -23,6 +23,7 @@
# include <sys/wait.h>
# include <sys/types.h>
# include <sys/stat.h>
# include <signal.h>

# define SHELL_NAME "minishell"
# define MODE_DEFAULT 0
@@ -103,4 +104,7 @@ pid_t spawn_cmd(t_shell *shell, t_cmd *cmd);

int execute_builtin(t_shell *shell, t_cmd *cmd);

void main_signal_handler(int signum);
void cmd_signal_handler(int signum);

#endif

+ 30
- 40
srcs/command/cmd_cd.c View File

@@ -6,31 +6,12 @@
/* By: chlee <chlee@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/01/17 17:38:26 by chlee #+# #+# */
/* Updated: 2021/01/29 19:23:37 by chlee ### ########.fr */
/* Updated: 2021/01/30 05:24:49 by chlee ### ########.fr */
/* */
/* ************************************************************************** */

#include "command.h"

static int print_error(char *locate, int option)
{
if (option == OP_HOME || option == OP_DEFAULT)
{
ft_putstr_fd(SHELL_NAME, STDERR_FILENO);
ft_putstr_fd(": cd: ", STDERR_FILENO);
ft_putstr_fd(locate, STDERR_FILENO);
ft_putstr_fd(": ", STDERR_FILENO);
ft_putendl_fd(strerror(errno), STDERR_FILENO);
}
if (option == OP_OLDPWD)
{
ft_putstr_fd(SHELL_NAME, STDERR_FILENO);
ft_putendl_fd(": cd: --: invalid option", STDERR_FILENO);
ft_putendl_fd("cd: usage: cd [-L|-P] [dir]", STDERR_FILENO);
}
return (1);
}

static void update_oldpwd(t_list **list_var)
{
char *data;
@@ -44,18 +25,24 @@ static void update_oldpwd(t_list **list_var)
static char *make_path(t_list **list_var, int option)
{
char *path;
char *env_value;

path = NULL;
if (option == OP_HOME || option == OP_DEFAULT)
{
update_oldpwd(list_var);
path = ft_strdup(find_env(*list_var, "HOME"));
env_value = find_env(*list_var, "HOME");
if (env_value)
path = ft_strdup(env_value);
}
else if (option == OP_OLDPWD)
{
path = ft_strdup(find_env(*list_var, "OLDPWD"));
cmd_pwd();
update_oldpwd(list_var);
env_value = find_env(*list_var, "OLDPWD");
if (env_value)
{
path = ft_strdup(env_value);
update_oldpwd(list_var);
}
}
return (path);
}
@@ -68,19 +55,21 @@ static int process_home(t_list **list_var, char *str)

ret = 0;
path = make_path(list_var, OP_HOME);
if (!str || !ft_strncmp(str, "~", 2))
join_path = ft_strdup(path);
else if (!ft_strncmp(str, "~/", 2))
join_path = ft_strjoin(path, str + 1);
if (!path)
ret = cd_print_not_set_error(OP_HOME);
else
join_path = ft_strdup(str);
if (chdir(join_path) == -1)
{
ret = 1;
print_error(join_path, OP_HOME);
if (!str || !ft_strncmp(str, "~", 2))
join_path = ft_strdup(path);
else if (!ft_strncmp(str, "~/", 2))
join_path = ft_strjoin(path, str + 1);
else
join_path = ft_strdup(str);
if (chdir(join_path) == -1)
ret = cd_print_default_error(join_path, OP_HOME);
free(path);
free(join_path);
}
free(path);
free(join_path);
return (ret);
}

@@ -108,20 +97,21 @@ int cmd_cd(t_list *list_var, char **argv)
if (!argv[1] || !ft_strncmp(argv[1], "~", 1))
return (process_home(&list_var, argv[1]));
else if (!ft_strncmp(argv[1], "--", 2) && ft_strlen(argv[1]) != 2)
return (print_error(path, OP_OLDPWD));
return (cd_print_default_error(path, OP_OLDPWD));
else if (!ft_strncmp(argv[1], "--", 3))
{
path = make_path(&list_var, OP_HOME);
if (chdir(path) == -1)
ret = print_error(path, OP_OLDPWD);
if (!(path = make_path(&list_var, OP_HOME)))
ret = cd_print_not_set_error(OP_HOME);
}
else if (!ft_strncmp(argv[1], "-", 2))
{
path = make_path(&list_var, OP_OLDPWD);
if (!(path = make_path(&list_var, OP_OLDPWD)))
ret = cd_print_not_set_error(OP_OLDPWD);
chdir(path);
cmd_pwd();
}
else if (chdir(argv[1]) == -1)
ret = print_error(argv[1], OP_DEFAULT);
ret = cd_print_default_error(argv[1], OP_DEFAULT);
free(path);
return (ret);
}

+ 47
- 0
srcs/command/cmd_cd_util.c View File

@@ -0,0 +1,47 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cmd_cd_util.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: chlee <chlee@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/01/30 03:06:43 by chlee #+# #+# */
/* Updated: 2021/01/30 03:09:22 by chlee ### ########.fr */
/* */
/* ************************************************************************** */

#include "command.h"

int cd_print_not_set_error(int option)
{
if (option == OP_HOME)
{
ft_putstr_fd(SHELL_NAME, STDERR_FILENO);
ft_putendl_fd(": cd: HOME not set", STDERR_FILENO);
}
else if (option == OP_OLDPWD)
{
ft_putstr_fd(SHELL_NAME, STDERR_FILENO);
ft_putendl_fd(": cd: OLDPWD not set", STDERR_FILENO);
}
return (1);
}

int cd_print_default_error(char *locate, int option)
{
if (option == OP_HOME || option == OP_DEFAULT)
{
ft_putstr_fd(SHELL_NAME, STDERR_FILENO);
ft_putstr_fd(": cd: ", STDERR_FILENO);
ft_putstr_fd(locate, STDERR_FILENO);
ft_putstr_fd(": ", STDERR_FILENO);
ft_putendl_fd(strerror(errno), STDERR_FILENO);
}
if (option == OP_OLDPWD)
{
ft_putstr_fd(SHELL_NAME, STDERR_FILENO);
ft_putendl_fd(": cd: --: invalid option", STDERR_FILENO);
ft_putendl_fd("cd: usage: cd [-L|-P] [dir]", STDERR_FILENO);
}
return (1);
}

+ 1
- 1
srcs/execute_builtin.c View File

@@ -6,7 +6,7 @@
/* By: chlee <chlee@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/01/29 00:21:55 by ina #+# #+# */
/* Updated: 2021/01/29 15:46:19 by chlee ### ########.fr */
/* Updated: 2021/01/30 03:15:48 by chlee ### ########.fr */
/* */
/* ************************************************************************** */



+ 2
- 2
srcs/execute_cmd.c View File

@@ -3,10 +3,10 @@
/* ::: :::::::: */
/* execute_cmd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ina <ina@student.42seoul.kr> +#+ +:+ +#+ */
/* By: chlee <chlee@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/01/26 17:08:26 by ina #+# #+# */
/* Updated: 2021/01/29 22:01:04 by ina ### ########.fr */
/* Updated: 2021/01/30 04:39:33 by chlee ### ########.fr */
/* */
/* ************************************************************************** */



+ 2
- 1
srcs/execute_line.c View File

@@ -6,7 +6,7 @@
/* By: chlee <chlee@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/01/21 01:24:34 by ina #+# #+# */
/* Updated: 2021/01/29 19:15:51 by chlee ### ########.fr */
/* Updated: 2021/01/30 04:41:19 by chlee ### ########.fr */
/* */
/* ************************************************************************** */

@@ -42,6 +42,7 @@ void execute_line(t_shell *shell, char *cmd_line)
t_list *list_cmd;
int cmd_cnt;

signal(SIGINT, cmd_signal_handler);
arr_cmds = split_line(cmd_line, '|');
list_cmd = create_list_cmd(arr_cmds);
if (list_cmd)


+ 5
- 3
srcs/main.c View File

@@ -3,10 +3,10 @@
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ina <ina@student.42seoul.kr> +#+ +:+ +#+ */
/* By: chlee <chlee@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/07 17:01:45 by ina #+# #+# */
/* Updated: 2021/01/26 20:00:59 by ina ### ########.fr */
/* Updated: 2021/01/30 05:29:59 by chlee ### ########.fr */
/* */
/* ************************************************************************** */

@@ -21,13 +21,15 @@ int run_shell(t_shell *shell)
{
while (!shell->is_end)
{
signal(SIGQUIT, main_signal_handler);
signal(SIGINT, main_signal_handler);
print_prompt(MODE_DEFAULT);
get_verified_input(shell);
make_arr_cmd_line(shell);
execute_each_line(shell);
cleanup_shell(shell);
}
ft_putendl_fd("ENDFLAG!", STDOUT_FILENO);
ft_putendl_fd("exit", STDERR_FILENO);
return (0);
}



+ 2
- 2
srcs/run_one_cmd.c View File

@@ -3,10 +3,10 @@
/* ::: :::::::: */
/* run_one_cmd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ina <ina@student.42seoul.kr> +#+ +:+ +#+ */
/* By: chlee <chlee@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/01/24 17:27:24 by ina #+# #+# */
/* Updated: 2021/01/29 18:03:44 by ina ### ########.fr */
/* Updated: 2021/01/30 04:31:07 by chlee ### ########.fr */
/* */
/* ************************************************************************** */



+ 41
- 0
srcs/signal.c View File

@@ -0,0 +1,41 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* signal.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: chlee <chlee@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/01/30 03:23:41 by chlee #+# #+# */
/* Updated: 2021/01/30 05:30:46 by chlee ### ########.fr */
/* */
/* ************************************************************************** */

#include "minishell.h"

void main_signal_handler(int signo)
{
if (signo == SIGINT)
{
ft_putstr_fd("\n", STDERR_FILENO);
print_prompt(MODE_DEFAULT);
signal(SIGINT, main_signal_handler);
}
else if (signo == SIGQUIT)
{
signal(SIGQUIT, SIG_IGN);
}
}

void cmd_signal_handler(int signo)
{
if (signo == SIGINT)
{
ft_putstr_fd("\n", STDERR_FILENO);
signal(SIGINT, main_signal_handler);
}
if (signo == SIGQUIT)
{
ft_putstr_fd("Quit: 3\n", STDERR_FILENO);
signal(SIGQUIT, main_signal_handler);
}
}

Loading…
Cancel
Save