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