summaryrefslogtreecommitdiff
path: root/src/execline/backtick.c
blob: e4a543b59dfee38e5665ea801fb3a577dba5904c (plain)
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/* ISC license. */

#include <string.h>
#include <sys/wait.h>
#include <unistd.h>

#include <skalibs/types.h>
#include <skalibs/sgetopt.h>
#include <skalibs/strerr.h>
#include <skalibs/stralloc.h>
#include <skalibs/cspawn.h>
#include <skalibs/djbunix.h>
#include <skalibs/exec.h>

#include <execline/execline.h>

#define USAGE "backtick [ -i | -I | -x | -D default ] [ -N | -n ] [ -E | -e ] var { prog... } remainder..."
#define dieusage() strerr_dieusage(100, USAGE)

int main (int argc, char const **argv, char const *const *envp)
{
  subgetopt localopt = SUBGETOPT_ZERO ;
  int argc1, fdwstat ;
  stralloc value = STRALLOC_ZERO ;
  char val1[UINT_FMT] ;
  char const *var[2] = { [1] = "?" } ;
  char const *val[2] = { [1] = val1 } ;
  int insist = 2, chomp = 1, doimport = 0, allgood = 0 ;
  char const *def = 0 ;
  PROG = "backtick" ;
  for (;;)
  {
    int opt = subgetopt_r(argc, argv, "iINnxD:Ee", &localopt) ;
    if (opt < 0) break ;
    switch (opt)
    {
      case 'i' : insist = 2 ; break ;
      case 'I' : insist = 0 ; break ;
      case 'N' : chomp = 0 ; break ;
      case 'n' : chomp = 1 ; break ;
      case 'x' : insist = 1 ; def = 0 ; break ;
      case 'D' : insist = 1 ; def = localopt.arg ; break ;
      case 'E' : doimport = 1 ; break ;
      case 'e' : doimport = 0 ; break ;
      default : dieusage() ;
    }
  }
  argc -= localopt.ind ; argv += localopt.ind ;

  if (argc < 2) dieusage() ;
  if (!argv[0][0] || strchr(argv[0], '=')) strerr_dief1x(100, "invalid variable name") ;
  argc-- ; var[0] = *argv++ ;
  argc1 = el_semicolon(argv) ;
  if (!argc1) strerr_dief1x(100, "empty block") ;
  if (argc1 >= argc) strerr_dief1x(100, "unterminated block") ;
  argv[argc1] = 0 ;

  {
    pid_t pid = child_spawn1_pipe(argv[0], argv, envp, &fdwstat, 1) ;
    if (!pid) strerr_diefu2sys(111, "spawn ", argv[0]) ;
    if (!slurp(&value, fdwstat) || !stralloc_0(&value))
      strerr_diefu1sys(111, "slurp") ;
    close(fdwstat) ;
    if (wait_pid(pid, &fdwstat) < 0) strerr_diefu1sys(111, "wait_pid") ;
  }

  val[0] = value.s ;
  if (wait_status(fdwstat))
  {
    if (insist >= 2)
      strerr_dief1x(wait_estatus(fdwstat), WIFSIGNALED(fdwstat) ? "child process crashed" : "child process exited non-zero") ;
    else if (insist) val[0] = def ;
  }
  else if (strlen(value.s) < value.len - 1)
  {
    if (insist >= 2)
      strerr_dief1x(124, "child process output contained a null character") ;
    else if (insist)
    {
      val[0] = def ;
      strerr_warnw1x("child process output contained a null character") ;
    }
    else value.len = strlen(value.s) + 1 ;
  }
  else allgood = 1 ;
  if ((!insist || allgood) && chomp && (value.s[value.len - 2] == '\n'))
    value.s[--value.len - 1] = 0 ;
  val1[uint_fmt(val1, wait_status(fdwstat))] = 0 ; 
  el_modifs_and_exec(argv + argc1 + 1, var, val, insist < 2 ? 2 : 1, doimport) ;
}