version 1.1, 2009/09/17 07:41:28 |
version 1.61, 2014/10/26 18:07:28 |
|
|
/* $Id$ */ |
/* $Id$ */ |
/* |
/* |
* Copyright (c) 2009 Kristaps Dzonsons <kristaps@kth.se> |
* Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv> |
|
* Copyright (c) 2011, 2014 Ingo Schwarze <schwarze@openbsd.org> |
* |
* |
* Permission to use, copy, modify, and distribute this software for any |
* Permission to use, copy, modify, and distribute this software for any |
* purpose with or without fee is hereby granted, provided that the above |
* purpose with or without fee is hereby granted, provided that the above |
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
*/ |
*/ |
|
#include "config.h" |
|
|
|
#include <sys/types.h> |
|
|
#include <assert.h> |
#include <assert.h> |
#include <err.h> |
#include <ctype.h> |
#include <stdlib.h> |
#include <stdlib.h> |
#include <string.h> |
#include <string.h> |
|
|
#include "chars.h" |
#include "mandoc.h" |
|
#include "mandoc_aux.h" |
|
#include "libmandoc.h" |
|
|
#define ASCII_PRINT_HI 126 |
#define PRINT_HI 126 |
#define ASCII_PRINT_LO 32 |
#define PRINT_LO 32 |
|
|
struct ln { |
struct ln { |
struct ln *next; |
struct ln *next; |
const char *code; |
const char *code; |
const char *out; |
const char *ascii; |
size_t codesz; |
int unicode; |
size_t outsz; |
|
int type; |
|
#define CHARS_CHAR (1 << 0) |
|
#define CHARS_STRING (1 << 1) |
|
#define CHARS_BOTH (0x03) |
|
}; |
}; |
|
|
#define LINES_MAX 266 |
#define LINES_MAX 330 |
|
|
#define CHAR(w, x, y, z) \ |
#define CHAR(in, ch, code) \ |
{ NULL, (w), (y), (x), (z), CHARS_CHAR }, |
{ NULL, (in), (ch), (code) }, |
#define STRING(w, x, y, z) \ |
|
{ NULL, (w), (y), (x), (z), CHARS_STRING }, |
|
#define BOTH(w, x, y, z) \ |
|
{ NULL, (w), (y), (x), (z), CHARS_BOTH }, |
|
|
|
static struct ln lines[LINES_MAX] = { |
#define CHAR_TBL_START static struct ln lines[LINES_MAX] = { |
|
#define CHAR_TBL_END }; |
|
|
#include "chars.in" |
#include "chars.in" |
}; |
|
|
|
struct tbl { |
struct mchars { |
struct ln **htab; |
struct ln **htab; |
}; |
}; |
|
|
static inline int match(const struct ln *, |
static const struct ln *find(const struct mchars *, |
const char *, size_t, int); |
const char *, size_t); |
static const char *find(struct tbl *, const char *, |
|
size_t, size_t *, int); |
|
|
|
|
|
void |
void |
chars_free(void *arg) |
mchars_free(struct mchars *arg) |
{ |
{ |
struct tbl *tab; |
|
|
|
tab = (struct tbl *)arg; |
free(arg->htab); |
|
free(arg); |
free(tab->htab); |
|
free(tab); |
|
} |
} |
|
|
|
struct mchars * |
/* ARGSUSED */ |
mchars_alloc(void) |
void * |
|
chars_init(enum chars type) |
|
{ |
{ |
struct tbl *tab; |
struct mchars *tab; |
struct ln **htab; |
struct ln **htab; |
struct ln *pp; |
struct ln *pp; |
int i, hash; |
int i, hash; |
Line 83 chars_init(enum chars type) |
|
Line 75 chars_init(enum chars type) |
|
/* |
/* |
* Constructs a very basic chaining hashtable. The hash routine |
* Constructs a very basic chaining hashtable. The hash routine |
* is simply the integral value of the first character. |
* is simply the integral value of the first character. |
* Subsequent entries are chained in the order they're processed |
* Subsequent entries are chained in the order they're processed. |
* (they're in-line re-ordered during lookup). |
|
*/ |
*/ |
|
|
if (NULL == (tab = malloc(sizeof(struct tbl)))) |
tab = mandoc_malloc(sizeof(struct mchars)); |
err(1, "malloc"); |
htab = mandoc_calloc(PRINT_HI - PRINT_LO + 1, sizeof(struct ln *)); |
|
|
htab = calloc(ASCII_PRINT_HI - ASCII_PRINT_LO + 1, |
|
sizeof(struct ln **)); |
|
|
|
if (NULL == htab) |
|
err(1, "malloc"); |
|
|
|
for (i = 0; i < LINES_MAX; i++) { |
for (i = 0; i < LINES_MAX; i++) { |
assert(lines[i].codesz > 0); |
hash = (int)lines[i].code[0] - PRINT_LO; |
assert(lines[i].code); |
|
assert(lines[i].out); |
|
|
|
hash = (int)lines[i].code[0] - ASCII_PRINT_LO; |
|
|
|
if (NULL == (pp = htab[hash])) { |
if (NULL == (pp = htab[hash])) { |
htab[hash] = &lines[i]; |
htab[hash] = &lines[i]; |
continue; |
continue; |
Line 110 chars_init(enum chars type) |
|
Line 91 chars_init(enum chars type) |
|
|
|
for ( ; pp->next; pp = pp->next) |
for ( ; pp->next; pp = pp->next) |
/* Scan ahead. */ ; |
/* Scan ahead. */ ; |
|
|
pp->next = &lines[i]; |
pp->next = &lines[i]; |
} |
} |
|
|
Line 118 chars_init(enum chars type) |
|
Line 98 chars_init(enum chars type) |
|
return(tab); |
return(tab); |
} |
} |
|
|
|
int |
const char * |
mchars_spec2cp(const struct mchars *arg, const char *p, size_t sz) |
chars_a2ascii(void *arg, const char *p, size_t sz, size_t *rsz) |
|
{ |
{ |
|
const struct ln *ln; |
|
|
return(find((struct tbl *)arg, p, sz, rsz, CHARS_CHAR)); |
ln = find(arg, p, sz); |
|
return(ln != NULL ? ln->unicode : sz == 1 ? *p : -1); |
} |
} |
|
|
|
char |
const char * |
mchars_num2char(const char *p, size_t sz) |
chars_a2res(void *arg, const char *p, size_t sz, size_t *rsz) |
|
{ |
{ |
|
int i; |
|
|
return(find((struct tbl *)arg, p, sz, rsz, CHARS_STRING)); |
if ((i = mandoc_strntoi(p, sz, 10)) < 0) |
|
return('\0'); |
|
|
|
return(i > 0 && i < 256 && isprint(i) ? i : '\0'); |
} |
} |
|
|
|
int |
static const char * |
mchars_num2uc(const char *p, size_t sz) |
find(struct tbl *tab, const char *p, size_t sz, size_t *rsz, int type) |
|
{ |
{ |
struct ln *pp, *prev; |
int i; |
struct ln **htab; |
|
int hash; |
|
|
|
assert(p); |
if ((i = mandoc_strntoi(p, sz, 16)) < 0) |
assert(sz > 0); |
return(0xFFFD); |
|
|
if (p[0] < ASCII_PRINT_LO || p[0] > ASCII_PRINT_HI) |
|
return(NULL); |
|
|
|
/* |
/* |
* Lookup the symbol in the symbol hash. See ascii2htab for the |
* XXX Code is missing here to exclude bogus ranges. |
* hashtable specs. This dynamically re-orders the hash chain |
|
* to optimise for repeat hits. |
|
*/ |
*/ |
|
|
hash = (int)p[0] - ASCII_PRINT_LO; |
return(i <= 0x10FFFF ? i : 0xFFFD); |
htab = tab->htab; |
} |
|
|
if (NULL == (pp = htab[hash])) |
const char * |
return(NULL); |
mchars_spec2str(const struct mchars *arg, |
|
const char *p, size_t sz, size_t *rsz) |
|
{ |
|
const struct ln *ln; |
|
|
if (NULL == pp->next) { |
ln = find(arg, p, sz); |
if ( ! match(pp, p, sz, type)) |
if (ln == NULL) { |
return(NULL); |
*rsz = 1; |
*rsz = pp->outsz; |
return(sz == 1 ? p : NULL); |
return(pp->out); |
|
} |
} |
|
|
for (prev = NULL; pp; pp = pp->next) { |
*rsz = strlen(ln->ascii); |
if ( ! match(pp, p, sz, type)) { |
return(ln->ascii); |
prev = pp; |
} |
continue; |
|
} |
|
|
|
/* Re-order the hash chain. */ |
const char * |
|
mchars_uc2str(int uc) |
|
{ |
|
int i; |
|
|
if (prev) { |
for (i = 0; i < LINES_MAX; i++) |
prev->next = pp->next; |
if (uc == lines[i].unicode) |
pp->next = htab[hash]; |
return(lines[i].ascii); |
htab[hash] = pp; |
return("<?>"); |
} |
} |
|
|
*rsz = pp->outsz; |
static const struct ln * |
return(pp->out); |
find(const struct mchars *tab, const char *p, size_t sz) |
} |
{ |
|
const struct ln *pp; |
|
int hash; |
|
|
return(NULL); |
assert(p); |
} |
|
|
|
|
if (0 == sz || p[0] < PRINT_LO || p[0] > PRINT_HI) |
|
return(NULL); |
|
|
static inline int |
hash = (int)p[0] - PRINT_LO; |
match(const struct ln *ln, const char *p, size_t sz, int type) |
|
{ |
|
|
|
if ( ! (ln->type & type)) |
for (pp = tab->htab[hash]; pp; pp = pp->next) |
return(0); |
if (0 == strncmp(pp->code, p, sz) && |
if (ln->codesz != sz) |
'\0' == pp->code[(int)sz]) |
return(0); |
return(pp); |
return(0 == strncmp(ln->code, p, sz)); |
|
|
return(NULL); |
} |
} |