2 * Copyright (c) 2002-2007 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.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27 * THE POSSIBILITY OF SUCH DAMAGES.
38 statfoo_setfmt(struct statfoo *sf, const char *fmt0)
40 #define N(a) (sizeof(a)/sizeof(a[0]))
46 strlcpy(fmt, fmt0, sizeof(fmt));
47 for (fp = fmt; (tok = strsep(&fp, ", ")) != NULL;) {
48 for (i = 0; i < sf->nstats; i++)
49 if (strcasecmp(tok, sf->stats[i].name) == 0)
51 if (i >= sf->nstats) {
52 fprintf(stderr, "%s: unknown statistic name \"%s\" "
53 "skipped\n", sf->name, tok);
56 if (j+3 > sizeof(sf->fmts)) {
57 fprintf(stderr, "%s: not enough room for all stats; "
58 "stopped at %s\n", sf->name, tok);
63 sf->fmts[j++] = 0x80 | i;
70 statfoo_collect(struct statfoo *sf)
72 fprintf(stderr, "%s: don't know how to collect data\n", sf->name);
76 statfoo_update_tot(struct statfoo *sf)
78 fprintf(stderr, "%s: don't know how to update total data\n", sf->name);
82 statfoo_get(struct statfoo *sf, int s, char b[], size_t bs)
84 fprintf(stderr, "%s: don't know how to get stat #%u\n", sf->name, s);
89 statfoo_print_header(struct statfoo *sf, FILE *fd)
91 const unsigned char *cp;
93 for (cp = sf->fmts; *cp != '\0'; cp++) {
95 const struct fmt *f = &sf->stats[*cp &~ 0x80];
96 fprintf(fd, "%*s", f->width, f->label);
104 statfoo_print_current(struct statfoo *sf, FILE *fd)
107 const unsigned char *cp;
109 for (cp = sf->fmts; *cp != '\0'; cp++) {
111 const struct fmt *f = &sf->stats[*cp &~ 0x80];
112 if (sf->get_curstat(sf, *cp &~ 0x80, buf, sizeof(buf)))
113 fprintf(fd, "%*s", f->width, buf);
121 statfoo_print_total(struct statfoo *sf, FILE *fd)
124 const unsigned char *cp;
126 for (cp = sf->fmts; *cp != '\0'; cp++) {
128 const struct fmt *f = &sf->stats[*cp &~ 0x80];
129 if (sf->get_totstat(sf, *cp &~ 0x80, buf, sizeof(buf)))
130 fprintf(fd, "%*s", f->width, buf);
138 statfoo_print_verbose(struct statfoo *sf, FILE *fd)
145 for (i = 0; i < sf->nstats; i++) {
147 if (f->width > width)
150 for (i = 0; i < sf->nstats; i++) {
152 if (sf->get_totstat(sf, i, s, sizeof(s)) && strcmp(s, "0"))
153 fprintf(fd, "%-*s %s\n", width, s, f->desc);
158 statfoo_print_fields(struct statfoo *sf, FILE *fd)
163 for (i = 0; i < sf->nstats; i++) {
164 w = strlen(sf->stats[i].name);
168 for (i = 0; i < sf->nstats; i++) {
169 const struct fmt *f = &sf->stats[i];
171 fprintf(fd, "%-*s %s\n", width, f->name, f->desc);
176 statfoo_init(struct statfoo *sf, const char *name, const struct fmt *stats, int nstats)
181 sf->setfmt = statfoo_setfmt;
182 sf->collect_cur = statfoo_collect;
183 sf->collect_tot = statfoo_collect;
184 sf->update_tot = statfoo_update_tot;
185 sf->get_curstat = statfoo_get;
186 sf->get_totstat = statfoo_get;
187 sf->print_header = statfoo_print_header;
188 sf->print_current = statfoo_print_current;
189 sf->print_total = statfoo_print_total;
190 sf->print_verbose = statfoo_print_verbose;
191 sf->print_fields = statfoo_print_fields;