1192661Ssam/*-
2192661Ssam * Copyright (c) 2002-2007 Sam Leffler, Errno Consulting
3192661Ssam * All rights reserved.
4192661Ssam *
5192661Ssam * Redistribution and use in source and binary forms, with or without
6192661Ssam * modification, are permitted provided that the following conditions
7192661Ssam * are met:
8192661Ssam * 1. Redistributions of source code must retain the above copyright
9192661Ssam *    notice, this list of conditions and the following disclaimer,
10192661Ssam *    without modification.
11192661Ssam * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12192661Ssam *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13192661Ssam *    redistribution must be conditioned upon including a substantially
14192661Ssam *    similar Disclaimer requirement for further binary redistribution.
15192661Ssam *
16192661Ssam * NO WARRANTY
17192661Ssam * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18192661Ssam * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19192661Ssam * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20192661Ssam * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21192661Ssam * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22192661Ssam * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23192661Ssam * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24192661Ssam * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25192661Ssam * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26192661Ssam * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27192661Ssam * THE POSSIBILITY OF SUCH DAMAGES.
28192661Ssam *
29192661Ssam * $FreeBSD$
30192661Ssam */
31192661Ssam
32192661Ssam#include <stdio.h>
33192661Ssam#include <string.h>
34192661Ssam
35192661Ssam#include "statfoo.h"
36192661Ssam
37192661Ssamstatic void
38192661Ssamstatfoo_setfmt(struct statfoo *sf, const char *fmt0)
39192661Ssam{
40192661Ssam#define	N(a)	(sizeof(a)/sizeof(a[0]))
41192661Ssam	char fmt[4096];
42192661Ssam	char *fp, *tok;
43192661Ssam	int i, j;
44192661Ssam
45192661Ssam	j = 0;
46192661Ssam	strlcpy(fmt, fmt0, sizeof(fmt));
47192661Ssam	for (fp = fmt; (tok = strsep(&fp, ", ")) != NULL;) {
48192661Ssam		for (i = 0; i < sf->nstats; i++)
49192661Ssam			if (strcasecmp(tok, sf->stats[i].name) == 0)
50192661Ssam				break;
51192661Ssam		if (i >= sf->nstats) {
52192661Ssam			fprintf(stderr, "%s: unknown statistic name \"%s\" "
53192661Ssam				"skipped\n", sf->name, tok);
54192661Ssam			continue;
55192661Ssam		}
56192661Ssam		if (j+3 > sizeof(sf->fmts)) {
57192661Ssam			fprintf(stderr, "%s: not enough room for all stats; "
58192661Ssam				"stopped at %s\n", sf->name, tok);
59192661Ssam			break;
60192661Ssam		}
61192661Ssam		if (j != 0)
62192661Ssam			sf->fmts[j++] = ' ';
63192661Ssam		sf->fmts[j++] = 0x80 | i;
64192661Ssam	}
65192661Ssam	sf->fmts[j] = '\0';
66192661Ssam#undef N
67192661Ssam}
68192661Ssam
69192661Ssamstatic void
70192661Ssamstatfoo_collect(struct statfoo *sf)
71192661Ssam{
72192661Ssam	fprintf(stderr, "%s: don't know how to collect data\n", sf->name);
73192661Ssam}
74192661Ssam
75192661Ssamstatic void
76192661Ssamstatfoo_update_tot(struct statfoo *sf)
77192661Ssam{
78192661Ssam	fprintf(stderr, "%s: don't know how to update total data\n", sf->name);
79192661Ssam}
80192661Ssam
81192661Ssamstatic int
82192661Ssamstatfoo_get(struct statfoo *sf, int s, char b[], size_t bs)
83192661Ssam{
84192661Ssam	fprintf(stderr, "%s: don't know how to get stat #%u\n", sf->name, s);
85192661Ssam	return 0;
86192661Ssam}
87192661Ssam
88192661Ssamstatic void
89192661Ssamstatfoo_print_header(struct statfoo *sf, FILE *fd)
90192661Ssam{
91192661Ssam	const unsigned char *cp;
92192661Ssam
93192661Ssam	for (cp = sf->fmts; *cp != '\0'; cp++) {
94192661Ssam		if (*cp & 0x80) {
95192661Ssam			const struct fmt *f = &sf->stats[*cp &~ 0x80];
96192661Ssam			fprintf(fd, "%*s", f->width, f->label);
97192661Ssam		} else
98192661Ssam			putc(*cp, fd);
99192661Ssam	}
100192661Ssam	putc('\n', fd);
101192661Ssam}
102192661Ssam
103192661Ssamstatic void
104192661Ssamstatfoo_print_current(struct statfoo *sf, FILE *fd)
105192661Ssam{
106192661Ssam	char buf[32];
107192661Ssam	const unsigned char *cp;
108192661Ssam
109192661Ssam	for (cp = sf->fmts; *cp != '\0'; cp++) {
110192661Ssam		if (*cp & 0x80) {
111192661Ssam			const struct fmt *f = &sf->stats[*cp &~ 0x80];
112192661Ssam			if (sf->get_curstat(sf, *cp &~ 0x80, buf, sizeof(buf)))
113192661Ssam				fprintf(fd, "%*s", f->width, buf);
114192661Ssam		} else
115192661Ssam			putc(*cp, fd);
116192661Ssam	}
117192661Ssam	putc('\n', fd);
118192661Ssam}
119192661Ssam
120192661Ssamstatic void
121192661Ssamstatfoo_print_total(struct statfoo *sf, FILE *fd)
122192661Ssam{
123192661Ssam	char buf[32];
124192661Ssam	const unsigned char *cp;
125192661Ssam
126192661Ssam	for (cp = sf->fmts; *cp != '\0'; cp++) {
127192661Ssam		if (*cp & 0x80) {
128192661Ssam			const struct fmt *f = &sf->stats[*cp &~ 0x80];
129192661Ssam			if (sf->get_totstat(sf, *cp &~ 0x80, buf, sizeof(buf)))
130192661Ssam				fprintf(fd, "%*s", f->width, buf);
131192661Ssam		} else
132192661Ssam			putc(*cp, fd);
133192661Ssam	}
134192661Ssam	putc('\n', fd);
135192661Ssam}
136192661Ssam
137192661Ssamstatic void
138192661Ssamstatfoo_print_verbose(struct statfoo *sf, FILE *fd)
139192661Ssam{
140192661Ssam	const struct fmt *f;
141192661Ssam	char s[32];
142192661Ssam	int i, width;
143192661Ssam
144192661Ssam	width = 0;
145192661Ssam	for (i = 0; i < sf->nstats; i++) {
146192661Ssam		f = &sf->stats[i];
147192661Ssam		if (f->width > width)
148192661Ssam			width = f->width;
149192661Ssam	}
150192661Ssam	for (i = 0; i < sf->nstats; i++) {
151192661Ssam		f = &sf->stats[i];
152192661Ssam		if (sf->get_totstat(sf, i, s, sizeof(s)) && strcmp(s, "0"))
153192661Ssam			fprintf(fd, "%-*s %s\n", width, s, f->desc);
154192661Ssam	}
155192661Ssam}
156192661Ssam
157192661Ssamstatic void
158192661Ssamstatfoo_print_fields(struct statfoo *sf, FILE *fd)
159192661Ssam{
160192661Ssam	int i, w, width;
161192661Ssam
162192661Ssam	width = 0;
163192661Ssam	for (i = 0; i < sf->nstats; i++) {
164192661Ssam		w = strlen(sf->stats[i].name);
165192661Ssam		if (w > width)
166192661Ssam			width = w;
167192661Ssam	}
168192661Ssam	for (i = 0; i < sf->nstats; i++) {
169192661Ssam		const struct fmt *f = &sf->stats[i];
170192661Ssam		if (f->width != 0)
171192661Ssam			fprintf(fd, "%-*s %s\n", width, f->name, f->desc);
172192661Ssam	}
173192661Ssam}
174192661Ssam
175192661Ssamvoid
176192661Ssamstatfoo_init(struct statfoo *sf, const char *name, const struct fmt *stats, int nstats)
177192661Ssam{
178192661Ssam	sf->name = name;
179192661Ssam	sf->stats = stats;
180192661Ssam	sf->nstats = nstats;
181192661Ssam	sf->setfmt = statfoo_setfmt;
182192661Ssam	sf->collect_cur = statfoo_collect;
183192661Ssam	sf->collect_tot = statfoo_collect;
184192661Ssam	sf->update_tot = statfoo_update_tot;
185192661Ssam	sf->get_curstat = statfoo_get;
186192661Ssam	sf->get_totstat = statfoo_get;
187192661Ssam	sf->print_header = statfoo_print_header;
188192661Ssam	sf->print_current = statfoo_print_current;
189192661Ssam	sf->print_total = statfoo_print_total;
190192661Ssam	sf->print_verbose = statfoo_print_verbose;
191192661Ssam	sf->print_fields = statfoo_print_fields;
192192661Ssam}
193