Home | History | Annotate | Line # | Download | only in dev
      1  1.4  pgoyette /*	$NetBSD: dev_verbose.c,v 1.4 2021/06/29 21:03:36 pgoyette 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.4  pgoyette __KERNEL_RCSID(0, "$NetBSD: dev_verbose.c,v 1.4 2021/06/29 21:03:36 pgoyette 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.4  pgoyette dev_untokenstring(const char *words, const uint32_t *token, char *buf,
     41  1.1  christos     size_t len)
     42  1.1  christos {
     43  1.1  christos 	char *cp = buf;
     44  1.3  pgoyette 	size_t newlen;
     45  1.1  christos 
     46  1.1  christos 	buf[0] = '\0';
     47  1.1  christos 	for (; *token != 0; token++) {
     48  1.3  pgoyette 		newlen = strlcat(buf, words + *token, len - 2);
     49  1.3  pgoyette 		if (newlen > len - 2)
     50  1.3  pgoyette 			newlen = len - 2;
     51  1.3  pgoyette 		cp = buf + newlen;
     52  1.1  christos 		cp[0] = ' ';
     53  1.1  christos 		cp[1] = '\0';
     54  1.1  christos 	}
     55  1.1  christos 	*cp = '\0';
     56  1.1  christos 	return cp != buf ? buf : NULL;
     57  1.1  christos }
     58  1.1  christos 
     59  1.1  christos const char *
     60  1.1  christos dev_findvendor(char *buf, size_t len, const char *words, size_t nwords,
     61  1.4  pgoyette     const uint32_t *vendors, size_t nvendors, uint32_t vendor,
     62  1.4  pgoyette     const char *fmt)
     63  1.1  christos {
     64  1.1  christos 	size_t n;
     65  1.1  christos 
     66  1.1  christos 	for (n = 0; n < nvendors; n++) {
     67  1.1  christos 		if (vendors[n] == vendor)
     68  1.1  christos 			return dev_untokenstring(words, &vendors[n + 1],
     69  1.1  christos 			    buf, len);
     70  1.1  christos 
     71  1.1  christos 		/* Skip Tokens */
     72  1.1  christos 		n++;
     73  1.2  christos 		while (n < nvendors && vendors[n] != 0)
     74  1.1  christos 			n++;
     75  1.1  christos 	}
     76  1.4  pgoyette 	snprintf(buf, len, fmt, vendor);
     77  1.1  christos 	return NULL;
     78  1.1  christos }
     79  1.1  christos 
     80  1.1  christos const char *
     81  1.1  christos dev_findproduct(char *buf, size_t len, const char *words, size_t nwords,
     82  1.4  pgoyette     const uint32_t *products, size_t nproducts, uint32_t vendor,
     83  1.4  pgoyette     uint32_t product, const char *fmt)
     84  1.1  christos {
     85  1.1  christos 	size_t n;
     86  1.1  christos 
     87  1.1  christos 	for (n = 0; n < nproducts; n++) {
     88  1.1  christos 		if (products[n] == vendor && products[n + 1] == product)
     89  1.1  christos 			return dev_untokenstring(words, &products[n + 2],
     90  1.1  christos 			    buf, len);
     91  1.1  christos 
     92  1.1  christos 		/* Skip Tokens */
     93  1.1  christos 		n += 2;
     94  1.2  christos 		while (n < nproducts && products[n] != 0)
     95  1.1  christos 			n++;
     96  1.1  christos 	}
     97  1.4  pgoyette 	snprintf(buf, len, fmt, product);
     98  1.1  christos 	return NULL;
     99  1.1  christos }
    100