commit c8444142f7beed84d043687e0c6239b04bb9f522 from: Stefan Sperling date: Mon Oct 02 16:41:48 2023 UTC add no-command support to ctl commit - 8fff88de81a48b1dd32ae121542e09e3bc7de5bb commit + c8444142f7beed84d043687e0c6239b04bb9f522 blob - 700b931c36131a8b0513e60d55ab18b3a8da45d0 blob + f37126dafbe6151b22e25e7425563a4b45b42dcf --- ctl.c +++ ctl.c @@ -440,22 +440,22 @@ struct ctl ctl_nppp[] = { /* dhcpd */ struct ctl2 ctl_dhcp[] = { - { "enable", "enable DHCPd daemon", + { "enable", "enable DHCPd daemon", NULL, { DHCPD, "-c", REQTEMP, "-l", DHCPLEASES, NULL }, { NULL, }, - start_dhcpd, DB_X_ENABLE, T_HANDLER }, - { "disable", "disable DHCPd daemon", - { PKILL, table, "dhcpd", NULL }, { NULL, }, NULL, - DB_X_DISABLE, T_EXEC }, - { "edit", "edit,test and stage DHCPd config", - { "dhcp", NULL }, { DHCPD, "-nc", REQTEMP, NULL }, call_editor, 0, - T_HANDLER_FILL1 }, - { "config-test", "test staged DHCPd config", - { DHCPD, "-nc", REQTEMP, "-l", DHCPLEASES, NULL }, { NULL, }, + start_dhcpd, DB_X_ENABLE, NULL, 0, T_HANDLER }, + { "disable", "disable DHCPd daemon", NULL, + { PKILL, table, "dhcpd", NULL }, { NULL, }, NULL, DB_X_DISABLE, NULL, 0, T_EXEC }, - { "restart", "restart DHCPd daemon", + { "edit", "edit,test and stage DHCPd config", NULL, + { "dhcp", NULL }, { DHCPD, "-nc", REQTEMP, NULL }, + call_editor, 0, NULL, 0, T_HANDLER_FILL1 }, + { "config-test", "test staged DHCPd config", NULL, + { DHCPD, "-nc", REQTEMP, "-l", DHCPLEASES, NULL }, { NULL, }, + NULL, 0, NULL, 0, T_EXEC }, + { "restart", "restart DHCPd daemon", NULL, { DHCPD, "-c", REQTEMP, "-l", DHCPLEASES, NULL }, { NULL, }, - restart_dhcpd, DB_X_ENABLE, T_HANDLER }, - { NULL, NULL, { NULL }, { NULL }, NULL, 0, 0 } + restart_dhcpd, DB_X_ENABLE, NULL, 0, T_HANDLER }, + { NULL, NULL, NULL, { NULL }, { NULL }, NULL, 0, NULL, 0, 0 } }; /* snmpd, snmpctl */ @@ -648,6 +648,34 @@ flag_x(char *name, char *daemon, int dbflag, char *dat return; if (db_insert_flag_x(name, daemon, cli_rtable, dbflag, data) < 0) { printf("%% database insert failure ctl ctl\n"); + } +} + +static void +ctl_help(const char *daemon_name, struct ctl2 *c0) +{ + int z = 0; + struct ctl2 *c; + + printf("%% Arguments may be abbreviated\n\n"); + + c = c0; + while (c->name != NULL) { + if (strlen(c->name) > z) + z = strlen(c->name) + 3; /* strlen("no ") */ + c++; + } + + c = c0; + while (c->name != NULL) { + if (c->help == NULL) + continue; + printf(" %s %-*s %s\n", daemon_name, z, c->name, c->help); + if (c->noarg_help) { + printf(" no %s %-*s %s\n", daemon_name, z - 3, + c->name, c->noarg_help); + } + c++; } } @@ -671,25 +699,32 @@ ctlhandler(int argc, char **argv, char *modhvar) char **xtest_args = NULL; int rv = 0; int nargs; + int noarg = 0; memset(&daemons1, 0, sizeof(daemons1)); + if (NO_ARG(argv[0])) { + noarg = 1; + argv++; + argc--; + } /* loop daemon list to find table pointer */ - daemons = (struct daemons *) genget(hname, (char **)ctl_daemons, + daemons = (struct daemons *) genget(argv[0], (char **)ctl_daemons, sizeof(struct daemons)); if (daemons == NULL) { - daemons2 = (struct daemons2 *) genget(hname, + daemons2 = (struct daemons2 *) genget(argv[0], (char **)ctl_daemons2, sizeof(struct daemons2)); if (daemons2 == NULL || Ambiguous(daemons2)) { - printf("%% Internal error - Invalid argument %s\n", argv[1]); + printf("%% Internal error - Invalid argument %s\n", + argv[0]); return 0; } if (daemons2todaemons(&daemons1, daemons2)) goto done; daemons = &daemons1; } else if (Ambiguous(daemons)) { - printf("%% Internal error - Ambiguous argument %s\n", argv[1]); + printf("%% Internal error - Ambiguous argument %s\n", argv[0]); return 0; } @@ -720,25 +755,34 @@ ctlhandler(int argc, char **argv, char *modhvar) goto done; } } - if (argc < 2 || argv[1][0] == '?') { - gen_help((char **)daemons->table, "", "", sizeof(struct ctl)); + if (argc > 1 && strcmp(argv[1], "?") == 0) { + if (daemons1.name != NULL) + ctl_help(daemons2->name, daemons2->table); + else + gen_help((char **)daemons->table, "", "", sizeof(struct ctl)); goto done; } if (daemons1.name != NULL) { - x2 = (struct ctl2 *) genget(argv[1], + char *subcmd = argc > 1 ? argv[1] : ""; + x2 = (struct ctl2 *)genget(subcmd, (char **)daemons2->table, sizeof(struct ctl2)); if (x2 == NULL) { - printf("%% Invalid argument %s\n", argv[1]); + printf("%% Invalid argument %s\n", subcmd); goto done; } else if (Ambiguous(x2)) { - printf("%% Ambiguous argument %s\n", argv[1]); + printf("%% Ambiguous argument %s\n", subcmd); goto done; } xargs = x2->args; xtype = x2->type; - xhandler = x2->handler; - xflag_x = x2->flag_x; + if (noarg) { + xhandler = x2->noarg_handler; + xflag_x = x2->noarg_flag_x; + } else { + xhandler = x2->handler; + xflag_x = x2->flag_x; + } xtest_args = x2->test_args; } else { x = (struct ctl *) genget(argv[1], (char **)daemons->table, @@ -795,7 +839,7 @@ ctlhandler(int argc, char **argv, char *modhvar) case NOPTFILL: /* bump this when adding more cases */ default: printf("%% handler %s %s requires too many " - "arguments: %d\n", hname, argv[1], nargs); + "arguments: %d\n", argv[0], argv[1], nargs); break; } break; blob - 016abaae50e11fbd1d3585543f0a51c23e3fcfc2 blob + 1e2ba7495352a561d0f854108777530ad4546c2e --- ctl.h +++ ctl.h @@ -82,10 +82,13 @@ struct ctl { struct ctl2 { char *name; char *help; + char *noarg_help; char *args[32]; char *test_args[32]; void (*handler)(); int flag_x; + void (*noarg_handler)(); + int noarg_flag_x; int type; };