[hackers] [sbase] Factor out table walk into a separate function || sin

From: <git_AT_suckless.org>
Date: Tue, 09 Dec 2014 16:51:29 +0100

commit 296af80dbae381898dcee762ecbe36a36079fa66
Author: sin <sin_AT_2f30.org>
Date: Tue Dec 9 15:44:31 2014 +0000

    Factor out table walk into a separate function

diff --git a/logger.c b/logger.c
index 94e1d89..3496db7 100644
--- a/logger.c
+++ b/logger.c
_AT_@ -10,44 +10,30 @@
 #include "util.h"
 
 static int
-decodefac(char *fac)
+decodetable(CODE *table, char *name)
 {
         CODE *c;
- int facility = -1;
 
- for (c = facilitynames; c->c_name; c++)
- if (!strcasecmp(fac, c->c_name))
- facility = c->c_val;
- if (facility == -1)
- eprintf("invalid facility name: %s\n", fac);
- return facility & LOG_FACMASK;
-}
-
-static int
-decodelev(char *lev)
-{
- CODE *c;
- int level = -1;
-
- for (c = prioritynames; c->c_name; c++)
- if (!strcasecmp(lev, c->c_name))
- level = c->c_val;
- if (level == -1)
- eprintf("invalid level name: %s\n", lev);
- return level & LOG_PRIMASK;
+ for (c = table; c->c_name; c++)
+ if (!strcasecmp(name, c->c_name))
+ return c->c_val;
+ eprintf("invalid priority name: %s\n", name);
+ /* NOTREACHED */
+ return -1;
 }
 
 static int
 decodepri(char *pri)
 {
- char *p;
+ char *lev, *fac = pri;
 
- if (!(p = strchr(pri, '.')))
+ if (!(lev = strchr(pri, '.')))
                 eprintf("invalid priority name: %s\n", pri);
- *p++ = '\0';
- if (!*p)
+ *lev++ = '\0';
+ if (!*lev)
                 eprintf("invalid priority name: %s\n", pri);
- return decodefac(pri) | decodelev(p);
+ return (decodetable(facilitynames, fac) & LOG_FACMASK) |
+ (decodetable(prioritynames, lev) & LOG_PRIMASK);
 }
 
 static void
Received on Tue Dec 09 2014 - 16:51:29 CET

This archive was generated by hypermail 2.3.0 : Tue Dec 09 2014 - 17:00:15 CET