xref: /onnv-gate/usr/src/cmd/sgs/libconv/common/config.c (revision 9273:9a0603d78ad3)
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
51618Srie  * Common Development and Distribution License (the "License").
61618Srie  * 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  */
211618Srie 
220Sstevel@tonic-gate /*
23*9273SAli.Bahrami@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #include	<stdlib.h>
280Sstevel@tonic-gate #include	<sys/types.h>
290Sstevel@tonic-gate #include	<string.h>
300Sstevel@tonic-gate #include	"rtc.h"
310Sstevel@tonic-gate #include	"_conv.h"
320Sstevel@tonic-gate #include	"config_msg.h"
330Sstevel@tonic-gate 
342352Sab196087 #define	FEATSZ	CONV_EXPN_FIELD_DEF_PREFIX_SIZE + \
352352Sab196087 		MSG_CONF_EDLIBPATH_SIZE + CONV_EXPN_FIELD_DEF_SEP_SIZE + \
362352Sab196087 		MSG_CONF_ESLIBPATH_SIZE + CONV_EXPN_FIELD_DEF_SEP_SIZE + \
372352Sab196087 		MSG_CONF_ADLIBPATH_SIZE + CONV_EXPN_FIELD_DEF_SEP_SIZE + \
382352Sab196087 		MSG_CONF_ASLIBPATH_SIZE + CONV_EXPN_FIELD_DEF_SEP_SIZE + \
392352Sab196087 		MSG_CONF_DIRCFG_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
402352Sab196087 		MSG_CONF_OBJALT_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
412352Sab196087 		MSG_CONF_MEMRESV_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
422352Sab196087 		MSG_CONF_ENVS_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
432352Sab196087 		MSG_CONF_FLTR_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
444734Sab196087 		CONV_INV_BUFSIZE + CONV_EXPN_FIELD_DEF_SUFFIX_SIZE
454734Sab196087 
464734Sab196087 /*
474734Sab196087  * Ensure that Conv_config_feat_buf_t is large enough:
484734Sab196087  *
494734Sab196087  * FEATSZ is the real minimum size of the buffer required by conv_config_feat().
504734Sab196087  * However, Conv_config_feat_buf_t uses CONV_CONFIG_FEAT_BUFSIZE to set the
514734Sab196087  * buffer size. We do things this way because the definition of FEATSZ uses
524734Sab196087  * information that is not available in the environment of other programs
534734Sab196087  * that include the conv.h header file.
544734Sab196087  */
555152Sab196087 #if (CONV_CONFIG_FEAT_BUFSIZE != FEATSZ) && !defined(__lint)
565152Sab196087 #define	REPORT_BUFSIZE FEATSZ
575152Sab196087 #include "report_bufsize.h"
585152Sab196087 #error "CONV_CONFIG_FEAT_BUFSIZE does not match FEATSZ"
594734Sab196087 #endif
600Sstevel@tonic-gate 
610Sstevel@tonic-gate /*
620Sstevel@tonic-gate  * String conversion routine for configuration file information.
630Sstevel@tonic-gate  */
640Sstevel@tonic-gate const char *
conv_config_feat(int features,Conv_config_feat_buf_t * config_feat_buf)654734Sab196087 conv_config_feat(int features, Conv_config_feat_buf_t *config_feat_buf)
660Sstevel@tonic-gate {
67*9273SAli.Bahrami@Sun.COM 	static const Val_desc	vda[] = {
68*9273SAli.Bahrami@Sun.COM 		{ CONF_EDLIBPATH,	MSG_CONF_EDLIBPATH },
69*9273SAli.Bahrami@Sun.COM 		{ CONF_ESLIBPATH,	MSG_CONF_ESLIBPATH },
70*9273SAli.Bahrami@Sun.COM 		{ CONF_ADLIBPATH,	MSG_CONF_ADLIBPATH },
71*9273SAli.Bahrami@Sun.COM 		{ CONF_ASLIBPATH,	MSG_CONF_ASLIBPATH },
72*9273SAli.Bahrami@Sun.COM 		{ CONF_DIRCFG,		MSG_CONF_DIRCFG },
73*9273SAli.Bahrami@Sun.COM 		{ CONF_OBJALT,		MSG_CONF_OBJALT },
74*9273SAli.Bahrami@Sun.COM 		{ CONF_MEMRESV,		MSG_CONF_MEMRESV },
75*9273SAli.Bahrami@Sun.COM 		{ CONF_ENVS,		MSG_CONF_ENVS },
76*9273SAli.Bahrami@Sun.COM 		{ CONF_FLTR,		MSG_CONF_FLTR },
771618Srie 		{ 0,			0 }
781618Srie 	};
794734Sab196087 	static CONV_EXPN_FIELD_ARG conv_arg = {
80*9273SAli.Bahrami@Sun.COM 	    NULL, sizeof (config_feat_buf->buf) };
810Sstevel@tonic-gate 
824734Sab196087 	conv_arg.buf = config_feat_buf->buf;
832352Sab196087 	conv_arg.oflags = conv_arg.rflags = features;
84*9273SAli.Bahrami@Sun.COM 	(void) conv_expn_field(&conv_arg, vda, 0);
850Sstevel@tonic-gate 
864734Sab196087 	return ((const char *)config_feat_buf->buf);
870Sstevel@tonic-gate }
880Sstevel@tonic-gate 
892352Sab196087 #define	FLAGSZ	CONV_EXPN_FIELD_DEF_PREFIX_SIZE + \
902352Sab196087 		MSG_CONF_DIRENT_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
912352Sab196087 		MSG_CONF_ALLENTS_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
922352Sab196087 		MSG_CONF_NOEXIST_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
932352Sab196087 		MSG_CONF_EXEC_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
942352Sab196087 		MSG_CONF_ALTER_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
952352Sab196087 		MSG_CONF_OPTIONAL_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
962352Sab196087 		MSG_CONF_DUMP_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
972352Sab196087 		MSG_CONF_REALPATH_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
982352Sab196087 		MSG_CONF_NOALTER_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
992352Sab196087 		MSG_CONF_GROUP_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
1002352Sab196087 		MSG_CONF_APP_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
1012352Sab196087 		MSG_CONF_CMDLINE_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
1022352Sab196087 		MSG_CONF_FILTER_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
1032352Sab196087 		MSG_CONF_FILTEE_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
1044734Sab196087 		CONV_INV_BUFSIZE + CONV_EXPN_FIELD_DEF_SUFFIX_SIZE
1054734Sab196087 
1064734Sab196087 /*
1074734Sab196087  * Ensure that Conv_config_obj_buf_t is large enough:
1084734Sab196087  *
1094734Sab196087  * FLAGSZ is the real minimum size of the buffer required by conv_config_obj().
1104734Sab196087  * However, Conv_config_obj_buf_t uses CONV_CONFIG_OBJ_BUFSIZE to set the
1114734Sab196087  * buffer size. We do things this way because the definition of FLAGSZ uses
1124734Sab196087  * information that is not available in the environment of other programs
1134734Sab196087  * that include the conv.h header file.
1144734Sab196087  */
1155152Sab196087 #if (CONV_CONFIG_OBJ_BUFSIZE != FLAGSZ) && !defined(__lint)
1165152Sab196087 #define	REPORT_BUFSIZE FLAGSZ
1175152Sab196087 #include "report_bufsize.h"
1185152Sab196087 #error "CONV_CONFIG_OBJ_BUFSIZE does not match FLAGSZ"
1194734Sab196087 #endif
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate /*
1220Sstevel@tonic-gate  * String conversion routine for object flags.
1230Sstevel@tonic-gate  */
1240Sstevel@tonic-gate const char *
conv_config_obj(ushort_t flags,Conv_config_obj_buf_t * config_obj_buf)1254734Sab196087 conv_config_obj(ushort_t flags, Conv_config_obj_buf_t *config_obj_buf)
1260Sstevel@tonic-gate {
127*9273SAli.Bahrami@Sun.COM 	static const Val_desc vda[] = {
128*9273SAli.Bahrami@Sun.COM 		{ RTC_OBJ_DIRENT,	MSG_CONF_DIRENT },
129*9273SAli.Bahrami@Sun.COM 		{ RTC_OBJ_ALLENTS,	MSG_CONF_ALLENTS },
130*9273SAli.Bahrami@Sun.COM 		{ RTC_OBJ_NOEXIST,	MSG_CONF_NOEXIST },
131*9273SAli.Bahrami@Sun.COM 		{ RTC_OBJ_EXEC,		MSG_CONF_EXEC },
132*9273SAli.Bahrami@Sun.COM 		{ RTC_OBJ_ALTER,	MSG_CONF_ALTER },
133*9273SAli.Bahrami@Sun.COM 		{ RTC_OBJ_DUMP,		MSG_CONF_DUMP },
134*9273SAli.Bahrami@Sun.COM 		{ RTC_OBJ_NOALTER,	MSG_CONF_NOALTER },
135*9273SAli.Bahrami@Sun.COM 		{ RTC_OBJ_REALPTH,	MSG_CONF_REALPATH },
136*9273SAli.Bahrami@Sun.COM 		{ RTC_OBJ_GROUP,	MSG_CONF_GROUP },
137*9273SAli.Bahrami@Sun.COM 		{ RTC_OBJ_APP,		MSG_CONF_APP },
138*9273SAli.Bahrami@Sun.COM 		{ RTC_OBJ_CMDLINE,	MSG_CONF_CMDLINE },
139*9273SAli.Bahrami@Sun.COM 		{ RTC_OBJ_FILTER,	MSG_CONF_FILTER },
140*9273SAli.Bahrami@Sun.COM 		{ RTC_OBJ_FILTEE,	MSG_CONF_FILTEE },
1411618Srie 		{ 0,			0 }
1421618Srie 	};
1432352Sab196087 	static const char *leading_str_arr[2];
1444734Sab196087 	static CONV_EXPN_FIELD_ARG conv_arg = {
145*9273SAli.Bahrami@Sun.COM 	    NULL, sizeof (config_obj_buf->buf), leading_str_arr };
1462352Sab196087 
1472352Sab196087 	const char **lstr = leading_str_arr;
1480Sstevel@tonic-gate 
1491618Srie 	if ((flags == 0) || (flags == RTC_OBJ_OPTINAL))
1501618Srie 		return (MSG_ORIG(MSG_GBL_NULL));
1511618Srie 
1524734Sab196087 	conv_arg.buf = config_obj_buf->buf;
1532352Sab196087 	conv_arg.rflags = flags;
1541618Srie 
1551618Srie 	/*
1561618Srie 	 * Print an alternative-optional object simply as optional.
1571618Srie 	 */
1581618Srie 	if ((flags & (RTC_OBJ_ALTER | RTC_OBJ_OPTINAL)) ==
1591618Srie 	    (RTC_OBJ_ALTER | RTC_OBJ_OPTINAL)) {
1602352Sab196087 		*lstr++ = MSG_ORIG(MSG_CONF_OPTIONAL);
1612352Sab196087 		conv_arg.rflags &= ~(RTC_OBJ_ALTER | RTC_OBJ_OPTINAL);
1620Sstevel@tonic-gate 	}
1632352Sab196087 	*lstr = NULL;
1642352Sab196087 	conv_arg.oflags = conv_arg.rflags &= ~RTC_OBJ_OPTINAL;
1650Sstevel@tonic-gate 
166*9273SAli.Bahrami@Sun.COM 	(void) conv_expn_field(&conv_arg, vda, 0);
1670Sstevel@tonic-gate 
1684734Sab196087 	return ((const char *)config_obj_buf->buf);
1690Sstevel@tonic-gate }
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate /*
1720Sstevel@tonic-gate  * Determine whether and old pathname exists within a search path string,
1730Sstevel@tonic-gate  * without a new pathname, i.e., does the search path string contain "/usr/lib"
1740Sstevel@tonic-gate  * but not "/lib".  If so, add the new pathname before the old pathname.  For
1750Sstevel@tonic-gate  * example, convert:
1760Sstevel@tonic-gate  *
1770Sstevel@tonic-gate  *	/local/lib:/opt/sfw/lib:/usr/lib
1780Sstevel@tonic-gate  * to:
1790Sstevel@tonic-gate  *	/local/lib:/opt/sfw/lib:/lib:/usr/lib
1800Sstevel@tonic-gate  */
1810Sstevel@tonic-gate const char *
conv_config_upm(const char * str,const char * old,const char * new,size_t newlen)1821618Srie conv_config_upm(const char *str, const char *old, const char *new,
1830Sstevel@tonic-gate     size_t newlen)
1840Sstevel@tonic-gate {
1850Sstevel@tonic-gate 	const char	*curstr, *ptr;
1860Sstevel@tonic-gate 	const char	*curold = 0, *curnew = 0;
1870Sstevel@tonic-gate 	const char	*ptrold = old, * ptrnew = new;
1880Sstevel@tonic-gate 	int		chkold = 1, chknew = 1;
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate 	for (curstr = ptr = str; *ptr; ptr++) {
1910Sstevel@tonic-gate 		if (*ptr == ':') {
1920Sstevel@tonic-gate 			/*
1930Sstevel@tonic-gate 			 * We've come to the end of a token within the string.
1940Sstevel@tonic-gate 			 */
1950Sstevel@tonic-gate 			if ((uintptr_t)ptr - (uintptr_t)curstr) {
1960Sstevel@tonic-gate 				/*
1970Sstevel@tonic-gate 				 * If the old or new string checking is still
1980Sstevel@tonic-gate 				 * enabled, we've found a match.
1990Sstevel@tonic-gate 				 */
2000Sstevel@tonic-gate 				if (chkold)
2010Sstevel@tonic-gate 					curold = curstr;
2020Sstevel@tonic-gate 				if (chknew)
2030Sstevel@tonic-gate 					curnew = curstr;
2040Sstevel@tonic-gate 			}
2050Sstevel@tonic-gate 			curstr = (char *)(ptr + 1);
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate 			/*
2080Sstevel@tonic-gate 			 * If an old or new string hasn't yet been matched,
2090Sstevel@tonic-gate 			 * re-enable the checking for either.
2100Sstevel@tonic-gate 			 */
2110Sstevel@tonic-gate 			if (curold == 0) {
2120Sstevel@tonic-gate 				ptrold = old;
2130Sstevel@tonic-gate 				chkold = 1;
2140Sstevel@tonic-gate 			}
2150Sstevel@tonic-gate 			if (curnew == 0) {
2160Sstevel@tonic-gate 				ptrnew = new;
2170Sstevel@tonic-gate 				chknew = 1;
2180Sstevel@tonic-gate 			}
2190Sstevel@tonic-gate 			continue;
2200Sstevel@tonic-gate 		}
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 		/*
2230Sstevel@tonic-gate 		 * Determine if the current token matches the old or new string.
2240Sstevel@tonic-gate 		 * If not, disable the checking for each string.
2250Sstevel@tonic-gate 		 */
2260Sstevel@tonic-gate 		if (chkold && (*ptr != *ptrold++))
2270Sstevel@tonic-gate 			chkold = 0;
2280Sstevel@tonic-gate 		if (chknew && (*ptr != *ptrnew++))
2290Sstevel@tonic-gate 			chknew = 0;
2300Sstevel@tonic-gate 	}
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 	/*
2330Sstevel@tonic-gate 	 * We've come to the end of the string, if the old or new string
2340Sstevel@tonic-gate 	 * checking is still enabled, we've found a match.
2350Sstevel@tonic-gate 	 */
2360Sstevel@tonic-gate 	if ((uintptr_t)ptr - (uintptr_t)curstr) {
2370Sstevel@tonic-gate 		if (chkold)
2380Sstevel@tonic-gate 			curold = curstr;
2390Sstevel@tonic-gate 		if (chknew)
2400Sstevel@tonic-gate 			curnew = curstr;
2410Sstevel@tonic-gate 	}
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate 	/*
2440Sstevel@tonic-gate 	 * If an old string hasn't been found, or it has and a new string has
2450Sstevel@tonic-gate 	 * been found, return the original string.
2460Sstevel@tonic-gate 	 */
2470Sstevel@tonic-gate 	if ((curold == 0) || curnew)
2480Sstevel@tonic-gate 		return (str);
2490Sstevel@tonic-gate 	else {
2500Sstevel@tonic-gate 		char	*newstr;
2510Sstevel@tonic-gate 		size_t	len;
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate 		/*
2540Sstevel@tonic-gate 		 * Allocate a new string, enlarged to accommodate the new string
2550Sstevel@tonic-gate 		 * that will be inserted, and an associated separator.
2560Sstevel@tonic-gate 		 */
2570Sstevel@tonic-gate 		if ((curstr = malloc(newlen + 2 +
2580Sstevel@tonic-gate 		    (uintptr_t)ptr - (uintptr_t)str)) == 0)
2590Sstevel@tonic-gate 			return (str);
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 		newstr = (char *)curstr;
2620Sstevel@tonic-gate 		for (len = (uintptr_t)curold - (uintptr_t)str; len; len--)
2630Sstevel@tonic-gate 			*(newstr++) = *(str++);		/* copy up to */
2640Sstevel@tonic-gate 							/*    insertion point */
2650Sstevel@tonic-gate 		for (len = newlen; len; len--)
2660Sstevel@tonic-gate 			*(newstr++) = *(new++);		/* add new string and */
2670Sstevel@tonic-gate 		*(newstr++) = ':';			/*    separator */
2680Sstevel@tonic-gate 		for (len = (uintptr_t)ptr - (uintptr_t)str; len; len--)
2690Sstevel@tonic-gate 			*(newstr++) = *(str++);		/* add remaining */
2700Sstevel@tonic-gate 		*(newstr++) = '\0';			/*	string */
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate 		return (curstr);
2730Sstevel@tonic-gate 	}
2740Sstevel@tonic-gate }
275