xref: /freebsd-src/lib/libsysdecode/support.c (revision a2f733abcff64628b7771a47089628b7327a88bd)
1*9dac6096SDmitry Chagin /*
2*9dac6096SDmitry Chagin  * Copyright (c) 2006 "David Kirchner" <dpk@dpk.net>. All rights reserved.
3*9dac6096SDmitry Chagin  *
4*9dac6096SDmitry Chagin  * Redistribution and use in source and binary forms, with or without
5*9dac6096SDmitry Chagin  * modification, are permitted provided that the following conditions
6*9dac6096SDmitry Chagin  * are met:
7*9dac6096SDmitry Chagin  * 1. Redistributions of source code must retain the above copyright
8*9dac6096SDmitry Chagin  *    notice, this list of conditions and the following disclaimer.
9*9dac6096SDmitry Chagin  * 2. Redistributions in binary form must reproduce the above copyright
10*9dac6096SDmitry Chagin  *    notice, this list of conditions and the following disclaimer in the
11*9dac6096SDmitry Chagin  *    documentation and/or other materials provided with the distribution.
12*9dac6096SDmitry Chagin  *
13*9dac6096SDmitry Chagin  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14*9dac6096SDmitry Chagin  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15*9dac6096SDmitry Chagin  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16*9dac6096SDmitry Chagin  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17*9dac6096SDmitry Chagin  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18*9dac6096SDmitry Chagin  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19*9dac6096SDmitry Chagin  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20*9dac6096SDmitry Chagin  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21*9dac6096SDmitry Chagin  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22*9dac6096SDmitry Chagin  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23*9dac6096SDmitry Chagin  * SUCH DAMAGE.
24*9dac6096SDmitry Chagin  */
25*9dac6096SDmitry Chagin 
26*9dac6096SDmitry Chagin #include <sys/types.h>
27*9dac6096SDmitry Chagin #include <stdbool.h>
28*9dac6096SDmitry Chagin #include <stdio.h>
29*9dac6096SDmitry Chagin #include <stdlib.h>
30*9dac6096SDmitry Chagin 
31*9dac6096SDmitry Chagin #include "support.h"
32*9dac6096SDmitry Chagin 
33*9dac6096SDmitry Chagin const char *
lookup_value(struct name_table * table,uintmax_t val)34*9dac6096SDmitry Chagin lookup_value(struct name_table *table, uintmax_t val)
35*9dac6096SDmitry Chagin {
36*9dac6096SDmitry Chagin 
37*9dac6096SDmitry Chagin 	for (; table->str != NULL; table++)
38*9dac6096SDmitry Chagin 		if (table->val == val)
39*9dac6096SDmitry Chagin 			return (table->str);
40*9dac6096SDmitry Chagin 	return (NULL);
41*9dac6096SDmitry Chagin }
42*9dac6096SDmitry Chagin 
43*9dac6096SDmitry Chagin /*
44*9dac6096SDmitry Chagin  * Used when the value maps to a bitmask of #definition values in the
45*9dac6096SDmitry Chagin  * table.  This is a helper routine which outputs a symbolic mask of
46*9dac6096SDmitry Chagin  * matched masks.  Multiple masks are separated by a pipe ('|').
47*9dac6096SDmitry Chagin  * The value is modified on return to only hold unmatched bits.
48*9dac6096SDmitry Chagin  */
49*9dac6096SDmitry Chagin void
print_mask_part(FILE * fp,struct name_table * table,uintmax_t * valp,bool * printed)50*9dac6096SDmitry Chagin print_mask_part(FILE *fp, struct name_table *table, uintmax_t *valp,
51*9dac6096SDmitry Chagin     bool *printed)
52*9dac6096SDmitry Chagin {
53*9dac6096SDmitry Chagin 	uintmax_t rem;
54*9dac6096SDmitry Chagin 
55*9dac6096SDmitry Chagin 	rem = *valp;
56*9dac6096SDmitry Chagin 	for (; table->str != NULL; table++) {
57*9dac6096SDmitry Chagin 		if ((table->val & rem) == table->val) {
58*9dac6096SDmitry Chagin 			/*
59*9dac6096SDmitry Chagin 			 * Only print a zero mask if the raw value is
60*9dac6096SDmitry Chagin 			 * zero.
61*9dac6096SDmitry Chagin 			 */
62*9dac6096SDmitry Chagin 			if (table->val == 0 && *valp != 0)
63*9dac6096SDmitry Chagin 				continue;
64*9dac6096SDmitry Chagin 			fprintf(fp, "%s%s", *printed ? "|" : "", table->str);
65*9dac6096SDmitry Chagin 			*printed = true;
66*9dac6096SDmitry Chagin 			rem &= ~table->val;
67*9dac6096SDmitry Chagin 		}
68*9dac6096SDmitry Chagin 	}
69*9dac6096SDmitry Chagin 
70*9dac6096SDmitry Chagin 	*valp = rem;
71*9dac6096SDmitry Chagin }
72*9dac6096SDmitry Chagin 
73*9dac6096SDmitry Chagin /*
74*9dac6096SDmitry Chagin  * Used when the value maps to a bitmask of #definition values in the
75*9dac6096SDmitry Chagin  * table.  The return value is true if something was printed.  If
76*9dac6096SDmitry Chagin  * rem is not NULL, *rem holds any bits not decoded if something was
77*9dac6096SDmitry Chagin  * printed.  If nothing was printed and rem is not NULL, *rem holds
78*9dac6096SDmitry Chagin  * the original value.
79*9dac6096SDmitry Chagin  */
80*9dac6096SDmitry Chagin bool
print_mask_int(FILE * fp,struct name_table * table,int ival,int * rem)81*9dac6096SDmitry Chagin print_mask_int(FILE *fp, struct name_table *table, int ival, int *rem)
82*9dac6096SDmitry Chagin {
83*9dac6096SDmitry Chagin 	uintmax_t val;
84*9dac6096SDmitry Chagin 	bool printed;
85*9dac6096SDmitry Chagin 
86*9dac6096SDmitry Chagin 	printed = false;
87*9dac6096SDmitry Chagin 	val = (unsigned)ival;
88*9dac6096SDmitry Chagin 	print_mask_part(fp, table, &val, &printed);
89*9dac6096SDmitry Chagin 	if (rem != NULL)
90*9dac6096SDmitry Chagin 		*rem = val;
91*9dac6096SDmitry Chagin 	return (printed);
92*9dac6096SDmitry Chagin }
93*9dac6096SDmitry Chagin 
94*9dac6096SDmitry Chagin /*
95*9dac6096SDmitry Chagin  * Used for a mask of optional flags where a value of 0 is valid.
96*9dac6096SDmitry Chagin  */
97*9dac6096SDmitry Chagin bool
print_mask_0(FILE * fp,struct name_table * table,int val,int * rem)98*9dac6096SDmitry Chagin print_mask_0(FILE *fp, struct name_table *table, int val, int *rem)
99*9dac6096SDmitry Chagin {
100*9dac6096SDmitry Chagin 
101*9dac6096SDmitry Chagin 	if (val == 0) {
102*9dac6096SDmitry Chagin 		fputs("0", fp);
103*9dac6096SDmitry Chagin 		if (rem != NULL)
104*9dac6096SDmitry Chagin 			*rem = 0;
105*9dac6096SDmitry Chagin 		return (true);
106*9dac6096SDmitry Chagin 	}
107*9dac6096SDmitry Chagin 	return (print_mask_int(fp, table, val, rem));
108*9dac6096SDmitry Chagin }
109*9dac6096SDmitry Chagin 
110*9dac6096SDmitry Chagin /*
111*9dac6096SDmitry Chagin  * Like print_mask_0 but for a unsigned long instead of an int.
112*9dac6096SDmitry Chagin  */
113*9dac6096SDmitry Chagin bool
print_mask_0ul(FILE * fp,struct name_table * table,u_long lval,u_long * rem)114*9dac6096SDmitry Chagin print_mask_0ul(FILE *fp, struct name_table *table, u_long lval, u_long *rem)
115*9dac6096SDmitry Chagin {
116*9dac6096SDmitry Chagin 	uintmax_t val;
117*9dac6096SDmitry Chagin 	bool printed;
118*9dac6096SDmitry Chagin 
119*9dac6096SDmitry Chagin 	if (lval == 0) {
120*9dac6096SDmitry Chagin 		fputs("0", fp);
121*9dac6096SDmitry Chagin 		if (rem != NULL)
122*9dac6096SDmitry Chagin 			*rem = 0;
123*9dac6096SDmitry Chagin 		return (true);
124*9dac6096SDmitry Chagin 	}
125*9dac6096SDmitry Chagin 
126*9dac6096SDmitry Chagin 	printed = false;
127*9dac6096SDmitry Chagin 	val = lval;
128*9dac6096SDmitry Chagin 	print_mask_part(fp, table, &val, &printed);
129*9dac6096SDmitry Chagin 	if (rem != NULL)
130*9dac6096SDmitry Chagin 		*rem = val;
131*9dac6096SDmitry Chagin 	return (printed);
132*9dac6096SDmitry Chagin }
133*9dac6096SDmitry Chagin 
134*9dac6096SDmitry Chagin void
print_integer(FILE * fp,int val,int base)135*9dac6096SDmitry Chagin print_integer(FILE *fp, int val, int base)
136*9dac6096SDmitry Chagin {
137*9dac6096SDmitry Chagin 
138*9dac6096SDmitry Chagin 	switch (base) {
139*9dac6096SDmitry Chagin 	case 8:
140*9dac6096SDmitry Chagin 		fprintf(fp, "0%o", val);
141*9dac6096SDmitry Chagin 		break;
142*9dac6096SDmitry Chagin 	case 10:
143*9dac6096SDmitry Chagin 		fprintf(fp, "%d", val);
144*9dac6096SDmitry Chagin 		break;
145*9dac6096SDmitry Chagin 	case 16:
146*9dac6096SDmitry Chagin 		fprintf(fp, "0x%x", val);
147*9dac6096SDmitry Chagin 		break;
148*9dac6096SDmitry Chagin 	default:
149*9dac6096SDmitry Chagin 		abort2("bad base", 0, NULL);
150*9dac6096SDmitry Chagin 		break;
151*9dac6096SDmitry Chagin 	}
152*9dac6096SDmitry Chagin }
153*9dac6096SDmitry Chagin 
154*9dac6096SDmitry Chagin bool
print_value(FILE * fp,struct name_table * table,uintmax_t val)155*9dac6096SDmitry Chagin print_value(FILE *fp, struct name_table *table, uintmax_t val)
156*9dac6096SDmitry Chagin {
157*9dac6096SDmitry Chagin 	const char *str;
158*9dac6096SDmitry Chagin 
159*9dac6096SDmitry Chagin 	str = lookup_value(table, val);
160*9dac6096SDmitry Chagin 	if (str != NULL) {
161*9dac6096SDmitry Chagin 		fputs(str, fp);
162*9dac6096SDmitry Chagin 		return (true);
163*9dac6096SDmitry Chagin 	}
164*9dac6096SDmitry Chagin 	return (false);
165*9dac6096SDmitry Chagin }
166