annotate src/fftw-3.3.8/libbench2/my-getopt.c @ 84:08ae793730bd

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