]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - tools/tools/mwl/mwlstats/statfoo.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / tools / tools / mwl / mwlstats / statfoo.c
1 /*-
2  * Copyright (c) 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
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;
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
69 static void 
70 statfoo_collect(struct statfoo *sf)
71 {
72         fprintf(stderr, "%s: don't know how to collect data\n", sf->name);
73 }
74
75 static void 
76 statfoo_update_tot(struct statfoo *sf)
77 {
78         fprintf(stderr, "%s: don't know how to update total data\n", sf->name);
79 }
80
81 static int 
82 statfoo_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
88 static void
89 statfoo_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
103 static void
104 statfoo_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
120 static void
121 statfoo_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
137 static void
138 statfoo_print_verbose(struct statfoo *sf, FILE *fd)
139 {
140         char s[32];
141         int i;
142
143         for (i = 0; i < sf->nstats; i++) {
144                 if (sf->get_totstat(sf, i, s, sizeof(s)) && strcmp(s, "0"))
145                         fprintf(fd, "%s %s\n", s, sf->stats[i].desc);
146         }
147 }
148
149 static void
150 statfoo_print_fields(struct statfoo *sf, FILE *fd)
151 {
152         int i, w, width;
153
154         width = 0;
155         for (i = 0; i < sf->nstats; i++) {
156                 w = strlen(sf->stats[i].name);
157                 if (w > width)
158                         width = w;
159         }
160         for (i = 0; i < sf->nstats; i++) {
161                 const struct fmt *f = &sf->stats[i];
162                 if (f->width != 0)
163                         fprintf(fd, "%-*s %s\n", width, f->name, f->desc);
164         }
165 }
166
167 void
168 statfoo_init(struct statfoo *sf, const char *name, const struct fmt *stats, int nstats)
169 {
170         sf->name = name;
171         sf->stats = stats;
172         sf->nstats = nstats;
173         sf->setfmt = statfoo_setfmt;
174         sf->collect_cur = statfoo_collect;
175         sf->collect_tot = statfoo_collect;
176         sf->update_tot = statfoo_update_tot;
177         sf->get_curstat = statfoo_get;
178         sf->get_totstat = statfoo_get;
179         sf->print_header = statfoo_print_header;
180         sf->print_current = statfoo_print_current;
181         sf->print_total = statfoo_print_total;
182         sf->print_verbose = statfoo_print_verbose;
183         sf->print_fields = statfoo_print_fields;
184 }