Initial import of binutils 2.22 on the new vendor branch
[dragonfly.git] / tools / tools / net80211 / wlanstats / statfoo.c
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: src/tools/tools/net80211/wlanstats/statfoo.c,v 1.4 2008/04/30 19:47:31 sam Exp $
30  */
31
32 #include <stdio.h>
33 #include <string.h>
34
35 #include "statfoo.h"
36
37 static void
38 statfoo_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, field;
44
45         j = field = 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 (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
75 static void
76 statfoo_collect(struct statfoo *sf)
77 {
78         fprintf(stderr, "%s: don't know how to collect data\n", sf->name);
79 }
80
81 static void
82 statfoo_update_tot(struct statfoo *sf)
83 {
84         fprintf(stderr, "%s: don't know how to update total data\n", sf->name);
85 }
86
87 static int
88 statfoo_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
94 static void
95 statfoo_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
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                         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
128 static void
129 statfoo_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
146 static void
147 statfoo_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
166 static void
167 statfoo_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
184 void
185 statfoo_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 }