-
Notifications
You must be signed in to change notification settings - Fork 10
/
reg.c
91 lines (78 loc) · 1.68 KB
/
reg.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "eqn.h"
#define NREGS 2048
#define EPREFIX ""
static int sreg_max; /* maximum allocated string register */
static int sreg_free[NREGS]; /* free string registers */
static int sreg_n; /* number of items in sreg_free[] */
static char sreg_name[NREGS][RLEN];
static char sreg_read[NREGS][RLEN];
static int nreg_max;
static int nreg_free[NREGS];
static int nreg_n;
static char nreg_name[NREGS][RLEN];
static char nreg_read[NREGS][RLEN];
/* allocate a troff string register */
int sregmk(void)
{
int id = sreg_n ? sreg_free[--sreg_n] : ++sreg_max;
sprintf(sreg_name[id], "%s%02d", EPREFIX, id);
sprintf(sreg_read[id], "\\*%s", escarg(sreg_name[id]));
return id;
}
/* free a troff string register */
void sregrm(int id)
{
sreg_free[sreg_n++] = id;
}
char *sregname(int id)
{
return sreg_name[id];
}
char *sreg(int id)
{
return sreg_read[id];
}
/* allocate a troff number register */
int nregmk(void)
{
int id = nreg_n ? nreg_free[--nreg_n] : ++nreg_max;
sprintf(nreg_name[id], "%s%02d", EPREFIX, id);
sprintf(nreg_read[id], "\\n%s", escarg(nreg_name[id]));
return id;
}
/* free a troff number register */
void nregrm(int id)
{
nreg_free[nreg_n++] = id;
}
char *nregname(int id)
{
return nreg_name[id];
}
char *nreg(int id)
{
return nreg_read[id];
}
/* free all allocated registers */
void reg_reset(void)
{
nreg_max = 0;
nreg_n = 0;
sreg_max = 11;
sreg_n = 0;
}
/* format the argument of a troff escape like \s or \f */
char *escarg(char *arg)
{
static char buf[256];
if (!arg[1])
sprintf(buf, "%c", arg[0]);
else if (!arg[2])
sprintf(buf, "(%c%c", arg[0], arg[1]);
else
sprintf(buf, "[%s]", arg);
return buf;
}