5 * Subcommand infrastructure
7 * (c) 2004 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /*----- Header files ------------------------------------------------------*/
32 #include <mLib/quis.h>
33 #include <mLib/report.h>
37 /*----- Main code ---------------------------------------------------------*/
39 /* --- @findcmd@ --- *
41 * Arguments: @const cmd *cmds@ = pointer to command table
42 * @const char *name@ = a command name
44 * Returns: Pointer to the command structure.
46 * Use: Looks up a command by name. If the command isn't found, an
47 * error is reported and the program is terminated.
50 const cmd *findcmd(const cmd *cmds, const char *name)
52 const cmd *c, *chosen = 0;
53 size_t sz = strlen(name);
55 for (c = cmds; c->name; c++) {
56 if (strncmp(name, c->name, sz) == 0) {
57 if (c->name[sz] == 0) {
61 die(EXIT_FAILURE, "ambiguous command name `%s'", name);
67 die(EXIT_FAILURE, "unknown command name `%s'", name);
71 /* --- @sc_help@ --- *
73 * Arguments: @const cmd *cmds@ = pointer to command table
74 * @FILE *fp@ = output file handle
75 * @char *const *argv@ = remaining arguments
79 * Use: Prints a help message, maybe with help about subcommands.
82 void sc_help(const cmd *cmds, FILE *fp, char *const *argv)
91 The following commands are understood:\n\n",
93 for (c = cmds; c->name; c++)
94 fprintf(fp, "%s\n", c->usage);
97 c = findcmd(cmds, *argv);
98 fprintf(fp, "Usage: %s [-OPTIONS] %s\n", QUIS, c->usage);
104 if (*argv) fputc('\n', fp);
109 /*----- That's all, folks -------------------------------------------------*/