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