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
|
/***********************************************************************
* *
* This software is part of the ast package *
* Copyright (c) 1985-2010 AT&T Intellectual Property *
* and is licensed under the *
* Common Public License, Version 1.0 *
* by AT&T Intellectual Property *
* *
* A copy of the License is available at *
* http://www.opensource.org/licenses/cpl1.0.txt *
* (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
* *
* Information and Software Systems Research *
* AT&T Research *
* Florham Park NJ *
* *
* Glenn Fowler <gsf@research.att.com> *
* David Korn <dgk@research.att.com> *
* Phong Vo <kpv@research.att.com> *
* *
***********************************************************************/
#pragma prototyped
#include <ast.h>
#include <ast_getopt.h>
#undef _BLD_ast /* enable ast imports since we're user static */
#include <error.h>
#include <option.h>
#include <getopt.h>
#include <ctype.h>
static const char* lastoptstring;
static const struct option* lastlongopts;
static char* usage;
static Sfio_t* up;
static int lastoptind;
static int
golly(int argc, char* const* argv, const char* optstring, const struct option* longopts, int* longindex, int flags)
{
register char* s;
register const struct option* o;
register int c;
char* t;
if (!up || optstring != lastoptstring || longopts != lastlongopts)
{
if (!up && !(up = sfstropen()))
return -1;
sfprintf(up, "[-1p%d]", flags);
t = strdup(optstring);
for (o = longopts; o->name; o++)
{
if (o->flag || o->val <= 0 || o->val > UCHAR_MAX || !isalnum(o->val))
sfprintf(up, "\n[%d:%s]", UCHAR_MAX + 1 + (o - longopts), o->name);
else
{
sfprintf(up, "\n[%c:%s]", o->val, o->name);
if (s = strchr(t, o->val))
{
*s++ = ' ';
if (*s == ':')
{
*s++ = ' ';
if (*s == ':')
*s = ' ';
}
}
}
if (o->has_arg)
{
sfputc(up, ':');
if (o->has_arg == optional_argument)
sfputc(up, '?');
sfprintf(up, "[string]");
}
}
s = t;
while (c = *s++)
if (c != ' ')
{
sfprintf(up, "\n[%c]", c);
if (*s == ':')
{
sfputc(up, *s);
if (*++s == ':')
{
sfputc(up, '?');
s++;
}
sfputc(up, '[');
sfputc(up, ']');
}
}
sfputc(up, '\n');
if (!(usage = sfstruse(up)))
return -1;
lastoptstring = optstring;
lastlongopts = longopts;
}
opt_info.index = (optind > 1 || optind == lastoptind) ? optind : 0;
if (opt_info.index >= argc || !(c = optget((char**)argv, usage)))
{
sfstrclose(up);
up = 0;
c = -1;
}
else
{
if (c == ':' || c == '?')
{
if (opterr && (!optstring || *optstring != ':'))
{
if (!error_info.id)
error_info.id = argv[0];
errormsg(NiL, c == '?' ? (ERROR_USAGE|4) : 2, "%s", opt_info.arg);
}
optopt = opt_info.option[1];
c = '?';
}
optarg = opt_info.arg;
if (c < 0)
{
o = longopts - c - UCHAR_MAX - 1;
if (o->flag)
{
*o->flag = o->val;
c = 0;
}
else
c = o->val;
}
}
lastoptind = optind = opt_info.index;
return c;
}
extern int
getopt_long(int argc, char* const* argv, const char* optstring, const struct option* longopts, int* longindex)
{
return golly(argc, argv, optstring, longopts, longindex, 2);
}
extern int
getopt_long_only(int argc, char* const* argv, const char* optstring, const struct option* longopts, int* longindex)
{
return golly(argc, argv, optstring, longopts, longindex, 1);
}
|