Deploy the new <ctype.h> and `foocmp' macros from mLib.
[catacomb] / progs / cc-subcmd.c
CommitLineData
c65df279 1/* -*-c-*-
2 *
c65df279 3 * Subcommand infrastructure
4 *
5 * (c) 2004 Straylight/Edgeware
6 */
7
45c0fd36 8/*----- Licensing notice --------------------------------------------------*
c65df279 9 *
10 * This file is part of Catacomb.
11 *
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.
45c0fd36 16 *
c65df279 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.
45c0fd36 21 *
c65df279 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,
25 * MA 02111-1307, USA.
26 */
27
28/*----- Header files ------------------------------------------------------*/
29
cd6eca43
MW
30#define _FILE_OFFSET_BITS 64
31
141c1284 32#include <mLib/macros.h>
c65df279 33#include <mLib/quis.h>
34#include <mLib/report.h>
35
36#include "cc.h"
37
38/*----- Main code ---------------------------------------------------------*/
39
40/* --- @findcmd@ --- *
41 *
42 * Arguments: @const cmd *cmds@ = pointer to command table
43 * @const char *name@ = a command name
44 *
45 * Returns: Pointer to the command structure.
46 *
47 * Use: Looks up a command by name. If the command isn't found, an
48 * error is reported and the program is terminated.
49 */
50
51const cmd *findcmd(const cmd *cmds, const char *name)
52{
53 const cmd *c, *chosen = 0;
54 size_t sz = strlen(name);
55
56 for (c = cmds; c->name; c++) {
141c1284 57 if (STRNCMP(name, ==, c->name, sz)) {
c65df279 58 if (c->name[sz] == 0) {
59 chosen = c;
60 break;
61 } else if (chosen)
62 die(EXIT_FAILURE, "ambiguous command name `%s'", name);
63 else
64 chosen = c;
65 }
66 }
67 if (!chosen)
68 die(EXIT_FAILURE, "unknown command name `%s'", name);
69 return (chosen);
70}
71
72/* --- @sc_help@ --- *
73 *
74 * Arguments: @const cmd *cmds@ = pointer to command table
75 * @FILE *fp@ = output file handle
76 * @char *const *argv@ = remaining arguments
77 *
78 * Returns: ---
79 *
80 * Use: Prints a help message, maybe with help about subcommands.
81 */
82
83void sc_help(const cmd *cmds, FILE *fp, char *const *argv)
84{
85 const cmd *c;
86
87 version(fp);
88 fputc('\n', fp);
89 if (!*argv) {
90 help_global(fp);
91 fputs("\n\
92The following commands are understood:\n\n",
93 fp);
94 for (c = cmds; c->name; c++)
95 fprintf(fp, "%s\n", c->usage);
96 } else {
97 while (*argv) {
98 c = findcmd(cmds, *argv);
99 fprintf(fp, "Usage: %s [-OPTIONS] %s\n", QUIS, c->usage);
100 if (c->help) {
45c0fd36 101 fputc('\n', fp);
c65df279 102 pquis(fp, c->help);
103 }
104 argv++;
105 if (*argv) fputc('\n', fp);
106 }
107 }
108}
109
110/*----- That's all, folks -------------------------------------------------*/