Commit | Line | Data |
---|---|---|
dc0d77d7 CE |
1 | #define SUBGETOPTNOSHORT |
2 | #include "subgetopt.h" | |
3 | ||
4 | #define sgopt subgetopt | |
5 | #define optind subgetoptind | |
6 | #define optpos subgetoptpos | |
7 | #define optarg subgetoptarg | |
8 | #define optproblem subgetoptproblem | |
9 | #define optdone subgetoptdone | |
10 | ||
11 | int optind = 1; | |
12 | int optpos = 0; | |
13 | char *optarg = 0; | |
14 | int optproblem = 0; | |
15 | int optdone = SUBGETOPTDONE; | |
16 | ||
17 | int sgopt(int argc,char **argv,const char *opts) | |
18 | { | |
19 | int c; | |
20 | const char *s; | |
21 | ||
22 | optarg = 0; | |
23 | if (!argv || (optind >= argc) || !argv[optind]) return optdone; | |
24 | if (optpos && !argv[optind][optpos]) { | |
25 | ++optind; | |
26 | optpos = 0; | |
27 | if ((optind >= argc) || !argv[optind]) return optdone; | |
28 | } | |
29 | if (!optpos) { | |
30 | if (argv[optind][0] != '-') return optdone; | |
31 | ++optpos; | |
32 | c = argv[optind][1]; | |
33 | if ((c == '-') || (c == 0)) { | |
34 | if (c) ++optind; | |
35 | optpos = 0; | |
36 | return optdone; | |
37 | } | |
38 | /* otherwise c is reassigned below */ | |
39 | } | |
40 | c = argv[optind][optpos]; | |
41 | ++optpos; | |
42 | s = opts; | |
43 | while (*s) { | |
44 | if (c == *s) { | |
45 | if (s[1] == ':') { | |
46 | optarg = argv[optind] + optpos; | |
47 | ++optind; | |
48 | optpos = 0; | |
49 | if (!*optarg) { | |
50 | optarg = argv[optind]; | |
51 | if ((optind >= argc) || !optarg) { /* argument past end */ | |
52 | optproblem = c; | |
53 | return '?'; | |
54 | } | |
55 | ++optind; | |
56 | } | |
57 | } | |
58 | return c; | |
59 | } | |
60 | ++s; | |
61 | if (*s == ':') ++s; | |
62 | } | |
63 | optproblem = c; | |
64 | return '?'; | |
65 | } |