]> git.cameronkatri.com Git - mandoc.git/blob - mdoc.c
Avoid out-of-bounds read access before the beginning of the
[mandoc.git] / mdoc.c
1 /* $Id: mdoc.c,v 1.250 2015/04/19 14:57:38 schwarze Exp $ */
2 /*
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010, 2012-2015 Ingo Schwarze <schwarze@openbsd.org>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 #include "config.h"
19
20 #include <sys/types.h>
21
22 #include <assert.h>
23 #include <ctype.h>
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <time.h>
29
30 #include "mandoc_aux.h"
31 #include "mandoc.h"
32 #include "roff.h"
33 #include "mdoc.h"
34 #include "libmandoc.h"
35 #include "roff_int.h"
36 #include "libmdoc.h"
37
38 const char *const __mdoc_macronames[MDOC_MAX + 1] = {
39 "text",
40 "Ap", "Dd", "Dt", "Os",
41 "Sh", "Ss", "Pp", "D1",
42 "Dl", "Bd", "Ed", "Bl",
43 "El", "It", "Ad", "An",
44 "Ar", "Cd", "Cm", "Dv",
45 "Er", "Ev", "Ex", "Fa",
46 "Fd", "Fl", "Fn", "Ft",
47 "Ic", "In", "Li", "Nd",
48 "Nm", "Op", "Ot", "Pa",
49 "Rv", "St", "Va", "Vt",
50 "Xr", "%A", "%B", "%D",
51 "%I", "%J", "%N", "%O",
52 "%P", "%R", "%T", "%V",
53 "Ac", "Ao", "Aq", "At",
54 "Bc", "Bf", "Bo", "Bq",
55 "Bsx", "Bx", "Db", "Dc",
56 "Do", "Dq", "Ec", "Ef",
57 "Em", "Eo", "Fx", "Ms",
58 "No", "Ns", "Nx", "Ox",
59 "Pc", "Pf", "Po", "Pq",
60 "Qc", "Ql", "Qo", "Qq",
61 "Re", "Rs", "Sc", "So",
62 "Sq", "Sm", "Sx", "Sy",
63 "Tn", "Ux", "Xc", "Xo",
64 "Fo", "Fc", "Oo", "Oc",
65 "Bk", "Ek", "Bt", "Hf",
66 "Fr", "Ud", "Lb", "Lp",
67 "Lk", "Mt", "Brq", "Bro",
68 "Brc", "%C", "Es", "En",
69 "Dx", "%Q", "br", "sp",
70 "%U", "Ta", "ll",
71 };
72
73 const char *const __mdoc_argnames[MDOC_ARG_MAX] = {
74 "split", "nosplit", "ragged",
75 "unfilled", "literal", "file",
76 "offset", "bullet", "dash",
77 "hyphen", "item", "enum",
78 "tag", "diag", "hang",
79 "ohang", "inset", "column",
80 "width", "compact", "std",
81 "filled", "words", "emphasis",
82 "symbolic", "nested", "centered"
83 };
84
85 const char * const *mdoc_macronames = __mdoc_macronames + 1;
86 const char * const *mdoc_argnames = __mdoc_argnames;
87
88 static int mdoc_ptext(struct roff_man *, int, char *, int);
89 static int mdoc_pmacro(struct roff_man *, int, char *, int);
90
91
92 void
93 mdoc_endparse(struct roff_man *mdoc)
94 {
95
96 mdoc_macroend(mdoc);
97 }
98
99 /*
100 * Main parse routine. Parses a single line -- really just hands off to
101 * the macro (mdoc_pmacro()) or text parser (mdoc_ptext()).
102 */
103 int
104 mdoc_parseln(struct roff_man *mdoc, int ln, char *buf, int offs)
105 {
106
107 if (mdoc->last->type != ROFFT_EQN || ln > mdoc->last->line)
108 mdoc->flags |= MDOC_NEWLINE;
109
110 /*
111 * Let the roff nS register switch SYNOPSIS mode early,
112 * such that the parser knows at all times
113 * whether this mode is on or off.
114 * Note that this mode is also switched by the Sh macro.
115 */
116 if (roff_getreg(mdoc->roff, "nS"))
117 mdoc->flags |= MDOC_SYNOPSIS;
118 else
119 mdoc->flags &= ~MDOC_SYNOPSIS;
120
121 return(roff_getcontrol(mdoc->roff, buf, &offs) ?
122 mdoc_pmacro(mdoc, ln, buf, offs) :
123 mdoc_ptext(mdoc, ln, buf, offs));
124 }
125
126 void
127 mdoc_macro(MACRO_PROT_ARGS)
128 {
129 assert(tok > TOKEN_NONE && tok < MDOC_MAX);
130
131 if (mdoc->flags & MDOC_PBODY) {
132 if (tok == MDOC_Dt) {
133 mandoc_vmsg(MANDOCERR_DT_LATE,
134 mdoc->parse, line, ppos,
135 "Dt %s", buf + *pos);
136 return;
137 }
138 } else if ( ! (mdoc_macros[tok].flags & MDOC_PROLOGUE)) {
139 if (mdoc->meta.title == NULL) {
140 mandoc_vmsg(MANDOCERR_DT_NOTITLE,
141 mdoc->parse, line, ppos, "%s %s",
142 mdoc_macronames[tok], buf + *pos);
143 mdoc->meta.title = mandoc_strdup("UNTITLED");
144 }
145 if (NULL == mdoc->meta.vol)
146 mdoc->meta.vol = mandoc_strdup("LOCAL");
147 mdoc->flags |= MDOC_PBODY;
148 }
149 (*mdoc_macros[tok].fp)(mdoc, tok, line, ppos, pos, buf);
150 }
151
152 void
153 mdoc_tail_alloc(struct roff_man *mdoc, int line, int pos, int tok)
154 {
155 struct roff_node *p;
156
157 p = roff_node_alloc(mdoc, line, pos, ROFFT_TAIL, tok);
158 roff_node_append(mdoc, p);
159 mdoc->next = ROFF_NEXT_CHILD;
160 }
161
162 struct roff_node *
163 mdoc_endbody_alloc(struct roff_man *mdoc, int line, int pos, int tok,
164 struct roff_node *body, enum mdoc_endbody end)
165 {
166 struct roff_node *p;
167
168 body->flags |= MDOC_ENDED;
169 body->parent->flags |= MDOC_ENDED;
170 p = roff_node_alloc(mdoc, line, pos, ROFFT_BODY, tok);
171 p->body = body;
172 p->norm = body->norm;
173 p->end = end;
174 roff_node_append(mdoc, p);
175 mdoc->next = ROFF_NEXT_SIBLING;
176 return(p);
177 }
178
179 struct roff_node *
180 mdoc_block_alloc(struct roff_man *mdoc, int line, int pos,
181 int tok, struct mdoc_arg *args)
182 {
183 struct roff_node *p;
184
185 p = roff_node_alloc(mdoc, line, pos, ROFFT_BLOCK, tok);
186 p->args = args;
187 if (p->args)
188 (args->refcnt)++;
189
190 switch (tok) {
191 case MDOC_Bd:
192 /* FALLTHROUGH */
193 case MDOC_Bf:
194 /* FALLTHROUGH */
195 case MDOC_Bl:
196 /* FALLTHROUGH */
197 case MDOC_En:
198 /* FALLTHROUGH */
199 case MDOC_Rs:
200 p->norm = mandoc_calloc(1, sizeof(union mdoc_data));
201 break;
202 default:
203 break;
204 }
205 roff_node_append(mdoc, p);
206 mdoc->next = ROFF_NEXT_CHILD;
207 return(p);
208 }
209
210 void
211 mdoc_elem_alloc(struct roff_man *mdoc, int line, int pos,
212 int tok, struct mdoc_arg *args)
213 {
214 struct roff_node *p;
215
216 p = roff_node_alloc(mdoc, line, pos, ROFFT_ELEM, tok);
217 p->args = args;
218 if (p->args)
219 (args->refcnt)++;
220
221 switch (tok) {
222 case MDOC_An:
223 p->norm = mandoc_calloc(1, sizeof(union mdoc_data));
224 break;
225 default:
226 break;
227 }
228 roff_node_append(mdoc, p);
229 mdoc->next = ROFF_NEXT_CHILD;
230 }
231
232 void
233 mdoc_node_relink(struct roff_man *mdoc, struct roff_node *p)
234 {
235
236 roff_node_unlink(mdoc, p);
237 roff_node_append(mdoc, p);
238 }
239
240 /*
241 * Parse free-form text, that is, a line that does not begin with the
242 * control character.
243 */
244 static int
245 mdoc_ptext(struct roff_man *mdoc, int line, char *buf, int offs)
246 {
247 struct roff_node *n;
248 char *c, *ws, *end;
249
250 assert(mdoc->last);
251 n = mdoc->last;
252
253 /*
254 * Divert directly to list processing if we're encountering a
255 * columnar ROFFT_BLOCK with or without a prior ROFFT_BLOCK entry
256 * (a ROFFT_BODY means it's already open, in which case we should
257 * process within its context in the normal way).
258 */
259
260 if (n->tok == MDOC_Bl && n->type == ROFFT_BODY &&
261 n->end == ENDBODY_NOT && n->norm->Bl.type == LIST_column) {
262 /* `Bl' is open without any children. */
263 mdoc->flags |= MDOC_FREECOL;
264 mdoc_macro(mdoc, MDOC_It, line, offs, &offs, buf);
265 return(1);
266 }
267
268 if (n->tok == MDOC_It && n->type == ROFFT_BLOCK &&
269 NULL != n->parent &&
270 MDOC_Bl == n->parent->tok &&
271 LIST_column == n->parent->norm->Bl.type) {
272 /* `Bl' has block-level `It' children. */
273 mdoc->flags |= MDOC_FREECOL;
274 mdoc_macro(mdoc, MDOC_It, line, offs, &offs, buf);
275 return(1);
276 }
277
278 /*
279 * Search for the beginning of unescaped trailing whitespace (ws)
280 * and for the first character not to be output (end).
281 */
282
283 /* FIXME: replace with strcspn(). */
284 ws = NULL;
285 for (c = end = buf + offs; *c; c++) {
286 switch (*c) {
287 case ' ':
288 if (NULL == ws)
289 ws = c;
290 continue;
291 case '\t':
292 /*
293 * Always warn about trailing tabs,
294 * even outside literal context,
295 * where they should be put on the next line.
296 */
297 if (NULL == ws)
298 ws = c;
299 /*
300 * Strip trailing tabs in literal context only;
301 * outside, they affect the next line.
302 */
303 if (MDOC_LITERAL & mdoc->flags)
304 continue;
305 break;
306 case '\\':
307 /* Skip the escaped character, too, if any. */
308 if (c[1])
309 c++;
310 /* FALLTHROUGH */
311 default:
312 ws = NULL;
313 break;
314 }
315 end = c + 1;
316 }
317 *end = '\0';
318
319 if (ws)
320 mandoc_msg(MANDOCERR_SPACE_EOL, mdoc->parse,
321 line, (int)(ws-buf), NULL);
322
323 if (buf[offs] == '\0' && ! (mdoc->flags & MDOC_LITERAL)) {
324 mandoc_msg(MANDOCERR_FI_BLANK, mdoc->parse,
325 line, (int)(c - buf), NULL);
326
327 /*
328 * Insert a `sp' in the case of a blank line. Technically,
329 * blank lines aren't allowed, but enough manuals assume this
330 * behaviour that we want to work around it.
331 */
332 roff_elem_alloc(mdoc, line, offs, MDOC_sp);
333 mdoc->next = ROFF_NEXT_SIBLING;
334 mdoc_valid_post(mdoc);
335 return(1);
336 }
337
338 roff_word_alloc(mdoc, line, offs, buf+offs);
339
340 if (mdoc->flags & MDOC_LITERAL)
341 return(1);
342
343 /*
344 * End-of-sentence check. If the last character is an unescaped
345 * EOS character, then flag the node as being the end of a
346 * sentence. The front-end will know how to interpret this.
347 */
348
349 assert(buf < end);
350
351 if (mandoc_eos(buf+offs, (size_t)(end-buf-offs)))
352 mdoc->last->flags |= MDOC_EOS;
353 return(1);
354 }
355
356 /*
357 * Parse a macro line, that is, a line beginning with the control
358 * character.
359 */
360 static int
361 mdoc_pmacro(struct roff_man *mdoc, int ln, char *buf, int offs)
362 {
363 struct roff_node *n;
364 const char *cp;
365 int tok;
366 int i, sv;
367 char mac[5];
368
369 sv = offs;
370
371 /*
372 * Copy the first word into a nil-terminated buffer.
373 * Stop when a space, tab, escape, or eoln is encountered.
374 */
375
376 i = 0;
377 while (i < 4 && strchr(" \t\\", buf[offs]) == NULL)
378 mac[i++] = buf[offs++];
379
380 mac[i] = '\0';
381
382 tok = (i > 1 && i < 4) ? mdoc_hash_find(mac) : TOKEN_NONE;
383
384 if (tok == TOKEN_NONE) {
385 mandoc_msg(MANDOCERR_MACRO, mdoc->parse,
386 ln, sv, buf + sv - 1);
387 return(1);
388 }
389
390 /* Skip a leading escape sequence or tab. */
391
392 switch (buf[offs]) {
393 case '\\':
394 cp = buf + offs + 1;
395 mandoc_escape(&cp, NULL, NULL);
396 offs = cp - buf;
397 break;
398 case '\t':
399 offs++;
400 break;
401 default:
402 break;
403 }
404
405 /* Jump to the next non-whitespace word. */
406
407 while (buf[offs] && ' ' == buf[offs])
408 offs++;
409
410 /*
411 * Trailing whitespace. Note that tabs are allowed to be passed
412 * into the parser as "text", so we only warn about spaces here.
413 */
414
415 if ('\0' == buf[offs] && ' ' == buf[offs - 1])
416 mandoc_msg(MANDOCERR_SPACE_EOL, mdoc->parse,
417 ln, offs - 1, NULL);
418
419 /*
420 * If an initial macro or a list invocation, divert directly
421 * into macro processing.
422 */
423
424 if (NULL == mdoc->last || MDOC_It == tok || MDOC_El == tok) {
425 mdoc_macro(mdoc, tok, ln, sv, &offs, buf);
426 return(1);
427 }
428
429 n = mdoc->last;
430 assert(mdoc->last);
431
432 /*
433 * If the first macro of a `Bl -column', open an `It' block
434 * context around the parsed macro.
435 */
436
437 if (n->tok == MDOC_Bl && n->type == ROFFT_BODY &&
438 n->end == ENDBODY_NOT && n->norm->Bl.type == LIST_column) {
439 mdoc->flags |= MDOC_FREECOL;
440 mdoc_macro(mdoc, MDOC_It, ln, sv, &sv, buf);
441 return(1);
442 }
443
444 /*
445 * If we're following a block-level `It' within a `Bl -column'
446 * context (perhaps opened in the above block or in ptext()),
447 * then open an `It' block context around the parsed macro.
448 */
449
450 if (n->tok == MDOC_It && n->type == ROFFT_BLOCK &&
451 NULL != n->parent &&
452 MDOC_Bl == n->parent->tok &&
453 LIST_column == n->parent->norm->Bl.type) {
454 mdoc->flags |= MDOC_FREECOL;
455 mdoc_macro(mdoc, MDOC_It, ln, sv, &sv, buf);
456 return(1);
457 }
458
459 /* Normal processing of a macro. */
460
461 mdoc_macro(mdoc, tok, ln, sv, &offs, buf);
462
463 /* In quick mode (for mandocdb), abort after the NAME section. */
464
465 if (mdoc->quick && MDOC_Sh == tok &&
466 SEC_NAME != mdoc->last->sec)
467 return(2);
468
469 return(1);
470 }
471
472 enum mdelim
473 mdoc_isdelim(const char *p)
474 {
475
476 if ('\0' == p[0])
477 return(DELIM_NONE);
478
479 if ('\0' == p[1])
480 switch (p[0]) {
481 case '(':
482 /* FALLTHROUGH */
483 case '[':
484 return(DELIM_OPEN);
485 case '|':
486 return(DELIM_MIDDLE);
487 case '.':
488 /* FALLTHROUGH */
489 case ',':
490 /* FALLTHROUGH */
491 case ';':
492 /* FALLTHROUGH */
493 case ':':
494 /* FALLTHROUGH */
495 case '?':
496 /* FALLTHROUGH */
497 case '!':
498 /* FALLTHROUGH */
499 case ')':
500 /* FALLTHROUGH */
501 case ']':
502 return(DELIM_CLOSE);
503 default:
504 return(DELIM_NONE);
505 }
506
507 if ('\\' != p[0])
508 return(DELIM_NONE);
509
510 if (0 == strcmp(p + 1, "."))
511 return(DELIM_CLOSE);
512 if (0 == strcmp(p + 1, "fR|\\fP"))
513 return(DELIM_MIDDLE);
514
515 return(DELIM_NONE);
516 }
517
518 void
519 mdoc_deroff(char **dest, const struct roff_node *n)
520 {
521 char *cp;
522 size_t sz;
523
524 if (n->type != ROFFT_TEXT) {
525 for (n = n->child; n; n = n->next)
526 mdoc_deroff(dest, n);
527 return;
528 }
529
530 /* Skip leading whitespace. */
531
532 for (cp = n->string; '\0' != *cp; cp++)
533 if (0 == isspace((unsigned char)*cp))
534 break;
535
536 /* Skip trailing whitespace. */
537
538 for (sz = strlen(cp); sz; sz--)
539 if (0 == isspace((unsigned char)cp[sz-1]))
540 break;
541
542 /* Skip empty strings. */
543
544 if (0 == sz)
545 return;
546
547 if (NULL == *dest) {
548 *dest = mandoc_strndup(cp, sz);
549 return;
550 }
551
552 mandoc_asprintf(&cp, "%s %*s", *dest, (int)sz, cp);
553 free(*dest);
554 *dest = cp;
555 }