Annotation of mandoc/roff_escape.c, Revision 1.10
1.1 schwarze 1: /* $OpenBSD$ */
2: /*
3: * Copyright (c) 2011, 2012, 2013, 2014, 2015, 2017, 2018, 2020, 2022
4: * Ingo Schwarze <schwarze@openbsd.org>
5: * Copyright (c) 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
6: *
7: * Permission to use, copy, modify, and distribute this software for any
8: * purpose with or without fee is hereby granted, provided that the above
9: * copyright notice and this permission notice appear in all copies.
10: *
11: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
12: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
14: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18: *
19: * Parser for roff(7) escape sequences.
20: * To be used by all mandoc(1) parsers and formatters.
21: */
22: #include <assert.h>
23: #include <ctype.h>
24: #include <limits.h>
25: #include <stdio.h>
26: #include <string.h>
27:
28: #include "mandoc.h"
29: #include "roff.h"
30: #include "roff_int.h"
31:
32: /*
33: * Traditional escape sequence interpreter for general use
34: * including in high-level formatters. This function does not issue
35: * diagnostics and is not usable for expansion in the roff(7) parser.
36: * It is documented in the mandoc_escape(3) manual page.
37: */
38: enum mandoc_esc
39: mandoc_escape(const char **rendarg, const char **rarg, int *rargl)
40: {
41: int iarg, iendarg, iend;
42: enum mandoc_esc rval;
43:
1.7 schwarze 44: rval = roff_escape(--*rendarg, 0, 0,
45: NULL, NULL, &iarg, &iendarg, &iend);
1.1 schwarze 46: assert(rval != ESCAPE_EXPAND);
47: if (rarg != NULL)
48: *rarg = *rendarg + iarg;
49: if (rargl != NULL)
50: *rargl = iendarg - iarg;
51: *rendarg += iend;
52: return rval;
53: }
54:
55: /*
56: * Full-featured escape sequence parser.
57: * If it encounters a nested escape sequence that requires expansion
58: * by the parser and re-parsing, the positions of that inner escape
59: * sequence are returned in *resc ... *rend.
60: * Otherwise, *resc is set to aesc and the positions of the escape
61: * sequence starting at aesc are returned.
62: * Diagnostic messages are generated if and only if resc != NULL,
63: * that is, if and only if called by roff_expand().
64: */
65: enum mandoc_esc
66: roff_escape(const char *buf, const int ln, const int aesc,
1.7 schwarze 67: int *resc, int *rnam, int *rarg, int *rendarg, int *rend)
1.1 schwarze 68: {
69: int iesc; /* index of leading escape char */
1.7 schwarze 70: int inam; /* index of escape name */
1.1 schwarze 71: int iarg; /* index beginning the argument */
72: int iendarg; /* index right after the argument */
73: int iend; /* index right after the sequence */
1.7 schwarze 74: int sesc, snam, sarg, sendarg, send; /* for sub-escape */
1.1 schwarze 75: int maxl; /* expected length of the argument */
76: int argl; /* actual length of the argument */
77: int c, i; /* for \[char...] parsing */
1.5 schwarze 78: int valid_A; /* for \A parsing */
1.1 schwarze 79: enum mandoc_esc rval; /* return value */
80: enum mandocerr err; /* diagnostic code */
81: char term; /* byte terminating the argument */
82:
83: /*
84: * Treat "\E" just like "\";
85: * it only makes a difference in copy mode.
86: */
87:
1.7 schwarze 88: iesc = inam = aesc;
1.1 schwarze 89: do {
1.7 schwarze 90: inam++;
91: } while (buf[inam] == 'E');
1.1 schwarze 92:
93: /*
94: * Sort the following cases first by syntax category,
95: * then by escape sequence type, and finally by ASCII code.
96: */
97:
1.7 schwarze 98: iarg = iendarg = iend = inam + 1;
1.1 schwarze 99: maxl = INT_MAX;
100: term = '\0';
1.9 schwarze 101: err = MANDOCERR_OK;
1.7 schwarze 102: switch (buf[inam]) {
1.1 schwarze 103:
104: /* Escape sequences taking no arguments at all. */
105:
106: case '!':
107: case '?':
1.2 schwarze 108: case 'r':
1.1 schwarze 109: rval = ESCAPE_UNSUPP;
110: goto out;
111:
112: case '%':
113: case '&':
114: case ')':
115: case ',':
116: case '/':
117: case '^':
118: case 'a':
119: case 'd':
120: case 't':
121: case 'u':
122: case '{':
123: case '|':
124: case '}':
125: rval = ESCAPE_IGNORE;
126: goto out;
127:
1.6 schwarze 128: case '\0':
129: iendarg = --iend;
130: /* FALLTHROUGH */
1.8 schwarze 131: case '.':
1.1 schwarze 132: case '\\':
133: default:
134: iarg--;
135: rval = ESCAPE_UNDEF;
136: goto out;
137:
138: case ' ':
139: case '\'':
140: case '-':
141: case '0':
142: case ':':
143: case '_':
144: case '`':
145: case 'e':
146: case '~':
147: iarg--;
148: argl = 1;
149: rval = ESCAPE_SPECIAL;
150: goto out;
151: case 'p':
152: rval = ESCAPE_BREAK;
153: goto out;
154: case 'c':
155: rval = ESCAPE_NOSPACE;
156: goto out;
157: case 'z':
158: rval = ESCAPE_SKIPCHAR;
159: goto out;
160:
161: /* Standard argument format. */
162:
163: case '$':
164: case '*':
1.3 schwarze 165: case 'V':
1.4 schwarze 166: case 'g':
1.1 schwarze 167: case 'n':
168: rval = ESCAPE_EXPAND;
169: break;
170: case 'F':
171: case 'M':
172: case 'O':
173: case 'Y':
174: case 'k':
175: case 'm':
176: rval = ESCAPE_IGNORE;
177: break;
178: case '(':
179: case '[':
180: rval = ESCAPE_SPECIAL;
181: iendarg = iend = --iarg;
182: break;
183: case 'f':
184: rval = ESCAPE_FONT;
185: break;
186:
187: /* Quoted arguments */
188:
1.5 schwarze 189: case 'A':
1.1 schwarze 190: case 'B':
191: case 'w':
192: rval = ESCAPE_EXPAND;
193: term = '\b';
194: break;
195: case 'D':
196: case 'H':
197: case 'L':
198: case 'R':
199: case 'S':
200: case 'X':
201: case 'Z':
202: case 'b':
203: case 'v':
204: case 'x':
205: rval = ESCAPE_IGNORE;
206: term = '\b';
207: break;
208: case 'C':
209: if (buf[iarg] != '\'') {
210: rval = ESCAPE_ERROR;
211: goto out;
212: }
213: rval = ESCAPE_SPECIAL;
214: term = '\b';
215: break;
216: case 'N':
217: rval = ESCAPE_NUMBERED;
218: term = '\b';
219: break;
220: case 'h':
221: rval = ESCAPE_HORIZ;
222: term = '\b';
223: break;
224: case 'l':
225: rval = ESCAPE_HLINE;
226: term = '\b';
227: break;
228: case 'o':
229: rval = ESCAPE_OVERSTRIKE;
230: term = '\b';
231: break;
232:
233: /* Sizes support both forms, with additional peculiarities. */
234:
235: case 's':
236: rval = ESCAPE_IGNORE;
237: if (buf[iarg] == '+' || buf[iarg] == '-'||
238: buf[iarg] == ASCII_HYPH)
239: iarg++;
240: switch (buf[iarg]) {
241: case '(':
242: maxl = 2;
243: iarg++;
244: break;
245: case '[':
246: term = ']';
247: iarg++;
248: break;
249: case '\'':
250: term = '\'';
251: iarg++;
252: break;
253: case '1':
254: case '2':
255: case '3':
256: if (buf[iarg - 1] == 's' &&
257: isdigit((unsigned char)buf[iarg + 1])) {
258: maxl = 2;
259: break;
260: }
261: /* FALLTHROUGH */
262: default:
263: maxl = 1;
264: break;
265: }
266: iendarg = iend = iarg;
267: }
268:
269: /* Decide how to end the argument. */
270:
271: if ((term == '\b' || (term == '\0' && maxl == INT_MAX)) &&
272: buf[iarg] == buf[iesc] && roff_escape(buf, ln, iendarg,
1.7 schwarze 273: &sesc, &snam, &sarg, &sendarg, &send) == ESCAPE_EXPAND)
1.1 schwarze 274: goto out_sub;
275:
276: if (term == '\b') {
1.7 schwarze 277: if ((buf[inam] == 'N' && isdigit((unsigned char)buf[iarg])) ||
278: (buf[inam] == 'h' && strchr(" %&()*+-./0123456789:<=>",
1.1 schwarze 279: buf[iarg]) != NULL)) {
280: iendarg = iend = iarg + 1;
281: rval = ESCAPE_ERROR;
282: goto out;
283: }
284: term = buf[iarg++];
285: } else if (term == '\0' && maxl == INT_MAX) {
1.7 schwarze 286: if (buf[inam] == 'n' && (buf[iarg] == '+' || buf[iarg] == '-'))
1.1 schwarze 287: iarg++;
288: switch (buf[iarg]) {
289: case '(':
290: maxl = 2;
291: iarg++;
292: break;
293: case '[':
294: if (buf[++iarg] == ' ') {
295: iendarg = iend = iarg + 1;
296: rval = ESCAPE_ERROR;
297: goto out;
298: }
299: term = ']';
300: break;
301: default:
302: maxl = 1;
303: break;
304: }
305: }
306:
307: /* Advance to the end of the argument. */
308:
1.5 schwarze 309: valid_A = 1;
1.1 schwarze 310: iendarg = iarg;
311: while (maxl > 0) {
312: if (buf[iendarg] == '\0') {
1.10 ! schwarze 313: err = MANDOCERR_ESC_INCOMPLETE;
! 314: if (rval != ESCAPE_EXPAND)
! 315: rval = ESCAPE_ERROR;
1.1 schwarze 316: /* Ignore an incomplete argument except for \w. */
1.7 schwarze 317: if (buf[inam] != 'w')
1.1 schwarze 318: iendarg = iarg;
319: break;
320: }
321: if (buf[iendarg] == term) {
322: iend = iendarg + 1;
323: break;
324: }
1.7 schwarze 325: if (buf[inam] == 'N' &&
1.1 schwarze 326: isdigit((unsigned char)buf[iendarg]) == 0) {
327: iend = iendarg + 1;
328: break;
329: }
330: if (buf[iendarg] == buf[iesc]) {
1.5 schwarze 331: switch (roff_escape(buf, ln, iendarg,
1.7 schwarze 332: &sesc, &snam, &sarg, &sendarg, &send)) {
1.5 schwarze 333: case ESCAPE_EXPAND:
1.1 schwarze 334: goto out_sub;
1.5 schwarze 335: case ESCAPE_UNDEF:
336: break;
337: default:
338: valid_A = 0;
339: break;
340: }
1.1 schwarze 341: iendarg = iend = send;
342: } else {
1.5 schwarze 343: if (buf[iendarg] == ' ' || buf[iendarg] == '\t')
344: valid_A = 0;
1.1 schwarze 345: if (maxl != INT_MAX)
346: maxl--;
347: iend = ++iendarg;
348: }
349: }
350:
351: /* Post-process depending on the content of the argument. */
352:
353: argl = iendarg - iarg;
1.7 schwarze 354: switch (buf[inam]) {
1.1 schwarze 355: case '*':
356: if (resc == NULL && argl == 2 &&
357: buf[iarg] == '.' && buf[iarg + 1] == 'T')
358: rval = ESCAPE_DEVICE;
1.5 schwarze 359: break;
360: case 'A':
361: if (valid_A == 0)
362: iendarg = iarg;
1.1 schwarze 363: break;
364: case 'O':
365: switch (buf[iarg]) {
366: case '0':
367: rval = ESCAPE_UNSUPP;
368: break;
369: case '1':
370: case '2':
371: case '3':
372: case '4':
373: rval = argl == 1 ? ESCAPE_IGNORE : ESCAPE_ERROR;
374: break;
375: case '5':
376: rval = buf[iarg - 1] == '[' ? ESCAPE_UNSUPP :
377: ESCAPE_ERROR;
378: break;
379: default:
380: rval = ESCAPE_ERROR;
381: break;
382: }
383: break;
384: default:
385: break;
386: }
387:
388: switch (rval) {
389: case ESCAPE_FONT:
390: rval = mandoc_font(buf + iarg, argl);
391: break;
392:
393: case ESCAPE_SPECIAL:
394:
395: /*
396: * The file chars.c only provides one common list of
397: * character names, but \[-] == \- is the only one of
398: * the characters with one-byte names that allows
399: * enclosing the name in brackets.
400: */
401:
402: if (term != '\0' && argl == 1 && buf[iarg] != '-') {
1.10 ! schwarze 403: err = MANDOCERR_ESC_BADCHAR;
1.1 schwarze 404: rval = ESCAPE_ERROR;
405: break;
406: }
407:
408: /* Treat \[char...] as an alias for \N'...'. */
409:
410: if (buf[iarg] == 'c') {
411: if (argl < 6 || argl > 7 ||
412: strncmp(buf + iarg, "char", 4) != 0 ||
413: (int)strspn(buf + iarg + 4, "0123456789")
414: + 4 < argl)
415: break;
416: c = 0;
417: for (i = iarg; i < iendarg; i++)
418: c = 10 * c + (buf[i] - '0');
1.10 ! schwarze 419: if (c < 0x21 || (c > 0x7e && c < 0xa0) || c > 0xff) {
! 420: err = MANDOCERR_ESC_BADCHAR;
1.1 schwarze 421: break;
1.10 ! schwarze 422: }
1.1 schwarze 423: iarg += 4;
424: rval = ESCAPE_NUMBERED;
425: break;
426: }
427:
428: /*
429: * Unicode escapes are defined in groff as \[u0000]
430: * to \[u10FFFF], where the contained value must be
431: * a valid Unicode codepoint. Here, however, only
432: * check the length and range.
433: */
434:
435: if (buf[iarg] != 'u' || argl < 5 || argl > 7)
436: break;
437: if (argl == 7 &&
1.10 ! schwarze 438: (buf[iarg + 1] != '1' || buf[iarg + 2] != '0')) {
! 439: err = MANDOCERR_ESC_BADCHAR;
1.1 schwarze 440: break;
1.10 ! schwarze 441: }
! 442: if (argl == 6 && buf[iarg + 1] == '0') {
! 443: err = MANDOCERR_ESC_BADCHAR;
1.1 schwarze 444: break;
1.10 ! schwarze 445: }
1.1 schwarze 446: if (argl == 5 && buf[iarg + 1] == 'D' &&
1.10 ! schwarze 447: strchr("89ABCDEF", buf[iarg + 2]) != NULL) {
! 448: err = MANDOCERR_ESC_BADCHAR;
1.1 schwarze 449: break;
1.10 ! schwarze 450: }
1.1 schwarze 451: if ((int)strspn(buf + iarg + 1, "0123456789ABCDEFabcdef")
452: + 1 == argl)
453: rval = ESCAPE_UNICODE;
454: break;
455: default:
456: break;
457: }
458: goto out;
459:
460: out_sub:
461: iesc = sesc;
1.7 schwarze 462: inam = snam;
1.1 schwarze 463: iarg = sarg;
464: iendarg = sendarg;
465: iend = send;
466: rval = ESCAPE_EXPAND;
467:
468: out:
1.7 schwarze 469: if (rnam != NULL)
470: *rnam = inam;
1.1 schwarze 471: if (rarg != NULL)
472: *rarg = iarg;
473: if (rendarg != NULL)
474: *rendarg = iendarg;
475: if (rend != NULL)
476: *rend = iend;
477: if (resc == NULL)
478: return rval;
479:
480: /*
481: * Diagnostic messages are only issued when called
482: * from the parser, not when called from the formatters.
483: */
484:
485: *resc = iesc;
486: switch (rval) {
487: case ESCAPE_ERROR:
1.10 ! schwarze 488: if (err == MANDOCERR_OK)
! 489: err = MANDOCERR_ESC_BAD;
1.1 schwarze 490: break;
491: case ESCAPE_UNSUPP:
492: err = MANDOCERR_ESC_UNSUPP;
493: break;
494: case ESCAPE_UNDEF:
1.9 schwarze 495: if (buf[inam] != '\\' && buf[inam] != '.')
496: err = MANDOCERR_ESC_UNDEF;
1.1 schwarze 497: break;
498: case ESCAPE_SPECIAL:
1.10 ! schwarze 499: if (mchars_spec2cp(buf + iarg, argl) >= 0)
! 500: err = MANDOCERR_OK;
! 501: else if (err == MANDOCERR_OK)
! 502: err = MANDOCERR_ESC_UNKCHAR;
1.1 schwarze 503: break;
504: default:
1.9 schwarze 505: break;
1.1 schwarze 506: }
1.9 schwarze 507: if (err != MANDOCERR_OK)
508: mandoc_msg(err, ln, iesc, "%.*s", iend - iesc, buf + iesc);
1.1 schwarze 509: return rval;
510: }
CVSweb