]> git.cameronkatri.com Git - mandoc.git/blob - man.c
Clarify -man -T[x]html handling of `br' within `B'.
[mandoc.git] / man.c
1 /* $Id: man.c,v 1.51 2010/03/22 14:03:03 kristaps Exp $ */
2 /*
3 * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
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 "libman.h"
31 #include "libmandoc.h"
32
33 const char *const __man_merrnames[WERRMAX] = {
34 "invalid character", /* WNPRINT */
35 "invalid manual section", /* WMSEC */
36 "invalid date format", /* WDATE */
37 "scope of prior line violated", /* WLNSCOPE */
38 "over-zealous prior line scope violation", /* WLNSCOPE2 */
39 "trailing whitespace", /* WTSPACE */
40 "unterminated quoted parameter", /* WTQUOTE */
41 "document has no body", /* WNODATA */
42 "document has no title/section", /* WNOTITLE */
43 "invalid escape sequence", /* WESCAPE */
44 "invalid number format", /* WNUMFMT */
45 "expected block head arguments", /* WHEADARGS */
46 "expected block body arguments", /* WBODYARGS */
47 "expected empty block head", /* WNHEADARGS */
48 "ill-formed macro", /* WMACROFORM */
49 "scope open on exit", /* WEXITSCOPE */
50 "no scope context", /* WNOSCOPE */
51 "literal context already open", /* WOLITERAL */
52 "no literal context open" /* WNLITERAL */
53 };
54
55 const char *const __man_macronames[MAN_MAX] = {
56 "br", "TH", "SH", "SS",
57 "TP", "LP", "PP", "P",
58 "IP", "HP", "SM", "SB",
59 "BI", "IB", "BR", "RB",
60 "R", "B", "I", "IR",
61 "RI", "na", "i", "sp",
62 "nf", "fi", "r", "RE",
63 "RS", "DT", "UC", "PD"
64 };
65
66 const char * const *man_macronames = __man_macronames;
67
68 static struct man_node *man_node_alloc(int, int,
69 enum man_type, int);
70 static int man_node_append(struct man *,
71 struct man_node *);
72 static int man_ptext(struct man *, int, char *);
73 static int man_pmacro(struct man *, int, char *);
74 static void man_free1(struct man *);
75 static void man_alloc1(struct man *);
76 static int pstring(struct man *, int, int,
77 const char *, size_t);
78 static int macrowarn(struct man *, int, const char *);
79
80
81 const struct man_node *
82 man_node(const struct man *m)
83 {
84
85 return(MAN_HALT & m->flags ? NULL : m->first);
86 }
87
88
89 const struct man_meta *
90 man_meta(const struct man *m)
91 {
92
93 return(MAN_HALT & m->flags ? NULL : &m->meta);
94 }
95
96
97 void
98 man_reset(struct man *man)
99 {
100
101 man_free1(man);
102 man_alloc1(man);
103 }
104
105
106 void
107 man_free(struct man *man)
108 {
109
110 man_free1(man);
111 free(man);
112 }
113
114
115 struct man *
116 man_alloc(void *data, int pflags, const struct man_cb *cb)
117 {
118 struct man *p;
119
120 p = mandoc_calloc(1, sizeof(struct man));
121
122 if (cb)
123 memcpy(&p->cb, cb, sizeof(struct man_cb));
124
125 man_hash_init();
126 p->data = data;
127 p->pflags = pflags;
128
129 man_alloc1(p);
130 return(p);
131 }
132
133
134 int
135 man_endparse(struct man *m)
136 {
137
138 if (MAN_HALT & m->flags)
139 return(0);
140 else if (man_macroend(m))
141 return(1);
142 m->flags |= MAN_HALT;
143 return(0);
144 }
145
146
147 int
148 man_parseln(struct man *m, int ln, char *buf)
149 {
150
151 return('.' == *buf ?
152 man_pmacro(m, ln, buf) :
153 man_ptext(m, ln, buf));
154 }
155
156
157 static void
158 man_free1(struct man *man)
159 {
160
161 if (man->first)
162 man_node_freelist(man->first);
163 if (man->meta.title)
164 free(man->meta.title);
165 if (man->meta.source)
166 free(man->meta.source);
167 if (man->meta.vol)
168 free(man->meta.vol);
169 }
170
171
172 static void
173 man_alloc1(struct man *m)
174 {
175
176 memset(&m->meta, 0, sizeof(struct man_meta));
177 m->flags = 0;
178 m->last = mandoc_calloc(1, sizeof(struct man_node));
179 m->first = m->last;
180 m->last->type = MAN_ROOT;
181 m->next = MAN_NEXT_CHILD;
182 }
183
184
185 static int
186 man_node_append(struct man *man, struct man_node *p)
187 {
188
189 assert(man->last);
190 assert(man->first);
191 assert(MAN_ROOT != p->type);
192
193 switch (man->next) {
194 case (MAN_NEXT_SIBLING):
195 man->last->next = p;
196 p->prev = man->last;
197 p->parent = man->last->parent;
198 break;
199 case (MAN_NEXT_CHILD):
200 man->last->child = p;
201 p->parent = man->last;
202 break;
203 default:
204 abort();
205 /* NOTREACHED */
206 }
207
208 p->parent->nchild++;
209
210 if ( ! man_valid_pre(man, p))
211 return(0);
212
213 switch (p->type) {
214 case (MAN_HEAD):
215 assert(MAN_BLOCK == p->parent->type);
216 p->parent->head = p;
217 break;
218 case (MAN_BODY):
219 assert(MAN_BLOCK == p->parent->type);
220 p->parent->body = p;
221 break;
222 default:
223 break;
224 }
225
226 man->last = p;
227
228 switch (p->type) {
229 case (MAN_TEXT):
230 if ( ! man_valid_post(man))
231 return(0);
232 if ( ! man_action_post(man))
233 return(0);
234 break;
235 default:
236 break;
237 }
238
239 return(1);
240 }
241
242
243 static struct man_node *
244 man_node_alloc(int line, int pos, enum man_type type, int tok)
245 {
246 struct man_node *p;
247
248 p = mandoc_calloc(1, sizeof(struct man_node));
249 p->line = line;
250 p->pos = pos;
251 p->type = type;
252 p->tok = tok;
253 return(p);
254 }
255
256
257 int
258 man_elem_alloc(struct man *m, int line, int pos, int tok)
259 {
260 struct man_node *p;
261
262 p = man_node_alloc(line, pos, MAN_ELEM, tok);
263 if ( ! man_node_append(m, p))
264 return(0);
265 m->next = MAN_NEXT_CHILD;
266 return(1);
267 }
268
269
270 int
271 man_head_alloc(struct man *m, int line, int pos, int tok)
272 {
273 struct man_node *p;
274
275 p = man_node_alloc(line, pos, MAN_HEAD, tok);
276 if ( ! man_node_append(m, p))
277 return(0);
278 m->next = MAN_NEXT_CHILD;
279 return(1);
280 }
281
282
283 int
284 man_body_alloc(struct man *m, int line, int pos, int tok)
285 {
286 struct man_node *p;
287
288 p = man_node_alloc(line, pos, MAN_BODY, tok);
289 if ( ! man_node_append(m, p))
290 return(0);
291 m->next = MAN_NEXT_CHILD;
292 return(1);
293 }
294
295
296 int
297 man_block_alloc(struct man *m, int line, int pos, int tok)
298 {
299 struct man_node *p;
300
301 p = man_node_alloc(line, pos, MAN_BLOCK, tok);
302 if ( ! man_node_append(m, p))
303 return(0);
304 m->next = MAN_NEXT_CHILD;
305 return(1);
306 }
307
308
309 static int
310 pstring(struct man *m, int line, int pos,
311 const char *p, size_t len)
312 {
313 struct man_node *n;
314 size_t sv;
315
316 n = man_node_alloc(line, pos, MAN_TEXT, -1);
317 n->string = mandoc_malloc(len + 1);
318 sv = strlcpy(n->string, p, len + 1);
319
320 /* Prohibit truncation. */
321 assert(sv < len + 1);
322
323 if ( ! man_node_append(m, n))
324 return(0);
325 m->next = MAN_NEXT_SIBLING;
326 return(1);
327 }
328
329
330 int
331 man_word_alloc(struct man *m, int line, int pos, const char *word)
332 {
333
334 return(pstring(m, line, pos, word, strlen(word)));
335 }
336
337
338 void
339 man_node_free(struct man_node *p)
340 {
341
342 if (p->string)
343 free(p->string);
344 if (p->parent)
345 p->parent->nchild--;
346 free(p);
347 }
348
349
350 void
351 man_node_freelist(struct man_node *p)
352 {
353 struct man_node *n;
354
355 if (p->child)
356 man_node_freelist(p->child);
357 assert(0 == p->nchild);
358 n = p->next;
359 man_node_free(p);
360 if (n)
361 man_node_freelist(n);
362 }
363
364
365 static int
366 man_ptext(struct man *m, int line, char *buf)
367 {
368 int i, j;
369 char sv;
370
371 /* Literal free-form text whitespace is preserved. */
372
373 if (MAN_LITERAL & m->flags) {
374 if ( ! man_word_alloc(m, line, 0, buf))
375 return(0);
376 goto descope;
377 }
378
379 /* First de-chunk and allocate words. */
380
381 for (i = 0; ' ' == buf[i]; i++)
382 /* Skip leading whitespace. */ ;
383
384 if ('\0' == buf[i]) {
385 /* Trailing whitespace? */
386 if (i && ' ' == buf[i - 1])
387 if ( ! man_pwarn(m, line, i - 1, WTSPACE))
388 return(0);
389 if ( ! pstring(m, line, 0, &buf[i], 0))
390 return(0);
391 goto descope;
392 }
393
394 for (j = i; buf[i]; i++) {
395 if (' ' != buf[i])
396 continue;
397
398 /* Escaped whitespace. */
399 if (i && ' ' == buf[i] && '\\' == buf[i - 1])
400 continue;
401
402 sv = buf[i];
403 buf[i++] = '\0';
404
405 if ( ! pstring(m, line, j, &buf[j], (size_t)(i - j)))
406 return(0);
407
408 /* Trailing whitespace? Check at overwritten byte. */
409
410 if (' ' == sv && '\0' == buf[i])
411 if ( ! man_pwarn(m, line, i - 1, WTSPACE))
412 return(0);
413
414 for ( ; ' ' == buf[i]; i++)
415 /* Skip trailing whitespace. */ ;
416
417 j = i;
418
419 /* Trailing whitespace? */
420
421 if (' ' == buf[i - 1] && '\0' == buf[i])
422 if ( ! man_pwarn(m, line, i - 1, WTSPACE))
423 return(0);
424
425 if ('\0' == buf[i])
426 break;
427 }
428
429 if (j != i && ! pstring(m, line, j, &buf[j], (size_t)(i - j)))
430 return(0);
431
432 descope:
433
434 /*
435 * Co-ordinate what happens with having a next-line scope open:
436 * first close out the element scope (if applicable), then close
437 * out the block scope (also if applicable).
438 */
439
440 if (MAN_ELINE & m->flags) {
441 m->flags &= ~MAN_ELINE;
442 if ( ! man_unscope(m, m->last->parent))
443 return(0);
444 }
445
446 if ( ! (MAN_BLINE & m->flags))
447 return(1);
448 m->flags &= ~MAN_BLINE;
449
450 if ( ! man_unscope(m, m->last->parent))
451 return(0);
452 return(man_body_alloc(m, line, 0, m->last->tok));
453 }
454
455
456 static int
457 macrowarn(struct man *m, int ln, const char *buf)
458 {
459 if ( ! (MAN_IGN_MACRO & m->pflags))
460 return(man_verr(m, ln, 0,
461 "unknown macro: %s%s",
462 buf, strlen(buf) > 3 ? "..." : ""));
463 return(man_vwarn(m, ln, 0, "unknown macro: %s%s",
464 buf, strlen(buf) > 3 ? "..." : ""));
465 }
466
467
468 int
469 man_pmacro(struct man *m, int ln, char *buf)
470 {
471 int i, j, c, ppos, fl;
472 char mac[5];
473 struct man_node *n;
474
475 /* Comments and empties are quickly ignored. */
476
477 fl = m->flags;
478
479 if ('\0' == buf[1])
480 return(1);
481
482 i = 1;
483
484 if (' ' == buf[i]) {
485 i++;
486 while (buf[i] && ' ' == buf[i])
487 i++;
488 if ('\0' == buf[i])
489 goto out;
490 }
491
492 ppos = i;
493
494 /* Copy the first word into a nil-terminated buffer. */
495
496 for (j = 0; j < 4; j++, i++) {
497 if ('\0' == (mac[j] = buf[i]))
498 break;
499 else if (' ' == buf[i])
500 break;
501
502 /* Check for invalid characters. */
503
504 if (isgraph((u_char)buf[i]))
505 continue;
506 return(man_perr(m, ln, i, WNPRINT));
507 }
508
509 mac[j] = '\0';
510
511 if (j == 4 || j < 1) {
512 if ( ! (MAN_IGN_MACRO & m->pflags)) {
513 (void)man_perr(m, ln, ppos, WMACROFORM);
514 goto err;
515 }
516 if ( ! man_pwarn(m, ln, ppos, WMACROFORM))
517 goto err;
518 return(1);
519 }
520
521 if (MAN_MAX == (c = man_hash_find(mac))) {
522 if ( ! macrowarn(m, ln, mac))
523 goto err;
524 return(1);
525 }
526
527 /* The macro is sane. Jump to the next word. */
528
529 while (buf[i] && ' ' == buf[i])
530 i++;
531
532 /* Trailing whitespace? */
533
534 if ('\0' == buf[i] && ' ' == buf[i - 1])
535 if ( ! man_pwarn(m, ln, i - 1, WTSPACE))
536 goto err;
537
538 /*
539 * Remove prior ELINE macro, as it's being clobbering by a new
540 * macro. Note that NSCOPED macros do not close out ELINE
541 * macros---they don't print text---so we let those slip by.
542 */
543
544 if ( ! (MAN_NSCOPED & man_macros[c].flags) &&
545 m->flags & MAN_ELINE) {
546 assert(MAN_TEXT != m->last->type);
547
548 /*
549 * This occurs in the following construction:
550 * .B
551 * .br
552 * .B
553 * .br
554 * I hate man macros.
555 * Flat-out disallow this madness.
556 */
557 if (MAN_NSCOPED & man_macros[m->last->tok].flags)
558 return(man_perr(m, ln, ppos, WLNSCOPE));
559
560 n = m->last;
561
562 assert(n);
563 assert(NULL == n->child);
564 assert(0 == n->nchild);
565
566 if ( ! man_nwarn(m, n, WLNSCOPE))
567 return(0);
568
569 man_node_unlink(m, n);
570 man_node_free(n);
571 m->flags &= ~MAN_ELINE;
572 }
573
574 /* Begin recursive parse sequence. */
575
576 assert(man_macros[c].fp);
577
578 if ( ! (*man_macros[c].fp)(m, c, ln, ppos, &i, buf))
579 goto err;
580
581 out:
582 /*
583 * We weren't in a block-line scope when entering the
584 * above-parsed macro, so return.
585 */
586
587 if ( ! (MAN_BLINE & fl)) {
588 m->flags &= ~MAN_ILINE;
589 return(1);
590 }
591
592 /*
593 * If we're in a block scope, then allow this macro to slip by
594 * without closing scope around it.
595 */
596
597 if (MAN_ILINE & m->flags) {
598 m->flags &= ~MAN_ILINE;
599 return(1);
600 }
601
602 /*
603 * If we've opened a new next-line element scope, then return
604 * now, as the next line will close out the block scope.
605 */
606
607 if (MAN_ELINE & m->flags)
608 return(1);
609
610 /* Close out the block scope opened in the prior line. */
611
612 assert(MAN_BLINE & m->flags);
613 m->flags &= ~MAN_BLINE;
614
615 if ( ! man_unscope(m, m->last->parent))
616 return(0);
617 return(man_body_alloc(m, ln, 0, m->last->tok));
618
619 err: /* Error out. */
620
621 m->flags |= MAN_HALT;
622 return(0);
623 }
624
625
626 int
627 man_verr(struct man *man, int ln, int pos, const char *fmt, ...)
628 {
629 char buf[256];
630 va_list ap;
631
632 if (NULL == man->cb.man_err)
633 return(0);
634
635 va_start(ap, fmt);
636 (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
637 va_end(ap);
638 return((*man->cb.man_err)(man->data, ln, pos, buf));
639 }
640
641
642 int
643 man_vwarn(struct man *man, int ln, int pos, const char *fmt, ...)
644 {
645 char buf[256];
646 va_list ap;
647
648 if (NULL == man->cb.man_warn)
649 return(0);
650
651 va_start(ap, fmt);
652 (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
653 va_end(ap);
654 return((*man->cb.man_warn)(man->data, ln, pos, buf));
655 }
656
657
658 int
659 man_err(struct man *m, int line, int pos, int iserr, enum merr type)
660 {
661 const char *p;
662
663 p = __man_merrnames[(int)type];
664 assert(p);
665
666 if (iserr)
667 return(man_verr(m, line, pos, p));
668
669 return(man_vwarn(m, line, pos, p));
670 }
671
672
673 void
674 man_node_unlink(struct man *m, struct man_node *n)
675 {
676
677 if (n->prev) {
678 n->prev->next = n->next;
679 if (m->last == n) {
680 assert(NULL == n->next);
681 m->last = n->prev;
682 m->next = MAN_NEXT_SIBLING;
683 }
684 } else {
685 n->parent->child = n->next;
686 if (m->last == n) {
687 assert(NULL == n->next);
688 m->last = n->parent;
689 m->next = MAN_NEXT_CHILD;
690 }
691 }
692
693 if (n->next)
694 n->next->prev = n->prev;
695 }