17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
55705cefcSMichael Bergknoff  * Common Development and Distribution License (the "License").
65705cefcSMichael Bergknoff  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
225705cefcSMichael Bergknoff  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * This module implements the routine to parse the configuration file.
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <stdio.h>
327c478bd9Sstevel@tonic-gate #include <fcntl.h>
337c478bd9Sstevel@tonic-gate #include <unistd.h>
347c478bd9Sstevel@tonic-gate #include <stdlib.h>
357c478bd9Sstevel@tonic-gate #include <stdarg.h>
367c478bd9Sstevel@tonic-gate #include <string.h>
377c478bd9Sstevel@tonic-gate #include <ctype.h>
387c478bd9Sstevel@tonic-gate #include <alloca.h>
397c478bd9Sstevel@tonic-gate #include <limits.h>
407c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
417c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
427c478bd9Sstevel@tonic-gate #include <sys/types.h>
437c478bd9Sstevel@tonic-gate #include <libintl.h>
447c478bd9Sstevel@tonic-gate #include <syslog.h>
457c478bd9Sstevel@tonic-gate #include <locale.h>
467c478bd9Sstevel@tonic-gate #include <picl.h>
477c478bd9Sstevel@tonic-gate #include <picltree.h>
487c478bd9Sstevel@tonic-gate #include "picld_pluginutil.h"
497c478bd9Sstevel@tonic-gate #include "picld_pluginutil_impl.h"
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate /* error codes returned from syntax checking */
527c478bd9Sstevel@tonic-gate #define	EC_SYNTAX_OK		0
537c478bd9Sstevel@tonic-gate #define	EC_INSUFFICIENT_TOKEN	1
547c478bd9Sstevel@tonic-gate #define	EC_SYNTAX_ERR		2
557c478bd9Sstevel@tonic-gate #define	EC_UNSUPPORTED		3
567c478bd9Sstevel@tonic-gate #define	EC_PATH_ERR		4
577c478bd9Sstevel@tonic-gate #define	EC_NODE_MISMATCH	5
587c478bd9Sstevel@tonic-gate #define	EC_FAILURE		6
597c478bd9Sstevel@tonic-gate #define	EC_PICL_ERR		7
607c478bd9Sstevel@tonic-gate #define	EC_TABLE_MISMATCH	8
617c478bd9Sstevel@tonic-gate #define	EC_ROW_MISMATCH		9
627c478bd9Sstevel@tonic-gate #define	EC_ROW_EMPTY		10
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate /*
657c478bd9Sstevel@tonic-gate  * Error message texts
667c478bd9Sstevel@tonic-gate  */
677c478bd9Sstevel@tonic-gate static	char	*err_msg[] = {
687c478bd9Sstevel@tonic-gate 	"%s: Syntax OK",					/* 0 */
697c478bd9Sstevel@tonic-gate 	"%s::%s[line %d]: Insufficient token\n",		/* 1 */
707c478bd9Sstevel@tonic-gate 	"%s::%s[line %d]: Syntax error\n",			/* 2 */
717c478bd9Sstevel@tonic-gate 	"%s::%s[line %d]: Unsupported or missing version\n",	/* 3 */
727c478bd9Sstevel@tonic-gate 	"%s::%s[line %d]: Illegal use of nodepath or namepath\n",	/* 4 */
737c478bd9Sstevel@tonic-gate 	"%s::%s[line %d]: Node and endnode mismatch\n",		/* 5 */
747c478bd9Sstevel@tonic-gate 	"%s::%s[line %d]: General system failure\n",		/* 6 */
757c478bd9Sstevel@tonic-gate 	"%s: PICL error code %d\n",				/* 7 */
767c478bd9Sstevel@tonic-gate 	"%s::%s[line %d]: Table and endtable mismatch\n",	/* 8 */
777c478bd9Sstevel@tonic-gate 	"%s::%s[line %d]: Row and endrow mismatch\n",		/* 9 */
787c478bd9Sstevel@tonic-gate 	"%s::%s[line %d]: Row has no entries \n"		/* 10 */
797c478bd9Sstevel@tonic-gate };
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate /* token per directive */
827c478bd9Sstevel@tonic-gate #define	TOK_CLASSPATH	0
837c478bd9Sstevel@tonic-gate #define	TOK_NAMEPATH	1
847c478bd9Sstevel@tonic-gate #define	TOK_NODE	2
857c478bd9Sstevel@tonic-gate #define	TOK_ENDNODE	3
867c478bd9Sstevel@tonic-gate #define	TOK_PROP	4
877c478bd9Sstevel@tonic-gate #define	TOK_REFPROP	5
887c478bd9Sstevel@tonic-gate #define	TOK_VERSION	6
897c478bd9Sstevel@tonic-gate #define	TOK_REFNODE	7
907c478bd9Sstevel@tonic-gate #define	TOK_VERBOSE	8
917c478bd9Sstevel@tonic-gate #define	TOK_TABLE	9
927c478bd9Sstevel@tonic-gate #define	TOK_ENDTABLE	10
937c478bd9Sstevel@tonic-gate #define	TOK_ROW		11
947c478bd9Sstevel@tonic-gate #define	TOK_ENDROW	12
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate static const char	*tokens[] = {
977c478bd9Sstevel@tonic-gate 	"_class",	/* _CLASS:<classpath> */
987c478bd9Sstevel@tonic-gate 	"name",		/* NAME:<namepath> */
997c478bd9Sstevel@tonic-gate 	"node",		/* NODE <name> <class> */
1007c478bd9Sstevel@tonic-gate 	"endnode",	/* ENDNODE */
1017c478bd9Sstevel@tonic-gate 	"prop",		/* PROP <name> <type> <access_mode> <size> <value> */
1027c478bd9Sstevel@tonic-gate 	"refprop",	/* REFPROP <prop> <destnode> */
1037c478bd9Sstevel@tonic-gate 	"version",	/* VERSION <version_number> */
1047c478bd9Sstevel@tonic-gate 	"refnode",	/* REFNODE <node> <class> WITH <destnode> */
1057c478bd9Sstevel@tonic-gate 	"verbose",	/* VERBOSE <level> */
1067c478bd9Sstevel@tonic-gate 	"table",	/* TABLE   <table_prop_name> */
1077c478bd9Sstevel@tonic-gate 	"endtable",	/* ENDTABLE */
1087c478bd9Sstevel@tonic-gate 	"row",		/* ROW  */
1097c478bd9Sstevel@tonic-gate 	"endrow"	/* ENDROW */
1107c478bd9Sstevel@tonic-gate };
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate #define	BUF_SIZE_MAX	1024
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate /*
1157c478bd9Sstevel@tonic-gate  * print error message
1167c478bd9Sstevel@tonic-gate  */
1177c478bd9Sstevel@tonic-gate /*VARARGS2*/
1187c478bd9Sstevel@tonic-gate static void
verbose_log(int pri,const char * fmt,...)1197c478bd9Sstevel@tonic-gate verbose_log(int pri, const char *fmt, ...)
1207c478bd9Sstevel@tonic-gate {
1217c478bd9Sstevel@tonic-gate 	va_list	ap;
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
1247c478bd9Sstevel@tonic-gate 	vsyslog(pri, fmt, ap);
1257c478bd9Sstevel@tonic-gate 	va_end(ap);
1267c478bd9Sstevel@tonic-gate }
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate /*
1297c478bd9Sstevel@tonic-gate  * Undo the commands which have created valid node/prop handle
1307c478bd9Sstevel@tonic-gate  * The undo order is from last command to the first command.
1317c478bd9Sstevel@tonic-gate  */
1327c478bd9Sstevel@tonic-gate static void
undo_commands(cmdbuf_t * cmds,int last_cmd_index)1337c478bd9Sstevel@tonic-gate undo_commands(cmdbuf_t *cmds, int last_cmd_index)
1347c478bd9Sstevel@tonic-gate {
135*e9610e3eSToomas Soome 	int		i;
1367c478bd9Sstevel@tonic-gate 	command_t	*com = cmds->commands;
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	for (i = last_cmd_index; i >= 0; i--) {
1397c478bd9Sstevel@tonic-gate 		switch (com[i].type) {
1407c478bd9Sstevel@tonic-gate 		case TOK_NODE:
141*e9610e3eSToomas Soome 			if (com[i].nodecmd_nodeh == 0)
1427c478bd9Sstevel@tonic-gate 				break;
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 			(void) ptree_delete_node(com[i].nodecmd_nodeh);
1457c478bd9Sstevel@tonic-gate 			(void) ptree_destroy_node(com[i].nodecmd_nodeh);
1467c478bd9Sstevel@tonic-gate 			break;
1477c478bd9Sstevel@tonic-gate 		case TOK_REFNODE:
148*e9610e3eSToomas Soome 			if (com[i].refnodecmd_nodeh == 0)
1497c478bd9Sstevel@tonic-gate 				break;
1507c478bd9Sstevel@tonic-gate 			(void) ptree_delete_node(com[i].refnodecmd_nodeh);
1517c478bd9Sstevel@tonic-gate 			(void) ptree_destroy_node(com[i].refnodecmd_nodeh);
1527c478bd9Sstevel@tonic-gate 			break;
1537c478bd9Sstevel@tonic-gate 		case TOK_PROP:
154*e9610e3eSToomas Soome 			if (com[i].propcmd_proph == 0)
1557c478bd9Sstevel@tonic-gate 				break;
1567c478bd9Sstevel@tonic-gate 			(void) ptree_delete_prop(com[i].propcmd_proph);
1577c478bd9Sstevel@tonic-gate 			(void) ptree_destroy_prop(com[i].propcmd_proph);
1587c478bd9Sstevel@tonic-gate 			break;
1597c478bd9Sstevel@tonic-gate 		case TOK_REFPROP:
160*e9610e3eSToomas Soome 			if (com[i].refpropcmd_proph == 0)
1617c478bd9Sstevel@tonic-gate 				break;
1627c478bd9Sstevel@tonic-gate 			(void) ptree_delete_prop(com[i].refpropcmd_proph);
1637c478bd9Sstevel@tonic-gate 			(void) ptree_destroy_prop(com[i].refpropcmd_proph);
1647c478bd9Sstevel@tonic-gate 			break;
1657c478bd9Sstevel@tonic-gate 		case TOK_TABLE:
166*e9610e3eSToomas Soome 			if ((com[i].tablecmd_tblh == 0) ||
1677c478bd9Sstevel@tonic-gate 			    (com[i].tablecmd_newtbl == 0))
1687c478bd9Sstevel@tonic-gate 				break;
1697c478bd9Sstevel@tonic-gate 			(void) ptree_delete_prop(com[i].tablecmd_tblh);
1707c478bd9Sstevel@tonic-gate 			(void) ptree_destroy_prop(com[i].tablecmd_tblh);
1717c478bd9Sstevel@tonic-gate 			break;
1727c478bd9Sstevel@tonic-gate 		case TOK_ENDTABLE:
1737c478bd9Sstevel@tonic-gate 			/*FALLTHROUGH*/
1747c478bd9Sstevel@tonic-gate 		case TOK_ROW:
1757c478bd9Sstevel@tonic-gate 			/*FALLTHROUGH*/
1767c478bd9Sstevel@tonic-gate 		case TOK_ENDROW:
1777c478bd9Sstevel@tonic-gate 			/*FALLTHROUGH*/
1787c478bd9Sstevel@tonic-gate 		case TOK_NAMEPATH:
1797c478bd9Sstevel@tonic-gate 			/*FALLTHROUGH*/
1807c478bd9Sstevel@tonic-gate 		case TOK_CLASSPATH:
1817c478bd9Sstevel@tonic-gate 			/*FALLTHROUGH*/
1827c478bd9Sstevel@tonic-gate 		case TOK_ENDNODE:
1837c478bd9Sstevel@tonic-gate 			/*FALLTHROUGH*/
1847c478bd9Sstevel@tonic-gate 		case TOK_VERBOSE:
1857c478bd9Sstevel@tonic-gate 			/*FALLTHROUGH*/
1867c478bd9Sstevel@tonic-gate 		default:
1877c478bd9Sstevel@tonic-gate 			break;
1887c478bd9Sstevel@tonic-gate 		}
1897c478bd9Sstevel@tonic-gate 	}
1907c478bd9Sstevel@tonic-gate }
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate /*
1937c478bd9Sstevel@tonic-gate  * Get the token index from the tokens table
1947c478bd9Sstevel@tonic-gate  */
1957c478bd9Sstevel@tonic-gate static int
get_token_id(char * t)1967c478bd9Sstevel@tonic-gate get_token_id(char *t)
1977c478bd9Sstevel@tonic-gate {
1987c478bd9Sstevel@tonic-gate 	int	i;
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	for (i = 0; i < sizeof (tokens)/ sizeof (char *); ++i)
2017c478bd9Sstevel@tonic-gate 		if (strcasecmp(tokens[i], t) == 0)
2027c478bd9Sstevel@tonic-gate 			return (i);
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	return (-1);
2057c478bd9Sstevel@tonic-gate }
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate /*
2087c478bd9Sstevel@tonic-gate  * Check the version syntax and set the version_no
2097c478bd9Sstevel@tonic-gate  *
2107c478bd9Sstevel@tonic-gate  * VERSION <version_num> --   specify the configuration version
2117c478bd9Sstevel@tonic-gate  */
2127c478bd9Sstevel@tonic-gate static int
parse_version(cmdbuf_t * cmds,char * line)2137c478bd9Sstevel@tonic-gate parse_version(cmdbuf_t *cmds, char *line)
2147c478bd9Sstevel@tonic-gate {
2157c478bd9Sstevel@tonic-gate 	char	*tok;
2167c478bd9Sstevel@tonic-gate 	char	*vertok;
2177c478bd9Sstevel@tonic-gate 	char	*last;
2187c478bd9Sstevel@tonic-gate 	char	*endptr;
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	/* get the VERSION directive */
2217c478bd9Sstevel@tonic-gate 	tok = strtok_r(line, WHITESPACE, &last);
2227c478bd9Sstevel@tonic-gate 	if (tok == NULL)
2237c478bd9Sstevel@tonic-gate 		return (EC_INSUFFICIENT_TOKEN);
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 	/* get the version number */
2267c478bd9Sstevel@tonic-gate 	vertok = strtok_r(last, WHITESPACE, &last);
2277c478bd9Sstevel@tonic-gate 	if (vertok == NULL)
2287c478bd9Sstevel@tonic-gate 		return (EC_INSUFFICIENT_TOKEN);
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 	cmds->version_no = (float)strtod(vertok, &endptr);
2317c478bd9Sstevel@tonic-gate 	if (endptr != (vertok + strlen(vertok)))
2327c478bd9Sstevel@tonic-gate 		return (EC_UNSUPPORTED);
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	if (cmds->version_no > (float)SUPPORTED_VERSION_NUM)
2357c478bd9Sstevel@tonic-gate 		return (EC_UNSUPPORTED);
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	/* check if more tokens */
2387c478bd9Sstevel@tonic-gate 	tok = strtok_r(last, WHITESPACE, &last);
2397c478bd9Sstevel@tonic-gate 	if (tok != NULL)
2407c478bd9Sstevel@tonic-gate 		return (EC_SYNTAX_ERR);
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 	return (EC_SYNTAX_OK);
2437c478bd9Sstevel@tonic-gate }
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate /*
2467c478bd9Sstevel@tonic-gate  * free path_cmd_t
2477c478bd9Sstevel@tonic-gate  */
2487c478bd9Sstevel@tonic-gate static void
free_path(command_t * command)2497c478bd9Sstevel@tonic-gate free_path(command_t *command)
2507c478bd9Sstevel@tonic-gate {
2517c478bd9Sstevel@tonic-gate 	free(command->pathcmd_name);
2527c478bd9Sstevel@tonic-gate }
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate /*
2557c478bd9Sstevel@tonic-gate  * Check the path syntax
2567c478bd9Sstevel@tonic-gate  * NAMEPATH:<namepath> --     gives the anchor node
2577c478bd9Sstevel@tonic-gate  * or
2587c478bd9Sstevel@tonic-gate  * CLASSPATH:<classpath> --   gives the anchor node
2597c478bd9Sstevel@tonic-gate  */
2607c478bd9Sstevel@tonic-gate static int
parse_path(char * line,command_t * command)2617c478bd9Sstevel@tonic-gate parse_path(char *line, command_t *command)
2627c478bd9Sstevel@tonic-gate {
2637c478bd9Sstevel@tonic-gate 	char	*tok;
2647c478bd9Sstevel@tonic-gate 	char	*pathtok;
2657c478bd9Sstevel@tonic-gate 	char	*last;
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 	pathtok = strtok_r(line, WHITESPACE, &last);
2687c478bd9Sstevel@tonic-gate 	if (pathtok == NULL)
2697c478bd9Sstevel@tonic-gate 		return (EC_INSUFFICIENT_TOKEN);
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 	/* check if more tokens */
2727c478bd9Sstevel@tonic-gate 	tok = strtok_r(last, WHITESPACE, &last);
2737c478bd9Sstevel@tonic-gate 	if (tok != NULL)
2747c478bd9Sstevel@tonic-gate 		return (EC_SYNTAX_ERR);
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 	command->pathcmd_name = strdup(pathtok);
2777c478bd9Sstevel@tonic-gate 	if (command->pathcmd_name == NULL)
2787c478bd9Sstevel@tonic-gate 		return (EC_FAILURE);
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 	return (EC_SYNTAX_OK);
2817c478bd9Sstevel@tonic-gate }
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate /*
2847c478bd9Sstevel@tonic-gate  * Process the path command and return PICL node handle
2857c478bd9Sstevel@tonic-gate  */
2867c478bd9Sstevel@tonic-gate static int
process_path(command_t * command,picl_nodehdl_t * nodeh)2877c478bd9Sstevel@tonic-gate process_path(command_t *command, picl_nodehdl_t *nodeh)
2887c478bd9Sstevel@tonic-gate {
2897c478bd9Sstevel@tonic-gate 	int	err;
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 	err = ptree_get_node_by_path(command->pathcmd_name, nodeh);
2927c478bd9Sstevel@tonic-gate 	return (err);
2937c478bd9Sstevel@tonic-gate }
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate /*
2967c478bd9Sstevel@tonic-gate  * free node_cmd_t
2977c478bd9Sstevel@tonic-gate  */
2987c478bd9Sstevel@tonic-gate static void
free_node(command_t * command)2997c478bd9Sstevel@tonic-gate free_node(command_t *command)
3007c478bd9Sstevel@tonic-gate {
3017c478bd9Sstevel@tonic-gate 	free(command->nodecmd_nodename);
3027c478bd9Sstevel@tonic-gate 	free(command->nodecmd_classname);
3037c478bd9Sstevel@tonic-gate }
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate /*
3067c478bd9Sstevel@tonic-gate  * Check the NODE syntax
3077c478bd9Sstevel@tonic-gate  * NODE <name> <class>
3087c478bd9Sstevel@tonic-gate  */
3097c478bd9Sstevel@tonic-gate static int
parse_node(char * line,command_t * command)3107c478bd9Sstevel@tonic-gate parse_node(char *line, command_t *command)
3117c478bd9Sstevel@tonic-gate {
3127c478bd9Sstevel@tonic-gate 	char	*tok;
3137c478bd9Sstevel@tonic-gate 	char	*nametok;
3147c478bd9Sstevel@tonic-gate 	char	*classtok;
3157c478bd9Sstevel@tonic-gate 	char	*last;
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 	/* get the NODE directive */
3187c478bd9Sstevel@tonic-gate 	tok = strtok_r(line, WHITESPACE, &last);
3197c478bd9Sstevel@tonic-gate 	if (tok == NULL)
3207c478bd9Sstevel@tonic-gate 		return (EC_INSUFFICIENT_TOKEN);
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	/* get name */
3237c478bd9Sstevel@tonic-gate 	nametok = strtok_r(last, WHITESPACE, &last);
3247c478bd9Sstevel@tonic-gate 	if (nametok == NULL)
3257c478bd9Sstevel@tonic-gate 		return (EC_INSUFFICIENT_TOKEN);
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 	classtok = strtok_r(last, WHITESPACE, &last);
3287c478bd9Sstevel@tonic-gate 	if (classtok == NULL)
3297c478bd9Sstevel@tonic-gate 		return (EC_INSUFFICIENT_TOKEN);
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 	/* check if more tokens */
3327c478bd9Sstevel@tonic-gate 	tok = strtok_r(last, WHITESPACE, &last);
3337c478bd9Sstevel@tonic-gate 	if (tok != NULL)
3347c478bd9Sstevel@tonic-gate 		return (EC_SYNTAX_ERR);
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 	command->nodecmd_nodename = strdup(nametok);
3377c478bd9Sstevel@tonic-gate 	command->nodecmd_classname = strdup(classtok);
338*e9610e3eSToomas Soome 	command->nodecmd_nodeh = 0;
3397c478bd9Sstevel@tonic-gate 	if ((command->nodecmd_nodename == NULL) ||
3407c478bd9Sstevel@tonic-gate 	    (command->nodecmd_classname == NULL))
3417c478bd9Sstevel@tonic-gate 		return (EC_FAILURE);
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 	return (EC_SYNTAX_OK);
3447c478bd9Sstevel@tonic-gate }
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate /*
3477c478bd9Sstevel@tonic-gate  * Process the NODE command and return PICL node handle
3487c478bd9Sstevel@tonic-gate  */
3497c478bd9Sstevel@tonic-gate static int
process_node(command_t * command,picl_nodehdl_t parh,picl_nodehdl_t * nodeh)3507c478bd9Sstevel@tonic-gate process_node(command_t *command, picl_nodehdl_t parh, picl_nodehdl_t *nodeh)
3517c478bd9Sstevel@tonic-gate {
3527c478bd9Sstevel@tonic-gate 	int	err;
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 	err = ptree_create_and_add_node(parh, command->nodecmd_nodename,
3557c478bd9Sstevel@tonic-gate 	    command->nodecmd_classname, nodeh);
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 	if (err == PICL_SUCCESS)
3587c478bd9Sstevel@tonic-gate 		command->nodecmd_nodeh = *nodeh;
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 	return (err);
3617c478bd9Sstevel@tonic-gate }
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate /*
3647c478bd9Sstevel@tonic-gate  * get the PICL property type
3657c478bd9Sstevel@tonic-gate  */
3667c478bd9Sstevel@tonic-gate static int
getpicltype(char * type)3677c478bd9Sstevel@tonic-gate getpicltype(char *type)
3687c478bd9Sstevel@tonic-gate {
3697c478bd9Sstevel@tonic-gate 	if (strcasecmp(type, KEYWORD_INT_TYPE) == 0)
3707c478bd9Sstevel@tonic-gate 		return (PICL_PTYPE_INT);
3717c478bd9Sstevel@tonic-gate 	else if (strcasecmp(type, KEYWORD_UINT_TYPE) == 0)
3727c478bd9Sstevel@tonic-gate 		return (PICL_PTYPE_UNSIGNED_INT);
3737c478bd9Sstevel@tonic-gate 	else if (strcasecmp(type, KEYWORD_FLOAT_TYPE) == 0)
3747c478bd9Sstevel@tonic-gate 		return (PICL_PTYPE_FLOAT);
3757c478bd9Sstevel@tonic-gate 	else if (strcasecmp(type, KEYWORD_STRING_TYPE) == 0)
3767c478bd9Sstevel@tonic-gate 		return (PICL_PTYPE_CHARSTRING);
3777c478bd9Sstevel@tonic-gate 	else if (strcasecmp(type, KEYWORD_VOID_TYPE) == 0)
3787c478bd9Sstevel@tonic-gate 		return (PICL_PTYPE_VOID);
3797c478bd9Sstevel@tonic-gate 	else
3807c478bd9Sstevel@tonic-gate 		return (-1);
3817c478bd9Sstevel@tonic-gate }
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate /*
3847c478bd9Sstevel@tonic-gate  * get the PICL accessmode mode
3857c478bd9Sstevel@tonic-gate  */
3867c478bd9Sstevel@tonic-gate static int
getpiclmode(char * mode)3877c478bd9Sstevel@tonic-gate getpiclmode(char *mode)
3887c478bd9Sstevel@tonic-gate {
3897c478bd9Sstevel@tonic-gate 	if (strcasecmp(mode, KEYWORD_READ_MODE) == 0)
3907c478bd9Sstevel@tonic-gate 		return (PICL_READ);
3917c478bd9Sstevel@tonic-gate 	else if (strcasecmp(mode, KEYWORD_WRITE_MODE) == 0)
3927c478bd9Sstevel@tonic-gate 		return (PICL_WRITE);
3937c478bd9Sstevel@tonic-gate 	else if (strcasecmp(mode, KEYWORD_READWRITE_MODE) == 0)
3947c478bd9Sstevel@tonic-gate 		return (PICL_READ|PICL_WRITE);
3957c478bd9Sstevel@tonic-gate 	else
3967c478bd9Sstevel@tonic-gate 		return (-1);
3977c478bd9Sstevel@tonic-gate }
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate /*
4007c478bd9Sstevel@tonic-gate  * check if the size and value are valid given by the prop type
4017c478bd9Sstevel@tonic-gate  */
4027c478bd9Sstevel@tonic-gate static int
validate_size_and_cvt_val(void * outbuf,size_t size,int type,char * val)4037c478bd9Sstevel@tonic-gate validate_size_and_cvt_val(void *outbuf, size_t size, int type, char *val)
4047c478bd9Sstevel@tonic-gate {
405*e9610e3eSToomas Soome 	int64_t		llval;
4067c478bd9Sstevel@tonic-gate 	int32_t		intval;
4077c478bd9Sstevel@tonic-gate 	int16_t		sval;
4087c478bd9Sstevel@tonic-gate 	int8_t		cval;
409*e9610e3eSToomas Soome 	uint64_t	ullval;
4107c478bd9Sstevel@tonic-gate 	uint32_t	uintval;
4117c478bd9Sstevel@tonic-gate 	uint16_t	usval;
4127c478bd9Sstevel@tonic-gate 	uint8_t		ucval;
4137c478bd9Sstevel@tonic-gate 	float		fval;
4147c478bd9Sstevel@tonic-gate 	double		dval;
4157c478bd9Sstevel@tonic-gate 	char		*endptr;
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 	switch (type) {
4187c478bd9Sstevel@tonic-gate 	case PICL_PTYPE_CHARSTRING:
4197c478bd9Sstevel@tonic-gate 		break;
4207c478bd9Sstevel@tonic-gate 	case PICL_PTYPE_INT:
4217c478bd9Sstevel@tonic-gate 		switch (size) {
4227c478bd9Sstevel@tonic-gate 		case sizeof (int64_t):
4237c478bd9Sstevel@tonic-gate 			llval = strtoll(val, &endptr, 0);
4247c478bd9Sstevel@tonic-gate 			if (endptr != (val + strlen(val)))
4257c478bd9Sstevel@tonic-gate 				return (EC_SYNTAX_ERR);
4267c478bd9Sstevel@tonic-gate 			(void) memcpy(outbuf, &llval, size);
4277c478bd9Sstevel@tonic-gate 			break;
4287c478bd9Sstevel@tonic-gate 		case sizeof (int32_t):
4297c478bd9Sstevel@tonic-gate 			intval = strtol(val, &endptr, 0);
4307c478bd9Sstevel@tonic-gate 			if (endptr != (val + strlen(val)))
4317c478bd9Sstevel@tonic-gate 				return (EC_SYNTAX_ERR);
4327c478bd9Sstevel@tonic-gate 			(void) memcpy(outbuf, &intval, size);
4337c478bd9Sstevel@tonic-gate 			break;
4347c478bd9Sstevel@tonic-gate 		case sizeof (int16_t):
4357c478bd9Sstevel@tonic-gate 			sval = (int16_t)strtol(val, &endptr, 0);
4367c478bd9Sstevel@tonic-gate 			if (endptr != (val + strlen(val)))
4377c478bd9Sstevel@tonic-gate 				return (EC_SYNTAX_ERR);
4387c478bd9Sstevel@tonic-gate 			(void) memcpy(outbuf, &sval, size);
4397c478bd9Sstevel@tonic-gate 			break;
4407c478bd9Sstevel@tonic-gate 		case sizeof (int8_t):
4417c478bd9Sstevel@tonic-gate 			cval = (int8_t)strtol(val, &endptr, 0);
4427c478bd9Sstevel@tonic-gate 			if (endptr != (val + strlen(val)))
4437c478bd9Sstevel@tonic-gate 				return (EC_SYNTAX_ERR);
4447c478bd9Sstevel@tonic-gate 			(void) memcpy(outbuf, &cval, size);
4457c478bd9Sstevel@tonic-gate 			break;
4467c478bd9Sstevel@tonic-gate 		default:	/* invalid size */
4477c478bd9Sstevel@tonic-gate 			return (EC_SYNTAX_ERR);
4487c478bd9Sstevel@tonic-gate 		}
4497c478bd9Sstevel@tonic-gate 		break;
4507c478bd9Sstevel@tonic-gate 	case PICL_PTYPE_UNSIGNED_INT:
4517c478bd9Sstevel@tonic-gate 		switch (size) {
4527c478bd9Sstevel@tonic-gate 		case sizeof (uint64_t):
4537c478bd9Sstevel@tonic-gate 			ullval = strtoull(val, &endptr, 0);
4547c478bd9Sstevel@tonic-gate 			if (endptr != (val + strlen(val)))
4557c478bd9Sstevel@tonic-gate 				return (EC_SYNTAX_ERR);
4567c478bd9Sstevel@tonic-gate 			(void) memcpy(outbuf, &ullval, size);
4577c478bd9Sstevel@tonic-gate 			break;
4587c478bd9Sstevel@tonic-gate 		case sizeof (uint32_t):
4597c478bd9Sstevel@tonic-gate 			uintval = strtoul(val, &endptr, 0);
4607c478bd9Sstevel@tonic-gate 			if (endptr != (val + strlen(val)))
4617c478bd9Sstevel@tonic-gate 				return (EC_SYNTAX_ERR);
4627c478bd9Sstevel@tonic-gate 			(void) memcpy(outbuf, &uintval, size);
4637c478bd9Sstevel@tonic-gate 			break;
4647c478bd9Sstevel@tonic-gate 		case sizeof (uint16_t):
4657c478bd9Sstevel@tonic-gate 			usval = (uint16_t)strtoul(val, &endptr, 0);
4667c478bd9Sstevel@tonic-gate 			if (endptr != (val + strlen(val)))
4677c478bd9Sstevel@tonic-gate 				return (EC_SYNTAX_ERR);
4687c478bd9Sstevel@tonic-gate 			(void) memcpy(outbuf, &usval, size);
4697c478bd9Sstevel@tonic-gate 			break;
4707c478bd9Sstevel@tonic-gate 		case sizeof (uint8_t):
4717c478bd9Sstevel@tonic-gate 			ucval = (uint8_t)strtoul(val, &endptr, 0);
4727c478bd9Sstevel@tonic-gate 			if (endptr != (val + strlen(val)))
4737c478bd9Sstevel@tonic-gate 				return (EC_SYNTAX_ERR);
4747c478bd9Sstevel@tonic-gate 			(void) memcpy(outbuf, &ucval, size);
4757c478bd9Sstevel@tonic-gate 			break;
4767c478bd9Sstevel@tonic-gate 		default:	/* invalid size */
4777c478bd9Sstevel@tonic-gate 			return (EC_SYNTAX_ERR);
4787c478bd9Sstevel@tonic-gate 		}
4797c478bd9Sstevel@tonic-gate 		break;
4807c478bd9Sstevel@tonic-gate 	case PICL_PTYPE_FLOAT:
4817c478bd9Sstevel@tonic-gate 		switch (size) {
4827c478bd9Sstevel@tonic-gate 		case sizeof (double):
4837c478bd9Sstevel@tonic-gate 			dval = strtod(val, &endptr);
4847c478bd9Sstevel@tonic-gate 			if (endptr != (val + strlen(val)))
4857c478bd9Sstevel@tonic-gate 				return (EC_SYNTAX_ERR);
4867c478bd9Sstevel@tonic-gate 			(void) memcpy(outbuf, &dval, size);
4877c478bd9Sstevel@tonic-gate 			break;
4887c478bd9Sstevel@tonic-gate 		case sizeof (float):
4897c478bd9Sstevel@tonic-gate 			fval = (float)strtod(val, &endptr);
4907c478bd9Sstevel@tonic-gate 			if (endptr != (val + strlen(val)))
4917c478bd9Sstevel@tonic-gate 				return (EC_SYNTAX_ERR);
4927c478bd9Sstevel@tonic-gate 			(void) memcpy(outbuf, &fval, size);
4937c478bd9Sstevel@tonic-gate 			break;
4947c478bd9Sstevel@tonic-gate 		default:	/* invalid size */
4957c478bd9Sstevel@tonic-gate 			return (EC_SYNTAX_ERR);
4967c478bd9Sstevel@tonic-gate 		}
4977c478bd9Sstevel@tonic-gate 		break;
4987c478bd9Sstevel@tonic-gate 	default:	/* not supported type */
4997c478bd9Sstevel@tonic-gate 		return (EC_SYNTAX_ERR);
5007c478bd9Sstevel@tonic-gate 	}
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate 	return (EC_SYNTAX_OK);
5037c478bd9Sstevel@tonic-gate }
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate /*
5067c478bd9Sstevel@tonic-gate  * free prop_cmd_t
5077c478bd9Sstevel@tonic-gate  */
5087c478bd9Sstevel@tonic-gate static void
free_prop(command_t * command)5097c478bd9Sstevel@tonic-gate free_prop(command_t *command)
5107c478bd9Sstevel@tonic-gate {
5117c478bd9Sstevel@tonic-gate 	free(command->propcmd_pname);
5127c478bd9Sstevel@tonic-gate 	if (command->propcmd_type != PICL_PTYPE_VOID)
5137c478bd9Sstevel@tonic-gate 		free(command->propcmd_valbuf);
5147c478bd9Sstevel@tonic-gate }
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate /*
5177c478bd9Sstevel@tonic-gate  * return the string token in two double quotes
5187c478bd9Sstevel@tonic-gate  * The current version won't support multiple-line string
5197c478bd9Sstevel@tonic-gate  */
5207c478bd9Sstevel@tonic-gate static int
get_string_token(char * line,char ** valtok)5217c478bd9Sstevel@tonic-gate get_string_token(char *line, char **valtok)
5227c478bd9Sstevel@tonic-gate {
5237c478bd9Sstevel@tonic-gate 	char	*optr;	/* ptr to the open quote */
5247c478bd9Sstevel@tonic-gate 	char	*cptr;	/* ptr to the close quote */
5257c478bd9Sstevel@tonic-gate 	char	*ptr;
5267c478bd9Sstevel@tonic-gate 	char	*tmpbuf;
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate 	if (line == NULL)
5297c478bd9Sstevel@tonic-gate 		return (EC_INSUFFICIENT_TOKEN);
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate 	/* skipping leading white spaces */
5327c478bd9Sstevel@tonic-gate 	optr = line;
5337c478bd9Sstevel@tonic-gate 	while ((*optr == ' ') || (*optr == '\t') || (*optr == '\n'))
5347c478bd9Sstevel@tonic-gate 		optr++;
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate 	/* reach end of string */
5377c478bd9Sstevel@tonic-gate 	if (*optr == '\0')
5387c478bd9Sstevel@tonic-gate 		return (EC_INSUFFICIENT_TOKEN);
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate 	/* it's not an open double quote */
5417c478bd9Sstevel@tonic-gate 	if (*optr != '"')
5427c478bd9Sstevel@tonic-gate 		return (EC_SYNTAX_ERR);
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 	/* skipping ending white spaces */
5457c478bd9Sstevel@tonic-gate 	cptr = line + strlen(line) - 1;
5467c478bd9Sstevel@tonic-gate 	while ((*cptr == ' ') || (*cptr == '\t') || (*cptr == '\n'))
5477c478bd9Sstevel@tonic-gate 		cptr--;
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate 	/* it's not an close double quote */
5507c478bd9Sstevel@tonic-gate 	if (*cptr != '"')
5517c478bd9Sstevel@tonic-gate 		return (EC_SYNTAX_ERR);
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 	/* close double quote is missing */
5547c478bd9Sstevel@tonic-gate 	if (cptr == optr)
5557c478bd9Sstevel@tonic-gate 		return (EC_SYNTAX_ERR);
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate 	/* replace close qoute by null to make a string */
5587c478bd9Sstevel@tonic-gate 	*cptr = '\0';
5597c478bd9Sstevel@tonic-gate 	/* move the begin pointer to the first char of string */
5607c478bd9Sstevel@tonic-gate 	optr++;
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate 	tmpbuf = malloc(strlen(optr) + 1);
5637c478bd9Sstevel@tonic-gate 	if (tmpbuf == NULL)
5647c478bd9Sstevel@tonic-gate 		return (EC_FAILURE);
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 	for (ptr = tmpbuf; *optr != '\0'; ptr++, optr++) {
5677c478bd9Sstevel@tonic-gate 		/* if escape character, go to next character */
5687c478bd9Sstevel@tonic-gate 		if (*optr == '\\') {
5697c478bd9Sstevel@tonic-gate 			optr++;
5707c478bd9Sstevel@tonic-gate 			if (*optr == '\0') {	/* for exampe, "xxx\" */
5717c478bd9Sstevel@tonic-gate 				free(tmpbuf);
5727c478bd9Sstevel@tonic-gate 				return (EC_SYNTAX_ERR);
5737c478bd9Sstevel@tonic-gate 			}
5747c478bd9Sstevel@tonic-gate 		}
5757c478bd9Sstevel@tonic-gate 		*ptr = *optr;
5767c478bd9Sstevel@tonic-gate 	}
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate 	*ptr = '\0';
5797c478bd9Sstevel@tonic-gate 	*valtok = tmpbuf;
5807c478bd9Sstevel@tonic-gate 	return (EC_SYNTAX_OK);
5817c478bd9Sstevel@tonic-gate }
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate /*
5847c478bd9Sstevel@tonic-gate  * Check the PROP syntax
5857c478bd9Sstevel@tonic-gate  * PROP <name> <type> <access_mode> [<size> <value>]
5867c478bd9Sstevel@tonic-gate  * supported prop types: void, int, uint, float, string
5877c478bd9Sstevel@tonic-gate  * supported prop access_modes: r, w, rw
5887c478bd9Sstevel@tonic-gate  * For void prop, <size> and <value> are not needed
5897c478bd9Sstevel@tonic-gate  * For string prop, <size> will be set the actual string size if <size>
5907c478bd9Sstevel@tonic-gate  * is 0
5917c478bd9Sstevel@tonic-gate  */
5927c478bd9Sstevel@tonic-gate static int
parse_prop(char * line,command_t * command)5937c478bd9Sstevel@tonic-gate parse_prop(char *line, command_t *command)
5947c478bd9Sstevel@tonic-gate {
5957c478bd9Sstevel@tonic-gate 	char	*tok;
5967c478bd9Sstevel@tonic-gate 	char	*pnametok;
5977c478bd9Sstevel@tonic-gate 	int	typetok;
5987c478bd9Sstevel@tonic-gate 	size_t	sizetok;
5997c478bd9Sstevel@tonic-gate 	int	modetok;
6007c478bd9Sstevel@tonic-gate 	char	*valtok;
6017c478bd9Sstevel@tonic-gate 	char	*last;
6027c478bd9Sstevel@tonic-gate 	char	*endptr;
6037c478bd9Sstevel@tonic-gate 	int	err;
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate 	/* get the PROP directive */
6067c478bd9Sstevel@tonic-gate 	tok = strtok_r(line, WHITESPACE, &last);
6077c478bd9Sstevel@tonic-gate 	if (tok == NULL)
6087c478bd9Sstevel@tonic-gate 		return (EC_INSUFFICIENT_TOKEN);
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate 	/* get the property name */
6117c478bd9Sstevel@tonic-gate 	pnametok = strtok_r(last, WHITESPACE, &last);
6127c478bd9Sstevel@tonic-gate 	if (pnametok == NULL)
6137c478bd9Sstevel@tonic-gate 		return (EC_INSUFFICIENT_TOKEN);
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate 	/* get the type */
6167c478bd9Sstevel@tonic-gate 	tok = strtok_r(last, WHITESPACE, &last);
6177c478bd9Sstevel@tonic-gate 	if (tok == NULL)
6187c478bd9Sstevel@tonic-gate 		return (EC_INSUFFICIENT_TOKEN);
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate 	if ((typetok = getpicltype(tok)) < 0)
6217c478bd9Sstevel@tonic-gate 		return (EC_SYNTAX_ERR);
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate 	/* get mode */
6247c478bd9Sstevel@tonic-gate 	tok = strtok_r(last, WHITESPACE, &last);
6257c478bd9Sstevel@tonic-gate 	if (tok == NULL)
6267c478bd9Sstevel@tonic-gate 		return (EC_INSUFFICIENT_TOKEN);
6277c478bd9Sstevel@tonic-gate 
6287c478bd9Sstevel@tonic-gate 	if ((modetok = getpiclmode(tok)) < 0)
6297c478bd9Sstevel@tonic-gate 		return (EC_SYNTAX_ERR);
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate 	if (typetok == PICL_PTYPE_VOID) {
6327c478bd9Sstevel@tonic-gate 		/* ignore the rest of arguments */
6337c478bd9Sstevel@tonic-gate 		command->propcmd_valbuf = NULL;
6347c478bd9Sstevel@tonic-gate 		command->propcmd_pname = strdup(pnametok);
6357c478bd9Sstevel@tonic-gate 		if (command->propcmd_pname == NULL)
6367c478bd9Sstevel@tonic-gate 			return (EC_FAILURE);
6377c478bd9Sstevel@tonic-gate 		command->propcmd_type = typetok;
6387c478bd9Sstevel@tonic-gate 		command->propcmd_accessmode = modetok;
6397c478bd9Sstevel@tonic-gate 		command->propcmd_size = 0;
640*e9610e3eSToomas Soome 		command->propcmd_proph = 0;
6417c478bd9Sstevel@tonic-gate 		return (EC_SYNTAX_OK);
6427c478bd9Sstevel@tonic-gate 	}
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate 	/* get size */
6457c478bd9Sstevel@tonic-gate 	tok = strtok_r(last, WHITESPACE, &last);
6467c478bd9Sstevel@tonic-gate 	if (tok == NULL)
6477c478bd9Sstevel@tonic-gate 		return (EC_INSUFFICIENT_TOKEN);
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate 	sizetok = (size_t)strtol(tok, &endptr, 0);
6507c478bd9Sstevel@tonic-gate 	if (endptr != (tok + strlen(tok)))
6517c478bd9Sstevel@tonic-gate 		return (EC_SYNTAX_ERR);
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate 	/* get val */
6547c478bd9Sstevel@tonic-gate 	if (typetok == PICL_PTYPE_CHARSTRING) {
6557c478bd9Sstevel@tonic-gate 		err = get_string_token(last, &valtok);
6567c478bd9Sstevel@tonic-gate 		if (err != EC_SYNTAX_OK)
6577c478bd9Sstevel@tonic-gate 			return (err);
6587c478bd9Sstevel@tonic-gate 		if (sizetok == 0)
6597c478bd9Sstevel@tonic-gate 			sizetok = strlen(valtok) + 1;
6607c478bd9Sstevel@tonic-gate 		command->propcmd_valbuf = valtok;
6617c478bd9Sstevel@tonic-gate 	} else {
6627c478bd9Sstevel@tonic-gate 		valtok = strtok_r(last, WHITESPACE, &last);
6637c478bd9Sstevel@tonic-gate 		if (valtok == NULL)
6647c478bd9Sstevel@tonic-gate 			return (EC_INSUFFICIENT_TOKEN);
6657c478bd9Sstevel@tonic-gate 		/* check if more tokens */
6667c478bd9Sstevel@tonic-gate 		tok = strtok_r(last, WHITESPACE, &last);
6677c478bd9Sstevel@tonic-gate 		if (tok != NULL)
6687c478bd9Sstevel@tonic-gate 			return (EC_SYNTAX_ERR);
6697c478bd9Sstevel@tonic-gate 		command->propcmd_valbuf = malloc(sizetok);
6707c478bd9Sstevel@tonic-gate 		if (command->propcmd_valbuf == NULL)
6717c478bd9Sstevel@tonic-gate 			return (EC_FAILURE);
6727c478bd9Sstevel@tonic-gate 		err = validate_size_and_cvt_val(command->propcmd_valbuf,
6737c478bd9Sstevel@tonic-gate 		    sizetok, typetok, valtok);
6747c478bd9Sstevel@tonic-gate 		if (err != EC_SYNTAX_OK) {
6757c478bd9Sstevel@tonic-gate 			free(command->propcmd_valbuf);
6767c478bd9Sstevel@tonic-gate 			return (err);
6777c478bd9Sstevel@tonic-gate 		}
6787c478bd9Sstevel@tonic-gate 	}
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate 	command->propcmd_pname = strdup(pnametok);
6817c478bd9Sstevel@tonic-gate 	if (command->propcmd_pname == NULL)
6827c478bd9Sstevel@tonic-gate 		return (EC_FAILURE);
6837c478bd9Sstevel@tonic-gate 	command->propcmd_type = typetok;
6847c478bd9Sstevel@tonic-gate 	command->propcmd_accessmode = modetok;
6857c478bd9Sstevel@tonic-gate 	command->propcmd_size = sizetok;
686*e9610e3eSToomas Soome 	command->propcmd_proph = 0;
6877c478bd9Sstevel@tonic-gate 	return (EC_SYNTAX_OK);
6887c478bd9Sstevel@tonic-gate }
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate /*
6917c478bd9Sstevel@tonic-gate  * Add a property to the row, the row gets added to the node at endrow
6927c478bd9Sstevel@tonic-gate  */
6937c478bd9Sstevel@tonic-gate static int
add_proph_to_row(command_t * command,picl_prophdl_t proph)6947c478bd9Sstevel@tonic-gate add_proph_to_row(command_t *command, picl_prophdl_t proph)
6957c478bd9Sstevel@tonic-gate {
6967c478bd9Sstevel@tonic-gate 	if (command->rowcmd_index >= command->rowcmd_nproph)
6977c478bd9Sstevel@tonic-gate 		return (PICL_FAILURE);
6987c478bd9Sstevel@tonic-gate 	command->rowcmd_prophs[command->rowcmd_index] = proph;
6997c478bd9Sstevel@tonic-gate 	command->rowcmd_index++;
7007c478bd9Sstevel@tonic-gate 	return (PICL_SUCCESS);
7017c478bd9Sstevel@tonic-gate }
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate /*
7047c478bd9Sstevel@tonic-gate  * Process the PROP command and add the specified property under the given
7057c478bd9Sstevel@tonic-gate  * node handle
7067c478bd9Sstevel@tonic-gate  */
7077c478bd9Sstevel@tonic-gate static int
process_prop(cmdbuf_t * cmds,command_t * command,picl_nodehdl_t nodeh)7087c478bd9Sstevel@tonic-gate process_prop(cmdbuf_t *cmds, command_t *command, picl_nodehdl_t nodeh)
7097c478bd9Sstevel@tonic-gate {
7107c478bd9Sstevel@tonic-gate 	ptree_propinfo_t	propinfo;
7117c478bd9Sstevel@tonic-gate 	picl_prophdl_t		proph;
7127c478bd9Sstevel@tonic-gate 	int			err;
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate 	/* prop in discarded row */
7157c478bd9Sstevel@tonic-gate 	if (cmds->inside_row_block &&
7167c478bd9Sstevel@tonic-gate 	    cmds->commands[cmds->current_row].rowcmd_nproph == 0)
7177c478bd9Sstevel@tonic-gate 		return (PICL_SUCCESS);
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate 	err = ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
7207c478bd9Sstevel@tonic-gate 	    command->propcmd_type, command->propcmd_accessmode,
7217c478bd9Sstevel@tonic-gate 	    command->propcmd_size, command->propcmd_pname, NULL,
7227c478bd9Sstevel@tonic-gate 	    NULL);
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
7257c478bd9Sstevel@tonic-gate 		return (err);
7267c478bd9Sstevel@tonic-gate 
7277c478bd9Sstevel@tonic-gate 	err = ptree_create_prop(&propinfo, command->propcmd_valbuf, &proph);
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
7307c478bd9Sstevel@tonic-gate 		return (err);
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate 	command->propcmd_proph = proph;
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate 	if (cmds->inside_row_block) {
7357c478bd9Sstevel@tonic-gate 		err = add_proph_to_row(&cmds->commands[cmds->current_row],
7367c478bd9Sstevel@tonic-gate 		    proph);
7377c478bd9Sstevel@tonic-gate 	} else {
7387c478bd9Sstevel@tonic-gate 		err = ptree_add_prop(nodeh, proph);
7397c478bd9Sstevel@tonic-gate 	}
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate 	return (err);
7427c478bd9Sstevel@tonic-gate }
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate /*
7457c478bd9Sstevel@tonic-gate  * free refnode_cmd_t
7467c478bd9Sstevel@tonic-gate  */
7477c478bd9Sstevel@tonic-gate static void
free_refnode(command_t * command)7487c478bd9Sstevel@tonic-gate free_refnode(command_t *command)
7497c478bd9Sstevel@tonic-gate {
7507c478bd9Sstevel@tonic-gate 	free(command->refnodecmd_name);
7517c478bd9Sstevel@tonic-gate 	free(command->refnodecmd_class);
7527c478bd9Sstevel@tonic-gate 	free(command->refnodecmd_dstnode);
7537c478bd9Sstevel@tonic-gate }
7547c478bd9Sstevel@tonic-gate 
7557c478bd9Sstevel@tonic-gate /*
7567c478bd9Sstevel@tonic-gate  * Check the REFNODE syntax
7577c478bd9Sstevel@tonic-gate  *
7587c478bd9Sstevel@tonic-gate  * REFNODE <name> <class> with <destnode> -- if <destnode> exists,
7597c478bd9Sstevel@tonic-gate  * create node with nodename <name> and piclclass <class>
7607c478bd9Sstevel@tonic-gate  */
7617c478bd9Sstevel@tonic-gate static int
parse_refnode(char * line,command_t * command)7627c478bd9Sstevel@tonic-gate parse_refnode(char *line, command_t *command)
7637c478bd9Sstevel@tonic-gate {
7647c478bd9Sstevel@tonic-gate 	char	*tok;
7657c478bd9Sstevel@tonic-gate 	char	*dsttok;
7667c478bd9Sstevel@tonic-gate 	char	*classnm;
7677c478bd9Sstevel@tonic-gate 	char	*nodenm;
7687c478bd9Sstevel@tonic-gate 	char	*last;
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate 	/* get the directive */
7717c478bd9Sstevel@tonic-gate 	tok = strtok_r(line, WHITESPACE, &last);
7727c478bd9Sstevel@tonic-gate 	if (tok == NULL)
7737c478bd9Sstevel@tonic-gate 		return (EC_INSUFFICIENT_TOKEN);
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate 	/* get the nodename */
7767c478bd9Sstevel@tonic-gate 	nodenm = strtok_r(last, WHITESPACE, &last);
7777c478bd9Sstevel@tonic-gate 	if (nodenm == NULL)
7787c478bd9Sstevel@tonic-gate 		return (EC_INSUFFICIENT_TOKEN);
7797c478bd9Sstevel@tonic-gate 
7807c478bd9Sstevel@tonic-gate 	/* get the class */
7817c478bd9Sstevel@tonic-gate 	classnm = strtok_r(last, WHITESPACE, &last);
7827c478bd9Sstevel@tonic-gate 	if (classnm == NULL)
7837c478bd9Sstevel@tonic-gate 		return (EC_INSUFFICIENT_TOKEN);
7847c478bd9Sstevel@tonic-gate 
7857c478bd9Sstevel@tonic-gate 	/* get the WITH keyword */
7867c478bd9Sstevel@tonic-gate 	tok = strtok_r(last, WHITESPACE, &last);
7877c478bd9Sstevel@tonic-gate 	if (tok == NULL)
7887c478bd9Sstevel@tonic-gate 		return (EC_INSUFFICIENT_TOKEN);
7897c478bd9Sstevel@tonic-gate 
7907c478bd9Sstevel@tonic-gate 	if (strcasecmp(tok, KEYWORD_WITH_STR) != 0)
7917c478bd9Sstevel@tonic-gate 		return (EC_SYNTAX_ERR);
7927c478bd9Sstevel@tonic-gate 
7937c478bd9Sstevel@tonic-gate 	/* get the dst node */
7947c478bd9Sstevel@tonic-gate 	dsttok = strtok_r(last, WHITESPACE, &last);
7957c478bd9Sstevel@tonic-gate 	if (dsttok == NULL)
7967c478bd9Sstevel@tonic-gate 		return (EC_INSUFFICIENT_TOKEN);
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate 	/* check if more tokens */
7997c478bd9Sstevel@tonic-gate 	tok = strtok_r(last, WHITESPACE, &last);
8007c478bd9Sstevel@tonic-gate 	if (tok != NULL)
8017c478bd9Sstevel@tonic-gate 		return (EC_SYNTAX_ERR);
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate 	command->refnodecmd_name = strdup(nodenm);
8047c478bd9Sstevel@tonic-gate 	command->refnodecmd_class = strdup(classnm);
8057c478bd9Sstevel@tonic-gate 	command->refnodecmd_dstnode = strdup(dsttok);
806*e9610e3eSToomas Soome 	command->refnodecmd_nodeh = 0;
8077c478bd9Sstevel@tonic-gate 	if ((command->refnodecmd_name == NULL) ||
8087c478bd9Sstevel@tonic-gate 	    (command->refnodecmd_class == NULL) ||
8097c478bd9Sstevel@tonic-gate 	    (command->refnodecmd_dstnode == NULL))
8107c478bd9Sstevel@tonic-gate 		return (EC_FAILURE);
8117c478bd9Sstevel@tonic-gate 
8127c478bd9Sstevel@tonic-gate 	return (EC_SYNTAX_OK);
8137c478bd9Sstevel@tonic-gate }
8147c478bd9Sstevel@tonic-gate 
8157c478bd9Sstevel@tonic-gate /*
8167c478bd9Sstevel@tonic-gate  * Process the REFNODE command
8177c478bd9Sstevel@tonic-gate  */
8187c478bd9Sstevel@tonic-gate static int
process_refnode(command_t * command,picl_nodehdl_t parh)8197c478bd9Sstevel@tonic-gate process_refnode(command_t *command, picl_nodehdl_t parh)
8207c478bd9Sstevel@tonic-gate {
8217c478bd9Sstevel@tonic-gate 	picl_nodehdl_t	dsth;
8227c478bd9Sstevel@tonic-gate 	picl_nodehdl_t	nodeh;
8237c478bd9Sstevel@tonic-gate 	int		err;
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate 	if ((ptree_get_node_by_path(command->refnodecmd_dstnode,
8267c478bd9Sstevel@tonic-gate 	    &dsth) == PICL_SUCCESS)) {
8277c478bd9Sstevel@tonic-gate 		err = ptree_create_and_add_node(parh, command->refnodecmd_name,
8287c478bd9Sstevel@tonic-gate 		    command->refnodecmd_class, &nodeh);
8297c478bd9Sstevel@tonic-gate 		if (err == PICL_SUCCESS)
8307c478bd9Sstevel@tonic-gate 			command->refnodecmd_nodeh = nodeh;
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate 		return (err);
8337c478bd9Sstevel@tonic-gate 	}
8347c478bd9Sstevel@tonic-gate 
8357c478bd9Sstevel@tonic-gate 	return (PICL_SUCCESS);
8367c478bd9Sstevel@tonic-gate }
8377c478bd9Sstevel@tonic-gate 
8387c478bd9Sstevel@tonic-gate /*
8397c478bd9Sstevel@tonic-gate  * free refprop_cmd_t
8407c478bd9Sstevel@tonic-gate  */
8417c478bd9Sstevel@tonic-gate static void
free_refprop(command_t * command)8427c478bd9Sstevel@tonic-gate free_refprop(command_t *command)
8437c478bd9Sstevel@tonic-gate {
8447c478bd9Sstevel@tonic-gate 	free(command->refpropcmd_pname);
8457c478bd9Sstevel@tonic-gate 	free(command->refpropcmd_dstnode);
8467c478bd9Sstevel@tonic-gate }
8477c478bd9Sstevel@tonic-gate 
8487c478bd9Sstevel@tonic-gate /*
8497c478bd9Sstevel@tonic-gate  * Check the REFPROP syntax
8507c478bd9Sstevel@tonic-gate  *
8517c478bd9Sstevel@tonic-gate  * REFPROP <prop> <destnode> -- creates a reference property to <destnode>
8527c478bd9Sstevel@tonic-gate  */
8537c478bd9Sstevel@tonic-gate static int
parse_refprop(char * line,command_t * command)8547c478bd9Sstevel@tonic-gate parse_refprop(char *line, command_t *command)
8557c478bd9Sstevel@tonic-gate {
8567c478bd9Sstevel@tonic-gate 	char	*tok;
8577c478bd9Sstevel@tonic-gate 	char	*pnametok;
8587c478bd9Sstevel@tonic-gate 	char	*dsttok;
8597c478bd9Sstevel@tonic-gate 	char	*last;
8607c478bd9Sstevel@tonic-gate 
8617c478bd9Sstevel@tonic-gate 	/* get the REFPROP directive */
8627c478bd9Sstevel@tonic-gate 	tok = strtok_r(line, WHITESPACE, &last);
8637c478bd9Sstevel@tonic-gate 	if (tok == NULL)
8647c478bd9Sstevel@tonic-gate 		return (EC_INSUFFICIENT_TOKEN);
8657c478bd9Sstevel@tonic-gate 
8667c478bd9Sstevel@tonic-gate 	/* get the propname  */
8677c478bd9Sstevel@tonic-gate 	pnametok = strtok_r(last, WHITESPACE, &last);
8687c478bd9Sstevel@tonic-gate 	if (pnametok == NULL)
8697c478bd9Sstevel@tonic-gate 		return (EC_INSUFFICIENT_TOKEN);
8707c478bd9Sstevel@tonic-gate 
8717c478bd9Sstevel@tonic-gate 	dsttok = strtok_r(last, WHITESPACE, &last);
8727c478bd9Sstevel@tonic-gate 	if (dsttok == NULL)
8737c478bd9Sstevel@tonic-gate 		return (EC_INSUFFICIENT_TOKEN);
8747c478bd9Sstevel@tonic-gate 
8757c478bd9Sstevel@tonic-gate 	/* check if more tokens */
8767c478bd9Sstevel@tonic-gate 	tok = strtok_r(last, WHITESPACE, &last);
8777c478bd9Sstevel@tonic-gate 	if (tok != NULL)
8787c478bd9Sstevel@tonic-gate 		return (EC_SYNTAX_ERR);
8797c478bd9Sstevel@tonic-gate 
8807c478bd9Sstevel@tonic-gate 	command->refpropcmd_pname = strdup(pnametok);
8817c478bd9Sstevel@tonic-gate 	command->refpropcmd_dstnode = strdup(dsttok);
882*e9610e3eSToomas Soome 	command->refpropcmd_proph = 0;
8837c478bd9Sstevel@tonic-gate 	if ((command->refpropcmd_pname == NULL) ||
8847c478bd9Sstevel@tonic-gate 	    (command->refpropcmd_dstnode == NULL))
8857c478bd9Sstevel@tonic-gate 		return (EC_FAILURE);
8867c478bd9Sstevel@tonic-gate 
8877c478bd9Sstevel@tonic-gate 	return (EC_SYNTAX_OK);
8887c478bd9Sstevel@tonic-gate }
8897c478bd9Sstevel@tonic-gate 
8907c478bd9Sstevel@tonic-gate /*
8917c478bd9Sstevel@tonic-gate  * Process the REFPROP command
8927c478bd9Sstevel@tonic-gate  */
8937c478bd9Sstevel@tonic-gate static int
process_refprop(cmdbuf_t * cmds,command_t * command,picl_nodehdl_t nodeh)8947c478bd9Sstevel@tonic-gate process_refprop(cmdbuf_t *cmds, command_t *command, picl_nodehdl_t nodeh)
8957c478bd9Sstevel@tonic-gate {
8967c478bd9Sstevel@tonic-gate 	int			err;
897*e9610e3eSToomas Soome 	picl_nodehdl_t		dsth;
898*e9610e3eSToomas Soome 	picl_prophdl_t		proph;
8997c478bd9Sstevel@tonic-gate 	ptree_propinfo_t	propinfo;
9007c478bd9Sstevel@tonic-gate 
9017c478bd9Sstevel@tonic-gate 	/* refprop in discarded row */
9027c478bd9Sstevel@tonic-gate 	if (cmds->inside_row_block &&
9037c478bd9Sstevel@tonic-gate 	    cmds->commands[cmds->current_row].rowcmd_nproph == 0)
9047c478bd9Sstevel@tonic-gate 		return (PICL_SUCCESS);
9057c478bd9Sstevel@tonic-gate 
9067c478bd9Sstevel@tonic-gate 	/* try finding the refprop's dstnode */
9077c478bd9Sstevel@tonic-gate 	err = ptree_get_node_by_path(command->refpropcmd_dstnode, &dsth);
9087c478bd9Sstevel@tonic-gate 
9097c478bd9Sstevel@tonic-gate 	/* dstnode doesn't exist, return */
9107c478bd9Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
9117c478bd9Sstevel@tonic-gate 		return (err);
9127c478bd9Sstevel@tonic-gate 
9137c478bd9Sstevel@tonic-gate 	/* dstnode exists, try adding the refprop to nodeh */
9147c478bd9Sstevel@tonic-gate 	err = ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
9155705cefcSMichael Bergknoff 	    PICL_PTYPE_REFERENCE, PICL_READ, sizeof (picl_nodehdl_t),
9165705cefcSMichael Bergknoff 	    command->refpropcmd_pname, NULL, NULL);
9177c478bd9Sstevel@tonic-gate 
9187c478bd9Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
9197c478bd9Sstevel@tonic-gate 		return (err);
9207c478bd9Sstevel@tonic-gate 
9217c478bd9Sstevel@tonic-gate 	err = ptree_create_prop(&propinfo, &dsth, &proph);
9227c478bd9Sstevel@tonic-gate 
9237c478bd9Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
9247c478bd9Sstevel@tonic-gate 		return (err);
9257c478bd9Sstevel@tonic-gate 
9267c478bd9Sstevel@tonic-gate 	command->refpropcmd_proph = proph;
9277c478bd9Sstevel@tonic-gate 
9287c478bd9Sstevel@tonic-gate 	if (cmds->inside_row_block) {
9297c478bd9Sstevel@tonic-gate 		err = add_proph_to_row(&cmds->commands[cmds->current_row],
9307c478bd9Sstevel@tonic-gate 		    proph);
9317c478bd9Sstevel@tonic-gate 	} else {
9327c478bd9Sstevel@tonic-gate 		err = ptree_add_prop(nodeh, proph);
9337c478bd9Sstevel@tonic-gate 	}
9347c478bd9Sstevel@tonic-gate 
9357c478bd9Sstevel@tonic-gate 	return (err);
9367c478bd9Sstevel@tonic-gate }
9377c478bd9Sstevel@tonic-gate 
9387c478bd9Sstevel@tonic-gate /*
9397c478bd9Sstevel@tonic-gate  * free table_cmd_t
9407c478bd9Sstevel@tonic-gate  */
9417c478bd9Sstevel@tonic-gate static void
free_table(command_t * command)9427c478bd9Sstevel@tonic-gate free_table(command_t *command)
9437c478bd9Sstevel@tonic-gate {
9447c478bd9Sstevel@tonic-gate 	if (command->tablecmd_tname)
9457c478bd9Sstevel@tonic-gate 		free(command->tablecmd_tname);
9467c478bd9Sstevel@tonic-gate }
9477c478bd9Sstevel@tonic-gate 
9487c478bd9Sstevel@tonic-gate /*
9497c478bd9Sstevel@tonic-gate  * Check the TABLE syntax
9507c478bd9Sstevel@tonic-gate  * TABLE <table_prop_name>
9517c478bd9Sstevel@tonic-gate  *
9527c478bd9Sstevel@tonic-gate  */
9537c478bd9Sstevel@tonic-gate static int
parse_table(char * line,command_t * command)9547c478bd9Sstevel@tonic-gate parse_table(char *line, command_t *command)
9557c478bd9Sstevel@tonic-gate {
9567c478bd9Sstevel@tonic-gate 	char	*tok = NULL;
9577c478bd9Sstevel@tonic-gate 	char	*tnametok = NULL;
9587c478bd9Sstevel@tonic-gate 	char	*last = NULL;
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate 	/* get the TABLE directive */
9617c478bd9Sstevel@tonic-gate 	tok = strtok_r(line, WHITESPACE, &last);
9627c478bd9Sstevel@tonic-gate 	if (tok == NULL)
9637c478bd9Sstevel@tonic-gate 		return (EC_INSUFFICIENT_TOKEN);
9647c478bd9Sstevel@tonic-gate 
9657c478bd9Sstevel@tonic-gate 	/* get the property name */
9667c478bd9Sstevel@tonic-gate 	tnametok = strtok_r(last, WHITESPACE, &last);
9677c478bd9Sstevel@tonic-gate 	if (tnametok == NULL)
9687c478bd9Sstevel@tonic-gate 		return (EC_INSUFFICIENT_TOKEN);
9697c478bd9Sstevel@tonic-gate 
9707c478bd9Sstevel@tonic-gate 	command->tablecmd_tname = strdup(tnametok);
9717c478bd9Sstevel@tonic-gate 	if (command->tablecmd_tname == NULL)
9727c478bd9Sstevel@tonic-gate 		return (EC_FAILURE);
9737c478bd9Sstevel@tonic-gate 
9747c478bd9Sstevel@tonic-gate 	command->tablecmd_newtbl = 0;
975*e9610e3eSToomas Soome 	command->tablecmd_tblh = 0;
9767c478bd9Sstevel@tonic-gate 
9777c478bd9Sstevel@tonic-gate 	return (EC_SYNTAX_OK);
9787c478bd9Sstevel@tonic-gate }
9797c478bd9Sstevel@tonic-gate 
9807c478bd9Sstevel@tonic-gate /*
9817c478bd9Sstevel@tonic-gate  * Process the TABLE command and add the specified property under the given
9827c478bd9Sstevel@tonic-gate  * node handle
9837c478bd9Sstevel@tonic-gate  */
9847c478bd9Sstevel@tonic-gate static int
process_table(command_t * command,picl_nodehdl_t nodeh)9857c478bd9Sstevel@tonic-gate process_table(command_t *command, picl_nodehdl_t nodeh)
9867c478bd9Sstevel@tonic-gate {
9877c478bd9Sstevel@tonic-gate 	int			err;
988*e9610e3eSToomas Soome 	picl_prophdl_t		tblh;
989*e9610e3eSToomas Soome 	picl_prophdl_t		proph;
9907c478bd9Sstevel@tonic-gate 	ptree_propinfo_t	propinfo;
9917c478bd9Sstevel@tonic-gate 
9927c478bd9Sstevel@tonic-gate 	/* find if table already exists */
9937c478bd9Sstevel@tonic-gate 	err = ptree_get_prop_by_name(nodeh, command->tablecmd_tname, &tblh);
9947c478bd9Sstevel@tonic-gate 	if (err == PICL_SUCCESS) {
9957c478bd9Sstevel@tonic-gate 		err = ptree_get_propinfo(tblh, &propinfo);
9967c478bd9Sstevel@tonic-gate 		if (err != PICL_SUCCESS)
9977c478bd9Sstevel@tonic-gate 			return (err);
9987c478bd9Sstevel@tonic-gate 		/* prop with the same name as table? */
9997c478bd9Sstevel@tonic-gate 		if (propinfo.piclinfo.type != PICL_PTYPE_TABLE)
10007c478bd9Sstevel@tonic-gate 			return (EC_SYNTAX_ERR);
10017c478bd9Sstevel@tonic-gate 		command->tablecmd_newtbl = 0;
10027c478bd9Sstevel@tonic-gate 		command->tablecmd_tblh = tblh;
10037c478bd9Sstevel@tonic-gate 		return (PICL_SUCCESS);
10047c478bd9Sstevel@tonic-gate 	}
10057c478bd9Sstevel@tonic-gate 
10067c478bd9Sstevel@tonic-gate 	/* init and create a new table */
10077c478bd9Sstevel@tonic-gate 	err = ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
10085705cefcSMichael Bergknoff 	    PICL_PTYPE_TABLE, PICL_READ|PICL_WRITE,
10095705cefcSMichael Bergknoff 	    sizeof (picl_prophdl_t), command->tablecmd_tname, NULL, NULL);
10107c478bd9Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
10117c478bd9Sstevel@tonic-gate 		return (err);
10127c478bd9Sstevel@tonic-gate 
10137c478bd9Sstevel@tonic-gate 	err = ptree_create_table(&tblh);
10147c478bd9Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
10157c478bd9Sstevel@tonic-gate 		return (err);
10167c478bd9Sstevel@tonic-gate 
10177c478bd9Sstevel@tonic-gate 	command->tablecmd_newtbl = 1;
10187c478bd9Sstevel@tonic-gate 	command->tablecmd_tblh = tblh;
10197c478bd9Sstevel@tonic-gate 
10207c478bd9Sstevel@tonic-gate 	err = ptree_create_prop(&propinfo, &tblh, &proph);
10217c478bd9Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
10227c478bd9Sstevel@tonic-gate 		return (err);
10237c478bd9Sstevel@tonic-gate 
10247c478bd9Sstevel@tonic-gate 	err = ptree_add_prop(nodeh, proph);
10257c478bd9Sstevel@tonic-gate 
10267c478bd9Sstevel@tonic-gate 	return (err);
10277c478bd9Sstevel@tonic-gate }
10287c478bd9Sstevel@tonic-gate 
10297c478bd9Sstevel@tonic-gate /*
10307c478bd9Sstevel@tonic-gate  * Process the ROW command by alloc'ing space to store the prop handles for
10317c478bd9Sstevel@tonic-gate  * the whole row. The number of props in the row gets known while parsing.
10327c478bd9Sstevel@tonic-gate  */
10337c478bd9Sstevel@tonic-gate static int
process_row(command_t * command)10347c478bd9Sstevel@tonic-gate process_row(command_t *command)
10357c478bd9Sstevel@tonic-gate {
10367c478bd9Sstevel@tonic-gate 	command->rowcmd_index = 0;
10377c478bd9Sstevel@tonic-gate 	command->rowcmd_prophs =
10385705cefcSMichael Bergknoff 	    malloc(command->rowcmd_nproph * sizeof (picl_prophdl_t));
10397c478bd9Sstevel@tonic-gate 
10407c478bd9Sstevel@tonic-gate 	if (command->rowcmd_prophs == NULL)
10417c478bd9Sstevel@tonic-gate 		return (PICL_FAILURE);
10427c478bd9Sstevel@tonic-gate 
10437c478bd9Sstevel@tonic-gate 	return (PICL_SUCCESS);
10447c478bd9Sstevel@tonic-gate }
10457c478bd9Sstevel@tonic-gate 
10467c478bd9Sstevel@tonic-gate /*
10477c478bd9Sstevel@tonic-gate  * Process the ENDROW command. If a valid row, add the row to the ptree.
10487c478bd9Sstevel@tonic-gate  */
10497c478bd9Sstevel@tonic-gate static int
process_endrow(cmdbuf_t * cmds)10507c478bd9Sstevel@tonic-gate process_endrow(cmdbuf_t *cmds)
10517c478bd9Sstevel@tonic-gate {
10527c478bd9Sstevel@tonic-gate 	int		err;
10537c478bd9Sstevel@tonic-gate 	int		i;
10547c478bd9Sstevel@tonic-gate 	command_t	*curr_row;
10557c478bd9Sstevel@tonic-gate 
10567c478bd9Sstevel@tonic-gate 	curr_row = &cmds->commands[cmds->current_row];
10577c478bd9Sstevel@tonic-gate 
10587c478bd9Sstevel@tonic-gate 	/* if nproph == 0, some row prop had problems, don't add  */
10597c478bd9Sstevel@tonic-gate 	if (curr_row->rowcmd_nproph == 0) {
10607c478bd9Sstevel@tonic-gate 		for (i = 0; i < curr_row->rowcmd_index; i++) {
10617c478bd9Sstevel@tonic-gate 			(void) ptree_delete_prop(curr_row->rowcmd_prophs[i]);
10627c478bd9Sstevel@tonic-gate 			(void) ptree_destroy_prop(curr_row->rowcmd_prophs[i]);
10637c478bd9Sstevel@tonic-gate 		}
10647c478bd9Sstevel@tonic-gate 		err = PICL_SUCCESS;
10657c478bd9Sstevel@tonic-gate 	} else
10667c478bd9Sstevel@tonic-gate 		err = ptree_add_row_to_table(
10675705cefcSMichael Bergknoff 		    cmds->commands[cmds->current_tbl].tablecmd_tblh,
10685705cefcSMichael Bergknoff 		    curr_row->rowcmd_nproph,
10695705cefcSMichael Bergknoff 		    curr_row->rowcmd_prophs);
10707c478bd9Sstevel@tonic-gate 
10717c478bd9Sstevel@tonic-gate 	/* let go the space alloc'd in process_row */
10727c478bd9Sstevel@tonic-gate 	free(curr_row->rowcmd_prophs);
10737c478bd9Sstevel@tonic-gate 	curr_row->rowcmd_prophs = NULL;
10747c478bd9Sstevel@tonic-gate 
10757c478bd9Sstevel@tonic-gate 	return (err);
10767c478bd9Sstevel@tonic-gate }
10777c478bd9Sstevel@tonic-gate 
10787c478bd9Sstevel@tonic-gate /*
10797c478bd9Sstevel@tonic-gate  * Check the VERBOSE syntax
10807c478bd9Sstevel@tonic-gate  * VERBOSE <level>
10817c478bd9Sstevel@tonic-gate  */
10827c478bd9Sstevel@tonic-gate static int
parse_verbose(cmdbuf_t * cmds,char * line,command_t * command)10837c478bd9Sstevel@tonic-gate parse_verbose(cmdbuf_t *cmds, char *line, command_t *command)
10847c478bd9Sstevel@tonic-gate {
10857c478bd9Sstevel@tonic-gate 	char	*tok;
10867c478bd9Sstevel@tonic-gate 	char	*level;
10877c478bd9Sstevel@tonic-gate 	char	*last;
10887c478bd9Sstevel@tonic-gate 	char	*endptr;
10897c478bd9Sstevel@tonic-gate 	int	verbose_level;
10907c478bd9Sstevel@tonic-gate 
10917c478bd9Sstevel@tonic-gate 	/* get the VERBOSE directive */
10927c478bd9Sstevel@tonic-gate 	tok = strtok_r(line, WHITESPACE, &last);
10937c478bd9Sstevel@tonic-gate 	if (tok == NULL)
10947c478bd9Sstevel@tonic-gate 		return (EC_INSUFFICIENT_TOKEN);
10957c478bd9Sstevel@tonic-gate 
10967c478bd9Sstevel@tonic-gate 	/* get verbose level */
10977c478bd9Sstevel@tonic-gate 	level = strtok_r(last, WHITESPACE, &last);
10987c478bd9Sstevel@tonic-gate 	if (level == NULL)
10997c478bd9Sstevel@tonic-gate 		return (EC_INSUFFICIENT_TOKEN);
11007c478bd9Sstevel@tonic-gate 	verbose_level = strtol(level, &endptr, 0);
11017c478bd9Sstevel@tonic-gate 	if (endptr != (level + strlen(level)))
11027c478bd9Sstevel@tonic-gate 		return (EC_SYNTAX_ERR);
11037c478bd9Sstevel@tonic-gate 
11047c478bd9Sstevel@tonic-gate 	/* check if more tokens */
11057c478bd9Sstevel@tonic-gate 	tok = strtok_r(last, WHITESPACE, &last);
11067c478bd9Sstevel@tonic-gate 	if (tok != NULL)
11077c478bd9Sstevel@tonic-gate 		return (EC_SYNTAX_ERR);
11087c478bd9Sstevel@tonic-gate 
11097c478bd9Sstevel@tonic-gate 	cmds->verbose = verbose_level;
11107c478bd9Sstevel@tonic-gate 	command->verbosecmd_level = verbose_level;
11117c478bd9Sstevel@tonic-gate 
11127c478bd9Sstevel@tonic-gate 	return (EC_SYNTAX_OK);
11137c478bd9Sstevel@tonic-gate }
11147c478bd9Sstevel@tonic-gate 
11157c478bd9Sstevel@tonic-gate /*
11167c478bd9Sstevel@tonic-gate  * Process the VERBOSE command to set the verbose level
11177c478bd9Sstevel@tonic-gate  */
11187c478bd9Sstevel@tonic-gate static int
process_verbose(cmdbuf_t * cmds,command_t * command)11197c478bd9Sstevel@tonic-gate process_verbose(cmdbuf_t *cmds, command_t *command)
11207c478bd9Sstevel@tonic-gate {
11217c478bd9Sstevel@tonic-gate 	cmds->verbose = command->verbosecmd_level;
11227c478bd9Sstevel@tonic-gate 	return (PICL_SUCCESS);
11237c478bd9Sstevel@tonic-gate }
11247c478bd9Sstevel@tonic-gate 
11257c478bd9Sstevel@tonic-gate /*
11267c478bd9Sstevel@tonic-gate  * parse and tokenize the line
11277c478bd9Sstevel@tonic-gate  */
11287c478bd9Sstevel@tonic-gate static int
parse_and_tokenize_line(cmdbuf_t * cmds,char * buf,command_t * command)11297c478bd9Sstevel@tonic-gate parse_and_tokenize_line(cmdbuf_t *cmds, char *buf, command_t *command)
11307c478bd9Sstevel@tonic-gate {
11317c478bd9Sstevel@tonic-gate 	char		rec[RECORD_SIZE_MAX];
11327c478bd9Sstevel@tonic-gate 	char		*tok;
11337c478bd9Sstevel@tonic-gate 	int		err;
11347c478bd9Sstevel@tonic-gate 	char		*last;
11357c478bd9Sstevel@tonic-gate 	int		id;
11367c478bd9Sstevel@tonic-gate 
11377c478bd9Sstevel@tonic-gate 	(void) strcpy(rec, buf);
11387c478bd9Sstevel@tonic-gate 	tok = strtok_r(rec, RECORD_WHITESPACE, &last);
11397c478bd9Sstevel@tonic-gate 	if (tok == NULL)
11407c478bd9Sstevel@tonic-gate 		return (EC_INSUFFICIENT_TOKEN);
11417c478bd9Sstevel@tonic-gate 
11427c478bd9Sstevel@tonic-gate 	id = get_token_id(tok);
11437c478bd9Sstevel@tonic-gate 
11447c478bd9Sstevel@tonic-gate 	(void) strcpy(rec, buf);
11457c478bd9Sstevel@tonic-gate 
11467c478bd9Sstevel@tonic-gate 	switch (id) {
11477c478bd9Sstevel@tonic-gate 	case TOK_VERSION:
11487c478bd9Sstevel@tonic-gate 		err = parse_version(cmds, rec);
11497c478bd9Sstevel@tonic-gate 		break;
11507c478bd9Sstevel@tonic-gate 	case TOK_CLASSPATH:
11517c478bd9Sstevel@tonic-gate 	case TOK_NAMEPATH:
11527c478bd9Sstevel@tonic-gate 		if (cmds->inside_node_block != 0)
11537c478bd9Sstevel@tonic-gate 			return (EC_PATH_ERR);
11547c478bd9Sstevel@tonic-gate 
11557c478bd9Sstevel@tonic-gate 		err = parse_path(rec, command);
11567c478bd9Sstevel@tonic-gate 		if (err != EC_SYNTAX_OK)
11577c478bd9Sstevel@tonic-gate 			return (err);
11587c478bd9Sstevel@tonic-gate 		break;
11597c478bd9Sstevel@tonic-gate 	case TOK_NODE:
11607c478bd9Sstevel@tonic-gate 		/* Check for NODE outside of TABLE, ROW */
11617c478bd9Sstevel@tonic-gate 		if ((cmds->inside_table_block != 0) ||
11627c478bd9Sstevel@tonic-gate 		    (cmds->inside_row_block != 0))
11637c478bd9Sstevel@tonic-gate 			return (EC_SYNTAX_ERR);
11647c478bd9Sstevel@tonic-gate 		err = parse_node(rec, command);
11657c478bd9Sstevel@tonic-gate 		if (err != EC_SYNTAX_OK)
11667c478bd9Sstevel@tonic-gate 			return (err);
11677c478bd9Sstevel@tonic-gate 		cmds->inside_node_block++;
11687c478bd9Sstevel@tonic-gate 		break;
11697c478bd9Sstevel@tonic-gate 	case TOK_ENDNODE:
11707c478bd9Sstevel@tonic-gate 		/* Check for ENDNODE outside of TABLE, ROW */
11717c478bd9Sstevel@tonic-gate 		if ((cmds->inside_table_block != 0) ||
11727c478bd9Sstevel@tonic-gate 		    (cmds->inside_row_block != 0))
11737c478bd9Sstevel@tonic-gate 			return (EC_SYNTAX_ERR);
11747c478bd9Sstevel@tonic-gate 		cmds->