xref: /onnv-gate/usr/src/cmd/sgs/libconv/common/globals.c (revision 1618:8c9a4f31d225)
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
5*1618Srie  * Common Development and Distribution License (the "License").
6*1618Srie  * 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  */
21*1618Srie 
220Sstevel@tonic-gate /*
23*1618Srie  * 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>
29*1618Srie #include	<strings.h>
300Sstevel@tonic-gate #include	<sys/machelf.h>
31*1618Srie #include	"_conv.h"
320Sstevel@tonic-gate #include	"globals_msg.h"
330Sstevel@tonic-gate 
340Sstevel@tonic-gate const char *
35*1618Srie conv_invalid_val(char *string, size_t size, Xword value, int flags)
360Sstevel@tonic-gate {
37*1618Srie 	const char	*fmt;
380Sstevel@tonic-gate 
39*1618Srie 	if (flags & CONV_INV_DECIMAL) {
40*1618Srie 		if (flags & CONV_INV_SPACE)
41*1618Srie 			fmt = MSG_ORIG(MSG_GBL_FMT_DECS);
42*1618Srie 		else
43*1618Srie 			fmt = MSG_ORIG(MSG_GBL_FMT_DEC);
44*1618Srie 	} else {
45*1618Srie 		if (flags & CONV_INV_SPACE)
46*1618Srie 			fmt = MSG_ORIG(MSG_GBL_FMT_HEXS);
47*1618Srie 		else
48*1618Srie 			fmt = MSG_ORIG(MSG_GBL_FMT_HEX);
49*1618Srie 	}
50*1618Srie 	(void) snprintf(string, size, fmt, value);
510Sstevel@tonic-gate 	return ((const char *)string);
520Sstevel@tonic-gate }
53*1618Srie 
54*1618Srie /*
55*1618Srie  * Provide a focal point for expanding values (typically bit-fields) into
56*1618Srie  * their corresponding strings.
57*1618Srie  */
58*1618Srie int
59*1618Srie conv_expn_field(char *string, size_t size, const Val_desc *vdp,
60*1618Srie     Xword oflags, Xword rflags, const char *separator, int element)
61*1618Srie {
62*1618Srie 	const Val_desc	*vde;
63*1618Srie 
64*1618Srie 	/*
65*1618Srie 	 * Traverse the callers Val_desc array and determine if the value
66*1618Srie 	 * corresponds to any array item.
67*1618Srie 	 */
68*1618Srie 	for (vde = vdp; vde->v_msg; vde++) {
69*1618Srie 		if (oflags & vde->v_val) {
70*1618Srie 			/*
71*1618Srie 			 * If a separator is required, and elements have already
72*1618Srie 			 * been added to the users output buffer, add the
73*1618Srie 			 * separator to the buffer first.
74*1618Srie 			 */
75*1618Srie 			if (separator && element++) {
76*1618Srie 				if (strlcat(string, separator, size) >= size) {
77*1618Srie 					(void) conv_invalid_val(string, size,
78*1618Srie 					    oflags, 0);
79*1618Srie 					return (0);
80*1618Srie 				}
81*1618Srie 			}
82*1618Srie 
83*1618Srie 			/*
84*1618Srie 			 * Add the items strings to the users output buffer.
85*1618Srie 			 */
86*1618Srie 			if (strlcat(string, vde->v_msg, size) >= size) {
87*1618Srie 				(void) conv_invalid_val(string, size,
88*1618Srie 				    oflags, 0);
89*1618Srie 				return (0);
90*1618Srie 			}
91*1618Srie 
92*1618Srie 			/*
93*1618Srie 			 * Indicate this item has been collected.
94*1618Srie 			 */
95*1618Srie 			rflags &= ~(vde->v_val);
96*1618Srie 		}
97*1618Srie 	}
98*1618Srie 
99*1618Srie 	/*
100*1618Srie 	 * If any flags remain, then they are unidentified.  Add the number
101*1618Srie 	 * representation of these flags to the users output buffer.
102*1618Srie 	 */
103*1618Srie 	if (rflags) {
104*1618Srie 		size_t  off = strlen(string);
105*1618Srie 		size_t  rem = size - off;
106*1618Srie 
107*1618Srie 		(void) conv_invalid_val(&string[off], rem, rflags,
108*1618Srie 		    CONV_INV_SPACE);
109*1618Srie 	}
110*1618Srie 
111*1618Srie 	return (1);
112*1618Srie }
113