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/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 -------------------------------------------------*/