diff options
author | Laurent Bercot <ska-skaware@skarnet.org> | 2020-12-30 23:06:31 +0000 |
---|---|---|
committer | Laurent Bercot <ska-skaware@skarnet.org> | 2020-12-30 23:06:31 +0000 |
commit | f021f61f10c566fcae2f77b1a4b095869076062b (patch) | |
tree | ce84d18f30a4ebf5f6dc3b7ea1b009ba1a06ffcf /src/libexecline | |
parent | b6b71f9942fb5f209e183c2ca997f33071c38c71 (diff) | |
download | execline-f021f61f10c566fcae2f77b1a4b095869076062b.tar.xz |
Add -E option for variable autoimport
Diffstat (limited to 'src/libexecline')
-rw-r--r-- | src/libexecline/deps-lib/execline | 2 | ||||
-rw-r--r-- | src/libexecline/el_modif_and_exec.c | 36 | ||||
-rw-r--r-- | src/libexecline/el_modif_and_spawn.c | 39 | ||||
-rw-r--r-- | src/libexecline/el_spawn0.c | 1 |
4 files changed, 78 insertions, 0 deletions
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 <string.h> + +#include <skalibs/env.h> +#include <skalibs/exec.h> + +#include <execline/config.h> +#include <execline/execline.h> + +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 <string.h> + +#include <skalibs/posixplz.h> +#include <skalibs/env.h> + +#include <execline/config.h> +#include <execline/execline.h> + +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 <skalibs/djbunix.h> + #include <execline/config.h> #include <execline/execline.h> |