xref: /onnv-gate/usr/src/cmd/sgs/libconv/common/globals.c (revision 1976:f0691a145b7e)
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 /*
231618Srie  * Copyright 2006 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	<stdio.h>
291618Srie #include	<strings.h>
300Sstevel@tonic-gate #include	<sys/machelf.h>
311618Srie #include	"_conv.h"
320Sstevel@tonic-gate #include	"globals_msg.h"
330Sstevel@tonic-gate 
34*1976Sab196087 
35*1976Sab196087 /*
36*1976Sab196087  * Given an integer value, generate an ASCII representation of it.
37*1976Sab196087  *
38*1976Sab196087  * entry:
39*1976Sab196087  *	string - Buffer into which the resulting string is generated.
40*1976Sab196087  *	size - Size of string buffer (i.e. sizeof(string))
41*1976Sab196087  *	value - Value to be formatted.
42*1976Sab196087  *	fmt_flags - CONV_FMT_* values, used to specify formatting details.
43*1976Sab196087  *
44*1976Sab196087  * exit:
45*1976Sab196087  *	The formatted string, or as much as will fit, is placed into
46*1976Sab196087  *	string. String is returned.
47*1976Sab196087  */
480Sstevel@tonic-gate const char *
49*1976Sab196087 conv_invalid_val(char *string, size_t size, Xword value, int fmt_flags)
500Sstevel@tonic-gate {
511618Srie 	const char	*fmt;
520Sstevel@tonic-gate 
53*1976Sab196087 	if (fmt_flags & CONV_FMT_DECIMAL) {
54*1976Sab196087 		if (fmt_flags & CONV_FMT_SPACE)
551618Srie 			fmt = MSG_ORIG(MSG_GBL_FMT_DECS);
561618Srie 		else
571618Srie 			fmt = MSG_ORIG(MSG_GBL_FMT_DEC);
581618Srie 	} else {
59*1976Sab196087 		if (fmt_flags & CONV_FMT_SPACE)
601618Srie 			fmt = MSG_ORIG(MSG_GBL_FMT_HEXS);
611618Srie 		else
621618Srie 			fmt = MSG_ORIG(MSG_GBL_FMT_HEX);
631618Srie 	}
641618Srie 	(void) snprintf(string, size, fmt, value);
650Sstevel@tonic-gate 	return ((const char *)string);
660Sstevel@tonic-gate }
671618Srie 
68*1976Sab196087 
69*1976Sab196087 
701618Srie /*
71*1976Sab196087  * Provide a focal point for expanding bit-fields values into
721618Srie  * their corresponding strings.
73*1976Sab196087  *
74*1976Sab196087  * entry:
75*1976Sab196087  *	string - Buffer into which the resulting string is generated.
76*1976Sab196087  *	size - Size of string buffer (i.e. sizeof(string))
77*1976Sab196087  *	vdp - Array of value descriptors, giving the possible bit
78*1976Sab196087  *		values, and their corresponding strings. Note that the
79*1976Sab196087  *		final element must contain only NULL values. This
80*1976Sab196087  *		terminates the list.
81*1976Sab196087  *	oflags - Bits for which output strings are desired.
82*1976Sab196087  *	rflags - Bits for which a numeric value should be printed
83*1976Sab196087  *		if vdp does not provide a corresponding string. This
84*1976Sab196087  *		must be a proper subset of oflags.
85*1976Sab196087  *	separator - If non-NULL, a separator string to be inserted
86*1976Sab196087  *		between each string value copied into the output.
87*1976Sab196087  *	element - TRUE if first element output should be preceeded
88*1976Sab196087  *		by a separator, and FALSE otherwise.
89*1976Sab196087  *
90*1976Sab196087  * exit:
91*1976Sab196087  *	string contains the formatted result. True (1) is returned if there
92*1976Sab196087  *	was no error, and False (0) if the buffer was too small.
931618Srie  */
941618Srie int
951618Srie conv_expn_field(char *string, size_t size, const Val_desc *vdp,
961618Srie     Xword oflags, Xword rflags, const char *separator, int element)
971618Srie {
981618Srie 	const Val_desc	*vde;
991618Srie 
1001618Srie 	/*
1011618Srie 	 * Traverse the callers Val_desc array and determine if the value
1021618Srie 	 * corresponds to any array item.
1031618Srie 	 */
1041618Srie 	for (vde = vdp; vde->v_msg; vde++) {
1051618Srie 		if (oflags & vde->v_val) {
1061618Srie 			/*
1071618Srie 			 * If a separator is required, and elements have already
1081618Srie 			 * been added to the users output buffer, add the
1091618Srie 			 * separator to the buffer first.
1101618Srie 			 */
1111618Srie 			if (separator && element++) {
1121618Srie 				if (strlcat(string, separator, size) >= size) {
1131618Srie 					(void) conv_invalid_val(string, size,
1141618Srie 					    oflags, 0);
1151618Srie 					return (0);
1161618Srie 				}
1171618Srie 			}
1181618Srie 
1191618Srie 			/*
1201618Srie 			 * Add the items strings to the users output buffer.
1211618Srie 			 */
1221618Srie 			if (strlcat(string, vde->v_msg, size) >= size) {
1231618Srie 				(void) conv_invalid_val(string, size,
1241618Srie 				    oflags, 0);
1251618Srie 				return (0);
1261618Srie 			}
1271618Srie 
1281618Srie 			/*
1291618Srie 			 * Indicate this item has been collected.
1301618Srie 			 */
1311618Srie 			rflags &= ~(vde->v_val);
1321618Srie 		}
1331618Srie 	}
1341618Srie 
1351618Srie 	/*
1361618Srie 	 * If any flags remain, then they are unidentified.  Add the number
1371618Srie 	 * representation of these flags to the users output buffer.
1381618Srie 	 */
1391618Srie 	if (rflags) {
1401618Srie 		size_t  off = strlen(string);
1411618Srie 		size_t  rem = size - off;
1421618Srie 
1431618Srie 		(void) conv_invalid_val(&string[off], rem, rflags,
144*1976Sab196087 		    CONV_FMT_SPACE);
1451618Srie 	}
1461618Srie 
1471618Srie 	return (1);
1481618Srie }
149