-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgetopt.c
executable file
·224 lines (216 loc) · 6.98 KB
/
getopt.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
* getopt - POSIX like getopt for Windows console Application
*
* win-c - Windows Console Library
* Copyright (c) 2015 Koji Takami
* Released under the MIT license
* https://github.com/takamin/win-c/blob/master/LICENSE
*/
#include <stdio.h>
#include <string.h>
#include "getopt.h"
char* optarg = 0;
int optind = 1;
int opterr = 1;
int optopt = 0;
int postpone_count = 0;
int nextchar = 0;
static void postpone(int argc, char* const argv[], int index) {
char** nc_argv = (char**)argv;
char* p = nc_argv[index];
int j = index;
for(; j < argc - 1; j++) {
nc_argv[j] = nc_argv[j + 1];
}
nc_argv[argc - 1] = p;
}
static int postpone_noopt(int argc, char* const argv[], int index) {
int i = index;
for(; i < argc; i++) {
if(*(argv[i]) == '-') {
postpone(argc, argv, index);
return 1;
}
}
return 0;
}
static int _getopt_(int argc, char* const argv[],
const char* optstring,
const struct option* longopts, int* longindex)
{
while(1) {
int c;
const char* optptr = 0;
if(optind >= argc - postpone_count) {
c = 0;
optarg = 0;
break;
}
c = *(argv[optind] + nextchar);
if(c == '\0') {
nextchar = 0;
++optind;
continue;
}
if(nextchar == 0) {
if(optstring[0] != '+' && optstring[0] != '-') {
while(c != '-') {
/* postpone non-opt parameter */
if(!postpone_noopt(argc, argv, optind)) {
break; /* all args are non-opt param */
}
++postpone_count;
c = *argv[optind];
}
}
if(c != '-') {
if(optstring[0] == '-') {
optarg = argv[optind];
nextchar = 0;
++optind;
return 1;
}
break;
} else {
if(strcmp(argv[optind], "--") == 0) {
optind++;
break;
}
++nextchar;
if(longopts != 0 && *(argv[optind] + 1) == '-') {
char const* spec_long = argv[optind] + 2;
char const* pos_eq = strchr(spec_long, '=');
int spec_len = (pos_eq == NULL ? strlen(spec_long) : pos_eq - spec_long);
int index_search = 0;
int index_found = -1;
const struct option* optdef = 0;
while(longopts->name != 0) {
if(strncmp(spec_long, longopts->name, spec_len) == 0) {
if(optdef != 0) {
if(opterr) {
fprintf(stderr, "ambiguous option: %s\n", spec_long);
}
return '?';
}
optdef = longopts;
index_found = index_search;
}
longopts++;
index_search++;
}
if(optdef == 0) {
if(opterr) {
fprintf(stderr, "no such a option: %s\n", spec_long);
}
return '?';
}
switch(optdef->has_arg) {
case no_argument:
optarg = 0;
if(pos_eq != 0) {
if(opterr) {
fprintf(stderr, "no argument for %s\n", optdef->name);
}
return '?';
}
break;
case required_argument:
if(pos_eq == NULL) {
++optind;
optarg = argv[optind];
} else {
optarg = (char*)pos_eq + 1;
}
break;
}
++optind;
nextchar = 0;
if(longindex != 0) {
*longindex = index_found;
}
if(optdef->flag != 0) {
*optdef->flag = optdef->val;
return 0;
}
return optdef->val;
}
continue;
}
}
optptr = strchr(optstring, c);
if(optptr == NULL) {
optopt = c;
if(opterr) {
fprintf(stderr,
"%s: invalid option -- %c\n",
argv[0], c);
}
++nextchar;
return '?';
}
if(*(optptr+1) != ':') {
nextchar++;
if(*(argv[optind] + nextchar) == '\0') {
++optind;
nextchar = 0;
}
optarg = 0;
} else {
nextchar++;
if(*(argv[optind] + nextchar) != '\0') {
optarg = argv[optind] + nextchar;
} else {
++optind;
if(optind < argc - postpone_count) {
optarg = argv[optind];
} else {
optopt = c;
if(opterr) {
fprintf(stderr,
"%s: option requires an argument -- %c\n",
argv[0], c);
}
if(optstring[0] == ':' ||
(optstring[0] == '-' || optstring[0] == '+') &&
optstring[1] == ':')
{
c = ':';
} else {
c = '?';
}
}
}
++optind;
nextchar = 0;
}
return c;
}
/* end of option analysis */
/* fix the order of non-opt params to original */
while((argc - optind - postpone_count) > 0) {
postpone(argc, argv, optind);
++postpone_count;
}
nextchar = 0;
postpone_count = 0;
return -1;
}
int getopt(int argc, char* const argv[],
const char* optstring)
{
return _getopt_(argc, argv, optstring, 0, 0);
}
int getopt_long(int argc, char* const argv[],
const char* optstring,
const struct option* longopts, int* longindex)
{
return _getopt_(argc, argv, optstring, longopts, longindex);
}
/********************************************************
int getopt_long_only(int argc, char* const argv[],
const char* optstring,
const struct option* longopts, int* longindex)
{
return -1;
}
********************************************************/