2 * Copyright (c) 2006 "David Kirchner" <dpk@dpk.net>. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
29 #include <sys/types.h>
37 lookup_value(struct name_table *table, uintmax_t val)
40 for (; table->str != NULL; table++)
41 if (table->val == val)
47 * Used when the value maps to a bitmask of #definition values in the
48 * table. This is a helper routine which outputs a symbolic mask of
49 * matched masks. Multiple masks are separated by a pipe ('|').
50 * The value is modified on return to only hold unmatched bits.
53 print_mask_part(FILE *fp, struct name_table *table, uintmax_t *valp,
59 for (; table->str != NULL; table++) {
60 if ((table->val & rem) == table->val) {
62 * Only print a zero mask if the raw value is
65 if (table->val == 0 && *valp != 0)
67 fprintf(fp, "%s%s", *printed ? "|" : "", table->str);
77 * Used when the value maps to a bitmask of #definition values in the
78 * table. The return value is true if something was printed. If
79 * rem is not NULL, *rem holds any bits not decoded if something was
80 * printed. If nothing was printed and rem is not NULL, *rem holds
84 print_mask_int(FILE *fp, struct name_table *table, int ival, int *rem)
91 print_mask_part(fp, table, &val, &printed);
98 * Used for a mask of optional flags where a value of 0 is valid.
101 print_mask_0(FILE *fp, struct name_table *table, int val, int *rem)
110 return (print_mask_int(fp, table, val, rem));
114 * Like print_mask_0 but for a unsigned long instead of an int.
117 print_mask_0ul(FILE *fp, struct name_table *table, u_long lval, u_long *rem)
131 print_mask_part(fp, table, &val, &printed);
138 print_integer(FILE *fp, int val, int base)
143 fprintf(fp, "0%o", val);
146 fprintf(fp, "%d", val);
149 fprintf(fp, "0x%x", val);
152 abort2("bad base", 0, NULL);
158 print_value(FILE *fp, struct name_table *table, uintmax_t val)
162 str = lookup_value(table, val);