]> git.cameronkatri.com Git - mandoc.git/blob - man.c
Fix allowing silly '\'' control character.
[mandoc.git] / man.c
1 /* $Id: man.c,v 1.71 2010/05/16 00:04:46 kristaps Exp $ */
2 /*
3 * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20
21 #include <sys/types.h>
22
23 #include <assert.h>
24 #include <ctype.h>
25 #include <stdarg.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29
30 #include "libman.h"
31 #include "libmandoc.h"
32
33 const char *const __man_merrnames[WERRMAX] = {
34 "invalid character", /* WNPRINT */
35 "invalid date format", /* WDATE */
36 "scope of prior line violated", /* WLNSCOPE */
37 "over-zealous prior line scope violation", /* WLNSCOPE2 */
38 "trailing whitespace", /* WTSPACE */
39 "unterminated quoted parameter", /* WTQUOTE */
40 "document has no body", /* WNODATA */
41 "document has no title/section", /* WNOTITLE */
42 "invalid escape sequence", /* WESCAPE */
43 "invalid number format", /* WNUMFMT */
44 "expected block head arguments", /* WHEADARGS */
45 "expected block body arguments", /* WBODYARGS */
46 "expected empty block head", /* WNHEADARGS */
47 "ill-formed macro", /* WMACROFORM */
48 "scope open on exit", /* WEXITSCOPE */
49 "no scope context", /* WNOSCOPE */
50 "literal context already open", /* WOLITERAL */
51 "no literal context open", /* WNLITERAL */
52 "document title should be uppercase", /* WTITLECASE */
53 "deprecated comment style", /* WBADCOMMENT */
54 };
55
56 const char *const __man_macronames[MAN_MAX] = {
57 "br", "TH", "SH", "SS",
58 "TP", "LP", "PP", "P",
59 "IP", "HP", "SM", "SB",
60 "BI", "IB", "BR", "RB",
61 "R", "B", "I", "IR",
62 "RI", "na", "i", "sp",
63 "nf", "fi", "r", "RE",
64 "RS", "DT", "UC", "PD",
65 "Sp", "Vb", "Ve",
66 };
67
68 const char * const *man_macronames = __man_macronames;
69
70 static struct man_node *man_node_alloc(int, int,
71 enum man_type, enum mant);
72 static int man_node_append(struct man *,
73 struct man_node *);
74 static void man_node_free(struct man_node *);
75 static void man_node_unlink(struct man *,
76 struct man_node *);
77 static int man_ptext(struct man *, int, char *);
78 static int man_pmacro(struct man *, int, char *);
79 static void man_free1(struct man *);
80 static void man_alloc1(struct man *);
81 static int macrowarn(struct man *, int, const char *);
82
83
84 const struct man_node *
85 man_node(const struct man *m)
86 {
87
88 return(MAN_HALT & m->flags ? NULL : m->first);
89 }
90
91
92 const struct man_meta *
93 man_meta(const struct man *m)
94 {
95
96 return(MAN_HALT & m->flags ? NULL : &m->meta);
97 }
98
99
100 void
101 man_reset(struct man *man)
102 {
103
104 man_free1(man);
105 man_alloc1(man);
106 }
107
108
109 void
110 man_free(struct man *man)
111 {
112
113 man_free1(man);
114 free(man);
115 }
116
117
118 struct man *
119 man_alloc(void *data, int pflags, const struct man_cb *cb)
120 {
121 struct man *p;
122
123 p = mandoc_calloc(1, sizeof(struct man));
124
125 if (cb)
126 memcpy(&p->cb, cb, sizeof(struct man_cb));
127
128 man_hash_init();
129 p->data = data;
130 p->pflags = pflags;
131
132 man_alloc1(p);
133 return(p);
134 }
135
136
137 int
138 man_endparse(struct man *m)
139 {
140
141 if (MAN_HALT & m->flags)
142 return(0);
143 else if (man_macroend(m))
144 return(1);
145 m->flags |= MAN_HALT;
146 return(0);
147 }
148
149
150 int
151 man_parseln(struct man *m, int ln, char *buf)
152 {
153
154 return(('.' == *buf || '\'' == *buf) ?
155 man_pmacro(m, ln, buf) :
156 man_ptext(m, ln, buf));
157 }
158
159
160 static void
161 man_free1(struct man *man)
162 {
163
164 if (man->first)
165 man_node_delete(man, man->first);
166 if (man->meta.title)
167 free(man->meta.title);
168 if (man->meta.source)
169 free(man->meta.source);
170 if (man->meta.vol)
171 free(man->meta.vol);
172 if (man->meta.msec)
173 free(man->meta.msec);
174 }
175
176
177 static void
178 man_alloc1(struct man *m)
179 {
180
181 memset(&m->meta, 0, sizeof(struct man_meta));
182 m->flags = 0;
183 m->last = mandoc_calloc(1, sizeof(struct man_node));
184 m->first = m->last;
185 m->last->type = MAN_ROOT;
186 m->last->tok = MAN_MAX;
187 m->next = MAN_NEXT_CHILD;
188 }
189
190
191 static int
192 man_node_append(struct man *man, struct man_node *p)
193 {
194
195 assert(man->last);
196 assert(man->first);
197 assert(MAN_ROOT != p->type);
198
199 switch (man->next) {
200 case (MAN_NEXT_SIBLING):
201 man->last->next = p;
202 p->prev = man->last;
203 p->parent = man->last->parent;
204 break;
205 case (MAN_NEXT_CHILD):
206 man->last->child = p;
207 p->parent = man->last;
208 break;
209 default:
210 abort();
211 /* NOTREACHED */
212 }
213
214 assert(p->parent);
215 p->parent->nchild++;
216
217 if ( ! man_valid_pre(man, p))
218 return(0);
219
220 switch (p->type) {
221 case (MAN_HEAD):
222 assert(MAN_BLOCK == p->parent->type);
223 p->parent->head = p;
224 break;
225 case (MAN_BODY):
226 assert(MAN_BLOCK == p->parent->type);
227 p->parent->body = p;
228 break;
229 default:
230 break;
231 }
232
233 man->last = p;
234
235 switch (p->type) {
236 case (MAN_TEXT):
237 if ( ! man_valid_post(man))
238 return(0);
239 if ( ! man_action_post(man))
240 return(0);
241 break;
242 default:
243 break;
244 }
245
246 return(1);
247 }
248
249
250 static struct man_node *
251 man_node_alloc(int line, int pos, enum man_type type, enum mant tok)
252 {
253 struct man_node *p;
254
255 p = mandoc_calloc(1, sizeof(struct man_node));
256 p->line = line;
257 p->pos = pos;
258 p->type = type;
259 p->tok = tok;
260 return(p);
261 }
262
263
264 int
265 man_elem_alloc(struct man *m, int line, int pos, enum mant tok)
266 {
267 struct man_node *p;
268
269 p = man_node_alloc(line, pos, MAN_ELEM, tok);
270 if ( ! man_node_append(m, p))
271 return(0);
272 m->next = MAN_NEXT_CHILD;
273 return(1);
274 }
275
276
277 int
278 man_head_alloc(struct man *m, int line, int pos, enum mant tok)
279 {
280 struct man_node *p;
281
282 p = man_node_alloc(line, pos, MAN_HEAD, tok);
283 if ( ! man_node_append(m, p))
284 return(0);
285 m->next = MAN_NEXT_CHILD;
286 return(1);
287 }
288
289
290 int
291 man_body_alloc(struct man *m, int line, int pos, enum mant tok)
292 {
293 struct man_node *p;
294
295 p = man_node_alloc(line, pos, MAN_BODY, tok);
296 if ( ! man_node_append(m, p))
297 return(0);
298 m->next = MAN_NEXT_CHILD;
299 return(1);
300 }
301
302
303 int
304 man_block_alloc(struct man *m, int line, int pos, enum mant tok)
305 {
306 struct man_node *p;
307
308 p = man_node_alloc(line, pos, MAN_BLOCK, tok);
309 if ( ! man_node_append(m, p))
310 return(0);
311 m->next = MAN_NEXT_CHILD;
312 return(1);
313 }
314
315
316 int
317 man_word_alloc(struct man *m, int line, int pos, const char *word)
318 {
319 struct man_node *n;
320 size_t sv, len;
321
322 len = strlen(word);
323
324 n = man_node_alloc(line, pos, MAN_TEXT, MAN_MAX);
325 n->string = mandoc_malloc(len + 1);
326 sv = strlcpy(n->string, word, len + 1);
327
328 /* Prohibit truncation. */
329 assert(sv < len + 1);
330
331 if ( ! man_node_append(m, n))
332 return(0);
333
334 m->next = MAN_NEXT_SIBLING;
335 return(1);
336 }
337
338
339 /*
340 * Free all of the resources held by a node. This does NOT unlink a
341 * node from its context; for that, see man_node_unlink().
342 */
343 static void
344 man_node_free(struct man_node *p)
345 {
346
347 if (p->string)
348 free(p->string);
349 free(p);
350 }
351
352
353 void
354 man_node_delete(struct man *m, struct man_node *p)
355 {
356
357 while (p->child)
358 man_node_delete(m, p->child);
359
360 man_node_unlink(m, p);
361 man_node_free(p);
362 }
363
364
365 static int
366 man_ptext(struct man *m, int line, char *buf)
367 {
368 int i;
369
370 /* Ignore bogus comments. */
371
372 if ('\\' == buf[0] && '.' == buf[1] && '\"' == buf[2])
373 return(man_pwarn(m, line, 0, WBADCOMMENT));
374
375 /* Literal free-form text whitespace is preserved. */
376
377 if (MAN_LITERAL & m->flags) {
378 if ( ! man_word_alloc(m, line, 0, buf))
379 return(0);
380 goto descope;
381 }
382
383 /* Pump blank lines directly into the backend. */
384
385 for (i = 0; ' ' == buf[i]; i++)
386 /* Skip leading whitespace. */ ;
387
388 if ('\0' == buf[i]) {
389 /* Allocate a blank entry. */
390 if ( ! man_word_alloc(m, line, 0, ""))
391 return(0);
392 goto descope;
393 }
394
395 /*
396 * Warn if the last un-escaped character is whitespace. Then
397 * strip away the remaining spaces (tabs stay!).
398 */
399
400 i = (int)strlen(buf);
401 assert(i);
402
403 if (' ' == buf[i - 1] || '\t' == buf[i - 1]) {
404 if (i > 1 && '\\' != buf[i - 2])
405 if ( ! man_pwarn(m, line, i - 1, WTSPACE))
406 return(0);
407
408 for (--i; i && ' ' == buf[i]; i--)
409 /* Spin back to non-space. */ ;
410
411 /* Jump ahead of escaped whitespace. */
412 i += '\\' == buf[i] ? 2 : 1;
413
414 buf[i] = '\0';
415 }
416
417 if ( ! man_word_alloc(m, line, 0, buf))
418 return(0);
419
420 /*
421 * End-of-sentence check. If the last character is an unescaped
422 * EOS character, then flag the node as being the end of a
423 * sentence. The front-end will know how to interpret this.
424 */
425
426 /* FIXME: chain of close delims. */
427
428 assert(i);
429
430 if (mandoc_eos(buf, (size_t)i))
431 m->last->flags |= MAN_EOS;
432
433 descope:
434 /*
435 * Co-ordinate what happens with having a next-line scope open:
436 * first close out the element scope (if applicable), then close
437 * out the block scope (also if applicable).
438 */
439
440 if (MAN_ELINE & m->flags) {
441 m->flags &= ~MAN_ELINE;
442 if ( ! man_unscope(m, m->last->parent, WERRMAX))
443 return(0);
444 }
445
446 if ( ! (MAN_BLINE & m->flags))
447 return(1);
448 m->flags &= ~MAN_BLINE;
449
450 if ( ! man_unscope(m, m->last->parent, WERRMAX))
451 return(0);
452 return(man_body_alloc(m, line, 0, m->last->tok));
453 }
454
455
456 static int
457 macrowarn(struct man *m, int ln, const char *buf)
458 {
459 if ( ! (MAN_IGN_MACRO & m->pflags))
460 return(man_verr(m, ln, 0, "unknown macro: %s%s",
461 buf, strlen(buf) > 3 ? "..." : ""));
462 return(man_vwarn(m, ln, 0, "unknown macro: %s%s",
463 buf, strlen(buf) > 3 ? "..." : ""));
464 }
465
466
467 int
468 man_pmacro(struct man *m, int ln, char *buf)
469 {
470 int i, j, ppos;
471 enum mant tok;
472 char mac[5];
473 struct man_node *n;
474
475 /* Comments and empties are quickly ignored. */
476
477 if ('\0' == buf[1])
478 return(1);
479
480 i = 1;
481
482 /*
483 * Skip whitespace between the control character and initial
484 * text. "Whitespace" is both spaces and tabs.
485 */
486
487 if (' ' == buf[i] || '\t' == buf[i]) {
488 i++;
489 while (buf[i] && (' ' == buf[i] || '\t' == buf[i]))
490 i++;
491 if ('\0' == buf[i])
492 goto out;
493 }
494
495 ppos = i;
496
497 /* Copy the first word into a nil-terminated buffer. */
498
499 for (j = 0; j < 4; j++, i++) {
500 if ('\0' == (mac[j] = buf[i]))
501 break;
502 else if (' ' == buf[i])
503 break;
504
505 /* Check for invalid characters. */
506
507 if (isgraph((u_char)buf[i]))
508 continue;
509 return(man_perr(m, ln, i, WNPRINT));
510 }
511
512 mac[j] = '\0';
513
514 if (j == 4 || j < 1) {
515 if ( ! (MAN_IGN_MACRO & m->pflags)) {
516 (void)man_perr(m, ln, ppos, WMACROFORM);
517 goto err;
518 }
519 if ( ! man_pwarn(m, ln, ppos, WMACROFORM))
520 goto err;
521 return(1);
522 }
523
524 if (MAN_MAX == (tok = man_hash_find(mac))) {
525 if ( ! macrowarn(m, ln, mac))
526 goto err;
527 return(1);
528 }
529
530 /* The macro is sane. Jump to the next word. */
531
532 while (buf[i] && ' ' == buf[i])
533 i++;
534
535 /*
536 * Trailing whitespace. Note that tabs are allowed to be passed
537 * into the parser as "text", so we only warn about spaces here.
538 */
539
540 if ('\0' == buf[i] && ' ' == buf[i - 1])
541 if ( ! man_pwarn(m, ln, i - 1, WTSPACE))
542 goto err;
543
544 /*
545 * Remove prior ELINE macro, as it's being clobbering by a new
546 * macro. Note that NSCOPED macros do not close out ELINE
547 * macros---they don't print text---so we let those slip by.
548 */
549
550 if ( ! (MAN_NSCOPED & man_macros[tok].flags) &&
551 m->flags & MAN_ELINE) {
552 assert(MAN_TEXT != m->last->type);
553
554 /*
555 * This occurs in the following construction:
556 * .B
557 * .br
558 * .B
559 * .br
560 * I hate man macros.
561 * Flat-out disallow this madness.
562 */
563 if (MAN_NSCOPED & man_macros[m->last->tok].flags)
564 return(man_perr(m, ln, ppos, WLNSCOPE));
565
566 n = m->last;
567
568 assert(n);
569 assert(NULL == n->child);
570 assert(0 == n->nchild);
571
572 if ( ! man_nwarn(m, n, WLNSCOPE))
573 return(0);
574
575 man_node_delete(m, n);
576 m->flags &= ~MAN_ELINE;
577 }
578
579 /*
580 * Save the fact that we're in the next-line for a block. In
581 * this way, embedded roff instructions can "remember" state
582 * when they exit.
583 */
584
585 if (MAN_BLINE & m->flags)
586 m->flags |= MAN_BPLINE;
587
588 /* Call to handler... */
589
590 assert(man_macros[tok].fp);
591 if ( ! (*man_macros[tok].fp)(m, tok, ln, ppos, &i, buf))
592 goto err;
593
594 out:
595 /*
596 * We weren't in a block-line scope when entering the
597 * above-parsed macro, so return.
598 */
599
600 if ( ! (MAN_BPLINE & m->flags)) {
601 m->flags &= ~MAN_ILINE;
602 return(1);
603 }
604 m->flags &= ~MAN_BPLINE;
605
606 /*
607 * If we're in a block scope, then allow this macro to slip by
608 * without closing scope around it.
609 */
610
611 if (MAN_ILINE & m->flags) {
612 m->flags &= ~MAN_ILINE;
613 return(1);
614 }
615
616 /*
617 * If we've opened a new next-line element scope, then return
618 * now, as the next line will close out the block scope.
619 */
620
621 if (MAN_ELINE & m->flags)
622 return(1);
623
624 /* Close out the block scope opened in the prior line. */
625
626 assert(MAN_BLINE & m->flags);
627 m->flags &= ~MAN_BLINE;
628
629 if ( ! man_unscope(m, m->last->parent, WERRMAX))
630 return(0);
631 return(man_body_alloc(m, ln, 0, m->last->tok));
632
633 err: /* Error out. */
634
635 m->flags |= MAN_HALT;
636 return(0);
637 }
638
639
640 int
641 man_verr(struct man *man, int ln, int pos, const char *fmt, ...)
642 {
643 char buf[256];
644 va_list ap;
645
646 if (NULL == man->cb.man_err)
647 return(0);
648
649 va_start(ap, fmt);
650 (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
651 va_end(ap);
652 return((*man->cb.man_err)(man->data, ln, pos, buf));
653 }
654
655
656 int
657 man_vwarn(struct man *man, int ln, int pos, const char *fmt, ...)
658 {
659 char buf[256];
660 va_list ap;
661
662 if (NULL == man->cb.man_warn)
663 return(0);
664
665 va_start(ap, fmt);
666 (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
667 va_end(ap);
668 return((*man->cb.man_warn)(man->data, ln, pos, buf));
669 }
670
671
672 int
673 man_err(struct man *m, int line, int pos, int iserr, enum merr type)
674 {
675 const char *p;
676
677 p = __man_merrnames[(int)type];
678 assert(p);
679
680 if (iserr)
681 return(man_verr(m, line, pos, p));
682
683 return(man_vwarn(m, line, pos, p));
684 }
685
686
687 /*
688 * Unlink a node from its context. If "m" is provided, the last parse
689 * point will also be adjusted accordingly.
690 */
691 static void
692 man_node_unlink(struct man *m, struct man_node *n)
693 {
694
695 /* Adjust siblings. */
696
697 if (n->prev)
698 n->prev->next = n->next;
699 if (n->next)
700 n->next->prev = n->prev;
701
702 /* Adjust parent. */
703
704 if (n->parent) {
705 n->parent->nchild--;
706 if (n->parent->child == n)
707 n->parent->child = n->prev ? n->prev : n->next;
708 }
709
710 /* Adjust parse point, if applicable. */
711
712 if (m && m->last == n) {
713 /*XXX: this can occur when bailing from validation. */
714 /*assert(NULL == n->next);*/
715 if (n->prev) {
716 m->last = n->prev;
717 m->next = MAN_NEXT_SIBLING;
718 } else {
719 m->last = n->parent;
720 m->next = MAN_NEXT_CHILD;
721 }
722 }
723
724 if (m && m->first == n)
725 m->first = NULL;
726 }