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