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 #define _FILE_OFFSET_BITS 64
34 #include <mLib/quis.h>
35 #include <mLib/report.h>
39 /*----- Main code ---------------------------------------------------------*/
41 /* --- @findcmd@ --- *
43 * Arguments: @const cmd *cmds@ = pointer to command table
44 * @const char *name@ = a command name
46 * Returns: Pointer to the command structure.
48 * Use: Looks up a command by name. If the command isn't found, an
49 * error is reported and the program is terminated.
52 const cmd *findcmd(const cmd *cmds, const char *name)
54 const cmd *c, *chosen = 0;
55 size_t sz = strlen(name);
57 for (c = cmds; c->name; c++) {
58 if (strncmp(name, c->name, sz) == 0) {
59 if (c->name[sz] == 0) {
63 die(EXIT_FAILURE, "ambiguous command name `%s'", name);
69 die(EXIT_FAILURE, "unknown command name `%s'", name);
73 /* --- @sc_help@ --- *
75 * Arguments: @const cmd *cmds@ = pointer to command table
76 * @FILE *fp@ = output file handle
77 * @char *const *argv@ = remaining arguments
81 * Use: Prints a help message, maybe with help about subcommands.
84 void sc_help(const cmd *cmds, FILE *fp, char *const *argv)
93 The following commands are understood:\n\n",
95 for (c = cmds; c->name; c++)
96 fprintf(fp, "%s\n", c->usage);
99 c = findcmd(cmds, *argv);
100 fprintf(fp, "Usage: %s [-OPTIONS] %s\n", QUIS, c->usage);
106 if (*argv) fputc('\n', fp);
111 /*----- That's all, folks -------------------------------------------------*/