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