forked from aligrudi/neatroff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cp.c
301 lines (284 loc) · 5.38 KB
/
cp.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/* copy-mode character interpretation */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "roff.h"
static int cp_blkdep; /* input block depth (text in \{ and \}) */
static int cp_cpmode; /* disable the interpretation of \w and \E */
static int cp_reqdep; /* the block depth of current request line */
/* just like cp_next(), but remove c_ni characters */
static int cp_noninext(void)
{
int c = cp_next();
while (c == c_ni)
c = cp_next();
return c;
}
/* return 1 if \*[] includes a space */
static int cparg(char *d, int len)
{
int c = cp_noninext();
int i = 0;
if (c == '(') {
i += utf8next(d + i, cp_noninext);
i += utf8next(d + i, cp_noninext);
} else if (!n_cp && c == '[') {
c = cp_noninext();
while (c >= 0 && c != ']' && c != ' ') {
if (i + 1 < len)
d[i++] = c;
c = cp_noninext();
}
d[i] = '\0';
return c == ' ';
} else {
cp_back(c);
utf8next(d, cp_noninext);
}
return 0;
}
static int regid(void)
{
char regname[NMLEN];
cparg(regname, sizeof(regname));
return map(regname);
}
/* interpolate \n(xy */
static void cp_num(void)
{
int id;
int c = cp_noninext();
if (c != '-' && c != '+')
cp_back(c);
id = regid();
if (c == '-' || c == '+')
num_inc(id, c == '+');
if (num_str(id))
in_push(num_str(id), NULL);
}
/* interpolate \*(xy */
static void cp_str(void)
{
char reg[NMLEN];
char *args[NARGS + 2] = {reg};
char *buf = NULL;
if (cparg(reg, sizeof(reg))) {
buf = tr_args(args + 1, ']', cp_noninext, cp_back);
cp_noninext();
}
if (str_get(map(reg)))
in_push(str_get(map(reg)), buf ? args : NULL);
else if (!n_cp)
tr_req(map(reg), args);
free(buf);
}
/* interpolate \g(xy */
static void cp_numfmt(void)
{
in_push(num_getfmt(regid()), NULL);
}
/* interpolate \$*, \$@, and \$^ */
static void cp_args(int quote, int escape)
{
struct sbuf sb;
char *s;
int i;
sbuf_init(&sb);
for (i = 1; i < in_nargs(); i++) {
sbuf_append(&sb, i > 1 ? " " : "");
sbuf_append(&sb, quote ? "\"" : "");
s = in_arg(i);
while (*s) {
sbuf_append(&sb, escape && *s == '"' ? "\"" : "");
sbuf_add(&sb, (unsigned char) *s++);
}
sbuf_append(&sb, quote ? "\"" : "");
}
in_push(sbuf_buf(&sb), NULL);
sbuf_done(&sb);
}
/* interpolate \$1 */
static void cp_arg(void)
{
char argname[NMLEN];
char *arg = NULL;
int argnum;
cparg(argname, sizeof(argname));
if (!strcmp("@", argname)) {
cp_args(1, 0);
return;
}
if (!strcmp("*", argname)) {
cp_args(0, 0);
return;
}
if (!strcmp("^", argname)) {
cp_args(1, 1);
return;
}
argnum = atoi(argname);
if (argnum >= 0 && argnum < NARGS)
arg = in_arg(argnum);
if (arg)
in_push(arg, NULL);
}
/* interpolate \w'xyz' */
static void cp_width(void)
{
char wid[16];
sprintf(wid, "%d", ren_wid(cp_next, cp_back));
in_push(wid, NULL);
}
/* define a register as \R'xyz expr' */
static void cp_numdef(void)
{
char *arg = quotednext(cp_noninext, cp_back);
char *s = arg;
while (*s && *s != ' ')
s++;
if (!*s) {
free(arg);
return;
}
*s++ = '\0';
num_set(map(arg), eval_re(s, num_get(map(arg)), 'u'));
free(arg);
}
/* conditional interpolation as \?'cond@expr1@expr2@' */
static void cp_cond(void)
{
char delim[GNLEN], cs[GNLEN];
char *r, *s;
char *s1, *s2;
int n;
char *arg = quotednext(cp_noninext, cp_back);
s = arg;
n = eval_up(&s, '\0');
if (charread(&s, delim) < 0) {
free(arg);
return;
}
if (!strcmp(delim, "\\&") && charread(&s, delim) < 0) {
free(arg);
return;
}
s1 = s;
r = s;
while (charread_delim(&s, cs, delim) >= 0)
r = s;
*r = '\0';
s2 = s;
r = s;
while (charread_delim(&s, cs, delim) >= 0)
r = s;
*r = '\0';
in_push(n > 0 ? s1 : s2, NULL);
free(arg);
}
static int cp_raw(void)
{
int c;
if (in_top() >= 0)
return in_next();
do {
c = in_next();
} while (c == c_ni);
if (c == c_ec) {
do {
c = in_next();
} while (c == c_ni);
if (c == '\n')
return cp_raw();
if (c == '.')
return '.';
if (c == '\\') {
in_back('\\');
return c_ni;
}
if (c == 't') {
in_back('\t');
return c_ni;
}
if (c == 'a') {
in_back('');
return c_ni;
}
/* replace \{ and \} with a space if not in copy mode */
if (c == '}' && !cp_cpmode) {
cp_blkdep--;
return ' ';
}
if (c == '{' && !cp_cpmode) {
cp_blkdep++;
return ' ';
}
in_back(c);
return c_ec;
}
return c;
}
int cp_next(void)
{
int c;
if (in_top() >= 0)
return in_next();
c = cp_raw();
if (c == c_ec) {
c = cp_raw();
if (c == 'E' && !cp_cpmode)
c = cp_next();
if (c == '"') {
while (c >= 0 && c != '\n')
c = cp_raw();
} else if (c == 'w' && !cp_cpmode) {
cp_width();
c = cp_next();
} else if (c == 'n') {
cp_num();
c = cp_next();
} else if (c == '*') {
cp_str();
c = cp_next();
} else if (c == 'g') {
cp_numfmt();
c = cp_next();
} else if (c == '$') {
cp_arg();
c = cp_next();
} else if (c == '?') {
cp_cond();
c = cp_next();
} else if (c == 'R' && !cp_cpmode) {
cp_numdef();
c = cp_next();
} else {
cp_back(c);
c = c_ec;
}
}
return c;
}
void cp_blk(int skip)
{
if (skip) {
int c = cp_raw();
while (c >= 0 && (c != '\n' || cp_blkdep > cp_reqdep))
c = cp_raw();
} else {
int c = cp_next();
while (c == ' ')
c = cp_next();
/* push back if the space is not inserted due to \{ and \} */
if (c != ' ')
cp_back(c);
}
}
void cp_copymode(int mode)
{
cp_cpmode = mode;
}
/* beginning of a request; save current cp_blkdep */
void cp_reqbeg(void)
{
cp_reqdep = cp_blkdep;
}