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