refactoring
This commit is contained in:
6
Makefile
6
Makefile
@@ -10,7 +10,9 @@ DIR_SRCS = srcs
|
||||
INCLUDES = -I$(HEADERS_D) -I$(LIBFT_D)
|
||||
|
||||
HEADERS_D = ./headers
|
||||
HEADERS = minishell.h
|
||||
HEADERS = minishell.h \
|
||||
minishell_structs.h minishell_prototypes.h \
|
||||
minishell_macro.h minishell_term_colors.h \
|
||||
|
||||
LIBS = -L $(LIBFT_D) -lft \
|
||||
-lreadline -ltermcap
|
||||
@@ -18,7 +20,7 @@ LIBS = -L $(LIBFT_D) -lft \
|
||||
LIBFT_D = ./libft
|
||||
LIBFT = $(LIBFT_D)/libft.a
|
||||
|
||||
SRCS = main.c
|
||||
SRCS = main.c init.c generic.c
|
||||
|
||||
DIR_OBJS = builds
|
||||
OBJS = $(SRCS:%.c=$(DIR_OBJS)/%.o)
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/10/03 19:14:46 by lperrey #+# #+# */
|
||||
/* Updated: 2021/10/07 06:33:55 by lperrey ### ########.fr */
|
||||
/* Updated: 2021/10/08 03:04:26 by lperrey ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -33,6 +33,11 @@
|
||||
# include <readline/readline.h> // sudo apt install libreadline-dev
|
||||
# include <readline/history.h>
|
||||
|
||||
# include "minishell_structs.h"
|
||||
# include "minishell_macro.h"
|
||||
# include "minishell_term_colors.h"
|
||||
# include "minishell_prototypes.h"
|
||||
|
||||
/*
|
||||
** <stdio.h>: printf(), perror(), readline()
|
||||
** <unistd.h>: access(), unlink()
|
||||
|
||||
21
headers/minishell_macro.h
Normal file
21
headers/minishell_macro.h
Normal file
@@ -0,0 +1,21 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* minishell_macro.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/10/08 02:35:55 by lperrey #+# #+# */
|
||||
/* Updated: 2021/10/08 03:01:43 by lperrey ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef MINISHELL_MACRO_H
|
||||
# define MINISHELL_MACRO_H
|
||||
|
||||
// Macro RL_PROMPT_START_IGNORE == '\001'
|
||||
// Macro RL_PROMPT_END_IGNORE == '\002'
|
||||
# define PROMPT_CHEVRON "> "
|
||||
# define PROMPT_EURO "\001€\002 \001\b\002"
|
||||
|
||||
#endif
|
||||
24
headers/minishell_prototypes.h
Normal file
24
headers/minishell_prototypes.h
Normal file
@@ -0,0 +1,24 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* minishell_prototypes.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* 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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef MINISHELL_PROTOTYPES_H
|
||||
# define MINISHELL_PROTOTYPES_H
|
||||
|
||||
// Init
|
||||
int init(t_all *c);
|
||||
|
||||
// Generic
|
||||
char *ft_strjoinfree(char *s1, char *s2);
|
||||
char *ft_strjoinfree_s1(char *s1, const char *s2);
|
||||
char *ft_strjoinfree_s2(const char *s1, char *s2);
|
||||
|
||||
#endif
|
||||
22
headers/minishell_structs.h
Normal file
22
headers/minishell_structs.h
Normal file
@@ -0,0 +1,22 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* minishell_structs.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* 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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef MINISHELL_STRUCTS_H
|
||||
# define MINISHELL_STRUCTS_H
|
||||
|
||||
typedef struct s_all
|
||||
{
|
||||
char *prompt_base;
|
||||
char *prompt;
|
||||
} t_all;
|
||||
|
||||
#endif
|
||||
38
headers/minishell_term_colors.h
Normal file
38
headers/minishell_term_colors.h
Normal file
@@ -0,0 +1,38 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* minishell_term_colors.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/10/08 02:37:57 by lperrey #+# #+# */
|
||||
/* Updated: 2021/10/08 02:51:13 by lperrey ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef MINISHELL_TERM_COLORS_H
|
||||
# define MINISHELL_TERM_COLORS_H
|
||||
|
||||
// https://chrisyeh96.github.io/2020/03/28/terminal-colors.html
|
||||
|
||||
// Macro RL_PROMPT_START_IGNORE == '\001'
|
||||
// Macro RL_PROMPT_END_IGNORE == '\002'
|
||||
# define TERM_RESET "\001\e[0m\002"
|
||||
# define TERM_BLACK "\001\e[0;30m\002"
|
||||
# define TERM_GRAY "\001\e[1;30m\002"
|
||||
# define TERM_RED "\001\e[0;31m\002"
|
||||
# define TERM_LIGHT_RED "\001\e[1;31m\002"
|
||||
# define TERM_GREEN "\001\e[0;32m\002"
|
||||
# define TERM_LIGHT_GREEN "\001\e[1;32m\002"
|
||||
# define TERM_BROWN "\001\e[0;33m\002"
|
||||
# define TERM_YELLOW "\001\e[1;33m\002"
|
||||
# define TERM_BLUE "\001\e[0;34m\002"
|
||||
# define TERM_LIGHT_BLUE "\001\e[1;34m\002"
|
||||
# define TERM_PURPLE "\001\e[0;35m\002"
|
||||
# define TERM_LIGHT_PURPLE "\001\e[1;35m\002"
|
||||
# define TERM_CYAN "\001\e[0;36m\002"
|
||||
# define TERM_LIGHT_CYAN "\001\e[1;36m\002"
|
||||
# define TERM_LIGHT_GRAY "\001\e[0;37m\002"
|
||||
# define TERM_WHITE "\001\e[1;37m\002"
|
||||
|
||||
#endif
|
||||
41
srcs/generic.c
Normal file
41
srcs/generic.c
Normal file
@@ -0,0 +1,41 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* generic.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/10/08 09:25:35 by lperrey #+# #+# */
|
||||
/* Updated: 2021/10/08 09:28:49 by lperrey ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
char *ft_strjoinfree(char *s1, char *s2)
|
||||
{
|
||||
char *str;
|
||||
|
||||
str = ft_strjoin(s1, s2);
|
||||
free(s1);
|
||||
free(s2);
|
||||
return (str);
|
||||
}
|
||||
|
||||
char *ft_strjoinfree_s1(char *s1, const char *s2)
|
||||
{
|
||||
char *str;
|
||||
|
||||
str = ft_strjoin(s1, s2);
|
||||
free(s1);
|
||||
return (str);
|
||||
}
|
||||
|
||||
char *ft_strjoinfree_s2(const char *s1, char *s2)
|
||||
{
|
||||
char *str;
|
||||
|
||||
str = ft_strjoin(s1, s2);
|
||||
free(s2);
|
||||
return (str);
|
||||
}
|
||||
57
srcs/init.c
Normal file
57
srcs/init.c
Normal file
@@ -0,0 +1,57 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* init.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* 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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
static char *init_prompt_base(void);
|
||||
static char *init_prompt(char *prompt_base);
|
||||
|
||||
int init(t_all *c)
|
||||
{
|
||||
ft_bzero(c, sizeof *c);
|
||||
c->prompt_base = init_prompt_base();
|
||||
if (!c->prompt_base)
|
||||
return (ft_reti_perror(0, "init_prompt_base() fail"));
|
||||
c->prompt = init_prompt(c->prompt_base);
|
||||
if (!c->prompt)
|
||||
return (ft_reti_perror(0, "init_prompt() fail"));
|
||||
return (1);
|
||||
}
|
||||
|
||||
static char *init_prompt_base(void)
|
||||
{
|
||||
char *prompt_base;
|
||||
char *tmp;
|
||||
|
||||
tmp = getenv("USER");
|
||||
if (!tmp)
|
||||
tmp = getenv("LOGNAME");
|
||||
if (!tmp)
|
||||
tmp = "NoName";
|
||||
prompt_base = ft_strjoin(TERM_LIGHT_GREEN, tmp);
|
||||
prompt_base = ft_strjoinfree_s1(prompt_base, "@");
|
||||
tmp = getenv("NAME");
|
||||
if (!tmp)
|
||||
tmp = "NoHost";
|
||||
prompt_base = ft_strjoinfree_s1(prompt_base, tmp);
|
||||
prompt_base = ft_strjoinfree_s1(prompt_base, TERM_RESET":"TERM_LIGHT_BLUE);
|
||||
return (prompt_base);
|
||||
}
|
||||
|
||||
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);
|
||||
return (prompt);
|
||||
}
|
||||
109
srcs/main.c
109
srcs/main.c
@@ -6,66 +6,41 @@
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/10/04 05:59:26 by lperrey #+# #+# */
|
||||
/* Updated: 2021/10/07 07:20:04 by lperrey ### ########.fr */
|
||||
/* Updated: 2021/10/08 19:11:03 by lperrey ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
// Macro RL_PROMPT_START_IGNORE == '\001'
|
||||
// Macro RL_PROMPT_END_IGNORE == '\002'
|
||||
#define TERM_RESET "\001\e[0m\002"
|
||||
#define TERM_BLACK "\001\e[0;30m\002"
|
||||
#define TERM_GRAY "\001\e[1;30m\002"
|
||||
#define TERM_RED "\001\e[0;31m\002"
|
||||
#define TERM_LIGHT_RED "\001\e[1;31m\002"
|
||||
#define TERM_GREEN "\001\e[0;32m\002"
|
||||
#define TERM_LIGHT_GREEN "\001\e[1;32m\002"
|
||||
#define TERM_BROWN "\001\e[0;33m\002"
|
||||
#define TERM_YELLOW "\001\e[1;33m\002"
|
||||
#define TERM_BLUE "\001\e[0;34m\002"
|
||||
#define TERM_LIGHT_BLUE "\001\e[1;34m\002"
|
||||
#define TERM_PURPLE "\001\e[0;35m\002"
|
||||
#define TERM_LIGHT_PURPLE "\001\e[1;35m\002"
|
||||
#define TERM_CYAN "\001\e[0;36m\002"
|
||||
#define TERM_LIGHT_CYAN "\001\e[1;36m\002"
|
||||
#define TERM_LIGHT_GRAY "\001\e[0;37m\002"
|
||||
#define TERM_WHITE "\001\e[1;37m\002"
|
||||
|
||||
char *ft_strjoinfree_s1(char *s1, const char *s2)
|
||||
void shell_loop(t_all *c)
|
||||
{
|
||||
char *str;
|
||||
|
||||
str = ft_strjoin(s1, s2);
|
||||
free(s1);
|
||||
return (str);
|
||||
}
|
||||
|
||||
char *ft_strjoinfree_s2(const char *s1, char *s2)
|
||||
{
|
||||
char *str;
|
||||
|
||||
str = ft_strjoin(s1, s2);
|
||||
free(s2);
|
||||
return (str);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[], char *envp[])
|
||||
{
|
||||
char *prompt_base;
|
||||
char *prompt;
|
||||
char *line_input;
|
||||
|
||||
line_input = NULL;
|
||||
while (1)
|
||||
{
|
||||
if (line_input)
|
||||
free(line_input);
|
||||
line_input = readline(c->prompt);
|
||||
if (line_input && *line_input)
|
||||
{
|
||||
if (!ft_strncmp(line_input, "exit", 5))
|
||||
exit(EXIT_SUCCESS);
|
||||
else
|
||||
printf("echo: %s\n", line_input);
|
||||
}
|
||||
//rl_redisplay();
|
||||
//rl_on_new_line();
|
||||
}
|
||||
}
|
||||
|
||||
void wip_test()
|
||||
{
|
||||
char term_desc[2048];
|
||||
char *term_type;
|
||||
int term_width;
|
||||
int term_height;
|
||||
int ret;
|
||||
|
||||
//ft_putendl_arr_fd(envp, 1);
|
||||
//char *path = getenv("PATH");
|
||||
(void)argc;
|
||||
(void)argv;
|
||||
(void)envp;
|
||||
|
||||
term_type = getenv("TERM");
|
||||
if (term_type == 0)
|
||||
@@ -82,30 +57,20 @@ int main(int argc, char *argv[], char *envp[])
|
||||
PC = temp ? *temp : 0;
|
||||
BC = tgetstr ("le", BUFFADDR);
|
||||
UP = tgetstr ("up", BUFFADDR); */
|
||||
}
|
||||
|
||||
prompt_base = ft_strjoin(TERM_LIGHT_GREEN, getenv("USER"));
|
||||
prompt_base = ft_strjoinfree_s1(prompt_base, "@");
|
||||
prompt_base = ft_strjoinfree_s1(prompt_base, getenv("NAME"));
|
||||
prompt_base = ft_strjoinfree_s1(prompt_base, TERM_RESET":"TERM_LIGHT_BLUE);
|
||||
prompt = malloc(1);
|
||||
line_input = NULL;
|
||||
while (1)
|
||||
{
|
||||
if (line_input)
|
||||
free(line_input);
|
||||
free(prompt);
|
||||
prompt = ft_strjoinfree_s2(prompt_base, getcwd(NULL, 0));
|
||||
prompt = ft_strjoinfree_s1(prompt, TERM_RESET"& ");
|
||||
line_input = readline(prompt);
|
||||
if (line_input && *line_input)
|
||||
{
|
||||
if (!ft_strncmp(line_input, "exit", 5))
|
||||
exit(EXIT_SUCCESS);
|
||||
else
|
||||
printf("echo: %s\n", line_input);
|
||||
}
|
||||
//rl_redisplay();
|
||||
//rl_on_new_line();
|
||||
}
|
||||
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))
|
||||
exit(EXIT_FAILURE);
|
||||
shell_loop(&c);
|
||||
return (0);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user