summaryrefslogtreecommitdiff
path: root/usr.bin
diff options
context:
space:
mode:
authorNicholas Marriott <nicm@cvs.openbsd.org>2010-07-02 07:55:01 +0000
committerNicholas Marriott <nicm@cvs.openbsd.org>2010-07-02 07:55:01 +0000
commite50ecb964bb79cdcca67fd5978f8b8309c8a88fe (patch)
treedcb7f9595b044396cbc8b7bb4b30fb4ec58c2bcb /usr.bin
parent751b1334adc4c8ca2968246b70aab0d375b48f63 (diff)
Rewrite how printing all variables works to avoid using a global variable
accessed by many functions - instead do it in one function.
Diffstat (limited to 'usr.bin')
-rw-r--r--usr.bin/tip/value.c141
1 files changed, 75 insertions, 66 deletions
diff --git a/usr.bin/tip/value.c b/usr.bin/tip/value.c
index 82eb2f5b3e4..bccec933fff 100644
--- a/usr.bin/tip/value.c
+++ b/usr.bin/tip/value.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: value.c,v 1.30 2010/07/02 07:40:03 nicm Exp $ */
+/* $OpenBSD: value.c,v 1.31 2010/07/02 07:55:00 nicm Exp $ */
/* $NetBSD: value.c,v 1.6 1997/02/11 09:24:09 mrg Exp $ */
/*
@@ -78,15 +78,12 @@ value_t vtable[] = {
{ NULL, 0, NULL, NULL, 0 },
};
-#define MIDDLE 35
-
static int vlookup(char *);
static void vtoken(char *);
-static void vprint(value_t *);
+static size_t vprint(value_t *);
+static void vprintall(void);
static char *vinterp(char *, int);
-static size_t col = 0;
-
/* Get a string value. */
char *
vgetstr(int value)
@@ -150,6 +147,73 @@ vsetnum(int value, int number)
vp->v_number = number;
}
+/* Print a single variable and its value. */
+static size_t
+vprint(value_t *p)
+{
+ char *cp;
+ size_t width;
+
+ width = size(p->v_name);
+ switch (p->v_flags & V_TYPEMASK) {
+ case V_BOOL:
+ if (!p->v_number) {
+ width++;
+ putchar('!');
+ }
+ printf("%s", p->v_name);
+ break;
+ case V_STRING:
+ printf("%s=", p->v_name);
+ width++;
+ if (p->v_string) {
+ cp = interp(p->v_string);
+ width += size(cp);
+ printf("%s", cp);
+ }
+ break;
+ case V_NUMBER:
+ width += 6;
+ printf("%s=%-5d", p->v_name, p->v_number);
+ break;
+ case V_CHAR:
+ printf("%s=", p->v_name);
+ width++;
+ if (p->v_number) {
+ cp = ctrl(p->v_number);
+ width += size(cp);
+ printf("%s", cp);
+ }
+ break;
+ }
+ return (width);
+}
+
+/* Print all variables. */
+static void
+vprintall(void)
+{
+ value_t *vp;
+ size_t width;
+
+#define MIDDLE 35
+ width = 0;
+ for (vp = vtable; vp->v_name; vp++) {
+ if (vp->v_flags & V_READONLY)
+ continue;
+ if (width > 0 && width < MIDDLE) {
+ while (width++ < MIDDLE)
+ putchar(' ');
+ }
+ width += vprint(vp);
+ if (width > MIDDLE) {
+ printf("\r\n");
+ width = 0;
+ }
+ }
+#undef MIDDLE
+}
+
/* Find index of variable by name or abbreviation. */
static int
vlookup(char *s)
@@ -230,10 +294,9 @@ vlex(char *s)
value_t *p;
char *cp;
- if (strcmp(s, "all") == 0) {
- for (p = vtable; p->v_name; p++)
- vprint(p);
- } else {
+ if (strcmp(s, "all") == 0)
+ vprintall();
+ else {
do {
if ((cp = vinterp(s, ' ')))
cp++;
@@ -241,10 +304,6 @@ vlex(char *s)
s = cp;
} while (s);
}
- if (col > 0) {
- printf("\r\n");
- col = 0;
- }
}
/* Set a variable from a token. */
@@ -293,7 +352,8 @@ vtoken(char *s)
} else if ((cp = strchr(s, '?'))) {
*cp = '\0';
if ((i = vlookup(s)) != -1) {
- vprint(&vtable[i]);
+ if (vprint(&vtable[i]) > 0)
+ printf("\r\n");
return;
}
} else {
@@ -319,57 +379,6 @@ vtoken(char *s)
printf("%s: unknown variable\r\n", s);
}
-static void
-vprint(value_t *p)
-{
- char *cp;
-
- if (col > 0 && col < MIDDLE)
- while (col++ < MIDDLE)
- putchar(' ');
- col += size(p->v_name);
- switch (p->v_flags & V_TYPEMASK) {
-
- case V_BOOL:
- if (!p->v_number) {
- col++;
- putchar('!');
- }
- printf("%s", p->v_name);
- break;
-
- case V_STRING:
- printf("%s=", p->v_name);
- col++;
- if (p->v_string) {
- cp = interp(p->v_string);
- col += size(cp);
- printf("%s", cp);
- }
- break;
-
- case V_NUMBER:
- col += 6;
- printf("%s=%-5d", p->v_name, p->v_number);
- break;
-
- case V_CHAR:
- printf("%s=", p->v_name);
- col++;
- if (p->v_number) {
- cp = ctrl(p->v_number);
- col += size(cp);
- printf("%s", cp);
- }
- break;
- }
- if (col >= MIDDLE) {
- col = 0;
- printf("\r\n");
- return;
- }
-}
-
static char *
vinterp(char *s, int stop)
{