Chris@82: /* Chris@82: * Copyright (c) 2003, 2007-14 Matteo Frigo Chris@82: * Copyright (c) 2003, 2007-14 Massachusetts Institute of Technology Chris@82: * Chris@82: * This program is free software; you can redistribute it and/or modify Chris@82: * it under the terms of the GNU General Public License as published by Chris@82: * the Free Software Foundation; either version 2 of the License, or Chris@82: * (at your option) any later version. Chris@82: * Chris@82: * This program is distributed in the hope that it will be useful, Chris@82: * but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@82: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@82: * GNU General Public License for more details. Chris@82: * Chris@82: * You should have received a copy of the GNU General Public License Chris@82: * along with this program; if not, write to the Free Software Chris@82: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Chris@82: * Chris@82: */ Chris@82: Chris@82: #include Chris@82: #include Chris@82: Chris@82: #include "config.h" Chris@82: #include "my-getopt.h" Chris@82: Chris@82: int my_optind = 1; Chris@82: const char *my_optarg = 0; Chris@82: static const char *scan_pointer = 0; Chris@82: Chris@82: void my_usage(const char *progname, const struct my_option *opt) Chris@82: { Chris@82: int i; Chris@82: size_t col = 0; Chris@82: Chris@82: fprintf(stdout, "Usage: %s", progname); Chris@82: col += (strlen(progname) + 7); Chris@82: for (i = 0; opt[i].long_name; i++) { Chris@82: size_t option_len; Chris@82: Chris@82: option_len = strlen(opt[i].long_name); Chris@82: if (col >= 80 - (option_len + 16)) { Chris@82: fputs("\n\t", stdout); Chris@82: col = 8; Chris@82: } Chris@82: fprintf(stdout, " [--%s", opt[i].long_name); Chris@82: col += (option_len + 4); Chris@82: if (opt[i].short_name < 128) { Chris@82: fprintf(stdout, " | -%c", opt[i].short_name); Chris@82: col += 5; Chris@82: } Chris@82: switch (opt[i].argtype) { Chris@82: case REQARG: Chris@82: fputs(" arg]", stdout); Chris@82: col += 5; Chris@82: break; Chris@82: case OPTARG: Chris@82: fputs(" [arg]]", stdout); Chris@82: col += 10; Chris@82: break; Chris@82: default: Chris@82: fputs("]", stdout); Chris@82: col++; Chris@82: } Chris@82: } Chris@82: Chris@82: fputs ("\n", stdout); Chris@82: } Chris@82: Chris@82: int my_getopt(int argc, char *argv[], const struct my_option *optarray) Chris@82: { Chris@82: const char *p; Chris@82: const struct my_option *l; Chris@82: Chris@82: if (scan_pointer && *scan_pointer) { Chris@82: /* continue a previously scanned argv[] element */ Chris@82: p = scan_pointer; Chris@82: goto short_option; Chris@82: } else { Chris@82: /* new argv[] element */ Chris@82: if (my_optind >= argc) Chris@82: return -1; /* no more options */ Chris@82: Chris@82: p = argv[my_optind]; Chris@82: Chris@82: if (*p++ != '-') Chris@82: return (-1); /* not an option */ Chris@82: Chris@82: if (!*p) Chris@82: return (-1); /* string is exactly '-' */ Chris@82: Chris@82: ++my_optind; Chris@82: } Chris@82: Chris@82: if (*p == '-') { Chris@82: /* long option */ Chris@82: scan_pointer = 0; Chris@82: my_optarg = 0; Chris@82: Chris@82: ++p; Chris@82: Chris@82: for (l = optarray; l->short_name; ++l) { Chris@82: size_t len = strlen(l->long_name); Chris@82: if (!strncmp(l->long_name, p, len) && Chris@82: (!p[len] || p[len] == '=')) { Chris@82: switch (l->argtype) { Chris@82: case NOARG: Chris@82: goto ok; Chris@82: case OPTARG: Chris@82: if (p[len] == '=') Chris@82: my_optarg = p + len + 1; Chris@82: goto ok; Chris@82: case REQARG: Chris@82: if (p[len] == '=') { Chris@82: my_optarg = p + len + 1; Chris@82: goto ok; Chris@82: } Chris@82: if (my_optind >= argc) { Chris@82: fprintf(stderr, Chris@82: "option --%s requires an argument\n", Chris@82: l->long_name); Chris@82: return '?'; Chris@82: } Chris@82: my_optarg = argv[my_optind]; Chris@82: ++my_optind; Chris@82: goto ok; Chris@82: } Chris@82: } Chris@82: } Chris@82: } else { Chris@82: short_option: Chris@82: scan_pointer = 0; Chris@82: my_optarg = 0; Chris@82: Chris@82: for (l = optarray; l->short_name; ++l) { Chris@82: if (l->short_name == (char)l->short_name && Chris@82: *p == l->short_name) { Chris@82: ++p; Chris@82: switch (l->argtype) { Chris@82: case NOARG: Chris@82: scan_pointer = p; Chris@82: goto ok; Chris@82: case OPTARG: Chris@82: if (*p) Chris@82: my_optarg = p; Chris@82: goto ok; Chris@82: case REQARG: Chris@82: if (*p) { Chris@82: my_optarg = p; Chris@82: } else { Chris@82: if (my_optind >= argc) { Chris@82: fprintf(stderr, Chris@82: "option -%c requires an argument\n", Chris@82: l->short_name); Chris@82: return '?'; Chris@82: } Chris@82: my_optarg = argv[my_optind]; Chris@82: ++my_optind; Chris@82: } Chris@82: goto ok; Chris@82: } Chris@82: } Chris@82: } Chris@82: } Chris@82: Chris@82: fprintf(stderr, "unrecognized option %s\n", argv[my_optind - 1]); Chris@82: return '?'; Chris@82: Chris@82: ok: Chris@82: return l->short_name; Chris@82: } Chris@82: