xref: /original-bsd/lib/libc/stdlib/getsubopt.3 (revision 730ff649)
1.\" Copyright (c) 1990, 1991 The Regents of the University of California.
2.\" All rights reserved.
3.\"
4.\" %sccs.include.redist.man%
5.\"
6.\"     @(#)getsubopt.3	5.7 (Berkeley) 03/02/93
7.\"
8.Dd
9.Dt GETSUBOPT 3
10.Os
11.Sh NAME
12.Nm getsubopt
13.Nd get sub options from an argument
14.Sh SYNOPSIS
15.Fd #include <stdlib.h>
16.Vt extern char *suboptarg
17.Ft int
18.Fn getsubopt "char **optionp" "char * const *tokens" "char **valuep"
19.Sh DESCRIPTION
20The
21.Fn getsubopt
22function
23parses a string containing tokens delimited by one or more tab, space or
24comma
25.Pq Ql \&,
26characters.
27It is intended for use in parsing groups of option arguments provided
28as part of a utility command line.
29.Pp
30The argument
31.Fa optionp
32is a pointer to a pointer to the string.
33The argument
34.Fa tokens
35is a pointer to a
36.Dv NULL Ns -terminated
37array of pointers to strings.
38.Pp
39The
40.Fn getsubopt
41function
42returns the zero-based offset of the pointer in the
43.Fa tokens
44array referencing a string which matches the first token
45in the string, or, \-1 if the string contains no tokens or
46.Fa tokens
47does not contain a matching string.
48.Pp
49If the token is of the form ``name=value'', the location referenced by
50.Fa valuep
51will be set to point to the start of the ``value'' portion of the token.
52.Pp
53On return from
54.Fn getsubopt ,
55.Fa optionp
56will be set to point to the start of the next token in the string,
57or the null at the end of the string if no more tokens are present.
58The external variable
59.Fa suboptarg
60will be set to point to the start of the current token, or
61.Dv NULL
62if no
63tokens were present.
64The argument
65.Fa valuep
66will be set to point to the ``value'' portion of the token, or
67.Dv NULL
68if no ``value'' portion was present.
69.Sh EXAMPLE
70.Bd -literal -compact
71char *tokens[] = {
72	#define	ONE	0
73		"one",
74	#define	TWO	1
75		"two",
76	NULL
77};
78
79\&...
80
81extern char *optarg, *suboptarg;
82char *options, *value;
83
84while ((ch = getopt(argc, argv, "ab:")) != \-1) {
85	switch(ch) {
86	case 'a':
87		/* process ``a'' option */
88		break;
89	case 'b':
90		options = optarg;
91		while (*options) {
92			switch(getsubopt(&options, tokens, &value)) {
93			case ONE:
94				/* process ``one'' sub option */
95				break;
96			case TWO:
97				/* process ``two'' sub option */
98				if (!value)
99					error("no value for two");
100				i = atoi(value);
101				break;
102			case \-1:
103				if (suboptarg)
104					error("illegal sub option %s",
105					  suboptarg);
106				else
107					error("missing sub option");
108				break;
109		}
110		break;
111	}
112.Ed
113.Sh SEE ALSO
114.Xr getopt 3 ,
115.Xr strsep 3
116.Sh HISTORY
117The
118.Fn getsubopt
119function is
120.Ud .
121