/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* 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_escape.h" #include "u_parse.h" #include "u_utils.h" static void p_meet_bs(char *ptr, t_quote_mode mode) { if (mode != Q_SINGLE) { if (*(ptr + 1) == C_BACKS) { *ptr = C_SUB; *(ptr + 1) = C_SUB; } } } 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); } static char *p_skip_whitespace(char *ptr) { while (*ptr != C_NUL && ft_iswhitespace(*ptr)) ptr++; return (ptr); } /* ** TODO: quotes parse error */ static uint16_t p_count_args(const char word[], size_t start[], uint16_t count, t_quote_mode mode) { char *ptr; ptr = (char *)word; ptr = p_skip_whitespace(ptr); start[0] = (ptr - word); while (*ptr != C_NUL) { if (*ptr == C_BACKS) p_meet_bs(ptr, mode); if (*ptr == C_DQUOTE) mode = u_meet_dquote(word, ptr, mode); else 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 = p_skip_whitespace(ptr); start[count] = (ptr - word); count += (*ptr != C_NUL) ? (1) : (0); ptr -= 1; } ptr++; } return (count); } char **p_split_args(char word[]) { size_t start[512]; char **words; uint16_t argc; uint16_t to_del; words = NULL; argc = p_count_args(word, start, 1, Q_NONE); 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_escape_chars_and_quotes(words); return (words); }