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.c150
1 files changed, 150 insertions, 0 deletions
diff --git a/src/p_args_escape.c b/src/p_args_escape.c
new file mode 100644
index 0000000..abdf3b3
--- /dev/null
+++ b/src/p_args_escape.c
@@ -0,0 +1,150 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* p_args_escape.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */
+/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */
+/* */
+/* ************************************************************************** */
+
+#include <libft.h>
+#include <stddef.h>
+
+#include "d_define.h"
+#include "u_utils.h"
+
+static t_quote_mode
+ p_escape_squote(char *ptr, char **p, char word[], t_quote_mode mode)
+{
+ if (mode == Q_NONE)
+ {
+ if (u_is_not_escaped(word, ptr) == TRUE)
+ {
+ (void)ft_memmove(word + (ptr - word), ptr + 1,
+ (ft_strlen(ptr + 1) + 1) * sizeof(char));
+ *(p) -= 1;
+ return (Q_SINGLE);
+ }
+ else
+ {
+ return (Q_NONE);
+ }
+ }
+ else if (mode == Q_SINGLE)
+ {
+ (void)ft_memmove(word + (ptr - word), ptr + 1,
+ (ft_strlen(ptr + 1) + 1) * sizeof(char));
+ *(p) -= 1;
+ return (Q_NONE);
+ }
+ return (mode);
+}
+
+static t_quote_mode
+ p_escape_dquote(char *ptr, char **p, char word[], t_quote_mode mode)
+{
+ if (mode == Q_NONE)
+ {
+ if (u_is_not_escaped(word, ptr) == TRUE)
+ {
+ (void)ft_memmove(word + (ptr - word), ptr + 1,
+ (ft_strlen(ptr + 1) + 1) * sizeof(char));
+ *(p) -= 1;
+ return (Q_DOUBLE);
+ }
+ else
+ {
+ return (Q_NONE);
+ }
+ }
+ else if (mode == Q_DOUBLE && u_is_not_escaped(word, ptr) == TRUE)
+ {
+ (void)ft_memmove(word + (ptr - word), ptr + 1,
+ (ft_strlen(ptr + 1) + 1) * sizeof(char));
+ *(p) -= 1;
+ 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_BACKS)
+ {
+ (void)ft_memmove(word + (ptr - word), ptr + 1,
+ (ft_strlen(ptr + 1) + 1) * sizeof(char));
+ }
+ 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 word[])
+{
+ char *ptr;
+ t_quote_mode mode;
+
+ ptr = word;
+ mode = Q_NONE;
+ while (*ptr != C_NUL)
+ {
+ if (*ptr == C_SQUOTE)
+ mode = p_escape_squote(ptr, &ptr, word, mode);
+ else if (*ptr == C_DQUOTE)
+ mode = p_escape_dquote(ptr, &ptr, word, mode);
+ else if (*ptr == C_BACKS)
+ p_escape_bs(ptr, word, mode);
+ ptr++;
+ }
+}
+
+static void
+ p_replace_bs(char *ptr)
+{
+ char *head;
+
+ head = ptr;
+ while (*ptr != C_NUL)
+ {
+ if (*ptr == C_SUB && *(ptr + 1) == C_SUB)
+ {
+ (void)ft_memmove(head + (ptr - head), ptr + 1,
+ (ft_strlen(ptr + 1) + 1) * sizeof(char));
+ *ptr = C_BACKS;
+ }
+ ptr++;
+ }
+}
+
+void
+ p_args_escape_chars_and_quotes(char *words[])
+{
+ /* TODO: escape \ */
+ /* TODO: escape $# special vars */
+ /* TODO: comments ################ */
+ /* TODO: escape my life */
+ char **ptr;
+
+ ptr = words;
+ while (*ptr != NULL)
+ {
+ p_escape_arg(*ptr);
+ p_replace_bs(*ptr);
+ ptr++;
+ }
+}