xref: /onnv-gate/usr/src/cmd/sgs/libconv/common/config.c (revision 2352:9cdfed81bb1c)
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 /*
231618Srie  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include	<stdlib.h>
290Sstevel@tonic-gate #include	<sys/types.h>
300Sstevel@tonic-gate #include	<string.h>
310Sstevel@tonic-gate #include	"rtc.h"
320Sstevel@tonic-gate #include	"_conv.h"
330Sstevel@tonic-gate #include	"config_msg.h"
340Sstevel@tonic-gate 
35*2352Sab196087 #define	FEATSZ	CONV_EXPN_FIELD_DEF_PREFIX_SIZE + \
36*2352Sab196087 		MSG_CONF_EDLIBPATH_SIZE + CONV_EXPN_FIELD_DEF_SEP_SIZE + \
37*2352Sab196087 		MSG_CONF_ESLIBPATH_SIZE + CONV_EXPN_FIELD_DEF_SEP_SIZE + \
38*2352Sab196087 		MSG_CONF_ADLIBPATH_SIZE + CONV_EXPN_FIELD_DEF_SEP_SIZE + \
39*2352Sab196087 		MSG_CONF_ASLIBPATH_SIZE + CONV_EXPN_FIELD_DEF_SEP_SIZE + \
40*2352Sab196087 		MSG_CONF_DIRCFG_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
41*2352Sab196087 		MSG_CONF_OBJALT_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
42*2352Sab196087 		MSG_CONF_MEMRESV_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
43*2352Sab196087 		MSG_CONF_ENVS_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
44*2352Sab196087 		MSG_CONF_FLTR_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
45*2352Sab196087 		CONV_INV_STRSIZE + CONV_EXPN_FIELD_DEF_SUFFIX_SIZE
460Sstevel@tonic-gate 
470Sstevel@tonic-gate /*
480Sstevel@tonic-gate  * String conversion routine for configuration file information.
490Sstevel@tonic-gate  */
500Sstevel@tonic-gate const char *
511618Srie conv_config_feat(int features)
520Sstevel@tonic-gate {
531618Srie 	static	char	string[FEATSZ];
541618Srie 	static Val_desc	vda[] = {
551618Srie 		{ CONF_EDLIBPATH,	MSG_ORIG(MSG_CONF_EDLIBPATH) },
561618Srie 		{ CONF_ESLIBPATH,	MSG_ORIG(MSG_CONF_ESLIBPATH) },
571618Srie 		{ CONF_ADLIBPATH,	MSG_ORIG(MSG_CONF_ADLIBPATH) },
581618Srie 		{ CONF_ASLIBPATH,	MSG_ORIG(MSG_CONF_ASLIBPATH) },
591618Srie 		{ CONF_DIRCFG,		MSG_ORIG(MSG_CONF_DIRCFG) },
601618Srie 		{ CONF_OBJALT,		MSG_ORIG(MSG_CONF_OBJALT) },
611618Srie 		{ CONF_MEMRESV,		MSG_ORIG(MSG_CONF_MEMRESV) },
621618Srie 		{ CONF_ENVS,		MSG_ORIG(MSG_CONF_ENVS) },
631618Srie 		{ CONF_FLTR,		MSG_ORIG(MSG_CONF_FLTR) },
641618Srie 		{ 0,			0 }
651618Srie 	};
66*2352Sab196087 	static CONV_EXPN_FIELD_ARG conv_arg = { string, sizeof (string), vda };
670Sstevel@tonic-gate 
68*2352Sab196087 	conv_arg.oflags = conv_arg.rflags = features;
69*2352Sab196087 	(void) conv_expn_field(&conv_arg);
700Sstevel@tonic-gate 
710Sstevel@tonic-gate 	return ((const char *)string);
720Sstevel@tonic-gate }
730Sstevel@tonic-gate 
74*2352Sab196087 #define	FLAGSZ	CONV_EXPN_FIELD_DEF_PREFIX_SIZE + \
75*2352Sab196087 		MSG_CONF_DIRENT_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
76*2352Sab196087 		MSG_CONF_ALLENTS_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
77*2352Sab196087 		MSG_CONF_NOEXIST_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
78*2352Sab196087 		MSG_CONF_EXEC_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
79*2352Sab196087 		MSG_CONF_ALTER_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
80*2352Sab196087 		MSG_CONF_OPTIONAL_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
81*2352Sab196087 		MSG_CONF_DUMP_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
82*2352Sab196087 		MSG_CONF_REALPATH_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
83*2352Sab196087 		MSG_CONF_NOALTER_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
84*2352Sab196087 		MSG_CONF_GROUP_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
85*2352Sab196087 		MSG_CONF_APP_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
86*2352Sab196087 		MSG_CONF_CMDLINE_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
87*2352Sab196087 		MSG_CONF_FILTER_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
88*2352Sab196087 		MSG_CONF_FILTEE_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
89*2352Sab196087 		CONV_INV_STRSIZE + CONV_EXPN_FIELD_DEF_SUFFIX_SIZE
900Sstevel@tonic-gate 
910Sstevel@tonic-gate /*
920Sstevel@tonic-gate  * String conversion routine for object flags.
930Sstevel@tonic-gate  */
940Sstevel@tonic-gate const char *
950Sstevel@tonic-gate conv_config_obj(ushort_t flags)
960Sstevel@tonic-gate {
971618Srie 	static char	string[FLAGSZ];
981618Srie 	static Val_desc vda[] = {
991618Srie 		{ RTC_OBJ_DIRENT,	MSG_ORIG(MSG_CONF_DIRENT) },
1001618Srie 		{ RTC_OBJ_ALLENTS,	MSG_ORIG(MSG_CONF_ALLENTS) },
1011618Srie 		{ RTC_OBJ_NOEXIST,	MSG_ORIG(MSG_CONF_NOEXIST) },
1021618Srie 		{ RTC_OBJ_EXEC,		MSG_ORIG(MSG_CONF_EXEC) },
1031618Srie 		{ RTC_OBJ_ALTER,	MSG_ORIG(MSG_CONF_ALTER) },
1041618Srie 		{ RTC_OBJ_DUMP,		MSG_ORIG(MSG_CONF_DUMP) },
1051618Srie 		{ RTC_OBJ_NOALTER,	MSG_ORIG(MSG_CONF_NOALTER) },
1061618Srie 		{ RTC_OBJ_REALPTH,	MSG_ORIG(MSG_CONF_REALPATH) },
1071618Srie 		{ RTC_OBJ_GROUP,	MSG_ORIG(MSG_CONF_GROUP) },
1081618Srie 		{ RTC_OBJ_APP,		MSG_ORIG(MSG_CONF_APP) },
1091618Srie 		{ RTC_OBJ_CMDLINE,	MSG_ORIG(MSG_CONF_CMDLINE) },
1101618Srie 		{ RTC_OBJ_FILTER,	MSG_ORIG(MSG_CONF_FILTER) },
1111618Srie 		{ RTC_OBJ_FILTEE,	MSG_ORIG(MSG_CONF_FILTEE) },
1121618Srie 		{ 0,			0 }
1131618Srie 	};
114*2352Sab196087 	static const char *leading_str_arr[2];
115*2352Sab196087 	static CONV_EXPN_FIELD_ARG conv_arg = { string, sizeof (string), vda,
116*2352Sab196087 		leading_str_arr };
117*2352Sab196087 
118*2352Sab196087 	const char **lstr = leading_str_arr;
1190Sstevel@tonic-gate 
1201618Srie 	if ((flags == 0) || (flags == RTC_OBJ_OPTINAL))
1211618Srie 		return (MSG_ORIG(MSG_GBL_NULL));
1221618Srie 
123*2352Sab196087 	conv_arg.rflags = flags;
1241618Srie 
1251618Srie 	/*
1261618Srie 	 * Print an alternative-optional object simply as optional.
1271618Srie 	 */
1281618Srie 	if ((flags & (RTC_OBJ_ALTER | RTC_OBJ_OPTINAL)) ==
1291618Srie 	    (RTC_OBJ_ALTER | RTC_OBJ_OPTINAL)) {
130*2352Sab196087 		*lstr++ = MSG_ORIG(MSG_CONF_OPTIONAL);
131*2352Sab196087 		conv_arg.rflags &= ~(RTC_OBJ_ALTER | RTC_OBJ_OPTINAL);
1320Sstevel@tonic-gate 	}
133*2352Sab196087 	*lstr = NULL;
134*2352Sab196087 	conv_arg.oflags = conv_arg.rflags &= ~RTC_OBJ_OPTINAL;
1350Sstevel@tonic-gate 
136*2352Sab196087 	(void) conv_expn_field(&conv_arg);
1370Sstevel@tonic-gate 
1381618Srie 	return ((const char *)string);
1390Sstevel@tonic-gate }
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate /*
1420Sstevel@tonic-gate  * Determine whether and old pathname exists within a search path string,
1430Sstevel@tonic-gate  * without a new pathname, i.e., does the search path string contain "/usr/lib"
1440Sstevel@tonic-gate  * but not "/lib".  If so, add the new pathname before the old pathname.  For
1450Sstevel@tonic-gate  * example, convert:
1460Sstevel@tonic-gate  *
1470Sstevel@tonic-gate  *	/local/lib:/opt/sfw/lib:/usr/lib
1480Sstevel@tonic-gate  * to:
1490Sstevel@tonic-gate  *	/local/lib:/opt/sfw/lib:/lib:/usr/lib
1500Sstevel@tonic-gate  */
1510Sstevel@tonic-gate const char *
1521618Srie conv_config_upm(const char *str, const char *old, const char *new,
1530Sstevel@tonic-gate     size_t newlen)
1540Sstevel@tonic-gate {
1550Sstevel@tonic-gate 	const char	*curstr, *ptr;
1560Sstevel@tonic-gate 	const char	*curold = 0, *curnew = 0;
1570Sstevel@tonic-gate 	const char	*ptrold = old, * ptrnew = new;
1580Sstevel@tonic-gate 	int		chkold = 1, chknew = 1;
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate 	for (curstr = ptr = str; *ptr; ptr++) {
1610Sstevel@tonic-gate 		if (*ptr == ':') {
1620Sstevel@tonic-gate 			/*
1630Sstevel@tonic-gate 			 * We've come to the end of a token within the string.
1640Sstevel@tonic-gate 			 */
1650Sstevel@tonic-gate 			if ((uintptr_t)ptr - (uintptr_t)curstr) {
1660Sstevel@tonic-gate 				/*
1670Sstevel@tonic-gate 				 * If the old or new string checking is still
1680Sstevel@tonic-gate 				 * enabled, we've found a match.
1690Sstevel@tonic-gate 				 */
1700Sstevel@tonic-gate 				if (chkold)
1710Sstevel@tonic-gate 					curold = curstr;
1720Sstevel@tonic-gate 				if (chknew)
1730Sstevel@tonic-gate 					curnew = curstr;
1740Sstevel@tonic-gate 			}
1750Sstevel@tonic-gate 			curstr = (char *)(ptr + 1);
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 			/*
1780Sstevel@tonic-gate 			 * If an old or new string hasn't yet been matched,
1790Sstevel@tonic-gate 			 * re-enable the checking for either.
1800Sstevel@tonic-gate 			 */
1810Sstevel@tonic-gate 			if (curold == 0) {
1820Sstevel@tonic-gate 				ptrold = old;
1830Sstevel@tonic-gate 				chkold = 1;
1840Sstevel@tonic-gate 			}
1850Sstevel@tonic-gate 			if (curnew == 0) {
1860Sstevel@tonic-gate 				ptrnew = new;
1870Sstevel@tonic-gate 				chknew = 1;
1880Sstevel@tonic-gate 			}
1890Sstevel@tonic-gate 			continue;
1900Sstevel@tonic-gate 		}
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate 		/*
1930Sstevel@tonic-gate 		 * Determine if the current token matches the old or new string.
1940Sstevel@tonic-gate 		 * If not, disable the checking for each string.
1950Sstevel@tonic-gate 		 */
1960Sstevel@tonic-gate 		if (chkold && (*ptr != *ptrold++))
1970Sstevel@tonic-gate 			chkold = 0;
1980Sstevel@tonic-gate 		if (chknew && (*ptr != *ptrnew++))
1990Sstevel@tonic-gate 			chknew = 0;
2000Sstevel@tonic-gate 	}
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate 	/*
2030Sstevel@tonic-gate 	 * We've come to the end of the string, if the old or new string
2040Sstevel@tonic-gate 	 * checking is still enabled, we've found a match.
2050Sstevel@tonic-gate 	 */
2060Sstevel@tonic-gate 	if ((uintptr_t)ptr - (uintptr_t)curstr) {
2070Sstevel@tonic-gate 		if (chkold)
2080Sstevel@tonic-gate 			curold = curstr;
2090Sstevel@tonic-gate 		if (chknew)
2100Sstevel@tonic-gate 			curnew = curstr;
2110Sstevel@tonic-gate 	}
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate 	/*
2140Sstevel@tonic-gate 	 * If an old string hasn't been found, or it has and a new string has
2150Sstevel@tonic-gate 	 * been found, return the original string.
2160Sstevel@tonic-gate 	 */
2170Sstevel@tonic-gate 	if ((curold == 0) || curnew)
2180Sstevel@tonic-gate 		return (str);
2190Sstevel@tonic-gate 	else {
2200Sstevel@tonic-gate 		char	*newstr;
2210Sstevel@tonic-gate 		size_t	len;
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate 		/*
2240Sstevel@tonic-gate 		 * Allocate a new string, enlarged to accommodate the new string
2250Sstevel@tonic-gate 		 * that will be inserted, and an associated separator.
2260Sstevel@tonic-gate 		 */
2270Sstevel@tonic-gate 		if ((curstr = malloc(newlen + 2 +
2280Sstevel@tonic-gate 		    (uintptr_t)ptr - (uintptr_t)str)) == 0)
2290Sstevel@tonic-gate 			return (str);
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 		newstr = (char *)curstr;
2320Sstevel@tonic-gate 		for (len = (uintptr_t)curold - (uintptr_t)str; len; len--)
2330Sstevel@tonic-gate 			*(newstr++) = *(str++);		/* copy up to */
2340Sstevel@tonic-gate 							/*    insertion point */
2350Sstevel@tonic-gate 		for (len = newlen; len; len--)
2360Sstevel@tonic-gate 			*(newstr++) = *(new++);		/* add new string and */
2370Sstevel@tonic-gate 		*(newstr++) = ':';			/*    separator */
2380Sstevel@tonic-gate 		for (len = (uintptr_t)ptr - (uintptr_t)str; len; len--)
2390Sstevel@tonic-gate 			*(newstr++) = *(str++);		/* add remaining */
2400Sstevel@tonic-gate 		*(newstr++) = '\0';			/*	string */
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 		return (curstr);
2430Sstevel@tonic-gate 	}
2440Sstevel@tonic-gate }
245