xref: /original-bsd/lib/libc/stdlib/getsubopt.3 (revision 42c7e7a1)
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.6 (Berkeley) 11/14/91
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 <stdio.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
22parses a string containing tokens delimited by one or more tab, space or
23comma
24.Pq Ql \&,
25characters.
26It is intended for use in parsing groups of option arguments provided
27as part of a utility command line.
28.Pp
29The argument
30.Fa optionp
31is a pointer to a pointer to the string.
32The argument
33.Fa tokens
34is a pointer to a
35.Dv NULL Ns -terminated
36array of pointers to strings.
37.Pp
38The
39.Fn getsubopt
40function
41returns the zero-based offset of the pointer in the
42.Fa tokens
43array referencing a string which matches the first token
44in the string, or, \-1 if the string contains no tokens or
45.Fa tokens
46does not contain a matching string.
47.Pp
48If the token is of the form ``name=value'', the location referenced by
49.Fa valuep
50will be set to point to the start of the ``value'' portion of the token.
51.Pp
52On return from
53.Fn getsubopt ,
54.Fa optionp
55will be set to point to the start of the next token in the string,
56or the null at the end of the string if no more tokens are present.
57The external variable
58.Fa suboptarg
59will be set to point to the start of the current token, or
60.Dv NULL
61if no
62tokens were present.
63The argument
64.Fa valuep
65will be set to point to the ``value'' portion of the token, or
66.Dv NULL
67if no ``value'' portion was present.
68.Sh EXAMPLE
69.Bd -literal -compact
70char *tokens[] = {
71	#define	ONE	0
72		"one",
73	#define	TWO	1
74		"two",
75	NULL
76};
77
78\&...
79
80extern char *optarg, *suboptarg;
81char *options, *value;
82
83while ((ch = getopt(argc, argv, "ab:")) != \-1) {
84	switch(ch) {
85	case 'a':
86		/* process ``a'' option */
87		break;
88	case 'b':
89		options = optarg;
90		while (*options) {
91			switch(getsubopt(&options, tokens, &value)) {
92			case ONE:
93				/* process ``one'' sub option */
94				break;
95			case TWO:
96				/* process ``two'' sub option */
97				if (!value)
98					error("no value for two");
99				i = atoi(value);
100				break;
101			case \-1:
102				if (suboptarg)
103					error("illegal sub option %s",
104					  suboptarg);
105				else
106					error("missing sub option");
107				break;
108			}
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