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