+static const os_subcommand_t __help_cmd;
+static const os_subcommand_t *_help_cmd = &__help_cmd;
+
+static const os_subcommand_t __main_cmd;
+static const os_subcommand_t *_main_cmd = &__main_cmd;
+static const os_subcommand_t *_internal_main_cmd = &__main_cmd;
+
+static struct ttysize __ttys = {
+ .ts_lines = 24,
+ .ts_cols = CTL_OUTPUT_WIDTH,
+};
+
+static const struct ttysize *_ttys = &__ttys;
+
+#pragma mark Module Private
+static void
+_init_column_count(void)
+{
+ const char *columns_env = NULL;
+ char *end = NULL;
+ struct ttysize ttys = {
+ .ts_lines = 24,
+ .ts_cols = CTL_OUTPUT_WIDTH,
+ };
+ int ret = -1;
+
+ columns_env = getenv("COLUMNS");
+ if (columns_env) {
+ unsigned short cols = -1;
+
+ cols = strtoul(columns_env, &end, 0);
+ if (end != columns_env && end[0] != 0) {
+ ttys.ts_lines = cols;
+ }
+ } else {
+ ret = ioctl(0, TIOCGSIZE, &ttys);
+ if (ret) {
+ ttys.ts_lines = 24;
+ ttys.ts_cols = CTL_OUTPUT_WIDTH;
+ }
+ }
+
+ __ttys = ttys;
+}
+
+static void
+_stoupper(char *str)
+{
+ size_t i = 0;
+ size_t len = strlen(str);
+
+ for (i = 0; i < len; i++) {
+ char *cp = &str[i];
+ *cp = ___toupper(*cp);
+ }
+}
+
+#pragma mark Main Subcommand
+static int _main_invoke(const os_subcommand_t *osc,
+ int argc,
+ const char *argv[]);
+
+static const os_subcommand_option_t _main_positional[] = {
+ [0] = {
+ .osco_version = OS_SUBCOMMAND_OPTION_VERSION,
+ .osco_flags = 0,
+ .osco_option = NULL,
+ .osco_argument_usage = "subcommand",
+ .osco_argument_human = "The subcommand to invoke",
+ },
+ OS_SUBCOMMAND_OPTION_TERMINATOR,
+};
+
+static const os_subcommand_t __main_cmd = {
+ .osc_version = OS_SUBCOMMAND_VERSION,
+ .osc_flags = OS_SUBCOMMAND_FLAG_MAIN,
+ .osc_name = "_main",
+ .osc_desc = "main command",
+ .osc_optstring = NULL,
+ .osc_options = NULL,
+ .osc_required = NULL,
+ .osc_optional = NULL,
+ .osc_positional = _main_positional,
+ .osc_invoke = &_main_invoke,
+};
+
+static int
+_main_invoke(const os_subcommand_t *osc, int argc, const char *argv[])
+{
+ return 0;
+}
+
+#pragma mark Help Subcommand
+static int _help_invoke(const os_subcommand_t *osc,
+ int argc,
+ const char *argv[]);
+
+static const os_subcommand_option_t _help_positional[] = {
+ [0] = {
+ .osco_version = OS_SUBCOMMAND_OPTION_VERSION,
+ .osco_flags = 0,
+ .osco_option = NULL,
+ .osco_argument_usage = "SUBCOMMAND",
+ .osco_argument_human = "The subcommand to query for help",
+ },
+ OS_SUBCOMMAND_OPTION_TERMINATOR,
+};
+
+static const os_subcommand_t __help_cmd = {
+ .osc_version = OS_SUBCOMMAND_VERSION,
+ .osc_flags = 0,
+ .osc_name = "help",
+ .osc_desc = "prints helpful information",
+ .osc_optstring = NULL,
+ .osc_options = NULL,
+ .osc_required = NULL,
+ .osc_optional = NULL,
+ .osc_positional = _help_positional,
+ .osc_invoke = &_help_invoke,
+};
+
+static int
+_help_invoke(const os_subcommand_t *osc, int argc, const char *argv[])
+{
+ int xit = -1;
+ const char *cmdname = NULL;
+ const os_subcommand_t *target = NULL;
+ FILE *f = stdout;
+
+ if (argc > 1) {
+ cmdname = argv[1];
+ }
+
+ // Print usage information for the requested subcommand.
+ target = _os_subcommand_find(cmdname);
+ if (!target) {
+ // If it's a bogus subcommand, just print top-level usage.
+ fprintf(stderr, "unrecognized subcommand: %s\n", cmdname);
+ target = _main_cmd;
+ xit = EX_UNAVAILABLE;
+ } else {
+ xit = 0;
+ }
+
+ if (xit) {
+ f = stderr;
+ }
+
+ _os_subcommand_print_usage(target, f);
+
+ if (target == _main_cmd) {
+ _print_subcommand_list(_help_cmd, f);
+ }
+
+ return xit;
+}
+
+#pragma mark Utilities
+static void
+_print_header(FILE *f, const char *hdr, bool *already_done)
+{
+ if (already_done && *already_done) {
+ return;
+ }
+
+ crfprintf_np(f, "");
+ crfprintf_np(f, "%s:", hdr);
+ crfprintf_np(f, "");
+
+ if (already_done) {
+ *already_done = true;
+ }
+}