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