diff options
| author | Laurent Bercot <ska-skaware@skarnet.org> | 2025-03-26 04:27:39 +0000 |
|---|---|---|
| committer | Laurent Bercot <ska@appnovation.com> | 2025-03-26 04:27:39 +0000 |
| commit | ea714e771e8964114285915acacf80707cc17bdd (patch) | |
| tree | 05e036cd629693de1667225cd8df4bf26aa5db10 /src | |
| parent | 7390d0eacf18f70a3ac4f6f3351cadc12656234e (diff) | |
| download | execline-ea714e771e8964114285915acacf80707cc17bdd.tar.gz | |
Add export-array; update deps
Signed-off-by: Laurent Bercot <ska@appnovation.com>
Diffstat (limited to 'src')
| -rw-r--r-- | src/execline/deps-exe/export-array | 2 | ||||
| -rw-r--r-- | src/execline/export-array.c | 55 |
2 files changed, 57 insertions, 0 deletions
diff --git a/src/execline/deps-exe/export-array b/src/execline/deps-exe/export-array new file mode 100644 index 0000000..97021b5 --- /dev/null +++ b/src/execline/deps-exe/export-array @@ -0,0 +1,2 @@ +${LIBEXECLINE} +-lskarnet diff --git a/src/execline/export-array.c b/src/execline/export-array.c new file mode 100644 index 0000000..d883c2d --- /dev/null +++ b/src/execline/export-array.c @@ -0,0 +1,55 @@ +/* ISC license. */ + +#include <string.h> + +#include <skalibs/sgetopt.h> +#include <skalibs/strerr.h> +#include <skalibs/stralloc.h> +#include <skalibs/netstring.h> +#include <skalibs/exec.h> + +#include <execline/execline.h> + +#define USAGE "export-array [ -d delim ] variable { value-list } prog..." +#define dieusage() strerr_dieusage(100, USAGE) +#define dienomem() strerr_diefu1sys(111, "stralloc_catb") + +int main (int argc, char const **argv) +{ + stralloc sa = STRALLOC_ZERO ; + char const *var ; + int argc1 ; + char delim = 0 ; + PROG = "export-array" ; + { + subgetopt l = SUBGETOPT_ZERO ; + for (;;) + { + int opt = subgetopt_r(argc, argv, "d", &l) ; + if (opt == -1) break ; + switch (opt) + { + case 'd' : delim = *l.arg ; break ; + default : dieusage() ; + } + } + argc -= l.ind ; argv += l.ind ; + } + if (argc < 2) dieusage() ; + var = *argv++ ; argc-- ; + if (strchr(var, '=')) strerr_dief2x(100, "invalid variable name: ", var) ; + + argc1 = el_semicolon(argv) ; + if (!argc1) strerr_dief1x(100, "empty block") ; + if (argc1 >= argc) strerr_dief1x(100, "unterminated block") ; + if (argc1 + 1 == argc) dieusage() ; + + if (!stralloc_cats(&sa, var) || !stralloc_catb(&sa, "=", 1)) dienomem() ; + for (; argc1 ; argv++, argc1--) + { + if (delim ? !stralloc_cats(&sa, *argv) || !stralloc_catb(&sa, &delim, 1) : !netstring_appends(&sa, *argv)) dienomem() ; + } + if (!stralloc_0(&sa)) dienomem() ; + + xmexec_n(argv + 1, sa.s, sa.len, 1) ; +} |
