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