mirror of
https://codeberg.org/la-chouette/minishell.git
synced 2025-12-06 07:28:09 +01:00
Compare commits
6 commits
271794ef68
...
094cce3c8c
| Author | SHA1 | Date | |
|---|---|---|---|
| 094cce3c8c | |||
| f4e9955f75 | |||
| 3661feefaa | |||
| 74f013e5c6 | |||
| e84b88394a | |||
| 1df6a6ad9a |
10 changed files with 166 additions and 25 deletions
3
Makefile
3
Makefile
|
|
@ -31,11 +31,13 @@ srcs = \
|
|||
src/executing/here_doc/random_filename.c \
|
||||
src/executing/here_doc/strip_newline.c \
|
||||
src/executing/simple_cmd/builtin_cd.c \
|
||||
src/executing/simple_cmd/builtin_exit.c \
|
||||
src/executing/simple_cmd/builtin_export.c \
|
||||
src/executing/simple_cmd/builtin_invalid.c \
|
||||
src/executing/simple_cmd/builtin_pwd.c \
|
||||
src/executing/simple_cmd/builtins.c \
|
||||
src/executing/simple_cmd/simple_cmd_execute.c \
|
||||
src/executing/simple_cmd/subprocess.c \
|
||||
src/ft_errno.c \
|
||||
src/get_command.c \
|
||||
src/parser/cmdgroup/cmdgroup.c \
|
||||
|
|
@ -100,6 +102,7 @@ all: $(NAME)
|
|||
$(NAME): $(minishell_objs) $(LIBFT)
|
||||
$(CC) $(CFLAGS) -o $@ $(minishell_objs) $(LINCLUDE) $(LDLIBS)
|
||||
|
||||
$(LIBFT): CFLAGS+=-DBUFFER_SIZE=1
|
||||
$(LIBFT):
|
||||
+$(MAKE) -C $(LIBFTDIR)
|
||||
|
||||
|
|
|
|||
27
src/executing/simple_cmd/builtin_exit.c
Normal file
27
src/executing/simple_cmd/builtin_exit.c
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* builtin_exit.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/01 18:17:56 by khais #+# #+# */
|
||||
/* Updated: 2025/04/02 15:06:22 by khais ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "builtins.h"
|
||||
#include <stdlib.h>
|
||||
#include "libft.h"
|
||||
|
||||
t_builtin_type builtin_exit(t_simple_cmd *cmd, t_minishell *app)
|
||||
{
|
||||
int status;
|
||||
|
||||
status = 0;
|
||||
if (cmd->words->next != NULL)
|
||||
status = ft_atoi(cmd->words->next->word->word);
|
||||
simple_cmd_destroy(cmd);
|
||||
env_destroy(app->env);
|
||||
exit(status);
|
||||
}
|
||||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/01 16:37:21 by khais #+# #+# */
|
||||
/* Updated: 2025/04/01 16:37:38 by khais ### ########.fr */
|
||||
/* Updated: 2025/04/01 18:17:25 by khais ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -24,6 +24,8 @@ t_builtin_type get_builtin(t_simple_cmd *cmd)
|
|||
return (BUILTIN_CD);
|
||||
if (ft_strcmp("export", word) == 0)
|
||||
return (BUILTIN_EXPORT);
|
||||
if (ft_strcmp("exit", word) == 0)
|
||||
return (BUILTIN_EXIT);
|
||||
return (BUILTIN_INVALID);
|
||||
}
|
||||
|
||||
|
|
@ -35,6 +37,7 @@ t_builtin_type execute_builtin(t_simple_cmd *cmd, t_minishell *app)
|
|||
[BUILTIN_PWD] = builtin_pwd,
|
||||
[BUILTIN_CD] = builtin_cd,
|
||||
[BUILTIN_EXPORT] = builtin_export,
|
||||
[BUILTIN_EXIT] = builtin_exit,
|
||||
};
|
||||
|
||||
type = get_builtin(cmd);
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/03/31 14:16:13 by khais #+# #+# */
|
||||
/* Updated: 2025/04/01 16:37:15 by khais ### ########.fr */
|
||||
/* Updated: 2025/04/01 18:17:50 by khais ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -19,6 +19,7 @@ t_builtin_type builtin_invalid(t_simple_cmd *cmd, t_minishell *app);
|
|||
t_builtin_type builtin_pwd(t_simple_cmd *cmd, t_minishell *app);
|
||||
t_builtin_type builtin_cd(t_simple_cmd *cmd, t_minishell *app);
|
||||
t_builtin_type builtin_export(t_simple_cmd *cmd, t_minishell *app);
|
||||
t_builtin_type builtin_exit(t_simple_cmd *cmd, t_minishell *app);
|
||||
|
||||
t_builtin_type get_builtin(t_simple_cmd *cmd);
|
||||
t_builtin_type execute_builtin(t_simple_cmd *cmd, t_minishell *app);
|
||||
|
|
|
|||
|
|
@ -6,32 +6,19 @@
|
|||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/03/27 16:21:56 by khais #+# #+# */
|
||||
/* Updated: 2025/04/01 16:37:31 by khais ### ########.fr */
|
||||
/* Updated: 2025/04/02 19:15:08 by khais ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "simple_cmd_execute.h"
|
||||
#include "builtins.h"
|
||||
#include "subprocess.h"
|
||||
#include "libft.h"
|
||||
#include "../../subst/subst.h"
|
||||
#include "../../env/env_convert.h"
|
||||
#include <unistd.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
static char **argv_from_wordlist(t_wordlist *wordlist)
|
||||
{
|
||||
char **out;
|
||||
int i;
|
||||
|
||||
out = ft_calloc(wordlist_size(wordlist) + 1, sizeof(char *));
|
||||
i = 0;
|
||||
while (wordlist != NULL)
|
||||
{
|
||||
out[i++] = ft_strdup(wordlist->word->word);
|
||||
wordlist = wordlist->next;
|
||||
}
|
||||
return (out);
|
||||
}
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
static void command_not_found(t_simple_cmd *cmd)
|
||||
{
|
||||
|
|
@ -39,6 +26,17 @@ static void command_not_found(t_simple_cmd *cmd)
|
|||
cmd->words->word->word);
|
||||
}
|
||||
|
||||
static void do_waitpid(t_minishell *app, int pid)
|
||||
{
|
||||
int wstatus;
|
||||
|
||||
waitpid(pid, &wstatus, 0);
|
||||
if (WIFEXITED(wstatus))
|
||||
app->last_return_value = WEXITSTATUS(wstatus);
|
||||
if (WIFSIGNALED(wstatus))
|
||||
app->last_return_value = 128 + WTERMSIG(wstatus);
|
||||
}
|
||||
|
||||
void simple_cmd_execute(t_simple_cmd *cmd, t_minishell *app)
|
||||
{
|
||||
char *exe;
|
||||
|
|
@ -56,8 +54,8 @@ void simple_cmd_execute(t_simple_cmd *cmd, t_minishell *app)
|
|||
}
|
||||
pid = fork();
|
||||
if (pid == 0)
|
||||
execve(exe, argv_from_wordlist(cmd->words), envp_from_env(app->env));
|
||||
execute_subprocess(exe, cmd, app);
|
||||
free(exe);
|
||||
waitpid(pid, NULL, 0);
|
||||
do_waitpid(app, pid);
|
||||
return ;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/03/27 16:20:31 by khais #+# #+# */
|
||||
/* Updated: 2025/04/01 13:54:10 by khais ### ########.fr */
|
||||
/* Updated: 2025/04/01 18:17:33 by khais ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -24,6 +24,7 @@ typedef enum e_builtin_type
|
|||
BUILTIN_PWD,
|
||||
BUILTIN_CD,
|
||||
BUILTIN_EXPORT,
|
||||
BUILTIN_EXIT,
|
||||
} t_builtin_type;
|
||||
|
||||
#endif // SIMPLE_CMD_EXECUTE_H
|
||||
|
|
|
|||
50
src/executing/simple_cmd/subprocess.c
Normal file
50
src/executing/simple_cmd/subprocess.c
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* subprocess.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/02 18:19:23 by khais #+# #+# */
|
||||
/* Updated: 2025/04/02 18:20:54 by khais ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "subprocess.h"
|
||||
#include "../../env/env_convert.h"
|
||||
#include "../../parser/simple_cmd/simple_cmd.h"
|
||||
#include "../../subst/path_split.h"
|
||||
#include <stdio.h>
|
||||
|
||||
static char **argv_from_wordlist(t_wordlist *wordlist)
|
||||
{
|
||||
char **out;
|
||||
int i;
|
||||
|
||||
out = ft_calloc(wordlist_size(wordlist) + 1, sizeof(char *));
|
||||
i = 0;
|
||||
while (wordlist != NULL)
|
||||
{
|
||||
out[i++] = ft_strdup(wordlist->word->word);
|
||||
wordlist = wordlist->next;
|
||||
}
|
||||
return (out);
|
||||
}
|
||||
|
||||
static void ft_execve(char *exe, char **argv, char **envp)
|
||||
{
|
||||
execve(exe, argv, envp);
|
||||
ft_dprintf(STDERR_FILENO, "minishell: %s: ", argv[0]);
|
||||
perror(NULL);
|
||||
free(exe);
|
||||
path_split_destroy(argv);
|
||||
path_split_destroy(envp);
|
||||
}
|
||||
|
||||
void execute_subprocess(char *exe, t_simple_cmd *cmd, t_minishell *app)
|
||||
{
|
||||
ft_execve(exe, argv_from_wordlist(cmd->words), envp_from_env(app->env));
|
||||
simple_cmd_destroy(cmd);
|
||||
env_destroy(app->env);
|
||||
exit(127);
|
||||
}
|
||||
20
src/executing/simple_cmd/subprocess.h
Normal file
20
src/executing/simple_cmd/subprocess.h
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* subprocess.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/02 18:17:48 by khais #+# #+# */
|
||||
/* Updated: 2025/04/02 18:21:30 by khais ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef SUBPROCESS_H
|
||||
# define SUBPROCESS_H
|
||||
|
||||
# include "../../minishell.h"
|
||||
|
||||
void execute_subprocess(char *exe, t_simple_cmd *cmd, t_minishell *app);
|
||||
|
||||
#endif // SUBPROCESS_H
|
||||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/03/02 13:40:10 by jguelen #+# #+# */
|
||||
/* Updated: 2025/03/27 18:39:23 by khais ### ########.fr */
|
||||
/* Updated: 2025/04/01 18:38:35 by khais ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -77,8 +77,6 @@ static char *select_path(char *filepath, char **oldpath, char **path,
|
|||
else
|
||||
return (free(*oldpath), path_split_destroy(path), filepath);
|
||||
}
|
||||
if (*oldpath != filepath)
|
||||
free(filepath);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
|
|
|
|||
40
test.sh
40
test.sh
|
|
@ -335,6 +335,46 @@ status=1
|
|||
hello=hi
|
||||
EOF
|
||||
|
||||
when_run <<EOF "empty command is not found"
|
||||
""
|
||||
EOF
|
||||
expecting <<EOF
|
||||
minishell: : command not found
|
||||
EOF
|
||||
|
||||
when_run <<EOF "exit status of last command is preserved"
|
||||
echo \$?
|
||||
ls nonexist
|
||||
echo \$?
|
||||
echo hi
|
||||
echo \$?
|
||||
EOF
|
||||
expecting <<EOF
|
||||
0
|
||||
ls: cannot access 'nonexist': No such file or directory
|
||||
2
|
||||
hi
|
||||
0
|
||||
EOF
|
||||
|
||||
when_run <<EOF "exit works"
|
||||
$MINISHELL
|
||||
exit
|
||||
echo \$?
|
||||
EOF
|
||||
expecting <<EOF
|
||||
0
|
||||
EOF
|
||||
|
||||
when_run <<EOF "exit works with custom exit code"
|
||||
$MINISHELL
|
||||
exit 99
|
||||
echo \$?
|
||||
EOF
|
||||
expecting <<EOF
|
||||
99
|
||||
EOF
|
||||
|
||||
when_run <<EOF "quoted parentheses are not operators"
|
||||
echo unclosed '('
|
||||
EOF
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue