Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/lib/libkern Add a generic set of routines for interactin...



details:   https://anonhg.NetBSD.org/src/rev/350aa3e3a0f7
branches:  trunk
changeset: 950222:350aa3e3a0f7
user:      thorpej <thorpej%NetBSD.org@localhost>
date:      Thu Jan 21 15:43:37 2021 +0000

description:
Add a generic set of routines for interacting with OpenFirmware-style
string lists.

diffstat:

 sys/lib/libkern/libkern.h |    4 +-
 sys/lib/libkern/strlist.c |  490 ++++++++++++++++++++++++++++++++++++++++++++++
 sys/lib/libkern/strlist.h |   50 ++++
 3 files changed, 543 insertions(+), 1 deletions(-)

diffs (truncated from 566 to 300 lines):

diff -r 89f71b3d7f1b -r 350aa3e3a0f7 sys/lib/libkern/libkern.h
--- a/sys/lib/libkern/libkern.h Thu Jan 21 14:30:01 2021 +0000
+++ b/sys/lib/libkern/libkern.h Thu Jan 21 15:43:37 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: libkern.h,v 1.141 2021/01/16 23:51:51 chs Exp $        */
+/*     $NetBSD: libkern.h,v 1.142 2021/01/21 15:43:37 thorpej Exp $    */
 
 /*-
  * Copyright (c) 1992, 1993
@@ -44,6 +44,8 @@
 #include <sys/inttypes.h>
 #include <sys/null.h>
 
+#include <lib/libkern/strlist.h>
+
 #ifndef LIBKERN_INLINE
 #define LIBKERN_INLINE static __inline
 #define LIBKERN_BODY
diff -r 89f71b3d7f1b -r 350aa3e3a0f7 sys/lib/libkern/strlist.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/lib/libkern/strlist.c Thu Jan 21 15:43:37 2021 +0000
@@ -0,0 +1,490 @@
+/*     $NetBSD: strlist.c,v 1.1 2021/01/21 15:43:37 thorpej 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.
+ */
+
+/*
+ * strlist --
+ *
+ *     A set of routines for interacting with IEEE 1275 (OpenFirmware)
+ *     style string lists.
+ *
+ *     An OpenFirmware string list is simply a buffer containing
+ *     multiple NUL-terminated strings concatenated together.
+ *
+ *     So, for example, the a string list consisting of the strings
+ *     "foo", "bar", and "baz" would be represented in memory like:
+ *
+ *             foo\0bar\0baz\0
+ */
+
+#include <sys/types.h>
+
+/*
+ * Memory allocation wrappers to handle different environments.
+ */
+#if defined(_KERNEL)
+#include <sys/kmem.h>
+#include <sys/systm.h>
+
+static void *
+strlist_alloc(size_t const size)
+{
+       return kmem_zalloc(size, KM_SLEEP);
+}
+
+static void
+strlist_free(void * const v, size_t const size)
+{
+       kmem_free(v, size);
+}
+#elif defined(_STANDALONE)
+#include <lib/libsa/stand.h>
+
+static void *
+strlist_alloc(size_t const size)
+{
+       cp = alloc(size);
+       if (cp != NULL) {
+               memset(cp, 0, size);
+       }
+       return cp;
+}
+
+static void
+strlist_free(void * const v, size_t const size)
+{
+       dealloc(v, size);
+}
+#else /* user-space */
+#include <stdlib.h>
+#include <string.h>
+
+extern int pmatch(const char *, const char *, const char **);
+
+static void *
+strlist_alloc(size_t const size)
+{
+       return calloc(1, size);
+}
+
+static void
+strlist_free(void * const v, size_t const size __unused)
+{
+       free(v);
+}
+#endif
+
+#include "strlist.h"
+
+/*
+ * strlist_next --
+ *
+ *     Return a pointer to the next string in the strlist,
+ *     or NULL if there are no more strings.
+ */
+const char *
+strlist_next(const char * const sl, size_t const slsize, size_t * const cursorp)
+{
+
+       if (sl == NULL || slsize == 0 || cursorp == NULL) {
+               return NULL;
+       }
+
+       size_t cursor = *cursorp;
+
+       if (cursor >= slsize) {
+               /* No more strings in the list. */
+               return NULL;
+       }
+
+       const char *cp = sl + cursor;
+       *cursorp = cursor + strlen(cp) + 1;
+
+       return cp;
+}
+
+/*
+ * strlist_count --
+ *
+ *     Return the number of strings in the strlist.
+ */
+unsigned int
+strlist_count(const char *sl, size_t slsize)
+{
+
+       if (sl == NULL || slsize == 0) {
+               return 0;
+       }
+
+       size_t cursize;
+       unsigned int count;
+
+       for (count = 0; slsize != 0;
+            count++, sl += cursize, slsize -= cursize) {
+               cursize = strlen(sl) + 1;
+       }
+       return count;
+}
+
+/*
+ * strlist_string --
+ *
+ *     Returns the string in the strlist at the specified index.
+ *     Returns NULL if the index is beyond the strlist range.
+ */
+const char *
+strlist_string(const char * sl, size_t slsize, unsigned int const idx)
+{
+
+       if (sl == NULL || slsize == 0) {
+               return NULL;
+       }
+
+       size_t cursize;
+       unsigned int i;
+
+       for (i = 0; slsize != 0; i++, slsize -= cursize, sl += cursize) {
+               cursize = strlen(sl) + 1;
+               if (i == idx) {
+                       return sl;
+               }
+       }
+
+       return NULL;
+}
+
+static bool
+match_strcmp(const char * const s1, const char * const s2)
+{
+       return strcmp(s1, s2) == 0;
+}
+
+#if !defined(_STANDALONE)
+static bool
+match_pmatch(const char * const s1, const char * const s2)
+{
+       return pmatch(s1, s2, NULL) == 2;
+}
+#endif /* _STANDALONE */
+
+static bool
+strlist_match_internal(const char * const sl, size_t slsize,
+    const char * const str, int * const indexp, unsigned int * const countp,
+    bool (*match_fn)(const char *, const char *))
+{
+       const char *cp;
+       size_t l;
+       int i;
+       bool rv = false;
+
+       if (sl == NULL || slsize == 0) {
+               return false;
+       }
+
+       cp = sl;
+
+       for (i = 0; slsize != 0;
+            l = strlen(cp) + 1, slsize -= l, cp += l, i++) {
+               if (rv) {
+                       /*
+                        * We've already matched. We must be
+                        * counting to the end.
+                        */
+                       continue;
+               }
+               if ((*match_fn)(cp, str)) {
+                       /*
+                        * Matched!  Get the index.  If we don't
+                        * also want the total count, then get
+                        * out early.
+                        */
+                       *indexp = i;
+                       rv = true;
+                       if (countp == NULL) {
+                               break;
+                       }
+               }
+       }
+
+       if (countp != NULL) {
+               *countp = i;
+       }
+
+       return rv;
+}
+
+/*
+ * strlist_match --
+ *
+ *     Returns a weighted match value (1 <= match <= sl->count) if the
+ *     specified string appears in the strlist.  A match at the
+ *     beginning of the list carriest the greatest weight (i.e. sl->count)
+ *     and a match at the end of the list carriest the least (i.e. 1).
+ *     Returns 0 if there is no match.
+ *
+ *     This routine operates independently of the cursor used to enumerate
+ *     a strlist.
+ */
+int
+strlist_match(const char * const sl, size_t const slsize,
+    const char * const str)
+{
+       unsigned int count;
+       int idx;
+
+       if (strlist_match_internal(sl, slsize, str, &idx, &count,
+                                  match_strcmp)) {
+               return count - idx;
+       }
+       return 0;
+}
+
+#if !defined(_STANDALONE)
+/*
+ * strlist_pmatch --
+ *
+ *     Like strlist_match(), but uses pmatch(9) to match the
+ *     strings.
+ */
+int
+strlist_pmatch(const char * const sl, size_t const slsize,
+    const char * const pattern)



Home | Main Index | Thread Index | Old Index