Implement options string parsing of the form 'a=b c=d ...'. This will be

initially used by i386's Multiboot support but will be useful in other
situations too (think refactoring mount(2)'s API).
This commit is contained in:
jmmv 2006-02-03 11:04:46 +00:00
parent 5bb4d25b69
commit 47d8ee4167
7 changed files with 249 additions and 5 deletions

View File

@ -1,4 +1,4 @@
# $NetBSD: mi,v 1.845 2006/02/02 15:26:34 reinoud Exp $
# $NetBSD: mi,v 1.846 2006/02/03 11:04:46 jmmv Exp $
./etc/mtree/set.comp comp-sys-root
./usr/bin/addr2line comp-debug-bin bfd
./usr/bin/ar comp-util-bin bfd
@ -1395,6 +1395,7 @@
./usr/include/sys/mtio.h comp-c-include
./usr/include/sys/namei.h comp-c-include
./usr/include/sys/null.h comp-c-include
./usr/include/sys/optstr.h comp-c-include
./usr/include/sys/param.h comp-c-include
./usr/include/sys/pipe.h comp-c-include
./usr/include/sys/pmc.h comp-c-include
@ -5432,6 +5433,8 @@
./usr/share/man/cat9/nextrunqueue.0 comp-sys-catman .cat
./usr/share/man/cat9/old_sysctl.0 comp-sys-catman .cat
./usr/share/man/cat9/opencrypto.0 comp-sys-catman .cat
./usr/share/man/cat9/optstr.0 comp-sys-catman .cat
./usr/share/man/cat9/optstr_get.0 comp-sys-catman .cat
./usr/share/man/cat9/panic.0 comp-sys-catman .cat
./usr/share/man/cat9/pci.0 comp-sys-catman .cat
./usr/share/man/cat9/pci_conf_hook.0 comp-sys-catman .cat
@ -9376,6 +9379,8 @@
./usr/share/man/man9/nextrunqueue.9 comp-sys-man .man
./usr/share/man/man9/old_sysctl.9 comp-sys-man .man
./usr/share/man/man9/opencrypto.9 comp-sys-man .man
./usr/share/man/man9/optstr.9 comp-sys-man .man
./usr/share/man/man9/optstr_get.9 comp-sys-man .man
./usr/share/man/man9/panic.9 comp-sys-man .man
./usr/share/man/man9/pci.9 comp-sys-man .man
./usr/share/man/man9/pci_conf_hook.9 comp-sys-man .man

View File

@ -1,4 +1,4 @@
# $NetBSD: Makefile,v 1.189 2006/01/17 17:29:28 thorpej Exp $
# $NetBSD: Makefile,v 1.190 2006/02/03 11:04:46 jmmv Exp $
# Makefile for section 9 (kernel function and variable) manual pages.
@ -27,7 +27,7 @@ MAN= altq.9 arc4random.9 arp.9 audio.9 autoconf.9 \
kprintf.9 kthread.9 linedisc.9 lock.9 log.9 ltsleep.9 \
malloc.9 mbuf.9 mca.9 memcmp.9 memcpy.9 memmove.9 memset.9 \
microtime.9 mstohz.9 m_tag.9 namecache.9 namei.9 need_resched.9 \
opencrypto.9 \
opencrypto.9 optstr.9 \
panic.9 pci.9 pci_configure_bus.9 pci_intr.9 pckbport.9 \
pcmcia.9 pfil.9 physio.9 pmap.9 pmatch.9 pmc.9 \
pool.9 pool_cache.9 powerhook_establish.9 ppsratecheck.9 \
@ -334,6 +334,7 @@ MLINKS+=opencrypto.9 crypto_get_driverid.9 \
opencrypto.9 crypto_kdispatch.9 \
opencrypto.9 crypto_getreq.9 \
opencrypto.9 crypto_freereq.9
MLINKS+=optstr.9 optstr_get.9
MLINKS+=pci.9 pci_conf_read.9 \
pci.9 pci_conf_write.9 \
pci.9 pci_conf_print.9 \

88
share/man/man9/optstr.9 Normal file
View File

@ -0,0 +1,88 @@
.\" $NetBSD: optstr.9,v 1.1 2006/02/03 11:04:46 jmmv Exp $
.\"
.\" Copyright (c) 2006 The NetBSD Foundation, Inc.
.\" All rights reserved.
.\"
.\" This code is derived from software contributed to The NetBSD Foundation
.\" by Julio M. Merino Vidal.
.\"
.\" 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 NetBSD
.\" Foundation, Inc. and its contributors.
.\" 4. Neither the name of The NetBSD Foundation 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
.\"
.Dd February 3, 2006
.Dt OPTSTR 9
.Os
.Sh NAME
.Nm optstr_get
.Nd Options string management
.Sh SYNOPSIS
.In sys/optstr.h
.Ft boolean_t
.Fn optstr_get "const char *optstr" "const char *key" "char *buf" "size_t bufsize"
.Sh DESCRIPTION
An options string is a list of key/value pairs represented in textual form.
Each pair is expressed as
.Sq 'key=value'
and is separated from other pairs by one or more spaces.
For example:
.Bd -literal
key1=value1 key2=value2 key3=value3
.Ed
.Pp
Options strings are used to pass information between userland programs and
the kernel in a binary-agnostic way.
This makes them endianness and ABI independent.
.Sh FUNCTIONS
The following functions are provided to manage options strings:
.Bl -tag -width compact
.It Fn optstr_get "optstr" "key" "buf" "bufsize"
Scans the
.Va optstr
options string looking for the key
.Va key
and stores its value in the buffer pointed to by
.Va buf
copying a maximum of
.Va bufsize
bytes.
Returns
.Sq TRUE
if the key was found or
.Sq FALSE
otherwise, in which case
.Va buf
is left unmodified.
.El
.Sh CODE REFERENCES
The options string management functions are implemented within the files
.Pa sys/kern/subr_optstr.c
and
.Pa sys/sys/optstr.h .
.Sh HISTORY
Options strings appeared in
.Nx 4.0 .

View File

@ -1,4 +1,4 @@
# $NetBSD: files,v 1.756 2006/02/02 15:26:35 reinoud Exp $
# $NetBSD: files,v 1.757 2006/02/03 11:04:46 jmmv Exp $
# @(#)files.newconf 7.5 (Berkeley) 5/10/93
@ -1250,6 +1250,7 @@ file kern/subr_prf.c
file kern/subr_prof.c
file kern/subr_prop.c
file kern/subr_once.c
file kern/subr_optstr.c
file kern/subr_userconf.c userconf
file kern/subr_workqueue.c
file kern/subr_xxx.c

101
sys/kern/subr_optstr.c Normal file
View File

@ -0,0 +1,101 @@
/* $NetBSD: subr_optstr.c,v 1.1 2006/02/03 11:04:46 jmmv Exp $ */
/*-
* Copyright (c) 2006 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Julio M. Merino Vidal.
*
* 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 NetBSD
* Foundation, Inc. and its contributors.
* 4. Neither the name of The NetBSD Foundation 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: subr_optstr.c,v 1.1 2006/02/03 11:04:46 jmmv Exp $");
#include <sys/param.h>
#include <sys/optstr.h>
/* --------------------------------------------------------------------- */
/*
* Given an options string of the form 'a=b c=d ... y=z' and a key,
* looks for the given key's value in the string and returns it in buf
* with a maximum of bufsize bytes. If the key is found, returns TRUE;
* otherwise FALSE.
*/
boolean_t
optstr_get(const char *optstr, const char *key, char *buf, size_t bufsize)
{
boolean_t found;
const char *keyp;
found = FALSE;
/* Skip any initial spaces until we find a word. */
while (*optstr == ' ' && *optstr != '\0')
optstr++;
/* Search for the given key within the option string. */
while (!found && *optstr != '\0') {
/* Check if the next word matches the key. */
keyp = key;
while (*optstr == *keyp) {
optstr++;
keyp++;
}
if (*optstr == '=')
found = TRUE;
else {
/* Key not found; skip until next space. */
while (*optstr != ' ' && *optstr != '\0')
optstr++;
/* And now skip until next word. */
while (*optstr == ' ' && *optstr != '\0')
optstr++;
}
}
/* If the key was found; copy its value to the target buffer. */
if (found) {
const char *lastbuf;
lastbuf = buf + (bufsize - 1);
found = TRUE;
optstr++; /* Skip '='. */
while (buf != lastbuf && *optstr != ' ' && *optstr != '\0')
*buf++ = *optstr++;
*buf = '\0';
}
return found;
}

View File

@ -1,4 +1,4 @@
# $NetBSD: Makefile,v 1.80 2005/12/20 16:28:55 thorpej Exp $
# $NetBSD: Makefile,v 1.81 2006/02/03 11:04:47 jmmv Exp $
INCSDIR= /usr/include/sys
@ -19,6 +19,7 @@ INCS= acct.h agpio.h ansi.h ataio.h audioio.h \
malloc.h mallocvar.h mbuf.h md4.h \
md5.h midiio.h mman.h mount.h msg.h msgbuf.h mtio.h \
namei.h null.h \
optstr.h \
param.h pipe.h pmc.h poll.h pool.h power.h proc.h properties.h \
protosw.h ptrace.h queue.h \
ras.h reboot.h radioio.h resource.h resourcevar.h rnd.h \

47
sys/sys/optstr.h Normal file
View File

@ -0,0 +1,47 @@
/* $NetBSD: optstr.h,v 1.1 2006/02/03 11:04:47 jmmv Exp $ */
/*-
* Copyright (c) 2006 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Julio M. Merino Vidal.
*
* 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 NetBSD
* Foundation, Inc. and its contributors.
* 4. Neither the name of The NetBSD Foundation 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
*/
#if !defined(_SYS_OPTSTR_H_)
#define _SYS_OPTSTR_H_
/*
* Prototypes for functions defined in sys/kern/subr_optstr.c.
*/
boolean_t optstr_get(const char *, const char *, char *, size_t);
#endif /* !defined(_SYS_OPTSTR_H_) */