1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
|
.\" $NetBSD: strlist.9,v 1.3 2021/01/21 17:05:50 wiz Exp $
.\"
.\" Copyright (c) 2021 The NetBSD Foundation, Inc.
.\" All rights reserved.
.\"
.\" This code is derived from software contributed to The NetBSD Foundation
.\" by Jason R. Thorpe.
.\"
.\" 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.
.\"
.\" 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 January 20, 2021
.Dt OFSL 9
.Os
.Sh NAME
.Nm strlist ,
.Nm strlist_next ,
.Nm strlist_count ,
.Nm strlist_string ,
.Nm strlist_match ,
.Nm strlist_index ,
.Nm strlist_append
.Nd functions to interact with OpenFirmware-style string lists
.Sh SYNOPSIS
.In sys/systm.h
.Ft const char *
.Fn strlist_next "const char *sl" "size_t slsize" "size_t *cursorp"
.Ft void
.Fn strlist_count "const char *sl" "size_t slsize"
.Ft const char *
.Fn strlist_string "const char *sl" "size_t slsize" "unsigned int index"
.Ft int
.Fn strlist_match "const char *sl" "size_t slsize" "const char *str"
.Ft int
.Fn strlist_pmatch "const char *sl" "size_t slsize" "const char *pattern"
.Ft int
.Fn strlist_index "const char *sl" "size_t slsize" "const char *str"
.Ft bool
.Fn strlist_append "char **slp" "size_t *slsizep" "const char *str"
.Sh DESCRIPTION
The
.Nm
functions provide a simple way to interact with OpenFirmware
.Pq IEEE 1275
string lists.
.Pp
An OpenFirmware string list is simply a buffer containing one or more
NUL-terminated strings concatenated together.
For example, a string list containing the strings
.Dq foo ,
.Dq bar ,
and
.Dq baz
would be represented in memory as:
.Bd -literal -offset indent
foo\\0bar\\0baz\\0
.Ed
.Pp
The following functions are available:
.Bl -tag -width "xxxxx"
.It Fn strlist_next "const char *sl" "size_t slsize" "size_t *cursorp"
This function provides a way to enumerate the strings in a string list.
To enumerate a string list, initialize
.Fa cursor
to 0 and pass it by reference to
.Fn strlist_next .
Each call to
.Fn strlist_next
returns the current string and advances the cursor to the next string in
the list.
If all strings in the list have been enumerated,
.Fn strlist_next
will return
.Dv NULL .
.It Fn strlist_count "const char *sl" "size_t slsize"
Returns the number of strings in the string list.
.It Fn strlist_string "const char *sl" "size_t slsize" "unsigned int index"
Returns a pointer to the string in the string list at the specified
index or
.Dv NULL
if the index is out of range.
.It Fn strlist_match "const char *sl" "size_t slsize" "const char *str"
Returns a weighted match value if the specified string appears in
the string list.
The value returned is the number of strings in the string list
minus the index of the matched string.
For example, if a string list contains the strings
.Dq foo ,
.Dq bar ,
and
.Dq baz ,
a match against
.Dq foo
returns 3 and a match against
.Dq baz
returns 1.
If the string does not appear in the string list, 0 is returned.
.It Fn strlist_pmatch "const char *sl" "size_t slsize" "const char *pattern"
Like
.Fn strlist_match ,
but uses
.Fn pmatch
to compare strings, allowing for wildcard characters to be specified in
.Fa pattern .
.It Fn strlist_index "const char *sl" "size_t slsize" "const char *str"
Returns the index of the specified string if it appears in the
string list, or \-1 if the string does not appear in the string list.
.It Fn strlist_append "char **slp" "size_t *slsizep" "const char *str"
Appends a copy of the specified string to the stringlist.
Begin by initializing
.Fa sl
to
.Dv NULL
and
.Fa slsize
to 0.
Pass these by reference to
.Fn strlist_append .
New memory for the string list will be allocated as needed.
The resulting string list can be freed with
.Fn kmem_free .
Returns
.Dv true
if the string was successfully appended to the string list or
.Dv false
if memory allocation fails.
.El
.Sh EXAMPLES
The following shows an example of string list enumeration using
.Fn strlist_next :
.Bd -literal
void
print_stringlist(const char *sl, size_t slsize)
{
const char *cp;
size_t cursor;
printf("There are %u strings in the string list:\\n",
strlist_count(sl, slsize));
for (cursor = 0;
(cp = strlist_next(sl, slsize, &cursor) != NULL; ) {
printf("\\t%s\\n", cp);
}
}
.Ed
.Pp
The following example shows a simple way to use
.Fn strlist_match :
.Bd -literal
bool
is_compatible(int phandle, const char *compat_str)
{
char buf[128];
int proplen;
proplen = OF_getprop(phandle, "compatible", buf, sizeof(buf));
return strlist_match(buf, proplen, compat_str) != 0;
}
.Ed
.Pp
The following example shows a use of
.Fn strlist_pmatch :
.Bd -literal
bool
is_pc_printer_port(const char *pnp_id_list, size_t list_size)
{
return strlist_pmatch(pnp_id_list, list_size, "PNP04??") != 0;
}
.Ed
.Pp
The following example converts an array of strings to a string list using
.Fn strlist_append :
.Bd -literal
char *
string_array_to_string_list(const char **array, int count,
size_t *slsizep)
{
char *sl;
size_t slsize;
int i;
for (i = 0, sl = NULL, slsize = 0; i < count; i++) {
if (!strlist_append(&sl, &slsize, array[i])) {
kmem_free(sl, slsize);
return NULL;
}
}
*slsizep = slsize;
return sl;
}
.Ed
.Sh SEE ALSO
.Xr kmem 9 ,
.Xr pmatch 9
.Sh HISTORY
The
.Nm
functions first appeared in
.Nx 10.0 .
|