summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKristaps Dzonsons <kristaps@bsd.lv>2010-03-23 21:50:43 +0000
committerKristaps Dzonsons <kristaps@bsd.lv>2010-03-23 21:50:43 +0000
commita520ef48b70e5c01709c968c95b3f02314515f6f (patch)
tree03ee911af1fc7b600ee80106511b10f90c14fba9
parent35ac570ed4a7f722db3bafc6585c2e75c36b7413 (diff)
downloadmandoc-a520ef48b70e5c01709c968c95b3f02314515f6f.tar.gz
libman using enum mant instead of #defines for macros.
Clean-ups, better documentation in man_hash.c. Added extra space for "." in man_hash.c (unused for the time being).
-rw-r--r--libman.h12
-rw-r--r--man.c25
-rw-r--r--man.h76
-rw-r--r--man_hash.c55
-rw-r--r--man_macro.c15
5 files changed, 106 insertions, 77 deletions
diff --git a/libman.h b/libman.h
index 2efafe78..a4d5cbb5 100644
--- a/libman.h
+++ b/libman.h
@@ -63,7 +63,7 @@ enum merr {
WERRMAX
};
-#define MACRO_PROT_ARGS struct man *m, int tok, int line, \
+#define MACRO_PROT_ARGS struct man *m, enum mant tok, int line, \
int ppos, int *pos, char *buf
struct man_macro {
@@ -89,15 +89,15 @@ __BEGIN_DECLS
man_err((m), (n)->line, (n)->pos, 0, (t))
int man_word_alloc(struct man *, int, int, const char *);
-int man_block_alloc(struct man *, int, int, int);
-int man_head_alloc(struct man *, int, int, int);
-int man_body_alloc(struct man *, int, int, int);
-int man_elem_alloc(struct man *, int, int, int);
+int man_block_alloc(struct man *, int, int, enum mant);
+int man_head_alloc(struct man *, int, int, enum mant);
+int man_body_alloc(struct man *, int, int, enum mant);
+int man_elem_alloc(struct man *, int, int, enum mant);
void man_node_free(struct man_node *);
void man_node_freelist(struct man_node *);
void man_node_unlink(struct man *, struct man_node *);
void man_hash_init(void);
-int man_hash_find(const char *);
+enum mant man_hash_find(const char *);
int man_macroend(struct man *);
int man_args(struct man *, int, int *, char *, char **);
#define ARGS_ERROR (-1)
diff --git a/man.c b/man.c
index 88035875..986efec6 100644
--- a/man.c
+++ b/man.c
@@ -67,7 +67,7 @@ const char *const __man_macronames[MAN_MAX] = {
const char * const *man_macronames = __man_macronames;
static struct man_node *man_node_alloc(int, int,
- enum man_type, int);
+ enum man_type, enum mant);
static int man_node_append(struct man *,
struct man_node *);
static int man_ptext(struct man *, int, char *);
@@ -242,7 +242,7 @@ man_node_append(struct man *man, struct man_node *p)
static struct man_node *
-man_node_alloc(int line, int pos, enum man_type type, int tok)
+man_node_alloc(int line, int pos, enum man_type type, enum mant tok)
{
struct man_node *p;
@@ -256,7 +256,7 @@ man_node_alloc(int line, int pos, enum man_type type, int tok)
int
-man_elem_alloc(struct man *m, int line, int pos, int tok)
+man_elem_alloc(struct man *m, int line, int pos, enum mant tok)
{
struct man_node *p;
@@ -269,7 +269,7 @@ man_elem_alloc(struct man *m, int line, int pos, int tok)
int
-man_head_alloc(struct man *m, int line, int pos, int tok)
+man_head_alloc(struct man *m, int line, int pos, enum mant tok)
{
struct man_node *p;
@@ -282,7 +282,7 @@ man_head_alloc(struct man *m, int line, int pos, int tok)
int
-man_body_alloc(struct man *m, int line, int pos, int tok)
+man_body_alloc(struct man *m, int line, int pos, enum mant tok)
{
struct man_node *p;
@@ -295,7 +295,7 @@ man_body_alloc(struct man *m, int line, int pos, int tok)
int
-man_block_alloc(struct man *m, int line, int pos, int tok)
+man_block_alloc(struct man *m, int line, int pos, enum mant tok)
{
struct man_node *p;
@@ -314,7 +314,7 @@ pstring(struct man *m, int line, int pos,
struct man_node *n;
size_t sv;
- n = man_node_alloc(line, pos, MAN_TEXT, -1);
+ n = man_node_alloc(line, pos, MAN_TEXT, MAN_MAX);
n->string = mandoc_malloc(len + 1);
sv = strlcpy(n->string, p, len + 1);
@@ -469,7 +469,8 @@ macrowarn(struct man *m, int ln, const char *buf)
int
man_pmacro(struct man *m, int ln, char *buf)
{
- int i, j, c, ppos, fl;
+ int i, j, ppos, fl;
+ enum mant tok;
char mac[5];
struct man_node *n;
@@ -519,7 +520,7 @@ man_pmacro(struct man *m, int ln, char *buf)
return(1);
}
- if (MAN_MAX == (c = man_hash_find(mac))) {
+ if (MAN_MAX == (tok = man_hash_find(mac))) {
if ( ! macrowarn(m, ln, mac))
goto err;
return(1);
@@ -542,7 +543,7 @@ man_pmacro(struct man *m, int ln, char *buf)
* macros---they don't print text---so we let those slip by.
*/
- if ( ! (MAN_NSCOPED & man_macros[c].flags) &&
+ if ( ! (MAN_NSCOPED & man_macros[tok].flags) &&
m->flags & MAN_ELINE) {
assert(MAN_TEXT != m->last->type);
@@ -574,9 +575,9 @@ man_pmacro(struct man *m, int ln, char *buf)
/* Begin recursive parse sequence. */
- assert(man_macros[c].fp);
+ assert(man_macros[tok].fp);
- if ( ! (*man_macros[c].fp)(m, c, ln, ppos, &i, buf))
+ if ( ! (*man_macros[tok].fp)(m, tok, ln, ppos, &i, buf))
goto err;
out:
diff --git a/man.h b/man.h
index 524a5dd0..872fbf48 100644
--- a/man.h
+++ b/man.h
@@ -19,42 +19,44 @@
#include <time.h>
-#define MAN_br 0
-#define MAN_TH 1
-#define MAN_SH 2
-#define MAN_SS 3
-#define MAN_TP 4
-#define MAN_LP 5
-#define MAN_PP 6
-#define MAN_P 7
-#define MAN_IP 8
-#define MAN_HP 9
-#define MAN_SM 10
-#define MAN_SB 11
-#define MAN_BI 12
-#define MAN_IB 13
-#define MAN_BR 14
-#define MAN_RB 15
-#define MAN_R 16
-#define MAN_B 17
-#define MAN_I 18
-#define MAN_IR 19
-#define MAN_RI 20
-#define MAN_na 21
-#define MAN_i 22
-#define MAN_sp 23
-#define MAN_nf 24
-#define MAN_fi 25
-#define MAN_r 26
-#define MAN_RE 27
-#define MAN_RS 28
-#define MAN_DT 29
-#define MAN_UC 30
-#define MAN_PD 31
-#define MAN_Sp 32
-#define MAN_Vb 33
-#define MAN_Ve 34
-#define MAN_MAX 35
+enum mant {
+ MAN_br = 0,
+ MAN_TH,
+ MAN_SH,
+ MAN_SS,
+ MAN_TP,
+ MAN_LP,
+ MAN_PP,
+ MAN_P,
+ MAN_IP,
+ MAN_HP,
+ MAN_SM,
+ MAN_SB,
+ MAN_BI,
+ MAN_IB,
+ MAN_BR,
+ MAN_RB,
+ MAN_R,
+ MAN_B,
+ MAN_I,
+ MAN_IR,
+ MAN_RI,
+ MAN_na,
+ MAN_i,
+ MAN_sp,
+ MAN_nf,
+ MAN_fi,
+ MAN_r,
+ MAN_RE,
+ MAN_RS,
+ MAN_DT,
+ MAN_UC,
+ MAN_PD,
+ MAN_Sp,
+ MAN_Vb,
+ MAN_Ve,
+ MAN_MAX,
+};
enum man_type {
MAN_TEXT,
@@ -81,7 +83,7 @@ struct man_node {
int nchild;
int line;
int pos;
- int tok;
+ enum mant tok;
int flags;
#define MAN_VALID (1 << 0)
#define MAN_ACTED (1 << 1)
diff --git a/man_hash.c b/man_hash.c
index 7ca0da44..44b5c9f1 100644
--- a/man_hash.c
+++ b/man_hash.c
@@ -21,13 +21,33 @@
#include <sys/types.h>
#include <assert.h>
+#include <ctype.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include "libman.h"
-static u_char table[26 * 6];
+#define HASH_DEPTH 6
+
+#define HASH_ROW(x) do { \
+ if ('.' == (x)) \
+ (x) = 26; \
+ else if (isupper((u_char)(x))) \
+ (x) -= 65; \
+ else \
+ (x) -= 97; \
+ (x) *= HASH_DEPTH; \
+ } while (/* CONSTCOND */ 0)
+
+/*
+ * Lookup table is indexed first by lower-case first letter (plus one
+ * for the period, which is stored in the last row), then by lower or
+ * uppercase second letter. Buckets correspond to the index of the
+ * macro (the integer value of the enum stored as a char to save a bit
+ * of space).
+ */
+static u_char table[27 * HASH_DEPTH];
/*
* XXX - this hash has global scope, so if intended for use as a library
@@ -40,39 +60,44 @@ man_hash_init(void)
memset(table, UCHAR_MAX, sizeof(table));
+ assert(/* CONSTCOND */ MAN_MAX < UCHAR_MAX);
+
for (i = 0; i < MAN_MAX; i++) {
x = man_macronames[i][0];
- assert((x >= 65 && x <= 90) ||
- (x >= 97 && x <= 122));
- x -= (x <= 90) ? 65 : 97;
- x *= 6;
+ assert(isalpha((u_char)x) || '.' == x);
- for (j = 0; j < 6; j++)
+ HASH_ROW(x);
+
+ for (j = 0; j < HASH_DEPTH; j++)
if (UCHAR_MAX == table[x + j]) {
table[x + j] = (u_char)i;
break;
}
- assert(j < 6);
+
+ assert(j < HASH_DEPTH);
}
}
-int
+
+enum mant
man_hash_find(const char *tmp)
{
- int x, i, tok;
+ int x, y, i;
+ enum mant tok;
- if (0 == (x = tmp[0]))
+ if ('\0' == (x = tmp[0]))
return(MAN_MAX);
- if ( ! ((x >= 65 && x <= 90) || (x >= 97 && x <= 122)))
+ if ( ! (isalpha((u_char)x) || '.' == x))
return(MAN_MAX);
- x -= (x <= 90) ? 65 : 97;
- x *= 6;
+ HASH_ROW(x);
- for (i = 0; i < 6; i++) {
- if (UCHAR_MAX == (tok = table[x + i]))
+ for (i = 0; i < HASH_DEPTH; i++) {
+ if (UCHAR_MAX == (y = table[x + i]))
return(MAN_MAX);
+
+ tok = (enum mant)y;
if (0 == strcmp(tmp, man_macronames[tok]))
return(tok);
}
diff --git a/man_macro.c b/man_macro.c
index 1a7ed50c..3137e375 100644
--- a/man_macro.c
+++ b/man_macro.c
@@ -33,10 +33,11 @@ static int in_line_eoln(MACRO_PROT_ARGS);
static int blk_imp(MACRO_PROT_ARGS);
static int blk_close(MACRO_PROT_ARGS);
-static int rew_scope(enum man_type, struct man *, int);
-static int rew_dohalt(int, enum man_type,
+static int rew_scope(enum man_type,
+ struct man *, enum mant);
+static int rew_dohalt(enum mant, enum man_type,
const struct man_node *);
-static int rew_block(int, enum man_type,
+static int rew_block(enum mant, enum man_type,
const struct man_node *);
const struct man_macro __man_macros[MAN_MAX] = {
@@ -104,7 +105,7 @@ man_unscope(struct man *m, const struct man_node *n)
static int
-rew_block(int ntok, enum man_type type, const struct man_node *n)
+rew_block(enum mant ntok, enum man_type type, const struct man_node *n)
{
if (MAN_BLOCK == type && ntok == n->parent->tok &&
@@ -120,7 +121,7 @@ rew_block(int ntok, enum man_type type, const struct man_node *n)
* sections and subsections).
*/
static int
-rew_dohalt(int tok, enum man_type type, const struct man_node *n)
+rew_dohalt(enum mant tok, enum man_type type, const struct man_node *n)
{
int c;
@@ -175,7 +176,7 @@ rew_dohalt(int tok, enum man_type type, const struct man_node *n)
* scopes. When a scope is closed, it must be validated and actioned.
*/
static int
-rew_scope(enum man_type type, struct man *m, int tok)
+rew_scope(enum man_type type, struct man *m, enum mant tok)
{
struct man_node *n;
int c;
@@ -205,7 +206,7 @@ rew_scope(enum man_type type, struct man *m, int tok)
int
blk_close(MACRO_PROT_ARGS)
{
- int ntok;
+ enum mant ntok;
const struct man_node *nn;
switch (tok) {