summaryrefslogtreecommitdiffstats
path: root/upstream/archlinux/man3p/getsubopt.3p
blob: 69a0a916a83887110b7607978eba08bd9bfbf6b8 (plain)
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
'\" et
.TH GETSUBOPT "3P" 2017 "IEEE/The Open Group" "POSIX Programmer's Manual"
.\"
.SH PROLOG
This manual page is part of the POSIX Programmer's Manual.
The Linux implementation of this interface may differ (consult
the corresponding Linux manual page for details of Linux behavior),
or the interface may not be implemented on Linux.
.\"
.SH NAME
getsubopt
\(em parse suboption arguments from a string
.SH SYNOPSIS
.LP
.nf
#include <stdlib.h>
.P
int getsubopt(char **\fIoptionp\fP, char * const *\fIkeylistp\fP, char **\fIvaluep\fP);
.fi
.SH DESCRIPTION
The
\fIgetsubopt\fR()
function shall parse suboption arguments in a flag argument. Such
options often result from the use of
\fIgetopt\fR().
.P
The
\fIgetsubopt\fR()
argument
.IR optionp
is a pointer to a pointer to the option argument string. The suboption
arguments shall be separated by
<comma>
characters and each may consist of either a single token, or a token-value
pair separated by an
<equals-sign>.
.P
The
.IR keylistp
argument shall be a pointer to a vector of strings. The end of the
vector is identified by a null pointer. Each entry in the vector is one
of the possible tokens that might be found in *\fIoptionp\fP. Since
<comma>
characters delimit suboption arguments in
.IR optionp ,
they should not appear in any of the strings pointed to by
.IR keylistp .
Similarly, because an
<equals-sign>
separates a token from its value, the application should not include an
<equals-sign>
in any of the strings pointed to by
.IR keylistp .
The
\fIgetsubopt\fR()
function shall not modify the
.IR keylistp
vector.
.P
The
.IR valuep
argument is the address of a value string pointer.
.P
If a
<comma>
appears in
.IR optionp ,
it shall be interpreted as a suboption separator. After
<comma>
characters have been processed, if there are one or more
<equals-sign>
characters in a suboption string, the first
<equals-sign>
in any suboption string shall be interpreted as a separator between a
token and a value. Subsequent
<equals-sign>
characters in a suboption string shall be interpreted as part of the
value.
.P
If the string at *\fIoptionp\fP contains only one suboption argument
(equivalently, no
<comma>
characters),
\fIgetsubopt\fR()
shall update *\fIoptionp\fP to point to the null character at the end of
the string. Otherwise, it shall isolate the suboption argument by
replacing the
<comma>
separator with a null character, and shall update *\fIoptionp\fP to point
to the start of the next suboption argument. If the suboption argument
has an associated value (equivalently, contains an
<equals-sign>),
\fIgetsubopt\fR()
shall update *\fIvaluep\fP to point to the value's first character.
Otherwise, it shall set *\fIvaluep\fP to a null pointer. The calling
application may use this information to determine whether the presence
or absence of a value for the suboption is an error.
.P
Additionally, when
\fIgetsubopt\fR()
fails to match the suboption argument with a token in the
.IR keylistp
array, the calling application should decide if this is an error, or if
the unrecognized option should be processed in another way.
.SH "RETURN VALUE"
The
\fIgetsubopt\fR()
function shall return the index of the matched token string, or \-1
if no token strings were matched.
.SH ERRORS
No errors are defined.
.LP
.IR "The following sections are informative."
.SH EXAMPLES
.SS "Parsing Suboptions"
.P
The following example uses the
\fIgetsubopt\fR()
function to parse a
.IR value
argument in the
.IR optarg
external variable returned by a call to
\fIgetopt\fR().
.sp
.RS 4
.nf

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
.P
int do_all;
const char *type;
int read_size;
int write_size;
int read_only;
.P
enum
{
    RO_OPTION = 0,
    RW_OPTION,
    READ_SIZE_OPTION,
    WRITE_SIZE_OPTION
};
.P
const char *mount_opts[] =
{
    [RO_OPTION] = "ro",
    [RW_OPTION] = "rw",
    [READ_SIZE_OPTION] = "rsize",
    [WRITE_SIZE_OPTION] = "wsize",
    NULL
};
.P
int
main(int argc, char *argv[])
{
    char *subopts, *value;
    int opt;
.P
    while ((opt = getopt(argc, argv, "at:o:")) != -1)
        switch(opt)
            {
            case \(aqa\(aq:
                do_all = 1;
                break;
            case \(aqt\(aq:
                type = optarg;
                break;
            case \(aqo\(aq:
                subopts = optarg;
                while (*subopts != \(aq\0\(aq)
                {
                    char *saved = subopts;
                    switch(getsubopt(&subopts, (char **)mount_opts,
                        &value))
                    {
                    case RO_OPTION:
                        read_only = 1;
                        break;
                    case RW_OPTION:
                        read_only = 0;
                        break;
                    case READ_SIZE_OPTION:
                        if (value == NULL)
                            abort();
                        read_size = atoi(value);
                        break;
                    case WRITE_SIZE_OPTION:
                        if (value == NULL)
                            abort();
                        write_size = atoi(value);
                        break;
                    default:
                        /* Unknown suboption. */
                        printf("Unknown suboption `%s\(aq\en", saved);
                        abort();
                    }
                }
                break;
            default:
                abort();
            }
.P
    /* Do the real work. */
.P
    return 0;
}
.fi
.P
.RE
.P
If the above example is invoked with:
.sp
.RS 4
.nf

program -o ro,rsize=512
.fi
.P
.RE
.P
then after option parsing, the variable
.IR do_all
will be 0,
.IR type
will be a null pointer,
.IR read_size
will be 512,
.IR write_size
will be 0, and
.IR read_only
will be 1. If it is invoked with:
.sp
.RS 4
.nf

program -o oops
.fi
.P
.RE
.P
it will print:
.sp
.RS 4
.nf

"Unknown suboption `oops\(aq"
.fi
.P
.RE
.P
before aborting.
.SH "APPLICATION USAGE"
The value of *\fIvaluep\fR when
\fIgetsubopt\fR()
returns \-1 is unspecified. Historical implementations provide various
incompatible extensions to allow an application to access the suboption
text that was not found in the
.IR keylistp
array.
.SH RATIONALE
The
.IR keylistp
argument of
\fIgetsubopt\fR()
is typed as
.BR "char * const *"
to match historical practice. However, the standard is clear that
implementations will not modify either the array or the strings contained
in the array, as if the argument had been typed
.BR "const char * const *" .
.SH "FUTURE DIRECTIONS"
None.
.SH "SEE ALSO"
.IR "\fIgetopt\fR\^(\|)"
.P
The Base Definitions volume of POSIX.1\(hy2017,
.IR "\fB<stdlib.h>\fP"
.\"
.SH COPYRIGHT
Portions of this text are reprinted and reproduced in electronic form
from IEEE Std 1003.1-2017, Standard for Information Technology
-- Portable Operating System Interface (POSIX), The Open Group Base
Specifications Issue 7, 2018 Edition,
Copyright (C) 2018 by the Institute of
Electrical and Electronics Engineers, Inc and The Open Group.
In the event of any discrepancy between this version and the original IEEE and
The Open Group Standard, the original IEEE and The Open Group Standard
is the referee document. The original Standard can be obtained online at
http://www.opengroup.org/unix/online.html .
.PP
Any typographical or formatting errors that appear
in this page are most likely
to have been introduced during the conversion of the source files to
man page format. To report such errors, see
https://www.kernel.org/doc/man-pages/reporting_bugs.html .