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