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