[6974a34] | 1 | /* cmdline.c |
---|
[aadc86e] | 2 | * Wrapper for GNU getopt which deals with standard options |
---|
[bd1913f] | 3 | * Copyright (C) 1998-2001 Olly Betts |
---|
[846746e] | 4 | * |
---|
[89231c4] | 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. |
---|
[846746e] | 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 |
---|
[89231c4] | 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 13 | * GNU General Public License for more details. |
---|
[846746e] | 14 | * |
---|
[89231c4] | 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 |
---|
[aadc86e] | 18 | */ |
---|
| 19 | |
---|
| 20 | #ifdef HAVE_CONFIG_H |
---|
| 21 | #include <config.h> |
---|
| 22 | #endif |
---|
| 23 | |
---|
| 24 | #include <ctype.h> |
---|
[3b9ec3f] | 25 | #include <errno.h> |
---|
[dd6f74e] | 26 | #include <float.h> |
---|
[aadc86e] | 27 | #include <stdio.h> |
---|
[e05bdb8] | 28 | #include <string.h> |
---|
[f2122f0] | 29 | #include <limits.h> |
---|
[aadc86e] | 30 | |
---|
| 31 | #include "cmdline.h" |
---|
[e05bdb8] | 32 | #include "filename.h" |
---|
[aadc86e] | 33 | |
---|
[d227ee5] | 34 | #include "message.h" |
---|
| 35 | |
---|
[aadc86e] | 36 | /* It might be useful to be able to disable all long options on small |
---|
[3b9ec3f] | 37 | * platforms like pre-386 DOS and perhaps some PDAs... |
---|
[aadc86e] | 38 | */ |
---|
| 39 | #if 0 |
---|
| 40 | # define getopt_long(ARGC, ARGV, STR, OPTS, PTR) getopt(ARGC, ARGV, STR) |
---|
| 41 | #endif |
---|
| 42 | |
---|
| 43 | /* |
---|
| 44 | * bad command line give: |
---|
| 45 | * <problem> |
---|
[cb3d1e2] | 46 | * |
---|
[e05bdb8] | 47 | * <short syntax> |
---|
[cb3d1e2] | 48 | * |
---|
[aadc86e] | 49 | * --help gives: |
---|
| 50 | * <version> |
---|
[cb3d1e2] | 51 | * |
---|
[aadc86e] | 52 | * <short syntax> |
---|
| 53 | * |
---|
| 54 | * <table> |
---|
[cb3d1e2] | 55 | * |
---|
[aadc86e] | 56 | * <blurb> |
---|
| 57 | * |
---|
| 58 | * --version gives: |
---|
| 59 | * <version> |
---|
| 60 | */ |
---|
| 61 | |
---|
| 62 | /* |
---|
| 63 | * want to cope with optional/required parameters on long options |
---|
| 64 | * and also parameters on short options |
---|
[3b9ec3f] | 65 | */ |
---|
[aadc86e] | 66 | |
---|
| 67 | static const char newline_tabs[] = "\n\t\t\t\t"; |
---|
| 68 | |
---|
[3b9ec3f] | 69 | static int argc; |
---|
[647407d] | 70 | static char * const *argv; |
---|
[3b9ec3f] | 71 | static const char *shortopts; |
---|
| 72 | static const struct option *longopts; |
---|
| 73 | static int *longind; |
---|
| 74 | static const struct help_msg *help; |
---|
| 75 | static int min_args, max_args; |
---|
[acc20b1] | 76 | static const char *args_msg = NULL, *extra_msg = NULL; |
---|
[3b9ec3f] | 77 | |
---|
| 78 | void |
---|
| 79 | cmdline_help(void) |
---|
[aadc86e] | 80 | { |
---|
| 81 | while (help->opt) { |
---|
| 82 | const char *longopt = 0; |
---|
| 83 | int opt = help->opt; |
---|
| 84 | const struct option *o = 0; |
---|
| 85 | |
---|
| 86 | if (HLP_ISLONG(opt)) { |
---|
[3b9ec3f] | 87 | o = longopts + HLP_DECODELONG(opt); |
---|
[aadc86e] | 88 | longopt = o->name; |
---|
| 89 | opt = o->val; |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | if (isalnum(opt)) |
---|
[5fbfd8d] | 93 | printf(" -%c%c", opt, longopt ? ',' : ' '); |
---|
[aadc86e] | 94 | else |
---|
| 95 | fputs(" ", stdout); |
---|
| 96 | |
---|
| 97 | if (longopt) { |
---|
| 98 | int len = strlen(longopt); |
---|
| 99 | printf(" --%s", longopt); |
---|
| 100 | if (o && o->has_arg) { |
---|
| 101 | const char *p; |
---|
| 102 | len += len + 1; |
---|
| 103 | |
---|
| 104 | if (o->has_arg == optional_argument) { |
---|
| 105 | putchar('['); |
---|
| 106 | len += 2; |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | putchar('='); |
---|
| 110 | |
---|
| 111 | for (p = longopt; *p ; p++) putchar(toupper(*p)); |
---|
| 112 | |
---|
| 113 | if (o->has_arg == optional_argument) putchar(']'); |
---|
| 114 | } |
---|
| 115 | len = (len >> 3) + 2; |
---|
| 116 | if (len > 4) len = 0; |
---|
| 117 | fputs(newline_tabs + len, stdout); |
---|
| 118 | } else { |
---|
| 119 | fputs(newline_tabs + 1, stdout); |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | puts(help->msg); |
---|
| 123 | help++; |
---|
| 124 | } |
---|
[a580dc1] | 125 | printf(" --help\t\t\t%s\n", msg(/*display this help and exit*/150)); |
---|
| 126 | printf(" --version\t\t\t%s\n", msg(/*output version information and exit*/151)); |
---|
[acc20b1] | 127 | |
---|
| 128 | if (extra_msg) { |
---|
| 129 | putnl(); |
---|
| 130 | puts(extra_msg); |
---|
| 131 | } |
---|
[647407d] | 132 | |
---|
[aadc86e] | 133 | exit(0); |
---|
| 134 | } |
---|
| 135 | |
---|
[3b9ec3f] | 136 | void |
---|
| 137 | cmdline_version(void) |
---|
[aadc86e] | 138 | { |
---|
[82afbc4] | 139 | printf("%s - "PRETTYPACKAGE" "VERSION"\n", msg_appname()); |
---|
[aadc86e] | 140 | } |
---|
| 141 | |
---|
[3b9ec3f] | 142 | void |
---|
| 143 | cmdline_syntax(void) |
---|
[cb3d1e2] | 144 | { |
---|
[bdfe97f] | 145 | printf("\n%s: %s", msg(/*Syntax*/49), msg_appname()); |
---|
[a580dc1] | 146 | if (help->opt) printf(" [%s]...", msg(/*OPTION*/153)); |
---|
[acc20b1] | 147 | if (args_msg) { |
---|
| 148 | putchar(' '); |
---|
| 149 | puts(args_msg); |
---|
| 150 | return; |
---|
| 151 | } |
---|
[3b9ec3f] | 152 | if (min_args) { |
---|
| 153 | int i = min_args; |
---|
[a580dc1] | 154 | while (i--) printf(" %s", msg(/*FILE*/124)); |
---|
[3b9ec3f] | 155 | } |
---|
[647407d] | 156 | if (max_args == -1) { |
---|
[a580dc1] | 157 | if (!min_args) printf(" [%s]", msg(/*FILE*/124)); |
---|
[647407d] | 158 | fputs("...", stdout); |
---|
| 159 | } else if (max_args > min_args) { |
---|
| 160 | int i = max_args - min_args; |
---|
[a580dc1] | 161 | while (i--) printf(" [%s]", msg(/*FILE*/124)); |
---|
[647407d] | 162 | } |
---|
[3b9ec3f] | 163 | putnl(); |
---|
| 164 | } |
---|
| 165 | |
---|
[2668614] | 166 | static void |
---|
| 167 | syntax_and_help_pointer(void) |
---|
| 168 | { |
---|
| 169 | cmdline_syntax(); |
---|
| 170 | fprintf(stderr, msg(/*Try `%s --help' for more information.\n*/157), |
---|
| 171 | msg_appname()); |
---|
| 172 | exit(1); |
---|
| 173 | } |
---|
| 174 | |
---|
| 175 | static void |
---|
| 176 | moan_and_die(int msgno) |
---|
| 177 | { |
---|
| 178 | fprintf(stderr, "%s: ", msg_appname()); |
---|
| 179 | fprintf(stderr, msg(msgno), optarg); |
---|
| 180 | fputnl(stderr); |
---|
| 181 | cmdline_syntax(); |
---|
| 182 | exit(1); |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | void |
---|
| 186 | cmdline_too_few_args(void) |
---|
| 187 | { |
---|
| 188 | fprintf(stderr, "%s: %s\n", msg_appname(), msg(/*too few arguments*/122)); |
---|
| 189 | syntax_and_help_pointer(); |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | void |
---|
| 193 | cmdline_too_many_args(void) |
---|
| 194 | { |
---|
| 195 | fprintf(stderr, "%s: %s\n", msg_appname(), msg(/*too many arguments*/123)); |
---|
| 196 | syntax_and_help_pointer(); |
---|
| 197 | } |
---|
| 198 | |
---|
[acc20b1] | 199 | void |
---|
| 200 | cmdline_set_syntax_message(const char *args, const char *extra) |
---|
| 201 | { |
---|
| 202 | args_msg = args; |
---|
| 203 | extra_msg = extra; |
---|
| 204 | } |
---|
| 205 | |
---|
[3b9ec3f] | 206 | int |
---|
| 207 | cmdline_int_arg(void) |
---|
[aadc86e] | 208 | { |
---|
[f2122f0] | 209 | long result; |
---|
[3b9ec3f] | 210 | char *endptr; |
---|
[cb3d1e2] | 211 | |
---|
[3b9ec3f] | 212 | errno = 0; |
---|
| 213 | |
---|
| 214 | result = strtol(optarg, &endptr, 10); |
---|
| 215 | |
---|
[f2122f0] | 216 | if (errno == ERANGE || result > INT_MAX || result < INT_MIN) { |
---|
[2668614] | 217 | moan_and_die(/*numeric argument `%s' out of range*/185); |
---|
[3b9ec3f] | 218 | } else if (*optarg == '\0' || *endptr != '\0') { |
---|
[2668614] | 219 | moan_and_die(/*argument `%s' not an integer*/186); |
---|
[3b9ec3f] | 220 | } |
---|
[cb3d1e2] | 221 | |
---|
[f2122f0] | 222 | return (int)result; |
---|
[aadc86e] | 223 | } |
---|
| 224 | |
---|
[acc20b1] | 225 | double |
---|
| 226 | cmdline_double_arg(void) |
---|
[aadc86e] | 227 | { |
---|
[acc20b1] | 228 | double result; |
---|
[3b9ec3f] | 229 | char *endptr; |
---|
[cb3d1e2] | 230 | |
---|
[3b9ec3f] | 231 | errno = 0; |
---|
| 232 | |
---|
| 233 | result = strtod(optarg, &endptr); |
---|
| 234 | |
---|
| 235 | if (errno == ERANGE) { |
---|
[2668614] | 236 | moan_and_die(/*numeric argument `%s' out of range*/185); |
---|
[3b9ec3f] | 237 | } else if (*optarg == '\0' || *endptr != '\0') { |
---|
[2668614] | 238 | moan_and_die(/*argument `%s' not a number*/187); |
---|
[3b9ec3f] | 239 | } |
---|
[cb3d1e2] | 240 | |
---|
[3b9ec3f] | 241 | return 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 | argc = argc_; |
---|
| 251 | argv = argv_; |
---|
| 252 | shortopts = shortopts_; |
---|
| 253 | longopts = longopts_; |
---|
| 254 | longind = longind_; |
---|
| 255 | help = help_; |
---|
| 256 | min_args = min_args_; |
---|
| 257 | max_args = max_args_; |
---|
| 258 | } |
---|
| 259 | |
---|
| 260 | int |
---|
| 261 | cmdline_getopt(void) |
---|
| 262 | { |
---|
| 263 | int opt = getopt_long(argc, argv, shortopts, longopts, longind); |
---|
[aadc86e] | 264 | |
---|
[2668614] | 265 | switch (opt) { |
---|
| 266 | case EOF: |
---|
[a580dc1] | 267 | /* check valid # of args given - if not give syntax message */ |
---|
[3b9ec3f] | 268 | if (argc - optind < min_args) { |
---|
[2668614] | 269 | cmdline_too_few_args(); |
---|
[3b9ec3f] | 270 | } else if (max_args >= 0 && argc - optind > max_args) { |
---|
[2668614] | 271 | cmdline_too_many_args(); |
---|
[3b9ec3f] | 272 | } |
---|
[2668614] | 273 | break; |
---|
[aadc86e] | 274 | case ':': /* parameter missing */ |
---|
| 275 | case '?': /* unknown opt, ambiguous match, or extraneous param */ |
---|
[a580dc1] | 276 | /* getopt displays a message for us */ |
---|
[2668614] | 277 | syntax_and_help_pointer(); |
---|
| 278 | break; |
---|
[aadc86e] | 279 | case HLP_VERSION: /* --version */ |
---|
[3b9ec3f] | 280 | cmdline_version(); |
---|
[aadc86e] | 281 | exit(0); |
---|
| 282 | case HLP_HELP: /* --help */ |
---|
[3b9ec3f] | 283 | cmdline_version(); |
---|
| 284 | cmdline_syntax(); |
---|
[aadc86e] | 285 | putchar('\n'); |
---|
[3b9ec3f] | 286 | cmdline_help(); |
---|
[aadc86e] | 287 | exit(0); |
---|
| 288 | } |
---|
| 289 | return opt; |
---|
| 290 | } |
---|