tr: clean up macrobody()
[neatroff.git] / map.c
blob4ed12472f3d8f6f19c6f50bb05d4a6f2250adc87
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 struct dict mapdict;
10 static int mapinit;
12 /* map register names to [0..NREGS] */
13 int map(char *s)
15 int i;
16 if (s[0] == '.' && s[1] && !s[2]) /* ".x" is mapped to 'x' */
17 return (unsigned char) s[1];
18 if (!mapinit) {
19 dict_init(&mapdict, NREGS, -1, 1, 1);
20 mapinit = 1;
22 i = dict_idx(&mapdict, s);
23 if (i < 0) {
24 dict_put(&mapdict, s, 0);
25 i = dict_idx(&mapdict, s);
27 return MAPBEG + i;
30 /* return the name mapped to id; returns a static buffer */
31 char *map_name(int id)
33 static char map_buf[NMLEN];
34 if (id >= MAPBEG)
35 return dict_key(&mapdict, id - MAPBEG);
36 map_buf[0] = '.';
37 map_buf[1] = id;
38 map_buf[2] = '\0';
39 return map_buf;