xref: /onnv-gate/usr/src/cmd/sgs/libconv/common/config.c (revision 5152:fa5ab16f3606)
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 /*
234734Sab196087  * Copyright 2007 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 
352352Sab196087 #define	FEATSZ	CONV_EXPN_FIELD_DEF_PREFIX_SIZE + \
362352Sab196087 		MSG_CONF_EDLIBPATH_SIZE + CONV_EXPN_FIELD_DEF_SEP_SIZE + \
372352Sab196087 		MSG_CONF_ESLIBPATH_SIZE + CONV_EXPN_FIELD_DEF_SEP_SIZE + \
382352Sab196087 		MSG_CONF_ADLIBPATH_SIZE + CONV_EXPN_FIELD_DEF_SEP_SIZE + \
392352Sab196087 		MSG_CONF_ASLIBPATH_SIZE + CONV_EXPN_FIELD_DEF_SEP_SIZE + \
402352Sab196087 		MSG_CONF_DIRCFG_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
412352Sab196087 		MSG_CONF_OBJALT_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
422352Sab196087 		MSG_CONF_MEMRESV_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
432352Sab196087 		MSG_CONF_ENVS_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
442352Sab196087 		MSG_CONF_FLTR_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
454734Sab196087 		CONV_INV_BUFSIZE + CONV_EXPN_FIELD_DEF_SUFFIX_SIZE
464734Sab196087 
474734Sab196087 /*
484734Sab196087  * Ensure that Conv_config_feat_buf_t is large enough:
494734Sab196087  *
504734Sab196087  * FEATSZ is the real minimum size of the buffer required by conv_config_feat().
514734Sab196087  * However, Conv_config_feat_buf_t uses CONV_CONFIG_FEAT_BUFSIZE to set the
524734Sab196087  * buffer size. We do things this way because the definition of FEATSZ uses
534734Sab196087  * information that is not available in the environment of other programs
544734Sab196087  * that include the conv.h header file.
554734Sab196087  */
56*5152Sab196087 #if (CONV_CONFIG_FEAT_BUFSIZE != FEATSZ) && !defined(__lint)
57*5152Sab196087 #define	REPORT_BUFSIZE FEATSZ
58*5152Sab196087 #include "report_bufsize.h"
59*5152Sab196087 #error "CONV_CONFIG_FEAT_BUFSIZE does not match FEATSZ"
604734Sab196087 #endif
610Sstevel@tonic-gate 
620Sstevel@tonic-gate /*
630Sstevel@tonic-gate  * String conversion routine for configuration file information.
640Sstevel@tonic-gate  */
650Sstevel@tonic-gate const char *
664734Sab196087 conv_config_feat(int features, Conv_config_feat_buf_t *config_feat_buf)
670Sstevel@tonic-gate {
681618Srie 	static Val_desc	vda[] = {
691618Srie 		{ CONF_EDLIBPATH,	MSG_ORIG(MSG_CONF_EDLIBPATH) },
701618Srie 		{ CONF_ESLIBPATH,	MSG_ORIG(MSG_CONF_ESLIBPATH) },
711618Srie 		{ CONF_ADLIBPATH,	MSG_ORIG(MSG_CONF_ADLIBPATH) },
721618Srie 		{ CONF_ASLIBPATH,	MSG_ORIG(MSG_CONF_ASLIBPATH) },
731618Srie 		{ CONF_DIRCFG,		MSG_ORIG(MSG_CONF_DIRCFG) },
741618Srie 		{ CONF_OBJALT,		MSG_ORIG(MSG_CONF_OBJALT) },
751618Srie 		{ CONF_MEMRESV,		MSG_ORIG(MSG_CONF_MEMRESV) },
761618Srie 		{ CONF_ENVS,		MSG_ORIG(MSG_CONF_ENVS) },
771618Srie 		{ CONF_FLTR,		MSG_ORIG(MSG_CONF_FLTR) },
781618Srie 		{ 0,			0 }
791618Srie 	};
804734Sab196087 	static CONV_EXPN_FIELD_ARG conv_arg = {
814734Sab196087 	    NULL, sizeof (config_feat_buf->buf), vda };
820Sstevel@tonic-gate 
834734Sab196087 	conv_arg.buf = config_feat_buf->buf;
842352Sab196087 	conv_arg.oflags = conv_arg.rflags = features;
855088Sab196087 	(void) conv_expn_field(&conv_arg, 0);
860Sstevel@tonic-gate 
874734Sab196087 	return ((const char *)config_feat_buf->buf);
880Sstevel@tonic-gate }
890Sstevel@tonic-gate 
902352Sab196087 #define	FLAGSZ	CONV_EXPN_FIELD_DEF_PREFIX_SIZE + \
912352Sab196087 		MSG_CONF_DIRENT_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
922352Sab196087 		MSG_CONF_ALLENTS_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
932352Sab196087 		MSG_CONF_NOEXIST_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
942352Sab196087 		MSG_CONF_EXEC_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
952352Sab196087 		MSG_CONF_ALTER_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
962352Sab196087 		MSG_CONF_OPTIONAL_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
972352Sab196087 		MSG_CONF_DUMP_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
982352Sab196087 		MSG_CONF_REALPATH_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
992352Sab196087 		MSG_CONF_NOALTER_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
1002352Sab196087 		MSG_CONF_GROUP_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
1012352Sab196087 		MSG_CONF_APP_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
1022352Sab196087 		MSG_CONF_CMDLINE_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
1032352Sab196087 		MSG_CONF_FILTER_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
1042352Sab196087 		MSG_CONF_FILTEE_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
1054734Sab196087 		CONV_INV_BUFSIZE + CONV_EXPN_FIELD_DEF_SUFFIX_SIZE
1064734Sab196087 
1074734Sab196087 /*
1084734Sab196087  * Ensure that Conv_config_obj_buf_t is large enough:
1094734Sab196087  *
1104734Sab196087  * FLAGSZ is the real minimum size of the buffer required by conv_config_obj().
1114734Sab196087  * However, Conv_config_obj_buf_t uses CONV_CONFIG_OBJ_BUFSIZE to set the
1124734Sab196087  * buffer size. We do things this way because the definition of FLAGSZ uses
1134734Sab196087  * information that is not available in the environment of other programs
1144734Sab196087  * that include the conv.h header file.
1154734Sab196087  */
116*5152Sab196087 #if (CONV_CONFIG_OBJ_BUFSIZE != FLAGSZ) && !defined(__lint)
117*5152Sab196087 #define	REPORT_BUFSIZE FLAGSZ
118*5152Sab196087 #include "report_bufsize.h"
119*5152Sab196087 #error "CONV_CONFIG_OBJ_BUFSIZE does not match FLAGSZ"
1204734Sab196087 #endif
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate /*
1230Sstevel@tonic-gate  * String conversion routine for object flags.
1240Sstevel@tonic-gate  */
1250Sstevel@tonic-gate const char *
1264734Sab196087 conv_config_obj(ushort_t flags, Conv_config_obj_buf_t *config_obj_buf)
1270Sstevel@tonic-gate {
1281618Srie 	static Val_desc vda[] = {
1291618Srie 		{ RTC_OBJ_DIRENT,	MSG_ORIG(MSG_CONF_DIRENT) },
1301618Srie 		{ RTC_OBJ_ALLENTS,	MSG_ORIG(MSG_CONF_ALLENTS) },
1311618Srie 		{ RTC_OBJ_NOEXIST,	MSG_ORIG(MSG_CONF_NOEXIST) },
1321618Srie 		{ RTC_OBJ_EXEC,		MSG_ORIG(MSG_CONF_EXEC) },
1331618Srie 		{ RTC_OBJ_ALTER,	MSG_ORIG(MSG_CONF_ALTER) },
1341618Srie 		{ RTC_OBJ_DUMP,		MSG_ORIG(MSG_CONF_DUMP) },
1351618Srie 		{ RTC_OBJ_NOALTER,	MSG_ORIG(MSG_CONF_NOALTER) },
1361618Srie 		{ RTC_OBJ_REALPTH,	MSG_ORIG(MSG_CONF_REALPATH) },
1371618Srie 		{ RTC_OBJ_GROUP,	MSG_ORIG(MSG_CONF_GROUP) },
1381618Srie 		{ RTC_OBJ_APP,		MSG_ORIG(MSG_CONF_APP) },
1391618Srie 		{ RTC_OBJ_CMDLINE,	MSG_ORIG(MSG_CONF_CMDLINE) },
1401618Srie 		{ RTC_OBJ_FILTER,	MSG_ORIG(MSG_CONF_FILTER) },
1411618Srie 		{ RTC_OBJ_FILTEE,	MSG_ORIG(MSG_CONF_FILTEE) },
1421618Srie 		{ 0,			0 }
1431618Srie 	};
1442352Sab196087 	static const char *leading_str_arr[2];
1454734Sab196087 	static CONV_EXPN_FIELD_ARG conv_arg = {
1464734Sab196087 	    NULL, sizeof (config_obj_buf->buf), vda, leading_str_arr };
1472352Sab196087 
1482352Sab196087 	const char **lstr = leading_str_arr;
1490Sstevel@tonic-gate 
1501618Srie 	if ((flags == 0) || (flags == RTC_OBJ_OPTINAL))
1511618Srie 		return (MSG_ORIG(MSG_GBL_NULL));
1521618Srie 
1534734Sab196087 	conv_arg.buf = config_obj_buf->buf;
1542352Sab196087 	conv_arg.rflags = flags;
1551618Srie 
1561618Srie 	/*
1571618Srie 	 * Print an alternative-optional object simply as optional.
1581618Srie 	 */
1591618Srie 	if ((flags & (RTC_OBJ_ALTER | RTC_OBJ_OPTINAL)) ==
1601618Srie 	    (RTC_OBJ_ALTER | RTC_OBJ_OPTINAL)) {
1612352Sab196087 		*lstr++ = MSG_ORIG(MSG_CONF_OPTIONAL);
1622352Sab196087 		conv_arg.rflags &= ~(RTC_OBJ_ALTER | RTC_OBJ_OPTINAL);
1630Sstevel@tonic-gate 	}
1642352Sab196087 	*lstr = NULL;
1652352Sab196087 	conv_arg.oflags = conv_arg.rflags &= ~RTC_OBJ_OPTINAL;
1660Sstevel@tonic-gate 
1675088Sab196087 	(void) conv_expn_field(&conv_arg, 0);
1680Sstevel@tonic-gate 
1694734Sab196087 	return ((const char *)config_obj_buf->buf);
1700Sstevel@tonic-gate }
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate /*
1730Sstevel@tonic-gate  * Determine whether and old pathname exists within a search path string,
1740Sstevel@tonic-gate  * without a new pathname, i.e., does the search path string contain "/usr/lib"
1750Sstevel@tonic-gate  * but not "/lib".  If so, add the new pathname before the old pathname.  For
1760Sstevel@tonic-gate  * example, convert:
1770Sstevel@tonic-gate  *
1780Sstevel@tonic-gate  *	/local/lib:/opt/sfw/lib:/usr/lib
1790Sstevel@tonic-gate  * to:
1800Sstevel@tonic-gate  *	/local/lib:/opt/sfw/lib:/lib:/usr/lib
1810Sstevel@tonic-gate  */
1820Sstevel@tonic-gate const char *
1831618Srie conv_config_upm(const char *str, const char *old, const char *new,
1840Sstevel@tonic-gate     size_t newlen)
1850Sstevel@tonic-gate {
1860Sstevel@tonic-gate 	const char	*curstr, *ptr;
1870Sstevel@tonic-gate 	const char	*curold = 0, *curnew = 0;
1880Sstevel@tonic-gate 	const char	*ptrold = old, * ptrnew = new;
1890Sstevel@tonic-gate 	int		chkold = 1, chknew = 1;
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 	for (curstr = ptr = str; *ptr; ptr++) {
1920Sstevel@tonic-gate 		if (*ptr == ':') {
1930Sstevel@tonic-gate 			/*
1940Sstevel@tonic-gate 			 * We've come to the end of a token within the string.
1950Sstevel@tonic-gate 			 */
1960Sstevel@tonic-gate 			if ((uintptr_t)ptr - (uintptr_t)curstr) {
1970Sstevel@tonic-gate 				/*
1980Sstevel@tonic-gate 				 * If the old or new string checking is still
1990Sstevel@tonic-gate 				 * enabled, we've found a match.
2000Sstevel@tonic-gate 				 */
2010Sstevel@tonic-gate 				if (chkold)
2020Sstevel@tonic-gate 					curold = curstr;
2030Sstevel@tonic-gate 				if (chknew)
2040Sstevel@tonic-gate 					curnew = curstr;
2050Sstevel@tonic-gate 			}
2060Sstevel@tonic-gate 			curstr = (char *)(ptr + 1);
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 			/*
2090Sstevel@tonic-gate 			 * If an old or new string hasn't yet been matched,
2100Sstevel@tonic-gate 			 * re-enable the checking for either.
2110Sstevel@tonic-gate 			 */
2120Sstevel@tonic-gate 			if (curold == 0) {
2130Sstevel@tonic-gate 				ptrold = old;
2140Sstevel@tonic-gate 				chkold = 1;
2150Sstevel@tonic-gate 			}
2160Sstevel@tonic-gate 			if (curnew == 0) {
2170Sstevel@tonic-gate 				ptrnew = new;
2180Sstevel@tonic-gate 				chknew = 1;
2190Sstevel@tonic-gate 			}
2200Sstevel@tonic-gate 			continue;
2210Sstevel@tonic-gate 		}
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate 		/*
2240Sstevel@tonic-gate 		 * Determine if the current token matches the old or new string.
2250Sstevel@tonic-gate 		 * If not, disable the checking for each string.
2260Sstevel@tonic-gate 		 */
2270Sstevel@tonic-gate 		if (chkold && (*ptr != *ptrold++))
2280Sstevel@tonic-gate 			chkold = 0;
2290Sstevel@tonic-gate 		if (chknew && (*ptr != *ptrnew++))
2300Sstevel@tonic-gate 			chknew = 0;
2310Sstevel@tonic-gate 	}
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate 	/*
2340Sstevel@tonic-gate 	 * We've come to the end of the string, if the old or new string
2350Sstevel@tonic-gate 	 * checking is still enabled, we've found a match.
2360Sstevel@tonic-gate 	 */
2370Sstevel@tonic-gate 	if ((uintptr_t)ptr - (uintptr_t)curstr) {
2380Sstevel@tonic-gate 		if (chkold)
2390Sstevel@tonic-gate 			curold = curstr;
2400Sstevel@tonic-gate 		if (chknew)
2410Sstevel@tonic-gate 			curnew = curstr;
2420Sstevel@tonic-gate 	}
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate 	/*
2450Sstevel@tonic-gate 	 * If an old string hasn't been found, or it has and a new string has
2460Sstevel@tonic-gate 	 * been found, return the original string.
2470Sstevel@tonic-gate 	 */
2480Sstevel@tonic-gate 	if ((curold == 0) || curnew)
2490Sstevel@tonic-gate 		return (str);
2500Sstevel@tonic-gate 	else {
2510Sstevel@tonic-gate 		char	*newstr;
2520Sstevel@tonic-gate 		size_t	len;
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate 		/*
2550Sstevel@tonic-gate 		 * Allocate a new string, enlarged to accommodate the new string
2560Sstevel@tonic-gate 		 * that will be inserted, and an associated separator.
2570Sstevel@tonic-gate 		 */
2580Sstevel@tonic-gate 		if ((curstr = malloc(newlen + 2 +
2590Sstevel@tonic-gate 		    (uintptr_t)ptr - (uintptr_t)str)) == 0)
2600Sstevel@tonic-gate 			return (str);
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate 		newstr = (char *)curstr;
2630Sstevel@tonic-gate 		for (len = (uintptr_t)curold - (uintptr_t)str; len; len--)
2640Sstevel@tonic-gate 			*(newstr++) = *(str++);		/* copy up to */
2650Sstevel@tonic-gate 							/*    insertion point */
2660Sstevel@tonic-gate 		for (len = newlen; len; len--)
2670Sstevel@tonic-gate 			*(newstr++) = *(new++);		/* add new string and */
2680Sstevel@tonic-gate 		*(newstr++) = ':';			/*    separator */
2690Sstevel@tonic-gate 		for (len = (uintptr_t)ptr - (uintptr_t)str; len; len--)
2700Sstevel@tonic-gate 			*(newstr++) = *(str++);		/* add remaining */
2710Sstevel@tonic-gate 		*(newstr++) = '\0';			/*	string */
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate 		return (curstr);
2740Sstevel@tonic-gate 	}
2750Sstevel@tonic-gate }
276