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