statfoo.c revision 161200
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: head/tools/tools/ath/athstats/statfoo.c 161200 2006-08-10 19:01:16Z sam $ 37 */ 38 39#include <stdio.h> 40#include <string.h> 41 42#include "statfoo.h" 43 44static void 45statfoo_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 76static void 77statfoo_collect(struct statfoo *sf) 78{ 79 fprintf(stderr, "%s: don't know how to collect data\n", sf->name); 80} 81 82static void 83statfoo_update_tot(struct statfoo *sf) 84{ 85 fprintf(stderr, "%s: don't know how to update total data\n", sf->name); 86} 87 88static int 89statfoo_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 95static void 96statfoo_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 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 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 127static void 128statfoo_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 144static void 145statfoo_print_verbose(struct statfoo *sf, FILE *fd) 146{ 147 char s[32]; 148 int i; 149 150 for (i = 0; i < sf->nstats; i++) { 151 if (sf->get_totstat(sf, i, s, sizeof(s)) && strcmp(s, "0")) 152 fprintf(fd, "%s %s\n", s, sf->stats[i].desc); 153 } 154} 155 156static void 157statfoo_print_fields(struct statfoo *sf, FILE *fd) 158{ 159 int i, w, width; 160 161 width = 0; 162 for (i = 0; i < sf->nstats; i++) { 163 w = strlen(sf->stats[i].name); 164 if (w > width) 165 width = w; 166 } 167 for (i = 0; i < sf->nstats; i++) { 168 const struct fmt *f = &sf->stats[i]; 169 if (f->width != 0) 170 fprintf(fd, "%-*s %s\n", width, f->name, f->desc); 171 } 172} 173 174void 175statfoo_init(struct statfoo *sf, const char *name, const struct fmt *stats, int nstats) 176{ 177 sf->name = name; 178 sf->stats = stats; 179 sf->nstats = nstats; 180 sf->setfmt = statfoo_setfmt; 181 sf->collect_cur = statfoo_collect; 182 sf->collect_tot = statfoo_collect; 183 sf->update_tot = statfoo_update_tot; 184 sf->get_curstat = statfoo_get; 185 sf->get_totstat = statfoo_get; 186 sf->print_header = statfoo_print_header; 187 sf->print_current = statfoo_print_current; 188 sf->print_total = statfoo_print_total; 189 sf->print_verbose = statfoo_print_verbose; 190 sf->print_fields = statfoo_print_fields; 191} 192