summaryrefslogtreecommitdiff
path: root/src/execline/envfile.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/execline/envfile.c')
-rw-r--r--src/execline/envfile.c50
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) ;
}