1GETSUBOPT(P)               POSIX Programmer's Manual              GETSUBOPT(P)
2
3
4

NAME

6       getsubopt - parse suboption arguments from a string
7

SYNOPSIS

9       #include <stdlib.h>
10
11       int getsubopt(char **optionp, char * const *keylistp, char **valuep);
12
13

DESCRIPTION

15       The  getsubopt()  function  shall  parse  suboption arguments in a flag
16       argument. Such options often result from the use of getopt().
17
18       The getsubopt() argument optionp is a  pointer  to  a  pointer  to  the
19       option  argument  string. The suboption arguments shall be separated by
20       commas and each may consist of either a single token, or a  token-value
21       pair separated by an equal sign.
22
23       The  keylistp  argument shall be a pointer to a vector of strings.  The
24       end of the vector is identified by a null pointer.  Each entry  in  the
25       vector  is  one of the possible tokens that might be found in *optionp.
26       Since commas delimit suboption arguments in optionp,  they  should  not
27       appear in any of the strings pointed to by keylistp. Similarly, because
28       an equal sign separates a token from its value, the application  should
29       not include an equal sign in any of the strings pointed to by keylistp.
30
31       The valuep argument is the address of a value string pointer.
32
33       If  a  comma appears in optionp, it shall be interpreted as a suboption
34       separator. After commas have been processed, if there are one  or  more
35       equal  signs  in a suboption string, the first equal sign in any subop‐
36       tion string shall be interpreted as a separator between a token  and  a
37       value.  Subsequent  equal  signs  in a suboption string shall be inter‐
38       preted as part of the value.
39
40       If the string at *optionp contains only one suboption argument (equiva‐
41       lently,  no  commas), getsubopt() shall update *optionp to point to the
42       null character at the end of the string.  Otherwise, it  shall  isolate
43       the  suboption  argument  by  replacing the comma separator with a null
44       character, and shall update *optionp to point to the start of the  next
45       suboption  argument.  If the suboption argument has an associated value
46       (equivalently, contains an equal sign), getsubopt() shall update  *val‐
47       uep  to  point  to the value's first character. Otherwise, it shall set
48       *valuep to a null pointer. The calling application may use this  infor‐
49       mation  to determine whether the presence or absence of a value for the
50       suboption is an error.
51
52       Additionally, when getsubopt() fails to match  the  suboption  argument
53       with  a  token  in  the  keylistp array, the calling application should
54       decide if this is an error, or if the  unrecognized  option  should  be
55       processed in another way.
56

RETURN VALUE

58       The  getsubopt()  function  shall return the index of the matched token
59       string, or -1 if no token strings were matched.
60

ERRORS

62       No errors are defined.
63
64       The following sections are informative.
65

EXAMPLES

67              #include <stdio.h>
68              #include <stdlib.h>
69
70
71              int do_all;
72              const char *type;
73              int read_size;
74              int write_size;
75              int read_only;
76
77
78              enum
79              {
80                  RO_OPTION = 0,
81                  RW_OPTION,
82                  READ_SIZE_OPTION,
83                  WRITE_SIZE_OPTION
84              };
85
86
87              const char *mount_opts[] =
88              {
89                  [RO_OPTION] = "ro",
90                  [RW_OPTION] = "rw",
91                  [READ_SIZE_OPTION] = "rsize",
92                  [WRITE_SIZE_OPTION] = "wsize",
93                  NULL
94              };
95
96
97              int
98              main(int argc, char *argv[])
99              {
100                  char *subopts, *value;
101                  int opt;
102
103
104                  while ((opt = getopt(argc, argv, "at:o:")) != -1)
105                      switch(opt)
106                          {
107                          case 'a':
108                              do_all = 1;
109                              break;
110                          case 't':
111                              type = optarg;
112                              break;
113                          case 'o':
114                              subopts = optarg;
115                              while (*subopts != '\0')
116                                  switch(getsubopt(&subopts, mount_opts, &value))
117                              {
118                              case RO_OPTION:
119                                  read_only = 1;
120                                  break;
121                              case RW_OPTION:
122                                  read_only = 0;
123                                  break;
124                              case READ_SIZE_OPTION:
125                                  if (value == NULL)
126                                      abort();
127                                  read_size = atoi(value);
128                                  break;
129                              case WRITE_SIZE_OPTION:
130                                  if (value == NULL)
131                                      abort();
132                                  write_size = atoi(value);
133                                  break;
134                              default:
135                                  /* Unknown suboption. */
136                                  printf("Unknown suboption `%s'\n", value);
137                                  break;
138                              }
139                          break;
140                      default:
141                          abort();
142                      }
143
144
145                  /* Do the real work. */
146
147
148                  return 0;
149              }
150
151   Parsing Suboptions
152       The following example uses the getsubopt() function to  parse  a  value
153       argument  in  the  optarg  external  variable  returned  by  a  call to
154       getopt().
155
156
157              #include <stdlib.h>
158              ...
159              char *tokens[] = {"HOME", "PATH", "LOGNAME", (char *) NULL };
160              char *value;
161              int opt, index;
162
163
164              while ((opt = getopt(argc, argv, "e:")) != -1) {
165                  switch(opt)  {
166                  case 'e' :
167                      while ((index = getsubopt(&optarg, tokens, &value)) != -1) {
168                          switch(index) {
169              ...
170                      }
171                      break;
172              ...
173                  }
174              }
175              ...
176

APPLICATION USAGE

178       None.
179

RATIONALE

181       None.
182

FUTURE DIRECTIONS

184       None.
185

SEE ALSO

187       getopt()  ,  the  Base  Definitions  volume  of   IEEE Std 1003.1-2001,
188       <stdlib.h>
189
191       Portions  of  this text are reprinted and reproduced in electronic form
192       from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
193       --  Portable  Operating  System  Interface (POSIX), The Open Group Base
194       Specifications Issue 6, Copyright (C) 2001-2003  by  the  Institute  of
195       Electrical  and  Electronics  Engineers, Inc and The Open Group. In the
196       event of any discrepancy between this version and the original IEEE and
197       The  Open Group Standard, the original IEEE and The Open Group Standard
198       is the referee document. The original Standard can be  obtained  online
199       at http://www.opengroup.org/unix/online.html .
200
201
202
203IEEE/The Open Group                  2003                         GETSUBOPT(P)
Impressum