diff options
author | JozanLeClerc <bousset.rudy@gmail.com> | 2020-09-03 17:18:41 +0200 |
---|---|---|
committer | JozanLeClerc <bousset.rudy@gmail.com> | 2020-09-03 17:18:41 +0200 |
commit | 869498a720e3c54b206f346dea6bf9b7995a7778 (patch) | |
tree | 92f1baffbb318fd8d86a1e82942d0992530f9c87 | |
parent | how tf do i use tok (diff) | |
download | 42-minishell-869498a720e3c54b206f346dea6bf9b7995a7778.tar.gz 42-minishell-869498a720e3c54b206f346dea6bf9b7995a7778.tar.bz2 42-minishell-869498a720e3c54b206f346dea6bf9b7995a7778.tar.xz 42-minishell-869498a720e3c54b206f346dea6bf9b7995a7778.tar.zst 42-minishell-869498a720e3c54b206f346dea6bf9b7995a7778.zip |
Genius in progress
-rw-r--r-- | src/p_args.c | 37 | ||||
-rw-r--r-- | src/p_args_next.c | 44 | ||||
-rw-r--r-- | 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 <stdlib.h> #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 <libft.h> #include <stdint.h> -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 <stdint.h> -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 |