finish everything :)

This commit is contained in:
mcolonna 2024-06-28 15:31:08 +02:00
parent b72be8973e
commit 3007dc9980
5 changed files with 54 additions and 33 deletions

View file

@ -1,16 +1,3 @@
- finding commands
- built-in commands
- builtins:
x echo (with option -n)
x cd [path]
x pwd (with no options)
- export (with no options)
- unset (with no options)
x env (with no options or arguments)
x exit (with no options)
- fix
- the builtins should get all arguments and check if they're good
(unnecessary part) (unnecessary part)
- working history (rl_* functions?) - working history (rl_* functions?)
- '<<' with history? - '<<' with history?

View file

@ -6,7 +6,7 @@
/* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */ /* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/23 14:15:12 by mcolonna #+# #+# */ /* Created: 2024/04/23 14:15:12 by mcolonna #+# #+# */
/* Updated: 2024/06/28 14:25:50 by mcolonna ### ########.fr */ /* Updated: 2024/06/28 15:26:29 by mcolonna ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -22,7 +22,7 @@ bool is_builtin(const char *str);
int exec_builtin(t_env *env, t_call call); int exec_builtin(t_env *env, t_call call);
// Execute echo builtin // Execute echo builtin
int echo_builtin(const char **argv); int echo_builtin(t_env *env, t_call call);
// Execute cd builtin // Execute cd builtin
int cd_builtin(t_env *env, t_call call); int cd_builtin(t_env *env, t_call call);
@ -30,4 +30,7 @@ int cd_builtin(t_env *env, t_call call);
// Execute export builtin // Execute export builtin
int export_builtin(t_env *env, t_call call); int export_builtin(t_env *env, t_call call);
// Execute exit builtin
int exit_builtin(t_env *env, t_call call);
#endif #endif

View file

@ -6,7 +6,7 @@
/* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */ /* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/03 12:02:45 by jschaft #+# #+# */ /* Created: 2024/06/03 12:02:45 by jschaft #+# #+# */
/* Updated: 2024/06/28 14:24:40 by mcolonna ### ########.fr */ /* Updated: 2024/06/28 15:30:51 by mcolonna ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -18,7 +18,7 @@ static int unset_builtin(t_env *env, t_call call)
i = 0; i = 0;
while (call.argv[++i]) while (call.argv[++i])
variables_set(env->variables, call.argv[i], ""); variables_unset(env->variables, call.argv[i]);
return (0); return (0);
} }
@ -39,24 +39,33 @@ bool is_builtin(const char *str)
// Execute the builtin pwd for the current directory. // Execute the builtin pwd for the current directory.
// Return 0 for the sake of returning something for exec_builtin // Return 0 for the sake of returning something for exec_builtin
static int pwd_builtin(void) static int pwd_builtin(t_env *env, t_call call)
{ {
char *str; char *str;
(void)env;
if (call.argv[1])
{
print_line(fatal_error, 2, "too many arguments");
return (1);
}
str = getcwd(NULL, 0); str = getcwd(NULL, 0);
printf("%s\n", str); printf("%s\n", str);
free(str); free(str);
return (0); return (0);
} }
// TODO check arguments
static int env_builtin(t_env *env, t_call call) static int env_builtin(t_env *env, t_call call)
{ {
int i; int i;
const t_memclass mc = mem_subclass(fatal_error, env->mc_global); const t_memclass mc = mem_subclass(fatal_error, env->mc_global);
char *const *envp = variables_envp(env->variables, mc); char *const *envp = variables_envp(env->variables, mc);
(void)call; if (call.argv[1])
{
print_line(fatal_error, 2, "too many arguments");
return (1);
}
i = 0; i = 0;
while (envp[i] != NULL) while (envp[i] != NULL)
{ {
@ -70,21 +79,20 @@ static int env_builtin(t_env *env, t_call call)
// Execute the specified builtin. // Execute the specified builtin.
// The builtin must exist. // The builtin must exist.
// Return the error value returned by the builtin. // Return the error value returned by the builtin.
// TODO all the builtins
int exec_builtin(t_env *env, t_call call) int exec_builtin(t_env *env, t_call call)
{ {
if (ft_strncmp(call.program, "cd", 3) == 0) if (ft_strncmp(call.program, "cd", 3) == 0)
return (cd_builtin(env, call)); return (cd_builtin(env, call));
if (ft_strncmp(call.program, "exit", 5) == 0) if (ft_strncmp(call.program, "exit", 5) == 0)
env->exit = true; return (exit_builtin(env, call));
if (ft_strncmp(call.program, "export", 7) == 0) if (ft_strncmp(call.program, "export", 7) == 0)
return (export_builtin(env, call)); return (export_builtin(env, call));
if (ft_strncmp(call.program, "unset", 6) == 0) if (ft_strncmp(call.program, "unset", 6) == 0)
return (unset_builtin(env, call)); return (unset_builtin(env, call));
if (ft_strncmp(call.program, "pwd", 4) == 0) if (ft_strncmp(call.program, "pwd", 4) == 0)
return (pwd_builtin()); return (pwd_builtin(env, call));
if (ft_strncmp(call.program, "echo", 5) == 0) if (ft_strncmp(call.program, "echo", 5) == 0)
return (echo_builtin((const char **)call.argv)); return (echo_builtin(env, call));
if (ft_strncmp(call.program, "env", 4) == 0) if (ft_strncmp(call.program, "env", 4) == 0)
return (env_builtin(env, call)); return (env_builtin(env, call));
return (1); return (1);

View file

@ -6,13 +6,12 @@
/* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */ /* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/25 13:28:29 by jschaft #+# #+# */ /* Created: 2024/06/25 13:28:29 by jschaft #+# #+# */
/* Updated: 2024/06/28 14:50:17 by mcolonna ### ########.fr */ /* Updated: 2024/06/28 15:30:55 by mcolonna ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "include.h" #include "include.h"
// TODO 'name=value' notation
int export_builtin(t_env *env, t_call call) int export_builtin(t_env *env, t_call call)
{ {
int i; int i;
@ -41,22 +40,46 @@ int export_builtin(t_env *env, t_call call)
return (0); return (0);
} }
int echo_builtin(const char **argv) int exit_builtin(t_env *env, t_call call)
{
int r;
if (call.argv[1] && call.argv[2])
{
print_line(fatal_error, 2, "exit: too many arguments");
return (1);
}
r = env->errorstatus;
if (call.argv[1])
{
r = (int)str_touint(err_remember, call.argv[1]);
if (err_get())
{
print_line(fatal_error, 2, "exit: numeric argument required");
return (1);
}
}
env->exit = true;
return (r);
}
int echo_builtin(t_env *env, t_call call)
{ {
int i; int i;
bool newline; bool newline;
(void)env;
i = 1; i = 1;
newline = true; newline = true;
if (argv[1] && str_eq(argv[1], "-n")) if (call.argv[1] && str_eq(call.argv[1], "-n"))
{ {
newline = false; newline = false;
i++; i++;
} }
while (argv[i] != NULL) while (call.argv[i] != NULL)
{ {
printf("%s", argv[i]); printf("%s", call.argv[i]);
if (argv[i + 1] != NULL) if (call.argv[i + 1] != NULL)
printf(" "); printf(" ");
i++; i++;
} }

View file

@ -6,7 +6,7 @@
/* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */ /* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/16 12:07:26 by jschaft #+# #+# */ /* Created: 2023/11/16 12:07:26 by jschaft #+# #+# */
/* Updated: 2024/06/25 14:27:31 by mcolonna ### ########.fr */ /* Updated: 2024/06/28 15:28:55 by mcolonna ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -38,7 +38,7 @@ const char *search_path(
if (is_path(prog)) if (is_path(prog))
{ {
if (access(prog, X_OK) >= 0) if (access(prog, X_OK) >= 0)
return (ft_strdup(prog)); return (str_dup(fatal_error, mc, prog));
return (NULL); return (NULL);
} }
i = 0; i = 0;