[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 | |
---|
[acc20b1] | 166 | void |
---|
| 167 | cmdline_set_syntax_message(const char *args, const char *extra) |
---|
| 168 | { |
---|
| 169 | args_msg = args; |
---|
| 170 | extra_msg = extra; |
---|
| 171 | } |
---|
| 172 | |
---|
[3b9ec3f] | 173 | int |
---|
| 174 | cmdline_int_arg(void) |
---|
[aadc86e] | 175 | { |
---|
[f2122f0] | 176 | long result; |
---|
[3b9ec3f] | 177 | char *endptr; |
---|
[cb3d1e2] | 178 | |
---|
[3b9ec3f] | 179 | errno = 0; |
---|
| 180 | |
---|
| 181 | result = strtol(optarg, &endptr, 10); |
---|
| 182 | |
---|
[f2122f0] | 183 | if (errno == ERANGE || result > INT_MAX || result < INT_MIN) { |
---|
[bdfe97f] | 184 | fprintf(stderr, "%s: ", msg_appname()); |
---|
[a580dc1] | 185 | fprintf(stderr, msg(/*numeric argument `%s' out of range*/185), optarg); |
---|
| 186 | fputnl(stderr); |
---|
[3b9ec3f] | 187 | cmdline_syntax(); |
---|
[acc20b1] | 188 | exit(1); |
---|
[3b9ec3f] | 189 | } else if (*optarg == '\0' || *endptr != '\0') { |
---|
[bdfe97f] | 190 | fprintf(stderr, "%s: ", msg_appname()); |
---|
[a580dc1] | 191 | fprintf(stderr, msg(/*argument `%s' not an integer*/186), optarg); |
---|
| 192 | fputnl(stderr); |
---|
[3b9ec3f] | 193 | cmdline_syntax(); |
---|
[acc20b1] | 194 | exit(1); |
---|
[3b9ec3f] | 195 | } |
---|
[cb3d1e2] | 196 | |
---|
[f2122f0] | 197 | return (int)result; |
---|
[aadc86e] | 198 | } |
---|
| 199 | |
---|
[acc20b1] | 200 | double |
---|
| 201 | cmdline_double_arg(void) |
---|
[aadc86e] | 202 | { |
---|
[acc20b1] | 203 | double result; |
---|
[3b9ec3f] | 204 | char *endptr; |
---|
[cb3d1e2] | 205 | |
---|
[3b9ec3f] | 206 | errno = 0; |
---|
| 207 | |
---|
| 208 | result = strtod(optarg, &endptr); |
---|
| 209 | |
---|
| 210 | if (errno == ERANGE) { |
---|
[bdfe97f] | 211 | fprintf(stderr, "%s: ", msg_appname()); |
---|
[a580dc1] | 212 | fprintf(stderr, msg(/*numeric argument `%s' out of range*/185), optarg); |
---|
| 213 | fputnl(stderr); |
---|
[3b9ec3f] | 214 | cmdline_syntax(); |
---|
[acc20b1] | 215 | exit(1); |
---|
[3b9ec3f] | 216 | } else if (*optarg == '\0' || *endptr != '\0') { |
---|
[bdfe97f] | 217 | fprintf(stderr, "%s: ", msg_appname()); |
---|
[a580dc1] | 218 | fprintf(stderr, msg(/*argument `%s' not a number*/187), optarg); |
---|
| 219 | fputnl(stderr); |
---|
[3b9ec3f] | 220 | cmdline_syntax(); |
---|
[acc20b1] | 221 | exit(1); |
---|
[3b9ec3f] | 222 | } |
---|
[cb3d1e2] | 223 | |
---|
[3b9ec3f] | 224 | return result; |
---|
| 225 | } |
---|
| 226 | |
---|
| 227 | void |
---|
| 228 | cmdline_init(int argc_, char *const *argv_, const char *shortopts_, |
---|
| 229 | const struct option *longopts_, int *longind_, |
---|
| 230 | const struct help_msg *help_, |
---|
| 231 | int min_args_, int max_args_) |
---|
| 232 | { |
---|
| 233 | argc = argc_; |
---|
| 234 | argv = argv_; |
---|
| 235 | shortopts = shortopts_; |
---|
| 236 | longopts = longopts_; |
---|
| 237 | longind = longind_; |
---|
| 238 | help = help_; |
---|
| 239 | min_args = min_args_; |
---|
| 240 | max_args = max_args_; |
---|
| 241 | } |
---|
| 242 | |
---|
| 243 | int |
---|
| 244 | cmdline_getopt(void) |
---|
| 245 | { |
---|
| 246 | int opt = getopt_long(argc, argv, shortopts, longopts, longind); |
---|
[aadc86e] | 247 | |
---|
| 248 | if (opt == EOF) { |
---|
[a580dc1] | 249 | /* check valid # of args given - if not give syntax message */ |
---|
[3b9ec3f] | 250 | if (argc - optind < min_args) { |
---|
[bdfe97f] | 251 | fprintf(stderr, "%s: %s\n", msg_appname(), msg(/*too few arguments*/122)); |
---|
[3b9ec3f] | 252 | opt = '?'; |
---|
| 253 | } else if (max_args >= 0 && argc - optind > max_args) { |
---|
[bdfe97f] | 254 | fprintf(stderr, "%s: %s\n", msg_appname(), msg(/*too many arguments*/123)); |
---|
[3b9ec3f] | 255 | opt = '?'; |
---|
| 256 | } |
---|
[aadc86e] | 257 | } |
---|
| 258 | |
---|
| 259 | switch (opt) { |
---|
| 260 | case ':': /* parameter missing */ |
---|
| 261 | case '?': /* unknown opt, ambiguous match, or extraneous param */ |
---|
[a580dc1] | 262 | /* getopt displays a message for us */ |
---|
[3b9ec3f] | 263 | cmdline_syntax(); |
---|
[a580dc1] | 264 | fprintf(stderr, msg(/*Try `%s --help' for more information.\n*/157), |
---|
[bdfe97f] | 265 | msg_appname()); |
---|
[acc20b1] | 266 | exit(1); |
---|
[aadc86e] | 267 | case HLP_VERSION: /* --version */ |
---|
[3b9ec3f] | 268 | cmdline_version(); |
---|
[aadc86e] | 269 | exit(0); |
---|
| 270 | case HLP_HELP: /* --help */ |
---|
[3b9ec3f] | 271 | cmdline_version(); |
---|
| 272 | cmdline_syntax(); |
---|
[aadc86e] | 273 | putchar('\n'); |
---|
[3b9ec3f] | 274 | cmdline_help(); |
---|
[aadc86e] | 275 | exit(0); |
---|
| 276 | } |
---|
| 277 | return opt; |
---|
| 278 | } |
---|