xref: /netbsd-src/sys/dev/dev_verbose.c (revision 74007e33ee94f1e0c4c6a9d698d39ab4de9f04cc)
1 /*	$NetBSD: dev_verbose.c,v 1.4 2021/06/29 21:03:36 pgoyette Exp $	*/
2 
3 /*
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  */
24 
25 #include <sys/cdefs.h>
26 __KERNEL_RCSID(0, "$NetBSD: dev_verbose.c,v 1.4 2021/06/29 21:03:36 pgoyette Exp $");
27 
28 #include <sys/param.h>
29 
30 #ifdef _KERNEL
31 #include <sys/systm.h>
32 #else
33 #include <stdio.h>
34 #include <string.h>
35 #endif
36 
37 #include <dev/dev_verbose.h>
38 
39 static const char *
dev_untokenstring(const char * words,const uint32_t * token,char * buf,size_t len)40 dev_untokenstring(const char *words, const uint32_t *token, char *buf,
41     size_t len)
42 {
43 	char *cp = buf;
44 	size_t newlen;
45 
46 	buf[0] = '\0';
47 	for (; *token != 0; token++) {
48 		newlen = strlcat(buf, words + *token, len - 2);
49 		if (newlen > len - 2)
50 			newlen = len - 2;
51 		cp = buf + newlen;
52 		cp[0] = ' ';
53 		cp[1] = '\0';
54 	}
55 	*cp = '\0';
56 	return cp != buf ? buf : NULL;
57 }
58 
59 const char *
dev_findvendor(char * buf,size_t len,const char * words,size_t nwords,const uint32_t * vendors,size_t nvendors,uint32_t vendor,const char * fmt)60 dev_findvendor(char *buf, size_t len, const char *words, size_t nwords,
61     const uint32_t *vendors, size_t nvendors, uint32_t vendor,
62     const char *fmt)
63 {
64 	size_t n;
65 
66 	for (n = 0; n < nvendors; n++) {
67 		if (vendors[n] == vendor)
68 			return dev_untokenstring(words, &vendors[n + 1],
69 			    buf, len);
70 
71 		/* Skip Tokens */
72 		n++;
73 		while (n < nvendors && vendors[n] != 0)
74 			n++;
75 	}
76 	snprintf(buf, len, fmt, vendor);
77 	return NULL;
78 }
79 
80 const char *
dev_findproduct(char * buf,size_t len,const char * words,size_t nwords,const uint32_t * products,size_t nproducts,uint32_t vendor,uint32_t product,const char * fmt)81 dev_findproduct(char *buf, size_t len, const char *words, size_t nwords,
82     const uint32_t *products, size_t nproducts, uint32_t vendor,
83     uint32_t product, const char *fmt)
84 {
85 	size_t n;
86 
87 	for (n = 0; n < nproducts; n++) {
88 		if (products[n] == vendor && products[n + 1] == product)
89 			return dev_untokenstring(words, &products[n + 2],
90 			    buf, len);
91 
92 		/* Skip Tokens */
93 		n += 2;
94 		while (n < nproducts && products[n] != 0)
95 			n++;
96 	}
97 	snprintf(buf, len, fmt, product);
98 	return NULL;
99 }
100