xref: /csrg-svn/lib/libc/stdlib/getsubopt.3 (revision 50725)
148352Scael.\" Copyright (c) 1990, 1991 The Regents of the University of California.
245446Sbostic.\" All rights reserved.
345446Sbostic.\"
445446Sbostic.\" %sccs.include.redist.man%
545446Sbostic.\"
6*50725Scael.\"     @(#)getsubopt.3	5.4 (Berkeley) 07/31/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>
1648352Scael.Ft int
1748352Scael.Fn getsubopt "char **optionp" "char * const *tokens" "char **valuep"
1848352Scael.Sh DESCRIPTION
1948352ScaelThe
2048352Scael.Fn getsubopt
2145446Sbosticparses a string containing tokens delimited by one or more tab, space or
2248352Scaelcomma
2348352Scael.Pq Ql \&,
2448352Scaelcharacters.
2545446SbosticIt is intended for use in parsing groups of option arguments provided
2645446Sbosticas part of a utility command line.
2748352Scael.Pp
2848352ScaelThe argument
2948352Scael.Fa optionp
3045446Sbosticis a pointer to a pointer to the string.
3148352ScaelThe argument
3248352Scael.Fa tokens
3348352Scaelis a pointer to a
3448352Scael.Dv NULL Ns -terminated
3548352Scaelarray of pointers to strings.
3648352Scael.Pp
3748352ScaelThe
3848352Scael.Fn getsubopt
3948352Scaelfunction
4045446Sbosticreturns the zero-based offset of the pointer in the
4148352Scael.Fa tokens
4245446Sbosticarray referencing a string which matches the first token
4348352Scaelin the string, or, \-1 if the string contains no tokens or
4448352Scael.Fa tokens
4545446Sbosticdoes not contain a matching string.
4648352Scael.Pp
4745446SbosticIf the token is of the form ``name=value'', the location referenced by
4848352Scael.Fa valuep
4945446Sbosticwill be set to point to the start of the ``value'' portion of the token.
5048352Scael.Pp
5145446SbosticOn return from
5248352Scael.Fn getsubopt ,
5348352Scael.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
5748352Scael.Fa suboptarg
5848352Scaelwill be set to point to the start of the current token, or
5948352Scael.Dv NULL
6048352Scaelif no
6145446Sbostictokens were present.
6248352ScaelThe argument
6348352Scael.Fa valuep
6448352Scaelwill be set to point to the ``value'' portion of the token, or
6548352Scael.Dv NULL
6645446Sbosticif no ``value'' portion was present.
6748352Scael.Sh EXAMPLE
68*50725Scael.Bd -literal -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
8248352Scaelwhile ((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;
10048352Scael			case \-1:
10145446Sbostic				if (suboptarg)
10245446Sbostic					error("illegal sub option %s",
10348352Scael					  suboptarg);
10445446Sbostic				else
10545446Sbostic					error("missing sub option");
10645446Sbostic				break;
10745446Sbostic		}
10845446Sbostic		break;
10945446Sbostic	}
11048352Scael.Ed
11148352Scael.Sh SEE ALSO
11248352Scael.Xr getopt 3 ,
11348352Scael.Xr strsep 3
11448352Scael.Sh HISTORY
11548352ScaelThe
11648352Scael.Fn getsubopt
11748352Scaelfunction is
11848352Scael.Ud .
119