xref: /onnv-gate/usr/src/cmd/sgs/libconv/common/config.c (revision 1618:8c9a4f31d225)
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*1618Srie  * Common Development and Distribution License (the "License").
6*1618Srie  * 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  */
21*1618Srie 
220Sstevel@tonic-gate /*
23*1618Srie  * 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*1618Srie #define	FEATSZ	MSG_GBL_OSQBRKT_SIZE + \
360Sstevel@tonic-gate 		MSG_CONF_EDLIBPATH_SIZE + \
37*1618Srie 		MSG_CONF_ESLIBPATH_SIZE + \
380Sstevel@tonic-gate 		MSG_CONF_ADLIBPATH_SIZE + \
390Sstevel@tonic-gate 		MSG_CONF_ASLIBPATH_SIZE + \
400Sstevel@tonic-gate 		MSG_CONF_DIRCFG_SIZE + \
410Sstevel@tonic-gate 		MSG_CONF_OBJALT_SIZE + \
42*1618Srie 		MSG_CONF_MEMRESV_SIZE + \
430Sstevel@tonic-gate 		MSG_CONF_ENVS_SIZE + \
44*1618Srie 		MSG_CONF_FLTR_SIZE + \
45*1618Srie 		CONV_INV_STRSIZE + MSG_GBL_CSQBRKT_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 *
51*1618Srie conv_config_feat(int features)
520Sstevel@tonic-gate {
53*1618Srie 	static	char	string[FEATSZ];
54*1618Srie 	static Val_desc	vda[] = {
55*1618Srie 		{ CONF_EDLIBPATH,	MSG_ORIG(MSG_CONF_EDLIBPATH) },
56*1618Srie 		{ CONF_ESLIBPATH,	MSG_ORIG(MSG_CONF_ESLIBPATH) },
57*1618Srie 		{ CONF_ADLIBPATH,	MSG_ORIG(MSG_CONF_ADLIBPATH) },
58*1618Srie 		{ CONF_ASLIBPATH,	MSG_ORIG(MSG_CONF_ASLIBPATH) },
59*1618Srie 		{ CONF_DIRCFG,		MSG_ORIG(MSG_CONF_DIRCFG) },
60*1618Srie 		{ CONF_OBJALT,		MSG_ORIG(MSG_CONF_OBJALT) },
61*1618Srie 		{ CONF_MEMRESV,		MSG_ORIG(MSG_CONF_MEMRESV) },
62*1618Srie 		{ CONF_ENVS,		MSG_ORIG(MSG_CONF_ENVS) },
63*1618Srie 		{ CONF_FLTR,		MSG_ORIG(MSG_CONF_FLTR) },
64*1618Srie 		{ 0,			0 }
65*1618Srie 	};
660Sstevel@tonic-gate 
67*1618Srie 	(void) strlcpy(string, MSG_ORIG(MSG_GBL_OSQBRKT), FEATSZ);
68*1618Srie 	if (conv_expn_field(string, FEATSZ, vda, features,
69*1618Srie 	    features, 0, 0))
70*1618Srie 		(void) strlcat(string, MSG_ORIG(MSG_GBL_CSQBRKT), FEATSZ);
710Sstevel@tonic-gate 
720Sstevel@tonic-gate 	return ((const char *)string);
730Sstevel@tonic-gate }
740Sstevel@tonic-gate 
750Sstevel@tonic-gate #define	FLAGSZ	MSG_GBL_OSQBRKT_SIZE + \
760Sstevel@tonic-gate 		MSG_CONF_DIRENT_SIZE + \
77*1618Srie 		MSG_CONF_ALLENTS_SIZE + \
780Sstevel@tonic-gate 		MSG_CONF_NOEXIST_SIZE + \
790Sstevel@tonic-gate 		MSG_CONF_EXEC_SIZE + \
800Sstevel@tonic-gate 		MSG_CONF_ALTER_SIZE + \
81*1618Srie 		MSG_CONF_OPTIONAL_SIZE + \
820Sstevel@tonic-gate 		MSG_CONF_DUMP_SIZE + \
830Sstevel@tonic-gate 		MSG_CONF_REALPATH_SIZE + \
84*1618Srie 		MSG_CONF_NOALTER_SIZE + \
850Sstevel@tonic-gate 		MSG_CONF_GROUP_SIZE + \
860Sstevel@tonic-gate 		MSG_CONF_APP_SIZE + \
870Sstevel@tonic-gate 		MSG_CONF_CMDLINE_SIZE + \
880Sstevel@tonic-gate 		MSG_CONF_FILTER_SIZE + \
890Sstevel@tonic-gate 		MSG_CONF_FILTEE_SIZE + \
90*1618Srie 		CONV_INV_STRSIZE + MSG_GBL_CSQBRKT_SIZE
910Sstevel@tonic-gate 
920Sstevel@tonic-gate /*
930Sstevel@tonic-gate  * String conversion routine for object flags.
940Sstevel@tonic-gate  */
950Sstevel@tonic-gate const char *
960Sstevel@tonic-gate conv_config_obj(ushort_t flags)
970Sstevel@tonic-gate {
98*1618Srie 	static char	string[FLAGSZ];
99*1618Srie 	static Val_desc vda[] = {
100*1618Srie 		{ RTC_OBJ_DIRENT,	MSG_ORIG(MSG_CONF_DIRENT) },
101*1618Srie 		{ RTC_OBJ_ALLENTS,	MSG_ORIG(MSG_CONF_ALLENTS) },
102*1618Srie 		{ RTC_OBJ_NOEXIST,	MSG_ORIG(MSG_CONF_NOEXIST) },
103*1618Srie 		{ RTC_OBJ_EXEC,		MSG_ORIG(MSG_CONF_EXEC) },
104*1618Srie 		{ RTC_OBJ_ALTER,	MSG_ORIG(MSG_CONF_ALTER) },
105*1618Srie 		{ RTC_OBJ_DUMP,		MSG_ORIG(MSG_CONF_DUMP) },
106*1618Srie 		{ RTC_OBJ_NOALTER,	MSG_ORIG(MSG_CONF_NOALTER) },
107*1618Srie 		{ RTC_OBJ_REALPTH,	MSG_ORIG(MSG_CONF_REALPATH) },
108*1618Srie 		{ RTC_OBJ_GROUP,	MSG_ORIG(MSG_CONF_GROUP) },
109*1618Srie 		{ RTC_OBJ_APP,		MSG_ORIG(MSG_CONF_APP) },
110*1618Srie 		{ RTC_OBJ_CMDLINE,	MSG_ORIG(MSG_CONF_CMDLINE) },
111*1618Srie 		{ RTC_OBJ_FILTER,	MSG_ORIG(MSG_CONF_FILTER) },
112*1618Srie 		{ RTC_OBJ_FILTEE,	MSG_ORIG(MSG_CONF_FILTEE) },
113*1618Srie 		{ 0,			0 }
114*1618Srie 	};
115*1618Srie 	ushort_t	_flags = flags;
1160Sstevel@tonic-gate 
117*1618Srie 	if ((flags == 0) || (flags == RTC_OBJ_OPTINAL))
118*1618Srie 		return (MSG_ORIG(MSG_GBL_NULL));
119*1618Srie 
120*1618Srie 	(void) strlcpy(string, MSG_ORIG(MSG_GBL_OSQBRKT), FLAGSZ);
121*1618Srie 
122*1618Srie 	/*
123*1618Srie 	 * Print an alternative-optional object simply as optional.
124*1618Srie 	 */
125*1618Srie 	if ((flags & (RTC_OBJ_ALTER | RTC_OBJ_OPTINAL)) ==
126*1618Srie 	    (RTC_OBJ_ALTER | RTC_OBJ_OPTINAL)) {
127*1618Srie 		if (strlcat(string, MSG_ORIG(MSG_CONF_OPTIONAL),
128*1618Srie 		    FLAGSZ) >= FLAGSZ)
129*1618Srie 			return (conv_invalid_val(string, FLAGSZ, flags, 0));
130*1618Srie 		flags = _flags &= ~(RTC_OBJ_ALTER | RTC_OBJ_OPTINAL);
1310Sstevel@tonic-gate 	}
132*1618Srie 	flags = _flags &= ~RTC_OBJ_OPTINAL;
1330Sstevel@tonic-gate 
134*1618Srie 	if (conv_expn_field(string, FLAGSZ, vda, flags, _flags, 0, 0))
135*1618Srie 		(void) strlcat(string, MSG_ORIG(MSG_GBL_CSQBRKT), FLAGSZ);
1360Sstevel@tonic-gate 
137*1618Srie 	return ((const char *)string);
1380Sstevel@tonic-gate }
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate /*
1410Sstevel@tonic-gate  * Determine whether and old pathname exists within a search path string,
1420Sstevel@tonic-gate  * without a new pathname, i.e., does the search path string contain "/usr/lib"
1430Sstevel@tonic-gate  * but not "/lib".  If so, add the new pathname before the old pathname.  For
1440Sstevel@tonic-gate  * example, convert:
1450Sstevel@tonic-gate  *
1460Sstevel@tonic-gate  *	/local/lib:/opt/sfw/lib:/usr/lib
1470Sstevel@tonic-gate  * to:
1480Sstevel@tonic-gate  *	/local/lib:/opt/sfw/lib:/lib:/usr/lib
1490Sstevel@tonic-gate  */
1500Sstevel@tonic-gate const char *
151*1618Srie conv_config_upm(const char *str, const char *old, const char *new,
1520Sstevel@tonic-gate     size_t newlen)
1530Sstevel@tonic-gate {
1540Sstevel@tonic-gate 	const char	*curstr, *ptr;
1550Sstevel@tonic-gate 	const char	*curold = 0, *curnew = 0;
1560Sstevel@tonic-gate 	const char	*ptrold = old, * ptrnew = new;
1570Sstevel@tonic-gate 	int		chkold = 1, chknew = 1;
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 	for (curstr = ptr = str; *ptr; ptr++) {
1600Sstevel@tonic-gate 		if (*ptr == ':') {
1610Sstevel@tonic-gate 			/*
1620Sstevel@tonic-gate 			 * We've come to the end of a token within the string.
1630Sstevel@tonic-gate 			 */
1640Sstevel@tonic-gate 			if ((uintptr_t)ptr - (uintptr_t)curstr) {
1650Sstevel@tonic-gate 				/*
1660Sstevel@tonic-gate 				 * If the old or new string checking is still
1670Sstevel@tonic-gate 				 * enabled, we've found a match.
1680Sstevel@tonic-gate 				 */
1690Sstevel@tonic-gate 				if (chkold)
1700Sstevel@tonic-gate 					curold = curstr;
1710Sstevel@tonic-gate 				if (chknew)
1720Sstevel@tonic-gate 					curnew = curstr;
1730Sstevel@tonic-gate 			}
1740Sstevel@tonic-gate 			curstr = (char *)(ptr + 1);
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate 			/*
1770Sstevel@tonic-gate 			 * If an old or new string hasn't yet been matched,
1780Sstevel@tonic-gate 			 * re-enable the checking for either.
1790Sstevel@tonic-gate 			 */
1800Sstevel@tonic-gate 			if (curold == 0) {
1810Sstevel@tonic-gate 				ptrold = old;
1820Sstevel@tonic-gate 				chkold = 1;
1830Sstevel@tonic-gate 			}
1840Sstevel@tonic-gate 			if (curnew == 0) {
1850Sstevel@tonic-gate 				ptrnew = new;
1860Sstevel@tonic-gate 				chknew = 1;
1870Sstevel@tonic-gate 			}
1880Sstevel@tonic-gate 			continue;
1890Sstevel@tonic-gate 		}
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 		/*
1920Sstevel@tonic-gate 		 * Determine if the current token matches the old or new string.
1930Sstevel@tonic-gate 		 * If not, disable the checking for each string.
1940Sstevel@tonic-gate 		 */
1950Sstevel@tonic-gate 		if (chkold && (*ptr != *ptrold++))
1960Sstevel@tonic-gate 			chkold = 0;
1970Sstevel@tonic-gate 		if (chknew && (*ptr != *ptrnew++))
1980Sstevel@tonic-gate 			chknew = 0;
1990Sstevel@tonic-gate 	}
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate 	/*
2020Sstevel@tonic-gate 	 * We've come to the end of the string, if the old or new string
2030Sstevel@tonic-gate 	 * checking is still enabled, we've found a match.
2040Sstevel@tonic-gate 	 */
2050Sstevel@tonic-gate 	if ((uintptr_t)ptr - (uintptr_t)curstr) {
2060Sstevel@tonic-gate 		if (chkold)
2070Sstevel@tonic-gate 			curold = curstr;
2080Sstevel@tonic-gate 		if (chknew)
2090Sstevel@tonic-gate 			curnew = curstr;
2100Sstevel@tonic-gate 	}
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate 	/*
2130Sstevel@tonic-gate 	 * If an old string hasn't been found, or it has and a new string has
2140Sstevel@tonic-gate 	 * been found, return the original string.
2150Sstevel@tonic-gate 	 */
2160Sstevel@tonic-gate 	if ((curold == 0) || curnew)
2170Sstevel@tonic-gate 		return (str);
2180Sstevel@tonic-gate 	else {
2190Sstevel@tonic-gate 		char	*newstr;
2200Sstevel@tonic-gate 		size_t	len;
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 		/*
2230Sstevel@tonic-gate 		 * Allocate a new string, enlarged to accommodate the new string
2240Sstevel@tonic-gate 		 * that will be inserted, and an associated separator.
2250Sstevel@tonic-gate 		 */
2260Sstevel@tonic-gate 		if ((curstr = malloc(newlen + 2 +
2270Sstevel@tonic-gate 		    (uintptr_t)ptr - (uintptr_t)str)) == 0)
2280Sstevel@tonic-gate 			return (str);
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 		newstr = (char *)curstr;
2310Sstevel@tonic-gate 		for (len = (uintptr_t)curold - (uintptr_t)str; len; len--)
2320Sstevel@tonic-gate 			*(newstr++) = *(str++);		/* copy up to */
2330Sstevel@tonic-gate 							/*    insertion point */
2340Sstevel@tonic-gate 		for (len = newlen; len; len--)
2350Sstevel@tonic-gate 			*(newstr++) = *(new++);		/* add new string and */
2360Sstevel@tonic-gate 		*(newstr++) = ':';			/*    separator */
2370Sstevel@tonic-gate 		for (len = (uintptr_t)ptr - (uintptr_t)str; len; len--)
2380Sstevel@tonic-gate 			*(newstr++) = *(str++);		/* add remaining */
2390Sstevel@tonic-gate 		*(newstr++) = '\0';			/*	string */
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 		return (curstr);
2420Sstevel@tonic-gate 	}
2430Sstevel@tonic-gate }
244