10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*9407SMichael.Bergknoff@Sun.COM  * Common Development and Distribution License (the "License").
6*9407SMichael.Bergknoff@Sun.COM  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*9407SMichael.Bergknoff@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate  * This module implements the routine to parse the configuration file.
280Sstevel@tonic-gate  */
290Sstevel@tonic-gate 
300Sstevel@tonic-gate 
310Sstevel@tonic-gate #include <stdio.h>
320Sstevel@tonic-gate #include <fcntl.h>
330Sstevel@tonic-gate #include <unistd.h>
340Sstevel@tonic-gate #include <stdlib.h>
350Sstevel@tonic-gate #include <stdarg.h>
360Sstevel@tonic-gate #include <string.h>
370Sstevel@tonic-gate #include <ctype.h>
380Sstevel@tonic-gate #include <alloca.h>
390Sstevel@tonic-gate #include <limits.h>
400Sstevel@tonic-gate #include <sys/utsname.h>
410Sstevel@tonic-gate #include <sys/systeminfo.h>
420Sstevel@tonic-gate #include <sys/types.h>
430Sstevel@tonic-gate #include <libintl.h>
440Sstevel@tonic-gate #include <syslog.h>
450Sstevel@tonic-gate #include <locale.h>
460Sstevel@tonic-gate #include <picl.h>
470Sstevel@tonic-gate #include <picltree.h>
480Sstevel@tonic-gate #include "picld_pluginutil.h"
490Sstevel@tonic-gate #include "picld_pluginutil_impl.h"
500Sstevel@tonic-gate 
510Sstevel@tonic-gate /* error codes returned from syntax checking */
520Sstevel@tonic-gate #define	EC_SYNTAX_OK		0
530Sstevel@tonic-gate #define	EC_INSUFFICIENT_TOKEN	1
540Sstevel@tonic-gate #define	EC_SYNTAX_ERR		2
550Sstevel@tonic-gate #define	EC_UNSUPPORTED		3
560Sstevel@tonic-gate #define	EC_PATH_ERR		4
570Sstevel@tonic-gate #define	EC_NODE_MISMATCH	5
580Sstevel@tonic-gate #define	EC_FAILURE		6
590Sstevel@tonic-gate #define	EC_PICL_ERR		7
600Sstevel@tonic-gate #define	EC_TABLE_MISMATCH	8
610Sstevel@tonic-gate #define	EC_ROW_MISMATCH		9
620Sstevel@tonic-gate #define	EC_ROW_EMPTY		10
630Sstevel@tonic-gate 
640Sstevel@tonic-gate /*
650Sstevel@tonic-gate  * Error message texts
660Sstevel@tonic-gate  */
670Sstevel@tonic-gate static	char	*err_msg[] = {
680Sstevel@tonic-gate 	"%s: Syntax OK",					/* 0 */
690Sstevel@tonic-gate 	"%s::%s[line %d]: Insufficient token\n",		/* 1 */
700Sstevel@tonic-gate 	"%s::%s[line %d]: Syntax error\n",			/* 2 */
710Sstevel@tonic-gate 	"%s::%s[line %d]: Unsupported or missing version\n",	/* 3 */
720Sstevel@tonic-gate 	"%s::%s[line %d]: Illegal use of nodepath or namepath\n",	/* 4 */
730Sstevel@tonic-gate 	"%s::%s[line %d]: Node and endnode mismatch\n",		/* 5 */
740Sstevel@tonic-gate 	"%s::%s[line %d]: General system failure\n",		/* 6 */
750Sstevel@tonic-gate 	"%s: PICL error code %d\n",				/* 7 */
760Sstevel@tonic-gate 	"%s::%s[line %d]: Table and endtable mismatch\n",	/* 8 */
770Sstevel@tonic-gate 	"%s::%s[line %d]: Row and endrow mismatch\n",		/* 9 */
780Sstevel@tonic-gate 	"%s::%s[line %d]: Row has no entries \n"		/* 10 */
790Sstevel@tonic-gate };
800Sstevel@tonic-gate 
810Sstevel@tonic-gate /* token per directive */
820Sstevel@tonic-gate #define	TOK_CLASSPATH	0
830Sstevel@tonic-gate #define	TOK_NAMEPATH	1
840Sstevel@tonic-gate #define	TOK_NODE	2
850Sstevel@tonic-gate #define	TOK_ENDNODE	3
860Sstevel@tonic-gate #define	TOK_PROP	4
870Sstevel@tonic-gate #define	TOK_REFPROP	5
880Sstevel@tonic-gate #define	TOK_VERSION	6
890Sstevel@tonic-gate #define	TOK_REFNODE	7
900Sstevel@tonic-gate #define	TOK_VERBOSE	8
910Sstevel@tonic-gate #define	TOK_TABLE	9
920Sstevel@tonic-gate #define	TOK_ENDTABLE	10
930Sstevel@tonic-gate #define	TOK_ROW		11
940Sstevel@tonic-gate #define	TOK_ENDROW	12
950Sstevel@tonic-gate 
960Sstevel@tonic-gate static const char	*tokens[] = {
970Sstevel@tonic-gate 	"_class",	/* _CLASS:<classpath> */
980Sstevel@tonic-gate 	"name",		/* NAME:<namepath> */
990Sstevel@tonic-gate 	"node",		/* NODE <name> <class> */
1000Sstevel@tonic-gate 	"endnode",	/* ENDNODE */
1010Sstevel@tonic-gate 	"prop",		/* PROP <name> <type> <access_mode> <size> <value> */
1020Sstevel@tonic-gate 	"refprop",	/* REFPROP <prop> <destnode> */
1030Sstevel@tonic-gate 	"version",	/* VERSION <version_number> */
1040Sstevel@tonic-gate 	"refnode",	/* REFNODE <node> <class> WITH <destnode> */
1050Sstevel@tonic-gate 	"verbose",	/* VERBOSE <level> */
1060Sstevel@tonic-gate 	"table",	/* TABLE   <table_prop_name> */
1070Sstevel@tonic-gate 	"endtable",	/* ENDTABLE */
1080Sstevel@tonic-gate 	"row",		/* ROW  */
1090Sstevel@tonic-gate 	"endrow"	/* ENDROW */
1100Sstevel@tonic-gate };
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate #define	BUF_SIZE_MAX	1024
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate /*
1150Sstevel@tonic-gate  * print error message
1160Sstevel@tonic-gate  */
1170Sstevel@tonic-gate /*VARARGS2*/
1180Sstevel@tonic-gate static void
verbose_log(int pri,const char * fmt,...)1190Sstevel@tonic-gate verbose_log(int pri, const char *fmt, ...)
1200Sstevel@tonic-gate {
1210Sstevel@tonic-gate 	va_list	ap;
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 	va_start(ap, fmt);
1240Sstevel@tonic-gate 	vsyslog(pri, fmt, ap);
1250Sstevel@tonic-gate 	va_end(ap);
1260Sstevel@tonic-gate }
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate /*
1290Sstevel@tonic-gate  * Undo the commands which have created valid node/prop handle
1300Sstevel@tonic-gate  * The undo order is from last command to the first command.
1310Sstevel@tonic-gate  */
1320Sstevel@tonic-gate static void
undo_commands(cmdbuf_t * cmds,int last_cmd_index)1330Sstevel@tonic-gate undo_commands(cmdbuf_t *cmds, int last_cmd_index)
1340Sstevel@tonic-gate {
1350Sstevel@tonic-gate 	int 		i;
1360Sstevel@tonic-gate 	command_t	*com = cmds->commands;
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate 	for (i = last_cmd_index; i >= 0; i--) {
1390Sstevel@tonic-gate 		switch (com[i].type) {
1400Sstevel@tonic-gate 		case TOK_NODE:
1410Sstevel@tonic-gate 			if (com[i].nodecmd_nodeh == NULL)
1420Sstevel@tonic-gate 				break;
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 			(void) ptree_delete_node(com[i].nodecmd_nodeh);
1450Sstevel@tonic-gate 			(void) ptree_destroy_node(com[i].nodecmd_nodeh);
1460Sstevel@tonic-gate 			break;
1470Sstevel@tonic-gate 		case TOK_REFNODE:
1480Sstevel@tonic-gate 			if (com[i].refnodecmd_nodeh == NULL)
1490Sstevel@tonic-gate 				break;
1500Sstevel@tonic-gate 			(void) ptree_delete_node(com[i].refnodecmd_nodeh);
1510Sstevel@tonic-gate 			(void) ptree_destroy_node(com[i].refnodecmd_nodeh);
1520Sstevel@tonic-gate 			break;
1530Sstevel@tonic-gate 		case TOK_PROP:
1540Sstevel@tonic-gate 			if (com[i].propcmd_proph == NULL)
1550Sstevel@tonic-gate 				break;
1560Sstevel@tonic-gate 			(void) ptree_delete_prop(com[i].propcmd_proph);
1570Sstevel@tonic-gate 			(void) ptree_destroy_prop(com[i].propcmd_proph);
1580Sstevel@tonic-gate 			break;
1590Sstevel@tonic-gate 		case TOK_REFPROP:
1600Sstevel@tonic-gate 			if (com[i].refpropcmd_proph == NULL)
1610Sstevel@tonic-gate 				break;
1620Sstevel@tonic-gate 			(void) ptree_delete_prop(com[i].refpropcmd_proph);
1630Sstevel@tonic-gate 			(void) ptree_destroy_prop(com[i].refpropcmd_proph);
1640Sstevel@tonic-gate 			break;
1650Sstevel@tonic-gate 		case TOK_TABLE:
1660Sstevel@tonic-gate 			if ((com[i].tablecmd_tblh == NULL) ||
1670Sstevel@tonic-gate 			    (com[i].tablecmd_newtbl == 0))
1680Sstevel@tonic-gate 				break;
1690Sstevel@tonic-gate 			(void) ptree_delete_prop(com[i].tablecmd_tblh);
1700Sstevel@tonic-gate 			(void) ptree_destroy_prop(com[i].tablecmd_tblh);
1710Sstevel@tonic-gate 			break;
1720Sstevel@tonic-gate 		case TOK_ENDTABLE:
1730Sstevel@tonic-gate 			/*FALLTHROUGH*/
1740Sstevel@tonic-gate 		case TOK_ROW:
1750Sstevel@tonic-gate 			/*FALLTHROUGH*/
1760Sstevel@tonic-gate 		case TOK_ENDROW:
1770Sstevel@tonic-gate 			/*FALLTHROUGH*/
1780Sstevel@tonic-gate 		case TOK_NAMEPATH:
1790Sstevel@tonic-gate 			/*FALLTHROUGH*/
1800Sstevel@tonic-gate 		case TOK_CLASSPATH:
1810Sstevel@tonic-gate 			/*FALLTHROUGH*/
1820Sstevel@tonic-gate 		case TOK_ENDNODE:
1830Sstevel@tonic-gate 			/*FALLTHROUGH*/
1840Sstevel@tonic-gate 		case TOK_VERBOSE:
1850Sstevel@tonic-gate 			/*FALLTHROUGH*/
1860Sstevel@tonic-gate 		default:
1870Sstevel@tonic-gate 			break;
1880Sstevel@tonic-gate 		}
1890Sstevel@tonic-gate 	}
1900Sstevel@tonic-gate }
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate /*
1930Sstevel@tonic-gate  * Get the token index from the tokens table
1940Sstevel@tonic-gate  */
1950Sstevel@tonic-gate static int
get_token_id(char * t)1960Sstevel@tonic-gate get_token_id(char *t)
1970Sstevel@tonic-gate {
1980Sstevel@tonic-gate 	int	i;
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 	for (i = 0; i < sizeof (tokens)/ sizeof (char *); ++i)
2010Sstevel@tonic-gate 		if (strcasecmp(tokens[i], t) == 0)
2020Sstevel@tonic-gate 			return (i);
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 	return (-1);
2050Sstevel@tonic-gate }
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate /*
2080Sstevel@tonic-gate  * Check the version syntax and set the version_no
2090Sstevel@tonic-gate  *
2100Sstevel@tonic-gate  * VERSION <version_num> --   specify the configuration version
2110Sstevel@tonic-gate  */
2120Sstevel@tonic-gate static int
parse_version(cmdbuf_t * cmds,char * line)2130Sstevel@tonic-gate parse_version(cmdbuf_t *cmds, char *line)
2140Sstevel@tonic-gate {
2150Sstevel@tonic-gate 	char	*tok;
2160Sstevel@tonic-gate 	char	*vertok;
2170Sstevel@tonic-gate 	char	*last;
2180Sstevel@tonic-gate 	char	*endptr;
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate 	/* get the VERSION directive */
2210Sstevel@tonic-gate 	tok = strtok_r(line, WHITESPACE, &last);
2220Sstevel@tonic-gate 	if (tok == NULL)
2230Sstevel@tonic-gate 		return (EC_INSUFFICIENT_TOKEN);
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate 	/* get the version number */
2260Sstevel@tonic-gate 	vertok = strtok_r(last, WHITESPACE, &last);
2270Sstevel@tonic-gate 	if (vertok == NULL)
2280Sstevel@tonic-gate 		return (EC_INSUFFICIENT_TOKEN);
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 	cmds->version_no = (float)strtod(vertok, &endptr);
2310Sstevel@tonic-gate 	if (endptr != (vertok + strlen(vertok)))
2320Sstevel@tonic-gate 		return (EC_UNSUPPORTED);
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 	if (cmds->version_no > (float)SUPPORTED_VERSION_NUM)
2350Sstevel@tonic-gate 		return (EC_UNSUPPORTED);
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate 	/* check if more tokens */
2380Sstevel@tonic-gate 	tok = strtok_r(last, WHITESPACE, &last);
2390Sstevel@tonic-gate 	if (tok != NULL)
2400Sstevel@tonic-gate 		return (EC_SYNTAX_ERR);
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 	return (EC_SYNTAX_OK);
2430Sstevel@tonic-gate }
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate /*
2460Sstevel@tonic-gate  * free path_cmd_t
2470Sstevel@tonic-gate  */
2480Sstevel@tonic-gate static void
free_path(command_t * command)2490Sstevel@tonic-gate free_path(command_t *command)
2500Sstevel@tonic-gate {
2510Sstevel@tonic-gate 	free(command->pathcmd_name);
2520Sstevel@tonic-gate }
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate /*
2550Sstevel@tonic-gate  * Check the path syntax
2560Sstevel@tonic-gate  * NAMEPATH:<namepath> --     gives the anchor node
2570Sstevel@tonic-gate  * or
2580Sstevel@tonic-gate  * CLASSPATH:<classpath> --   gives the anchor node
2590Sstevel@tonic-gate  */
2600Sstevel@tonic-gate static int
parse_path(char * line,command_t * command)2610Sstevel@tonic-gate parse_path(char *line, command_t *command)
2620Sstevel@tonic-gate {
2630Sstevel@tonic-gate 	char	*tok;
2640Sstevel@tonic-gate 	char	*pathtok;
2650Sstevel@tonic-gate 	char	*last;
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate 	pathtok = strtok_r(line, WHITESPACE, &last);
2680Sstevel@tonic-gate 	if (pathtok == NULL)
2690Sstevel@tonic-gate 		return (EC_INSUFFICIENT_TOKEN);
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate 	/* check if more tokens */
2720Sstevel@tonic-gate 	tok = strtok_r(last, WHITESPACE, &last);
2730Sstevel@tonic-gate 	if (tok != NULL)
2740Sstevel@tonic-gate 		return (EC_SYNTAX_ERR);
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate 	command->pathcmd_name = strdup(pathtok);
2770Sstevel@tonic-gate 	if (command->pathcmd_name == NULL)
2780Sstevel@tonic-gate 		return (EC_FAILURE);
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate 	return (EC_SYNTAX_OK);
2810Sstevel@tonic-gate }
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate /*
2840Sstevel@tonic-gate  * Process the path command and return PICL node handle
2850Sstevel@tonic-gate  */
2860Sstevel@tonic-gate static int
process_path(command_t * command,picl_nodehdl_t * nodeh)2870Sstevel@tonic-gate process_path(command_t *command, picl_nodehdl_t *nodeh)
2880Sstevel@tonic-gate {
2890Sstevel@tonic-gate 	int	err;
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate 	err = ptree_get_node_by_path(command->pathcmd_name, nodeh);
2920Sstevel@tonic-gate 	return (err);
2930Sstevel@tonic-gate }
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate /*
2960Sstevel@tonic-gate  * free node_cmd_t
2970Sstevel@tonic-gate  */
2980Sstevel@tonic-gate static void
free_node(command_t * command)2990Sstevel@tonic-gate free_node(command_t *command)
3000Sstevel@tonic-gate {
3010Sstevel@tonic-gate 	free(command->nodecmd_nodename);
3020Sstevel@tonic-gate 	free(command->nodecmd_classname);
3030Sstevel@tonic-gate }
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate /*
3060Sstevel@tonic-gate  * Check the NODE syntax
3070Sstevel@tonic-gate  * NODE <name> <class>
3080Sstevel@tonic-gate  */
3090Sstevel@tonic-gate static int
parse_node(char * line,command_t * command)3100Sstevel@tonic-gate parse_node(char *line, command_t *command)
3110Sstevel@tonic-gate {
3120Sstevel@tonic-gate 	char	*tok;
3130Sstevel@tonic-gate 	char	*nametok;
3140Sstevel@tonic-gate 	char	*classtok;
3150Sstevel@tonic-gate 	char	*last;
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate 	/* get the NODE directive */
3180Sstevel@tonic-gate 	tok = strtok_r(line, WHITESPACE, &last);
3190Sstevel@tonic-gate 	if (tok == NULL)
3200Sstevel@tonic-gate 		return (EC_INSUFFICIENT_TOKEN);
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate 	/* get name */
3230Sstevel@tonic-gate 	nametok = strtok_r(last, WHITESPACE, &last);
3240Sstevel@tonic-gate 	if (nametok == NULL)
3250Sstevel@tonic-gate 		return (EC_INSUFFICIENT_TOKEN);
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate 	classtok = strtok_r(last, WHITESPACE, &last);
3280Sstevel@tonic-gate 	if (classtok == NULL)
3290Sstevel@tonic-gate 		return (EC_INSUFFICIENT_TOKEN);
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate 	/* check if more tokens */
3320Sstevel@tonic-gate 	tok = strtok_r(last, WHITESPACE, &last);
3330Sstevel@tonic-gate 	if (tok != NULL)
3340Sstevel@tonic-gate 		return (EC_SYNTAX_ERR);
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate 	command->nodecmd_nodename = strdup(nametok);
3370Sstevel@tonic-gate 	command->nodecmd_classname = strdup(classtok);
3380Sstevel@tonic-gate 	command->nodecmd_nodeh = NULL;
3390Sstevel@tonic-gate 	if ((command->nodecmd_nodename == NULL) ||
3400Sstevel@tonic-gate 	    (command->nodecmd_classname == NULL))
3410Sstevel@tonic-gate 		return (EC_FAILURE);
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 	return (EC_SYNTAX_OK);
3440Sstevel@tonic-gate }
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate /*
3470Sstevel@tonic-gate  * Process the NODE command and return PICL node handle
3480Sstevel@tonic-gate  */
3490Sstevel@tonic-gate static int
process_node(command_t * command,picl_nodehdl_t parh,picl_nodehdl_t * nodeh)3500Sstevel@tonic-gate process_node(command_t *command, picl_nodehdl_t parh, picl_nodehdl_t *nodeh)
3510Sstevel@tonic-gate {
3520Sstevel@tonic-gate 	int	err;
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate 	err = ptree_create_and_add_node(parh, command->nodecmd_nodename,
3550Sstevel@tonic-gate 	    command->nodecmd_classname, nodeh);
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate 	if (err == PICL_SUCCESS)
3580Sstevel@tonic-gate 		command->nodecmd_nodeh = *nodeh;
3590Sstevel@tonic-gate 
3600Sstevel@tonic-gate 	return (err);
3610Sstevel@tonic-gate }
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate /*
3640Sstevel@tonic-gate  * get the PICL property type
3650Sstevel@tonic-gate  */
3660Sstevel@tonic-gate static int
getpicltype(char * type)3670Sstevel@tonic-gate getpicltype(char *type)
3680Sstevel@tonic-gate {
3690Sstevel@tonic-gate 	if (strcasecmp(type, KEYWORD_INT_TYPE) == 0)
3700Sstevel@tonic-gate 		return (PICL_PTYPE_INT);
3710Sstevel@tonic-gate 	else if (strcasecmp(type, KEYWORD_UINT_TYPE) == 0)
3720Sstevel@tonic-gate 		return (PICL_PTYPE_UNSIGNED_INT);
3730Sstevel@tonic-gate 	else if (strcasecmp(type, KEYWORD_FLOAT_TYPE) == 0)
3740Sstevel@tonic-gate 		return (PICL_PTYPE_FLOAT);
3750Sstevel@tonic-gate 	else if (strcasecmp(type, KEYWORD_STRING_TYPE) == 0)
3760Sstevel@tonic-gate 		return (PICL_PTYPE_CHARSTRING);
3770Sstevel@tonic-gate 	else if (strcasecmp(type, KEYWORD_VOID_TYPE) == 0)
3780Sstevel@tonic-gate 		return (PICL_PTYPE_VOID);
3790Sstevel@tonic-gate 	else
3800Sstevel@tonic-gate 		return (-1);
3810Sstevel@tonic-gate }
3820Sstevel@tonic-gate 
3830Sstevel@tonic-gate /*
3840Sstevel@tonic-gate  * get the PICL accessmode mode
3850Sstevel@tonic-gate  */
3860Sstevel@tonic-gate static int
getpiclmode(char * mode)3870Sstevel@tonic-gate getpiclmode(char *mode)
3880Sstevel@tonic-gate {
3890Sstevel@tonic-gate 	if (strcasecmp(mode, KEYWORD_READ_MODE) == 0)
3900Sstevel@tonic-gate 		return (PICL_READ);
3910Sstevel@tonic-gate 	else if (strcasecmp(mode, KEYWORD_WRITE_MODE) == 0)
3920Sstevel@tonic-gate 		return (PICL_WRITE);
3930Sstevel@tonic-gate 	else if (strcasecmp(mode, KEYWORD_READWRITE_MODE) == 0)
3940Sstevel@tonic-gate 		return (PICL_READ|PICL_WRITE);
3950Sstevel@tonic-gate 	else
3960Sstevel@tonic-gate 		return (-1);
3970Sstevel@tonic-gate }
3980Sstevel@tonic-gate 
3990Sstevel@tonic-gate /*
4000Sstevel@tonic-gate  * check if the size and value are valid given by the prop type
4010Sstevel@tonic-gate  */
4020Sstevel@tonic-gate static int
validate_size_and_cvt_val(void * outbuf,size_t size,int type,char * val)4030Sstevel@tonic-gate validate_size_and_cvt_val(void *outbuf, size_t size, int type, char *val)
4040Sstevel@tonic-gate {
4050Sstevel@tonic-gate 	int64_t 	llval;
4060Sstevel@tonic-gate 	int32_t		intval;
4070Sstevel@tonic-gate 	int16_t		sval;
4080Sstevel@tonic-gate 	int8_t		cval;
4090Sstevel@tonic-gate 	uint64_t 	ullval;
4100Sstevel@tonic-gate 	uint32_t	uintval;
4110Sstevel@tonic-gate 	uint16_t	usval;
4120Sstevel@tonic-gate 	uint8_t		ucval;
4130Sstevel@tonic-gate 	float		fval;
4140Sstevel@tonic-gate 	double		dval;
4150Sstevel@tonic-gate 	char		*endptr;
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate 	switch (type) {
4180Sstevel@tonic-gate 	case PICL_PTYPE_CHARSTRING:
4190Sstevel@tonic-gate 		break;
4200Sstevel@tonic-gate 	case PICL_PTYPE_INT:
4210Sstevel@tonic-gate 		switch (size) {
4220Sstevel@tonic-gate 		case sizeof (int64_t):
4230Sstevel@tonic-gate 			llval = strtoll(val, &endptr, 0);
4240Sstevel@tonic-gate 			if (endptr != (val + strlen(val)))
4250Sstevel@tonic-gate 				return (EC_SYNTAX_ERR);
4260Sstevel@tonic-gate 			(void) memcpy(outbuf, &llval, size);
4270Sstevel@tonic-gate 			break;
4280Sstevel@tonic-gate 		case sizeof (int32_t):
4290Sstevel@tonic-gate 			intval = strtol(val, &endptr, 0);
4300Sstevel@tonic-gate 			if (endptr != (val + strlen(val)))
4310Sstevel@tonic-gate 				return (EC_SYNTAX_ERR);
4320Sstevel@tonic-gate 			(void) memcpy(outbuf, &intval, size);
4330Sstevel@tonic-gate 			break;
4340Sstevel@tonic-gate 		case sizeof (int16_t):
4350Sstevel@tonic-gate 			sval = (int16_t)strtol(val, &endptr, 0);
4360Sstevel@tonic-gate 			if (endptr != (val + strlen(val)))
4370Sstevel@tonic-gate 				return (EC_SYNTAX_ERR);
4380Sstevel@tonic-gate 			(void) memcpy(outbuf, &sval, size);
4390Sstevel@tonic-gate 			break;
4400Sstevel@tonic-gate 		case sizeof (int8_t):
4410Sstevel@tonic-gate 			cval = (int8_t)strtol(val, &endptr, 0);
4420Sstevel@tonic-gate 			if (endptr != (val + strlen(val)))
4430Sstevel@tonic-gate 				return (EC_SYNTAX_ERR);
4440Sstevel@tonic-gate 			(void) memcpy(outbuf, &cval, size);
4450Sstevel@tonic-gate 			break;
4460Sstevel@tonic-gate 		default:	/* invalid size */
4470Sstevel@tonic-gate 			return (EC_SYNTAX_ERR);
4480Sstevel@tonic-gate 		}
4490Sstevel@tonic-gate 		break;
4500Sstevel@tonic-gate 	case PICL_PTYPE_UNSIGNED_INT:
4510Sstevel@tonic-gate 		switch (size) {
4520Sstevel@tonic-gate 		case sizeof (uint64_t):
4530Sstevel@tonic-gate 			ullval = strtoull(val, &endptr, 0);
4540Sstevel@tonic-gate 			if (endptr != (val + strlen(val)))
4550Sstevel@tonic-gate 				return (EC_SYNTAX_ERR);
4560Sstevel@tonic-gate 			(void) memcpy(outbuf, &ullval, size);
4570Sstevel@tonic-gate 			break;
4580Sstevel@tonic-gate 		case sizeof (uint32_t):
4590Sstevel@tonic-gate 			uintval = strtoul(val, &endptr, 0);
4600Sstevel@tonic-gate 			if (endptr != (val + strlen(val)))
4610Sstevel@tonic-gate 				return (EC_SYNTAX_ERR);
4620Sstevel@tonic-gate 			(void) memcpy(outbuf, &uintval, size);
4630Sstevel@tonic-gate 			break;
4640Sstevel@tonic-gate 		case sizeof (uint16_t):
4650Sstevel@tonic-gate 			usval = (uint16_t)strtoul(val, &endptr, 0);
4660Sstevel@tonic-gate 			if (endptr != (val + strlen(val)))
4670Sstevel@tonic-gate 				return (EC_SYNTAX_ERR);
4680Sstevel@tonic-gate 			(void) memcpy(outbuf, &usval, size);
4690Sstevel@tonic-gate 			break;
4700Sstevel@tonic-gate 		case sizeof (uint8_t):
4710Sstevel@tonic-gate 			ucval = (uint8_t)strtoul(val, &endptr, 0);
4720Sstevel@tonic-gate 			if (endptr != (val + strlen(val)))
4730Sstevel@tonic-gate 				return (EC_SYNTAX_ERR);
4740Sstevel@tonic-gate 			(void) memcpy(outbuf, &ucval, size);
4750Sstevel@tonic-gate 			break;
4760Sstevel@tonic-gate 		default:	/* invalid size */
4770Sstevel@tonic-gate 			return (EC_SYNTAX_ERR);
4780Sstevel@tonic-gate 		}
4790Sstevel@tonic-gate 		break;
4800Sstevel@tonic-gate 	case PICL_PTYPE_FLOAT:
4810Sstevel@tonic-gate 		switch (size) {
4820Sstevel@tonic-gate 		case sizeof (double):
4830Sstevel@tonic-gate 			dval = strtod(val, &endptr);
4840Sstevel@tonic-gate 			if (endptr != (val + strlen(val)))
4850Sstevel@tonic-gate 				return (EC_SYNTAX_ERR);
4860Sstevel@tonic-gate 			(void) memcpy(outbuf, &dval, size);
4870Sstevel@tonic-gate 			break;
4880Sstevel@tonic-gate 		case sizeof (float):
4890Sstevel@tonic-gate 			fval = (float)strtod(val, &endptr);
4900Sstevel@tonic-gate 			if (endptr != (val + strlen(val)))
4910Sstevel@tonic-gate 				return (EC_SYNTAX_ERR);
4920Sstevel@tonic-gate 			(void) memcpy(outbuf, &fval, size);
4930Sstevel@tonic-gate 			break;
4940Sstevel@tonic-gate 		default:	/* invalid size */
4950Sstevel@tonic-gate 			return (EC_SYNTAX_ERR);
4960Sstevel@tonic-gate 		}
4970Sstevel@tonic-gate 		break;
4980Sstevel@tonic-gate 	default:	/* not supported type */
4990Sstevel@tonic-gate 		return (EC_SYNTAX_ERR);
5000Sstevel@tonic-gate 	}
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate 	return (EC_SYNTAX_OK);
5030Sstevel@tonic-gate }
5040Sstevel@tonic-gate 
5050Sstevel@tonic-gate /*
5060Sstevel@tonic-gate  * free prop_cmd_t
5070Sstevel@tonic-gate  */
5080Sstevel@tonic-gate static void
free_prop(command_t * command)5090Sstevel@tonic-gate free_prop(command_t *command)
5100Sstevel@tonic-gate {
5110Sstevel@tonic-gate 	free(command->propcmd_pname);
5120Sstevel@tonic-gate 	if (command->propcmd_type != PICL_PTYPE_VOID)
5130Sstevel@tonic-gate 		free(command->propcmd_valbuf);
5140Sstevel@tonic-gate }
5150Sstevel@tonic-gate 
5160Sstevel@tonic-gate /*
5170Sstevel@tonic-gate  * return the string token in two double quotes
5180Sstevel@tonic-gate  * The current version won't support multiple-line string
5190Sstevel@tonic-gate  */
5200Sstevel@tonic-gate static int
get_string_token(char * line,char ** valtok)5210Sstevel@tonic-gate get_string_token(char *line, char **valtok)
5220Sstevel@tonic-gate {
5230Sstevel@tonic-gate 	char	*optr;	/* ptr to the open quote */
5240Sstevel@tonic-gate 	char	*cptr;	/* ptr to the close quote */
5250Sstevel@tonic-gate 	char	*ptr;
5260Sstevel@tonic-gate 	char	*tmpbuf;
5270Sstevel@tonic-gate 
5280Sstevel@tonic-gate 	if (line == NULL)
5290Sstevel@tonic-gate 		return (EC_INSUFFICIENT_TOKEN);
5300Sstevel@tonic-gate 
5310Sstevel@tonic-gate 	/* skipping leading white spaces */
5320Sstevel@tonic-gate 	optr = line;
5330Sstevel@tonic-gate 	while ((*optr == ' ') || (*optr == '\t') || (*optr == '\n'))
5340Sstevel@tonic-gate 		optr++;
5350Sstevel@tonic-gate 
5360Sstevel@tonic-gate 	/* reach end of string */
5370Sstevel@tonic-gate 	if (*optr == '\0')
5380Sstevel@tonic-gate 		return (EC_INSUFFICIENT_TOKEN);
5390Sstevel@tonic-gate 
5400Sstevel@tonic-gate 	/* it's not an open double quote */
5410Sstevel@tonic-gate 	if (*optr != '"')
5420Sstevel@tonic-gate 		return (EC_SYNTAX_ERR);
5430Sstevel@tonic-gate 
5440Sstevel@tonic-gate 	/* skipping ending white spaces */
5450Sstevel@tonic-gate 	cptr = line + strlen(line) - 1;
5460Sstevel@tonic-gate 	while ((*cptr == ' ') || (*cptr == '\t') || (*cptr == '\n'))
5470Sstevel@tonic-gate 		cptr--;
5480Sstevel@tonic-gate 
5490Sstevel@tonic-gate 	/* it's not an close double quote */
5500Sstevel@tonic-gate 	if (*cptr != '"')
5510Sstevel@tonic-gate 		return (EC_SYNTAX_ERR);
5520Sstevel@tonic-gate 
5530Sstevel@tonic-gate 	/* close double quote is missing */
5540Sstevel@tonic-gate 	if (cptr == optr)
5550Sstevel@tonic-gate 		return (EC_SYNTAX_ERR);
5560Sstevel@tonic-gate 
5570Sstevel@tonic-gate 	/* replace close qoute by null to make a string */
5580Sstevel@tonic-gate 	*cptr = '\0';
5590Sstevel@tonic-gate 	/* move the begin pointer to the first char of string */
5600Sstevel@tonic-gate 	optr++;
5610Sstevel@tonic-gate 
5620Sstevel@tonic-gate 	tmpbuf = malloc(strlen(optr) + 1);
5630Sstevel@tonic-gate 	if (tmpbuf == NULL)
5640Sstevel@tonic-gate 		return (EC_FAILURE);
5650Sstevel@tonic-gate 
5660Sstevel@tonic-gate 	for (ptr = tmpbuf; *optr != '\0'; ptr++, optr++) {
5670Sstevel@tonic-gate 		/* if escape character, go to next character */
5680Sstevel@tonic-gate 		if (*optr == '\\') {
5690Sstevel@tonic-gate 			optr++;
5700Sstevel@tonic-gate 			if (*optr == '\0') {	/* for exampe, "xxx\" */
5710Sstevel@tonic-gate 				free(tmpbuf);
5720Sstevel@tonic-gate 				return (EC_SYNTAX_ERR);
5730Sstevel@tonic-gate 			}
5740Sstevel@tonic-gate 		}
5750Sstevel@tonic-gate 		*ptr = *optr;
5760Sstevel@tonic-gate 	}
5770Sstevel@tonic-gate 
5780Sstevel@tonic-gate 	*ptr = '\0';
5790Sstevel@tonic-gate 	*valtok = tmpbuf;
5800Sstevel@tonic-gate 	return (EC_SYNTAX_OK);
5810Sstevel@tonic-gate }
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate /*
5840Sstevel@tonic-gate  * Check the PROP syntax
5850Sstevel@tonic-gate  * PROP <name> <type> <access_mode> [<size> <value>]
5860Sstevel@tonic-gate  * supported prop types: void, int, uint, float, string
5870Sstevel@tonic-gate  * supported prop access_modes: r, w, rw
5880Sstevel@tonic-gate  * For void prop, <size> and <value> are not needed
5890Sstevel@tonic-gate  * For string prop, <size> will be set the actual string size if <size>
5900Sstevel@tonic-gate  * is 0
5910Sstevel@tonic-gate  */
5920Sstevel@tonic-gate static int
parse_prop(char * line,command_t * command)5930Sstevel@tonic-gate parse_prop(char *line, command_t *command)
5940Sstevel@tonic-gate {
5950Sstevel@tonic-gate 	char	*tok;
5960Sstevel@tonic-gate 	char	*pnametok;
5970Sstevel@tonic-gate 	int	typetok;
5980Sstevel@tonic-gate 	size_t	sizetok;
5990Sstevel@tonic-gate 	int	modetok;
6000Sstevel@tonic-gate 	char	*valtok;
6010Sstevel@tonic-gate 	char	*last;
6020Sstevel@tonic-gate 	char	*endptr;
6030Sstevel@tonic-gate 	int	err;
6040Sstevel@tonic-gate 
6050Sstevel@tonic-gate 	/* get the PROP directive */
6060Sstevel@tonic-gate 	tok = strtok_r(line, WHITESPACE, &last);
6070Sstevel@tonic-gate 	if (tok == NULL)
6080Sstevel@tonic-gate 		return (EC_INSUFFICIENT_TOKEN);
6090Sstevel@tonic-gate 
6100Sstevel@tonic-gate 	/* get the property name */
6110Sstevel@tonic-gate 	pnametok = strtok_r(last, WHITESPACE, &last);
6120Sstevel@tonic-gate 	if (pnametok == NULL)
6130Sstevel@tonic-gate 		return (EC_INSUFFICIENT_TOKEN);
6140Sstevel@tonic-gate 
6150Sstevel@tonic-gate 	/* get the type */
6160Sstevel@tonic-gate 	tok = strtok_r(last, WHITESPACE, &last);
6170Sstevel@tonic-gate 	if (tok == NULL)
6180Sstevel@tonic-gate 		return (EC_INSUFFICIENT_TOKEN);
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate 	if ((typetok = getpicltype(tok)) < 0)
6210Sstevel@tonic-gate 		return (EC_SYNTAX_ERR);
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate 	/* get mode */
6240Sstevel@tonic-gate 	tok = strtok_r(last, WHITESPACE, &last);
6250Sstevel@tonic-gate 	if (tok == NULL)
6260Sstevel@tonic-gate 		return (EC_INSUFFICIENT_TOKEN);
6270Sstevel@tonic-gate 
6280Sstevel@tonic-gate 	if ((modetok = getpiclmode(tok)) < 0)
6290Sstevel@tonic-gate 		return (EC_SYNTAX_ERR);
6300Sstevel@tonic-gate 
6310Sstevel@tonic-gate 	if (typetok == PICL_PTYPE_VOID) {
6320Sstevel@tonic-gate 		/* ignore the rest of arguments */
6330Sstevel@tonic-gate 		command->propcmd_valbuf = NULL;
6340Sstevel@tonic-gate 		command->propcmd_pname = strdup(pnametok);
6350Sstevel@tonic-gate 		if (command->propcmd_pname == NULL)
6360Sstevel@tonic-gate 			return (EC_FAILURE);
6370Sstevel@tonic-gate 		command->propcmd_type = typetok;
6380Sstevel@tonic-gate 		command->propcmd_accessmode = modetok;
6390Sstevel@tonic-gate 		command->propcmd_size = 0;
6400Sstevel@tonic-gate 		command->propcmd_proph = NULL;
6410Sstevel@tonic-gate 		return (EC_SYNTAX_OK);
6420Sstevel@tonic-gate 	}
6430Sstevel@tonic-gate 
6440Sstevel@tonic-gate 	/* get size */
6450Sstevel@tonic-gate 	tok = strtok_r(last, WHITESPACE, &last);
6460Sstevel@tonic-gate 	if (tok == NULL)
6470Sstevel@tonic-gate 		return (EC_INSUFFICIENT_TOKEN);
6480Sstevel@tonic-gate 
6490Sstevel@tonic-gate 	sizetok = (size_t)strtol(tok, &endptr, 0);
6500Sstevel@tonic-gate 	if (endptr != (tok + strlen(tok)))
6510Sstevel@tonic-gate 		return (EC_SYNTAX_ERR);
6520Sstevel@tonic-gate 
6530Sstevel@tonic-gate 	/* get val */
6540Sstevel@tonic-gate 	if (typetok == PICL_PTYPE_CHARSTRING) {
6550Sstevel@tonic-gate 		err = get_string_token(last, &valtok);
6560Sstevel@tonic-gate 		if (err != EC_SYNTAX_OK)
6570Sstevel@tonic-gate 			return (err);
6580Sstevel@tonic-gate 		if (sizetok == 0)
6590Sstevel@tonic-gate 			sizetok = strlen(valtok) + 1;
6600Sstevel@tonic-gate 		command->propcmd_valbuf = valtok;
6610Sstevel@tonic-gate 	} else {
6620Sstevel@tonic-gate 		valtok = strtok_r(last, WHITESPACE, &last);
6630Sstevel@tonic-gate 		if (valtok == NULL)
6640Sstevel@tonic-gate 			return (EC_INSUFFICIENT_TOKEN);
6650Sstevel@tonic-gate 		/* check if more tokens */
6660Sstevel@tonic-gate 		tok = strtok_r(last, WHITESPACE, &last);
6670Sstevel@tonic-gate 		if (tok != NULL)
6680Sstevel@tonic-gate 			return (EC_SYNTAX_ERR);
6690Sstevel@tonic-gate 		command->propcmd_valbuf = malloc(sizetok);
6700Sstevel@tonic-gate 		if (command->propcmd_valbuf == NULL)
6710Sstevel@tonic-gate 			return (EC_FAILURE);
6720Sstevel@tonic-gate 		err = validate_size_and_cvt_val(command->propcmd_valbuf,
6730Sstevel@tonic-gate 		    sizetok, typetok, valtok);
6740Sstevel@tonic-gate 		if (err != EC_SYNTAX_OK) {
6750Sstevel@tonic-gate 			free(command->propcmd_valbuf);
6760Sstevel@tonic-gate 			return (err);
6770Sstevel@tonic-gate 		}
6780Sstevel@tonic-gate 	}
6790Sstevel@tonic-gate 
6800Sstevel@tonic-gate 	command->propcmd_pname = strdup(pnametok);
6810Sstevel@tonic-gate 	if (command->propcmd_pname == NULL)
6820Sstevel@tonic-gate 		return (EC_FAILURE);
6830Sstevel@tonic-gate 	command->propcmd_type = typetok;
6840Sstevel@tonic-gate 	command->propcmd_accessmode = modetok;
6850Sstevel@tonic-gate 	command->propcmd_size = sizetok;
6860Sstevel@tonic-gate 	command->propcmd_proph = NULL;
6870Sstevel@tonic-gate 	return (EC_SYNTAX_OK);
6880Sstevel@tonic-gate }
6890Sstevel@tonic-gate 
6900Sstevel@tonic-gate /*
6910Sstevel@tonic-gate  * Add a property to the row, the row gets added to the node at endrow
6920Sstevel@tonic-gate  */
6930Sstevel@tonic-gate static int
add_proph_to_row(command_t * command,picl_prophdl_t proph)6940Sstevel@tonic-gate add_proph_to_row(command_t *command, picl_prophdl_t proph)
6950Sstevel@tonic-gate {
6960Sstevel@tonic-gate 	if (command->rowcmd_index >= command->rowcmd_nproph)
6970Sstevel@tonic-gate 		return (PICL_FAILURE);
6980Sstevel@tonic-gate 	command->rowcmd_prophs[command->rowcmd_index] = proph;
6990Sstevel@tonic-gate 	command->rowcmd_index++;
7000Sstevel@tonic-gate 	return (PICL_SUCCESS);
7010Sstevel@tonic-gate }
7020Sstevel@tonic-gate 
7030Sstevel@tonic-gate /*
7040Sstevel@tonic-gate  * Process the PROP command and add the specified property under the given
7050Sstevel@tonic-gate  * node handle
7060Sstevel@tonic-gate  */
7070Sstevel@tonic-gate static int
process_prop(cmdbuf_t * cmds,command_t * command,picl_nodehdl_t nodeh)7080Sstevel@tonic-gate process_prop(cmdbuf_t *cmds, command_t *command, picl_nodehdl_t nodeh)
7090Sstevel@tonic-gate {
7100Sstevel@tonic-gate 	ptree_propinfo_t	propinfo;
7110Sstevel@tonic-gate 	picl_prophdl_t		proph;
7120Sstevel@tonic-gate 	int			err;
7130Sstevel@tonic-gate 
7140Sstevel@tonic-gate 	/* prop in discarded row */
7150Sstevel@tonic-gate 	if (cmds->inside_row_block &&
7160Sstevel@tonic-gate 	    cmds->commands[cmds->current_row].rowcmd_nproph == 0)
7170Sstevel@tonic-gate 		return (PICL_SUCCESS);
7180Sstevel@tonic-gate 
7190Sstevel@tonic-gate 	err = ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
7200Sstevel@tonic-gate 	    command->propcmd_type, command->propcmd_accessmode,
7210Sstevel@tonic-gate 	    command->propcmd_size, command->propcmd_pname, NULL,
7220Sstevel@tonic-gate 	    NULL);
7230Sstevel@tonic-gate 
7240Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
7250Sstevel@tonic-gate 		return (err);
7260Sstevel@tonic-gate 
7270Sstevel@tonic-gate 	err = ptree_create_prop(&propinfo, command->propcmd_valbuf, &proph);
7280Sstevel@tonic-gate 
7290Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
7300Sstevel@tonic-gate 		return (err);
7310Sstevel@tonic-gate 
7320Sstevel@tonic-gate 	command->propcmd_proph = proph;
7330Sstevel@tonic-gate 
7340Sstevel@tonic-gate 	if (cmds->inside_row_block) {
7350Sstevel@tonic-gate 		err = add_proph_to_row(&cmds->commands[cmds->current_row],
7360Sstevel@tonic-gate 		    proph);
7370Sstevel@tonic-gate 	} else {
7380Sstevel@tonic-gate 		err = ptree_add_prop(nodeh, proph);
7390Sstevel@tonic-gate 	}
7400Sstevel@tonic-gate 
7410Sstevel@tonic-gate 	return (err);
7420Sstevel@tonic-gate }
7430Sstevel@tonic-gate 
7440Sstevel@tonic-gate /*
7450Sstevel@tonic-gate  * free refnode_cmd_t
7460Sstevel@tonic-gate  */
7470Sstevel@tonic-gate static void
free_refnode(command_t * command)7480Sstevel@tonic-gate free_refnode(command_t *command)
7490Sstevel@tonic-gate {
7500Sstevel@tonic-gate 	free(command->refnodecmd_name);
7510Sstevel@tonic-gate 	free(command->refnodecmd_class);
7520Sstevel@tonic-gate 	free(command->refnodecmd_dstnode);
7530Sstevel@tonic-gate }
7540Sstevel@tonic-gate 
7550Sstevel@tonic-gate /*
7560Sstevel@tonic-gate  * Check the REFNODE syntax
7570Sstevel@tonic-gate  *
7580Sstevel@tonic-gate  * REFNODE <name> <class> with <destnode> -- if <destnode> exists,
7590Sstevel@tonic-gate  * create node with nodename <name> and piclclass <class>
7600Sstevel@tonic-gate  */
7610Sstevel@tonic-gate static int
parse_refnode(char * line,command_t * command)7620Sstevel@tonic-gate parse_refnode(char *line, command_t *command)
7630Sstevel@tonic-gate {
7640Sstevel@tonic-gate 	char	*tok;
7650Sstevel@tonic-gate 	char	*dsttok;
7660Sstevel@tonic-gate 	char	*classnm;
7670Sstevel@tonic-gate 	char	*nodenm;
7680Sstevel@tonic-gate 	char	*last;
7690Sstevel@tonic-gate 
7700Sstevel@tonic-gate 	/* get the directive */
7710Sstevel@tonic-gate 	tok = strtok_r(line, WHITESPACE, &last);
7720Sstevel@tonic-gate 	if (tok == NULL)
7730Sstevel@tonic-gate 		return (EC_INSUFFICIENT_TOKEN);
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate 	/* get the nodename */
7760Sstevel@tonic-gate 	nodenm = strtok_r(last, WHITESPACE, &last);
7770Sstevel@tonic-gate 	if (nodenm == NULL)
7780Sstevel@tonic-gate 		return (EC_INSUFFICIENT_TOKEN);
7790Sstevel@tonic-gate 
7800Sstevel@tonic-gate 	/* get the class */
7810Sstevel@tonic-gate 	classnm = strtok_r(last, WHITESPACE, &last);
7820Sstevel@tonic-gate 	if (classnm == NULL)
7830Sstevel@tonic-gate 		return (EC_INSUFFICIENT_TOKEN);
7840Sstevel@tonic-gate 
7850Sstevel@tonic-gate 	/* get the WITH keyword */
7860Sstevel@tonic-gate 	tok = strtok_r(last, WHITESPACE, &last);
7870Sstevel@tonic-gate 	if (tok == NULL)
7880Sstevel@tonic-gate 		return (EC_INSUFFICIENT_TOKEN);
7890Sstevel@tonic-gate 
7900Sstevel@tonic-gate 	if (strcasecmp(tok, KEYWORD_WITH_STR) != 0)
7910Sstevel@tonic-gate 		return (EC_SYNTAX_ERR);
7920Sstevel@tonic-gate 
7930Sstevel@tonic-gate 	/* get the dst node */
7940Sstevel@tonic-gate 	dsttok = strtok_r(last, WHITESPACE, &last);
7950Sstevel@tonic-gate 	if (dsttok == NULL)
7960Sstevel@tonic-gate 		return (EC_INSUFFICIENT_TOKEN);
7970Sstevel@tonic-gate 
7980Sstevel@tonic-gate 	/* check if more tokens */
7990Sstevel@tonic-gate 	tok = strtok_r(last, WHITESPACE, &last);
8000Sstevel@tonic-gate 	if (tok != NULL)
8010Sstevel@tonic-gate 		return (EC_SYNTAX_ERR);
8020Sstevel@tonic-gate 
8030Sstevel@tonic-gate 	command->refnodecmd_name = strdup(nodenm);
8040Sstevel@tonic-gate 	command->refnodecmd_class = strdup(classnm);
8050Sstevel@tonic-gate 	command->refnodecmd_dstnode = strdup(dsttok);
8060Sstevel@tonic-gate 	command->refnodecmd_nodeh = NULL;
8070Sstevel@tonic-gate 	if ((command->refnodecmd_name == NULL) ||
8080Sstevel@tonic-gate 	    (command->refnodecmd_class == NULL) ||
8090Sstevel@tonic-gate 	    (command->refnodecmd_dstnode == NULL))
8100Sstevel@tonic-gate 		return (EC_FAILURE);
8110Sstevel@tonic-gate 
8120Sstevel@tonic-gate 	return (EC_SYNTAX_OK);
8130Sstevel@tonic-gate }
8140Sstevel@tonic-gate 
8150Sstevel@tonic-gate /*
8160Sstevel@tonic-gate  * Process the REFNODE command
8170Sstevel@tonic-gate  */
8180Sstevel@tonic-gate static int
process_refnode(command_t * command,picl_nodehdl_t parh)8190Sstevel@tonic-gate process_refnode(command_t *command, picl_nodehdl_t parh)
8200Sstevel@tonic-gate {
8210Sstevel@tonic-gate 	picl_nodehdl_t	dsth;
8220Sstevel@tonic-gate 	picl_nodehdl_t	nodeh;
8230Sstevel@tonic-gate 	int		err;
8240Sstevel@tonic-gate 
8250Sstevel@tonic-gate 	if ((ptree_get_node_by_path(command->refnodecmd_dstnode,
8260Sstevel@tonic-gate 	    &dsth) == PICL_SUCCESS)) {
8270Sstevel@tonic-gate 		err = ptree_create_and_add_node(parh, command->refnodecmd_name,
8280Sstevel@tonic-gate 		    command->refnodecmd_class, &nodeh);
8290Sstevel@tonic-gate 		if (err == PICL_SUCCESS)
8300Sstevel@tonic-gate 			command->refnodecmd_nodeh = nodeh;
8310Sstevel@tonic-gate 
8320Sstevel@tonic-gate 		return (err);
8330Sstevel@tonic-gate 	}
8340Sstevel@tonic-gate 
8350Sstevel@tonic-gate 	return (PICL_SUCCESS);
8360Sstevel@tonic-gate }
8370Sstevel@tonic-gate 
8380Sstevel@tonic-gate /*
8390Sstevel@tonic-gate  * free refprop_cmd_t
8400Sstevel@tonic-gate  */
8410Sstevel@tonic-gate static void
free_refprop(command_t * command)8420Sstevel@tonic-gate free_refprop(command_t *command)
8430Sstevel@tonic-gate {
8440Sstevel@tonic-gate 	free(command->refpropcmd_pname);
8450Sstevel@tonic-gate 	free(command->refpropcmd_dstnode);
8460Sstevel@tonic-gate }
8470Sstevel@tonic-gate 
8480Sstevel@tonic-gate /*
8490Sstevel@tonic-gate  * Check the REFPROP syntax
8500Sstevel@tonic-gate  *
8510Sstevel@tonic-gate  * REFPROP <prop> <destnode> -- creates a reference property to <destnode>
8520Sstevel@tonic-gate  */
8530Sstevel@tonic-gate static int
parse_refprop(char * line,command_t * command)8540Sstevel@tonic-gate parse_refprop(char *line, command_t *command)
8550Sstevel@tonic-gate {
8560Sstevel@tonic-gate 	char	*tok;
8570Sstevel@tonic-gate 	char	*pnametok;
8580Sstevel@tonic-gate 	char	*dsttok;
8590Sstevel@tonic-gate 	char	*last;
8600Sstevel@tonic-gate 
8610Sstevel@tonic-gate 	/* get the REFPROP directive */
8620Sstevel@tonic-gate 	tok = strtok_r(line, WHITESPACE, &last);
8630Sstevel@tonic-gate 	if (tok == NULL)
8640Sstevel@tonic-gate 		return (EC_INSUFFICIENT_TOKEN);
8650Sstevel@tonic-gate 
8660Sstevel@tonic-gate 	/* get the propname  */
8670Sstevel@tonic-gate 	pnametok = strtok_r(last, WHITESPACE, &last);
8680Sstevel@tonic-gate 	if (pnametok == NULL)
8690Sstevel@tonic-gate 		return (EC_INSUFFICIENT_TOKEN);
8700Sstevel@tonic-gate 
8710Sstevel@tonic-gate 	dsttok = strtok_r(last, WHITESPACE, &last);
8720Sstevel@tonic-gate 	if (dsttok == NULL)
8730Sstevel@tonic-gate 		return (EC_INSUFFICIENT_TOKEN);
8740Sstevel@tonic-gate 
8750Sstevel@tonic-gate 	/* check if more tokens */
8760Sstevel@tonic-gate 	tok = strtok_r(last, WHITESPACE, &last);
8770Sstevel@tonic-gate 	if (tok != NULL)
8780Sstevel@tonic-gate 		return (EC_SYNTAX_ERR);
8790Sstevel@tonic-gate 
8800Sstevel@tonic-gate 	command->refpropcmd_pname = strdup(pnametok);
8810Sstevel@tonic-gate 	command->refpropcmd_dstnode = strdup(dsttok);
8820Sstevel@tonic-gate 	command->refpropcmd_proph = NULL;
8830Sstevel@tonic-gate 	if ((command->refpropcmd_pname == NULL) ||
8840Sstevel@tonic-gate 	    (command->refpropcmd_dstnode == NULL))
8850Sstevel@tonic-gate 		return (EC_FAILURE);
8860Sstevel@tonic-gate 
8870Sstevel@tonic-gate 	return (EC_SYNTAX_OK);
8880Sstevel@tonic-gate }
8890Sstevel@tonic-gate 
8900Sstevel@tonic-gate /*
8910Sstevel@tonic-gate  * Process the REFPROP command
8920Sstevel@tonic-gate  */
8930Sstevel@tonic-gate static int
process_refprop(cmdbuf_t * cmds,command_t * command,picl_nodehdl_t nodeh)8940Sstevel@tonic-gate process_refprop(cmdbuf_t *cmds, command_t *command, picl_nodehdl_t nodeh)
8950Sstevel@tonic-gate {
8960Sstevel@tonic-gate 	int			err;
8970Sstevel@tonic-gate 	picl_nodehdl_t 		dsth;
8980Sstevel@tonic-gate 	picl_prophdl_t 		proph;
8990Sstevel@tonic-gate 	ptree_propinfo_t	propinfo;
9000Sstevel@tonic-gate 
9010Sstevel@tonic-gate 	/* refprop in discarded row */
9020Sstevel@tonic-gate 	if (cmds->inside_row_block &&
9030Sstevel@tonic-gate 	    cmds->commands[cmds->current_row].rowcmd_nproph == 0)
9040Sstevel@tonic-gate 		return (PICL_SUCCESS);
9050Sstevel@tonic-gate 
9060Sstevel@tonic-gate 	/* try finding the refprop's dstnode */
9070Sstevel@tonic-gate 	err = ptree_get_node_by_path(command->refpropcmd_dstnode, &dsth);
9080Sstevel@tonic-gate 
9090Sstevel@tonic-gate 	/* dstnode doesn't exist, return */
9100Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
9110Sstevel@tonic-gate 		return (err);
9120Sstevel@tonic-gate 
9130Sstevel@tonic-gate 	/* dstnode exists, try adding the refprop to nodeh */
9140Sstevel@tonic-gate 	err = ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
915*9407SMichael.Bergknoff@Sun.COM 	    PICL_PTYPE_REFERENCE, PICL_READ, sizeof (picl_nodehdl_t),
916*9407SMichael.Bergknoff@Sun.COM 	    command->refpropcmd_pname, NULL, NULL);
9170Sstevel@tonic-gate 
9180Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
9190Sstevel@tonic-gate 		return (err);
9200Sstevel@tonic-gate 
9210Sstevel@tonic-gate 	err = ptree_create_prop(&propinfo, &dsth, &proph);
9220Sstevel@tonic-gate 
9230Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
9240Sstevel@tonic-gate 		return (err);
9250Sstevel@tonic-gate 
9260Sstevel@tonic-gate 	command->refpropcmd_proph = proph;
9270Sstevel@tonic-gate 
9280Sstevel@tonic-gate 	if (cmds->inside_row_block) {
9290Sstevel@tonic-gate 		err = add_proph_to_row(&cmds->commands[cmds->current_row],
9300Sstevel@tonic-gate 		    proph);
9310Sstevel@tonic-gate 	} else {
9320Sstevel@tonic-gate 		err = ptree_add_prop(nodeh, proph);
9330Sstevel@tonic-gate 	}
9340Sstevel@tonic-gate 
9350Sstevel@tonic-gate 	return (err);
9360Sstevel@tonic-gate }
9370Sstevel@tonic-gate 
9380Sstevel@tonic-gate /*
9390Sstevel@tonic-gate  * free table_cmd_t
9400Sstevel@tonic-gate  */
9410Sstevel@tonic-gate static void
free_table(command_t * command)9420Sstevel@tonic-gate free_table(command_t *command)
9430Sstevel@tonic-gate {
9440Sstevel@tonic-gate 	if (command->tablecmd_tname)
9450Sstevel@tonic-gate 		free(command->tablecmd_tname);
9460Sstevel@tonic-gate }
9470Sstevel@tonic-gate 
9480Sstevel@tonic-gate /*
9490Sstevel@tonic-gate  * Check the TABLE syntax
9500Sstevel@tonic-gate  * TABLE <table_prop_name>
9510Sstevel@tonic-gate  *
9520Sstevel@tonic-gate  */
9530Sstevel@tonic-gate static int
parse_table(char * line,command_t * command)9540Sstevel@tonic-gate parse_table(char *line, command_t *command)
9550Sstevel@tonic-gate {
9560Sstevel@tonic-gate 	char	*tok = NULL;
9570Sstevel@tonic-gate 	char	*tnametok = NULL;
9580Sstevel@tonic-gate 	char	*last = NULL;
9590Sstevel@tonic-gate 
9600Sstevel@tonic-gate 	/* get the TABLE directive */
9610Sstevel@tonic-gate 	tok = strtok_r(line, WHITESPACE, &last);
9620Sstevel@tonic-gate 	if (tok == NULL)
9630Sstevel@tonic-gate 		return (EC_INSUFFICIENT_TOKEN);
9640Sstevel@tonic-gate 
9650Sstevel@tonic-gate 	/* get the property name */
9660Sstevel@tonic-gate 	tnametok = strtok_r(last, WHITESPACE, &last);
9670Sstevel@tonic-gate 	if (tnametok == NULL)
9680Sstevel@tonic-gate 		return (EC_INSUFFICIENT_TOKEN);
9690Sstevel@tonic-gate 
9700Sstevel@tonic-gate 	command->tablecmd_tname = strdup(tnametok);
9710Sstevel@tonic-gate 	if (command->tablecmd_tname == NULL)
9720Sstevel@tonic-gate 		return (EC_FAILURE);
9730Sstevel@tonic-gate 
9740Sstevel@tonic-gate 	command->tablecmd_newtbl = 0;
9750Sstevel@tonic-gate 	command->tablecmd_tblh = NULL;
9760Sstevel@tonic-gate 
9770Sstevel@tonic-gate 	return (EC_SYNTAX_OK);
9780Sstevel@tonic-gate }
9790Sstevel@tonic-gate 
9800Sstevel@tonic-gate /*
9810Sstevel@tonic-gate  * Process the TABLE command and add the specified property under the given
9820Sstevel@tonic-gate  * node handle
9830Sstevel@tonic-gate  */
9840Sstevel@tonic-gate static int
process_table(command_t * command,picl_nodehdl_t nodeh)9850Sstevel@tonic-gate process_table(command_t *command, picl_nodehdl_t nodeh)
9860Sstevel@tonic-gate {
9870Sstevel@tonic-gate 	int			err;
9880Sstevel@tonic-gate 	picl_prophdl_t 		tblh;
9890Sstevel@tonic-gate 	picl_prophdl_t 		proph;
9900Sstevel@tonic-gate 	ptree_propinfo_t	propinfo;
9910Sstevel@tonic-gate 
9920Sstevel@tonic-gate 	/* find if table already exists */
9930Sstevel@tonic-gate 	err = ptree_get_prop_by_name(nodeh, command->tablecmd_tname, &tblh);
9940Sstevel@tonic-gate 	if (err == PICL_SUCCESS) {
9950Sstevel@tonic-gate 		err = ptree_get_propinfo(tblh, &propinfo);
9960Sstevel@tonic-gate 		if (err != PICL_SUCCESS)
9970Sstevel@tonic-gate 			return (err);
9980Sstevel@tonic-gate 		/* prop with the same name as table? */
9990Sstevel@tonic-gate 		if (propinfo.piclinfo.type != PICL_PTYPE_TABLE)
10000Sstevel@tonic-gate 			return (EC_SYNTAX_ERR);
10010Sstevel@tonic-gate 		command->tablecmd_newtbl = 0;
10020Sstevel@tonic-gate 		command->tablecmd_tblh = tblh;
10030Sstevel@tonic-gate 		return (PICL_SUCCESS);
10040Sstevel@tonic-gate 	}
10050Sstevel@tonic-gate 
10060Sstevel@tonic-gate 	/* init and create a new table */
10070Sstevel@tonic-gate 	err = ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
1008*9407SMichael.Bergknoff@Sun.COM 	    PICL_PTYPE_TABLE, PICL_READ|PICL_WRITE,
1009*9407SMichael.Bergknoff@Sun.COM 	    sizeof (picl_prophdl_t), command->tablecmd_tname, NULL, NULL);
10100Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
10110Sstevel@tonic-gate 		return (err);
10120Sstevel@tonic-gate 
10130Sstevel@tonic-gate 	err = ptree_create_table(&tblh);
10140Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
10150Sstevel@tonic-gate 		return (err);
10160Sstevel@tonic-gate 
10170Sstevel@tonic-gate 	command->tablecmd_newtbl = 1;
10180Sstevel@tonic-gate 	command->tablecmd_tblh = tblh;
10190Sstevel@tonic-gate 
10200Sstevel@tonic-gate 	err = ptree_create_prop(&propinfo, &tblh, &proph);
10210Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
10220Sstevel@tonic-gate 		return (err);
10230Sstevel@tonic-gate 
10240Sstevel@tonic-gate 	err = ptree_add_prop(nodeh, proph);
10250Sstevel@tonic-gate 
10260Sstevel@tonic-gate 	return (err);
10270Sstevel@tonic-gate }
10280Sstevel@tonic-gate 
10290Sstevel@tonic-gate /*
10300Sstevel@tonic-gate  * Process the ROW command by alloc'ing space to store the prop handles for
10310Sstevel@tonic-gate  * the whole row. The number of props in the row gets known while parsing.
10320Sstevel@tonic-gate  */
10330Sstevel@tonic-gate static int
process_row(command_t * command)10340Sstevel@tonic-gate process_row(command_t *command)
10350Sstevel@tonic-gate {
10360Sstevel@tonic-gate 	command->rowcmd_index = 0;
10370Sstevel@tonic-gate 	command->rowcmd_prophs =
1038*9407SMichael.Bergknoff@Sun.COM 	    malloc(command->rowcmd_nproph * sizeof (picl_prophdl_t));
10390Sstevel@tonic-gate 
10400Sstevel@tonic-gate 	if (command->rowcmd_prophs == NULL)
10410Sstevel@tonic-gate 		return (PICL_FAILURE);
10420Sstevel@tonic-gate 
10430Sstevel@tonic-gate 	return (PICL_SUCCESS);
10440Sstevel@tonic-gate }
10450Sstevel@tonic-gate 
10460Sstevel@tonic-gate /*
10470Sstevel@tonic-gate  * Process the ENDROW command. If a valid row, add the row to the ptree.
10480Sstevel@tonic-gate  */
10490Sstevel@tonic-gate static int
process_endrow(cmdbuf_t * cmds)10500Sstevel@tonic-gate process_endrow(cmdbuf_t *cmds)
10510Sstevel@tonic-gate {
10520Sstevel@tonic-gate 	int		err;
10530Sstevel@tonic-gate 	int		i;
10540Sstevel@tonic-gate 	command_t	*curr_row;
10550Sstevel@tonic-gate 
10560Sstevel@tonic-gate 	curr_row = &cmds->commands[cmds->current_row];
10570Sstevel@tonic-gate 
10580Sstevel@tonic-gate 	/* if nproph == 0, some row prop had problems, don't add  */
10590Sstevel@tonic-gate 	if (curr_row->rowcmd_nproph == 0) {
10600Sstevel@tonic-gate 		for (i = 0; i < curr_row->rowcmd_index; i++) {
10610Sstevel@tonic-gate 			(void) ptree_delete_prop(curr_row->rowcmd_prophs[i]);
10620Sstevel@tonic-gate 			(void) ptree_destroy_prop(curr_row->rowcmd_prophs[i]);
10630Sstevel@tonic-gate 		}
10640Sstevel@tonic-gate 		err = PICL_SUCCESS;
10650Sstevel@tonic-gate 	} else
10660Sstevel@tonic-gate 		err = ptree_add_row_to_table(
1067*9407SMichael.Bergknoff@Sun.COM 		    cmds->commands[cmds->current_tbl].tablecmd_tblh,
1068*9407SMichael.Bergknoff@Sun.COM 		    curr_row->rowcmd_nproph,
1069*9407SMichael.Bergknoff@Sun.COM 		    curr_row->rowcmd_prophs);
10700Sstevel@tonic-gate 
10710Sstevel@tonic-gate 	/* let go the space alloc'd in process_row */
10720Sstevel@tonic-gate 	free(curr_row->rowcmd_prophs);
10730Sstevel@tonic-gate 	curr_row->rowcmd_prophs = NULL;
10740Sstevel@tonic-gate 
10750Sstevel@tonic-gate 	return (err);
10760Sstevel@tonic-gate }
10770Sstevel@tonic-gate 
10780Sstevel@tonic-gate /*
10790Sstevel@tonic-gate  * Check the VERBOSE syntax
10800Sstevel@tonic-gate  * VERBOSE <level>
10810Sstevel@tonic-gate  */
10820Sstevel@tonic-gate static int
parse_verbose(cmdbuf_t * cmds,char * line,command_t * command)10830Sstevel@tonic-gate parse_verbose(cmdbuf_t *cmds, char *line, command_t *command)
10840Sstevel@tonic-gate {
10850Sstevel@tonic-gate 	char	*tok;
10860Sstevel@tonic-gate 	char	*level;
10870Sstevel@tonic-gate 	char	*last;
10880Sstevel@tonic-gate 	char	*endptr;
10890Sstevel@tonic-gate 	int	verbose_level;
10900Sstevel@tonic-gate 
10910Sstevel@tonic-gate 	/* get the VERBOSE directive */
10920Sstevel@tonic-gate 	tok = strtok_r(line, WHITESPACE, &last);
10930Sstevel@tonic-gate 	if (tok == NULL)
10940Sstevel@tonic-gate 		return (EC_INSUFFICIENT_TOKEN);
10950Sstevel@tonic-gate 
10960Sstevel@tonic-gate 	/* get verbose level */
10970Sstevel@tonic-gate 	level = strtok_r(last, WHITESPACE, &last);
10980Sstevel@tonic-gate 	if (level == NULL)
10990Sstevel@tonic-gate 		return (EC_INSUFFICIENT_TOKEN);
11000Sstevel@tonic-gate 	verbose_level = strtol(level, &endptr, 0);
11010Sstevel@tonic-gate 	if (endptr != (level + strlen(level)))
11020Sstevel@tonic-gate 		return (EC_SYNTAX_ERR);
11030Sstevel@tonic-gate 
11040Sstevel@tonic-gate 	/* check if more tokens */
11050Sstevel@tonic-gate 	tok = strtok_r(last, WHITESPACE, &last);
11060Sstevel@tonic-gate 	if (tok != NULL)
11070Sstevel@tonic-gate 		return (EC_SYNTAX_ERR);
11080Sstevel@tonic-gate 
11090Sstevel@tonic-gate 	cmds->verbose = verbose_level;
11100Sstevel@tonic-gate 	command->verbosecmd_level = verbose_level;
11110Sstevel@tonic-gate 
11120Sstevel@tonic-gate 	return (EC_SYNTAX_OK);
11130Sstevel@tonic-gate }
11140Sstevel@tonic-gate 
11150Sstevel@tonic-gate /*
11160Sstevel@tonic-gate  * Process the VERBOSE command to set the verbose level
11170Sstevel@tonic-gate  */
11180Sstevel@tonic-gate static int
process_verbose(cmdbuf_t * cmds,command_t * command)11190Sstevel@tonic-gate process_verbose(cmdbuf_t *cmds, command_t *command)
11200Sstevel@tonic-gate {
11210Sstevel@tonic-gate 	cmds->verbose = command->verbosecmd_level;
11220Sstevel@tonic-gate 	return (PICL_SUCCESS);
11230Sstevel@tonic-gate }
11240Sstevel@tonic-gate 
11250Sstevel@tonic-gate /*
11260Sstevel@tonic-gate  * parse and tokenize the line
11270Sstevel@tonic-gate  */
11280Sstevel@tonic-gate static int
parse_and_tokenize_line(cmdbuf_t * cmds,char * buf,command_t * command)11290Sstevel@tonic-gate parse_and_tokenize_line(cmdbuf_t *cmds, char *buf, command_t *command)
11300Sstevel@tonic-gate {
11310Sstevel@tonic-gate 	char		rec[RECORD_SIZE_MAX];
11320Sstevel@tonic-gate 	char		*tok;
11330Sstevel@tonic-gate 	int		err;
11340Sstevel@tonic-gate 	char		*last;
11350Sstevel@tonic-gate 	int		id;
11360Sstevel@tonic-gate 
11370Sstevel@tonic-gate 	(void) strcpy(rec, buf);
11380Sstevel@tonic-gate 	tok = strtok_r(rec, RECORD_WHITESPACE, &last);
11390Sstevel@tonic-gate 	if (tok == NULL)
11400Sstevel@tonic-gate 		return (EC_INSUFFICIENT_TOKEN);
11410Sstevel@tonic-gate 
11420Sstevel@tonic-gate 	id = get_token_id(tok);
11430Sstevel@tonic-gate 
11440Sstevel@tonic-gate 	(void) strcpy(rec, buf);
11450Sstevel@tonic-gate 
11460Sstevel@tonic-gate 	switch (id) {
11470Sstevel@tonic-gate 	case TOK_VERSION:
11480Sstevel@tonic-gate 		err = parse_version(cmds, rec);
11490Sstevel@tonic-gate 		break;
11500Sstevel@tonic-gate 	case TOK_CLASSPATH:
11510Sstevel@tonic-gate 	case TOK_NAMEPATH:
11520Sstevel@tonic-gate 		if (cmds->inside_node_block != 0)
11530Sstevel@tonic-gate 			return (EC_PATH_ERR);
11540Sstevel@tonic-gate 
11550Sstevel@tonic-gate 		err = parse_path(rec, command);
11560Sstevel@tonic-gate 		if (err != EC_SYNTAX_OK)
11570Sstevel@tonic-gate 			return (err);
11580Sstevel@tonic-gate 		break;
11590Sstevel@tonic-gate 	case TOK_NODE:
11600Sstevel@tonic-gate 		/* Check for NODE outside of TABLE, ROW */
11610Sstevel@tonic-gate 		if ((cmds->inside_table_block != 0) ||
11620Sstevel@tonic-gate 		    (cmds->inside_row_block != 0))
11630Sstevel@tonic-gate 			return (EC_SYNTAX_ERR);
11640Sstevel@tonic-gate 		err = parse_node(rec, command);
11650Sstevel@tonic-gate 		if (err != EC_SYNTAX_OK)
11660Sstevel@tonic-gate 			return (err);
11670Sstevel@tonic-gate 		cmds->inside_node_block++;
11680Sstevel@tonic-gate 		break;
11690Sstevel@tonic-gate 	case TOK_ENDNODE:
11700Sstevel@tonic-gate 		/* Check for ENDNODE outside of TABLE, ROW */
11710Sstevel@tonic-gate 		if ((cmds->inside_table_block != 0) ||
11720Sstevel@tonic-gate 		    (cmds->inside_row_block != 0))
11730Sstevel@tonic-gate 			return (EC_SYNTAX_ERR);
11740Sstevel@tonic-gate 		cmds->inside_node_block--;
11750Sstevel@tonic-gate 		err = EC_SYNTAX_OK;
11760Sstevel@tonic-gate 		break;
11770Sstevel@tonic-gate 	case TOK_PROP:
11780Sstevel@tonic-gate 		/* Check if inside TABLE, but not in ROW */
11790Sstevel@tonic-gate 		if ((cmds->inside_table_block != 0) &&
11800Sstevel@tonic-gate 		    (cmds->inside_row_block == 0))
11810Sstevel@tonic-gate 			return (EC_SYNTAX_ERR);
11820Sstevel@tonic-gate 		err = parse_prop(rec, command);
11830Sstevel@tonic-gate 		if (err != EC_SYNTAX_OK)
11840Sstevel@tonic-gate 			return (err);
11850Sstevel@tonic-gate 		if (cmds->inside_row_block) {
11860Sstevel@tonic-gate 			cmds->commands[cmds->current_row].rowcmd_nproph++;
11870Sstevel@tonic-gate 		}
11880Sstevel@tonic-gate 		break;
11890Sstevel@tonic-gate 	case TOK_REFNODE:
11900Sstevel@tonic-gate 		err = parse_refnode(rec, command);
11910Sstevel@tonic-gate 		if (err != EC_SYNTAX_OK)
11920Sstevel@tonic-gate 			return (err);
11930Sstevel@tonic-gate 		break;
11940Sstevel@tonic-gate 	case TOK_REFPROP:
11950Sstevel@tonic-gate 		/* Check if inside TABLE, but not in ROW */
11960Sstevel@tonic-gate 		if ((cmds->inside_table_block != 0) &&
11970Sstevel@tonic-gate 		    (cmds->inside_row_block == 0))
11980Sstevel@tonic-gate 			return (EC_SYNTAX_ERR);
11990Sstevel@tonic-gate 		err = parse_refprop(rec, command);
12000Sstevel@tonic-gate 		if (err != EC_SYNTAX_OK)
12010Sstevel@tonic-gate 			return (err);
12020Sstevel@tonic-gate 		if (cmds->inside_row_block) {
12030Sstevel@tonic-gate 			cmds->commands[cmds->current_row].rowcmd_nproph++;
12040Sstevel@tonic-gate 		}
12050Sstevel@tonic-gate 		break;
12060Sstevel@tonic-gate 	case TOK_TABLE:
12070Sstevel@tonic-gate 		/* Table/Row supported in version 1.1 and above */
12080Sstevel@tonic-gate 		if (cmds->version_no < (float)SUPPORTED_VERSION_NUM)
12090Sstevel@tonic-gate 			return (EC_UNSUPPORTED);
12100Sstevel@tonic-gate 		if (cmds->inside_table_block != 0)
12110Sstevel@tonic-gate 			return (EC_SYNTAX_ERR);
12120Sstevel@tonic-gate 		err = parse_table(rec, command);
12130Sstevel@tonic-gate 		if (err != EC_SYNTAX_OK)
12140Sstevel@tonic-gate 			return (err);
12150Sstevel@tonic-gate 		cmds->inside_table_block = 1;
12160Sstevel@tonic-gate 		break;
12170Sstevel@tonic-gate 	case TOK_ENDTABLE:
12180Sstevel@tonic-gate 		/* Check for ENDTABLE before TABLE */
12190Sstevel@tonic-gate 		if (cmds->inside_table_block == 0)
12200Sstevel@tonic-gate 			return (EC_SYNTAX_ERR);
12210Sstevel@tonic-gate 
12220Sstevel@tonic-gate 		cmds->inside_table_block = 0;
12230Sstevel@tonic-gate 
12240Sstevel@tonic-gate 		break;
12250Sstevel@tonic-gate 	case TOK_ROW:
12260Sstevel@tonic-gate 		/* Check for ROW outside of TABLE, ROW inside ROW */
12270Sstevel@tonic-gate 		if ((cmds->inside_table_block == 0) ||
12280Sstevel@tonic-gate 		    (cmds->inside_row_block != 0))
12290Sstevel@tonic-gate 			return (EC_SYNTAX_ERR);
12300Sstevel@tonic-gate 		cmds->inside_row_block = 1;
12310Sstevel@tonic-gate 		break;
12320Sstevel@tonic-gate 	case TOK_ENDROW:
12330Sstevel@tonic-gate 		/* Check for ENDROW outside of TABLE, ENDROW before ROW */
12340Sstevel@tonic-gate 		if ((cmds->inside_table_block == 0) ||
12350Sstevel@tonic-gate 		    (cmds->inside_row_block == 0))
12360Sstevel@tonic-gate 			return (EC_SYNTAX_ERR);
12370Sstevel@tonic-gate 		else
12380Sstevel@tonic-gate 			err = EC_SYNTAX_OK;
12390Sstevel@tonic-gate 
12400Sstevel@tonic-gate 		cmds->inside_row_block = 0;
12410Sstevel@tonic-gate 
12420Sstevel@tonic-gate 		/* error if row is empty */
12430Sstevel@tonic-gate 		if (cmds->commands[cmds->current_row].rowcmd_nproph <= 0)
12440Sstevel@tonic-gate 			return (EC_ROW_EMPTY);
12450Sstevel@tonic-gate 		break;
12460Sstevel@tonic-gate 	case TOK_VERBOSE:
12470Sstevel@tonic-gate 		err = parse_verbose(cmds, rec, command);
12480Sstevel@tonic-gate 		if (err != EC_SYNTAX_OK)
12490Sstevel@tonic-gate 			return (err);
12500Sstevel@tonic-gate 		break;
12510Sstevel@tonic-gate 	default:	/* unsupported command */
12520Sstevel@tonic-gate 		return (EC_SYNTAX_ERR);
12530Sstevel@tonic-gate 	}
12540Sstevel@tonic-gate 
12550Sstevel@tonic-gate 	command->type = id;
12560Sstevel@tonic-gate 	return (EC_SYNTAX_OK);
12570Sstevel@tonic-gate }
12580Sstevel@tonic-gate 
12590Sstevel@tonic-gate /*
12600Sstevel@tonic-gate  * Check the syntax and save the tokens in the commands buffer
12610Sstevel@tonic-gate  */
12620Sstevel@tonic-gate static int
check_line_syntax(cmdbuf_t * cmds,char * buf)12630Sstevel@tonic-gate check_line_syntax(cmdbuf_t *cmds, char *buf)
12640Sstevel@tonic-gate {
12650Sstevel@tonic-gate 	int		err;
12660Sstevel@tonic-gate 	command_t	command;
12670Sstevel@tonic-gate 
12680Sstevel@tonic-gate 	(void) memset(&command, 0, sizeof (command_t));
12690Sstevel@tonic-gate 	err = parse_and_tokenize_line(cmds, buf, &command);
12700Sstevel@tonic-gate 	if (err != EC_SYNTAX_OK)
12710Sstevel@tonic-gate 		return (err);
12720Sstevel@tonic-gate 
12730Sstevel@tonic-gate 	/*
12740Sstevel@tonic-gate 	 * don't add and count version command in the command buffer
12750Sstevel@tonic-gate 	 */
12760Sstevel@tonic-gate 	if (command.type == TOK_VERSION)
12770Sstevel@tonic-gate 		return (EC_SYNTAX_OK);
12780Sstevel@tonic-gate 
12790Sstevel@tonic-gate 	/*
12800Sstevel@tonic-gate 	 * check if the commands buffer has been filled
12810Sstevel@tonic-gate 	 * If it is full, reallocate the buffer.
12820Sstevel@tonic-gate 	 */
12830Sstevel@tonic-gate 	if (cmds->count == cmds->allocated) {
12840Sstevel@tonic-gate 		cmds->commands = realloc(cmds->commands,
12850Sstevel@tonic-gate 		    sizeof (command_t) * (cmds->allocated + PER_ALLOC_COUNT));
12860Sstevel@tonic-gate 		if (cmds->commands == NULL)
12870Sstevel@tonic-gate 			return (EC_FAILURE);
12880Sstevel@tonic-gate 		cmds->allocated += PER_ALLOC_COUNT;
12890Sstevel@tonic-gate 	}
12900Sstevel@tonic-gate 
12910Sstevel@tonic-gate 	cmds->commands[cmds->count] = command;	/* copy */
12920Sstevel@tonic-gate 
12930Sstevel@tonic-gate 	/*
12940Sstevel@tonic-gate 	 * make a note of the row/endrow command, to keep track of # of props
12950Sstevel@tonic-gate 	 */
12960Sstevel@tonic-gate 	if (command.type == TOK_ROW)
12970Sstevel@tonic-gate 		cmds->current_row = cmds->count;
12980Sstevel@tonic-gate 
12990Sstevel@tonic-gate 	if (command.type == TOK_ENDROW)
13000Sstevel@tonic-gate 		cmds->current_row = 0;
13010Sstevel@tonic-gate 
13020Sstevel@tonic-gate 	cmds->count++;
13030Sstevel@tonic-gate 
13040Sstevel@tonic-gate 	return (EC_SYNTAX_OK);
13050Sstevel@tonic-gate }
13060Sstevel@tonic-gate 
13070Sstevel@tonic-gate /*
13080Sstevel@tonic-gate  * get the line control information
13090Sstevel@tonic-gate  * return 1 if it's the line control information, else return 0
13100Sstevel@tonic-gate  */
13110Sstevel@tonic-gate static int
get_line_control_info(char * buf,uint32_t * linenum,char * filename)13120Sstevel@tonic-gate get_line_control_info(char *buf, uint32_t *linenum, char *filename)
13130Sstevel@tonic-gate {
13140Sstevel@tonic-gate 	char		*ptr;
13150Sstevel@tonic-gate 	char		*last;
13160Sstevel@tonic-gate 	uint32_t	num;
13170Sstevel@tonic-gate 	char		*fname;
13180Sstevel@tonic-gate 	char		*endptr;
13190Sstevel@tonic-gate 
13200Sstevel@tonic-gate 	/* skip # and get next string */
13210Sstevel@tonic-gate 	ptr = strtok_r(buf + 1, WHITESPACE, &last);
13220Sstevel@tonic-gate 	if (ptr == NULL) {
13230Sstevel@tonic-gate 		return (0);
13240Sstevel@tonic-gate 	}
13250Sstevel@tonic-gate 
13260Sstevel@tonic-gate 	num = strtoul(ptr, &endptr, 0);
13270Sstevel@tonic-gate 
13280Sstevel@tonic-gate 	/*
13290Sstevel@tonic-gate 	 * It's not the line control information
13300Sstevel@tonic-gate 	 */
13310Sstevel@tonic-gate 	if (endptr != (ptr + strlen(ptr))) {
13320Sstevel@tonic-gate 		return (0);
13330Sstevel@tonic-gate 	}
13340Sstevel@tonic-gate 
13350Sstevel@tonic-gate 	/*
13360Sstevel@tonic-gate 	 * get the filename
13370Sstevel@tonic-gate 	 */
13380Sstevel@tonic-gate 
13390Sstevel@tonic-gate 	/* get the beginning double quote */
13400Sstevel@tonic-gate 	last = strchr(last, '"');
13410Sstevel@tonic-gate 	if (last == NULL)
13420Sstevel@tonic-gate 		return (0);
13430Sstevel@tonic-gate 
13440Sstevel@tonic-gate 	last++;
13450Sstevel@tonic-gate 
13460Sstevel@tonic-gate 	/* get the ending double quote */
13470Sstevel@tonic-gate 	fname = strtok_r(last, DOUBLE_QUOTE, &last);
13480Sstevel@tonic-gate 	if (fname == NULL)
13490Sstevel@tonic-gate 		return (0);
13500Sstevel@tonic-gate 
13510Sstevel@tonic-gate 	*linenum = num;
13520Sstevel@tonic-gate 	(void) strlcpy(filename, fname, PATH_MAX);
13530Sstevel@tonic-gate 	return (1);
13540Sstevel@tonic-gate }
13550Sstevel@tonic-gate 
13560Sstevel@tonic-gate /*
13570Sstevel@tonic-gate  * check the syntax of the configuration file
13580Sstevel@tonic-gate  */
13590Sstevel@tonic-gate static int
check_conffile_syntax(cmdbuf_t * cmds,FILE * fp)13600Sstevel@tonic-gate check_conffile_syntax(cmdbuf_t *cmds, FILE *fp)
13610Sstevel@tonic-gate {
13620Sstevel@tonic-gate 	char		lbuf[RECORD_SIZE_MAX];
13630Sstevel@tonic-gate 	char		buf[RECORD_SIZE_MAX];
13640Sstevel@tonic-gate 	uint32_t	linenum;
13650Sstevel@tonic-gate 	char		cppfile[PATH_MAX] = "";
13660Sstevel@tonic-gate 	int		err = EC_SYNTAX_OK;
13670Sstevel@tonic-gate 
13680Sstevel@tonic-gate 	linenum = 0;
13690Sstevel@tonic-gate 	while (fgets(buf, sizeof (buf), fp) != NULL) {
13700Sstevel@tonic-gate 		/*
13710Sstevel@tonic-gate 		 * get cpp line control information, if any
13720Sstevel@tonic-gate 		 */
13730Sstevel@tonic-gate 		if (buf[0] == '#') {
13740Sstevel@tonic-gate 			if (!get_line_control_info(buf, &linenum, cppfile))
13750Sstevel@tonic-gate 				++linenum;
13760Sstevel@tonic-gate 			continue;
13770Sstevel@tonic-gate 		}
13780Sstevel@tonic-gate 
13790Sstevel@tonic-gate 		++linenum;
13800Sstevel@tonic-gate 		/*
13810Sstevel@tonic-gate 		 * skip line whose first char is a newline char
13820Sstevel@tonic-gate 		 */
13830Sstevel@tonic-gate 		if (buf[0] == '\n') {
13840Sstevel@tonic-gate 			continue;
13850Sstevel@tonic-gate 		}
13860Sstevel@tonic-gate 
13870Sstevel@tonic-gate 		if (err == EC_SYNTAX_OK)
13880Sstevel@tonic-gate 			(void) strlcpy(lbuf, buf, RECORD_SIZE_MAX);
13890Sstevel@tonic-gate 		else if (strlcat(lbuf, buf, RECORD_SIZE_MAX) >=
13900Sstevel@tonic-gate 		    RECORD_SIZE_MAX) { 	/* buffer overflow */
13910Sstevel@tonic-gate 			err = EC_FAILURE;
13920Sstevel@tonic-gate 			break;
13930Sstevel@tonic-gate 		}
13940Sstevel@tonic-gate 
13950Sstevel@tonic-gate 		err = check_line_syntax(cmds, lbuf);
13960Sstevel@tonic-gate 		if ((err != EC_INSUFFICIENT_TOKEN) && (err != EC_SYNTAX_OK))
13970Sstevel@tonic-gate 			break;
13980Sstevel@tonic-gate 	}
13990Sstevel@tonic-gate 
14000Sstevel@tonic-gate 	if (err != EC_SYNTAX_OK) {
14010Sstevel@tonic-gate 		if (cmds->verbose) {
14020Sstevel@tonic-gate 			verbose_log(LOG_ERR, err_msg[err],
14030Sstevel@tonic-gate 			    cmds->fname, cppfile, linenum);
14040Sstevel@tonic-gate 		}
14050Sstevel@tonic-gate 		return (err);
14060Sstevel@tonic-gate 	}
14070Sstevel@tonic-gate 
14080Sstevel@tonic-gate 	/*
14090Sstevel@tonic-gate 	 * check if the version has been set
14100Sstevel@tonic-gate 	 */
14110Sstevel@tonic-gate 	if (cmds->version_no > (float)SUPPORTED_VERSION_NUM) {
14120Sstevel@tonic-gate 		if (cmds->verbose) {
14130Sstevel@tonic-gate 			verbose_log(LOG_ERR, err_msg[EC_UNSUPPORTED],
14140Sstevel@tonic-gate 			    cmds->fname, cppfile, linenum);
14150Sstevel@tonic-gate 		}
14160Sstevel@tonic-gate 		return (EC_UNSUPPORTED);
14170Sstevel@tonic-gate 	}
14180Sstevel@tonic-gate 
14190Sstevel@tonic-gate 	/*
14200Sstevel@tonic-gate 	 * check if node and endnode command mismatch
14210Sstevel@tonic-gate 	 */
14220Sstevel@tonic-gate 	if (cmds->inside_node_block != 0) {
14230Sstevel@tonic-gate 		if (cmds->verbose) {
14240Sstevel@tonic-gate 			verbose_log(LOG_ERR, err_msg[EC_NODE_MISMATCH],
14250Sstevel@tonic-gate 			    cmds->fname, cppfile, linenum);
14260Sstevel@tonic-gate 		}
14270Sstevel@tonic-gate 		return (EC_NODE_MISMATCH);
14280Sstevel@tonic-gate 	}
14290Sstevel@tonic-gate 
14300Sstevel@tonic-gate 	/*
14310Sstevel@tonic-gate 	 * check if row and endrow command mismatch
14320Sstevel@tonic-gate 	 */
14330Sstevel@tonic-gate 	if (cmds->inside_row_block != 0) {
14340Sstevel@tonic-gate 		if (cmds->verbose) {
14350Sstevel@tonic-gate 			verbose_log(LOG_ERR, err_msg[EC_ROW_MISMATCH],
14360Sstevel@tonic-gate 			    cmds->fname, cppfile, linenum);
14370Sstevel@tonic-gate 		}
14380Sstevel@tonic-gate 		return (EC_ROW_MISMATCH);
14390Sstevel@tonic-gate 	}
14400Sstevel@tonic-gate 
14410Sstevel@tonic-gate 	/*
14420Sstevel@tonic-gate 	 * check if table and endtable command mismatch
14430Sstevel@tonic-gate 	 */
14440Sstevel@tonic-gate 	if (cmds->inside_table_block != 0) {
14450Sstevel@tonic-gate 		if (cmds->verbose) {
14460Sstevel@tonic-gate 			verbose_log(LOG_ERR, err_msg[EC_TABLE_MISMATCH],
14470Sstevel@tonic-gate 			    cmds->fname, cppfile, linenum);
14480Sstevel@tonic-gate 		}
14490Sstevel@tonic-gate 		return (EC_TABLE_MISMATCH);
14500Sstevel@tonic-gate 	}
14510Sstevel@tonic-gate 
14520Sstevel@tonic-gate 	return (EC_SYNTAX_OK);
14530Sstevel@tonic-gate }
14540Sstevel@tonic-gate 
14550Sstevel@tonic-gate /*
14560Sstevel@tonic-gate  * If classpath/namepath given is not found in the picl tree,
14570Sstevel@tonic-gate  * skip the whole blocks until next valid classpath or namepath
14580Sstevel@tonic-gate  */
14590Sstevel@tonic-gate static void
skip_to_next_valid_path(cmdbuf_t * cmds,int starting_index,picl_nodehdl_t * parent,int * last_processed_index)14600Sstevel@tonic-gate skip_to_next_valid_path(cmdbuf_t *cmds, int starting_index,
14610Sstevel@tonic-gate 	picl_nodehdl_t *parent, int *last_processed_index)
14620Sstevel@tonic-gate {
14630Sstevel@tonic-gate 	int	err;
14640Sstevel@tonic-gate 	int	index;
14650Sstevel@tonic-gate 
14660Sstevel@tonic-gate 	for (index = starting_index; index < cmds->count; ++index) {
14670Sstevel@tonic-gate 		switch (cmds->commands[index].type) {
1468*9407SMichael.Bergknoff@Sun.COM 		case TOK_CLASSPATH:
1469*9407SMichael.Bergknoff@Sun.COM 		case TOK_NAMEPATH:
14700Sstevel@tonic-gate 			err = process_path(&cmds->commands[index], parent);
14710Sstevel@tonic-gate 			if (err == PICL_SUCCESS) {
14720Sstevel@tonic-gate 				*last_processed_index = index;
14730Sstevel@tonic-gate 				return;
14740Sstevel@tonic-gate 			}
1475*9407SMichael.Bergknoff@Sun.COM 		default:
14760Sstevel@tonic-gate 			/* skipped this line */
14770Sstevel@tonic-gate 			break;
14780Sstevel@tonic-gate 		}
14790Sstevel@tonic-gate 	}
14800Sstevel@tonic-gate 
14810Sstevel@tonic-gate 	/* reach last command  */
14820Sstevel@tonic-gate 	*last_processed_index = cmds->count - 1;
14830Sstevel@tonic-gate }
14840Sstevel@tonic-gate 
14850Sstevel@tonic-gate /*
14860Sstevel@tonic-gate  * Process the command buffer and return last command index and the new head of
14870Sstevel@tonic-gate  * the handle list
14880Sstevel@tonic-gate  */
14890Sstevel@tonic-gate static int
process_commands(cmdbuf_t * cmds,int starting_index,picl_nodehdl_t parent,int * last_processed_index)14900Sstevel@tonic-gate process_commands(cmdbuf_t *cmds, int starting_index, picl_nodehdl_t parent,
14910Sstevel@tonic-gate     int *last_processed_index)
14920Sstevel@tonic-gate {
14930Sstevel@tonic-gate 	int		err;
14940Sstevel@tonic-gate 	int		index;
14950Sstevel@tonic-gate 	picl_nodehdl_t	rooth;
14960Sstevel@tonic-gate 	picl_nodehdl_t	nodeh;
14970Sstevel@tonic-gate 	command_t	*commands = cmds->commands;
14980Sstevel@tonic-gate 
14990Sstevel@tonic-gate 	for (index = starting_index; index < cmds->count; ++index) {
15000Sstevel@tonic-gate 		switch (commands[index].type) {
15010Sstevel@tonic-gate 		case TOK_CLASSPATH:
15020Sstevel@tonic-gate 		case TOK_NAMEPATH:
15030Sstevel@tonic-gate 			err = process_path(&commands[index], &rooth);
15040Sstevel@tonic-gate 			if (err != PICL_SUCCESS) {
15050Sstevel@tonic-gate 				index++;
15060Sstevel@tonic-gate 				(void) skip_to_next_valid_path(cmds, index,
15070Sstevel@tonic-gate 				    &rooth, &index);
15080Sstevel@tonic-gate 			}
15090Sstevel@tonic-gate 			parent = rooth;
15100Sstevel@tonic-gate 			continue;
15110Sstevel@tonic-gate 		case TOK_NODE:
15120Sstevel@tonic-gate 			err = process_node(&commands[index], parent, &nodeh);
15130Sstevel@tonic-gate 			if (err == PICL_SUCCESS) {
15140Sstevel@tonic-gate 				index++;
15150Sstevel@tonic-gate 				err = process_commands(cmds, index, nodeh,
15160Sstevel@tonic-gate 				    &index);
15170Sstevel@tonic-gate 			}
15180Sstevel@tonic-gate 			break;
15190Sstevel@tonic-gate 		case TOK_ENDNODE:
15200Sstevel@tonic-gate 			*last_processed_index = index;
15210Sstevel@tonic-gate 			return (PICL_SUCCESS);
15220Sstevel@tonic-gate 		case TOK_PROP:
15230Sstevel@tonic-gate 			err =  process_prop(cmds, &commands[index], parent);
15240Sstevel@tonic-gate 			break;
15250Sstevel@tonic-gate 		case TOK_REFPROP:
15260Sstevel@tonic-gate 			err = process_refprop(cmds, &commands[index], parent);
15270Sstevel@tonic-gate 			/* no reference node */
15280Sstevel@tonic-gate 			if (err == PICL_NOTNODE) {
15290Sstevel@tonic-gate 				err = PICL_SUCCESS;	/* discard prop */
15300Sstevel@tonic-gate 				/* discard row by setting nproph = 0 */
15310Sstevel@tonic-gate 				if (cmds->inside_row_block)
15320Sstevel@tonic-gate 					cmds->commands[cmds->current_row]
15330Sstevel@tonic-gate 					    .rowcmd_nproph = 0;
15340Sstevel@tonic-gate 			}
15350Sstevel@tonic-gate 			break;
15360Sstevel@tonic-gate 		case TOK_REFNODE:
15370Sstevel@tonic-gate 			err =  process_refnode(&commands[index], parent);
15380Sstevel@tonic-gate 			break;
15390Sstevel@tonic-gate 		case TOK_TABLE:
15400Sstevel@tonic-gate 			cmds->inside_table_block = 1;
15410Sstevel@tonic-gate 			err = process_table(&commands[index], parent);
15420Sstevel@tonic-gate 			cmds->current_tbl = index;
15430Sstevel@tonic-gate 			break;
15440Sstevel@tonic-gate 		case TOK_ENDTABLE:
15450Sstevel@tonic-gate 			cmds->inside_table_block = 0;
15460Sstevel@tonic-gate 			cmds->current_tbl = 0;
15470Sstevel@tonic-gate 			break;
15480Sstevel@tonic-gate 		case TOK_ROW:
15490Sstevel@tonic-gate 			cmds->inside_row_block = 1;
15500Sstevel@tonic-gate 			err = process_row(&commands[index]);
15510Sstevel@tonic-gate 			cmds->current_row = index;
15520Sstevel@tonic-gate 			break;
15530Sstevel@tonic-gate 		case TOK_ENDROW:
15540Sstevel@tonic-gate 			err = process_endrow(cmds);
15550Sstevel@tonic-gate 			cmds->inside_row_block = 0;
15560Sstevel@tonic-gate 			cmds->current_row = 0;
15570Sstevel@tonic-gate 			break;
15580Sstevel@tonic-gate 		case TOK_VERBOSE:
15590Sstevel@tonic-gate 			err = process_verbose(cmds, &commands[index]);
15600Sstevel@tonic-gate 			break;
15610Sstevel@tonic-gate 		default:	/* won't reach here */
15620Sstevel@tonic-gate 			err =  PICL_FAILURE;
15630Sstevel@tonic-gate 			break;
15640Sstevel@tonic-gate 		}
15650Sstevel@tonic-gate 
15660Sstevel@tonic-gate 		if ((err != PICL_SUCCESS) && (err != PICL_PROPEXISTS)) {
15670Sstevel@tonic-gate 			*last_processed_index = index;
15680Sstevel@tonic-gate 			return (err);
15690Sstevel@tonic-gate 		}
15700Sstevel@tonic-gate 	}
15710Sstevel@tonic-gate 
15720Sstevel@tonic-gate 	/* reach last command */
15730Sstevel@tonic-gate 	*last_processed_index = cmds->count - 1;
15740Sstevel@tonic-gate 	return (PICL_SUCCESS);
15750Sstevel@tonic-gate }
15760Sstevel@tonic-gate 
15770Sstevel@tonic-gate /*
15780Sstevel@tonic-gate  * clean up the commands buffer
15790Sstevel@tonic-gate  */
15800Sstevel@tonic-gate static void
clean_up(cmdbuf_t * cmds)15810Sstevel@tonic-gate clean_up(cmdbuf_t *cmds)
15820Sstevel@tonic-gate {
15830Sstevel@tonic-gate 	int	cmd_index;
15840Sstevel@tonic-gate 
15850Sstevel@tonic-gate 	for (cmd_index = 0; cmd_index < cmds->count; cmd_index++) {
15860Sstevel@tonic-gate 		switch (cmds->commands[cmd_index].type) {
15870Sstevel@tonic-gate 		case TOK_CLASSPATH:
15880Sstevel@tonic-gate 		case TOK_NAMEPATH:
15890Sstevel@tonic-gate 			free_path(&cmds->commands[cmd_index]);
15900Sstevel@tonic-gate 			break;
15910Sstevel@tonic-gate 		case TOK_NODE:
15920Sstevel@tonic-gate 			free_node(&cmds->commands[cmd_index]);
15930Sstevel@tonic-gate 			break;
15940Sstevel@tonic-gate 		case TOK_PROP:
15950Sstevel@tonic-gate 			free_prop(&cmds->commands[cmd_index]);
15960Sstevel@tonic-gate 			break;
15970Sstevel@tonic-gate 		case TOK_REFPROP:
15980Sstevel@tonic-gate 			free_refprop(&cmds->commands[cmd_index]);
15990Sstevel@tonic-gate 			break;
16000Sstevel@tonic-gate 		case TOK_REFNODE:
16010Sstevel@tonic-gate 			free_refnode(&cmds->commands[cmd_index]);
16020Sstevel@tonic-gate 			break;
16030Sstevel@tonic-gate 		case TOK_TABLE:
16040Sstevel@tonic-gate 			free_table(&cmds->commands[cmd_index]);
16050Sstevel@tonic-gate 			break;
16060Sstevel@tonic-gate 		case TOK_ENDTABLE:
16070Sstevel@tonic-gate 		case TOK_ROW:
16080Sstevel@tonic-gate 		case TOK_ENDROW:
16090Sstevel@tonic-gate 		case TOK_ENDNODE:
16100Sstevel@tonic-gate 		case TOK_VERBOSE:
16110Sstevel@tonic-gate 		default:
16120Sstevel@tonic-gate 			break;
16130Sstevel@tonic-gate 		}
16140Sstevel@tonic-gate 	}
16150Sstevel@tonic-gate 	if (cmds->commands)
16160Sstevel@tonic-gate 		free(cmds->commands);
16170Sstevel@tonic-gate }
16180Sstevel@tonic-gate 
16190Sstevel@tonic-gate /*
16200Sstevel@tonic-gate  * Parse the configuration file and create nodes/properties under nh
16210Sstevel@tonic-gate  *
16220Sstevel@tonic-gate  * It checks the syntax first.  If there is any syntax error,
16230Sstevel@tonic-gate  * it returns 1 and won't continue processing the file to add nodes or props.
16240Sstevel@tonic-gate  *
16250Sstevel@tonic-gate  * If any error happens during command processing, all nodes
16260Sstevel@tonic-gate  * and properties just created will be deleted, i.e. undo
16270Sstevel@tonic-gate  * commands which have been processed.  It returns 1.
16280Sstevel@tonic-gate  *
16290Sstevel@tonic-gate  * If success, return 0.
16300Sstevel@tonic-gate  */
16310Sstevel@tonic-gate int
picld_pluginutil_parse_config_file(picl_nodehdl_t nh,const char * filename)16320Sstevel@tonic-gate picld_pluginutil_parse_config_file(picl_nodehdl_t nh, const char *filename)
16330Sstevel@tonic-gate {
16340Sstevel@tonic-gate 	FILE		*ifp;
16350Sstevel@tonic-gate 	int		last_processed_index;
16360Sstevel@tonic-gate 	int		err;
16370Sstevel@tonic-gate 	cmdbuf_t	*cmds;
16380Sstevel@tonic-gate 
16390Sstevel@tonic-gate 	/* set correct locale for use inside pluginutil */
16400Sstevel@tonic-gate 	setlocale(LC_ALL, "C");
16410Sstevel@tonic-gate 
16420Sstevel@tonic-gate 	/*
16430Sstevel@tonic-gate 	 * Initialize the command buffer
16440Sstevel@tonic-gate 	 */
16450Sstevel@tonic-gate 
16460Sstevel@tonic-gate 	cmds = malloc(sizeof (*cmds));
16470Sstevel@tonic-gate 	if (cmds == NULL) {
16480Sstevel@tonic-gate 		setlocale(LC_ALL, "");
16490Sstevel@tonic-gate 		return (1);
16500Sstevel@tonic-gate 	}
16510Sstevel@tonic-gate 
16520Sstevel@tonic-gate 	memset(cmds, 0, sizeof (cmdbuf_t));
16530Sstevel@tonic-gate 
16540Sstevel@tonic-gate 	cmds->fname = filename;
16550Sstevel@tonic-gate 
16560Sstevel@tonic-gate 	ifp = fopen(filename, "r");
16570Sstevel@tonic-gate 	if (ifp == NULL) {
16580Sstevel@tonic-gate 		setlocale(LC_ALL, "");
1659*9407SMichael.Bergknoff@Sun.COM 		free(cmds);
16600Sstevel@tonic-gate 		return (1);
16610Sstevel@tonic-gate 	}
16620Sstevel@tonic-gate 
16630Sstevel@tonic-gate 	/*
16640Sstevel@tonic-gate 	 * check the syntax of the configuration file
16650Sstevel@tonic-gate 	 */
16660Sstevel@tonic-gate 	err = check_conffile_syntax(cmds, ifp);
16670Sstevel@tonic-gate 
16680Sstevel@tonic-gate 	(void) fclose(ifp);
16690Sstevel@tonic-gate 
16700Sstevel@tonic-gate 	if (err != EC_SYNTAX_OK) {
16710Sstevel@tonic-gate 		clean_up(cmds);
16720Sstevel@tonic-gate 		free(cmds);
16730Sstevel@tonic-gate 		setlocale(LC_ALL, "");
16740Sstevel@tonic-gate 		return (1);
16750Sstevel@tonic-gate 	}
16760Sstevel@tonic-gate 
16770Sstevel@tonic-gate 	/*
16780Sstevel@tonic-gate 	 * Process the commands
16790Sstevel@tonic-gate 	 */
16800Sstevel@tonic-gate 	err = process_commands(cmds, STARTING_INDEX, nh, &last_processed_index);
16810Sstevel@tonic-gate 
16820Sstevel@tonic-gate 	/*
16830Sstevel@tonic-gate 	 * If any PICL error, remove the newly created node/prop
16840Sstevel@tonic-gate 	 * handles from the PICL tree.
16850Sstevel@tonic-gate 	 */
16860Sstevel@tonic-gate 	if (err != PICL_SUCCESS) {
16870Sstevel@tonic-gate 		undo_commands(cmds, last_processed_index);
16880Sstevel@tonic-gate 		if (cmds->verbose)
16890Sstevel@tonic-gate 			verbose_log(LOG_ERR, err_msg[EC_PICL_ERR], filename,
16900Sstevel@tonic-gate 			    err);
16910Sstevel@tonic-gate 	}
16920Sstevel@tonic-gate 
16930Sstevel@tonic-gate 	clean_up(cmds);
16940Sstevel@tonic-gate 	free(cmds);
16950Sstevel@tonic-gate 
16960Sstevel@tonic-gate 	/* reset the locale */
16970Sstevel@tonic-gate 	setlocale(LC_ALL, "");
16980Sstevel@tonic-gate 
16990Sstevel@tonic-gate 	return ((err == PICL_SUCCESS) ? 0 : 1);
17000Sstevel@tonic-gate }
1701