annotate src/fftw-3.3.5/libbench2/my-getopt.c @ 83:ae30d91d2ffe

Replace these with versions built using an older toolset (so as to avoid ABI compatibilities when linking on Ubuntu 14.04 for packaging purposes)
author Chris Cannam
date Fri, 07 Feb 2020 11:51:13 +0000
parents 2cd0e3b3e1fd
children
rev   line source
Chris@42 1 /*
Chris@42 2 * Copyright (c) 2003, 2007-14 Matteo Frigo
Chris@42 3 * Copyright (c) 2003, 2007-14 Massachusetts Institute of Technology
Chris@42 4 *
Chris@42 5 * This program is free software; you can redistribute it and/or modify
Chris@42 6 * it under the terms of the GNU General Public License as published by
Chris@42 7 * the Free Software Foundation; either version 2 of the License, or
Chris@42 8 * (at your option) any later version.
Chris@42 9 *
Chris@42 10 * This program is distributed in the hope that it will be useful,
Chris@42 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@42 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@42 13 * GNU General Public License for more details.
Chris@42 14 *
Chris@42 15 * You should have received a copy of the GNU General Public License
Chris@42 16 * along with this program; if not, write to the Free Software
Chris@42 17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Chris@42 18 *
Chris@42 19 */
Chris@42 20
Chris@42 21 #include <string.h>
Chris@42 22 #include <stdio.h>
Chris@42 23
Chris@42 24 #include "config.h"
Chris@42 25 #include "my-getopt.h"
Chris@42 26
Chris@42 27 int my_optind = 1;
Chris@42 28 const char *my_optarg = 0;
Chris@42 29 static const char *scan_pointer = 0;
Chris@42 30
Chris@42 31 void my_usage(const char *progname, const struct my_option *opt)
Chris@42 32 {
Chris@42 33 int i;
Chris@42 34 size_t col = 0;
Chris@42 35
Chris@42 36 fprintf(stdout, "Usage: %s", progname);
Chris@42 37 col += (strlen(progname) + 7);
Chris@42 38 for (i = 0; opt[i].long_name; i++) {
Chris@42 39 size_t option_len;
Chris@42 40
Chris@42 41 option_len = strlen(opt[i].long_name);
Chris@42 42 if (col >= 80 - (option_len + 16)) {
Chris@42 43 fputs("\n\t", stdout);
Chris@42 44 col = 8;
Chris@42 45 }
Chris@42 46 fprintf(stdout, " [--%s", opt[i].long_name);
Chris@42 47 col += (option_len + 4);
Chris@42 48 if (opt[i].short_name < 128) {
Chris@42 49 fprintf(stdout, " | -%c", opt[i].short_name);
Chris@42 50 col += 5;
Chris@42 51 }
Chris@42 52 switch (opt[i].argtype) {
Chris@42 53 case REQARG:
Chris@42 54 fputs(" arg]", stdout);
Chris@42 55 col += 5;
Chris@42 56 break;
Chris@42 57 case OPTARG:
Chris@42 58 fputs(" [arg]]", stdout);
Chris@42 59 col += 10;
Chris@42 60 break;
Chris@42 61 default:
Chris@42 62 fputs("]", stdout);
Chris@42 63 col++;
Chris@42 64 }
Chris@42 65 }
Chris@42 66
Chris@42 67 fputs ("\n", stdout);
Chris@42 68 }
Chris@42 69
Chris@42 70 int my_getopt(int argc, char *argv[], const struct my_option *optarray)
Chris@42 71 {
Chris@42 72 const char *p;
Chris@42 73 const struct my_option *l;
Chris@42 74
Chris@42 75 if (scan_pointer && *scan_pointer) {
Chris@42 76 /* continue a previously scanned argv[] element */
Chris@42 77 p = scan_pointer;
Chris@42 78 goto short_option;
Chris@42 79 } else {
Chris@42 80 /* new argv[] element */
Chris@42 81 if (my_optind >= argc)
Chris@42 82 return -1; /* no more options */
Chris@42 83
Chris@42 84 p = argv[my_optind];
Chris@42 85
Chris@42 86 if (*p++ != '-')
Chris@42 87 return (-1); /* not an option */
Chris@42 88
Chris@42 89 if (!*p)
Chris@42 90 return (-1); /* string is exactly '-' */
Chris@42 91
Chris@42 92 ++my_optind;
Chris@42 93 }
Chris@42 94
Chris@42 95 if (*p == '-') {
Chris@42 96 /* long option */
Chris@42 97 scan_pointer = 0;
Chris@42 98 my_optarg = 0;
Chris@42 99
Chris@42 100 ++p;
Chris@42 101
Chris@42 102 for (l = optarray; l->short_name; ++l) {
Chris@42 103 size_t len = strlen(l->long_name);
Chris@42 104 if (!strncmp(l->long_name, p, len) &&
Chris@42 105 (!p[len] || p[len] == '=')) {
Chris@42 106 switch (l->argtype) {
Chris@42 107 case NOARG:
Chris@42 108 goto ok;
Chris@42 109 case OPTARG:
Chris@42 110 if (p[len] == '=')
Chris@42 111 my_optarg = p + len + 1;
Chris@42 112 goto ok;
Chris@42 113 case REQARG:
Chris@42 114 if (p[len] == '=') {
Chris@42 115 my_optarg = p + len + 1;
Chris@42 116 goto ok;
Chris@42 117 }
Chris@42 118 if (my_optind >= argc) {
Chris@42 119 fprintf(stderr,
Chris@42 120 "option --%s requires an argument\n",
Chris@42 121 l->long_name);
Chris@42 122 return '?';
Chris@42 123 }
Chris@42 124 my_optarg = argv[my_optind];
Chris@42 125 ++my_optind;
Chris@42 126 goto ok;
Chris@42 127 }
Chris@42 128 }
Chris@42 129 }
Chris@42 130 } else {
Chris@42 131 short_option:
Chris@42 132 scan_pointer = 0;
Chris@42 133 my_optarg = 0;
Chris@42 134
Chris@42 135 for (l = optarray; l->short_name; ++l) {
Chris@42 136 if (l->short_name == (char)l->short_name &&
Chris@42 137 *p == l->short_name) {
Chris@42 138 ++p;
Chris@42 139 switch (l->argtype) {
Chris@42 140 case NOARG:
Chris@42 141 scan_pointer = p;
Chris@42 142 goto ok;
Chris@42 143 case OPTARG:
Chris@42 144 if (*p)
Chris@42 145 my_optarg = p;
Chris@42 146 goto ok;
Chris@42 147 case REQARG:
Chris@42 148 if (*p) {
Chris@42 149 my_optarg = p;
Chris@42 150 } else {
Chris@42 151 if (my_optind >= argc) {
Chris@42 152 fprintf(stderr,
Chris@42 153 "option -%c requires an argument\n",
Chris@42 154 l->short_name);
Chris@42 155 return '?';
Chris@42 156 }
Chris@42 157 my_optarg = argv[my_optind];
Chris@42 158 ++my_optind;
Chris@42 159 }
Chris@42 160 goto ok;
Chris@42 161 }
Chris@42 162 }
Chris@42 163 }
Chris@42 164 }
Chris@42 165
Chris@42 166 fprintf(stderr, "unrecognized option %s\n", argv[my_optind - 1]);
Chris@42 167 return '?';
Chris@42 168
Chris@42 169 ok:
Chris@42 170 return l->short_name;
Chris@42 171 }
Chris@42 172