diff options
author | Laurent Bercot <ska-skaware@skarnet.org> | 2020-05-26 15:30:18 +0000 |
---|---|---|
committer | Laurent Bercot <ska-skaware@skarnet.org> | 2020-05-26 15:30:18 +0000 |
commit | a84e3fd640243d6b06a34019c6d0966cd72caad7 (patch) | |
tree | 0a2ebd684e1323c409c1efe709e18db1c39bf4fa /src | |
parent | 4f7270df65d7c79222ab29b8628f251eee16053b (diff) | |
download | execline-a84e3fd640243d6b06a34019c6d0966cd72caad7.tar.xz |
Add -I/-i options to envfile
Diffstat (limited to 'src')
-rw-r--r-- | src/execline/envfile.c | 50 |
1 files changed, 41 insertions, 9 deletions
diff --git a/src/execline/envfile.c b/src/execline/envfile.c index 18a175b..aff5964 100644 --- a/src/execline/envfile.c +++ b/src/execline/envfile.c @@ -2,17 +2,19 @@ #include <stdint.h> #include <string.h> +#include <errno.h> #include <skalibs/bytestr.h> #include <skalibs/types.h> #include <skalibs/buffer.h> #include <skalibs/strerr2.h> +#include <skalibs/sgetopt.h> #include <skalibs/fmtscan.h> #include <skalibs/env.h> #include <skalibs/stralloc.h> #include <skalibs/djbunix.h> -#define USAGE "envfile file prog..." +#define USAGE "envfile [ -i | -I ] file prog..." #define dieusage() strerr_dieusage(100, USAGE) #define dienomem() strerr_diefu1sys(111, "stralloc_catb") @@ -126,26 +128,56 @@ static inline void parse_config (char const *file, buffer *b, stralloc *sa) { char fmt[UINT_FMT] ; fmt[uint_fmt(fmt, line)] = 0 ; - strerr_dief4x(1, "syntax error line ", fmt, " while parsing ", file) ; + strerr_dief4x(1, "in ", file, ": syntax error line ", fmt) ; } } int main (int argc, char const *const *argv, char const *const *envp) { stralloc modif = STRALLOC_ZERO ; + int fd ; + char const *name ; + int strict = 1 ; PROG = "envfile" ; - if (argc < 3) dieusage() ; - if (!strcmp(argv[1], "-")) - parse_config("standard input", buffer_0, &modif) ; + { + subgetopt_t l = SUBGETOPT_ZERO ; + for (;;) + { + int opt = subgetopt_r(argc, argv, "iI", &l) ; + if (opt == -1) break ; + switch (opt) + { + case 'i' : strict = 1 ; break ; + case 'I' : strict = 0 ; break ; + default : dieusage() ; + } + } + argc -= l.ind ; argv += l.ind ; + } + + if (argc < 2) dieusage() ; + if (strcmp(argv[0], "-")) + { + fd = open_readb(argv[0]) ; + name = argv[0] ; + } + else + { + fd = 0 ; + name = "standard input" ; + } + if (fd == -1) + { + if (strict || errno != ENOENT) + strerr_diefu2sys(111, "open ", name) ; + } else { buffer b ; char buf[BUFFER_INSIZE] ; - int fd = open_readb(argv[1]) ; - if (fd == -1) strerr_diefu2sys(111, "open ", argv[1]) ; buffer_init(&b, &buffer_read, fd, buf, BUFFER_INSIZE) ; - parse_config(argv[1], &b, &modif) ; + parse_config(name, &b, &modif) ; fd_close(fd) ; } - xpathexec_r(argv + 2, envp, env_len(envp), modif.s, modif.len) ; + xpathexec_r(argv + 1, envp, env_len(envp), modif.s, modif.len) ; } |