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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
|
/* $Id$ */
/*
* Copyright (c) 2011, 2012, 2013, 2014, 2015, 2017, 2018, 2020, 2022
* Ingo Schwarze <schwarze@openbsd.org>
* Copyright (c) 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* Parser for roff(7) escape sequences.
* To be used by all mandoc(1) parsers and formatters.
*/
#include <assert.h>
#include <ctype.h>
#include <limits.h>
#include <stdio.h>
#include <string.h>
#include "mandoc.h"
#include "roff.h"
#include "roff_int.h"
/*
* Traditional escape sequence interpreter for general use
* including in high-level formatters. This function does not issue
* diagnostics and is not usable for expansion in the roff(7) parser.
* It is documented in the mandoc_escape(3) manual page.
*/
enum mandoc_esc
mandoc_escape(const char **rendarg, const char **rarg, int *rargl)
{
int iarg, iendarg, iend;
enum mandoc_esc rval;
rval = roff_escape(--*rendarg, 0, 0,
NULL, NULL, &iarg, &iendarg, &iend);
assert(rval != ESCAPE_EXPAND);
if (rarg != NULL)
*rarg = *rendarg + iarg;
if (rargl != NULL)
*rargl = iendarg - iarg;
*rendarg += iend;
return rval;
}
/*
* Full-featured escape sequence parser.
* If it encounters a nested escape sequence that requires expansion
* by the parser and re-parsing, the positions of that inner escape
* sequence are returned in *resc ... *rend.
* Otherwise, *resc is set to aesc and the positions of the escape
* sequence starting at aesc are returned.
* Diagnostic messages are generated if and only if resc != NULL,
* that is, if and only if called by roff_expand().
*/
enum mandoc_esc
roff_escape(const char *buf, const int ln, const int aesc,
int *resc, int *rnam, int *rarg, int *rendarg, int *rend)
{
int iesc; /* index of leading escape char */
int inam; /* index of escape name */
int iarg; /* index beginning the argument */
int iendarg; /* index right after the argument */
int iend; /* index right after the sequence */
int sesc, snam, sarg, sendarg, send; /* for sub-escape */
int maxl; /* expected length of the argument */
int argl; /* actual length of the argument */
int c, i; /* for \[char...] parsing */
int valid_A; /* for \A parsing */
enum mandoc_esc rval; /* return value */
enum mandocerr err; /* diagnostic code */
char term; /* byte terminating the argument */
/*
* Treat "\E" just like "\";
* it only makes a difference in copy mode.
*/
iesc = inam = aesc;
do {
inam++;
} while (buf[inam] == 'E');
/*
* Sort the following cases first by syntax category,
* then by escape sequence type, and finally by ASCII code.
*/
iarg = iendarg = iend = inam + 1;
maxl = INT_MAX;
term = '\0';
err = MANDOCERR_OK;
switch (buf[inam]) {
/* Escape sequences taking no arguments at all. */
case '!':
case '?':
case 'r':
rval = ESCAPE_UNSUPP;
goto out;
case '%':
case '&':
case ')':
case ',':
case '/':
case '^':
case 'a':
case 'd':
case 't':
case 'u':
case '{':
case '|':
case '}':
rval = ESCAPE_IGNORE;
goto out;
case '\0':
iendarg = --iend;
/* FALLTHROUGH */
case '.':
case '\\':
default:
iarg--;
rval = ESCAPE_UNDEF;
goto out;
case ' ':
case '\'':
case '-':
case '0':
case ':':
case '_':
case '`':
case 'e':
case '~':
iarg--;
argl = 1;
rval = ESCAPE_SPECIAL;
goto out;
case 'p':
rval = ESCAPE_BREAK;
goto out;
case 'c':
rval = ESCAPE_NOSPACE;
goto out;
case 'z':
rval = ESCAPE_SKIPCHAR;
goto out;
/* Standard argument format. */
case '$':
case '*':
case 'V':
case 'g':
case 'n':
rval = ESCAPE_EXPAND;
break;
case 'F':
case 'M':
case 'O':
case 'Y':
case 'k':
case 'm':
rval = ESCAPE_IGNORE;
break;
case '(':
case '[':
rval = ESCAPE_SPECIAL;
iendarg = iend = --iarg;
break;
case 'f':
rval = ESCAPE_FONT;
break;
/* Quoted arguments */
case 'A':
case 'B':
case 'w':
rval = ESCAPE_EXPAND;
term = '\b';
break;
case 'D':
case 'H':
case 'L':
case 'R':
case 'S':
case 'X':
case 'Z':
case 'b':
case 'v':
case 'x':
rval = ESCAPE_IGNORE;
term = '\b';
break;
case 'C':
rval = ESCAPE_SPECIAL;
term = '\b';
break;
case 'N':
rval = ESCAPE_NUMBERED;
term = '\b';
break;
case 'h':
rval = ESCAPE_HORIZ;
term = '\b';
break;
case 'l':
rval = ESCAPE_HLINE;
term = '\b';
break;
case 'o':
rval = ESCAPE_OVERSTRIKE;
term = '\b';
break;
/* Sizes support both forms, with additional peculiarities. */
case 's':
rval = ESCAPE_IGNORE;
if (buf[iarg] == '+' || buf[iarg] == '-'||
buf[iarg] == ASCII_HYPH)
iarg++;
switch (buf[iarg]) {
case '(':
maxl = 2;
iarg++;
break;
case '[':
term = ']';
iarg++;
break;
case '\'':
term = '\'';
iarg++;
break;
case '1':
case '2':
case '3':
if (buf[iarg - 1] == 's' &&
isdigit((unsigned char)buf[iarg + 1])) {
maxl = 2;
break;
}
/* FALLTHROUGH */
default:
maxl = 1;
break;
}
iendarg = iend = iarg;
}
/* Decide how to end the argument. */
if ((term == '\b' || (term == '\0' && maxl == INT_MAX)) &&
buf[iarg] == buf[iesc] && roff_escape(buf, ln, iendarg,
&sesc, &snam, &sarg, &sendarg, &send) == ESCAPE_EXPAND)
goto out_sub;
if (term == '\b') {
if (strchr("BDHLRSvxNhl", buf[inam]) != NULL &&
strchr(" %&()*+-./0123456789:<=>", buf[iarg]) != NULL) {
if (rval != ESCAPE_EXPAND)
rval = ESCAPE_ERROR;
if (buf[inam] != 'D') {
iendarg = iend = iarg + 1;
goto out;
}
}
term = buf[iarg++];
} else if (term == '\0' && maxl == INT_MAX) {
if (buf[inam] == 'n' && (buf[iarg] == '+' || buf[iarg] == '-'))
iarg++;
switch (buf[iarg]) {
case '(':
maxl = 2;
iarg++;
break;
case '[':
if (buf[++iarg] == ' ') {
iendarg = iend = iarg + 1;
rval = ESCAPE_ERROR;
goto out;
}
term = ']';
break;
default:
maxl = 1;
break;
}
}
/* Advance to the end of the argument. */
valid_A = 1;
iendarg = iarg;
while (maxl > 0) {
if (buf[iendarg] == '\0') {
err = MANDOCERR_ESC_INCOMPLETE;
if (rval != ESCAPE_EXPAND)
rval = ESCAPE_ERROR;
/* Ignore an incomplete argument except for \w. */
if (buf[inam] != 'w')
iendarg = iarg;
break;
}
if (buf[iendarg] == term) {
iend = iendarg + 1;
break;
}
if (buf[inam] == 'N' &&
isdigit((unsigned char)buf[iendarg]) == 0) {
iend = iendarg + 1;
break;
}
if (buf[iendarg] == buf[iesc]) {
switch (roff_escape(buf, ln, iendarg,
&sesc, &snam, &sarg, &sendarg, &send)) {
case ESCAPE_EXPAND:
goto out_sub;
case ESCAPE_UNDEF:
break;
default:
valid_A = 0;
break;
}
iendarg = iend = send;
} else {
if (buf[iendarg] == ' ' || buf[iendarg] == '\t')
valid_A = 0;
if (maxl != INT_MAX)
maxl--;
iend = ++iendarg;
}
}
/* Post-process depending on the content of the argument. */
argl = iendarg - iarg;
switch (buf[inam]) {
case '*':
if (resc == NULL && argl == 2 &&
buf[iarg] == '.' && buf[iarg + 1] == 'T')
rval = ESCAPE_DEVICE;
break;
case 'A':
if (valid_A == 0)
iendarg = iarg;
break;
case 'O':
switch (buf[iarg]) {
case '0':
rval = ESCAPE_UNSUPP;
break;
case '1':
case '2':
case '3':
case '4':
rval = argl == 1 ? ESCAPE_IGNORE : ESCAPE_ERROR;
break;
case '5':
rval = buf[iarg - 1] == '[' ? ESCAPE_UNSUPP :
ESCAPE_ERROR;
break;
default:
rval = ESCAPE_ERROR;
break;
}
break;
default:
break;
}
switch (rval) {
case ESCAPE_FONT:
rval = mandoc_font(buf + iarg, argl);
break;
case ESCAPE_SPECIAL:
if (argl == 0) {
err = MANDOCERR_ESC_BADCHAR;
rval = ESCAPE_ERROR;
break;
}
/*
* The file chars.c only provides one common list of
* character names, but \[-] == \- is the only one of
* the characters with one-byte names that allows
* enclosing the name in brackets.
*/
if (term != '\0' && argl == 1 && buf[iarg] != '-') {
err = MANDOCERR_ESC_BADCHAR;
rval = ESCAPE_ERROR;
break;
}
/* Treat \[char...] as an alias for \N'...'. */
if (buf[iarg] == 'c') {
if (argl < 6 || argl > 7 ||
strncmp(buf + iarg, "char", 4) != 0 ||
(int)strspn(buf + iarg + 4, "0123456789")
+ 4 < argl)
break;
c = 0;
for (i = iarg; i < iendarg; i++)
c = 10 * c + (buf[i] - '0');
if (c < 0x21 || (c > 0x7e && c < 0xa0) || c > 0xff) {
err = MANDOCERR_ESC_BADCHAR;
break;
}
iarg += 4;
rval = ESCAPE_NUMBERED;
break;
}
/*
* Unicode escapes are defined in groff as \[u0000]
* to \[u10FFFF], where the contained value must be
* a valid Unicode codepoint. Here, however, only
* check the length and range.
*/
if (buf[iarg] != 'u' || argl < 5 || argl > 7)
break;
if (argl == 7 &&
(buf[iarg + 1] != '1' || buf[iarg + 2] != '0')) {
err = MANDOCERR_ESC_BADCHAR;
break;
}
if (argl == 6 && buf[iarg + 1] == '0') {
err = MANDOCERR_ESC_BADCHAR;
break;
}
if (argl == 5 && buf[iarg + 1] == 'D' &&
strchr("89ABCDEF", buf[iarg + 2]) != NULL) {
err = MANDOCERR_ESC_BADCHAR;
break;
}
if ((int)strspn(buf + iarg + 1, "0123456789ABCDEFabcdef")
+ 1 == argl)
rval = ESCAPE_UNICODE;
break;
default:
break;
}
goto out;
out_sub:
iesc = sesc;
inam = snam;
iarg = sarg;
iendarg = sendarg;
iend = send;
rval = ESCAPE_EXPAND;
out:
if (rnam != NULL)
*rnam = inam;
if (rarg != NULL)
*rarg = iarg;
if (rendarg != NULL)
*rendarg = iendarg;
if (rend != NULL)
*rend = iend;
if (resc == NULL)
return rval;
/*
* Diagnostic messages are only issued when called
* from the parser, not when called from the formatters.
*/
*resc = iesc;
switch (rval) {
case ESCAPE_ERROR:
if (err == MANDOCERR_OK)
err = MANDOCERR_ESC_BAD;
break;
case ESCAPE_UNSUPP:
err = MANDOCERR_ESC_UNSUPP;
break;
case ESCAPE_UNDEF:
if (buf[inam] != '\\' && buf[inam] != '.')
err = MANDOCERR_ESC_UNDEF;
break;
case ESCAPE_SPECIAL:
if (mchars_spec2cp(buf + iarg, argl) >= 0)
err = MANDOCERR_OK;
else if (err == MANDOCERR_OK)
err = MANDOCERR_ESC_UNKCHAR;
break;
default:
break;
}
if (err != MANDOCERR_OK)
mandoc_msg(err, ln, iesc, "%.*s", iend - iesc, buf + iesc);
return rval;
}
|