]> git.cameronkatri.com Git - mandoc.git/blob - man.c
Clean up error reporting:
[mandoc.git] / man.c
1 /* $Id: man.c,v 1.133 2014/07/07 21:36:20 schwarze Exp $ */
2 /*
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2013, 2014 Ingo Schwarze <schwarze@openbsd.org>
5 * Copyright (c) 2011 Joerg Sonnenberger <joerg@netbsd.org>
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 AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR 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 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22
23 #include <sys/types.h>
24
25 #include <assert.h>
26 #include <ctype.h>
27 #include <stdarg.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31
32 #include "man.h"
33 #include "mandoc.h"
34 #include "mandoc_aux.h"
35 #include "libman.h"
36 #include "libmandoc.h"
37
38 const char *const __man_macronames[MAN_MAX] = {
39 "br", "TH", "SH", "SS",
40 "TP", "LP", "PP", "P",
41 "IP", "HP", "SM", "SB",
42 "BI", "IB", "BR", "RB",
43 "R", "B", "I", "IR",
44 "RI", "na", "sp", "nf",
45 "fi", "RE", "RS", "DT",
46 "UC", "PD", "AT", "in",
47 "ft", "OP", "EX", "EE",
48 "UR", "UE", "ll"
49 };
50
51 const char * const *man_macronames = __man_macronames;
52
53 static struct man_node *man_node_alloc(struct man *, int, int,
54 enum man_type, enum mant);
55 static int man_node_append(struct man *,
56 struct man_node *);
57 static void man_node_free(struct man_node *);
58 static void man_node_unlink(struct man *,
59 struct man_node *);
60 static int man_ptext(struct man *, int, char *, int);
61 static int man_pmacro(struct man *, int, char *, int);
62 static void man_free1(struct man *);
63 static void man_alloc1(struct man *);
64 static int man_descope(struct man *, int, int);
65
66
67 const struct man_node *
68 man_node(const struct man *man)
69 {
70
71 assert( ! (MAN_HALT & man->flags));
72 return(man->first);
73 }
74
75 const struct man_meta *
76 man_meta(const struct man *man)
77 {
78
79 assert( ! (MAN_HALT & man->flags));
80 return(&man->meta);
81 }
82
83 void
84 man_reset(struct man *man)
85 {
86
87 man_free1(man);
88 man_alloc1(man);
89 }
90
91 void
92 man_free(struct man *man)
93 {
94
95 man_free1(man);
96 free(man);
97 }
98
99 struct man *
100 man_alloc(struct roff *roff, struct mparse *parse, int quick)
101 {
102 struct man *p;
103
104 p = mandoc_calloc(1, sizeof(struct man));
105
106 man_hash_init();
107 p->parse = parse;
108 p->quick = quick;
109 p->roff = roff;
110
111 man_alloc1(p);
112 return(p);
113 }
114
115 int
116 man_endparse(struct man *man)
117 {
118
119 assert( ! (MAN_HALT & man->flags));
120 if (man_macroend(man))
121 return(1);
122 man->flags |= MAN_HALT;
123 return(0);
124 }
125
126 int
127 man_parseln(struct man *man, int ln, char *buf, int offs)
128 {
129
130 man->flags |= MAN_NEWLINE;
131
132 assert( ! (MAN_HALT & man->flags));
133
134 return (roff_getcontrol(man->roff, buf, &offs) ?
135 man_pmacro(man, ln, buf, offs) :
136 man_ptext(man, ln, buf, offs));
137 }
138
139 static void
140 man_free1(struct man *man)
141 {
142
143 if (man->first)
144 man_node_delete(man, man->first);
145 if (man->meta.title)
146 free(man->meta.title);
147 if (man->meta.source)
148 free(man->meta.source);
149 if (man->meta.date)
150 free(man->meta.date);
151 if (man->meta.vol)
152 free(man->meta.vol);
153 if (man->meta.msec)
154 free(man->meta.msec);
155 }
156
157 static void
158 man_alloc1(struct man *man)
159 {
160
161 memset(&man->meta, 0, sizeof(struct man_meta));
162 man->flags = 0;
163 man->last = mandoc_calloc(1, sizeof(struct man_node));
164 man->first = man->last;
165 man->last->type = MAN_ROOT;
166 man->last->tok = MAN_MAX;
167 man->next = MAN_NEXT_CHILD;
168 }
169
170
171 static int
172 man_node_append(struct man *man, struct man_node *p)
173 {
174
175 assert(man->last);
176 assert(man->first);
177 assert(MAN_ROOT != p->type);
178
179 switch (man->next) {
180 case MAN_NEXT_SIBLING:
181 man->last->next = p;
182 p->prev = man->last;
183 p->parent = man->last->parent;
184 break;
185 case MAN_NEXT_CHILD:
186 man->last->child = p;
187 p->parent = man->last;
188 break;
189 default:
190 abort();
191 /* NOTREACHED */
192 }
193
194 assert(p->parent);
195 p->parent->nchild++;
196
197 if ( ! man_valid_pre(man, p))
198 return(0);
199
200 switch (p->type) {
201 case MAN_HEAD:
202 assert(MAN_BLOCK == p->parent->type);
203 p->parent->head = p;
204 break;
205 case MAN_TAIL:
206 assert(MAN_BLOCK == p->parent->type);
207 p->parent->tail = p;
208 break;
209 case MAN_BODY:
210 assert(MAN_BLOCK == p->parent->type);
211 p->parent->body = p;
212 break;
213 default:
214 break;
215 }
216
217 man->last = p;
218
219 switch (p->type) {
220 case MAN_TBL:
221 /* FALLTHROUGH */
222 case MAN_TEXT:
223 if ( ! man_valid_post(man))
224 return(0);
225 break;
226 default:
227 break;
228 }
229
230 return(1);
231 }
232
233 static struct man_node *
234 man_node_alloc(struct man *man, int line, int pos,
235 enum man_type type, enum mant tok)
236 {
237 struct man_node *p;
238
239 p = mandoc_calloc(1, sizeof(struct man_node));
240 p->line = line;
241 p->pos = pos;
242 p->type = type;
243 p->tok = tok;
244
245 if (MAN_NEWLINE & man->flags)
246 p->flags |= MAN_LINE;
247 man->flags &= ~MAN_NEWLINE;
248 return(p);
249 }
250
251 int
252 man_elem_alloc(struct man *man, int line, int pos, enum mant tok)
253 {
254 struct man_node *p;
255
256 p = man_node_alloc(man, line, pos, MAN_ELEM, tok);
257 if ( ! man_node_append(man, p))
258 return(0);
259 man->next = MAN_NEXT_CHILD;
260 return(1);
261 }
262
263 int
264 man_tail_alloc(struct man *man, int line, int pos, enum mant tok)
265 {
266 struct man_node *p;
267
268 p = man_node_alloc(man, line, pos, MAN_TAIL, tok);
269 if ( ! man_node_append(man, p))
270 return(0);
271 man->next = MAN_NEXT_CHILD;
272 return(1);
273 }
274
275 int
276 man_head_alloc(struct man *man, int line, int pos, enum mant tok)
277 {
278 struct man_node *p;
279
280 p = man_node_alloc(man, line, pos, MAN_HEAD, tok);
281 if ( ! man_node_append(man, p))
282 return(0);
283 man->next = MAN_NEXT_CHILD;
284 return(1);
285 }
286
287 int
288 man_body_alloc(struct man *man, int line, int pos, enum mant tok)
289 {
290 struct man_node *p;
291
292 p = man_node_alloc(man, line, pos, MAN_BODY, tok);
293 if ( ! man_node_append(man, p))
294 return(0);
295 man->next = MAN_NEXT_CHILD;
296 return(1);
297 }
298
299 int
300 man_block_alloc(struct man *man, int line, int pos, enum mant tok)
301 {
302 struct man_node *p;
303
304 p = man_node_alloc(man, line, pos, MAN_BLOCK, tok);
305 if ( ! man_node_append(man, p))
306 return(0);
307 man->next = MAN_NEXT_CHILD;
308 return(1);
309 }
310
311 int
312 man_word_alloc(struct man *man, int line, int pos, const char *word)
313 {
314 struct man_node *n;
315
316 n = man_node_alloc(man, line, pos, MAN_TEXT, MAN_MAX);
317 n->string = roff_strdup(man->roff, word);
318
319 if ( ! man_node_append(man, n))
320 return(0);
321
322 man->next = MAN_NEXT_SIBLING;
323 return(1);
324 }
325
326 /*
327 * Free all of the resources held by a node. This does NOT unlink a
328 * node from its context; for that, see man_node_unlink().
329 */
330 static void
331 man_node_free(struct man_node *p)
332 {
333
334 if (p->string)
335 free(p->string);
336 free(p);
337 }
338
339 void
340 man_node_delete(struct man *man, struct man_node *p)
341 {
342
343 while (p->child)
344 man_node_delete(man, p->child);
345
346 man_node_unlink(man, p);
347 man_node_free(p);
348 }
349
350 int
351 man_addeqn(struct man *man, const struct eqn *ep)
352 {
353 struct man_node *n;
354
355 assert( ! (MAN_HALT & man->flags));
356
357 n = man_node_alloc(man, ep->ln, ep->pos, MAN_EQN, MAN_MAX);
358 n->eqn = ep;
359
360 if ( ! man_node_append(man, n))
361 return(0);
362
363 man->next = MAN_NEXT_SIBLING;
364 return(man_descope(man, ep->ln, ep->pos));
365 }
366
367 int
368 man_addspan(struct man *man, const struct tbl_span *sp)
369 {
370 struct man_node *n;
371
372 assert( ! (MAN_HALT & man->flags));
373
374 n = man_node_alloc(man, sp->line, 0, MAN_TBL, MAN_MAX);
375 n->span = sp;
376
377 if ( ! man_node_append(man, n))
378 return(0);
379
380 man->next = MAN_NEXT_SIBLING;
381 return(man_descope(man, sp->line, 0));
382 }
383
384 static int
385 man_descope(struct man *man, int line, int offs)
386 {
387 /*
388 * Co-ordinate what happens with having a next-line scope open:
389 * first close out the element scope (if applicable), then close
390 * out the block scope (also if applicable).
391 */
392
393 if (MAN_ELINE & man->flags) {
394 man->flags &= ~MAN_ELINE;
395 if ( ! man_unscope(man, man->last->parent))
396 return(0);
397 }
398
399 if ( ! (MAN_BLINE & man->flags))
400 return(1);
401 man->flags &= ~MAN_BLINE;
402
403 if ( ! man_unscope(man, man->last->parent))
404 return(0);
405 return(man_body_alloc(man, line, offs, man->last->tok));
406 }
407
408 static int
409 man_ptext(struct man *man, int line, char *buf, int offs)
410 {
411 int i;
412
413 /* Literal free-form text whitespace is preserved. */
414
415 if (MAN_LITERAL & man->flags) {
416 if ( ! man_word_alloc(man, line, offs, buf + offs))
417 return(0);
418 return(man_descope(man, line, offs));
419 }
420
421 for (i = offs; ' ' == buf[i]; i++)
422 /* Skip leading whitespace. */ ;
423
424 /*
425 * Blank lines are ignored right after headings
426 * but add a single vertical space elsewhere.
427 */
428
429 if ('\0' == buf[i]) {
430 /* Allocate a blank entry. */
431 if (MAN_SH != man->last->tok &&
432 MAN_SS != man->last->tok) {
433 if ( ! man_elem_alloc(man, line, offs, MAN_sp))
434 return(0);
435 man->next = MAN_NEXT_SIBLING;
436 }
437 return(1);
438 }
439
440 /*
441 * Warn if the last un-escaped character is whitespace. Then
442 * strip away the remaining spaces (tabs stay!).
443 */
444
445 i = (int)strlen(buf);
446 assert(i);
447
448 if (' ' == buf[i - 1] || '\t' == buf[i - 1]) {
449 if (i > 1 && '\\' != buf[i - 2])
450 mandoc_msg(MANDOCERR_SPACE_EOL, man->parse,
451 line, i - 1, NULL);
452
453 for (--i; i && ' ' == buf[i]; i--)
454 /* Spin back to non-space. */ ;
455
456 /* Jump ahead of escaped whitespace. */
457 i += '\\' == buf[i] ? 2 : 1;
458
459 buf[i] = '\0';
460 }
461
462 if ( ! man_word_alloc(man, line, offs, buf + offs))
463 return(0);
464
465 /*
466 * End-of-sentence check. If the last character is an unescaped
467 * EOS character, then flag the node as being the end of a
468 * sentence. The front-end will know how to interpret this.
469 */
470
471 assert(i);
472 if (mandoc_eos(buf, (size_t)i))
473 man->last->flags |= MAN_EOS;
474
475 return(man_descope(man, line, offs));
476 }
477
478 static int
479 man_pmacro(struct man *man, int ln, char *buf, int offs)
480 {
481 int i, ppos;
482 enum mant tok;
483 char mac[5];
484 struct man_node *n;
485
486 if ('"' == buf[offs]) {
487 mandoc_msg(MANDOCERR_COMMENT_BAD, man->parse,
488 ln, offs, NULL);
489 return(1);
490 } else if ('\0' == buf[offs])
491 return(1);
492
493 ppos = offs;
494
495 /*
496 * Copy the first word into a nil-terminated buffer.
497 * Stop copying when a tab, space, or eoln is encountered.
498 */
499
500 i = 0;
501 while (i < 4 && '\0' != buf[offs] && ' ' != buf[offs] &&
502 '\t' != buf[offs])
503 mac[i++] = buf[offs++];
504
505 mac[i] = '\0';
506
507 tok = (i > 0 && i < 4) ? man_hash_find(mac) : MAN_MAX;
508
509 if (MAN_MAX == tok) {
510 mandoc_vmsg(MANDOCERR_MACRO, man->parse, ln, ppos,
511 "%s", buf + ppos - 1);
512 return(1);
513 }
514
515 /* The macro is sane. Jump to the next word. */
516
517 while (buf[offs] && ' ' == buf[offs])
518 offs++;
519
520 /*
521 * Trailing whitespace. Note that tabs are allowed to be passed
522 * into the parser as "text", so we only warn about spaces here.
523 */
524
525 if ('\0' == buf[offs] && ' ' == buf[offs - 1])
526 mandoc_msg(MANDOCERR_SPACE_EOL, man->parse,
527 ln, offs - 1, NULL);
528
529 /*
530 * Remove prior ELINE macro, as it's being clobbered by a new
531 * macro. Note that NSCOPED macros do not close out ELINE
532 * macros---they don't print text---so we let those slip by.
533 */
534
535 if ( ! (MAN_NSCOPED & man_macros[tok].flags) &&
536 man->flags & MAN_ELINE) {
537 n = man->last;
538 assert(MAN_TEXT != n->type);
539
540 /* Remove repeated NSCOPED macros causing ELINE. */
541
542 if (MAN_NSCOPED & man_macros[n->tok].flags)
543 n = n->parent;
544
545 mandoc_vmsg(MANDOCERR_BLK_LINE, man->parse, n->line,
546 n->pos, "%s breaks %s", man_macronames[tok],
547 man_macronames[n->tok]);
548
549 man_node_delete(man, n);
550 man->flags &= ~MAN_ELINE;
551 }
552
553 /*
554 * Remove prior BLINE macro that is being clobbered.
555 */
556 if ((man->flags & MAN_BLINE) &&
557 (MAN_BSCOPE & man_macros[tok].flags)) {
558 n = man->last;
559
560 /* Might be a text node like 8 in
561 * .TP 8
562 * .SH foo
563 */
564 if (MAN_TEXT == n->type)
565 n = n->parent;
566
567 /* Remove element that didn't end BLINE, if any. */
568 if ( ! (MAN_BSCOPE & man_macros[n->tok].flags))
569 n = n->parent;
570
571 assert(MAN_HEAD == n->type);
572 n = n->parent;
573 assert(MAN_BLOCK == n->type);
574 assert(MAN_SCOPED & man_macros[n->tok].flags);
575
576 mandoc_vmsg(MANDOCERR_BLK_LINE, man->parse, n->line,
577 n->pos, "%s breaks %s", man_macronames[tok],
578 man_macronames[n->tok]);
579
580 man_node_delete(man, n);
581 man->flags &= ~MAN_BLINE;
582 }
583
584 /*
585 * Save the fact that we're in the next-line for a block. In
586 * this way, embedded roff instructions can "remember" state
587 * when they exit.
588 */
589
590 if (MAN_BLINE & man->flags)
591 man->flags |= MAN_BPLINE;
592
593 /* Call to handler... */
594
595 assert(man_macros[tok].fp);
596 if ( ! (*man_macros[tok].fp)(man, tok, ln, ppos, &offs, buf))
597 goto err;
598
599 /* In quick mode (for mandocdb), abort after the NAME section. */
600
601 if (man->quick && MAN_SH == tok) {
602 n = man->last;
603 if (MAN_BODY == n->type &&
604 strcmp(n->prev->child->string, "NAME"))
605 return(2);
606 }
607
608 /*
609 * We weren't in a block-line scope when entering the
610 * above-parsed macro, so return.
611 */
612
613 if ( ! (MAN_BPLINE & man->flags)) {
614 man->flags &= ~MAN_ILINE;
615 return(1);
616 }
617 man->flags &= ~MAN_BPLINE;
618
619 /*
620 * If we're in a block scope, then allow this macro to slip by
621 * without closing scope around it.
622 */
623
624 if (MAN_ILINE & man->flags) {
625 man->flags &= ~MAN_ILINE;
626 return(1);
627 }
628
629 /*
630 * If we've opened a new next-line element scope, then return
631 * now, as the next line will close out the block scope.
632 */
633
634 if (MAN_ELINE & man->flags)
635 return(1);
636
637 /* Close out the block scope opened in the prior line. */
638
639 assert(MAN_BLINE & man->flags);
640 man->flags &= ~MAN_BLINE;
641
642 if ( ! man_unscope(man, man->last->parent))
643 return(0);
644 return(man_body_alloc(man, ln, ppos, man->last->tok));
645
646 err: /* Error out. */
647
648 man->flags |= MAN_HALT;
649 return(0);
650 }
651
652 /*
653 * Unlink a node from its context. If "man" is provided, the last parse
654 * point will also be adjusted accordingly.
655 */
656 static void
657 man_node_unlink(struct man *man, struct man_node *n)
658 {
659
660 /* Adjust siblings. */
661
662 if (n->prev)
663 n->prev->next = n->next;
664 if (n->next)
665 n->next->prev = n->prev;
666
667 /* Adjust parent. */
668
669 if (n->parent) {
670 n->parent->nchild--;
671 if (n->parent->child == n)
672 n->parent->child = n->prev ? n->prev : n->next;
673 }
674
675 /* Adjust parse point, if applicable. */
676
677 if (man && man->last == n) {
678 /*XXX: this can occur when bailing from validation. */
679 /*assert(NULL == n->next);*/
680 if (n->prev) {
681 man->last = n->prev;
682 man->next = MAN_NEXT_SIBLING;
683 } else {
684 man->last = n->parent;
685 man->next = MAN_NEXT_CHILD;
686 }
687 }
688
689 if (man && man->first == n)
690 man->first = NULL;
691 }
692
693 const struct mparse *
694 man_mparse(const struct man *man)
695 {
696
697 assert(man && man->parse);
698 return(man->parse);
699 }
700
701 void
702 man_deroff(char **dest, const struct man_node *n)
703 {
704 char *cp;
705 size_t sz;
706
707 if (MAN_TEXT != n->type) {
708 for (n = n->child; n; n = n->next)
709 man_deroff(dest, n);
710 return;
711 }
712
713 /* Skip leading whitespace and escape sequences. */
714
715 cp = n->string;
716 while ('\0' != *cp) {
717 if ('\\' == *cp) {
718 cp++;
719 mandoc_escape((const char **)&cp, NULL, NULL);
720 } else if (isspace((unsigned char)*cp))
721 cp++;
722 else
723 break;
724 }
725
726 /* Skip trailing whitespace. */
727
728 for (sz = strlen(cp); sz; sz--)
729 if (0 == isspace((unsigned char)cp[sz-1]))
730 break;
731
732 /* Skip empty strings. */
733
734 if (0 == sz)
735 return;
736
737 if (NULL == *dest) {
738 *dest = mandoc_strndup(cp, sz);
739 return;
740 }
741
742 mandoc_asprintf(&cp, "%s %*s", *dest, (int)sz, cp);
743 free(*dest);
744 *dest = cp;
745 }