1180059Sjhb/*-
2180059Sjhb * Copyright (c) 2002-2007 Sam Leffler, Errno Consulting
3180059Sjhb * All rights reserved.
4180059Sjhb *
5180059Sjhb * Redistribution and use in source and binary forms, with or without
6180059Sjhb * modification, are permitted provided that the following conditions
7180059Sjhb * are met:
8180059Sjhb * 1. Redistributions of source code must retain the above copyright
9180059Sjhb *    notice, this list of conditions and the following disclaimer,
10180059Sjhb *    without modification.
11180059Sjhb * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12180059Sjhb *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13180059Sjhb *    redistribution must be conditioned upon including a substantially
14180059Sjhb *    similar Disclaimer requirement for further binary redistribution.
15180059Sjhb *
16180059Sjhb * NO WARRANTY
17180059Sjhb * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18180059Sjhb * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19180059Sjhb * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20180059Sjhb * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21180059Sjhb * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22180059Sjhb * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23180059Sjhb * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24180059Sjhb * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25180059Sjhb * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26180059Sjhb * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27180059Sjhb * THE POSSIBILITY OF SUCH DAMAGES.
28180059Sjhb *
29180059Sjhb * $FreeBSD: releng/10.3/tools/tools/net80211/wlanstats/statfoo.c 178699 2008-04-30 19:47:31Z sam $
30180059Sjhb */
31180059Sjhb
32180059Sjhb#include <stdio.h>
33180059Sjhb#include <string.h>
34180059Sjhb
35180059Sjhb#include "statfoo.h"
36180059Sjhb
37180059Sjhbstatic void
38180059Sjhbstatfoo_setfmt(struct statfoo *sf, const char *fmt0)
39180059Sjhb{
40180059Sjhb#define	N(a)	(sizeof(a)/sizeof(a[0]))
41180059Sjhb	char fmt[4096];
42180059Sjhb	char *fp, *tok;
43180059Sjhb	int i, j, field;
44180059Sjhb
45180059Sjhb	j = field = 0;
46180059Sjhb	strlcpy(fmt, fmt0, sizeof(fmt));
47180059Sjhb	for (fp = fmt; (tok = strsep(&fp, ", ")) != NULL;) {
48180059Sjhb		for (i = 0; i < sf->nstats; i++)
49180059Sjhb			if (strcasecmp(tok, sf->stats[i].name) == 0)
50180059Sjhb				break;
51180059Sjhb		if (i >= sf->nstats) {
52180059Sjhb			fprintf(stderr, "%s: unknown statistic name \"%s\" "
53180059Sjhb				"skipped\n", sf->name, tok);
54180059Sjhb			continue;
55180059Sjhb		}
56180059Sjhb		if (j+3 > sizeof(sf->fmts)) {
57180059Sjhb			fprintf(stderr, "%s: not enough room for all stats; "
58180059Sjhb				"stopped at %s\n", sf->name, tok);
59180059Sjhb			break;
60		}
61		if (field > 127) {
62			fprintf(stderr, "%s: too many fields; "
63				"stopped at %s\n", sf->name, tok);
64			break;
65		}
66		if (j != 0)
67			sf->fmts[j++] = ' ';
68		sf->fmts[j++] = 0x80 | field;
69		sf->fields[field++] = i;
70	}
71	sf->fmts[j] = '\0';
72#undef N
73}
74
75static void
76statfoo_collect(struct statfoo *sf)
77{
78	fprintf(stderr, "%s: don't know how to collect data\n", sf->name);
79}
80
81static void
82statfoo_update_tot(struct statfoo *sf)
83{
84	fprintf(stderr, "%s: don't know how to update total data\n", sf->name);
85}
86
87static int
88statfoo_get(struct statfoo *sf, int s, char b[], size_t bs)
89{
90	fprintf(stderr, "%s: don't know how to get stat #%u\n", sf->name, s);
91	return 0;
92}
93
94static void
95statfoo_print_header(struct statfoo *sf, FILE *fd)
96{
97	const unsigned char *cp;
98
99	for (cp = sf->fmts; *cp != '\0'; cp++) {
100		if (*cp & 0x80) {
101			int six = sf->fields[*cp &~ 0x80];
102			const struct fmt *f = &sf->stats[six];
103			fprintf(fd, "%*s", f->width, f->label);
104		} else
105			putc(*cp, fd);
106	}
107	putc('\n', fd);
108}
109
110static void
111statfoo_print_current(struct statfoo *sf, FILE *fd)
112{
113	char buf[32];
114	const unsigned char *cp;
115
116	for (cp = sf->fmts; *cp != '\0'; cp++) {
117		if (*cp & 0x80) {
118			int six = sf->fields[*cp &~ 0x80];
119			const struct fmt *f = &sf->stats[six];
120			if (sf->get_curstat(sf, six, buf, sizeof(buf)))
121				fprintf(fd, "%*s", f->width, buf);
122		} else
123			putc(*cp, fd);
124	}
125	putc('\n', fd);
126}
127
128static void
129statfoo_print_total(struct statfoo *sf, FILE *fd)
130{
131	char buf[32];
132	const unsigned char *cp;
133
134	for (cp = sf->fmts; *cp != '\0'; cp++) {
135		if (*cp & 0x80) {
136			int six = sf->fields[*cp &~ 0x80];
137			const struct fmt *f = &sf->stats[six];
138			if (sf->get_totstat(sf, six, buf, sizeof(buf)))
139				fprintf(fd, "%*s", f->width, buf);
140		} else
141			putc(*cp, fd);
142	}
143	putc('\n', fd);
144}
145
146static void
147statfoo_print_verbose(struct statfoo *sf, FILE *fd)
148{
149	const struct fmt *f;
150	char s[32];
151	int i, width;
152
153	width = 0;
154	for (i = 0; i < sf->nstats; i++) {
155		f = &sf->stats[i];
156		if (f->width > width)
157			width = f->width;
158	}
159	for (i = 0; i < sf->nstats; i++) {
160		f = &sf->stats[i];
161		if (sf->get_totstat(sf, i, s, sizeof(s)) && strcmp(s, "0"))
162			fprintf(fd, "%-*s %s\n", width, s, f->desc);
163	}
164}
165
166static void
167statfoo_print_fields(struct statfoo *sf, FILE *fd)
168{
169	int i, w, width;
170
171	width = 0;
172	for (i = 0; i < sf->nstats; i++) {
173		w = strlen(sf->stats[i].name);
174		if (w > width)
175			width = w;
176	}
177	for (i = 0; i < sf->nstats; i++) {
178		const struct fmt *f = &sf->stats[i];
179		if (f->width != 0)
180			fprintf(fd, "%-*s %s\n", width, f->name, f->desc);
181	}
182}
183
184void
185statfoo_init(struct statfoo *sf, const char *name, const struct fmt *stats, int nstats)
186{
187	sf->name = name;
188	sf->stats = stats;
189	sf->nstats = nstats;
190	sf->setfmt = statfoo_setfmt;
191	sf->collect_cur = statfoo_collect;
192	sf->collect_tot = statfoo_collect;
193	sf->update_tot = statfoo_update_tot;
194	sf->get_curstat = statfoo_get;
195	sf->get_totstat = statfoo_get;
196	sf->print_header = statfoo_print_header;
197	sf->print_current = statfoo_print_current;
198	sf->print_total = statfoo_print_total;
199	sf->print_verbose = statfoo_print_verbose;
200	sf->print_fields = statfoo_print_fields;
201}
202