Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/lib/libast/comp/getoptl.c
1810 views
1
/***********************************************************************
2
* *
3
* This software is part of the ast package *
4
* Copyright (c) 1985-2011 AT&T Intellectual Property *
5
* and is licensed under the *
6
* Eclipse Public License, Version 1.0 *
7
* by AT&T Intellectual Property *
8
* *
9
* A copy of the License is available at *
10
* http://www.eclipse.org/org/documents/epl-v10.html *
11
* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
12
* *
13
* Information and Software Systems Research *
14
* AT&T Research *
15
* Florham Park NJ *
16
* *
17
* Glenn Fowler <[email protected]> *
18
* David Korn <[email protected]> *
19
* Phong Vo <[email protected]> *
20
* *
21
***********************************************************************/
22
#pragma prototyped
23
24
#include <ast.h>
25
#include <ast_getopt.h>
26
27
#undef _BLD_ast /* enable ast imports since we're user static */
28
29
#include <error.h>
30
#include <option.h>
31
#include <getopt.h>
32
#include <ctype.h>
33
34
static const char* lastoptstring;
35
static const struct option* lastlongopts;
36
static char* usage;
37
static Sfio_t* up;
38
39
static int lastoptind;
40
41
static int
42
golly(int argc, char* const* argv, const char* optstring, const struct option* longopts, int* longindex, int flags)
43
{
44
register char* s;
45
register const struct option* o;
46
register int c;
47
char* t;
48
49
if (!up || optstring != lastoptstring || longopts != lastlongopts)
50
{
51
if (!up && !(up = sfstropen()) || !(t = strdup(optstring)))
52
return -1;
53
sfprintf(up, "[-1p%d]", flags);
54
for (o = longopts; o->name; o++)
55
{
56
if (o->flag || o->val <= 0 || o->val > UCHAR_MAX || !isalnum(o->val))
57
sfprintf(up, "\n[%d:%s]", UCHAR_MAX + 1 + (o - longopts), o->name);
58
else
59
{
60
sfprintf(up, "\n[%c:%s]", o->val, o->name);
61
if (s = strchr(t, o->val))
62
{
63
*s++ = ' ';
64
if (*s == ':')
65
{
66
*s++ = ' ';
67
if (*s == ':')
68
*s = ' ';
69
}
70
}
71
}
72
if (o->has_arg)
73
{
74
sfputc(up, ':');
75
if (o->has_arg == optional_argument)
76
sfputc(up, '?');
77
sfprintf(up, "[string]");
78
}
79
}
80
s = t;
81
while (c = *s++)
82
if (c != ' ')
83
{
84
sfprintf(up, "\n[%c]", c);
85
if (*s == ':')
86
{
87
sfputc(up, *s);
88
if (*++s == ':')
89
{
90
sfputc(up, '?');
91
s++;
92
}
93
sfputc(up, '[');
94
sfputc(up, ']');
95
}
96
}
97
sfputc(up, '\n');
98
free(t);
99
if (!(usage = sfstruse(up)))
100
return -1;
101
lastoptstring = optstring;
102
lastlongopts = longopts;
103
}
104
opt_info.index = (optind > 1 || optind == lastoptind) ? optind : 0;
105
if (opt_info.index >= argc || !(c = optget((char**)argv, usage)))
106
{
107
sfstrclose(up);
108
up = 0;
109
c = -1;
110
}
111
else
112
{
113
if (c == ':' || c == '?')
114
{
115
if (opterr && (!optstring || *optstring != ':'))
116
{
117
if (!error_info.id)
118
error_info.id = argv[0];
119
errormsg(NiL, c == '?' ? (ERROR_USAGE|4) : 2, "%s", opt_info.arg);
120
}
121
optopt = opt_info.option[1];
122
c = '?';
123
}
124
optarg = opt_info.arg;
125
if (c < 0)
126
{
127
o = longopts - c - UCHAR_MAX - 1;
128
if (o->flag)
129
{
130
*o->flag = o->val;
131
c = 0;
132
}
133
else
134
c = o->val;
135
}
136
}
137
lastoptind = optind = opt_info.index;
138
return c;
139
}
140
141
extern int
142
getopt_long(int argc, char* const* argv, const char* optstring, const struct option* longopts, int* longindex)
143
{
144
return golly(argc, argv, optstring, longopts, longindex, 2);
145
}
146
147
extern int
148
getopt_long_only(int argc, char* const* argv, const char* optstring, const struct option* longopts, int* longindex)
149
{
150
return golly(argc, argv, optstring, longopts, longindex, 1);
151
}
152
153