9804 hal-set-property should support --direct option
[unleashed.git] / usr / src / cmd / sgs / libconv / common / globals.c
blob3486045238560c5306313ed3f594b4437daa731d
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #include <stdio.h>
28 #include <strings.h>
29 #include <_machelf.h>
30 #include "_conv.h"
31 #include "globals_msg.h"
35 * Map an integer into a descriptive string.
37 * entry:
38 * inv_buf - A buffer into which this routine can format
39 * a result string, if necessary.
40 * val - The value for which a string is desired.
41 * flags - CONV_FMT_* values to be passed to conv_invalid_val() if
42 * necessary. The caller is reponsible for having examined
43 * the CONV_FMT_ALT_* part of flags and passing the proper
44 * msg array.
45 * num_msg - # of Msg entries in msg.
46 * msg - Array of num_msg Msg items corresponding to the possible
47 * strings corresponding to val.
48 * local_sgs_msg - Message string table from module from which
49 * this function is called.
51 * exit:
52 * If val lies in the range [0-(num_msg-1)], then the string
53 * corresponding to it is returned. If val is outside the range,
54 * conv_invalid_val() is called to format an ASCII representation
55 * of it into inv_buf, and that is returned.
57 /*ARGSUSED5*/
58 static const char *
59 map_msg2str(Conv_inv_buf_t *inv_buf, Conv_elfvalue_t val,
60 Conv_fmt_flags_t flags, size_t num_msg, const Msg *msg,
61 const char *local_sgs_msg)
63 if ((val < num_msg) && (msg[val] != 0))
64 return (MSG_ORIG_STRTAB(msg[val], local_sgs_msg));
66 /* If we get here, it's an unknown value */
67 return (conv_invalid_val(inv_buf, val, flags));
71 * Map an integer into a descriptive string from a NULL terminated
72 * array of Val_desc or Val_desc2 descriptors.
74 * entry:
75 * inv_buf - A buffer into which this routine can format
76 * a result string, if necessary.
77 * osabi,mach (_conv_vd22str only) - The osab/mach under which
78 * val is to be interpreted. Items with a non-0 osabi or machine
79 * that do not match are quietly ignored.
80 * val - The value for which a string is desired.
81 * flags - CONV_FMT_* values to be passed to conv_invalid_val() if
82 * necessary. The caller is reponsible for having examined
83 * the CONV_FMT_ALT_* part of flags and passing the proper
84 * descriptor array.
85 * vdp - Pointer to NULL terminated array of Val_desc descriptors.
86 * local_sgs_msg - Message string table from module from which
87 * this function is called.
89 * exit:
90 * If val is found in the vdp array, and in the osabi version of
91 * this function if the osabi matches, then the string corresponding
92 * val is returned. If a string for val is not found, conv_invalid_val()
93 * is called to format an ASCII representation of it into inv_buf, and
94 * that is returned.
96 /*ARGSUSED4*/
97 static const char *
98 map_vd2str(Conv_inv_buf_t *inv_buf, Conv_elfvalue_t val,
99 Conv_fmt_flags_t flags, const Val_desc *vdp, const char *local_sgs_msg)
101 for (; vdp->v_msg; vdp++) {
102 if (val == vdp->v_val)
103 return (MSG_ORIG_STRTAB(vdp->v_msg, local_sgs_msg));
106 /* If we get here, it's an unknown value */
107 return (conv_invalid_val(inv_buf, val, flags));
110 /*ARGSUSED6*/
111 static const char *
112 map_vd22str(Conv_inv_buf_t *inv_buf, uchar_t osabi, Half mach,
113 Conv_elfvalue_t val, Conv_fmt_flags_t flags, const Val_desc2 *vdp,
114 const char *local_sgs_msg)
116 for (; vdp->v_msg; vdp++) {
117 if (CONV_VD2_SKIP(osabi, mach, vdp))
118 continue;
120 if (val == vdp->v_val)
121 return (MSG_ORIG_STRTAB(vdp->v_msg, local_sgs_msg));
124 /* If we get here, it's an unknown value */
125 return (conv_invalid_val(inv_buf, val, flags));
129 * Process an array of conv_ds_XXX_t structures and call the appropriate
130 * map functions for the format of the strings given.
132 const char *
133 _conv_map_ds(uchar_t osabi, Half mach, Conv_elfvalue_t value,
134 const conv_ds_t **dsp, Conv_fmt_flags_t fmt_flags, Conv_inv_buf_t *inv_buf,
135 const char *local_sgs_msg)
137 const conv_ds_t *ds;
139 for (ds = *dsp; ds != NULL; ds = *(++dsp)) {
140 if ((value < ds->ds_baseval) || (value > ds->ds_topval))
141 continue;
143 switch (ds->ds_type) {
144 case CONV_DS_MSGARR:
145 return (map_msg2str(inv_buf, value - ds->ds_baseval,
146 fmt_flags, ds->ds_topval - ds->ds_baseval + 1,
147 /*LINTED*/
148 ((conv_ds_msg_t *)ds)->ds_msg,
149 local_sgs_msg));
151 case CONV_DS_VD:
152 return (map_vd2str(inv_buf, value, fmt_flags,
153 /*LINTED*/
154 ((conv_ds_vd_t *)ds)->ds_vd,
155 local_sgs_msg));
157 case CONV_DS_VD2:
158 return (map_vd22str(inv_buf, osabi, mach, value,
159 fmt_flags,
160 /*LINTED*/
161 ((conv_ds_vd2_t *)ds)->ds_vd2,
162 local_sgs_msg));
166 return (conv_invalid_val(inv_buf, value, fmt_flags));
170 * Iterate over every message string in a given array of Msg codes,
171 * calling a user supplied callback for each one.
173 * entry:
174 * basevalue - Value corresponding to the first Msg in the array.
175 * local_sgs_msg - Pointer to the __sgs_msg array for the
176 * libconv module making the call.
177 * num_msg - # of items in array referenced by msg
178 * msg - Array of Msg indexes for the strings to iterate over.
179 * The value corresponding to each element of msg must be:
180 * value[i] = basevalue + i
181 * func, uvalue - User supplied function to be called for each
182 * string in msg. uvalue is an arbitrary user supplied pointer
183 * to be passed to func.
184 * local_sgs_msg - Pointer to the __sgs_msg array for the
185 * libconv module making the call.
187 * exit:
188 * The callback function is called for every non-zero item in
189 * msg[]. If any callback returns CONV_ITER_DONE, execution stops
190 * with that item and the function returns immediately. Otherwise,
191 * it continues to the end of the array.
193 * The value from the last callback is returned.
195 /*ARGSUSED5*/
196 static conv_iter_ret_t
197 _conv_iter_msgarr(uint32_t basevalue, const Msg *msg, size_t num_msg,
198 conv_iter_cb_t func, void *uvalue, const char *local_sgs_msg)
200 for (; num_msg-- > 0; basevalue++, msg++) {
201 if (*msg != 0)
202 if ((* func)(MSG_ORIG_STRTAB(*msg, local_sgs_msg),
203 basevalue, uvalue) == CONV_ITER_DONE)
204 return (CONV_ITER_DONE);
207 return (CONV_ITER_CONT);
211 * Iterate over every message string in a given array of Val_desc or
212 * Val_desc2 descriptors, calling a user supplied callback for each one.
214 * entry:
215 * osabi,mach (_conv_iter_vd2 only) - The osabi/mach for which
216 * strings are desired. Strings with a non-0 osabi or machine
217 * that do not match are quietly ignored.
218 * vdp - Pointer to NULL terminated array of Val_desc descriptors.
219 * func, uvalue - User supplied function to be called for each
220 * string in msg. uvalue is an arbitrary user supplied pointer
221 * to be passed to func.
222 * local_sgs_msg - Pointer to the __sgs_msg array for the
223 * libconv module making the call.
225 * exit:
226 * The callback function is called for every descriptor referenced by
227 * vdp. In the case of the OSABI-version of this function, strings from
228 * the wrong osabi are not used. If any callback returns CONV_ITER_DONE,
229 * execution stops with that item and the function returns immediately.
230 * Otherwise, it continues to the end of the array.
232 * The value from the last callback is returned.
234 /*ARGSUSED3*/
235 conv_iter_ret_t
236 _conv_iter_vd(const Val_desc *vdp, conv_iter_cb_t func, void *uvalue,
237 const char *local_sgs_msg)
239 for (; vdp->v_msg; vdp++) {
240 if ((* func)(MSG_ORIG_STRTAB(vdp->v_msg, local_sgs_msg),
241 vdp->v_val, uvalue) == CONV_ITER_DONE)
242 return (CONV_ITER_DONE);
245 return (CONV_ITER_CONT);
248 /*ARGSUSED5*/
249 conv_iter_ret_t
250 _conv_iter_vd2(conv_iter_osabi_t osabi, Half mach, const Val_desc2 *vdp,
251 conv_iter_cb_t func, void *uvalue, const char *local_sgs_msg)
253 for (; vdp->v_msg; vdp++) {
254 if (CONV_ITER_VD2_SKIP(osabi, mach, vdp))
255 continue;
257 if ((* func)(MSG_ORIG_STRTAB(vdp->v_msg, local_sgs_msg),
258 vdp->v_val, uvalue) == CONV_ITER_DONE)
259 return (CONV_ITER_DONE);
262 return (CONV_ITER_CONT);
266 * Process an array of conv_ds_XXX_t structures and call the appropriate
267 * iteration functions for the format of the strings given.
269 conv_iter_ret_t
270 _conv_iter_ds(conv_iter_osabi_t osabi, Half mach, const conv_ds_t **dsp,
271 conv_iter_cb_t func, void *uvalue, const char *local_sgs_msg)
273 const conv_ds_t *ds;
275 for (ds = *dsp; ds != NULL; ds = *(++dsp)) {
276 switch (ds->ds_type) {
277 case CONV_DS_MSGARR:
278 if (_conv_iter_msgarr(ds->ds_baseval,
279 /*LINTED*/
280 ((conv_ds_msg_t *)ds)->ds_msg,
281 ds->ds_topval - ds->ds_baseval + 1, func, uvalue,
282 local_sgs_msg) == CONV_ITER_DONE)
283 return (CONV_ITER_DONE);
284 break;
286 case CONV_DS_VD:
287 /*LINTED*/
288 if (_conv_iter_vd(((conv_ds_vd_t *)ds)->ds_vd,
289 func, uvalue, local_sgs_msg) == CONV_ITER_DONE)
290 return (CONV_ITER_DONE);
291 break;
293 case CONV_DS_VD2:
294 if (_conv_iter_vd2(osabi, mach,
295 /*LINTED*/
296 ((conv_ds_vd2_t *)ds)->ds_vd2,
297 func, uvalue, local_sgs_msg) == CONV_ITER_DONE)
298 return (CONV_ITER_DONE);
299 break;
303 return (CONV_ITER_CONT);
307 * Initialize the uvalue block prior to use of an interation function
308 * employing conv_iter_strtol().
310 * entry:
311 * str - String to be matched to a value
312 * uvalue - Pointer to uninitialized uvalue block
314 * exit:
315 * Initializes the uvalue block for use. Returns True (1) if a non-empty
316 * string was supplied, and False (0).
319 conv_iter_strtol_init(const char *str, conv_strtol_uvalue_t *uvalue)
321 const char *tail;
323 while (conv_strproc_isspace(*str))
324 str++;
325 uvalue->csl_str = str;
326 uvalue->csl_found = 0;
328 tail = str + strlen(str);
329 while ((tail > str) && conv_strproc_isspace(*(tail - 1)))
330 tail--;
331 uvalue->csl_strlen = tail - str;
333 return (uvalue->csl_strlen > 0);
337 * conv_iter_strtol() is used with iteration functions to map a string
338 * to the value of its corresponding ELF constant.
340 * entry:
341 * str - String supplied by this iteration
342 * value - Value of ELF constant corresponding to str
343 * uvalue - Pointer to conv_strtol_uvalue_t block previously
344 * initialized by a call to conv_iter_strtol_init().
346 conv_iter_ret_t
347 conv_iter_strtol(const char *str, uint32_t value, void *uvalue)
349 conv_strtol_uvalue_t *state = (conv_strtol_uvalue_t *)uvalue;
351 if ((strlen(str) == state->csl_strlen) &&
352 (strncasecmp(str, state->csl_str, state->csl_strlen) == 0)) {
353 state->csl_found = 1;
354 state->csl_value = value;
355 return (CONV_ITER_DONE); /* Found it. Stop now. */
358 return (CONV_ITER_CONT); /* Keep looking */