aboutsummaryrefslogtreecommitdiffstats
path: root/src/s6
diff options
context:
space:
mode:
Diffstat (limited to 'src/s6')
-rw-r--r--src/s6/deps-exe/s64
-rw-r--r--src/s6/help.c16
-rw-r--r--src/s6/s6-internal.h48
-rw-r--r--src/s6/s6.c86
-rw-r--r--src/s6/util.c15
-rw-r--r--src/s6/version.c14
6 files changed, 183 insertions, 0 deletions
diff --git a/src/s6/deps-exe/s6 b/src/s6/deps-exe/s6
new file mode 100644
index 0000000..92e3455
--- /dev/null
+++ b/src/s6/deps-exe/s6
@@ -0,0 +1,4 @@
+help.o
+util.o
+version.o
+-lskarnet
diff --git a/src/s6/help.c b/src/s6/help.c
new file mode 100644
index 0000000..f7de80f
--- /dev/null
+++ b/src/s6/help.c
@@ -0,0 +1,16 @@
+/* ISC license. */
+
+#include <skalibs/buffer.h>
+#include <skalibs/strerr.h>
+
+#include "s6-internal.h"
+
+#define HELP_MESSAGE "This is the main help message.\n"
+
+int help (char const *const *argv)
+{
+ (void)argv ;
+ if (!buffer_putsflush(buffer_1, HELP_MESSAGE))
+ strerr_diefu1sys(111, "write to stdout") ;
+ return 0 ;
+}
diff --git a/src/s6/s6-internal.h b/src/s6/s6-internal.h
new file mode 100644
index 0000000..216d666
--- /dev/null
+++ b/src/s6/s6-internal.h
@@ -0,0 +1,48 @@
+/* ISC license. */
+
+#ifndef S6_INTERNAL_H
+#define S6_INTERNAL_H
+
+#include <stdlib.h>
+
+#include <skalibs/functypes.h>
+#include <s6-frontend/config.h>
+
+
+ /* util */
+
+extern int keycmp (void const *, void const *) ;
+#define BSEARCH(type, key, array) bsearch(key, (array), sizeof(array)/sizeof(type), sizeof(type), &keycmp)
+
+
+ /* help */
+
+extern int help (char const *const *) ;
+
+
+ /* version */
+
+extern int version (char const *const *) ;
+
+
+ /* main */
+
+struct global_s
+{
+ unsigned int verbosity ;
+} ;
+#define GLOBAL_ZERO \
+{ \
+ .verbosity = 1, \
+}
+
+extern struct global_s *g ;
+
+struct command_s
+{
+ char const *s ;
+ main_func_ref f ;
+} ;
+#define COMMAND_ZERO { .s = 0, .f = 0 }
+
+#endif
diff --git a/src/s6/s6.c b/src/s6/s6.c
new file mode 100644
index 0000000..ea11423
--- /dev/null
+++ b/src/s6/s6.c
@@ -0,0 +1,86 @@
+/* ISC license. */
+
+#include <unistd.h>
+
+#include <skalibs/uint64.h>
+#include <skalibs/types.h>
+#include <skalibs/buffer.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 ] subcommand [ command options ] command_arguments... Type \"s6 help\" for details."
+#define dieusage() strerr_dieusage(100, USAGE)
+
+enum s6_gb_e
+{
+ S6_GB_HELP,
+ S6_GB_VERSION,
+ S6_GB_N
+} ;
+
+enum s6_ga_e
+{
+ S6_GA_SCANDIR,
+ S6_GA_LIVEDIR,
+ S6_GA_REPODIR,
+ S6_GA_VERBOSITY,
+ S6_GA_N
+} ;
+
+static gol_bool const s6_gb[S6_GB_N] =
+{
+ { .so = 'h', .lo = "help", .set = 1, .mask = 1 << S6_GB_HELP },
+ { .so = 'V', .lo = "version", .set = 1, .mask = 1 << S6_GB_VERSION }
+} ;
+
+static gol_arg const s6_ga[S6_GA_N] =
+{
+ { .so = 's', .lo = "scandir", .i = S6_GA_SCANDIR },
+ { .so = 'l', .lo = "livedir", .i = S6_GA_LIVEDIR },
+ { .so = 'r', .lo = "repodir", .i = S6_GA_REPODIR },
+ { .so = 'v', .lo = "verbosity", .i = S6_GA_VERBOSITY }
+} ;
+
+struct global_s *g ;
+
+static struct command_s const main_commands[] =
+{
+ { .s = "help", .f = &help },
+ { .s = "version", .f = &version },
+} ;
+
+int main (int argc, char const *const *argv, char const *const *envp)
+{
+ struct global_s global = GLOBAL_ZERO ;
+ char const *gola[S6_GA_N] = { 0 } ;
+ uint64_t golb = 0 ;
+ struct command_s *cmd ;
+ unsigned int golc ;
+ PROG = "s6" ;
+ g = &global ;
+
+ golc = gol_main(argc, argv, s6_gb, S6_GB_N, s6_ga, S6_GA_N, &golb, gola) ;
+ argc -= golc ; argv += golc ;
+
+ if (gola[S6_GA_VERBOSITY] && !uint0_scan(gola[S6_GA_VERBOSITY], &g->verbosity))
+ strerr_dief1x(100, "verbosity must be an unsigned integer") ;
+
+ if (golb & (1 << S6_GB_VERSION)) version(argv) ;
+ if (golb & (1 << S6_GB_HELP)) help(argv) ;
+ if (golb & ((1 << S6_GB_VERSION) | (1 << S6_GB_HELP))) return 0 ;
+
+ if (gola[S6_GA_SCANDIR]) strerr_warni("scandir is ", gola[S6_GA_SCANDIR]) ;
+ if (gola[S6_GA_LIVEDIR]) strerr_warni("livedir is ", gola[S6_GA_LIVEDIR]) ;
+ if (gola[S6_GA_REPODIR]) strerr_warni("repodir is ", gola[S6_GA_REPODIR]) ;
+
+ if (!*argv) dieusage() ;
+ cmd = BSEARCH(struct command_s, *argv, main_commands) ;
+ if (!cmd) dieusage() ;
+ return (*cmd->f)(argv+1) ;
+}
diff --git a/src/s6/util.c b/src/s6/util.c
new file mode 100644
index 0000000..699f976
--- /dev/null
+++ b/src/s6/util.c
@@ -0,0 +1,15 @@
+/* ISC license. */
+
+#include <string.h>
+
+#include "s6-internal.h"
+
+struct starts_with_a_string_key_s
+{
+ char const *s ;
+} ;
+
+int keycmp (void const *a, void const *b)
+{
+ return strcmp((char const *)a, ((struct starts_with_a_string_key_s const *)b)->s) ;
+}
diff --git a/src/s6/version.c b/src/s6/version.c
new file mode 100644
index 0000000..acb06ad
--- /dev/null
+++ b/src/s6/version.c
@@ -0,0 +1,14 @@
+/* ISC license. */
+
+#include <skalibs/buffer.h>
+#include <skalibs/strerr.h>
+
+#include "s6-internal.h"
+
+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 ;
+}