xref: /csrg-svn/lib/libc/stdlib/getsubopt.3 (revision 48352)
1*48352Scael.\" Copyright (c) 1990, 1991 The Regents of the University of California.
245446Sbostic.\" All rights reserved.
345446Sbostic.\"
445446Sbostic.\" %sccs.include.redist.man%
545446Sbostic.\"
6*48352Scael.\"     @(#)getsubopt.3	5.3 (Berkeley) 04/19/91
745446Sbostic.\"
8*48352Scael.Dd
9*48352Scael.Dt GETSUBOPT 3
10*48352Scael.Os
11*48352Scael.Sh NAME
12*48352Scael.Nm getsubopt
13*48352Scael.Nd get sub options from an argument
14*48352Scael.Sh SYNOPSIS
15*48352Scael.Fd #include <stdio.h>
16*48352Scael.Ft int
17*48352Scael.Fn getsubopt "char **optionp" "char * const *tokens" "char **valuep"
18*48352Scael.Sh DESCRIPTION
19*48352ScaelThe
20*48352Scael.Fn getsubopt
2145446Sbosticparses a string containing tokens delimited by one or more tab, space or
22*48352Scaelcomma
23*48352Scael.Pq Ql \&,
24*48352Scaelcharacters.
2545446SbosticIt is intended for use in parsing groups of option arguments provided
2645446Sbosticas part of a utility command line.
27*48352Scael.Pp
28*48352ScaelThe argument
29*48352Scael.Fa optionp
3045446Sbosticis a pointer to a pointer to the string.
31*48352ScaelThe argument
32*48352Scael.Fa tokens
33*48352Scaelis a pointer to a
34*48352Scael.Dv NULL Ns -terminated
35*48352Scaelarray of pointers to strings.
36*48352Scael.Pp
37*48352ScaelThe
38*48352Scael.Fn getsubopt
39*48352Scaelfunction
4045446Sbosticreturns the zero-based offset of the pointer in the
41*48352Scael.Fa tokens
4245446Sbosticarray referencing a string which matches the first token
43*48352Scaelin the string, or, \-1 if the string contains no tokens or
44*48352Scael.Fa tokens
4545446Sbosticdoes not contain a matching string.
46*48352Scael.Pp
4745446SbosticIf the token is of the form ``name=value'', the location referenced by
48*48352Scael.Fa valuep
4945446Sbosticwill be set to point to the start of the ``value'' portion of the token.
50*48352Scael.Pp
5145446SbosticOn return from
52*48352Scael.Fn getsubopt ,
53*48352Scael.Fa optionp
5445446Sbosticwill be set to point to the start of the next token in the string,
5545446Sbosticor the null at the end of the string if no more tokens are present.
5645446SbosticThe external variable
57*48352Scael.Fa suboptarg
58*48352Scaelwill be set to point to the start of the current token, or
59*48352Scael.Dv NULL
60*48352Scaelif no
6145446Sbostictokens were present.
62*48352ScaelThe argument
63*48352Scael.Fa valuep
64*48352Scaelwill be set to point to the ``value'' portion of the token, or
65*48352Scael.Dv NULL
6645446Sbosticif no ``value'' portion was present.
67*48352Scael.Sh EXAMPLE
68*48352Scael.Bd -literal -offset indent -compact
6945446Sbosticchar *tokens[] = {
7045446Sbostic	#define	ONE	0
7145446Sbostic		"one",
7245446Sbostic	#define	TWO	1
7345446Sbostic		"two",
7445446Sbostic	NULL
7545446Sbostic};
7645446Sbostic
7745446Sbostic\&...
7845446Sbostic
7945446Sbosticextern char *optarg, *suboptarg;
8045446Sbosticchar *options, *value;
8145446Sbostic
82*48352Scaelwhile ((ch = getopt(argc, argv, "ab:")) != \-1) {
8345446Sbostic	switch(ch) {
8445446Sbostic	case 'a':
8545446Sbostic		/* process ``a'' option */
8645446Sbostic		break;
8745446Sbostic	case 'b':
8845446Sbostic		options = optarg;
8945446Sbostic		while (*options) {
9045446Sbostic			switch(getsubopt(&options, tokens, &value)) {
9145446Sbostic			case ONE:
9245446Sbostic				/* process ``one'' sub option */
9345446Sbostic				break;
9445446Sbostic			case TWO:
9545446Sbostic				/* process ``two'' sub option */
9645446Sbostic				if (!value)
9745446Sbostic					error("no value for two");
9845446Sbostic				i = atoi(value);
9945446Sbostic				break;
100*48352Scael			case \-1:
10145446Sbostic				if (suboptarg)
10245446Sbostic					error("illegal sub option %s",
103*48352Scael					  suboptarg);
10445446Sbostic				else
10545446Sbostic					error("missing sub option");
10645446Sbostic				break;
10745446Sbostic		}
10845446Sbostic		break;
10945446Sbostic	}
110*48352Scael.Ed
111*48352Scael.Sh SEE ALSO
112*48352Scael.Xr getopt 3 ,
113*48352Scael.Xr strsep 3
114*48352Scael.Sh HISTORY
115*48352ScaelThe
116*48352Scael.Fn getsubopt
117*48352Scaelfunction is
118*48352Scael.Ud .
119