/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* 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 #include "d_define.h" #include "p_args.h" #include "p_args_next.h" #include "p_args_quotes.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 char *p_skip_delim(char *ptr, char c) { ptr++; if (*ptr == c) return (++ptr); while (*ptr != C_NULL && *ptr != c) { 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 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[]) { static uint16_t preset = 512; 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) { p_skip_clean_arg(head, &ptr, argc, start); } else if (*ptr == C_SQUOTE || *ptr == C_DQUOTE) { 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)); } else if (*ptr == C_NULL) return (argc); return (p_count_args(head, ptr, argc + 1, start)); /* TODO: quotes parse error */ } 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); ft_printf("%hu\n", argc); if ((words = (char**)malloc((argc + 1) * sizeof(char*))) == NULL) return (NULL); words[argc] = NULL; if ((to_del = p_dup_words(words, word, argc, start)) != argc) { p_del_alloced_words(words, to_del); return (NULL); } p_args_quotes(words); return (words); } char **p_split_args(const char word[], int8_t redir) { 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); exit(0); return (words); } p_print(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); */ }