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