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 #include <sys/types.h>
35 lookup_value(struct name_table *table, uintmax_t val)
38 for (; table->str != NULL; table++)
39 if (table->val == val)
45 * Used when the value maps to a bitmask of #definition values in the
46 * table. This is a helper routine which outputs a symbolic mask of
47 * matched masks. Multiple masks are separated by a pipe ('|').
48 * The value is modified on return to only hold unmatched bits.
51 print_mask_part(FILE *fp, struct name_table *table, uintmax_t *valp,
57 for (; table->str != NULL; table++) {
58 if ((table->val & rem) == table->val) {
60 * Only print a zero mask if the raw value is
63 if (table->val == 0 && *valp != 0)
65 fprintf(fp, "%s%s", *printed ? "|" : "", table->str);
75 * Used when the value maps to a bitmask of #definition values in the
76 * table. The return value is true if something was printed. If
77 * rem is not NULL, *rem holds any bits not decoded if something was
78 * printed. If nothing was printed and rem is not NULL, *rem holds
82 print_mask_int(FILE *fp, struct name_table *table, int ival, int *rem)
89 print_mask_part(fp, table, &val, &printed);
96 * Used for a mask of optional flags where a value of 0 is valid.
99 print_mask_0(FILE *fp, struct name_table *table, int val, int *rem)
108 return (print_mask_int(fp, table, val, rem));
112 * Like print_mask_0 but for a unsigned long instead of an int.
115 print_mask_0ul(FILE *fp, struct name_table *table, u_long lval, u_long *rem)
129 print_mask_part(fp, table, &val, &printed);
136 print_integer(FILE *fp, int val, int base)
141 fprintf(fp, "0%o", val);
144 fprintf(fp, "%d", val);
147 fprintf(fp, "0x%x", val);
150 abort2("bad base", 0, NULL);
156 print_value(FILE *fp, struct name_table *table, uintmax_t val)
160 str = lookup_value(table, val);