comparison src/fftw-3.3.3/libbench2/my-getopt.c @ 10:37bf6b4a2645

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