3 * Subcommand infrastructure
5 * (c) 2004 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
30 #define _FILE_OFFSET_BITS 64
32 #include <mLib/macros.h>
33 #include <mLib/quis.h>
34 #include <mLib/report.h>
38 /*----- Main code ---------------------------------------------------------*/
40 /* --- @findcmd@ --- *
42 * Arguments: @const cmd *cmds@ = pointer to command table
43 * @const char *name@ = a command name
45 * Returns: Pointer to the command structure.
47 * Use: Looks up a command by name. If the command isn't found, an
48 * error is reported and the program is terminated.
51 const cmd
*findcmd(const cmd
*cmds
, const char *name
)
53 const cmd
*c
, *chosen
= 0;
54 size_t sz
= strlen(name
);
56 for (c
= cmds
; c
->name
; c
++) {
57 if (STRNCMP(name
, ==, c
->name
, sz
)) {
58 if (c
->name
[sz
] == 0) {
62 die(EXIT_FAILURE
, "ambiguous command name `%s'", name
);
68 die(EXIT_FAILURE
, "unknown command name `%s'", name
);
72 /* --- @sc_help@ --- *
74 * Arguments: @const cmd *cmds@ = pointer to command table
75 * @FILE *fp@ = output file handle
76 * @char *const *argv@ = remaining arguments
80 * Use: Prints a help message, maybe with help about subcommands.
83 void sc_help(const cmd
*cmds
, FILE *fp
, char *const *argv
)
92 The following commands are understood:\n\n",
94 for (c
= cmds
; c
->name
; c
++)
95 fprintf(fp
, "%s\n", c
->usage
);
98 c
= findcmd(cmds
, *argv
);
99 fprintf(fp
, "Usage: %s [-OPTIONS] %s\n", QUIS
, c
->usage
);
105 if (*argv
) fputc('\n', fp
);
110 /*----- That's all, folks -------------------------------------------------*/