xref: /386bsd/usr/src/lib/libc/gen/getsubopt.3 (revision a2142627)
1.\" Copyright (c) 1990, 1991 The Regents of the University of California.
2.\" 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. All advertising materials mentioning features or use of this software
13.\"    must display the following acknowledgement:
14.\"	This product includes software developed by the University of
15.\"	California, Berkeley and its contributors.
16.\" 4. Neither the name of the University nor the names of its contributors
17.\"    may be used to endorse or promote products derived from this software
18.\"    without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\"     @(#)getsubopt.3	5.4 (Berkeley) 7/31/91
33.\"
34.Dd July 31, 1991
35.Dt GETSUBOPT 3
36.Os
37.Sh NAME
38.Nm getsubopt
39.Nd get sub options from an argument
40.Sh SYNOPSIS
41.Fd #include <stdio.h>
42.Ft int
43.Fn getsubopt "char **optionp" "char * const *tokens" "char **valuep"
44.Sh DESCRIPTION
45The
46.Fn getsubopt
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 EXAMPLE
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		break;
135	}
136.Ed
137.Sh SEE ALSO
138.Xr getopt 3 ,
139.Xr strsep 3
140.Sh HISTORY
141The
142.Fn getsubopt
143function is
144.Ud .
145