xref: /netbsd-src/lib/libc/stdlib/getsubopt.3 (revision 7dba0fd9edd18271199fc7ce60499ed5cb32500c)
1*7dba0fd9Smartin.\"	$NetBSD: getsubopt.3,v 1.16 2018/08/15 10:49:47 martin Exp $
2edf59accSthorpej.\"
378ef0c30Sperry.\" Copyright (c) 1990, 1991, 1993
478ef0c30Sperry.\"	The Regents of the University of California.  All rights reserved.
578ef0c30Sperry.\"
678ef0c30Sperry.\" Redistribution and use in source and binary forms, with or without
778ef0c30Sperry.\" modification, are permitted provided that the following conditions
878ef0c30Sperry.\" are met:
978ef0c30Sperry.\" 1. Redistributions of source code must retain the above copyright
1078ef0c30Sperry.\"    notice, this list of conditions and the following disclaimer.
1178ef0c30Sperry.\" 2. Redistributions in binary form must reproduce the above copyright
1278ef0c30Sperry.\"    notice, this list of conditions and the following disclaimer in the
1378ef0c30Sperry.\"    documentation and/or other materials provided with the distribution.
14eb7c1594Sagc.\" 3. Neither the name of the University nor the names of its contributors
1578ef0c30Sperry.\"    may be used to endorse or promote products derived from this software
1678ef0c30Sperry.\"    without specific prior written permission.
1778ef0c30Sperry.\"
1878ef0c30Sperry.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1978ef0c30Sperry.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2078ef0c30Sperry.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2178ef0c30Sperry.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2278ef0c30Sperry.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2378ef0c30Sperry.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2478ef0c30Sperry.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2578ef0c30Sperry.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2678ef0c30Sperry.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2778ef0c30Sperry.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2878ef0c30Sperry.\" SUCH DAMAGE.
2978ef0c30Sperry.\"
3078ef0c30Sperry.\"     @(#)getsubopt.3	8.1 (Berkeley) 6/9/93
3178ef0c30Sperry.\"
32*7dba0fd9Smartin.Dd August 15, 2018
3378ef0c30Sperry.Dt GETSUBOPT 3
3478ef0c30Sperry.Os
3578ef0c30Sperry.Sh NAME
3678ef0c30Sperry.Nm getsubopt
3778ef0c30Sperry.Nd get sub options from an argument
38312aca53Sperry.Sh LIBRARY
39312aca53Sperry.Lb libc
4078ef0c30Sperry.Sh SYNOPSIS
41*7dba0fd9Smartin.In stdlib.h
4278ef0c30Sperry.Vt extern char *suboptarg
4378ef0c30Sperry.Ft int
4478ef0c30Sperry.Fn getsubopt "char **optionp" "char * const *tokens" "char **valuep"
4578ef0c30Sperry.Sh DESCRIPTION
4678ef0c30SperryThe
4778ef0c30Sperry.Fn getsubopt
4878ef0c30Sperryfunction
4978ef0c30Sperryparses a string containing tokens delimited by one or more tab, space or
5078ef0c30Sperrycomma
5178ef0c30Sperry.Pq Ql \&,
5278ef0c30Sperrycharacters.
5378ef0c30SperryIt is intended for use in parsing groups of option arguments provided
5478ef0c30Sperryas part of a utility command line.
5578ef0c30Sperry.Pp
5678ef0c30SperryThe argument
5778ef0c30Sperry.Fa optionp
5878ef0c30Sperryis a pointer to a pointer to the string.
5978ef0c30SperryThe argument
6078ef0c30Sperry.Fa tokens
6178ef0c30Sperryis a pointer to a
6278ef0c30Sperry.Dv NULL Ns -terminated
6378ef0c30Sperryarray of pointers to strings.
6478ef0c30Sperry.Pp
6578ef0c30SperryThe
6678ef0c30Sperry.Fn getsubopt
6778ef0c30Sperryfunction
6878ef0c30Sperryreturns the zero-based offset of the pointer in the
6978ef0c30Sperry.Fa tokens
7078ef0c30Sperryarray referencing a string which matches the first token
717a7ee80bSwizin the string, or \-1 if the string contains no tokens or
7278ef0c30Sperry.Fa tokens
7378ef0c30Sperrydoes not contain a matching string.
7478ef0c30Sperry.Pp
7578ef0c30SperryIf the token is of the form ``name=value'', the location referenced by
7678ef0c30Sperry.Fa valuep
7778ef0c30Sperrywill be set to point to the start of the ``value'' portion of the token.
7878ef0c30Sperry.Pp
7978ef0c30SperryOn return from
8078ef0c30Sperry.Fn getsubopt ,
8178ef0c30Sperry.Fa optionp
8278ef0c30Sperrywill be set to point to the start of the next token in the string,
8378ef0c30Sperryor the null at the end of the string if no more tokens are present.
8478ef0c30SperryThe external variable
8578ef0c30Sperry.Fa suboptarg
8678ef0c30Sperrywill be set to point to the start of the current token, or
8778ef0c30Sperry.Dv NULL
8878ef0c30Sperryif no
8978ef0c30Sperrytokens were present.
9078ef0c30SperryThe argument
9178ef0c30Sperry.Fa valuep
9278ef0c30Sperrywill be set to point to the ``value'' portion of the token, or
9378ef0c30Sperry.Dv NULL
9478ef0c30Sperryif no ``value'' portion was present.
954e59d266Swiz.Sh EXAMPLES
9678ef0c30Sperry.Bd -literal -compact
9778ef0c30Sperrychar *tokens[] = {
9878ef0c30Sperry	#define	ONE	0
9978ef0c30Sperry		"one",
10078ef0c30Sperry	#define	TWO	1
10178ef0c30Sperry		"two",
10278ef0c30Sperry	NULL
10378ef0c30Sperry};
10478ef0c30Sperry
10578ef0c30Sperry\&...
10678ef0c30Sperry
10778ef0c30Sperryextern char *optarg, *suboptarg;
10878ef0c30Sperrychar *options, *value;
10978ef0c30Sperry
11078ef0c30Sperrywhile ((ch = getopt(argc, argv, "ab:")) != \-1) {
11178ef0c30Sperry	switch(ch) {
11278ef0c30Sperry	case 'a':
11378ef0c30Sperry		/* process ``a'' option */
11478ef0c30Sperry		break;
11578ef0c30Sperry	case 'b':
11678ef0c30Sperry		options = optarg;
11778ef0c30Sperry		while (*options) {
11801869ca4Swiz			switch(getsubopt(&options, tokens, &value)) {
11978ef0c30Sperry			case ONE:
12078ef0c30Sperry				/* process ``one'' sub option */
12178ef0c30Sperry				break;
12278ef0c30Sperry			case TWO:
12378ef0c30Sperry				/* process ``two'' sub option */
12478ef0c30Sperry				if (!value)
12578ef0c30Sperry					error("no value for two");
12678ef0c30Sperry				i = atoi(value);
12778ef0c30Sperry				break;
12878ef0c30Sperry			case \-1:
12978ef0c30Sperry				if (suboptarg)
13077dec036Sjoda					error("unknown sub option %s",
13178ef0c30Sperry					  suboptarg);
13278ef0c30Sperry				else
13378ef0c30Sperry					error("missing sub option");
13478ef0c30Sperry				break;
13578ef0c30Sperry		}
13678ef0c30Sperry		break;
13778ef0c30Sperry	}
13878ef0c30Sperry.Ed
13978ef0c30Sperry.Sh SEE ALSO
14078ef0c30Sperry.Xr getopt 3 ,
14178ef0c30Sperry.Xr strsep 3
14278ef0c30Sperry.Sh HISTORY
14378ef0c30SperryThe
14478ef0c30Sperry.Fn getsubopt
145a16d9e86Sperryfunction first appeared in
1462e7202c5Swiz.Bx 4.4 ,
147567e675cSwizand is included in
148567e675cSwiz.St -p1003.1-2008 .
149