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