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.c70
1 files changed, 69 insertions, 1 deletions
diff --git a/src/p_args.c b/src/p_args.c
index d8ca029..1cab59d 100644
--- a/src/p_args.c
+++ b/src/p_args.c
@@ -14,6 +14,8 @@
#include <stdint.h>
#include <stdlib.h>
+#include "d_define.h"
+
/* ================= */
/* TODO: DELETE THIS */
/* ================= */
@@ -35,14 +37,80 @@ p_print(char *words[])
/* TODO: DELETE ABOVE */
/* ================== */
+static void
+ p_skip_delim(char *ptr, char c)
+{
+ ptr++;
+ while (*ptr != C_NULL && *ptr != c)
+ {
+ ptr++;
+ if (*ptr == c && *(ptr - 1) == C_BACKSLASH)
+ ptr++;
+ }
+ if (*ptr != C_NULL)
+ ptr++;
+}
+
+static uint16_t
+ p_count_args(char *ptr, uint16_t argc)
+{
+ if (*ptr == C_NULL)
+ return (argc);
+ while (*ptr != C_NULL && ft_iswhitespace(*ptr) == TRUE)
+ ptr++;
+ if (*ptr != C_SQUOTE && *ptr != C_DQUOTE)
+ {
+ while (*ptr != C_NULL && ft_iswhitespace(*ptr) == FALSE)
+ {
+ ptr++;
+ if ((*ptr == C_SQUOTE || *ptr == C_DQUOTE) &&
+ *(ptr - 1) != C_BACKSLASH)
+ {
+ ptr++;
+ return (p_count_args(ptr, argc));
+ }
+ }
+ }
+ else if (*ptr == C_SQUOTE)
+ {
+ p_skip_delim(ptr, C_SQUOTE);
+ }
+ else if (*ptr == C_DQUOTE)
+ {
+ p_skip_delim(ptr, C_DQUOTE);
+ }
+ return (p_count_args(ptr, argc + 1));
+}
+
+static char
+ **p_split_words_no_rdr(const char word[])
+{
+ char **words;
+ char *ptr;
+ uint16_t argc;
+
+ ptr = (char*)word;
+ argc = p_count_args(ptr, 0);
+ ft_printf("%hu\n", argc);
+ exit(0);
+ if ((words = (char**)malloc((argc + 1) * sizeof(char*))) == NULL)
+ return (NULL);
+ words[argc] = NULL;
+ return (words);
+}
+
+
char
**p_split_args(const char word[], int8_t redir)
{
- char **words;
+ 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);
return (words);
}