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