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