Browse Source

Move getsubopt(3) to stdlib like lite2 and XPG4.2 say...

Update man page from lite2 while I'm at it.
OPENBSD_2_2
millert 27 years ago
parent
commit
9519b9b6ca
3 changed files with 259 additions and 6 deletions
  1. +6
    -6
      src/lib/libc/stdlib/Makefile.inc
  2. +147
    -0
      src/lib/libc/stdlib/getsubopt.3
  3. +106
    -0
      src/lib/libc/stdlib/getsubopt.c

+ 6
- 6
src/lib/libc/stdlib/Makefile.inc View File

@ -4,10 +4,10 @@
.PATH: ${.CURDIR}/arch/${MACHINE_ARCH}/stdlib ${.CURDIR}/stdlib
SRCS+= a64l.c abort.c atexit.c atoi.c atof.c atol.c bsearch.c calloc.c \
cfree.c exit.c getenv.c getopt.c heapsort.c l64a.c malloc.c merge.c \
multibyte.c putenv.c qsort.c radixsort.c rand.c random.c realpath.c \
setenv.c strtod.c strtol.c strtoq.c strtoul.c strtouq.c system.c \
tfind.c tsearch.c \
cfree.c exit.c getenv.c getopt.c getsubopt.c heapsort.c l64a.c \
malloc.c merge.c multibyte.c putenv.c qsort.c radixsort.c rand.c \
random.c realpath.c setenv.c strtod.c strtol.c strtoq.c strtoul.c \
strtouq.c system.c tfind.c tsearch.c \
_rand48.c drand48.c erand48.c jrand48.c lcong48.c lrand48.c \
mrand48.c nrand48.c seed48.c srand48.c qabs.c qdiv.c
@ -33,8 +33,8 @@ SRCS+= abs.c div.c labs.c ldiv.c
.endif
MAN+= a64l.3 abort.3 abs.3 alloca.3 atexit.3 atof.3 atoi.3 atol.3 bsearch.3 \
calloc.3 div.3 exit.3 getenv.3 getopt.3 labs.3 ldiv.3 malloc.3 \
memory.3 qabs.3 qdiv.3 qsort.3 radixsort.3 rand48.3 rand.3 \
calloc.3 div.3 exit.3 getenv.3 getopt.3 getsubopt.3 labs.3 ldiv.3 \
malloc.3 memory.3 qabs.3 qdiv.3 qsort.3 radixsort.3 rand48.3 rand.3 \
random.3 realpath.3 strtod.3 strtol.3 strtoul.3 system.3 tsearch.3
MLINKS+=getenv.3 setenv.3 getenv.3 unsetenv.3 getenv.3 putenv.3


+ 147
- 0
src/lib/libc/stdlib/getsubopt.3 View File

