diff options
-rw-r--r-- | Makefile | 12 | ||||
-rw-r--r-- | chars.c (renamed from ascii.c) | 118 | ||||
-rw-r--r-- | chars.h | 34 | ||||
-rw-r--r-- | chars.in (renamed from ascii.in) | 3 | ||||
-rw-r--r-- | html.c | 32 | ||||
-rw-r--r-- | man_hash.c | 7 | ||||
-rw-r--r-- | mdoc_hash.c | 7 | ||||
-rw-r--r-- | term.c | 11 | ||||
-rw-r--r-- | term.h | 5 |
9 files changed, 136 insertions, 93 deletions
@@ -35,11 +35,11 @@ MANOBJS = man_macro.o man.o man_hash.o man_validate.o \ MANSRCS = man_macro.c man.c man_hash.c man_validate.c \ man_action.c mandoc.c man_argv.c -MAINLNS = main.ln mdoc_term.ln ascii.ln term.ln tree.ln \ +MAINLNS = main.ln mdoc_term.ln chars.ln term.ln tree.ln \ compat.ln man_term.ln html.ln -MAINOBJS = main.o mdoc_term.o ascii.o term.o tree.o compat.o \ +MAINOBJS = main.o mdoc_term.o chars.o term.o tree.o compat.o \ man_term.o html.o -MAINSRCS = main.c mdoc_term.c ascii.c term.c tree.c compat.c \ +MAINSRCS = main.c mdoc_term.c chars.c term.c tree.c compat.c \ man_term.c html.c LLNS = llib-llibmdoc.ln llib-llibman.ln llib-lmandoc.ln @@ -47,7 +47,7 @@ LNS = $(MAINLNS) $(MDOCLNS) $(MANLNS) LIBS = libmdoc.a libman.a OBJS = $(MDOCOBJS) $(MAINOBJS) $(MANOBJS) SRCS = $(MDOCSRCS) $(MAINSRCS) $(MANSRCS) -DATAS = arch.in att.in lib.in msec.in st.in vol.in ascii.in +DATAS = arch.in att.in lib.in msec.in st.in vol.in chars.in HEADS = mdoc.h libmdoc.h man.h libman.h term.h libmandoc.h SGMLS = index.sgml XSLS = ChangeLog.xsl @@ -117,8 +117,8 @@ arch.o: arch.c arch.in libmdoc.h vol.ln: vol.c vol.in libmdoc.h vol.o: vol.c vol.in libmdoc.h -ascii.ln: ascii.c ascii.in term.h -ascii.o: ascii.c ascii.in term.h +chars.ln: chars.c chars.in chars.h +chars.o: chars.c chars.in chars.h msec.ln: msec.c msec.in libmdoc.h msec.o: msec.c msec.in libmdoc.h @@ -19,69 +19,66 @@ #include <stdlib.h> #include <string.h> -#include "term.h" +#include "chars.h" #define ASCII_PRINT_HI 126 #define ASCII_PRINT_LO 32 -struct line { +struct ln { + struct ln *next; const char *code; const char *out; size_t codesz; size_t outsz; int type; -#define ASCII_CHAR (1 << 0) -#define ASCII_STRING (1 << 1) -#define ASCII_BOTH (0x03) +#define CHARS_CHAR (1 << 0) +#define CHARS_STRING (1 << 1) +#define CHARS_BOTH (0x03) }; -struct linep { - const struct line *line; - struct linep *next; -}; +#define LINES_MAX 266 #define CHAR(w, x, y, z) \ - { (w), (y), (x), (z), ASCII_CHAR }, + { NULL, (w), (y), (x), (z), CHARS_CHAR }, #define STRING(w, x, y, z) \ - { (w), (y), (x), (z), ASCII_STRING }, + { NULL, (w), (y), (x), (z), CHARS_STRING }, #define BOTH(w, x, y, z) \ - { (w), (y), (x), (z), ASCII_BOTH }, -static const struct line lines[] = { -#include "ascii.in" -}; + { NULL, (w), (y), (x), (z), CHARS_BOTH }, -struct asciitab { - struct linep *lines; - void **htab; +static struct ln lines[LINES_MAX] = { +#include "chars.in" }; +struct tbl { + struct ln **htab; +}; -static inline int match(const struct line *, +static inline int match(const struct ln *, const char *, size_t, int); -static const char * lookup(struct asciitab *, const char *, +static const char *find(struct tbl *, const char *, size_t, size_t *, int); void -term_asciifree(void *arg) +chars_free(void *arg) { - struct asciitab *tab; + struct tbl *tab; - tab = (struct asciitab *)arg; + tab = (struct tbl *)arg; - free(tab->lines); free(tab->htab); free(tab); } +/* ARGSUSED */ void * -term_ascii2htab(void) +chars_init(enum chars type) { - struct asciitab *tab; - void **htab; - struct linep *pp, *p; - int i, len, hash; + struct tbl *tab; + struct ln **htab; + struct ln *pp; + int i, hash; /* * Constructs a very basic chaining hashtable. The hash routine @@ -90,71 +87,59 @@ term_ascii2htab(void) * (they're in-line re-ordered during lookup). */ - if (NULL == (tab = malloc(sizeof(struct asciitab)))) - err(1, "malloc"); - - len = sizeof(lines) / sizeof(struct line); - - if (NULL == (p = calloc((size_t)len, sizeof(struct linep)))) + if (NULL == (tab = malloc(sizeof(struct tbl)))) err(1, "malloc"); htab = calloc(ASCII_PRINT_HI - ASCII_PRINT_LO + 1, - sizeof(struct linep **)); + sizeof(struct ln **)); if (NULL == htab) err(1, "malloc"); - for (i = 0; i < len; i++) { + for (i = 0; i < LINES_MAX; i++) { assert(lines[i].codesz > 0); assert(lines[i].code); assert(lines[i].out); - p[i].line = &lines[i]; - hash = (int)lines[i].code[0] - ASCII_PRINT_LO; - if (NULL == (pp = ((struct linep **)htab)[hash])) { - htab[hash] = &p[i]; + if (NULL == (pp = htab[hash])) { + htab[hash] = &lines[i]; continue; } for ( ; pp->next; pp = pp->next) /* Scan ahead. */ ; - pp->next = &p[i]; + pp->next = &lines[i]; } tab->htab = htab; - tab->lines = p; - return(tab); } const char * -term_a2ascii(void *arg, const char *p, size_t sz, size_t *rsz) +chars_a2ascii(void *arg, const char *p, size_t sz, size_t *rsz) { - return(lookup((struct asciitab *)arg, p, - sz, rsz, ASCII_CHAR)); + return(find((struct tbl *)arg, p, sz, rsz, CHARS_CHAR)); } const char * -term_a2res(void *arg, const char *p, size_t sz, size_t *rsz) +chars_a2res(void *arg, const char *p, size_t sz, size_t *rsz) { - return(lookup((struct asciitab *)arg, p, - sz, rsz, ASCII_STRING)); + return(find((struct tbl *)arg, p, sz, rsz, CHARS_STRING)); } static const char * -lookup(struct asciitab *tab, const char *p, - size_t sz, size_t *rsz, int type) +find(struct tbl *tab, const char *p, size_t sz, size_t *rsz, int type) { - struct linep *pp, *prev; - void **htab; + struct ln *pp, *prev; + struct ln **htab; int hash; assert(p); @@ -163,7 +148,6 @@ lookup(struct asciitab *tab, const char *p, if (p[0] < ASCII_PRINT_LO || p[0] > ASCII_PRINT_HI) return(NULL); - /* * Lookup the symbol in the symbol hash. See ascii2htab for the * hashtable specs. This dynamically re-orders the hash chain @@ -173,18 +157,18 @@ lookup(struct asciitab *tab, const char *p, hash = (int)p[0] - ASCII_PRINT_LO; htab = tab->htab; - if (NULL == (pp = ((struct linep **)htab)[hash])) + if (NULL == (pp = htab[hash])) return(NULL); if (NULL == pp->next) { - if ( ! match(pp->line, p, sz, type)) + if ( ! match(pp, p, sz, type)) return(NULL); - *rsz = pp->line->outsz; - return(pp->line->out); + *rsz = pp->outsz; + return(pp->out); } for (prev = NULL; pp; pp = pp->next) { - if ( ! match(pp->line, p, sz, type)) { + if ( ! match(pp, p, sz, type)) { prev = pp; continue; } @@ -193,12 +177,12 @@ lookup(struct asciitab *tab, const char *p, if (prev) { prev->next = pp->next; - pp->next = ((struct linep **)htab)[hash]; + pp->next = htab[hash]; htab[hash] = pp; } - *rsz = pp->line->outsz; - return(pp->line->out); + *rsz = pp->outsz; + return(pp->out); } return(NULL); @@ -206,12 +190,12 @@ lookup(struct asciitab *tab, const char *p, static inline int -match(const struct line *line, const char *p, size_t sz, int type) +match(const struct ln *ln, const char *p, size_t sz, int type) { - if ( ! (line->type & type)) + if ( ! (ln->type & type)) return(0); - if (line->codesz != sz) + if (ln->codesz != sz) return(0); - return(0 == strncmp(line->code, p, sz)); + return(0 == strncmp(ln->code, p, sz)); } diff --git a/chars.h b/chars.h new file mode 100644 index 00000000..a18c2880 --- /dev/null +++ b/chars.h @@ -0,0 +1,34 @@ +/* $Id$ */ +/* + * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ +#ifndef CHARS_H +#define CHARS_H + +__BEGIN_DECLS + +enum chars { + CHARS_ASCII, + CHARS_HTML +}; + +void *chars_init(enum chars); +const char *chars_a2ascii(void *, const char *, size_t, size_t *); +const char *chars_a2res(void *, const char *, size_t, size_t *); +void chars_free(void *); + +__END_DECLS + +#endif /*!CHARS_H*/ @@ -26,7 +26,8 @@ * right-hand side is what's produced by the front-end, with the fourth * element being its length. * - * Be sure to C-escape strings! + * XXX - C-escape strings! + * XXX - update LINES_MAX if adding more! */ STRING("Am", 2, "&", 1) @@ -138,9 +138,11 @@ static void print_encode(const char *); static void print_text(struct html *, const char *); static int mdoc_root_pre(MDOC_ARGS); +static int mdoc_ar_pre(MDOC_ARGS); static int mdoc_fl_pre(MDOC_ARGS); static int mdoc_nd_pre(MDOC_ARGS); static int mdoc_nm_pre(MDOC_ARGS); +static int mdoc_ns_pre(MDOC_ARGS); static int mdoc_op_pre(MDOC_ARGS); static void mdoc_op_post(MDOC_ARGS); static int mdoc_pp_pre(MDOC_ARGS); @@ -165,7 +167,7 @@ static const struct htmlmdoc mdocs[MDOC_MAX] = { {NULL, NULL}, /* It */ {NULL, NULL}, /* Ad */ {NULL, NULL}, /* An */ - {NULL, NULL}, /* Ar */ + {mdoc_ar_pre, NULL}, /* Ar */ {NULL, NULL}, /* Cd */ {NULL, NULL}, /* Cm */ {NULL, NULL}, /* Dv */ @@ -222,7 +224,7 @@ static const struct htmlmdoc mdocs[MDOC_MAX] = { {NULL, NULL}, /* Fx */ {NULL, NULL}, /* Ms */ {NULL, NULL}, /* No */ - {NULL, NULL}, /* Ns */ + {mdoc_ns_pre, NULL}, /* Ns */ {NULL, NULL}, /* Nx */ {NULL, NULL}, /* Ox */ {NULL, NULL}, /* Pc */ @@ -697,7 +699,7 @@ mdoc_nd_pre(MDOC_ARGS) { if (MDOC_BODY == n->type) - print_text(h, "--"); + print_text(h, "\\(en"); return(1); } @@ -785,3 +787,27 @@ mdoc_xr_pre(MDOC_ARGS) return(0); } + + +/* ARGSUSED */ +static int +mdoc_ns_pre(MDOC_ARGS) +{ + + h->flags |= HTML_NOSPACE; + return(1); +} + + +/* ARGSUSED */ +static int +mdoc_ar_pre(MDOC_ARGS) +{ + struct htmlpair tag; + + tag.key = ATTR_CLASS; + tag.val = "arg"; + + print_otag(h, TAG_SPAN, 1, &tag); + return(1); +} @@ -21,10 +21,12 @@ #include "libman.h" - static u_char table[26 * 6]; - +/* + * XXX - this hash has global scope, so if intended for use as a library + * with multiple callers, it will need re-invocation protection. + */ void man_hash_init(void) { @@ -49,7 +51,6 @@ man_hash_init(void) } } - int man_hash_find(const char *tmp) { diff --git a/mdoc_hash.c b/mdoc_hash.c index 4088e79a..be2d457a 100644 --- a/mdoc_hash.c +++ b/mdoc_hash.c @@ -27,7 +27,10 @@ static u_char table[27 * 12]; - +/* + * XXX - this hash has global scope, so if intended for use as a library + * with multiple callers, it will need re-invocation protection. + */ void mdoc_hash_init(void) { @@ -54,7 +57,6 @@ mdoc_hash_init(void) } } - int mdoc_hash_find(const char *p) { @@ -84,4 +86,3 @@ mdoc_hash_find(const char *p) return(MDOC_MAX); } - @@ -20,6 +20,7 @@ #include <stdlib.h> #include <string.h> +#include "chars.h" #include "term.h" #include "man.h" #include "mdoc.h" @@ -56,7 +57,7 @@ terminal_man(void *arg, const struct man *man) p = (struct termp *)arg; if (NULL == p->symtab) - p->symtab = term_ascii2htab(); + p->symtab = chars_init(CHARS_ASCII); man_run(p, man); } @@ -69,7 +70,7 @@ terminal_mdoc(void *arg, const struct mdoc *mdoc) p = (struct termp *)arg; if (NULL == p->symtab) - p->symtab = term_ascii2htab(); + p->symtab = chars_init(CHARS_ASCII); mdoc_run(p, mdoc); } @@ -90,7 +91,7 @@ term_free(struct termp *p) if (p->buf) free(p->buf); if (TERMENC_ASCII == p->enc && p->symtab) - term_asciifree(p->symtab); + chars_free(p->symtab); free(p); } @@ -331,7 +332,7 @@ do_special(struct termp *p, const char *word, size_t len) size_t sz; int i; - rhs = term_a2ascii(p->symtab, word, len, &sz); + rhs = chars_a2ascii(p->symtab, word, len, &sz); if (NULL == rhs) { #if 0 @@ -354,7 +355,7 @@ do_reserved(struct termp *p, const char *word, size_t len) size_t sz; int i; - rhs = term_a2res(p->symtab, word, len, &sz); + rhs = chars_a2res(p->symtab, word, len, &sz); if (NULL == rhs) { #if 0 @@ -48,11 +48,6 @@ struct termp { void *symtab; /* Encoded-symbol table. */ }; -void *term_ascii2htab(void); -const char *term_a2ascii(void *, const char *, size_t, size_t *); -const char *term_a2res(void *, const char *, size_t, size_t *); -void term_asciifree(void *); - void term_newln(struct termp *); void term_vspace(struct termp *); void term_word(struct termp *, const char *); |