reg: use snprintf for string values in num_str()
[neatroff.git] / map.c
blob1e6fa59dcc1fbc90daba0af177b9814b94b61889
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;
11 /* map register names to [0..NREGS] */
12 int map(char *s)
14 int i;
15 if (!s[0])
16 return 0;
17 if (s[0] == '.' && s[1] && !s[2]) /* ".x" is mapped to 'x' */
18 return (unsigned char) s[1];
19 if (!mapdict)
20 mapdict = dict_make(-1, 1, 2);
21 i = dict_idx(mapdict, s);
22 if (i < 0) {
23 dict_put(mapdict, s, 0);
24 i = dict_idx(mapdict, s);
25 if (MAPBEG + i >= NREGS)
26 errdie("neatroff: increase NREGS\n");
28 return MAPBEG + i;
31 /* return the name mapped to id; returns a static buffer */
32 char *map_name(int id)
34 static char map_buf[NMLEN];
35 if (id >= MAPBEG)
36 return dict_key(mapdict, id - MAPBEG);
37 map_buf[0] = '.';
38 map_buf[1] = id;
39 map_buf[2] = '\0';
40 return map_buf;
43 void map_done(void)
45 if (mapdict)
46 dict_free(mapdict);