From 487a66394061f2d14a2fa421302966b5442d643f Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Mon, 27 Jul 2020 18:55:07 +0200 Subject: I couldn't see shit --- src/b_export.c | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 src/b_export.c (limited to 'src/b_export.c') diff --git a/src/b_export.c b/src/b_export.c new file mode 100644 index 0000000..5e7ee14 --- /dev/null +++ b/src/b_export.c @@ -0,0 +1,80 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* b_export.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 "b_env.h" +#include "f_fail.h" +#include "s_struct.h" +#include "u_utils.h" + +static int8_t + check_valid_identifier(const char *arg) +{ + char *ptr; + + ptr = (char*)arg; + if (ft_isalpha(ptr[0])) + { + return (1); + } + return (0); +} + +static int8_t + check_equals(const char *arg) +{ + char *ptr; + + ptr = (char*)arg; + while (*ptr) + { + if (*ptr == '=') + return (1); + ptr++; + } + return (0); +} + +uint8_t + b_export(char *args[], + t_msh *msh) +{ + const uint64_t argc = get_argc((const char**)args); + char **ptr; + int8_t next; + uint8_t r; + + if (argc == 0) + { + return (b_env(NULL, msh)); + } + ptr = args; + r = 0; + while (*ptr) + { + next = 0; + if (!check_valid_identifier(*ptr)) + { + fail_identifier("export", *ptr, msh); + next = 1; + r = 1; + } + if (next == 0 && !check_equals(*ptr)) + next = 1; + ptr++; + } + /* TODO: finish export */ + return (r); +} -- cgit v1.2.3 From f441e00ea60a5e485d853d5dd3ac99a8535a40cc Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Thu, 30 Jul 2020 18:22:32 +0200 Subject: In progress --- src/b_export.c | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'src/b_export.c') diff --git a/src/b_export.c b/src/b_export.c index 5e7ee14..95ccf40 100644 --- a/src/b_export.c +++ b/src/b_export.c @@ -64,6 +64,7 @@ uint8_t r = 0; while (*ptr) { + ft_printf("export: [%s]\n", *ptr); next = 0; if (!check_valid_identifier(*ptr)) { @@ -72,7 +73,16 @@ uint8_t r = 1; } if (next == 0 && !check_equals(*ptr)) + { next = 1; + /* TODO: export already set variable */ + /* examples: */ + /* ~> QWE=qwe */ + /* ~> export QWE */ + /* -------------- */ + /* # non-existant variable QWE */ + /* ~> export QWE */ + } ptr++; } /* TODO: finish export */ -- cgit v1.2.3 From c19bd35afdb45e49cebdfd96e7adb1e6fe477f0c Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Thu, 30 Jul 2020 20:30:58 +0200 Subject: Double exit no more --- src/b_export.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/b_export.c') diff --git a/src/b_export.c b/src/b_export.c index 95ccf40..9cb1c14 100644 --- a/src/b_export.c +++ b/src/b_export.c @@ -68,7 +68,7 @@ uint8_t next = 0; if (!check_valid_identifier(*ptr)) { - fail_identifier("export", *ptr, msh); + f_fail_identifier("export", *ptr, msh); next = 1; r = 1; } -- cgit v1.2.3 From 27046e89845d2c25ed743a074815c6ef88a7e6de Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Sat, 1 Aug 2020 20:05:15 +0200 Subject: Bug fix --- src/b_export.c | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) (limited to 'src/b_export.c') diff --git a/src/b_export.c b/src/b_export.c index 9cb1c14..8225b51 100644 --- a/src/b_export.c +++ b/src/b_export.c @@ -14,12 +14,13 @@ #include #include +#include "d_enum.h" #include "b_env.h" #include "f_fail.h" #include "s_struct.h" #include "u_utils.h" -static int8_t +static t_bool check_valid_identifier(const char *arg) { char *ptr; @@ -27,24 +28,24 @@ static int8_t ptr = (char*)arg; if (ft_isalpha(ptr[0])) { - return (1); + return (TRUE); } - return (0); + return (FALSE); } -static int8_t +static t_bool check_equals(const char *arg) { char *ptr; ptr = (char*)arg; - while (*ptr) + while (*ptr != '\0') { if (*ptr == '=') - return (1); + return (TRUE); ptr++; } - return (0); + return (FALSE); } uint8_t @@ -53,7 +54,7 @@ uint8_t { const uint64_t argc = get_argc((const char**)args); char **ptr; - int8_t next; + t_bool next; uint8_t r; if (argc == 0) @@ -65,16 +66,16 @@ uint8_t while (*ptr) { ft_printf("export: [%s]\n", *ptr); - next = 0; - if (!check_valid_identifier(*ptr)) + next = FALSE; + if (check_valid_identifier(*ptr) == FALSE) { f_fail_identifier("export", *ptr, msh); - next = 1; + next = TRUE; r = 1; } - if (next == 0 && !check_equals(*ptr)) + if (next == FALSE && check_equals(*ptr) == FALSE) { - next = 1; + next = TRUE; /* TODO: export already set variable */ /* examples: */ /* ~> QWE=qwe */ @@ -83,6 +84,9 @@ uint8_t /* # non-existant variable QWE */ /* ~> export QWE */ } + else if (next == FALSE && check_equals(*ptr) == TRUE) + { + } ptr++; } /* TODO: finish export */ -- cgit v1.2.3 From 7c0ce2228470d69a9dcc1d734f000c77c026ec24 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Sat, 1 Aug 2020 20:48:32 +0200 Subject: export first part is working fine --- src/b_export.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 56 insertions(+), 9 deletions(-) (limited to 'src/b_export.c') diff --git a/src/b_export.c b/src/b_export.c index 8225b51..9fe5709 100644 --- a/src/b_export.c +++ b/src/b_export.c @@ -17,8 +17,12 @@ #include "d_enum.h" #include "b_env.h" #include "f_fail.h" +#include "s_destroy.h" +#include "s_lcom.h" +#include "s_lvars.h" #include "s_struct.h" #include "u_utils.h" +#include "u_vars.h" static t_bool check_valid_identifier(const char *arg) @@ -48,12 +52,55 @@ static t_bool return (FALSE); } +static void + b_add_to_env_from_globals(const char varname[], + const char var[], + t_msh *msh) +{ + size_t i; + char **nenvp; + + i = 0; + while (msh->envp[i] != NULL) + i++; + if (!(nenvp = (char**)malloc((i + 2) * sizeof(char*)))) + { + lcom_clear(&msh->curr); + s_destroy(msh); + f_fail_alloc(msh); + } + i = 0; + while (msh->envp[i] != NULL) + { + if (!(nenvp[i] = ft_strdup(msh->envp[i]))) + { + lcom_clear(&msh->curr); + s_destroy(msh); + f_fail_alloc(msh); + } + i++; + } + if (!(nenvp[i] = ft_strdup(var))) + { + lcom_clear(&msh->curr); + s_destroy(msh); + f_fail_alloc(msh); + } + nenvp[i + 1] = 0; + ft_delwords(msh->envp); + lvars_delone(&msh->vars, varname); + msh->envp = nenvp; +} + uint8_t b_export(char *args[], - t_msh *msh) + t_msh *msh) { + /* TODO: norme */ const uint64_t argc = get_argc((const char**)args); char **ptr; + char *varval; + char fmt[255]; t_bool next; uint8_t r; @@ -65,7 +112,6 @@ uint8_t r = 0; while (*ptr) { - ft_printf("export: [%s]\n", *ptr); next = FALSE; if (check_valid_identifier(*ptr) == FALSE) { @@ -76,16 +122,17 @@ uint8_t if (next == FALSE && check_equals(*ptr) == FALSE) { next = TRUE; - /* TODO: export already set variable */ - /* examples: */ - /* ~> QWE=qwe */ - /* ~> export QWE */ - /* -------------- */ - /* # non-existant variable QWE */ - /* ~> export QWE */ + ft_sprintf(fmt, "$%s", *ptr); + varval = get_cstm_vr(fmt, msh); + if (varval != NULL) + { + ft_sprintf(fmt, "%s=%s", *ptr, varval); + b_add_to_env_from_globals(*ptr, fmt, msh); + } } else if (next == FALSE && check_equals(*ptr) == TRUE) { + /* TODO: verify global vars, delete if needed, export the thing */ } ptr++; } -- cgit v1.2.3 From 8680737a802539f3c21a295ad45eb9be72c73f5f Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Sat, 1 Aug 2020 21:25:09 +0200 Subject: More UNIX friendly libft --- src/b_export.c | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) (limited to 'src/b_export.c') diff --git a/src/b_export.c b/src/b_export.c index 9fe5709..9a7c37d 100644 --- a/src/b_export.c +++ b/src/b_export.c @@ -64,28 +64,16 @@ static void while (msh->envp[i] != NULL) i++; if (!(nenvp = (char**)malloc((i + 2) * sizeof(char*)))) - { - lcom_clear(&msh->curr); - s_destroy(msh); - f_fail_alloc(msh); - } + f_fail_alloc_and_destroy(msh); i = 0; while (msh->envp[i] != NULL) { if (!(nenvp[i] = ft_strdup(msh->envp[i]))) - { - lcom_clear(&msh->curr); - s_destroy(msh); - f_fail_alloc(msh); - } + f_fail_alloc_and_destroy(msh); i++; } if (!(nenvp[i] = ft_strdup(var))) - { - lcom_clear(&msh->curr); - s_destroy(msh); - f_fail_alloc(msh); - } + f_fail_alloc_and_destroy(msh); nenvp[i + 1] = 0; ft_delwords(msh->envp); lvars_delone(&msh->vars, varname); -- cgit v1.2.3 From ebee57ed2034a90538a75648453951480f17318d Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Sat, 1 Aug 2020 22:19:08 +0200 Subject: Pretty good --- src/b_export.c | 1 + 1 file changed, 1 insertion(+) (limited to 'src/b_export.c') diff --git a/src/b_export.c b/src/b_export.c index 9a7c37d..52790e4 100644 --- a/src/b_export.c +++ b/src/b_export.c @@ -88,6 +88,7 @@ uint8_t const uint64_t argc = get_argc((const char**)args); char **ptr; char *varval; + /* char *varname; */ char fmt[255]; t_bool next; uint8_t r; -- cgit v1.2.3 From 6ca882c8feb9fb88cf8e1421e882fb7e9efb97f8 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Mon, 3 Aug 2020 16:50:32 +0200 Subject: In progress --- src/b_export.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'src/b_export.c') diff --git a/src/b_export.c b/src/b_export.c index 52790e4..c47a8fb 100644 --- a/src/b_export.c +++ b/src/b_export.c @@ -14,8 +14,9 @@ #include #include -#include "d_enum.h" #include "b_env.h" +#include "b_export_next.h" +#include "d_enum.h" #include "f_fail.h" #include "s_destroy.h" #include "s_lcom.h" @@ -121,7 +122,7 @@ uint8_t } else if (next == FALSE && check_equals(*ptr) == TRUE) { - /* TODO: verify global vars, delete if needed, export the thing */ + b_export_with_equals(*ptr, msh); } ptr++; } -- cgit v1.2.3 From 3b32afb082ba823885ac355413294b5e7ff3c360 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Mon, 3 Aug 2020 17:04:30 +0200 Subject: Changed function name --- src/b_export.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/b_export.c') diff --git a/src/b_export.c b/src/b_export.c index c47a8fb..fc1657e 100644 --- a/src/b_export.c +++ b/src/b_export.c @@ -86,7 +86,7 @@ uint8_t t_msh *msh) { /* TODO: norme */ - const uint64_t argc = get_argc((const char**)args); + const uint64_t argc = u_builtins_get_argc((const char**)args); char **ptr; char *varval; /* char *varname; */ -- cgit v1.2.3 From 1886374416e5dc436db68e3518603258ae32bac0 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Mon, 3 Aug 2020 17:26:41 +0200 Subject: Splitted var --- src/b_export.c | 1 - 1 file changed, 1 deletion(-) (limited to 'src/b_export.c') diff --git a/src/b_export.c b/src/b_export.c index fc1657e..7aeed93 100644 --- a/src/b_export.c +++ b/src/b_export.c @@ -89,7 +89,6 @@ uint8_t const uint64_t argc = u_builtins_get_argc((const char**)args); char **ptr; char *varval; - /* char *varname; */ char fmt[255]; t_bool next; uint8_t r; -- cgit v1.2.3 From bfaf4c53a8406ea0bfbd8699807b567a5322a03f Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Mon, 3 Aug 2020 19:00:47 +0200 Subject: Export is working --- src/b_export.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/b_export.c') diff --git a/src/b_export.c b/src/b_export.c index 7aeed93..43e1c7d 100644 --- a/src/b_export.c +++ b/src/b_export.c @@ -31,7 +31,7 @@ static t_bool char *ptr; ptr = (char*)arg; - if (ft_isalpha(ptr[0])) + if (ft_isalpha(ptr[0]) || ptr[0] == '_') { return (TRUE); } @@ -89,7 +89,7 @@ uint8_t const uint64_t argc = u_builtins_get_argc((const char**)args); char **ptr; char *varval; - char fmt[255]; + char fmt[4096]; t_bool next; uint8_t r; @@ -112,7 +112,7 @@ uint8_t { next = TRUE; ft_sprintf(fmt, "$%s", *ptr); - varval = get_cstm_vr(fmt, msh); + varval = u_get_cstm_vr(fmt, msh); if (varval != NULL) { ft_sprintf(fmt, "%s=%s", *ptr, varval); -- cgit v1.2.3 From 48acb01ff5a2cf22adebfc25213c2597911b2fd4 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Tue, 4 Aug 2020 13:55:12 +0200 Subject: Unset is fine --- src/b_export.c | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) (limited to 'src/b_export.c') diff --git a/src/b_export.c b/src/b_export.c index 43e1c7d..fa9a6d1 100644 --- a/src/b_export.c +++ b/src/b_export.c @@ -26,7 +26,7 @@ #include "u_vars.h" static t_bool - check_valid_identifier(const char *arg) + check_valid_identifier(const char arg[]) { char *ptr; @@ -47,7 +47,9 @@ static t_bool while (*ptr != '\0') { if (*ptr == '=') + { return (TRUE); + } ptr++; } return (FALSE); @@ -86,25 +88,24 @@ uint8_t t_msh *msh) { /* TODO: norme */ - const uint64_t argc = u_builtins_get_argc((const char**)args); - char **ptr; - char *varval; - char fmt[4096]; - t_bool next; - uint8_t r; + char **ptr; + char *varval; + char fmt[4096]; + t_bool next; + uint8_t r; - if (argc == 0) + if (args[0] == NULL) { return (b_env(NULL, msh)); } - ptr = args; r = 0; - while (*ptr) + ptr = args; + while (*ptr != NULL) { next = FALSE; if (check_valid_identifier(*ptr) == FALSE) { - f_fail_identifier("export", *ptr, msh); + f_fail_identifier("export", *ptr); next = TRUE; r = 1; } -- cgit v1.2.3