xref: /csrg-svn/lib/libc/stdlib/getsubopt.3 (revision 51707)
148352Scael.\" Copyright (c) 1990, 1991 The Regents of the University of California.
245446Sbostic.\" All rights reserved.
345446Sbostic.\"
445446Sbostic.\" %sccs.include.redist.man%
545446Sbostic.\"
6*51707Sbostic.\"     @(#)getsubopt.3	5.5 (Berkeley) 11/14/91
745446Sbostic.\"
848352Scael.Dd
948352Scael.Dt GETSUBOPT 3
1048352Scael.Os
1148352Scael.Sh NAME
1248352Scael.Nm getsubopt
1348352Scael.Nd get sub options from an argument
1448352Scael.Sh SYNOPSIS
1548352Scael.Fd #include <stdio.h>
16*51707Sbostic.Vt extern char *suboptarg
1748352Scael.Ft int
1848352Scael.Fn getsubopt "char **optionp" "char * const *tokens" "char **valuep"
1948352Scael.Sh DESCRIPTION
2048352ScaelThe
2148352Scael.Fn getsubopt
2245446Sbosticparses a string containing tokens delimited by one or more tab, space or
2348352Scaelcomma
2448352Scael.Pq Ql \&,
2548352Scaelcharacters.
2645446SbosticIt is intended for use in parsing groups of option arguments provided
2745446Sbosticas part of a utility command line.
2848352Scael.Pp
2948352ScaelThe argument
3048352Scael.Fa optionp
3145446Sbosticis a pointer to a pointer to the string.
3248352ScaelThe argument
3348352Scael.Fa tokens
3448352Scaelis a pointer to a
3548352Scael.Dv NULL Ns -terminated
3648352Scaelarray of pointers to strings.
3748352Scael.Pp
3848352ScaelThe
3948352Scael.Fn getsubopt
4048352Scaelfunction
4145446Sbosticreturns the zero-based offset of the pointer in the
4248352Scael.Fa tokens
4345446Sbosticarray referencing a string which matches the first token
4448352Scaelin the string, or, \-1 if the string contains no tokens or
4548352Scael.Fa tokens
4645446Sbosticdoes not contain a matching string.
4748352Scael.Pp
4845446SbosticIf the token is of the form ``name=value'', the location referenced by
4948352Scael.Fa valuep
5045446Sbosticwill be set to point to the start of the ``value'' portion of the token.
5148352Scael.Pp
5245446SbosticOn return from
5348352Scael.Fn getsubopt ,
5448352Scael.Fa optionp
5545446Sbosticwill be set to point to the start of the next token in the string,
5645446Sbosticor the null at the end of the string if no more tokens are present.
5745446SbosticThe external variable
5848352Scael.Fa suboptarg
5948352Scaelwill be set to point to the start of the current token, or
6048352Scael.Dv NULL
6148352Scaelif no
6245446Sbostictokens were present.
6348352ScaelThe argument
6448352Scael.Fa valuep
6548352Scaelwill be set to point to the ``value'' portion of the token, or
6648352Scael.Dv NULL
6745446Sbosticif no ``value'' portion was present.
6848352Scael.Sh EXAMPLE
6950725Scael.Bd -literal -compact
7045446Sbosticchar *tokens[] = {
7145446Sbostic	#define	ONE	0
7245446Sbostic		"one",
7345446Sbostic	#define	TWO	1
7445446Sbostic		"two",
7545446Sbostic	NULL
7645446Sbostic};
7745446Sbostic
7845446Sbostic\&...
7945446Sbostic
8045446Sbosticextern char *optarg, *suboptarg;
8145446Sbosticchar *options, *value;
8245446Sbostic
8348352Scaelwhile ((ch = getopt(argc, argv, "ab:")) != \-1) {
8445446Sbostic	switch(ch) {
8545446Sbostic	case 'a':
8645446Sbostic		/* process ``a'' option */
8745446Sbostic		break;
8845446Sbostic	case 'b':
8945446Sbostic		options = optarg;
9045446Sbostic		while (*options) {
9145446Sbostic			switch(getsubopt(&options, tokens, &value)) {
9245446Sbostic			case ONE:
9345446Sbostic				/* process ``one'' sub option */
9445446Sbostic				break;
9545446Sbostic			case TWO:
9645446Sbostic				/* process ``two'' sub option */
9745446Sbostic				if (!value)
9845446Sbostic					error("no value for two");
9945446Sbostic				i = atoi(value);
10045446Sbostic				break;
10148352Scael			case \-1:
10245446Sbostic				if (suboptarg)
10345446Sbostic					error("illegal sub option %s",
10448352Scael					  suboptarg);
10545446Sbostic				else
10645446Sbostic					error("missing sub option");
10745446Sbostic				break;
10845446Sbostic		}
10945446Sbostic		break;
11045446Sbostic	}
11148352Scael.Ed
11248352Scael.Sh SEE ALSO
11348352Scael.Xr getopt 3 ,
11448352Scael.Xr strsep 3
11548352Scael.Sh HISTORY
11648352ScaelThe
11748352Scael.Fn getsubopt
11848352Scaelfunction is
11948352Scael.Ud .
120