summaryrefslogtreecommitdiffstats
path: root/usr.bin
diff options
context:
space:
mode:
authoryar <yar@FreeBSD.org>2006-11-27 16:14:32 +0000
committeryar <yar@FreeBSD.org>2006-11-27 16:14:32 +0000
commit5218213e10691f9f5c166c41b3c374ff133a2fd2 (patch)
tree6cb3b92893bdc56c1aefcb3db96a4f423e6ebc1f /usr.bin
parentb581495b16a0d6be96b25af8fb0a968263d8e306 (diff)
downloadFreeBSD-src-5218213e10691f9f5c166c41b3c374ff133a2fd2.zip
FreeBSD-src-5218213e10691f9f5c166c41b3c374ff133a2fd2.tar.gz
Keep all convtbl-related constants and strings in convtbl.[ch].
Diffstat (limited to 'usr.bin')
-rw-r--r--usr.bin/systat/convtbl.c63
-rw-r--r--usr.bin/systat/convtbl.h5
-rw-r--r--usr.bin/systat/ifcmds.c41
3 files changed, 59 insertions, 50 deletions
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 <sys/types.h>
-#include <unistd.h>
+#include <stdlib.h>
+#include <string.h>
#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 <string.h>
-
#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);
-}
OpenPOWER on IntegriCloud