[6974a34] | 1 | /* cmdline.c |
---|
[aadc86e] | 2 | * Wrapper for GNU getopt which deals with standard options |
---|
[ab8bd76] | 3 | * Copyright (C) 1998-2001,2003,2004,2011,2012,2014,2024 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 | #include <config.h> |
---|
| 21 | |
---|
| 22 | #include <ctype.h> |
---|
[3b9ec3f] | 23 | #include <errno.h> |
---|
[dd6f74e] | 24 | #include <float.h> |
---|
[aadc86e] | 25 | #include <stdio.h> |
---|
[a49a80c0] | 26 | #include <stdlib.h> |
---|
[e05bdb8] | 27 | #include <string.h> |
---|
[f2122f0] | 28 | #include <limits.h> |
---|
[aadc86e] | 29 | |
---|
[be97baf] | 30 | #include "getopt.h" |
---|
| 31 | |
---|
[aadc86e] | 32 | #include "cmdline.h" |
---|
[45af761] | 33 | #include "debug.h" |
---|
[e05bdb8] | 34 | #include "filename.h" |
---|
[aadc86e] | 35 | |
---|
[d227ee5] | 36 | #include "message.h" |
---|
| 37 | |
---|
[aadc86e] | 38 | /* It might be useful to be able to disable all long options on small |
---|
[175cac6] | 39 | * platforms like older PDAs. |
---|
[aadc86e] | 40 | */ |
---|
| 41 | #if 0 |
---|
| 42 | # define getopt_long(ARGC, ARGV, STR, OPTS, PTR) getopt(ARGC, ARGV, STR) |
---|
| 43 | #endif |
---|
| 44 | |
---|
| 45 | /* |
---|
| 46 | * bad command line give: |
---|
| 47 | * <problem> |
---|
[cb3d1e2] | 48 | * |
---|
[e05bdb8] | 49 | * <short syntax> |
---|
[cb3d1e2] | 50 | * |
---|
[aadc86e] | 51 | * --help gives: |
---|
| 52 | * <version> |
---|
[cb3d1e2] | 53 | * |
---|
[aadc86e] | 54 | * <short syntax> |
---|
| 55 | * |
---|
| 56 | * <table> |
---|
[cb3d1e2] | 57 | * |
---|
[aadc86e] | 58 | * <blurb> |
---|
| 59 | * |
---|
| 60 | * --version gives: |
---|
| 61 | * <version> |
---|
| 62 | */ |
---|
| 63 | |
---|
| 64 | /* |
---|
| 65 | * want to cope with optional/required parameters on long options |
---|
| 66 | * and also parameters on short options |
---|
[3b9ec3f] | 67 | */ |
---|
[aadc86e] | 68 | |
---|
| 69 | static const char newline_tabs[] = "\n\t\t\t\t"; |
---|
| 70 | |
---|
[3b9ec3f] | 71 | static int argc; |
---|
[647407d] | 72 | static char * const *argv; |
---|
[3b9ec3f] | 73 | static const char *shortopts; |
---|
| 74 | static const struct option *longopts; |
---|
| 75 | static int *longind; |
---|
| 76 | static const struct help_msg *help; |
---|
| 77 | static int min_args, max_args; |
---|
[d8dbdff] | 78 | static int msg_args, msg_extra; |
---|
| 79 | static const char * msg_extra_arg; |
---|
[3b9ec3f] | 80 | |
---|
| 81 | void |
---|
| 82 | cmdline_help(void) |
---|
[aadc86e] | 83 | { |
---|
[9e513bd3] | 84 | while (help && help->opt) { |
---|
[aadc86e] | 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) { |
---|
[ce668fb] | 108 | PUTCHAR('['); |
---|
[aadc86e] | 109 | len += 2; |
---|
| 110 | } |
---|
| 111 | |
---|
[ce668fb] | 112 | PUTCHAR('='); |
---|
[aadc86e] | 113 | |
---|
[5a2d346] | 114 | if (help->placeholder) { |
---|
| 115 | fputs(help->placeholder, stdout); |
---|
| 116 | } else { |
---|
| 117 | for (p = longopt; *p ; p++) { |
---|
| 118 | unsigned char ch = *p; |
---|
[ce668fb] | 119 | PUTCHAR((ch == '-') ? '_' : toupper(ch)); |
---|
[5a2d346] | 120 | } |
---|
[ab8bd76] | 121 | } |
---|
[aadc86e] | 122 | |
---|
[ce668fb] | 123 | if (o->has_arg == optional_argument) PUTCHAR(']'); |
---|
[aadc86e] | 124 | } |
---|
| 125 | len = (len >> 3) + 2; |
---|
| 126 | if (len > 4) len = 0; |
---|
| 127 | fputs(newline_tabs + len, stdout); |
---|
| 128 | } else { |
---|
| 129 | fputs(newline_tabs + 1, stdout); |
---|
| 130 | } |
---|
| 131 | |
---|
[45af761] | 132 | if (help->arg) { |
---|
| 133 | SVX_ASSERT(strstr(msg(help->msg_no), "%s") != NULL); |
---|
| 134 | printf(msg(help->msg_no), help->arg); |
---|
| 135 | putnl(); |
---|
| 136 | } else { |
---|
| 137 | SVX_ASSERT(strstr(msg(help->msg_no), "%s") == NULL); |
---|
| 138 | puts(msg(help->msg_no)); |
---|
| 139 | } |
---|
[aadc86e] | 140 | help++; |
---|
| 141 | } |
---|
[45af761] | 142 | fputs(" --help\t\t\t", stdout); |
---|
[736f7df] | 143 | /* TRANSLATORS: description of --help option */ |
---|
[45af761] | 144 | puts(msg(/*display this help and exit*/150)); |
---|
| 145 | fputs(" --version\t\t\t", stdout); |
---|
[c5d45ba] | 146 | /* TRANSLATORS: description of --version option */ |
---|
[45af761] | 147 | puts(msg(/*output version information and exit*/151)); |
---|
[acc20b1] | 148 | |
---|
[d8dbdff] | 149 | if (msg_extra) { |
---|
[acc20b1] | 150 | putnl(); |
---|
[d8dbdff] | 151 | if (msg_extra_arg) { |
---|
| 152 | SVX_ASSERT(strstr(msg(msg_extra), "%s") != NULL); |
---|
| 153 | printf(msg(msg_extra), msg_extra_arg); |
---|
| 154 | putnl(); |
---|
| 155 | } else { |
---|
| 156 | SVX_ASSERT(strstr(msg(msg_extra), "%s") == NULL); |
---|
| 157 | puts(msg(msg_extra)); |
---|
| 158 | } |
---|
[acc20b1] | 159 | } |
---|
[647407d] | 160 | |
---|
[aadc86e] | 161 | exit(0); |
---|
| 162 | } |
---|
| 163 | |
---|
[3b9ec3f] | 164 | void |
---|
| 165 | cmdline_version(void) |
---|
[aadc86e] | 166 | { |
---|
[82afbc4] | 167 | printf("%s - "PRETTYPACKAGE" "VERSION"\n", msg_appname()); |
---|
[aadc86e] | 168 | } |
---|
| 169 | |
---|
[3b9ec3f] | 170 | void |
---|
| 171 | cmdline_syntax(void) |
---|
[cb3d1e2] | 172 | { |
---|
[736f7df] | 173 | /* TRANSLATORS: as in: Usage: cavern … */ |
---|
[ee7511a] | 174 | printf("\n%s: %s", msg(/*Usage*/49), msg_appname()); |
---|
[736f7df] | 175 | /* TRANSLATORS: in command line usage messages e.g. Usage: cavern [OPTION]… */ |
---|
[9e513bd3] | 176 | if (help && help->opt) printf(" [%s]...", msg(/*OPTION*/153)); |
---|
[d8dbdff] | 177 | if (msg_args) { |
---|
[ce668fb] | 178 | PUTCHAR(' '); |
---|
[d8dbdff] | 179 | puts(msg(msg_args)); |
---|
[acc20b1] | 180 | return; |
---|
| 181 | } |
---|
[3b9ec3f] | 182 | if (min_args) { |
---|
| 183 | int i = min_args; |
---|
[a580dc1] | 184 | while (i--) printf(" %s", msg(/*FILE*/124)); |
---|
[3b9ec3f] | 185 | } |
---|
[647407d] | 186 | if (max_args == -1) { |
---|
[a580dc1] | 187 | if (!min_args) printf(" [%s]", msg(/*FILE*/124)); |
---|
[647407d] | 188 | fputs("...", stdout); |
---|
| 189 | } else if (max_args > min_args) { |
---|
| 190 | int i = max_args - min_args; |
---|
[a580dc1] | 191 | while (i--) printf(" [%s]", msg(/*FILE*/124)); |
---|
[647407d] | 192 | } |
---|
[3b9ec3f] | 193 | putnl(); |
---|
| 194 | } |
---|
| 195 | |
---|
[2668614] | 196 | static void |
---|
| 197 | syntax_and_help_pointer(void) |
---|
| 198 | { |
---|
| 199 | cmdline_syntax(); |
---|
[ee7511a] | 200 | fprintf(stderr, msg(/*Try “%s --help” for more information.\n*/157), |
---|
[2668614] | 201 | msg_appname()); |
---|
| 202 | exit(1); |
---|
| 203 | } |
---|
| 204 | |
---|
| 205 | static void |
---|
| 206 | moan_and_die(int msgno) |
---|
| 207 | { |
---|
| 208 | fprintf(stderr, "%s: ", msg_appname()); |
---|
| 209 | fprintf(stderr, msg(msgno), optarg); |
---|
| 210 | fputnl(stderr); |
---|
| 211 | cmdline_syntax(); |
---|
| 212 | exit(1); |
---|
| 213 | } |
---|
| 214 | |
---|
| 215 | void |
---|
| 216 | cmdline_too_few_args(void) |
---|
| 217 | { |
---|
| 218 | fprintf(stderr, "%s: %s\n", msg_appname(), msg(/*too few arguments*/122)); |
---|
| 219 | syntax_and_help_pointer(); |
---|
| 220 | } |
---|
| 221 | |
---|
| 222 | void |
---|
| 223 | cmdline_too_many_args(void) |
---|
| 224 | { |
---|
| 225 | fprintf(stderr, "%s: %s\n", msg_appname(), msg(/*too many arguments*/123)); |
---|
| 226 | syntax_and_help_pointer(); |
---|
| 227 | } |
---|
| 228 | |
---|
[acc20b1] | 229 | void |
---|
[d8dbdff] | 230 | cmdline_set_syntax_message(int msg_args_, int msg_extra_, const char * arg) |
---|
[acc20b1] | 231 | { |
---|
[d8dbdff] | 232 | msg_args = msg_args_; |
---|
| 233 | msg_extra = msg_extra_; |
---|
| 234 | msg_extra_arg = arg; |
---|
[acc20b1] | 235 | } |
---|
| 236 | |
---|
[3b9ec3f] | 237 | int |
---|
| 238 | cmdline_int_arg(void) |
---|
[aadc86e] | 239 | { |
---|
[f2122f0] | 240 | long result; |
---|
[3b9ec3f] | 241 | char *endptr; |
---|
[cb3d1e2] | 242 | |
---|
[3b9ec3f] | 243 | errno = 0; |
---|
| 244 | |
---|
| 245 | result = strtol(optarg, &endptr, 10); |
---|
| 246 | |
---|
[f2122f0] | 247 | if (errno == ERANGE || result > INT_MAX || result < INT_MIN) { |
---|
[0804fbe] | 248 | moan_and_die(/*numeric argument “%s” out of range*/185); |
---|
[3b9ec3f] | 249 | } else if (*optarg == '\0' || *endptr != '\0') { |
---|
[0804fbe] | 250 | moan_and_die(/*argument “%s” not an integer*/186); |
---|
[3b9ec3f] | 251 | } |
---|
[cb3d1e2] | 252 | |
---|
[f2122f0] | 253 | return (int)result; |
---|
[aadc86e] | 254 | } |
---|
| 255 | |
---|
[acc20b1] | 256 | double |
---|
| 257 | cmdline_double_arg(void) |
---|
[aadc86e] | 258 | { |
---|
[acc20b1] | 259 | double result; |
---|
[3b9ec3f] | 260 | char *endptr; |
---|
[cb3d1e2] | 261 | |
---|
[3b9ec3f] | 262 | errno = 0; |
---|
| 263 | |
---|
| 264 | result = strtod(optarg, &endptr); |
---|
| 265 | |
---|
| 266 | if (errno == ERANGE) { |
---|
[0804fbe] | 267 | moan_and_die(/*numeric argument “%s” out of range*/185); |
---|
[3b9ec3f] | 268 | } else if (*optarg == '\0' || *endptr != '\0') { |
---|
[0804fbe] | 269 | moan_and_die(/*argument “%s” not a number*/187); |
---|
[3b9ec3f] | 270 | } |
---|
[cb3d1e2] | 271 | |
---|
[3b9ec3f] | 272 | return result; |
---|
| 273 | } |
---|
| 274 | |
---|
| 275 | void |
---|
| 276 | cmdline_init(int argc_, char *const *argv_, const char *shortopts_, |
---|
| 277 | const struct option *longopts_, int *longind_, |
---|
| 278 | const struct help_msg *help_, |
---|
| 279 | int min_args_, int max_args_) |
---|
| 280 | { |
---|
| 281 | argc = argc_; |
---|
| 282 | argv = argv_; |
---|
| 283 | shortopts = shortopts_; |
---|
| 284 | longopts = longopts_; |
---|
| 285 | longind = longind_; |
---|
| 286 | help = help_; |
---|
| 287 | min_args = min_args_; |
---|
| 288 | max_args = max_args_; |
---|
| 289 | } |
---|
| 290 | |
---|
| 291 | int |
---|
| 292 | cmdline_getopt(void) |
---|
| 293 | { |
---|
| 294 | int opt = getopt_long(argc, argv, shortopts, longopts, longind); |
---|
[aadc86e] | 295 | |
---|
[2668614] | 296 | switch (opt) { |
---|
| 297 | case EOF: |
---|
[a580dc1] | 298 | /* check valid # of args given - if not give syntax message */ |
---|
[3b9ec3f] | 299 | if (argc - optind < min_args) { |
---|
[2668614] | 300 | cmdline_too_few_args(); |
---|
[3b9ec3f] | 301 | } else if (max_args >= 0 && argc - optind > max_args) { |
---|
[2668614] | 302 | cmdline_too_many_args(); |
---|
[3b9ec3f] | 303 | } |
---|
[2668614] | 304 | break; |
---|
[aadc86e] | 305 | case ':': /* parameter missing */ |
---|
| 306 | case '?': /* unknown opt, ambiguous match, or extraneous param */ |
---|
[a580dc1] | 307 | /* getopt displays a message for us */ |
---|
[2668614] | 308 | syntax_and_help_pointer(); |
---|
| 309 | break; |
---|
[aadc86e] | 310 | case HLP_VERSION: /* --version */ |
---|
[3b9ec3f] | 311 | cmdline_version(); |
---|
[aadc86e] | 312 | exit(0); |
---|
| 313 | case HLP_HELP: /* --help */ |
---|
[3b9ec3f] | 314 | cmdline_version(); |
---|
| 315 | cmdline_syntax(); |
---|
[ce668fb] | 316 | PUTCHAR('\n'); |
---|
[3b9ec3f] | 317 | cmdline_help(); |
---|
[aadc86e] | 318 | exit(0); |
---|
| 319 | } |
---|
| 320 | return opt; |
---|
| 321 | } |
---|