Merge manage variables with builtins & history

This commit is contained in:
mcolonna 2024-06-06 16:37:28 +02:00
commit 44daaaf097
7 changed files with 87 additions and 14 deletions

View file

@ -6,7 +6,7 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/23 15:03:23 by mcolonna #+# #+# */
/* Updated: 2024/05/01 12:46:34 by mcolonna ### ########.fr */
/* Updated: 2024/05/31 13:52:07 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
@ -16,8 +16,8 @@ const char *ask_command(const t_memclass mc)
{
const char *r;
print_str(fatal_error, 1, "\e[1m\e[38;5;45m( ^.^)> \e[0m");
r = read_line(fatal_error, mc, 0);
(void)mc;
r = readline("\e[1m\e[38;5;45m( ^.^)> \e[0m");
if (r)
return (r);
else

56
src/builtin.c Normal file
View file

@ -0,0 +1,56 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* builtin.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/03 12:02:45 by jschaft #+# #+# */
/* Updated: 2024/06/06 16:34:51 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#include "include.h"
// Check if str is the name of a builtin.
// Return true if it is, false if not.
bool is_builtin(const char *str)
{
return (
ft_strncmp(str, "cd", 3) == 0
|| ft_strncmp(str, "exit", 5) == 0
|| ft_strncmp(str, "export", 7) == 0
|| ft_strncmp(str, "unset", 6) == 0);
}
// 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 specified builtin.
// The builtin must exist.
// Return the error value returned by the builtin.
// TODO all the builtins
int exec_builtin(t_call call)
{
if (ft_strncmp(call.program, "cd", 3) == 0)
return (cd_builtin(call.argv[1]));
/* if (ft_strncmp(call.program, "exit", 5) == 0)
exit_builtin();
if (ft_strncmp(call.program, "export", 7) == 0)
return (export_builtin(call.argv));
if (ft_strncmp(call.program, "unset", 6) == 0)
return (unset_builtin());*/
return (1);
}

View file

@ -6,7 +6,7 @@
/* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/24 13:48:00 by jschaft #+# #+# */
/* Updated: 2024/05/17 14:43:37 by mcolonna ### ########.fr */
/* Updated: 2024/06/06 16:31:21 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
@ -102,14 +102,17 @@ 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];
r = execute_call(global, command.calls[i], inout, envp);
if (is_builtin(command.calls[i].program))
r = exec_builtin(command.calls[i]);
else
r = execute_call(global, command.calls[i], inout, envp);
if (r)
return (r);
}
return (r);
}
// TODO line jump if command not found
// FIXME line jump if command not found
int execute_command(t_memclass mc, t_command command, char *const envp[])
{
t_exec_command_global global;

View file

@ -6,7 +6,7 @@
/* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/23 14:33:45 by mcolonna #+# #+# */
/* Updated: 2024/06/05 17:45:43 by mcolonna ### ########.fr */
/* Updated: 2024/06/06 16:21:46 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
@ -54,6 +54,7 @@ int main(const int argc, const char *argv[], char *const envp[])
{
mc = mem_subclass(fatal_error, g_mc);
command_str = ask_command(mc);
add_history(command_str);
do_command(envp, &variables, command_str);
}
return (errorstatus);