3 * $Id: sw.c,v 1.4 2004/04/08 01:52:19 mdw Exp $
5 * Main driver code for sw-tools
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of sw-tools.
14 * sw-tools is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * sw-tools 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 General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with sw-tools; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Header files ------------------------------------------------------*/
42 #include <mLib/alloc.h>
43 #include <mLib/dstr.h>
45 #include <mLib/mdwopt.h>
46 #include <mLib/quis.h>
47 #include <mLib/report.h>
58 /*----- Static variables --------------------------------------------------*/
60 static cmd
*cmds
= CMD_LINK
;
62 /*----- Global option variables -------------------------------------------*
64 * It'd be nicer if these could be parsed by the build commands, but alas
65 * this is not to be. Consider `sw configure --arch=i386-linux
66 * --with-diffutils': how is the program to know which options are for it and
67 * which are for the remote one? The `configure' command is important,
68 * because it does some hairy stuff under the covers (inserting magical
69 * options, and running `../configure'), so I can't just require `sw run
70 * configure'. Requiring the user to always put in a `--' to separate the
71 * two lots of options is horrific. So they get picked up at the main
72 * program and stuffed into global variables.
74 * On the other hand, there is the advantage that `--output' options can be
75 * put in an environment variable here.
78 const char *opt_output
= 0;
79 const char *opt_arch
= 0;
80 unsigned int opt_flags
= optFlag_percent
;
82 /*----- Helpful GNUy message routines -------------------------------------*/
84 /* --- @version@ --- */
86 static void version(FILE *fp
)
88 fprintf(fp
, "%s v. " VERSION
"\n", QUIS
);
93 static void usage(FILE *fp
)
95 fprintf(fp
, "Usage: %s [-fbip] [-a arch,...] [-o style] command [args]\n",
101 static void help(FILE *fp
, int full
)
108 Performs various handy jobs with multiple-architecture builds.\n\
110 There are some options which affect a few of the available commands:\n\
112 -a, --arch=ARCH,... Only build on the named architectures.\n\
113 -b, --beep Beep when the build is complete.\n\
114 -i, --install Mark architectures as done when build succeeds.\n\
115 -f, --force Run build commands on installed architectures.\n\
116 -p, --percent Enable `%'-escapes in build command arguments.\n\
117 -o, --output=STYLE Display output in a particular style. Use style\n\
118 `help' for a list.\n\
122 fputs("The various commands provided are:\n\n", fp
);
123 for (p
= cmds
; p
; p
= p
->next
) {
128 "`%s' is an [mdw] production, brought to you in association with the\n\
129 European Bioinformatics Institute.\n", QUIS
);
131 fputs("The various commands, in summary:\n\n", fp
);
132 for (p
= cmds
; p
; p
= p
->next
) {
133 size_t sz
= strcspn(p
->help
, "\n\t");
134 fwrite(p
->help
, 1, sz
, fp
);
138 "\nType `%s --help-full' for complete information. There's a lot of it!\n",
143 /*----- Main code ---------------------------------------------------------*/
147 * Arguments: @int argc@ = number of command line arguments
148 * @char *argv[]@ = array of command line strings
150 * Returns: Zero on success, nonzero on failure.
152 * Use: Main program. Parses some trivial arguments out, and
153 * dispatches control to one of the subprogram handlers.
156 int main(int argc
, char *argv
[])
164 /* --- Initialize the support library --- */
169 /* --- Parse command line flags --- */
172 static struct option opt
[] = {
174 /* --- Standard GNUy help options --- */
176 { "help", 0, 0, 'h' },
177 { "help-full", 0, 0, 'H' },
178 { "version", 0, 0, 'v' },
179 { "usage", 0, 0, 'u' },
181 /* --- Build options --- *
185 { "arch", OPTF_ARGREQ
, 0, 'a' },
186 { "force", 0, 0, 'f' },
187 { "install", 0, 0, 'i' },
188 { "output", OPTF_ARGREQ
, 0, 'o' },
189 { "beep", OPTF_NEGATE
, 0, 'b' },
190 { "percent", OPTF_NEGATE
, 0, 'p' },
191 { "escape", OPTF_NEGATE
, 0, 'p' },
193 /* --- Internal-use-only magical options --- *
195 * You get what you deserve if you use this.
198 { "me", OPTF_ARGREQ
, 0, '=' },
199 { "remote", OPTF_ARGREQ
, 0, '!' },
201 /* --- Termination marker --- */
205 int i
= mdwopt(argc
, argv
, "+hHvu a:b+p+fio:", opt
, 0, 0,
206 OPTF_ENVVAR
| OPTF_NEGATION
);
212 /* --- GNUy help --- */
227 /* --- Build options --- */
233 opt_flags
|= optFlag_force
;
236 opt_flags
|= optFlag_beep
;
238 case 'b' | OPTF_NEGATED
:
239 opt_flags
&= ~optFlag_beep
;
242 opt_flags
|= optFlag_percent
;
244 case 'p' | OPTF_NEGATED
:
245 opt_flags
&= ~optFlag_percent
;
248 opt_flags
|= optFlag_install
;
254 /* --- Magical options for internal use --- */
260 swrsh_remote(optarg
);
263 /* --- The user screwed up --- */
271 if (f
& f_bogus
|| argc
== optind
) {
276 /* --- Pick up the operation name --- */
284 const char *which
= argv
[0];
285 size_t sz
= strlen(which
);
287 for (p
= cmds
; p
; p
= p
->next
) {
288 if (strncmp(which
, p
->name
, sz
) == 0) {
289 if (p
->name
[sz
] == 0) {
293 die(1, "ambiguous command name `%s'", which
);
299 die(1, "unknown command name `%s'", which
);
300 signal(SIGPIPE
, SIG_IGN
);
302 return (chosen
->cmd(argc
, argv
));
303 CATCH
switch (exc_type
) {
305 die(1, "not enough memory");
313 /*----- That's all, folks -------------------------------------------------*/