1 /* ISC license. */
2 
3 #include <string.h>
4 #include <skalibs/strerr2.h>
5 #include <execline/execline.h>
6 #include "exlsn.h"
7 
8 #define USAGE "see http://skarnet.org/software/execline/multisubstitute.html"
9 
10 static char const *const commands[] =
11 {
12   "define",
13   "importas",
14   "elglob",
15   "elgetpositionals",
16   "multidefine",
17   0
18 } ;
19 
20 static exlsnfunc_t_ref const functions[] =
21 {
22   &exlsn_define,
23   &exlsn_importas,
24   &exlsn_elglob,
25   &exlsn_exlp,
26   &exlsn_multidefine,
27   0
28 } ;
29 
main(int argc,char const ** argv,char const * const * envp)30 int main (int argc, char const **argv, char const *const *envp)
31 {
32   exlsn_t info = EXLSN_ZERO ;
33   int argc1 ;
34   PROG = "multisubstitute" ;
35   if (!--argc) strerr_dieusage(100, USAGE) ;
36 
37   argc1 = el_semicolon(++argv) ;
38   if (argc1 >= argc) strerr_dief1x(100, "unterminated block") ;
39   if (argc1 + 1 == argc) strerr_dieusage(100, USAGE) ;
40 
41   while (argc1)
42   {
43     int n ;
44     unsigned int i = 0 ;
45     for (; commands[i] ; i++) if (!strcmp(*argv, commands[i])) break ;
46     if (!commands[i]) strerr_dief3x(100, "syntax error: unrecognized", " directive ", *argv) ;
47     n = (*(functions[i]))(argc1, argv, envp, &info) ;
48     if (n < 0) switch (n)
49     {
50       case -3 : strerr_dief3x(100, "syntax error at", " directive ", commands[i]) ;
51       case -2 : strerr_dief3x(100, "wrong key for", " directive ", commands[i]) ;
52       case -1 : strerr_diefu3sys(111, "run", " directive ", commands[i]) ;
53       default : strerr_dief3x(111, "unknown error with", " directive ", commands[i]) ;
54     }
55     argv += n ; argc1 -= n ; argc -= n ;
56   }
57 
58   el_substandrun(argc-1, argv+1, envp, &info) ;
59 }
60