Browse Source

-added: signals restaured, -rm: some useless things

master
narnaud 3 years ago
parent
commit
c4edead900
  1. 4
      Makefile
  2. 18
      README.md
  3. 39
      caller.c
  4. 41
      minishell.c
  5. 7
      minishell.h
  6. 102
      old_caller.c
  7. 90
      old_export.c
  8. 100
      old_parser.c
  9. 94
      old_utils.c
  10. 20
      parser.c
  11. 54
      utils.c

4
Makefile

@ -8,14 +8,14 @@ OBJS = ${SRCS:.c=.o}
LIB = -lreadline -lhistory -L. -lft LIB = -lreadline -lhistory -L. -lft
$(NAME): $(OBJS) $(NAME): $(LIBFT) $(OBJS)
gcc ${OBJS} ${LIB} -o ${NAME} gcc ${OBJS} ${LIB} -o ${NAME}
$(LIBFT): $(LIBFT):
${MAKE} -C ./libft ${MAKE} -C ./libft
cp ./libft/libft.a . cp ./libft/libft.a .
all: $(LIBFT) $(NAME) all: $(NAME)
%.o: %.c %.o: %.c
gcc -Werror -Wextra -Wall -g -c $< # -I ${READLINE_INC} #for rl_replace_line gcc -Werror -Wextra -Wall -g -c $< # -I ${READLINE_INC} #for rl_replace_line

18
README.md

@ -1,13 +1,19 @@
# Minishell #Minishell
---
## TODO : ## TODO :
- free stack,
- handle environment,
- finish built-ins,
- handle multiples heredocs,
- better env management
- j'ai oublié ><'
- ... ?
## Notes : ## Notes :
## Issues : ## Issues :
- cat | cat | ls crash on linux ?
- segfault on echo $PATH (g_env bad restaured)
-

39
caller.c

