=================================================================== RCS file: /cvs/mandoc/Attic/ascii.c,v retrieving revision 1.2 retrieving revision 1.9 diff -u -p -r1.2 -r1.9 --- mandoc/Attic/ascii.c 2009/03/17 13:35:46 1.2 +++ mandoc/Attic/ascii.c 2009/07/27 12:02:49 1.9 @@ -1,20 +1,18 @@ -/* $Id: ascii.c,v 1.2 2009/03/17 13:35:46 kristaps Exp $ */ +/* $Id: ascii.c,v 1.9 2009/07/27 12:02:49 kristaps Exp $ */ /* - * Copyright (c) 2009 Kristaps Dzonsons + * Copyright (c) 2009 Kristaps Dzonsons * * 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. + * 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. + * 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. */ #include #include @@ -26,18 +24,15 @@ #define ASCII_PRINT_HI 126 #define ASCII_PRINT_LO 32 -/* - * Lookup and hashing routines for constructing the ASCII symbol table, - * which should contain a significant portion of mdoc(7)'s special - * symbols. - */ - struct line { const char *code; const char *out; - /* 32- and 64-bit alignment safe. */ size_t codesz; size_t outsz; + int type; +#define ASCII_CHAR (1 << 0) +#define ASCII_STRING (1 << 1) +#define ASCII_BOTH (0x03) }; struct linep { @@ -45,8 +40,12 @@ struct linep { struct linep *next; }; -#define LINE(w, x, y, z) \ - { (w), (y), (x), (z) }, +#define CHAR(w, x, y, z) \ + { (w), (y), (x), (z), ASCII_CHAR }, +#define STRING(w, x, y, z) \ + { (w), (y), (x), (z), ASCII_STRING }, +#define BOTH(w, x, y, z) \ + { (w), (y), (x), (z), ASCII_BOTH }, static const struct line lines[] = { #include "ascii.in" }; @@ -58,11 +57,13 @@ struct asciitab { static inline int match(const struct line *, - const char *, size_t); + const char *, size_t, int); +static const char * lookup(struct asciitab *, const char *, + size_t, size_t *, int); void -asciifree(void *arg) +term_asciifree(void *arg) { struct asciitab *tab; @@ -75,7 +76,7 @@ asciifree(void *arg) void * -ascii2htab(void) +term_ascii2htab(void) { struct asciitab *tab; void **htab; @@ -92,7 +93,6 @@ ascii2htab(void) if (NULL == (tab = malloc(sizeof(struct asciitab)))) err(1, "malloc"); - assert(0 == sizeof(lines) % sizeof(struct line)); len = sizeof(lines) / sizeof(struct line); if (NULL == (p = calloc((size_t)len, sizeof(struct linep)))) @@ -132,20 +132,38 @@ ascii2htab(void) const char * -a2ascii(void *arg, const char *p, size_t sz, size_t *rsz) +term_a2ascii(void *arg, const char *p, size_t sz, size_t *rsz) { - struct asciitab *tab; + + return(lookup((struct asciitab *)arg, p, + sz, rsz, ASCII_CHAR)); +} + + +const char * +term_a2res(void *arg, const char *p, size_t sz, size_t *rsz) +{ + + return(lookup((struct asciitab *)arg, p, + sz, rsz, ASCII_STRING)); +} + + +static const char * +lookup(struct asciitab *tab, const char *p, + size_t sz, size_t *rsz, int type) +{ struct linep *pp, *prev; void **htab; int hash; - tab = (struct asciitab *)arg; - htab = tab->htab; - assert(p); assert(sz > 0); - assert(p[0] >= ASCII_PRINT_LO && p[0] <= ASCII_PRINT_HI); + 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 @@ -153,19 +171,20 @@ a2ascii(void *arg, const char *p, size_t sz, size_t *r */ hash = (int)p[0] - ASCII_PRINT_LO; + htab = tab->htab; if (NULL == (pp = ((struct linep **)htab)[hash])) return(NULL); if (NULL == pp->next) { - if ( ! match(pp->line, p, sz)) + if ( ! match(pp->line, p, sz, type)) return(NULL); *rsz = pp->line->outsz; return(pp->line->out); } for (prev = NULL; pp; pp = pp->next) { - if ( ! match(pp->line, p, sz)) { + if ( ! match(pp->line, p, sz, type)) { prev = pp; continue; } @@ -187,9 +206,11 @@ a2ascii(void *arg, const char *p, size_t sz, size_t *r static inline int -match(const struct line *line, const char *p, size_t sz) +match(const struct line *line, const char *p, size_t sz, int type) { + if ( ! (line->type & type)) + return(0); if (line->codesz != sz) return(0); return(0 == strncmp(line->code, p, sz));