fmt: new hyphenation support with penalties
[neatroff.git] / map.c
blobad5348a73f63f595ef69f8cbf38fad7959c30671
1 /* mapping register/macro names to indices */
2 #include <stdio.h>
3 #include <string.h>
4 #include "roff.h"
6 #define MAPBEG 256 /* the entries reserved for .x names */
8 /* register, macro, or environments names */
9 static char keys[NREGS][GNLEN];
10 static int nkeys = 1;
11 /* per starting character name lists */
12 static int key_head[256];
13 static int key_next[NREGS];
15 /* return the index of s in keys[]; insert it if not in keys[] */
16 static int key_get(char *s)
18 int head = (unsigned char) s[0];
19 int i = key_head[head];
20 if (*s == '\0')
21 return 0;
22 while (i > 0) {
23 if (keys[i][1] == s[1] && !strcmp(keys[i], s))
24 return i;
25 i = key_next[i];
27 if (nkeys >= NREGS - MAPBEG)
28 errdie("neatroff: out of register names (NREGS)\n");
29 i = nkeys++;
30 strcpy(keys[i], s);
31 key_next[i] = key_head[head];
32 key_head[head] = i;
33 return i;
36 /* map register names to [0..NREGS] */
37 int map(char *s)
39 if (s[0] == '.' && s[1] && !s[2]) /* ".x" is mapped to 'x' */
40 return (unsigned char) s[1];
41 return MAPBEG + key_get(s);
44 /* return the name mapped to id; returns a static buffer */
45 char *map_name(int id)
47 static char map_buf[NMLEN];
48 if (id >= MAPBEG)
49 return keys[id - MAPBEG];
50 map_buf[0] = '.';
51 map_buf[1] = id;
52 map_buf[2] = '\0';
53 return map_buf;