@ -14,19 +14,19 @@
int command_call(char *argv[]) int command_call(char *argv[])
{ {
if (ft_strncmp(argv[0], "echo", 4) == 0) if (ft_strncmp(argv[0], "echo", 5) == 0)
return (ft_echo(argv)); return (ft_echo(argv));
else if (ft_strncmp(argv[0], "pwd", 3) == 0) else if (ft_strncmp(argv[0], "pwd", 4) == 0)
return (ft_pwd(argv)); return (ft_pwd(argv));
else if (ft_strncmp(argv[0], "cd", 2) == 0) else if (ft_strncmp(argv[0], "cd", 3) == 0)
return (ft_cd(argv)); return (ft_cd(argv));
else if (ft_strncmp(argv[0], "export", 6) == 0) else if (ft_strncmp(argv[0], "export", 7) == 0)
return (ft_export(argv)); return (ft_export(argv));
else if (ft_strncmp(argv[0], "unset", 5) == 0) else if (ft_strncmp(argv[0], "unset", 6) == 0)
return (ft_unset(argv)); return (ft_unset(argv));
else if (ft_strncmp(argv[0], "env", 3) == 0) else if (ft_strncmp(argv[0], "env", 4) == 0)
return (ft_env(argv)); return (ft_env(argv));
else if (ft_strncmp(argv[0], "exit", 4) == 0) else if (ft_strncmp(argv[0], "exit", 5) == 0)
return (close_minishell(0)); return (close_minishell(0));
else else
{ {
@ -41,17 +41,20 @@ void exe(char *argv[], char *const envp[])
char *path; char *path;
size_t path_length; size_t path_length;
execve(argv[0], argv, envp); if (ft_strchr(argv[0], '/'))
path_dirs = ft_split(getenv("PATH"), ':'); execve(argv[0], argv, envp);
while (path_dirs) else
{ {
path_length = ft_strlen(*path_dirs) + ft_strlen(argv[0]) + 2; path_dirs = ft_split(getenv("PATH"), ':');
path = ft_calloc(path_length, sizeof(char)); while (path_dirs)
ft_memcpy(path, *path_dirs, ft_strlen(*path_dirs)); {
ft_memcpy(path + ft_strlen(*path_dirs),"/", 1); path_length = ft_strlen(*path_dirs) + ft_strlen(argv[0]) + 2;
ft_memcpy(path + ft_strlen(*path_dirs) + 1, argv[0], ft_strlen(argv[0])); path = ft_calloc(path_length, sizeof(char));
free(path); ft_memcpy(path, *path_dirs, ft_strlen(*path_dirs));
execve(path, argv, envp); ft_memcpy(path + ft_strlen(*path_dirs),"/", 1);
path_dirs++; ft_memcpy(path + ft_strlen(*path_dirs) + 1, argv[0], ft_strlen(argv[0]));
execve(path, argv, envp);
path_dirs++;
}
} }
} }

41
minishell.c

@ -16,7 +16,6 @@ char **g_env;
int caller(t_command *cmd) int caller(t_command *cmd)
{ {
int cmd_ret;
pid_t pid; pid_t pid;
int pip[2]; int pip[2];
int status; int status;
@ -30,16 +29,15 @@ int caller(t_command *cmd)
pid = fork(); pid = fork();
if (!pid) if (!pid)
{ {
cmd_ret = command_call(cmd->argv); if (command_call(cmd->argv) == -1)
if (cmd_ret == -1)
{ {
if (cmd->fd[0]) if (cmd->fd[0])
dup2(cmd->fd[0], STDIN_FILENO); dup2(cmd->fd[0], STDIN_FILENO);
if (cmd->fd[1]) if (cmd->fd[1])
dup2(cmd->fd[1], STDOUT_FILENO); dup2(cmd->fd[1], STDOUT_FILENO);
exe(cmd->argv, cmd->envp); exe(cmd->argv, cmd->envp);
exit(1);
} }
exit(1);
} }
if (cmd->fd[1]) if (cmd->fd[1])
close(cmd->fd[1]); close(cmd->fd[1]);
@ -48,7 +46,7 @@ int caller(t_command *cmd)
waitpid(pid, &status, 0); waitpid(pid, &status, 0);
if (cmd->fd[0]) if (cmd->fd[0])
close(cmd->fd[0]); close(cmd->fd[0]);
return (cmd_ret); return (1);
} }
t_command *parser(t_token *tok, t_command *prev) t_command *parser(t_token *tok, t_command *prev)
@ -111,31 +109,26 @@ t_token *lexer(char *line)
return (lex->tokens); return (lex->tokens);
} }
int launcher(t_env env)
{
char *line;
(void)env;
// TODO: handle ret
line = readline("$ ");
if (line == NULL)
halt(EXIT_FAILURE);
if (is_empty(line))
return (EXIT_FAILURE);
caller(parser(lexer(line), NULL));
add_history(line);
return (0);
}
int main(int argc, char **argv, char**envp) int main(int argc, char **argv, char**envp)
{ {
char *line;
if (argc < 2) if (argc < 2)
(void)argv; (void)argv;
g_env = envp; g_env = envp;
t_env env; signal(SIGINT, sigs_handler);
env.null = 0; signal(SIGQUIT, sigs_handler);
while (1) while (1)
launcher(env); {
termios(0);
line = readline("$ ");
termios(1);
if (line == NULL)
halt(EXIT_FAILURE);
if (is_empty(line))
continue ;
caller(parser(lexer(line), NULL));
add_history(line);
}
clear_history(); clear_history();
return (EXIT_SUCCESS); return (EXIT_SUCCESS);
} }

7
minishell.h

