xref: /freebsd/lib/libbsdstat/bsdstat.c (revision c697fb7f)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2002-2007 Sam Leffler, Errno Consulting
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer,
12  *    without modification.
13  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
14  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
15  *    redistribution must be conditioned upon including a substantially
16  *    similar Disclaimer requirement for further binary redistribution.
17  *
18  * NO WARRANTY
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
22  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
24  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
27  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
29  * THE POSSIBILITY OF SUCH DAMAGES.
30  *
31  * $FreeBSD$
32  */
33 
34 #include <stdio.h>
35 #include <string.h>
36 
37 #include "bsdstat.h"
38 
39 static void
40 bsdstat_setfmt(struct bsdstat *sf, const char *fmt0)
41 {
42 #define	N(a)	(sizeof(a)/sizeof(a[0]))
43 	char fmt[4096];
44 	char *fp, *tok;
45 	int i, j;
46 
47 	j = 0;
48 	strlcpy(fmt, fmt0, sizeof(fmt));
49 	for (fp = fmt; (tok = strsep(&fp, ", ")) != NULL;) {
50 		for (i = 0; i < sf->nstats; i++)
51 			if (strcasecmp(tok, sf->stats[i].name) == 0)
52 				break;
53 		if (i >= sf->nstats) {
54 			fprintf(stderr, "%s: unknown statistic name \"%s\" "
55 				"skipped\n", sf->name, tok);
56 			continue;
57 		}
58 		if (j+4 > (int) sizeof(sf->fmts)) {
59 			fprintf(stderr, "%s: not enough room for all stats; "
60 				"stopped at %s\n", sf->name, tok);
61 			break;
62 		}
63 		if (j != 0)
64 			sf->fmts[j++] = ' ';
65 		sf->fmts[j++] = FMTS_IS_STAT;
66 		sf->fmts[j++] = i & 0xff;
67 		sf->fmts[j++] = (i >> 8) & 0xff;
68 	}
69 	sf->fmts[j] = '\0';
70 #undef N
71 }
72 
73 static void
74 bsdstat_collect(struct bsdstat *sf)
75 {
76 	fprintf(stderr, "%s: don't know how to collect data\n", sf->name);
77 }
78 
79 static void
80 bsdstat_update_tot(struct bsdstat *sf)
81 {
82 	fprintf(stderr, "%s: don't know how to update total data\n", sf->name);
83 }
84 
85 static int
86 bsdstat_get(struct bsdstat *sf, int s, char b[] __unused, size_t bs __unused)
87 {
88 	fprintf(stderr, "%s: don't know how to get stat #%u\n", sf->name, s);
89 	return 0;
90 }
91 
92 static void
93 bsdstat_print_header(struct bsdstat *sf, FILE *fd)
94 {
95 	const unsigned char *cp;
96 	int i;
97 	const struct fmt *f;
98 
99 	for (cp = sf->fmts; *cp != '\0'; cp++) {
100 		if (*cp == FMTS_IS_STAT) {
101 			i = *(++cp);
102 			i |= ((int) *(++cp)) << 8;
103 			f = &sf->stats[i];
104 			fprintf(fd, "%*s", f->width, f->label);
105 		} else
106 			putc(*cp, fd);
107 	}
108 	putc('\n', fd);
109 }
110 
111 static void
112 bsdstat_print_current(struct bsdstat *sf, FILE *fd)
113 {
114 	char buf[32];
115 	const unsigned char *cp;
116 	int i;
117 	const struct fmt *f;
118 
119 	for (cp = sf->fmts; *cp != '\0'; cp++) {
120 		if (*cp == FMTS_IS_STAT) {
121 			i = *(++cp);
122 			i |= ((int) *(++cp)) << 8;
123 			f = &sf->stats[i];
124 			if (sf->get_curstat(sf, i, buf, sizeof(buf)))
125 				fprintf(fd, "%*s", f->width, buf);
126 		} else
127 			putc(*cp, fd);
128 	}
129 	putc('\n', fd);
130 }
131 
132 static void
133 bsdstat_print_total(struct bsdstat *sf, FILE *fd)
134 {
135 	char buf[32];
136 	const unsigned char *cp;
137 	const struct fmt *f;
138 	int i;
139 
140 	for (cp = sf->fmts; *cp != '\0'; cp++) {
141 		if (*cp == FMTS_IS_STAT) {
142 			i = *(++cp);
143 			i |= ((int) *(++cp)) << 8;
144 			f = &sf->stats[i];
145 			if (sf->get_totstat(sf, i, buf, sizeof(buf)))
146 				fprintf(fd, "%*s", f->width, buf);
147 		} else
148 			putc(*cp, fd);
149 	}
150 	putc('\n', fd);
151 }
152 
153 static void
154 bsdstat_print_verbose(struct bsdstat *sf, FILE *fd)
155 {
156 	const struct fmt *f;
157 	char s[32];
158 	int i, width;
159 
160 	width = 0;
161 	for (i = 0; i < sf->nstats; i++) {
162 		f = &sf->stats[i];
163 		if (f->width > width)
164 			width = f->width;
165 	}
166 	for (i = 0; i < sf->nstats; i++) {
167 		f = &sf->stats[i];
168 		if (sf->get_totstat(sf, i, s, sizeof(s)) && strcmp(s, "0"))
169 			fprintf(fd, "%-*s %s\n", width, s, f->desc);
170 	}
171 }
172 
173 static void
174 bsdstat_print_fields(struct bsdstat *sf, FILE *fd)
175 {
176 	int i, w, width;
177 
178 	width = 0;
179 	for (i = 0; i < sf->nstats; i++) {
180 		w = strlen(sf->stats[i].name);
181 		if (w > width)
182 			width = w;
183 	}
184 	for (i = 0; i < sf->nstats; i++) {
185 		const struct fmt *f = &sf->stats[i];
186 		if (f->width != 0)
187 			fprintf(fd, "%-*s %s\n", width, f->name, f->desc);
188 	}
189 }
190 
191 void
192 bsdstat_init(struct bsdstat *sf, const char *name, const struct fmt *stats, int nstats)
193 {
194 	sf->name = name;
195 	sf->stats = stats;
196 	sf->nstats = nstats;
197 	sf->setfmt = bsdstat_setfmt;
198 	sf->collect_cur = bsdstat_collect;
199 	sf->collect_tot = bsdstat_collect;
200 	sf->update_tot = bsdstat_update_tot;
201 	sf->get_curstat = bsdstat_get;
202 	sf->get_totstat = bsdstat_get;
203 	sf->print_header = bsdstat_print_header;
204 	sf->print_current = bsdstat_print_current;
205 	sf->print_total = bsdstat_print_total;
206 	sf->print_verbose = bsdstat_print_verbose;
207 	sf->print_fields = bsdstat_print_fields;
208 }
209