1GETSUBOPT(3P) POSIX Programmer's Manual GETSUBOPT(3P)
2
3
4
6 This manual page is part of the POSIX Programmer's Manual. The Linux
7 implementation of this interface may differ (consult the corresponding
8 Linux manual page for details of Linux behavior), or the interface may
9 not be implemented on Linux.
10
12 getsubopt - parse suboption arguments from a string
13
15 #include <stdlib.h>
16
17 int getsubopt(char **optionp, char * const *keylistp, char **valuep);
18
19
21 The getsubopt() function shall parse suboption arguments in a flag
22 argument. Such options often result from the use of getopt().
23
24 The getsubopt() argument optionp is a pointer to a pointer to the
25 option argument string. The suboption arguments shall be separated by
26 commas and each may consist of either a single token, or a token-value
27 pair separated by an equal sign.
28
29 The keylistp argument shall be a pointer to a vector of strings. The
30 end of the vector is identified by a null pointer. Each entry in the
31 vector is one of the possible tokens that might be found in *optionp.
32 Since commas delimit suboption arguments in optionp, they should not
33 appear in any of the strings pointed to by keylistp. Similarly, because
34 an equal sign separates a token from its value, the application should
35 not include an equal sign in any of the strings pointed to by keylistp.
36
37 The valuep argument is the address of a value string pointer.
38
39 If a comma appears in optionp, it shall be interpreted as a suboption
40 separator. After commas have been processed, if there are one or more
41 equal signs in a suboption string, the first equal sign in any subop‐
42 tion string shall be interpreted as a separator between a token and a
43 value. Subsequent equal signs in a suboption string shall be inter‐
44 preted as part of the value.
45
46 If the string at *optionp contains only one suboption argument (equiva‐
47 lently, no commas), getsubopt() shall update *optionp to point to the
48 null character at the end of the string. Otherwise, it shall isolate
49 the suboption argument by replacing the comma separator with a null
50 character, and shall update *optionp to point to the start of the next
51 suboption argument. If the suboption argument has an associated value
52 (equivalently, contains an equal sign), getsubopt() shall update *val‐
53 uep to point to the value's first character. Otherwise, it shall set
54 *valuep to a null pointer. The calling application may use this infor‐
55 mation to determine whether the presence or absence of a value for the
56 suboption is an error.
57
58 Additionally, when getsubopt() fails to match the suboption argument
59 with a token in the keylistp array, the calling application should
60 decide if this is an error, or if the unrecognized option should be
61 processed in another way.
62
64 The getsubopt() function shall return the index of the matched token
65 string, or -1 if no token strings were matched.
66
68 No errors are defined.
69
70 The following sections are informative.
71
73 #include <stdio.h>
74 #include <stdlib.h>
75
76
77 int do_all;
78 const char *type;
79 int read_size;
80 int write_size;
81 int read_only;
82
83
84 enum
85 {
86 RO_OPTION = 0,
87 RW_OPTION,
88 READ_SIZE_OPTION,
89 WRITE_SIZE_OPTION
90 };
91
92
93 const char *mount_opts[] =
94 {
95 [RO_OPTION] = "ro",
96 [RW_OPTION] = "rw",
97 [READ_SIZE_OPTION] = "rsize",
98 [WRITE_SIZE_OPTION] = "wsize",
99 NULL
100 };
101
102
103 int
104 main(int argc, char *argv[])
105 {
106 char *subopts, *value;
107 int opt;
108
109
110 while ((opt = getopt(argc, argv, "at:o:")) != -1)
111 switch(opt)
112 {
113 case 'a':
114 do_all = 1;
115 break;
116 case 't':
117 type = optarg;
118 break;
119 case 'o':
120 subopts = optarg;
121 while (*subopts != '\0')
122 switch(getsubopt(&subopts, mount_opts, &value))
123 {
124 case RO_OPTION:
125 read_only = 1;
126 break;
127 case RW_OPTION:
128 read_only = 0;
129 break;
130 case READ_SIZE_OPTION:
131 if (value == NULL)
132 abort();
133 read_size = atoi(value);
134 break;
135 case WRITE_SIZE_OPTION:
136 if (value == NULL)
137 abort();
138 write_size = atoi(value);
139 break;
140 default:
141 /* Unknown suboption. */
142 printf("Unknown suboption `%s'\n", value);
143 break;
144 }
145 break;
146 default:
147 abort();
148 }
149
150
151 /* Do the real work. */
152
153
154 return 0;
155 }
156
157 Parsing Suboptions
158 The following example uses the getsubopt() function to parse a value
159 argument in the optarg external variable returned by a call to
160 getopt().
161
162
163 #include <stdlib.h>
164 ...
165 char *tokens[] = {"HOME", "PATH", "LOGNAME", (char *) NULL };
166 char *value;
167 int opt, index;
168
169
170 while ((opt = getopt(argc, argv, "e:")) != -1) {
171 switch(opt) {
172 case 'e' :
173 while ((index = getsubopt(&optarg, tokens, &value)) != -1) {
174 switch(index) {
175 ...
176 }
177 break;
178 ...
179 }
180 }
181 ...
182
184 None.
185
187 None.
188
190 None.
191
193 getopt(), the Base Definitions volume of IEEE Std 1003.1-2001,
194 <stdlib.h>
195
197 Portions of this text are reprinted and reproduced in electronic form
198 from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
199 -- Portable Operating System Interface (POSIX), The Open Group Base
200 Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of
201 Electrical and Electronics Engineers, Inc and The Open Group. In the
202 event of any discrepancy between this version and the original IEEE and
203 The Open Group Standard, the original IEEE and The Open Group Standard
204 is the referee document. The original Standard can be obtained online
205 at http://www.opengroup.org/unix/online.html .
206
207
208
209IEEE/The Open Group 2003 GETSUBOPT(3P)