Makefile: compatibility with openbsd
[neateqn.git] / reg.c
bloba88747567bcd9c955232d72c1acf7daedf1a4812
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "eqn.h"
6 #define NREGS 2048
7 #define EPREFIX ""
9 static int sreg_max; /* maximum allocated string register */
10 static int sreg_free[NREGS]; /* free string registers */
11 static int sreg_n; /* number of items in sreg_free[] */
12 static char sreg_name[NREGS][RLEN];
13 static char sreg_read[NREGS][RLEN];
15 static int nreg_max;
16 static int nreg_free[NREGS];
17 static int nreg_n;
18 static char nreg_name[NREGS][RLEN];
19 static char nreg_read[NREGS][RLEN];
21 /* allocate a troff string register */
22 int sregmk(void)
24 int id = sreg_n ? sreg_free[--sreg_n] : ++sreg_max;
25 sprintf(sreg_name[id], "%s%02d", EPREFIX, id);
26 sprintf(sreg_read[id], "\\*%s", escarg(sreg_name[id]));
27 return id;
30 /* free a troff string register */
31 void sregrm(int id)
33 sreg_free[sreg_n++] = id;
36 char *sregname(int id)
38 return sreg_name[id];
41 char *sreg(int id)
43 return sreg_read[id];
46 /* allocate a troff number register */
47 int nregmk(void)
49 int id = nreg_n ? nreg_free[--nreg_n] : ++nreg_max;
50 sprintf(nreg_name[id], "%s%02d", EPREFIX, id);
51 sprintf(nreg_read[id], "\\n%s", escarg(nreg_name[id]));
52 return id;
55 /* free a troff number register */
56 void nregrm(int id)
58 nreg_free[nreg_n++] = id;
61 char *nregname(int id)
63 return nreg_name[id];
66 char *nreg(int id)
68 return nreg_read[id];
71 /* free all allocated registers */
72 void reg_reset(void)
74 nreg_max = 0;
75 nreg_n = 0;
76 sreg_max = 11;
77 sreg_n = 0;
80 /* format the argument of a troff escape like \s or \f */
81 char *escarg(char *arg)
83 static char buf[256];
84 if (!arg[1])
85 sprintf(buf, "%c", arg[0]);
86 else if (!arg[2])
87 sprintf(buf, "(%c%c", arg[0], arg[1]);
88 else
89 sprintf(buf, "[%s]", arg);
90 return buf;