shithub: neatroff

ref: 3421776cece57041488f5a8c30f8906cf2929595
dir: /map.c/

View raw version
/* mapping register/macro names to indices */
#include <stdio.h>
#include <string.h>
#include "roff.h"

#define MAPBEG		256	/* the entries reserved for .x names */

/* register, macro, or environments names */
static struct dict mapdict;
static int mapinit;

/* map register names to [0..NREGS] */
int map(char *s)
{
	int i;
	if (s[0] == '.' && s[1] && !s[2])	/* ".x" is mapped to 'x' */
		return (unsigned char) s[1];
	if (!mapinit) {
		dict_init(&mapdict, NREGS, -1, 1, 1);
		mapinit = 1;
	}
	i = dict_idx(&mapdict, s);
	if (i < 0) {
		dict_put(&mapdict, s, 0);
		i = dict_idx(&mapdict, s);
	}
	return MAPBEG + i;
}

/* return the name mapped to id; returns a static buffer */
char *map_name(int id)
{
	static char map_buf[NMLEN];
	if (id >= MAPBEG)
		return dict_key(&mapdict, id - MAPBEG);
	map_buf[0] = '.';
	map_buf[1] = id;
	map_buf[2] = '\0';
	return map_buf;
}