4223
|
1 #include "opt.h"
|
|
2
|
|
3 #include <string.h>
|
|
4
|
|
5 int opt_ind;
|
|
6 int opt_err;
|
|
7 const char *opt_arg;
|
|
8 static const char *nextchar;
|
|
9
|
|
10 int opt_get(int argc, char *const *argv, const char *opts) {
|
|
11 const char *p;
|
|
12
|
|
13 if (!nextchar || !*nextchar) {
|
|
14 if (opt_ind < argc)
|
|
15 ++opt_ind;
|
|
16 if (opt_ind >= argc || argv[opt_ind][0] != '-' || !argv[opt_ind][1]) {
|
|
17 return -1;
|
|
18 }
|
|
19 if (argv[opt_ind][1] == '-' && argv[opt_ind][2] == '\0') {
|
|
20 ++opt_ind;
|
|
21 return -1;
|
|
22 }
|
|
23 nextchar = argv[opt_ind] + 1;
|
|
24 }
|
|
25
|
|
26 if ((p = strchr(opts, *nextchar)) && (*p != ':' || p == opts)) {
|
|
27 if (p[1] == ':') {
|
|
28 if (nextchar[1]) {
|
|
29 opt_arg = nextchar + 1;
|
|
30 } else if (opt_ind + 1 >= argc) {
|
|
31 opt_arg = NULL;
|
|
32 } else {
|
|
33 opt_arg = argv[++opt_ind];
|
|
34 }
|
|
35 nextchar = NULL;
|
|
36 } else {
|
|
37 ++nextchar;
|
|
38 }
|
|
39 return p[0];
|
|
40 }
|
|
41
|
|
42 opt_err = *nextchar++;
|
|
43 return '\0';
|
|
44 }
|
|
45
|
|
46 #if 0
|
|
47 void opt_reset(void) {
|
|
48 opt_ind = 0;
|
|
49 nextchar = NULL;
|
|
50 }
|
|
51 #endif
|