xref: /titanic_41/usr/src/cmd/sgs/libconv/common/config.c (revision 28cdc3d776761766afeb198769d1b70ed7e0f2e1)
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 2006 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	MSG_GBL_OSQBRKT_SIZE + \
36 		MSG_CONF_EDLIBPATH_SIZE + \
37 		MSG_CONF_ESLIBPATH_SIZE + \
38 		MSG_CONF_ADLIBPATH_SIZE + \
39 		MSG_CONF_ASLIBPATH_SIZE + \
40 		MSG_CONF_DIRCFG_SIZE + \
41 		MSG_CONF_OBJALT_SIZE + \
42 		MSG_CONF_MEMRESV_SIZE + \
43 		MSG_CONF_ENVS_SIZE + \
44 		MSG_CONF_FLTR_SIZE + \
45 		CONV_INV_STRSIZE + MSG_GBL_CSQBRKT_SIZE
46 
47 /*
48  * String conversion routine for configuration file information.
49  */
50 const char *
51 conv_config_feat(int features)
52 {
53 	static	char	string[FEATSZ];
54 	static Val_desc	vda[] = {
55 		{ CONF_EDLIBPATH,	MSG_ORIG(MSG_CONF_EDLIBPATH) },
56 		{ CONF_ESLIBPATH,	MSG_ORIG(MSG_CONF_ESLIBPATH) },
57 		{ CONF_ADLIBPATH,	MSG_ORIG(MSG_CONF_ADLIBPATH) },
58 		{ CONF_ASLIBPATH,	MSG_ORIG(MSG_CONF_ASLIBPATH) },
59 		{ CONF_DIRCFG,		MSG_ORIG(MSG_CONF_DIRCFG) },
60 		{ CONF_OBJALT,		MSG_ORIG(MSG_CONF_OBJALT) },
61 		{ CONF_MEMRESV,		MSG_ORIG(MSG_CONF_MEMRESV) },
62 		{ CONF_ENVS,		MSG_ORIG(MSG_CONF_ENVS) },
63 		{ CONF_FLTR,		MSG_ORIG(MSG_CONF_FLTR) },
64 		{ 0,			0 }
65 	};
66 
67 	(void) strlcpy(string, MSG_ORIG(MSG_GBL_OSQBRKT), FEATSZ);
68 	if (conv_expn_field(string, FEATSZ, vda, features,
69 	    features, 0, 0))
70 		(void) strlcat(string, MSG_ORIG(MSG_GBL_CSQBRKT), FEATSZ);
71 
72 	return ((const char *)string);
73 }
74 
75 #define	FLAGSZ	MSG_GBL_OSQBRKT_SIZE + \
76 		MSG_CONF_DIRENT_SIZE + \
77 		MSG_CONF_ALLENTS_SIZE + \
78 		MSG_CONF_NOEXIST_SIZE + \
79 		MSG_CONF_EXEC_SIZE + \
80 		MSG_CONF_ALTER_SIZE + \
81 		MSG_CONF_OPTIONAL_SIZE + \
82 		MSG_CONF_DUMP_SIZE + \
83 		MSG_CONF_REALPATH_SIZE + \
84 		MSG_CONF_NOALTER_SIZE + \
85 		MSG_CONF_GROUP_SIZE + \
86 		MSG_CONF_APP_SIZE + \
87 		MSG_CONF_CMDLINE_SIZE + \
88 		MSG_CONF_FILTER_SIZE + \
89 		MSG_CONF_FILTEE_SIZE + \
90 		CONV_INV_STRSIZE + MSG_GBL_CSQBRKT_SIZE
91 
92 /*
93  * String conversion routine for object flags.
94  */
95 const char *
96 conv_config_obj(ushort_t flags)
97 {
98 	static char	string[FLAGSZ];
99 	static Val_desc vda[] = {
100 		{ RTC_OBJ_DIRENT,	MSG_ORIG(MSG_CONF_DIRENT) },
101 		{ RTC_OBJ_ALLENTS,	MSG_ORIG(MSG_CONF_ALLENTS) },
102 		{ RTC_OBJ_NOEXIST,	MSG_ORIG(MSG_CONF_NOEXIST) },
103 		{ RTC_OBJ_EXEC,		MSG_ORIG(MSG_CONF_EXEC) },
104 		{ RTC_OBJ_ALTER,	MSG_ORIG(MSG_CONF_ALTER) },
105 		{ RTC_OBJ_DUMP,		MSG_ORIG(MSG_CONF_DUMP) },
106 		{ RTC_OBJ_NOALTER,	MSG_ORIG(MSG_CONF_NOALTER) },
107 		{ RTC_OBJ_REALPTH,	MSG_ORIG(MSG_CONF_REALPATH) },
108 		{ RTC_OBJ_GROUP,	MSG_ORIG(MSG_CONF_GROUP) },
109 		{ RTC_OBJ_APP,		MSG_ORIG(MSG_CONF_APP) },
110 		{ RTC_OBJ_CMDLINE,	MSG_ORIG(MSG_CONF_CMDLINE) },
111 		{ RTC_OBJ_FILTER,	MSG_ORIG(MSG_CONF_FILTER) },
112 		{ RTC_OBJ_FILTEE,	MSG_ORIG(MSG_CONF_FILTEE) },
113 		{ 0,			0 }
114 	};
115 	ushort_t	_flags = flags;
116 
117 	if ((flags == 0) || (flags == RTC_OBJ_OPTINAL))
118 		return (MSG_ORIG(MSG_GBL_NULL));
119 
120 	(void) strlcpy(string, MSG_ORIG(MSG_GBL_OSQBRKT), FLAGSZ);
121 
122 	/*
123 	 * Print an alternative-optional object simply as optional.
124 	 */
125 	if ((flags & (RTC_OBJ_ALTER | RTC_OBJ_OPTINAL)) ==
126 	    (RTC_OBJ_ALTER | RTC_OBJ_OPTINAL)) {
127 		if (strlcat(string, MSG_ORIG(MSG_CONF_OPTIONAL),
128 		    FLAGSZ) >= FLAGSZ)
129 			return (conv_invalid_val(string, FLAGSZ, flags, 0));
130 		flags = _flags &= ~(RTC_OBJ_ALTER | RTC_OBJ_OPTINAL);
131 	}
132 	flags = _flags &= ~RTC_OBJ_OPTINAL;
133 
134 	if (conv_expn_field(string, FLAGSZ, vda, flags, _flags, 0, 0))
135 		(void) strlcat(string, MSG_ORIG(MSG_GBL_CSQBRKT), FLAGSZ);
136 
137 	return ((const char *)string);
138 }
139 
140 /*
141  * Determine whether and old pathname exists within a search path string,
142  * without a new pathname, i.e., does the search path string contain "/usr/lib"
143  * but not "/lib".  If so, add the new pathname before the old pathname.  For
144  * example, convert:
145  *
146  *	/local/lib:/opt/sfw/lib:/usr/lib
147  * to:
148  *	/local/lib:/opt/sfw/lib:/lib:/usr/lib
149  */
150 const char *
151 conv_config_upm(const char *str, const char *old, const char *new,
152     size_t newlen)
153 {
154 	const char	*curstr, *ptr;
155 	const char	*curold = 0, *curnew = 0;
156 	const char	*ptrold = old, * ptrnew = new;
157 	int		chkold = 1, chknew = 1;
158 
159 	for (curstr = ptr = str; *ptr; ptr++) {
160 		if (*ptr == ':') {
161 			/*
162 			 * We've come to the end of a token within the string.
163 			 */
164 			if ((uintptr_t)ptr - (uintptr_t)curstr) {
165 				/*
166 				 * If the old or new string checking is still
167 				 * enabled, we've found a match.
168 				 */
169 				if (chkold)
170 					curold = curstr;
171 				if (chknew)
172 					curnew = curstr;
173 			}
174 			curstr = (char *)(ptr + 1);
175 
176 			/*
177 			 * If an old or new string hasn't yet been matched,
178 			 * re-enable the checking for either.
179 			 */
180 			if (curold == 0) {
181 				ptrold = old;
182 				chkold = 1;
183 			}
184 			if (curnew == 0) {
185 				ptrnew = new;
186 				chknew = 1;
187 			}
188 			continue;
189 		}
190 
191 		/*
192 		 * Determine if the current token matches the old or new string.
193 		 * If not, disable the checking for each string.
194 		 */
195 		if (chkold && (*ptr != *ptrold++))
196 			chkold = 0;
197 		if (chknew && (*ptr != *ptrnew++))
198 			chknew = 0;
199 	}
200 
201 	/*
202 	 * We've come to the end of the string, if the old or new string
203 	 * checking is still enabled, we've found a match.
204 	 */
205 	if ((uintptr_t)ptr - (uintptr_t)curstr) {
206 		if (chkold)
207 			curold = curstr;
208 		if (chknew)
209 			curnew = curstr;
210 	}
211 
212 	/*
213 	 * If an old string hasn't been found, or it has and a new string has
214 	 * been found, return the original string.
215 	 */
216 	if ((curold == 0) || curnew)
217 		return (str);
218 	else {
219 		char	*newstr;
220 		size_t	len;
221 
222 		/*
223 		 * Allocate a new string, enlarged to accommodate the new string
224 		 * that will be inserted, and an associated separator.
225 		 */
226 		if ((curstr = malloc(newlen + 2 +
227 		    (uintptr_t)ptr - (uintptr_t)str)) == 0)
228 			return (str);
229 
230 		newstr = (char *)curstr;
231 		for (len = (uintptr_t)curold - (uintptr_t)str; len; len--)
232 			*(newstr++) = *(str++);		/* copy up to */
233 							/*    insertion point */
234 		for (len = newlen; len; len--)
235 			*(newstr++) = *(new++);		/* add new string and */
236 		*(newstr++) = ':';			/*    separator */
237 		for (len = (uintptr_t)ptr - (uintptr_t)str; len; len--)
238 			*(newstr++) = *(str++);		/* add remaining */
239 		*(newstr++) = '\0';			/*	string */
240 
241 		return (curstr);
242 	}
243 }
244