@ -49,13 +49,10 @@ int ft_export(char **argv);
void halt(int ret_code); void halt(int ret_code);
int is_empty(char *line); int is_empty(char *line);
int init_file(char *filename, int mode); void termios(int ctl);
void sigs_handler(int sig_num);
// ----------------------------------Caller.c // ----------------------------------Caller.c
typedef struct s_env
{
int null;
} t_env;
typedef struct s_command typedef struct s_command
{ {

102
old_caller.c

@ -1,102 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* caller.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/16 16:46:53 by narnaud #+# #+# */
/* Updated: 2022/03/24 17:15:24 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int command_call(char *argv[], int input_fd, int output_fd)
{
if (ft_strncmp(argv[0], "echo", 4) == 0)
return (ft_echo(argv, output_fd));
else if (ft_strncmp(argv[0], "pwd", 3) == 0)
return (ft_pwd(argv, input_fd, output_fd));
else if (ft_strncmp(argv[0], "cd", 2) == 0)
return (ft_cd(argv, input_fd, output_fd));
else if (ft_strncmp(argv[0], "export", 6) == 0)
return (ft_export(argv, input_fd, output_fd));
else if (ft_strncmp(argv[0], "unset", 5) == 0)
return (ft_unset(argv, input_fd, output_fd));
else if (ft_strncmp(argv[0], "env", 3) == 0)
return (ft_env(argv, input_fd, output_fd));
else if (ft_strncmp(argv[0], "exit", 4) == 0)
return (close_minishell(0));
else
{
errno = EINVAL;
return (-1);
}
}
void exe(char *argv[], char *const envp[])
{
char **path_dirs;
char *path;
size_t path_length;
execve(argv[0], argv, envp);
path_dirs = ft_split(getenv("PATH"), ':');
while (path_dirs)
{
path_length = ft_strlen(*path_dirs) + ft_strlen(argv[0]) + 2;
path = ft_calloc(path_length, sizeof(char));
ft_memcpy(path, *path_dirs, ft_strlen(*path_dirs));
ft_memcpy(path + ft_strlen(*path_dirs),"/", 1);
ft_memcpy(path + ft_strlen(*path_dirs) + 1, argv[0], ft_strlen(argv[0]));
free(path);
execve(path, argv, envp);
path_dirs++;
}
}
int caller(t_command *command, int input_fd, int output_fd)
{
pid_t pid;
int cmd_ret;
int status;
if (command->input != 0)
input_fd = command->fd[0];
if (command->output != 1)
output_fd = command->fd[1];
else if (!command->next)
output_fd = 1;
cmd_ret = command_call(command->argv, input_fd, output_fd);
if (cmd_ret == -1 && errno == EINVAL)
{
pid = fork();
termios(1);
if (pid == 0)
{
if (command->prev || command->input || command->heredoc)
{
dup2(input_fd, STDIN_FILENO);
close(input_fd);
}
if (command->next || command->output)
dup2(output_fd, STDOUT_FILENO);
exe(command->argv, command->envp);
}
//signal(SIGINT, NULL);
//signal(SIGQUIT, NULL);
waitpid(pid, &status, 0);
//signal(SIGINT, sigs_handler);
//signal(SIGQUIT, sigs_handler);
termios(0);
if (command->output)
close(command->output);
if (WIFSIGNALED(status))
return (-1);
if (!WIFEXITED(status))
printf("Minishell: command not found: '%s'\n", command->argv[0]);
}
return (0);
}

90
old_export.c

@ -1,90 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* export.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/16 09:41:29 by narnaud #+# #+# */
/* Updated: 2022/04/14 07:45:21 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int ft_unset(char **argv, int input_fd, int output_fd)
{
(void)output_fd;
(void)input_fd;
if (!argv[1])
{
//read(input_fd, PIPE_MAX_SIZE);
return (1);
}
return (0);
}
int ft_env(char **argv, int input_fd, int output_fd)
{
(void)output_fd;
(void)input_fd;
(void)argv;
int i;
i = 0;
while (g_env[i])
{
printf("%s\n", g_env[i]);
i++;
}
return (0);
}
int is_valid_identifier(char *ident)
{
if (ft_isalpha(ident[0]) || ident[0] == '_')
{
while (*ident)
{
if (!ft_isalpha(*ident) && !ft_isdigit(*ident) && *ident != '_')
return (0);
ident++;
}
return (1);
}
return (0);
}
int ft_export(char **argv, int input_fd, int output_fd)
{
char **new;
char **env;
int i;
(void)output_fd;
(void)input_fd;
i = 0;
new = ft_split(argv[1], '=');
if (!is_valid_identifier(new[0]))
{
printf ("export: not an identifier: %s\n", new[0]);
return (0);
}
while (g_env[i])
{
env = ft_split(g_env[i], '=');
if (!ft_strncmp(new[0], env[0], ft_strlen(new[0]) + 1))
{
g_env[i] = argv[1];
return (1);
}
i++;
}
g_env[i] = argv[1];
g_env[i + 1] = NULL;
return (1);
}

100
old_parser.c

@ -1,100 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parser.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/06 07:52:29 by narnaud #+# #+# */
/* Updated: 2022/04/14 09:19:12 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
size_t count_arguments(t_token tok)
{
size_t ret;
ret = 0;
while (tok.value)
{
if (tok.type == 0 && tok.value[0] != '|')
ret++;
if (tok.next)
tok = *(tok.next);
else
break ;
}
return (ret);
}
void update_redir(t_command *cmd, t_token tok)
{
if (tok.type == OUT)
{
if (cmd->fd[1])
close(cmd->fd[1]);
cmd->output = init_file(tok.value, O_TRUNC | O_WRONLY);
}
else if (tok.type == ADD)
{
if (cmd->fd[1])
close(cmd->fd[1]);
cmd->fd[1] = init_file(tok.value, O_APPEND | O_WRONLY);
}
else if (tok.type == IN)
{
if (cmd->fd[0])
close(cmd->fd[0]);
cmd->fd[0] = init_file(tok.value, O_RDONLY);
}
else if (tok.type == HD)
{
if (cmd->fd[0])
close(cmd->fd[0]);
cmd->fd[0] = 0;
cmd->heredoc = tok.value;
}
}
t_command *parser(t_token tok)
{
int i;
t_command *cmd;
t_command *ret;
cmd = ft_calloc(1, sizeof(t_command));
ret = cmd;
while (tok.value)
{
i = 0;
cmd->argv = ft_calloc(count_arguments(tok) + 1, sizeof(char *));
while(tok.value[0] != '|')
{
if (tok.type)
update_redir(cmd, tok);
else
{
cmd->argv[i] = tok.value;
i++;
}
if (tok.next)
tok = *(tok.next);
else
break ;
}
//printf("Input : %d\n", cmd->input);
//printf("Output : %d\n", cmd->output);
cmd->argv[i] = NULL;
if (tok.next)
tok = *(tok.next);
else
break ;
cmd->next = ft_calloc(1, sizeof(t_command));
cmd->next->prev = cmd;
cmd = cmd->next;
}
return (ret);
}

94
old_utils.c

@ -1,94 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/07 11:54:34 by narnaud #+# #+# */
/* Updated: 2022/03/22 15:38:08 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
char **get_commands(char *line)
{
char **split;
split = ft_split(line, '|');
return (split);
}
int is_empty(char *line)
{
while (*line)
{
if (*line <= 9 || (*line >= 13 && *line != 32))
return (0);
line++;
}
return (1);
}
int msg_n_ret(char *msg, int ret)
{
printf("%s", msg);
return (ret);
}
int init_file(char *filename, int mode)
{
return (open(filename, mode | O_CREAT, 0644));
}
int is_space(char ch)
{
if (ch == 32 || (ch > 8 && ch < 14))
return (1);
else
return (0);
}
void termios(int ctl)
{
struct termios termios_p;
int tty;
tty = ttyslot();
if (tcgetattr(tty, &termios_p) < 0)
{
perror("stdin");
exit (EXIT_FAILURE);
}
//termios_p.c_oflag &= CRDLY;
//termios_p.c_lflag &= ICANON;
if (ctl)
{
termios_p.c_lflag |= ECHOCTL;
termios_p.c_cc[VQUIT] = 28;
}
else
{
termios_p.c_lflag &= ~(ECHOCTL);
termios_p.c_cc[VQUIT] = 0;
}
tcsetattr(tty, TCSANOW, &termios_p);
}
void sigs_handler(int sig_num)
{
if (sig_num == SIGQUIT)
printf("QUIT: 3\n");
else if (sig_num == SIGINT)
printf("\n");
rl_on_new_line();
rl_replace_line("", 0);
rl_redisplay();
return ;
}
void halt(int ret_code)
{
printf("exit");
exit(ret_code);
}

20
parser.c

@ -12,6 +12,20 @@
#include "minishell.h" #include "minishell.h"
void push_heredoc(char *str, int fd)
{
char *line;
while(1)
{
line = readline(">");
if (!ft_strncmp(line, str, ft_strlen(str) + 1))
break;
ft_putstr_fd(line, fd);
write(fd, "\n", 1);
}
}
size_t count_arguments(t_token *tok) size_t count_arguments(t_token *tok)
{ {
size_t ret; size_t ret;
@ -32,19 +46,19 @@ void update_redir(t_command *cmd, t_token *tok)
{ {
if (cmd->fd[1]) if (cmd->fd[1])
close(cmd->fd[1]); close(cmd->fd[1]);
cmd->fd[1] = init_file(tok->value, O_TRUNC | O_WRONLY); cmd->fd[1] = open(tok->value, O_CREAT | O_TRUNC | O_WRONLY, 0644);
} }
else if (tok->type == ADD) else if (tok->type == ADD)
{ {
if (cmd->fd[1]) if (cmd->fd[1])
close(cmd->fd[1]); close(cmd->fd[1]);
cmd->fd[1] = init_file(tok->value, O_APPEND | O_WRONLY); cmd->fd[1] = open(tok->value, O_CREAT | O_APPEND | O_WRONLY, 0644);
} }
else if (tok->type == IN) else if (tok->type == IN)
{ {
if (cmd->fd[0]) if (cmd->fd[0])
close(cmd->fd[0]); close(cmd->fd[0]);
cmd->fd[0] = init_file(tok->value, O_RDONLY); cmd->fd[0] = open(tok->value, O_RDONLY);
} }
else if (tok->type == HD) else if (tok->type == HD)
{ {

54
utils.c

@ -12,28 +12,6 @@
#include "minishell.h" #include "minishell.h"
void push_heredoc(char *str, int fd)
{
char *line;
while(1)
{
line = readline(">");
if (!ft_strncmp(line, str, ft_strlen(str) + 1))
break;
ft_putstr_fd(line, fd);
write(fd, "\n", 1);
}
}
char **get_commands(char *line)
{
char **split;
split = ft_split(line, '|');
return (split);
}
int is_empty(char *line) int is_empty(char *line)
{ {
while (*line) while (*line)
@ -45,25 +23,6 @@ int is_empty(char *line)
return (1); return (1);
} }
int msg_n_ret(char *msg, int ret)
{
printf("%s", msg);
return (ret);
}
int init_file(char *filename, int mode)
{
return (open(filename, mode | O_CREAT, 0644));
}
int is_space(char ch)
{
if (ch == 32 || (ch > 8 && ch < 14))
return (1);
else
return (0);
}
void termios(int ctl) void termios(int ctl)
{ {
struct termios termios_p; struct termios termios_p;
@ -71,22 +30,14 @@ void termios(int ctl)
tty = ttyslot(); tty = ttyslot();
if (tcgetattr(tty, &termios_p) < 0) if (tcgetattr(tty, &termios_p) < 0)
{ exit ((perror("stdin"), EXIT_FAILURE));
perror("stdin");
exit (EXIT_FAILURE);
}
//termios_p.c_oflag &= CRDLY; //termios_p.c_oflag &= CRDLY;
//termios_p.c_lflag &= ICANON; //termios_p.c_lflag &= ICANON;
if (ctl) if (ctl)
{
termios_p.c_lflag |= ECHOCTL; termios_p.c_lflag |= ECHOCTL;
termios_p.c_cc[VQUIT] = 28;
}
else else
{
termios_p.c_lflag &= ~(ECHOCTL); termios_p.c_lflag &= ~(ECHOCTL);
termios_p.c_cc[VQUIT] = 0; termios_p.c_cc[VQUIT] = ctl * 28;
}
tcsetattr(tty, TCSANOW, &termios_p); tcsetattr(tty, TCSANOW, &termios_p);
} }
@ -101,6 +52,7 @@ void sigs_handler(int sig_num)
rl_redisplay(); rl_redisplay();
return ; return ;
} }
void halt(int ret_code) void halt(int ret_code)
{ {
printf("exit"); printf("exit");

Loading…
Cancel
Save