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