chiark / gitweb /
Merge branch '2.4.x' into 2.5.x
[catacomb] / progs / cc-subcmd.c
1 /* -*-c-*-
2  *
3  * Subcommand infrastructure
4  *
5  * (c) 2004 Straylight/Edgeware
6  */
7
8 /*----- Licensing notice --------------------------------------------------*
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.
16  *
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.
21  *
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
30 #define _FILE_OFFSET_BITS 64
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 -------------------------------------------------*/