Lines Matching refs:h
10 static void cmdopt_shift(cmdopt_t *h) { in cmdopt_shift() argument
14 tmp = h->argv[h->optind]; in cmdopt_shift()
15 for (i = h->optind; i < h->argc - 1; i++) { in cmdopt_shift()
16 h->argv[i] = h->argv[i + 1]; in cmdopt_shift()
18 h->argv[i] = tmp; in cmdopt_shift()
20 h->nextchar = NULL; in cmdopt_shift()
21 h->optnum--; in cmdopt_shift()
25 static void cmdopt_next(cmdopt_t *h) { in cmdopt_next() argument
26 h->optind++; in cmdopt_next()
27 h->nextchar = NULL; in cmdopt_next()
31 static int cmdopt_check(cmdopt_t *h) { in cmdopt_check() argument
33 const char *arg = h->argv[h->optind]; in cmdopt_check()
48 static void cmdopt_getopt(cmdopt_t *h) { in cmdopt_getopt() argument
50 if (*h->nextchar == '\0') { in cmdopt_getopt()
51 cmdopt_next(h); in cmdopt_getopt()
52 h->nextchar = h->argv[h->optind]; in cmdopt_getopt()
56 if (h->optind < h->optnum) { in cmdopt_getopt()
57 h->optarg = h->nextchar; in cmdopt_getopt()
58 cmdopt_next(h); in cmdopt_getopt()
60 h->optarg = NULL; in cmdopt_getopt()
65 static int cmdopt_search(cmdopt_t *h) { in cmdopt_search() argument
69 h->optopt = *h->nextchar++; in cmdopt_search()
71 for (ptr = h->optstring; *ptr != '\0'; ptr++) { in cmdopt_search()
72 if (*ptr == h->optopt) { in cmdopt_search()
75 cmdopt_getopt(h); in cmdopt_search()
78 if (h->optarg == NULL && ptr[2] != ':') { in cmdopt_search()
82 return h->optopt; in cmdopt_search()
86 if (h->optopt == '-') { in cmdopt_search()
87 cmdopt_next(h); in cmdopt_search()
88 while (h->optind < h->optnum) { in cmdopt_search()
89 cmdopt_shift(h); in cmdopt_search()
120 static int cmdopt_match(cmdopt_t *h) { in cmdopt_match() argument
125 if (h->longopts == NULL) { in cmdopt_match()
129 for (i = 0; h->longopts[i].name != NULL; i++) { in cmdopt_match()
130 len = cmdopt_match_len(h->longopts[i].name, h->nextchar); in cmdopt_match()
133 h->nextchar -= len; in cmdopt_match()
146 h->nextchar += max; in cmdopt_match()
151 static void cmdopt_getopt_long(cmdopt_t *h) { in cmdopt_getopt_long() argument
152 if (*h->nextchar == '=') { in cmdopt_getopt_long()
153 h->optarg = h->nextchar + 1; in cmdopt_getopt_long()
154 cmdopt_next(h); in cmdopt_getopt_long()
156 cmdopt_next(h); in cmdopt_getopt_long()
159 if (h->optind < h->optnum) { in cmdopt_getopt_long()
160 h->optarg = h->argv[h->optind]; in cmdopt_getopt_long()
161 cmdopt_next(h); in cmdopt_getopt_long()
163 h->optarg = NULL; in cmdopt_getopt_long()
169 static int cmdopt_search_long(cmdopt_t *h) { in cmdopt_search_long() argument
173 h->optlong = h->argv[h->optind]; in cmdopt_search_long()
176 h->longindex = cmdopt_match(h); in cmdopt_search_long()
177 if (h->longindex < 0) { in cmdopt_search_long()
178 cmdopt_next(h); in cmdopt_search_long()
183 option = h->longopts + h->longindex; in cmdopt_search_long()
185 cmdopt_getopt_long(h); in cmdopt_search_long()
188 if (h->optarg == NULL) { in cmdopt_search_long()
191 } else if (*h->nextchar == '=') { in cmdopt_search_long()
193 cmdopt_getopt_long(h); in cmdopt_search_long()
207 static int cmdopt_main(cmdopt_t *h) { in cmdopt_main() argument
211 h->optopt = 0; in cmdopt_main()
212 h->optlong = NULL; in cmdopt_main()
213 h->optarg = NULL; in cmdopt_main()
214 h->longindex = 0; in cmdopt_main()
216 while (h->optind < h->optnum) { in cmdopt_main()
217 if (h->nextchar == NULL) { in cmdopt_main()
219 type = cmdopt_check(h); in cmdopt_main()
221 cmdopt_shift(h); in cmdopt_main()
223 h->nextchar = h->argv[h->optind] + type; in cmdopt_main()
225 return cmdopt_search_long(h); in cmdopt_main()
229 if (*h->nextchar == '\0') { in cmdopt_main()
230 cmdopt_next(h); in cmdopt_main()
234 return cmdopt_search(h); in cmdopt_main()
242 void cmdopt_init(cmdopt_t *h, int argc, char **argv, in cmdopt_init() argument
246 h->argc = argc; in cmdopt_init()
247 h->argv = argv; in cmdopt_init()
248 h->optnum = h->argc; in cmdopt_init()
250 h->longopts = longopts; in cmdopt_init()
251 h->optstring = (optstring != NULL) ? optstring : empty_optstring; in cmdopt_init()
253 h->optind = 1; in cmdopt_init()
254 h->nextchar = NULL; in cmdopt_init()
255 h->optarg = NULL; in cmdopt_init()
256 h->optopt = 0; in cmdopt_init()
257 h->optlong = NULL; in cmdopt_init()
258 h->opterr = 1; in cmdopt_init()
259 h->longindex = 0; in cmdopt_init()
263 int cmdopt_get(cmdopt_t *h) { in cmdopt_get() argument
264 int value = cmdopt_main(h); in cmdopt_get()
267 if (h->opterr) { in cmdopt_get()
270 if (h->optlong == NULL) { in cmdopt_get()
271 fprintf(stderr, "option requires an argument -- %c\n", h->optopt); in cmdopt_get()
274 h->longopts[h->longindex].name); in cmdopt_get()
278 if (h->optlong == NULL) { in cmdopt_get()
279 fprintf(stderr, "invalid option -- %c\n", h->optopt); in cmdopt_get()
281 fprintf(stderr, "unrecognized option `%s'\n", h->optlong); in cmdopt_get()
283 } else if ((value != -1) && (h->opterr == 2)) { in cmdopt_get()
285 if (h->optlong == NULL) { in cmdopt_get()
286 fprintf(stderr, "option with `%s' -- %c\n", h->optarg, h->optopt); in cmdopt_get()
289 h->longopts[h->longindex].name, h->optarg); in cmdopt_get()