From 39d80124439e6c164b5b0a758effe6df5a94b20e Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Mon, 24 Aug 2020 20:57:09 +0200 Subject: sqb in progress --- Makefile | 1 + src/b_builtins.h | 1 + src/b_sqb.c | 33 +++++++++++++++++++++++++++++++++ src/b_sqb.h | 22 ++++++++++++++++++++++ src/m_funptr.c | 17 +++++++++-------- 5 files changed, 66 insertions(+), 8 deletions(-) create mode 100644 src/b_sqb.c create mode 100644 src/b_sqb.h diff --git a/Makefile b/Makefile index 29f8d96..a5777fa 100644 --- a/Makefile +++ b/Makefile @@ -23,6 +23,7 @@ SRCS_NAME += b_export SRCS_NAME += b_export_next SRCS_NAME += b_export_mute SRCS_NAME += b_pwd +SRCS_NAME += b_sqb SRCS_NAME += b_type SRCS_NAME += b_unset SRCS_NAME += e_builtins diff --git a/src/b_builtins.h b/src/b_builtins.h index f7e7e99..0cb430a 100644 --- a/src/b_builtins.h +++ b/src/b_builtins.h @@ -21,5 +21,6 @@ #include "b_pwd.h" #include "b_type.h" #include "b_unset.h" +#include "b_sqb.h" #endif diff --git a/src/b_sqb.c b/src/b_sqb.c new file mode 100644 index 0000000..312fd90 --- /dev/null +++ b/src/b_sqb.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* b_sqb.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include + +#include "s_struct.h" +#include "u_utils.h" + +uint8_t + b_sqb(char *args[], + t_msh *msh) +{ + const uint64_t argc = u_builtins_get_argc((const char**)args); + + (void)msh; + if (ft_strncmp(args[argc - 1], "]", 2) != 0) + { + ft_dprintf(STDERR_FILENO, "[: missing ]\n"); + return (2); + } + return (0); +} diff --git a/src/b_sqb.h b/src/b_sqb.h new file mode 100644 index 0000000..c0cfcb8 --- /dev/null +++ b/src/b_sqb.h @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* b_sqb.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef B_SQB_H +#define B_SQB_H + +#include + +#include "s_struct.h" + +uint8_t b_sqb(char *args[], t_msh *msh); + +#endif diff --git a/src/m_funptr.c b/src/m_funptr.c index 2fdf4d1..a6b31f2 100644 --- a/src/m_funptr.c +++ b/src/m_funptr.c @@ -22,14 +22,15 @@ void init_buptr(t_msh *msh) { - msh->bu_ptr[0] = b_echo; - msh->bu_ptr[1] = b_cd; - msh->bu_ptr[2] = b_pwd; - msh->bu_ptr[3] = b_export; - msh->bu_ptr[4] = b_unset; - msh->bu_ptr[5] = b_env; - msh->bu_ptr[6] = b_exit; - msh->bu_ptr[7] = b_type; + msh->bu_ptr[FT_ID_ECHO] = b_echo; + msh->bu_ptr[FT_ID_CD] = b_cd; + msh->bu_ptr[FT_ID_PWD] = b_pwd; + msh->bu_ptr[FT_ID_EXPORT] = b_export; + msh->bu_ptr[FT_ID_UNSET] = b_unset; + msh->bu_ptr[FT_ID_ENV] = b_env; + msh->bu_ptr[FT_ID_EXIT] = b_exit; + msh->bu_ptr[FT_ID_TYPE] = b_type; + msh->bu_ptr[FT_ID_SQB] = b_sqb; if (!(msh->bu_ref = ft_split(FT_BUILTINS, '|'))) { f_alloc_and_destroy_msh(msh); -- cgit v1.2.3 From d05cc52ae73d3daf54b71d030e7bce659b159f7d Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Tue, 25 Aug 2020 21:46:45 +0200 Subject: New todo, in progress --- TODO.org | 1 + src/b_sqb.c | 23 +++++++++++++++++++-- src/d_define.h | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/e_builtins.c | 2 +- 4 files changed, 85 insertions(+), 3 deletions(-) diff --git a/TODO.org b/TODO.org index e466f7a..c125993 100644 --- a/TODO.org +++ b/TODO.org @@ -15,6 +15,7 @@ ** DONE [#C] leaks on builtins (same as above man) ** TODO [#C] Handle memory ** TODO [#C] Go full stack to ken dash in the ass +** TODO [#C] Handle comments better # * Stuff to add ** DONE [#A] builtins to pipes diff --git a/src/b_sqb.c b/src/b_sqb.c index 312fd90..9e7015c 100644 --- a/src/b_sqb.c +++ b/src/b_sqb.c @@ -17,17 +17,36 @@ #include "s_struct.h" #include "u_utils.h" +/* static uint8_t */ +/* b_get_sqb_id(uint64_t argc, */ +/* char *argv[],) */ +/* { */ +/* } */ + +static uint8_t + b_eval_sqb(uint64_t argc, + char *argv[], + t_msh *msh) +{ + (void)argc; + (void)argv; + (void)msh; + + return (0); +} + uint8_t b_sqb(char *args[], t_msh *msh) { const uint64_t argc = u_builtins_get_argc((const char**)args); - (void)msh; if (ft_strncmp(args[argc - 1], "]", 2) != 0) { ft_dprintf(STDERR_FILENO, "[: missing ]\n"); return (2); } - return (0); + if (argc == 1) + return (1); + return (b_eval_sqb(argc, args, msh)); } diff --git a/src/d_define.h b/src/d_define.h index 9042037..e094df1 100644 --- a/src/d_define.h +++ b/src/d_define.h @@ -68,4 +68,66 @@ #define FT_MINISHELLRC ".minishellrc" #define FT_DEFAULT_HISTFILE ".minishell_history" +/* +** ====== SQB OPTIONS ====== +*/ + +#define FT_SQB_B "-b" +#define FT_SQB_C "-c" +#define FT_SQB_D "-d" +#define FT_SQB_E "-e" +#define FT_SQB_F "-f" +#define FT_SQB_G "-g" +#define FT_SQB_H "-h" +#define FT_SQB_K "-k" +#define FT_SQB_N "-n" +#define FT_SQB_S "-s" +#define FT_SQB_U "-u" +#define FT_SQB_W "-w" +#define FT_SQB_X "-x" +#define FT_SQB_Z "-z" +#define FT_SQB_L_MAJ "-L" +#define FT_SQB_S_MAJ "-b" +#define FT_SQB_NT "-nt" +#define FT_SQB_OT "-ot" +#define FT_SQB_EQUALS "=" +#define FT_SQB_DIFF "!=" +#define FT_SQB_EQ "-eq" +#define FT_SQB_NE "-ne" +#define FT_SQB_GT "-gt" +#define FT_SQB_GE "-ge" +#define FT_SQB_LT "-lt" +#define FT_SQB_LE "-le" + +enum +{ + FT_ID_SQB_B, + FT_ID_SQB_C, + FT_ID_SQB_D, + FT_ID_SQB_E, + FT_ID_SQB_F, + FT_ID_SQB_G, + FT_ID_SQB_H, + FT_ID_SQB_K, + FT_ID_SQB_N, + FT_ID_SQB_S, + FT_ID_SQB_U, + FT_ID_SQB_W, + FT_ID_SQB_X, + FT_ID_SQB_Z, + FT_ID_SQB_L_MAJ, + FT_ID_SQB_S_MAJ, + FT_ID_SQB_NT, + FT_ID_SQB_OT, + FT_ID_SQB_EQUALS, + FT_ID_SQB_DIFF, + FT_ID_SQB_EQ, + FT_ID_SQB_NE, + FT_ID_SQB_GT, + FT_ID_SQB_GE, + FT_ID_SQB_LT, + FT_ID_SQB_LE, + FT_ID_SQB_COUNT +}; + #endif diff --git a/src/e_builtins.c b/src/e_builtins.c index 18abc16..b94c328 100644 --- a/src/e_builtins.c +++ b/src/e_builtins.c @@ -75,7 +75,7 @@ static void b_export_mute(ptr->argv + 1, msh); else if (bu_id == FT_ID_UNSET) msh->bu_ptr[bu_id](ptr->argv + 1, msh); - else if (bu_id == FT_ID_EXIT && ret == 0) + else if (bu_id == FT_ID_EXIT) { s_line_clear(&msh->curr); s_destroy(msh); -- cgit v1.2.3 From b86d639bb28510fef4398207f67b68da3acc3a2a Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Tue, 25 Aug 2020 21:48:38 +0200 Subject: TODO update --- TODO.org | 1 + 1 file changed, 1 insertion(+) diff --git a/TODO.org b/TODO.org index c125993..cfeb9c2 100644 --- a/TODO.org +++ b/TODO.org @@ -7,6 +7,7 @@ ** DONE [#A] fix pwd when cd / ** DONE [#A] Multiline pipes (ls |) ** TODO [#A] Go full tok + quotes +** TODO [#A] ls qweqwe; echo $? <---- reparse ** DONE [#B] forked write(2) stuff on cd ** TODO [#B] Multiline && || ** TODO [#B] ./qwe.sh <=== handle bad shebang -- cgit v1.2.3 From 441295b57b9892267841840d8d7166b9306f8501 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Tue, 25 Aug 2020 23:25:07 +0200 Subject: In progress --- src/b_sqb.c | 50 ++++++++++++++++++++++++++++++++++++++++++-------- src/d_define.h | 4 ++-- src/s_init.c | 42 ++++++++++++++++++++++++++++++++++++++++++ src/s_struct.h | 1 + 4 files changed, 87 insertions(+), 10 deletions(-) diff --git a/src/b_sqb.c b/src/b_sqb.c index 9e7015c..91bf7b6 100644 --- a/src/b_sqb.c +++ b/src/b_sqb.c @@ -14,24 +14,56 @@ #include #include +#include "d_define.h" #include "s_struct.h" #include "u_utils.h" -/* static uint8_t */ -/* b_get_sqb_id(uint64_t argc, */ -/* char *argv[],) */ -/* { */ -/* } */ +static void + b_sqb_unexpected_operator(const char op[]) +{ + ft_dprintf(STDERR_FILENO, "[: %s: unexpected operator\n", op); +} + +static uint8_t + b_get_sqb_id(uint64_t argc, + char *argv[], + t_msh *msh) +{ + size_t j; + uint8_t i; + + i = 0; + while (i < FT_ID_SQB_COUNT) + { + j = 0; + while (j < argc) + { + if ( + ft_strncmp(argv[j], + msh->sqb_ref[i], + ft_strlen(msh->sqb_ref[i]) + 1) == 0) + return (i); + j++; + } + i++; + } + return (i); +} static uint8_t b_eval_sqb(uint64_t argc, char *argv[], t_msh *msh) { - (void)argc; - (void)argv; - (void)msh; + uint8_t id; + id = b_get_sqb_id(argc, argv, msh); + if (id == FT_ID_SQB_COUNT) + { + b_sqb_unexpected_operator(argv[0]); + return (2); + } + ft_printf("[%s]\n", msh->sqb_ref[id]); return (0); } @@ -48,5 +80,7 @@ uint8_t } if (argc == 1) return (1); + else if (argc == 2) + return (0); return (b_eval_sqb(argc, args, msh)); } diff --git a/src/d_define.h b/src/d_define.h index e094df1..18a60b4 100644 --- a/src/d_define.h +++ b/src/d_define.h @@ -90,7 +90,7 @@ #define FT_SQB_S_MAJ "-b" #define FT_SQB_NT "-nt" #define FT_SQB_OT "-ot" -#define FT_SQB_EQUALS "=" +#define FT_SQB_EQUA "=" #define FT_SQB_DIFF "!=" #define FT_SQB_EQ "-eq" #define FT_SQB_NE "-ne" @@ -119,7 +119,7 @@ enum FT_ID_SQB_S_MAJ, FT_ID_SQB_NT, FT_ID_SQB_OT, - FT_ID_SQB_EQUALS, + FT_ID_SQB_EQUA, FT_ID_SQB_DIFF, FT_ID_SQB_EQ, FT_ID_SQB_NE, diff --git a/src/s_init.c b/src/s_init.c index 7b36653..acef5d6 100644 --- a/src/s_init.c +++ b/src/s_init.c @@ -26,6 +26,46 @@ #include "u_vars.h" #include "u_vars_next.h" +static void + s_init_sqb_ref_one(t_msh *msh) +{ + ft_memcpy(msh->sqb_ref[FT_ID_SQB_B], FT_SQB_B, ft_strlen(FT_SQB_B) + 1); + ft_memcpy(msh->sqb_ref[FT_ID_SQB_C], FT_SQB_C, ft_strlen(FT_SQB_C) + 1); + ft_memcpy(msh->sqb_ref[FT_ID_SQB_D], FT_SQB_D, ft_strlen(FT_SQB_D) + 1); + ft_memcpy(msh->sqb_ref[FT_ID_SQB_E], FT_SQB_E, ft_strlen(FT_SQB_E) + 1); + ft_memcpy(msh->sqb_ref[FT_ID_SQB_F], FT_SQB_F, ft_strlen(FT_SQB_F) + 1); + ft_memcpy(msh->sqb_ref[FT_ID_SQB_G], FT_SQB_G, ft_strlen(FT_SQB_G) + 1); + ft_memcpy(msh->sqb_ref[FT_ID_SQB_H], FT_SQB_H, ft_strlen(FT_SQB_H) + 1); + ft_memcpy(msh->sqb_ref[FT_ID_SQB_K], FT_SQB_K, ft_strlen(FT_SQB_K) + 1); + ft_memcpy(msh->sqb_ref[FT_ID_SQB_N], FT_SQB_N, ft_strlen(FT_SQB_N) + 1); + ft_memcpy(msh->sqb_ref[FT_ID_SQB_S], FT_SQB_S, ft_strlen(FT_SQB_S) + 1); + ft_memcpy(msh->sqb_ref[FT_ID_SQB_U], FT_SQB_U, ft_strlen(FT_SQB_U) + 1); + ft_memcpy(msh->sqb_ref[FT_ID_SQB_W], FT_SQB_W, ft_strlen(FT_SQB_W) + 1); + ft_memcpy(msh->sqb_ref[FT_ID_SQB_X], FT_SQB_X, ft_strlen(FT_SQB_X) + 1); + ft_memcpy(msh->sqb_ref[FT_ID_SQB_Z], FT_SQB_Z, ft_strlen(FT_SQB_Z) + 1); + ft_memcpy(msh->sqb_ref[FT_ID_SQB_L_MAJ], + FT_SQB_L_MAJ, ft_strlen(FT_SQB_L_MAJ) + 1); + ft_memcpy(msh->sqb_ref[FT_ID_SQB_S_MAJ], + FT_SQB_S_MAJ, ft_strlen(FT_SQB_S_MAJ) + 1); + ft_memcpy(msh->sqb_ref[FT_ID_SQB_NT], FT_SQB_NT, ft_strlen(FT_SQB_NT) + 1); + ft_memcpy(msh->sqb_ref[FT_ID_SQB_OT], FT_SQB_OT, ft_strlen(FT_SQB_OT) + 1); + ft_memcpy(msh->sqb_ref[FT_ID_SQB_EQUA], + FT_SQB_EQUA, ft_strlen(FT_SQB_EQUA) + 1); + ft_memcpy(msh->sqb_ref[FT_ID_SQB_DIFF], + FT_SQB_DIFF, ft_strlen(FT_SQB_DIFF) + 1); +} + +static void + s_init_sqb_ref_two(t_msh *msh) +{ + ft_memcpy(msh->sqb_ref[FT_ID_SQB_EQ], FT_SQB_EQ, ft_strlen(FT_SQB_EQ) + 1); + ft_memcpy(msh->sqb_ref[FT_ID_SQB_NE], FT_SQB_NE, ft_strlen(FT_SQB_NE) + 1); + ft_memcpy(msh->sqb_ref[FT_ID_SQB_GT], FT_SQB_GT, ft_strlen(FT_SQB_GT) + 1); + ft_memcpy(msh->sqb_ref[FT_ID_SQB_GE], FT_SQB_GE, ft_strlen(FT_SQB_GE) + 1); + ft_memcpy(msh->sqb_ref[FT_ID_SQB_LT], FT_SQB_LT, ft_strlen(FT_SQB_LT) + 1); + ft_memcpy(msh->sqb_ref[FT_ID_SQB_LE], FT_SQB_LE, ft_strlen(FT_SQB_LE) + 1); +} + static char **dupenv_del(char **nenvp, uint64_t i) @@ -143,5 +183,7 @@ t_msh msh->cwd = set_cwd(msh); msh->env_fork_tmp[0][0] = '\0'; inc_shlvl(msh); + s_init_sqb_ref_one(msh); + s_init_sqb_ref_two(msh); return (msh); } diff --git a/src/s_struct.h b/src/s_struct.h index 15505d5..54d3af3 100644 --- a/src/s_struct.h +++ b/src/s_struct.h @@ -74,6 +74,7 @@ typedef struct s_msh char **bu_ref; char *cwd; char env_fork_tmp[128][1024]; + char sqb_ref[FT_ID_SQB_COUNT][4]; uint8_t (*bu_ptr[FT_BUILTINS_COUNT])(char **, struct s_msh*); struct s_line *curr; struct s_lvars *vars; -- cgit v1.2.3 From c8a5f03103ac70d6525cab05ac85312099336989 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Tue, 25 Aug 2020 23:33:27 +0200 Subject: qweqwe --- src/b_sqb.c | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/src/b_sqb.c b/src/b_sqb.c index 91bf7b6..28e0fa6 100644 --- a/src/b_sqb.c +++ b/src/b_sqb.c @@ -25,26 +25,24 @@ static void } static uint8_t - b_get_sqb_id(uint64_t argc, - char *argv[], + b_get_sqb_id(char *argv[], t_msh *msh) { - size_t j; uint8_t i; i = 0; while (i < FT_ID_SQB_COUNT) { - j = 0; - while (j < argc) - { - if ( - ft_strncmp(argv[j], + if (i < FT_ID_SQB_NT && + ft_strncmp(argv[0], msh->sqb_ref[i], ft_strlen(msh->sqb_ref[i]) + 1) == 0) - return (i); - j++; - } + return (i); + if (i >= FT_ID_SQB_NT && + ft_strncmp(argv[1], + msh->sqb_ref[i], + ft_strlen(msh->sqb_ref[i]) + 1) == 0) + return (i); i++; } return (i); @@ -57,13 +55,13 @@ static uint8_t { uint8_t id; - id = b_get_sqb_id(argc, argv, msh); + (void)argc; + id = b_get_sqb_id(argv, msh); if (id == FT_ID_SQB_COUNT) { b_sqb_unexpected_operator(argv[0]); return (2); } - ft_printf("[%s]\n", msh->sqb_ref[id]); return (0); } -- cgit v1.2.3 From 6199ee75ee2ccb87a57f31fe50301f0a2c9bb325 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Tue, 25 Aug 2020 23:46:32 +0200 Subject: In progress --- src/b_sqb.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/b_sqb.c b/src/b_sqb.c index 28e0fa6..8dcea35 100644 --- a/src/b_sqb.c +++ b/src/b_sqb.c @@ -62,6 +62,7 @@ static uint8_t b_sqb_unexpected_operator(argv[0]); return (2); } + /* TODO: check errors */ return (0); } -- cgit v1.2.3 From 602e21eeeb6ebff0602abe1d9bf77ba18b248b93 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Wed, 26 Aug 2020 00:37:46 +0200 Subject: New flags, in progress --- src/b_sqb.c | 2 ++ src/d_define.h | 6 ++++-- src/s_init.c | 1 - 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/b_sqb.c b/src/b_sqb.c index 8dcea35..d78e345 100644 --- a/src/b_sqb.c +++ b/src/b_sqb.c @@ -63,6 +63,8 @@ static uint8_t return (2); } /* TODO: check errors */ + if (id < ) + ft_printf("%s\n", msh->sqb_ref[id]); return (0); } diff --git a/src/d_define.h b/src/d_define.h index 18a60b4..b0840f7 100644 --- a/src/d_define.h +++ b/src/d_define.h @@ -77,10 +77,11 @@ #define FT_SQB_D "-d" #define FT_SQB_E "-e" #define FT_SQB_F "-f" -#define FT_SQB_G "-g" #define FT_SQB_H "-h" #define FT_SQB_K "-k" #define FT_SQB_N "-n" +#define FT_SQB_P "-p" +#define FT_SQB_R "-r" #define FT_SQB_S "-s" #define FT_SQB_U "-u" #define FT_SQB_W "-w" @@ -106,10 +107,11 @@ enum FT_ID_SQB_D, FT_ID_SQB_E, FT_ID_SQB_F, - FT_ID_SQB_G, FT_ID_SQB_H, FT_ID_SQB_K, FT_ID_SQB_N, + FT_ID_SQB_P, + FT_ID_SQB_R, FT_ID_SQB_S, FT_ID_SQB_U, FT_ID_SQB_W, diff --git a/src/s_init.c b/src/s_init.c index acef5d6..d42822c 100644 --- a/src/s_init.c +++ b/src/s_init.c @@ -34,7 +34,6 @@ static void ft_memcpy(msh->sqb_ref[FT_ID_SQB_D], FT_SQB_D, ft_strlen(FT_SQB_D) + 1); ft_memcpy(msh->sqb_ref[FT_ID_SQB_E], FT_SQB_E, ft_strlen(FT_SQB_E) + 1); ft_memcpy(msh->sqb_ref[FT_ID_SQB_F], FT_SQB_F, ft_strlen(FT_SQB_F) + 1); - ft_memcpy(msh->sqb_ref[FT_ID_SQB_G], FT_SQB_G, ft_strlen(FT_SQB_G) + 1); ft_memcpy(msh->sqb_ref[FT_ID_SQB_H], FT_SQB_H, ft_strlen(FT_SQB_H) + 1); ft_memcpy(msh->sqb_ref[FT_ID_SQB_K], FT_SQB_K, ft_strlen(FT_SQB_K) + 1); ft_memcpy(msh->sqb_ref[FT_ID_SQB_N], FT_SQB_N, ft_strlen(FT_SQB_N) + 1); -- cgit v1.2.3 From 0c70b27f97513563096c65f9d2b5202caeaf6d26 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Wed, 26 Aug 2020 00:52:38 +0200 Subject: Coolio --- src/b_sqb.c | 4 ++-- src/d_define.h | 4 ---- src/s_init.c | 4 ++-- 3 files changed, 4 insertions(+), 8 deletions(-) diff --git a/src/b_sqb.c b/src/b_sqb.c index d78e345..90e9b58 100644 --- a/src/b_sqb.c +++ b/src/b_sqb.c @@ -63,8 +63,8 @@ static uint8_t return (2); } /* TODO: check errors */ - if (id < ) - ft_printf("%s\n", msh->sqb_ref[id]); + if (id < FT_ID_SQB_NT && id != FT_ID_SQB_Z && id != FT_ID_SQB_N) + ft_printf("%s\n", msh->sqb_ref[id]); return (0); } diff --git a/src/d_define.h b/src/d_define.h index b0840f7..b5ae142 100644 --- a/src/d_define.h +++ b/src/d_define.h @@ -78,12 +78,10 @@ #define FT_SQB_E "-e" #define FT_SQB_F "-f" #define FT_SQB_H "-h" -#define FT_SQB_K "-k" #define FT_SQB_N "-n" #define FT_SQB_P "-p" #define FT_SQB_R "-r" #define FT_SQB_S "-s" -#define FT_SQB_U "-u" #define FT_SQB_W "-w" #define FT_SQB_X "-x" #define FT_SQB_Z "-z" @@ -108,12 +106,10 @@ enum FT_ID_SQB_E, FT_ID_SQB_F, FT_ID_SQB_H, - FT_ID_SQB_K, FT_ID_SQB_N, FT_ID_SQB_P, FT_ID_SQB_R, FT_ID_SQB_S, - FT_ID_SQB_U, FT_ID_SQB_W, FT_ID_SQB_X, FT_ID_SQB_Z, diff --git a/src/s_init.c b/src/s_init.c index d42822c..2722c4b 100644 --- a/src/s_init.c +++ b/src/s_init.c @@ -35,10 +35,10 @@ static void ft_memcpy(msh->sqb_ref[FT_ID_SQB_E], FT_SQB_E, ft_strlen(FT_SQB_E) + 1); ft_memcpy(msh->sqb_ref[FT_ID_SQB_F], FT_SQB_F, ft_strlen(FT_SQB_F) + 1); ft_memcpy(msh->sqb_ref[FT_ID_SQB_H], FT_SQB_H, ft_strlen(FT_SQB_H) + 1); - ft_memcpy(msh->sqb_ref[FT_ID_SQB_K], FT_SQB_K, ft_strlen(FT_SQB_K) + 1); ft_memcpy(msh->sqb_ref[FT_ID_SQB_N], FT_SQB_N, ft_strlen(FT_SQB_N) + 1); + ft_memcpy(msh->sqb_ref[FT_ID_SQB_R], FT_SQB_P, ft_strlen(FT_SQB_R) + 1); + ft_memcpy(msh->sqb_ref[FT_ID_SQB_P], FT_SQB_R, ft_strlen(FT_SQB_P) + 1); ft_memcpy(msh->sqb_ref[FT_ID_SQB_S], FT_SQB_S, ft_strlen(FT_SQB_S) + 1); - ft_memcpy(msh->sqb_ref[FT_ID_SQB_U], FT_SQB_U, ft_strlen(FT_SQB_U) + 1); ft_memcpy(msh->sqb_ref[FT_ID_SQB_W], FT_SQB_W, ft_strlen(FT_SQB_W) + 1); ft_memcpy(msh->sqb_ref[FT_ID_SQB_X], FT_SQB_X, ft_strlen(FT_SQB_X) + 1); ft_memcpy(msh->sqb_ref[FT_ID_SQB_Z], FT_SQB_Z, ft_strlen(FT_SQB_Z) + 1); -- cgit v1.2.3 From d85db55d30ce3ec53055206903757e1c318b6843 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Wed, 26 Aug 2020 01:26:34 +0200 Subject: Commit --- Makefile | 1 + src/b_sqb.c | 11 ++++++----- src/b_sqb_file.c | 39 +++++++++++++++++++++++++++++++++++++++ src/b_sqb_file.h | 20 ++++++++++++++++++++ 4 files changed, 66 insertions(+), 5 deletions(-) create mode 100644 src/b_sqb_file.c create mode 100644 src/b_sqb_file.h diff --git a/Makefile b/Makefile index a5777fa..2cb1b03 100644 --- a/Makefile +++ b/Makefile @@ -24,6 +24,7 @@ SRCS_NAME += b_export_next SRCS_NAME += b_export_mute SRCS_NAME += b_pwd SRCS_NAME += b_sqb +SRCS_NAME += b_sqb_file SRCS_NAME += b_type SRCS_NAME += b_unset SRCS_NAME += e_builtins diff --git a/src/b_sqb.c b/src/b_sqb.c index 90e9b58..438f692 100644 --- a/src/b_sqb.c +++ b/src/b_sqb.c @@ -14,6 +14,7 @@ #include #include +#include "b_sqb_file.h" #include "d_define.h" #include "s_struct.h" #include "u_utils.h" @@ -49,13 +50,12 @@ static uint8_t } static uint8_t - b_eval_sqb(uint64_t argc, - char *argv[], + b_eval_sqb(char *argv[], t_msh *msh) { uint8_t id; + uint8_t ret; - (void)argc; id = b_get_sqb_id(argv, msh); if (id == FT_ID_SQB_COUNT) { @@ -63,9 +63,10 @@ static uint8_t return (2); } /* TODO: check errors */ + ret = 0; if (id < FT_ID_SQB_NT && id != FT_ID_SQB_Z && id != FT_ID_SQB_N) - ft_printf("%s\n", msh->sqb_ref[id]); - return (0); + ret = b_sqb_file_tests(id, argv); + return (ret); } uint8_t diff --git a/src/b_sqb_file.c b/src/b_sqb_file.c new file mode 100644 index 0000000..d6d07e0 --- /dev/null +++ b/src/b_sqb_file.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* b_sqb_file.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include + +static uint8_t + b_file_tests_one(uint8_t id, + struct stat *st) +{ + if (id == FT_ID_SQB_B && S_st->st_mode) + return (0); +} + +uint8_t + b_sqb_file(uint8_t id, + char *argv[]) +{ + struct stat st; + int32_t fd; + uint8_t ret; + + ret = 0; + if ((fd = open(argv[1])) == -1) + return (1); + fstat(fd, &st); + ret = b_file_tests_one(id, &st); + close(fd); + return (ret); +} diff --git a/src/b_sqb_file.h b/src/b_sqb_file.h new file mode 100644 index 0000000..d048fd1 --- /dev/null +++ b/src/b_sqb_file.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* b_sqb_file.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef B_SQB_FILE_H +#define B_SQB_FILE_H + +#include + +uint8_t b_sqb_file(uint8_t id, char *argv[]); + +#endif -- cgit v1.2.3 From fd708f9e7895f4d094ec0004afa57e4a23bb5252 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Wed, 26 Aug 2020 01:56:47 +0200 Subject: Some tests already --- src/b_sqb.c | 2 +- src/b_sqb_file.c | 43 ++++++++++++++++++++++++++++++++----------- src/b_sqb_file.h | 2 +- 3 files changed, 34 insertions(+), 13 deletions(-) diff --git a/src/b_sqb.c b/src/b_sqb.c index 438f692..226d09f 100644 --- a/src/b_sqb.c +++ b/src/b_sqb.c @@ -84,5 +84,5 @@ uint8_t return (1); else if (argc == 2) return (0); - return (b_eval_sqb(argc, args, msh)); + return (b_eval_sqb(args, msh)); } diff --git a/src/b_sqb_file.c b/src/b_sqb_file.c index d6d07e0..b69f160 100644 --- a/src/b_sqb_file.c +++ b/src/b_sqb_file.c @@ -11,29 +11,50 @@ /* ************************************************************************** */ #include +#include +#include +#include #include -static uint8_t +#include "d_define.h" + +static t_bool b_file_tests_one(uint8_t id, struct stat *st) { - if (id == FT_ID_SQB_B && S_st->st_mode) - return (0); + if (id == FT_ID_SQB_B && S_ISBLK(st->st_mode) == 1) + return (TRUE); + else if (id == FT_ID_SQB_C && S_ISCHR(st->st_mode) == 1) + return (TRUE); + else if (id == FT_ID_SQB_D && S_ISDIR(st->st_mode) == 1) + return (TRUE); + else if (id == FT_ID_SQB_F && S_ISREG(st->st_mode) == 1) + return (TRUE); + else if ((id == FT_ID_SQB_H || id == FT_ID_SQB_L_MAJ) + && S_ISLNK(st->st_mode) == 1) + return (TRUE); + else if (id == FT_ID_SQB_P && S_ISFIFO(st->st_mode) == 1) + return (TRUE); + return (FALSE); } uint8_t - b_sqb_file(uint8_t id, - char *argv[]) + b_sqb_file_tests(uint8_t id, + char *argv[]) { struct stat st; - int32_t fd; - uint8_t ret; + int32_t fd; - ret = 0; - if ((fd = open(argv[1])) == -1) + if ((fd = open(argv[1], O_RDONLY)) == -1) return (1); + if (id == FT_ID_SQB_E) + { + close(fd); + return (0); + } fstat(fd, &st); - ret = b_file_tests_one(id, &st); close(fd); - return (ret); + if (b_file_tests_one(id, &st) == TRUE) + return (0); + return (1); } diff --git a/src/b_sqb_file.h b/src/b_sqb_file.h index d048fd1..0fb7e88 100644 --- a/src/b_sqb_file.h +++ b/src/b_sqb_file.h @@ -15,6 +15,6 @@ #include -uint8_t b_sqb_file(uint8_t id, char *argv[]); +uint8_t b_sqb_file_tests(uint8_t id, char *argv[]); #endif -- cgit v1.2.3 From 711138bc07284620979621340d97aecc00b9f520 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Wed, 26 Aug 2020 02:54:05 +0200 Subject: In progress, .gitignore update --- .gitignore | 1 + src/b_sqb_file.c | 14 +++++++++++--- src/s_init.c | 4 ++-- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 59710b5..64e192b 100644 --- a/.gitignore +++ b/.gitignore @@ -128,4 +128,5 @@ minishell_history vgcore* qwe qwe.sh +noread .clang_complete \ No newline at end of file diff --git a/src/b_sqb_file.c b/src/b_sqb_file.c index b69f160..c371d50 100644 --- a/src/b_sqb_file.c +++ b/src/b_sqb_file.c @@ -12,14 +12,15 @@ #include #include +#include #include #include -#include #include "d_define.h" static t_bool b_file_tests_one(uint8_t id, + const char path[], struct stat *st) { if (id == FT_ID_SQB_B && S_ISBLK(st->st_mode) == 1) @@ -35,6 +36,10 @@ static t_bool return (TRUE); else if (id == FT_ID_SQB_P && S_ISFIFO(st->st_mode) == 1) return (TRUE); + else if (id == FT_ID_SQB_R && access(path, R_OK) == 0) + return (TRUE); + else if (id == FT_ID_SQB_S && st->st_size != 0) + return (TRUE); return (FALSE); } @@ -52,9 +57,12 @@ uint8_t close(fd); return (0); } - fstat(fd, &st); close(fd); - if (b_file_tests_one(id, &st) == TRUE) + if (id == FT_ID_SQB_H || id == FT_ID_SQB_L_MAJ) + lstat(argv[1], &st); + else + stat(argv[1], &st); + if (b_file_tests_one(id, argv[1], &st) == TRUE) return (0); return (1); } diff --git a/src/s_init.c b/src/s_init.c index 2722c4b..7353ed8 100644 --- a/src/s_init.c +++ b/src/s_init.c @@ -36,8 +36,8 @@ static void ft_memcpy(msh->sqb_ref[FT_ID_SQB_F], FT_SQB_F, ft_strlen(FT_SQB_F) + 1); ft_memcpy(msh->sqb_ref[FT_ID_SQB_H], FT_SQB_H, ft_strlen(FT_SQB_H) + 1); ft_memcpy(msh->sqb_ref[FT_ID_SQB_N], FT_SQB_N, ft_strlen(FT_SQB_N) + 1); - ft_memcpy(msh->sqb_ref[FT_ID_SQB_R], FT_SQB_P, ft_strlen(FT_SQB_R) + 1); - ft_memcpy(msh->sqb_ref[FT_ID_SQB_P], FT_SQB_R, ft_strlen(FT_SQB_P) + 1); + ft_memcpy(msh->sqb_ref[FT_ID_SQB_P], FT_SQB_P, ft_strlen(FT_SQB_P) + 1); + ft_memcpy(msh->sqb_ref[FT_ID_SQB_R], FT_SQB_R, ft_strlen(FT_SQB_R) + 1); ft_memcpy(msh->sqb_ref[FT_ID_SQB_S], FT_SQB_S, ft_strlen(FT_SQB_S) + 1); ft_memcpy(msh->sqb_ref[FT_ID_SQB_W], FT_SQB_W, ft_strlen(FT_SQB_W) + 1); ft_memcpy(msh->sqb_ref[FT_ID_SQB_X], FT_SQB_X, ft_strlen(FT_SQB_X) + 1); -- cgit v1.2.3 From 9ec595d51a34d795903579644ca93c6b41786221 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Wed, 26 Aug 2020 03:08:33 +0200 Subject: In progress --- src/b_sqb_file.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/b_sqb_file.c b/src/b_sqb_file.c index c371d50..5b6576a 100644 --- a/src/b_sqb_file.c +++ b/src/b_sqb_file.c @@ -40,6 +40,10 @@ static t_bool return (TRUE); else if (id == FT_ID_SQB_S && st->st_size != 0) return (TRUE); + else if (id == FT_ID_SQB_W && access(path, W_OK) == 0) + return (TRUE); + else if (id == FT_ID_SQB_X && access(path, X_OK) == 0) + return (TRUE); return (FALSE); } -- cgit v1.2.3 From 8b3650de36265a93f2ad077e23c4b58b11a38cf4 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Wed, 26 Aug 2020 03:26:07 +0200 Subject: File tests, all good --- src/b_sqb_file.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/b_sqb_file.c b/src/b_sqb_file.c index 5b6576a..02ad601 100644 --- a/src/b_sqb_file.c +++ b/src/b_sqb_file.c @@ -44,6 +44,8 @@ static t_bool return (TRUE); else if (id == FT_ID_SQB_X && access(path, X_OK) == 0) return (TRUE); + else if (id == FT_ID_SQB_S_MAJ && S_ISSOCK(st->st_mode) == 0) + return (TRUE); return (FALSE); } -- cgit v1.2.3 From 0cdd4719ca55e1084c37c7dab192ceb4e277650c Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Wed, 26 Aug 2020 16:30:22 +0200 Subject: in progress --- src/b_sqb_file.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/b_sqb_file.c b/src/b_sqb_file.c index 02ad601..d876516 100644 --- a/src/b_sqb_file.c +++ b/src/b_sqb_file.c @@ -38,7 +38,7 @@ static t_bool return (TRUE); else if (id == FT_ID_SQB_R && access(path, R_OK) == 0) return (TRUE); - else if (id == FT_ID_SQB_S && st->st_size != 0) + else if (id == FT_ID_SQB_S && st->st_size > 0) return (TRUE); else if (id == FT_ID_SQB_W && access(path, W_OK) == 0) return (TRUE); @@ -56,14 +56,10 @@ uint8_t struct stat st; int32_t fd; - if ((fd = open(argv[1], O_RDONLY)) == -1) + if (access(argv[1], 0) == -1) return (1); if (id == FT_ID_SQB_E) - { - close(fd); return (0); - } - close(fd); if (id == FT_ID_SQB_H || id == FT_ID_SQB_L_MAJ) lstat(argv[1], &st); else -- cgit v1.2.3 From fbfbc7b731e90ec32200148a2e2f1281f17a633d Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Wed, 26 Aug 2020 16:51:23 +0200 Subject: -nt abd -ot are bav --- src/b_sqb.c | 2 ++ src/b_sqb_file.c | 62 +++++++++++++++++++++++++++++++++++++++++--------------- src/b_sqb_file.h | 1 + 3 files changed, 49 insertions(+), 16 deletions(-) diff --git a/src/b_sqb.c b/src/b_sqb.c index 226d09f..adfad0f 100644 --- a/src/b_sqb.c +++ b/src/b_sqb.c @@ -66,6 +66,8 @@ static uint8_t ret = 0; if (id < FT_ID_SQB_NT && id != FT_ID_SQB_Z && id != FT_ID_SQB_N) ret = b_sqb_file_tests(id, argv); + else if (id == FT_ID_SQB_NT || id == FT_ID_SQB_OT) + ret = b_sqb_double_file_tests(id, argv); return (ret); } diff --git a/src/b_sqb_file.c b/src/b_sqb_file.c index d876516..f0cd374 100644 --- a/src/b_sqb_file.c +++ b/src/b_sqb_file.c @@ -19,32 +19,32 @@ #include "d_define.h" static t_bool - b_file_tests_one(uint8_t id, - const char path[], - struct stat *st) + b_file_tests(uint8_t id, + const char path[], + struct stat sb) { - if (id == FT_ID_SQB_B && S_ISBLK(st->st_mode) == 1) + if (id == FT_ID_SQB_B && S_ISBLK(sb.st_mode) == 1) return (TRUE); - else if (id == FT_ID_SQB_C && S_ISCHR(st->st_mode) == 1) + else if (id == FT_ID_SQB_C && S_ISCHR(sb.st_mode) == 1) return (TRUE); - else if (id == FT_ID_SQB_D && S_ISDIR(st->st_mode) == 1) + else if (id == FT_ID_SQB_D && S_ISDIR(sb.st_mode) == 1) return (TRUE); - else if (id == FT_ID_SQB_F && S_ISREG(st->st_mode) == 1) + else if (id == FT_ID_SQB_F && S_ISREG(sb.st_mode) == 1) return (TRUE); else if ((id == FT_ID_SQB_H || id == FT_ID_SQB_L_MAJ) - && S_ISLNK(st->st_mode) == 1) + && S_ISLNK(sb.st_mode) == 1) return (TRUE); - else if (id == FT_ID_SQB_P && S_ISFIFO(st->st_mode) == 1) + else if (id == FT_ID_SQB_P && S_ISFIFO(sb.st_mode) == 1) return (TRUE); else if (id == FT_ID_SQB_R && access(path, R_OK) == 0) return (TRUE); - else if (id == FT_ID_SQB_S && st->st_size > 0) + else if (id == FT_ID_SQB_S && sb.st_size > 0) return (TRUE); else if (id == FT_ID_SQB_W && access(path, W_OK) == 0) return (TRUE); else if (id == FT_ID_SQB_X && access(path, X_OK) == 0) return (TRUE); - else if (id == FT_ID_SQB_S_MAJ && S_ISSOCK(st->st_mode) == 0) + else if (id == FT_ID_SQB_S_MAJ && S_ISSOCK(sb.st_mode) == 0) return (TRUE); return (FALSE); } @@ -53,18 +53,48 @@ uint8_t b_sqb_file_tests(uint8_t id, char *argv[]) { - struct stat st; - int32_t fd; + struct stat sb; if (access(argv[1], 0) == -1) return (1); if (id == FT_ID_SQB_E) return (0); if (id == FT_ID_SQB_H || id == FT_ID_SQB_L_MAJ) - lstat(argv[1], &st); + lstat(argv[1], &sb); else - stat(argv[1], &st); - if (b_file_tests_one(id, argv[1], &st) == TRUE) + stat(argv[1], &sb); + if (b_file_tests(id, argv[1], sb) == TRUE) + return (0); + return (1); +} + +static t_bool + b_sqb_file_comp_tests(uint8_t id, + struct stat sb_one, + struct stat sb_two) +{ + if (id == FT_ID_SQB_NT && + sb_one.st_mtim.tv_sec > sb_two.st_mtim.tv_sec) + return (TRUE); + else if (id == FT_ID_SQB_OT && + sb_one.st_mtim.tv_sec < sb_two.st_mtim.tv_sec) + return (TRUE); + return (FALSE); +} + +uint8_t + b_sqb_double_file_tests(uint8_t id, + char *argv[]) +{ + struct stat sb[2]; + + if (access(argv[0], 0) == -1) + return (1); + if (access(argv[2], 0) == -1) + return (1); + stat(argv[0], &sb[0]); + stat(argv[2], &sb[1]); + if (b_sqb_file_comp_tests(id, sb[0], sb[1]) == TRUE) return (0); return (1); } diff --git a/src/b_sqb_file.h b/src/b_sqb_file.h index 0fb7e88..4d9b410 100644 --- a/src/b_sqb_file.h +++ b/src/b_sqb_file.h @@ -16,5 +16,6 @@ #include uint8_t b_sqb_file_tests(uint8_t id, char *argv[]); +uint8_t b_sqb_double_file_tests(uint8_t id, char *argv[]); #endif -- cgit v1.2.3 From 50bcdd00048869327d576021ae85d7d10ec6f012 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Wed, 26 Aug 2020 16:59:10 +0200 Subject: New files --- Makefile | 1 + src/b_sqb.c | 3 +++ src/b_sqb_str.c | 29 +++++++++++++++++++++++++++++ src/b_sqb_str.h | 20 ++++++++++++++++++++ 4 files changed, 53 insertions(+) create mode 100644 src/b_sqb_str.c create mode 100644 src/b_sqb_str.h diff --git a/Makefile b/Makefile index 2cb1b03..95a5d4a 100644 --- a/Makefile +++ b/Makefile @@ -25,6 +25,7 @@ SRCS_NAME += b_export_mute SRCS_NAME += b_pwd SRCS_NAME += b_sqb SRCS_NAME += b_sqb_file +SRCS_NAME += b_sqb_str SRCS_NAME += b_type SRCS_NAME += b_unset SRCS_NAME += e_builtins diff --git a/src/b_sqb.c b/src/b_sqb.c index adfad0f..8ac1f16 100644 --- a/src/b_sqb.c +++ b/src/b_sqb.c @@ -15,6 +15,7 @@ #include #include "b_sqb_file.h" +#include "b_sqb_str.h" #include "d_define.h" #include "s_struct.h" #include "u_utils.h" @@ -68,6 +69,8 @@ static uint8_t ret = b_sqb_file_tests(id, argv); else if (id == FT_ID_SQB_NT || id == FT_ID_SQB_OT) ret = b_sqb_double_file_tests(id, argv); + else if (id == FT_ID_SQB_Z || id == FT_ID_SQB_EQUA || id == FT_ID_SQB_DIFF) + ret = b_sqb_str(id, argv); return (ret); } diff --git a/src/b_sqb_str.c b/src/b_sqb_str.c new file mode 100644 index 0000000..0b23d7b --- /dev/null +++ b/src/b_sqb_str.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* b_sqb_str.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include + +#include "d_define.h" + +uint8_t + b_sqb_str(uint8_t id, + char *argv[]) +{ + if (id == FT_ID_SQB_Z) + { + if (argv[1][0] != '\0') + return (0); + else + return (1); + } + return (1); +} diff --git a/src/b_sqb_str.h b/src/b_sqb_str.h new file mode 100644 index 0000000..9e27973 --- /dev/null +++ b/src/b_sqb_str.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* b_sqb_str.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef B_SQB_STR_H +#define B_SQB_STR_H + +#include + +uint8_t b_sqb_str(uint8_t id, char *argv[]); + +#endif -- cgit v1.2.3 From 76748544fde8c95125ff0b40643f8e2720fe9308 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Wed, 26 Aug 2020 17:09:43 +0200 Subject: Strings comp done --- src/b_sqb_str.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/b_sqb_str.c b/src/b_sqb_str.c index 0b23d7b..7c6a160 100644 --- a/src/b_sqb_str.c +++ b/src/b_sqb_str.c @@ -10,10 +10,25 @@ /* */ /* ************************************************************************** */ +#include #include #include "d_define.h" +static t_bool + b_sqb_str_comp(uint8_t id, + const char s_one[], + const char s_two[]) +{ + if (id == FT_ID_SQB_EQUA && + ft_strncmp(s_one, s_two, ft_strlen(s_one) + 1) == 0) + return (TRUE); + else if (id == FT_ID_SQB_EQUA && + ft_strncmp(s_one, s_two, ft_strlen(s_one) + 1) != 0) + return (TRUE); + return (FALSE); +} + uint8_t b_sqb_str(uint8_t id, char *argv[]) @@ -25,5 +40,7 @@ uint8_t else return (1); } + if (b_sqb_str_comp(id, argv[0], argv[2]) == TRUE) + return (0); return (1); } -- cgit v1.2.3 From 6450dd00b959c8b74cdb5c151588072e33bad91e Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Wed, 26 Aug 2020 17:16:32 +0200 Subject: New comparison strings stuff --- src/b_sqb.c | 3 ++- src/b_sqb_str.c | 9 ++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/b_sqb.c b/src/b_sqb.c index 8ac1f16..34d5c06 100644 --- a/src/b_sqb.c +++ b/src/b_sqb.c @@ -69,7 +69,8 @@ static uint8_t ret = b_sqb_file_tests(id, argv); else if (id == FT_ID_SQB_NT || id == FT_ID_SQB_OT) ret = b_sqb_double_file_tests(id, argv); - else if (id == FT_ID_SQB_Z || id == FT_ID_SQB_EQUA || id == FT_ID_SQB_DIFF) + else if (id == FT_ID_SQB_N || id == FT_ID_SQB_Z || + id == FT_ID_SQB_EQUA || id == FT_ID_SQB_DIFF) ret = b_sqb_str(id, argv); return (ret); } diff --git a/src/b_sqb_str.c b/src/b_sqb_str.c index 7c6a160..cc7e4ed 100644 --- a/src/b_sqb_str.c +++ b/src/b_sqb_str.c @@ -33,13 +33,20 @@ uint8_t b_sqb_str(uint8_t id, char *argv[]) { - if (id == FT_ID_SQB_Z) + if (id == FT_ID_SQB_N) { if (argv[1][0] != '\0') return (0); else return (1); } + else if (id == FT_ID_SQB_Z) + { + if (argv[1][0] == '\0') + return (0); + else + return (1); + } if (b_sqb_str_comp(id, argv[0], argv[2]) == TRUE) return (0); return (1); -- cgit v1.2.3 From 63d4df1fbb5ac13cd388a1a0069ab82a13f81069 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Wed, 26 Aug 2020 17:54:40 +0200 Subject: Numbers are goodio, now errcheck --- Makefile | 1 + src/b_sqb.c | 3 +++ src/b_sqb_file.c | 8 ++++---- src/b_sqb_nbr.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ src/b_sqb_nbr.h | 20 ++++++++++++++++++++ src/b_sqb_str.c | 8 ++++---- 6 files changed, 77 insertions(+), 8 deletions(-) create mode 100644 src/b_sqb_nbr.c create mode 100644 src/b_sqb_nbr.h diff --git a/Makefile b/Makefile index 95a5d4a..f0ed77d 100644 --- a/Makefile +++ b/Makefile @@ -25,6 +25,7 @@ SRCS_NAME += b_export_mute SRCS_NAME += b_pwd SRCS_NAME += b_sqb SRCS_NAME += b_sqb_file +SRCS_NAME += b_sqb_nbr SRCS_NAME += b_sqb_str SRCS_NAME += b_type SRCS_NAME += b_unset diff --git a/src/b_sqb.c b/src/b_sqb.c index 34d5c06..fb52e00 100644 --- a/src/b_sqb.c +++ b/src/b_sqb.c @@ -15,6 +15,7 @@ #include #include "b_sqb_file.h" +#include "b_sqb_nbr.h" #include "b_sqb_str.h" #include "d_define.h" #include "s_struct.h" @@ -72,6 +73,8 @@ static uint8_t else if (id == FT_ID_SQB_N || id == FT_ID_SQB_Z || id == FT_ID_SQB_EQUA || id == FT_ID_SQB_DIFF) ret = b_sqb_str(id, argv); + else if (id >= FT_ID_SQB_EQ) + ret = b_sqb_nbr(id, argv); return (ret); } diff --git a/src/b_sqb_file.c b/src/b_sqb_file.c index f0cd374..c3a568d 100644 --- a/src/b_sqb_file.c +++ b/src/b_sqb_file.c @@ -70,14 +70,14 @@ uint8_t static t_bool b_sqb_file_comp_tests(uint8_t id, - struct stat sb_one, - struct stat sb_two) + struct stat sb1, + struct stat sb2) { if (id == FT_ID_SQB_NT && - sb_one.st_mtim.tv_sec > sb_two.st_mtim.tv_sec) + sb1.st_mtim.tv_sec > sb2.st_mtim.tv_sec) return (TRUE); else if (id == FT_ID_SQB_OT && - sb_one.st_mtim.tv_sec < sb_two.st_mtim.tv_sec) + sb1.st_mtim.tv_sec < sb2.st_mtim.tv_sec) return (TRUE); return (FALSE); } diff --git a/src/b_sqb_nbr.c b/src/b_sqb_nbr.c new file mode 100644 index 0000000..6ea2456 --- /dev/null +++ b/src/b_sqb_nbr.c @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* b_sqb_nbr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include + +#include "d_define.h" + +static t_bool + b_sqb_nbr_tests(uint8_t id, + int32_t n1, + int32_t n2) +{ + if (id == FT_ID_SQB_EQ && n1 == n2) + return (TRUE); + else if (id == FT_ID_SQB_NE && n1 != n2) + return (TRUE); + else if (id == FT_ID_SQB_GT && n1 > n2) + return (TRUE); + else if (id == FT_ID_SQB_GE && n1 >= n2) + return (TRUE); + else if (id == FT_ID_SQB_LT && n1 < n2) + return (TRUE); + else if (id == FT_ID_SQB_LE && n1 <= n2) + return (TRUE); + return (FALSE); +} + +uint8_t + b_sqb_nbr(uint8_t id, + char *argv[]) +{ + if (b_sqb_nbr_tests(id, ft_atoi(argv[0]), ft_atoi(argv[2])) == TRUE) + return (0); + return (1); +} diff --git a/src/b_sqb_nbr.h b/src/b_sqb_nbr.h new file mode 100644 index 0000000..ecdf901 --- /dev/null +++ b/src/b_sqb_nbr.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* b_sqb_nbr.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef B_SQB_NBR_H +#define B_SQB_NBR_H + +#include + +uint8_t b_sqb_nbr(uint8_t id, char *argv[]); + +#endif diff --git a/src/b_sqb_str.c b/src/b_sqb_str.c index cc7e4ed..d3a1978 100644 --- a/src/b_sqb_str.c +++ b/src/b_sqb_str.c @@ -17,14 +17,14 @@ static t_bool b_sqb_str_comp(uint8_t id, - const char s_one[], - const char s_two[]) + const char s1[], + const char s2[]) { if (id == FT_ID_SQB_EQUA && - ft_strncmp(s_one, s_two, ft_strlen(s_one) + 1) == 0) + ft_strncmp(s1, s2, ft_strlen(s1) + 1) == 0) return (TRUE); else if (id == FT_ID_SQB_EQUA && - ft_strncmp(s_one, s_two, ft_strlen(s_one) + 1) != 0) + ft_strncmp(s1, s2, ft_strlen(s1) + 1) != 0) return (TRUE); return (FALSE); } -- cgit v1.2.3 From dcd2a41e545cbee2a4487e28f684d19f9aa79877 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Wed, 26 Aug 2020 18:17:02 +0200 Subject: Err check in progress --- Makefile | 1 + src/b_sqb.c | 12 +++----- src/b_sqb_err.c | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/b_sqb_err.h | 20 +++++++++++++ src/u_utils.h | 2 ++ 5 files changed, 116 insertions(+), 8 deletions(-) create mode 100644 src/b_sqb_err.c create mode 100644 src/b_sqb_err.h diff --git a/Makefile b/Makefile index f0ed77d..fd79f8f 100644 --- a/Makefile +++ b/Makefile @@ -24,6 +24,7 @@ SRCS_NAME += b_export_next SRCS_NAME += b_export_mute SRCS_NAME += b_pwd SRCS_NAME += b_sqb +SRCS_NAME += b_sqb_err SRCS_NAME += b_sqb_file SRCS_NAME += b_sqb_nbr SRCS_NAME += b_sqb_str diff --git a/src/b_sqb.c b/src/b_sqb.c index fb52e00..a5a0fd5 100644 --- a/src/b_sqb.c +++ b/src/b_sqb.c @@ -14,6 +14,7 @@ #include #include +#include "b_sqb_err.h" #include "b_sqb_file.h" #include "b_sqb_nbr.h" #include "b_sqb_str.h" @@ -21,12 +22,6 @@ #include "s_struct.h" #include "u_utils.h" -static void - b_sqb_unexpected_operator(const char op[]) -{ - ft_dprintf(STDERR_FILENO, "[: %s: unexpected operator\n", op); -} - static uint8_t b_get_sqb_id(char *argv[], t_msh *msh) @@ -61,10 +56,11 @@ static uint8_t id = b_get_sqb_id(argv, msh); if (id == FT_ID_SQB_COUNT) { - b_sqb_unexpected_operator(argv[0]); + ft_dprintf(STDERR_FILENO, "[: %s: unexpected operator\n", argv[0]); return (2); } - /* TODO: check errors */ + if (b_sqb_err_check(id, argv) != 0) + return (2); ret = 0; if (id < FT_ID_SQB_NT && id != FT_ID_SQB_Z && id != FT_ID_SQB_N) ret = b_sqb_file_tests(id, argv); diff --git a/src/b_sqb_err.c b/src/b_sqb_err.c new file mode 100644 index 0000000..1755082 --- /dev/null +++ b/src/b_sqb_err.c @@ -0,0 +1,89 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* b_sqb_err.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include + +#include "d_define.h" +#include "u_utils.h" + +static uint8_t + b_file_err(const uint64_t argc, + char *argv[]) +{ + if (argc > 3) + { + ft_dprintf(STDERR_FILENO, "[: %s: unexpected operator\n", argv[1]); + return (1); + } + return (0); +} + +static uint8_t + b_double_file_err(const uint64_t argc, + char *argv[]) +{ + if (argc > 4) + { + ft_dprintf(STDERR_FILENO, "[: %s: unexpected operator\n", argv[2]); + return (1); + } + else if (argc == 3) + { + ft_dprintf(STDERR_FILENO, "[: %s: argument expected\n", argv[1]); + return (1); + } + return (0); +} + +static uint8_t + b_str_err(const uint64_t argc, + char *argv[]) +{ + if (argc > 4) + { + ft_dprintf(STDERR_FILENO, "[: %s: unexpected operator\n", argv[2]); + return (1); + } + else if (argc == 3) + { + ft_dprintf(STDERR_FILENO, "[: %s: argument expected\n", argv[1]); + return (1); + } + return (0); +} + +uint8_t + b_sqb_err_check(uint8_t id, + char *argv[]) +{ + const uint64_t argc = u_builtins_get_argc((const char**)argv); + + if (id < FT_ID_SQB_NT && id != FT_ID_SQB_Z && id != FT_ID_SQB_N) + { + if (b_file_err(argc, argv) != 0) + return (1); + } + else if (id == FT_ID_SQB_NT || id == FT_ID_SQB_OT) + { + if (b_double_file_err(argc, argv) != 0) + return (1); + } + else if (id == FT_ID_SQB_N || id == FT_ID_SQB_Z || + id == FT_ID_SQB_EQUA || id == FT_ID_SQB_DIFF) + { + if (b_str_err(argc, argv) != 0) + return (1); + } + return (0); +} diff --git a/src/b_sqb_err.h b/src/b_sqb_err.h new file mode 100644 index 0000000..7c27646 --- /dev/null +++ b/src/b_sqb_err.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* b_sqb_err.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef B_SQB_ERR_H +#define B_SQB_ERR_H + +#include + +uint8_t b_sqb_err_check(uint8_t id, char *argv[]); + +#endif diff --git a/src/u_utils.h b/src/u_utils.h index 6362e14..9943f41 100644 --- a/src/u_utils.h +++ b/src/u_utils.h @@ -15,6 +15,8 @@ #include +#include "s_struct.h" + void u_eof_stdin(void); uint64_t u_builtins_get_argc(const char *args[]); char **u_get_env_var_names(t_msh *msh); -- cgit v1.2.3 From f7ed540c183e6a20f1956901ee6ebd908745ccb8 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Wed, 26 Aug 2020 18:26:44 +0200 Subject: Err check in progress --- src/b_sqb_err.c | 35 ++++++++++++----------------------- 1 file changed, 12 insertions(+), 23 deletions(-) diff --git a/src/b_sqb_err.c b/src/b_sqb_err.c index 1755082..eac6a8a 100644 --- a/src/b_sqb_err.c +++ b/src/b_sqb_err.c @@ -18,8 +18,8 @@ #include "u_utils.h" static uint8_t - b_file_err(const uint64_t argc, - char *argv[]) + b_single_op(const uint64_t argc, + char *argv[]) { if (argc > 3) { @@ -30,8 +30,8 @@ static uint8_t } static uint8_t - b_double_file_err(const uint64_t argc, - char *argv[]) + b_double_op(const uint64_t argc, + char *argv[]) { if (argc > 4) { @@ -47,19 +47,9 @@ static uint8_t } static uint8_t - b_str_err(const uint64_t argc, - char *argv[]) + b_bad_nbr(char *argv[]) { - if (argc > 4) - { - ft_dprintf(STDERR_FILENO, "[: %s: unexpected operator\n", argv[2]); - return (1); - } - else if (argc == 3) - { - ft_dprintf(STDERR_FILENO, "[: %s: argument expected\n", argv[1]); - return (1); - } + (void)argv; return (0); } @@ -69,20 +59,19 @@ uint8_t { const uint64_t argc = u_builtins_get_argc((const char**)argv); - if (id < FT_ID_SQB_NT && id != FT_ID_SQB_Z && id != FT_ID_SQB_N) + if (id < FT_ID_SQB_NT) { - if (b_file_err(argc, argv) != 0) + if (b_single_op(argc, argv) != 0) return (1); } - else if (id == FT_ID_SQB_NT || id == FT_ID_SQB_OT) + else if (id >= FT_ID_SQB_NT) { - if (b_double_file_err(argc, argv) != 0) + if (b_double_op(argc, argv) != 0) return (1); } - else if (id == FT_ID_SQB_N || id == FT_ID_SQB_Z || - id == FT_ID_SQB_EQUA || id == FT_ID_SQB_DIFF) + if (id >= FT_ID_SQB_EQ) { - if (b_str_err(argc, argv) != 0) + if (b_bad_nbr(argv) != 0) return (1); } return (0); -- cgit v1.2.3 From eb40600dc1e7e4c61547173953958d50bf6a89b3 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Wed, 26 Aug 2020 18:39:48 +0200 Subject: Err check done --- src/b_sqb_err.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 46 insertions(+), 11 deletions(-) diff --git a/src/b_sqb_err.c b/src/b_sqb_err.c index eac6a8a..6c68841 100644 --- a/src/b_sqb_err.c +++ b/src/b_sqb_err.c @@ -30,17 +30,24 @@ static uint8_t } static uint8_t - b_double_op(const uint64_t argc, + b_expect_arg(const uint64_t argc, char *argv[]) { - if (argc > 4) + if (argc == 3) { - ft_dprintf(STDERR_FILENO, "[: %s: unexpected operator\n", argv[2]); + ft_dprintf(STDERR_FILENO, "[: %s: argument expected\n", argv[1]); return (1); } - else if (argc == 3) + return (0); +} + +static uint8_t + b_double_op(const uint64_t argc, + char *argv[]) +{ + if (argc > 4) { - ft_dprintf(STDERR_FILENO, "[: %s: argument expected\n", argv[1]); + ft_dprintf(STDERR_FILENO, "[: %s: unexpected operator\n", argv[2]); return (1); } return (0); @@ -49,7 +56,30 @@ static uint8_t static uint8_t b_bad_nbr(char *argv[]) { - (void)argv; + char *ptr; + + if (ft_ischarset("+-0123456789", argv[0][0]) == FALSE || + ft_ischarset("+-0123456789", argv[2][0]) == FALSE) + return (1); + ptr = argv[0] + 1; + while (*ptr != '\0') + { + if (ft_isdigit(*ptr) == FALSE) + { + ft_dprintf(STDERR_FILENO, "[: %s: bad number\n", argv[0]); + return (1); + } + ptr++; + } + ptr = argv[2] + 1; + while (*ptr != '\0') + { + if (ft_isdigit(*ptr) == FALSE) + ft_dprintf(STDERR_FILENO, "[: %s: bad number\n", argv[2]); + if (ft_isdigit(*ptr) == FALSE) + return (1); + ptr++; + } return (0); } @@ -59,6 +89,16 @@ uint8_t { const uint64_t argc = u_builtins_get_argc((const char**)argv); + if (id >= FT_ID_SQB_NT) + { + if (b_expect_arg(argc, argv) != 0) + return (1); + } + if (id >= FT_ID_SQB_EQ) + { + if (b_bad_nbr(argv) != 0) + return (1); + } if (id < FT_ID_SQB_NT) { if (b_single_op(argc, argv) != 0) @@ -69,10 +109,5 @@ uint8_t if (b_double_op(argc, argv) != 0) return (1); } - if (id >= FT_ID_SQB_EQ) - { - if (b_bad_nbr(argv) != 0) - return (1); - } return (0); } -- cgit v1.2.3 From f0de09e90d31c54d056700075879161206f01068 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Wed, 26 Aug 2020 18:42:18 +0200 Subject: Huge fix --- src/d_define.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/d_define.h b/src/d_define.h index b5ae142..62cfe7c 100644 --- a/src/d_define.h +++ b/src/d_define.h @@ -86,7 +86,7 @@ #define FT_SQB_X "-x" #define FT_SQB_Z "-z" #define FT_SQB_L_MAJ "-L" -#define FT_SQB_S_MAJ "-b" +#define FT_SQB_S_MAJ "-S" #define FT_SQB_NT "-nt" #define FT_SQB_OT "-ot" #define FT_SQB_EQUA "=" -- cgit v1.2.3 From abd9a0c72d7ffb634afbbcf24161c8e4f953b5ca Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Wed, 26 Aug 2020 18:43:55 +0200 Subject: TODO update --- TODO.org | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TODO.org b/TODO.org index cfeb9c2..74ca649 100644 --- a/TODO.org +++ b/TODO.org @@ -27,7 +27,7 @@ ** DONE [#B] .minishellrc ** TODO [#B] .minishell_history ** TODO [#C] Norm -** TODO [#C] [ builtin <=== pretty cool yet simple stuff ( [ $a -eq 1 ] ) +** DONE [#C] [ builtin <=== pretty cool yet simple stuff ( [ $a -eq 1 ] ) ** TODO [#C] alias ** DONE [#C] Read scripts ** DONE [#C] PSX rice -- cgit v1.2.3 From 443dabeae0353e950300a82b8d4e818bfed18951 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Thu, 27 Aug 2020 14:10:02 +0200 Subject: TODO update --- TODO.org | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TODO.org b/TODO.org index 74ca649..20acc28 100644 --- a/TODO.org +++ b/TODO.org @@ -9,7 +9,7 @@ ** TODO [#A] Go full tok + quotes ** TODO [#A] ls qweqwe; echo $? <---- reparse ** DONE [#B] forked write(2) stuff on cd -** TODO [#B] Multiline && || +** DONE [#B] Multiline && || ** TODO [#B] ./qwe.sh <=== handle bad shebang ** DONE [#C] fix that goddamn exit(2) 25 leak ** TODO [#C] lpipes leaks on builtins (same as above man) -- cgit v1.2.3 From 8205539da9d3e1b47eed9cd556113157233900eb Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Thu, 27 Aug 2020 14:17:00 +0200 Subject: Norm and TODO update --- TODO.org | 1 + src/m_argv.c | 19 +++++++++++++------ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/TODO.org b/TODO.org index 20acc28..2c460f9 100644 --- a/TODO.org +++ b/TODO.org @@ -26,6 +26,7 @@ ** TODO [#B] $_ ** DONE [#B] .minishellrc ** TODO [#B] .minishell_history +** TODO [#B] SIGNAL ** TODO [#C] Norm ** DONE [#C] [ builtin <=== pretty cool yet simple stuff ( [ $a -eq 1 ] ) ** TODO [#C] alias diff --git a/src/m_argv.c b/src/m_argv.c index 46e2640..5b2bc96 100644 --- a/src/m_argv.c +++ b/src/m_argv.c @@ -23,13 +23,23 @@ #include "m_mshrc.h" #include "s_struct.h" +static void + m_read_script(char *const argv[], + t_msh *msh) +{ + int32_t fd; + + if ((fd = open(*(argv + 1), O_RDONLY)) == -1) + f_open_file(*(argv + 1), msh); + msh->ret = m_loop(fd, msh); + close(fd); +} + uint8_t m_argv(int argc, char *const argv[], t_msh *msh) { - int32_t fd; - if (argc == 1) { m_init_custom_vars(msh); @@ -48,10 +58,7 @@ uint8_t } else { - if ((fd = open(*(argv + 1), O_RDONLY)) < 0) - f_open_file(*(argv + 1), msh); - msh->ret = m_loop(fd, msh); - close(fd); + m_read_script(argv, msh); } return (msh->ret); } -- cgit v1.2.3 From c80b4e513eaa39e3a132b4bb47237e4673314eca Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Thu, 27 Aug 2020 15:15:21 +0200 Subject: Fuck this shit --- src/e_externs.c | 11 +++++------ src/f_exec.c | 6 +++--- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/e_externs.c b/src/e_externs.c index 9e2f8f4..4cc63e1 100644 --- a/src/e_externs.c +++ b/src/e_externs.c @@ -29,10 +29,9 @@ static void e_extern_child(const char *fullpath, - t_com *ptr, - t_msh *msh) + t_com *ptr, + t_msh *msh) { - dup_redirs(ptr, msh); if (execve(fullpath, ptr->argv, msh->envp) == -1) { f_exec(fullpath); @@ -59,7 +58,7 @@ static void } static void - exec_path(const char fullpath[], + e_exec_path(const char fullpath[], t_com *ptr, t_msh *msh) { @@ -95,7 +94,7 @@ void { if ((fullpath = ft_strdup(ptr->bin)) == NULL) return ; - exec_path(fullpath, ptr, msh); + e_exec_path(fullpath, ptr, msh); ft_memdel((void*)&fullpath); return ; } @@ -109,7 +108,7 @@ void } else { - exec_path(fullpath, ptr, msh); + e_exec_path(fullpath, ptr, msh); ft_memdel((void*)&fullpath); } } diff --git a/src/f_exec.c b/src/f_exec.c index 3587700..fccfdea 100644 --- a/src/f_exec.c +++ b/src/f_exec.c @@ -21,7 +21,7 @@ void f_exec(const char fullpath[]) { ft_dprintf(STDERR_FILENO, - "minishell: %s: %s\n", - fullpath, - strerror(errno)); + "minishell: %s: %s\n", + fullpath, + strerror(errno)); } -- cgit v1.2.3 From 5fb84af48e62c096c8979443cdca61af121727e7 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Thu, 27 Aug 2020 15:15:45 +0200 Subject: Fuck this --- TODO.org | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TODO.org b/TODO.org index 2c460f9..f43f9b0 100644 --- a/TODO.org +++ b/TODO.org @@ -10,7 +10,7 @@ ** TODO [#A] ls qweqwe; echo $? <---- reparse ** DONE [#B] forked write(2) stuff on cd ** DONE [#B] Multiline && || -** TODO [#B] ./qwe.sh <=== handle bad shebang +** DONE [#B] ./qwe.sh <=== handle bad shebang ** DONE [#C] fix that goddamn exit(2) 25 leak ** TODO [#C] lpipes leaks on builtins (same as above man) ** DONE [#C] leaks on builtins (same as above man) -- cgit v1.2.3 From 3a09e7ae69c3eb1fc3d1f8dfa25f7376ca066e86 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Thu, 27 Aug 2020 15:33:35 +0200 Subject: Freed leaks --- .gitignore | 2 +- src/e_builtins.c | 2 +- src/e_externs.c | 2 +- src/m_loop.c | 1 + src/s_struct.h | 21 +++++++++++---------- src/u_utils.c | 6 +++--- src/u_utils.h | 2 +- 7 files changed, 19 insertions(+), 17 deletions(-) diff --git a/.gitignore b/.gitignore index 64e192b..6513a01 100644 --- a/.gitignore +++ b/.gitignore @@ -127,6 +127,6 @@ minishell_history *.core vgcore* qwe -qwe.sh +*.sh noread .clang_complete \ No newline at end of file diff --git a/src/e_builtins.c b/src/e_builtins.c index b94c328..666cf11 100644 --- a/src/e_builtins.c +++ b/src/e_builtins.c @@ -49,7 +49,7 @@ static void e_export_env_fork(ptr, msh); dup_redirs(ptr, msh); ret = msh->bu_ptr[bu_id](ptr->argv + 1, msh); - u_eof_stdin(); + u_eof_fd(msh->fd); s_line_clear(&msh->curr); s_destroy(msh); exit(ret); diff --git a/src/e_externs.c b/src/e_externs.c index 4cc63e1..253d758 100644 --- a/src/e_externs.c +++ b/src/e_externs.c @@ -36,7 +36,7 @@ static void { f_exec(fullpath); ft_memdel((void*)&fullpath); - u_eof_stdin(); + u_eof_fd(msh->fd); s_line_clear(&msh->curr); s_destroy(msh); exit(errno); diff --git a/src/m_loop.c b/src/m_loop.c index e174f30..c25188e 100644 --- a/src/m_loop.c +++ b/src/m_loop.c @@ -36,6 +36,7 @@ uint8_t char *line; int8_t gnl; + msh->fd = fd; gnl = 1; while (gnl > 0) { diff --git a/src/s_struct.h b/src/s_struct.h index 54d3af3..3f9e84f 100644 --- a/src/s_struct.h +++ b/src/s_struct.h @@ -28,19 +28,19 @@ typedef struct s_lvars { + struct s_lvars *next; char *name; char *val; - struct s_lvars *next; } t_lvars; typedef struct s_com { - char *bin; char **argv; - int8_t redir; - int32_t rdrfd; - char *rdrpath; char **env_fork; + char *rdrpath; + char *bin; + int32_t rdrfd; + int8_t redir; } t_com; struct s_lpipes @@ -59,25 +59,26 @@ struct s_lpipes typedef struct s_line { - uint8_t nextif; struct s_com *com; struct s_lpipes *pipes; struct s_line *next; + uint8_t nextif; } t_line; typedef struct s_msh { + struct s_line *curr; + struct s_lvars *vars; char **envp; + char **bu_ref; char ps[4][1024]; - uint8_t ret; char *shname; - char **bu_ref; char *cwd; + int32_t fd; char env_fork_tmp[128][1024]; char sqb_ref[FT_ID_SQB_COUNT][4]; uint8_t (*bu_ptr[FT_BUILTINS_COUNT])(char **, struct s_msh*); - struct s_line *curr; - struct s_lvars *vars; + uint8_t ret; } t_msh; #endif diff --git a/src/u_utils.c b/src/u_utils.c index f50d861..ad44e82 100644 --- a/src/u_utils.c +++ b/src/u_utils.c @@ -19,12 +19,12 @@ #include "s_struct.h" void -u_eof_stdin(void) + u_eof_fd(int32_t fd) { char *line; - close(STDIN_FILENO); - get_next_line(STDIN_FILENO, &line); + close(fd); + get_next_line(fd, &line); ft_memdel((void*)&line); } diff --git a/src/u_utils.h b/src/u_utils.h index 9943f41..22b63ff 100644 --- a/src/u_utils.h +++ b/src/u_utils.h @@ -17,7 +17,7 @@ #include "s_struct.h" -void u_eof_stdin(void); +void u_eof_fd(int32_t fd); uint64_t u_builtins_get_argc(const char *args[]); char **u_get_env_var_names(t_msh *msh); -- cgit v1.2.3 From 93e157bb48e8139e3988b7e0d3deca80dafd51b1 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Thu, 27 Aug 2020 15:49:11 +0200 Subject: In progress --- src/e_externs_pipes.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/e_externs_pipes.c b/src/e_externs_pipes.c index 858e60f..320b31d 100644 --- a/src/e_externs_pipes.c +++ b/src/e_externs_pipes.c @@ -26,6 +26,7 @@ #include "s_line.h" #include "s_lpipes.h" #include "s_struct.h" +#include "u_utils.h" static uint8_t get_builtin_id(const char com[], @@ -55,8 +56,10 @@ static void { bu_id = get_builtin_id(ptr->bin, msh); ret = msh->bu_ptr[bu_id](ptr->argv + 1, msh); + u_eof_fd(msh->fd); s_line_clear(&msh->curr); s_destroy(msh); + ft_memdel((void*)&fullpath); exit(ret); } else @@ -82,13 +85,13 @@ static size_t static void exec_path(const char *fullpath[], - struct s_lpipes *head, - t_msh *msh) + struct s_lpipes *head, + t_msh *msh) { + int fd[256][2]; size_t pipes; size_t i; size_t j; - int fd[256][2]; int pid; int status; @@ -147,7 +150,7 @@ void head = ptr; rptr = ptr; pipes = e_get_pipes_count(head); - if (!(fullpath = (char **)malloc((pipes + 2) * sizeof(char *)))) + if ((fullpath = (char **)malloc((pipes + 2) * sizeof(char *))) == NULL) f_alloc_and_destroy_msh(msh); fullpath[pipes + 1] = NULL; i = 0; -- cgit v1.2.3 From 515c77bb410d6d2e10d8f11de3ee4986739757ee Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Thu, 27 Aug 2020 16:05:40 +0200 Subject: TODO update --- TODO.org | 1 + src/e_externs_pipes.c | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/TODO.org b/TODO.org index f43f9b0..5a247e5 100644 --- a/TODO.org +++ b/TODO.org @@ -8,6 +8,7 @@ ** DONE [#A] Multiline pipes (ls |) ** TODO [#A] Go full tok + quotes ** TODO [#A] ls qweqwe; echo $? <---- reparse +** TODO [#A] <>> ** DONE [#B] forked write(2) stuff on cd ** DONE [#B] Multiline && || ** DONE [#B] ./qwe.sh <=== handle bad shebang diff --git a/src/e_externs_pipes.c b/src/e_externs_pipes.c index 320b31d..6f24435 100644 --- a/src/e_externs_pipes.c +++ b/src/e_externs_pipes.c @@ -44,7 +44,7 @@ static uint8_t } static void - e_extern_child(const char *fullpath, + e_pipe_child(const char *fullpath, t_com *ptr, t_msh *msh) { @@ -119,7 +119,7 @@ static void close(fd[j][FT_READ_END]); j++; } - e_extern_child(fullpath[i], head->com, msh); + e_pipe_child(fullpath[i], head->com, msh); } head = head->next; i++; -- cgit v1.2.3 From abc749bfb38ce6c1cfb818bbc0da1cfbd0451bfe Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Thu, 27 Aug 2020 16:14:40 +0200 Subject: Freed pipe leaks on builtins --- TODO.org | 2 +- src/e_externs_pipes.c | 20 +++++++++++--------- src/e_line.c | 2 +- src/e_pipes.c | 2 +- src/p_lcom.c | 2 +- src/s_lpipes.c | 18 +++++++++--------- src/s_lpipes.h | 10 +++++----- 7 files changed, 29 insertions(+), 27 deletions(-) diff --git a/TODO.org b/TODO.org index 5a247e5..622bdc9 100644 --- a/TODO.org +++ b/TODO.org @@ -13,7 +13,7 @@ ** DONE [#B] Multiline && || ** DONE [#B] ./qwe.sh <=== handle bad shebang ** DONE [#C] fix that goddamn exit(2) 25 leak -** TODO [#C] lpipes leaks on builtins (same as above man) +** DONE [#C] lpipes leaks on builtins (same as above man) ** DONE [#C] leaks on builtins (same as above man) ** TODO [#C] Handle memory ** TODO [#C] Go full stack to ken dash in the ass diff --git a/src/e_externs_pipes.c b/src/e_externs_pipes.c index 6f24435..f6243e5 100644 --- a/src/e_externs_pipes.c +++ b/src/e_externs_pipes.c @@ -44,26 +44,28 @@ static uint8_t } static void - e_pipe_child(const char *fullpath, - t_com *ptr, - t_msh *msh) + e_pipe_child(char *fullpath[], + uint8_t pipe_id, + t_com *ptr, + t_msh *msh) { uint8_t bu_id; uint8_t ret; dup_redirs(ptr, msh); - if (ft_strncmp(fullpath, "builtin", 8) == 0) + if (ft_strncmp(fullpath[pipe_id], "builtin", 8) == 0) { bu_id = get_builtin_id(ptr->bin, msh); ret = msh->bu_ptr[bu_id](ptr->argv + 1, msh); u_eof_fd(msh->fd); + s_lpipes_clear(&msh->curr->pipes); s_line_clear(&msh->curr); s_destroy(msh); - ft_memdel((void*)&fullpath); + ft_delwords(fullpath); exit(ret); } else - execve(fullpath, ptr->argv, msh->envp); + execve(fullpath[pipe_id], ptr->argv, msh->envp); /* TODO: handle execve failed */ } @@ -84,7 +86,7 @@ static size_t } static void - exec_path(const char *fullpath[], + e_pipe_exec_path(char *fullpath[], struct s_lpipes *head, t_msh *msh) { @@ -119,7 +121,7 @@ static void close(fd[j][FT_READ_END]); j++; } - e_pipe_child(fullpath[i], head->com, msh); + e_pipe_child(fullpath, i, head->com, msh); } head = head->next; i++; @@ -177,6 +179,6 @@ void rptr = rptr->next; } i = 0; - exec_path((const char**)fullpath, head, msh); + e_pipe_exec_path(fullpath, head, msh); ft_delwords(fullpath); } diff --git a/src/e_line.c b/src/e_line.c index 6da7f77..f47170f 100644 --- a/src/e_line.c +++ b/src/e_line.c @@ -41,7 +41,7 @@ static void { if (ptr->pipes != NULL) { - lpipes_clear(&ptr->pipes); + s_lpipes_clear(&ptr->pipes); } } diff --git a/src/e_pipes.c b/src/e_pipes.c index 30fced2..6fdc149 100644 --- a/src/e_pipes.c +++ b/src/e_pipes.c @@ -44,5 +44,5 @@ void /* < FT_BUILTINS_COUNT) */ /* e_builtin(ptr->pipes->one, bu_id, msh); */ e_externs_pipes(ptr->pipes, msh); - lpipes_clear(&ptr->pipes); + s_lpipes_clear(&ptr->pipes); } diff --git a/src/p_lcom.c b/src/p_lcom.c index 13b4d11..bd5edcb 100644 --- a/src/p_lcom.c +++ b/src/p_lcom.c @@ -152,7 +152,7 @@ int8_t { if ((link = s_line_new(NULL, msh)) == NULL) return (-1); - if ((split_pipes(words[i], link, msh)) == NULL) + if ((s_split_pipes(words[i], link, msh)) == NULL) return (-1); next = TRUE; } diff --git a/src/s_lpipes.c b/src/s_lpipes.c index 9d10849..f6a6845 100644 --- a/src/s_lpipes.c +++ b/src/s_lpipes.c @@ -20,7 +20,7 @@ #include "s_struct.h" struct s_lpipes - *lpipes_last(struct s_lpipes *lpipes) + *s_lpipes_last(struct s_lpipes *lpipes) { while (lpipes->next != NULL) lpipes = lpipes->next; @@ -28,7 +28,7 @@ struct s_lpipes } void - lpipes_add_back(struct s_lpipes **alpipes, + s_lpipes_add_back(struct s_lpipes **alpipes, struct s_lpipes *new) { struct s_lpipes *tmp; @@ -37,13 +37,13 @@ void *alpipes = new; else { - tmp = lpipes_last(*alpipes); + tmp = s_lpipes_last(*alpipes); tmp->next = new; } } void - lpipes_clear(struct s_lpipes **lpipes) + s_lpipes_clear(struct s_lpipes **lpipes) { struct s_lpipes *tmp; struct s_lpipes *renext; @@ -62,12 +62,12 @@ void } struct s_lpipes - *lpipes_new(const char pipedword[], + *s_lpipes_new(const char pipedword[], t_msh *msh) { struct s_lpipes *link; - if (!(link = (struct s_lpipes*)malloc(sizeof(struct s_lpipes)))) + if ((link = (struct s_lpipes*)malloc(sizeof(struct s_lpipes))) == NULL) return (NULL); link->com = NULL; if ((link->com = s_com_new(pipedword, msh)) == NULL) @@ -79,7 +79,7 @@ struct s_lpipes } struct s_lpipes - *split_pipes(const char word[], + *s_split_pipes(const char word[], t_line *line, t_msh *msh) { @@ -92,11 +92,11 @@ struct s_lpipes i = 0; while (words[i]) { - if (!(lpipes = lpipes_new(words[i], msh))) + if (!(lpipes = s_lpipes_new(words[i], msh))) { return (NULL); } - lpipes_add_back(&line->pipes, lpipes); + s_lpipes_add_back(&line->pipes, lpipes); i++; } ft_delwords(words); diff --git a/src/s_lpipes.h b/src/s_lpipes.h index 7b90d2c..5b2795a 100644 --- a/src/s_lpipes.h +++ b/src/s_lpipes.h @@ -15,11 +15,11 @@ #include "s_struct.h" -struct s_lpipes *lpipes_last(struct s_lpipes *lpipes); -void lpipes_add_back(struct s_lpipes **alpipes, +struct s_lpipes *s_lpipes_last(struct s_lpipes *lpipes); +void s_lpipes_add_back(struct s_lpipes **alpipes, struct s_lpipes *new); -void lpipes_clear(struct s_lpipes **lpipes); -struct s_lpipes *lpipes_new(const char pipedword[], t_msh *msh); -struct s_lpipes *split_pipes(const char word[], t_line *line, t_msh *msh); +void s_lpipes_clear(struct s_lpipes **lpipes); +struct s_lpipes *s_lpipes_new(const char pipedword[], t_msh *msh); +struct s_lpipes *s_split_pipes(const char word[], t_line *line, t_msh *msh); #endif -- cgit v1.2.3 From 094468246db36a3a0e82d007e7fc92c5e3fec014 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Thu, 27 Aug 2020 16:22:04 +0200 Subject: TODO update --- TODO.org | 1 + 1 file changed, 1 insertion(+) diff --git a/TODO.org b/TODO.org index 622bdc9..94675dc 100644 --- a/TODO.org +++ b/TODO.org @@ -34,3 +34,4 @@ ** DONE [#C] Read scripts ** DONE [#C] PSX rice ** TODO [#C] Termcaps +** TODO [#C] Simple arithmetics $((a + 1)) -- cgit v1.2.3 From 8a1352d62c0dd66ecdd5807e83c16a00fb864635 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Thu, 27 Aug 2020 17:47:04 +0200 Subject: Histfile is BAV --- src/e_builtins.c | 3 +++ src/m_init.c | 12 +++++++++++- src/m_loop.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++--- src/m_loop.h | 1 + src/s_struct.h | 5 +++-- 5 files changed, 64 insertions(+), 6 deletions(-) diff --git a/src/e_builtins.c b/src/e_builtins.c index 666cf11..f4b363b 100644 --- a/src/e_builtins.c +++ b/src/e_builtins.c @@ -18,6 +18,7 @@ #include "b_export_next.h" #include "b_export_mute.h" +#include "m_loop.h" #include "m_redirs.h" #include "s_destroy.h" #include "s_line.h" @@ -77,6 +78,8 @@ static void msh->bu_ptr[bu_id](ptr->argv + 1, msh); else if (bu_id == FT_ID_EXIT) { + if (msh->fd == STDIN_FILENO) + m_dump_hist(msh); s_line_clear(&msh->curr); s_destroy(msh); exit(ret); diff --git a/src/m_init.c b/src/m_init.c index c145063..8b8a994 100644 --- a/src/m_init.c +++ b/src/m_init.c @@ -10,17 +10,27 @@ /* */ /* ************************************************************************** */ +#include #include #include "s_struct.h" +#include "u_vars.h" #include "u_vars_next.h" void m_init_custom_vars(t_msh *msh) { + char *home; + char fmt[255]; + u_subst_var_value("$PS1", FT_DEFAULT_PS_ONE, msh); u_subst_var_value("$PS2", FT_DEFAULT_PS_TWO, msh); u_subst_var_value("$PS3", FT_DEFAULT_PS_THR, msh); u_subst_var_value("$PS4", FT_DEFAULT_PS_FOU, msh); - u_subst_var_value("$HISTFILE", FT_DEFAULT_HISTFILE, msh); + if ((home = u_get_var_value("$HOME", msh)) != NULL) + { + ft_sprintf(fmt, "%s/%s", home, FT_DEFAULT_HISTFILE); + u_subst_var_value("$HISTFILE", fmt, msh); + ft_memdel((void*)&home); + } } diff --git a/src/m_loop.c b/src/m_loop.c index c25188e..a5e2723 100644 --- a/src/m_loop.c +++ b/src/m_loop.c @@ -13,6 +13,7 @@ #include #include #include +#include #include #include "e_line.h" @@ -20,9 +21,10 @@ #include "m_prompt.h" #include "p_line.h" #include "s_line.h" +#include "u_vars.h" static void - m_parse_and_run_line(char *line, t_msh *msh) + m_parse_and_run_line(char line[], t_msh *msh) { p_line(line, msh); ft_memdel((void*)&line); @@ -30,11 +32,48 @@ static void s_line_clear(&msh->curr); } +void + m_dump_hist(t_msh *msh) +{ + int32_t fd; + char *histfile_path; + + if (ft_strlen(msh->hist) > 0 && + (histfile_path = u_get_var_value("$HISTFILE", msh)) != NULL) + { + if ((fd = open(histfile_path, + O_WRONLY | O_CREAT | O_APPEND, 0644)) != -1) + { + write(fd, msh->hist, ft_strlen(msh->hist)); + close(fd); + } + ft_memdel((void*)&histfile_path); + } +} + +static void + m_handle_hist(char line[], t_msh *msh) +{ + static uint16_t hist_i = 0; + + if (hist_i == 0) + msh->hist[0] = '\0'; + ft_strlcpy(msh->hist + ft_strlen(msh->hist), line, 4096); + msh->hist[ft_strlen(msh->hist) + 1] = '\0'; + msh->hist[ft_strlen(msh->hist)] = '\n'; + hist_i += 1; + if (hist_i == 255) + { + m_dump_hist(msh); + hist_i = 0; + } +} + uint8_t m_loop(int32_t fd, t_msh *msh) { - char *line; - int8_t gnl; + char *line; + int8_t gnl; msh->fd = fd; gnl = 1; @@ -47,6 +86,8 @@ uint8_t { line = m_check_multi_backslash(fd, line, msh); line = m_check_multi_pipe(fd, line, msh); + if (fd == STDIN_FILENO) + m_handle_hist(line, msh); m_parse_and_run_line(line, msh); /* TODO: (null): Bad address on "msh ~> echo a > asd; cat < asd" but not on "msh ~> echo a > asd; cat asd" */ /* TODO: "msh ~> some command \": re GNL into ft_nrealloc */ @@ -57,5 +98,7 @@ uint8_t ft_memdel((void*)&line); } } + if (fd == STDIN_FILENO) + m_dump_hist(msh); return (msh->ret); } diff --git a/src/m_loop.h b/src/m_loop.h index 3f95a5e..1f60744 100644 --- a/src/m_loop.h +++ b/src/m_loop.h @@ -17,6 +17,7 @@ #include "s_struct.h" +void m_dump_hist(t_msh *msh); uint8_t m_loop(int32_t fd, t_msh *msh); #endif diff --git a/src/s_struct.h b/src/s_struct.h index 3f9e84f..01c8f1f 100644 --- a/src/s_struct.h +++ b/src/s_struct.h @@ -72,11 +72,12 @@ typedef struct s_msh char **envp; char **bu_ref; char ps[4][1024]; + char env_fork_tmp[128][1024]; + char sqb_ref[FT_ID_SQB_COUNT][4]; + char hist[1037595]; char *shname; char *cwd; int32_t fd; - char env_fork_tmp[128][1024]; - char sqb_ref[FT_ID_SQB_COUNT][4]; uint8_t (*bu_ptr[FT_BUILTINS_COUNT])(char **, struct s_msh*); uint8_t ret; } t_msh; -- cgit v1.2.3 From 6cea94e5dfe54b3a2ee0b86fb5eabec0e6ba6ab5 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Thu, 27 Aug 2020 17:47:28 +0200 Subject: Histfile TODOed --- TODO.org | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TODO.org b/TODO.org index 94675dc..b46e26e 100644 --- a/TODO.org +++ b/TODO.org @@ -26,7 +26,7 @@ ** TODO [#B] & ** TODO [#B] $_ ** DONE [#B] .minishellrc -** TODO [#B] .minishell_history +** DONE [#B] .minishell_history ** TODO [#B] SIGNAL ** TODO [#C] Norm ** DONE [#C] [ builtin <=== pretty cool yet simple stuff ( [ $a -eq 1 ] ) -- cgit v1.2.3 From 9e901f1070ac4a3b709c62472c8874b2ad2e120d Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Fri, 28 Aug 2020 14:25:49 +0200 Subject: Quick tabs fix --- src/p_line.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/p_line.c b/src/p_line.c index 8b13424..ec148e3 100644 --- a/src/p_line.c +++ b/src/p_line.c @@ -37,6 +37,22 @@ static void } } +static void + p_subst_spaces(char line[]) +{ + char *ptr; + + ptr = line; + while (*ptr != '\0') + { + if (ft_isspace(*ptr) == TRUE) + { + *ptr = ' '; + } + ptr++; + } +} + static t_bool p_check_whitespaces_only(char line[]) { @@ -45,7 +61,7 @@ static t_bool ptr = line; while (*ptr != '\0') { - if (*ptr != ' ' && *ptr != '\t') + if (ft_isspace(*ptr) == FALSE) { return (FALSE); } @@ -59,6 +75,7 @@ void t_msh *msh) { p_delete_comments(line); + p_subst_spaces(line); if (p_check_whitespaces_only(line) == TRUE) { return ; -- cgit v1.2.3 From 6f4b096ba1cff30c1c33dd887aa70740be7990c7 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Fri, 28 Aug 2020 14:46:35 +0200 Subject: Multiline added for && --- src/m_loop.c | 5 +---- src/m_loop_next.c | 29 ++++++++++++++++++++++++++--- src/m_loop_next.h | 5 +++-- 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/src/m_loop.c b/src/m_loop.c index a5e2723..1faa2cf 100644 --- a/src/m_loop.c +++ b/src/m_loop.c @@ -86,17 +86,14 @@ uint8_t { line = m_check_multi_backslash(fd, line, msh); line = m_check_multi_pipe(fd, line, msh); + line = m_check_multi_and(fd, line, msh); if (fd == STDIN_FILENO) m_handle_hist(line, msh); m_parse_and_run_line(line, msh); /* TODO: (null): Bad address on "msh ~> echo a > asd; cat < asd" but not on "msh ~> echo a > asd; cat asd" */ - /* TODO: "msh ~> some command \": re GNL into ft_nrealloc */ - /* TODO: a histfile would be nice */ } else - { ft_memdel((void*)&line); - } } if (fd == STDIN_FILENO) m_dump_hist(msh); diff --git a/src/m_loop_next.c b/src/m_loop_next.c index 9aa7129..1a8d867 100644 --- a/src/m_loop_next.c +++ b/src/m_loop_next.c @@ -60,7 +60,7 @@ static char } char - *m_check_multi_backslash(int32_t fd, char *line, t_msh *msh) + *m_check_multi_backslash(int32_t fd, char line[], t_msh *msh) { if (line[ft_strlen(line) - 1] == '\\') { @@ -71,13 +71,13 @@ char } char - *m_check_multi_pipe(int32_t fd, char *line, t_msh *msh) + *m_check_multi_pipe(int32_t fd, char line[], t_msh *msh) { char *pipe; if ((pipe = ft_strrchr(line, '|')) != NULL) { - pipe++; + pipe += 1; while (*pipe != '\0') { if (ft_isspace(*pipe) == FALSE) @@ -93,3 +93,26 @@ char return (line); } +char + *m_check_multi_and(int32_t fd, char line[], t_msh *msh) +{ + char *and; + + if ((and = ft_strrchr(line, '&')) != NULL && *(and - 1) == '&') + { + and += 1; + while (*and != '\0') + { + if (ft_isspace(*and) == FALSE) + { + return (line); + } + and++; + } + line = m_counter_line_pipes(fd, 2, line, msh); + line = m_check_multi_backslash(fd, line, msh); + line = m_check_multi_pipe(fd, line, msh); + line = m_check_multi_and(fd, line, msh); + } + return (line); +} diff --git a/src/m_loop_next.h b/src/m_loop_next.h index 56221e7..225e2ed 100644 --- a/src/m_loop_next.h +++ b/src/m_loop_next.h @@ -17,8 +17,9 @@ #include "s_struct.h" -char *m_check_multi_backslash(int32_t fd, char *line, t_msh *msh); -char *m_check_multi_pipe(int32_t fd, char *line, t_msh *msh); +char *m_check_multi_backslash(int32_t fd, char line[], t_msh *msh); +char *m_check_multi_pipe(int32_t fd, char line[], t_msh *msh); +char *m_check_multi_and(int32_t fd, char line[], t_msh *msh); #endif -- cgit v1.2.3 From fb2f240c6ffb1d10c0dd0d482322b7f08308e63e Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Tue, 1 Sep 2020 16:25:07 +0200 Subject: Secured malloc --- src/s_line.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/s_line.c b/src/s_line.c index b47b453..ae0ec07 100644 --- a/src/s_line.c +++ b/src/s_line.c @@ -80,7 +80,7 @@ t_line link->next = NULL; return (link); } - link->com = s_com_new(word, msh); - /* TODO: check link->com = NULL */ + if ((link->com = s_com_new(word, msh)) == NULL) + return (NULL); return (link); } -- cgit v1.2.3 From 06217e697520e5c97393bc06b89fda073efa7ea6 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Tue, 1 Sep 2020 16:40:05 +0200 Subject: Update --- Makefile | 1 + TODO.org | 2 +- src/p_args.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/p_args.h | 20 ++++++++++++++++++++ src/p_lcom_next.c | 47 +++++++++++++++++++++++++---------------------- src/s_com.c | 3 ++- 6 files changed, 101 insertions(+), 24 deletions(-) create mode 100644 src/p_args.c create mode 100644 src/p_args.h diff --git a/Makefile b/Makefile index fd79f8f..bf8bb0c 100644 --- a/Makefile +++ b/Makefile @@ -61,6 +61,7 @@ SRCS_NAME += s_init SRCS_NAME += s_line SRCS_NAME += s_lvars SRCS_NAME += s_lpipes +SRCS_NAME += p_args SRCS_NAME += p_line SRCS_NAME += p_lcom SRCS_NAME += p_lcom_next diff --git a/TODO.org b/TODO.org index b46e26e..5fa00b7 100644 --- a/TODO.org +++ b/TODO.org @@ -7,7 +7,7 @@ ** DONE [#A] fix pwd when cd / ** DONE [#A] Multiline pipes (ls |) ** TODO [#A] Go full tok + quotes -** TODO [#A] ls qweqwe; echo $? <---- reparse +** TODO [#A] ls qweqwe; echo $? <---- substitute ** TODO [#A] <>> ** DONE [#B] forked write(2) stuff on cd ** DONE [#B] Multiline && || diff --git a/src/p_args.c b/src/p_args.c new file mode 100644 index 0000000..6b285da --- /dev/null +++ b/src/p_args.c @@ -0,0 +1,52 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* p_args.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include + +char + **p_split_args(const char word[], int8_t redir) +{ + char **words; + + (void)word; + if (redir == 0) + { + return (words); + } + return (words); + /* char **words; */ + /* char *subst; */ + /* size_t i; */ + + /* if (redir == 0) */ + /* { */ + /* if (!(words = ft_split(word, ' '))) */ + /* return (NULL); */ + /* return (words); */ + /* } */ + /* i = 0; */ + /* while (word[i] && ft_ischarset("<>", word[i]) == FALSE) */ + /* i++; */ + /* while (redir > 0 && ft_isdigit(word[i]) == TRUE) */ + /* i--; */ + /* if (!(subst = ft_substr(word, 0, i))) */ + /* return (NULL); */ + /* if (!(words = ft_split(subst, ' '))) */ + /* { */ + /* ft_memdel((void*)&subst); */ + /* return (NULL); */ + /* } */ + /* ft_memdel((void*)&subst); */ + /* return (words); */ +} diff --git a/src/p_args.h b/src/p_args.h new file mode 100644 index 0000000..162723d --- /dev/null +++ b/src/p_args.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* p_args.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef P_ARGS_H +#define P_ARGS_H + +#include + +char **p_split_args(const char word[], int8_t redir); + +#endif diff --git a/src/p_lcom_next.c b/src/p_lcom_next.c index b04d393..28ad821 100644 --- a/src/p_lcom_next.c +++ b/src/p_lcom_next.c @@ -76,29 +76,32 @@ char int8_t redir) { char **words; - char *subst; - size_t i; - if (redir == 0) - { - if (!(words = ft_split(word, ' '))) - return (NULL); - return (words); - } - i = 0; - while (word[i] && ft_ischarset("<>", word[i]) == FALSE) - i++; - while (redir > 0 && ft_isdigit(word[i]) == TRUE) - i--; - if (!(subst = ft_substr(word, 0, i))) - return (NULL); - if (!(words = ft_split(subst, ' '))) - { - ft_memdel((void*)&subst); - return (NULL); - } - ft_memdel((void*)&subst); return (words); + /* char **words; */ + /* char *subst; */ + /* size_t i; */ + + /* if (redir == 0) */ + /* { */ + /* if (!(words = ft_split(word, ' '))) */ + /* return (NULL); */ + /* return (words); */ + /* } */ + /* i = 0; */ + /* while (word[i] && ft_ischarset("<>", word[i]) == FALSE) */ + /* i++; */ + /* while (redir > 0 && ft_isdigit(word[i]) == TRUE) */ + /* i--; */ + /* if (!(subst = ft_substr(word, 0, i))) */ + /* return (NULL); */ + /* if (!(words = ft_split(subst, ' '))) */ + /* { */ + /* ft_memdel((void*)&subst); */ + /* return (NULL); */ + /* } */ + /* ft_memdel((void*)&subst); */ + /* return (words); */ } char @@ -184,7 +187,7 @@ static char k = i; while (i - k < j) { - if (!(rewords[i - k] = ft_strdup(words[i]))) + if ((rewords[i - k] = ft_strdup(words[i])) == NULL) { ft_delwords(words); f_alloc_and_destroy_msh(msh); diff --git a/src/s_com.c b/src/s_com.c index 550616c..75a5040 100644 --- a/src/s_com.c +++ b/src/s_com.c @@ -14,6 +14,7 @@ #include #include "f_fail.h" +#include "p_args.h" #include "p_lcom.h" #include "p_lcom_next.h" #include "s_struct.h" @@ -113,7 +114,7 @@ t_com com->env_fork = NULL; if (get_redir(word, &com) != 0) return (NULL); - if ((words = p_subst_args(word, com->redir)) == NULL) + if ((words = p_split_args(word, com->redir)) == NULL) return (NULL); if ((words = p_subst_vars(words, msh)) == NULL) return (NULL); -- cgit v1.2.3 From d5a11fcb60022edfc96bf83c9ab8cdb03e6b762a Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Tue, 1 Sep 2020 16:44:52 +0200 Subject: Cool --- src/p_args.c | 25 ++++++++++++++++++++++++- src/s_com.c | 1 + 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/p_args.c b/src/p_args.c index 6b285da..d8ca029 100644 --- a/src/p_args.c +++ b/src/p_args.c @@ -14,16 +14,39 @@ #include #include +/* ================= */ +/* TODO: DELETE THIS */ +/* ================= */ + +static void +p_print(char *words[]) +{ + char **ptr; + + ptr = words; + while (*ptr != NULL) + { + ft_printf("[%s]\n", *ptr); + ptr++; + } +} + +/* ================== */ +/* TODO: DELETE ABOVE */ +/* ================== */ + char **p_split_args(const char word[], int8_t redir) { char **words; - (void)word; + ft_printf("word at start: [%s]\n", word); if (redir == 0) { + p_print(words); return (words); } + p_print(words); return (words); /* char **words; */ /* char *subst; */ diff --git a/src/s_com.c b/src/s_com.c index 75a5040..99b3158 100644 --- a/src/s_com.c +++ b/src/s_com.c @@ -116,6 +116,7 @@ t_com return (NULL); if ((words = p_split_args(word, com->redir)) == NULL) return (NULL); + /* TODO: subst vars is before getting words fuck my life */ if ((words = p_subst_vars(words, msh)) == NULL) return (NULL); if ((words = p_subst_home(words, msh)) == NULL) -- cgit v1.2.3 From 70e3b4133b89e4e77a67139ccaa37322e2507e49 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Tue, 1 Sep 2020 18:28:12 +0200 Subject: Added quotes multiline --- libft/Makefile | 1 + libft/include/libft.h | 1 + libft/src/ft_isspace.c | 1 - libft/src/ft_iswhitespace.c | 25 ++++++++++++++++ src/d_define.h | 9 ++++++ src/m_loop.c | 1 + src/m_loop_next.c | 61 ++++++++++++++++++++++++++++++++++++--- src/m_loop_next.h | 1 + src/p_args.c | 70 ++++++++++++++++++++++++++++++++++++++++++++- src/p_lcom_next.c | 33 --------------------- src/p_line.c | 4 +-- 11 files changed, 166 insertions(+), 41 deletions(-) create mode 100644 libft/src/ft_iswhitespace.c diff --git a/libft/Makefile b/libft/Makefile index 6ada2e1..431ec0b 100644 --- a/libft/Makefile +++ b/libft/Makefile @@ -78,6 +78,7 @@ SRCS_NAME += ft_putnbr_base.c SRCS_NAME += ft_strcat.c SRCS_NAME += ft_strcmp.c SRCS_NAME += ft_isspace.c +SRCS_NAME += ft_iswhitespace.c SRCS_NAME += ft_sqrt.c SRCS_NAME += ft_intlen.c SRCS_NAME += ft_intlen_base.c diff --git a/libft/include/libft.h b/libft/include/libft.h index 96615fd..68b8d9b 100644 --- a/libft/include/libft.h +++ b/libft/include/libft.h @@ -137,6 +137,7 @@ uint8_t ft_uintlen(unsigned long n); uint8_t ft_uintlen_base(unsigned long n, char *base); int ft_memcmp(const void *s1, const void *s2, size_t n); t_bool ft_isspace(int c); +t_bool ft_iswhitespace(int c); t_bool ft_ischarset(const char *charset, int c); t_bool ft_isupper(int c); t_bool ft_islower(int c); diff --git a/libft/src/ft_isspace.c b/libft/src/ft_isspace.c index b9b653f..8454c7c 100644 --- a/libft/src/ft_isspace.c +++ b/libft/src/ft_isspace.c @@ -11,7 +11,6 @@ /* ************************************************************************** */ #include -#include t_bool ft_isspace(int c) diff --git a/libft/src/ft_iswhitespace.c b/libft/src/ft_iswhitespace.c new file mode 100644 index 0000000..3bb5868 --- /dev/null +++ b/libft/src/ft_iswhitespace.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_iswhitespace.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:06:40 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:06:40 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include + +t_bool + ft_iswhitespace(int c) +{ + if (c == '\t' || + c == '\v' || + c == '\f' || + c == '\r' || + c == ' ') + return (TRUE); + return (FALSE); +} diff --git a/src/d_define.h b/src/d_define.h index 62cfe7c..66cc619 100644 --- a/src/d_define.h +++ b/src/d_define.h @@ -61,6 +61,15 @@ #define FT_FAIL_TOO_MANY_ARGS "too many arguments" #define FT_FAIL_HOME_NOT_SET "HOME not set" +/* +** ====== UTILS ====== +*/ + +#define C_SQUOTE '\'' +#define C_DQUOTE '"' +#define C_BACKSLASH '\\' +#define C_NULL '\000' + /* ** ====== FILES ====== */ diff --git a/src/m_loop.c b/src/m_loop.c index 1faa2cf..17668a5 100644 --- a/src/m_loop.c +++ b/src/m_loop.c @@ -87,6 +87,7 @@ uint8_t line = m_check_multi_backslash(fd, line, msh); line = m_check_multi_pipe(fd, line, msh); line = m_check_multi_and(fd, line, msh); + line = m_check_multi_quotes(fd, line, msh); if (fd == STDIN_FILENO) m_handle_hist(line, msh); m_parse_and_run_line(line, msh); diff --git a/src/m_loop_next.c b/src/m_loop_next.c index 1a8d867..22eadba 100644 --- a/src/m_loop_next.c +++ b/src/m_loop_next.c @@ -14,6 +14,7 @@ #include #include +#include "d_define.h" #include "m_prompt.h" #include "s_struct.h" @@ -25,7 +26,7 @@ static char if (fd == STDIN_FILENO) m_prompt_psx(psx, msh); get_next_line(fd, &counter_line); - if (counter_line[0] != 0) + if (counter_line[0] != C_NULL) { line = ft_nrealloc(line, ft_strlen(line) + 1, @@ -46,7 +47,7 @@ static char if (fd == STDIN_FILENO) m_prompt_psx(psx, msh); get_next_line(fd, &counter_line); - if (counter_line[0] != 0) + if (counter_line[0] != C_NULL) { line = ft_nrealloc(line, ft_strlen(line) + 1, @@ -59,6 +60,30 @@ static char return (line); } +static char + *m_counter_line_quotes(int32_t fd, uint8_t psx, char *line, t_msh *msh) +{ + char *counter_line; + + if (fd == STDIN_FILENO) + m_prompt_psx(psx, msh); + get_next_line(fd, &counter_line); + if (counter_line[0] != C_NULL) + { + line = ft_nrealloc(line, + ft_strlen(line) + 1, + ft_strlen(line) + ft_strlen(counter_line) + 2); + line[ft_strlen(line) + 1] = '\0'; + line[ft_strlen(line)] = '\n'; + ft_memcpy(line + ft_strlen(line), + counter_line, + ft_strlen(counter_line) + 1); + } + ft_memdel((void*)&counter_line); + ft_printf("[%s]\n", line); + return (line); +} + char *m_check_multi_backslash(int32_t fd, char line[], t_msh *msh) { @@ -80,7 +105,7 @@ char pipe += 1; while (*pipe != '\0') { - if (ft_isspace(*pipe) == FALSE) + if (ft_iswhitespace(*pipe) == FALSE) { return (line); } @@ -103,7 +128,7 @@ char and += 1; while (*and != '\0') { - if (ft_isspace(*and) == FALSE) + if (ft_iswhitespace(*and) == FALSE) { return (line); } @@ -116,3 +141,31 @@ char } return (line); } + +char + *m_check_multi_quotes(int32_t fd, char line[], t_msh *msh) +{ + char *ptr; + size_t q[2]; + + q[0] = 0; + q[1] = 0; + ptr = line; + while (*ptr != C_NULL) + { + if (*ptr == C_SQUOTE && *(ptr - 1) != C_BACKSLASH) + q[0] += 1; + else if (*ptr == C_DQUOTE && *(ptr - 1) != C_BACKSLASH) + q[1] += 1; + ptr++; + } + if (q[0] % 2 == 1 || q[1] % 2 == 1) + { + line = m_counter_line_quotes(fd, 2, line, msh); + line = m_check_multi_backslash(fd, line, msh); + line = m_check_multi_pipe(fd, line, msh); + line = m_check_multi_and(fd, line, msh); + line = m_check_multi_quotes(fd, line, msh); + } + return (line); +} diff --git a/src/m_loop_next.h b/src/m_loop_next.h index 225e2ed..b4ca62c 100644 --- a/src/m_loop_next.h +++ b/src/m_loop_next.h @@ -20,6 +20,7 @@ char *m_check_multi_backslash(int32_t fd, char line[], t_msh *msh); char *m_check_multi_pipe(int32_t fd, char line[], t_msh *msh); char *m_check_multi_and(int32_t fd, char line[], t_msh *msh); +char *m_check_multi_quotes(int32_t fd, char line[], t_msh *msh); #endif diff --git a/src/p_args.c b/src/p_args.c index d8ca029..1cab59d 100644 --- a/src/p_args.c +++ b/src/p_args.c @@ -14,6 +14,8 @@ #include #include +#include "d_define.h" + /* ================= */ /* TODO: DELETE THIS */ /* ================= */ @@ -35,14 +37,80 @@ p_print(char *words[]) /* TODO: DELETE ABOVE */ /* ================== */ +static void + p_skip_delim(char *ptr, char c) +{ + ptr++; + while (*ptr != C_NULL && *ptr != c) + { + ptr++; + if (*ptr == c && *(ptr - 1) == C_BACKSLASH) + ptr++; + } + if (*ptr != C_NULL) + ptr++; +} + +static uint16_t + p_count_args(char *ptr, uint16_t argc) +{ + if (*ptr == C_NULL) + return (argc); + while (*ptr != C_NULL && ft_iswhitespace(*ptr) == TRUE) + ptr++; + if (*ptr != C_SQUOTE && *ptr != C_DQUOTE) + { + while (*ptr != C_NULL && ft_iswhitespace(*ptr) == FALSE) + { + ptr++; + if ((*ptr == C_SQUOTE || *ptr == C_DQUOTE) && + *(ptr - 1) != C_BACKSLASH) + { + ptr++; + return (p_count_args(ptr, argc)); + } + } + } + else if (*ptr == C_SQUOTE) + { + p_skip_delim(ptr, C_SQUOTE); + } + else if (*ptr == C_DQUOTE) + { + p_skip_delim(ptr, C_DQUOTE); + } + return (p_count_args(ptr, argc + 1)); +} + +static char + **p_split_words_no_rdr(const char word[]) +{ + char **words; + char *ptr; + uint16_t argc; + + ptr = (char*)word; + argc = p_count_args(ptr, 0); + ft_printf("%hu\n", argc); + exit(0); + if ((words = (char**)malloc((argc + 1) * sizeof(char*))) == NULL) + return (NULL); + words[argc] = NULL; + return (words); +} + + char **p_split_args(const char word[], int8_t redir) { - char **words; + char **words; + words = NULL; ft_printf("word at start: [%s]\n", word); if (redir == 0) { + if ((words = p_split_words_no_rdr(word)) == NULL) + return (NULL); p_print(words); return (words); } diff --git a/src/p_lcom_next.c b/src/p_lcom_next.c index 28ad821..435a710 100644 --- a/src/p_lcom_next.c +++ b/src/p_lcom_next.c @@ -71,39 +71,6 @@ char return (words); } -char - **p_subst_args(const char word[], - int8_t redir) -{ - char **words; - - return (words); - /* char **words; */ - /* char *subst; */ - /* size_t i; */ - - /* if (redir == 0) */ - /* { */ - /* if (!(words = ft_split(word, ' '))) */ - /* return (NULL); */ - /* return (words); */ - /* } */ - /* i = 0; */ - /* while (word[i] && ft_ischarset("<>", word[i]) == FALSE) */ - /* i++; */ - /* while (redir > 0 && ft_isdigit(word[i]) == TRUE) */ - /* i--; */ - /* if (!(subst = ft_substr(word, 0, i))) */ - /* return (NULL); */ - /* if (!(words = ft_split(subst, ' '))) */ - /* { */ - /* ft_memdel((void*)&subst); */ - /* return (NULL); */ - /* } */ - /* ft_memdel((void*)&subst); */ - /* return (words); */ -} - char **p_subst_home(char *words[], t_msh *msh) diff --git a/src/p_line.c b/src/p_line.c index ec148e3..0224bbb 100644 --- a/src/p_line.c +++ b/src/p_line.c @@ -45,7 +45,7 @@ static void ptr = line; while (*ptr != '\0') { - if (ft_isspace(*ptr) == TRUE) + if (ft_iswhitespace(*ptr) == TRUE) { *ptr = ' '; } @@ -61,7 +61,7 @@ static t_bool ptr = line; while (*ptr != '\0') { - if (ft_isspace(*ptr) == FALSE) + if (ft_iswhitespace(*ptr) == FALSE) { return (FALSE); } -- cgit v1.2.3 From 93ec22714f7021e874e5418d1f5743ccdd258cdc Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Tue, 1 Sep 2020 18:39:30 +0200 Subject: Better, removed debug printf --- src/m_loop_next.c | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/src/m_loop_next.c b/src/m_loop_next.c index 22eadba..d6c085e 100644 --- a/src/m_loop_next.c +++ b/src/m_loop_next.c @@ -80,7 +80,6 @@ static char ft_strlen(counter_line) + 1); } ft_memdel((void*)&counter_line); - ft_printf("[%s]\n", line); return (line); } @@ -142,23 +141,40 @@ char return (line); } -char - *m_check_multi_quotes(int32_t fd, char line[], t_msh *msh) +static void + m_set_q_arr(size_t q[], char line[]) { char *ptr; - size_t q[2]; q[0] = 0; q[1] = 0; ptr = line; while (*ptr != C_NULL) { - if (*ptr == C_SQUOTE && *(ptr - 1) != C_BACKSLASH) - q[0] += 1; - else if (*ptr == C_DQUOTE && *(ptr - 1) != C_BACKSLASH) - q[1] += 1; + if (*ptr == C_SQUOTE) + { + if (*(ptr - 1) != C_BACKSLASH) + q[0] += 1; + else if (*(ptr - 2) == C_BACKSLASH) + q[0] += 1; + } + else if (*ptr == C_DQUOTE) + { + if (*(ptr - 1) != C_BACKSLASH) + q[1] += 1; + else if (*(ptr - 2) == C_BACKSLASH) + q[1] += 1; + } ptr++; } +} + +char + *m_check_multi_quotes(int32_t fd, char line[], t_msh *msh) +{ + size_t q[2]; + + m_set_q_arr(q, line); if (q[0] % 2 == 1 || q[1] % 2 == 1) { line = m_counter_line_quotes(fd, 2, line, msh); -- cgit v1.2.3 From 834e84e40b9a5e489ddcb1aef43f231ccdb3dfc2 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Tue, 1 Sep 2020 19:19:01 +0200 Subject: Fine --- src/m_loop_next.c | 5 +---- src/p_args.c | 25 +++++++++++++++---------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/m_loop_next.c b/src/m_loop_next.c index d6c085e..1163a49 100644 --- a/src/m_loop_next.c +++ b/src/m_loop_next.c @@ -153,10 +153,7 @@ static void { if (*ptr == C_SQUOTE) { - if (*(ptr - 1) != C_BACKSLASH) - q[0] += 1; - else if (*(ptr - 2) == C_BACKSLASH) - q[0] += 1; + q[0] += 1; } else if (*ptr == C_DQUOTE) { diff --git a/src/p_args.c b/src/p_args.c index 1cab59d..d42dabe 100644 --- a/src/p_args.c +++ b/src/p_args.c @@ -37,18 +37,25 @@ p_print(char *words[]) /* TODO: DELETE ABOVE */ /* ================== */ -static void - p_skip_delim(char *ptr, char c) +static char + *p_skip_delim(char *ptr, char c) { ptr++; while (*ptr != C_NULL && *ptr != c) { ptr++; - if (*ptr == c && *(ptr - 1) == C_BACKSLASH) - ptr++; + if (*ptr == c && c == C_DQUOTE) + { + if (*(ptr - 1) == C_BACKSLASH) + { + if (*(ptr - 2) != C_BACKSLASH) + ptr++; + } + } } if (*ptr != C_NULL) ptr++; + return (ptr); } static uint16_t @@ -71,13 +78,11 @@ static uint16_t } } } - else if (*ptr == C_SQUOTE) - { - p_skip_delim(ptr, C_SQUOTE); - } - else if (*ptr == C_DQUOTE) + else if (*ptr == C_SQUOTE || *ptr == C_DQUOTE) { - p_skip_delim(ptr, C_DQUOTE); + ptr = p_skip_delim(ptr, *ptr); + if (ft_isspace(*ptr) == FALSE) + return (p_count_args(ptr, argc)); } return (p_count_args(ptr, argc + 1)); } -- cgit v1.2.3 From 33b24656b7f0637d74531231f3d1b02bcc76dfc7 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Tue, 1 Sep 2020 19:45:03 +0200 Subject: Shit in progress --- src/p_args.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/p_args.c b/src/p_args.c index d42dabe..f14b467 100644 --- a/src/p_args.c +++ b/src/p_args.c @@ -65,13 +65,12 @@ static uint16_t return (argc); while (*ptr != C_NULL && ft_iswhitespace(*ptr) == TRUE) ptr++; - if (*ptr != C_SQUOTE && *ptr != C_DQUOTE) + if (*ptr != C_SQUOTE && *ptr != C_DQUOTE && *ptr != C_NULL) { while (*ptr != C_NULL && ft_iswhitespace(*ptr) == FALSE) { ptr++; - if ((*ptr == C_SQUOTE || *ptr == C_DQUOTE) && - *(ptr - 1) != C_BACKSLASH) + if ((*ptr == C_SQUOTE || *ptr == C_DQUOTE) && *(ptr - 1) != '\\') { ptr++; return (p_count_args(ptr, argc)); @@ -81,9 +80,11 @@ static uint16_t else if (*ptr == C_SQUOTE || *ptr == C_DQUOTE) { ptr = p_skip_delim(ptr, *ptr); - if (ft_isspace(*ptr) == FALSE) + if (ft_iswhitespace(*ptr) == FALSE) return (p_count_args(ptr, argc)); } + else if (*ptr == C_NULL) + return (argc); return (p_count_args(ptr, argc + 1)); } -- cgit v1.2.3 From f3f724dd7f434f2731f63d03a925b0e24983b484 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Thu, 3 Sep 2020 16:09:12 +0200 Subject: how tf do i use tok --- Makefile | 1 + src/p_args.c | 21 +++++++++++++++++++-- src/p_args_next.c | 36 ++++++++++++++++++++++++++++++++++++ src/p_args_next.h | 20 ++++++++++++++++++++ 4 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 src/p_args_next.c create mode 100644 src/p_args_next.h diff --git a/Makefile b/Makefile index bf8bb0c..94e77ee 100644 --- a/Makefile +++ b/Makefile @@ -62,6 +62,7 @@ SRCS_NAME += s_line SRCS_NAME += s_lvars SRCS_NAME += s_lpipes SRCS_NAME += p_args +SRCS_NAME += p_args_next SRCS_NAME += p_line SRCS_NAME += p_lcom SRCS_NAME += p_lcom_next diff --git a/src/p_args.c b/src/p_args.c index f14b467..6a15cb8 100644 --- a/src/p_args.c +++ b/src/p_args.c @@ -15,6 +15,7 @@ #include #include "d_define.h" +#include "p_args_next.h" /* ================= */ /* TODO: DELETE THIS */ @@ -86,6 +87,17 @@ static uint16_t else if (*ptr == C_NULL) return (argc); return (p_count_args(ptr, argc + 1)); + /* TODO: quotes parse error */ +} + +static void + p_del_alloced_words(char *words[], uint16_t to_del, uint16_t i) +{ + while (i > to_del) + { + ft_memdel((void*)&words[i]); + i--; + } } static char @@ -94,6 +106,7 @@ static char char **words; char *ptr; uint16_t argc; + uint16_t to_del; ptr = (char*)word; argc = p_count_args(ptr, 0); @@ -102,14 +115,18 @@ static char if ((words = (char**)malloc((argc + 1) * sizeof(char*))) == NULL) return (NULL); words[argc] = NULL; + if ((to_del = p_dup_words(words, word, argc)) != 0) + { + p_del_alloced_words(words, to_del, argc); + return (NULL); + } return (words); } - char **p_split_args(const char word[], int8_t redir) { - char **words; + char **words; words = NULL; ft_printf("word at start: [%s]\n", word); diff --git a/src/p_args_next.c b/src/p_args_next.c new file mode 100644 index 0000000..d371ba0 --- /dev/null +++ b/src/p_args_next.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* p_args_next.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include + +static char + p_give_me_an_arg(const char word[]) +{ + char str[4096]; + + str[0] = '\0'; + return (str); +} + +uint16_t + p_dup_words(char *words[], const char word[], uint16_t i) +{ + while (i > 0) + { + if ((words[i] = ft_strdup(p_give_me_an_arg(word))) == NULL) + return (i); + i--; + } + return (0); +} + diff --git a/src/p_args_next.h b/src/p_args_next.h new file mode 100644 index 0000000..46998b7 --- /dev/null +++ b/src/p_args_next.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* p_args_next.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef P_ARGS_NEXT_H +#define P_ARGS_NEXT_H + +#include + +uint16_t p_dup_words(char *words[], const char word[], uint16_t i); + +#endif -- cgit v1.2.3 From 869498a720e3c54b206f346dea6bf9b7995a7778 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Thu, 3 Sep 2020 17:18:41 +0200 Subject: Genius in progress --- src/p_args.c | 37 +++++++++++++++++++++---------------- src/p_args_next.c | 44 ++++++++++++++++++++++++++++++++++---------- src/p_args_next.h | 5 ++++- 3 files changed, 59 insertions(+), 27 deletions(-) diff --git a/src/p_args.c b/src/p_args.c index 6a15cb8..572b965 100644 --- a/src/p_args.c +++ b/src/p_args.c @@ -15,6 +15,7 @@ #include #include "d_define.h" +#include "p_args.h" #include "p_args_next.h" /* ================= */ @@ -60,12 +61,13 @@ static char } static uint16_t - p_count_args(char *ptr, uint16_t argc) + p_count_args(const char *head, char *ptr, uint16_t argc, size_t start[]) { if (*ptr == C_NULL) return (argc); while (*ptr != C_NULL && ft_iswhitespace(*ptr) == TRUE) ptr++; + start[argc] = ptr - head; if (*ptr != C_SQUOTE && *ptr != C_DQUOTE && *ptr != C_NULL) { while (*ptr != C_NULL && ft_iswhitespace(*ptr) == FALSE) @@ -74,7 +76,7 @@ static uint16_t if ((*ptr == C_SQUOTE || *ptr == C_DQUOTE) && *(ptr - 1) != '\\') { ptr++; - return (p_count_args(ptr, argc)); + return (p_count_args(head, ptr, argc, start)); } } } @@ -82,42 +84,44 @@ static uint16_t { ptr = p_skip_delim(ptr, *ptr); if (ft_iswhitespace(*ptr) == FALSE) - return (p_count_args(ptr, argc)); + return (p_count_args(head, ptr, argc, start)); } else if (*ptr == C_NULL) return (argc); - return (p_count_args(ptr, argc + 1)); + return (p_count_args(head, ptr, argc + 1, start)); /* TODO: quotes parse error */ } static void - p_del_alloced_words(char *words[], uint16_t to_del, uint16_t i) + p_del_alloced_words(char *words[], uint16_t to_del) { - while (i > to_del) + uint16_t i; + + i = 0; + while (i < to_del) { ft_memdel((void*)&words[i]); - i--; + i++; } } static char **p_split_words_no_rdr(const char word[]) { - char **words; - char *ptr; - uint16_t argc; - uint16_t to_del; + char **words; + char *ptr; + size_t start[512]; + uint16_t argc; + uint16_t to_del; ptr = (char*)word; - argc = p_count_args(ptr, 0); - ft_printf("%hu\n", argc); - exit(0); + argc = p_count_args(word, ptr, 0, start); if ((words = (char**)malloc((argc + 1) * sizeof(char*))) == NULL) return (NULL); words[argc] = NULL; - if ((to_del = p_dup_words(words, word, argc)) != 0) + if ((to_del = p_dup_words(words, word, argc, start)) != argc) { - p_del_alloced_words(words, to_del, argc); + p_del_alloced_words(words, to_del); return (NULL); } return (words); @@ -135,6 +139,7 @@ char if ((words = p_split_words_no_rdr(word)) == NULL) return (NULL); p_print(words); + exit(0); return (words); } p_print(words); diff --git a/src/p_args_next.c b/src/p_args_next.c index d371ba0..5967963 100644 --- a/src/p_args_next.c +++ b/src/p_args_next.c @@ -13,24 +13,48 @@ #include #include -static char - p_give_me_an_arg(const char word[]) +#include "p_args.h" + +static size_t + p_arg_len(const char word[], const size_t start) { - char str[4096]; + size_t end; - str[0] = '\0'; - return (str); + (void)word; + end = start; + return (end); +} + +static char + *p_give_me_an_arg(char tmp[], + const char word[], + const uint16_t i, + const size_t start[]) +{ + tmp[0] = '\0'; + ft_strlcpy(tmp, + word + start[i], + (p_arg_len(word, start[i]) - start[i]) + 1); + /* TODO: remove quotes */ + return (tmp); } uint16_t - p_dup_words(char *words[], const char word[], uint16_t i) + p_dup_words(char *words[], + const char word[], + const uint16_t argc, + const size_t start[]) { - while (i > 0) + char tmp[4096]; + uint16_t i; + + i = 0; + while (i < argc) { - if ((words[i] = ft_strdup(p_give_me_an_arg(word))) == NULL) + if ((words[i] = ft_strdup(p_give_me_an_arg(tmp, word, i, start))) == NULL) return (i); - i--; + i++; } - return (0); + return (i); } diff --git a/src/p_args_next.h b/src/p_args_next.h index 46998b7..e918254 100644 --- a/src/p_args_next.h +++ b/src/p_args_next.h @@ -15,6 +15,9 @@ #include -uint16_t p_dup_words(char *words[], const char word[], uint16_t i); +uint16_t p_dup_words(char *words[], + const char word[], + const uint16_t argc, + const size_t start[]); #endif -- cgit v1.2.3 From 93fd5f480c4fb841945611ed13ca3fa81b376dff Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Thu, 3 Sep 2020 18:37:45 +0200 Subject: Better conditions, fuck the norm niggas --- src/p_args.c | 7 ++++++- src/p_args_next.c | 38 +++++++++++++++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/src/p_args.c b/src/p_args.c index 572b965..953fa17 100644 --- a/src/p_args.c +++ b/src/p_args.c @@ -43,6 +43,8 @@ static char *p_skip_delim(char *ptr, char c) { ptr++; + if (*ptr == c) + return (++ptr); while (*ptr != C_NULL && *ptr != c) { ptr++; @@ -83,7 +85,9 @@ static uint16_t else if (*ptr == C_SQUOTE || *ptr == C_DQUOTE) { ptr = p_skip_delim(ptr, *ptr); - if (ft_iswhitespace(*ptr) == FALSE) + if (*ptr == C_NULL) + return (argc += 1); + else if (ft_iswhitespace(*ptr) == FALSE) return (p_count_args(head, ptr, argc, start)); } else if (*ptr == C_NULL) @@ -116,6 +120,7 @@ static char ptr = (char*)word; argc = p_count_args(word, ptr, 0, start); + ft_printf("%hu\n", argc); if ((words = (char**)malloc((argc + 1) * sizeof(char*))) == NULL) return (NULL); words[argc] = NULL; diff --git a/src/p_args_next.c b/src/p_args_next.c index 5967963..28410cd 100644 --- a/src/p_args_next.c +++ b/src/p_args_next.c @@ -13,15 +13,51 @@ #include #include +#include "d_define.h" #include "p_args.h" +static size_t + p_skip_delim_size(const char word[], char c, size_t end) +{ + end++; + if (word[end] == c) + return (end + 1); + while (word[end] != C_NULL && word[end] != c) + { + end++; + if (word[end] == c && c == C_DQUOTE) + { + if (word[end - 1] == C_BACKSLASH) + { + if (word[end - 2] != C_BACKSLASH) + end++; + } + } + } + if (word[end] != C_NULL) + { + end++; + } + return (end); +} + static size_t p_arg_len(const char word[], const size_t start) { size_t end; - (void)word; end = start; + if (word[start] != C_SQUOTE && + word[start] != C_DQUOTE && + word[start] != C_NULL) + { + while (word[end] != C_NULL && ft_iswhitespace(word[end]) == FALSE) + end++; + } + else if (word[end] == C_SQUOTE || word[end] == C_DQUOTE) + { + end = p_skip_delim_size(word, word[end], end); + } return (end); } -- cgit v1.2.3 From 757801bea2d236148e061324716425fc0e6aa686 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Thu, 3 Sep 2020 19:09:05 +0200 Subject: In progress --- Makefile | 1 + src/p_args.c | 43 +++++++++++++++++++++---------------------- src/p_args.h | 6 +++++- src/p_args_next.c | 14 +++++++++++++- src/p_args_next.h | 1 + src/p_args_quotes.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ src/p_args_quotes.h | 18 ++++++++++++++++++ 7 files changed, 103 insertions(+), 24 deletions(-) create mode 100644 src/p_args_quotes.c create mode 100644 src/p_args_quotes.h diff --git a/Makefile b/Makefile index 94e77ee..bf199f6 100644 --- a/Makefile +++ b/Makefile @@ -63,6 +63,7 @@ SRCS_NAME += s_lvars SRCS_NAME += s_lpipes SRCS_NAME += p_args SRCS_NAME += p_args_next +SRCS_NAME += p_args_quotes SRCS_NAME += p_line SRCS_NAME += p_lcom SRCS_NAME += p_lcom_next diff --git a/src/p_args.c b/src/p_args.c index 953fa17..a0da592 100644 --- a/src/p_args.c +++ b/src/p_args.c @@ -17,6 +17,7 @@ #include "d_define.h" #include "p_args.h" #include "p_args_next.h" +#include "p_args_quotes.h" /* ================= */ /* TODO: DELETE THIS */ @@ -63,6 +64,24 @@ static char } static uint16_t + p_skip_clean_arg(const char *head, + char **ptr, + uint16_t argc, + size_t start[]) +{ + while (*(*ptr) != C_NULL && ft_iswhitespace(*(*ptr)) == FALSE) + { + (*ptr)++; + if ((*(*ptr) == C_SQUOTE || *(*ptr) == C_DQUOTE) && *(*(ptr - 1)) != '\\') + { + (*ptr)++; + return (p_count_args(head, *ptr, argc, start)); + } + } + return (0); +} + +uint16_t p_count_args(const char *head, char *ptr, uint16_t argc, size_t start[]) { if (*ptr == C_NULL) @@ -72,15 +91,7 @@ static uint16_t start[argc] = ptr - head; if (*ptr != C_SQUOTE && *ptr != C_DQUOTE && *ptr != C_NULL) { - while (*ptr != C_NULL && ft_iswhitespace(*ptr) == FALSE) - { - ptr++; - if ((*ptr == C_SQUOTE || *ptr == C_DQUOTE) && *(ptr - 1) != '\\') - { - ptr++; - return (p_count_args(head, ptr, argc, start)); - } - } + p_skip_clean_arg(head, &ptr, argc, start); } else if (*ptr == C_SQUOTE || *ptr == C_DQUOTE) { @@ -96,19 +107,6 @@ static uint16_t /* TODO: quotes parse error */ } -static void - p_del_alloced_words(char *words[], uint16_t to_del) -{ - uint16_t i; - - i = 0; - while (i < to_del) - { - ft_memdel((void*)&words[i]); - i++; - } -} - static char **p_split_words_no_rdr(const char word[]) { @@ -129,6 +127,7 @@ static char p_del_alloced_words(words, to_del); return (NULL); } + p_args_quotes(words); return (words); } diff --git a/src/p_args.h b/src/p_args.h index 162723d..27f5729 100644 --- a/src/p_args.h +++ b/src/p_args.h @@ -15,6 +15,10 @@ #include -char **p_split_args(const char word[], int8_t redir); +uint16_t p_count_args(const char *head, + char *ptr, + uint16_t argc, + size_t start[]); +char **p_split_args(const char word[], int8_t redir); #endif diff --git a/src/p_args_next.c b/src/p_args_next.c index 28410cd..4d7f979 100644 --- a/src/p_args_next.c +++ b/src/p_args_next.c @@ -71,10 +71,22 @@ static char ft_strlcpy(tmp, word + start[i], (p_arg_len(word, start[i]) - start[i]) + 1); - /* TODO: remove quotes */ return (tmp); } +void + p_del_alloced_words(char *words[], uint16_t to_del) +{ + uint16_t i; + + i = 0; + while (i < to_del) + { + ft_memdel((void*)&words[i]); + i++; + } +} + uint16_t p_dup_words(char *words[], const char word[], diff --git a/src/p_args_next.h b/src/p_args_next.h index e918254..365f228 100644 --- a/src/p_args_next.h +++ b/src/p_args_next.h @@ -19,5 +19,6 @@ uint16_t p_dup_words(char *words[], const char word[], const uint16_t argc, const size_t start[]); +void p_del_alloced_words(char *words[], uint16_t to_del); #endif diff --git a/src/p_args_quotes.c b/src/p_args_quotes.c new file mode 100644 index 0000000..2b47b6b --- /dev/null +++ b/src/p_args_quotes.c @@ -0,0 +1,44 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* p_args_quotes.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include + +#include "d_define.h" + +static void + p_arg_squotes(char word[]) +{ + ft_memmove(word, word + 1, ft_strlen(word)); +} + +static void + p_arg_dquotes(char word[]) +{ + (void)word; +} + +void + p_args_quotes(char *words[]) +{ + char **ptr; + + ptr = words; + while (*ptr != NULL) + { + if (**ptr == C_SQUOTE) + p_arg_squotes(*ptr); + else if (**ptr == C_DQUOTE) + p_arg_dquotes(*ptr); + ptr++; + } +} diff --git a/src/p_args_quotes.h b/src/p_args_quotes.h new file mode 100644 index 0000000..b902438 --- /dev/null +++ b/src/p_args_quotes.h @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* p_args_quotes.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef P_ARGS_QUOTES_H +#define P_ARGS_QUOTES_H + +void p_args_quotes(char *words[]); + +#endif -- cgit v1.2.3 From be81297dfc28503f7c1014aad4efcf52bd13aad1 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Thu, 3 Sep 2020 19:24:54 +0200 Subject: am good --- src/p_args.c | 6 +++++- src/p_args_next.c | 3 ++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/p_args.c b/src/p_args.c index a0da592..72182fd 100644 --- a/src/p_args.c +++ b/src/p_args.c @@ -84,11 +84,15 @@ static uint16_t uint16_t p_count_args(const char *head, char *ptr, uint16_t argc, size_t start[]) { + static uint16_t preset = 512; + if (*ptr == C_NULL) return (argc); while (*ptr != C_NULL && ft_iswhitespace(*ptr) == TRUE) ptr++; - start[argc] = ptr - head; + if (preset != argc) + start[argc] = ptr - head; + preset = argc; if (*ptr != C_SQUOTE && *ptr != C_DQUOTE && *ptr != C_NULL) { p_skip_clean_arg(head, &ptr, argc, start); diff --git a/src/p_args_next.c b/src/p_args_next.c index 4d7f979..36f6bf1 100644 --- a/src/p_args_next.c +++ b/src/p_args_next.c @@ -56,7 +56,8 @@ static size_t } else if (word[end] == C_SQUOTE || word[end] == C_DQUOTE) { - end = p_skip_delim_size(word, word[end], end); + while (word[end] != C_NULL && ft_iswhitespace(word[end]) == FALSE) + end = p_skip_delim_size(word, word[end], end); } return (end); } -- cgit v1.2.3 From 7e3b7d9c3a0a4f4f0e03b3ade0b2b69fdc7804be Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Thu, 3 Sep 2020 19:37:37 +0200 Subject: =?UTF-8?q?On=20est=20sur=20un=20probl=C3=A8me?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/p_args_quotes.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/p_args_quotes.c b/src/p_args_quotes.c index 2b47b6b..3290a01 100644 --- a/src/p_args_quotes.c +++ b/src/p_args_quotes.c @@ -18,13 +18,29 @@ static void p_arg_squotes(char word[]) { - ft_memmove(word, word + 1, ft_strlen(word)); + char *ptr; + + ptr = word; + while ((ptr = ft_strchr(ptr, C_SQUOTE)) != NULL) + { + ft_memmove(word + (ptr - word), ptr + 1, ft_strlen(ptr + 1) + 1); + } } static void p_arg_dquotes(char word[]) { - (void)word; + char *ptr; + + ft_memmove(word, word + 1, ft_strlen(word)); + ptr = word; + while ((ptr = ft_strchr(ptr, C_DQUOTE)) != NULL) + { + if (*(ptr - 1) == C_BACKSLASH && *(ptr - 2) != C_BACKSLASH) + ptr++; + else + ft_memmove(word + (ptr - word), ptr + 1, ft_strlen(ptr + 1) + 1); + } } void -- cgit v1.2.3 From 59172914d2a6831474431c068eea9eca1e710499 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Thu, 3 Sep 2020 19:41:05 +0200 Subject: =?UTF-8?q?Pourquoi=20je=20suis=20n=C3=A9=20idiot?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/p_args_next.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/p_args_next.c b/src/p_args_next.c index 36f6bf1..10ca37f 100644 --- a/src/p_args_next.c +++ b/src/p_args_next.c @@ -27,10 +27,9 @@ static size_t end++; if (word[end] == c && c == C_DQUOTE) { - if (word[end - 1] == C_BACKSLASH) + if (word[end - 1] == C_BACKSLASH && word[end - 2] != C_BACKSLASH) { - if (word[end - 2] != C_BACKSLASH) - end++; + end++; } } } @@ -57,7 +56,16 @@ static size_t else if (word[end] == C_SQUOTE || word[end] == C_DQUOTE) { while (word[end] != C_NULL && ft_iswhitespace(word[end]) == FALSE) - end = p_skip_delim_size(word, word[end], end); + { + if (word[end] == C_SQUOTE || word[end] == C_DQUOTE) + end = p_skip_delim_size(word, word[end], end); + else + { + while (word[end] != C_NULL && + ft_iswhitespace(word[end]) == FALSE) + end++; + } + } } return (end); } -- cgit v1.2.3 From 8d3d25e2fbd912d4d1589a74c0bf0a3ba0b50025 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Thu, 3 Sep 2020 19:42:11 +0200 Subject: Next time --- src/p_args_quotes.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/p_args_quotes.c b/src/p_args_quotes.c index 3290a01..d0ade4f 100644 --- a/src/p_args_quotes.c +++ b/src/p_args_quotes.c @@ -37,10 +37,15 @@ static void while ((ptr = ft_strchr(ptr, C_DQUOTE)) != NULL) { if (*(ptr - 1) == C_BACKSLASH && *(ptr - 2) != C_BACKSLASH) + { ptr++; + } else + { ft_memmove(word + (ptr - word), ptr + 1, ft_strlen(ptr + 1) + 1); + } } + /* TODO: subst various dquotes chars */ } void -- cgit v1.2.3 From 0e9cfacaf15bb0f4d8a7bd72bae56010d263b2c1 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Fri, 4 Sep 2020 22:02:44 +0200 Subject: Time to escape chars --- Makefile | 1 + src/p_args.c | 32 +++++++++++------ src/p_args_len.c | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/p_args_len.h | 20 +++++++++++ src/p_args_next.c | 59 +++----------------------------- 5 files changed, 146 insertions(+), 66 deletions(-) create mode 100644 src/p_args_len.c create mode 100644 src/p_args_len.h diff --git a/Makefile b/Makefile index bf199f6..9d113ac 100644 --- a/Makefile +++ b/Makefile @@ -63,6 +63,7 @@ SRCS_NAME += s_lvars SRCS_NAME += s_lpipes SRCS_NAME += p_args SRCS_NAME += p_args_next +SRCS_NAME += p_args_len SRCS_NAME += p_args_quotes SRCS_NAME += p_line SRCS_NAME += p_lcom diff --git a/src/p_args.c b/src/p_args.c index 72182fd..c25fc40 100644 --- a/src/p_args.c +++ b/src/p_args.c @@ -63,7 +63,7 @@ static char return (ptr); } -static uint16_t +static int32_t p_skip_clean_arg(const char *head, char **ptr, uint16_t argc, @@ -72,11 +72,18 @@ static uint16_t while (*(*ptr) != C_NULL && ft_iswhitespace(*(*ptr)) == FALSE) { (*ptr)++; - if ((*(*ptr) == C_SQUOTE || *(*ptr) == C_DQUOTE) && *(*(ptr - 1)) != '\\') + if ((*(*ptr) == C_SQUOTE || *(*ptr) == C_DQUOTE) && + *(*ptr - 1) != C_BACKSLASH) { - (*ptr)++; + (*ptr) += 1; return (p_count_args(head, *ptr, argc, start)); } + if (ft_iswhitespace(*(*ptr)) == TRUE && *(*ptr - 1) == C_BACKSLASH && + *(*ptr - 2) != C_BACKSLASH) + { + (*ptr) += 1; + return (-1); + } } return (0); } @@ -84,7 +91,7 @@ static uint16_t uint16_t p_count_args(const char *head, char *ptr, uint16_t argc, size_t start[]) { - static uint16_t preset = 512; + static int32_t preset = -1; if (*ptr == C_NULL) return (argc); @@ -95,7 +102,8 @@ uint16_t preset = argc; if (*ptr != C_SQUOTE && *ptr != C_DQUOTE && *ptr != C_NULL) { - p_skip_clean_arg(head, &ptr, argc, start); + if (p_skip_clean_arg(head, &ptr, argc, start) == -1) + argc -= 1; } else if (*ptr == C_SQUOTE || *ptr == C_DQUOTE) { @@ -114,15 +122,15 @@ uint16_t static char **p_split_words_no_rdr(const char word[]) { - char **words; - char *ptr; - size_t start[512]; - uint16_t argc; - uint16_t to_del; + char **words; + char *ptr; + size_t start[512]; + uint16_t argc; + uint16_t to_del; ptr = (char*)word; argc = p_count_args(word, ptr, 0, start); - ft_printf("%hu\n", argc); + ft_printf("argc: %hu\n", argc); if ((words = (char**)malloc((argc + 1) * sizeof(char*))) == NULL) return (NULL); words[argc] = NULL; @@ -131,6 +139,8 @@ static char p_del_alloced_words(words, to_del); return (NULL); } + p_print(words); + p_escape_chars(words); p_args_quotes(words); return (words); } diff --git a/src/p_args_len.c b/src/p_args_len.c new file mode 100644 index 0000000..8bab9d7 --- /dev/null +++ b/src/p_args_len.c @@ -0,0 +1,100 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* p_args_len.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include + +#include "d_define.h" + +static size_t + p_skip_delim_size(const char word[], char c, size_t end) +{ + end++; + if (word[end] == c) + return (end + 1); + while (word[end] != C_NULL && word[end] != c) + { + end++; + if (word[end] == c && c == C_DQUOTE) + { + if (word[end - 1] == C_BACKSLASH && word[end - 2] != C_BACKSLASH) + { + end++; + } + } + } + if (word[end] != C_NULL) + { + end++; + } + return (end); +} + +static size_t + p_skip_unquote(const char word[], size_t end) +{ + while (word[end] != C_NULL && ft_iswhitespace(word[end]) == FALSE) + { + end++; + if (ft_iswhitespace(word[end]) == TRUE && end == 1 && + word[end - 1] == C_BACKSLASH) + { + end++; + } + else if (ft_iswhitespace(word[end]) == TRUE && end > 1 && + word[end - 1] == C_BACKSLASH && word[end - 2] != C_BACKSLASH) + { + end++; + } + } + return (end); +} + +static size_t + p_skip_quote(const char word[], size_t end) +{ + while (word[end] != C_NULL && ft_iswhitespace(word[end]) == FALSE) + { + if (word[end] == C_SQUOTE || word[end] == C_DQUOTE) + { + end = p_skip_delim_size(word, word[end], end); + } + else + { + while (word[end] != C_NULL && + ft_iswhitespace(word[end]) == FALSE) + { + end++; + } + } + } + return (end); +} + +size_t + p_arg_len(const char word[], const size_t start) +{ + size_t end; + + end = start; + if (word[start] != C_SQUOTE && word[start] != C_DQUOTE && + word[start] != C_NULL) + { + end = p_skip_unquote(word, end); + } + else if (word[end] == C_SQUOTE || word[end] == C_DQUOTE) + { + end = p_skip_quote(word, end); + } + return (end); +} + diff --git a/src/p_args_len.h b/src/p_args_len.h new file mode 100644 index 0000000..093d8f7 --- /dev/null +++ b/src/p_args_len.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* p_args_len.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef P_ARGS_LEN_H +#define P_ARGS_LEN_H + +#include + +size_t p_arg_len(const char word[], const size_t start); + +#endif diff --git a/src/p_args_next.c b/src/p_args_next.c index 10ca37f..77f8ced 100644 --- a/src/p_args_next.c +++ b/src/p_args_next.c @@ -15,60 +15,7 @@ #include "d_define.h" #include "p_args.h" - -static size_t - p_skip_delim_size(const char word[], char c, size_t end) -{ - end++; - if (word[end] == c) - return (end + 1); - while (word[end] != C_NULL && word[end] != c) - { - end++; - if (word[end] == c && c == C_DQUOTE) - { - if (word[end - 1] == C_BACKSLASH && word[end - 2] != C_BACKSLASH) - { - end++; - } - } - } - if (word[end] != C_NULL) - { - end++; - } - return (end); -} - -static size_t - p_arg_len(const char word[], const size_t start) -{ - size_t end; - - end = start; - if (word[start] != C_SQUOTE && - word[start] != C_DQUOTE && - word[start] != C_NULL) - { - while (word[end] != C_NULL && ft_iswhitespace(word[end]) == FALSE) - end++; - } - else if (word[end] == C_SQUOTE || word[end] == C_DQUOTE) - { - while (word[end] != C_NULL && ft_iswhitespace(word[end]) == FALSE) - { - if (word[end] == C_SQUOTE || word[end] == C_DQUOTE) - end = p_skip_delim_size(word, word[end], end); - else - { - while (word[end] != C_NULL && - ft_iswhitespace(word[end]) == FALSE) - end++; - } - } - } - return (end); -} +#include "p_args_len.h" static char *p_give_me_an_arg(char tmp[], @@ -108,7 +55,9 @@ uint16_t i = 0; while (i < argc) { - if ((words[i] = ft_strdup(p_give_me_an_arg(tmp, word, i, start))) == NULL) + if ((words[i] = ft_strdup( + p_give_me_an_arg(tmp, word, i, start) + )) == NULL) return (i); i++; } -- cgit v1.2.3 From 385b5c002a83d4683d29d9d4f306d929e1ad1e41 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Fri, 4 Sep 2020 22:24:49 +0200 Subject: gotta do stuff --- Makefile | 1 + src/p_args.c | 7 +++---- src/p_args_escape.c | 30 ++++++++++++++++++++++++++++++ src/p_args_escape.h | 18 ++++++++++++++++++ src/p_args_quotes.c | 3 ++- 5 files changed, 54 insertions(+), 5 deletions(-) create mode 100644 src/p_args_escape.c create mode 100644 src/p_args_escape.h diff --git a/Makefile b/Makefile index 9d113ac..9fa1924 100644 --- a/Makefile +++ b/Makefile @@ -64,6 +64,7 @@ SRCS_NAME += s_lpipes SRCS_NAME += p_args SRCS_NAME += p_args_next SRCS_NAME += p_args_len +SRCS_NAME += p_args_escape SRCS_NAME += p_args_quotes SRCS_NAME += p_line SRCS_NAME += p_lcom diff --git a/src/p_args.c b/src/p_args.c index c25fc40..9ce0230 100644 --- a/src/p_args.c +++ b/src/p_args.c @@ -17,6 +17,7 @@ #include "d_define.h" #include "p_args.h" #include "p_args_next.h" +#include "p_args_escape.h" #include "p_args_quotes.h" /* ================= */ @@ -139,9 +140,9 @@ static char p_del_alloced_words(words, to_del); return (NULL); } - p_print(words); - p_escape_chars(words); + p_args_escape_chars(words); p_args_quotes(words); + p_print(words); return (words); } @@ -156,11 +157,9 @@ char { if ((words = p_split_words_no_rdr(word)) == NULL) return (NULL); - p_print(words); exit(0); return (words); } - p_print(words); return (words); /* char **words; */ /* char *subst; */ diff --git a/src/p_args_escape.c b/src/p_args_escape.c new file mode 100644 index 0000000..e6c9a24 --- /dev/null +++ b/src/p_args_escape.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* p_args_escape.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include + +void + p_args_escape_chars(char *words[]) +{ + /* TODO: escape \ */ + /* TODO: escape $vars */ + /* TODO: escape $# special vars */ + /* TODO: escape my life */ + char **ptr; + + ptr = words; + while (*ptr != NULL) + { + /* if (**ptr != C_SQUOTE) */ + ptr++; + } +} diff --git a/src/p_args_escape.h b/src/p_args_escape.h new file mode 100644 index 0000000..1b6926c --- /dev/null +++ b/src/p_args_escape.h @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* p_args_escape.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef P_ARGS_ESCAPE_H +#define P_ARGS_ESCAPE_H + +void p_args_escape_chars(char *words[]); + +#endif diff --git a/src/p_args_quotes.c b/src/p_args_quotes.c index d0ade4f..24d39e7 100644 --- a/src/p_args_quotes.c +++ b/src/p_args_quotes.c @@ -45,9 +45,10 @@ static void ft_memmove(word + (ptr - word), ptr + 1, ft_strlen(ptr + 1) + 1); } } - /* TODO: subst various dquotes chars */ } +WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRROOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOONNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG + void p_args_quotes(char *words[]) { -- cgit v1.2.3 From 14d5ccd7d4a20143bdf60fff6ed54ba8342d8e94 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Fri, 4 Sep 2020 23:13:05 +0200 Subject: REALLY --- src/p_args_quotes.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/p_args_quotes.c b/src/p_args_quotes.c index 24d39e7..a63ce38 100644 --- a/src/p_args_quotes.c +++ b/src/p_args_quotes.c @@ -47,18 +47,17 @@ static void } } -WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRROOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOONNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG - void p_args_quotes(char *words[]) { char **ptr; + char *dq_ptr; + char *sq_ptr; ptr = words; while (*ptr != NULL) { - if (**ptr == C_SQUOTE) - p_arg_squotes(*ptr); + p_arg_squotes(*ptr); else if (**ptr == C_DQUOTE) p_arg_dquotes(*ptr); ptr++; -- cgit v1.2.3 From 011c05984decefb425114d2d73667366d3af2045 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Fri, 4 Sep 2020 23:37:56 +0200 Subject: This is all wrong --- src/p_args_quotes.c | 50 +++++++++++++++++++++++++++++++------------------- 1 file changed, 31 insertions(+), 19 deletions(-) diff --git a/src/p_args_quotes.c b/src/p_args_quotes.c index a63ce38..6ecbb90 100644 --- a/src/p_args_quotes.c +++ b/src/p_args_quotes.c @@ -15,35 +15,35 @@ #include "d_define.h" -static void - p_arg_squotes(char word[]) -{ - char *ptr; +/* static void */ +/* p_arg_squotes(char word[]) */ +/* { */ +/* char *ptr; */ - ptr = word; - while ((ptr = ft_strchr(ptr, C_SQUOTE)) != NULL) - { - ft_memmove(word + (ptr - word), ptr + 1, ft_strlen(ptr + 1) + 1); - } -} +/* ptr = word; */ +/* while ((ptr = ft_strchr(ptr, C_SQUOTE)) != NULL) */ +/* { */ +/* ft_memmove(word + (ptr - word), ptr + 1, ft_strlen(ptr + 1) + 1); */ +/* } */ +/* } */ static void p_arg_dquotes(char word[]) { char *ptr; - ft_memmove(word, word + 1, ft_strlen(word)); ptr = word; while ((ptr = ft_strchr(ptr, C_DQUOTE)) != NULL) { - if (*(ptr - 1) == C_BACKSLASH && *(ptr - 2) != C_BACKSLASH) - { + if (ptr - word == 0) + ft_memmove(word, word + 1, ft_strlen(word)); + else if (ptr - word == 1 && *(ptr - 1) == C_BACKSLASH) + ptr++; + else if (ptr - word > 1 && *(ptr - 1) == C_BACKSLASH && + *(ptr - 2) != C_BACKSLASH) ptr++; - } else - { ft_memmove(word + (ptr - word), ptr + 1, ft_strlen(ptr + 1) + 1); - } } } @@ -55,11 +55,23 @@ void char *sq_ptr; ptr = words; + dq_ptr = NULL; + sq_ptr = NULL; while (*ptr != NULL) { - p_arg_squotes(*ptr); - else if (**ptr == C_DQUOTE) - p_arg_dquotes(*ptr); + dq_ptr = ft_strchr(*ptr, C_DQUOTE); + sq_ptr = ft_strchr(*ptr, C_SQUOTE); + if (dq_ptr != NULL && sq_ptr != NULL) + { + if (dq_ptr < sq_ptr) + p_arg_dquotes(*ptr); + } + /* if (**ptr == C_SQUOTE) */ + /* p_arg_squotes(*ptr); */ + /* else if (**ptr == C_DQUOTE) */ + /* p_arg_dquotes(*ptr); */ ptr++; + dq_ptr = NULL; + sq_ptr = NULL; } } -- cgit v1.2.3 From 3976118a6e9acd035d20ef79d92632ecb962814e Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Sat, 5 Sep 2020 14:48:49 +0200 Subject: Rewrite in progress --- src/d_define.h | 2 +- src/m_loop_next.c | 44 +++++++++++++++++++++++--------------------- src/m_loop_next.h | 7 +++++++ src/p_args.c | 10 +++++----- src/p_args_len.c | 6 +++--- src/p_args_quotes.c | 6 +++--- 6 files changed, 42 insertions(+), 33 deletions(-) diff --git a/src/d_define.h b/src/d_define.h index 66cc619..ce0ccc9 100644 --- a/src/d_define.h +++ b/src/d_define.h @@ -67,7 +67,7 @@ #define C_SQUOTE '\'' #define C_DQUOTE '"' -#define C_BACKSLASH '\\' +#define C_BS '\\' #define C_NULL '\000' /* diff --git a/src/m_loop_next.c b/src/m_loop_next.c index 1163a49..75c00ac 100644 --- a/src/m_loop_next.c +++ b/src/m_loop_next.c @@ -15,6 +15,7 @@ #include #include "d_define.h" +#include "m_loop_next.h" #include "m_prompt.h" #include "s_struct.h" @@ -141,38 +142,39 @@ char return (line); } -static void - m_set_q_arr(size_t q[], char line[]) +static t_bool + m_check_missing_quotes(char line[]) { - char *ptr; - - q[0] = 0; - q[1] = 0; - ptr = line; - while (*ptr != C_NULL) + char *p; + uint8_t mode; + t_bool ret; + + ret = FALSE; + p = line; + mode = M_NO_MODE; + while (*p != C_NULL) { - if (*ptr == C_SQUOTE) - { - q[0] += 1; - } - else if (*ptr == C_DQUOTE) + if (*p == C_SQUOTE || *p == C_DQUOTE) { - if (*(ptr - 1) != C_BACKSLASH) - q[1] += 1; - else if (*(ptr - 2) == C_BACKSLASH) - q[1] += 1; + if (((p - line) == 0) || + ((p - line) == 1 && *(p - 1) != C_BS) || + ((p - line) > 1 && *(p - 1) == C_BS && + *(p - 2) == C_BS)) + mode = (*p == C_SQUOTE) ? (M_SQ_MODE) : (M_DQ_MODE); } - ptr++; + p++; } + return (ret); } char *m_check_multi_quotes(int32_t fd, char line[], t_msh *msh) { - size_t q[2]; + t_bool reparse; - m_set_q_arr(q, line); - if (q[0] % 2 == 1 || q[1] % 2 == 1) + reparse = FALSE; + reparse = m_check_missing_quotes(line); + if (reparse == TRUE) { line = m_counter_line_quotes(fd, 2, line, msh); line = m_check_multi_backslash(fd, line, msh); diff --git a/src/m_loop_next.h b/src/m_loop_next.h index b4ca62c..1c80e2a 100644 --- a/src/m_loop_next.h +++ b/src/m_loop_next.h @@ -17,6 +17,13 @@ #include "s_struct.h" +enum +{ + M_NO_MODE, + M_SQ_MODE, + M_DQ_MODE +}; + char *m_check_multi_backslash(int32_t fd, char line[], t_msh *msh); char *m_check_multi_pipe(int32_t fd, char line[], t_msh *msh); char *m_check_multi_and(int32_t fd, char line[], t_msh *msh); diff --git a/src/p_args.c b/src/p_args.c index 9ce0230..0ec391c 100644 --- a/src/p_args.c +++ b/src/p_args.c @@ -52,9 +52,9 @@ static char ptr++; if (*ptr == c && c == C_DQUOTE) { - if (*(ptr - 1) == C_BACKSLASH) + if (*(ptr - 1) == C_BS) { - if (*(ptr - 2) != C_BACKSLASH) + if (*(ptr - 2) != C_BS) ptr++; } } @@ -74,13 +74,13 @@ static int32_t { (*ptr)++; if ((*(*ptr) == C_SQUOTE || *(*ptr) == C_DQUOTE) && - *(*ptr - 1) != C_BACKSLASH) + *(*ptr - 1) != C_BS) { (*ptr) += 1; return (p_count_args(head, *ptr, argc, start)); } - if (ft_iswhitespace(*(*ptr)) == TRUE && *(*ptr - 1) == C_BACKSLASH && - *(*ptr - 2) != C_BACKSLASH) + if (ft_iswhitespace(*(*ptr)) == TRUE && *(*ptr - 1) == C_BS && + *(*ptr - 2) != C_BS) { (*ptr) += 1; return (-1); diff --git a/src/p_args_len.c b/src/p_args_len.c index 8bab9d7..c736a3a 100644 --- a/src/p_args_len.c +++ b/src/p_args_len.c @@ -26,7 +26,7 @@ static size_t end++; if (word[end] == c && c == C_DQUOTE) { - if (word[end - 1] == C_BACKSLASH && word[end - 2] != C_BACKSLASH) + if (word[end - 1] == C_BS && word[end - 2] != C_BS) { end++; } @@ -46,12 +46,12 @@ static size_t { end++; if (ft_iswhitespace(word[end]) == TRUE && end == 1 && - word[end - 1] == C_BACKSLASH) + word[end - 1] == C_BS) { end++; } else if (ft_iswhitespace(word[end]) == TRUE && end > 1 && - word[end - 1] == C_BACKSLASH && word[end - 2] != C_BACKSLASH) + word[end - 1] == C_BS && word[end - 2] != C_BS) { end++; } diff --git a/src/p_args_quotes.c b/src/p_args_quotes.c index 6ecbb90..e6fdd92 100644 --- a/src/p_args_quotes.c +++ b/src/p_args_quotes.c @@ -37,10 +37,10 @@ static void { if (ptr - word == 0) ft_memmove(word, word + 1, ft_strlen(word)); - else if (ptr - word == 1 && *(ptr - 1) == C_BACKSLASH) + else if (ptr - word == 1 && *(ptr - 1) == C_BS) ptr++; - else if (ptr - word > 1 && *(ptr - 1) == C_BACKSLASH && - *(ptr - 2) != C_BACKSLASH) + else if (ptr - word > 1 && *(ptr - 1) == C_BS && + *(ptr - 2) != C_BS) ptr++; else ft_memmove(word + (ptr - word), ptr + 1, ft_strlen(ptr + 1) + 1); -- cgit v1.2.3 From 8895d8b4d33d3c8a0242178b6d78178e055b11c7 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Sat, 5 Sep 2020 15:34:41 +0200 Subject: Good relining --- src/m_loop_next.c | 73 +++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 60 insertions(+), 13 deletions(-) diff --git a/src/m_loop_next.c b/src/m_loop_next.c index 75c00ac..f1d1f0b 100644 --- a/src/m_loop_next.c +++ b/src/m_loop_next.c @@ -142,29 +142,76 @@ char return (line); } +static t_bool + m_check_dquote(char **ptr, char line[]) +{ + if (((*ptr - line) == 0) || + ((*ptr - line) == 1 && *(*ptr - 1) == C_BS) || + ((*ptr - line) > 1 && *(*ptr - 1) == C_BS && *(*ptr - 2) != C_BS)) + { + return (FALSE); + } + else + { + return (TRUE); + } +} + +static t_bool + m_find_next_quote(char **ptr, char line[], uint8_t mode) +{ + char c; + + if (mode == M_NO_MODE) + return (FALSE); + c = (mode == M_SQ_MODE) ? (C_SQUOTE) : (C_DQUOTE); + (*ptr) += 1; + while (**ptr != C_NULL) + { + if (**ptr == c && c == C_DQUOTE) + { + if (m_check_dquote(ptr, line) == FALSE) + { + (*ptr)++; + if (**ptr == C_NULL) + break ; + } + else + return (FALSE); + } + else if (**ptr == c && c == C_SQUOTE) + return (FALSE); + (*ptr)++; + } + return (TRUE); +} + static t_bool m_check_missing_quotes(char line[]) { - char *p; + char *ptr; uint8_t mode; - t_bool ret; - ret = FALSE; - p = line; + ptr = line; mode = M_NO_MODE; - while (*p != C_NULL) + while (*ptr != C_NULL) { - if (*p == C_SQUOTE || *p == C_DQUOTE) + if (*ptr == C_SQUOTE || *ptr == C_DQUOTE) { - if (((p - line) == 0) || - ((p - line) == 1 && *(p - 1) != C_BS) || - ((p - line) > 1 && *(p - 1) == C_BS && - *(p - 2) == C_BS)) - mode = (*p == C_SQUOTE) ? (M_SQ_MODE) : (M_DQ_MODE); + if (((ptr - line) == 0) || + ((ptr - line) >= 1 && *(ptr - 1) != C_BS) || + ((ptr - line) > 1 && *(ptr - 1) == C_BS && *(ptr - 2) == C_BS)) + { + mode = (*ptr == C_SQUOTE) ? (M_SQ_MODE) : (M_DQ_MODE); + if (m_find_next_quote(&ptr, line, mode) == FALSE) + mode = M_NO_MODE; + else + return (TRUE); + } } - p++; + ptr++; } - return (ret); + return (FALSE); } char -- cgit v1.2.3 From 219803c3746620780f49a09d0966c1dbbba5ad50 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Sat, 5 Sep 2020 15:38:51 +0200 Subject: TODO --- TODO.org | 3 ++- src/d_define.h | 30 ------------------------------ src/d_enum.h | 37 +++++++++++++++++++++++++++++++++++++ src/m_loop_next.h | 7 ------- 4 files changed, 39 insertions(+), 38 deletions(-) diff --git a/TODO.org b/TODO.org index 5fa00b7..ac234bb 100644 --- a/TODO.org +++ b/TODO.org @@ -22,12 +22,13 @@ * Stuff to add ** DONE [#A] builtins to pipes ** DONE [#A] Proper ~ subst +** TODO [#A] SIGNAL ** DONE [#B] && || ** TODO [#B] & ** TODO [#B] $_ ** DONE [#B] .minishellrc ** DONE [#B] .minishell_history -** TODO [#B] SIGNAL +** TODO [#B] history builtin ** TODO [#C] Norm ** DONE [#C] [ builtin <=== pretty cool yet simple stuff ( [ $a -eq 1 ] ) ** TODO [#C] alias diff --git a/src/d_define.h b/src/d_define.h index ce0ccc9..ae3d92a 100644 --- a/src/d_define.h +++ b/src/d_define.h @@ -107,34 +107,4 @@ #define FT_SQB_LT "-lt" #define FT_SQB_LE "-le" -enum -{ - FT_ID_SQB_B, - FT_ID_SQB_C, - FT_ID_SQB_D, - FT_ID_SQB_E, - FT_ID_SQB_F, - FT_ID_SQB_H, - FT_ID_SQB_N, - FT_ID_SQB_P, - FT_ID_SQB_R, - FT_ID_SQB_S, - FT_ID_SQB_W, - FT_ID_SQB_X, - FT_ID_SQB_Z, - FT_ID_SQB_L_MAJ, - FT_ID_SQB_S_MAJ, - FT_ID_SQB_NT, - FT_ID_SQB_OT, - FT_ID_SQB_EQUA, - FT_ID_SQB_DIFF, - FT_ID_SQB_EQ, - FT_ID_SQB_NE, - FT_ID_SQB_GT, - FT_ID_SQB_GE, - FT_ID_SQB_LT, - FT_ID_SQB_LE, - FT_ID_SQB_COUNT -}; - #endif diff --git a/src/d_enum.h b/src/d_enum.h index c046118..6bd82df 100644 --- a/src/d_enum.h +++ b/src/d_enum.h @@ -32,6 +32,13 @@ enum FT_READ_END }; +enum +{ + FT_NO_MODE, + FT_SQ_MODE, + FT_DQ_MODE +}; + enum { FT_ID_ECHO, @@ -46,4 +53,34 @@ enum FT_BUILTINS_COUNT }; +enum +{ + FT_ID_SQB_B, + FT_ID_SQB_C, + FT_ID_SQB_D, + FT_ID_SQB_E, + FT_ID_SQB_F, + FT_ID_SQB_H, + FT_ID_SQB_N, + FT_ID_SQB_P, + FT_ID_SQB_R, + FT_ID_SQB_S, + FT_ID_SQB_W, + FT_ID_SQB_X, + FT_ID_SQB_Z, + FT_ID_SQB_L_MAJ, + FT_ID_SQB_S_MAJ, + FT_ID_SQB_NT, + FT_ID_SQB_OT, + FT_ID_SQB_EQUA, + FT_ID_SQB_DIFF, + FT_ID_SQB_EQ, + FT_ID_SQB_NE, + FT_ID_SQB_GT, + FT_ID_SQB_GE, + FT_ID_SQB_LT, + FT_ID_SQB_LE, + FT_ID_SQB_COUNT +}; + #endif diff --git a/src/m_loop_next.h b/src/m_loop_next.h index 1c80e2a..b4ca62c 100644 --- a/src/m_loop_next.h +++ b/src/m_loop_next.h @@ -17,13 +17,6 @@ #include "s_struct.h" -enum -{ - M_NO_MODE, - M_SQ_MODE, - M_DQ_MODE -}; - char *m_check_multi_backslash(int32_t fd, char line[], t_msh *msh); char *m_check_multi_pipe(int32_t fd, char line[], t_msh *msh); char *m_check_multi_and(int32_t fd, char line[], t_msh *msh); -- cgit v1.2.3 From b098fc000bed958f7df5e9edd3ed0871c59676e2 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Sat, 5 Sep 2020 15:44:35 +0200 Subject: commit --- src/m_loop_next.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/m_loop_next.c b/src/m_loop_next.c index f1d1f0b..31751f2 100644 --- a/src/m_loop_next.c +++ b/src/m_loop_next.c @@ -162,9 +162,9 @@ static t_bool { char c; - if (mode == M_NO_MODE) + if (mode == FT_NO_MODE) return (FALSE); - c = (mode == M_SQ_MODE) ? (C_SQUOTE) : (C_DQUOTE); + c = (mode == FT_SQ_MODE) ? (C_SQUOTE) : (C_DQUOTE); (*ptr) += 1; while (**ptr != C_NULL) { @@ -193,7 +193,7 @@ static t_bool uint8_t mode; ptr = line; - mode = M_NO_MODE; + mode = FT_NO_MODE; while (*ptr != C_NULL) { if (*ptr == C_SQUOTE || *ptr == C_DQUOTE) @@ -202,9 +202,9 @@ static t_bool ((ptr - line) >= 1 && *(ptr - 1) != C_BS) || ((ptr - line) > 1 && *(ptr - 1) == C_BS && *(ptr - 2) == C_BS)) { - mode = (*ptr == C_SQUOTE) ? (M_SQ_MODE) : (M_DQ_MODE); + mode = (*ptr == C_SQUOTE) ? (FT_SQ_MODE) : (FT_DQ_MODE); if (m_find_next_quote(&ptr, line, mode) == FALSE) - mode = M_NO_MODE; + mode = FT_NO_MODE; else return (TRUE); } -- cgit v1.2.3 From 8e8508c068111729d74d43d51bd6f2f9b9e1cdc0 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Sat, 5 Sep 2020 15:57:20 +0200 Subject: In progress --- Makefile | 1 - src/d_enum.h | 10 +++---- src/m_loop_next.c | 16 +++++------ src/p_args.c | 4 +-- src/p_args_escape.c | 14 ++++++++-- src/p_args_quotes.c | 77 ----------------------------------------------------- src/p_args_quotes.h | 18 ------------- 7 files changed, 26 insertions(+), 114 deletions(-) delete mode 100644 src/p_args_quotes.c delete mode 100644 src/p_args_quotes.h diff --git a/Makefile b/Makefile index 9fa1924..86d9a1f 100644 --- a/Makefile +++ b/Makefile @@ -65,7 +65,6 @@ SRCS_NAME += p_args SRCS_NAME += p_args_next SRCS_NAME += p_args_len SRCS_NAME += p_args_escape -SRCS_NAME += p_args_quotes SRCS_NAME += p_line SRCS_NAME += p_lcom SRCS_NAME += p_lcom_next diff --git a/src/d_enum.h b/src/d_enum.h index 6bd82df..e5078a7 100644 --- a/src/d_enum.h +++ b/src/d_enum.h @@ -32,12 +32,12 @@ enum FT_READ_END }; -enum +typedef enum { - FT_NO_MODE, - FT_SQ_MODE, - FT_DQ_MODE -}; + Q_NONE, + Q_SINGLE, + Q_DOUBLE +} t_quote_mode; enum { diff --git a/src/m_loop_next.c b/src/m_loop_next.c index 31751f2..4bb8f78 100644 --- a/src/m_loop_next.c +++ b/src/m_loop_next.c @@ -158,13 +158,13 @@ static t_bool } static t_bool - m_find_next_quote(char **ptr, char line[], uint8_t mode) + m_find_next_quote(char **ptr, char line[], t_quote_mode mode) { char c; - if (mode == FT_NO_MODE) + if (mode == Q_NONE) return (FALSE); - c = (mode == FT_SQ_MODE) ? (C_SQUOTE) : (C_DQUOTE); + c = (mode == Q_SINGLE) ? (C_SQUOTE) : (C_DQUOTE); (*ptr) += 1; while (**ptr != C_NULL) { @@ -189,11 +189,11 @@ static t_bool static t_bool m_check_missing_quotes(char line[]) { - char *ptr; - uint8_t mode; + char *ptr; + t_quote_mode mode; ptr = line; - mode = FT_NO_MODE; + mode = Q_NONE; while (*ptr != C_NULL) { if (*ptr == C_SQUOTE || *ptr == C_DQUOTE) @@ -202,9 +202,9 @@ static t_bool ((ptr - line) >= 1 && *(ptr - 1) != C_BS) || ((ptr - line) > 1 && *(ptr - 1) == C_BS && *(ptr - 2) == C_BS)) { - mode = (*ptr == C_SQUOTE) ? (FT_SQ_MODE) : (FT_DQ_MODE); + mode = (*ptr == C_SQUOTE) ? (Q_SINGLE) : (Q_DOUBLE); if (m_find_next_quote(&ptr, line, mode) == FALSE) - mode = FT_NO_MODE; + mode = Q_NONE; else return (TRUE); } diff --git a/src/p_args.c b/src/p_args.c index 0ec391c..fa01472 100644 --- a/src/p_args.c +++ b/src/p_args.c @@ -18,7 +18,6 @@ #include "p_args.h" #include "p_args_next.h" #include "p_args_escape.h" -#include "p_args_quotes.h" /* ================= */ /* TODO: DELETE THIS */ @@ -140,8 +139,7 @@ static char p_del_alloced_words(words, to_del); return (NULL); } - p_args_escape_chars(words); - p_args_quotes(words); + p_args_escape_chars_and_quotes(words); p_print(words); return (words); } diff --git a/src/p_args_escape.c b/src/p_args_escape.c index e6c9a24..6052991 100644 --- a/src/p_args_escape.c +++ b/src/p_args_escape.c @@ -12,11 +12,21 @@ #include +#include "d_define.h" + +static void + p_escape_arg(char *ptr) +{ + while (*ptr != C_NULL) + { + ptr++; + } +} + void p_args_escape_chars(char *words[]) { /* TODO: escape \ */ - /* TODO: escape $vars */ /* TODO: escape $# special vars */ /* TODO: escape my life */ char **ptr; @@ -24,7 +34,7 @@ void ptr = words; while (*ptr != NULL) { - /* if (**ptr != C_SQUOTE) */ + p_escape_arg(*ptr); ptr++; } } diff --git a/src/p_args_quotes.c b/src/p_args_quotes.c deleted file mode 100644 index e6fdd92..0000000 --- a/src/p_args_quotes.c +++ /dev/null @@ -1,77 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* p_args_quotes.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#include -#include - -#include "d_define.h" - -/* static void */ -/* p_arg_squotes(char word[]) */ -/* { */ -/* char *ptr; */ - -/* ptr = word; */ -/* while ((ptr = ft_strchr(ptr, C_SQUOTE)) != NULL) */ -/* { */ -/* ft_memmove(word + (ptr - word), ptr + 1, ft_strlen(ptr + 1) + 1); */ -/* } */ -/* } */ - -static void - p_arg_dquotes(char word[]) -{ - char *ptr; - - ptr = word; - while ((ptr = ft_strchr(ptr, C_DQUOTE)) != NULL) - { - if (ptr - word == 0) - ft_memmove(word, word + 1, ft_strlen(word)); - else if (ptr - word == 1 && *(ptr - 1) == C_BS) - ptr++; - else if (ptr - word > 1 && *(ptr - 1) == C_BS && - *(ptr - 2) != C_BS) - ptr++; - else - ft_memmove(word + (ptr - word), ptr + 1, ft_strlen(ptr + 1) + 1); - } -} - -void - p_args_quotes(char *words[]) -{ - char **ptr; - char *dq_ptr; - char *sq_ptr; - - ptr = words; - dq_ptr = NULL; - sq_ptr = NULL; - while (*ptr != NULL) - { - dq_ptr = ft_strchr(*ptr, C_DQUOTE); - sq_ptr = ft_strchr(*ptr, C_SQUOTE); - if (dq_ptr != NULL && sq_ptr != NULL) - { - if (dq_ptr < sq_ptr) - p_arg_dquotes(*ptr); - } - /* if (**ptr == C_SQUOTE) */ - /* p_arg_squotes(*ptr); */ - /* else if (**ptr == C_DQUOTE) */ - /* p_arg_dquotes(*ptr); */ - ptr++; - dq_ptr = NULL; - sq_ptr = NULL; - } -} diff --git a/src/p_args_quotes.h b/src/p_args_quotes.h deleted file mode 100644 index b902438..0000000 --- a/src/p_args_quotes.h +++ /dev/null @@ -1,18 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* p_args_quotes.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef P_ARGS_QUOTES_H -#define P_ARGS_QUOTES_H - -void p_args_quotes(char *words[]); - -#endif -- cgit v1.2.3 From 727ba70a61d66b18a9cfc035dbc2ccfdcc4a30a5 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Sat, 5 Sep 2020 15:57:59 +0200 Subject: Correct func name --- src/p_args_escape.c | 2 +- src/p_args_escape.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/p_args_escape.c b/src/p_args_escape.c index 6052991..ca77916 100644 --- a/src/p_args_escape.c +++ b/src/p_args_escape.c @@ -24,7 +24,7 @@ static void } void - p_args_escape_chars(char *words[]) + p_args_escape_chars_and_quotes(char *words[]) { /* TODO: escape \ */ /* TODO: escape $# special vars */ diff --git a/src/p_args_escape.h b/src/p_args_escape.h index 1b6926c..746a957 100644 --- a/src/p_args_escape.h +++ b/src/p_args_escape.h @@ -13,6 +13,6 @@ #ifndef P_ARGS_ESCAPE_H #define P_ARGS_ESCAPE_H -void p_args_escape_chars(char *words[]); +void p_args_escape_chars_and_quotes(char *words[]); #endif -- cgit v1.2.3 From 80e6471b3eb9dfa10fb0d86cf59fc76235c39554 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Sat, 5 Sep 2020 16:00:25 +0200 Subject: Not quite done yet --- src/p_args_escape.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/p_args_escape.c b/src/p_args_escape.c index ca77916..2a3a702 100644 --- a/src/p_args_escape.c +++ b/src/p_args_escape.c @@ -17,6 +17,9 @@ static void p_escape_arg(char *ptr) { + t_quote_mode mode; + + mode = Q_NONE; while (*ptr != C_NULL) { ptr++; -- cgit v1.2.3 From 247023e3fac41c99b5580d69262837429fb2bfed Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Sat, 5 Sep 2020 17:03:49 +0200 Subject: Fuck shell --- TODO.org | 1 + src/m_loop_next.c | 5 ++- src/m_prompt.c | 3 +- src/p_args_escape.c | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++++- src/u_utils.c | 11 +++++++ src/u_utils.h | 2 ++ 6 files changed, 108 insertions(+), 5 deletions(-) diff --git a/TODO.org b/TODO.org index ac234bb..62f583e 100644 --- a/TODO.org +++ b/TODO.org @@ -18,6 +18,7 @@ ** TODO [#C] Handle memory ** TODO [#C] Go full stack to ken dash in the ass ** TODO [#C] Handle comments better # +** TODO [#C] msh ~> ./qwe.sh <---- without shebang (maybe works find already) * Stuff to add ** DONE [#A] builtins to pipes diff --git a/src/m_loop_next.c b/src/m_loop_next.c index 4bb8f78..3bc85e8 100644 --- a/src/m_loop_next.c +++ b/src/m_loop_next.c @@ -18,6 +18,7 @@ #include "m_loop_next.h" #include "m_prompt.h" #include "s_struct.h" +#include "u_utils.h" static char *m_counter_line_backslash(int32_t fd, uint8_t psx, char *line, t_msh *msh) @@ -198,9 +199,7 @@ static t_bool { if (*ptr == C_SQUOTE || *ptr == C_DQUOTE) { - if (((ptr - line) == 0) || - ((ptr - line) >= 1 && *(ptr - 1) != C_BS) || - ((ptr - line) > 1 && *(ptr - 1) == C_BS && *(ptr - 2) == C_BS)) + if (u_is_true_quote(line, ptr) == TRUE) { mode = (*ptr == C_SQUOTE) ? (Q_SINGLE) : (Q_DOUBLE); if (m_find_next_quote(&ptr, line, mode) == FALSE) diff --git a/src/m_prompt.c b/src/m_prompt.c index ad3c957..1ea8d70 100644 --- a/src/m_prompt.c +++ b/src/m_prompt.c @@ -54,7 +54,8 @@ static char while (tmp[j] != '/' && j != 0) j--; j++; - ft_memmove(tmp, tmp + j, (ft_strlen(tmp) - j) + 1); + (void)ft_memmove(tmp, tmp + j, + ((ft_strlen(tmp) - j) + 1) * sizeof(char)); } var = ft_strsubst(var, "\\W", tmp); ft_memdel((void*)&tmp); diff --git a/src/p_args_escape.c b/src/p_args_escape.c index 2a3a702..182ef2f 100644 --- a/src/p_args_escape.c +++ b/src/p_args_escape.c @@ -10,18 +10,105 @@ /* */ /* ************************************************************************** */ +#include #include #include "d_define.h" +#include "u_utils.h" + +static t_quote_mode + p_escape_squote(char *ptr, char word[], t_quote_mode mode) +{ + if (mode == Q_NONE) + { + (void)ft_memmove(word + (ptr - word), ptr + 1, + (ft_strlen(ptr + 1) + 1) * sizeof(char)); + return (Q_SINGLE); + } + else if (mode == Q_SINGLE) + { + (void)ft_memmove(word + (ptr - word), ptr + 1, + (ft_strlen(ptr + 1) + 1) * sizeof(char)); + return (Q_NONE); + } + return (mode); +} + +static t_quote_mode + p_escape_dquote(char *ptr, char word[], t_quote_mode mode) +{ + if (mode == Q_NONE) + { + if (u_is_true_quote(word, ptr) == TRUE) + { + (void)ft_memmove(word + (ptr - word), ptr + 1, + (ft_strlen(ptr + 1) + 1) * sizeof(char)); + } + return (Q_DOUBLE); + } + else if (mode == Q_DOUBLE) + { + if (u_is_true_quote(word, ptr) == TRUE) + { + (void)ft_memmove(word + (ptr - word), ptr + 1, + (ft_strlen(ptr + 1) + 1) * sizeof(char)); + } + return (Q_NONE); + } + return (mode); +} + +static void + p_escape_bs(char *ptr, char word[], t_quote_mode mode) +{ + if (mode == Q_NONE) + { + (void)ft_memmove(word + (ptr - word), ptr + 1, + (ft_strlen(ptr + 1) + 1) * sizeof(char)); + } + else if (mode == Q_DOUBLE) + { + if (*(ptr + 1) == C_BS) + { + (void)ft_memmove(word + (ptr - word), ptr + 1, + (ft_strlen(ptr + 1) + 1) * sizeof(char)); + *(word + (ptr - word)) = 26; + } + else if (*(ptr + 1) == C_DQUOTE) + { + (void)ft_memmove(word + (ptr - word), ptr + 1, + (ft_strlen(ptr + 1) + 1) * sizeof(char)); + } + } +} static void - p_escape_arg(char *ptr) + p_escape_arg(char word[]) { + char *ptr; t_quote_mode mode; + ptr = word; mode = Q_NONE; while (*ptr != C_NULL) { + if (*ptr == C_SQUOTE) + mode = p_escape_squote(ptr, word, mode); + else if (*ptr == C_DQUOTE) + mode = p_escape_dquote(ptr, word, mode); + if (*ptr == C_BS) + p_escape_bs(ptr, word, mode); + ptr++; + } +} + +static void + p_replace_bs(char *ptr) +{ + while (*ptr != C_NULL) + { + if (*ptr == 26) + *ptr = C_BS; ptr++; } } @@ -32,12 +119,14 @@ void /* TODO: escape \ */ /* TODO: escape $# special vars */ /* TODO: escape my life */ + /* TODO: comments */ char **ptr; ptr = words; while (*ptr != NULL) { p_escape_arg(*ptr); + p_replace_bs(*ptr); ptr++; } } diff --git a/src/u_utils.c b/src/u_utils.c index ad44e82..d554a98 100644 --- a/src/u_utils.c +++ b/src/u_utils.c @@ -15,9 +15,20 @@ #include #include +#include "d_define.h" #include "f_fail.h" #include "s_struct.h" +t_bool + u_is_true_quote(char *head, char *ptr) +{ + if (((ptr - head) == 0) || + ((ptr - head) >= 1 && *(ptr - 1) != C_BS) || + ((ptr - head) > 1 && *(ptr - 1) == C_BS && *(ptr - 2) == C_BS)) + return (TRUE); + return (FALSE); +} + void u_eof_fd(int32_t fd) { diff --git a/src/u_utils.h b/src/u_utils.h index 22b63ff..1b83d65 100644 --- a/src/u_utils.h +++ b/src/u_utils.h @@ -13,10 +13,12 @@ #ifndef U_UTILS_H #define U_UTILS_H +#include #include #include "s_struct.h" +t_bool u_is_true_quote(char *head, char *ptr); void u_eof_fd(int32_t fd); uint64_t u_builtins_get_argc(const char *args[]); char **u_get_env_var_names(t_msh *msh); -- cgit v1.2.3 From 2d9205bd7bd7dd3efa5aa2f7b19638f778271bc8 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Sat, 5 Sep 2020 17:06:30 +0200 Subject: More defines --- src/d_define.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/d_define.h b/src/d_define.h index ae3d92a..b8196a7 100644 --- a/src/d_define.h +++ b/src/d_define.h @@ -51,6 +51,7 @@ #define FT_ZER_VAR "$0" #define FT_RET_VAR "$?" #define FT_PID_VAR "$$" +#define FT_ARGC_VAR "$#" /* ** ====== FAIL MSG ====== -- cgit v1.2.3 From 0bd6ce0d398af5cb9e19482e77dbec1bb26fc9bd Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Sat, 5 Sep 2020 17:14:00 +0200 Subject: Better defines --- src/d_define.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/d_define.h b/src/d_define.h index b8196a7..ef980c6 100644 --- a/src/d_define.h +++ b/src/d_define.h @@ -66,10 +66,10 @@ ** ====== UTILS ====== */ -#define C_SQUOTE '\'' -#define C_DQUOTE '"' -#define C_BS '\\' -#define C_NULL '\000' +#define C_SQUOTE 0x27 +#define C_DQUOTE 0x22 +#define C_BS 0x5c +#define C_NULL 0x00 /* ** ====== FILES ====== -- cgit v1.2.3 From c75434256658e3009a5c61d02524cb49492bd120 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Sat, 5 Sep 2020 18:40:28 +0200 Subject: Clean af --- src/m_loop_next.c | 2 +- src/p_args.c | 121 +++++++++++++++++++++++++++------------------------- src/p_args.h | 4 -- src/p_args_escape.c | 32 ++++++++------ src/p_line.c | 17 -------- src/u_utils.c | 2 +- src/u_utils.h | 2 +- 7 files changed, 86 insertions(+), 94 deletions(-) diff --git a/src/m_loop_next.c b/src/m_loop_next.c index 3bc85e8..fb95236 100644 --- a/src/m_loop_next.c +++ b/src/m_loop_next.c @@ -199,7 +199,7 @@ static t_bool { if (*ptr == C_SQUOTE || *ptr == C_DQUOTE) { - if (u_is_true_quote(line, ptr) == TRUE) + if (u_is_not_escaped(line, ptr) == TRUE) { mode = (*ptr == C_SQUOTE) ? (Q_SINGLE) : (Q_DOUBLE); if (m_find_next_quote(&ptr, line, mode) == FALSE) diff --git a/src/p_args.c b/src/p_args.c index fa01472..f3e7403 100644 --- a/src/p_args.c +++ b/src/p_args.c @@ -18,6 +18,7 @@ #include "p_args.h" #include "p_args_next.h" #include "p_args_escape.h" +#include "u_utils.h" /* ================= */ /* TODO: DELETE THIS */ @@ -40,82 +41,87 @@ p_print(char *words[]) /* TODO: DELETE ABOVE */ /* ================== */ -static char - *p_skip_delim(char *ptr, char c) +static t_quote_mode + p_meet_dquote(char *head, char *ptr, t_quote_mode mode) { - ptr++; - if (*ptr == c) - return (++ptr); - while (*ptr != C_NULL && *ptr != c) + if (mode == Q_NONE) { - ptr++; - if (*ptr == c && c == C_DQUOTE) + if (u_is_not_escaped(head, ptr) == TRUE) { - if (*(ptr - 1) == C_BS) - { - if (*(ptr - 2) != C_BS) - ptr++; - } + return (Q_DOUBLE); + } + else + { + return (Q_NONE); } } - if (*ptr != C_NULL) - ptr++; - return (ptr); + else if (mode == Q_DOUBLE && u_is_not_escaped(head, ptr) == TRUE) + { + return (Q_NONE); + } + return (mode); } -static int32_t - p_skip_clean_arg(const char *head, - char **ptr, - uint16_t argc, - size_t start[]) +static t_quote_mode + p_meet_squote(char *head, char *ptr, t_quote_mode mode) { - while (*(*ptr) != C_NULL && ft_iswhitespace(*(*ptr)) == FALSE) + if (mode == Q_NONE) { - (*ptr)++; - if ((*(*ptr) == C_SQUOTE || *(*ptr) == C_DQUOTE) && - *(*ptr - 1) != C_BS) + if (u_is_not_escaped(head, ptr) == TRUE) { - (*ptr) += 1; - return (p_count_args(head, *ptr, argc, start)); + return (Q_SINGLE); } - if (ft_iswhitespace(*(*ptr)) == TRUE && *(*ptr - 1) == C_BS && - *(*ptr - 2) != C_BS) + else { - (*ptr) += 1; - return (-1); + return (Q_NONE); } } - return (0); + else if (mode == Q_SINGLE) + { + return (Q_NONE); + } + return (mode); } -uint16_t - p_count_args(const char *head, char *ptr, uint16_t argc, size_t start[]) +static t_bool + p_meet_whitespace(char *head, char *ptr, t_quote_mode mode) { - static int32_t preset = -1; - - if (*ptr == C_NULL) - return (argc); - while (*ptr != C_NULL && ft_iswhitespace(*ptr) == TRUE) - ptr++; - if (preset != argc) - start[argc] = ptr - head; - preset = argc; - if (*ptr != C_SQUOTE && *ptr != C_DQUOTE && *ptr != C_NULL) + if (mode == Q_NONE && u_is_not_escaped(head, ptr) == TRUE) { - if (p_skip_clean_arg(head, &ptr, argc, start) == -1) - argc -= 1; + return (TRUE); } - else if (*ptr == C_SQUOTE || *ptr == C_DQUOTE) + return (FALSE); +} + +static uint16_t + p_count_args(const char word[], size_t start[]) +{ + char *ptr; + t_quote_mode mode; + uint16_t count; + + (void)start; + ptr = (char *)word; + mode = Q_NONE; + count = 0; + while (*ptr != C_NULL) { - ptr = p_skip_delim(ptr, *ptr); - if (*ptr == C_NULL) - return (argc += 1); - else if (ft_iswhitespace(*ptr) == FALSE) - return (p_count_args(head, ptr, argc, start)); + if (*ptr == C_DQUOTE) + mode = p_meet_dquote((char*)word, ptr, mode); + else if (*ptr == C_SQUOTE) + mode = p_meet_squote((char*)word, ptr, mode); + if (ft_iswhitespace(*ptr) && + p_meet_whitespace((char*)word, ptr, mode) == TRUE) + { + count += 1; + while (*ptr != C_NULL && ft_iswhitespace(*ptr)) + ptr++; + ptr -= 1; + ft_printf("[%s]\n", ptr); + } + ptr++; } - else if (*ptr == C_NULL) - return (argc); - return (p_count_args(head, ptr, argc + 1, start)); + return (count); /* TODO: quotes parse error */ } @@ -123,14 +129,13 @@ static char **p_split_words_no_rdr(const char word[]) { char **words; - char *ptr; size_t start[512]; uint16_t argc; uint16_t to_del; - ptr = (char*)word; - argc = p_count_args(word, ptr, 0, start); + argc = p_count_args(word, start); ft_printf("argc: %hu\n", argc); + exit(0); if ((words = (char**)malloc((argc + 1) * sizeof(char*))) == NULL) return (NULL); words[argc] = NULL; diff --git a/src/p_args.h b/src/p_args.h index 27f5729..899f7ea 100644 --- a/src/p_args.h +++ b/src/p_args.h @@ -15,10 +15,6 @@ #include -uint16_t p_count_args(const char *head, - char *ptr, - uint16_t argc, - size_t start[]); char **p_split_args(const char word[], int8_t redir); #endif diff --git a/src/p_args_escape.c b/src/p_args_escape.c index 182ef2f..2df2e0f 100644 --- a/src/p_args_escape.c +++ b/src/p_args_escape.c @@ -21,9 +21,16 @@ static t_quote_mode { if (mode == Q_NONE) { - (void)ft_memmove(word + (ptr - word), ptr + 1, - (ft_strlen(ptr + 1) + 1) * sizeof(char)); - return (Q_SINGLE); + if (u_is_not_escaped(word, ptr) == TRUE) + { + (void)ft_memmove(word + (ptr - word), ptr + 1, + (ft_strlen(ptr + 1) + 1) * sizeof(char)); + return (Q_SINGLE); + } + else + { + return (Q_NONE); + } } else if (mode == Q_SINGLE) { @@ -39,20 +46,21 @@ static t_quote_mode { if (mode == Q_NONE) { - if (u_is_true_quote(word, ptr) == TRUE) + if (u_is_not_escaped(word, ptr) == TRUE) { (void)ft_memmove(word + (ptr - word), ptr + 1, (ft_strlen(ptr + 1) + 1) * sizeof(char)); + return (Q_DOUBLE); } - return (Q_DOUBLE); - } - else if (mode == Q_DOUBLE) - { - if (u_is_true_quote(word, ptr) == TRUE) + else { - (void)ft_memmove(word + (ptr - word), ptr + 1, - (ft_strlen(ptr + 1) + 1) * sizeof(char)); + return (Q_NONE); } + } + else if (mode == Q_DOUBLE && u_is_not_escaped(word, ptr) == TRUE) + { + (void)ft_memmove(word + (ptr - word), ptr + 1, + (ft_strlen(ptr + 1) + 1) * sizeof(char)); return (Q_NONE); } return (mode); @@ -118,8 +126,8 @@ void { /* TODO: escape \ */ /* TODO: escape $# special vars */ + /* TODO: comments ################ */ /* TODO: escape my life */ - /* TODO: comments */ char **ptr; ptr = words; diff --git a/src/p_line.c b/src/p_line.c index 0224bbb..9c96610 100644 --- a/src/p_line.c +++ b/src/p_line.c @@ -37,22 +37,6 @@ static void } } -static void - p_subst_spaces(char line[]) -{ - char *ptr; - - ptr = line; - while (*ptr != '\0') - { - if (ft_iswhitespace(*ptr) == TRUE) - { - *ptr = ' '; - } - ptr++; - } -} - static t_bool p_check_whitespaces_only(char line[]) { @@ -75,7 +59,6 @@ void t_msh *msh) { p_delete_comments(line); - p_subst_spaces(line); if (p_check_whitespaces_only(line) == TRUE) { return ; diff --git a/src/u_utils.c b/src/u_utils.c index d554a98..fb1dcf6 100644 --- a/src/u_utils.c +++ b/src/u_utils.c @@ -20,7 +20,7 @@ #include "s_struct.h" t_bool - u_is_true_quote(char *head, char *ptr) + u_is_not_escaped(char *head, char *ptr) { if (((ptr - head) == 0) || ((ptr - head) >= 1 && *(ptr - 1) != C_BS) || diff --git a/src/u_utils.h b/src/u_utils.h index 1b83d65..2aa2c72 100644 --- a/src/u_utils.h +++ b/src/u_utils.h @@ -18,7 +18,7 @@ #include "s_struct.h" -t_bool u_is_true_quote(char *head, char *ptr); +t_bool u_is_not_escaped(char *head, char *ptr); void u_eof_fd(int32_t fd); uint64_t u_builtins_get_argc(const char *args[]); char **u_get_env_var_names(t_msh *msh); -- cgit v1.2.3 From f359ecc021b8ec5360cfd9e7806d445f69a3fb63 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Sat, 5 Sep 2020 19:05:22 +0200 Subject: In progress boyyy --- src/p_args.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/p_args.c b/src/p_args.c index f3e7403..ee2b937 100644 --- a/src/p_args.c +++ b/src/p_args.c @@ -93,6 +93,14 @@ static t_bool return (FALSE); } +static char + *p_skip_whitespace(char *ptr) +{ + while (*ptr != C_NULL && ft_iswhitespace(*ptr)) + ptr++; + return (ptr); +} + static uint16_t p_count_args(const char word[], size_t start[]) { @@ -100,10 +108,11 @@ static uint16_t t_quote_mode mode; uint16_t count; - (void)start; ptr = (char *)word; mode = Q_NONE; - count = 0; + count = 1; + ptr = p_skip_whitespace(ptr); + start[0] = (ptr - word); while (*ptr != C_NULL) { if (*ptr == C_DQUOTE) @@ -114,10 +123,9 @@ static uint16_t p_meet_whitespace((char*)word, ptr, mode) == TRUE) { count += 1; - while (*ptr != C_NULL && ft_iswhitespace(*ptr)) - ptr++; + ptr = p_skip_whitespace(ptr); + start[count - 1] = (ptr - word); ptr -= 1; - ft_printf("[%s]\n", ptr); } ptr++; } @@ -135,7 +143,6 @@ static char argc = p_count_args(word, start); ft_printf("argc: %hu\n", argc); - exit(0); if ((words = (char**)malloc((argc + 1) * sizeof(char*))) == NULL) return (NULL); words[argc] = NULL; -- cgit v1.2.3 From 6b5a91d7e94b5fe40a20eed3ae1f2f3559c9248d Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Sun, 6 Sep 2020 17:33:30 +0200 Subject: TODO.org update --- TODO.org | 1 + 1 file changed, 1 insertion(+) diff --git a/TODO.org b/TODO.org index 62f583e..fd602a9 100644 --- a/TODO.org +++ b/TODO.org @@ -8,6 +8,7 @@ ** DONE [#A] Multiline pipes (ls |) ** TODO [#A] Go full tok + quotes ** TODO [#A] ls qweqwe; echo $? <---- substitute +** TODO [#A] Careful p_line \; ** TODO [#A] <>> ** DONE [#B] forked write(2) stuff on cd ** DONE [#B] Multiline && || -- cgit v1.2.3 From 9c587711fc62b560809686491548aad35627bc57 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Sun, 6 Sep 2020 17:47:39 +0200 Subject: I'm lost --- src/p_args_escape.c | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/p_args_escape.c b/src/p_args_escape.c index 2df2e0f..8a2e27f 100644 --- a/src/p_args_escape.c +++ b/src/p_args_escape.c @@ -17,7 +17,7 @@ #include "u_utils.h" static t_quote_mode - p_escape_squote(char *ptr, char word[], t_quote_mode mode) + p_escape_squote(char *ptr, char **p, char word[], t_quote_mode mode) { if (mode == Q_NONE) { @@ -25,6 +25,7 @@ static t_quote_mode { (void)ft_memmove(word + (ptr - word), ptr + 1, (ft_strlen(ptr + 1) + 1) * sizeof(char)); + *(p) -= 1; return (Q_SINGLE); } else @@ -36,13 +37,14 @@ static t_quote_mode { (void)ft_memmove(word + (ptr - word), ptr + 1, (ft_strlen(ptr + 1) + 1) * sizeof(char)); + *(p) -= 1; return (Q_NONE); } return (mode); } static t_quote_mode - p_escape_dquote(char *ptr, char word[], t_quote_mode mode) + p_escape_dquote(char *ptr, char **p, char word[], t_quote_mode mode) { if (mode == Q_NONE) { @@ -50,6 +52,7 @@ static t_quote_mode { (void)ft_memmove(word + (ptr - word), ptr + 1, (ft_strlen(ptr + 1) + 1) * sizeof(char)); + *(p) -= 1; return (Q_DOUBLE); } else @@ -61,18 +64,24 @@ static t_quote_mode { (void)ft_memmove(word + (ptr - word), ptr + 1, (ft_strlen(ptr + 1) + 1) * sizeof(char)); + *(p) -= 1; return (Q_NONE); } return (mode); } static void - p_escape_bs(char *ptr, char word[], t_quote_mode mode) + p_escape_bs(char *ptr, char **p, char word[], t_quote_mode mode) { if (mode == Q_NONE) { (void)ft_memmove(word + (ptr - word), ptr + 1, (ft_strlen(ptr + 1) + 1) * sizeof(char)); + if (*ptr == C_BS) + { + *(word + (ptr - word)) = 26; + } + *(p) -= 1; } else if (mode == Q_DOUBLE) { @@ -81,11 +90,13 @@ static void (void)ft_memmove(word + (ptr - word), ptr + 1, (ft_strlen(ptr + 1) + 1) * sizeof(char)); *(word + (ptr - word)) = 26; + *(p) -= 1; } else if (*(ptr + 1) == C_DQUOTE) { (void)ft_memmove(word + (ptr - word), ptr + 1, (ft_strlen(ptr + 1) + 1) * sizeof(char)); + *(p) -= 1; } } } @@ -101,11 +112,11 @@ static void while (*ptr != C_NULL) { if (*ptr == C_SQUOTE) - mode = p_escape_squote(ptr, word, mode); + mode = p_escape_squote(ptr, &ptr, word, mode); else if (*ptr == C_DQUOTE) - mode = p_escape_dquote(ptr, word, mode); + mode = p_escape_dquote(ptr, &ptr, word, mode); if (*ptr == C_BS) - p_escape_bs(ptr, word, mode); + p_escape_bs(ptr, &ptr, word, mode); ptr++; } } -- cgit v1.2.3 From ab69117ff18f8b4a2b13adeada00cabd7d997855 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Sun, 6 Sep 2020 17:56:59 +0200 Subject: Better defines --- src/d_define.h | 5 +++-- src/m_loop_next.c | 16 ++++++++-------- src/p_args.c | 5 +++-- src/p_args_escape.c | 13 +++++++------ src/p_args_len.c | 18 +++++++++--------- src/u_utils.c | 4 ++-- 6 files changed, 32 insertions(+), 29 deletions(-) diff --git a/src/d_define.h b/src/d_define.h index ef980c6..5c10526 100644 --- a/src/d_define.h +++ b/src/d_define.h @@ -68,8 +68,9 @@ #define C_SQUOTE 0x27 #define C_DQUOTE 0x22 -#define C_BS 0x5c -#define C_NULL 0x00 +#define C_BACKS 0x5c +#define C_NUL 0x00 +#define C_SUB 0x1a /* ** ====== FILES ====== diff --git a/src/m_loop_next.c b/src/m_loop_next.c index fb95236..d138206 100644 --- a/src/m_loop_next.c +++ b/src/m_loop_next.c @@ -28,7 +28,7 @@ static char if (fd == STDIN_FILENO) m_prompt_psx(psx, msh); get_next_line(fd, &counter_line); - if (counter_line[0] != C_NULL) + if (counter_line[0] != C_NUL) { line = ft_nrealloc(line, ft_strlen(line) + 1, @@ -49,7 +49,7 @@ static char if (fd == STDIN_FILENO) m_prompt_psx(psx, msh); get_next_line(fd, &counter_line); - if (counter_line[0] != C_NULL) + if (counter_line[0] != C_NUL) { line = ft_nrealloc(line, ft_strlen(line) + 1, @@ -70,7 +70,7 @@ static char if (fd == STDIN_FILENO) m_prompt_psx(psx, msh); get_next_line(fd, &counter_line); - if (counter_line[0] != C_NULL) + if (counter_line[0] != C_NUL) { line = ft_nrealloc(line, ft_strlen(line) + 1, @@ -147,8 +147,8 @@ static t_bool m_check_dquote(char **ptr, char line[]) { if (((*ptr - line) == 0) || - ((*ptr - line) == 1 && *(*ptr - 1) == C_BS) || - ((*ptr - line) > 1 && *(*ptr - 1) == C_BS && *(*ptr - 2) != C_BS)) + ((*ptr - line) == 1 && *(*ptr - 1) == C_BACKS) || + ((*ptr - line) > 1 && *(*ptr - 1) == C_BACKS && *(*ptr - 2) != C_BACKS)) { return (FALSE); } @@ -167,14 +167,14 @@ static t_bool return (FALSE); c = (mode == Q_SINGLE) ? (C_SQUOTE) : (C_DQUOTE); (*ptr) += 1; - while (**ptr != C_NULL) + while (**ptr != C_NUL) { if (**ptr == c && c == C_DQUOTE) { if (m_check_dquote(ptr, line) == FALSE) { (*ptr)++; - if (**ptr == C_NULL) + if (**ptr == C_NUL) break ; } else @@ -195,7 +195,7 @@ static t_bool ptr = line; mode = Q_NONE; - while (*ptr != C_NULL) + while (*ptr != C_NUL) { if (*ptr == C_SQUOTE || *ptr == C_DQUOTE) { diff --git a/src/p_args.c b/src/p_args.c index ee2b937..7d5a579 100644 --- a/src/p_args.c +++ b/src/p_args.c @@ -96,7 +96,7 @@ static t_bool static char *p_skip_whitespace(char *ptr) { - while (*ptr != C_NULL && ft_iswhitespace(*ptr)) + while (*ptr != C_NUL && ft_iswhitespace(*ptr)) ptr++; return (ptr); } @@ -113,8 +113,9 @@ static uint16_t count = 1; ptr = p_skip_whitespace(ptr); start[0] = (ptr - word); - while (*ptr != C_NULL) + while (*ptr != C_NUL) { + if (*ptr == C_BACKS) if (*ptr == C_DQUOTE) mode = p_meet_dquote((char*)word, ptr, mode); else if (*ptr == C_SQUOTE) diff --git a/src/p_args_escape.c b/src/p_args_escape.c index 8a2e27f..ae96bb0 100644 --- a/src/p_args_escape.c +++ b/src/p_args_escape.c @@ -77,15 +77,16 @@ static void { (void)ft_memmove(word + (ptr - word), ptr + 1, (ft_strlen(ptr + 1) + 1) * sizeof(char)); - if (*ptr == C_BS) + if (*ptr == C_BACKS) { *(word + (ptr - word)) = 26; + ft_printf("%s\n", word); } *(p) -= 1; } else if (mode == Q_DOUBLE) { - if (*(ptr + 1) == C_BS) + if (*(ptr + 1) == C_BACKS) { (void)ft_memmove(word + (ptr - word), ptr + 1, (ft_strlen(ptr + 1) + 1) * sizeof(char)); @@ -109,13 +110,13 @@ static void ptr = word; mode = Q_NONE; - while (*ptr != C_NULL) + while (*ptr != C_NUL) { if (*ptr == C_SQUOTE) mode = p_escape_squote(ptr, &ptr, word, mode); else if (*ptr == C_DQUOTE) mode = p_escape_dquote(ptr, &ptr, word, mode); - if (*ptr == C_BS) + if (*ptr == C_BACKS) p_escape_bs(ptr, &ptr, word, mode); ptr++; } @@ -124,10 +125,10 @@ static void static void p_replace_bs(char *ptr) { - while (*ptr != C_NULL) + while (*ptr != C_NUL) { if (*ptr == 26) - *ptr = C_BS; + *ptr = C_BACKS; ptr++; } } diff --git a/src/p_args_len.c b/src/p_args_len.c index c736a3a..801406f 100644 --- a/src/p_args_len.c +++ b/src/p_args_len.c @@ -21,18 +21,18 @@ static size_t end++; if (word[end] == c) return (end + 1); - while (word[end] != C_NULL && word[end] != c) + while (word[end] != C_NUL && word[end] != c) { end++; if (word[end] == c && c == C_DQUOTE) { - if (word[end - 1] == C_BS && word[end - 2] != C_BS) + if (word[end - 1] == C_BACKS && word[end - 2] != C_BACKS) { end++; } } } - if (word[end] != C_NULL) + if (word[end] != C_NUL) { end++; } @@ -42,16 +42,16 @@ static size_t static size_t p_skip_unquote(const char word[], size_t end) { - while (word[end] != C_NULL && ft_iswhitespace(word[end]) == FALSE) + while (word[end] != C_NUL && ft_iswhitespace(word[end]) == FALSE) { end++; if (ft_iswhitespace(word[end]) == TRUE && end == 1 && - word[end - 1] == C_BS) + word[end - 1] == C_BACKS) { end++; } else if (ft_iswhitespace(word[end]) == TRUE && end > 1 && - word[end - 1] == C_BS && word[end - 2] != C_BS) + word[end - 1] == C_BACKS && word[end - 2] != C_BACKS) { end++; } @@ -62,7 +62,7 @@ static size_t static size_t p_skip_quote(const char word[], size_t end) { - while (word[end] != C_NULL && ft_iswhitespace(word[end]) == FALSE) + while (word[end] != C_NUL && ft_iswhitespace(word[end]) == FALSE) { if (word[end] == C_SQUOTE || word[end] == C_DQUOTE) { @@ -70,7 +70,7 @@ static size_t } else { - while (word[end] != C_NULL && + while (word[end] != C_NUL && ft_iswhitespace(word[end]) == FALSE) { end++; @@ -87,7 +87,7 @@ size_t end = start; if (word[start] != C_SQUOTE && word[start] != C_DQUOTE && - word[start] != C_NULL) + word[start] != C_NUL) { end = p_skip_unquote(word, end); } diff --git a/src/u_utils.c b/src/u_utils.c index fb1dcf6..7f75c4d 100644 --- a/src/u_utils.c +++ b/src/u_utils.c @@ -23,8 +23,8 @@ t_bool u_is_not_escaped(char *head, char *ptr) { if (((ptr - head) == 0) || - ((ptr - head) >= 1 && *(ptr - 1) != C_BS) || - ((ptr - head) > 1 && *(ptr - 1) == C_BS && *(ptr - 2) == C_BS)) + ((ptr - head) >= 1 && *(ptr - 1) != C_BACKS) || + ((ptr - head) > 1 && *(ptr - 1) == C_BACKS && *(ptr - 2) == C_BACKS)) return (TRUE); return (FALSE); } -- cgit v1.2.3 From 92f7b8562ddc1e8d0484f7ec212a12e3019a24a5 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Sun, 6 Sep 2020 18:14:26 +0200 Subject: Gitignore update --- .gitignore | 4 +++- src/p_args.c | 39 +++++++++++++++++++++++++-------------- src/p_args_escape.c | 15 ++++++++------- 3 files changed, 36 insertions(+), 22 deletions(-) diff --git a/.gitignore b/.gitignore index 6513a01..03b4dd2 100644 --- a/.gitignore +++ b/.gitignore @@ -129,4 +129,6 @@ vgcore* qwe *.sh noread -.clang_complete \ No newline at end of file +.clang_complete +/bash +/mini diff --git a/src/p_args.c b/src/p_args.c index 7d5a579..7f5302f 100644 --- a/src/p_args.c +++ b/src/p_args.c @@ -24,23 +24,36 @@ /* TODO: DELETE THIS */ /* ================= */ +/* static void */ +/* p_print(char *words[]) */ +/* { */ +/* char **ptr; */ + +/* ptr = words; */ +/* while (*ptr != NULL) */ +/* { */ +/* ft_printf("[%s]\n", *ptr); */ +/* ptr++; */ +/* } */ +/* } */ + +/* ================== */ +/* TODO: DELETE ABOVE */ +/* ================== */ + static void -p_print(char *words[]) + p_meet_bs(char *ptr, t_quote_mode mode) { - char **ptr; - - ptr = words; - while (*ptr != NULL) + if (mode != Q_SINGLE) { - ft_printf("[%s]\n", *ptr); - ptr++; + if (*(ptr + 1) == C_BACKS) + { + *ptr = C_SUB; + *(ptr + 1) = C_SUB; + } } } -/* ================== */ -/* TODO: DELETE ABOVE */ -/* ================== */ - static t_quote_mode p_meet_dquote(char *head, char *ptr, t_quote_mode mode) { @@ -116,6 +129,7 @@ static uint16_t while (*ptr != C_NUL) { if (*ptr == C_BACKS) + p_meet_bs(ptr, mode); if (*ptr == C_DQUOTE) mode = p_meet_dquote((char*)word, ptr, mode); else if (*ptr == C_SQUOTE) @@ -143,7 +157,6 @@ static char uint16_t to_del; argc = p_count_args(word, start); - ft_printf("argc: %hu\n", argc); if ((words = (char**)malloc((argc + 1) * sizeof(char*))) == NULL) return (NULL); words[argc] = NULL; @@ -153,7 +166,6 @@ static char return (NULL); } p_args_escape_chars_and_quotes(words); - p_print(words); return (words); } @@ -163,7 +175,6 @@ char char **words; words = NULL; - ft_printf("word at start: [%s]\n", word); if (redir == 0) { if ((words = p_split_words_no_rdr(word)) == NULL) diff --git a/src/p_args_escape.c b/src/p_args_escape.c index ae96bb0..834d9f7 100644 --- a/src/p_args_escape.c +++ b/src/p_args_escape.c @@ -77,11 +77,6 @@ static void { (void)ft_memmove(word + (ptr - word), ptr + 1, (ft_strlen(ptr + 1) + 1) * sizeof(char)); - if (*ptr == C_BACKS) - { - *(word + (ptr - word)) = 26; - ft_printf("%s\n", word); - } *(p) -= 1; } else if (mode == Q_DOUBLE) @@ -90,7 +85,6 @@ static void { (void)ft_memmove(word + (ptr - word), ptr + 1, (ft_strlen(ptr + 1) + 1) * sizeof(char)); - *(word + (ptr - word)) = 26; *(p) -= 1; } else if (*(ptr + 1) == C_DQUOTE) @@ -125,10 +119,17 @@ static void static void p_replace_bs(char *ptr) { + char *head; + + head = ptr; while (*ptr != C_NUL) { - if (*ptr == 26) + if (*ptr == C_SUB && *(ptr + 1) == C_SUB) + { + (void)ft_memmove(head + (ptr - head), ptr + 1, + (ft_strlen(ptr + 1) + 1) * sizeof(char)); *ptr = C_BACKS; + } ptr++; } } -- cgit v1.2.3 From 9ed927a6d4fd26dcdc7c094a0001521f072d8ae4 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Sun, 6 Sep 2020 18:25:02 +0200 Subject: Update --- TODO.org | 1 + src/b_echo.c | 66 +---------------------------------------------------- src/p_args.c | 27 +++++++++++----------- src/p_args_escape.c | 2 +- 4 files changed, 17 insertions(+), 79 deletions(-) diff --git a/TODO.org b/TODO.org index fd602a9..3eadba8 100644 --- a/TODO.org +++ b/TODO.org @@ -7,6 +7,7 @@ ** DONE [#A] fix pwd when cd / ** DONE [#A] Multiline pipes (ls |) ** TODO [#A] Go full tok + quotes +** TODO [#A] Variables quotes qwe="okokoko kkok" asd='bav' ** TODO [#A] ls qweqwe; echo $? <---- substitute ** TODO [#A] Careful p_line \; ** TODO [#A] <>> diff --git a/src/b_echo.c b/src/b_echo.c index ec66049..b3d4953 100644 --- a/src/b_echo.c +++ b/src/b_echo.c @@ -18,79 +18,16 @@ #include "s_struct.h" #include "u_utils.h" -/* TODO: echo "quoted text", echo 'quoted text', done*/ -/* echo kill\nbackslash\nbut\nnot\nn, echo "quoted\nnew\nlines", done */ -/* full buffer */ - -void - e_put(char *str, - uint8_t op) -{ - size_t i; - - i = -1; - if (op == 0) - while (str[++i]) - { - if (str[i] == '\\') - i++; - ft_putchar(str[i]); - } -} - -char - *e_initb(char *ptr[], - char *str) -{ - size_t len; - uint8_t i; - - len = 0; - i = -1; - while (ptr[++i]) - { - len += ft_strlen(ptr[i]); - } - if (!(str = (char*)malloc(len * sizeof(char)))) - return (NULL); - return (str); -} - -/***********************************************/ -/* void */ -/* e_fill(char *ptr[], char **str) */ -/* { */ -/* char *bs; */ -/* */ -/* ft_sprintf(*str, "%s", *ptr); */ -/* if (*str[0] == '\"' || *str[0] == '\'') */ -/* { */ -/* *str = ft_strtrim(*str, "\"\'"); */ -/* ft_printf("%s", *str); */ -/* } */ -/* else if ((bs = ft_strrchr(*str, '\\'))) */ -/* { */ -/* e_put(*str, 0); */ -/* } */ -/* else */ -/* ft_printf("%s", *str); */ -/* } */ -/***********************************************/ - uint8_t - b_echo(char *args[], - t_msh *msh) + b_echo(char *args[], t_msh *msh) { const uint64_t argc = u_builtins_get_argc((const char **)args); char **ptr; - char *str; int8_t nopt; (void)msh; ptr = args; nopt = 0; - str = NULL; - str = e_initb(ptr, str); if (argc >= 1) { if (ft_strncmp(ptr[0], "-n", 3) == 0) @@ -109,7 +46,6 @@ uint8_t } } } - ft_memdel((void*)&str); if (nopt == 0) ft_printf("\n"); return (0); diff --git a/src/p_args.c b/src/p_args.c index 7f5302f..79f66bd 100644 --- a/src/p_args.c +++ b/src/p_args.c @@ -24,18 +24,18 @@ /* TODO: DELETE THIS */ /* ================= */ -/* static void */ -/* p_print(char *words[]) */ -/* { */ -/* char **ptr; */ - -/* ptr = words; */ -/* while (*ptr != NULL) */ -/* { */ -/* ft_printf("[%s]\n", *ptr); */ -/* ptr++; */ -/* } */ -/* } */ +static void +p_print(char *words[]) +{ + char **ptr; + + ptr = words; + while (*ptr != NULL) + { + ft_printf("[%s]\n", *ptr); + ptr++; + } +} /* ================== */ /* TODO: DELETE ABOVE */ @@ -166,6 +166,8 @@ static char return (NULL); } p_args_escape_chars_and_quotes(words); + p_print(words); + exit(0); return (words); } @@ -179,7 +181,6 @@ char { if ((words = p_split_words_no_rdr(word)) == NULL) return (NULL); - exit(0); return (words); } return (words); diff --git a/src/p_args_escape.c b/src/p_args_escape.c index 834d9f7..ed319a1 100644 --- a/src/p_args_escape.c +++ b/src/p_args_escape.c @@ -108,7 +108,7 @@ static void { if (*ptr == C_SQUOTE) mode = p_escape_squote(ptr, &ptr, word, mode); - else if (*ptr == C_DQUOTE) + if (*ptr == C_DQUOTE) mode = p_escape_dquote(ptr, &ptr, word, mode); if (*ptr == C_BACKS) p_escape_bs(ptr, &ptr, word, mode); -- cgit v1.2.3 From 5c08ce82d29f8484ea477f8bb709ac358789c9bb Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Sun, 6 Sep 2020 18:46:43 +0200 Subject: Commit from minishell --- src/p_args.c | 28 ++++++++++++++-------------- src/p_args_escape.c | 11 ++++------- 2 files changed, 18 insertions(+), 21 deletions(-) diff --git a/src/p_args.c b/src/p_args.c index 79f66bd..ae92523 100644 --- a/src/p_args.c +++ b/src/p_args.c @@ -24,18 +24,18 @@ /* TODO: DELETE THIS */ /* ================= */ -static void -p_print(char *words[]) -{ - char **ptr; - - ptr = words; - while (*ptr != NULL) - { - ft_printf("[%s]\n", *ptr); - ptr++; - } -} +/* static void */ +/* p_print(char *words[]) */ +/* { */ +/* char **ptr; */ + +/* ptr = words; */ +/* while (*ptr != NULL) */ +/* { */ +/* ft_printf("[%s]\n", *ptr); */ +/* ptr++; */ +/* } */ +/* } */ /* ================== */ /* TODO: DELETE ABOVE */ @@ -166,8 +166,8 @@ static char return (NULL); } p_args_escape_chars_and_quotes(words); - p_print(words); - exit(0); + /* p_print(words); */ + /* exit(0); */ return (words); } diff --git a/src/p_args_escape.c b/src/p_args_escape.c index ed319a1..abdf3b3 100644 --- a/src/p_args_escape.c +++ b/src/p_args_escape.c @@ -71,13 +71,12 @@ static t_quote_mode } static void - p_escape_bs(char *ptr, char **p, char word[], t_quote_mode mode) + p_escape_bs(char *ptr, char word[], t_quote_mode mode) { if (mode == Q_NONE) { (void)ft_memmove(word + (ptr - word), ptr + 1, (ft_strlen(ptr + 1) + 1) * sizeof(char)); - *(p) -= 1; } else if (mode == Q_DOUBLE) { @@ -85,13 +84,11 @@ static void { (void)ft_memmove(word + (ptr - word), ptr + 1, (ft_strlen(ptr + 1) + 1) * sizeof(char)); - *(p) -= 1; } else if (*(ptr + 1) == C_DQUOTE) { (void)ft_memmove(word + (ptr - word), ptr + 1, (ft_strlen(ptr + 1) + 1) * sizeof(char)); - *(p) -= 1; } } } @@ -108,10 +105,10 @@ static void { if (*ptr == C_SQUOTE) mode = p_escape_squote(ptr, &ptr, word, mode); - if (*ptr == C_DQUOTE) + else if (*ptr == C_DQUOTE) mode = p_escape_dquote(ptr, &ptr, word, mode); - if (*ptr == C_BACKS) - p_escape_bs(ptr, &ptr, word, mode); + else if (*ptr == C_BACKS) + p_escape_bs(ptr, word, mode); ptr++; } } -- cgit v1.2.3 From 4543c3ba3222d47780ad3e091cfe6f3098cc2bca Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Sun, 6 Sep 2020 21:56:20 +0200 Subject: Stacked --- TODO.org | 3 +- src/b_cd.c | 42 +++++++------------- src/b_export.c | 10 ++--- src/b_export_mute.c | 12 +++--- src/b_export_next.c | 9 +++-- src/m_init.c | 18 ++++++--- src/m_loop.c | 20 ++++++---- src/m_mshrc.c | 9 ++--- src/m_prompt.c | 32 +++++++-------- src/p_args.c | 36 ++++++----------- src/p_args.h | 2 +- src/p_lcom.c | 2 +- src/p_lcom.h | 2 +- src/p_lcom_next.c | 93 +++++++++++++++++++++++++++----------------- src/p_lcom_next.h | 2 +- src/s_com.c | 7 ++-- src/s_init.c | 44 ++++++++++----------- src/s_struct.h | 4 +- src/u_vars.c | 109 ++++++++++++++++++---------------------------------- src/u_vars.h | 10 ++++- 20 files changed, 215 insertions(+), 251 deletions(-) diff --git a/TODO.org b/TODO.org index 3eadba8..5db08f1 100644 --- a/TODO.org +++ b/TODO.org @@ -6,7 +6,7 @@ ** DONE [#A] /true/path/to/non-existant/bin/fail-plus-bad-fork ** DONE [#A] fix pwd when cd / ** DONE [#A] Multiline pipes (ls |) -** TODO [#A] Go full tok + quotes +** DONE [#A] Go full tok + quotes ** TODO [#A] Variables quotes qwe="okokoko kkok" asd='bav' ** TODO [#A] ls qweqwe; echo $? <---- substitute ** TODO [#A] Careful p_line \; @@ -17,6 +17,7 @@ ** DONE [#C] fix that goddamn exit(2) 25 leak ** DONE [#C] lpipes leaks on builtins (same as above man) ** DONE [#C] leaks on builtins (same as above man) +** TODO [#C] Variables stack rework ** TODO [#C] Handle memory ** TODO [#C] Go full stack to ken dash in the ass ** TODO [#C] Handle comments better # diff --git a/src/b_cd.c b/src/b_cd.c index e322ef8..0e497aa 100644 --- a/src/b_cd.c +++ b/src/b_cd.c @@ -16,6 +16,7 @@ #include #include "b_export_next.h" +#include "d_define.h" #include "f_fail.h" #include "s_destroy.h" #include "s_struct.h" @@ -23,40 +24,26 @@ #include "u_vars.h" #include "u_vars_next.h" -static void - set_path(char **path, - char *args[], - t_msh *msh) -{ - if ((*path = ft_strdup(*args)) == NULL) - { - f_alloc_and_destroy_msh(msh); - } -} - static void b_set_oldpwd(t_msh *msh) { - char *pwd; - char *tmp; - char fmt[PATH_MAX]; + char pwd[PATH_MAX]; + char tmp[PATH_MAX]; - if ((pwd = u_get_var_value("$PWD", msh)) == NULL) + u_get_var_value(pwd, "$PWD", PATH_MAX, msh); + if (pwd[0] == C_NUL) { - if ((pwd = ft_strdup(msh->cwd)) == NULL) - f_alloc_and_destroy_msh(msh); + ft_strlcpy(pwd, msh->cwd, PATH_MAX); } - if ((tmp = u_get_var_value("$OLDPWD", msh)) == NULL) + u_get_var_value(tmp, "$OLDPWD", PATH_MAX, msh); + if (tmp[0] == C_NUL) { - ft_sprintf(fmt, "%s=%s", "OLDPWD", pwd); - b_export_with_equals(fmt, msh); - ft_memdel((void*)pwd); + ft_sprintf(tmp, "%s=%s", "OLDPWD", pwd); + b_export_with_equals(tmp, msh); } else { u_subst_var_value("$OLDPWD", pwd, msh); - ft_memdel((void*)&pwd); - ft_memdel((void*)&tmp); } } @@ -125,7 +112,7 @@ uint8_t t_msh *msh) { const uint64_t argc = u_builtins_get_argc((const char**)args); - char *path; + char path[PATH_MAX]; if (argc >= 2) { @@ -134,7 +121,8 @@ uint8_t } else if (argc == 0) { - if ((path = u_get_var_value("$HOME", msh)) == NULL) + u_get_var_value(path, "$HOME", PATH_MAX, msh); + if (path[0] == C_NUL) { ft_dprintf(STDERR_FILENO, "minishell: cd: %s\n", FT_FAIL_HOME_NOT_SET); @@ -142,14 +130,12 @@ uint8_t } } else - set_path(&path, args, msh); + ft_strlcpy(path, *args, PATH_MAX); if (chdir(path) != 0) { f_fail_chd("cd", path, msh); - ft_memdel((void*)&path); return (1); } b_upgrade_pwd(path, msh); - ft_memdel((void*)&path); return (0); } diff --git a/src/b_export.c b/src/b_export.c index 9987583..aa0c5d0 100644 --- a/src/b_export.c +++ b/src/b_export.c @@ -84,12 +84,11 @@ void } uint8_t - b_export(char *args[], - t_msh *msh) + b_export(char *args[], t_msh *msh) { /* TODO: norme */ char **ptr; - char *varval; + char varval[4096]; char fmt[4096]; t_bool next; uint8_t r; @@ -111,12 +110,11 @@ uint8_t { next = TRUE; ft_sprintf(fmt, "$%s", *ptr); - varval = u_get_cstm_vr(fmt, msh); - if (varval != NULL) + u_get_custom_var(varval, fmt, 4096, msh); + if (varval[0] != C_NUL) { ft_sprintf(fmt, "%s=%s", *ptr, varval); b_add_to_env_from_globals(*ptr, fmt, msh); - ft_memdel((void*)&varval); } } else if (next == FALSE && check_equals(*ptr) == TRUE) diff --git a/src/b_export_mute.c b/src/b_export_mute.c index 4969320..ac72a7e 100644 --- a/src/b_export_mute.c +++ b/src/b_export_mute.c @@ -16,7 +16,7 @@ #include "b_export.h" #include "b_export_next.h" -#include "d_enum.h" +#include "d_define.h" #include "f_fail.h" #include "s_destroy.h" #include "s_line.h" @@ -26,12 +26,11 @@ #include "u_vars.h" uint8_t - b_export_mute(char *args[], - t_msh *msh) + b_export_mute(char *args[], t_msh *msh) { /* TODO: norme */ char **ptr; - char *varval; + char varval[4096]; char fmt[4096]; t_bool next; uint8_t r; @@ -52,12 +51,11 @@ uint8_t { next = TRUE; ft_sprintf(fmt, "$%s", *ptr); - varval = u_get_cstm_vr(fmt, msh); - if (varval != NULL) + u_get_custom_var(varval, fmt, 4096, msh); + if (varval[0] != C_NUL) { ft_sprintf(fmt, "%s=%s", *ptr, varval); b_add_to_env_from_globals(*ptr, fmt, msh); - ft_memdel((void*)&varval); } } else if (next == FALSE && check_equals(*ptr) == TRUE) diff --git a/src/b_export_next.c b/src/b_export_next.c index c46389a..059fbc6 100644 --- a/src/b_export_next.c +++ b/src/b_export_next.c @@ -92,10 +92,9 @@ static void } void - b_export_with_equals(const char arg[], - t_msh *msh) + b_export_with_equals(const char arg[], t_msh *msh) { - char *varval; + char varval[4096]; char **var; int64_t env_i; @@ -105,8 +104,10 @@ void ft_memdel((void*)&msh->envp[env_i]); if ((msh->envp[env_i] = ft_strdup(arg)) == NULL) f_alloc_and_destroy_msh(msh); + return ; } - else if ((varval = u_get_cstm_vr(var[FT_VAR_NAME], msh)) != NULL) + u_get_custom_var(varval, var[FT_VAR_NAME], 4096, msh); + if (varval[0] != C_NUL) { b_add_to_env(arg, msh); lvars_delone(&msh->vars, var[FT_VAR_NAME] + 1); diff --git a/src/m_init.c b/src/m_init.c index 8b8a994..91baf7c 100644 --- a/src/m_init.c +++ b/src/m_init.c @@ -12,7 +12,9 @@ #include #include +#include +#include "d_define.h" #include "s_struct.h" #include "u_vars.h" #include "u_vars_next.h" @@ -20,17 +22,21 @@ void m_init_custom_vars(t_msh *msh) { - char *home; - char fmt[255]; + char home[PATH_MAX]; + char fmt[PATH_MAX]; u_subst_var_value("$PS1", FT_DEFAULT_PS_ONE, msh); u_subst_var_value("$PS2", FT_DEFAULT_PS_TWO, msh); u_subst_var_value("$PS3", FT_DEFAULT_PS_THR, msh); u_subst_var_value("$PS4", FT_DEFAULT_PS_FOU, msh); - if ((home = u_get_var_value("$HOME", msh)) != NULL) + u_get_var_value(home, "$HISTFILE", PATH_MAX, msh); + if (home[0] == C_NUL) { - ft_sprintf(fmt, "%s/%s", home, FT_DEFAULT_HISTFILE); - u_subst_var_value("$HISTFILE", fmt, msh); - ft_memdel((void*)&home); + u_get_var_value(home, "$HOME", PATH_MAX, msh); + if (home[0] != C_NUL) + { + ft_sprintf(fmt, "%s/%s", home, FT_DEFAULT_HISTFILE); + u_subst_var_value("$HISTFILE", fmt, msh); + } } } diff --git a/src/m_loop.c b/src/m_loop.c index 17668a5..d5a3ceb 100644 --- a/src/m_loop.c +++ b/src/m_loop.c @@ -15,7 +15,9 @@ #include #include #include +#include +#include "d_define.h" #include "e_line.h" #include "m_loop_next.h" #include "m_prompt.h" @@ -36,18 +38,20 @@ void m_dump_hist(t_msh *msh) { int32_t fd; - char *histfile_path; + char histfile[PATH_MAX]; - if (ft_strlen(msh->hist) > 0 && - (histfile_path = u_get_var_value("$HISTFILE", msh)) != NULL) + if (ft_strlen(msh->hist) > 0) { - if ((fd = open(histfile_path, - O_WRONLY | O_CREAT | O_APPEND, 0644)) != -1) + u_get_var_value(histfile, "$HISTFILE", PATH_MAX, msh); + if (histfile[0] != C_NUL) { - write(fd, msh->hist, ft_strlen(msh->hist)); - close(fd); + if ((fd = open(histfile, + O_WRONLY | O_CREAT | O_APPEND, 0644)) != -1) + { + ft_dprintf(fd, "%s", msh->hist); + close(fd); + } } - ft_memdel((void*)&histfile_path); } } diff --git a/src/m_mshrc.c b/src/m_mshrc.c index f730113..7f6b8b9 100644 --- a/src/m_mshrc.c +++ b/src/m_mshrc.c @@ -15,6 +15,7 @@ #include #include #include +#include #include "d_define.h" #include "s_struct.h" @@ -24,15 +25,13 @@ uint8_t m_source_mshrc(t_msh *msh) { + char file[PATH_MAX]; + char home[PATH_MAX]; int32_t fd; - char *home; - char file[2048]; uint8_t ret; - if ((home = u_get_var_value("$HOME", msh)) == NULL) - return (0); + u_get_var_value(home, "$HOME", PATH_MAX, msh); ft_sprintf(file, "%s/%s", home, FT_MINISHELLRC); - ft_memdel((void*)&home); if ((fd = open(file, O_RDONLY)) < 0) return (0); ret = m_loop(fd, msh); diff --git a/src/m_prompt.c b/src/m_prompt.c index 1ea8d70..d0269e4 100644 --- a/src/m_prompt.c +++ b/src/m_prompt.c @@ -13,6 +13,7 @@ #include #include #include +#include #include "d_define.h" #include "m_prompt.h" @@ -20,12 +21,11 @@ #include "u_vars.h" static char - *m_subst_prompt_rice(char var[], - t_msh *msh) + *m_subst_prompt_rice(char var[], t_msh *msh) { + char tmp[PATH_MAX]; size_t i; size_t j; - char *tmp; i = 0; while (var[i] != '\0') @@ -34,19 +34,16 @@ static char { if (var[i + 1] == 'w') { - tmp = u_get_var_value("$PWD", msh); - if (tmp != NULL) - { + u_get_var_value(tmp, "$PWD", PATH_MAX, msh); + if (tmp[0] != C_NUL) var = ft_strsubst(var, "\\w", tmp); - ft_memdel((void*)&tmp); - } else var = ft_strsubst(var, "\\w", "?"); } else if (var[i + 1] == 'W') { - tmp = u_get_var_value("$PWD", msh); - if (tmp != NULL) + u_get_var_value(tmp, "$PWD", PATH_MAX, msh); + if (tmp[0] != C_NUL) { j = ft_strlen(tmp); if (j > 1) @@ -58,7 +55,6 @@ static char ((ft_strlen(tmp) - j) + 1) * sizeof(char)); } var = ft_strsubst(var, "\\W", tmp); - ft_memdel((void*)&tmp); } else var = ft_strsubst(var, "\\w", "?"); @@ -74,26 +70,24 @@ static char } static void - m_update_psx(uint8_t x, - t_msh *msh) + m_update_psx(uint8_t x, t_msh *msh) { - char *var; + char var[2048]; char psx[5]; ft_sprintf(psx, "$PS%hhu", x); - if ((var = u_get_var_value(psx, msh)) == NULL) + u_get_var_value(var, psx, 2048, msh); + if (var[0] == C_NUL) { msh->ps[x - 1][0] = '\0'; return ; } - var = m_subst_prompt_rice(var, msh); + m_subst_prompt_rice(var, msh); ft_strlcpy(msh->ps[x - 1], var, ft_strlen(var) + 1); - ft_memdel((void*)&var); } void - m_prompt_psx(uint8_t x, - t_msh *msh) + m_prompt_psx(uint8_t x, t_msh *msh) { m_update_psx(x, msh); ft_dprintf(STDERR_FILENO, "%s", msh->ps[x - 1]); diff --git a/src/p_args.c b/src/p_args.c index ae92523..b449ba3 100644 --- a/src/p_args.c +++ b/src/p_args.c @@ -20,27 +20,6 @@ #include "p_args_escape.h" #include "u_utils.h" -/* ================= */ -/* TODO: DELETE THIS */ -/* ================= */ - -/* static void */ -/* p_print(char *words[]) */ -/* { */ -/* char **ptr; */ - -/* ptr = words; */ -/* while (*ptr != NULL) */ -/* { */ -/* ft_printf("[%s]\n", *ptr); */ -/* ptr++; */ -/* } */ -/* } */ - -/* ================== */ -/* TODO: DELETE ABOVE */ -/* ================== */ - static void p_meet_bs(char *ptr, t_quote_mode mode) { @@ -166,15 +145,14 @@ static char return (NULL); } p_args_escape_chars_and_quotes(words); - /* p_print(words); */ - /* exit(0); */ return (words); } char - **p_split_args(const char word[], int8_t redir) + **p_split_args(char word[], int8_t redir) { char **words; + size_t i; words = NULL; if (redir == 0) @@ -183,6 +161,16 @@ char return (NULL); return (words); } + i = ft_strlen(word); + while (ft_ischarset("<>", word[i]) == FALSE) + i--; + i--; + while (redir > 0 && ft_isdigit(word[i]) == TRUE) + i--; + word[i] = C_NUL; + ft_printf("[%s]\n", word); + if ((words = p_split_words_no_rdr(word)) == NULL) + return (NULL); return (words); /* char **words; */ /* char *subst; */ diff --git a/src/p_args.h b/src/p_args.h index 899f7ea..fa61d92 100644 --- a/src/p_args.h +++ b/src/p_args.h @@ -15,6 +15,6 @@ #include -char **p_split_args(const char word[], int8_t redir); +char **p_split_args(char word[], int8_t redir); #endif diff --git a/src/p_lcom.c b/src/p_lcom.c index bd5edcb..a8c42ce 100644 --- a/src/p_lcom.c +++ b/src/p_lcom.c @@ -86,7 +86,7 @@ static void } int8_t - get_redir(const char word[], + p_get_redir(const char word[], t_com **com) { /* TODO: norme */ diff --git a/src/p_lcom.h b/src/p_lcom.h index 2ff2c7e..796c684 100644 --- a/src/p_lcom.h +++ b/src/p_lcom.h @@ -17,7 +17,7 @@ #include "s_struct.h" -int8_t get_redir(const char word[], t_com **com); +int8_t p_get_redir(const char word[], t_com **com); int8_t p_lcom(const char line[], t_msh *msh); #endif diff --git a/src/p_lcom_next.c b/src/p_lcom_next.c index 435a710..7bc306e 100644 --- a/src/p_lcom_next.c +++ b/src/p_lcom_next.c @@ -13,72 +13,94 @@ #include #include #include +#include -#include "d_enum.h" +#include "d_define.h" #include "s_destroy.h" #include "f_fail.h" #include "s_struct.h" +#include "u_utils.h" #include "u_vars.h" #include "u_vars_next.h" /* TODO: norme */ -static int8_t - subst_those_vars(int64_t i, - char **p_words, - t_msh *msh) +/* static int8_t */ +/* p_subst_those_vars(int64_t i, */ +/* char **p_words, */ +/* t_msh *msh) */ +/* { */ +/* size_t varlen; */ +/* char *s_varname; */ +/* char *varval; */ + +/* varval = NULL; */ +/* s_varname = NULL; */ +/* varlen = i + 1; */ +/* while ((*p_words)[varlen] != '\0' && */ +/* ft_ischarset("$=/#@%^*+{}[],.-", (*p_words)[varlen]) == FALSE) */ +/* varlen += 1; */ +/* if (!(s_varname = ft_substr(*p_words, (uint32_t)i, varlen - i))) */ +/* return (-1); */ +/* varval = u_get_var_value(s_varname, msh); */ +/* *p_words = ft_strsubst(*p_words, s_varname, varval); */ +/* ft_memdel((void*)&s_varname); */ +/* ft_memdel((void*)&varval); */ +/* return (0); */ +/* } */ + +static char + *p_subst_this_var(int64_t i, char word[], t_msh *msh) { + char tmp[4096]; + char varval[4096]; + char *ptr; size_t varlen; - char *s_varname; - char *varval; - varval = NULL; - s_varname = NULL; + ptr = word; varlen = i + 1; - while ((*p_words)[varlen] != '\0' && - ft_ischarset("$=/#@%^*+{}[],.-", (*p_words)[varlen]) == FALSE) + while (ptr[varlen] != C_NUL && + ft_ischarset("$=\\/@%^*+{}[]<>,.-", ptr[varlen]) == FALSE && + ft_iswhitespace(ptr[varlen]) == FALSE) varlen += 1; - if (!(s_varname = ft_substr(*p_words, (uint32_t)i, varlen - i))) - return (-1); - varval = u_get_var_value(s_varname, msh); - *p_words = ft_strsubst(*p_words, s_varname, varval); - ft_memdel((void*)&s_varname); - ft_memdel((void*)&varval); - return (0); + ft_strlcpy(tmp, ptr + i, varlen + 1 - i); + u_get_var_value(varval, tmp, 4096, msh); + ft_strlcpy(tmp, ptr + varlen + 1, varlen); + word = ft_nrealloc(word, i, i + ft_strlen(varval) + ft_strlen(tmp) + 1); + ft_strlcpy(word + i, varval, ft_strlen(varval) + 1); + ft_printf("[%s]\n", tmp); + ft_strlcpy(word + i + ft_strlen(varval), tmp, ft_strlen(tmp) + 1); + exit(0); + return (word); } char - **p_subst_vars(char *words[], - t_msh *msh) + *p_subst_vars(char word[], t_msh *msh) { - char **p_words; - int64_t i; + char *ptr; - p_words = words; - i = 0; - while (*p_words) + ptr = word; + while (*ptr != C_NUL) { - while ((i = ft_strlchr((*p_words), '$')) != -1) + if (*ptr == '$' && u_is_not_escaped(word, ptr) == TRUE) { - if (*(*p_words) + i - 1 != '\\') - { - if (subst_those_vars(i, p_words, msh) != 0) - return (NULL); - } + if ((word = p_subst_this_var((ptr - word), word, msh)) == NULL) + return (NULL); } - p_words += 1; + ptr++; } - return (words); + return (word); } char **p_subst_home(char *words[], t_msh *msh) { - char *path; + char path[PATH_MAX]; char **ptr; - if ((path = u_get_var_value("$HOME", msh)) == NULL) + u_get_var_value(path, "$HOME", PATH_MAX, msh); + if (path[0] == C_NUL) return (words); ptr = words; while (*ptr != NULL) @@ -89,7 +111,6 @@ char } ptr++; } - ft_memdel((void*)&path); return (words); } diff --git a/src/p_lcom_next.h b/src/p_lcom_next.h index dea86bd..8fbbb99 100644 --- a/src/p_lcom_next.h +++ b/src/p_lcom_next.h @@ -17,7 +17,7 @@ #include "s_struct.h" -char **p_subst_vars(char *words[], t_msh *msh); +char *p_subst_vars(char word[], t_msh *msh); char **p_subst_args(const char word[], int8_t redir); char **p_subst_home(char *word[], t_msh *msh); char **p_check_args_equals(char *words[], t_msh *msh); diff --git a/src/s_com.c b/src/s_com.c index 99b3158..ac23f35 100644 --- a/src/s_com.c +++ b/src/s_com.c @@ -112,12 +112,11 @@ t_com com->rdrfd = 0; com->rdrpath = NULL; com->env_fork = NULL; - if (get_redir(word, &com) != 0) + if (p_get_redir(word, &com) != 0) return (NULL); - if ((words = p_split_args(word, com->redir)) == NULL) + if ((word = p_subst_vars(word, msh)) == NULL) return (NULL); - /* TODO: subst vars is before getting words fuck my life */ - if ((words = p_subst_vars(words, msh)) == NULL) + if ((words = p_split_args(word, com->redir)) == NULL) return (NULL); if ((words = p_subst_home(words, msh)) == NULL) return (NULL); diff --git a/src/s_init.c b/src/s_init.c index 7353ed8..8fc8a79 100644 --- a/src/s_init.c +++ b/src/s_init.c @@ -107,64 +107,60 @@ static char static void inc_shlvl(t_msh *msh) { - int32_t shlvl; - char *str_one; - char *str_two; - char *tmp; char fmt[8]; + char tmp[255]; + char *str; + int32_t shlvl; - if ((tmp = u_get_var_value("$SHLVL", msh)) == NULL) + u_get_var_value(tmp, "$SHLVL", 255, msh); + if (tmp[0] == C_NUL) { ft_memcpy(fmt, "SHLVL=1", 8); b_export_with_equals(fmt, msh); } else { - ft_memdel((void*)&tmp); - str_two = u_get_var_value("$SHLVL", msh); - shlvl = ft_atoi(str_two); + shlvl = ft_atoi(tmp); if (shlvl >= 999) f_shlvl_too_high(shlvl); shlvl = (shlvl >= 999) ? 0 : shlvl; shlvl = (shlvl < 0) ? 0 : shlvl + 1; - str_one = ft_itoa(shlvl); - u_subst_var_value("$SHLVL", str_one, msh); - ft_memdel((void*)&str_one); - ft_memdel((void*)&str_two); + str = ft_itoa(shlvl); + u_subst_var_value("$SHLVL", str, msh); + ft_memdel((void*)&str); } } -static char - *set_cwd(t_msh *msh) +static void + set_cwd(char cwd[], t_msh *msh) { - char *cwd; char fmt[PATH_MAX]; DIR *dir; - if ((cwd = u_get_var_value("$PWD", msh)) == NULL) + u_get_var_value(cwd, "$PWD", PATH_MAX, msh); + if (cwd[0] == C_NUL) { - cwd = getcwd(NULL, 0); + getcwd(cwd, PATH_MAX); ft_sprintf(fmt, "%s=%s", "PWD", cwd); b_export_with_equals(fmt, msh); - return (cwd); + return ; } if ((dir = opendir(cwd)) != NULL) closedir(dir); else if (errno == ENOENT) { - ft_memdel((void*)&cwd); - cwd = getcwd(NULL, 0); + getcwd(cwd, PATH_MAX); ft_sprintf(fmt, "%s=%s", "PWD", cwd); b_export_with_equals(fmt, msh); - return (cwd); + return ; } - return (cwd); } t_msh *init_msh(char *const argv[], char *const envp[]) { + char cwd[PATH_MAX]; t_msh *msh; if (!(msh = (t_msh*)malloc(sizeof(t_msh)))) @@ -179,7 +175,9 @@ t_msh init_buptr(msh); msh->curr = NULL; msh->vars = NULL; - msh->cwd = set_cwd(msh); + set_cwd(cwd, msh); + if ((msh->cwd = ft_strdup(cwd)) == NULL) + return (NULL); msh->env_fork_tmp[0][0] = '\0'; inc_shlvl(msh); s_init_sqb_ref_one(msh); diff --git a/src/s_struct.h b/src/s_struct.h index 01c8f1f..a1d1828 100644 --- a/src/s_struct.h +++ b/src/s_struct.h @@ -43,7 +43,7 @@ typedef struct s_com int8_t redir; } t_com; -struct s_lpipes +struct s_lpipes { struct s_com *com; struct s_lpipes *next; @@ -61,8 +61,8 @@ typedef struct s_line { struct s_com *com; struct s_lpipes *pipes; - struct s_line *next; uint8_t nextif; + struct s_line *next; } t_line; typedef struct s_msh diff --git a/src/u_vars.c b/src/u_vars.c index 9594036..95c6ffa 100644 --- a/src/u_vars.c +++ b/src/u_vars.c @@ -21,26 +21,11 @@ #include "s_struct.h" #include "u_utils.h" -static char - *u_dup_env(char *p_env, - t_msh *msh) -{ - char *varval; - - if (!(varval = ft_strdup(p_env))) - { - f_alloc_and_destroy_msh(msh); - } - return (varval); -} - -static char - *u_get_frm_env(const char varname[], - t_msh *msh) +static void + u_get_frm_env(char str[], const char varname[], size_t dstsize, t_msh *msh) { char **env_dup; char *pp_env; - char *varval; size_t i; env_dup = u_get_env_var_names(msh); @@ -54,47 +39,38 @@ static char pp_env += 1; if (*pp_env == '=') pp_env += 1; - varval = u_dup_env(pp_env, msh); + ft_strlcpy(str, pp_env, dstsize); ft_delwords(env_dup); - return (varval); + return ; } i++; } ft_delwords(env_dup); - return (NULL); } -static char - *u_get_special_var(const char varname[], +static void + u_get_special_var(char str[], + const char varname[], + size_t dstsize, t_msh *msh) { - char *varval; - if (ft_strncmp(varname, FT_RET_VAR, 3) == 0) { - if ((varval = ft_uitoa(msh->ret)) == NULL) - { - f_alloc_and_destroy_msh(msh); - } - return (varval); + ft_strlcpy(str, ft_uitoa(msh->ret), 4); } else if (ft_strncmp(varname, FT_ZER_VAR, 3) == 0) { - if ((varval = ft_strdup(msh->shname)) == NULL) - { - f_alloc_and_destroy_msh(msh); - } - return (varval); + ft_strlcpy(str, msh->shname, dstsize); } - return (NULL); } -char - *u_get_cstm_vr(const char varname[], - t_msh *msh) +void + u_get_custom_var(char str[], + const char varname[], + size_t dstsize, + t_msh *msh) { t_lvars *ptr; - char *varval; ptr = msh->vars; while (ptr != NULL && @@ -104,51 +80,40 @@ char } if (ptr != NULL) { - if (!(varval = ft_strdup(ptr->val))) - { - f_alloc_and_destroy_msh(msh); - } - return (varval); - } - else - { - return (NULL); + ft_strlcpy(str, ptr->val, dstsize); } } /* -** char* -** u_get_var_value(const char varname[], t_msh *msh); +** void +** u_get_var_value(char str[], const char varname[], size_t dstsize, t_msh *msh) ** ** DESCRIPTION -** The u_get_var_value() function returns -** a heap-allocated, null-terminated string -** that may later be free'd containing the -** value of the variable varname[] including -** the '$' prefix. NULL is returned if varname[] -** wasn't found. +** The u_get_var_value() function fills +** str[] with a null-terminated string +** containing the value of the variable varname[] +** including the '$' prefix, in a similar way as +** sprintf does. No more than dstsize bytes will be +** copied into str[]. str[0] is set to \000 if +** varname[] wasn't found. */ -char - *u_get_var_value(const char varname[], +void + u_get_var_value(char str[], + const char varname[], + size_t dstsize, t_msh *msh) { - /* TODO: check behaviour on empty vars -> "QWE=" */ - char *varval; - - varval = NULL; - if ((varval = u_get_special_var(varname, msh)) != NULL) - { - return (varval); - } - else if ((varval = u_get_cstm_vr(varname, msh)) != NULL) + str[0] = C_NUL; + u_get_special_var(str, varname, dstsize, msh); + if (str[0] != C_NUL) { - return (varval); + return ; } - else + u_get_custom_var(str, varname, dstsize, msh); + if (str[0] != C_NUL) { - varval = u_get_frm_env(varname, msh); - return (varval); + return ; } - return (NULL); + u_get_frm_env(str, varname, dstsize, msh); } diff --git a/src/u_vars.h b/src/u_vars.h index 0d83b4b..fbdaffc 100644 --- a/src/u_vars.h +++ b/src/u_vars.h @@ -15,7 +15,13 @@ #include "s_struct.h" -char *u_get_cstm_vr(const char varname[], t_msh *msh); -char *u_get_var_value(const char varname[], t_msh *msh); +void u_get_custom_var(char str[], + const char varname[], + size_t dstsize, + t_msh *msh); +void u_get_var_value(char str[], + const char varname[], + size_t dstsize, + t_msh *msh); #endif -- cgit v1.2.3 From 1f867b196a83d0aeaf67d431a0fc5271e01a310e Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Sun, 6 Sep 2020 22:02:47 +0200 Subject: Cool --- src/p_lcom_next.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/p_lcom_next.c b/src/p_lcom_next.c index 7bc306e..7ae0331 100644 --- a/src/p_lcom_next.c +++ b/src/p_lcom_next.c @@ -65,11 +65,11 @@ static char varlen += 1; ft_strlcpy(tmp, ptr + i, varlen + 1 - i); u_get_var_value(varval, tmp, 4096, msh); - ft_strlcpy(tmp, ptr + varlen + 1, varlen); + ft_strlcpy(tmp, ptr + varlen, varlen); word = ft_nrealloc(word, i, i + ft_strlen(varval) + ft_strlen(tmp) + 1); ft_strlcpy(word + i, varval, ft_strlen(varval) + 1); - ft_printf("[%s]\n", tmp); ft_strlcpy(word + i + ft_strlen(varval), tmp, ft_strlen(tmp) + 1); + ft_printf("[%s]\n", word); exit(0); return (word); } -- cgit v1.2.3 From 6805673bff96379c03ba32e681f8fb40aad52adc Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Sun, 6 Sep 2020 22:12:15 +0200 Subject: Improvement --- TODO.org | 2 +- src/m_loop.c | 2 +- src/p_lcom_next.c | 32 ++++---------------------------- src/s_init.c | 3 ++- 4 files changed, 8 insertions(+), 31 deletions(-) diff --git a/TODO.org b/TODO.org index 5db08f1..c19ad6c 100644 --- a/TODO.org +++ b/TODO.org @@ -17,7 +17,7 @@ ** DONE [#C] fix that goddamn exit(2) 25 leak ** DONE [#C] lpipes leaks on builtins (same as above man) ** DONE [#C] leaks on builtins (same as above man) -** TODO [#C] Variables stack rework +** DONE [#C] Variables stack rework ** TODO [#C] Handle memory ** TODO [#C] Go full stack to ken dash in the ass ** TODO [#C] Handle comments better # diff --git a/src/m_loop.c b/src/m_loop.c index d5a3ceb..0a810a0 100644 --- a/src/m_loop.c +++ b/src/m_loop.c @@ -40,7 +40,7 @@ void int32_t fd; char histfile[PATH_MAX]; - if (ft_strlen(msh->hist) > 0) + if (msh->hist[0] != C_NUL && ft_strlen(msh->hist) > 0) { u_get_var_value(histfile, "$HISTFILE", PATH_MAX, msh); if (histfile[0] != C_NUL) diff --git a/src/p_lcom_next.c b/src/p_lcom_next.c index 7ae0331..2f675f3 100644 --- a/src/p_lcom_next.c +++ b/src/p_lcom_next.c @@ -25,32 +25,8 @@ /* TODO: norme */ -/* static int8_t */ -/* p_subst_those_vars(int64_t i, */ -/* char **p_words, */ -/* t_msh *msh) */ -/* { */ -/* size_t varlen; */ -/* char *s_varname; */ -/* char *varval; */ - -/* varval = NULL; */ -/* s_varname = NULL; */ -/* varlen = i + 1; */ -/* while ((*p_words)[varlen] != '\0' && */ -/* ft_ischarset("$=/#@%^*+{}[],.-", (*p_words)[varlen]) == FALSE) */ -/* varlen += 1; */ -/* if (!(s_varname = ft_substr(*p_words, (uint32_t)i, varlen - i))) */ -/* return (-1); */ -/* varval = u_get_var_value(s_varname, msh); */ -/* *p_words = ft_strsubst(*p_words, s_varname, varval); */ -/* ft_memdel((void*)&s_varname); */ -/* ft_memdel((void*)&varval); */ -/* return (0); */ -/* } */ - static char - *p_subst_this_var(int64_t i, char word[], t_msh *msh) + *p_subst_this_var(char **p, int64_t i, char word[], t_msh *msh) { char tmp[4096]; char varval[4096]; @@ -68,9 +44,9 @@ static char ft_strlcpy(tmp, ptr + varlen, varlen); word = ft_nrealloc(word, i, i + ft_strlen(varval) + ft_strlen(tmp) + 1); ft_strlcpy(word + i, varval, ft_strlen(varval) + 1); - ft_strlcpy(word + i + ft_strlen(varval), tmp, ft_strlen(tmp) + 1); + ft_strlcpy(word + (i + ft_strlen(varval)), tmp, ft_strlen(tmp) + 1); ft_printf("[%s]\n", word); - exit(0); + *(p) = word + (i + ft_strlen(varval) - 1); return (word); } @@ -84,7 +60,7 @@ char { if (*ptr == '$' && u_is_not_escaped(word, ptr) == TRUE) { - if ((word = p_subst_this_var((ptr - word), word, msh)) == NULL) + if ((word = p_subst_this_var(&ptr, (ptr - word), word, msh)) == NULL) return (NULL); } ptr++; diff --git a/src/s_init.c b/src/s_init.c index 8fc8a79..55f9cf0 100644 --- a/src/s_init.c +++ b/src/s_init.c @@ -169,12 +169,13 @@ t_msh return (NULL); /* TODO: shname: care about "./", try with symlinks */ msh->envp = NULL; - if (!(msh->envp = dupenv(envp))) + if ((msh->envp = dupenv(envp)) == NULL) return (NULL); msh->ret = 0; init_buptr(msh); msh->curr = NULL; msh->vars = NULL; + msh->hist[0] = '\0'; set_cwd(cwd, msh); if ((msh->cwd = ft_strdup(cwd)) == NULL) return (NULL); -- cgit v1.2.3 From d5cd448de1e1c9ced57cfbf8f241ab5b68c855b3 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Sun, 6 Sep 2020 22:16:20 +0200 Subject: qweqwe --- src/p_args.c | 1 - src/p_lcom_next.c | 1 - 2 files changed, 2 deletions(-) diff --git a/src/p_args.c b/src/p_args.c index b449ba3..383700f 100644 --- a/src/p_args.c +++ b/src/p_args.c @@ -168,7 +168,6 @@ char while (redir > 0 && ft_isdigit(word[i]) == TRUE) i--; word[i] = C_NUL; - ft_printf("[%s]\n", word); if ((words = p_split_words_no_rdr(word)) == NULL) return (NULL); return (words); diff --git a/src/p_lcom_next.c b/src/p_lcom_next.c index 2f675f3..dc834a8 100644 --- a/src/p_lcom_next.c +++ b/src/p_lcom_next.c @@ -45,7 +45,6 @@ static char word = ft_nrealloc(word, i, i + ft_strlen(varval) + ft_strlen(tmp) + 1); ft_strlcpy(word + i, varval, ft_strlen(varval) + 1); ft_strlcpy(word + (i + ft_strlen(varval)), tmp, ft_strlen(tmp) + 1); - ft_printf("[%s]\n", word); *(p) = word + (i + ft_strlen(varval) - 1); return (word); } -- cgit v1.2.3 From 0944b3e39666f9ec14e23347bd17852a781a629c Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Sun, 6 Sep 2020 22:17:36 +0200 Subject: In progress --- src/p_lcom_next.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/p_lcom_next.c b/src/p_lcom_next.c index dc834a8..16d8aac 100644 --- a/src/p_lcom_next.c +++ b/src/p_lcom_next.c @@ -60,7 +60,9 @@ char if (*ptr == '$' && u_is_not_escaped(word, ptr) == TRUE) { if ((word = p_subst_this_var(&ptr, (ptr - word), word, msh)) == NULL) + { return (NULL); + } } ptr++; } -- cgit v1.2.3 From d0038f19a7cb23749588b72c9febcf114d9d31e9 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Tue, 8 Sep 2020 18:31:51 +0200 Subject: static hist, words fix --- TODO.org | 2 +- src/m_loop.c | 31 ++++++++++++++++--------------- src/p_args.c | 5 +++-- src/p_args_next.c | 5 ++--- src/p_line.c | 8 ++++++-- src/s_init.c | 1 - src/s_struct.h | 1 - 7 files changed, 28 insertions(+), 25 deletions(-) diff --git a/TODO.org b/TODO.org index c19ad6c..4746863 100644 --- a/TODO.org +++ b/TODO.org @@ -21,7 +21,7 @@ ** TODO [#C] Handle memory ** TODO [#C] Go full stack to ken dash in the ass ** TODO [#C] Handle comments better # -** TODO [#C] msh ~> ./qwe.sh <---- without shebang (maybe works find already) +** TODO [#C] msh ~> ./qwe.sh <---- without shebang (maybe works fine already) * Stuff to add ** DONE [#A] builtins to pipes diff --git a/src/m_loop.c b/src/m_loop.c index 0a810a0..7dc19e0 100644 --- a/src/m_loop.c +++ b/src/m_loop.c @@ -35,12 +35,12 @@ static void } void - m_dump_hist(t_msh *msh) + m_dump_hist(char hist[], t_msh *msh) { - int32_t fd; char histfile[PATH_MAX]; + int32_t fd; - if (msh->hist[0] != C_NUL && ft_strlen(msh->hist) > 0) + if (hist[0] != C_NUL && ft_strlen(hist) > 0) { u_get_var_value(histfile, "$HISTFILE", PATH_MAX, msh); if (histfile[0] != C_NUL) @@ -48,7 +48,7 @@ void if ((fd = open(histfile, O_WRONLY | O_CREAT | O_APPEND, 0644)) != -1) { - ft_dprintf(fd, "%s", msh->hist); + ft_dprintf(fd, "%s", hist); close(fd); } } @@ -56,19 +56,19 @@ void } static void - m_handle_hist(char line[], t_msh *msh) + m_handle_hist(char hist[], char line[], t_msh *msh) { static uint16_t hist_i = 0; if (hist_i == 0) - msh->hist[0] = '\0'; - ft_strlcpy(msh->hist + ft_strlen(msh->hist), line, 4096); - msh->hist[ft_strlen(msh->hist) + 1] = '\0'; - msh->hist[ft_strlen(msh->hist)] = '\n'; + hist[0] = '\0'; + ft_strlcpy(hist + ft_strlen(hist), line, 4096); + hist[ft_strlen(hist) + 1] = '\0'; + hist[ft_strlen(hist)] = '\n'; hist_i += 1; - if (hist_i == 255) + if (hist_i == 254) { - m_dump_hist(msh); + m_dump_hist(hist, msh); hist_i = 0; } } @@ -76,8 +76,9 @@ static void uint8_t m_loop(int32_t fd, t_msh *msh) { - char *line; - int8_t gnl; + static char hist[255 * 4096]; + char *line; + int8_t gnl; msh->fd = fd; gnl = 1; @@ -93,7 +94,7 @@ uint8_t line = m_check_multi_and(fd, line, msh); line = m_check_multi_quotes(fd, line, msh); if (fd == STDIN_FILENO) - m_handle_hist(line, msh); + m_handle_hist(hist, line, msh); m_parse_and_run_line(line, msh); /* TODO: (null): Bad address on "msh ~> echo a > asd; cat < asd" but not on "msh ~> echo a > asd; cat asd" */ } @@ -101,6 +102,6 @@ uint8_t ft_memdel((void*)&line); } if (fd == STDIN_FILENO) - m_dump_hist(msh); + m_dump_hist(hist, msh); return (msh->ret); } diff --git a/src/p_args.c b/src/p_args.c index 383700f..78cfc06 100644 --- a/src/p_args.c +++ b/src/p_args.c @@ -116,9 +116,10 @@ static uint16_t if (ft_iswhitespace(*ptr) && p_meet_whitespace((char*)word, ptr, mode) == TRUE) { - count += 1; ptr = p_skip_whitespace(ptr); - start[count - 1] = (ptr - word); + start[count] = (ptr - word); + if (*ptr != C_NUL) + count += 1; ptr -= 1; } ptr++; diff --git a/src/p_args_next.c b/src/p_args_next.c index 77f8ced..6fd6b08 100644 --- a/src/p_args_next.c +++ b/src/p_args_next.c @@ -25,8 +25,8 @@ static char { tmp[0] = '\0'; ft_strlcpy(tmp, - word + start[i], - (p_arg_len(word, start[i]) - start[i]) + 1); + word + start[i], + (p_arg_len(word, start[i]) - start[i]) + 1); return (tmp); } @@ -63,4 +63,3 @@ uint16_t } return (i); } - diff --git a/src/p_line.c b/src/p_line.c index 9c96610..170d8a8 100644 --- a/src/p_line.c +++ b/src/p_line.c @@ -19,6 +19,7 @@ #include "p_lcom.h" #include "s_destroy.h" #include "s_struct.h" +#include "u_utils.h" static void p_delete_comments(char line[]) @@ -30,8 +31,11 @@ static void { if (*ptr == '#') { - *ptr = '\0'; - return ; + if ((ptr - line) == 0 || ft_iswhitespace(*(ptr - 1)) == TRUE) + { + *ptr = '\0'; + return ; + } } ptr++; } diff --git a/src/s_init.c b/src/s_init.c index 55f9cf0..9ef856f 100644 --- a/src/s_init.c +++ b/src/s_init.c @@ -175,7 +175,6 @@ t_msh init_buptr(msh); msh->curr = NULL; msh->vars = NULL; - msh->hist[0] = '\0'; set_cwd(cwd, msh); if ((msh->cwd = ft_strdup(cwd)) == NULL) return (NULL); diff --git a/src/s_struct.h b/src/s_struct.h index a1d1828..1089dc6 100644 --- a/src/s_struct.h +++ b/src/s_struct.h @@ -74,7 +74,6 @@ typedef struct s_msh char ps[4][1024]; char env_fork_tmp[128][1024]; char sqb_ref[FT_ID_SQB_COUNT][4]; - char hist[1037595]; char *shname; char *cwd; int32_t fd; -- cgit v1.2.3 From 4dd10e6f4de22446ea84d7b194d2a18cb6e43c6c Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Tue, 8 Sep 2020 18:50:11 +0200 Subject: Names --- src/m_loop.c | 2 +- src/p_lblock.c | 167 +++++++++++++++++++++++++++++++++++++++ src/p_lblock.h | 23 ++++++ src/p_lblock_next.c | 223 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/p_lblock_next.h | 25 ++++++ src/p_lcom.c | 167 --------------------------------------- src/p_lcom.h | 23 ------ src/p_lcom_next.c | 223 ---------------------------------------------------- src/p_lcom_next.h | 25 ------ src/p_line.c | 2 +- src/s_init.c | 2 + src/s_struct.h | 73 ++++++++--------- 12 files changed, 479 insertions(+), 476 deletions(-) create mode 100644 src/p_lblock.c create mode 100644 src/p_lblock.h create mode 100644 src/p_lblock_next.c create mode 100644 src/p_lblock_next.h delete mode 100644 src/p_lcom.c delete mode 100644 src/p_lcom.h delete mode 100644 src/p_lcom_next.c delete mode 100644 src/p_lcom_next.h diff --git a/src/m_loop.c b/src/m_loop.c index 7dc19e0..9d00f2b 100644 --- a/src/m_loop.c +++ b/src/m_loop.c @@ -87,7 +87,7 @@ uint8_t if (fd == STDIN_FILENO) m_prompt_psx(1, msh); gnl = get_next_line(fd, &line); - if (line[0] != '\0') + if (line[0] != C_NUL) { line = m_check_multi_backslash(fd, line, msh); line = m_check_multi_pipe(fd, line, msh); diff --git a/src/p_lblock.c b/src/p_lblock.c new file mode 100644 index 0000000..a8c42ce --- /dev/null +++ b/src/p_lblock.c @@ -0,0 +1,167 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* p_lcom.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include +#include +#include +#include + +#include "d_define.h" +#include "f_fail.h" +#include "p_split.h" +#include "s_line.h" +#include "s_lpipes.h" +#include "s_struct.h" + +static void + rdr_err_check(char *ptr, + t_com **com) +{ + if ((*com)->redir == -1 && ft_ischarset("><", *(ptr + 1)) == TRUE) + { + /* TODO: syntax err */ + } + else if ((*com)->redir == 1 && ft_ischarset("<", *(ptr + 1)) == TRUE) + { + /* TODO: syntax err */ + } + else if ((*com)->redir == 2 && ft_ischarset("<>", *(ptr + 1)) == TRUE) + { + /* TODO: syntax err */ + } +} + +static int8_t + get_rdrpath(char *ptr, + t_com **com) +{ + char *p_rdrpath; + + ptr += ((*com)->redir == 2) ? (2) : (1); + if (!((*com)->rdrpath = + (char*)malloc((ft_strlen(ptr) + 1) * sizeof(char)))) + { + return (-1); + } + p_rdrpath = (*com)->rdrpath; + while (*ptr) + { + if (*ptr != ' ') + { + *p_rdrpath = *ptr; + p_rdrpath++; + } + ptr++; + } + *p_rdrpath = '\0'; + return (0); +} + +static void + get_rdrfd(const char *ptr, + t_com **com) +{ + while (ft_isdigit(*ptr) == TRUE) + { + ptr--; + } + if (*ptr != ' ') + (*com)->rdrfd = STDOUT_FILENO; + else + { + ptr += 1; + (*com)->rdrfd = ft_atoi(ptr); + } +} + +int8_t + p_get_redir(const char word[], + t_com **com) +{ + /* TODO: norme */ + char *ptr; + + ptr = (char *)word; + while (*ptr) + { + if (*ptr == '<') + { + (*com)->redir = -1; + break ; + } + if (*ptr == '>') + { + (*com)->redir = (*(ptr + 1) == '>') ? (2) : (1); + break ; + } + ptr++; + /* TODO: handle correctly multiples "msh ~> echo qwe > qwe > asd >> zxc > qweasdzxc" */ + /* hint: bash only handles the last one */ + /* TODO: handle "msh ~> cat < Makefile >qwe" | gl hf */ + } + if ((*com)->redir > 0) + { + if (ft_isdigit(*(ptr - 1)) == TRUE) + get_rdrfd(ptr - 1, com); + else + (*com)->rdrfd = STDOUT_FILENO; + rdr_err_check(ptr, com); + if (get_rdrpath(ptr, com) != 0) + return (-1); + } + return (0); +} + +int8_t + p_lcom(const char line[], + t_msh *msh) +{ + /* TODO: norme */ + uint64_t i; + t_line *link; + char **words; + char *ptr; + uint8_t nextif; + t_bool next; + + i = 0; + if ((words = p_split_line(line)) == NULL) + return (-1); + while (words[i] != NULL) + { + if (words[i][ft_strlen(words[i]) - 1] == ';') + nextif = 0; + else if (words[i][ft_strlen(words[i]) - 1] == '&') + nextif = 1; + else + nextif = 2; + words[i][ft_strlen(words[i]) - 1] = '\0'; + next = FALSE; + if ((ptr = ft_strchr(words[i], '|')) != NULL) + { + if ((link = s_line_new(NULL, msh)) == NULL) + return (-1); + if ((s_split_pipes(words[i], link, msh)) == NULL) + return (-1); + next = TRUE; + } + if (next == FALSE && (link = s_line_new(words[i], msh)) == NULL) + return (-1); + link->nextif = nextif; + s_line_add_back(&msh->curr, link); + i++; + } + ft_delwords(words); + return (0); +} diff --git a/src/p_lblock.h b/src/p_lblock.h new file mode 100644 index 0000000..796c684 --- /dev/null +++ b/src/p_lblock.h @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* p_lcom.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef P_LCOM_H +#define P_LCOM_H + +#include + +#include "s_struct.h" + +int8_t p_get_redir(const char word[], t_com **com); +int8_t p_lcom(const char line[], t_msh *msh); + +#endif diff --git a/src/p_lblock_next.c b/src/p_lblock_next.c new file mode 100644 index 0000000..16d8aac --- /dev/null +++ b/src/p_lblock_next.c @@ -0,0 +1,223 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* p_line_next.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include +#include + +#include "d_define.h" +#include "s_destroy.h" +#include "f_fail.h" +#include "s_struct.h" +#include "u_utils.h" +#include "u_vars.h" +#include "u_vars_next.h" + +/* TODO: norme */ + +static char + *p_subst_this_var(char **p, int64_t i, char word[], t_msh *msh) +{ + char tmp[4096]; + char varval[4096]; + char *ptr; + size_t varlen; + + ptr = word; + varlen = i + 1; + while (ptr[varlen] != C_NUL && + ft_ischarset("$=\\/@%^*+{}[]<>,.-", ptr[varlen]) == FALSE && + ft_iswhitespace(ptr[varlen]) == FALSE) + varlen += 1; + ft_strlcpy(tmp, ptr + i, varlen + 1 - i); + u_get_var_value(varval, tmp, 4096, msh); + ft_strlcpy(tmp, ptr + varlen, varlen); + word = ft_nrealloc(word, i, i + ft_strlen(varval) + ft_strlen(tmp) + 1); + ft_strlcpy(word + i, varval, ft_strlen(varval) + 1); + ft_strlcpy(word + (i + ft_strlen(varval)), tmp, ft_strlen(tmp) + 1); + *(p) = word + (i + ft_strlen(varval) - 1); + return (word); +} + +char + *p_subst_vars(char word[], t_msh *msh) +{ + char *ptr; + + ptr = word; + while (*ptr != C_NUL) + { + if (*ptr == '$' && u_is_not_escaped(word, ptr) == TRUE) + { + if ((word = p_subst_this_var(&ptr, (ptr - word), word, msh)) == NULL) + { + return (NULL); + } + } + ptr++; + } + return (word); +} + +char + **p_subst_home(char *words[], + t_msh *msh) +{ + char path[PATH_MAX]; + char **ptr; + + u_get_var_value(path, "$HOME", PATH_MAX, msh); + if (path[0] == C_NUL) + return (words); + ptr = words; + while (*ptr != NULL) + { + if (*ptr[0] == '~') + { + *ptr = ft_strsubst(*ptr, "~", path); + } + ptr++; + } + return (words); +} + +static void + p_register_word(char word[], + t_msh *msh) +{ + char name[255]; + char val[255]; + char *ptr; + size_t i; + + name[0] = '$'; + ptr = word; + i = 1; + while (*ptr != '=' && *ptr != '\0') + { + name[i] = *ptr; + i++; + ptr++; + } + name[i] = '\0'; + ptr++; + i = 0; + while (*ptr != '\0') + { + val[i] = *ptr; + i++; + ptr++; + } + val[i] = '\0'; + u_subst_var_value(name, val, msh); +} + +static char + **p_add_to_variables_and_delete(char *words[], + t_bool reg, + int64_t i, + t_msh *msh) +{ + int64_t j; + int64_t k; + char **rewords; + + j = 0; + if (reg == TRUE) + { + while (words[j] && j < i) + { + p_register_word(words[j], msh); + j++; + } + } + j = 0; + while (words[i + j] != NULL) + j++; + if (!(rewords = (char**)malloc((j + 1) * sizeof(char*)))) + { + ft_delwords(words); + f_alloc_and_destroy_msh(msh); + } + k = i; + while (i - k < j) + { + if ((rewords[i - k] = ft_strdup(words[i])) == NULL) + { + ft_delwords(words); + f_alloc_and_destroy_msh(msh); + } + i++; + } + rewords[i - k] = 0; + ft_delwords(words); + i++; + return (rewords); +} + +static void + p_add_to_env_fork(int64_t i, + char *words[], + t_msh *msh) +{ + int64_t j; + + j = 0; + while(j < i) + { + ft_strlcpy(msh->env_fork_tmp[j], words[j], ft_strlen(words[j]) + 1); + j++; + } + msh->env_fork_tmp[j][0] = '\0'; +} + +char + **p_check_args_equals(char *words[], + t_msh *msh) +{ + char *ptr; + t_bool reg; + t_bool isvar; + int64_t i; + + i = 0; + reg = FALSE; + isvar = FALSE; + while (words[i]) + { + ptr = words[i]; + while (*ptr != '\0' && *ptr != '=') + ptr++; + if (*ptr == '=') + { + reg = TRUE; + isvar = TRUE; + } + if (*ptr == '\0' || words[i][0] == '=' || + ft_isdigit(words[i][0]) == TRUE) + { + reg = FALSE; + if (i == 0) + isvar = FALSE; + if (isvar == TRUE) + p_add_to_env_fork(i, words, msh); + else + msh->env_fork_tmp[0][0] = '\0'; + break ; + } + i++; + } + if (isvar == TRUE) + return (p_add_to_variables_and_delete(words, reg, i, msh)); + return (words); +} diff --git a/src/p_lblock_next.h b/src/p_lblock_next.h new file mode 100644 index 0000000..8fbbb99 --- /dev/null +++ b/src/p_lblock_next.h @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* p_line_next.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef P_LCOM_NEXT_H +#define P_LCOM_NEXT_H + +#include + +#include "s_struct.h" + +char *p_subst_vars(char word[], t_msh *msh); +char **p_subst_args(const char word[], int8_t redir); +char **p_subst_home(char *word[], t_msh *msh); +char **p_check_args_equals(char *words[], t_msh *msh); + +#endif diff --git a/src/p_lcom.c b/src/p_lcom.c deleted file mode 100644 index a8c42ce..0000000 --- a/src/p_lcom.c +++ /dev/null @@ -1,167 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* p_lcom.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#include -#include -#include -#include -#include -#include - -#include "d_define.h" -#include "f_fail.h" -#include "p_split.h" -#include "s_line.h" -#include "s_lpipes.h" -#include "s_struct.h" - -static void - rdr_err_check(char *ptr, - t_com **com) -{ - if ((*com)->redir == -1 && ft_ischarset("><", *(ptr + 1)) == TRUE) - { - /* TODO: syntax err */ - } - else if ((*com)->redir == 1 && ft_ischarset("<", *(ptr + 1)) == TRUE) - { - /* TODO: syntax err */ - } - else if ((*com)->redir == 2 && ft_ischarset("<>", *(ptr + 1)) == TRUE) - { - /* TODO: syntax err */ - } -} - -static int8_t - get_rdrpath(char *ptr, - t_com **com) -{ - char *p_rdrpath; - - ptr += ((*com)->redir == 2) ? (2) : (1); - if (!((*com)->rdrpath = - (char*)malloc((ft_strlen(ptr) + 1) * sizeof(char)))) - { - return (-1); - } - p_rdrpath = (*com)->rdrpath; - while (*ptr) - { - if (*ptr != ' ') - { - *p_rdrpath = *ptr; - p_rdrpath++; - } - ptr++; - } - *p_rdrpath = '\0'; - return (0); -} - -static void - get_rdrfd(const char *ptr, - t_com **com) -{ - while (ft_isdigit(*ptr) == TRUE) - { - ptr--; - } - if (*ptr != ' ') - (*com)->rdrfd = STDOUT_FILENO; - else - { - ptr += 1; - (*com)->rdrfd = ft_atoi(ptr); - } -} - -int8_t - p_get_redir(const char word[], - t_com **com) -{ - /* TODO: norme */ - char *ptr; - - ptr = (char *)word; - while (*ptr) - { - if (*ptr == '<') - { - (*com)->redir = -1; - break ; - } - if (*ptr == '>') - { - (*com)->redir = (*(ptr + 1) == '>') ? (2) : (1); - break ; - } - ptr++; - /* TODO: handle correctly multiples "msh ~> echo qwe > qwe > asd >> zxc > qweasdzxc" */ - /* hint: bash only handles the last one */ - /* TODO: handle "msh ~> cat < Makefile >qwe" | gl hf */ - } - if ((*com)->redir > 0) - { - if (ft_isdigit(*(ptr - 1)) == TRUE) - get_rdrfd(ptr - 1, com); - else - (*com)->rdrfd = STDOUT_FILENO; - rdr_err_check(ptr, com); - if (get_rdrpath(ptr, com) != 0) - return (-1); - } - return (0); -} - -int8_t - p_lcom(const char line[], - t_msh *msh) -{ - /* TODO: norme */ - uint64_t i; - t_line *link; - char **words; - char *ptr; - uint8_t nextif; - t_bool next; - - i = 0; - if ((words = p_split_line(line)) == NULL) - return (-1); - while (words[i] != NULL) - { - if (words[i][ft_strlen(words[i]) - 1] == ';') - nextif = 0; - else if (words[i][ft_strlen(words[i]) - 1] == '&') - nextif = 1; - else - nextif = 2; - words[i][ft_strlen(words[i]) - 1] = '\0'; - next = FALSE; - if ((ptr = ft_strchr(words[i], '|')) != NULL) - { - if ((link = s_line_new(NULL, msh)) == NULL) - return (-1); - if ((s_split_pipes(words[i], link, msh)) == NULL) - return (-1); - next = TRUE; - } - if (next == FALSE && (link = s_line_new(words[i], msh)) == NULL) - return (-1); - link->nextif = nextif; - s_line_add_back(&msh->curr, link); - i++; - } - ft_delwords(words); - return (0); -} diff --git a/src/p_lcom.h b/src/p_lcom.h deleted file mode 100644 index 796c684..0000000 --- a/src/p_lcom.h +++ /dev/null @@ -1,23 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* p_lcom.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef P_LCOM_H -#define P_LCOM_H - -#include - -#include "s_struct.h" - -int8_t p_get_redir(const char word[], t_com **com); -int8_t p_lcom(const char line[], t_msh *msh); - -#endif diff --git a/src/p_lcom_next.c b/src/p_lcom_next.c deleted file mode 100644 index 16d8aac..0000000 --- a/src/p_lcom_next.c +++ /dev/null @@ -1,223 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* p_line_next.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#include -#include -#include -#include - -#include "d_define.h" -#include "s_destroy.h" -#include "f_fail.h" -#include "s_struct.h" -#include "u_utils.h" -#include "u_vars.h" -#include "u_vars_next.h" - -/* TODO: norme */ - -static char - *p_subst_this_var(char **p, int64_t i, char word[], t_msh *msh) -{ - char tmp[4096]; - char varval[4096]; - char *ptr; - size_t varlen; - - ptr = word; - varlen = i + 1; - while (ptr[varlen] != C_NUL && - ft_ischarset("$=\\/@%^*+{}[]<>,.-", ptr[varlen]) == FALSE && - ft_iswhitespace(ptr[varlen]) == FALSE) - varlen += 1; - ft_strlcpy(tmp, ptr + i, varlen + 1 - i); - u_get_var_value(varval, tmp, 4096, msh); - ft_strlcpy(tmp, ptr + varlen, varlen); - word = ft_nrealloc(word, i, i + ft_strlen(varval) + ft_strlen(tmp) + 1); - ft_strlcpy(word + i, varval, ft_strlen(varval) + 1); - ft_strlcpy(word + (i + ft_strlen(varval)), tmp, ft_strlen(tmp) + 1); - *(p) = word + (i + ft_strlen(varval) - 1); - return (word); -} - -char - *p_subst_vars(char word[], t_msh *msh) -{ - char *ptr; - - ptr = word; - while (*ptr != C_NUL) - { - if (*ptr == '$' && u_is_not_escaped(word, ptr) == TRUE) - { - if ((word = p_subst_this_var(&ptr, (ptr - word), word, msh)) == NULL) - { - return (NULL); - } - } - ptr++; - } - return (word); -} - -char - **p_subst_home(char *words[], - t_msh *msh) -{ - char path[PATH_MAX]; - char **ptr; - - u_get_var_value(path, "$HOME", PATH_MAX, msh); - if (path[0] == C_NUL) - return (words); - ptr = words; - while (*ptr != NULL) - { - if (*ptr[0] == '~') - { - *ptr = ft_strsubst(*ptr, "~", path); - } - ptr++; - } - return (words); -} - -static void - p_register_word(char word[], - t_msh *msh) -{ - char name[255]; - char val[255]; - char *ptr; - size_t i; - - name[0] = '$'; - ptr = word; - i = 1; - while (*ptr != '=' && *ptr != '\0') - { - name[i] = *ptr; - i++; - ptr++; - } - name[i] = '\0'; - ptr++; - i = 0; - while (*ptr != '\0') - { - val[i] = *ptr; - i++; - ptr++; - } - val[i] = '\0'; - u_subst_var_value(name, val, msh); -} - -static char - **p_add_to_variables_and_delete(char *words[], - t_bool reg, - int64_t i, - t_msh *msh) -{ - int64_t j; - int64_t k; - char **rewords; - - j = 0; - if (reg == TRUE) - { - while (words[j] && j < i) - { - p_register_word(words[j], msh); - j++; - } - } - j = 0; - while (words[i + j] != NULL) - j++; - if (!(rewords = (char**)malloc((j + 1) * sizeof(char*)))) - { - ft_delwords(words); - f_alloc_and_destroy_msh(msh); - } - k = i; - while (i - k < j) - { - if ((rewords[i - k] = ft_strdup(words[i])) == NULL) - { - ft_delwords(words); - f_alloc_and_destroy_msh(msh); - } - i++; - } - rewords[i - k] = 0; - ft_delwords(words); - i++; - return (rewords); -} - -static void - p_add_to_env_fork(int64_t i, - char *words[], - t_msh *msh) -{ - int64_t j; - - j = 0; - while(j < i) - { - ft_strlcpy(msh->env_fork_tmp[j], words[j], ft_strlen(words[j]) + 1); - j++; - } - msh->env_fork_tmp[j][0] = '\0'; -} - -char - **p_check_args_equals(char *words[], - t_msh *msh) -{ - char *ptr; - t_bool reg; - t_bool isvar; - int64_t i; - - i = 0; - reg = FALSE; - isvar = FALSE; - while (words[i]) - { - ptr = words[i]; - while (*ptr != '\0' && *ptr != '=') - ptr++; - if (*ptr == '=') - { - reg = TRUE; - isvar = TRUE; - } - if (*ptr == '\0' || words[i][0] == '=' || - ft_isdigit(words[i][0]) == TRUE) - { - reg = FALSE; - if (i == 0) - isvar = FALSE; - if (isvar == TRUE) - p_add_to_env_fork(i, words, msh); - else - msh->env_fork_tmp[0][0] = '\0'; - break ; - } - i++; - } - if (isvar == TRUE) - return (p_add_to_variables_and_delete(words, reg, i, msh)); - return (words); -} diff --git a/src/p_lcom_next.h b/src/p_lcom_next.h deleted file mode 100644 index 8fbbb99..0000000 --- a/src/p_lcom_next.h +++ /dev/null @@ -1,25 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* p_line_next.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef P_LCOM_NEXT_H -#define P_LCOM_NEXT_H - -#include - -#include "s_struct.h" - -char *p_subst_vars(char word[], t_msh *msh); -char **p_subst_args(const char word[], int8_t redir); -char **p_subst_home(char *word[], t_msh *msh); -char **p_check_args_equals(char *words[], t_msh *msh); - -#endif diff --git a/src/p_line.c b/src/p_line.c index 170d8a8..645c7b6 100644 --- a/src/p_line.c +++ b/src/p_line.c @@ -67,7 +67,7 @@ void { return ; } - if (p_lcom(line, msh) < 0) + if (p_line_block(line, msh) < 0) { f_alloc_and_destroy_msh(msh); } diff --git a/src/s_init.c b/src/s_init.c index 9ef856f..cfe2eeb 100644 --- a/src/s_init.c +++ b/src/s_init.c @@ -174,6 +174,8 @@ t_msh msh->ret = 0; init_buptr(msh); msh->curr = NULL; + msh->com = NULL; + msh->pipes = NULL; msh->vars = NULL; set_cwd(cwd, msh); if ((msh->cwd = ft_strdup(cwd)) == NULL) diff --git a/src/s_struct.h b/src/s_struct.h index 1089dc6..f98f948 100644 --- a/src/s_struct.h +++ b/src/s_struct.h @@ -26,27 +26,27 @@ ** 0: means no redirection */ -typedef struct s_lvars +typedef struct s_lvars { - struct s_lvars *next; - char *name; - char *val; -} t_lvars; + struct s_lvars *next; + char *name; + char *val; +} t_lvars; -typedef struct s_com +typedef struct s_com { - char **argv; - char **env_fork; - char *rdrpath; - char *bin; - int32_t rdrfd; - int8_t redir; -} t_com; + char **argv; + char **env_fork; + char *rdrpath; + char *bin; + int32_t rdrfd; + int8_t redir; +} t_com; -struct s_lpipes +struct s_lpipes { - struct s_com *com; - struct s_lpipes *next; + struct s_com *com; + struct s_lpipes *next; }; /* @@ -57,28 +57,29 @@ struct s_lpipes ** 2: || */ -typedef struct s_line +typedef struct s_line { - struct s_com *com; - struct s_lpipes *pipes; - uint8_t nextif; - struct s_line *next; -} t_line; + char line[4096]; + uint8_t nextif; + struct s_line_block *next; +} t_line; -typedef struct s_msh +typedef struct s_msh { - struct s_line *curr; - struct s_lvars *vars; - char **envp; - char **bu_ref; - char ps[4][1024]; - char env_fork_tmp[128][1024]; - char sqb_ref[FT_ID_SQB_COUNT][4]; - char *shname; - char *cwd; - int32_t fd; - uint8_t (*bu_ptr[FT_BUILTINS_COUNT])(char **, struct s_msh*); - uint8_t ret; -} t_msh; + struct s_line_block *curr; + struct s_com *com; + struct s_lpipes *pipes; + struct s_lvars *vars; + char **envp; + char **bu_ref; + char ps[4][1024]; + char env_fork_tmp[128][1024]; + char sqb_ref[FT_ID_SQB_COUNT][4]; + char *shname; + char *cwd; + int32_t fd; + uint8_t (*bu_ptr[FT_BUILTINS_COUNT])(char **, struct s_msh*); + uint8_t ret; +} t_msh; #endif -- cgit v1.2.3 From bf6043888ebdafc3da4fc60af8f0f4888e8c53d0 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Tue, 8 Sep 2020 18:57:19 +0200 Subject: Update --- TODO.org | 1 + src/p_lblock.c | 12 ++++-------- src/p_lblock.h | 2 +- src/p_line.c | 2 +- src/p_split.c | 2 ++ 5 files changed, 9 insertions(+), 10 deletions(-) diff --git a/TODO.org b/TODO.org index 4746863..2fea1e9 100644 --- a/TODO.org +++ b/TODO.org @@ -7,6 +7,7 @@ ** DONE [#A] fix pwd when cd / ** DONE [#A] Multiline pipes (ls |) ** DONE [#A] Go full tok + quotes +** TODO [#A] fix \; \&& \|| ** TODO [#A] Variables quotes qwe="okokoko kkok" asd='bav' ** TODO [#A] ls qweqwe; echo $? <---- substitute ** TODO [#A] Careful p_line \; diff --git a/src/p_lblock.c b/src/p_lblock.c index a8c42ce..5be1c9d 100644 --- a/src/p_lblock.c +++ b/src/p_lblock.c @@ -25,8 +25,7 @@ #include "s_struct.h" static void - rdr_err_check(char *ptr, - t_com **com) + rdr_err_check(char *ptr, t_com **com) { if ((*com)->redir == -1 && ft_ischarset("><", *(ptr + 1)) == TRUE) { @@ -43,8 +42,7 @@ static void } static int8_t - get_rdrpath(char *ptr, - t_com **com) + get_rdrpath(char *ptr, t_com **com) { char *p_rdrpath; @@ -86,8 +84,7 @@ static void } int8_t - p_get_redir(const char word[], - t_com **com) + p_get_redir(const char word[], t_com **com) { /* TODO: norme */ char *ptr; @@ -124,8 +121,7 @@ int8_t } int8_t - p_lcom(const char line[], - t_msh *msh) + p_line_block(const char line[], t_msh *msh) { /* TODO: norme */ uint64_t i; diff --git a/src/p_lblock.h b/src/p_lblock.h index 796c684..6d4a023 100644 --- a/src/p_lblock.h +++ b/src/p_lblock.h @@ -18,6 +18,6 @@ #include "s_struct.h" int8_t p_get_redir(const char word[], t_com **com); -int8_t p_lcom(const char line[], t_msh *msh); +int8_t p_line_block(const char line[], t_msh *msh); #endif diff --git a/src/p_line.c b/src/p_line.c index 645c7b6..d839154 100644 --- a/src/p_line.c +++ b/src/p_line.c @@ -16,7 +16,7 @@ #include "d_enum.h" #include "f_fail.h" #include "p_line.h" -#include "p_lcom.h" +#include "p_lblock.h" #include "s_destroy.h" #include "s_struct.h" #include "u_utils.h" diff --git a/src/p_split.c b/src/p_split.c index bbe9bf5..998fda5 100644 --- a/src/p_split.c +++ b/src/p_split.c @@ -13,6 +13,8 @@ #include #include +#include "u_utils.h" + static size_t p_count_semi_words(const char line[]) { -- cgit v1.2.3 From d742075e1af0c063ef9677f157263c0d45253f73 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Tue, 8 Sep 2020 19:32:40 +0200 Subject: Rework in progress --- Makefile | 4 ++-- src/e_externs_pipes.c | 2 +- src/e_line.c | 45 ++++++++++----------------------------------- src/e_pipes.c | 7 +++---- src/e_pipes.h | 2 +- src/m_loop.c | 23 ++++++++++++++++++++++- src/p_lblock.c | 12 ++++++------ src/p_lblock.h | 2 +- src/p_lblock_next.c | 2 +- src/p_lblock_next.h | 2 +- src/s_com.c | 4 ++-- src/s_line.c | 50 ++++++++++++++++++++++++++------------------------ src/s_line.h | 10 ++++++---- src/s_lpipes.c | 6 ++---- src/s_lpipes.h | 2 +- src/s_struct.h | 6 +++--- 16 files changed, 88 insertions(+), 91 deletions(-) diff --git a/Makefile b/Makefile index 86d9a1f..af4a263 100644 --- a/Makefile +++ b/Makefile @@ -66,8 +66,8 @@ SRCS_NAME += p_args_next SRCS_NAME += p_args_len SRCS_NAME += p_args_escape SRCS_NAME += p_line -SRCS_NAME += p_lcom -SRCS_NAME += p_lcom_next +SRCS_NAME += p_lblock +SRCS_NAME += p_lblock_next SRCS_NAME += p_split SRCS_NAME += u_utils SRCS_NAME += u_vars diff --git a/src/e_externs_pipes.c b/src/e_externs_pipes.c index f6243e5..e6252e4 100644 --- a/src/e_externs_pipes.c +++ b/src/e_externs_pipes.c @@ -58,7 +58,7 @@ static void bu_id = get_builtin_id(ptr->bin, msh); ret = msh->bu_ptr[bu_id](ptr->argv + 1, msh); u_eof_fd(msh->fd); - s_lpipes_clear(&msh->curr->pipes); + s_lpipes_clear(&msh->pipes); s_line_clear(&msh->curr); s_destroy(msh); ft_delwords(fullpath); diff --git a/src/e_line.c b/src/e_line.c index f47170f..bb97556 100644 --- a/src/e_line.c +++ b/src/e_line.c @@ -22,8 +22,7 @@ #include "s_struct.h" static uint8_t - get_builtin_id(const char bin[], - t_msh *msh) + e_get_builtin_id(const char bin[], t_msh *msh) { uint8_t i; @@ -36,44 +35,20 @@ static uint8_t return (i); } -static void - e_line_destroy(t_line *ptr) -{ - if (ptr->pipes != NULL) - { - s_lpipes_clear(&ptr->pipes); - } -} - void e_line(t_msh *msh) { - t_line *ptr; uint8_t bu_id; - uint8_t previf; - previf = 0; - ptr = msh->curr; - while (ptr != NULL) + if (msh->pipes != NULL) + e_pipes(msh); + else if (msh->com != NULL) { - if ((previf == 0) || (previf == 1 && msh->ret == 0) || - (previf == 2 && msh->ret != 0)) - { - if (ptr->pipes != NULL) - e_pipes(ptr, msh); - else if (ptr->com != NULL) - { - if (ptr->com->bin != NULL && - (bu_id = get_builtin_id(ptr->com->bin, msh)) - < FT_BUILTINS_COUNT) - e_builtin(ptr->com, bu_id, msh); - else if (ptr->com->bin != NULL) - e_extern(ptr->com, msh); - } - } - else - e_line_destroy(ptr); - previf = ptr->nextif; - ptr = ptr->next; + if (msh->com->bin != NULL && + (bu_id = e_get_builtin_id(msh->com->bin, msh)) + < FT_BUILTINS_COUNT) + e_builtin(msh->com, bu_id, msh); + else if (msh->com->bin != NULL) + e_extern(msh->com, msh); } } diff --git a/src/e_pipes.c b/src/e_pipes.c index 6fdc149..a1807d1 100644 --- a/src/e_pipes.c +++ b/src/e_pipes.c @@ -35,14 +35,13 @@ /* } */ void - e_pipes(t_line *ptr, - t_msh *msh) + e_pipes(t_msh *msh) { /* uint8_t bu_id; */ /* if ((bu_id = get_builtin_id(ptr->pipes->one->com, msh)) */ /* < FT_BUILTINS_COUNT) */ /* e_builtin(ptr->pipes->one, bu_id, msh); */ - e_externs_pipes(ptr->pipes, msh); - s_lpipes_clear(&ptr->pipes); + e_externs_pipes(msh->pipes, msh); + s_lpipes_clear(&msh->pipes); } diff --git a/src/e_pipes.h b/src/e_pipes.h index 5bc073a..2f6c550 100644 --- a/src/e_pipes.h +++ b/src/e_pipes.h @@ -15,6 +15,6 @@ #include "s_struct.h" -void e_pipes(t_line *ptr, t_msh *msh); +void e_pipes(t_msh *msh); #endif diff --git a/src/m_loop.c b/src/m_loop.c index 9d00f2b..9ecea98 100644 --- a/src/m_loop.c +++ b/src/m_loop.c @@ -22,15 +22,36 @@ #include "m_loop_next.h" #include "m_prompt.h" #include "p_line.h" +#include "s_com.h" +#include "s_lpipes.h" #include "s_line.h" #include "u_vars.h" static void m_parse_and_run_line(char line[], t_msh *msh) { + t_line_block *ptr; + uint8_t previf; + p_line(line, msh); ft_memdel((void*)&line); - e_line(msh); + previf = 0; + ptr = msh->curr; + while (ptr != NULL) + { + ft_printf("[%s]\n", ptr->lblock); + if ((previf == 0) || (previf == 1 && msh->ret == 0) || + (previf == 2 && msh->ret != 0)) + { + if ((msh->com = s_com_new(msh->curr->lblock, msh)) == NULL) + break ; + e_line(msh); + } + else if (msh->pipes != NULL) + s_lpipes_clear(&msh->pipes); + previf = ptr->nextif; + ptr = ptr->next; + } s_line_clear(&msh->curr); } diff --git a/src/p_lblock.c b/src/p_lblock.c index 5be1c9d..856a546 100644 --- a/src/p_lblock.c +++ b/src/p_lblock.c @@ -1,7 +1,7 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* p_lcom.c :+: :+: :+: */ +/* p_lblock.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: rbousset +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ @@ -125,13 +125,14 @@ int8_t { /* TODO: norme */ uint64_t i; - t_line *link; + t_line_block *link; char **words; char *ptr; uint8_t nextif; t_bool next; i = 0; + nextif = 0; if ((words = p_split_line(line)) == NULL) return (-1); while (words[i] != NULL) @@ -146,15 +147,14 @@ int8_t next = FALSE; if ((ptr = ft_strchr(words[i], '|')) != NULL) { - if ((link = s_line_new(NULL, msh)) == NULL) + if ((link = s_line_new(NULL, 0)) == NULL) return (-1); - if ((s_split_pipes(words[i], link, msh)) == NULL) + if ((s_split_pipes(words[i], msh)) == NULL) return (-1); next = TRUE; } - if (next == FALSE && (link = s_line_new(words[i], msh)) == NULL) + if (next == FALSE && (link = s_line_new(words[i], nextif)) == NULL) return (-1); - link->nextif = nextif; s_line_add_back(&msh->curr, link); i++; } diff --git a/src/p_lblock.h b/src/p_lblock.h index 6d4a023..3979e31 100644 --- a/src/p_lblock.h +++ b/src/p_lblock.h @@ -1,7 +1,7 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* p_lcom.h :+: :+: :+: */ +/* p_lblock.h :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: rbousset +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ diff --git a/src/p_lblock_next.c b/src/p_lblock_next.c index 16d8aac..5777a3c 100644 --- a/src/p_lblock_next.c +++ b/src/p_lblock_next.c @@ -1,7 +1,7 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* p_line_next.c :+: :+: :+: */ +/* p_lblock_next.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: rbousset +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ diff --git a/src/p_lblock_next.h b/src/p_lblock_next.h index 8fbbb99..5fa9f45 100644 --- a/src/p_lblock_next.h +++ b/src/p_lblock_next.h @@ -1,7 +1,7 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* p_line_next.h :+: :+: :+: */ +/* p_lblock_next.h :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: rbousset +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ diff --git a/src/s_com.c b/src/s_com.c index ac23f35..5bdb36a 100644 --- a/src/s_com.c +++ b/src/s_com.c @@ -15,8 +15,8 @@ #include "f_fail.h" #include "p_args.h" -#include "p_lcom.h" -#include "p_lcom_next.h" +#include "p_lblock.h" +#include "p_lblock_next.h" #include "s_struct.h" static int8_t diff --git a/src/s_line.c b/src/s_line.c index ae0ec07..b198fff 100644 --- a/src/s_line.c +++ b/src/s_line.c @@ -14,26 +14,30 @@ #include #include +#include "d_define.h" #include "p_line.h" #include "s_com.h" #include "s_struct.h" -t_line - *s_line_last(t_line *line) +t_line_block + *s_line_last(t_line_block *line) { while (line->next != NULL) + { line = line->next; + } return (line); } void - s_line_add_back(t_line **aline, - t_line *new) + s_line_add_back(t_line_block **aline, t_line_block *new) { - t_line *tmp; + t_line_block *tmp; - if (!*aline) + if (*aline == NULL) + { *aline = new; + } else { tmp = s_line_last(*aline); @@ -42,45 +46,43 @@ void } void - s_line_clear(t_line **line) + s_line_clear(t_line_block **line) { - t_line *tmp; - t_line *renext; + t_line_block *tmp; + t_line_block *renext; - if (!line) + if (line == NULL) + { return ; + } tmp = *line; while (tmp != NULL) { renext = tmp->next; - if (tmp->com != NULL) - { - s_com_destroy(&tmp->com); - } ft_memdel((void*)&tmp); tmp = renext; } *line = NULL; } -t_line - *s_line_new(const char word[], - t_msh *msh) +t_line_block + *s_line_new(const char word[], uint8_t nextif) { - t_line *link; + t_line_block *link; - if ((link = (t_line*)malloc(sizeof(t_line))) == NULL) + if ((link = (t_line_block*)malloc(sizeof(t_line_block))) == NULL) return (NULL); - link->nextif = 0; - link->com = NULL; - link->pipes = NULL; + link->lblock[0] = C_NUL; + link->nextif = nextif; link->next = NULL; if (word == NULL) { link->next = NULL; return (link); } - if ((link->com = s_com_new(word, msh)) == NULL) - return (NULL); + else + { + ft_strlcpy(link->lblock, word, 4096); + } return (link); } diff --git a/src/s_line.h b/src/s_line.h index cd2211b..b46643f 100644 --- a/src/s_line.h +++ b/src/s_line.h @@ -13,11 +13,13 @@ #ifndef S_LCOM_H #define S_LCOM_H +#include + #include "s_struct.h" -void s_line_add_back(t_line **aline, t_line *new); -void s_line_clear(t_line **line); -t_line *s_line_new(const char word[], t_msh *msh); -t_line *s_line_last(t_line *line); +void s_line_add_back(t_line_block **aline, t_line_block *new); +void s_line_clear(t_line_block **line); +t_line_block *s_line_new(const char word[], uint8_t nextif); +t_line_block *s_line_last(t_line_block *line); #endif diff --git a/src/s_lpipes.c b/src/s_lpipes.c index f6a6845..0c1a6ea 100644 --- a/src/s_lpipes.c +++ b/src/s_lpipes.c @@ -79,9 +79,7 @@ struct s_lpipes } struct s_lpipes - *s_split_pipes(const char word[], - t_line *line, - t_msh *msh) + *s_split_pipes(const char word[], t_msh *msh) { struct s_lpipes *lpipes; char **words; @@ -96,7 +94,7 @@ struct s_lpipes { return (NULL); } - s_lpipes_add_back(&line->pipes, lpipes); + s_lpipes_add_back(&msh->pipes, lpipes); i++; } ft_delwords(words); diff --git a/src/s_lpipes.h b/src/s_lpipes.h index 5b2795a..e2b3f40 100644 --- a/src/s_lpipes.h +++ b/src/s_lpipes.h @@ -20,6 +20,6 @@ void s_lpipes_add_back(struct s_lpipes **alpipes, struct s_lpipes *new); void s_lpipes_clear(struct s_lpipes **lpipes); struct s_lpipes *s_lpipes_new(const char pipedword[], t_msh *msh); -struct s_lpipes *s_split_pipes(const char word[], t_line *line, t_msh *msh); +struct s_lpipes *s_split_pipes(const char word[], t_msh *msh); #endif diff --git a/src/s_struct.h b/src/s_struct.h index f98f948..f7ea271 100644 --- a/src/s_struct.h +++ b/src/s_struct.h @@ -57,12 +57,12 @@ struct s_lpipes ** 2: || */ -typedef struct s_line +typedef struct s_line_block { - char line[4096]; + char lblock[4096]; uint8_t nextif; struct s_line_block *next; -} t_line; +} t_line_block; typedef struct s_msh { -- cgit v1.2.3 From 332a55e859e8b213a9680b06022e4812d190951f Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Tue, 8 Sep 2020 19:37:19 +0200 Subject: Pretty bav --- src/m_loop.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/m_loop.c b/src/m_loop.c index 9ecea98..b3c06d0 100644 --- a/src/m_loop.c +++ b/src/m_loop.c @@ -39,11 +39,10 @@ static void ptr = msh->curr; while (ptr != NULL) { - ft_printf("[%s]\n", ptr->lblock); if ((previf == 0) || (previf == 1 && msh->ret == 0) || (previf == 2 && msh->ret != 0)) { - if ((msh->com = s_com_new(msh->curr->lblock, msh)) == NULL) + if ((msh->com = s_com_new(ptr->lblock, msh)) == NULL) break ; e_line(msh); } -- cgit v1.2.3 From 74766656e5b605785e0e9622eb7f10d20a5467e3 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Tue, 8 Sep 2020 20:26:51 +0200 Subject: I hate myself and I want to die --- Makefile | 1 + src/d_define.h | 9 +- src/p_args.c | 47 +--------- src/p_split.c | 271 +++++++++++++++++++++++++++++++++++---------------------- src/u_parse.c | 55 ++++++++++++ src/u_parse.h | 21 +++++ src/u_utils.c | 2 +- src/u_utils.h | 2 +- 8 files changed, 253 insertions(+), 155 deletions(-) create mode 100644 src/u_parse.c create mode 100644 src/u_parse.h diff --git a/Makefile b/Makefile index af4a263..929ffa5 100644 --- a/Makefile +++ b/Makefile @@ -69,6 +69,7 @@ SRCS_NAME += p_line SRCS_NAME += p_lblock SRCS_NAME += p_lblock_next SRCS_NAME += p_split +SRCS_NAME += u_parse SRCS_NAME += u_utils SRCS_NAME += u_vars SRCS_NAME += u_vars_next diff --git a/src/d_define.h b/src/d_define.h index 5c10526..8e73668 100644 --- a/src/d_define.h +++ b/src/d_define.h @@ -66,11 +66,14 @@ ** ====== UTILS ====== */ -#define C_SQUOTE 0x27 -#define C_DQUOTE 0x22 -#define C_BACKS 0x5c #define C_NUL 0x00 #define C_SUB 0x1a +#define C_DQUOTE 0x22 +#define C_SQUOTE 0x27 +#define C_AMP 0x26 +#define C_SEMIC 0x3b +#define C_BACKS 0x5c +#define C_PIPE 0x7c /* ** ====== FILES ====== diff --git a/src/p_args.c b/src/p_args.c index 78cfc06..a4b3d02 100644 --- a/src/p_args.c +++ b/src/p_args.c @@ -18,6 +18,7 @@ #include "p_args.h" #include "p_args_next.h" #include "p_args_escape.h" +#include "u_parse.h" #include "u_utils.h" static void @@ -33,48 +34,6 @@ static void } } -static t_quote_mode - p_meet_dquote(char *head, char *ptr, t_quote_mode mode) -{ - if (mode == Q_NONE) - { - if (u_is_not_escaped(head, ptr) == TRUE) - { - return (Q_DOUBLE); - } - else - { - return (Q_NONE); - } - } - else if (mode == Q_DOUBLE && u_is_not_escaped(head, ptr) == TRUE) - { - return (Q_NONE); - } - return (mode); -} - -static t_quote_mode - p_meet_squote(char *head, char *ptr, t_quote_mode mode) -{ - if (mode == Q_NONE) - { - if (u_is_not_escaped(head, ptr) == TRUE) - { - return (Q_SINGLE); - } - else - { - return (Q_NONE); - } - } - else if (mode == Q_SINGLE) - { - return (Q_NONE); - } - return (mode); -} - static t_bool p_meet_whitespace(char *head, char *ptr, t_quote_mode mode) { @@ -110,9 +69,9 @@ static uint16_t if (*ptr == C_BACKS) p_meet_bs(ptr, mode); if (*ptr == C_DQUOTE) - mode = p_meet_dquote((char*)word, ptr, mode); + mode = u_meet_dquote((char*)word, ptr, mode); else if (*ptr == C_SQUOTE) - mode = p_meet_squote((char*)word, ptr, mode); + mode = u_meet_squote((char*)word, ptr, mode); if (ft_iswhitespace(*ptr) && p_meet_whitespace((char*)word, ptr, mode) == TRUE) { diff --git a/src/p_split.c b/src/p_split.c index 998fda5..f3f3e27 100644 --- a/src/p_split.c +++ b/src/p_split.c @@ -12,134 +12,193 @@ #include #include +#include +#include "d_define.h" +#include "u_parse.h" #include "u_utils.h" -static size_t - p_count_semi_words(const char line[]) -{ - size_t count; - size_t i; +/* static size_t */ +/* p_count_semi_words(const char line[]) */ +/* { */ +/* size_t count; */ +/* size_t i; */ - count = 0; - i = 0; - while (line[i] != '\0') - { - if (line[i] == ';' && line[i + 1] != ';' && line[i + 1] != '\0') - count += 1; - i++; - } - return (count); -} +/* count = 0; */ +/* i = 0; */ +/* while (line[i] != '\0') */ +/* { */ +/* if (line[i] == ';' && line[i + 1] != ';' && line[i + 1] != '\0' && */ +/* u_is_not_escaped(line, line + i) == TRUE) */ +/* count += 1; */ +/* i++; */ +/* } */ +/* return (count); */ +/* } */ -static size_t - p_count_and_or_words(const char line[], const char c) -{ - size_t count; - size_t i; +/* static size_t */ +/* p_count_and_or_words(const char line[], const char c) */ +/* { */ +/* size_t count; */ +/* size_t i; */ - count = 0; - i = 0; - while (line[i] != '\0') - { - if (line[i] == c && line[i + 1] == c && - line[i + 2] != c && line[i + 2] != '\0') - count += 1; - i++; - } - return (count); -} +/* count = 0; */ +/* i = 0; */ +/* while (line[i] != '\0') */ +/* { */ +/* if (line[i] == c && line[i + 1] == c && */ +/* line[i + 2] != c && line[i + 2] != '\0') */ +/* count += 1; */ +/* i++; */ +/* } */ +/* return (count); */ +/* } */ + +/* static char */ +/* *p_get_first_occur(const char *line_ptr) */ +/* { */ +/* char *ptr[3]; */ +/* size_t len[3]; */ + +/* ptr[0] = ft_strnstr(line_ptr, ";", ft_strlen(line_ptr) + 1); */ +/* ptr[1] = ft_strnstr(line_ptr, "&&", ft_strlen(line_ptr) + 1); */ +/* ptr[2] = ft_strnstr(line_ptr, "||", ft_strlen(line_ptr) + 1); */ +/* len[0] = ft_strlen(ptr[0]); */ +/* len[1] = ft_strlen(ptr[1]); */ +/* len[2] = ft_strlen(ptr[2]); */ +/* if (len[0] > len[1] && len[0] > len[2]) */ +/* return (ptr[0]); */ +/* else if (len[1] > len[0] && len[1] > len[2]) */ +/* return (ptr[1]); */ +/* else if (len[2] > len[0] && len[2] > len[1]) */ +/* return (ptr[2]); */ +/* return ((char*)line_ptr); */ +/* } */ + +/* static char */ +/* **p_split_destroy(char **words, */ +/* size_t i) */ +/* { */ +/* while (i > 0) */ +/* { */ +/* ft_memdel((void*)&words[i]); */ +/* } */ +/* ft_memdel((void*)&words); */ +/* return (NULL); */ +/* } */ -static char - *p_get_first_occur(const char *line_ptr) +/* static char */ +/* **p_split_to_stuff(const char line[], */ +/* const size_t count) */ +/* { */ +/* char **words; */ +/* char *line_ptr; */ +/* char *need_ptr; */ +/* size_t i; */ +/* char c; */ + +/* if ((words = (char**)malloc((count + 1) * sizeof(char*))) == NULL) */ +/* return (NULL); */ +/* line_ptr = (char*)line; */ +/* i = 0; */ +/* while (i < count) */ +/* { */ +/* need_ptr = p_get_first_occur(line_ptr); */ +/* c = need_ptr[0]; */ +/* need_ptr += (c == ';') ? (1) : (0); */ +/* need_ptr += (c == '&') ? (2) : (0); */ +/* need_ptr += (c == '|') ? (2) : (0); */ +/* if (need_ptr - line_ptr == 0) */ +/* { */ +/* if ((words[i] = (char*)malloc(((ft_strlen(line_ptr) + 2) * */ +/* sizeof(char)))) == NULL) */ +/* return (p_split_destroy(words, i)); */ +/* ft_memcpy(words[i], line_ptr, ft_strlen(line_ptr)); */ +/* words[i][ft_strlen(line_ptr)] = ';'; */ +/* words[i][ft_strlen(line_ptr) + 1] = '\0'; */ +/* } */ +/* else */ +/* { */ +/* if ((words[i] = (char*)malloc(((need_ptr - line_ptr) + 1) * */ +/* sizeof(char))) == NULL) */ +/* return (p_split_destroy(words, i)); */ +/* ft_memcpy(words[i], line_ptr, (need_ptr - line_ptr) - 1); */ +/* words[i][(need_ptr - line_ptr) - ((c == ';') ? (1) : (2))] = c; */ +/* words[i][(need_ptr - line_ptr) - ((c == ';') ? (0) : (1))] = '\0'; */ +/* line_ptr = need_ptr; */ +/* } */ +/* i++; */ +/* } */ +/* words[i] = NULL; */ +/* return (words); */ +/* } */ + +/* char */ +/* **p_split_line(const char line[]) */ +/* { */ +/* char **words; */ +/* size_t count; */ + +/* count = p_count_semi_words(line); */ +/* count += p_count_and_or_words(line, '&'); */ +/* count += p_count_and_or_words(line, '|'); */ +/* count += 1; */ +/* if ((words = p_split_to_stuff(line, count)) == NULL) */ +/* return (NULL); */ +/* return (words); */ +/* } */ + +typedef struct s_split { - char *ptr[3]; - size_t len[3]; - - ptr[0] = ft_strnstr(line_ptr, ";", ft_strlen(line_ptr) + 1); - ptr[1] = ft_strnstr(line_ptr, "&&", ft_strlen(line_ptr) + 1); - ptr[2] = ft_strnstr(line_ptr, "||", ft_strlen(line_ptr) + 1); - len[0] = ft_strlen(ptr[0]); - len[1] = ft_strlen(ptr[1]); - len[2] = ft_strlen(ptr[2]); - if (len[0] > len[1] && len[0] > len[2]) - return (ptr[0]); - else if (len[1] > len[0] && len[1] > len[2]) - return (ptr[1]); - else if (len[2] > len[0] && len[2] > len[1]) - return (ptr[2]); - return ((char*)line_ptr); -} + size_t pos[ARG_MAX / 2]; + int8_t nextif[ARG_MAX / 2]; + int16_t count; +} t_split; -static char - **p_split_destroy(char **words, - size_t i) +static void + p_meet_splitter(char *ptr, char line[], t_split sp, t_quote_mode mode) { - while (i > 0) + const char c = *ptr; + + if (c == C_SEMIC && *ptr + 1 != C_SEMIC && *ptr + 1 != C_NUL && + u_is_not_escaped(line, ptr) == TRUE) { - ft_memdel((void*)&words[i]); + sp.pos[sp.count] = (ptr - line); } - ft_memdel((void*)&words); - return (NULL); } -static char - **p_split_to_stuff(const char line[], - const size_t count) +static t_split + p_fill_sp(const char line[]) { - char **words; - char *line_ptr; - char *need_ptr; - size_t i; - char c; - - if ((words = (char**)malloc((count + 1) * sizeof(char*))) == NULL) - return (NULL); - line_ptr = (char*)line; - i = 0; - while (i < count) + t_split sp; + char *ptr; + t_quote_mode mode; + + sp.pos[0] = 0; + sp.nextif[0] = 0; + sp.count = 0; + mode = Q_NONE; + ptr = line; + while (*ptr != C_NUL) { - need_ptr = p_get_first_occur(line_ptr); - c = need_ptr[0]; - need_ptr += (c == ';') ? (1) : (0); - need_ptr += (c == '&') ? (2) : (0); - need_ptr += (c == '|') ? (2) : (0); - if (need_ptr - line_ptr == 0) - { - if ((words[i] = (char*)malloc(((ft_strlen(line_ptr) + 2) * - sizeof(char)))) == NULL) - return (p_split_destroy(words, i)); - ft_memcpy(words[i], line_ptr, ft_strlen(line_ptr)); - words[i][ft_strlen(line_ptr)] = ';'; - words[i][ft_strlen(line_ptr) + 1] = '\0'; - } - else - { - if ((words[i] = (char*)malloc(((need_ptr - line_ptr) + 1) * - sizeof(char))) == NULL) - return (p_split_destroy(words, i)); - ft_memcpy(words[i], line_ptr, (need_ptr - line_ptr) - 1); - words[i][(need_ptr - line_ptr) - ((c == ';') ? (1) : (2))] = c; - words[i][(need_ptr - line_ptr) - ((c == ';') ? (0) : (1))] = '\0'; - line_ptr = need_ptr; - } - i++; + if (*ptr == C_SQUOTE) + mode = u_meet_squote(ptr, line, mode); + else if (*ptr == C_DQUOTE) + mode = u_meet_dquote(ptr, line, mode); + else if (*ptr == C_SEMIC || *ptr == C_AMP || *ptr == C_PIPE) + p_meet_splitter(ptr, line, sp, mode); + ptr++; } - words[i] = NULL; - return (words); + return (sp); } char **p_split_line(const char line[]) { + t_split sp; char **words; - size_t count; - - count = p_count_semi_words(line); - count += p_count_and_or_words(line, '&'); - count += p_count_and_or_words(line, '|'); - count += 1; - if ((words = p_split_to_stuff(line, count)) == NULL) - return (NULL); + + words = NULL; + sp = p_fill_sp(line); return (words); } diff --git a/src/u_parse.c b/src/u_parse.c new file mode 100644 index 0000000..ade8dd7 --- /dev/null +++ b/src/u_parse.c @@ -0,0 +1,55 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* u_parse.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include "d_define.h" + +t_quote_mode + u_meet_dquote(char *head, char *ptr, t_quote_mode mode) +{ + if (mode == Q_NONE) + { + if (u_is_not_escaped(head, ptr) == TRUE) + { + return (Q_DOUBLE); + } + else + { + return (Q_NONE); + } + } + else if (mode == Q_DOUBLE && u_is_not_escaped(head, ptr) == TRUE) + { + return (Q_NONE); + } + return (mode); +} + +t_quote_mode + u_meet_squote(char *head, char *ptr, t_quote_mode mode) +{ + if (mode == Q_NONE) + { + if (u_is_not_escaped(head, ptr) == TRUE) + { + return (Q_SINGLE); + } + else + { + return (Q_NONE); + } + } + else if (mode == Q_SINGLE) + { + return (Q_NONE); + } + return (mode); +} diff --git a/src/u_parse.h b/src/u_parse.h new file mode 100644 index 0000000..631468e --- /dev/null +++ b/src/u_parse.h @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* u_parse.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef U_PARSE_H +#define U_PARSE_H + +#include "d_define.h" + +t_quote_mode u_meet_dquote(char *head, char *ptr, t_quote_mode mode); +t_quote_mode u_meet_squote(char *head, char *ptr, t_quote_mode mode); + +#endif diff --git a/src/u_utils.c b/src/u_utils.c index 7f75c4d..dd1634b 100644 --- a/src/u_utils.c +++ b/src/u_utils.c @@ -20,7 +20,7 @@ #include "s_struct.h" t_bool - u_is_not_escaped(char *head, char *ptr) + u_is_not_escaped(const char *head, const char *ptr) { if (((ptr - head) == 0) || ((ptr - head) >= 1 && *(ptr - 1) != C_BACKS) || diff --git a/src/u_utils.h b/src/u_utils.h index 2aa2c72..12606b7 100644 --- a/src/u_utils.h +++ b/src/u_utils.h @@ -18,7 +18,7 @@ #include "s_struct.h" -t_bool u_is_not_escaped(char *head, char *ptr); +t_bool u_is_not_escaped(const char *head, const char *ptr); void u_eof_fd(int32_t fd); uint64_t u_builtins_get_argc(const char *args[]); char **u_get_env_var_names(t_msh *msh); -- cgit v1.2.3 From 370f650446b966794e3a1ce973acb5b5f396aa83 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Tue, 8 Sep 2020 20:28:22 +0200 Subject: In progress --- src/p_split.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/p_split.c b/src/p_split.c index f3f3e27..7e2260d 100644 --- a/src/p_split.c +++ b/src/p_split.c @@ -164,6 +164,8 @@ static void u_is_not_escaped(line, ptr) == TRUE) { sp.pos[sp.count] = (ptr - line); + sp.nextif[sp.count] = 0; + sp.count += 1; } } -- cgit v1.2.3 From c68b7637d42fac3986857685980e7a7090f5a5aa Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Wed, 9 Sep 2020 16:17:52 +0200 Subject: In progress --- Makefile | 1 + src/f_fail.h | 1 + src/f_parse.c | 22 ++++++++++++++++++++++ src/f_parse.h | 18 ++++++++++++++++++ src/m_loop.c | 1 - src/m_minishell.c | 3 ++- src/p_line.c | 3 +-- src/p_split.c | 42 ++++++++++++++++++++++++++++++------------ src/u_parse.c | 7 +++++-- src/u_parse.h | 4 ++-- 10 files changed, 82 insertions(+), 20 deletions(-) create mode 100644 src/f_parse.c create mode 100644 src/f_parse.h diff --git a/Makefile b/Makefile index 929ffa5..4e890fe 100644 --- a/Makefile +++ b/Makefile @@ -43,6 +43,7 @@ SRCS_NAME += f_errno SRCS_NAME += f_exec SRCS_NAME += f_fail SRCS_NAME += f_file +SRCS_NAME += f_parse SRCS_NAME += f_redir SRCS_NAME += f_shlvl SRCS_NAME += m_argv diff --git a/src/f_fail.h b/src/f_fail.h index 2e432e2..0046814 100644 --- a/src/f_fail.h +++ b/src/f_fail.h @@ -19,6 +19,7 @@ #include "f_errno.h" #include "f_exec.h" #include "f_file.h" +#include "f_parse.h" #include "f_redir.h" #include "f_shlvl.h" #include "s_struct.h" diff --git a/src/f_parse.c b/src/f_parse.c new file mode 100644 index 0000000..67a7bf1 --- /dev/null +++ b/src/f_parse.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* f_parse.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include + +void + f_parse_token(char c) +{ + ft_dprintf(STDERR_FILENO, + "minishell: syntax error near unexpected token `%c'", + c); +} diff --git a/src/f_parse.h b/src/f_parse.h new file mode 100644 index 0000000..f6ac496 --- /dev/null +++ b/src/f_parse.h @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* f_parse.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef F_PARSE_H +#define F_PARSE_H + +void f_parse_token(char c); + +#endif diff --git a/src/m_loop.c b/src/m_loop.c index b3c06d0..5641686 100644 --- a/src/m_loop.c +++ b/src/m_loop.c @@ -34,7 +34,6 @@ static void uint8_t previf; p_line(line, msh); - ft_memdel((void*)&line); previf = 0; ptr = msh->curr; while (ptr != NULL) diff --git a/src/m_minishell.c b/src/m_minishell.c index a538041..6be605a 100644 --- a/src/m_minishell.c +++ b/src/m_minishell.c @@ -14,6 +14,7 @@ #include #include #include +#include #include #include "f_fail.h" @@ -32,7 +33,7 @@ int if ((msh = init_msh(argv, envp)) == NULL) { - ft_dprintf(2, "%s\n", strerror(errno)); + ft_dprintf(STDERR_FILENO, "%s\n", strerror(errno)); return (FT_RET_ALLOC); } ret = m_argv(argc, argv, msh); diff --git a/src/p_line.c b/src/p_line.c index d839154..cdb2fd4 100644 --- a/src/p_line.c +++ b/src/p_line.c @@ -59,8 +59,7 @@ static t_bool } void - p_line(char line[], - t_msh *msh) + p_line(char line[], t_msh *msh) { p_delete_comments(line); if (p_check_whitespaces_only(line) == TRUE) diff --git a/src/p_split.c b/src/p_split.c index 7e2260d..25f1cc7 100644 --- a/src/p_split.c +++ b/src/p_split.c @@ -14,7 +14,9 @@ #include #include +#include "f_fail.h" #include "d_define.h" +#include "s_struct.h" #include "u_parse.h" #include "u_utils.h" @@ -156,16 +158,31 @@ typedef struct s_split } t_split; static void - p_meet_splitter(char *ptr, char line[], t_split sp, t_quote_mode mode) + p_meet_splitter(char *ptr, + const char line[], + t_split *sp, + t_quote_mode mode) { - const char c = *ptr; - - if (c == C_SEMIC && *ptr + 1 != C_SEMIC && *ptr + 1 != C_NUL && + if (mode == Q_NONE && *ptr == C_SEMIC && *(ptr + 1) != C_SEMIC && u_is_not_escaped(line, ptr) == TRUE) { - sp.pos[sp.count] = (ptr - line); - sp.nextif[sp.count] = 0; - sp.count += 1; + sp->pos[sp->count] = (ptr - line); + sp->nextif[sp->count] = 0; + sp->count += 1; + } + else if (mode == Q_NONE && *ptr == C_AMP && *(ptr + 1) == C_AMP && + (*ptr + 2) != C_PIPE && u_is_not_escaped(line, ptr) == TRUE) + { + sp->pos[sp->count] = (ptr - line); + sp->nextif[sp->count] = 1; + sp->count += 1; + } + else if (mode == Q_NONE && *ptr == C_PIPE && *(ptr + 1) == C_PIPE && + *(ptr + 2) != C_PIPE && u_is_not_escaped(line, ptr) == TRUE) + { + sp->pos[sp->count] = (ptr - line); + sp->nextif[sp->count] = 2; + sp->count += 1; } } @@ -180,27 +197,28 @@ static t_split sp.nextif[0] = 0; sp.count = 0; mode = Q_NONE; - ptr = line; + ptr = (char*)line; while (*ptr != C_NUL) { if (*ptr == C_SQUOTE) - mode = u_meet_squote(ptr, line, mode); + mode = u_meet_squote(line, ptr, mode); else if (*ptr == C_DQUOTE) - mode = u_meet_dquote(ptr, line, mode); + mode = u_meet_dquote(line, ptr, mode); else if (*ptr == C_SEMIC || *ptr == C_AMP || *ptr == C_PIPE) - p_meet_splitter(ptr, line, sp, mode); + p_meet_splitter(ptr, line, &sp, mode); ptr++; } return (sp); } char - **p_split_line(const char line[]) + **p_split_line(char line[]) { t_split sp; char **words; words = NULL; sp = p_fill_sp(line); + ft_memdel((void*)&line); return (words); } diff --git a/src/u_parse.c b/src/u_parse.c index ade8dd7..b49e6ba 100644 --- a/src/u_parse.c +++ b/src/u_parse.c @@ -10,10 +10,13 @@ /* */ /* ************************************************************************** */ +#include + +#include "u_utils.h" #include "d_define.h" t_quote_mode - u_meet_dquote(char *head, char *ptr, t_quote_mode mode) + u_meet_dquote(const char *head, char *ptr, t_quote_mode mode) { if (mode == Q_NONE) { @@ -34,7 +37,7 @@ t_quote_mode } t_quote_mode - u_meet_squote(char *head, char *ptr, t_quote_mode mode) + u_meet_squote(const char *head, char *ptr, t_quote_mode mode) { if (mode == Q_NONE) { diff --git a/src/u_parse.h b/src/u_parse.h index 631468e..8542f80 100644 --- a/src/u_parse.h +++ b/src/u_parse.h @@ -15,7 +15,7 @@ #include "d_define.h" -t_quote_mode u_meet_dquote(char *head, char *ptr, t_quote_mode mode); -t_quote_mode u_meet_squote(char *head, char *ptr, t_quote_mode mode); +t_quote_mode u_meet_dquote(const char *head, char *ptr, t_quote_mode mode); +t_quote_mode u_meet_squote(const char *head, char *ptr, t_quote_mode mode); #endif -- cgit v1.2.3 From ddf4006f9db4920c47efdd8488203408cf8d5e58 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Wed, 9 Sep 2020 16:45:07 +0200 Subject: qwe --- src/p_split.c | 14 ++++++++++++++ src/s_struct.h | 3 ++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/p_split.c b/src/p_split.c index 25f1cc7..482bc58 100644 --- a/src/p_split.c +++ b/src/p_split.c @@ -211,6 +211,15 @@ static t_split return (sp); } +static char +**p_get_words(const char line[], const t_split sp) +{ + char **words; + + words = NULL; + return (words); +} + char **p_split_line(char line[]) { @@ -219,6 +228,11 @@ char words = NULL; sp = p_fill_sp(line); + if ((words = p_get_words(line, sp)) == NULL) + { + ft_memdel((void*)&line); + return (NULL); + } ft_memdel((void*)&line); return (words); } diff --git a/src/s_struct.h b/src/s_struct.h index f7ea271..2734618 100644 --- a/src/s_struct.h +++ b/src/s_struct.h @@ -14,6 +14,7 @@ #define S_STRUCT_H #include +#include #include "d_define.h" @@ -59,7 +60,7 @@ struct s_lpipes typedef struct s_line_block { - char lblock[4096]; + char lblock[ARG_MAX]; uint8_t nextif; struct s_line_block *next; } t_line_block; -- cgit v1.2.3 From c8365681f146bc8ccf2c95f6ceb3cbeab3470f08 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Wed, 9 Sep 2020 17:53:40 +0200 Subject: Pretty bav --- src/p_split.c | 42 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/src/p_split.c b/src/p_split.c index 482bc58..d39a4cb 100644 --- a/src/p_split.c +++ b/src/p_split.c @@ -154,7 +154,7 @@ typedef struct s_split { size_t pos[ARG_MAX / 2]; int8_t nextif[ARG_MAX / 2]; - int16_t count; + uint32_t count; } t_split; static void @@ -208,15 +208,51 @@ static t_split p_meet_splitter(ptr, line, &sp, mode); ptr++; } + sp.pos[sp.count] = ptr - line; + sp.nextif[sp.count] = 0; + sp.count += 1; return (sp); } +static void + *p_del_split(char *words[], size_t todel) +{ + size_t i; + + i = 0; + while (i < todel) + { + ft_memdel((void*)&words[i]); + } + ft_memdel((void*)&words); + return (NULL); +} + static char -**p_get_words(const char line[], const t_split sp) + **p_get_words(const char line[], const t_split sp) { char **words; + size_t i; + size_t oldpos; + int8_t oldif; - words = NULL; + if ((words = (char**)malloc((sp.count + 1) * sizeof(char*))) == NULL) + return (NULL); + oldpos = 0; + oldif = -1; + i = 0; + while (i < sp.count) + { + if ((words[i] = + (char*)malloc(((sp.pos[i] - oldpos) + 1) * sizeof(char))) == NULL) + return ((char**)p_del_split(words, i)); + ft_strlcpy(words[i], line + oldpos + ((oldif > 0) ? (2) : (oldif) + 1), sp.pos[i] - oldpos); + ft_printf("[%s]\n", words[i]); + oldpos = sp.pos[i]; + oldif = sp.nextif[i]; + i++; + } + words[i] = NULL; return (words); } -- cgit v1.2.3 From 42aa32df3f63d4dd1213b0683ab7110d788defef Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Wed, 9 Sep 2020 18:29:25 +0200 Subject: The more memory the better LOL --- src/m_loop.c | 1 + src/p_lblock.c | 6 +- src/p_split.c | 194 +++++++++------------------------------------------------ src/p_split.h | 9 ++- 4 files changed, 41 insertions(+), 169 deletions(-) diff --git a/src/m_loop.c b/src/m_loop.c index 5641686..50357eb 100644 --- a/src/m_loop.c +++ b/src/m_loop.c @@ -44,6 +44,7 @@ static void if ((msh->com = s_com_new(ptr->lblock, msh)) == NULL) break ; e_line(msh); + s_com_destroy(&msh->com); } else if (msh->pipes != NULL) s_lpipes_clear(&msh->pipes); diff --git a/src/p_lblock.c b/src/p_lblock.c index 856a546..377757d 100644 --- a/src/p_lblock.c +++ b/src/p_lblock.c @@ -133,13 +133,13 @@ int8_t i = 0; nextif = 0; - if ((words = p_split_line(line)) == NULL) + if ((words = p_split_line((char*)line)) == NULL) return (-1); while (words[i] != NULL) { - if (words[i][ft_strlen(words[i]) - 1] == ';') + if (words[i][ft_strlen(words[i]) - 1] == '0') nextif = 0; - else if (words[i][ft_strlen(words[i]) - 1] == '&') + else if (words[i][ft_strlen(words[i]) - 1] == '1') nextif = 1; else nextif = 2; diff --git a/src/p_split.c b/src/p_split.c index d39a4cb..dc26c04 100644 --- a/src/p_split.c +++ b/src/p_split.c @@ -16,151 +16,15 @@ #include "f_fail.h" #include "d_define.h" +#include "p_split.h" #include "s_struct.h" #include "u_parse.h" #include "u_utils.h" -/* static size_t */ -/* p_count_semi_words(const char line[]) */ -/* { */ -/* size_t count; */ -/* size_t i; */ - -/* count = 0; */ -/* i = 0; */ -/* while (line[i] != '\0') */ -/* { */ -/* if (line[i] == ';' && line[i + 1] != ';' && line[i + 1] != '\0' && */ -/* u_is_not_escaped(line, line + i) == TRUE) */ -/* count += 1; */ -/* i++; */ -/* } */ -/* return (count); */ -/* } */ - -/* static size_t */ -/* p_count_and_or_words(const char line[], const char c) */ -/* { */ -/* size_t count; */ -/* size_t i; */ - -/* count = 0; */ -/* i = 0; */ -/* while (line[i] != '\0') */ -/* { */ -/* if (line[i] == c && line[i + 1] == c && */ -/* line[i + 2] != c && line[i + 2] != '\0') */ -/* count += 1; */ -/* i++; */ -/* } */ -/* return (count); */ -/* } */ - -/* static char */ -/* *p_get_first_occur(const char *line_ptr) */ -/* { */ -/* char *ptr[3]; */ -/* size_t len[3]; */ - -/* ptr[0] = ft_strnstr(line_ptr, ";", ft_strlen(line_ptr) + 1); */ -/* ptr[1] = ft_strnstr(line_ptr, "&&", ft_strlen(line_ptr) + 1); */ -/* ptr[2] = ft_strnstr(line_ptr, "||", ft_strlen(line_ptr) + 1); */ -/* len[0] = ft_strlen(ptr[0]); */ -/* len[1] = ft_strlen(ptr[1]); */ -/* len[2] = ft_strlen(ptr[2]); */ -/* if (len[0] > len[1] && len[0] > len[2]) */ -/* return (ptr[0]); */ -/* else if (len[1] > len[0] && len[1] > len[2]) */ -/* return (ptr[1]); */ -/* else if (len[2] > len[0] && len[2] > len[1]) */ -/* return (ptr[2]); */ -/* return ((char*)line_ptr); */ -/* } */ - -/* static char */ -/* **p_split_destroy(char **words, */ -/* size_t i) */ -/* { */ -/* while (i > 0) */ -/* { */ -/* ft_memdel((void*)&words[i]); */ -/* } */ -/* ft_memdel((void*)&words); */ -/* return (NULL); */ -/* } */ - -/* static char */ -/* **p_split_to_stuff(const char line[], */ -/* const size_t count) */ -/* { */ -/* char **words; */ -/* char *line_ptr; */ -/* char *need_ptr; */ -/* size_t i; */ -/* char c; */ - -/* if ((words = (char**)malloc((count + 1) * sizeof(char*))) == NULL) */ -/* return (NULL); */ -/* line_ptr = (char*)line; */ -/* i = 0; */ -/* while (i < count) */ -/* { */ -/* need_ptr = p_get_first_occur(line_ptr); */ -/* c = need_ptr[0]; */ -/* need_ptr += (c == ';') ? (1) : (0); */ -/* need_ptr += (c == '&') ? (2) : (0); */ -/* need_ptr += (c == '|') ? (2) : (0); */ -/* if (need_ptr - line_ptr == 0) */ -/* { */ -/* if ((words[i] = (char*)malloc(((ft_strlen(line_ptr) + 2) * */ -/* sizeof(char)))) == NULL) */ -/* return (p_split_destroy(words, i)); */ -/* ft_memcpy(words[i], line_ptr, ft_strlen(line_ptr)); */ -/* words[i][ft_strlen(line_ptr)] = ';'; */ -/* words[i][ft_strlen(line_ptr) + 1] = '\0'; */ -/* } */ -/* else */ -/* { */ -/* if ((words[i] = (char*)malloc(((need_ptr - line_ptr) + 1) * */ -/* sizeof(char))) == NULL) */ -/* return (p_split_destroy(words, i)); */ -/* ft_memcpy(words[i], line_ptr, (need_ptr - line_ptr) - 1); */ -/* words[i][(need_ptr - line_ptr) - ((c == ';') ? (1) : (2))] = c; */ -/* words[i][(need_ptr - line_ptr) - ((c == ';') ? (0) : (1))] = '\0'; */ -/* line_ptr = need_ptr; */ -/* } */ -/* i++; */ -/* } */ -/* words[i] = NULL; */ -/* return (words); */ -/* } */ - -/* char */ -/* **p_split_line(const char line[]) */ -/* { */ -/* char **words; */ -/* size_t count; */ - -/* count = p_count_semi_words(line); */ -/* count += p_count_and_or_words(line, '&'); */ -/* count += p_count_and_or_words(line, '|'); */ -/* count += 1; */ -/* if ((words = p_split_to_stuff(line, count)) == NULL) */ -/* return (NULL); */ -/* return (words); */ -/* } */ - -typedef struct s_split -{ - size_t pos[ARG_MAX / 2]; - int8_t nextif[ARG_MAX / 2]; - uint32_t count; -} t_split; - static void p_meet_splitter(char *ptr, const char line[], - t_split *sp, + t_split_block *sp, t_quote_mode mode) { if (mode == Q_NONE && *ptr == C_SEMIC && *(ptr + 1) != C_SEMIC && @@ -186,16 +50,15 @@ static void } } -static t_split - p_fill_sp(const char line[]) +static void + p_fill_sp(t_split_block *sp, const char line[]) { - t_split sp; char *ptr; t_quote_mode mode; - sp.pos[0] = 0; - sp.nextif[0] = 0; - sp.count = 0; + sp->pos[0] = 0; + sp->nextif[0] = 0; + sp->count = 0; mode = Q_NONE; ptr = (char*)line; while (*ptr != C_NUL) @@ -205,13 +68,12 @@ static t_split else if (*ptr == C_DQUOTE) mode = u_meet_dquote(line, ptr, mode); else if (*ptr == C_SEMIC || *ptr == C_AMP || *ptr == C_PIPE) - p_meet_splitter(ptr, line, &sp, mode); + p_meet_splitter(ptr, line, sp, mode); ptr++; } - sp.pos[sp.count] = ptr - line; - sp.nextif[sp.count] = 0; - sp.count += 1; - return (sp); + sp->pos[sp->count] = ptr - line; + sp->nextif[sp->count] = 0; + sp->count += 1; } static void @@ -229,28 +91,30 @@ static void } static char - **p_get_words(const char line[], const t_split sp) + **p_get_words(const char line[], const t_split_block *sp) { char **words; - size_t i; + int64_t i; size_t oldpos; int8_t oldif; - if ((words = (char**)malloc((sp.count + 1) * sizeof(char*))) == NULL) + if ((words = (char**)malloc((sp->count + 1) * sizeof(char*))) == NULL) return (NULL); oldpos = 0; oldif = -1; - i = 0; - while (i < sp.count) + i = -1; + while (++i < sp->count) { - if ((words[i] = - (char*)malloc(((sp.pos[i] - oldpos) + 1) * sizeof(char))) == NULL) + if ((words[i] = (char*)malloc(((sp->pos[i] - oldpos) + 2) * + sizeof(char))) == NULL) return ((char**)p_del_split(words, i)); - ft_strlcpy(words[i], line + oldpos + ((oldif > 0) ? (2) : (oldif) + 1), sp.pos[i] - oldpos); - ft_printf("[%s]\n", words[i]); - oldpos = sp.pos[i]; - oldif = sp.nextif[i]; - i++; + ft_strlcpy(words[i], line + oldpos + ((oldif > 0) ? (2) : (oldif) + 1), + sp->pos[i] - oldpos + 1 - ((oldif > 0) ? (2) : (1)) + + ((oldif < 0) ? (1) : (0))); + words[i][ft_strlen(words[i]) + 1] = C_NUL; + words[i][ft_strlen(words[i])] = sp->nextif[i] + 0x30; + oldpos = sp->pos[i]; + oldif = sp->nextif[i]; } words[i] = NULL; return (words); @@ -259,12 +123,12 @@ static char char **p_split_line(char line[]) { - t_split sp; - char **words; + t_split_block sp; + char **words; words = NULL; - sp = p_fill_sp(line); - if ((words = p_get_words(line, sp)) == NULL) + p_fill_sp(&sp, line); + if ((words = p_get_words(line, &sp)) == NULL) { ft_memdel((void*)&line); return (NULL); diff --git a/src/p_split.h b/src/p_split.h index 648e01c..00da660 100644 --- a/src/p_split.h +++ b/src/p_split.h @@ -13,6 +13,13 @@ #ifndef P_SPLIT_H #define P_SPLIT_H -char **p_split_line(const char line[]); +typedef struct s_split_block +{ + size_t pos[ARG_MAX / 2]; + int8_t nextif[ARG_MAX / 2]; + uint32_t count; +} t_split_block; + +char **p_split_line(char line[]); #endif -- cgit v1.2.3 From 44cd349d69a11fec6c698ec45a59e373cad56c2e Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Wed, 9 Sep 2020 18:40:11 +0200 Subject: qwe --- src/s_line.c | 7 ++++--- src/s_struct.h | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/s_line.c b/src/s_line.c index b198fff..c6e2e0b 100644 --- a/src/s_line.c +++ b/src/s_line.c @@ -59,6 +59,7 @@ void while (tmp != NULL) { renext = tmp->next; + ft_memdel((void*)&tmp->lblock); ft_memdel((void*)&tmp); tmp = renext; } @@ -72,7 +73,7 @@ t_line_block if ((link = (t_line_block*)malloc(sizeof(t_line_block))) == NULL) return (NULL); - link->lblock[0] = C_NUL; + link->lblock = NULL; link->nextif = nextif; link->next = NULL; if (word == NULL) @@ -80,9 +81,9 @@ t_line_block link->next = NULL; return (link); } - else + else if ((link->lblock = ft_strdup(word)) == NULL) { - ft_strlcpy(link->lblock, word, 4096); + return (NULL); } return (link); } diff --git a/src/s_struct.h b/src/s_struct.h index 2734618..31c80fc 100644 --- a/src/s_struct.h +++ b/src/s_struct.h @@ -60,7 +60,7 @@ struct s_lpipes typedef struct s_line_block { - char lblock[ARG_MAX]; + char *lblock; uint8_t nextif; struct s_line_block *next; } t_line_block; -- cgit v1.2.3 From 880ca01914139002e324b23bf0c4ff64b18481f9 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Thu, 10 Sep 2020 16:35:47 +0200 Subject: TODO update --- TODO.org | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/TODO.org b/TODO.org index 2fea1e9..8d7c42f 100644 --- a/TODO.org +++ b/TODO.org @@ -7,9 +7,9 @@ ** DONE [#A] fix pwd when cd / ** DONE [#A] Multiline pipes (ls |) ** DONE [#A] Go full tok + quotes -** TODO [#A] fix \; \&& \|| +** DONE [#A] fix \; \&& \|| ** TODO [#A] Variables quotes qwe="okokoko kkok" asd='bav' -** TODO [#A] ls qweqwe; echo $? <---- substitute +** DONE [#A] ls qweqwe; echo $? <---- substitute ** TODO [#A] Careful p_line \; ** TODO [#A] <>> ** DONE [#B] forked write(2) stuff on cd -- cgit v1.2.3 From 8e2f717b24db040fd8ae79452e5d05049d39e1dc Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Thu, 10 Sep 2020 16:36:47 +0200 Subject: New TODO update --- TODO.org | 1 + 1 file changed, 1 insertion(+) diff --git a/TODO.org b/TODO.org index 8d7c42f..40afc52 100644 --- a/TODO.org +++ b/TODO.org @@ -9,6 +9,7 @@ ** DONE [#A] Go full tok + quotes ** DONE [#A] fix \; \&& \|| ** TODO [#A] Variables quotes qwe="okokoko kkok" asd='bav' +** TODO [#A] Fix PSX \stuff subst with "" '' ** DONE [#A] ls qweqwe; echo $? <---- substitute ** TODO [#A] Careful p_line \; ** TODO [#A] <>> -- cgit v1.2.3 From f5a385a2384d33c47c9742629a27f40dba184b11 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Thu, 10 Sep 2020 16:44:05 +0200 Subject: Another TODO update --- TODO.org | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TODO.org b/TODO.org index 40afc52..61429e4 100644 --- a/TODO.org +++ b/TODO.org @@ -22,7 +22,7 @@ ** DONE [#C] Variables stack rework ** TODO [#C] Handle memory ** TODO [#C] Go full stack to ken dash in the ass -** TODO [#C] Handle comments better # +** DONE [#C] Handle comments better # ** TODO [#C] msh ~> ./qwe.sh <---- without shebang (maybe works fine already) * Stuff to add -- cgit v1.2.3 From d26eab1a9ae3b5630b5b637929c61dc3de873083 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Thu, 10 Sep 2020 16:45:55 +0200 Subject: qwe --- TODO.org | 1 - 1 file changed, 1 deletion(-) diff --git a/TODO.org b/TODO.org index 61429e4..556105e 100644 --- a/TODO.org +++ b/TODO.org @@ -11,7 +11,6 @@ ** TODO [#A] Variables quotes qwe="okokoko kkok" asd='bav' ** TODO [#A] Fix PSX \stuff subst with "" '' ** DONE [#A] ls qweqwe; echo $? <---- substitute -** TODO [#A] Careful p_line \; ** TODO [#A] <>> ** DONE [#B] forked write(2) stuff on cd ** DONE [#B] Multiline && || -- cgit v1.2.3 From bea195a2e8d8e26af7511a65af4c8641cb4d1c4f Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Thu, 10 Sep 2020 17:28:06 +0200 Subject: Even better parse --- src/p_args.c | 4 +-- src/p_args_len.c | 93 ++++++++++++++--------------------------------------- src/p_args_next.c | 3 +- src/p_lblock_next.c | 12 +++---- src/s_com.c | 2 +- src/u_vars_next.c | 22 +++++++++---- 6 files changed, 49 insertions(+), 87 deletions(-) diff --git a/src/p_args.c b/src/p_args.c index a4b3d02..5276ae1 100644 --- a/src/p_args.c +++ b/src/p_args.c @@ -69,9 +69,9 @@ static uint16_t if (*ptr == C_BACKS) p_meet_bs(ptr, mode); if (*ptr == C_DQUOTE) - mode = u_meet_dquote((char*)word, ptr, mode); + mode = u_meet_dquote(word, ptr, mode); else if (*ptr == C_SQUOTE) - mode = u_meet_squote((char*)word, ptr, mode); + mode = u_meet_squote(word, ptr, mode); if (ft_iswhitespace(*ptr) && p_meet_whitespace((char*)word, ptr, mode) == TRUE) { diff --git a/src/p_args_len.c b/src/p_args_len.c index 801406f..3e5bfbf 100644 --- a/src/p_args_len.c +++ b/src/p_args_len.c @@ -14,87 +14,42 @@ #include #include "d_define.h" +#include "u_parse.h" +#include "u_utils.h" -static size_t - p_skip_delim_size(const char word[], char c, size_t end) +static t_bool + p_meet_whitespace(const char *head, char *ptr, t_quote_mode mode) { - end++; - if (word[end] == c) - return (end + 1); - while (word[end] != C_NUL && word[end] != c) + if (mode == Q_NONE && u_is_not_escaped(head, ptr) == TRUE) { - end++; - if (word[end] == c && c == C_DQUOTE) - { - if (word[end - 1] == C_BACKS && word[end - 2] != C_BACKS) - { - end++; - } - } + return (TRUE); } - if (word[end] != C_NUL) - { - end++; - } - return (end); -} - -static size_t - p_skip_unquote(const char word[], size_t end) -{ - while (word[end] != C_NUL && ft_iswhitespace(word[end]) == FALSE) - { - end++; - if (ft_iswhitespace(word[end]) == TRUE && end == 1 && - word[end - 1] == C_BACKS) - { - end++; - } - else if (ft_iswhitespace(word[end]) == TRUE && end > 1 && - word[end - 1] == C_BACKS && word[end - 2] != C_BACKS) - { - end++; - } - } - return (end); -} - -static size_t - p_skip_quote(const char word[], size_t end) -{ - while (word[end] != C_NUL && ft_iswhitespace(word[end]) == FALSE) - { - if (word[end] == C_SQUOTE || word[end] == C_DQUOTE) - { - end = p_skip_delim_size(word, word[end], end); - } - else - { - while (word[end] != C_NUL && - ft_iswhitespace(word[end]) == FALSE) - { - end++; - } - } - } - return (end); + return (FALSE); } size_t p_arg_len(const char word[], const size_t start) { - size_t end; + t_quote_mode mode; + char *ptr; + size_t end; + t_bool terminate; + mode = Q_NONE; end = start; - if (word[start] != C_SQUOTE && word[start] != C_DQUOTE && - word[start] != C_NUL) - { - end = p_skip_unquote(word, end); - } - else if (word[end] == C_SQUOTE || word[end] == C_DQUOTE) + ptr = (char*)word + start; + terminate = FALSE; + while (*ptr != C_NUL && terminate == FALSE) { - end = p_skip_quote(word, end); + if (*ptr == C_DQUOTE) + mode = u_meet_dquote(word, ptr, mode); + else if (*ptr == C_SQUOTE) + mode = u_meet_squote(word, ptr, mode); + else if (ft_iswhitespace(*ptr)) + terminate = p_meet_whitespace(word, ptr, mode); + ptr++; } - return (end); + ptr -= 1; + return (ptr - word); } diff --git a/src/p_args_next.c b/src/p_args_next.c index 6fd6b08..ea8dd81 100644 --- a/src/p_args_next.c +++ b/src/p_args_next.c @@ -12,6 +12,7 @@ #include #include +#include #include "d_define.h" #include "p_args.h" @@ -49,7 +50,7 @@ uint16_t const uint16_t argc, const size_t start[]) { - char tmp[4096]; + char tmp[ARG_MAX]; uint16_t i; i = 0; diff --git a/src/p_lblock_next.c b/src/p_lblock_next.c index 5777a3c..56a789d 100644 --- a/src/p_lblock_next.c +++ b/src/p_lblock_next.c @@ -59,7 +59,8 @@ char { if (*ptr == '$' && u_is_not_escaped(word, ptr) == TRUE) { - if ((word = p_subst_this_var(&ptr, (ptr - word), word, msh)) == NULL) + if ((word = p_subst_this_var(&ptr, (ptr - word), word, msh)) + == NULL) { return (NULL); } @@ -70,8 +71,7 @@ char } char - **p_subst_home(char *words[], - t_msh *msh) + **p_subst_home(char *words[], t_msh *msh) { char path[PATH_MAX]; char **ptr; @@ -92,8 +92,7 @@ char } static void - p_register_word(char word[], - t_msh *msh) + p_register_word(char word[], t_msh *msh) { char name[255]; char val[255]; @@ -182,8 +181,7 @@ static void } char - **p_check_args_equals(char *words[], - t_msh *msh) + **p_check_args_equals(char *words[], t_msh *msh) { char *ptr; t_bool reg; diff --git a/src/s_com.c b/src/s_com.c index 5bdb36a..0f206c4 100644 --- a/src/s_com.c +++ b/src/s_com.c @@ -118,9 +118,9 @@ t_com return (NULL); if ((words = p_split_args(word, com->redir)) == NULL) return (NULL); + words = p_check_args_equals(words, msh); if ((words = p_subst_home(words, msh)) == NULL) return (NULL); - words = p_check_args_equals(words, msh); if (msh->env_fork_tmp[0][0] != '\0') com_cpy_env_fork(&com, msh); if (fill_com(words, &com) < 0) diff --git a/src/u_vars_next.c b/src/u_vars_next.c index c6c351b..6f63fb4 100644 --- a/src/u_vars_next.c +++ b/src/u_vars_next.c @@ -13,10 +13,12 @@ #include #include #include +#include #include "d_enum.h" #include "s_lvars.h" #include "s_struct.h" +#include "u_parse.h" #include "u_utils.h" static int64_t @@ -63,6 +65,11 @@ static t_bool } } +/* static void */ +/* u_newval_subst(char newval_subst[], const char newval[]) */ +/* { */ +/* } */ + /* ** void ** u_subst_var_value(const char varname[], const char newval[], t_msh *msh); @@ -70,23 +77,24 @@ static t_bool ** DESCRIPTION ** The subst_var_value() changes the value of msh->envp ** variable varname[] with newval[]. If varname[] wasn't found -** in msh->envp, varname[] is searched in msh->vars. +** in msh->envp, varname[] is searched in msh->vars. Otherwise +** varname[] is added to msh->vars. */ void - u_subst_var_value(const char varname[], - const char newval[], - t_msh *msh) + u_subst_var_value(const char varname[], const char newval[], t_msh *msh) { - char new_line_fmt[2048]; + char new_line_fmt[ARG_MAX]; + /* char newval_subst[ARG_MAX]; */ int64_t env_line; if ((env_line = u_get_env_var_line(varname + 1, msh)) > -1) { ft_memdel((void*)&msh->envp[env_line]); + /* ft_sprintf(new_line_fmt, "%s=%s", varname + 1, newval_subst); */ ft_sprintf(new_line_fmt, "%s=%s", varname + 1, newval); - if (!(msh->envp[env_line] = - (char*)malloc((ft_strlen(new_line_fmt) + 1) * sizeof(char)))) + if ((msh->envp[env_line] = (char*)malloc((ft_strlen(new_line_fmt) + 1) * + sizeof(char))) == NULL) { /* TODO: Handle this fail */ } -- cgit v1.2.3 From f2e3a002967da5077a2c6f38e97025c12f4b0986 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Thu, 10 Sep 2020 17:30:53 +0200 Subject: Quick update --- TODO.org | 1 + src/s_com.c | 8 ++++---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/TODO.org b/TODO.org index 556105e..945cae0 100644 --- a/TODO.org +++ b/TODO.org @@ -12,6 +12,7 @@ ** TODO [#A] Fix PSX \stuff subst with "" '' ** DONE [#A] ls qweqwe; echo $? <---- substitute ** TODO [#A] <>> +** TODO [#A] SEGV on pipes ** DONE [#B] forked write(2) stuff on cd ** DONE [#B] Multiline && || ** DONE [#B] ./qwe.sh <=== handle bad shebang diff --git a/src/s_com.c b/src/s_com.c index 0f206c4..dc81046 100644 --- a/src/s_com.c +++ b/src/s_com.c @@ -28,7 +28,7 @@ static int8_t uint64_t j; i = 0; - if (words[0]) + if (words[0] != NULL) { if (((*com)->bin = (char*)malloc((ft_strlen(words[0]) + 1) * sizeof(char))) == NULL) @@ -59,7 +59,7 @@ static int8_t } static void - com_cpy_env_fork(t_com **com, + s_com_cpy_env_fork(t_com **com, t_msh *msh) { size_t i; @@ -118,11 +118,11 @@ t_com return (NULL); if ((words = p_split_args(word, com->redir)) == NULL) return (NULL); - words = p_check_args_equals(words, msh); if ((words = p_subst_home(words, msh)) == NULL) return (NULL); + words = p_check_args_equals(words, msh); if (msh->env_fork_tmp[0][0] != '\0') - com_cpy_env_fork(&com, msh); + s_com_cpy_env_fork(&com, msh); if (fill_com(words, &com) < 0) { ft_delwords(words); -- cgit v1.2.3 From f165808b1cb551cd86655268b8dfa4fb7dcd5eb7 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Thu, 10 Sep 2020 18:13:47 +0200 Subject: FIX --- libft/src/ft_nrealloc.c | 4 ++-- libft/src/ft_strsubst.c | 4 ++-- src/p_args_len.c | 3 ++- src/p_lblock_next.c | 10 ++++++---- src/s_com.c | 7 +++---- src/u_vars_next.c | 6 ++---- 6 files changed, 17 insertions(+), 17 deletions(-) diff --git a/libft/src/ft_nrealloc.c b/libft/src/ft_nrealloc.c index 82975d1..6b42380 100644 --- a/libft/src/ft_nrealloc.c +++ b/libft/src/ft_nrealloc.c @@ -21,7 +21,7 @@ void if (!ptr) { - if (!(ptr = malloc(newsize))) + if ((ptr = malloc(newsize)) == NULL) return (NULL); return (ptr); } @@ -30,7 +30,7 @@ void ft_memdel((void*)&ptr); return (NULL); } - if (!(nptr = malloc(newsize))) + if ((nptr = malloc(newsize)) == NULL) return (ptr); ft_memcpy(nptr, ptr, oldsize); ft_memdel((void*)&ptr); diff --git a/libft/src/ft_strsubst.c b/libft/src/ft_strsubst.c index 57e0ac8..3c5c643 100644 --- a/libft/src/ft_strsubst.c +++ b/libft/src/ft_strsubst.c @@ -20,10 +20,10 @@ char char *nstr; char *ptr; - if (!(ptr = ft_strnstr(str, pattern, ft_strlen(str)))) + if ((ptr = ft_strnstr(str, pattern, ft_strlen(str))) == NULL) return (NULL); nlen = ft_strlen(str) - ft_strlen(pattern) + ft_strlen(subst); - if (!(nstr = (char*)malloc((nlen + 1) * sizeof(char)))) + if ((nstr = (char*)malloc((nlen + 1) * sizeof(char))) == NULL) return (NULL); ft_memcpy(nstr, str, ptr - str); ft_memcpy(nstr + (ptr - str), subst, ft_strlen(subst)); diff --git a/src/p_args_len.c b/src/p_args_len.c index 3e5bfbf..e624b37 100644 --- a/src/p_args_len.c +++ b/src/p_args_len.c @@ -49,7 +49,8 @@ size_t terminate = p_meet_whitespace(word, ptr, mode); ptr++; } - ptr -= 1; + if (*ptr != C_NUL) + ptr -= 1; return (ptr - word); } diff --git a/src/p_lblock_next.c b/src/p_lblock_next.c index 56a789d..8b22f7c 100644 --- a/src/p_lblock_next.c +++ b/src/p_lblock_next.c @@ -28,8 +28,8 @@ static char *p_subst_this_var(char **p, int64_t i, char word[], t_msh *msh) { - char tmp[4096]; - char varval[4096]; + char tmp[ARG_MAX]; + char varval[ARG_MAX]; char *ptr; size_t varlen; @@ -42,7 +42,9 @@ static char ft_strlcpy(tmp, ptr + i, varlen + 1 - i); u_get_var_value(varval, tmp, 4096, msh); ft_strlcpy(tmp, ptr + varlen, varlen); - word = ft_nrealloc(word, i, i + ft_strlen(varval) + ft_strlen(tmp) + 1); + if ((word = ft_nrealloc(word, i, i + ft_strlen(varval) + + ft_strlen(tmp) + 1)) == NULL) + return (NULL); ft_strlcpy(word + i, varval, ft_strlen(varval) + 1); ft_strlcpy(word + (i + ft_strlen(varval)), tmp, ft_strlen(tmp) + 1); *(p) = word + (i + ft_strlen(varval) - 1); @@ -143,7 +145,7 @@ static char j = 0; while (words[i + j] != NULL) j++; - if (!(rewords = (char**)malloc((j + 1) * sizeof(char*)))) + if ((rewords = (char**)malloc((j + 1) * sizeof(char*))) == NULL) { ft_delwords(words); f_alloc_and_destroy_msh(msh); diff --git a/src/s_com.c b/src/s_com.c index dc81046..36018ef 100644 --- a/src/s_com.c +++ b/src/s_com.c @@ -20,8 +20,7 @@ #include "s_struct.h" static int8_t - fill_com(char *words[], - t_com **com) + fill_com(char *words[], t_com **com) { /* TODO: norme */ uint64_t i; @@ -59,8 +58,7 @@ static int8_t } static void - s_com_cpy_env_fork(t_com **com, - t_msh *msh) + s_com_cpy_env_fork(t_com **com, t_msh *msh) { size_t i; size_t j; @@ -120,6 +118,7 @@ t_com return (NULL); if ((words = p_subst_home(words, msh)) == NULL) return (NULL); + ft_printf("%s\n", *words); words = p_check_args_equals(words, msh); if (msh->env_fork_tmp[0][0] != '\0') s_com_cpy_env_fork(&com, msh); diff --git a/src/u_vars_next.c b/src/u_vars_next.c index 6f63fb4..2135c6b 100644 --- a/src/u_vars_next.c +++ b/src/u_vars_next.c @@ -22,8 +22,7 @@ #include "u_utils.h" static int64_t - u_get_env_var_line(const char varname[], - t_msh *msh) + u_get_env_var_line(const char varname[], t_msh *msh) { char **env_dup; int64_t i; @@ -44,8 +43,7 @@ static int64_t } static t_bool - u_get_custom_var_existance(const char varname[], - t_msh *msh) + u_get_custom_var_existance(const char varname[], t_msh *msh) { t_lvars *ptr; -- cgit v1.2.3 From 9e7db36f7afe68a75911d425d496b5e1e9910abe Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Thu, 10 Sep 2020 18:14:01 +0200 Subject: Removed debug printf --- src/s_com.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/s_com.c b/src/s_com.c index 36018ef..95f2d94 100644 --- a/src/s_com.c +++ b/src/s_com.c @@ -118,7 +118,6 @@ t_com return (NULL); if ((words = p_subst_home(words, msh)) == NULL) return (NULL); - ft_printf("%s\n", *words); words = p_check_args_equals(words, msh); if (msh->env_fork_tmp[0][0] != '\0') s_com_cpy_env_fork(&com, msh); -- cgit v1.2.3 From 20b9a703186d7bf06936467ae3e8108cfbee43dd Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Thu, 10 Sep 2020 19:28:08 +0200 Subject: I was born an idiot I swear --- TODO.org | 2 +- src/d_define.h | 1 + src/m_prompt.c | 12 +++++++++--- src/p_lblock_next.c | 23 ++++++++++++++++++++++- 4 files changed, 33 insertions(+), 5 deletions(-) diff --git a/TODO.org b/TODO.org index 945cae0..b38f114 100644 --- a/TODO.org +++ b/TODO.org @@ -8,7 +8,7 @@ ** DONE [#A] Multiline pipes (ls |) ** DONE [#A] Go full tok + quotes ** DONE [#A] fix \; \&& \|| -** TODO [#A] Variables quotes qwe="okokoko kkok" asd='bav' +** DONE [#A] Variables quotes qwe="okokoko kkok" asd='bav' ** TODO [#A] Fix PSX \stuff subst with "" '' ** DONE [#A] ls qweqwe; echo $? <---- substitute ** TODO [#A] <>> diff --git a/src/d_define.h b/src/d_define.h index 8e73668..50b0f3c 100644 --- a/src/d_define.h +++ b/src/d_define.h @@ -68,6 +68,7 @@ #define C_NUL 0x00 #define C_SUB 0x1a +#define C_ESC 0x1b #define C_DQUOTE 0x22 #define C_SQUOTE 0x27 #define C_AMP 0x26 diff --git a/src/m_prompt.c b/src/m_prompt.c index d0269e4..dc17939 100644 --- a/src/m_prompt.c +++ b/src/m_prompt.c @@ -24,15 +24,21 @@ static char *m_subst_prompt_rice(char var[], t_msh *msh) { char tmp[PATH_MAX]; + char *ptr; size_t i; size_t j; i = 0; - while (var[i] != '\0') + while (var[i] != C_NUL) { - if (var[i] == '\\') + if (var[i] == C_BACKS) { - if (var[i + 1] == 'w') + if (var[i + 1] == '0' && var[i + 2] == '3' && var[i + 3] == '3') + { + ft_memmove(var + i, var + i + 3, ft_strlen(var + i + 3) + 1); + var[i] = C_ESC; + } + else if (var[i + 1] == 'w') { u_get_var_value(tmp, "$PWD", PATH_MAX, msh); if (tmp[0] != C_NUL) diff --git a/src/p_lblock_next.c b/src/p_lblock_next.c index 8b22f7c..21cd4a7 100644 --- a/src/p_lblock_next.c +++ b/src/p_lblock_next.c @@ -25,6 +25,24 @@ /* TODO: norme */ +static char + *p_double_them_bs(char varval[]) +{ + char *ptr; + + ptr = varval; + while (*ptr != C_NUL) + { + if (*ptr == C_BACKS) + { + ptr = ft_memmove(ptr + 1, ptr, ft_strlen(ptr) + 1); + varval[ptr - varval] = C_BACKS; + } + ptr++; + } + return (varval); +} + static char *p_subst_this_var(char **p, int64_t i, char word[], t_msh *msh) { @@ -32,6 +50,7 @@ static char char varval[ARG_MAX]; char *ptr; size_t varlen; + int32_t count; ptr = word; varlen = i + 1; @@ -40,13 +59,15 @@ static char ft_iswhitespace(ptr[varlen]) == FALSE) varlen += 1; ft_strlcpy(tmp, ptr + i, varlen + 1 - i); - u_get_var_value(varval, tmp, 4096, msh); + u_get_var_value(varval, tmp, ARG_MAX, msh); + p_double_them_bs(varval); ft_strlcpy(tmp, ptr + varlen, varlen); if ((word = ft_nrealloc(word, i, i + ft_strlen(varval) + ft_strlen(tmp) + 1)) == NULL) return (NULL); ft_strlcpy(word + i, varval, ft_strlen(varval) + 1); ft_strlcpy(word + (i + ft_strlen(varval)), tmp, ft_strlen(tmp) + 1); + count = 0; *(p) = word + (i + ft_strlen(varval) - 1); return (word); } -- cgit v1.2.3 From 89c27ba72e11665823cba7023f694a8639891ccb Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Thu, 10 Sep 2020 19:53:00 +0200 Subject: New libft func, cool stuff --- libft/Makefile | 1 + libft/include/libft.h | 5 ++++- libft/src/ft_strsubst_s.c | 31 +++++++++++++++++++++++++++++++ src/m_prompt.c | 9 ++++----- src/u_vars_next.c | 2 -- 5 files changed, 40 insertions(+), 8 deletions(-) create mode 100644 libft/src/ft_strsubst_s.c diff --git a/libft/Makefile b/libft/Makefile index 431ec0b..3c11199 100644 --- a/libft/Makefile +++ b/libft/Makefile @@ -112,6 +112,7 @@ SRCS_NAME += ft_printf_process.c SRCS_NAME += ft_printf_cat_output.c SRCS_NAME += ft_printf_flag_to_atoi.c SRCS_NAME += ft_strsubst.c +SRCS_NAME += ft_strsubst_s.c #------------------------------------------------------------------------------# SRCS = $(addprefix ${SRCS_DIR},${SRCS_NAME}) #------------------------------------------------------------------------------# diff --git a/libft/include/libft.h b/libft/include/libft.h index 68b8d9b..c31482c 100644 --- a/libft/include/libft.h +++ b/libft/include/libft.h @@ -15,7 +15,7 @@ #include #include -#include +#include # define FT_MIN_HEX_BASE "0123456789abcdef" # define FT_MAJ_HEX_BASE "0123456789ABCDEF" @@ -123,6 +123,9 @@ char *ft_nstr(size_t size); char *ft_strsubst(char *str, const char *pattern, const char *subst); +int8_t ft_strsubst_s(char *str, + const char *pattern, + const char *subst); char *ft_strtok_r(char *s, const char *delim, char **last); char *ft_strtok(char *s, const char *delim); char **ft_split(const char *s, char c); diff --git a/libft/src/ft_strsubst_s.c b/libft/src/ft_strsubst_s.c new file mode 100644 index 0000000..ea61610 --- /dev/null +++ b/libft/src/ft_strsubst_s.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strsubst_s.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include + +int8_t + ft_strsubst_s(char *str, const char *pattern, const char *subst) +{ + char *ptr; + + if ((ptr = ft_strnstr(str, pattern, ft_strlen(str))) == NULL) + return (1); + (void)ft_memmove(str + ((ptr - str) + ft_strlen(subst)), + str + ((ptr - str) + ft_strlen(pattern)), + ft_strlen(str + ((ptr - str) + ft_strlen(pattern))) + 1); + (void)ft_memmove(str + (ptr - str), + subst, + ft_strlen(subst)); + return (0); +} diff --git a/src/m_prompt.c b/src/m_prompt.c index dc17939..2d88088 100644 --- a/src/m_prompt.c +++ b/src/m_prompt.c @@ -24,7 +24,6 @@ static char *m_subst_prompt_rice(char var[], t_msh *msh) { char tmp[PATH_MAX]; - char *ptr; size_t i; size_t j; @@ -42,9 +41,9 @@ static char { u_get_var_value(tmp, "$PWD", PATH_MAX, msh); if (tmp[0] != C_NUL) - var = ft_strsubst(var, "\\w", tmp); + ft_strsubst_s(var, "\\w", tmp); else - var = ft_strsubst(var, "\\w", "?"); + ft_strsubst_s(var, "\\w", "?"); } else if (var[i + 1] == 'W') { @@ -60,10 +59,10 @@ static char (void)ft_memmove(tmp, tmp + j, ((ft_strlen(tmp) - j) + 1) * sizeof(char)); } - var = ft_strsubst(var, "\\W", tmp); + ft_strsubst_s(var, "\\W", tmp); } else - var = ft_strsubst(var, "\\w", "?"); + ft_strsubst_s(var, "\\w", "?"); } else if (var[i + 1] == 'v') var = ft_strsubst(var, "\\v", FT_MSH_VERSION); diff --git a/src/u_vars_next.c b/src/u_vars_next.c index 2135c6b..2f23667 100644 --- a/src/u_vars_next.c +++ b/src/u_vars_next.c @@ -83,13 +83,11 @@ void u_subst_var_value(const char varname[], const char newval[], t_msh *msh) { char new_line_fmt[ARG_MAX]; - /* char newval_subst[ARG_MAX]; */ int64_t env_line; if ((env_line = u_get_env_var_line(varname + 1, msh)) > -1) { ft_memdel((void*)&msh->envp[env_line]); - /* ft_sprintf(new_line_fmt, "%s=%s", varname + 1, newval_subst); */ ft_sprintf(new_line_fmt, "%s=%s", varname + 1, newval); if ((msh->envp[env_line] = (char*)malloc((ft_strlen(new_line_fmt) + 1) * sizeof(char))) == NULL) -- cgit v1.2.3 From 4b84aadfcb1dcc46d686fd984e0566d84e116278 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Thu, 10 Sep 2020 20:21:39 +0200 Subject: LOT to fix --- src/d_define.h | 1 + src/m_prompt.c | 11 ++++++++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/d_define.h b/src/d_define.h index 50b0f3c..6d3cc45 100644 --- a/src/d_define.h +++ b/src/d_define.h @@ -67,6 +67,7 @@ */ #define C_NUL 0x00 +#define C_LF 0x0a #define C_SUB 0x1a #define C_ESC 0x1b #define C_DQUOTE 0x22 diff --git a/src/m_prompt.c b/src/m_prompt.c index 2d88088..ff491c8 100644 --- a/src/m_prompt.c +++ b/src/m_prompt.c @@ -34,9 +34,14 @@ static char { if (var[i + 1] == '0' && var[i + 2] == '3' && var[i + 3] == '3') { - ft_memmove(var + i, var + i + 3, ft_strlen(var + i + 3) + 1); + (void)ft_memmove(var + i, var + i + 3, ft_strlen(var + i + 3) + 1); var[i] = C_ESC; } + else if (var[i + 1] == 'n') + { + (void)ft_memmove(var + i, var + i + 1, ft_strlen(var + i + 1) + 1); + var[i] = C_LF; + } else if (var[i + 1] == 'w') { u_get_var_value(tmp, "$PWD", PATH_MAX, msh); @@ -65,9 +70,9 @@ static char ft_strsubst_s(var, "\\w", "?"); } else if (var[i + 1] == 'v') - var = ft_strsubst(var, "\\v", FT_MSH_VERSION); + ft_strsubst_s(var, "\\v", FT_MSH_VERSION); else if (var[i + 1] == 's') - var = ft_strsubst(var, "\\s", FT_PROGNAME); + ft_strsubst_s(var, "\\s", FT_PROGNAME); } i++; } -- cgit v1.2.3 From 6f756ee6112c723cfe0f8ece2d530b249aa47e00 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Fri, 11 Sep 2020 15:05:31 +0200 Subject: Fix --- src/m_prompt.c | 14 ++++++++++---- src/p_lblock_next.c | 4 ++-- src/s_com.c | 4 ++-- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/m_prompt.c b/src/m_prompt.c index ff491c8..584a98d 100644 --- a/src/m_prompt.c +++ b/src/m_prompt.c @@ -34,14 +34,20 @@ static char { if (var[i + 1] == '0' && var[i + 2] == '3' && var[i + 3] == '3') { - (void)ft_memmove(var + i, var + i + 3, ft_strlen(var + i + 3) + 1); + (void)ft_memmove(var + i, var + (i + 3), (ft_strlen(var + (i + 3)) + 1) * sizeof(char)); var[i] = C_ESC; } else if (var[i + 1] == 'n') { - (void)ft_memmove(var + i, var + i + 1, ft_strlen(var + i + 1) + 1); + (void)ft_memmove(var + i, var + (i + 1), (ft_strlen(var + (i + 1)) + 1) * sizeof(char)); var[i] = C_LF; } + else if (var[i + 1] == 'h' || var[i + 1] == 'H') + { + gethostname(tmp, 255); + (void)ft_memmove(var + i + ft_strlen(tmp), var + (i + 2), (ft_strlen(var + (i + 2)) + 1) * sizeof(char)); + (void)ft_memcpy(var + i, tmp, ft_strlen(tmp) * sizeof(char)); + } else if (var[i + 1] == 'w') { u_get_var_value(tmp, "$PWD", PATH_MAX, msh); @@ -82,11 +88,11 @@ static char static void m_update_psx(uint8_t x, t_msh *msh) { - char var[2048]; + char var[ARG_MAX]; char psx[5]; ft_sprintf(psx, "$PS%hhu", x); - u_get_var_value(var, psx, 2048, msh); + u_get_var_value(var, psx, ARG_MAX, msh); if (var[0] == C_NUL) { msh->ps[x - 1][0] = '\0'; diff --git a/src/p_lblock_next.c b/src/p_lblock_next.c index 21cd4a7..e342f4a 100644 --- a/src/p_lblock_next.c +++ b/src/p_lblock_next.c @@ -61,7 +61,7 @@ static char ft_strlcpy(tmp, ptr + i, varlen + 1 - i); u_get_var_value(varval, tmp, ARG_MAX, msh); p_double_them_bs(varval); - ft_strlcpy(tmp, ptr + varlen, varlen); + ft_strlcpy(tmp, ptr + varlen, varlen + 1); if ((word = ft_nrealloc(word, i, i + ft_strlen(varval) + ft_strlen(tmp) + 1)) == NULL) return (NULL); @@ -150,9 +150,9 @@ static char int64_t i, t_msh *msh) { + char **rewords; int64_t j; int64_t k; - char **rewords; j = 0; if (reg == TRUE) diff --git a/src/s_com.c b/src/s_com.c index 95f2d94..ee55c58 100644 --- a/src/s_com.c +++ b/src/s_com.c @@ -96,8 +96,7 @@ void } t_com - *s_com_new(char word[], - t_msh *msh) + *s_com_new(char word[], t_msh *msh) { t_com *com; char **words; @@ -114,6 +113,7 @@ t_com return (NULL); if ((word = p_subst_vars(word, msh)) == NULL) return (NULL); + msh->curr->lblock = word; if ((words = p_split_args(word, com->redir)) == NULL) return (NULL); if ((words = p_subst_home(words, msh)) == NULL) -- cgit v1.2.3 From a0f1c28e809f036ebb06d9c017aceef032139316 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Fri, 11 Sep 2020 15:15:22 +0200 Subject: Huge fix --- src/p_lblock_next.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/p_lblock_next.c b/src/p_lblock_next.c index e342f4a..e5e46dc 100644 --- a/src/p_lblock_next.c +++ b/src/p_lblock_next.c @@ -61,7 +61,7 @@ static char ft_strlcpy(tmp, ptr + i, varlen + 1 - i); u_get_var_value(varval, tmp, ARG_MAX, msh); p_double_them_bs(varval); - ft_strlcpy(tmp, ptr + varlen, varlen + 1); + ft_strlcpy(tmp, ptr + varlen, ft_strlen(ptr + varlen) + 1); if ((word = ft_nrealloc(word, i, i + ft_strlen(varval) + ft_strlen(tmp) + 1)) == NULL) return (NULL); @@ -69,6 +69,7 @@ static char ft_strlcpy(word + (i + ft_strlen(varval)), tmp, ft_strlen(tmp) + 1); count = 0; *(p) = word + (i + ft_strlen(varval) - 1); + ft_printf("[%s]\n", word); return (word); } -- cgit v1.2.3 From a1120710a40fd1ac8fae1d68da9c3fe3fb407da2 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Fri, 11 Sep 2020 15:27:54 +0200 Subject: TODO update --- TODO.org | 2 +- src/p_lblock_next.c | 1 - src/s_com.c | 4 ++-- src/s_lpipes.c | 3 +-- 4 files changed, 4 insertions(+), 6 deletions(-) diff --git a/TODO.org b/TODO.org index b38f114..e52ad7d 100644 --- a/TODO.org +++ b/TODO.org @@ -9,7 +9,7 @@ ** DONE [#A] Go full tok + quotes ** DONE [#A] fix \; \&& \|| ** DONE [#A] Variables quotes qwe="okokoko kkok" asd='bav' -** TODO [#A] Fix PSX \stuff subst with "" '' +** DONE [#A] Fix PSX \stuff subst with "" '' ** DONE [#A] ls qweqwe; echo $? <---- substitute ** TODO [#A] <>> ** TODO [#A] SEGV on pipes diff --git a/src/p_lblock_next.c b/src/p_lblock_next.c index e5e46dc..8d89cf3 100644 --- a/src/p_lblock_next.c +++ b/src/p_lblock_next.c @@ -69,7 +69,6 @@ static char ft_strlcpy(word + (i + ft_strlen(varval)), tmp, ft_strlen(tmp) + 1); count = 0; *(p) = word + (i + ft_strlen(varval) - 1); - ft_printf("[%s]\n", word); return (word); } diff --git a/src/s_com.c b/src/s_com.c index ee55c58..6d6ee09 100644 --- a/src/s_com.c +++ b/src/s_com.c @@ -20,7 +20,7 @@ #include "s_struct.h" static int8_t - fill_com(char *words[], t_com **com) + s_fill_com(char *words[], t_com **com) { /* TODO: norme */ uint64_t i; @@ -121,7 +121,7 @@ t_com words = p_check_args_equals(words, msh); if (msh->env_fork_tmp[0][0] != '\0') s_com_cpy_env_fork(&com, msh); - if (fill_com(words, &com) < 0) + if (s_fill_com(words, &com) < 0) { ft_delwords(words); return (NULL); diff --git a/src/s_lpipes.c b/src/s_lpipes.c index 0c1a6ea..04320b8 100644 --- a/src/s_lpipes.c +++ b/src/s_lpipes.c @@ -62,8 +62,7 @@ void } struct s_lpipes - *s_lpipes_new(const char pipedword[], - t_msh *msh) + *s_lpipes_new(const char pipedword[], t_msh *msh) { struct s_lpipes *link; -- cgit v1.2.3 From b2ffd2aeb4a91501ae7931cb43a9679b01eb0351 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Fri, 11 Sep 2020 16:25:51 +0200 Subject: Nice fix --- TODO.org | 3 ++- src/d_define.h | 1 + src/p_args_len.c | 4 ++-- src/p_lblock_next.c | 33 +++++++++++++++------------------ src/s_com.c | 13 ++++++++----- 5 files changed, 28 insertions(+), 26 deletions(-) diff --git a/TODO.org b/TODO.org index e52ad7d..b256a0f 100644 --- a/TODO.org +++ b/TODO.org @@ -11,8 +11,9 @@ ** DONE [#A] Variables quotes qwe="okokoko kkok" asd='bav' ** DONE [#A] Fix PSX \stuff subst with "" '' ** DONE [#A] ls qweqwe; echo $? <---- substitute +** TODO [#A] Don't fuck with '$vars' ** TODO [#A] <>> -** TODO [#A] SEGV on pipes +** DONE [#A] SEGV on pipes ** DONE [#B] forked write(2) stuff on cd ** DONE [#B] Multiline && || ** DONE [#B] ./qwe.sh <=== handle bad shebang diff --git a/src/d_define.h b/src/d_define.h index 6d3cc45..0e62f36 100644 --- a/src/d_define.h +++ b/src/d_define.h @@ -71,6 +71,7 @@ #define C_SUB 0x1a #define C_ESC 0x1b #define C_DQUOTE 0x22 +#define C_DOLLAR 0x24 #define C_SQUOTE 0x27 #define C_AMP 0x26 #define C_SEMIC 0x3b diff --git a/src/p_args_len.c b/src/p_args_len.c index e624b37..729e5c5 100644 --- a/src/p_args_len.c +++ b/src/p_args_len.c @@ -45,11 +45,11 @@ size_t mode = u_meet_dquote(word, ptr, mode); else if (*ptr == C_SQUOTE) mode = u_meet_squote(word, ptr, mode); - else if (ft_iswhitespace(*ptr)) + else if (ft_iswhitespace(*ptr) == TRUE) terminate = p_meet_whitespace(word, ptr, mode); ptr++; } - if (*ptr != C_NUL) + if (terminate == TRUE) ptr -= 1; return (ptr - word); } diff --git a/src/p_lblock_next.c b/src/p_lblock_next.c index 8d89cf3..a5314f2 100644 --- a/src/p_lblock_next.c +++ b/src/p_lblock_next.c @@ -19,6 +19,7 @@ #include "s_destroy.h" #include "f_fail.h" #include "s_struct.h" +#include "u_parse.h" #include "u_utils.h" #include "u_vars.h" #include "u_vars_next.h" @@ -43,14 +44,13 @@ static char return (varval); } -static char - *p_subst_this_var(char **p, int64_t i, char word[], t_msh *msh) +static void + p_subst_this_var(char **p, int64_t i, char word[], t_msh *msh) { char tmp[ARG_MAX]; char varval[ARG_MAX]; char *ptr; size_t varlen; - int32_t count; ptr = word; varlen = i + 1; @@ -61,32 +61,29 @@ static char ft_strlcpy(tmp, ptr + i, varlen + 1 - i); u_get_var_value(varval, tmp, ARG_MAX, msh); p_double_them_bs(varval); - ft_strlcpy(tmp, ptr + varlen, ft_strlen(ptr + varlen) + 1); - if ((word = ft_nrealloc(word, i, i + ft_strlen(varval) + - ft_strlen(tmp) + 1)) == NULL) - return (NULL); - ft_strlcpy(word + i, varval, ft_strlen(varval) + 1); - ft_strlcpy(word + (i + ft_strlen(varval)), tmp, ft_strlen(tmp) + 1); - count = 0; + (void)ft_memmove(ptr + (i + ft_strlen(tmp)), ptr + varlen, (ft_strlen(ptr + varlen) + 1) * sizeof(char)); + (void)ft_memmove(word + i, varval, ft_strlen(varval) * sizeof(char)); *(p) = word + (i + ft_strlen(varval) - 1); - return (word); } char *p_subst_vars(char word[], t_msh *msh) { - char *ptr; + char *ptr; + t_quote_mode mode; + mode = Q_NONE; ptr = word; while (*ptr != C_NUL) { - if (*ptr == '$' && u_is_not_escaped(word, ptr) == TRUE) + if (*ptr == C_DQUOTE) + mode = u_meet_dquote(word, ptr, mode); + else if (*ptr == C_SQUOTE) + mode = u_meet_squote(word, ptr, mode); + if ((mode == Q_NONE || mode == Q_DOUBLE) && *ptr == C_DOLLAR && + u_is_not_escaped(word, ptr) == TRUE) { - if ((word = p_subst_this_var(&ptr, (ptr - word), word, msh)) - == NULL) - { - return (NULL); - } + p_subst_this_var(&ptr, (ptr - word), word, msh); } ptr++; } diff --git a/src/s_com.c b/src/s_com.c index 6d6ee09..277d996 100644 --- a/src/s_com.c +++ b/src/s_com.c @@ -12,7 +12,9 @@ #include #include +#include +#include "d_define.h" #include "f_fail.h" #include "p_args.h" #include "p_lblock.h" @@ -98,6 +100,7 @@ void t_com *s_com_new(char word[], t_msh *msh) { + char nword[ARG_MAX]; t_com *com; char **words; @@ -109,12 +112,12 @@ t_com com->rdrfd = 0; com->rdrpath = NULL; com->env_fork = NULL; - if (p_get_redir(word, &com) != 0) + nword[0] = C_NUL; + ft_strlcpy(nword, word, ARG_MAX); + if (p_get_redir(nword, &com) != 0) return (NULL); - if ((word = p_subst_vars(word, msh)) == NULL) - return (NULL); - msh->curr->lblock = word; - if ((words = p_split_args(word, com->redir)) == NULL) + p_subst_vars(nword, msh); + if ((words = p_split_args(nword, com->redir)) == NULL) return (NULL); if ((words = p_subst_home(words, msh)) == NULL) return (NULL); -- cgit v1.2.3 From b7cd94e75fa1728d28cd92fbd573c560d93b2c5a Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Fri, 11 Sep 2020 16:26:01 +0200 Subject: TODO update --- TODO.org | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TODO.org b/TODO.org index b256a0f..f36600b 100644 --- a/TODO.org +++ b/TODO.org @@ -11,7 +11,7 @@ ** DONE [#A] Variables quotes qwe="okokoko kkok" asd='bav' ** DONE [#A] Fix PSX \stuff subst with "" '' ** DONE [#A] ls qweqwe; echo $? <---- substitute -** TODO [#A] Don't fuck with '$vars' +** DONE [#A] Don't fuck with '$vars' ** TODO [#A] <>> ** DONE [#A] SEGV on pipes ** DONE [#B] forked write(2) stuff on cd -- cgit v1.2.3 From e83b29e6ed44be9a8f7b7e649def02b79a2c0c30 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Fri, 11 Sep 2020 16:29:18 +0200 Subject: TODO update --- TODO.org | 1 + 1 file changed, 1 insertion(+) diff --git a/TODO.org b/TODO.org index f36600b..652a9da 100644 --- a/TODO.org +++ b/TODO.org @@ -25,6 +25,7 @@ ** TODO [#C] Go full stack to ken dash in the ass ** DONE [#C] Handle comments better # ** TODO [#C] msh ~> ./qwe.sh <---- without shebang (maybe works fine already) +** TODO [#C] Fix ~ only first pos of arg * Stuff to add ** DONE [#A] builtins to pipes -- cgit v1.2.3 From 0ab424df075f09aac376890a9a5a73cb74e053bf Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Fri, 11 Sep 2020 16:56:23 +0200 Subject: alias in progress --- Makefile | 3 ++- src/b_alias.c | 23 +++++++++++++++++++++++ src/b_alias.h | 22 ++++++++++++++++++++++ src/b_builtins.h | 1 + src/d_define.h | 2 +- src/d_enum.h | 1 + src/m_funptr.c | 3 ++- src/s_init.c | 6 +++--- src/s_struct.h | 22 +++++++++++++++------- 9 files changed, 70 insertions(+), 13 deletions(-) create mode 100644 src/b_alias.c create mode 100644 src/b_alias.h diff --git a/Makefile b/Makefile index 4e890fe..16aa45a 100644 --- a/Makefile +++ b/Makefile @@ -15,7 +15,8 @@ LFT_SRCS_DIR = ${LFT_DIR}src/ #==============================================================================# #--------------------------------- FILES --------------------------------------# #==============================================================================# -SRCS_NAME = b_cd +SRCS_NAME = b_alias +SRCS_NAME += b_cd SRCS_NAME += b_echo SRCS_NAME += b_env SRCS_NAME += b_exit diff --git a/src/b_alias.c b/src/b_alias.c new file mode 100644 index 0000000..6848b68 --- /dev/null +++ b/src/b_alias.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* b_alias.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include + +#include "s_struct.h" + +uint8_t + b_alias(char *args[], t_msh *msh) +{ + (void)args; + (void)msh; + return (0); +} diff --git a/src/b_alias.h b/src/b_alias.h new file mode 100644 index 0000000..a4aa1d8 --- /dev/null +++ b/src/b_alias.h @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* b_alias.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef B_ALIAS_H +#define B_ALIAS_H + +#include + +#include "s_struct.h" + +uint8_t b_alias(char *args[], t_msh *msh); + +#endif diff --git a/src/b_builtins.h b/src/b_builtins.h index 0cb430a..62b2a41 100644 --- a/src/b_builtins.h +++ b/src/b_builtins.h @@ -13,6 +13,7 @@ #ifndef B_BUILTINS_H #define B_BUILTINS_H +#include "b_alias.h" #include "b_cd.h" #include "b_echo.h" #include "b_env.h" diff --git a/src/d_define.h b/src/d_define.h index 0e62f36..897b720 100644 --- a/src/d_define.h +++ b/src/d_define.h @@ -19,7 +19,7 @@ ** ====== CLASSICS ====== */ -#define FT_BUILTINS "echo|cd|pwd|export|unset|env|exit|type|[" +#define FT_BUILTINS "echo|cd|pwd|export|unset|env|exit|type|[|alias" /* ** ====== PSX ====== diff --git a/src/d_enum.h b/src/d_enum.h index e5078a7..69d34c9 100644 --- a/src/d_enum.h +++ b/src/d_enum.h @@ -50,6 +50,7 @@ enum FT_ID_EXIT, FT_ID_TYPE, FT_ID_SQB, + FT_ID_ALIAS, FT_BUILTINS_COUNT }; diff --git a/src/m_funptr.c b/src/m_funptr.c index a6b31f2..1983e73 100644 --- a/src/m_funptr.c +++ b/src/m_funptr.c @@ -31,7 +31,8 @@ void msh->bu_ptr[FT_ID_EXIT] = b_exit; msh->bu_ptr[FT_ID_TYPE] = b_type; msh->bu_ptr[FT_ID_SQB] = b_sqb; - if (!(msh->bu_ref = ft_split(FT_BUILTINS, '|'))) + msh->bu_ptr[FT_ID_ALIAS] = b_alias; + if ((msh->bu_ref = ft_split(FT_BUILTINS, '|')) == NULL) { f_alloc_and_destroy_msh(msh); } diff --git a/src/s_init.c b/src/s_init.c index cfe2eeb..fb63548 100644 --- a/src/s_init.c +++ b/src/s_init.c @@ -163,11 +163,10 @@ t_msh char cwd[PATH_MAX]; t_msh *msh; - if (!(msh = (t_msh*)malloc(sizeof(t_msh)))) + if ((msh = (t_msh*)malloc(sizeof(t_msh))) == NULL) return (NULL); - if (!(msh->shname = ft_strdup(argv[0]))) + if ((msh->shname = ft_strdup(argv[0])) == NULL) return (NULL); - /* TODO: shname: care about "./", try with symlinks */ msh->envp = NULL; if ((msh->envp = dupenv(envp)) == NULL) return (NULL); @@ -177,6 +176,7 @@ t_msh msh->com = NULL; msh->pipes = NULL; msh->vars = NULL; + msh->alias = NULL; set_cwd(cwd, msh); if ((msh->cwd = ft_strdup(cwd)) == NULL) return (NULL); diff --git a/src/s_struct.h b/src/s_struct.h index 31c80fc..b45af9f 100644 --- a/src/s_struct.h +++ b/src/s_struct.h @@ -18,6 +18,20 @@ #include "d_define.h" +typedef struct s_lalias +{ + char *name; + char *value; + struct s_lalias *next; +} t_lalias; + +typedef struct s_lvars +{ + struct s_lvars *next; + char *name; + char *val; +} t_lvars; + /* ** redir(int8_t) index ** ------------------- @@ -27,13 +41,6 @@ ** 0: means no redirection */ -typedef struct s_lvars -{ - struct s_lvars *next; - char *name; - char *val; -} t_lvars; - typedef struct s_com { char **argv; @@ -71,6 +78,7 @@ typedef struct s_msh struct s_com *com; struct s_lpipes *pipes; struct s_lvars *vars; + struct s_lalias *alias; char **envp; char **bu_ref; char ps[4][1024]; -- cgit v1.2.3 From 9d23dbe9fded1100b50073d29d06d99b4adb37fa Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Fri, 11 Sep 2020 17:01:33 +0200 Subject: Preparing files --- src/s_alias.c | 0 src/s_alias.h | 0 src/s_lvars.c | 13 ++++--------- src/s_struct.h | 14 +++++++------- src/u_alias.c | 0 src/u_alias.h | 0 6 files changed, 11 insertions(+), 16 deletions(-) create mode 100644 src/s_alias.c create mode 100644 src/s_alias.h create mode 100644 src/u_alias.c create mode 100644 src/u_alias.h diff --git a/src/s_alias.c b/src/s_alias.c new file mode 100644 index 0000000..e69de29 diff --git a/src/s_alias.h b/src/s_alias.h new file mode 100644 index 0000000..e69de29 diff --git a/src/s_lvars.c b/src/s_lvars.c index 9369f84..ff19d92 100644 --- a/src/s_lvars.c +++ b/src/s_lvars.c @@ -19,9 +19,7 @@ #include "s_struct.h" void - lvars_rebind(t_lvars **lvars, - const char name[], - const char newval[]) + lvars_rebind(t_lvars **lvars, const char name[], const char newval[]) { t_lvars *tmp; @@ -43,8 +41,7 @@ void } void - lvars_delone(t_lvars **lvars, - const char name[]) + lvars_delone(t_lvars **lvars, const char name[]) { t_lvars *tmp; t_lvars *prev; @@ -72,8 +69,7 @@ void } void - lvars_add_front(t_lvars **alvars, - t_lvars *new) + lvars_add_front(t_lvars **alvars, t_lvars *new) { if (!alvars || !new) { @@ -104,8 +100,7 @@ void } t_lvars - *lvars_new(const char name[], - const char val[]) + *lvars_new(const char name[], const char val[]) { t_lvars *link; diff --git a/src/s_struct.h b/src/s_struct.h index b45af9f..d678c36 100644 --- a/src/s_struct.h +++ b/src/s_struct.h @@ -18,6 +18,13 @@ #include "d_define.h" +typedef struct s_lvars +{ + char *name; + char *val; + struct s_lvars *next; +} t_lvars; + typedef struct s_lalias { char *name; @@ -25,13 +32,6 @@ typedef struct s_lalias struct s_lalias *next; } t_lalias; -typedef struct s_lvars -{ - struct s_lvars *next; - char *name; - char *val; -} t_lvars; - /* ** redir(int8_t) index ** ------------------- diff --git a/src/u_alias.c b/src/u_alias.c new file mode 100644 index 0000000..e69de29 diff --git a/src/u_alias.h b/src/u_alias.h new file mode 100644 index 0000000..e69de29 -- cgit v1.2.3 From eca7d2ebbc4db224a335837111771e5573bc30f2 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Fri, 11 Sep 2020 17:16:20 +0200 Subject: Better --- Makefile | 1 + src/s_alias.c | 0 src/s_alias.h | 0 src/s_destroy.c | 1 + src/s_lalias.c | 18 ++++++++++++++++++ src/s_lalias.h | 20 ++++++++++++++++++++ src/s_lvars.c | 2 +- src/u_alias.c | 0 src/u_alias.h | 0 9 files changed, 41 insertions(+), 1 deletion(-) delete mode 100644 src/s_alias.c delete mode 100644 src/s_alias.h create mode 100644 src/s_lalias.c create mode 100644 src/s_lalias.h delete mode 100644 src/u_alias.c delete mode 100644 src/u_alias.h diff --git a/Makefile b/Makefile index 16aa45a..8154c48 100644 --- a/Makefile +++ b/Makefile @@ -60,6 +60,7 @@ SRCS_NAME += m_redirs SRCS_NAME += s_destroy SRCS_NAME += s_com SRCS_NAME += s_init +SRCS_NAME += s_lalias SRCS_NAME += s_line SRCS_NAME += s_lvars SRCS_NAME += s_lpipes diff --git a/src/s_alias.c b/src/s_alias.c deleted file mode 100644 index e69de29..0000000 diff --git a/src/s_alias.h b/src/s_alias.h deleted file mode 100644 index e69de29..0000000 diff --git a/src/s_destroy.c b/src/s_destroy.c index 97a47d8..6626bd9 100644 --- a/src/s_destroy.c +++ b/src/s_destroy.c @@ -23,5 +23,6 @@ void ft_delwords(msh->bu_ref); ft_delwords(msh->envp); lvars_clear(&msh->vars); + s_lalias_clear(&msh->alias); ft_memdel((void*)&msh); } diff --git a/src/s_lalias.c b/src/s_lalias.c new file mode 100644 index 0000000..3bbee14 --- /dev/null +++ b/src/s_lalias.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* s_lalias.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include "s_struct.h" + +t_lalias +*s_lalias_new(const char name[], const char val[]) +{ +} diff --git a/src/s_lalias.h b/src/s_lalias.h new file mode 100644 index 0000000..8add358 --- /dev/null +++ b/src/s_lalias.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* s_lalias.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef S_LALIAS_H +#define S_LALIAS_H + +#include "s_struct.h" + +t_lalias *s_lalias_new(const char name[], const char val[]); + +#endif diff --git a/src/s_lvars.c b/src/s_lvars.c index ff19d92..3eb4943 100644 --- a/src/s_lvars.c +++ b/src/s_lvars.c @@ -85,7 +85,7 @@ void t_lvars *tmp; t_lvars *renext; - if (!lvars) + if (lvars == NULL) return ; tmp = *lvars; while (tmp) diff --git a/src/u_alias.c b/src/u_alias.c deleted file mode 100644 index e69de29..0000000 diff --git a/src/u_alias.h b/src/u_alias.h deleted file mode 100644 index e69de29..0000000 -- cgit v1.2.3 From 6e74d376f1a888f4730b9fa39df1b34b251eb3f5 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Fri, 11 Sep 2020 17:45:49 +0200 Subject: Aliases list in progress --- Makefile | 1 + src/s_destroy.c | 1 + src/s_lalias.c | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++- src/s_lalias.h | 5 ++++ src/s_lvars.c | 17 ++++++------- src/s_struct.h | 2 +- src/u_alias.c | 43 +++++++++++++++++++++++++++++++ src/u_alias.h | 26 +++++++++++++++++++ src/u_vars.h | 2 ++ 9 files changed, 164 insertions(+), 11 deletions(-) create mode 100644 src/u_alias.c create mode 100644 src/u_alias.h diff --git a/Makefile b/Makefile index 8154c48..77c270d 100644 --- a/Makefile +++ b/Makefile @@ -72,6 +72,7 @@ SRCS_NAME += p_line SRCS_NAME += p_lblock SRCS_NAME += p_lblock_next SRCS_NAME += p_split +SRCS_NAME += u_alias SRCS_NAME += u_parse SRCS_NAME += u_utils SRCS_NAME += u_vars diff --git a/src/s_destroy.c b/src/s_destroy.c index 6626bd9..f9ce3c4 100644 --- a/src/s_destroy.c +++ b/src/s_destroy.c @@ -13,6 +13,7 @@ #include #include "s_destroy.h" +#include "s_lalias.h" #include "s_lvars.h" void diff --git a/src/s_lalias.c b/src/s_lalias.c index 3bbee14..b4b9430 100644 --- a/src/s_lalias.c +++ b/src/s_lalias.c @@ -10,9 +10,85 @@ /* */ /* ************************************************************************** */ +#include +#include +#include +#include +#include + #include "s_struct.h" +void + s_lalias_rebind(t_lalias **lalias, + const char newname[], + const char newval[]) +{ + t_lalias *tmp; + + tmp = *lalias; + while (tmp != NULL && + ft_strncmp(tmp->name, newname, ft_strlen(newname) + 1) != 0) + { + tmp = tmp->next; + } + if (tmp == NULL) + { + return ; + } + ft_memdel((void*)&tmp->val); + if ((tmp->val = ft_strdup(newval)) == NULL) + { + ft_dprintf(STDERR_FILENO, "%s\n", strerror(errno)); + } +} + +void + s_lalias_add_front(t_lalias **lalias, t_lalias *new) +{ + if (lalias == NULL || new == NULL) + { + return ; + } + new->next = *lalias; + *lalias = new; +} + +void + s_lalias_clear(t_lalias **lalias) +{ + t_lalias *tmp; + t_lalias *renext; + + if (lalias == NULL) + return ; + tmp = *lalias; + while (tmp != NULL) + { + renext = tmp->next; + ft_memdel((void*)&tmp->name); + ft_memdel((void*)&tmp->val); + ft_memdel((void*)&tmp); + } + *lalias = NULL; +} + t_lalias -*s_lalias_new(const char name[], const char val[]) + *s_lalias_new(const char name[], const char val[]) { + t_lalias *link; + + if ((link = (t_lalias*)malloc(sizeof(t_lalias))) == NULL) + { + return (NULL); + } + if ((link->name = ft_strdup(name)) == NULL) + { + return (NULL); + } + if ((link->val = ft_strdup(val)) == NULL) + { + return (NULL); + } + link->next = NULL; + return (link); } diff --git a/src/s_lalias.h b/src/s_lalias.h index 8add358..5218a88 100644 --- a/src/s_lalias.h +++ b/src/s_lalias.h @@ -15,6 +15,11 @@ #include "s_struct.h" +void s_lalias_rebind(t_lalias **lalias, + const char name[], + const char newval[]); +void s_lalias_add_front(t_lalias **lalias, t_lalias *new); +void s_lalias_clear(t_lalias **lalias); t_lalias *s_lalias_new(const char name[], const char val[]); #endif diff --git a/src/s_lvars.c b/src/s_lvars.c index 3eb4943..e4b054e 100644 --- a/src/s_lvars.c +++ b/src/s_lvars.c @@ -11,8 +11,8 @@ /* ************************************************************************** */ #include -#include #include +#include #include #include @@ -24,7 +24,7 @@ void t_lvars *tmp; tmp = *lvars; - while (tmp && ft_strncmp(tmp->name, name, ft_strlen(name) + 1)) + while (tmp != NULL && ft_strncmp(tmp->name, name, ft_strlen(name) + 1) != 0) { tmp = tmp->next; } @@ -33,10 +33,9 @@ void return ; } ft_memdel((void*)&tmp->val); - if (!(tmp->val = ft_strdup(newval))) + if ((tmp->val = ft_strdup(newval)) == NULL) { ft_dprintf(STDERR_FILENO, "%s\n", strerror(errno)); - exit(FT_RET_ALLOC); } } @@ -71,7 +70,7 @@ void void lvars_add_front(t_lvars **alvars, t_lvars *new) { - if (!alvars || !new) + if (alvars == NULL || new == NULL) { return ; } @@ -88,7 +87,7 @@ void if (lvars == NULL) return ; tmp = *lvars; - while (tmp) + while (tmp != NULL) { renext = tmp->next; ft_memdel((void*)&tmp->name); @@ -104,15 +103,15 @@ t_lvars { t_lvars *link; - if (!(link = (t_lvars*)malloc(sizeof(t_lvars)))) + if ((link = (t_lvars*)malloc(sizeof(t_lvars))) == NULL) { return (NULL); } - if (!(link->name = ft_strdup(name))) + if ((link->name = ft_strdup(name)) == NULL) { return (NULL); } - if (!(link->val = ft_strdup(val))) + if ((link->val = ft_strdup(val)) == NULL) { return (NULL); } diff --git a/src/s_struct.h b/src/s_struct.h index d678c36..963d5ad 100644 --- a/src/s_struct.h +++ b/src/s_struct.h @@ -28,7 +28,7 @@ typedef struct s_lvars typedef struct s_lalias { char *name; - char *value; + char *val; struct s_lalias *next; } t_lalias; diff --git a/src/u_alias.c b/src/u_alias.c new file mode 100644 index 0000000..6253d60 --- /dev/null +++ b/src/u_alias.c @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* u_alias.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include + +#include "d_define.h" +#include "s_struct.h" + +void + u_get_alias_value(char str[], const char name[], size_t dstsize, t_msh *msh) +{ + t_lalias *ptr; + + str[0] = C_NUL; + ptr = msh->alias; + while (ptr != NULL && ft_strncmp(tmp->name, name, ft_strlen(name)) != 0) + { + ptr = ptr->next; + } + if (ptr != NULL) + { + ft_strlcpy(str, tmp->value, dstsize); + } +} + +void + u_set_alias_value(const char name[], const char value[], t_msh *msh) +{ + t_lalias *tmp; + + tmp = msh->alias; + while (tmp) +} diff --git a/src/u_alias.h b/src/u_alias.h new file mode 100644 index 0000000..c660484 --- /dev/null +++ b/src/u_alias.h @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* u_alias.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef U_ALIAS_H +#define U_ALIAS_H + +#include + +#include "s_struct.h" + +void u_get_alias_value(char str[], + const char name[], + size_t dstsize, + t_msh *msh); +void u_set_alias_value(const char name[], const char value[], t_msh *msh); + +#endif diff --git a/src/u_vars.h b/src/u_vars.h index fbdaffc..27c975f 100644 --- a/src/u_vars.h +++ b/src/u_vars.h @@ -13,6 +13,8 @@ #ifndef U_VARS_H #define U_VARS_H +#include + #include "s_struct.h" void u_get_custom_var(char str[], -- cgit v1.2.3 From cf39c418a49808eb63cf4e1b5e2e363455f2da84 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Fri, 11 Sep 2020 17:58:05 +0200 Subject: alias utils prepared --- src/m_loop.c | 1 + src/s_lalias.c | 7 ++----- src/u_alias.c | 36 ++++++++++++++++++++++++++++-------- src/u_alias.h | 2 +- 4 files changed, 32 insertions(+), 14 deletions(-) diff --git a/src/m_loop.c b/src/m_loop.c index 50357eb..bd4fb1c 100644 --- a/src/m_loop.c +++ b/src/m_loop.c @@ -26,6 +26,7 @@ #include "s_lpipes.h" #include "s_line.h" #include "u_vars.h" +#include "u_alias.h" static void m_parse_and_run_line(char line[], t_msh *msh) diff --git a/src/s_lalias.c b/src/s_lalias.c index b4b9430..eeac08b 100644 --- a/src/s_lalias.c +++ b/src/s_lalias.c @@ -19,15 +19,12 @@ #include "s_struct.h" void - s_lalias_rebind(t_lalias **lalias, - const char newname[], - const char newval[]) + s_lalias_rebind(t_lalias **lalias, const char name[], const char newval[]) { t_lalias *tmp; tmp = *lalias; - while (tmp != NULL && - ft_strncmp(tmp->name, newname, ft_strlen(newname) + 1) != 0) + while (tmp != NULL && ft_strncmp(tmp->name, name, ft_strlen(name) + 1) != 0) { tmp = tmp->next; } diff --git a/src/u_alias.c b/src/u_alias.c index 6253d60..f273a5c 100644 --- a/src/u_alias.c +++ b/src/u_alias.c @@ -11,33 +11,53 @@ /* ************************************************************************** */ #include +#include #include #include "d_define.h" +#include "s_lalias.h" #include "s_struct.h" -void +uint8_t u_get_alias_value(char str[], const char name[], size_t dstsize, t_msh *msh) { t_lalias *ptr; - str[0] = C_NUL; + if (str != NULL) + { + str[0] = C_NUL; + } ptr = msh->alias; - while (ptr != NULL && ft_strncmp(tmp->name, name, ft_strlen(name)) != 0) + while (ptr != NULL && ft_strncmp(ptr->name, name, ft_strlen(name) + 1) != 0) { ptr = ptr->next; } - if (ptr != NULL) + if (ptr == NULL) + { + return (1); + } + if (str != NULL) { - ft_strlcpy(str, tmp->value, dstsize); + ft_strlcpy(str, ptr->val, dstsize); } + return (0); } void u_set_alias_value(const char name[], const char value[], t_msh *msh) { - t_lalias *tmp; + t_lalias *new; - tmp = msh->alias; - while (tmp) + if (u_get_alias_value(NULL, name, 0, msh) != 0) + { + s_lalias_rebind(&msh->alias, name, value); + } + else + { + if ((new = s_lalias_new(name, value)) == NULL) + { + return ; + } + s_lalias_add_front(&msh->alias, new); + } } diff --git a/src/u_alias.h b/src/u_alias.h index c660484..59860db 100644 --- a/src/u_alias.h +++ b/src/u_alias.h @@ -17,7 +17,7 @@ #include "s_struct.h" -void u_get_alias_value(char str[], +uint8_t u_get_alias_value(char str[], const char name[], size_t dstsize, t_msh *msh); -- cgit v1.2.3 From 32fb33b2dea95c00dc37efd14d34c799b23d09cf Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Fri, 11 Sep 2020 18:07:48 +0200 Subject: In progress --- src/b_alias.c | 19 +++++++++++++++++++ src/b_alias.h | 1 + src/e_builtins.c | 3 +++ src/m_loop.c | 1 - 4 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/b_alias.c b/src/b_alias.c index 6848b68..3b70a02 100644 --- a/src/b_alias.c +++ b/src/b_alias.c @@ -13,9 +13,28 @@ #include #include "s_struct.h" +#include "u_utils.h" + +static void + b_print_alias_list(t_msh *msh) +{ + (void)msh; +} uint8_t b_alias(char *args[], t_msh *msh) +{ + const uint64_t argc = u_builtins_get_argc((const char**)args); + + if (argc == 0) + { + b_print_alias_list(msh); + } + return (0); +} + +uint8_t + b_alias_mute(char *args[], t_msh *msh) { (void)args; (void)msh; diff --git a/src/b_alias.h b/src/b_alias.h index a4aa1d8..2c80ffc 100644 --- a/src/b_alias.h +++ b/src/b_alias.h @@ -18,5 +18,6 @@ #include "s_struct.h" uint8_t b_alias(char *args[], t_msh *msh); +uint8_t b_alias_mute(char *args[], t_msh *msh); #endif diff --git a/src/e_builtins.c b/src/e_builtins.c index f4b363b..fb7b0ba 100644 --- a/src/e_builtins.c +++ b/src/e_builtins.c @@ -16,6 +16,7 @@ #include #include +#include "b_builtins.h" #include "b_export_next.h" #include "b_export_mute.h" #include "m_loop.h" @@ -76,6 +77,8 @@ static void b_export_mute(ptr->argv + 1, msh); else if (bu_id == FT_ID_UNSET) msh->bu_ptr[bu_id](ptr->argv + 1, msh); + else if (bu_id == FT_ID_ALIAS) + b_alias_mute(ptr->argv + 1, msh); else if (bu_id == FT_ID_EXIT) { if (msh->fd == STDIN_FILENO) diff --git a/src/m_loop.c b/src/m_loop.c index bd4fb1c..50357eb 100644 --- a/src/m_loop.c +++ b/src/m_loop.c @@ -26,7 +26,6 @@ #include "s_lpipes.h" #include "s_line.h" #include "u_vars.h" -#include "u_alias.h" static void m_parse_and_run_line(char line[], t_msh *msh) -- cgit v1.2.3 From 32592aa00b7799b172e3a54eb5a525ae17fed9ae Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Fri, 11 Sep 2020 18:33:11 +0200 Subject: In progress --- src/b_alias.c | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/src/b_alias.c b/src/b_alias.c index 3b70a02..08f40ed 100644 --- a/src/b_alias.c +++ b/src/b_alias.c @@ -18,25 +18,52 @@ static void b_print_alias_list(t_msh *msh) { - (void)msh; + t_lalias *ptr; + + ptr = msh->alias; + while (ptr != NULL) + { + ft_printf("alias %s='%s'\n", ptr->name, ptr->val); + ptr = ptr->next; + } +} + +static void + b_print_arg(const char arg[], t_msh *msh) +{ + char *ptr; } uint8_t b_alias(char *args[], t_msh *msh) { const uint64_t argc = u_builtins_get_argc((const char**)args); + int32_t i; if (argc == 0) { b_print_alias_list(msh); } + if (argc > 0) + { + i = 0; + while (args[i] != NULL) + { + b_print_arg(args[i], msh); + i++; + } + } return (0); } uint8_t b_alias_mute(char *args[], t_msh *msh) { - (void)args; + const uint64_t argc = u_builtins_get_argc((const char**)args); + (void)msh; + if (argc > 0) + { + } return (0); } -- cgit v1.2.3 From be1403ef70e51442df459b1ef89f547cbc9af4ca Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Fri, 11 Sep 2020 19:32:47 +0200 Subject: This shit is killing me --- TODO.org | 1 + src/b_alias.c | 81 ++++++++++++++++++++++++++++++++++++--- src/b_alias.h | 2 +- src/d_define.h | 1 + src/p_lblock_next.c | 108 ++++++++++++++++++++++++++++++++++------------------ src/p_lblock_next.h | 5 ++- src/s_com.c | 3 +- src/u_alias.c | 2 +- 8 files changed, 155 insertions(+), 48 deletions(-) diff --git a/TODO.org b/TODO.org index 652a9da..640f3af 100644 --- a/TODO.org +++ b/TODO.org @@ -26,6 +26,7 @@ ** DONE [#C] Handle comments better # ** TODO [#C] msh ~> ./qwe.sh <---- without shebang (maybe works fine already) ** TODO [#C] Fix ~ only first pos of arg +** TODO [#C] Fix $? * Stuff to add ** DONE [#A] builtins to pipes diff --git a/src/b_alias.c b/src/b_alias.c index 08f40ed..8aff531 100644 --- a/src/b_alias.c +++ b/src/b_alias.c @@ -10,9 +10,14 @@ /* */ /* ************************************************************************** */ +#include #include +#include +#include +#include "d_define.h" #include "s_struct.h" +#include "u_alias.h" #include "u_utils.h" static void @@ -28,10 +33,64 @@ static void } } -static void +static uint8_t b_print_arg(const char arg[], t_msh *msh) { char *ptr; + char val[ARG_MAX]; + t_bool invalid; + + ptr = (char*)arg; + invalid = FALSE; + while (*ptr != C_NUL && *ptr != C_EQUALS) + { + if (ft_iswhitespace(*ptr) == TRUE) + invalid = TRUE; + ptr++; + } + if (*ptr == C_NUL) + { + if (u_get_alias_value(val, arg, ARG_MAX, msh) == 0) + { + ft_printf("alias %s='%s'", arg, val); + return (0); + } + else + { + ft_dprintf(STDERR_FILENO, "minishell: alias: %s: not found\n", arg); + return (1); + } + } + else if (*ptr == C_EQUALS && invalid == TRUE) + { + ft_strlcpy(val, arg, ptr - arg); + ft_dprintf(STDERR_FILENO, + "minishell: alias: `%s': invalid alias name\n", + val); + return (1); + } + return (0); +} + +static void + b_register_arg(const char arg[], t_msh *msh) +{ + char *ptr; + char name[255]; + char value[ARG_MAX]; + + ptr = (char*)arg; + while (*ptr != C_NUL && *ptr != C_EQUALS) + { + ptr++; + } + if (*ptr == C_EQUALS) + { + ft_strlcpy(name, arg, (ptr - arg < 255) ? ((ptr - arg) + 1) : (255)); + ptr += 1; + ft_strlcpy(value, ptr, ARG_MAX); + u_set_alias_value(name, value, msh); + } } uint8_t @@ -39,7 +98,9 @@ uint8_t { const uint64_t argc = u_builtins_get_argc((const char**)args); int32_t i; + uint8_t ret; + ret = 0; if (argc == 0) { b_print_alias_list(msh); @@ -49,21 +110,29 @@ uint8_t i = 0; while (args[i] != NULL) { - b_print_arg(args[i], msh); + if (b_print_arg(args[i], msh) != 0) + { + ret = 1; + } i++; } } - return (0); + return (ret); } -uint8_t +void b_alias_mute(char *args[], t_msh *msh) { const uint64_t argc = u_builtins_get_argc((const char**)args); + int32_t i; - (void)msh; if (argc > 0) { + i = 0; + while (args[i] != NULL) + { + b_register_arg(args[i], msh); + i++; + } } - return (0); } diff --git a/src/b_alias.h b/src/b_alias.h index 2c80ffc..83b1878 100644 --- a/src/b_alias.h +++ b/src/b_alias.h @@ -18,6 +18,6 @@ #include "s_struct.h" uint8_t b_alias(char *args[], t_msh *msh); -uint8_t b_alias_mute(char *args[], t_msh *msh); +void b_alias_mute(char *args[], t_msh *msh); #endif diff --git a/src/d_define.h b/src/d_define.h index 897b720..1709bea 100644 --- a/src/d_define.h +++ b/src/d_define.h @@ -75,6 +75,7 @@ #define C_SQUOTE 0x27 #define C_AMP 0x26 #define C_SEMIC 0x3b +#define C_EQUALS 0x3d #define C_BACKS 0x5c #define C_PIPE 0x7c diff --git a/src/p_lblock_next.c b/src/p_lblock_next.c index a5314f2..adfa043 100644 --- a/src/p_lblock_next.c +++ b/src/p_lblock_next.c @@ -19,6 +19,7 @@ #include "s_destroy.h" #include "f_fail.h" #include "s_struct.h" +#include "u_alias.h" #include "u_parse.h" #include "u_utils.h" #include "u_vars.h" @@ -66,8 +67,8 @@ static void *(p) = word + (i + ft_strlen(varval) - 1); } -char - *p_subst_vars(char word[], t_msh *msh) +void + p_subst_vars(char word[], t_msh *msh) { char *ptr; t_quote_mode mode; @@ -87,7 +88,25 @@ char } ptr++; } - return (word); +} + +void + p_subst_alias(char word[], t_msh *msh) +{ + /* char subst[ARG_MAX]; */ + char *ptr; + + ptr = word; + (void)msh; + ft_printf("[%s]\n", ptr); + /* while (*ptr != C_NUL) */ + /* { */ + /* ptr++; */ + /* } */ + /* if (u_get_alias_value(subst, ?, ARG_MAX, msh) == 0) */ + /* { */ + /* /\* copy *\/ */ + /* } */ } char @@ -200,42 +219,57 @@ static void msh->env_fork_tmp[j][0] = '\0'; } -char - **p_check_args_equals(char *words[], t_msh *msh) +void + p_check_args_equals(char word[], t_msh *msh) { - char *ptr; - t_bool reg; - t_bool isvar; - int64_t i; + char *ptr; + t_quote_mode mode; - i = 0; - reg = FALSE; - isvar = FALSE; - while (words[i]) + mode = Q_NONE; + ptr = word; + while (*ptr != C_NUL) { - ptr = words[i]; - while (*ptr != '\0' && *ptr != '=') - ptr++; - if (*ptr == '=') - { - reg = TRUE; - isvar = TRUE; - } - if (*ptr == '\0' || words[i][0] == '=' || - ft_isdigit(words[i][0]) == TRUE) - { - reg = FALSE; - if (i == 0) - isvar = FALSE; - if (isvar == TRUE) - p_add_to_env_fork(i, words, msh); - else - msh->env_fork_tmp[0][0] = '\0'; - break ; - } - i++; + if (*ptr == C_DQUOTE) + mode = u_meet_dquote(word, ptr, mode); + else if (*ptr == C_SQUOTE) + mode = u_meet_squote(word, ptr, mode); + else if (mode == Q_NONE && *ptr == C_EQUALS) + u_meet_equals(); + ptr++; } - if (isvar == TRUE) - return (p_add_to_variables_and_delete(words, reg, i, msh)); - return (words); + /* char *ptr; */ + /* t_bool reg; */ + /* t_bool isvar; */ + /* int64_t i; */ + + /* i = 0; */ + /* reg = FALSE; */ + /* isvar = FALSE; */ + /* while (words[i]) */ + /* { */ + /* ptr = words[i]; */ + /* while (*ptr != '\0' && *ptr != '=') */ + /* ptr++; */ + /* if (*ptr == '=') */ + /* { */ + /* reg = TRUE; */ + /* isvar = TRUE; */ + /* } */ + /* if (*ptr == '\0' || words[i][0] == '=' || */ + /* ft_isdigit(words[i][0]) == TRUE) */ + /* { */ + /* reg = FALSE; */ + /* if (i == 0) */ + /* isvar = FALSE; */ + /* if (isvar == TRUE) */ + /* p_add_to_env_fork(i, words, msh); */ + /* else */ + /* msh->env_fork_tmp[0][0] = '\0'; */ + /* break ; */ + /* } */ + /* i++; */ + /* } */ + /* if (isvar == TRUE) */ + /* return (p_add_to_variables_and_delete(words, reg, i, msh)); */ + /* return (words); */ } diff --git a/src/p_lblock_next.h b/src/p_lblock_next.h index 5fa9f45..916055e 100644 --- a/src/p_lblock_next.h +++ b/src/p_lblock_next.h @@ -17,9 +17,10 @@ #include "s_struct.h" -char *p_subst_vars(char word[], t_msh *msh); +void p_subst_vars(char word[], t_msh *msh); +void p_subst_alias(char word[], t_msh *msh); char **p_subst_args(const char word[], int8_t redir); char **p_subst_home(char *word[], t_msh *msh); -char **p_check_args_equals(char *words[], t_msh *msh); +void p_check_args_equals(char word[], t_msh *msh); #endif diff --git a/src/s_com.c b/src/s_com.c index 277d996..51b2500 100644 --- a/src/s_com.c +++ b/src/s_com.c @@ -117,11 +117,12 @@ t_com if (p_get_redir(nword, &com) != 0) return (NULL); p_subst_vars(nword, msh); + p_check_args_equals(nword, msh); + p_subst_alias(nword, msh); if ((words = p_split_args(nword, com->redir)) == NULL) return (NULL); if ((words = p_subst_home(words, msh)) == NULL) return (NULL); - words = p_check_args_equals(words, msh); if (msh->env_fork_tmp[0][0] != '\0') s_com_cpy_env_fork(&com, msh); if (s_fill_com(words, &com) < 0) diff --git a/src/u_alias.c b/src/u_alias.c index f273a5c..92bd498 100644 --- a/src/u_alias.c +++ b/src/u_alias.c @@ -48,7 +48,7 @@ void { t_lalias *new; - if (u_get_alias_value(NULL, name, 0, msh) != 0) + if (u_get_alias_value(NULL, name, 0, msh) == 0) { s_lalias_rebind(&msh->alias, name, value); } -- cgit v1.2.3 From 2a5b43429d411bf5b2a71e32b6a8e11a896786b6 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Fri, 11 Sep 2020 19:55:23 +0200 Subject: qwe --- src/p_lblock_next.c | 1 - src/p_lblock_next.h | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/p_lblock_next.c b/src/p_lblock_next.c index adfa043..6f5f00d 100644 --- a/src/p_lblock_next.c +++ b/src/p_lblock_next.c @@ -234,7 +234,6 @@ void else if (*ptr == C_SQUOTE) mode = u_meet_squote(word, ptr, mode); else if (mode == Q_NONE && *ptr == C_EQUALS) - u_meet_equals(); ptr++; } /* char *ptr; */ diff --git a/src/p_lblock_next.h b/src/p_lblock_next.h index 916055e..030be27 100644 --- a/src/p_lblock_next.h +++ b/src/p_lblock_next.h @@ -21,6 +21,6 @@ void p_subst_vars(char word[], t_msh *msh); void p_subst_alias(char word[], t_msh *msh); char **p_subst_args(const char word[], int8_t redir); char **p_subst_home(char *word[], t_msh *msh); -void p_check_args_equals(char word[], t_msh *msh); +char **p_check_args_equals(char *words[], t_msh *msh); #endif -- cgit v1.2.3 From ff845bada3fe6898a228c10422f0561e2a532622 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Fri, 11 Sep 2020 21:18:27 +0200 Subject: Fuck this --- src/p_lblock_next.c | 96 +++++++++++++++++++---------------------------------- src/s_com.c | 4 +-- src/s_struct.h | 2 +- 3 files changed, 38 insertions(+), 64 deletions(-) diff --git a/src/p_lblock_next.c b/src/p_lblock_next.c index 6f5f00d..10c4d84 100644 --- a/src/p_lblock_next.c +++ b/src/p_lblock_next.c @@ -93,20 +93,8 @@ void void p_subst_alias(char word[], t_msh *msh) { - /* char subst[ARG_MAX]; */ - char *ptr; - - ptr = word; + (void)word; (void)msh; - ft_printf("[%s]\n", ptr); - /* while (*ptr != C_NUL) */ - /* { */ - /* ptr++; */ - /* } */ - /* if (u_get_alias_value(subst, ?, ARG_MAX, msh) == 0) */ - /* { */ - /* /\* copy *\/ */ - /* } */ } char @@ -219,56 +207,42 @@ static void msh->env_fork_tmp[j][0] = '\0'; } -void - p_check_args_equals(char word[], t_msh *msh) +char + **p_check_args_equals(char *words[], t_msh *msh) { - char *ptr; - t_quote_mode mode; + char *ptr; + t_bool reg; + t_bool isvar; + int64_t i; - mode = Q_NONE; - ptr = word; - while (*ptr != C_NUL) + i = 0; + reg = FALSE; + isvar = FALSE; + while (words[i]) { - if (*ptr == C_DQUOTE) - mode = u_meet_dquote(word, ptr, mode); - else if (*ptr == C_SQUOTE) - mode = u_meet_squote(word, ptr, mode); - else if (mode == Q_NONE && *ptr == C_EQUALS) - ptr++; + ptr = words[i]; + while (*ptr != '\0' && *ptr != '=') + ptr++; + if (*ptr == '=') + { + reg = TRUE; + isvar = TRUE; + } + if (*ptr == '\0' || words[i][0] == '=' || + ft_isdigit(words[i][0]) == TRUE) + { + reg = FALSE; + if (i == 0) + isvar = FALSE; + if (isvar == TRUE) + p_add_to_env_fork(i, words, msh); + else + msh->env_fork_tmp[0][0] = '\0'; + break ; + } + i++; } - /* char *ptr; */ - /* t_bool reg; */ - /* t_bool isvar; */ - /* int64_t i; */ - - /* i = 0; */ - /* reg = FALSE; */ - /* isvar = FALSE; */ - /* while (words[i]) */ - /* { */ - /* ptr = words[i]; */ - /* while (*ptr != '\0' && *ptr != '=') */ - /* ptr++; */ - /* if (*ptr == '=') */ - /* { */ - /* reg = TRUE; */ - /* isvar = TRUE; */ - /* } */ - /* if (*ptr == '\0' || words[i][0] == '=' || */ - /* ft_isdigit(words[i][0]) == TRUE) */ - /* { */ - /* reg = FALSE; */ - /* if (i == 0) */ - /* isvar = FALSE; */ - /* if (isvar == TRUE) */ - /* p_add_to_env_fork(i, words, msh); */ - /* else */ - /* msh->env_fork_tmp[0][0] = '\0'; */ - /* break ; */ - /* } */ - /* i++; */ - /* } */ - /* if (isvar == TRUE) */ - /* return (p_add_to_variables_and_delete(words, reg, i, msh)); */ - /* return (words); */ + if (isvar == TRUE) + return (p_add_to_variables_and_delete(words, reg, i, msh)); + return (words); } diff --git a/src/s_com.c b/src/s_com.c index 51b2500..4d78573 100644 --- a/src/s_com.c +++ b/src/s_com.c @@ -116,13 +116,13 @@ t_com ft_strlcpy(nword, word, ARG_MAX); if (p_get_redir(nword, &com) != 0) return (NULL); - p_subst_vars(nword, msh); - p_check_args_equals(nword, msh); p_subst_alias(nword, msh); + p_subst_vars(nword, msh); if ((words = p_split_args(nword, com->redir)) == NULL) return (NULL); if ((words = p_subst_home(words, msh)) == NULL) return (NULL); + words = p_check_args_equals(words, msh); if (msh->env_fork_tmp[0][0] != '\0') s_com_cpy_env_fork(&com, msh); if (s_fill_com(words, &com) < 0) diff --git a/src/s_struct.h b/src/s_struct.h index 963d5ad..310c5f4 100644 --- a/src/s_struct.h +++ b/src/s_struct.h @@ -82,7 +82,7 @@ typedef struct s_msh char **envp; char **bu_ref; char ps[4][1024]; - char env_fork_tmp[128][1024]; + char env_fork_tmp[128][4096]; char sqb_ref[FT_ID_SQB_COUNT][4]; char *shname; char *cwd; -- cgit v1.2.3 From 064c1e22c83f24c112bc2e3e3ce4427f8ff9e4c7 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Fri, 11 Sep 2020 21:22:42 +0200 Subject: Format --- src/p_lblock_next.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/p_lblock_next.c b/src/p_lblock_next.c index 10c4d84..0a982a5 100644 --- a/src/p_lblock_next.c +++ b/src/p_lblock_next.c @@ -62,7 +62,9 @@ static void ft_strlcpy(tmp, ptr + i, varlen + 1 - i); u_get_var_value(varval, tmp, ARG_MAX, msh); p_double_them_bs(varval); - (void)ft_memmove(ptr + (i + ft_strlen(tmp)), ptr + varlen, (ft_strlen(ptr + varlen) + 1) * sizeof(char)); + (void)ft_memmove(ptr + (i + ft_strlen(tmp)), + ptr + varlen, + (ft_strlen(ptr + varlen) + 1) * sizeof(char)); (void)ft_memmove(word + i, varval, ft_strlen(varval) * sizeof(char)); *(p) = word + (i + ft_strlen(varval) - 1); } -- cgit v1.2.3 From 291cabef51915c280c26e567d864b6ea3fc56dbd Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Sat, 12 Sep 2020 06:32:26 +0200 Subject: In progress --- src/p_lblock_next.c | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/p_lblock_next.c b/src/p_lblock_next.c index 0a982a5..036d6b5 100644 --- a/src/p_lblock_next.c +++ b/src/p_lblock_next.c @@ -92,11 +92,35 @@ void } } +static char + *p_skip_whitespace(char *ptr) +{ + while (*ptr != C_NUL && ft_iswhitespace(*ptr)) + ptr++; + return (ptr); +} + void p_subst_alias(char word[], t_msh *msh) { - (void)word; + char *ptr; + size_t first; + t_quote_mode mode; + (void)msh; + mode = Q_NONE; + ptr = word; + p_skip_whitespace(ptr); + first = 1; + while (*ptr != C_NUL) + { + if (*ptr == C_DQUOTE) + mode = u_meet_dquote(word, ptr, mode); + if (*ptr == C_SQUOTE) + mode = u_meet_squote(word, ptr, mode); + if (ft_iswhitespace(*ptr) == TRUE && p_meet_whitespace((char*)word, ptr, mode) == TRUE) + ptr++; + } } char -- cgit v1.2.3 From 8b15c7b0f233238a53088d8c095bd5af7f7592e7 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Sat, 12 Sep 2020 14:51:23 +0200 Subject: how tf will I norm this --- src/p_args.c | 2 +- src/p_lblock_next.c | 46 ++++++++++++++++++++++++++++++++++++++++++---- src/s_com.c | 3 ++- 3 files changed, 45 insertions(+), 6 deletions(-) diff --git a/src/p_args.c b/src/p_args.c index 5276ae1..54a95a7 100644 --- a/src/p_args.c +++ b/src/p_args.c @@ -72,7 +72,7 @@ static uint16_t mode = u_meet_dquote(word, ptr, mode); else if (*ptr == C_SQUOTE) mode = u_meet_squote(word, ptr, mode); - if (ft_iswhitespace(*ptr) && + if (ft_iswhitespace(*ptr) == TRUE && p_meet_whitespace((char*)word, ptr, mode) == TRUE) { ptr = p_skip_whitespace(ptr); diff --git a/src/p_lblock_next.c b/src/p_lblock_next.c index 036d6b5..8b755da 100644 --- a/src/p_lblock_next.c +++ b/src/p_lblock_next.c @@ -100,27 +100,65 @@ static char return (ptr); } +static t_bool + p_meet_whitespace(char *head, char *ptr, t_quote_mode mode) +{ + if (mode == Q_NONE && u_is_not_escaped(head, ptr) == TRUE) + { + return (TRUE); + } + return (FALSE); +} + void p_subst_alias(char word[], t_msh *msh) { char *ptr; - size_t first; + char tmp[255]; + size_t locat[2]; + t_bool good; t_quote_mode mode; (void)msh; mode = Q_NONE; ptr = word; - p_skip_whitespace(ptr); - first = 1; + ptr = p_skip_whitespace(ptr); + good = TRUE; + locat[0] = (ptr - word); + locat[1] = (ptr - word); while (*ptr != C_NUL) { if (*ptr == C_DQUOTE) mode = u_meet_dquote(word, ptr, mode); if (*ptr == C_SQUOTE) mode = u_meet_squote(word, ptr, mode); - if (ft_iswhitespace(*ptr) == TRUE && p_meet_whitespace((char*)word, ptr, mode) == TRUE) + if (mode == Q_NONE && *ptr == C_EQUALS) + good = FALSE; + if (ft_iswhitespace(*ptr) == TRUE && + p_meet_whitespace((char*)word, ptr, mode) == TRUE) + { + locat[1] = (ptr - word); + if (good == TRUE) + break ; + else + { + ptr = p_skip_whitespace(ptr); + locat[0] = (ptr - word); + ptr -= 1; + good = TRUE; + } + } ptr++; } + if (*ptr == C_NUL && good == TRUE) + locat[1] = (ptr - word); + if (good == TRUE) + { + ft_strlcpy(tmp, + word + locat[0], + ((locat[1] - locat[0] < 253) ? (locat[1] - locat[0]) : (254)) + 1); + ft_printf("(%s)\n", tmp); + } } char diff --git a/src/s_com.c b/src/s_com.c index 4d78573..b85905e 100644 --- a/src/s_com.c +++ b/src/s_com.c @@ -116,7 +116,8 @@ t_com ft_strlcpy(nword, word, ARG_MAX); if (p_get_redir(nword, &com) != 0) return (NULL); - p_subst_alias(nword, msh); + /* if (msh->alias != NULL) */ + p_subst_alias(nword, msh); p_subst_vars(nword, msh); if ((words = p_split_args(nword, com->redir)) == NULL) return (NULL); -- cgit v1.2.3 From 477ea057edf32486e944ef4e33023e9ab6636e07 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Sat, 12 Sep 2020 15:07:22 +0200 Subject: Aliases work, fucker --- src/b_export_next.c | 11 +++++------ src/e_builtins.c | 3 ++- src/p_lblock_next.c | 13 ++++++++++--- src/s_com.c | 2 +- 4 files changed, 18 insertions(+), 11 deletions(-) diff --git a/src/b_export_next.c b/src/b_export_next.c index 059fbc6..1fc732e 100644 --- a/src/b_export_next.c +++ b/src/b_export_next.c @@ -13,6 +13,7 @@ #include #include +#include "d_define.h" #include "b_export_next.h" #include "f_fail.h" #include "s_lvars.h" @@ -21,8 +22,7 @@ #include "u_vars.h" static char - **b_get_var(const char arg[], - t_msh *msh) + **b_get_var(const char arg[], t_msh *msh) { size_t len; char **var; @@ -44,8 +44,7 @@ static char } static int64_t - b_is_it_in_env(const char varname[], - t_msh *msh) + b_is_it_in_env(const char varname[], t_msh *msh) { char **env_dup; size_t i; @@ -66,8 +65,7 @@ static int64_t } static void - b_add_to_env(const char arg[], - t_msh *msh) + b_add_to_env(const char arg[], t_msh *msh) { size_t i; char **nenvp; @@ -98,6 +96,7 @@ void char **var; int64_t env_i; + varval[0] = C_NUL; var = b_get_var(arg, msh); if ((env_i = b_is_it_in_env(var[FT_VAR_NAME] + 1, msh)) != -1) { diff --git a/src/e_builtins.c b/src/e_builtins.c index fb7b0ba..7c86a5f 100644 --- a/src/e_builtins.c +++ b/src/e_builtins.c @@ -21,6 +21,7 @@ #include "b_export_mute.h" #include "m_loop.h" #include "m_redirs.h" +#include "s_com.h" #include "s_destroy.h" #include "s_line.h" #include "s_struct.h" @@ -66,7 +67,6 @@ static void int32_t status; int32_t ret; - (void)ptr; while (wait(&status) != pid) ; ret = WEXITSTATUS(status); @@ -83,6 +83,7 @@ static void { if (msh->fd == STDIN_FILENO) m_dump_hist(msh); + s_com_destroy(&msh->com); s_line_clear(&msh->curr); s_destroy(msh); exit(ret); diff --git a/src/p_lblock_next.c b/src/p_lblock_next.c index 8b755da..707acc0 100644 --- a/src/p_lblock_next.c +++ b/src/p_lblock_next.c @@ -113,13 +113,13 @@ static t_bool void p_subst_alias(char word[], t_msh *msh) { - char *ptr; + char value[ARG_MAX]; char tmp[255]; size_t locat[2]; + char *ptr; t_bool good; t_quote_mode mode; - (void)msh; mode = Q_NONE; ptr = word; ptr = p_skip_whitespace(ptr); @@ -157,7 +157,14 @@ void ft_strlcpy(tmp, word + locat[0], ((locat[1] - locat[0] < 253) ? (locat[1] - locat[0]) : (254)) + 1); - ft_printf("(%s)\n", tmp); + if (u_get_alias_value(value, tmp, ARG_MAX, msh) != 0) + return ; + (void)ft_memmove(word + (locat[0] + ft_strlen(value)), + word + locat[1], + ft_strlen(word + locat[1]) + 1 * sizeof(char)); + (void)ft_memmove(word + locat[0], + value, + ft_strlen(value) * sizeof(char)); } } diff --git a/src/s_com.c b/src/s_com.c index b85905e..a5c594a 100644 --- a/src/s_com.c +++ b/src/s_com.c @@ -116,7 +116,7 @@ t_com ft_strlcpy(nword, word, ARG_MAX); if (p_get_redir(nword, &com) != 0) return (NULL); - /* if (msh->alias != NULL) */ + if (msh->alias != NULL) p_subst_alias(nword, msh); p_subst_vars(nword, msh); if ((words = p_split_args(nword, com->redir)) == NULL) -- cgit v1.2.3 From 81900e8ff106bbd9602f6589fc54e4530f5cc3dd Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Sat, 12 Sep 2020 15:57:51 +0200 Subject: Fix --- src/e_builtins.c | 1 + src/m_argv.c | 7 ++----- src/p_lblock_next.c | 4 ++-- src/s_init.c | 3 +-- 4 files changed, 6 insertions(+), 9 deletions(-) diff --git a/src/e_builtins.c b/src/e_builtins.c index 7c86a5f..90e436e 100644 --- a/src/e_builtins.c +++ b/src/e_builtins.c @@ -53,6 +53,7 @@ static void dup_redirs(ptr, msh); ret = msh->bu_ptr[bu_id](ptr->argv + 1, msh); u_eof_fd(msh->fd); + s_com_destroy(&msh->com); s_line_clear(&msh->curr); s_destroy(msh); exit(ret); diff --git a/src/m_argv.c b/src/m_argv.c index 5b2bc96..98a8e3e 100644 --- a/src/m_argv.c +++ b/src/m_argv.c @@ -24,8 +24,7 @@ #include "s_struct.h" static void - m_read_script(char *const argv[], - t_msh *msh) + m_read_script(char *const argv[], t_msh *msh) { int32_t fd; @@ -36,9 +35,7 @@ static void } uint8_t - m_argv(int argc, - char *const argv[], - t_msh *msh) + m_argv(int argc, char *const argv[], t_msh *msh) { if (argc == 1) { diff --git a/src/p_lblock_next.c b/src/p_lblock_next.c index 707acc0..3fa0ea5 100644 --- a/src/p_lblock_next.c +++ b/src/p_lblock_next.c @@ -62,9 +62,9 @@ static void ft_strlcpy(tmp, ptr + i, varlen + 1 - i); u_get_var_value(varval, tmp, ARG_MAX, msh); p_double_them_bs(varval); - (void)ft_memmove(ptr + (i + ft_strlen(tmp)), + ft_strlcpy(ptr + (i + ft_strlen(varval)), ptr + varlen, - (ft_strlen(ptr + varlen) + 1) * sizeof(char)); + ft_strlen(ptr + varlen) + 1); (void)ft_memmove(word + i, varval, ft_strlen(varval) * sizeof(char)); *(p) = word + (i + ft_strlen(varval) - 1); } diff --git a/src/s_init.c b/src/s_init.c index fb63548..b229cda 100644 --- a/src/s_init.c +++ b/src/s_init.c @@ -157,8 +157,7 @@ static void } t_msh - *init_msh(char *const argv[], - char *const envp[]) + *init_msh(char *const argv[], char *const envp[]) { char cwd[PATH_MAX]; t_msh *msh; -- cgit v1.2.3 From 949f93b57b6411b07eb12110e0db37cdf393db0c Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Sat, 12 Sep 2020 16:33:02 +0200 Subject: Fuck --- src/b_alias.c | 2 +- src/p_lblock_next.c | 55 +++++++++++++++++++++++++++++++++++++++++++---------- src/p_lblock_next.h | 3 ++- src/s_com.c | 7 ++++++- src/s_lalias.c | 5 ++++- src/s_struct.h | 1 + src/u_alias.c | 8 ++++---- 7 files changed, 63 insertions(+), 18 deletions(-) diff --git a/src/b_alias.c b/src/b_alias.c index 8aff531..08298b3 100644 --- a/src/b_alias.c +++ b/src/b_alias.c @@ -50,7 +50,7 @@ static uint8_t } if (*ptr == C_NUL) { - if (u_get_alias_value(val, arg, ARG_MAX, msh) == 0) + if (u_get_alias_value(val, arg, ARG_MAX, msh) != 0) { ft_printf("alias %s='%s'", arg, val); return (0); diff --git a/src/p_lblock_next.c b/src/p_lblock_next.c index 3fa0ea5..0c1c1f4 100644 --- a/src/p_lblock_next.c +++ b/src/p_lblock_next.c @@ -110,16 +110,30 @@ static t_bool return (FALSE); } -void - p_subst_alias(char word[], t_msh *msh) +size_t + p_subst_alias(char word[], t_bool reset, t_msh *msh) { + static size_t used[4096]; + static size_t i = 0; char value[ARG_MAX]; char tmp[255]; size_t locat[2]; + size_t j; + size_t usedcmp; char *ptr; t_bool good; t_quote_mode mode; + if (reset == TRUE) + { + i = 0; + while (i < 4096) + { + used[i] = 0; + i++; + } + i = 0; + } mode = Q_NONE; ptr = word; ptr = p_skip_whitespace(ptr); @@ -157,15 +171,36 @@ void ft_strlcpy(tmp, word + locat[0], ((locat[1] - locat[0] < 253) ? (locat[1] - locat[0]) : (254)) + 1); - if (u_get_alias_value(value, tmp, ARG_MAX, msh) != 0) - return ; - (void)ft_memmove(word + (locat[0] + ft_strlen(value)), - word + locat[1], - ft_strlen(word + locat[1]) + 1 * sizeof(char)); - (void)ft_memmove(word + locat[0], - value, - ft_strlen(value) * sizeof(char)); + if ((usedcmp = u_get_alias_value(value, tmp, ARG_MAX, msh)) != 0) + { + j = 0; + good = TRUE; + while (j < i) + { + if (used[j] == usedcmp) + good = FALSE; + j++; + } + if (good == TRUE) + { + (void)ft_memmove(word + (locat[0] + ft_strlen(value)), + word + locat[1], + ft_strlen(word + locat[1]) + 1 * sizeof(char)); + (void)ft_memmove(word + locat[0], + value, + ft_strlen(value) * sizeof(char)); + used[i] = usedcmp; + i++; + return (usedcmp); + } + } + /* ptr = value; */ + /* save = value; */ + /* while (*ptr != C_NUL && ft_iswhitespace(*ptr) == FALSE) */ + /* ptr++; */ + /* ft_strlcpy(tmp, save, (ptr - save) + 1); */ } + return (0); } char diff --git a/src/p_lblock_next.h b/src/p_lblock_next.h index 030be27..4ffdfcf 100644 --- a/src/p_lblock_next.h +++ b/src/p_lblock_next.h @@ -13,12 +13,13 @@ #ifndef P_LCOM_NEXT_H #define P_LCOM_NEXT_H +#include #include #include "s_struct.h" void p_subst_vars(char word[], t_msh *msh); -void p_subst_alias(char word[], t_msh *msh); +size_t p_subst_alias(char word[], t_bool reset, t_msh *msh); char **p_subst_args(const char word[], int8_t redir); char **p_subst_home(char *word[], t_msh *msh); char **p_check_args_equals(char *words[], t_msh *msh); diff --git a/src/s_com.c b/src/s_com.c index a5c594a..b899555 100644 --- a/src/s_com.c +++ b/src/s_com.c @@ -103,6 +103,7 @@ t_com char nword[ARG_MAX]; t_com *com; char **words; + size_t ret; if ((com = (t_com*)malloc(sizeof(t_com))) == NULL) return (NULL); @@ -117,7 +118,11 @@ t_com if (p_get_redir(nword, &com) != 0) return (NULL); if (msh->alias != NULL) - p_subst_alias(nword, msh); + { + ret = p_subst_alias(nword, TRUE, msh); + while (ret != 0) + ret = p_subst_alias(nword, TRUE, msh); + } p_subst_vars(nword, msh); if ((words = p_split_args(nword, com->redir)) == NULL) return (NULL); diff --git a/src/s_lalias.c b/src/s_lalias.c index eeac08b..56b3985 100644 --- a/src/s_lalias.c +++ b/src/s_lalias.c @@ -72,7 +72,8 @@ void t_lalias *s_lalias_new(const char name[], const char val[]) { - t_lalias *link; + t_lalias *link; + static size_t id = 1; if ((link = (t_lalias*)malloc(sizeof(t_lalias))) == NULL) { @@ -86,6 +87,8 @@ t_lalias { return (NULL); } + link->id = id; link->next = NULL; + id += 1; return (link); } diff --git a/src/s_struct.h b/src/s_struct.h index 310c5f4..ba56e82 100644 --- a/src/s_struct.h +++ b/src/s_struct.h @@ -29,6 +29,7 @@ typedef struct s_lalias { char *name; char *val; + size_t id; struct s_lalias *next; } t_lalias; diff --git a/src/u_alias.c b/src/u_alias.c index 92bd498..14ca75e 100644 --- a/src/u_alias.c +++ b/src/u_alias.c @@ -18,7 +18,7 @@ #include "s_lalias.h" #include "s_struct.h" -uint8_t +size_t u_get_alias_value(char str[], const char name[], size_t dstsize, t_msh *msh) { t_lalias *ptr; @@ -34,13 +34,13 @@ uint8_t } if (ptr == NULL) { - return (1); + return (0); } if (str != NULL) { ft_strlcpy(str, ptr->val, dstsize); } - return (0); + return (ptr->id); } void @@ -48,7 +48,7 @@ void { t_lalias *new; - if (u_get_alias_value(NULL, name, 0, msh) == 0) + if (u_get_alias_value(NULL, name, 0, msh) != 0) { s_lalias_rebind(&msh->alias, name, value); } -- cgit v1.2.3 From f7eb7d0c123e71f8b3998199a690b5d57aa96e1f Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Sat, 12 Sep 2020 17:25:26 +0200 Subject: In progress --- src/b_alias.c | 2 ++ src/p_lblock_next.c | 5 ----- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/src/b_alias.c b/src/b_alias.c index 08298b3..55b7232 100644 --- a/src/b_alias.c +++ b/src/b_alias.c @@ -24,6 +24,8 @@ static void b_print_alias_list(t_msh *msh) { t_lalias *ptr; + size_t i; + /* char buff[255][255][ARG_MAX]; */ ptr = msh->alias; while (ptr != NULL) diff --git a/src/p_lblock_next.c b/src/p_lblock_next.c index 0c1c1f4..02ecbb2 100644 --- a/src/p_lblock_next.c +++ b/src/p_lblock_next.c @@ -194,11 +194,6 @@ size_t return (usedcmp); } } - /* ptr = value; */ - /* save = value; */ - /* while (*ptr != C_NUL && ft_iswhitespace(*ptr) == FALSE) */ - /* ptr++; */ - /* ft_strlcpy(tmp, save, (ptr - save) + 1); */ } return (0); } -- cgit v1.2.3 From 3f726e2433438176a6b7f73df7d6bf51a185c5b2 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Sat, 12 Sep 2020 18:11:56 +0200 Subject: Fix --- src/b_alias.c | 4 +++- src/s_com.c | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/b_alias.c b/src/b_alias.c index 55b7232..5ca86da 100644 --- a/src/b_alias.c +++ b/src/b_alias.c @@ -28,11 +28,13 @@ static void /* char buff[255][255][ARG_MAX]; */ ptr = msh->alias; + i = 0; while (ptr != NULL) { - ft_printf("alias %s='%s'\n", ptr->name, ptr->val); ptr = ptr->next; + i++; } + ft_printf("alias %s='%s'\n", ptr->name, ptr->val); } static uint8_t diff --git a/src/s_com.c b/src/s_com.c index b899555..28b460f 100644 --- a/src/s_com.c +++ b/src/s_com.c @@ -121,7 +121,7 @@ t_com { ret = p_subst_alias(nword, TRUE, msh); while (ret != 0) - ret = p_subst_alias(nword, TRUE, msh); + ret = p_subst_alias(nword, FALSE, msh); } p_subst_vars(nword, msh); if ((words = p_split_args(nword, com->redir)) == NULL) -- cgit v1.2.3 From b41111c07a40ee405fb8e8b6c178a315272db7ae Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Sat, 12 Sep 2020 19:16:21 +0200 Subject: Sorted alias --- libft/src/ft_strlcpy.c | 4 +-- src/b_alias.c | 87 ++++++++++++++++++++++++++++++++++++++++++++++++-- src/s_lalias.c | 1 + 3 files changed, 88 insertions(+), 4 deletions(-) diff --git a/libft/src/ft_strlcpy.c b/libft/src/ft_strlcpy.c index ed4d924..5076a69 100644 --- a/libft/src/ft_strlcpy.c +++ b/libft/src/ft_strlcpy.c @@ -16,9 +16,9 @@ size_t ft_strlcpy(char *dst, const char *src, size_t size) { - size_t src_len; + size_t src_len; - if (!dst || !src) + if (dst == NULL || src == NULL) return (0); src_len = ft_strlen(src); if (src_len + 1 < size) diff --git a/src/b_alias.c b/src/b_alias.c index 5ca86da..c00a616 100644 --- a/src/b_alias.c +++ b/src/b_alias.c @@ -12,6 +12,7 @@ #include #include +#include #include #include @@ -20,12 +21,87 @@ #include "u_alias.h" #include "u_utils.h" +static char + **b_alloc_buff(t_lalias *ptr, size_t i) +{ + char **buff; + + if ((buff = (char**)malloc((i + 1) * sizeof(char*))) == NULL) + return (NULL); + i = 0; + while (ptr != NULL) + { + buff[i + 1] = NULL; + if ((buff[i] = (char*)malloc((6 + ft_strlen(ptr->name) + 2 + + ft_strlen(ptr->val) + 3) * sizeof(char))) == NULL) + { + ft_delwords(buff); + return (NULL); + } + ptr = ptr->next; + i++; + } + return (buff); +} + +static void + b_fill_buff(t_lalias *ptr, char *buff[]) +{ + size_t i; + + i = 0; + while (ptr != NULL) + { + ft_sprintf(buff[i], "alias %s='%s'\n", ptr->name, ptr->val); + ptr = ptr->next; + i++; + } +} + +static void + b_sort_buff(char *buff[]) +{ + size_t i; + int cmp; + char tmp[ARG_MAX]; + + i = 1; + while (buff[i] != NULL) + { + if ((cmp = ft_strncmp(buff[i - 1], buff[i], ARG_MAX)) > 0) + { + ft_strlcpy(tmp, buff[i - 1], ARG_MAX); + ft_memdel((void*)&buff[i - 1]); + if ((buff[i - 1] = ft_strdup(buff[i])) == NULL) + return ; + ft_memdel((void*)&buff[i]); + if ((buff[i] = ft_strdup(tmp)) == NULL) + return ; + i = 0; + } + i++; + } +} + +static void + b_print_buff(char *buff[]) +{ + char **ptr; + + ptr = buff; + while (*ptr != NULL) + { + ft_printf("%s", *ptr); + ptr++; + } +} + static void b_print_alias_list(t_msh *msh) { t_lalias *ptr; + char **buff; size_t i; - /* char buff[255][255][ARG_MAX]; */ ptr = msh->alias; i = 0; @@ -34,7 +110,14 @@ static void ptr = ptr->next; i++; } - ft_printf("alias %s='%s'\n", ptr->name, ptr->val); + ptr = msh->alias; + if ((buff = b_alloc_buff(ptr, i)) == NULL) + return ; + ptr = msh->alias; + b_fill_buff(ptr, buff); + b_sort_buff(buff); + b_print_buff(buff); + ft_delwords(buff); } static uint8_t diff --git a/src/s_lalias.c b/src/s_lalias.c index 56b3985..a31909b 100644 --- a/src/s_lalias.c +++ b/src/s_lalias.c @@ -65,6 +65,7 @@ void ft_memdel((void*)&tmp->name); ft_memdel((void*)&tmp->val); ft_memdel((void*)&tmp); + tmp = renext; } *lalias = NULL; } -- cgit v1.2.3 From 690752d22b5aac9a836e8ba09c8c2544f8d026bd Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Sat, 12 Sep 2020 20:00:38 +0200 Subject: Norme in progress --- Makefile | 1 + src/b_alias.c | 100 +------------------------------------------- src/b_alias_print.c | 116 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/b_alias_print.h | 20 +++++++++ 4 files changed, 138 insertions(+), 99 deletions(-) create mode 100644 src/b_alias_print.c create mode 100644 src/b_alias_print.h diff --git a/Makefile b/Makefile index 77c270d..ac70797 100644 --- a/Makefile +++ b/Makefile @@ -16,6 +16,7 @@ LFT_SRCS_DIR = ${LFT_DIR}src/ #--------------------------------- FILES --------------------------------------# #==============================================================================# SRCS_NAME = b_alias +SRCS_NAME += b_alias_print SRCS_NAME += b_cd SRCS_NAME += b_echo SRCS_NAME += b_env diff --git a/src/b_alias.c b/src/b_alias.c index c00a616..badd281 100644 --- a/src/b_alias.c +++ b/src/b_alias.c @@ -16,110 +16,12 @@ #include #include +#include "b_alias_print.h" #include "d_define.h" #include "s_struct.h" #include "u_alias.h" #include "u_utils.h" -static char - **b_alloc_buff(t_lalias *ptr, size_t i) -{ - char **buff; - - if ((buff = (char**)malloc((i + 1) * sizeof(char*))) == NULL) - return (NULL); - i = 0; - while (ptr != NULL) - { - buff[i + 1] = NULL; - if ((buff[i] = (char*)malloc((6 + ft_strlen(ptr->name) + 2 - + ft_strlen(ptr->val) + 3) * sizeof(char))) == NULL) - { - ft_delwords(buff); - return (NULL); - } - ptr = ptr->next; - i++; - } - return (buff); -} - -static void - b_fill_buff(t_lalias *ptr, char *buff[]) -{ - size_t i; - - i = 0; - while (ptr != NULL) - { - ft_sprintf(buff[i], "alias %s='%s'\n", ptr->name, ptr->val); - ptr = ptr->next; - i++; - } -} - -static void - b_sort_buff(char *buff[]) -{ - size_t i; - int cmp; - char tmp[ARG_MAX]; - - i = 1; - while (buff[i] != NULL) - { - if ((cmp = ft_strncmp(buff[i - 1], buff[i], ARG_MAX)) > 0) - { - ft_strlcpy(tmp, buff[i - 1], ARG_MAX); - ft_memdel((void*)&buff[i - 1]); - if ((buff[i - 1] = ft_strdup(buff[i])) == NULL) - return ; - ft_memdel((void*)&buff[i]); - if ((buff[i] = ft_strdup(tmp)) == NULL) - return ; - i = 0; - } - i++; - } -} - -static void - b_print_buff(char *buff[]) -{ - char **ptr; - - ptr = buff; - while (*ptr != NULL) - { - ft_printf("%s", *ptr); - ptr++; - } -} - -static void - b_print_alias_list(t_msh *msh) -{ - t_lalias *ptr; - char **buff; - size_t i; - - ptr = msh->alias; - i = 0; - while (ptr != NULL) - { - ptr = ptr->next; - i++; - } - ptr = msh->alias; - if ((buff = b_alloc_buff(ptr, i)) == NULL) - return ; - ptr = msh->alias; - b_fill_buff(ptr, buff); - b_sort_buff(buff); - b_print_buff(buff); - ft_delwords(buff); -} - static uint8_t b_print_arg(const char arg[], t_msh *msh) { diff --git a/src/b_alias_print.c b/src/b_alias_print.c new file mode 100644 index 0000000..ebe2708 --- /dev/null +++ b/src/b_alias_print.c @@ -0,0 +1,116 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* b_alias_print.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include + +#include "d_define.h" +#include "s_struct.h" + +static char + **b_alloc_buff(t_lalias *ptr, size_t i) +{ + char **buff; + + if ((buff = (char**)malloc((i + 1) * sizeof(char*))) == NULL) + return (NULL); + i = 0; + while (ptr != NULL) + { + buff[i + 1] = NULL; + if ((buff[i] = (char*)malloc((6 + ft_strlen(ptr->name) + 2 + + ft_strlen(ptr->val) + 3) * sizeof(char))) == NULL) + { + ft_delwords(buff); + return (NULL); + } + ptr = ptr->next; + i++; + } + return (buff); +} + +static void + b_fill_buff(t_lalias *ptr, char *buff[]) +{ + size_t i; + + i = 0; + while (ptr != NULL) + { + ft_sprintf(buff[i], "alias %s='%s'\n", ptr->name, ptr->val); + ptr = ptr->next; + i++; + } +} + +static void + b_sort_buff(char *buff[]) +{ + size_t i; + int cmp; + char tmp[ARG_MAX]; + + i = 1; + while (buff[i] != NULL) + { + if ((cmp = ft_strncmp(buff[i - 1], buff[i], ARG_MAX)) > 0) + { + ft_strlcpy(tmp, buff[i - 1], ARG_MAX); + ft_memdel((void*)&buff[i - 1]); + if ((buff[i - 1] = ft_strdup(buff[i])) == NULL) + return ; + ft_memdel((void*)&buff[i]); + if ((buff[i] = ft_strdup(tmp)) == NULL) + return ; + i = 0; + } + i++; + } +} + +static void + b_print_buff(char *buff[]) +{ + char **ptr; + + ptr = buff; + while (*ptr != NULL) + { + ft_printf("%s", *ptr); + ptr++; + } +} + +void + b_print_alias_list(t_msh *msh) +{ + t_lalias *ptr; + char **buff; + size_t i; + + ptr = msh->alias; + i = 0; + while (ptr != NULL) + { + ptr = ptr->next; + i++; + } + ptr = msh->alias; + if ((buff = b_alloc_buff(ptr, i)) == NULL) + return ; + ptr = msh->alias; + b_fill_buff(ptr, buff); + b_sort_buff(buff); + b_print_buff(buff); + ft_delwords(buff); +} diff --git a/src/b_alias_print.h b/src/b_alias_print.h new file mode 100644 index 0000000..a3d2ee3 --- /dev/null +++ b/src/b_alias_print.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* b_alias_print.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef B_ALIAS_PRINT_H +#define B_ALIAS_PRINT_H + +#include "s_struct.h" + +void b_print_alias_list(t_msh *msh); + +#endif -- cgit v1.2.3 From ee62c034f1c6133976039fa51c31b91816befd63 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Sat, 12 Sep 2020 20:25:07 +0200 Subject: Aliases done --- TODO.org | 2 +- src/b_alias.c | 29 ++++++++++++++++++----------- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/TODO.org b/TODO.org index 640f3af..e3374ab 100644 --- a/TODO.org +++ b/TODO.org @@ -40,7 +40,7 @@ ** TODO [#B] history builtin ** TODO [#C] Norm ** DONE [#C] [ builtin <=== pretty cool yet simple stuff ( [ $a -eq 1 ] ) -** TODO [#C] alias +** DONE [#C] alias ** DONE [#C] Read scripts ** DONE [#C] PSX rice ** TODO [#C] Termcaps diff --git a/src/b_alias.c b/src/b_alias.c index badd281..daa6e99 100644 --- a/src/b_alias.c +++ b/src/b_alias.c @@ -23,20 +23,10 @@ #include "u_utils.h" static uint8_t - b_print_arg(const char arg[], t_msh *msh) + b_print_arg_next(char *ptr, const char arg[], t_bool invalid, t_msh *msh) { - char *ptr; char val[ARG_MAX]; - t_bool invalid; - ptr = (char*)arg; - invalid = FALSE; - while (*ptr != C_NUL && *ptr != C_EQUALS) - { - if (ft_iswhitespace(*ptr) == TRUE) - invalid = TRUE; - ptr++; - } if (*ptr == C_NUL) { if (u_get_alias_value(val, arg, ARG_MAX, msh) != 0) @@ -61,6 +51,23 @@ static uint8_t return (0); } +static uint8_t + b_print_arg(const char arg[], t_msh *msh) +{ + char *ptr; + t_bool invalid; + + ptr = (char*)arg; + invalid = FALSE; + while (*ptr != C_NUL && *ptr != C_EQUALS) + { + if (ft_iswhitespace(*ptr) == TRUE) + invalid = TRUE; + ptr++; + } + return (b_print_arg_next(ptr, arg, invalid, msh)); +} + static void b_register_arg(const char arg[], t_msh *msh) { -- cgit v1.2.3 From d6cee7ca8a59f8e5348a1d5dbbab723a6db35f93 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Tue, 15 Sep 2020 18:51:24 +0200 Subject: type builtin fix --- TODO.org | 4 +++- src/b_type.c | 68 ++++++++++++++++++++++++++++++++++-------------------------- 2 files changed, 42 insertions(+), 30 deletions(-) diff --git a/TODO.org b/TODO.org index e3374ab..9cc733d 100644 --- a/TODO.org +++ b/TODO.org @@ -26,7 +26,7 @@ ** DONE [#C] Handle comments better # ** TODO [#C] msh ~> ./qwe.sh <---- without shebang (maybe works fine already) ** TODO [#C] Fix ~ only first pos of arg -** TODO [#C] Fix $? +** DONE [#C] Fix $? * Stuff to add ** DONE [#A] builtins to pipes @@ -43,5 +43,7 @@ ** DONE [#C] alias ** DONE [#C] Read scripts ** DONE [#C] PSX rice +** TODO [#C] if else +** TODO [#C] << ** TODO [#C] Termcaps ** TODO [#C] Simple arithmetics $((a + 1)) diff --git a/src/b_type.c b/src/b_type.c index e55a87c..c09f441 100644 --- a/src/b_type.c +++ b/src/b_type.c @@ -22,8 +22,7 @@ #include "e_externs_next.h" #include "u_utils.h" -static int8_t - absolute_path_exists(char com[]) +static int8_t absolute_path_exists(char com[]) { int32_t fd; DIR *dir; @@ -41,9 +40,7 @@ static int8_t return (0); } -static char - *type_get_path(char com[], - t_msh *msh) +static char *type_get_path(char com[], t_msh *msh) { char **envpath; char *fullpath; @@ -70,49 +67,62 @@ static char return (fullpath); } -static uint8_t - chk_nonbuilt(char **ptr, - t_msh *msh) +static uint8_t b_check_nonbuilt(char *ptr, uint8_t ret, t_msh *msh) { char *fullpath; - int32_t ret; - ret = 0; - fullpath = type_get_path(*ptr, msh); - if (fullpath) - ft_printf("%s is %s\n", *ptr, fullpath); + fullpath = type_get_path(ptr, msh); + if (fullpath != NULL) + ft_printf("%s is %s\n", ptr, fullpath); else { - ft_printf("minishell: type: %s: not found\n", *ptr); + ft_printf("minishell: type: %s: not found\n", ptr); ret = 1; } ft_memdel((void*)&fullpath); return (ret); } -uint8_t - b_type(char *args[], - t_msh *msh) +static uint8_t b_check_builtins(char *ptr, t_msh *msh) +{ + char **p_bu; + + p_bu = msh->bu_ref; + while (*p_bu != NULL && ft_strncmp(ptr, *p_bu, ft_strlen(*p_bu) + 1) != 0) + { + p_bu++; + } + if (*p_bu != NULL) + { + ft_printf("%s is a shell builtin\n", ptr); + return (0); + } + return (1); +} + +uint8_t b_type(char *args[], t_msh *msh) { - char **ptr; - char **p_bu; - int32_t ret; + t_lalias *p_alias; + char **ptr; + int32_t ret; ptr = args; - if (!*ptr) + if (*ptr == NULL) return (0); ret = 0; - while (*ptr) + while (*ptr != NULL) { - p_bu = msh->bu_ref; - while (*p_bu && ft_strncmp(*ptr, *p_bu, ft_strlen(*p_bu) + 1)) - p_bu++; - if (*p_bu != NULL) - ft_printf("%s is a shell builtin\n", *ptr); - else + p_alias = msh->alias; + while (p_alias != NULL && + ft_strncmp(*ptr, p_alias->name, + ft_strlen(p_alias->name) + 1) != 0) + p_alias = p_alias->next; + if (p_alias != NULL) { - ret = chk_nonbuilt(ptr, msh); + ft_printf("%s is aliased to `%s'\n", *ptr, p_alias->val); } + else if (b_check_builtins(*ptr, msh) == 1) + ret = b_check_nonbuilt(*ptr, ret, msh); ptr++; } return (ret); -- cgit v1.2.3 From ca2e36781039eb7e9901ccde395600e7af87ff4f Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Tue, 15 Sep 2020 19:59:41 +0200 Subject: Huge fixes and stack stuff --- Makefile | 2 +- src/b_type.c | 35 ++++++----------- src/d_define.h | 1 + src/e_externs.c | 36 +++++++----------- src/e_externs_next.c | 102 -------------------------------------------------- src/e_externs_next.h | 21 ----------- src/e_externs_pipes.c | 13 ++++--- src/f_exec.c | 22 ++++++++--- src/f_exec.h | 2 +- src/m_loop.c | 29 ++++++++++++++ src/p_line.c | 21 ----------- src/u_path.c | 72 +++++++++++++++++++++++++++++++++++ src/u_path.h | 25 +++++++++++++ src/u_utils.c | 2 +- src/u_vars.c | 18 +++++---- src/u_vars.h | 3 +- 16 files changed, 192 insertions(+), 212 deletions(-) delete mode 100644 src/e_externs_next.c delete mode 100644 src/e_externs_next.h create mode 100644 src/u_path.c create mode 100644 src/u_path.h diff --git a/Makefile b/Makefile index ac70797..deda04b 100644 --- a/Makefile +++ b/Makefile @@ -34,7 +34,6 @@ SRCS_NAME += b_type SRCS_NAME += b_unset SRCS_NAME += e_builtins SRCS_NAME += e_externs -SRCS_NAME += e_externs_next SRCS_NAME += e_externs_pipes SRCS_NAME += e_line SRCS_NAME += e_pipes @@ -75,6 +74,7 @@ SRCS_NAME += p_lblock_next SRCS_NAME += p_split SRCS_NAME += u_alias SRCS_NAME += u_parse +SRCS_NAME += u_path SRCS_NAME += u_utils SRCS_NAME += u_vars SRCS_NAME += u_vars_next diff --git a/src/b_type.c b/src/b_type.c index c09f441..d8c6cef 100644 --- a/src/b_type.c +++ b/src/b_type.c @@ -15,11 +15,13 @@ #include #include #include +#include +#include "d_define.h" #include "f_fail.h" #include "s_line.h" #include "s_struct.h" -#include "e_externs_next.h" +#include "u_path.h" #include "u_utils.h" static int8_t absolute_path_exists(char com[]) @@ -40,46 +42,33 @@ static int8_t absolute_path_exists(char com[]) return (0); } -static char *type_get_path(char com[], t_msh *msh) +static void type_get_path(char fullpath[], char com[], t_msh *msh) { - char **envpath; - char *fullpath; - - envpath = NULL; - fullpath = NULL; if (ft_ischarset("/.", com[0]) == TRUE) { if (absolute_path_exists(com)) { - if (!(fullpath = ft_strdup(com))) - { - f_alloc_and_destroy_msh(msh); - } - return (fullpath); + ft_strlcpy(fullpath, com, PATH_MAX); + return ; } - return (NULL); - } - else if ((envpath = get_env_path(msh)) != NULL) - { - fullpath = search_in_path(com, envpath, msh); - ft_delwords(envpath); + return ; } - return (fullpath); + u_search_in_path(fullpath, com, PATH_MAX, msh); } static uint8_t b_check_nonbuilt(char *ptr, uint8_t ret, t_msh *msh) { - char *fullpath; + char fullpath[PATH_MAX]; - fullpath = type_get_path(ptr, msh); - if (fullpath != NULL) + fullpath[0] = C_NUL; + type_get_path(fullpath, ptr, msh); + if (fullpath[0] != C_NUL) ft_printf("%s is %s\n", ptr, fullpath); else { ft_printf("minishell: type: %s: not found\n", ptr); ret = 1; } - ft_memdel((void*)&fullpath); return (ret); } diff --git a/src/d_define.h b/src/d_define.h index 1709bea..e0ca360 100644 --- a/src/d_define.h +++ b/src/d_define.h @@ -71,6 +71,7 @@ #define C_SUB 0x1a #define C_ESC 0x1b #define C_DQUOTE 0x22 +#define C_SHARP 0x23 #define C_DOLLAR 0x24 #define C_SQUOTE 0x27 #define C_AMP 0x26 diff --git a/src/e_externs.c b/src/e_externs.c index 253d758..8761986 100644 --- a/src/e_externs.c +++ b/src/e_externs.c @@ -16,15 +16,18 @@ #include #include #include +#include #include #include "b_export_next.h" -#include "e_externs_next.h" +#include "d_define.h" #include "f_fail.h" #include "m_redirs.h" +#include "s_com.h" #include "s_destroy.h" #include "s_line.h" #include "s_struct.h" +#include "u_path.h" #include "u_utils.h" static void @@ -34,9 +37,9 @@ static void { if (execve(fullpath, ptr->argv, msh->envp) == -1) { - f_exec(fullpath); - ft_memdel((void*)&fullpath); + f_exec(fullpath, ptr->bin); u_eof_fd(msh->fd); + s_com_destroy(&msh->com); s_line_clear(&msh->curr); s_destroy(msh); exit(errno); @@ -83,33 +86,20 @@ static void } } -void - e_extern(t_com *ptr, - t_msh *msh) +void e_extern(t_com *ptr, t_msh *msh) { - char **envpath; - char *fullpath; + char fullpath[PATH_MAX]; + fullpath[0] = C_NUL; if (ft_ischarset("./", ptr->bin[0]) == TRUE) { - if ((fullpath = ft_strdup(ptr->bin)) == NULL) - return ; + ft_strlcpy(fullpath, ptr->bin, PATH_MAX); e_exec_path(fullpath, ptr, msh); - ft_memdel((void*)&fullpath); return ; } - else if ((envpath = get_env_path(msh)) != NULL) + else { - fullpath = search_in_path(ptr->bin, envpath, msh); - ft_delwords(envpath); - if (fullpath == NULL) - { - f_fail_command_not_found(ptr->bin, msh); - } - else - { - e_exec_path(fullpath, ptr, msh); - ft_memdel((void*)&fullpath); - } + u_search_in_path(fullpath, ptr->bin, PATH_MAX, msh); + e_exec_path(fullpath, ptr, msh); } } diff --git a/src/e_externs_next.c b/src/e_externs_next.c deleted file mode 100644 index ed3ee84..0000000 --- a/src/e_externs_next.c +++ /dev/null @@ -1,102 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* e_externs_next.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#include -#include -#include -#include - -#include "f_fail.h" -#include "s_destroy.h" -#include "s_line.h" -#include "s_struct.h" -#include "u_utils.h" - -static char - *get_fullpath(const char p_path[], - const char d_name[], - t_msh *msh) -{ - char *fullpath; - const size_t path_len = ft_strlen(p_path); - const size_t name_len = ft_strlen(d_name); - - if (!(fullpath = (char*)malloc((path_len + name_len + 2) * sizeof(char)))) - { - f_alloc_and_destroy_msh(msh); - } - (void)ft_memcpy(fullpath, p_path, path_len); - *(fullpath + (path_len)) = '/'; - (void)ft_memcpy(fullpath + path_len + 1, d_name, name_len); - *(fullpath + (path_len + name_len + 1)) = '\0'; - return (fullpath); -} - -char - *search_in_path(const char com[], - char *envpath[], - t_msh *msh) -{ - struct dirent *ent; - char **p_path; - char *fullpath; - DIR *dir; - - p_path = envpath; - while (*p_path) - { - if ((dir = opendir(*p_path)) != NULL) - { - while ((ent = readdir(dir)) != NULL) - { - /* TODO: check for not bins (dirs, etc) */ - if (ft_strncmp(com, ent->d_name, ft_strlen(com) + 1) == 0) - { - fullpath = get_fullpath(*p_path, ent->d_name, msh); - closedir(dir); - return (fullpath); - } - } - closedir(dir); - } - p_path++; - } - return (NULL); -} - -char - **get_env_path(t_msh *msh) -{ - size_t i; - char **env_dup; - char **envpath; - char *envline; - - env_dup = u_get_env_var_names(msh); - i = 0; - while (env_dup[i] && ft_strncmp("PATH", env_dup[i], 5) != 0) - i++; - if (env_dup[i] == NULL) - return (NULL); - ft_delwords(env_dup); - envline = ft_strchr(msh->envp[i], '='); - envline += 1; - if (*envline != '\0') - { - if ((envpath = ft_split(envline, ':')) == NULL) - { - f_alloc_and_destroy_msh(msh); - } - return (envpath); - } - return (NULL); -} diff --git a/src/e_externs_next.h b/src/e_externs_next.h deleted file mode 100644 index db03bac..0000000 --- a/src/e_externs_next.h +++ /dev/null @@ -1,21 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* e_externs_next.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef E_EXTERNS_NEXT_H -#define E_EXTERNS_NEXT_H - -#include "s_struct.h" - -char **get_env_path(t_msh *msh); -char *search_in_path(const char com[], char *envpath[], t_msh *msh); - -#endif diff --git a/src/e_externs_pipes.c b/src/e_externs_pipes.c index e6252e4..05a2cf7 100644 --- a/src/e_externs_pipes.c +++ b/src/e_externs_pipes.c @@ -16,16 +16,17 @@ #include #include #include +#include #include #include "d_define.h" -#include "e_externs_next.h" #include "f_fail.h" #include "m_redirs.h" #include "s_destroy.h" #include "s_line.h" #include "s_lpipes.h" #include "s_struct.h" +#include "u_path.h" #include "u_utils.h" static uint8_t @@ -143,8 +144,8 @@ void { struct s_lpipes *head; struct s_lpipes *rptr; - char **envpath; char **fullpath; + char tmp[PATH_MAX]; size_t i; size_t pipes; uint8_t bu_id; @@ -163,7 +164,7 @@ void if ((fullpath[i] = ft_strdup(rptr->com->bin)) == NULL) f_alloc_and_destroy_msh(msh); } - else if ((envpath = get_env_path(msh)) != NULL) + else { if ((bu_id = get_builtin_id(rptr->com->bin, msh)) < FT_BUILTINS_COUNT) @@ -172,8 +173,10 @@ void f_alloc_and_destroy_msh(msh); } else - fullpath[i] = search_in_path(rptr->com->bin, envpath, msh); - ft_delwords(envpath); + { + u_search_in_path(tmp, rptr->com->bin, PATH_MAX, msh); + fullpath[i] = ft_strdup(tmp); + } } i++; rptr = rptr->next; diff --git a/src/f_exec.c b/src/f_exec.c index fccfdea..37cb0f2 100644 --- a/src/f_exec.c +++ b/src/f_exec.c @@ -15,13 +15,23 @@ #include #include +#include "d_define.h" #include "s_struct.h" -void - f_exec(const char fullpath[]) +void f_exec(const char fullpath[], const char bin[]) { - ft_dprintf(STDERR_FILENO, - "minishell: %s: %s\n", - fullpath, - strerror(errno)); + if (fullpath[0] != C_NUL) + { + ft_dprintf(STDERR_FILENO, + "minishell: %s: %s\n", + fullpath, + strerror(errno)); + } + else + { + ft_dprintf(STDERR_FILENO, + "minishell: %s: %s\n", + bin, + strerror(errno)); + } } diff --git a/src/f_exec.h b/src/f_exec.h index 17f3484..1ea0683 100644 --- a/src/f_exec.h +++ b/src/f_exec.h @@ -15,6 +15,6 @@ #include "s_struct.h" -void f_exec(const char fullpath[]); +void f_exec(const char fullpath[], const char bin[]); #endif diff --git a/src/m_loop.c b/src/m_loop.c index 50357eb..41c9e36 100644 --- a/src/m_loop.c +++ b/src/m_loop.c @@ -25,6 +25,8 @@ #include "s_com.h" #include "s_lpipes.h" #include "s_line.h" +#include "u_utils.h" +#include "u_parse.h" #include "u_vars.h" static void @@ -93,6 +95,32 @@ static void } } +static void m_delete_comments(char line[]) +{ + char *ptr; + t_quote_mode mode; + + ptr = line; + mode = Q_NONE; + while (*ptr != C_NUL) + { + if (*ptr == C_DQUOTE) + mode = u_meet_dquote(line, ptr, mode); + else if (*ptr == C_SQUOTE) + mode = u_meet_squote(line, ptr, mode); + else if (mode == Q_NONE && *ptr == C_SHARP + && u_is_not_escaped(line, ptr) == TRUE) + { + if (ptr - line == 0) + *ptr = C_NUL; + else if (ptr - line > 0 && ft_iswhitespace(*(ptr - 1)) == TRUE + && u_is_not_escaped(line, ptr - 1) == TRUE) + *ptr = C_NUL; + } + ptr++; + } +} + uint8_t m_loop(int32_t fd, t_msh *msh) { @@ -107,6 +135,7 @@ uint8_t if (fd == STDIN_FILENO) m_prompt_psx(1, msh); gnl = get_next_line(fd, &line); + m_delete_comments(line); if (line[0] != C_NUL) { line = m_check_multi_backslash(fd, line, msh); diff --git a/src/p_line.c b/src/p_line.c index cdb2fd4..c8381c8 100644 --- a/src/p_line.c +++ b/src/p_line.c @@ -21,26 +21,6 @@ #include "s_struct.h" #include "u_utils.h" -static void - p_delete_comments(char line[]) -{ - char *ptr; - - ptr = line; - while (*ptr != '\0') - { - if (*ptr == '#') - { - if ((ptr - line) == 0 || ft_iswhitespace(*(ptr - 1)) == TRUE) - { - *ptr = '\0'; - return ; - } - } - ptr++; - } -} - static t_bool p_check_whitespaces_only(char line[]) { @@ -61,7 +41,6 @@ static t_bool void p_line(char line[], t_msh *msh) { - p_delete_comments(line); if (p_check_whitespaces_only(line) == TRUE) { return ; diff --git a/src/u_path.c b/src/u_path.c new file mode 100644 index 0000000..b43946f --- /dev/null +++ b/src/u_path.c @@ -0,0 +1,72 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* u_path.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include +#include + +#include "s_struct.h" +#include "u_vars.h" + +static void u_get_fullpath(char fullpath[], + const char p_path[], + const char d_name[], + size_t dstsize) +{ + const size_t path_len = ft_strlen(p_path); + const size_t name_len = ft_strlen(d_name); + + fullpath[0] = C_NUL; + if (path_len + name_len < dstsize) + { + (void)ft_memcpy(fullpath, p_path, path_len); + *(fullpath + (path_len)) = '/'; + ft_memcpy(fullpath + path_len + 1, d_name, name_len); + *(fullpath + (path_len + name_len + 1)) = '\0'; + } +} + +uint8_t u_search_in_path(char fullpath[], + const char com[], + size_t dstsize, + t_msh *msh) +{ + struct dirent *ent; + char tmp[ARG_MAX]; + char *tok_path; + DIR *dir; + + if (u_get_var_value(tmp, "$PATH", ARG_MAX, msh) != 0) + return (1); + tok_path = ft_strtok(tmp, ":"); + while (tok_path != NULL) + { + if ((dir = opendir(tok_path)) != NULL) + { + while ((ent = readdir(dir)) != NULL) + { + if (ft_strncmp(com, ent->d_name, ft_strlen(com) + 1) == 0) + { + u_get_fullpath(fullpath, tok_path, ent->d_name, dstsize); + closedir(dir); + if (fullpath[0] == C_NUL) + return (1); + return (0); + } + } + closedir(dir); + } + tok_path = ft_strtok(NULL, ":"); + } + return (1); +} diff --git a/src/u_path.h b/src/u_path.h new file mode 100644 index 0000000..0c8e256 --- /dev/null +++ b/src/u_path.h @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* u_path.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef U_PATH_H +#define U_PATH_H + +#include + +#include "s_struct.h" + +uint8_t u_search_in_path(char fullpath[], + const char com[], + size_t dstsize, + t_msh *msh); + +#endif diff --git a/src/u_utils.c b/src/u_utils.c index dd1634b..4e332b3 100644 --- a/src/u_utils.c +++ b/src/u_utils.c @@ -70,7 +70,7 @@ uint64_t uint64_t argc; argc = 0; - while (args[argc]) + while (args[argc] != NULL) { argc++; } diff --git a/src/u_vars.c b/src/u_vars.c index 95c6ffa..97fc580 100644 --- a/src/u_vars.c +++ b/src/u_vars.c @@ -98,22 +98,26 @@ void ** varname[] wasn't found. */ -void - u_get_var_value(char str[], - const char varname[], - size_t dstsize, - t_msh *msh) +uint8_t u_get_var_value(char str[], + const char varname[], + size_t dstsize, + t_msh *msh) { str[0] = C_NUL; u_get_special_var(str, varname, dstsize, msh); if (str[0] != C_NUL) { - return ; + return (0); } u_get_custom_var(str, varname, dstsize, msh); if (str[0] != C_NUL) { - return ; + return (0); } u_get_frm_env(str, varname, dstsize, msh); + if (str[0] != C_NUL) + { + return (0); + } + return (1); } diff --git a/src/u_vars.h b/src/u_vars.h index 27c975f..c76518e 100644 --- a/src/u_vars.h +++ b/src/u_vars.h @@ -13,6 +13,7 @@ #ifndef U_VARS_H #define U_VARS_H +#include #include #include "s_struct.h" @@ -21,7 +22,7 @@ void u_get_custom_var(char str[], const char varname[], size_t dstsize, t_msh *msh); -void u_get_var_value(char str[], +uint8_t u_get_var_value(char str[], const char varname[], size_t dstsize, t_msh *msh); -- cgit v1.2.3 From aeb1dec54283f4506f47893923b29a71499353de Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Tue, 15 Sep 2020 20:14:00 +0200 Subject: Init fix --- src/d_define.h | 2 +- src/m_funptr.c | 4 ---- src/s_struct.h | 1 - 3 files changed, 1 insertion(+), 6 deletions(-) diff --git a/src/d_define.h b/src/d_define.h index e0ca360..b905d5f 100644 --- a/src/d_define.h +++ b/src/d_define.h @@ -19,7 +19,7 @@ ** ====== CLASSICS ====== */ -#define FT_BUILTINS "echo|cd|pwd|export|unset|env|exit|type|[|alias" +#define FT_BUILTINS "echo:cd:pwd:export:unset:env:exit:type:[:alias" /* ** ====== PSX ====== diff --git a/src/m_funptr.c b/src/m_funptr.c index 1983e73..bb8d80d 100644 --- a/src/m_funptr.c +++ b/src/m_funptr.c @@ -32,8 +32,4 @@ void msh->bu_ptr[FT_ID_TYPE] = b_type; msh->bu_ptr[FT_ID_SQB] = b_sqb; msh->bu_ptr[FT_ID_ALIAS] = b_alias; - if ((msh->bu_ref = ft_split(FT_BUILTINS, '|')) == NULL) - { - f_alloc_and_destroy_msh(msh); - } } diff --git a/src/s_struct.h b/src/s_struct.h index ba56e82..871b0f4 100644 --- a/src/s_struct.h +++ b/src/s_struct.h @@ -81,7 +81,6 @@ typedef struct s_msh struct s_lvars *vars; struct s_lalias *alias; char **envp; - char **bu_ref; char ps[4][1024]; char env_fork_tmp[128][4096]; char sqb_ref[FT_ID_SQB_COUNT][4]; -- cgit v1.2.3 From 33a5806163fb91e4a72e188a9933ada20395f4e9 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Tue, 15 Sep 2020 20:44:45 +0200 Subject: Builtins ref toked --- src/b_alias.c | 4 +++- src/b_type.c | 17 ++++++++++------- src/d_define.h | 3 ++- src/e_externs_pipes.c | 19 ++----------------- src/e_line.c | 17 ++--------------- src/s_destroy.c | 1 - src/u_utils.c | 31 ++++++++++++++++++++++--------- src/u_utils.h | 1 + 8 files changed, 42 insertions(+), 51 deletions(-) diff --git a/src/b_alias.c b/src/b_alias.c index daa6e99..5ddc46f 100644 --- a/src/b_alias.c +++ b/src/b_alias.c @@ -99,9 +99,11 @@ uint8_t ret = 0; if (argc == 0) { + if (msh->alias == NULL) + return (0); b_print_alias_list(msh); } - if (argc > 0) + else if (argc > 0) { i = 0; while (args[i] != NULL) diff --git a/src/b_type.c b/src/b_type.c index d8c6cef..c490aae 100644 --- a/src/b_type.c +++ b/src/b_type.c @@ -72,16 +72,19 @@ static uint8_t b_check_nonbuilt(char *ptr, uint8_t ret, t_msh *msh) return (ret); } -static uint8_t b_check_builtins(char *ptr, t_msh *msh) +static uint8_t b_check_builtins(char *ptr) { - char **p_bu; + char tmp[M_BUILTINS_REF_LEN]; + char *tok_bu; - p_bu = msh->bu_ref; - while (*p_bu != NULL && ft_strncmp(ptr, *p_bu, ft_strlen(*p_bu) + 1) != 0) + ft_strlcpy(tmp, M_BUILTINS_REF, M_BUILTINS_REF_LEN); + tok_bu = ft_strtok(tmp, ":"); + while (tok_bu != NULL + && ft_strncmp(ptr, tok_bu, ft_strlen(tok_bu) + 1) != 0) { - p_bu++; + tok_bu = ft_strtok(NULL, ":"); } - if (*p_bu != NULL) + if (tok_bu != NULL) { ft_printf("%s is a shell builtin\n", ptr); return (0); @@ -110,7 +113,7 @@ uint8_t b_type(char *args[], t_msh *msh) { ft_printf("%s is aliased to `%s'\n", *ptr, p_alias->val); } - else if (b_check_builtins(*ptr, msh) == 1) + else if (b_check_builtins(*ptr) == 1) ret = b_check_nonbuilt(*ptr, ret, msh); ptr++; } diff --git a/src/d_define.h b/src/d_define.h index b905d5f..f5dd32e 100644 --- a/src/d_define.h +++ b/src/d_define.h @@ -19,7 +19,8 @@ ** ====== CLASSICS ====== */ -#define FT_BUILTINS "echo:cd:pwd:export:unset:env:exit:type:[:alias" +#define M_BUILTINS_REF "echo:cd:pwd:export:unset:env:exit:type:[:alias" +#define M_BUILTINS_REF_LEN 48 /* ** ====== PSX ====== diff --git a/src/e_externs_pipes.c b/src/e_externs_pipes.c index 05a2cf7..ca843d0 100644 --- a/src/e_externs_pipes.c +++ b/src/e_externs_pipes.c @@ -29,21 +29,6 @@ #include "u_path.h" #include "u_utils.h" -static uint8_t - get_builtin_id(const char com[], - t_msh *msh) -{ - uint8_t i; - - i = 0; - while (msh->bu_ref[i] && ft_strncmp(com, msh->bu_ref[i], - ft_strlen(msh->bu_ref[i]) + 1) != 0) - { - i++; - } - return (i); -} - static void e_pipe_child(char *fullpath[], uint8_t pipe_id, @@ -56,7 +41,7 @@ static void dup_redirs(ptr, msh); if (ft_strncmp(fullpath[pipe_id], "builtin", 8) == 0) { - bu_id = get_builtin_id(ptr->bin, msh); + bu_id = u_get_builtin_id(ptr->bin); ret = msh->bu_ptr[bu_id](ptr->argv + 1, msh); u_eof_fd(msh->fd); s_lpipes_clear(&msh->pipes); @@ -166,7 +151,7 @@ void } else { - if ((bu_id = get_builtin_id(rptr->com->bin, msh)) + if ((bu_id = u_get_builtin_id(rptr->com->bin)) < FT_BUILTINS_COUNT) { if ((fullpath[i] = ft_strdup("builtin")) == NULL) diff --git a/src/e_line.c b/src/e_line.c index bb97556..caf3d75 100644 --- a/src/e_line.c +++ b/src/e_line.c @@ -20,20 +20,7 @@ #include "s_lpipes.h" #include "s_com.h" #include "s_struct.h" - -static uint8_t - e_get_builtin_id(const char bin[], t_msh *msh) -{ - uint8_t i; - - i = 0; - while (msh->bu_ref[i] != NULL && ft_strncmp(bin, msh->bu_ref[i], - ft_strlen(msh->bu_ref[i]) + 1) != 0) - { - i++; - } - return (i); -} +#include "u_utils.h" void e_line(t_msh *msh) @@ -45,7 +32,7 @@ void else if (msh->com != NULL) { if (msh->com->bin != NULL && - (bu_id = e_get_builtin_id(msh->com->bin, msh)) + (bu_id = u_get_builtin_id(msh->com->bin)) < FT_BUILTINS_COUNT) e_builtin(msh->com, bu_id, msh); else if (msh->com->bin != NULL) diff --git a/src/s_destroy.c b/src/s_destroy.c index f9ce3c4..54f3d4a 100644 --- a/src/s_destroy.c +++ b/src/s_destroy.c @@ -21,7 +21,6 @@ void { ft_memdel((void*)&msh->shname); ft_memdel((void*)&msh->cwd); - ft_delwords(msh->bu_ref); ft_delwords(msh->envp); lvars_clear(&msh->vars); s_lalias_clear(&msh->alias); diff --git a/src/u_utils.c b/src/u_utils.c index 4e332b3..d56d7c0 100644 --- a/src/u_utils.c +++ b/src/u_utils.c @@ -19,8 +19,7 @@ #include "f_fail.h" #include "s_struct.h" -t_bool - u_is_not_escaped(const char *head, const char *ptr) +t_bool u_is_not_escaped(const char *head, const char *ptr) { if (((ptr - head) == 0) || ((ptr - head) >= 1 && *(ptr - 1) != C_BACKS) || @@ -29,8 +28,7 @@ t_bool return (FALSE); } -void - u_eof_fd(int32_t fd) +void u_eof_fd(int32_t fd) { char *line; @@ -39,11 +37,10 @@ void ft_memdel((void*)&line); } -char - **u_get_env_var_names(t_msh *msh) +char **u_get_env_var_names(t_msh *msh) { - size_t i; char **vars; + size_t i; i = 0; while (msh->envp[i] != NULL) @@ -64,8 +61,7 @@ char return (vars); } -uint64_t - u_builtins_get_argc(const char *args[]) +uint64_t u_builtins_get_argc(const char *args[]) { uint64_t argc; @@ -76,3 +72,20 @@ uint64_t } return (argc); } + +uint8_t u_get_builtin_id(const char bin[]) +{ + char tmp[M_BUILTINS_REF_LEN]; + char *tok; + uint8_t i; + + i = 0; + ft_strlcpy(tmp, M_BUILTINS_REF, M_BUILTINS_REF_LEN); + tok = ft_strtok(tmp, ":"); + while (tok != NULL && ft_strncmp(bin, tok, ft_strlen(tok) + 1) != 0) + { + tok = ft_strtok(NULL, ":"); + i++; + } + return (i); +} diff --git a/src/u_utils.h b/src/u_utils.h index 12606b7..496f50e 100644 --- a/src/u_utils.h +++ b/src/u_utils.h @@ -22,5 +22,6 @@ t_bool u_is_not_escaped(const char *head, const char *ptr); void u_eof_fd(int32_t fd); uint64_t u_builtins_get_argc(const char *args[]); char **u_get_env_var_names(t_msh *msh); +uint8_t u_get_builtin_id(const char bin[]); #endif -- cgit v1.2.3 From 647e497a2bef6fa6f34840df13fcddf58ec47355 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Wed, 16 Sep 2020 19:28:27 +0200 Subject: TODO update --- TODO.org | 1 + 1 file changed, 1 insertion(+) diff --git a/TODO.org b/TODO.org index 9cc733d..b94e93c 100644 --- a/TODO.org +++ b/TODO.org @@ -35,6 +35,7 @@ ** DONE [#B] && || ** TODO [#B] & ** TODO [#B] $_ +** TODO [#B] Parse error before p_line for scripts ** DONE [#B] .minishellrc ** DONE [#B] .minishell_history ** TODO [#B] history builtin -- cgit v1.2.3