1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
/* ISC license. */
#include <sys/types.h>
#include <sys/wait.h>
#include <skalibs/sgetopt.h>
#include <skalibs/types.h>
#include <skalibs/strerr2.h>
#include <skalibs/djbunix.h>
#include <execline/execline.h>
#define USAGE "ifthenelse [ -X ] { command-if... } { command-then... } { command-else... } [ remainder... ]"
int main (int argc, char const **argv, char const *const *envp)
{
int argc1, argc2, argc3, wstat ;
int magicscope = 0, flagnormalcrash = 0 ;
pid_t pid ;
PROG = "ifthenelse" ;
{
subgetopt_t l = SUBGETOPT_ZERO ;
for (;;)
{
int opt = subgetopt_r(argc, argv, "Xs", &l) ;
if (opt == -1) break ;
switch (opt)
{
case 'X' : flagnormalcrash = 1 ; break ;
case 's' : magicscope = 1 ; break ;
default : strerr_dieusage(100, USAGE) ;
}
}
argc -= l.ind ; argv += l.ind ;
}
argc1 = el_semicolon(argv) ;
if (argc1 >= argc) strerr_dief1x(100, "unterminated if block") ;
if (argc1 + 1 == argc) strerr_dief1x(100, "then block required") ;
argc2 = el_semicolon(argv + argc1 + 1) ;
if (argc1 + argc2 + 1 >= argc) strerr_dief1x(100, "unterminated then block") ;
argc3 = el_semicolon(argv + argc1 + argc2 + 2) ;
if (argc1 + argc2 + argc3 + 2 >= argc)
strerr_dief1x(100, "unterminated else block") ;
argv[argc1] = 0 ;
pid = el_spawn0(argv[0], argv, envp) ;
if (!pid) strerr_diefu2sys(111, "spawn ", argv[0]) ;
if (wait_pid(pid, &wstat) == -1)
strerr_diefu2sys(111, "wait for ", argv[0]) ;
argv += argc1 + 1 ;
{
char const *const *remainder = argv + argc2 + argc3 + 2 ;
if (!flagnormalcrash && WIFSIGNALED(wstat))
{
char fmt[UINT_FMT] ;
fmt[uint_fmt(fmt, WTERMSIG(wstat))] = 0 ;
strerr_dief2x(128 + WTERMSIG(wstat), "child crashed with signal ", fmt) ;
}
if (wait_estatus(wstat))
{
argv += argc2 + 1 ;
argc2 = argc3 ;
}
if (magicscope) /* undocumented voodoo - dangerous and powerful */
{
unsigned int i = 0 ;
for (; remainder[i] ; i++) argv[argc2+i] = remainder[i] ;
argv[argc2+i] = 0 ;
xpathexec0_run(argv, envp) ;
}
else
{
argv[argc2] = 0 ;
el_execsequence(argv, remainder, envp) ;
}
}
}
|