From 5218213e10691f9f5c166c41b3c374ff133a2fd2 Mon Sep 17 00:00:00 2001 From: yar Date: Mon, 27 Nov 2006 16:14:32 +0000 Subject: Keep all convtbl-related constants and strings in convtbl.[ch]. --- usr.bin/systat/convtbl.c | 63 +++++++++++++++++++++++++++++++++++++----------- usr.bin/systat/convtbl.h | 5 +++- usr.bin/systat/ifcmds.c | 41 +++++-------------------------- 3 files changed, 59 insertions(+), 50 deletions(-) (limited to 'usr.bin/systat') diff --git a/usr.bin/systat/convtbl.c b/usr.bin/systat/convtbl.c index 7daf7d6..79005ce 100644 --- a/usr.bin/systat/convtbl.c +++ b/usr.bin/systat/convtbl.c @@ -29,24 +29,24 @@ */ #include -#include +#include +#include #include "convtbl.h" struct convtbl convtbl[] = { - /* mul, scale, str */ - [SC_BYTE] = { BYTE, BYTES, "bytes" }, - [SC_KILOBYTE] = { BYTE, KILO, "KB" }, - [SC_MEGABYTE] = { BYTE, MEGA, "MB" }, - [SC_GIGABYTE] = { BYTE, GIGA, "GB" }, - - [SC_BIT] = { BIT, BITS, "b" }, - [SC_KILOBIT] = { BIT, KILO, "Kb" }, - [SC_MEGABIT] = { BIT, MEGA, "Mb" }, - [SC_GIGABIT] = { BIT, GIGA, "Gb" }, - - [SC_AUTO] = { 0, 0, "" } -}; + /* mul, scale, str, name */ + [SC_BYTE] = { BYTE, BYTES, "B", "byte" }, + [SC_KILOBYTE] = { BYTE, KILO, "KB", "kbyte" }, + [SC_MEGABYTE] = { BYTE, MEGA, "MB", "mbyte" }, + [SC_GIGABYTE] = { BYTE, GIGA, "GB", "gbyte" }, + + [SC_BIT] = { BIT, BITS, "b", "bit" }, + [SC_KILOBIT] = { BIT, KILO, "Kb", "kbit" }, + [SC_MEGABIT] = { BIT, MEGA, "Mb", "mbit" }, + [SC_GIGABIT] = { BIT, GIGA, "Gb", "gbit" }, + [SC_AUTO] = { 0, 0, "", "auto" } +}; static struct convtbl * @@ -90,3 +90,38 @@ get_string(const uintmax_t size, const int scale) tp = get_tbl_ptr(size, scale); return (tp->str); } + +int +get_scale(const char *name) +{ + int i; + + for (i = 0; i <= SC_AUTO; i++) + if (strcmp(convtbl[i].name, name) == 0) + return (i); + return (-1); +} + +const char * +get_helplist() +{ + int i; + size_t len; + static char *buf; + + if (buf == NULL) { + len = 0; + for (i = 0; i <= SC_AUTO; i++) + len += strlen(convtbl[i].name) + 2; + if ((buf = malloc(len)) != NULL) { + buf[0] = '\0'; + for (i = 0; i <= SC_AUTO; i++) { + strcat(buf, convtbl[i].name); + if (i < SC_AUTO) + strcat(buf, ", "); + } + } else + return (""); + } + return (buf); +} diff --git a/usr.bin/systat/convtbl.h b/usr.bin/systat/convtbl.h index e5cd702..87a6c09 100644 --- a/usr.bin/systat/convtbl.h +++ b/usr.bin/systat/convtbl.h @@ -49,7 +49,7 @@ enum scale { SC_KILOBIT, SC_MEGABIT, SC_GIGABIT, - SC_AUTO + SC_AUTO /* KEEP THIS LAST */ }; #define BIT (8) @@ -59,11 +59,14 @@ struct convtbl { uintmax_t mul; uintmax_t scale; const char *str; + const char *name; }; extern struct convtbl convtbl[]; extern double convert(const uintmax_t, const int); +extern const char *get_helplist(void); +extern int get_scale(const char *); extern const char *get_string(const uintmax_t, const int); #endif /* ! _CONVTBL_H_ */ diff --git a/usr.bin/systat/ifcmds.c b/usr.bin/systat/ifcmds.c index dbbdf27..495ac46 100644 --- a/usr.bin/systat/ifcmds.c +++ b/usr.bin/systat/ifcmds.c @@ -28,55 +28,26 @@ * $FreeBSD$ */ -#include - #include "systat.h" #include "extern.h" #include "convtbl.h" -int curscale = SC_AUTO; - -static int selectscale(const char *); +int curscale = SC_AUTO; int ifcmd(const char *cmd, const char *args) { + int scale; if (prefix(cmd, "scale")) { - if (*args != '\0' && selectscale(args) != -1) - ; + if ((scale = get_scale(args)) != -1) + curscale = scale; else { move(CMDLINE, 0); clrtoeol(); - addstr("what scale? kbit, kbyte, mbit, mbyte, " \ - "gbit, gbyte, auto"); + addstr("what scale? "); + addstr(get_helplist()); } } return (1); } - -static int -selectscale(const char *args) -{ - int retval = 0; - -#define streq(a,b) (strcmp(a,b) == 0) - if (streq(args, "default") || streq(args, "auto")) - curscale = SC_AUTO; - else if (streq(args, "kbit")) - curscale = SC_KILOBIT; - else if (streq(args, "kbyte")) - curscale = SC_KILOBYTE; - else if (streq(args, "mbit")) - curscale = SC_MEGABIT; - else if (streq(args, "mbyte")) - curscale = SC_MEGABYTE; - else if (streq(args, "gbit")) - curscale = SC_GIGABIT; - else if (streq(args, "gbyte")) - curscale = SC_GIGABYTE; - else - retval = -1; - - return (retval); -} -- cgit v1.1