=================================================================== RCS file: /cvs/mandoc/chars.c,v retrieving revision 1.42 retrieving revision 1.45 diff -u -p -r1.42 -r1.45 --- mandoc/chars.c 2011/05/15 15:47:46 1.42 +++ mandoc/chars.c 2011/05/24 21:24:16 1.45 @@ -1,4 +1,4 @@ -/* $Id: chars.c,v 1.42 2011/05/15 15:47:46 kristaps Exp $ */ +/* $Id: chars.c,v 1.45 2011/05/24 21:24:16 kristaps Exp $ */ /* * Copyright (c) 2009, 2010 Kristaps Dzonsons * Copyright (c) 2011 Ingo Schwarze @@ -26,6 +26,7 @@ #include #include "mandoc.h" +#include "libmandoc.h" #define PRINT_HI 126 #define PRINT_LO 32 @@ -35,20 +36,12 @@ struct ln { const char *code; const char *ascii; int unicode; - int type; -#define CHARS_CHAR (1 << 0) -#define CHARS_STRING (1 << 1) -#define CHARS_BOTH (CHARS_CHAR | CHARS_STRING) }; -#define LINES_MAX 353 +#define LINES_MAX 325 #define CHAR(in, ch, code) \ - { NULL, (in), (ch), (code), CHARS_CHAR }, -#define STRING(in, ch, code) \ - { NULL, (in), (ch), (code), CHARS_STRING }, -#define BOTH(in, ch, code) \ - { NULL, (in), (ch), (code), CHARS_BOTH }, + { NULL, (in), (ch), (code) }, #define CHAR_TBL_START static struct ln lines[LINES_MAX] = { #define CHAR_TBL_END }; @@ -59,9 +52,8 @@ struct mchars { struct ln **htab; }; -static inline int match(const struct ln *, - const char *, size_t, int); -static const struct ln *find(struct mchars *, const char *, size_t, int); +static inline int match(const struct ln *, const char *, size_t); +static const struct ln *find(struct mchars *, const char *, size_t); void mchars_free(struct mchars *arg) @@ -115,7 +107,7 @@ mchars_spec2cp(struct mchars *arg, const char *p, size { const struct ln *ln; - ln = find(arg, p, sz, CHARS_CHAR); + ln = find(arg, p, sz); if (NULL == ln) return(-1); return(ln->unicode); @@ -130,14 +122,14 @@ mchars_res2cp(struct mchars *arg, const char *p, size_ { const struct ln *ln; - ln = find(arg, p, sz, CHARS_STRING); + ln = find(arg, p, sz); if (NULL == ln) return(-1); return(ln->unicode); } /* - * Numbered character to literal character. + * Numbered character string to ASCII codepoint. * This can only be a printable character (i.e., alnum, punct, space) so * prevent the character from ruining our state (backspace, newline, and * so on). @@ -148,14 +140,26 @@ mchars_num2char(const char *p, size_t sz) { int i; - if (sz > 3) + if ((i = mandoc_strntou(p, sz, 10)) < 0) return('\0'); - - i = atoi(p); - /* LINTED */ return(isprint(i) ? i : '\0'); } +/* + * Hex character string to Unicode codepoint. + * If the character is illegal, returns '\0'. + */ +int +mchars_num2uc(const char *p, size_t sz) +{ + int i; + + if ((i = mandoc_strntou(p, sz, 16)) < 0) + return('\0'); + /* FIXME: make sure we're not in a bogus range. */ + return(i > 0x80 && i <= 0x10FFFF ? i : '\0'); +} + /* * Special character to string array. */ @@ -164,7 +168,7 @@ mchars_spec2str(struct mchars *arg, const char *p, siz { const struct ln *ln; - ln = find(arg, p, sz, CHARS_CHAR); + ln = find(arg, p, sz); if (NULL == ln) return(NULL); @@ -180,7 +184,7 @@ mchars_res2str(struct mchars *arg, const char *p, size { const struct ln *ln; - ln = find(arg, p, sz, CHARS_STRING); + ln = find(arg, p, sz); if (NULL == ln) return(NULL); @@ -189,7 +193,7 @@ mchars_res2str(struct mchars *arg, const char *p, size } static const struct ln * -find(struct mchars *tab, const char *p, size_t sz, int type) +find(struct mchars *tab, const char *p, size_t sz) { struct ln *pp, *prev; struct ln **htab; @@ -215,7 +219,7 @@ find(struct mchars *tab, const char *p, size_t sz, int return(NULL); for (prev = NULL; pp; pp = pp->next) { - if ( ! match(pp, p, sz, type)) { + if ( ! match(pp, p, sz)) { prev = pp; continue; } @@ -233,11 +237,9 @@ find(struct mchars *tab, const char *p, size_t sz, int } static inline int -match(const struct ln *ln, const char *p, size_t sz, int type) +match(const struct ln *ln, const char *p, size_t sz) { - if ( ! (ln->type & type)) - return(0); if (strncmp(ln->code, p, sz)) return(0); return('\0' == ln->code[(int)sz]);