17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*3977d6cdSvikram  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include "cfga_usb.h"
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #define	MAXLINESIZE	512
337c478bd9Sstevel@tonic-gate #define	FE_BUFLEN 256
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #define	isunary(ch)	((ch) == '~' || (ch) == '-')
367c478bd9Sstevel@tonic-gate #define	iswhite(ch)	((ch) == ' ' || (ch) == '\t')
377c478bd9Sstevel@tonic-gate #define	isnewline(ch)	((ch) == '\n' || (ch) == '\r' || (ch) == '\f')
387c478bd9Sstevel@tonic-gate #define	isalphanum(ch)	(isalpha(ch) || isdigit(ch))
397c478bd9Sstevel@tonic-gate #define	isnamechar(ch)	(isalphanum(ch) || (ch) == '_' || (ch) == '-')
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #define	MAX(a, b)	((a) < (b) ? (b) : (a))
427c478bd9Sstevel@tonic-gate #define	GETC(a, cntr)	a[cntr++]
437c478bd9Sstevel@tonic-gate #define	UNGETC(cntr)	cntr--
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate typedef struct usb_configrec {
477c478bd9Sstevel@tonic-gate 	char    *selection;
487c478bd9Sstevel@tonic-gate 	int	idVendor, idProduct, cfgndx;
497c478bd9Sstevel@tonic-gate 	char    *serialno;
507c478bd9Sstevel@tonic-gate 	char    *pathname;
517c478bd9Sstevel@tonic-gate 	char    *driver;
527c478bd9Sstevel@tonic-gate } usb_configrec_t;
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate typedef enum {
557c478bd9Sstevel@tonic-gate 	USB_SELECTION, USB_VENDOR, USB_PRODUCT, USB_CFGNDX, USB_SRNO,
567c478bd9Sstevel@tonic-gate 	USB_PATH, USB_DRIVER, USB_NONE
577c478bd9Sstevel@tonic-gate } config_field_t;
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate typedef struct usbcfg_var {
607c478bd9Sstevel@tonic-gate 	const char *name;
617c478bd9Sstevel@tonic-gate 	config_field_t field;
627c478bd9Sstevel@tonic-gate } usbcfg_var_t;
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate static usbcfg_var_t usbcfg_varlist[] = {
657c478bd9Sstevel@tonic-gate 	{ "selection",	USB_SELECTION },
667c478bd9Sstevel@tonic-gate 	{ "idVendor",	USB_VENDOR },
677c478bd9Sstevel@tonic-gate 	{ "idProduct",	USB_PRODUCT },
687c478bd9Sstevel@tonic-gate 	{ "cfgndx",	USB_CFGNDX },
697c478bd9Sstevel@tonic-gate 	{ "srno",	USB_SRNO },
707c478bd9Sstevel@tonic-gate 	{ "pathname",	USB_PATH },
717c478bd9Sstevel@tonic-gate 	{ "driver",	USB_DRIVER },
727c478bd9Sstevel@tonic-gate 	{ NULL,		USB_NONE }
737c478bd9Sstevel@tonic-gate };
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate typedef enum {
767c478bd9Sstevel@tonic-gate 	EQUALS,
777c478bd9Sstevel@tonic-gate 	AMPERSAND,
787c478bd9Sstevel@tonic-gate 	BIT_OR,
797c478bd9Sstevel@tonic-gate 	STAR,
807c478bd9Sstevel@tonic-gate 	POUND,
817c478bd9Sstevel@tonic-gate 	COLON,
827c478bd9Sstevel@tonic-gate 	SEMICOLON,
837c478bd9Sstevel@tonic-gate 	COMMA,
847c478bd9Sstevel@tonic-gate 	SLASH,
857c478bd9Sstevel@tonic-gate 	WHITE_SPACE,
867c478bd9Sstevel@tonic-gate 	NEWLINE,
877c478bd9Sstevel@tonic-gate 	E_O_F,
887c478bd9Sstevel@tonic-gate 	STRING,
897c478bd9Sstevel@tonic-gate 	HEXVAL,
907c478bd9Sstevel@tonic-gate 	DECVAL,
917c478bd9Sstevel@tonic-gate 	NAME
927c478bd9Sstevel@tonic-gate } token_t;
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate static char	usbconf_file[] = USBCONF_FILE;
967c478bd9Sstevel@tonic-gate static int	linenum = 1;
977c478bd9Sstevel@tonic-gate static int	cntr = 0;
987c478bd9Sstevel@tonic-gate static int	frec = 0;
997c478bd9Sstevel@tonic-gate static int	brec = 0;
1007c478bd9Sstevel@tonic-gate static int	btoken = 0;
1017c478bd9Sstevel@tonic-gate mutex_t		file_lock = DEFAULTMUTEX;
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate /*
1057c478bd9Sstevel@tonic-gate  * prototypes
1067c478bd9Sstevel@tonic-gate  */
1077c478bd9Sstevel@tonic-gate static int	get_string(u_longlong_t *llptr, char *tchar);
1087c478bd9Sstevel@tonic-gate static int	getvalue(char *token, u_longlong_t *valuep);
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate /*
1127c478bd9Sstevel@tonic-gate  * The next item on the line is a string value. Allocate memory for
1137c478bd9Sstevel@tonic-gate  * it and copy the string. Return 1, and set arg ptr to newly allocated
1147c478bd9Sstevel@tonic-gate  * and initialized buffer, or NULL if an error occurs.
1157c478bd9Sstevel@tonic-gate  */
1167c478bd9Sstevel@tonic-gate static int
1177c478bd9Sstevel@tonic-gate get_string(u_longlong_t *llptr, char *tchar)
1187c478bd9Sstevel@tonic-gate {
1197c478bd9Sstevel@tonic-gate 	register char *cp;
1207c478bd9Sstevel@tonic-gate 	register char *start = (char *)0;
1217c478bd9Sstevel@tonic-gate 	register int len = 0;
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 	len = strlen(tchar);
1247c478bd9Sstevel@tonic-gate 	start = tchar;
1257c478bd9Sstevel@tonic-gate 	/* copy string */
1267c478bd9Sstevel@tonic-gate 	cp = (char *)calloc(len + 1, sizeof (char));
1277c478bd9Sstevel@tonic-gate 	if (cp == (char *)NULL) {
1287c478bd9Sstevel@tonic-gate 		*llptr = NULL;
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 		return (0);
1317c478bd9Sstevel@tonic-gate 	}
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 	*llptr = (u_longlong_t)(uintptr_t)cp;
1347c478bd9Sstevel@tonic-gate 	for (; len > 0; len--) {
1357c478bd9Sstevel@tonic-gate 		/* convert some common escape sequences */
1367c478bd9Sstevel@tonic-gate 		if (*start == '\\') {
1377c478bd9Sstevel@tonic-gate 			switch (*(start + 1)) {
1387c478bd9Sstevel@tonic-gate 			case 't':
1397c478bd9Sstevel@tonic-gate 				/* tab */
1407c478bd9Sstevel@tonic-gate 				*cp++ = '\t';
1417c478bd9Sstevel@tonic-gate 				len--;
1427c478bd9Sstevel@tonic-gate 				start += 2;
1437c478bd9Sstevel@tonic-gate 				break;
1447c478bd9Sstevel@tonic-gate 			case 'n':
1457c478bd9Sstevel@tonic-gate 				/* new line */
1467c478bd9Sstevel@tonic-gate 				*cp++ = '\n';
1477c478bd9Sstevel@tonic-gate 				len--;
1487c478bd9Sstevel@tonic-gate 				start += 2;
1497c478bd9Sstevel@tonic-gate 				break;
1507c478bd9Sstevel@tonic-gate 			case 'b':
1517c478bd9Sstevel@tonic-gate 				/* back space */
1527c478bd9Sstevel@tonic-gate 				*cp++ = '\b';
1537c478bd9Sstevel@tonic-gate 				len--;
1547c478bd9Sstevel@tonic-gate 				start += 2;
1557c478bd9Sstevel@tonic-gate 				break;
1567c478bd9Sstevel@tonic-gate 			default:
1577c478bd9Sstevel@tonic-gate 				/* simply copy it */
1587c478bd9Sstevel@tonic-gate 				*cp++ = *start++;
1597c478bd9Sstevel@tonic-gate 				break;
1607c478bd9Sstevel@tonic-gate 			}
1617c478bd9Sstevel@tonic-gate 		} else {
1627c478bd9Sstevel@tonic-gate 			*cp++ = *start++;
1637c478bd9Sstevel@tonic-gate 		}
1647c478bd9Sstevel@tonic-gate 	}
1657c478bd9Sstevel@tonic-gate 	*cp = '\0';
1667c478bd9Sstevel@tonic-gate 	return (1);
1677c478bd9Sstevel@tonic-gate }
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate /*
1717c478bd9Sstevel@tonic-gate  * get a decimal octal or hex number. Handle '~' for one's complement.
1727c478bd9Sstevel@tonic-gate  */
1737c478bd9Sstevel@tonic-gate static int
1747c478bd9Sstevel@tonic-gate getvalue(char *token, u_longlong_t *valuep)
1757c478bd9Sstevel@tonic-gate {
1767c478bd9Sstevel@tonic-gate 	register int radix;
1777c478bd9Sstevel@tonic-gate 	register u_longlong_t retval = 0;
1787c478bd9Sstevel@tonic-gate 	register int onescompl = 0;
1797c478bd9Sstevel@tonic-gate 	register int negate = 0;
1807c478bd9Sstevel@tonic-gate 	register char c;
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	if (*token == '~') {
1837c478bd9Sstevel@tonic-gate 		onescompl++; /* perform one's complement on result */
1847c478bd9Sstevel@tonic-gate 		token++;
1857c478bd9Sstevel@tonic-gate 	} else if (*token == '-') {
1867c478bd9Sstevel@tonic-gate 		negate++;
1877c478bd9Sstevel@tonic-gate 		token++;
1887c478bd9Sstevel@tonic-gate 	}
1897c478bd9Sstevel@tonic-gate 	if (*token == '0') {
1907c478bd9Sstevel@tonic-gate 		token++;
1917c478bd9Sstevel@tonic-gate 		c = *token;
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 		if (c == '\0') {
1947c478bd9Sstevel@tonic-gate 			*valuep = 0;    /* value is 0 */
1957c478bd9Sstevel@tonic-gate 			return (0);
1967c478bd9Sstevel@tonic-gate 		}
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 		if (c == 'x' || c == 'X') {
1997c478bd9Sstevel@tonic-gate 			radix = 16;
2007c478bd9Sstevel@tonic-gate 			token++;
2017c478bd9Sstevel@tonic-gate 		} else {
2027c478bd9Sstevel@tonic-gate 			radix = 8;
2037c478bd9Sstevel@tonic-gate 		}
2047c478bd9Sstevel@tonic-gate 	} else {
2057c478bd9Sstevel@tonic-gate 		radix = 10;
2067c478bd9Sstevel@tonic-gate 	}
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	while ((c = *token++)) {
2097c478bd9Sstevel@tonic-gate 		switch (radix) {
2107c478bd9Sstevel@tonic-gate 		case 8:
2117c478bd9Sstevel@tonic-gate 			if (c >= '0' && c <= '7') {
2127c478bd9Sstevel@tonic-gate 				c -= '0';
2137c478bd9Sstevel@tonic-gate 			} else {
2147c478bd9Sstevel@tonic-gate 				return (-1);    /* invalid number */
2157c478bd9Sstevel@tonic-gate 			}
2167c478bd9Sstevel@tonic-gate 			retval = (retval << 3) + c;
2177c478bd9Sstevel@tonic-gate 			break;
2187c478bd9Sstevel@tonic-gate 		case 10:
2197c478bd9Sstevel@tonic-gate 			if (c >= '0' && c <= '9') {
2207c478bd9Sstevel@tonic-gate 				c -= '0';
2217c478bd9Sstevel@tonic-gate 			} else {
2227c478bd9Sstevel@tonic-gate 				return (-1);    /* invalid number */
2237c478bd9Sstevel@tonic-gate 			}
2247c478bd9Sstevel@tonic-gate 			retval = (retval * 10) + c;
2257c478bd9Sstevel@tonic-gate 			break;
2267c478bd9Sstevel@tonic-gate 		case 16:
2277c478bd9Sstevel@tonic-gate 			if (c >= 'a' && c <= 'f') {
2287c478bd9Sstevel@tonic-gate 				c = c - 'a' + 10;
2297c478bd9Sstevel@tonic-gate 			} else if (c >= 'A' && c <= 'F') {
2307c478bd9Sstevel@tonic-gate 				c = c - 'A' + 10;
2317c478bd9Sstevel@tonic-gate 			} else if (c >= '0' && c <= '9') {
2327c478bd9Sstevel@tonic-gate 				c -= '0';
2337c478bd9Sstevel@tonic-gate 			} else {
2347c478bd9Sstevel@tonic-gate 				return (-1);    /* invalid number */
2357c478bd9Sstevel@tonic-gate 			}
2367c478bd9Sstevel@tonic-gate 			retval = (retval << 4) + c;
2377c478bd9Sstevel@tonic-gate 			break;
2387c478bd9Sstevel@tonic-gate 		}
2397c478bd9Sstevel@tonic-gate 	}
2407c478bd9Sstevel@tonic-gate 	if (onescompl)
2417c478bd9Sstevel@tonic-gate 		retval = ~retval;
2427c478bd9Sstevel@tonic-gate 	if (negate)
2437c478bd9Sstevel@tonic-gate 		retval = -retval;
2447c478bd9Sstevel@tonic-gate 	*valuep = retval;
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 	return (0);
2477c478bd9Sstevel@tonic-gate }
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate /*
2507c478bd9Sstevel@tonic-gate  * returns the field from the token
2517c478bd9Sstevel@tonic-gate  */
2527c478bd9Sstevel@tonic-gate static config_field_t
2537c478bd9Sstevel@tonic-gate usb_get_var_type(char *str)
2547c478bd9Sstevel@tonic-gate {
2557c478bd9Sstevel@tonic-gate 	usbcfg_var_t    *cfgvar;
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	cfgvar = &usbcfg_varlist[0];
2587c478bd9Sstevel@tonic-gate 	while (cfgvar->field != USB_NONE) {
2597c478bd9Sstevel@tonic-gate 		if (strcasecmp(cfgvar->name, str) == NULL) {
2607c478bd9Sstevel@tonic-gate 			break;
2617c478bd9Sstevel@tonic-gate 		} else {
2627c478bd9Sstevel@tonic-gate 			cfgvar++;
2637c478bd9Sstevel@tonic-gate 		}
2647c478bd9Sstevel@tonic-gate 	}
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	return (cfgvar->field);
2677c478bd9Sstevel@tonic-gate }
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate /* ARGSUSED */
2717c478bd9Sstevel@tonic-gate static token_t
2727c478bd9Sstevel@tonic-gate lex(char *buf, char *val, char **errmsg)
2737c478bd9Sstevel@tonic-gate {
2747c478bd9Sstevel@tonic-gate 	int	ch, oval, badquote;
2757c478bd9Sstevel@tonic-gate 	char	*cp;
2767c478bd9Sstevel@tonic-gate 	token_t token;
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 	cp = val;
2797c478bd9Sstevel@tonic-gate 	while ((ch = GETC(buf, cntr)) == ' ' || ch == '\t');
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 	/*
2827c478bd9Sstevel@tonic-gate 	 * Note the beginning of a token
2837c478bd9Sstevel@tonic-gate 	 */
2847c478bd9Sstevel@tonic-gate 	btoken = cntr - 1;
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 	*cp++ = (char)ch;
2877c478bd9Sstevel@tonic-gate 	switch (ch) {
2887c478bd9Sstevel@tonic-gate 	case '=':
2897c478bd9Sstevel@tonic-gate 		token = EQUALS;
2907c478bd9Sstevel@tonic-gate 		break;
2917c478bd9Sstevel@tonic-gate 	case '&':
2927c478bd9Sstevel@tonic-gate 		token = AMPERSAND;
2937c478bd9Sstevel@tonic-gate 		break;
2947c478bd9Sstevel@tonic-gate 	case '|':
2957c478bd9Sstevel@tonic-gate 		token = BIT_OR;
2967c478bd9Sstevel@tonic-gate 		break;
2977c478bd9Sstevel@tonic-gate 	case '*':
2987c478bd9Sstevel@tonic-gate 		token = STAR;
2997c478bd9Sstevel@tonic-gate 		break;
3007c478bd9Sstevel@tonic-gate 	case '#':
3017c478bd9Sstevel@tonic-gate 		token = POUND;
3027c478bd9Sstevel@tonic-gate 		break;
3037c478bd9Sstevel@tonic-gate 	case ':':
3047c478bd9Sstevel@tonic-gate 		token = COLON;
3057c478bd9Sstevel@tonic-gate 		break;
3067c478bd9Sstevel@tonic-gate 	case ';':
3077c478bd9Sstevel@tonic-gate 		token = SEMICOLON;
3087c478bd9Sstevel@tonic-gate 		break;
3097c478bd9Sstevel@tonic-gate 	case ',':
3107c478bd9Sstevel@tonic-gate 		token = COMMA;
3117c478bd9Sstevel@tonic-gate 		break;
3127c478bd9Sstevel@tonic-gate 	case '/':
3137c478bd9Sstevel@tonic-gate 		token = SLASH;
3147c478bd9Sstevel@tonic-gate 		break;
3157c478bd9Sstevel@tonic-gate 	case ' ':
3167c478bd9Sstevel@tonic-gate 	case '\t':
3177c478bd9Sstevel@tonic-gate 	case '\f':
3187c478bd9Sstevel@tonic-gate 		while ((ch  = GETC(buf, cntr)) == ' ' ||
3197c478bd9Sstevel@tonic-gate 		    ch == '\t' || ch == '\f')
3207c478bd9Sstevel@tonic-gate 			*cp++ = (char)ch;
3217c478bd9Sstevel@tonic-gate 		(void) UNGETC(cntr);
3227c478bd9Sstevel@tonic-gate 		token = WHITE_SPACE;
3237c478bd9Sstevel@tonic-gate 		break;
3247c478bd9Sstevel@tonic-gate 	case '\n':
3257c478bd9Sstevel@tonic-gate 	case '\r':
3267c478bd9Sstevel@tonic-gate 		token = NEWLINE;
3277c478bd9Sstevel@tonic-gate 		break;
3287c478bd9Sstevel@tonic-gate 	case '"':
3297c478bd9Sstevel@tonic-gate 		cp--;
3307c478bd9Sstevel@tonic-gate 		badquote = 0;
3317c478bd9Sstevel@tonic-gate 		while (!badquote && (ch  = GETC(buf, cntr)) != '"') {
3327c478bd9Sstevel@tonic-gate 			switch (ch) {
3337c478bd9Sstevel@tonic-gate 			case '\n':
3347c478bd9Sstevel@tonic-gate 			case -1:
3357c478bd9Sstevel@tonic-gate 				(void) snprintf(*errmsg, MAXPATHLEN,
3367c478bd9Sstevel@tonic-gate 				    "Missing \"");
3377c478bd9Sstevel@tonic-gate 				cp = val;
3387c478bd9Sstevel@tonic-gate 				*cp++ = '\n';
3397c478bd9Sstevel@tonic-gate 				badquote = 1;
3407c478bd9Sstevel@tonic-gate 				/* since we consumed the newline/EOF */
3417c478bd9Sstevel@tonic-gate 				(void) UNGETC(cntr);
3427c478bd9Sstevel@tonic-gate 				break;
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 			case '\\':
3457c478bd9Sstevel@tonic-gate 				ch = (char)GETC(buf, cntr);
3467c478bd9Sstevel@tonic-gate 				if (!isdigit(ch)) {
3477c478bd9Sstevel@tonic-gate 					/* escape the character */
3487c478bd9Sstevel@tonic-gate 					*cp++ = (char)ch;
3497c478bd9Sstevel@tonic-gate 					break;
3507c478bd9Sstevel@tonic-gate 				}
3517c478bd9Sstevel@tonic-gate 				oval = 0;
3527c478bd9Sstevel@tonic-gate 				while (ch >= '0' && ch <= '7') {
3537c478bd9Sstevel@tonic-gate 					ch -= '0';
3547c478bd9Sstevel@tonic-gate 					oval = (oval << 3) + ch;
3557c478bd9Sstevel@tonic-gate 					ch = (char)GETC(buf, cntr);
3567c478bd9Sstevel@tonic-gate 				}
3577c478bd9Sstevel@tonic-gate 				(void) UNGETC(cntr);
3587c478bd9Sstevel@tonic-gate 				/* check for character overflow? */
3597c478bd9Sstevel@tonic-gate 				if (oval > 127) {
3607c478bd9Sstevel@tonic-gate 					(void) snprintf(*errmsg, MAXPATHLEN,
3617c478bd9Sstevel@tonic-gate 					    "Character overflow detected.\n");
3627c478bd9Sstevel@tonic-gate 				}
3637c478bd9Sstevel@tonic-gate 				*cp++ = (char)oval;
3647c478bd9Sstevel@tonic-gate 				break;
3657c478bd9Sstevel@tonic-gate 			default:
3667c478bd9Sstevel@tonic-gate 				*cp++ = (char)ch;
3677c478bd9Sstevel@tonic-gate 				break;
3687c478bd9Sstevel@tonic-gate 			}
3697c478bd9Sstevel@tonic-gate 		}
3707c478bd9Sstevel@tonic-gate 		token = STRING;
3717c478bd9Sstevel@tonic-gate 		break;
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 	default:
3747c478bd9Sstevel@tonic-gate 		if (ch == -1) {
3757c478bd9Sstevel@tonic-gate 			token = EOF;
3767c478bd9Sstevel@tonic-gate 			break;
3777c478bd9Sstevel@tonic-gate 		}
3787c478bd9Sstevel@tonic-gate 		/*
3797c478bd9Sstevel@tonic-gate 		 * detect a lone '-' (including at the end of a line), and
3807c478bd9Sstevel@tonic-gate 		 * identify it as a 'name'
3817c478bd9Sstevel@tonic-gate 		 */
3827c478bd9Sstevel@tonic-gate 		if (ch == '-') {
3837c478bd9Sstevel@tonic-gate 			*cp++ = (char)(ch = GETC(buf, cntr));
3847c478bd9Sstevel@tonic-gate 			if (iswhite(ch) || (ch == '\n')) {
3857c478bd9Sstevel@tonic-gate 				(void) UNGETC(cntr);
3867c478bd9Sstevel@tonic-gate 				cp--;
3877c478bd9Sstevel@tonic-gate 				token = NAME;
3887c478bd9Sstevel@tonic-gate 				break;
3897c478bd9Sstevel@tonic-gate 			}
3907c478bd9Sstevel@tonic-gate 		} else if (isunary(ch)) {
3917c478bd9Sstevel@tonic-gate 			*cp++ = (char)(ch = GETC(buf, cntr));
3927c478bd9Sstevel@tonic-gate 		}
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 		if (isdigit(ch)) {
3957c478bd9Sstevel@tonic-gate 			if (ch == '0') {
3967c478bd9Sstevel@tonic-gate 				if ((ch = GETC(buf, cntr)) == 'x') {
3977c478bd9Sstevel@tonic-gate 					*cp++ = (char)ch;
3987c478bd9Sstevel@tonic-gate 					ch = GETC(buf, cntr);
3997c478bd9Sstevel@tonic-gate 					while (isxdigit(ch)) {
4007c478bd9Sstevel@tonic-gate 						*cp++ = (char)ch;
4017c478bd9Sstevel@tonic-gate 						ch = GETC(buf, cntr);
4027c478bd9Sstevel@tonic-gate 					}
4037c478bd9Sstevel@tonic-gate 					(void) UNGETC(cntr);
4047c478bd9Sstevel@tonic-gate 					token = HEXVAL;
4057c478bd9Sstevel@tonic-gate 				} else {
4067c478bd9Sstevel@tonic-gate 					goto digit;
4077c478bd9Sstevel@tonic-gate 				}
4087c478bd9Sstevel@tonic-gate 			} else {
4097c478bd9Sstevel@tonic-gate 				ch = GETC(buf, cntr);
4107c478bd9Sstevel@tonic-gate digit:
4117c478bd9Sstevel@tonic-gate 				while (isdigit(ch)) {
4127c478bd9Sstevel@tonic-gate 					*cp++ = (char)ch;
4137c478bd9Sstevel@tonic-gate 					ch = GETC(buf, cntr);
4147c478bd9Sstevel@tonic-gate 				}
4157c478bd9Sstevel@tonic-gate 				(void) UNGETC(cntr);
4167c478bd9Sstevel@tonic-gate 				token = DECVAL;
4177c478bd9Sstevel@tonic-gate 			}
4187c478bd9Sstevel@tonic-gate 		} else if (isalpha(ch) || ch == '\\') {
4197c478bd9Sstevel@tonic-gate 			if (ch != '\\') {
4207c478bd9Sstevel@tonic-gate 				ch = GETC(buf, cntr);
4217c478bd9Sstevel@tonic-gate 			} else {
4227c478bd9Sstevel@tonic-gate 				/*
4237c478bd9Sstevel@tonic-gate 				 * if the character was a backslash,
4247c478bd9Sstevel@tonic-gate 				 * back up so we can overwrite it with
4257c478bd9Sstevel@tonic-gate 				 * the next (i.e. escaped) character.
4267c478bd9Sstevel@tonic-gate 				 */
4277c478bd9Sstevel@tonic-gate 				cp--;
4287c478bd9Sstevel@tonic-gate 			}
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate 			while (isnamechar(ch) || ch == '\\') {
4317c478bd9Sstevel@tonic-gate 				if (ch == '\\')
4327c478bd9Sstevel@tonic-gate 					ch = GETC(buf, cntr);
4337c478bd9Sstevel@tonic-gate 				*cp++ = (char)ch;
4347c478bd9Sstevel@tonic-gate 				ch = GETC(buf, cntr);
4357c478bd9Sstevel@tonic-gate 			}
4367c478bd9Sstevel@tonic-gate 			(void) UNGETC(cntr);
4377c478bd9Sstevel@tonic-gate 			token = NAME;
4387c478bd9Sstevel@tonic-gate 		} else {
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 			return (-1);
4417c478bd9Sstevel@tonic-gate 		}
4427c478bd9Sstevel@tonic-gate 		break;
4437c478bd9Sstevel@tonic-gate 	}
4447c478bd9Sstevel@tonic-gate 	*cp = '\0';
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 	return (token);
4477c478bd9Sstevel@tonic-gate }
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate /*
4517c478bd9Sstevel@tonic-gate  * Leave NEWLINE as the next character.
4527c478bd9Sstevel@tonic-gate  */
4537c478bd9Sstevel@tonic-gate static void
4547c478bd9Sstevel@tonic-gate find_eol(char *buf)
4557c478bd9Sstevel@tonic-gate {
4567c478bd9Sstevel@tonic-gate 	register int ch;
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 	while ((ch = GETC(buf, cntr)) != -1) {
4597c478bd9Sstevel@tonic-gate 		if (isnewline(ch)) {
4607c478bd9Sstevel@tonic-gate 			(void) UNGETC(cntr);
4617c478bd9Sstevel@tonic-gate 			break;
4627c478bd9Sstevel@tonic-gate 		}
4637c478bd9Sstevel@tonic-gate 	}
4647c478bd9Sstevel@tonic-gate }
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate /*
4687c478bd9Sstevel@tonic-gate  * Fetch one record from the USBCONF_FILE
4697c478bd9Sstevel@tonic-gate  */
4707c478bd9Sstevel@tonic-gate static token_t
4717c478bd9Sstevel@tonic-gate usb_get_conf_rec(char *buf, usb_configrec_t **rec, char **errmsg)
4727c478bd9Sstevel@tonic-gate {
4737c478bd9Sstevel@tonic-gate 	token_t token;
4747c478bd9Sstevel@tonic-gate 	char tokval[MAXLINESIZE];
4757c478bd9Sstevel@tonic-gate 	usb_configrec_t *user_rec;
4767c478bd9Sstevel@tonic-gate 	config_field_t  cfgvar;
4777c478bd9Sstevel@tonic-gate 	u_longlong_t    llptr;
4787c478bd9Sstevel@tonic-gate 	u_longlong_t    value;
4797c478bd9Sstevel@tonic-gate 	boolean_t	sor = B_TRUE;
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 	enum {
4827c478bd9Sstevel@tonic-gate 		USB_NEWVAR, USB_CONFIG_VAR, USB_VAR_EQUAL, USB_VAR_VALUE,
4837c478bd9Sstevel@tonic-gate 		USB_ERROR
4847c478bd9Sstevel@tonic-gate 	} parse_state = USB_NEWVAR;
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate 	DPRINTF("usb_get_conf_rec:\n");
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate 	user_rec = (usb_configrec_t *)calloc(1, sizeof (usb_configrec_t));
4897c478bd9Sstevel@tonic-gate 	if (user_rec == (usb_configrec_t *)NULL) {
4907c478bd9Sstevel@tonic-gate 		return (0);
4917c478bd9Sstevel@tonic-gate 	}
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate 	user_rec->idVendor = user_rec->idProduct = user_rec->cfgndx = -1;
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate 	token = lex(buf, tokval, errmsg);
4967c478bd9Sstevel@tonic-gate 	while ((token != EOF) && (token != SEMICOLON)) {
4977c478bd9Sstevel@tonic-gate 		switch (token) {
4987c478bd9Sstevel@tonic-gate 		case STAR:
4997c478bd9Sstevel@tonic-gate 		case POUND:
5007c478bd9Sstevel@tonic-gate 			/* skip comments */
5017c478bd9Sstevel@tonic-gate 			find_eol(buf);
5027c478bd9Sstevel@tonic-gate 			break;
5037c478bd9Sstevel@tonic-gate 		case NEWLINE:
5047c478bd9Sstevel@tonic-gate 			linenum++;
5057c478bd9Sstevel@tonic-gate 			break;
5067c478bd9Sstevel@tonic-gate 		case NAME:
5077c478bd9Sstevel@tonic-gate 		case STRING:
5087c478bd9Sstevel@tonic-gate 			switch (parse_state) {
5097c478bd9Sstevel@tonic-gate 			case USB_NEWVAR:
5107c478bd9Sstevel@tonic-gate 				cfgvar = usb_get_var_type(tokval);
5117c478bd9Sstevel@tonic-gate 				if (cfgvar == USB_NONE) {
5127c478bd9Sstevel@tonic-gate 					parse_state = USB_ERROR;
5137c478bd9Sstevel@tonic-gate 					(void) snprintf(*errmsg, MAXPATHLEN,
5147c478bd9Sstevel@tonic-gate 					    "Syntax Error: Invalid field %s",
5157c478bd9Sstevel@tonic-gate 					    tokval);
5167c478bd9Sstevel@tonic-gate 				} else {
5177c478bd9Sstevel@tonic-gate 					/*
5187c478bd9Sstevel@tonic-gate 					 * Note the beginning of a record
5197c478bd9Sstevel@tonic-gate 					 */
5207c478bd9Sstevel@tonic-gate 					if (sor) {
5217c478bd9Sstevel@tonic-gate 						brec = btoken;
5227c478bd9Sstevel@tonic-gate 						if (frec == 0) frec = brec;
5237c478bd9Sstevel@tonic-gate 						sor = B_FALSE;
5247c478bd9Sstevel@tonic-gate 					}
5257c478bd9Sstevel@tonic-gate 					parse_state = USB_CONFIG_VAR;
5267c478bd9Sstevel@tonic-gate 				}
5277c478bd9Sstevel@tonic-gate 				break;
5287c478bd9Sstevel@tonic-gate 			case USB_VAR_VALUE:
5297c478bd9Sstevel@tonic-gate 				if ((cfgvar == USB_VENDOR) ||
5307c478bd9Sstevel@tonic-gate 				    (cfgvar == USB_PRODUCT) ||
5317c478bd9Sstevel@tonic-gate 				    (cfgvar == USB_CFGNDX)) {
5327c478bd9Sstevel@tonic-gate 					parse_state = USB_ERROR;
5337c478bd9Sstevel@tonic-gate 					(void) snprintf(*errmsg, MAXPATHLEN,
5347c478bd9Sstevel@tonic-gate 					    "Syntax Error: Invalid value %s "
5357c478bd9Sstevel@tonic-gate 					    "for field: %s\n", tokval,
5367c478bd9Sstevel@tonic-gate 					    usbcfg_varlist[cfgvar].name);
5377c478bd9Sstevel@tonic-gate 				} else if (get_string(&llptr, tokval)) {
5387c478bd9Sstevel@tonic-gate 					switch (cfgvar) {
5397c478bd9Sstevel@tonic-gate 					case USB_SELECTION:
5407c478bd9Sstevel@tonic-gate 						user_rec->selection =
541*3977d6cdSvikram 						    (char *)(uintptr_t)llptr;
5427c478bd9Sstevel@tonic-gate 						parse_state = USB_NEWVAR;
5437c478bd9Sstevel@tonic-gate 						break;
5447c478bd9Sstevel@tonic-gate 					case USB_SRNO:
5457c478bd9Sstevel@tonic-gate 						user_rec->serialno =
546*3977d6cdSvikram 						    (char *)(uintptr_t)llptr;
5477c478bd9Sstevel@tonic-gate 						parse_state = USB_NEWVAR;
5487c478bd9Sstevel@tonic-gate 						break;
5497c478bd9Sstevel@tonic-gate 					case USB_PATH:
5507c478bd9Sstevel@tonic-gate 						user_rec->pathname =
551*3977d6cdSvikram 						    (char *)(uintptr_t)llptr;
5527c478bd9Sstevel@tonic-gate 						parse_state = USB_NEWVAR;
5537c478bd9Sstevel@tonic-gate 						break;
5547c478bd9Sstevel@tonic-gate 					case USB_DRIVER:
5557c478bd9Sstevel@tonic-gate 						user_rec->driver =
556*3977d6cdSvikram 						    (char *)(uintptr_t)llptr;
5577c478bd9Sstevel@tonic-gate 						parse_state = USB_NEWVAR;
5587c478bd9Sstevel@tonic-gate 						break;
5597c478bd9Sstevel@tonic-gate 					default:
5607c478bd9Sstevel@tonic-gate 						parse_state = USB_ERROR;
561*3977d6cdSvikram 						free((char *)(uintptr_t)llptr);
5627c478bd9Sstevel@tonic-gate 					}
5637c478bd9Sstevel@tonic-gate 				} else {
5647c478bd9Sstevel@tonic-gate 					parse_state = USB_ERROR;
5657c478bd9Sstevel@tonic-gate 					(void) snprintf(*errmsg, MAXPATHLEN,
5667c478bd9Sstevel@tonic-gate 					    "Syntax Error: Invalid value %s "
5677c478bd9Sstevel@tonic-gate 					    "for field: %s\n", tokval,
5687c478bd9Sstevel@tonic-gate 					    usbcfg_varlist[cfgvar].name);
5697c478bd9Sstevel@tonic-gate 				}
5707c478bd9Sstevel@tonic-gate 				break;
5717c478bd9Sstevel@tonic-gate 			case USB_ERROR:
5727c478bd9Sstevel@tonic-gate 				/* just skip */
5737c478bd9Sstevel@tonic-gate 				break;
5747c478bd9Sstevel@tonic-gate 			default:
5757c478bd9Sstevel@tonic-gate 				parse_state = USB_ERROR;
5767c478bd9Sstevel@tonic-gate 				(void) snprintf(*errmsg, MAXPATHLEN,
5777c478bd9Sstevel@tonic-gate 				    "Syntax Error: at %s", tokval);
5787c478bd9Sstevel@tonic-gate 				break;
5797c478bd9Sstevel@tonic-gate 			}
5807c478bd9Sstevel@tonic-gate 			break;
5817c478bd9Sstevel@tonic-gate 		case EQUALS:
5827c478bd9Sstevel@tonic-gate 			if (parse_state == USB_CONFIG_VAR) {
5837c478bd9Sstevel@tonic-gate 				if (cfgvar == USB_NONE) {
5847c478bd9Sstevel@tonic-gate 					parse_state = USB_ERROR;
5857c478bd9Sstevel@tonic-gate 					(void) snprintf(*errmsg, MAXPATHLEN,
5867c478bd9Sstevel@tonic-gate 					    "Syntax Error: unexpected '='");
5877c478bd9Sstevel@tonic-gate 				} else {
5887c478bd9Sstevel@tonic-gate 					parse_state = USB_VAR_VALUE;
5897c478bd9Sstevel@tonic-gate 				}
5907c478bd9Sstevel@tonic-gate 			} else if (parse_state != USB_ERROR) {
5917c478bd9Sstevel@tonic-gate 				(void) snprintf(*errmsg, MAXPATHLEN,
5927c478bd9Sstevel@tonic-gate 				    "Syntax Error: unexpected '='");
5937c478bd9Sstevel@tonic-gate 				parse_state = USB_ERROR;
5947c478bd9Sstevel@tonic-gate 			}
5957c478bd9Sstevel@tonic-gate 			break;
5967c478bd9Sstevel@tonic-gate 		case HEXVAL:
5977c478bd9Sstevel@tonic-gate 		case DECVAL:
5987c478bd9Sstevel@tonic-gate 			if ((parse_state == USB_VAR_VALUE) && (cfgvar !=
5997c478bd9Sstevel@tonic-gate 			    USB_NONE)) {
6007c478bd9Sstevel@tonic-gate 				(void) getvalue(tokval, &value);
6017c478bd9Sstevel@tonic-gate 				switch (cfgvar) {
6027c478bd9Sstevel@tonic-gate 				case USB_VENDOR:
6037c478bd9Sstevel@tonic-gate 					user_rec->idVendor = (int)value;
6047c478bd9Sstevel@tonic-gate 					parse_state = USB_NEWVAR;
6057c478bd9Sstevel@tonic-gate 					break;
6067c478bd9Sstevel@tonic-gate 				case USB_PRODUCT:
6077c478bd9Sstevel@tonic-gate 					user_rec->idProduct = (int)value;
6087c478bd9Sstevel@tonic-gate 					parse_state = USB_NEWVAR;
6097c478bd9Sstevel@tonic-gate 					break;
6107c478bd9Sstevel@tonic-gate 				case USB_CFGNDX:
6117c478bd9Sstevel@tonic-gate 					user_rec->cfgndx = (int)value;
6127c478bd9Sstevel@tonic-gate 					parse_state = USB_NEWVAR;
6137c478bd9Sstevel@tonic-gate 					break;
6147c478bd9Sstevel@tonic-gate 				default:
6157c478bd9Sstevel@tonic-gate 					(void) snprintf(*errmsg, MAXPATHLEN,
6167c478bd9Sstevel@tonic-gate 					    "Syntax Error: Invalid value for "
6177c478bd9Sstevel@tonic-gate 					    "%s", usbcfg_varlist[cfgvar].name);
6187c478bd9Sstevel@tonic-gate 				}
6197c478bd9Sstevel@tonic-gate 			} else if (parse_state != USB_ERROR) {
6207c478bd9Sstevel@tonic-gate 				parse_state = USB_ERROR;
6217c478bd9Sstevel@tonic-gate 				(void) snprintf(*errmsg, MAXPATHLEN,
6227c478bd9Sstevel@tonic-gate 				    "Syntax Error: unexpected hex/decimal: %s",
6237c478bd9Sstevel@tonic-gate 				    tokval);
6247c478bd9Sstevel@tonic-gate 			}
6257c478bd9Sstevel@tonic-gate 			break;
6267c478bd9Sstevel@tonic-gate 		default:
6277c478bd9Sstevel@tonic-gate 			(void) snprintf(*errmsg, MAXPATHLEN,
6287c478bd9Sstevel@tonic-gate 			    "Syntax Error: at: %s", tokval);
6297c478bd9Sstevel@tonic-gate 			parse_state = USB_ERROR;
6307c478bd9Sstevel@tonic-gate 			break;
6317c478bd9Sstevel@tonic-gate 		}
6327c478bd9Sstevel@tonic-gate 		token = lex(buf, tokval, errmsg);
6337c478bd9Sstevel@tonic-gate 	}
6347c478bd9Sstevel@tonic-gate 	*rec = user_rec;
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate 	return (token);
6377c478bd9Sstevel@tonic-gate }
6387c478bd9Sstevel@tonic-gate 
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate /*
6417c478bd9Sstevel@tonic-gate  * Here we compare the two records and determine if they are the same
6427c478bd9Sstevel@tonic-gate  */
6437c478bd9Sstevel@tonic-gate static boolean_t
6447c478bd9Sstevel@tonic-gate usb_cmp_rec(usb_configrec_t *cfg_rec, usb_configrec_t *user_rec)
6457c478bd9Sstevel@tonic-gate {
6467c478bd9Sstevel@tonic-gate 	char		*ustr, *cstr;
6477c478bd9Sstevel@tonic-gate 	boolean_t	srno = B_FALSE, path = B_FALSE;
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate 	DPRINTF("usb_cmp_rec:\n");
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate 	if ((cfg_rec->idVendor == user_rec->idVendor) &&
6527c478bd9Sstevel@tonic-gate 	    (cfg_rec->idProduct == user_rec->idProduct)) {
6537c478bd9Sstevel@tonic-gate 		if (user_rec->serialno) {
6547c478bd9Sstevel@tonic-gate 			if (cfg_rec->serialno) {
6557c478bd9Sstevel@tonic-gate 				srno = (strcmp(cfg_rec->serialno,
6567c478bd9Sstevel@tonic-gate 				    user_rec->serialno) == 0);
6577c478bd9Sstevel@tonic-gate 			} else {
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate 				return (B_FALSE);
6607c478bd9Sstevel@tonic-gate 			}
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate 		} else if (user_rec->pathname) {
6637c478bd9Sstevel@tonic-gate 			if (cfg_rec->pathname) {
6647c478bd9Sstevel@tonic-gate 				/*
6657c478bd9Sstevel@tonic-gate 				 * Comparing on this is tricky. At this point
6667c478bd9Sstevel@tonic-gate 				 * hubd knows: ../hubd@P/device@P while user
6677c478bd9Sstevel@tonic-gate 				 * will specify ..../hubd@P/keyboard@P
6687c478bd9Sstevel@tonic-gate 				 * First compare till .../hubd@P
6697c478bd9Sstevel@tonic-gate 				 * Second compare is just P in "device@P"
6707c478bd9Sstevel@tonic-gate 				 *
6717c478bd9Sstevel@tonic-gate 				 * XXX: note that we assume P as one character
6727c478bd9Sstevel@tonic-gate 				 * as there are no 2 digit hubs in the market.
6737c478bd9Sstevel@tonic-gate 				 */
6747c478bd9Sstevel@tonic-gate 				ustr = strrchr(user_rec->pathname, '/');
6757c478bd9Sstevel@tonic-gate 				cstr = strrchr(cfg_rec->pathname, '/');
6767c478bd9Sstevel@tonic-gate 				path = (strncmp(cfg_rec->pathname,
6777c478bd9Sstevel@tonic-gate 				    user_rec->pathname,
6787c478bd9Sstevel@tonic-gate 				    MAX(ustr - user_rec->pathname,
6797c478bd9Sstevel@tonic-gate 				    cstr - cfg_rec->pathname)) == 0);
6807c478bd9Sstevel@tonic-gate 				path = path && (*(user_rec->pathname +
6817c478bd9Sstevel@tonic-gate 				    strlen(user_rec->pathname) -1) ==
6827c478bd9Sstevel@tonic-gate 					*(cfg_rec->pathname +
6837c478bd9Sstevel@tonic-gate 					strlen(cfg_rec->pathname) - 1));
6847c478bd9Sstevel@tonic-gate 			} else {
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate 				return (B_FALSE);
6877c478bd9Sstevel@tonic-gate 			}
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate 		} else if (cfg_rec->serialno || cfg_rec->pathname) {
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate 			return (B_FALSE);
6927c478bd9Sstevel@tonic-gate 		} else {
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate 			return (B_TRUE);
6957c478bd9Sstevel@tonic-gate 		}
6967c478bd9Sstevel@tonic-gate 
6977c478bd9Sstevel@tonic-gate 		return (srno || path);
6987c478bd9Sstevel@tonic-gate 	} else {
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate 		return (B_FALSE);
7017c478bd9Sstevel@tonic-gate 	}
7027c478bd9Sstevel@tonic-gate }
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate 
7057c478bd9Sstevel@tonic-gate /*
7067c478bd9Sstevel@tonic-gate  * free the record allocated in usb_get_conf_rec
7077c478bd9Sstevel@tonic-gate  */
7087c478bd9Sstevel@tonic-gate static void
7097c478bd9Sstevel@tonic-gate usb_free_rec(usb_configrec_t *rec)
7107c478bd9Sstevel@tonic-gate {
7117c478bd9Sstevel@tonic-gate 	if (rec == (usb_configrec_t *)NULL) {
7127c478bd9Sstevel@tonic-gate 
7137c478bd9Sstevel@tonic-gate 		return;
7147c478bd9Sstevel@tonic-gate 	}
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate 	free(rec->selection);
7177c478bd9Sstevel@tonic-gate 	free(rec->serialno);
7187c478bd9Sstevel@tonic-gate 	free(rec->pathname);
7197c478bd9Sstevel@tonic-gate 	free(rec->driver);
7207c478bd9Sstevel@tonic-gate 	free(rec);
7217c478bd9Sstevel@tonic-gate }
7227c478bd9Sstevel@tonic-gate 
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate int
7257c478bd9Sstevel@tonic-gate add_entry(char *selection, int vid, int pid, int cfgndx, char *srno,
7267c478bd9Sstevel@tonic-gate     char *path, char *driver, char **errmsg)
7277c478bd9Sstevel@tonic-gate {
7287c478bd9Sstevel@tonic-gate 	int		file;
7297c478bd9Sstevel@tonic-gate 	int		rval = CFGA_USB_OK;
7307c478bd9Sstevel@tonic-gate 	char		*buf = (char *)NULL;
7317c478bd9Sstevel@tonic-gate 	char		str[MAXLINESIZE];
7327c478bd9Sstevel@tonic-gate 	token_t		token = NEWLINE;
7337c478bd9Sstevel@tonic-gate 	boolean_t	found = B_FALSE;
7347c478bd9Sstevel@tonic-gate 	struct stat	st;
7357c478bd9Sstevel@tonic-gate 	usb_configrec_t cfgrec, *user_rec = NULL;
7367c478bd9Sstevel@tonic-gate 
7377c478bd9Sstevel@tonic-gate 	DPRINTF("add_entry: driver=%s, path=%s\n",
7387c478bd9Sstevel@tonic-gate 	    driver ? driver : "", path ? path : "");
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate 	if (*errmsg == (char *)NULL) {
7417c478bd9Sstevel@tonic-gate 		if ((*errmsg = calloc(MAXPATHLEN, 1)) == (char *)NULL) {
7427c478bd9Sstevel@tonic-gate 
7437c478bd9Sstevel@tonic-gate 			return (CFGA_USB_CONFIG_FILE);
7447c478bd9Sstevel@tonic-gate 		}
7457c478bd9Sstevel@tonic-gate 	}
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&file_lock);
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate 	/* Initialize the cfgrec */
7507c478bd9Sstevel@tonic-gate 	cfgrec.selection = selection;
7517c478bd9Sstevel@tonic-gate 	cfgrec.idVendor = vid;
7527c478bd9Sstevel@tonic-gate 	cfgrec.idProduct = pid;
7537c478bd9Sstevel@tonic-gate 	cfgrec.cfgndx = cfgndx;
7547c478bd9Sstevel@tonic-gate 	cfgrec.serialno = srno;
7557c478bd9Sstevel@tonic-gate 	cfgrec.pathname = path;
7567c478bd9Sstevel@tonic-gate 	cfgrec.driver = driver;
7577c478bd9Sstevel@tonic-gate 
7587c478bd9Sstevel@tonic-gate 	/* open config_map.conf file */
7597c478bd9Sstevel@tonic-gate 	file = open(usbconf_file, O_RDWR, 0666);
7607c478bd9Sstevel@tonic-gate 	if (file == -1) {
7617c478bd9Sstevel@tonic-gate 		(void) snprintf(*errmsg, MAXPATHLEN,
7627c478bd9Sstevel@tonic-gate 		    "failed to open config file\n");
7637c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&file_lock);
7647c478bd9Sstevel@tonic-gate 
7657c478bd9Sstevel@tonic-gate 		return (CFGA_USB_CONFIG_FILE);
7667c478bd9Sstevel@tonic-gate 	}
7677c478bd9Sstevel@tonic-gate 
7687c478bd9Sstevel@tonic-gate 	if (lockf(file, F_TLOCK, 0) == -1) {
7697c478bd9Sstevel@tonic-gate 		(void) snprintf(*errmsg, MAXPATHLEN,
7707c478bd9Sstevel@tonic-gate 		    "failed to lock config file\n");
7717c478bd9Sstevel@tonic-gate 		close(file);
7727c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&file_lock);
7737c478bd9Sstevel@tonic-gate 
7747c478bd9Sstevel@tonic-gate 		return (CFGA_USB_LOCK_FILE);
7757c478bd9Sstevel@tonic-gate 	}
7767c478bd9Sstevel@tonic-gate 
7777c478bd9Sstevel@tonic-gate 	/*
7787c478bd9Sstevel@tonic-gate 	 * These variables need to be reinitialized here as they may
7797c478bd9Sstevel@tonic-gate 	 * have been modified by a previous thread that called this
7807c478bd9Sstevel@tonic-gate 	 * function
7817c478bd9Sstevel@tonic-gate 	 */
7827c478bd9Sstevel@tonic-gate 	linenum = 1;
7837c478bd9Sstevel@tonic-gate 	cntr = 0;
7847c478bd9Sstevel@tonic-gate 	frec = 0;
7857c478bd9Sstevel@tonic-gate 	brec = 0;
7867c478bd9Sstevel@tonic-gate 	btoken = 0;
7877c478bd9Sstevel@tonic-gate 
7887c478bd9Sstevel@tonic-gate 	if (fstat(file, &st) != 0) {
7897c478bd9Sstevel@tonic-gate 		DPRINTF("add_entry: failed to fstat config file\n");
7907c478bd9Sstevel@tonic-gate 		rval = CFGA_USB_CONFIG_FILE;
7917c478bd9Sstevel@tonic-gate 		goto exit;
7927c478bd9Sstevel@tonic-gate 	}
7937c478bd9Sstevel@tonic-gate 
7947c478bd9Sstevel@tonic-gate 	if ((buf = (char *)malloc(st.st_size)) == NULL) {
7957c478bd9Sstevel@tonic-gate 		DPRINTF("add_entry: failed to fstat config file\n");
7967c478bd9Sstevel@tonic-gate 		rval = CFGA_USB_ALLOC_FAIL;
7977c478bd9Sstevel@tonic-gate 		goto exit;
7987c478bd9Sstevel@tonic-gate 	}
7997c478bd9Sstevel@tonic-gate 
8007c478bd9Sstevel@tonic-gate 	if (st.st_size != read(file, buf, st.st_size)) {
8017c478bd9Sstevel@tonic-gate 		DPRINTF("add_entry: failed to read config file\n");
8027c478bd9Sstevel@tonic-gate 		rval = CFGA_USB_CONFIG_FILE;
8037c478bd9Sstevel@tonic-gate 		goto exit;
8047c478bd9Sstevel@tonic-gate 	}
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate 	/* set up for reading the file */
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate 	while ((token != EOF) && !found) {
8097c478bd9Sstevel@tonic-gate 		if (user_rec) {
8107c478bd9Sstevel@tonic-gate 			usb_free_rec(user_rec);
8117c478bd9Sstevel@tonic-gate 			user_rec = NULL;
8127c478bd9Sstevel@tonic-gate 		}
8137c478bd9Sstevel@tonic-gate 		token = usb_get_conf_rec(buf, &user_rec, errmsg);
8147c478bd9Sstevel@tonic-gate 		found = usb_cmp_rec(&cfgrec, user_rec);
8157c478bd9Sstevel@tonic-gate 		DPRINTF("add_entry: token=%x, found=%x\n", token, found);
8167c478bd9Sstevel@tonic-gate 	}
8177c478bd9Sstevel@tonic-gate 
8187c478bd9Sstevel@tonic-gate 	bzero(str, MAXLINESIZE);
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate 	if (found) {
8217c478bd9Sstevel@tonic-gate 		DPRINTF("FOUND\n");
8227c478bd9Sstevel@tonic-gate 		(void) snprintf(str, MAXLINESIZE, "selection=%s idVendor=0x%x "
8237c478bd9Sstevel@tonic-gate 		    "idProduct=0x%x ",
8247c478bd9Sstevel@tonic-gate 		    (cfgrec.selection) ? cfgrec.selection : user_rec->selection,
8257c478bd9Sstevel@tonic-gate 		    user_rec->idVendor, user_rec->idProduct);
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate 		if ((user_rec->cfgndx != -1) || (cfgrec.cfgndx != -1)) {
8287c478bd9Sstevel@tonic-gate 			(void) snprintf(&str[strlen(str)], MAXLINESIZE,
8297c478bd9Sstevel@tonic-gate 			    "cfgndx=0x%x ", (cfgrec.cfgndx != -1) ?
8307c478bd9Sstevel@tonic-gate 			    cfgrec.cfgndx : user_rec->cfgndx);
8317c478bd9Sstevel@tonic-gate 		}
8327c478bd9Sstevel@tonic-gate 
8337c478bd9Sstevel@tonic-gate 		if (user_rec->serialno) {
8347c478bd9Sstevel@tonic-gate 			(void) snprintf(&str[strlen(str)],  MAXLINESIZE,
8357c478bd9Sstevel@tonic-gate 			    "srno=\"%s\" ", user_rec->serialno);
8367c478bd9Sstevel@tonic-gate 		}
8377c478bd9Sstevel@tonic-gate 
8387c478bd9Sstevel@tonic-gate 		if (user_rec->pathname) {
8397c478bd9Sstevel@tonic-gate 			(void) snprintf(&str[strlen(str)],  MAXLINESIZE,
8407c478bd9Sstevel@tonic-gate 			    "pathname=\"%s\" ", user_rec->pathname);
8417c478bd9Sstevel@tonic-gate 		}
8427c478bd9Sstevel@tonic-gate 
8437c478bd9Sstevel@tonic-gate 		if (user_rec->driver) {
8447c478bd9Sstevel@tonic-gate 			(void) snprintf(&str[strlen(str)],  MAXLINESIZE,
8457c478bd9Sstevel@tonic-gate 			    "driver=\"%s\" ", user_rec->driver);
8467c478bd9Sstevel@tonic-gate 		} else if (cfgrec.driver != NULL) {
8477c478bd9Sstevel@tonic-gate 			if (strlen(cfgrec.driver)) {
8487c478bd9Sstevel@tonic-gate 				(void) snprintf(&str[strlen(str)],  MAXLINESIZE,
8497c478bd9Sstevel@tonic-gate 				    "driver=\"%s\" ", cfgrec.driver);
8507c478bd9Sstevel@tonic-gate 			}
8517c478bd9Sstevel@tonic-gate 		}
8527c478bd9Sstevel@tonic-gate 
8537c478bd9Sstevel@tonic-gate 		(void) strlcat(str, ";", sizeof (str));
8547c478bd9Sstevel@tonic-gate 
8557c478bd9Sstevel@tonic-gate 		/*
8567c478bd9Sstevel@tonic-gate 		 * Seek to the beginning of the record
8577c478bd9Sstevel@tonic-gate 		 */
8587c478bd9Sstevel@tonic-gate 		if (lseek(file, brec, SEEK_SET) == -1) {
8597c478bd9Sstevel@tonic-gate 			DPRINTF("add_entry: failed to lseek config file\n");
8607c478bd9Sstevel@tonic-gate 			rval = CFGA_USB_CONFIG_FILE;
8617c478bd9Sstevel@tonic-gate 			goto exit;
8627c478bd9Sstevel@tonic-gate 		}
8637c478bd9Sstevel@tonic-gate 
8647c478bd9Sstevel@tonic-gate 		/*
8657c478bd9Sstevel@tonic-gate 		 * Write the modified record
8667c478bd9Sstevel@tonic-gate 		 */
8677c478bd9Sstevel@tonic-gate 		if (write(file, str, strlen(str)) == -1) {
8687c478bd9Sstevel@tonic-gate 			DPRINTF("add_entry: failed to write config file\n");
8697c478bd9Sstevel@tonic-gate 			rval = CFGA_USB_CONFIG_FILE;
8707c478bd9Sstevel@tonic-gate 			goto exit;
8717c478bd9Sstevel@tonic-gate 		}
8727c478bd9Sstevel@tonic-gate 
8737c478bd9Sstevel@tonic-gate 		/*
8747c478bd9Sstevel@tonic-gate 		 * Write the rest of the file as it was
8757c478bd9Sstevel@tonic-gate 		 */
8767c478bd9Sstevel@tonic-gate 		if (write(file, buf+cntr, st.st_size - cntr) == -1) {
8777c478bd9Sstevel@tonic-gate 			DPRINTF("add_entry: failed to write config file\n");
8787c478bd9Sstevel@tonic-gate 			rval = CFGA_USB_CONFIG_FILE;
8797c478bd9Sstevel@tonic-gate 			goto exit;
8807c478bd9Sstevel@tonic-gate 		}
8817c478bd9Sstevel@tonic-gate 
8827c478bd9Sstevel@tonic-gate 	} else {
8837c478bd9Sstevel@tonic-gate 		DPRINTF("!FOUND\n");
8847c478bd9Sstevel@tonic-gate 		(void) snprintf(str, MAXLINESIZE,
8857c478bd9Sstevel@tonic-gate 		    "selection=%s idVendor=0x%x idProduct=0x%x ",
8867c478bd9Sstevel@tonic-gate 		    (cfgrec.selection) ? cfgrec.selection : "enable",
8877c478bd9Sstevel@tonic-gate 		    cfgrec.idVendor, cfgrec.idProduct);
8887c478bd9Sstevel@tonic-gate 
8897c478bd9Sstevel@tonic-gate 		if (cfgrec.cfgndx != -1) {
8907c478bd9Sstevel@tonic-gate 			(void) snprintf(&str[strlen(str)], MAXLINESIZE,
8917c478bd9Sstevel@tonic-gate 			    "cfgndx=0x%x ", cfgrec.cfgndx);
8927c478bd9Sstevel@tonic-gate 		}
8937c478bd9Sstevel@tonic-gate 
8947c478bd9Sstevel@tonic-gate 		if (cfgrec.serialno) {
8957c478bd9Sstevel@tonic-gate 			(void) snprintf(&str[strlen(str)], MAXLINESIZE,
8967c478bd9Sstevel@tonic-gate 			    "srno=\"%s\" ", cfgrec.serialno);
8977c478bd9Sstevel@tonic-gate 		}
8987c478bd9Sstevel@tonic-gate 
8997c478bd9Sstevel@tonic-gate 		if (cfgrec.pathname != NULL) {
9007c478bd9Sstevel@tonic-gate 			(void) snprintf(&str[strlen(str)], MAXLINESIZE,
9017c478bd9Sstevel@tonic-gate 			    "pathname=\"%s\" ", cfgrec.pathname);
9027c478bd9Sstevel@tonic-gate 		}
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate 		if (cfgrec.driver != NULL) {
9057c478bd9Sstevel@tonic-gate 			if (strlen(cfgrec.driver)) {
9067c478bd9Sstevel@tonic-gate 				(void) snprintf(&str[strlen(str)], MAXLINESIZE,
9077c478bd9Sstevel@tonic-gate 				    "driver=\"%s\" ", cfgrec.driver);
9087c478bd9Sstevel@tonic-gate 			}
9097c478bd9Sstevel@tonic-gate 		}
9107c478bd9Sstevel@tonic-gate 
9117c478bd9Sstevel@tonic-gate 		(void) strlcat(str, ";\n", sizeof (str));
9127c478bd9Sstevel@tonic-gate 
9137c478bd9Sstevel@tonic-gate 		/*
9147c478bd9Sstevel@tonic-gate 		 * Incase this is the first entry, add it after the comments
9157c478bd9Sstevel@tonic-gate 		 */
9167c478bd9Sstevel@tonic-gate 		if (frec == 0) {
9177c478bd9Sstevel@tonic-gate 			frec = st.st_size;
9187c478bd9Sstevel@tonic-gate 		}
9197c478bd9Sstevel@tonic-gate 
9207c478bd9Sstevel@tonic-gate 		/*
9217c478bd9Sstevel@tonic-gate 		 * Go to the beginning of the records
9227c478bd9Sstevel@tonic-gate 		 */
9237c478bd9Sstevel@tonic-gate 		if (lseek(file, frec, SEEK_SET) == -1) {
9247c478bd9Sstevel@tonic-gate 			DPRINTF("add_entry: failed to lseek config file\n");
9257c478bd9Sstevel@tonic-gate 			rval = CFGA_USB_CONFIG_FILE;
9267c478bd9Sstevel@tonic-gate 			goto exit;
9277c478bd9Sstevel@tonic-gate 		}
9287c478bd9Sstevel@tonic-gate 
9297c478bd9Sstevel@tonic-gate 		/*
9307c478bd9Sstevel@tonic-gate 		 * Add the entry
9317c478bd9Sstevel@tonic-gate 		 */
9327c478bd9Sstevel@tonic-gate 		if (write(file, str, strlen(str)) == -1) {
9337c478bd9Sstevel@tonic-gate 			DPRINTF("add_entry: failed to write config file\n");
9347c478bd9Sstevel@tonic-gate 			rval = CFGA_USB_CONFIG_FILE;
9357c478bd9Sstevel@tonic-gate 			goto exit;
9367c478bd9Sstevel@tonic-gate 		}
9377c478bd9Sstevel@tonic-gate 
9387c478bd9Sstevel@tonic-gate 		/*
9397c478bd9Sstevel@tonic-gate 		 * write the remaining file as it was
9407c478bd9Sstevel@tonic-gate 		 */
9417c478bd9Sstevel@tonic-gate 		if (write(file, buf+frec, st.st_size - frec) == -1) {
9427c478bd9Sstevel@tonic-gate 			DPRINTF("add_entry: failed to write config file\n");
9437c478bd9Sstevel@tonic-gate 			rval = CFGA_USB_CONFIG_FILE;
9447c478bd9Sstevel@tonic-gate 			goto exit;
9457c478bd9Sstevel@tonic-gate 		}
9467c478bd9Sstevel@tonic-gate 	}
9477c478bd9Sstevel@tonic-gate 
9487c478bd9Sstevel@tonic-gate 	/* no error encountered */
9497c478bd9Sstevel@tonic-gate 	if (rval == CFGA_USB_OK) {
9507c478bd9Sstevel@tonic-gate 		free(errmsg);
9517c478bd9Sstevel@tonic-gate 	}
9527c478bd9Sstevel@tonic-gate 
9537c478bd9Sstevel@tonic-gate exit:
9547c478bd9Sstevel@tonic-gate 	if (buf != NULL) {
9557c478bd9Sstevel@tonic-gate 		free(buf);
9567c478bd9Sstevel@tonic-gate 	}
9577c478bd9Sstevel@tonic-gate 
9587c478bd9Sstevel@tonic-gate 	if (lockf(file, F_ULOCK, 0) == -1) {
9597c478bd9Sstevel@tonic-gate 		DPRINTF("add_entry: failed to unlock config file\n");
9607c478bd9Sstevel@tonic-gate 
9617c478bd9Sstevel@tonic-gate 		rval = CFGA_USB_LOCK_FILE;
9627c478bd9Sstevel@tonic-gate 	}
9637c478bd9Sstevel@tonic-gate 
9647c478bd9Sstevel@tonic-gate 	close(file);
9657c478bd9Sstevel@tonic-gate 
9667c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&file_lock);
9677c478bd9Sstevel@tonic-gate 
9687c478bd9Sstevel@tonic-gate 	return (rval);
9697c478bd9Sstevel@tonic-gate }
970