xref: /netbsd-src/share/man/man9/optstr.9 (revision a9b7771ff9d933a40e07d69680f3f77d521d10a4)
1.\"     $NetBSD: optstr.9,v 1.6 2023/04/20 10:43:17 uwe Exp $
2.\"
3.\" Copyright (c) 2006 The NetBSD Foundation, Inc.
4.\" All rights reserved.
5.\"
6.\" This code is derived from software contributed to The NetBSD Foundation
7.\" by Julio M. Merino Vidal.
8.\"
9.\" Redistribution and use in source and binary forms, with or without
10.\" modification, are permitted provided that the following conditions
11.\" are met:
12.\" 1. Redistributions of source code must retain the above copyright
13.\"    notice, this list of conditions and the following disclaimer.
14.\" 2. Redistributions in binary form must reproduce the above copyright
15.\"    notice, this list of conditions and the following disclaimer in the
16.\"    documentation and/or other materials provided with the distribution.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28.\" POSSIBILITY OF SUCH DAMAGE.
29.\"
30.Dd May 20, 2023
31.Dt OPTSTR 9
32.Os
33.
34.Sh NAME
35.Nm optstr_get ,
36.Nm optstr_get_string ,
37.Nm optstr_get_number ,
38.Nm optstr_get_number_binary ,
39.Nm optstr_get_number_hex ,
40.Nm optstr_get_macaddr
41.Nd Options string management
42.
43.Sh SYNOPSIS
44.In sys/optstr.h
45.
46.Ft bool
47.Fo optstr_get
48.Fa "const char *optstr"
49.Fa "const char *key"
50.Fa "char *buf"
51.Fa "size_t bufsize"
52.Fc
53.
54.Ft bool
55.Fo optstr_get_string
56.Fa "const char *optstr"
57.Fa "const char *key"
58.Fa "char **result"
59.Fc
60.
61.Ft bool
62.Fo optstr_get_number
63.Fa "const char *optstr"
64.Fa "const char *key"
65.Fa "unsigned long *result"
66.Fc
67.
68.Ft bool
69.Fo optstr_get_number_binary
70.Fa "const char *optstr"
71.Fa "const char *key"
72.Fa "unsigned long *result"
73.Fc
74.
75.Ft bool
76.Fo optstr_get_number_hex
77.Fa "const char *optstr"
78.Fa "const char *key"
79.Fa "unsigned long *result"
80.Fc
81.
82.Ft bool
83.Fo optstr_get_macaddr
84.Fa "const char *optstr"
85.Fa "const char *key"
86.Fa "uint8_t result[ETHER_ADDR_LEN]"
87.Fc
88.
89.Sh DESCRIPTION
90An options string is a list of key/value pairs represented in textual form.
91Each pair is expressed as
92.Ar key\^ Ns Li = Ns Ar value
93and is separated from other pairs by one or more spaces.
94For example:
95.Pp
96.Dl key1=value1 key2=value2 key3=value3
97.Pp
98Options strings are used to pass information between userland programs and
99the kernel in a binary-agnostic way.
100This makes them endianness and ABI independent.
101.Sh FUNCTIONS
102The following functions are provided to manage options strings:
103.Bl -tag -width Fn
104.It Fn optstr_get "optstr" "key" "buf" "bufsize"
105Scans the
106.Fa optstr
107options string looking for the key
108.Fa key
109and stores its value in the buffer pointed to by
110.Fa buf
111copying a maximum of
112.Fa bufsize
113bytes.
114Returns
115.Ql true
116if the key was found or
117.Ql false
118otherwise, in which case
119.Fa buf
120is left unmodified.
121.El
122.Pp
123The
124.Li optstr_get_ Ns Ar item
125family of functions provide the ability to scan for the key, and
126return the value converted to an appropriate type.
127.Pp
128.Bl -tag -width Fn -compact
129.It Fn optstr_get_string "optstr" "key" "result"
130.It Fn optstr_get_number "optstr" "key" "result"
131.It Fn optstr_get_number_binary "optstr" "key" "result"
132.It Fn optstr_get_number_hex "optstr" "key" "result"
133.It Fn optstr_get_macaddr "optstr" "key" "result"
134These functions scan the
135.Fa optstr
136options string looking for the key
137.Fa key
138and returns the key value converted as per the function name in
139.Fa result .
140All functions return
141.Ql true
142if the key was found or
143.Ql false
144otherwise, in which case
145.Fa result
146is left unmodified.
147.El
148.Sh CODE REFERENCES
149The options string management functions are implemented within the files
150.Pa sys/kern/subr_optstr.c
151and
152.Pa sys/sys/optstr.h .
153.Sh HISTORY
154Options strings appeared in
155.Nx 4.0 .
156