1 | /* > cmdline.c |
---|
2 | * Wrapper for GNU getopt which deals with standard options |
---|
3 | * Copyright (C) 1998-2001 Olly Betts |
---|
4 | * |
---|
5 | * This program is free software; you can redistribute it and/or modify |
---|
6 | * it under the terms of the GNU General Public License as published by |
---|
7 | * the Free Software Foundation; either version 2 of the License, or |
---|
8 | * (at your option) any later version. |
---|
9 | * |
---|
10 | * This program is distributed in the hope that it will be useful, |
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
13 | * GNU General Public License for more details. |
---|
14 | * |
---|
15 | * You should have received a copy of the GNU General Public License |
---|
16 | * along with this program; if not, write to the Free Software |
---|
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
18 | */ |
---|
19 | |
---|
20 | #ifdef HAVE_CONFIG_H |
---|
21 | #include <config.h> |
---|
22 | #endif |
---|
23 | |
---|
24 | #include <ctype.h> |
---|
25 | #include <errno.h> |
---|
26 | #include <float.h> |
---|
27 | #include <stdio.h> |
---|
28 | #include <string.h> |
---|
29 | #include <limits.h> |
---|
30 | |
---|
31 | #include "cmdline.h" |
---|
32 | #include "filename.h" |
---|
33 | |
---|
34 | /* FIXME: */ |
---|
35 | #if 0 |
---|
36 | #include "message.h" |
---|
37 | #define SYNTAX msg(/*Syntax*/49) |
---|
38 | #else |
---|
39 | #define SYNTAX "syntax" |
---|
40 | #endif |
---|
41 | |
---|
42 | /* It might be useful to be able to disable all long options on small |
---|
43 | * platforms like pre-386 DOS and perhaps some PDAs... |
---|
44 | */ |
---|
45 | #if 0 |
---|
46 | # define getopt_long(ARGC, ARGV, STR, OPTS, PTR) getopt(ARGC, ARGV, STR) |
---|
47 | #endif |
---|
48 | |
---|
49 | /* |
---|
50 | * bad command line give: |
---|
51 | * <problem> |
---|
52 | * |
---|
53 | * <short syntax> |
---|
54 | * |
---|
55 | * --help gives: |
---|
56 | * <version> |
---|
57 | * |
---|
58 | * <short syntax> |
---|
59 | * |
---|
60 | * <table> |
---|
61 | * |
---|
62 | * <blurb> |
---|
63 | * |
---|
64 | * --version gives: |
---|
65 | * <version> |
---|
66 | */ |
---|
67 | |
---|
68 | /* |
---|
69 | * want to cope with optional/required parameters on long options |
---|
70 | * and also parameters on short options |
---|
71 | */ |
---|
72 | |
---|
73 | static const char newline_tabs[] = "\n\t\t\t\t"; |
---|
74 | |
---|
75 | static int argc; |
---|
76 | static char * const *argv; |
---|
77 | static const char *shortopts; |
---|
78 | static const struct option *longopts; |
---|
79 | static int *longind; |
---|
80 | static const struct help_msg *help; |
---|
81 | static int min_args, max_args; |
---|
82 | static const char *args_msg = NULL, *extra_msg = NULL; |
---|
83 | |
---|
84 | static const char *argv0 = NULL; |
---|
85 | |
---|
86 | void |
---|
87 | cmdline_help(void) |
---|
88 | { |
---|
89 | while (help->opt) { |
---|
90 | const char *longopt = 0; |
---|
91 | int opt = help->opt; |
---|
92 | const struct option *o = 0; |
---|
93 | |
---|
94 | if (HLP_ISLONG(opt)) { |
---|
95 | o = longopts + HLP_DECODELONG(opt); |
---|
96 | longopt = o->name; |
---|
97 | opt = o->val; |
---|
98 | } |
---|
99 | |
---|
100 | if (isalnum(opt)) |
---|
101 | printf(" -%c%c", opt, longopt ? ',' : ' '); |
---|
102 | else |
---|
103 | fputs(" ", stdout); |
---|
104 | |
---|
105 | if (longopt) { |
---|
106 | int len = strlen(longopt); |
---|
107 | printf(" --%s", longopt); |
---|
108 | if (o && o->has_arg) { |
---|
109 | const char *p; |
---|
110 | len += len + 1; |
---|
111 | |
---|
112 | if (o->has_arg == optional_argument) { |
---|
113 | putchar('['); |
---|
114 | len += 2; |
---|
115 | } |
---|
116 | |
---|
117 | putchar('='); |
---|
118 | |
---|
119 | for (p = longopt; *p ; p++) putchar(toupper(*p)); |
---|
120 | |
---|
121 | if (o->has_arg == optional_argument) putchar(']'); |
---|
122 | } |
---|
123 | len = (len >> 3) + 2; |
---|
124 | if (len > 4) len = 0; |
---|
125 | fputs(newline_tabs + len, stdout); |
---|
126 | } else { |
---|
127 | fputs(newline_tabs + 1, stdout); |
---|
128 | } |
---|
129 | |
---|
130 | puts(help->msg); |
---|
131 | help++; |
---|
132 | } |
---|
133 | /* TRANSLATE */ |
---|
134 | puts(" --help\t\t\tdisplay this help and exit\n" |
---|
135 | " --version\t\t\toutput version information and exit"); |
---|
136 | |
---|
137 | if (extra_msg) { |
---|
138 | putnl(); |
---|
139 | puts(extra_msg); |
---|
140 | } |
---|
141 | |
---|
142 | exit(0); |
---|
143 | } |
---|
144 | |
---|
145 | void |
---|
146 | cmdline_version(void) |
---|
147 | { |
---|
148 | printf("%s - "PACKAGE" "VERSION"\n", argv0); |
---|
149 | } |
---|
150 | |
---|
151 | void |
---|
152 | cmdline_syntax(void) |
---|
153 | { |
---|
154 | printf("\n%s: %s", SYNTAX, argv0); |
---|
155 | if (help->opt) fputs(" [OPTION]...", stdout); |
---|
156 | if (args_msg) { |
---|
157 | putchar(' '); |
---|
158 | puts(args_msg); |
---|
159 | return; |
---|
160 | } |
---|
161 | if (min_args) { |
---|
162 | int i = min_args; |
---|
163 | while (i--) fputs(" FILE", stdout); |
---|
164 | } |
---|
165 | if (max_args == -1) { |
---|
166 | if (!min_args) fputs(" [FILE]", stdout); |
---|
167 | fputs("...", stdout); |
---|
168 | } else if (max_args > min_args) { |
---|
169 | int i = max_args - min_args; |
---|
170 | while (i--) fputs(" [FILE]", stdout); |
---|
171 | } |
---|
172 | putnl(); |
---|
173 | } |
---|
174 | |
---|
175 | void |
---|
176 | cmdline_set_syntax_message(const char *args, const char *extra) |
---|
177 | { |
---|
178 | args_msg = args; |
---|
179 | extra_msg = extra; |
---|
180 | } |
---|
181 | |
---|
182 | int |
---|
183 | cmdline_int_arg(void) |
---|
184 | { |
---|
185 | long result; |
---|
186 | char *endptr; |
---|
187 | |
---|
188 | errno = 0; |
---|
189 | |
---|
190 | result = strtol(optarg, &endptr, 10); |
---|
191 | |
---|
192 | if (errno == ERANGE || result > INT_MAX || result < INT_MIN) { |
---|
193 | fprintf(stderr, "%s: numeric argument `%s' out of range\n", |
---|
194 | argv0, optarg); |
---|
195 | cmdline_syntax(); |
---|
196 | exit(1); |
---|
197 | } else if (*optarg == '\0' || *endptr != '\0') { |
---|
198 | fprintf(stderr, "%s: argument `%s' not an integer\n", |
---|
199 | argv0, optarg); |
---|
200 | cmdline_syntax(); |
---|
201 | exit(1); |
---|
202 | } |
---|
203 | |
---|
204 | return (int)result; |
---|
205 | } |
---|
206 | |
---|
207 | double |
---|
208 | cmdline_double_arg(void) |
---|
209 | { |
---|
210 | double result; |
---|
211 | char *endptr; |
---|
212 | |
---|
213 | errno = 0; |
---|
214 | |
---|
215 | result = strtod(optarg, &endptr); |
---|
216 | |
---|
217 | if (errno == ERANGE) { |
---|
218 | fprintf(stderr, "%s: numeric argument `%s' out of range\n", |
---|
219 | argv0, optarg); |
---|
220 | cmdline_syntax(); |
---|
221 | exit(1); |
---|
222 | } else if (*optarg == '\0' || *endptr != '\0') { |
---|
223 | fprintf(stderr, "%s: argument `%s' not a number\n", argv0, optarg); |
---|
224 | cmdline_syntax(); |
---|
225 | exit(1); |
---|
226 | } |
---|
227 | |
---|
228 | return result; |
---|
229 | } |
---|
230 | |
---|
231 | float |
---|
232 | cmdline_float_arg(void) |
---|
233 | { |
---|
234 | double result = cmdline_double_arg(); |
---|
235 | if (fabs(result) > FLT_MAX) { |
---|
236 | fprintf(stderr, "%s: numeric argument `%s' out of range\n", |
---|
237 | argv0, optarg); |
---|
238 | cmdline_syntax(); |
---|
239 | exit(1); |
---|
240 | } |
---|
241 | return (float)result; |
---|
242 | } |
---|
243 | |
---|
244 | void |
---|
245 | cmdline_init(int argc_, char *const *argv_, const char *shortopts_, |
---|
246 | const struct option *longopts_, int *longind_, |
---|
247 | const struct help_msg *help_, |
---|
248 | int min_args_, int max_args_) |
---|
249 | { |
---|
250 | if (!argv0) argv0 = argv_[0]; |
---|
251 | |
---|
252 | argc = argc_; |
---|
253 | argv = argv_; |
---|
254 | shortopts = shortopts_; |
---|
255 | longopts = longopts_; |
---|
256 | longind = longind_; |
---|
257 | help = help_; |
---|
258 | min_args = min_args_; |
---|
259 | max_args = max_args_; |
---|
260 | } |
---|
261 | |
---|
262 | int |
---|
263 | cmdline_getopt(void) |
---|
264 | { |
---|
265 | int opt = getopt_long(argc, argv, shortopts, longopts, longind); |
---|
266 | |
---|
267 | if (opt == EOF) { |
---|
268 | /* check minimum # of args given - if not give syntax message */ |
---|
269 | if (argc - optind < min_args) { |
---|
270 | /* TRANSLATE */ |
---|
271 | fprintf(stderr, "%s: too few arguments\n", argv0); |
---|
272 | opt = '?'; |
---|
273 | } else if (max_args >= 0 && argc - optind > max_args) { |
---|
274 | /* TRANSLATE */ |
---|
275 | fprintf(stderr, "%s: too many arguments\n", argv0); |
---|
276 | opt = '?'; |
---|
277 | } |
---|
278 | } |
---|
279 | |
---|
280 | switch (opt) { |
---|
281 | case ':': /* parameter missing */ |
---|
282 | case '?': /* unknown opt, ambiguous match, or extraneous param */ |
---|
283 | /* getopt displays a message for us (unless we set opterr to 0) */ |
---|
284 | /* FIXME: set opterr to 0 so we can translate messages? */ |
---|
285 | cmdline_syntax(); |
---|
286 | /* TRANSLATE */ |
---|
287 | fprintf(stderr, "Try `%s --help' for more information.\n", argv0); |
---|
288 | exit(1); |
---|
289 | case HLP_VERSION: /* --version */ |
---|
290 | cmdline_version(); |
---|
291 | exit(0); |
---|
292 | case HLP_HELP: /* --help */ |
---|
293 | cmdline_version(); |
---|
294 | cmdline_syntax(); |
---|
295 | putchar('\n'); |
---|
296 | cmdline_help(); |
---|
297 | exit(0); |
---|
298 | } |
---|
299 | return opt; |
---|
300 | } |
---|