aboutsummaryrefslogtreecommitdiffstats
path: root/src/s6/s6.c
blob: e7ecd6fa713da25e44a710083fe27baec6bf495e (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/* ISC license. */

#include <string.h>
#include <unistd.h>

#include <skalibs/uint64.h>
#include <skalibs/types.h>
#include <skalibs/buffer.h>
#include <skalibs/prog.h>
#include <skalibs/strerr.h>
#include <skalibs/gol.h>

#include <s6/config.h>
#include <s6-rc/config.h>

#include <s6-frontend/config.h>
#include "s6-internal.h"

#define USAGE "s6 [ generic options ] command [ command options ] command_arguments... Type \"s6 help\" for details."
#define dieusage() strerr_dieusage(100, USAGE)


int version (char const *const *argv)
{
  (void)argv ;
  if (!buffer_putsflush(buffer_1, "s6-frontend v" S6_FRONTEND_VERSION "\n"))
    strerr_diefu1sys(111, "write to stdout") ;
  return 0 ;
}

enum golb_e
{
  GOLB_HELP,
  GOLB_VERSION,
  GOLB_USER,
  GOLB_N
} ;

enum gola_e
{
  GOLA_SCANDIR,
  GOLA_LIVEDIR,
  GOLA_REPODIR,
  GOLA_BOOTDIR,
  GOLA_STMPDIR,
  GOLA_VERBOSITY,
  GOLA_COLOR,
  GOLA_N
} ;

static gol_bool const rgolb[GOLB_N] =
{
  { .so = 'h', .lo = "help", .clear = 0, .set = 1 << GOLB_HELP },
  { .so = 'V', .lo = "version", .clear = 0, .set = 1 << GOLB_VERSION },
  { .so = 0, .lo = "user", .clear = 0, .set = 1 << GOLB_USER }
} ;

static gol_arg const rgola[GOLA_N] =
{
  { .so = 's', .lo = "scandir", .i = GOLA_SCANDIR },
  { .so = 'l', .lo = "livedir", .i = GOLA_LIVEDIR },
  { .so = 'r', .lo = "repodir", .i = GOLA_REPODIR },
  { .so = 'b', .lo = "bootdir", .i = GOLA_BOOTDIR },
  { .so = 0,   .lo = "stmpdir", .i = GOLA_STMPDIR },
  { .so = 'v', .lo = "verbosity", .i = GOLA_VERBOSITY },
  { .so = 0,   .lo = "color", .i = GOLA_COLOR }
} ;

struct global_s *g ;

static struct command_s const commands[] =
{
  { .s = "help", .f = &help },
  { .s = "process", .f = &process },
  { .s = "service", .f = &service },
  { .s = "version", .f = &version },
} ;

int main (int argc, char const *const *argv, char const *const *envp)
{
  struct global_s globals_in_the_stack = GLOBAL_ZERO ;
  char const *wgola[GOLA_N] = { 0 } ;
  uint64_t wgolb = 0 ;
  unsigned int golc ;
  struct command_s *cmd ;
  PROG = "s6" ;
  g = &globals_in_the_stack ;

  golc = gol_main(argc, argv, rgolb, GOLB_N, rgola, GOLA_N, &wgolb, wgola) ;
  argc -= golc ; argv += golc ;

  if (wgola[GOLA_VERBOSITY] && !uint0_scan(wgola[GOLA_VERBOSITY], &g->verbosity))
    strerr_dief1x(100, "verbosity must be an unsigned integer") ;

  if (wgolb & 1 << GOLB_VERSION) version(argv) ;
  if (wgolb & 1 << GOLB_HELP) help(argv) ;
  if (wgolb & (1 << GOLB_VERSION | 1 << GOLB_HELP)) return 0 ;

  if (wgola[GOLA_SCANDIR]) g->dirs.scan = wgola[GOLA_SCANDIR] ;
  if (wgola[GOLA_LIVEDIR]) g->dirs.live = wgola[GOLA_LIVEDIR] ;
  if (wgola[GOLA_REPODIR]) g->dirs.repo = wgola[GOLA_REPODIR] ;
  if (wgola[GOLA_BOOTDIR]) g->dirs.boot = wgola[GOLA_BOOTDIR] ;
  if (wgola[GOLA_STMPDIR]) g->dirs.stmp = wgola[GOLA_STMPDIR] ;

  {
    int force_color = 0 ;
    g->istty = isatty(1) ;
    if (wgola[GOLA_COLOR])
    {
      if (!strcmp(wgola[GOLA_COLOR], "yes"))
      {
        force_color = 1 ;
        g->color = 1 ;
      }
      else if (!strcmp(wgola[GOLA_COLOR], "no"))
      {
        force_color = 1 ;
        g->color = 0 ;
      }
      else if (strcmp(wgola[GOLA_COLOR], "auto"))
        strerr_dief1x(100, "--color value must be yes, no, or auto") ;
    }
    if (!force_color) g->color = g->istty ;
  }

  g->isuser = !!(wgolb & 1 << GOLB_USER) ;
  if (g->isuser) s6f_user_get_confdirs(&g->dirs, &g->userstorage) ;

  if (!*argv) dieusage() ;
  cmd = BSEARCH(struct command_s, *argv, commands) ;
  if (!cmd) dieusage() ;
  return (*cmd->f)(++argv) ;
}