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