This commit is contained in:
mcolonna 2024-06-28 12:06:17 +02:00
commit 368b46e82c
6 changed files with 52 additions and 28 deletions

View file

@ -5,9 +5,9 @@ SRCS = src/
# include directory
INCLUDES = include/ libtf/ libft/
# .c files in src/ without the extension
CODE = main ask_command error path parse_command exec_command builtin \
CODE = main ask_command error path parse_command exec_command builtin builtin2\
signals cool_readline variables parse_command_utils \
parse_command_read_string parse_command_read_element echo_builtin
parse_command_read_string parse_command_read_element
# directories to 'make'
LIBRARIES = libtf libft
# .a files to include

View file

@ -6,7 +6,7 @@
/* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/23 14:15:12 by mcolonna #+# #+# */
/* Updated: 2024/06/27 16:08:14 by mcolonna ### ########.fr */
/* Updated: 2024/06/28 11:52:13 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
@ -19,9 +19,12 @@
bool is_builtin(const char *str);
// Execute the builtin command
int exec_builtin(t_call call, t_env *env);
int exec_builtin(t_env *env, t_call call);
// Execute echo builtin
int echo_builtin(const char **argv);
// Execute cd builtin
int cd_builtin(t_env *env, t_call call);
#endif

View file

@ -6,7 +6,7 @@
/* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/03 12:02:45 by jschaft #+# #+# */
/* Updated: 2024/06/27 16:07:13 by mcolonna ### ########.fr */
/* Updated: 2024/06/28 11:52:31 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
@ -42,21 +42,6 @@ bool is_builtin(const char *str)
);
}
// Execute the builtin cd for the specified path.
// Return 0 if success, errno if error.
static int cd_builtin(const char *path)
{
int r;
r = chdir(path);
if (r)
{
perror("cd");
return (errno);
}
return (0);
}
// Execute the builtin pwd for the current directory.
// Return 0 for the sake of returning something for exec_builtin
static int pwd_builtin(void)
@ -86,10 +71,10 @@ static int env_builtin(char *const envp[])
// The builtin must exist.
// Return the error value returned by the builtin.
// TODO all the builtins
int exec_builtin(t_call call, t_env *env)
int exec_builtin(t_env *env, t_call call)
{
if (ft_strncmp(call.program, "cd", 3) == 0)
return (cd_builtin(call.argv[1]));
return (cd_builtin(env, call));
if (ft_strncmp(call.program, "exit", 5) == 0)
env->exit = true;
/* if (ft_strncmp(call.program, "export", 7) == 0)

View file

@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* echo_builtin.c :+: :+: :+: */
/* builtin2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/25 13:28:29 by jschaft #+# #+# */
/* Updated: 2024/06/27 15:51:09 by mcolonna ### ########.fr */
/* Updated: 2024/06/28 12:04:26 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
@ -35,3 +35,35 @@ int echo_builtin(const char **argv)
printf("\n");
return (0);
}
static int go_home(t_env *env)
{
return (chdir(variables_get(env->variables, "HOME")));
}
// Execute the builtin cd for the specified path.
// Return 0 if success, errno if error.
int cd_builtin(t_env *env, t_call call)
{
int r;
int argc;
argc = 0;
while (call.argv[argc])
argc++;
if (argc == 2)
r = chdir(call.argv[1]);
else if (argc == 1)
r = go_home(env);
else
{
print_line(fatal_error, 2, "cd: too many arguments");
return (1);
}
if (r)
{
perror("cd");
return (errno);
}
return (0);
}

View file

@ -6,7 +6,7 @@
/* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/24 13:48:00 by jschaft #+# #+# */
/* Updated: 2024/06/27 14:45:31 by mcolonna ### ########.fr */
/* Updated: 2024/06/28 12:03:49 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
@ -95,7 +95,7 @@ static int exec_each_call(t_exec_command_global *global,
inout[0] = global->pipes[i].pipe[0];
inout[1] = global->pipes[i + 1].pipe[1];
if (is_builtin(command.calls[i].program))
r = exec_builtin(command.calls[i], env);
r = exec_builtin(env, command.calls[i]);
else
r = execute_call(global, command.calls[i], inout, env->envp);
if (r)

View file

@ -6,7 +6,7 @@
/* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/23 14:33:45 by mcolonna #+# #+# */
/* Updated: 2024/06/27 15:55:02 by mcolonna ### ########.fr */
/* Updated: 2024/06/28 11:51:01 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
@ -79,8 +79,12 @@ int main(const int argc, const char *argv[], char *const envp[])
t_env env;
t_list variables;
(void)argc;
(void)argv;
if (argc != 1)
{
minishell_error("Takes no argument");
return (2);
}
env.exit = false;
env.mc_global = mem_newclass(fatal_error);
set_global_mc(env.mc_global);