Browse Source

-fix: builtins not fork when launched alone, -new: t_datas for env and exit_code, -rm: argc useless in t_command

master
narnaud 3 years ago
parent
commit
7c10094c65
  1. 19
      caller.c
  2. 53
      env.c
  3. 5
      lexer.c
  4. 41
      minishell.c
  5. 37
      minishell.h
  6. 4
      parser.c
  7. 10
      utils.c

19
caller.c

@ -6,13 +6,13 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ /* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/03 11:48:16 by narnaud #+# #+# */ /* Created: 2022/05/03 11:48:16 by narnaud #+# #+# */
/* Updated: 2022/05/03 20:29:01 by narnaud ### ########.fr */ /* Updated: 2022/05/05 09:40:42 by narnaud ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
int command_call(t_command *cmd) int command_call(t_datas *datas, t_command *cmd)
{ {
if (ft_strncmp(cmd->argv[0], "echo", 5) == 0) if (ft_strncmp(cmd->argv[0], "echo", 5) == 0)
return (ft_echo(cmd)); return (ft_echo(cmd));
@ -21,28 +21,25 @@ int command_call(t_command *cmd)
else if (ft_strncmp(cmd->argv[0], "cd", 3) == 0) else if (ft_strncmp(cmd->argv[0], "cd", 3) == 0)
return (ft_cd(cmd)); return (ft_cd(cmd));
else if (ft_strncmp(cmd->argv[0], "export", 7) == 0) else if (ft_strncmp(cmd->argv[0], "export", 7) == 0)
return (ft_export(cmd)); return (ft_export(datas, cmd));
else if (ft_strncmp(cmd->argv[0], "unset", 6) == 0) else if (ft_strncmp(cmd->argv[0], "unset", 6) == 0)
return (ft_unset(cmd)); return (ft_unset(datas, cmd));
else if (ft_strncmp(cmd->argv[0], "env", 4) == 0) else if (ft_strncmp(cmd->argv[0], "env", 4) == 0)
return (ft_env(cmd)); return (ft_env(datas));
else if (ft_strncmp(cmd->argv[0], "exit", 5) == 0) else if (ft_strncmp(cmd->argv[0], "exit", 5) == 0)
return (close_minishell(0)); return (close_minishell(0));
else else
{
errno = EINVAL;
return (-1); return (-1);
} }
}
void exe(t_command *cmd) void exe(t_datas *datas, t_command *cmd)
{ {
char **path_dirs; char **path_dirs;
char *path; char *path;
size_t path_length; size_t path_length;
if (ft_strchr(cmd->argv[0], '/')) if (ft_strchr(cmd->argv[0], '/'))
execve(cmd->argv[0], cmd->argv, *cmd->envp); execve(cmd->argv[0], cmd->argv, datas->envp);
else else
{ {
path_dirs = ft_split(getenv("PATH"), ':'); path_dirs = ft_split(getenv("PATH"), ':');
@ -53,7 +50,7 @@ void exe(t_command *cmd)
ft_memcpy(path, *path_dirs, ft_strlen(*path_dirs)); 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);
ft_memcpy(path + ft_strlen(*path_dirs) + 1, cmd->argv[0], ft_strlen(cmd->argv[0])); ft_memcpy(path + ft_strlen(*path_dirs) + 1, cmd->argv[0], ft_strlen(cmd->argv[0]));
execve(path, cmd->argv, *cmd->envp); execve(path, cmd->argv, datas->envp);
path_dirs++; path_dirs++;
} }
} }

53
env.c

@ -6,51 +6,52 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ /* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/16 09:41:29 by narnaud #+# #+# */ /* Created: 2022/02/16 09:41:29 by narnaud #+# #+# */
/* Updated: 2022/05/03 18:17:46 by narnaud ### ########.fr */ /* Updated: 2022/05/05 09:30:51 by narnaud ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
int ft_unset(t_command *cmd) int is_valid_identifier(char *ident)
{ {
if (!cmd->argv[1]) if (ft_isalpha(ident[0]) || ident[0] == '_')
{ {
//read(input_fd, PIPE_MAX_SIZE); while (*ident)
{
if (!ft_isalpha(*ident) && !ft_isdigit(*ident) && *ident != '_')
return (0);
ident++;
}
return (1); return (1);
} }
return (0); return (0);
} }
int ft_env(t_command *cmd) int ft_unset(t_datas *datas, t_command *cmd)
{ {
int i; (void)datas;
if (!cmd->argv[1])
i = 0;
while ((*cmd->envp)[i])
{ {
printf("%s\n", (*cmd->envp)[i]); //read(input_fd, PIPE_MAX_SIZE);
i++; return (1);
} }
return (0); return (0);
} }
int is_valid_identifier(char *ident) int ft_env(t_datas *datas)
{
if (ft_isalpha(ident[0]) || ident[0] == '_')
{ {
while (*ident) int i;
i = 0;
while (datas->envp[i])
{ {
if (!ft_isalpha(*ident) && !ft_isdigit(*ident) && *ident != '_') printf("%s\n", datas->envp[i]);
return (0); i++;
ident++;
}
return (1);
} }
return (0); return (0);
} }
int ft_export(t_command *cmd) int ft_export(t_datas *datas, t_command *cmd)
{ {
char **new; char **new;
char **env; char **env;
@ -63,18 +64,18 @@ int ft_export(t_command *cmd)
printf ("export: not an identifier: %s\n", new[0]); printf ("export: not an identifier: %s\n", new[0]);
return (0); return (0);
} }
while ((*cmd->envp)[i]) while (datas->envp[i])
{ {
env = ft_split((*cmd->envp)[i], '='); env = ft_split(datas->envp[i], '=');
if (!ft_strncmp(new[0], env[0], ft_strlen(new[0]) + 1)) if (!ft_strncmp(new[0], env[0], ft_strlen(new[0]) + 1))
{ {
(*cmd->envp)[i] = cmd->argv[1]; datas->envp[i] = cmd->argv[1];
return (1); return (1);
} }
i++; i++;
} }
(*cmd->envp)[i] = cmd->argv[1]; datas->envp[i] = cmd->argv[1];
(*cmd->envp)[i + 1] = NULL; datas->envp[i + 1] = NULL;
return (1); return (1);
} }

5
lexer.c

@ -6,7 +6,7 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ /* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/02 13:44:57 by narnaud #+# #+# */ /* Created: 2022/05/02 13:44:57 by narnaud #+# #+# */
/* Updated: 2022/05/03 11:46:33 by narnaud ### ########.fr */ /* Updated: 2022/05/05 08:43:52 by narnaud ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -44,7 +44,6 @@ int create_token(t_lexer *lex, char str[])
return (1); return (1);
} }
int check_register(t_lexer *lex, char **line, char *tmp) int check_register(t_lexer *lex, char **line, char *tmp)
{ {
int spaces; int spaces;
@ -93,7 +92,6 @@ int replace_var(char **line, char *tmp, int tmp_i)
char *var_name; char *var_name;
char *value; char *value;
// TODO : free split and var_name // TODO : free split and var_name
i = 1; i = 1;
while (ft_isalpha((*line)[i]) || ft_isdigit((*line)[i]) || (*line)[i] == '_') while (ft_isalpha((*line)[i]) || ft_isdigit((*line)[i]) || (*line)[i] == '_')
@ -109,7 +107,6 @@ int replace_var(char **line, char *tmp, int tmp_i)
return (tmp_i); return (tmp_i);
} }
int check_state(t_lexer *lex, char **line) int check_state(t_lexer *lex, char **line)
{ {
t_state new; t_state new;

41
minishell.c

@ -6,13 +6,13 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ /* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/02 12:14:09 by narnaud #+# #+# */ /* Created: 2022/05/02 12:14:09 by narnaud #+# #+# */
/* Updated: 2022/05/03 20:31:44 by narnaud ### ########.fr */ /* Updated: 2022/05/05 10:04:02 by narnaud ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
# include "minishell.h" # include "minishell.h"
int caller(t_command *cmd) int caller(t_datas *datas, t_command *cmd)
{ {
pid_t pid; pid_t pid;
int pip[2]; int pip[2];
@ -24,6 +24,8 @@ int caller(t_command *cmd)
cmd->fd[1] = pip[1]; cmd->fd[1] = pip[1];
cmd->next->fd[0] = pip[0]; cmd->next->fd[0] = pip[0];
} }
if (!cmd->prev && !cmd->next && command_call(datas, cmd) != -1)
return (1);
pid = fork(); pid = fork();
if (!pid) if (!pid)
{ {
@ -31,25 +33,28 @@ int caller(t_command *cmd)
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);
if (command_call(cmd) == -1) if (command_call(datas, cmd) == -1)
exe(cmd); exe(datas, cmd);
exit(1); exit(EXIT_SUCCESS);
} }
if (cmd->fd[1]) if (cmd->fd[1])
close(cmd->fd[1]); close(cmd->fd[1]);
if (cmd->next) if (cmd->next)
return (caller(cmd->next)); return (caller(datas, cmd->next));
waitpid(pid, &status, 0); waitpid(pid, &status, 0);
//if (WIFEXITED(status) && WEXITSTATUS(status))
// write(1, "\n", 1);
if (WIFSIGNALED(status)) if (WIFSIGNALED(status))
printf("QUIT: %d\n", WTERMSIG(status)); {
datas->exit_code = WTERMSIG(status);
if (datas->exit_code == 3)
printf("Quit: 3\n");
}
if (cmd->fd[0]) if (cmd->fd[0])
close(cmd->fd[0]); close(cmd->fd[0]);
return (1); return (1);
} }
t_command *parser(char ***envp, t_token *tok, t_command *prev) t_command *parser(t_token *tok, t_command *prev)
{ {
int i; int i;
t_command *cmd; t_command *cmd;
@ -60,9 +65,7 @@ t_command *parser(char ***envp, t_token *tok, t_command *prev)
if (prev) if (prev)
cmd->prev = prev; cmd->prev = prev;
i = 0; i = 0;
cmd->argc = count_arguments(tok); cmd->argv = ft_calloc(count_arguments(tok) + 1, sizeof(char *));
cmd->argv = ft_calloc(cmd->argc + 1, sizeof(char *));
cmd->envp = envp;
while(tok && tok->type != PIPE) while(tok && tok->type != PIPE)
{ {
if (tok->type) if (tok->type)
@ -72,11 +75,11 @@ t_command *parser(char ***envp, t_token *tok, t_command *prev)
tok = tok->next; tok = tok->next;
} }
if (DEBUG) if (DEBUG)
printf("######\n->cmd : %s, argc : %d, in_fd : %d, out_fd : %d\n", \ printf("######\n->cmd : %s, in_fd : %d, out_fd : %d\n", \
cmd->argv[0], cmd->argc, cmd->fd[0], cmd->fd[1]); cmd->argv[0], cmd->fd[0], cmd->fd[1]);
cmd->argv[i] = NULL; cmd->argv[i] = NULL;
if (tok && tok->type == PIPE) if (tok && tok->type == PIPE)
cmd->next = parser(envp, tok->next, cmd); cmd->next = parser(tok->next, cmd);
return(cmd); return(cmd);
} }
@ -112,7 +115,11 @@ t_token *lexer(char *line)
int main(int argc, char **argv, char **envp) int main(int argc, char **argv, char **envp)
{ {
t_datas datas;
char *line; char *line;
datas.envp = envp;
datas.exit_code = 0;
if (argc < 2) if (argc < 2)
(void)argv; (void)argv;
while (1) while (1)
@ -124,7 +131,7 @@ int main(int argc, char **argv, char **envp)
halt(EXIT_FAILURE); halt(EXIT_FAILURE);
if (is_empty(line)) if (is_empty(line))
continue ; continue ;
caller(parser(&envp, lexer(line), NULL)); caller(&datas, parser(lexer(line), NULL));
add_history(line); add_history(line);
} }
clear_history(); clear_history();

37
minishell.h

@ -6,7 +6,7 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ /* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/02 13:50:44 by narnaud #+# #+# */ /* Created: 2022/05/02 13:50:44 by narnaud #+# #+# */
/* Updated: 2022/05/03 20:22:29 by narnaud ### ########.fr */ /* Updated: 2022/05/05 09:38:23 by narnaud ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -33,37 +33,40 @@
typedef struct s_command typedef struct s_command
{ {
int argc;
char **argv; char **argv;
char ***envp;
int fd[2]; int fd[2];
char *heredoc; char *heredoc;
struct s_command *next; struct s_command *next;
struct s_command *prev; struct s_command *prev;
} t_command; } t_command;
typedef struct s_datas
{
char **envp;
int exit_code;
} t_datas;
// ----------------------------------Utils.c // ----------------------------------Utils.c
int is_empty(char *line);
void halt(int ret_code);
void termios(int ctl);
void sigs_handler(int sig_num);
// ----------------------------------Builtins.c
int close_minishell(int exit_code);
int ft_echo(t_command *cmd); int ft_echo(t_command *cmd);
int ft_pwd(t_command *cmd); int ft_pwd(t_command *cmd);
int ft_cd(t_command *cmd); int ft_cd(t_command *cmd);
int close_minishell(int exit_code);
// ----------------------------------Utils.c // ----------------------------------Env.c
int ft_unset(t_command *cmd);
int ft_env(t_command *cmd);
int is_valid_identifier(char *ident); int is_valid_identifier(char *ident);
int ft_export(t_command *cmd); int ft_unset(t_datas *datas, t_command *cmd);
int ft_env(t_datas *datas);
// ----------------------------------Utils.c int ft_export(t_datas *datas, t_command *cmd);
void halt(int ret_code);
int is_empty(char *line);
void termios(int ctl);
void sigs_handler(int sig_num);
// ----------------------------------Caller.c // ----------------------------------Caller.c
void exe(t_command *cmd); void exe(t_datas *datas, t_command *cmd);
int command_call(t_command *cmd); int command_call(t_datas *datas, t_command *cmd);
// ----------------------------------Parser.c // ----------------------------------Parser.c

4
parser.c

@ -6,7 +6,7 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ /* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/02 16:09:25 by narnaud #+# #+# */ /* Created: 2022/05/02 16:09:25 by narnaud #+# #+# */
/* Updated: 2022/05/03 12:43:48 by narnaud ### ########.fr */ /* Updated: 2022/05/05 08:28:41 by narnaud ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -37,7 +37,7 @@ size_t count_arguments(t_token *tok)
ret = 0; ret = 0;
while (tok && tok->type != PIPE) while (tok && tok->type != PIPE)
{ {
if (!tok->type == WORD) if (tok->type == WORD)
ret++; ret++;
tok = tok->next; tok = tok->next;
} }

10
utils.c

@ -6,7 +6,7 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ /* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/03 08:57:53 by narnaud #+# #+# */ /* Created: 2022/05/03 08:57:53 by narnaud #+# #+# */
/* Updated: 2022/05/03 09:00:40 by narnaud ### ########.fr */ /* Updated: 2022/05/05 09:18:28 by narnaud ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -26,10 +26,6 @@ int is_empty(char *line)
void nothing(int sig_num) void nothing(int sig_num)
{ {
(void)sig_num; (void)sig_num;
//if (sig_num == SIGQUIT)
// write(1, "QUIT: 3\n", 8);
//else if (sig_num == SIGINT)
// write(1, "\n", 1);
} }
void termios(int ctl) void termios(int ctl)
{ {
@ -39,8 +35,6 @@ 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)); exit ((perror("stdin"), EXIT_FAILURE));
//termios_p.c_oflag &= CRDLY;
//termios_p.c_lflag &= ICANON;
if (ctl) if (ctl)
{ {
termios_p.c_lflag |= ECHOCTL; termios_p.c_lflag |= ECHOCTL;
@ -69,6 +63,6 @@ void sigs_handler(int sig_num)
void halt(int ret_code) void halt(int ret_code)
{ {
printf("exit"); printf("exit\n");
exit(ret_code); exit(ret_code);
} }

Loading…
Cancel
Save