1 #include <unistd.h>
2 #include <wchar.h>
3 #include <string.h>
4 #include <limits.h>
5 #include <stdlib.h>
6 #include <stdio.h>
7
8 #include "config.h"
9
10 SOLV_API char *optarg;
11 SOLV_API int optind=1, opterr=1, optopt, __optpos, __optreset=0;
12
13 #define optpos __optpos
14
__getopt_msg(const char * a,const char * b,const char * c,size_t l)15 void __getopt_msg(const char *a, const char *b, const char *c, size_t l)
16 {
17 FILE *f = stderr;
18 fputs(a, f)>=0
19 && fwrite(b, strlen(b), 1, f)
20 && fwrite(c, 1, l, f)==l
21 && putc('\n', f);
22 }
23
getopt(int argc,char * const argv[],const char * optstring)24 int getopt(int argc, char * const argv[], const char *optstring)
25 {
26 int i;
27 wchar_t c, d;
28 int k, l;
29 char *optchar;
30
31 if (!optind || __optreset) {
32 __optreset = 0;
33 __optpos = 0;
34 optind = 1;
35 }
36
37 if (optind >= argc || !argv[optind])
38 return -1;
39
40 if (argv[optind][0] != '-') {
41 if (optstring[0] == '-') {
42 optarg = argv[optind++];
43 return 1;
44 }
45 return -1;
46 }
47
48 if (!argv[optind][1])
49 return -1;
50
51 if (argv[optind][1] == '-' && !argv[optind][2])
52 return optind++, -1;
53
54 if (!optpos) optpos++;
55 if ((k = mbtowc(&c, argv[optind]+optpos, MB_LEN_MAX)) < 0) {
56 k = 1;
57 c = 0xfffd; /* replacement char */
58 }
59 optchar = argv[optind]+optpos;
60 optpos += k;
61
62 if (!argv[optind][optpos]) {
63 optind++;
64 optpos = 0;
65 }
66
67 if (optstring[0] == '-' || optstring[0] == '+')
68 optstring++;
69
70 i = 0;
71 d = 0;
72 do {
73 l = mbtowc(&d, optstring+i, MB_LEN_MAX);
74 if (l>0) i+=l; else i++;
75 } while (l && d != c);
76
77 if (d != c || c == ':') {
78 optopt = c;
79 if (optstring[0] != ':' && opterr)
80 __getopt_msg(argv[0], ": unrecognized option: ", optchar, k);
81 return '?';
82 }
83 if (optstring[i] == ':') {
84 optarg = 0;
85 if (optstring[i+1] != ':' || optpos) {
86 optarg = argv[optind++] + optpos;
87 optpos = 0;
88 }
89 if (optind > argc) {
90 optopt = c;
91 if (optstring[0] == ':') return ':';
92 if (opterr) __getopt_msg(argv[0],
93 ": option requires an argument: ",
94 optchar, k);
95 return '?';
96 }
97 }
98 return c;
99 }
100