[aadc86e] | 1 | /* > cmdline.c |
---|
| 2 | * Wrapper for GNU getopt which deals with standard options |
---|
[2e70979] | 3 | * Copyright (C) 1998-2000 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> |
---|
[aadc86e] | 26 | #include <stdio.h> |
---|
[e05bdb8] | 27 | #include <string.h> |
---|
[aadc86e] | 28 | |
---|
| 29 | #include "cmdline.h" |
---|
[e05bdb8] | 30 | #include "filename.h" |
---|
[aadc86e] | 31 | |
---|
| 32 | /* It might be useful to be able to disable all long options on small |
---|
[3b9ec3f] | 33 | * platforms like pre-386 DOS and perhaps some PDAs... |
---|
[aadc86e] | 34 | */ |
---|
| 35 | #if 0 |
---|
| 36 | # define getopt_long(ARGC, ARGV, STR, OPTS, PTR) getopt(ARGC, ARGV, STR) |
---|
| 37 | #endif |
---|
| 38 | |
---|
| 39 | /* |
---|
| 40 | * bad command line give: |
---|
| 41 | * <problem> |
---|
| 42 | * |
---|
[e05bdb8] | 43 | * <short syntax> |
---|
[aadc86e] | 44 | * |
---|
| 45 | * --help gives: |
---|
| 46 | * <version> |
---|
| 47 | * |
---|
| 48 | * <short syntax> |
---|
| 49 | * |
---|
| 50 | * <table> |
---|
| 51 | * |
---|
| 52 | * <blurb> |
---|
| 53 | * |
---|
| 54 | * --version gives: |
---|
| 55 | * <version> |
---|
| 56 | */ |
---|
| 57 | |
---|
| 58 | /* |
---|
| 59 | * want to cope with optional/required parameters on long options |
---|
| 60 | * and also parameters on short options |
---|
[3b9ec3f] | 61 | */ |
---|
[aadc86e] | 62 | |
---|
| 63 | static const char newline_tabs[] = "\n\t\t\t\t"; |
---|
| 64 | |
---|
[3b9ec3f] | 65 | static int argc; |
---|
| 66 | static char *const *argv; |
---|
| 67 | static const char *shortopts; |
---|
| 68 | static const struct option *longopts; |
---|
| 69 | static int *longind; |
---|
| 70 | static const struct help_msg *help; |
---|
| 71 | static int min_args, max_args; |
---|
| 72 | |
---|
| 73 | static const char *argv0 = NULL; |
---|
[aadc86e] | 74 | |
---|
[3b9ec3f] | 75 | void |
---|
| 76 | cmdline_help(void) |
---|
[aadc86e] | 77 | { |
---|
| 78 | while (help->opt) { |
---|
| 79 | const char *longopt = 0; |
---|
| 80 | int opt = help->opt; |
---|
| 81 | const struct option *o = 0; |
---|
| 82 | |
---|
| 83 | if (HLP_ISLONG(opt)) { |
---|
[3b9ec3f] | 84 | o = longopts + HLP_DECODELONG(opt); |
---|
[aadc86e] | 85 | longopt = o->name; |
---|
| 86 | opt = o->val; |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | if (isalnum(opt)) |
---|
| 90 | printf(" -%c,", opt); |
---|
| 91 | else |
---|
| 92 | fputs(" ", stdout); |
---|
| 93 | |
---|
| 94 | if (longopt) { |
---|
| 95 | int len = strlen(longopt); |
---|
| 96 | printf(" --%s", longopt); |
---|
| 97 | if (o && o->has_arg) { |
---|
| 98 | const char *p; |
---|
| 99 | len += len + 1; |
---|
| 100 | |
---|
| 101 | if (o->has_arg == optional_argument) { |
---|
| 102 | putchar('['); |
---|
| 103 | len += 2; |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | putchar('='); |
---|
| 107 | |
---|
| 108 | for (p = longopt; *p ; p++) putchar(toupper(*p)); |
---|
| 109 | |
---|
| 110 | if (o->has_arg == optional_argument) putchar(']'); |
---|
| 111 | } |
---|
| 112 | len = (len >> 3) + 2; |
---|
| 113 | if (len > 4) len = 0; |
---|
| 114 | fputs(newline_tabs + len, stdout); |
---|
| 115 | } else { |
---|
| 116 | fputs(newline_tabs + 1, stdout); |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | puts(help->msg); |
---|
| 120 | help++; |
---|
| 121 | } |
---|
[e05bdb8] | 122 | /* FIXME: translate */ |
---|
[aadc86e] | 123 | puts(" --help\t\t\tdisplay this help and exit\n" |
---|
| 124 | " --version\t\t\toutput version information and exit"); |
---|
| 125 | |
---|
| 126 | exit(0); |
---|
| 127 | } |
---|
| 128 | |
---|
[3b9ec3f] | 129 | void |
---|
| 130 | cmdline_version(void) |
---|
[aadc86e] | 131 | { |
---|
| 132 | printf("%s - "PACKAGE" "VERSION"\n", argv0); |
---|
| 133 | } |
---|
| 134 | |
---|
[3b9ec3f] | 135 | void |
---|
| 136 | cmdline_syntax(void) |
---|
| 137 | { |
---|
| 138 | printf("\nSyntax: %s [OPTION]...", argv0); |
---|
| 139 | if (min_args) { |
---|
| 140 | int i = min_args; |
---|
| 141 | while (i--) fputs(" FILE", stdout); |
---|
| 142 | } else { |
---|
| 143 | if (max_args) fputs(" [FILE]", stdout); |
---|
| 144 | } |
---|
| 145 | if (max_args == -1) fputs("...", stdout); |
---|
| 146 | |
---|
| 147 | /* FIXME: not quite right - "..." means an indefinite number */ |
---|
| 148 | if (max_args > min_args) fputs("...", stdout); |
---|
| 149 | putnl(); |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | int |
---|
| 153 | cmdline_int_arg(void) |
---|
[aadc86e] | 154 | { |
---|
[3b9ec3f] | 155 | int result; |
---|
| 156 | char *endptr; |
---|
| 157 | |
---|
| 158 | errno = 0; |
---|
| 159 | |
---|
| 160 | result = strtol(optarg, &endptr, 10); |
---|
| 161 | |
---|
| 162 | if (errno == ERANGE) { |
---|
| 163 | fprintf(stderr, "%s: numeric argument `%s' out of range\n", argv0, optarg); |
---|
| 164 | cmdline_syntax(); |
---|
| 165 | exit(0); |
---|
| 166 | } else if (*optarg == '\0' || *endptr != '\0') { |
---|
| 167 | fprintf(stderr, "%s: argument `%s' not an integer\n", argv0, optarg); |
---|
| 168 | cmdline_syntax(); |
---|
| 169 | exit(0); |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | return result; |
---|
[aadc86e] | 173 | } |
---|
| 174 | |
---|
[3b9ec3f] | 175 | float |
---|
| 176 | cmdline_float_arg(void) |
---|
[aadc86e] | 177 | { |
---|
[3b9ec3f] | 178 | float result; |
---|
| 179 | char *endptr; |
---|
[aadc86e] | 180 | |
---|
[3b9ec3f] | 181 | errno = 0; |
---|
| 182 | |
---|
| 183 | result = strtod(optarg, &endptr); |
---|
| 184 | |
---|
| 185 | if (errno == ERANGE) { |
---|
| 186 | fprintf(stderr, "%s: numeric argument `%s' out of range\n", argv0, optarg); |
---|
| 187 | cmdline_syntax(); |
---|
| 188 | exit(0); |
---|
| 189 | } else if (*optarg == '\0' || *endptr != '\0') { |
---|
| 190 | fprintf(stderr, "%s: argument `%s' not a number\n", argv0, optarg); |
---|
| 191 | cmdline_syntax(); |
---|
| 192 | exit(0); |
---|
| 193 | } |
---|
[aadc86e] | 194 | |
---|
[3b9ec3f] | 195 | return result; |
---|
| 196 | } |
---|
| 197 | |
---|
| 198 | void |
---|
| 199 | cmdline_init(int argc_, char *const *argv_, const char *shortopts_, |
---|
| 200 | const struct option *longopts_, int *longind_, |
---|
| 201 | const struct help_msg *help_, |
---|
| 202 | int min_args_, int max_args_) |
---|
| 203 | { |
---|
| 204 | if (!argv0) { |
---|
| 205 | argv0 = argv_[0]; |
---|
| 206 | /* FIXME: tidy up argv0 (remove path and extension) */ |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | argc = argc_; |
---|
| 210 | argv = argv_; |
---|
| 211 | shortopts = shortopts_; |
---|
| 212 | longopts = longopts_; |
---|
| 213 | longind = longind_; |
---|
| 214 | help = help_; |
---|
| 215 | min_args = min_args_; |
---|
| 216 | max_args = max_args_; |
---|
| 217 | } |
---|
| 218 | |
---|
| 219 | int |
---|
| 220 | cmdline_getopt(void) |
---|
| 221 | { |
---|
| 222 | int opt = getopt_long(argc, argv, shortopts, longopts, longind); |
---|
[aadc86e] | 223 | |
---|
| 224 | if (opt == EOF) { |
---|
[3b9ec3f] | 225 | /* check minimum # of args given - if not give syntax message */ |
---|
| 226 | if (argc - optind < min_args) { |
---|
| 227 | /* FIXME: TRANSLATE */ |
---|
| 228 | fprintf(stderr, "%s: too few arguments\n", argv0); |
---|
| 229 | opt = '?'; |
---|
| 230 | } else if (max_args >= 0 && argc - optind > max_args) { |
---|
| 231 | /* FIXME: TRANSLATE */ |
---|
| 232 | fprintf(stderr, "%s: too many arguments\n", argv0); |
---|
| 233 | opt = '?'; |
---|
| 234 | } |
---|
[aadc86e] | 235 | } |
---|
| 236 | |
---|
| 237 | switch (opt) { |
---|
| 238 | case ':': /* parameter missing */ |
---|
| 239 | case '?': /* unknown opt, ambiguous match, or extraneous param */ |
---|
| 240 | /* getopt displays a message for us (unless we set opterr to 0) */ |
---|
[3b9ec3f] | 241 | /* FIXME: set opterr to 0 so we can translate messages? */ |
---|
| 242 | cmdline_syntax(); |
---|
[a93df0a] | 243 | /* FIXME: translate */ |
---|
| 244 | fprintf(stderr, "Try `%s --help' for more information.\n", argv0); |
---|
[aadc86e] | 245 | exit(0); |
---|
| 246 | case HLP_VERSION: /* --version */ |
---|
[3b9ec3f] | 247 | cmdline_version(); |
---|
[aadc86e] | 248 | exit(0); |
---|
| 249 | case HLP_HELP: /* --help */ |
---|
[3b9ec3f] | 250 | cmdline_version(); |
---|
| 251 | cmdline_syntax(); |
---|
[aadc86e] | 252 | putchar('\n'); |
---|
[3b9ec3f] | 253 | cmdline_help(); |
---|
[aadc86e] | 254 | exit(0); |
---|
| 255 | } |
---|
| 256 | return opt; |
---|
| 257 | } |
---|