1/*-
2 * Copyright (c) 2002-2007 Sam Leffler, Errno Consulting
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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 *
16 * NO WARRANTY
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.
28 *
29 * $FreeBSD$
30 */
31
32#include <stdio.h>
33#include <string.h>
34
35#include "statfoo.h"
36
37static void
38statfoo_setfmt(struct statfoo *sf, const char *fmt0)
39{
40#define	N(a)	(sizeof(a)/sizeof(a[0]))
41	char fmt[4096];
42	char *fp, *tok;
43	int i, j;
44
45	j = 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)
50				break;
51		if (i >= sf->nstats) {
52			fprintf(stderr, "%s: unknown statistic name \"%s\" "
53				"skipped\n", sf->name, tok);
54			continue;
55		}
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);
59			break;
60		}
61		if (j != 0)
62			sf->fmts[j++] = ' ';
63		sf->fmts[j++] = 0x80 | i;
64	}
65	sf->fmts[j] = '\0';
66#undef N
67}
68
69static void
70statfoo_collect(struct statfoo *sf)
71{
72	fprintf(stderr, "%s: don't know how to collect data\n", sf->name);
73}
74
75static void
76statfoo_update_tot(struct statfoo *sf)
77{
78	fprintf(stderr, "%s: don't know how to update total data\n", sf->name);
79}
80
81static int
82statfoo_get(struct statfoo *sf, int s, char b[], size_t bs)
83{
84	fprintf(stderr, "%s: don't know how to get stat #%u\n", sf->name, s);
85	return 0;
86}
87
88static void
89statfoo_print_header(struct statfoo *sf, FILE *fd)
90{
91	const unsigned char *cp;
92
93	for (cp = sf->fmts; *cp != '\0'; cp++) {
94		if (*cp & 0x80) {
95			const struct fmt *f = &sf->stats[*cp &~ 0x80];
96			fprintf(fd, "%*s", f->width, f->label);
97		} else
98			putc(*cp, fd);
99	}
100	putc('\n', fd);
101}
102
103static void
104statfoo_print_current(struct statfoo *sf, FILE *fd)
105{
106	char buf[32];
107	const unsigned char *cp;
108
109	for (cp = sf->fmts; *cp != '\0'; cp++) {
110		if (*cp & 0x80) {
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);
114		} else
115			putc(*cp, fd);
116	}
117	putc('\n', fd);
118}
119
120static void
121statfoo_print_total(struct statfoo *sf, FILE *fd)
122{
123	char buf[32];
124	const unsigned char *cp;
125
126	for (cp = sf->fmts; *cp != '\0'; cp++) {
127		if (*cp & 0x80) {
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);
131		} else
132			putc(*cp, fd);
133	}
134	putc('\n', fd);
135}
136
137static void
138statfoo_print_verbose(struct statfoo *sf, FILE *fd)
139{
140	const struct fmt *f;
141	char s[32];
142	int i, width;
143
144	width = 0;
145	for (i = 0; i < sf->nstats; i++) {
146		f = &sf->stats[i];
147		if (f->width > width)
148			width = f->width;
149	}
150	for (i = 0; i < sf->nstats; i++) {
151		f = &sf->stats[i];
152		if (sf->get_totstat(sf, i, s, sizeof(s)) && strcmp(s, "0"))
153			fprintf(fd, "%-*s %s\n", width, s, f->desc);
154	}
155}
156
157static void
158statfoo_print_fields(struct statfoo *sf, FILE *fd)
159{
160	int i, w, width;
161
162	width = 0;
163	for (i = 0; i < sf->nstats; i++) {
164		w = strlen(sf->stats[i].name);
165		if (w > width)
166			width = w;
167	}
168	for (i = 0; i < sf->nstats; i++) {
169		const struct fmt *f = &sf->stats[i];
170		if (f->width != 0)
171			fprintf(fd, "%-*s %s\n", width, f->name, f->desc);
172	}
173}
174
175void
176statfoo_init(struct statfoo *sf, const char *name, const struct fmt *stats, int nstats)
177{
178	sf->name = name;
179	sf->stats = stats;
180	sf->nstats = 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;
192}
193