-/* $Id: chars.c,v 1.1 2009/09/17 07:41:28 kristaps Exp $ */
+/* $Id: chars.c,v 1.34 2011/03/22 10:13:01 kristaps Exp $ */
/*
- * Copyright (c) 2009 Kristaps Dzonsons <kristaps@kth.se>
+ * Copyright (c) 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
+ * Copyright (c) 2011 Ingo Schwarze <schwarze@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
#include <assert.h>
-#include <err.h>
+#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include "chars.h"
+#include "mandoc.h"
+#include "out.h"
-#define ASCII_PRINT_HI 126
-#define ASCII_PRINT_LO 32
+#define PRINT_HI 126
+#define PRINT_LO 32
struct ln {
struct ln *next;
const char *code;
- const char *out;
- size_t codesz;
- size_t outsz;
+ const char *ascii;
+ int unicode;
int type;
#define CHARS_CHAR (1 << 0)
#define CHARS_STRING (1 << 1)
-#define CHARS_BOTH (0x03)
+#define CHARS_BOTH (CHARS_CHAR | CHARS_STRING)
};
-#define LINES_MAX 266
+#define LINES_MAX 351
+
+#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 },
-#define CHAR(w, x, y, z) \
- { NULL, (w), (y), (x), (z), CHARS_CHAR },
-#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 },
+#define CHAR_TBL_START static struct ln lines[LINES_MAX] = {
+#define CHAR_TBL_END };
-static struct ln lines[LINES_MAX] = {
#include "chars.in"
-};
-struct tbl {
+struct ctab {
+ enum chars type;
struct ln **htab;
};
static inline int match(const struct ln *,
const char *, size_t, int);
-static const char *find(struct tbl *, const char *,
- size_t, size_t *, int);
+static const struct ln *find(struct ctab *, const char *, size_t, int);
void
chars_free(void *arg)
{
- struct tbl *tab;
+ struct ctab *tab;
- tab = (struct tbl *)arg;
+ tab = (struct ctab *)arg;
free(tab->htab);
free(tab);
}
-/* ARGSUSED */
void *
chars_init(enum chars type)
{
- struct tbl *tab;
+ struct ctab *tab;
struct ln **htab;
struct ln *pp;
int i, hash;
* (they're in-line re-ordered during lookup).
*/
- if (NULL == (tab = malloc(sizeof(struct tbl))))
- err(1, "malloc");
-
- htab = calloc(ASCII_PRINT_HI - ASCII_PRINT_LO + 1,
- sizeof(struct ln **));
-
- if (NULL == htab)
- err(1, "malloc");
+ tab = mandoc_malloc(sizeof(struct ctab));
+ htab = mandoc_calloc(PRINT_HI - PRINT_LO + 1, sizeof(struct ln **));
for (i = 0; i < LINES_MAX; i++) {
- assert(lines[i].codesz > 0);
- assert(lines[i].code);
- assert(lines[i].out);
-
- hash = (int)lines[i].code[0] - ASCII_PRINT_LO;
+ hash = (int)lines[i].code[0] - PRINT_LO;
if (NULL == (pp = htab[hash])) {
htab[hash] = &lines[i];
for ( ; pp->next; pp = pp->next)
/* Scan ahead. */ ;
-
pp->next = &lines[i];
}
tab->htab = htab;
+ tab->type = type;
return(tab);
}
+/*
+ * Special character to Unicode codepoint.
+ */
+int
+chars_spec2cp(void *arg, const char *p, size_t sz)
+{
+ const struct ln *ln;
+
+ ln = find((struct ctab *)arg, p, sz, CHARS_CHAR);
+ if (NULL == ln)
+ return(-1);
+ return(ln->unicode);
+}
+
+
+/*
+ * Reserved word to Unicode codepoint.
+ */
+int
+chars_res2cp(void *arg, const char *p, size_t sz)
+{
+ const struct ln *ln;
+
+ ln = find((struct ctab *)arg, p, sz, CHARS_STRING);
+ if (NULL == ln)
+ return(-1);
+ return(ln->unicode);
+}
+
+
+/*
+ * Numbered character to literal character,
+ * represented as a null-terminated string for additional safety.
+ */
const char *
-chars_a2ascii(void *arg, const char *p, size_t sz, size_t *rsz)
+chars_num2char(const char *p, size_t sz)
{
+ int i;
+ static char c[2];
- return(find((struct tbl *)arg, p, sz, rsz, CHARS_CHAR));
+ if (sz > 3)
+ return(NULL);
+ i = atoi(p);
+ if (i < 0 || i > 255)
+ return(NULL);
+ c[0] = (char)i;
+ c[1] = '\0';
+ return(c);
}
+/*
+ * Special character to string array.
+ */
const char *
-chars_a2res(void *arg, const char *p, size_t sz, size_t *rsz)
+chars_spec2str(void *arg, const char *p, size_t sz, size_t *rsz)
{
+ const struct ln *ln;
+
+ ln = find((struct ctab *)arg, p, sz, CHARS_CHAR);
+ if (NULL == ln)
+ return(NULL);
- return(find((struct tbl *)arg, p, sz, rsz, CHARS_STRING));
+ *rsz = strlen(ln->ascii);
+ return(ln->ascii);
}
-static const char *
-find(struct tbl *tab, const char *p, size_t sz, size_t *rsz, int type)
+/*
+ * Reserved word to string array.
+ */
+const char *
+chars_res2str(void *arg, const char *p, size_t sz, size_t *rsz)
+{
+ const struct ln *ln;
+
+ ln = find((struct ctab *)arg, p, sz, CHARS_STRING);
+ if (NULL == ln)
+ return(NULL);
+
+ *rsz = strlen(ln->ascii);
+ return(ln->ascii);
+}
+
+
+static const struct ln *
+find(struct ctab *tab, const char *p, size_t sz, int type)
{
struct ln *pp, *prev;
struct ln **htab;
int hash;
assert(p);
- assert(sz > 0);
+ if (0 == sz)
+ return(NULL);
- if (p[0] < ASCII_PRINT_LO || p[0] > ASCII_PRINT_HI)
+ if (p[0] < PRINT_LO || p[0] > PRINT_HI)
return(NULL);
/*
* to optimise for repeat hits.
*/
- hash = (int)p[0] - ASCII_PRINT_LO;
+ hash = (int)p[0] - PRINT_LO;
htab = tab->htab;
if (NULL == (pp = htab[hash]))
return(NULL);
- if (NULL == pp->next) {
- if ( ! match(pp, p, sz, type))
- return(NULL);
- *rsz = pp->outsz;
- return(pp->out);
- }
-
for (prev = NULL; pp; pp = pp->next) {
if ( ! match(pp, p, sz, type)) {
prev = pp;
continue;
}
- /* Re-order the hash chain. */
-
if (prev) {
prev->next = pp->next;
pp->next = htab[hash];
htab[hash] = pp;
}
- *rsz = pp->outsz;
- return(pp->out);
+ return(pp);
}
return(NULL);
if ( ! (ln->type & type))
return(0);
- if (ln->codesz != sz)
+ if (strncmp(ln->code, p, sz))
return(0);
- return(0 == strncmp(ln->code, p, sz));
+ return('\0' == ln->code[(int)sz]);
}