From 34499b8564d56a778ba364385e208a4f51fe3e04 Mon Sep 17 00:00:00 2001 From: Kristaps Dzonsons Date: Tue, 23 Mar 2010 21:50:43 +0000 Subject: 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). --- libman.h | 14 +++++------ man.c | 27 ++++++++++----------- man.h | 78 +++++++++++++++++++++++++++++++------------------------------ man_hash.c | 57 +++++++++++++++++++++++++++++++------------- man_macro.c | 17 +++++++------- 5 files changed, 111 insertions(+), 82 deletions(-) diff --git a/libman.h b/libman.h index 28dc5ffa..d366d6f8 100644 --- a/libman.h +++ b/libman.h @@ -1,4 +1,4 @@ -/* $Id: libman.h,v 1.25 2010/03/22 14:03:03 kristaps Exp $ */ +/* $Id: libman.h,v 1.26 2010/03/23 21:50:43 kristaps Exp $ */ /* * Copyright (c) 2009 Kristaps Dzonsons * @@ -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 00d42577..d09a17b6 100644 --- a/man.c +++ b/man.c @@ -1,4 +1,4 @@ -/* $Id: man.c,v 1.52 2010/03/23 11:30:48 kristaps Exp $ */ +/* $Id: man.c,v 1.53 2010/03/23 21:50:43 kristaps Exp $ */ /* * Copyright (c) 2008, 2009 Kristaps Dzonsons * @@ -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 7a752ece..9d4a7ef2 100644 --- a/man.h +++ b/man.h @@ -1,4 +1,4 @@ -/* $Id: man.h,v 1.24 2010/03/23 11:30:48 kristaps Exp $ */ +/* $Id: man.h,v 1.25 2010/03/23 21:50:43 kristaps Exp $ */ /* * Copyright (c) 2009 Kristaps Dzonsons * @@ -19,42 +19,44 @@ #include -#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 be5ca232..cee44f6e 100644 --- a/man_hash.c +++ b/man_hash.c @@ -1,4 +1,4 @@ -/* $Id: man_hash.c,v 1.16 2010/01/01 17:14:28 kristaps Exp $ */ +/* $Id: man_hash.c,v 1.17 2010/03/23 21:50:43 kristaps Exp $ */ /* * Copyright (c) 2008, 2009 Kristaps Dzonsons * @@ -21,13 +21,33 @@ #include #include +#include #include #include #include #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 1641d927..d67fdb3f 100644 --- a/man_macro.c +++ b/man_macro.c @@ -1,4 +1,4 @@ -/* $Id: man_macro.c,v 1.32 2010/03/23 11:30:48 kristaps Exp $ */ +/* $Id: man_macro.c,v 1.33 2010/03/23 21:50:43 kristaps Exp $ */ /* * Copyright (c) 2008, 2009 Kristaps Dzonsons * @@ -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) { -- cgit v1.2.3-56-ge451