From f021f61f10c566fcae2f77b1a4b095869076062b Mon Sep 17 00:00:00 2001 From: Laurent Bercot Date: Wed, 30 Dec 2020 23:06:31 +0000 Subject: Add -E option for variable autoimport --- src/libexecline/deps-lib/execline | 2 ++ src/libexecline/el_modif_and_exec.c | 36 +++++++++++++++++++++++++++++++++ src/libexecline/el_modif_and_spawn.c | 39 ++++++++++++++++++++++++++++++++++++ src/libexecline/el_spawn0.c | 1 + 4 files changed, 78 insertions(+) create mode 100644 src/libexecline/el_modif_and_exec.c create mode 100644 src/libexecline/el_modif_and_spawn.c (limited to 'src/libexecline') diff --git a/src/libexecline/deps-lib/execline b/src/libexecline/deps-lib/execline index 96d4e18..e896f2f 100644 --- a/src/libexecline/deps-lib/execline +++ b/src/libexecline/deps-lib/execline @@ -1,5 +1,7 @@ el_execsequence.o el_getstrict.o +el_modif_and_exec.o +el_modif_and_spawn.o el_parse.o el_parse_from_buffer.o el_parse_from_string.o diff --git a/src/libexecline/el_modif_and_exec.c b/src/libexecline/el_modif_and_exec.c new file mode 100644 index 0000000..e2a3618 --- /dev/null +++ b/src/libexecline/el_modif_and_exec.c @@ -0,0 +1,36 @@ +/* ISC license. */ + +#include + +#include +#include + +#include +#include + +void el_modif_and_exec (char const *const *argv, char const *var, char const *value, int doimport) +{ + size_t varlen = strlen(var) ; + size_t modiflen = value ? varlen + strlen(value) + 2 : 1 ; + char modifs[modiflen] ; + if (value) + { + memcpy(modifs, var, varlen) ; + modifs[varlen] = '=' ; + memcpy(modifs + varlen + 1, value, modiflen - varlen - 1) ; + } + if (doimport) + { + size_t m = 0 ; + char const *newargv[env_len(argv) + 6] ; + newargv[m++] = EXECLINE_BINPREFIX "importas" ; + newargv[m++] = "-ui" ; + newargv[m++] = "--" ; + newargv[m++] = var ; + newargv[m++] = var ; + while (*argv) newargv[m++] = *argv++ ; + newargv[m++] = 0 ; + xmexec0_n(newargv, value ? modifs : var, value ? modiflen : varlen + 1, 1) ; + } + else xmexec0_n(argv, value ? modifs : var, value ? modiflen : varlen + 1, 1) ; +} diff --git a/src/libexecline/el_modif_and_spawn.c b/src/libexecline/el_modif_and_spawn.c new file mode 100644 index 0000000..0070052 --- /dev/null +++ b/src/libexecline/el_modif_and_spawn.c @@ -0,0 +1,39 @@ +/* ISC license. */ + +#include + +#include +#include + +#include +#include + +pid_t el_modif_and_spawn (char const *const *argv, char const *var, char const *value, int doimport) +{ + size_t varlen = strlen(var) ; + size_t modiflen = value ? varlen + strlen(value) + 2 : 1 ; + size_t envlen = env_len((char const *const *)environ) ; + char const *newenv[envlen + 2] ; + char modifs[modiflen] ; + if (value) + { + memcpy(modifs, var, varlen) ; + modifs[varlen] = '=' ; + memcpy(modifs + varlen + 1, value, modiflen - varlen - 1) ; + } + if (!env_mergen(newenv, envlen + 2, (char const *const *)environ, envlen, value ? modifs : var, value ? modiflen : varlen + 1, 1)) return 0 ; + if (doimport) + { + size_t m = 0 ; + char const *newargv[env_len(argv) + 6] ; + newargv[m++] = EXECLINE_BINPREFIX "importas" ; + newargv[m++] = "-ui" ; + newargv[m++] = "--" ; + newargv[m++] = var ; + newargv[m++] = var ; + while (*argv) newargv[m++] = *argv++ ; + newargv[m++] = 0 ; + return el_spawn0(newargv[0], newargv, newenv) ; + } + else return el_spawn0(argv[0], argv, newenv) ; +} diff --git a/src/libexecline/el_spawn0.c b/src/libexecline/el_spawn0.c index 7e10902..0cfe017 100644 --- a/src/libexecline/el_spawn0.c +++ b/src/libexecline/el_spawn0.c @@ -1,6 +1,7 @@ /* ISC license. */ #include + #include #include -- cgit v1.2.3