summaryrefslogtreecommitdiffstats
path: root/src/p_args.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/p_args.c')
-rw-r--r--src/p_args.c121
1 files changed, 63 insertions, 58 deletions
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;