@ -0,0 +1,147 @@
.\" $OpenBSD: getsubopt.3,v 1.1 1997/08/20 04:02:17 millert Exp $
.\"
.\" Copyright (c) 1990, 1991, 1993
.\" The Regents of the University of California. All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\" 3. All advertising materials mentioning features or use of this software
.\" must display the following acknowledgement:
.\" This product includes software developed by the University of
.\" California, Berkeley and its contributors.
.\" 4. Neither the name of the University nor the names of its contributors
.\" may be used to endorse or promote products derived from this software
.\" without specific prior written permission.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\" @(#)getsubopt.3 8.1 (Berkeley) 6/9/93
.\"
.Dd June 9, 1993
.Dt GETSUBOPT 3
.Os
.Sh NAME
.Nm getsubopt
.Nd get sub options from an argument
.Sh SYNOPSIS
.Fd #include <stdlib.h>
.Vt extern char *suboptarg
.Ft int
.Fn getsubopt "char **optionp" "char * const *tokens" "char **valuep"
.Sh DESCRIPTION
The
.Fn getsubopt
function
parses a string containing tokens delimited by one or more tab, space or
comma
.Pq Ql \&,
characters.
It is intended for use in parsing groups of option arguments provided
as part of a utility command line.
.Pp
The argument
.Fa optionp
is a pointer to a pointer to the string.
The argument
.Fa tokens
is a pointer to a
.Dv NULL Ns -terminated
array of pointers to strings.
.Pp
The
.Fn getsubopt
function
returns the zero-based offset of the pointer in the
.Fa tokens
array referencing a string which matches the first token
in the string, or, \-1 if the string contains no tokens or
.Fa tokens
does not contain a matching string.
.Pp
If the token is of the form ``name=value'', the location referenced by
.Fa valuep
will be set to point to the start of the ``value'' portion of the token.
.Pp
On return from
.Fn getsubopt ,
.Fa optionp
will be set to point to the start of the next token in the string,
or the null at the end of the string if no more tokens are present.
The external variable
.Fa suboptarg
will be set to point to the start of the current token, or
.Dv NULL
if no
tokens were present.
The argument
.Fa valuep
will be set to point to the ``value'' portion of the token, or
.Dv NULL
if no ``value'' portion was present.
.Sh EXAMPLE
.Bd -literal -compact
char *tokens[] = {
#define ONE 0
"one",
#define TWO 1
"two",
NULL
};
\&...
extern char *optarg, *suboptarg;
char *options, *value;
while ((ch = getopt(argc, argv, "ab:")) != \-1) {
switch(ch) {
case 'a':
/* process ``a'' option */
break;
case 'b':
options = optarg;
while (*options) {
switch(getsubopt(&options, tokens, &value)) {
case ONE:
/* process ``one'' sub option */
break;
case TWO:
/* process ``two'' sub option */
if (!value)
error("no value for two");
i = atoi(value);
break;
case \-1:
if (suboptarg)
error("illegal sub option %s",
suboptarg);
else
error("missing sub option");
break;
}
break;
}
.Ed
.Sh SEE ALSO
.Xr getopt 3 ,
.Xr strsep 3
.Sh HISTORY
The
.Fn getsubopt
function first appeared in 4.4BSD.

+ 106
- 0
src/lib/libc/stdlib/getsubopt.c View File

@ -0,0 +1,106 @@
/* $OpenBSD: getsubopt.c,v 1.1 1997/08/20 04:02:17 millert Exp $ */
/*-
* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef lint
#if 0
static char sccsid[] = "@(#)getsubopt.c 8.1 (Berkeley) 6/4/93";
#else
static char rcsid[] = "$OpenBSD: getsubopt.c,v 1.1 1997/08/20 04:02:17 millert Exp $";
#endif
#endif /* not lint */
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
/*
* The SVID interface to getsubopt provides no way of figuring out which
* part of the suboptions list wasn't matched. This makes error messages
* tricky... The extern variable suboptarg is a pointer to the token
* which didn't match.
*/
char *suboptarg;
int
getsubopt(optionp, tokens, valuep)
register char **optionp, **valuep;
register char * const *tokens;
{
register int cnt;
register char *p;
suboptarg = *valuep = NULL;
if (!optionp || !*optionp)
return(-1);
/* skip leading white-space, commas */
for (p = *optionp; *p && (*p == ',' || *p == ' ' || *p == '\t'); ++p);
if (!*p) {
*optionp = p;
return(-1);
}
/* save the start of the token, and skip the rest of the token. */
for (suboptarg = p;
*++p && *p != ',' && *p != '=' && *p != ' ' && *p != '\t';);
if (*p) {
/*
* If there's an equals sign, set the value pointer, and
* skip over the value part of the token. Terminate the
* token.
*/
if (*p == '=') {
*p = '\0';
for (*valuep = ++p;
*p && *p != ',' && *p != ' ' && *p != '\t'; ++p);
if (*p)
*p++ = '\0';
} else
*p++ = '\0';
/* Skip any whitespace or commas after this token. */
for (; *p && (*p == ',' || *p == ' ' || *p == '\t'); ++p);
}
/* set optionp for next round. */
*optionp = p;
for (cnt = 0; *tokens; ++tokens, ++cnt)
if (!strcmp(suboptarg, *tokens))
return(cnt);
return(-1);
}

Loading…
Cancel
Save