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]
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
31 #include "globals_msg.h"
35 * Map an integer into a descriptive string.
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
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.
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.
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.
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
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.
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
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
));
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
))
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.
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
)
139 for (ds
= *dsp
; ds
!= NULL
; ds
= *(++dsp
)) {
140 if ((value
< ds
->ds_baseval
) || (value
> ds
->ds_topval
))
143 switch (ds
->ds_type
) {
145 return (map_msg2str(inv_buf
, value
- ds
->ds_baseval
,
146 fmt_flags
, ds
->ds_topval
- ds
->ds_baseval
+ 1,
148 ((conv_ds_msg_t
*)ds
)->ds_msg
,
152 return (map_vd2str(inv_buf
, value
, fmt_flags
,
154 ((conv_ds_vd_t
*)ds
)->ds_vd
,
158 return (map_vd22str(inv_buf
, osabi
, mach
, value
,
161 ((conv_ds_vd2_t
*)ds
)->ds_vd2
,
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.
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.
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.
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
++) {
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.
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.
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.
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
);
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
))
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.
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
)
275 for (ds
= *dsp
; ds
!= NULL
; ds
= *(++dsp
)) {
276 switch (ds
->ds_type
) {
278 if (_conv_iter_msgarr(ds
->ds_baseval
,
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
);
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
);
294 if (_conv_iter_vd2(osabi
, mach
,
296 ((conv_ds_vd2_t
*)ds
)->ds_vd2
,
297 func
, uvalue
, local_sgs_msg
) == CONV_ITER_DONE
)
298 return (CONV_ITER_DONE
);
303 return (CONV_ITER_CONT
);
307 * Initialize the uvalue block prior to use of an interation function
308 * employing conv_iter_strtol().
311 * str - String to be matched to a value
312 * uvalue - Pointer to uninitialized uvalue block
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
)
323 while (conv_strproc_isspace(*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)))
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.
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().
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 */