]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - tools/tools/net80211/wlanstats/statfoo.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / tools / tools / net80211 / wlanstats / statfoo.c
1 /*-
2  * Copyright (c) 2002-2006 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  * 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.
18  *
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.
22  *
23  * NO WARRANTY
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.
35  *
36  * $FreeBSD$
37  */
38
39 #include <stdio.h>
40 #include <string.h>
41
42 #include "statfoo.h"
43
44 static void
45 statfoo_setfmt(struct statfoo *sf, const char *fmt0)
46 {
47 #define N(a)    (sizeof(a)/sizeof(a[0]))
48         char fmt[4096];
49         char *fp, *tok;
50         int i, j;
51
52         j = 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)
57                                 break;
58                 if (i >= sf->nstats) {
59                         fprintf(stderr, "%s: unknown statistic name \"%s\" "
60                                 "skipped\n", sf->name, tok);
61                         continue;
62                 }
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);
66                         break;
67                 }
68                 if (j != 0)
69                         sf->fmts[j++] = ' ';
70                 sf->fmts[j++] = 0x80 | i;
71         }
72         sf->fmts[j] = '\0';
73 #undef N
74 }
75
76 static void 
77 statfoo_collect(struct statfoo *sf)
78 {
79         fprintf(stderr, "%s: don't know how to collect data\n", sf->name);
80 }
81
82 static void 
83 statfoo_update_tot(struct statfoo *sf)
84 {
85         fprintf(stderr, "%s: don't know how to update total data\n", sf->name);
86 }
87
88 static int 
89 statfoo_get(struct statfoo *sf, int s, char b[], size_t bs)
90 {
91         fprintf(stderr, "%s: don't know how to get stat #%u\n", sf->name, s);
92         return 0;
93 }
94
95 static void
96 statfoo_print_header(struct statfoo *sf, FILE *fd)
97 {
98         const unsigned char *cp;
99
100         for (cp = sf->fmts; *cp != '\0'; cp++) {
101                 if (*cp & 0x80) {
102                         const struct fmt *f = &sf->stats[*cp &~ 0x80];
103                         fprintf(fd, "%*s", f->width, f->label);
104                 } else
105                         putc(*cp, fd);
106         }
107         putc('\n', fd);
108 }
109
110 static void
111 statfoo_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                         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);
121                 } else
122                         putc(*cp, fd);
123         }
124         putc('\n', fd);
125 }
126
127 static void
128 statfoo_print_total(struct statfoo *sf, FILE *fd)
129 {
130         char buf[32];
131         const unsigned char *cp;
132
133         for (cp = sf->fmts; *cp != '\0'; cp++) {
134                 if (*cp & 0x80) {
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);
138                 } else
139                         putc(*cp, fd);
140         }
141         putc('\n', fd);
142 }
143
144 static void
145 statfoo_print_verbose(struct statfoo *sf, FILE *fd)
146 {
147         const struct fmt *f;
148         char s[32];
149         int i, width;
150
151         width = 0;
152         for (i = 0; i < sf->nstats; i++) {
153                 f = &sf->stats[i];
154                 if (f->width > width)
155                         width = f->width;
156         }
157         for (i = 0; i < sf->nstats; i++) {
158                 f = &sf->stats[i];
159                 if (sf->get_totstat(sf, i, s, sizeof(s)) && strcmp(s, "0"))
160                         fprintf(fd, "%-*s %s\n", width, s, f->desc);
161         }
162 }
163
164 static void
165 statfoo_print_fields(struct statfoo *sf, FILE *fd)
166 {
167         int i, w, width;
168
169         width = 0;
170         for (i = 0; i < sf->nstats; i++) {
171                 w = strlen(sf->stats[i].name);
172                 if (w > width)
173                         width = w;
174         }
175         for (i = 0; i < sf->nstats; i++) {
176                 const struct fmt *f = &sf->stats[i];
177                 if (f->width != 0)
178                         fprintf(fd, "%-*s %s\n", width, f->name, f->desc);
179         }
180 }
181
182 void
183 statfoo_init(struct statfoo *sf, const char *name, const struct fmt *stats, int nstats)
184 {
185         sf->name = name;
186         sf->stats = stats;
187         sf->nstats = 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;
199 }