xref: /illumos-gate/usr/src/cmd/sgs/libconv/common/config.c (revision 628e3cbed6489fa1db545d8524a06cd6535af456)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include	<stdlib.h>
29 #include	<sys/types.h>
30 #include	<string.h>
31 #include	"rtc.h"
32 #include	"_conv.h"
33 #include	"config_msg.h"
34 
35 #define	FEATSZ	CONV_EXPN_FIELD_DEF_PREFIX_SIZE + \
36 		MSG_CONF_EDLIBPATH_SIZE + CONV_EXPN_FIELD_DEF_SEP_SIZE + \
37 		MSG_CONF_ESLIBPATH_SIZE + CONV_EXPN_FIELD_DEF_SEP_SIZE + \
38 		MSG_CONF_ADLIBPATH_SIZE + CONV_EXPN_FIELD_DEF_SEP_SIZE + \
39 		MSG_CONF_ASLIBPATH_SIZE + CONV_EXPN_FIELD_DEF_SEP_SIZE + \
40 		MSG_CONF_DIRCFG_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
41 		MSG_CONF_OBJALT_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
42 		MSG_CONF_MEMRESV_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
43 		MSG_CONF_ENVS_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
44 		MSG_CONF_FLTR_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
45 		CONV_INV_BUFSIZE + CONV_EXPN_FIELD_DEF_SUFFIX_SIZE
46 
47 /*
48  * Ensure that Conv_config_feat_buf_t is large enough:
49  *
50  * FEATSZ is the real minimum size of the buffer required by conv_config_feat().
51  * However, Conv_config_feat_buf_t uses CONV_CONFIG_FEAT_BUFSIZE to set the
52  * buffer size. We do things this way because the definition of FEATSZ uses
53  * information that is not available in the environment of other programs
54  * that include the conv.h header file.
55  */
56 #if (CONV_CONFIG_FEAT_BUFSIZE != FEATSZ) && !defined(__lint)
57 #define	REPORT_BUFSIZE FEATSZ
58 #include "report_bufsize.h"
59 #error "CONV_CONFIG_FEAT_BUFSIZE does not match FEATSZ"
60 #endif
61 
62 /*
63  * String conversion routine for configuration file information.
64  */
65 const char *
66 conv_config_feat(int features, Conv_config_feat_buf_t *config_feat_buf)
67 {
68 	static Val_desc	vda[] = {
69 		{ CONF_EDLIBPATH,	MSG_ORIG(MSG_CONF_EDLIBPATH) },
70 		{ CONF_ESLIBPATH,	MSG_ORIG(MSG_CONF_ESLIBPATH) },
71 		{ CONF_ADLIBPATH,	MSG_ORIG(MSG_CONF_ADLIBPATH) },
72 		{ CONF_ASLIBPATH,	MSG_ORIG(MSG_CONF_ASLIBPATH) },
73 		{ CONF_DIRCFG,		MSG_ORIG(MSG_CONF_DIRCFG) },
74 		{ CONF_OBJALT,		MSG_ORIG(MSG_CONF_OBJALT) },
75 		{ CONF_MEMRESV,		MSG_ORIG(MSG_CONF_MEMRESV) },
76 		{ CONF_ENVS,		MSG_ORIG(MSG_CONF_ENVS) },
77 		{ CONF_FLTR,		MSG_ORIG(MSG_CONF_FLTR) },
78 		{ 0,			0 }
79 	};
80 	static CONV_EXPN_FIELD_ARG conv_arg = {
81 	    NULL, sizeof (config_feat_buf->buf), vda };
82 
83 	conv_arg.buf = config_feat_buf->buf;
84 	conv_arg.oflags = conv_arg.rflags = features;
85 	(void) conv_expn_field(&conv_arg, 0);
86 
87 	return ((const char *)config_feat_buf->buf);
88 }
89 
90 #define	FLAGSZ	CONV_EXPN_FIELD_DEF_PREFIX_SIZE + \
91 		MSG_CONF_DIRENT_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
92 		MSG_CONF_ALLENTS_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
93 		MSG_CONF_NOEXIST_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
94 		MSG_CONF_EXEC_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
95 		MSG_CONF_ALTER_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
96 		MSG_CONF_OPTIONAL_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
97 		MSG_CONF_DUMP_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
98 		MSG_CONF_REALPATH_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
99 		MSG_CONF_NOALTER_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
100 		MSG_CONF_GROUP_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
101 		MSG_CONF_APP_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
102 		MSG_CONF_CMDLINE_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
103 		MSG_CONF_FILTER_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
104 		MSG_CONF_FILTEE_SIZE	+ CONV_EXPN_FIELD_DEF_SEP_SIZE + \
105 		CONV_INV_BUFSIZE + CONV_EXPN_FIELD_DEF_SUFFIX_SIZE
106 
107 /*
108  * Ensure that Conv_config_obj_buf_t is large enough:
109  *
110  * FLAGSZ is the real minimum size of the buffer required by conv_config_obj().
111  * However, Conv_config_obj_buf_t uses CONV_CONFIG_OBJ_BUFSIZE to set the
112  * buffer size. We do things this way because the definition of FLAGSZ uses
113  * information that is not available in the environment of other programs
114  * that include the conv.h header file.
115  */
116 #if (CONV_CONFIG_OBJ_BUFSIZE != FLAGSZ) && !defined(__lint)
117 #define	REPORT_BUFSIZE FLAGSZ
118 #include "report_bufsize.h"
119 #error "CONV_CONFIG_OBJ_BUFSIZE does not match FLAGSZ"
120 #endif
121 
122 /*
123  * String conversion routine for object flags.
124  */
125 const char *
126 conv_config_obj(ushort_t flags, Conv_config_obj_buf_t *config_obj_buf)
127 {
128 	static Val_desc vda[] = {
129 		{ RTC_OBJ_DIRENT,	MSG_ORIG(MSG_CONF_DIRENT) },
130 		{ RTC_OBJ_ALLENTS,	MSG_ORIG(MSG_CONF_ALLENTS) },
131 		{ RTC_OBJ_NOEXIST,	MSG_ORIG(MSG_CONF_NOEXIST) },
132 		{ RTC_OBJ_EXEC,		MSG_ORIG(MSG_CONF_EXEC) },
133 		{ RTC_OBJ_ALTER,	MSG_ORIG(MSG_CONF_ALTER) },
134 		{ RTC_OBJ_DUMP,		MSG_ORIG(MSG_CONF_DUMP) },
135 		{ RTC_OBJ_NOALTER,	MSG_ORIG(MSG_CONF_NOALTER) },
136 		{ RTC_OBJ_REALPTH,	MSG_ORIG(MSG_CONF_REALPATH) },
137 		{ RTC_OBJ_GROUP,	MSG_ORIG(MSG_CONF_GROUP) },
138 		{ RTC_OBJ_APP,		MSG_ORIG(MSG_CONF_APP) },
139 		{ RTC_OBJ_CMDLINE,	MSG_ORIG(MSG_CONF_CMDLINE) },
140 		{ RTC_OBJ_FILTER,	MSG_ORIG(MSG_CONF_FILTER) },
141 		{ RTC_OBJ_FILTEE,	MSG_ORIG(MSG_CONF_FILTEE) },
142 		{ 0,			0 }
143 	};
144 	static const char *leading_str_arr[2];
145 	static CONV_EXPN_FIELD_ARG conv_arg = {
146 	    NULL, sizeof (config_obj_buf->buf), vda, leading_str_arr };
147 
148 	const char **lstr = leading_str_arr;
149 
150 	if ((flags == 0) || (flags == RTC_OBJ_OPTINAL))
151 		return (MSG_ORIG(MSG_GBL_NULL));
152 
153 	conv_arg.buf = config_obj_buf->buf;
154 	conv_arg.rflags = flags;
155 
156 	/*
157 	 * Print an alternative-optional object simply as optional.
158 	 */
159 	if ((flags & (RTC_OBJ_ALTER | RTC_OBJ_OPTINAL)) ==
160 	    (RTC_OBJ_ALTER | RTC_OBJ_OPTINAL)) {
161 		*lstr++ = MSG_ORIG(MSG_CONF_OPTIONAL);
162 		conv_arg.rflags &= ~(RTC_OBJ_ALTER | RTC_OBJ_OPTINAL);
163 	}
164 	*lstr = NULL;
165 	conv_arg.oflags = conv_arg.rflags &= ~RTC_OBJ_OPTINAL;
166 
167 	(void) conv_expn_field(&conv_arg, 0);
168 
169 	return ((const char *)config_obj_buf->buf);
170 }
171 
172 /*
173  * Determine whether and old pathname exists within a search path string,
174  * without a new pathname, i.e., does the search path string contain "/usr/lib"
175  * but not "/lib".  If so, add the new pathname before the old pathname.  For
176  * example, convert:
177  *
178  *	/local/lib:/opt/sfw/lib:/usr/lib
179  * to:
180  *	/local/lib:/opt/sfw/lib:/lib:/usr/lib
181  */
182 const char *
183 conv_config_upm(const char *str, const char *old, const char *new,
184     size_t newlen)
185 {
186 	const char	*curstr, *ptr;
187 	const char	*curold = 0, *curnew = 0;
188 	const char	*ptrold = old, * ptrnew = new;
189 	int		chkold = 1, chknew = 1;
190 
191 	for (curstr = ptr = str; *ptr; ptr++) {
192 		if (*ptr == ':') {
193 			/*
194 			 * We've come to the end of a token within the string.
195 			 */
196 			if ((uintptr_t)ptr - (uintptr_t)curstr) {
197 				/*
198 				 * If the old or new string checking is still
199 				 * enabled, we've found a match.
200 				 */
201 				if (chkold)
202 					curold = curstr;
203 				if (chknew)
204 					curnew = curstr;
205 			}
206 			curstr = (char *)(ptr + 1);
207 
208 			/*
209 			 * If an old or new string hasn't yet been matched,
210 			 * re-enable the checking for either.
211 			 */
212 			if (curold == 0) {
213 				ptrold = old;
214 				chkold = 1;
215 			}
216 			if (curnew == 0) {
217 				ptrnew = new;
218 				chknew = 1;
219 			}
220 			continue;
221 		}
222 
223 		/*
224 		 * Determine if the current token matches the old or new string.
225 		 * If not, disable the checking for each string.
226 		 */
227 		if (chkold && (*ptr != *ptrold++))
228 			chkold = 0;
229 		if (chknew && (*ptr != *ptrnew++))
230 			chknew = 0;
231 	}
232 
233 	/*
234 	 * We've come to the end of the string, if the old or new string
235 	 * checking is still enabled, we've found a match.
236 	 */
237 	if ((uintptr_t)ptr - (uintptr_t)curstr) {
238 		if (chkold)
239 			curold = curstr;
240 		if (chknew)
241 			curnew = curstr;
242 	}
243 
244 	/*
245 	 * If an old string hasn't been found, or it has and a new string has
246 	 * been found, return the original string.
247 	 */
248 	if ((curold == 0) || curnew)
249 		return (str);
250 	else {
251 		char	*newstr;
252 		size_t	len;
253 
254 		/*
255 		 * Allocate a new string, enlarged to accommodate the new string
256 		 * that will be inserted, and an associated separator.
257 		 */
258 		if ((curstr = malloc(newlen + 2 +
259 		    (uintptr_t)ptr - (uintptr_t)str)) == 0)
260 			return (str);
261 
262 		newstr = (char *)curstr;
263 		for (len = (uintptr_t)curold - (uintptr_t)str; len; len--)
264 			*(newstr++) = *(str++);		/* copy up to */
265 							/*    insertion point */
266 		for (len = newlen; len; len--)
267 			*(newstr++) = *(new++);		/* add new string and */
268 		*(newstr++) = ':';			/*    separator */
269 		for (len = (uintptr_t)ptr - (uintptr_t)str; len; len--)
270 			*(newstr++) = *(str++);		/* add remaining */
271 		*(newstr++) = '\0';			/*	string */
272 
273 		return (curstr);
274 	}
275 }
276