summaryrefslogtreecommitdiffstats
path: root/src/p_args_escape.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/p_args_escape.c')
-rw-r--r--src/p_args_escape.c91
1 files changed, 90 insertions, 1 deletions
diff --git a/src/p_args_escape.c b/src/p_args_escape.c
index 2a3a702..182ef2f 100644
--- a/src/p_args_escape.c
+++ b/src/p_args_escape.c
@@ -10,18 +10,105 @@
/* */
/* ************************************************************************** */
+#include <libft.h>
#include <stddef.h>
#include "d_define.h"
+#include "u_utils.h"
+
+static t_quote_mode
+ p_escape_squote(char *ptr, char word[], t_quote_mode mode)
+{
+ if (mode == Q_NONE)
+ {
+ (void)ft_memmove(word + (ptr - word), ptr + 1,
+ (ft_strlen(ptr + 1) + 1) * sizeof(char));
+ return (Q_SINGLE);
+ }
+ else if (mode == Q_SINGLE)
+ {
+ (void)ft_memmove(word + (ptr - word), ptr + 1,
+ (ft_strlen(ptr + 1) + 1) * sizeof(char));
+ return (Q_NONE);
+ }
+ return (mode);
+}
+
+static t_quote_mode
+ p_escape_dquote(char *ptr, char word[], t_quote_mode mode)
+{
+ if (mode == Q_NONE)
+ {
+ if (u_is_true_quote(word, ptr) == TRUE)
+ {
+ (void)ft_memmove(word + (ptr - word), ptr + 1,
+ (ft_strlen(ptr + 1) + 1) * sizeof(char));
+ }
+ return (Q_DOUBLE);
+ }
+ else if (mode == Q_DOUBLE)
+ {
+ if (u_is_true_quote(word, ptr) == TRUE)
+ {
+ (void)ft_memmove(word + (ptr - word), ptr + 1,
+ (ft_strlen(ptr + 1) + 1) * sizeof(char));
+ }
+ return (Q_NONE);
+ }
+ return (mode);
+}
+
+static void
+ p_escape_bs(char *ptr, char word[], t_quote_mode mode)
+{
+ if (mode == Q_NONE)
+ {
+ (void)ft_memmove(word + (ptr - word), ptr + 1,
+ (ft_strlen(ptr + 1) + 1) * sizeof(char));
+ }
+ else if (mode == Q_DOUBLE)
+ {
+ if (*(ptr + 1) == C_BS)
+ {
+ (void)ft_memmove(word + (ptr - word), ptr + 1,
+ (ft_strlen(ptr + 1) + 1) * sizeof(char));
+ *(word + (ptr - word)) = 26;
+ }
+ else if (*(ptr + 1) == C_DQUOTE)
+ {
+ (void)ft_memmove(word + (ptr - word), ptr + 1,
+ (ft_strlen(ptr + 1) + 1) * sizeof(char));
+ }
+ }
+}
static void
- p_escape_arg(char *ptr)
+ p_escape_arg(char word[])
{
+ char *ptr;
t_quote_mode mode;
+ ptr = word;
mode = Q_NONE;
while (*ptr != C_NULL)
{
+ if (*ptr == C_SQUOTE)
+ mode = p_escape_squote(ptr, word, mode);
+ else if (*ptr == C_DQUOTE)
+ mode = p_escape_dquote(ptr, word, mode);
+ if (*ptr == C_BS)
+ p_escape_bs(ptr, word, mode);
+ ptr++;
+ }
+}
+
+static void
+ p_replace_bs(char *ptr)
+{
+ while (*ptr != C_NULL)
+ {
+ if (*ptr == 26)
+ *ptr = C_BS;
ptr++;
}
}
@@ -32,12 +119,14 @@ void
/* TODO: escape \ */
/* TODO: escape $# special vars */
/* TODO: escape my life */
+ /* TODO: comments */
char **ptr;
ptr = words;
while (*ptr != NULL)
{
p_escape_arg(*ptr);
+ p_replace_bs(*ptr);
ptr++;
}
}