xref: /freebsd/lib/libc/stdlib/getsubopt.3 (revision f53355131f65d64e7643d734dbcd4fb2a5de20ed)
1.\" Copyright (c) 1990, 1991, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\" 3. Neither the name of the University nor the names of its contributors
13.\"    may be used to endorse or promote products derived from this software
14.\"    without specific prior written permission.
15.\"
16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26.\" SUCH DAMAGE.
27.\"
28.\"     @(#)getsubopt.3	8.1 (Berkeley) 6/9/93
29.\"
30.Dd December 25, 2011
31.Dt GETSUBOPT 3
32.Os
33.Sh NAME
34.Nm getsubopt
35.Nd get sub options from an argument
36.Sh LIBRARY
37.Lb libc
38.Sh SYNOPSIS
39.In stdlib.h
40.Vt extern char *suboptarg ;
41.Ft int
42.Fn getsubopt "char **optionp" "char * const *tokens" "char **valuep"
43.Sh DESCRIPTION
44The
45.Fn getsubopt
46function
47parses a string containing tokens delimited by one or more tab, space or
48comma
49.Pq Ql \&,
50characters.
51It is intended for use in parsing groups of option arguments provided
52as part of a utility command line.
53.Pp
54The argument
55.Fa optionp
56is a pointer to a pointer to the string.
57The argument
58.Fa tokens
59is a pointer to a
60.Dv NULL Ns -terminated
61array of pointers to strings.
62.Pp
63The
64.Fn getsubopt
65function
66returns the zero-based offset of the pointer in the
67.Fa tokens
68array referencing a string which matches the first token
69in the string, or, \-1 if the string contains no tokens or
70.Fa tokens
71does not contain a matching string.
72.Pp
73If the token is of the form ``name=value'', the location referenced by
74.Fa valuep
75will be set to point to the start of the ``value'' portion of the token.
76.Pp
77On return from
78.Fn getsubopt ,
79.Fa optionp
80will be set to point to the start of the next token in the string,
81or the null at the end of the string if no more tokens are present.
82The external variable
83.Fa suboptarg
84will be set to point to the start of the current token, or
85.Dv NULL
86if no
87tokens were present.
88The argument
89.Fa valuep
90will be set to point to the ``value'' portion of the token, or
91.Dv NULL
92if no ``value'' portion was present.
93.Sh EXAMPLES
94.Bd -literal -compact
95char *tokens[] = {
96	#define	ONE	0
97		"one",
98	#define	TWO	1
99		"two",
100	NULL
101};
102
103\&...
104
105extern char *optarg, *suboptarg;
106char *options, *value;
107
108while ((ch = getopt(argc, argv, "ab:")) != \-1) {
109	switch(ch) {
110	case 'a':
111		/* process ``a'' option */
112		break;
113	case 'b':
114		options = optarg;
115		while (*options) {
116			switch(getsubopt(&options, tokens, &value)) {
117			case ONE:
118				/* process ``one'' sub option */
119				break;
120			case TWO:
121				/* process ``two'' sub option */
122				if (!value)
123					error("no value for two");
124				i = atoi(value);
125				break;
126			case \-1:
127				if (suboptarg)
128					error("illegal sub option %s",
129					  suboptarg);
130				else
131					error("missing sub option");
132				break;
133			}
134		}
135		break;
136	}
137}
138.Ed
139.Sh SEE ALSO
140.Xr getopt 3 ,
141.Xr strsep 3
142.Sh HISTORY
143The
144.Fn getsubopt
145function first appeared in
146.Bx 4.4 .
147