」 feat(Prompt): prompt working

This commit is contained in:
2024-04-27 17:39:26 +02:00
parent 6c254630fa
commit 5c7a4e8b5a
19 changed files with 121 additions and 83 deletions

0
.minishellrc Normal file
View File

BIN
.minishellrc.swp Normal file

Binary file not shown.

21
compile_commands.json Normal file
View File

@ -0,0 +1,21 @@
[
{
"directory": "/nfs/homes/adjoly/Documents/ft_minipowershell",
"arguments": [
"cc",
"-I",
"include/",
"-I",
"libft/",
"-Werror",
"-Wall",
"-Wextra",
"-g",
"src/main.c",
"-c",
"-o",
"obj/src/main.o"
],
"file": "src/main.c"
}
]

View File

@ -1,28 +1,22 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* get_next_line.h :+: :+: :+: */ /* parsing.h :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/01 17:12:00 by adjoly #+# #+# */ /* Created: 2024/04/25 12:20:26 by adjoly #+# #+# */
/* Updated: 2024/02/04 15:24:21 by adjoly ### ########.fr */ /* Updated: 2024/04/25 14:30:50 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#ifndef GET_NEXT_LINE_H #ifndef PARSING_H
# define GET_NEXT_LINE_H # define PARSING_H
# include <unistd.h> typedef struct s_cmd
# include <stdlib.h> {
char *cmd;
# ifndef BUFFER_SIZE char **argv;
# define BUFFER_SIZE 1 } t_cmd;
# endif
char *get_next_line(int fd);
char *ft_strjoin_gnl(char *s1, char *s2);
void *ft_calloc(size_t nmemb, size_t size);
size_t ft_strlen(const char *s);
#endif #endif

View File

@ -6,7 +6,7 @@
# By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ # # By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2023/11/01 11:03:22 by adjoly #+# #+# # # Created: 2023/11/01 11:03:22 by adjoly #+# #+# #
# Updated: 2024/03/18 14:33:40 by adjoly ### ########.fr # # Updated: 2024/04/27 17:17:00 by adjoly ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
@ -37,6 +37,7 @@ SRCS = is/ft_isalnum.c \
mem/ft_memcpy.c \ mem/ft_memcpy.c \
mem/ft_memmove.c \ mem/ft_memmove.c \
mem/ft_memset.c \ mem/ft_memset.c \
mem/ft_freetab.c \
print/ft_putchar.c \ print/ft_putchar.c \
print/ft_putchar_fd.c \ print/ft_putchar_fd.c \
print/ft_putendl_fd.c \ print/ft_putendl_fd.c \
@ -70,16 +71,16 @@ SRCS = is/ft_isalnum.c \
print/printf/ft_putnbrbase.c \ print/printf/ft_putnbrbase.c \
print/printf/ft_putnbr.c \ print/printf/ft_putnbr.c \
print/printf/ft_putstr.c \ print/printf/ft_putstr.c \
io/get_next_line/get_next_line.c \
io/get_next_line/get_next_line_utils.c \
OBJS = $(addprefix $(OBJSDIR),$(SRCS:.c=.o)) OBJS = $(addprefix $(OBJSDIR),$(SRCS:.c=.o))
FLAGS = -Werror -Wall -Wextra -g FLAGS = -Werror -Wall -Wextra -g
LIB = io/get_next_line/get_next_line.a
$(NAME): $(OBJS) $(NAME): $(OBJS)
@make -s -C io/get_next_line/ @ar -rcs $(NAME) $(OBJS)
@ar -rcs $(NAME) $(OBJS) $(LIB)
@echo "[✔] Libft compiled" @echo "[✔] Libft compiled"
$(OBJSDIR)%.o: %.c $(OBJSDIR)%.o: %.c
@ -89,11 +90,9 @@ $(OBJSDIR)%.o: %.c
all: $(NAME) all: $(NAME)
clean: clean:
@make -s -C io/get_next_line/ clean
@rm -f $(OBJS) @rm -f $(OBJS)
fclean: clean fclean: clean
@make -s -C io/get_next_line/ fclean
@rm -f $(NAME) @rm -f $(NAME)
@echo "[X] Libft cleaned" @echo "[X] Libft cleaned"

View File

@ -6,11 +6,11 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/01 17:11:59 by adjoly #+# #+# */ /* Created: 2023/12/01 17:11:59 by adjoly #+# #+# */
/* Updated: 2024/02/04 14:23:07 by adjoly ### ########.fr */ /* Updated: 2024/04/27 17:25:28 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "get_next_line.h" #include "../../libft.h"
char check_line(char *res, char *buf) char check_line(char *res, char *buf)
{ {

View File

@ -6,11 +6,11 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/01 17:12:02 by adjoly #+# #+# */ /* Created: 2023/12/01 17:12:02 by adjoly #+# #+# */
/* Updated: 2024/03/16 21:45:27 by adjoly ### ########.fr */ /* Updated: 2024/04/27 17:25:44 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "get_next_line.h" #include "../../libft.h"
char *ft_strjoin_gnl(char *s1, char *s2) char *ft_strjoin_gnl(char *s1, char *s2)
{ {

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/01 10:06:03 by adjoly #+# #+# */ /* Created: 2023/11/01 10:06:03 by adjoly #+# #+# */
/* Updated: 2024/03/16 21:45:08 by adjoly ### ########.fr */ /* Updated: 2024/04/27 17:27:45 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -16,13 +16,15 @@
# include <stddef.h> # include <stddef.h>
# include <stdlib.h> # include <stdlib.h>
# include <unistd.h> # include <unistd.h>
# include <stdarg.h>
# include "print/printf/ft_printf.h"
# include "io/get_next_line/get_next_line.h"
# define INT_MAX 2147483647 # define INT_MAX 2147483647
# define INT_MIN -2147483648 # define INT_MIN -2147483648
# ifndef BUFFER_SIZE
# define BUFFER_SIZE 5
# endif
typedef struct s_list typedef struct s_list
{ {
void *content; void *content;
@ -36,6 +38,7 @@ typedef enum s_boolean
TRUE, TRUE,
} t_boolean; } t_boolean;
char *ft_strjoinvaarg(char *src, ...);
long long ft_atoll(const char *nptr); long long ft_atoll(const char *nptr);
int ft_atoi(const char *nptr); int ft_atoi(const char *nptr);
void *ft_calloc(size_t nmemb, size_t size); void *ft_calloc(size_t nmemb, size_t size);
@ -84,6 +87,10 @@ void ft_lstclear(t_list **lst, void (*del)(void *));
void ft_lstiter(t_list *lst, void (*f)(void *)); void ft_lstiter(t_list *lst, void (*f)(void *));
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)); t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *));
char *get_next_line(int fd);
char *ft_strjoin_gnl(char *s1, char *s2);
void *ft_calloc(size_t nmemb, size_t size);
void ft_putchar(char c); void ft_putchar(char c);
void ft_putstr(char *s); void ft_putstr(char *s);
void ft_putnbrbase_fd(int n, char *base, int fd); void ft_putnbrbase_fd(int n, char *base, int fd);
@ -95,10 +102,12 @@ int ft_printconversion(char conversion, va_list args);
int ft_putnbrulong(unsigned int n); int ft_putnbrulong(unsigned int n);
int ft_putaddr(void *ptr); int ft_putaddr(void *ptr);
int ft_putstr_p(char *s); int ft_putstr_p(char *s);
int ft_putnbrbase_p(unsigned long int n, char *base); int ft_putnbrbasep(unsigned long int n, char *base);
int ft_putnbrbase_pf(unsigned int n, char *base); int ft_putnbrbase_pf(unsigned int n, char *base);
int ft_putchar_p(char c); int ft_putchar_p(char c);
int ft_putnbr_p(int n); int ft_putnbr_p(int n);
size_t ft_strlen(const char *s); size_t ft_strlen(const char *s);
void ft_freearr(void **arr);
#endif #endif

View File

@ -1,33 +1,26 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* ft_printf.h :+: :+: :+: */ /* ft_freetab.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/17 16:50:36 by adjoly #+# #+# */ /* Created: 2024/04/27 15:33:20 by adjoly #+# #+# */
/* Updated: 2024/03/16 21:47:28 by adjoly ### ########.fr */ /* Updated: 2024/04/27 17:20:24 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#ifndef FT_PRINTF_H #include <stdlib.h>
# define FT_PRINTF_H
# include <stdlib.h> void ft_freearr(void **arr)
# include <stdarg.h> {
# include <unistd.h> void **tmp;
int ft_printf(const char *format, ...); tmp = arr;
int ft_printconversion(char conversion, va_list args); while (*tmp)
int ft_putnbrulong(unsigned int n); {
int ft_putaddr(void *ptr); free(*tmp);
tmp++;
int ft_putstr_p(char *s); }
free(arr);
int ft_putnbrbase_p(unsigned long int n, char *base); }
int ft_pputnbrbase(unsigned int n, char *base);
int ft_putchar_p(char c);
int ft_putnbr_p(int n);
size_t ft_strlen(const char *s);
#endif

Binary file not shown.

View File

@ -6,11 +6,11 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/17 16:48:37 by adjoly #+# #+# */ /* Created: 2023/11/17 16:48:37 by adjoly #+# #+# */
/* Updated: 2024/03/16 21:46:36 by adjoly ### ########.fr */ /* Updated: 2024/04/27 17:27:13 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "ft_printf.h" #include "../../libft.h"
int ft_putaddr(void *ptr) int ft_putaddr(void *ptr)
{ {
@ -19,7 +19,7 @@ int ft_putaddr(void *ptr)
if (ptr == NULL) if (ptr == NULL)
return (write(1, "(nil)", 5)); return (write(1, "(nil)", 5));
write(1, "0x", 2); write(1, "0x", 2);
r = ft_putnbrbase_p((long unsigned int)ptr, "0123456789abcdef"); r = ft_putnbrbasep((long unsigned int)ptr, "0123456789abcdef");
return (2 + r); return (2 + r);
} }
@ -56,9 +56,9 @@ int ft_printconversion(char conversion, va_list args)
else if (conversion == 'p') else if (conversion == 'p')
count = ft_putaddr(va_arg(args, void *)); count = ft_putaddr(va_arg(args, void *));
else if (conversion == 'x') else if (conversion == 'x')
count = ft_pputnbrbase(va_arg(args, unsigned long), "0123456789abcdef"); count = ft_putnbrbasep(va_arg(args, unsigned long), "0123456789abcdef");
else if (conversion == 'X') else if (conversion == 'X')
count = ft_pputnbrbase(va_arg(args, unsigned long), "0123456789ABCDEF"); count = ft_putnbrbasep(va_arg(args, unsigned long), "0123456789ABCDEF");
return (count); return (count);
} }

View File

@ -6,11 +6,11 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/18 10:49:00 by adjoly #+# #+# */ /* Created: 2023/11/18 10:49:00 by adjoly #+# #+# */
/* Updated: 2024/02/04 15:17:42 by adjoly ### ########.fr */ /* Updated: 2024/04/27 17:22:30 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "ft_printf.h" #include <unistd.h>
int ft_putchar_p(char c) int ft_putchar_p(char c)
{ {

View File

@ -6,11 +6,11 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/31 11:52:46 by adjoly #+# #+# */ /* Created: 2023/10/31 11:52:46 by adjoly #+# #+# */
/* Updated: 2024/02/04 15:20:26 by adjoly ### ########.fr */ /* Updated: 2024/04/27 17:24:27 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "ft_printf.h" #include "../../libft.h"
int ft_putnbr_p(int n) int ft_putnbr_p(int n)
{ {

View File

@ -6,11 +6,11 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/18 10:57:44 by adjoly #+# #+# */ /* Created: 2023/11/18 10:57:44 by adjoly #+# #+# */
/* Updated: 2024/02/04 15:21:56 by adjoly ### ########.fr */ /* Updated: 2024/04/27 17:29:04 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "ft_printf.h" #include "../../libft.h"
int ft_putnbrbase_pf(unsigned int n, char *base) int ft_putnbrbase_pf(unsigned int n, char *base)
{ {
@ -29,7 +29,7 @@ int ft_putnbrbase_pf(unsigned int n, char *base)
return (len); return (len);
} }
int ft_putnbrbase_p(unsigned long int n, char *base) int ft_putnbrbasep(unsigned long int n, char *base)
{ {
unsigned long int base_len; unsigned long int base_len;
int len; int len;
@ -40,7 +40,7 @@ int ft_putnbrbase_p(unsigned long int n, char *base)
len += write(1, &base[n % base_len], 1); len += write(1, &base[n % base_len], 1);
else else
{ {
len += ft_putnbrbase_p(n / base_len, base); len += ft_putnbrbasep(n / base_len, base);
len += write(1, &base[n % base_len], 1); len += write(1, &base[n % base_len], 1);
} }
return (len); return (len);

View File

@ -6,11 +6,11 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/31 11:45:55 by adjoly #+# #+# */ /* Created: 2023/10/31 11:45:55 by adjoly #+# #+# */
/* Updated: 2024/03/12 15:05:16 by adjoly ### ########.fr */ /* Updated: 2024/04/27 17:24:56 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "ft_printf.h" #include "../../libft.h"
int ft_putstr_p(char *s) int ft_putstr_p(char *s)
{ {

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/12 09:14:19 by adjoly #+# #+# */ /* Created: 2023/11/12 09:14:19 by adjoly #+# #+# */
/* Updated: 2024/02/04 14:43:50 by adjoly ### ########.fr */ /* Updated: 2024/04/27 17:21:29 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -38,7 +38,7 @@ static int ft_countletter(char const *s, char sep)
return (i); return (i);
} }
static void *ft_freearr(char **arr) static void *ft_freearr_s(char **arr)
{ {
int i; int i;
@ -67,7 +67,7 @@ char **ft_split_to_result(char **tab, char const *s, char c)
k = 0; k = 0;
tab[j] = ft_calloc(ft_countletter(&s[i], c) + 1, sizeof(char)); tab[j] = ft_calloc(ft_countletter(&s[i], c) + 1, sizeof(char));
if (!tab[j]) if (!tab[j])
return (ft_freearr(tab)); return (ft_freearr_s(tab));
while (s[i] && s[i] != c) while (s[i] && s[i] != c)
tab[j][k++] = s [i++]; tab[j][k++] = s [i++];
tab[j][k] = '\0'; tab[j][k] = '\0';

View File

@ -6,23 +6,47 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/24 11:18:04 by adjoly #+# #+# */ /* Created: 2024/04/24 11:18:04 by adjoly #+# #+# */
/* Updated: 2024/04/25 11:30:17 by adjoly ### ########.fr */ /* Updated: 2024/04/27 17:34:52 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include <readline/readline.h> #include <readline/readline.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include "libft.h" #include "libft.h"
t_boolean is_str(char *src, char *dst) t_boolean is_str(char *src, char *dst)
{ {
while (*src++ == *dst++) while (*src && *dst && *src == *dst)
; {
src++;
dst++;
}
if (*src) if (*src)
return (FALSE); return (FALSE);
return (TRUE); return (TRUE);
} }
void get_prompt(void)
{
char *pwd;
char *home;
char *user;
char *host;
pwd = getenv("PWD");
home = getenv("HOME");
host = getenv("HOST");
user = getenv("USER");
if (!ft_strncmp(pwd, home, ft_strlen(home)))
{
ft_printf("%s@%s:~%s> ", user, host, pwd + ft_strlen(home));
return ;
}
ft_printf("%s@%s:%s> ", user, host, pwd);
}
int main(int ac, char **av, char **env) int main(int ac, char **av, char **env)
{ {
char *test; char *test;
@ -33,16 +57,14 @@ int main(int ac, char **av, char **env)
(void)env; (void)env;
while (1) while (1)
{ {
test = readline("test>"); get_prompt();
test = readline(NULL);
lll = ft_split(test, ' '); lll = ft_split(test, ' ');
if (is_str(*lll, "exit") == TRUE) if (!*lll)
return (0); continue;
if (is_str(*lll, "uwu") == TRUE) if (is_str(test, "exit"))
{ break;
printf("go burn in hell\n");
return (0);
}
printf("%s\n", test);
} }
ft_freearr((void **)lll);
return (0); return (0);
} }