wip builtins: env, exit

This commit is contained in:
LuckyLaszlo
2021-10-10 08:57:33 +02:00
parent a7e0066458
commit c0068a5ec7
9 changed files with 113 additions and 21 deletions

View File

@@ -5,7 +5,7 @@ CC = clang
CFLAGS = -Wall -Wextra -Werror $(INCLUDES) -g
VPATH = $(DIR_SRCS)
DIR_SRCS = srcs
DIR_SRCS = srcs srcs/builtins
INCLUDES = -I$(HEADERS_D) -I$(LIBFT_D)
@@ -13,6 +13,7 @@ HEADERS_D = ./headers
HEADERS = minishell.h \
minishell_structs.h minishell_prototypes.h \
minishell_macro.h minishell_term_colors.h \
minishell_user_macro.h
LIBS = -L $(LIBFT_D) -lft \
-lreadline -ltermcap
@@ -20,7 +21,8 @@ LIBS = -L $(LIBFT_D) -lft \
LIBFT_D = ./libft
LIBFT = $(LIBFT_D)/libft.a
SRCS = main.c init.c generic.c
SRCS = main.c init.c generic.c \
env.c exit.c
DIR_OBJS = builds
OBJS = $(SRCS:%.c=$(DIR_OBJS)/%.o)

View File

@@ -6,7 +6,7 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/03 19:14:46 by lperrey #+# #+# */
/* Updated: 2021/10/08 03:04:26 by lperrey ### ########.fr */
/* Updated: 2021/10/10 03:37:29 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
@@ -36,6 +36,7 @@
# include "minishell_structs.h"
# include "minishell_macro.h"
# include "minishell_term_colors.h"
# include "minishell_user_macro.h"
# include "minishell_prototypes.h"
/*

View File

@@ -6,7 +6,7 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/08 02:59:58 by lperrey #+# #+# */
/* Updated: 2021/10/08 09:28:29 by lperrey ### ########.fr */
/* Updated: 2021/10/10 08:55:48 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
@@ -14,7 +14,11 @@
# define MINISHELL_PROTOTYPES_H
// Init
int init(t_all *c);
int init(t_all *c, char *envp[]);
// Builtins
int builtin_env(int argc, char *argv[], t_all *c);
int builtin_exit(int argc, char *argv[], t_all *c);
// Generic
char *ft_strjoinfree(char *s1, char *s2);

View File

@@ -6,7 +6,7 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/08 02:35:52 by lperrey #+# #+# */
/* Updated: 2021/10/08 18:59:02 by lperrey ### ########.fr */
/* Updated: 2021/10/10 05:39:09 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
@@ -15,6 +15,7 @@
typedef struct s_all
{
char **envp;
char *prompt_base;
char *prompt;
} t_all;

View File

@@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* minishell_user_macro.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/10 03:36:37 by lperrey #+# #+# */
/* Updated: 2021/10/10 04:55:46 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef MINISHELL_USER_MACRO_H
# define MINISHELL_USER_MACRO_H
# include "minishell_macro.h"
# define USER_PROMPT PROMPT_EURO
#endif

21
srcs/builtins/env.c Normal file
View File

@@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* env.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/10 05:01:26 by lperrey #+# #+# */
/* Updated: 2021/10/10 07:37:29 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int builtin_env(int argc, char *argv[], t_all *c) // WIP
{
(void)argc;
(void)argv;
ft_putendl_arr_fd(c->envp, 1);
return (0);
}

46
srcs/builtins/exit.c Normal file
View File

@@ -0,0 +1,46 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* exit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/10 05:01:22 by lperrey #+# #+# */
/* Updated: 2021/10/10 08:50:37 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int builtin_exit(int argc, char *argv[], t_all *c) // WIP
{
unsigned char status;
int i;
status = 0;
if (argc > 2)
return (ft_reti_print(1, "exit: too many arguments\n", 2));
if (argc == 2)
{
i = 0;
while (argv[1][i])
{
if ((argv[1][0] == '-' || argv[1][0] == '+') && argv[1][1] != '\0')
i++;
while (ft_isdigit(argv[1][i]))
i++;
if (argv[1][i] != '\0')
{
ft_putstr_fd("exit: ", 2);
ft_putstr_fd(argv[1], 2);
return (ft_reti_print(2, " numeric argument required\n", 2));
}
}
status = ft_atoi(argv[1]);
}
// TODO : remplacer exit(status) par
(void)c;
exit(status);
return (0);
// return (free_exit(&c, status));
}

View File

@@ -6,7 +6,7 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/08 09:22:12 by lperrey #+# #+# */
/* Updated: 2021/10/08 19:09:12 by lperrey ### ########.fr */
/* Updated: 2021/10/10 08:54:17 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
@@ -15,9 +15,10 @@
static char *init_prompt_base(void);
static char *init_prompt(char *prompt_base);
int init(t_all *c)
int init(t_all *c, char *envp[])
{
ft_bzero(c, sizeof *c);
c->envp = envp;
c->prompt_base = init_prompt_base();
if (!c->prompt_base)
return (ft_reti_perror(0, "init_prompt_base() fail"));
@@ -36,12 +37,12 @@ static char *init_prompt_base(void)
if (!tmp)
tmp = getenv("LOGNAME");
if (!tmp)
tmp = "NoName";
tmp = "NoUser";
prompt_base = ft_strjoin(TERM_LIGHT_GREEN, tmp);
prompt_base = ft_strjoinfree_s1(prompt_base, "@");
tmp = getenv("NAME");
if (!tmp)
tmp = "NoHost";
tmp = "NoName";
prompt_base = ft_strjoinfree_s1(prompt_base, tmp);
prompt_base = ft_strjoinfree_s1(prompt_base, TERM_RESET":"TERM_LIGHT_BLUE);
return (prompt_base);
@@ -52,6 +53,6 @@ static char *init_prompt(char *prompt_base)
char *prompt;
prompt = ft_strjoinfree_s2(prompt_base, getcwd(NULL, 0));
prompt = ft_strjoinfree_s1(prompt, TERM_RESET PROMPT_EURO);
prompt = ft_strjoinfree_s1(prompt, TERM_RESET USER_PROMPT);
return (prompt);
}

View File

@@ -6,7 +6,7 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/04 05:59:26 by lperrey #+# #+# */
/* Updated: 2021/10/08 19:11:03 by lperrey ### ########.fr */
/* Updated: 2021/10/10 08:50:20 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
@@ -24,13 +24,13 @@ void shell_loop(t_all *c)
line_input = readline(c->prompt);
if (line_input && *line_input)
{
if (!ft_strncmp(line_input, "exit", 5))
exit(EXIT_SUCCESS);
if (!ft_strncmp(line_input, "env", 4)) // temp placeholder
builtin_env(0, NULL, c);
else if (!ft_strncmp(line_input, "exit", 5)) // temp placeholder
builtin_exit(0, NULL, c);
else
printf("echo: %s\n", line_input);
}
//rl_redisplay();
//rl_on_new_line();
}
}
@@ -63,13 +63,9 @@ int main(int argc, char *argv[], char *envp[])
{
t_all c;
ft_putendl_arr_fd(envp, 1);
//char *path = getenv("PATH");
(void)argc;
(void)argv;
(void)envp;
if (!init(&c))
if (!init(&c, envp))
exit(EXIT_FAILURE);
shell_loop(&c);
return (0);