aboutsummaryrefslogtreecommitdiff
path: root/src/preprocess.c
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2020-06-09 10:53:57 +0200
committerCharles <sircharlesaze@gmail.com>2020-06-09 10:53:57 +0200
commitc16cf5fcc4a421608bf3c291ef84a79b7dbe7591 (patch)
tree2c7b814f7e9eee40d34a6cf26b4948c39fecfc4c /src/preprocess.c
parent9dca7dc98e46d5b29e236f2970072ffaf582e13e (diff)
parent9fabc25a980550afc6337fd729632462f2680daa (diff)
downloadminishell-c16cf5fcc4a421608bf3c291ef84a79b7dbe7591.tar.gz
minishell-c16cf5fcc4a421608bf3c291ef84a79b7dbe7591.tar.bz2
minishell-c16cf5fcc4a421608bf3c291ef84a79b7dbe7591.zip
Merge branch 'eval'
Diffstat (limited to 'src/preprocess.c')
-rw-r--r--src/preprocess.c69
1 files changed, 69 insertions, 0 deletions
diff --git a/src/preprocess.c b/src/preprocess.c
new file mode 100644
index 0000000..c30bb70
--- /dev/null
+++ b/src/preprocess.c
@@ -0,0 +1,69 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* preprocess.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/04/03 08:58:49 by charles #+# #+# */
+/* Updated: 2020/04/05 15:04:06 by charles ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "minishell.h"
+#include "ms_glob.h"
+
+static char *iterpolate(char *str, t_env env)
+{
+ size_t i;
+ t_ftdstr *dstr;
+ char *match;
+
+ if ((dstr = ft_dstrnew(str)) == NULL)
+ return (NULL);
+ free(str);
+ i = 0;
+ while (i < dstr->length)
+ {
+ if (dstr->str[i] == '$')
+ {
+ if ((match = env_search_first_match(env, dstr->str + i + 1)) == NULL)
+ ft_dstrerase(dstr, i, utils_var_end(dstr->str + i + 1));
+ else
+ {
+ if (ft_dstrsubstitute(dstr, match, i, utils_var_end(dstr->str + i + 1)) == NULL)
+ return (NULL);
+ }
+ }
+ i++;
+ }
+ return (ft_dstrunwrap(dstr));
+}
+
+static char *preprocess_arg(char *arg, t_env env)
+{
+ if (*arg == '\'')
+ return (ft_strsubf(arg, 1, ft_strlen(arg) - 1));
+ if (*arg == '"')
+ {
+ if (ft_strchr(arg, '$') != NULL)
+ arg = iterpolate(arg, env);
+ return (ft_strsubf(arg, 1, ft_strlen(arg) - 1));
+ }
+ if (ft_strchr(arg, '$') != NULL)
+ return (iterpolate(arg, env));
+ if (ft_strchr(arg, '*') != NULL)
+ return (ms_glob(arg));
+ return (arg);
+}
+
+char **preprocess_argv(char **argv, t_env env)
+{
+ int i;
+
+ i = -1;
+ while (argv[++i] != NULL)
+ if ((argv[i] = preprocess_arg(argv[i], env)) == NULL)
+ return (NULL);
+ return (argv);
+}