2 * Copyright (c) 2002-2006 Sam Leffler, Errno Consulting
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer,
10 * without modification.
11 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13 * redistribution must be conditioned upon including a substantially
14 * similar Disclaimer requirement for further binary redistribution.
15 * 3. Neither the names of the above-listed copyright holders nor the names
16 * of any contributors may be used to endorse or promote products derived
17 * from this software without specific prior written permission.
19 * Alternatively, this software may be distributed under the terms of the
20 * GNU General Public License ("GPL") version 2 as published by the Free
21 * Software Foundation.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
27 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
28 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
29 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
32 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
34 * THE POSSIBILITY OF SUCH DAMAGES.
45 statfoo_setfmt(struct statfoo *sf, const char *fmt0)
47 #define N(a) (sizeof(a)/sizeof(a[0]))
53 strlcpy(fmt, fmt0, sizeof(fmt));
54 for (fp = fmt; (tok = strsep(&fp, ", ")) != NULL;) {
55 for (i = 0; i < sf->nstats; i++)
56 if (strcasecmp(tok, sf->stats[i].name) == 0)
58 if (i >= sf->nstats) {
59 fprintf(stderr, "%s: unknown statistic name \"%s\" "
60 "skipped\n", sf->name, tok);
63 if (j+3 > sizeof(sf->fmts)) {
64 fprintf(stderr, "%s: not enough room for all stats; "
65 "stopped at %s\n", sf->name, tok);
70 sf->fmts[j++] = 0x80 | i;
77 statfoo_collect(struct statfoo *sf)
79 fprintf(stderr, "%s: don't know how to collect data\n", sf->name);
83 statfoo_update_tot(struct statfoo *sf)
85 fprintf(stderr, "%s: don't know how to update total data\n", sf->name);
89 statfoo_get(struct statfoo *sf, int s, char b[], size_t bs)
91 fprintf(stderr, "%s: don't know how to get stat #%u\n", sf->name, s);
96 statfoo_print_header(struct statfoo *sf, FILE *fd)
98 const unsigned char *cp;
100 for (cp = sf->fmts; *cp != '\0'; cp++) {
102 const struct fmt *f = &sf->stats[*cp &~ 0x80];
103 fprintf(fd, "%*s", f->width, f->label);
111 statfoo_print_current(struct statfoo *sf, FILE *fd)
114 const unsigned char *cp;
116 for (cp = sf->fmts; *cp != '\0'; cp++) {
118 const struct fmt *f = &sf->stats[*cp &~ 0x80];
119 if (sf->get_curstat(sf, *cp &~ 0x80, buf, sizeof(buf)))
120 fprintf(fd, "%*s", f->width, buf);
128 statfoo_print_total(struct statfoo *sf, FILE *fd)
131 const unsigned char *cp;
133 for (cp = sf->fmts; *cp != '\0'; cp++) {
135 const struct fmt *f = &sf->stats[*cp &~ 0x80];
136 if (sf->get_totstat(sf, *cp &~ 0x80, buf, sizeof(buf)))
137 fprintf(fd, "%*s", f->width, buf);
145 statfoo_print_verbose(struct statfoo *sf, FILE *fd)
152 for (i = 0; i < sf->nstats; i++) {
154 if (f->width > width)
157 for (i = 0; i < sf->nstats; i++) {
159 if (sf->get_totstat(sf, i, s, sizeof(s)) && strcmp(s, "0"))
160 fprintf(fd, "%-*s %s\n", width, s, f->desc);
165 statfoo_print_fields(struct statfoo *sf, FILE *fd)
170 for (i = 0; i < sf->nstats; i++) {
171 w = strlen(sf->stats[i].name);
175 for (i = 0; i < sf->nstats; i++) {
176 const struct fmt *f = &sf->stats[i];
178 fprintf(fd, "%-*s %s\n", width, f->name, f->desc);
183 statfoo_init(struct statfoo *sf, const char *name, const struct fmt *stats, int nstats)
188 sf->setfmt = statfoo_setfmt;
189 sf->collect_cur = statfoo_collect;
190 sf->collect_tot = statfoo_collect;
191 sf->update_tot = statfoo_update_tot;
192 sf->get_curstat = statfoo_get;
193 sf->get_totstat = statfoo_get;
194 sf->print_header = statfoo_print_header;
195 sf->print_current = statfoo_print_current;
196 sf->print_total = statfoo_print_total;
197 sf->print_verbose = statfoo_print_verbose;
198 sf->print_fields = statfoo_print_fields;