Restrict -Werror to gcc41.
[dragonfly.git] / usr.bin / mandoc / man.c
blob84fb72431a454a4c28592f9bbe8af9558cab2e83
1 /* $Id: man.c,v 1.15 2009/10/27 21:40:07 schwarze Exp $ */
2 /*
3 * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
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.
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.
17 #include <sys/types.h>
19 #include <assert.h>
20 #include <ctype.h>
21 #include <stdarg.h>
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
26 #include "libman.h"
28 const char *const __man_merrnames[WERRMAX] = {
29 "invalid character", /* WNPRINT */
30 "system: malloc error", /* WNMEM */
31 "invalid manual section", /* WMSEC */
32 "invalid date format", /* WDATE */
33 "scope of prior line violated", /* WLNSCOPE */
34 "trailing whitespace", /* WTSPACE */
35 "unterminated quoted parameter", /* WTQUOTE */
36 "document has no body", /* WNODATA */
37 "document has no title/section", /* WNOTITLE */
38 "invalid escape sequence", /* WESCAPE */
39 "invalid number format", /* WNUMFMT */
40 "expected block head arguments", /* WHEADARGS */
41 "expected block body arguments", /* WBODYARGS */
42 "expected empty block head", /* WNHEADARGS */
43 "ill-formed macro", /* WMACROFORM */
44 "scope open on exit", /* WEXITSCOPE */
45 "no scope context", /* WNOSCOPE */
46 "literal context already open", /* WOLITERAL */
47 "no literal context open" /* WNLITERAL */
50 const char *const __man_macronames[MAN_MAX] = {
51 "br", "TH", "SH", "SS",
52 "TP", "LP", "PP", "P",
53 "IP", "HP", "SM", "SB",
54 "BI", "IB", "BR", "RB",
55 "R", "B", "I", "IR",
56 "RI", "na", "i", "sp",
57 "nf", "fi", "r", "RE",
58 "RS", "DT", "UC", "PD"
61 const char * const *man_macronames = __man_macronames;
63 static struct man_node *man_node_alloc(int, int,
64 enum man_type, int);
65 static int man_node_append(struct man *,
66 struct man_node *);
67 static int man_ptext(struct man *, int, char *);
68 static int man_pmacro(struct man *, int, char *);
69 static void man_free1(struct man *);
70 static int man_alloc1(struct man *);
71 static int pstring(struct man *, int, int,
72 const char *, size_t);
73 static int macrowarn(struct man *, int, const char *);
76 const struct man_node *
77 man_node(const struct man *m)
80 return(MAN_HALT & m->flags ? NULL : m->first);
84 const struct man_meta *
85 man_meta(const struct man *m)
88 return(MAN_HALT & m->flags ? NULL : &m->meta);
92 int
93 man_reset(struct man *man)
96 man_free1(man);
97 return(man_alloc1(man));
101 void
102 man_free(struct man *man)
105 man_free1(man);
106 free(man);
110 struct man *
111 man_alloc(void *data, int pflags, const struct man_cb *cb)
113 struct man *p;
115 if (NULL == (p = calloc(1, sizeof(struct man))))
116 return(NULL);
118 if ( ! man_alloc1(p)) {
119 free(p);
120 return(NULL);
123 man_hash_init();
125 p->data = data;
126 p->pflags = pflags;
127 (void)memcpy(&p->cb, cb, sizeof(struct man_cb));
128 return(p);
133 man_endparse(struct man *m)
136 if (MAN_HALT & m->flags)
137 return(0);
138 else if (man_macroend(m))
139 return(1);
140 m->flags |= MAN_HALT;
141 return(0);
146 man_parseln(struct man *m, int ln, char *buf)
149 return('.' == *buf ?
150 man_pmacro(m, ln, buf) :
151 man_ptext(m, ln, buf));
155 static void
156 man_free1(struct man *man)
159 if (man->first)
160 man_node_freelist(man->first);
161 if (man->meta.title)
162 free(man->meta.title);
163 if (man->meta.source)
164 free(man->meta.source);
165 if (man->meta.vol)
166 free(man->meta.vol);
170 static int
171 man_alloc1(struct man *m)
174 bzero(&m->meta, sizeof(struct man_meta));
175 m->flags = 0;
176 m->last = calloc(1, sizeof(struct man_node));
177 if (NULL == m->last)
178 return(0);
179 m->first = m->last;
180 m->last->type = MAN_ROOT;
181 m->next = MAN_NEXT_CHILD;
182 return(1);
186 static int
187 man_node_append(struct man *man, struct man_node *p)
190 assert(man->last);
191 assert(man->first);
192 assert(MAN_ROOT != p->type);
194 switch (man->next) {
195 case (MAN_NEXT_SIBLING):
196 man->last->next = p;
197 p->prev = man->last;
198 p->parent = man->last->parent;
199 break;
200 case (MAN_NEXT_CHILD):
201 man->last->child = p;
202 p->parent = man->last;
203 break;
204 default:
205 abort();
206 /* NOTREACHED */
209 p->parent->nchild++;
211 if ( ! man_valid_pre(man, p))
212 return(0);
214 switch (p->type) {
215 case (MAN_HEAD):
216 assert(MAN_BLOCK == p->parent->type);
217 p->parent->head = p;
218 break;
219 case (MAN_BODY):
220 assert(MAN_BLOCK == p->parent->type);
221 p->parent->body = p;
222 break;
223 default:
224 break;
227 man->last = p;
229 switch (p->type) {
230 case (MAN_TEXT):
231 if ( ! man_valid_post(man))
232 return(0);
233 if ( ! man_action_post(man))
234 return(0);
235 break;
236 default:
237 break;
240 return(1);
244 static struct man_node *
245 man_node_alloc(int line, int pos, enum man_type type, int tok)
247 struct man_node *p;
249 p = calloc(1, sizeof(struct man_node));
250 if (NULL == p)
251 return(NULL);
253 p->line = line;
254 p->pos = pos;
255 p->type = type;
256 p->tok = tok;
257 return(p);
262 man_elem_alloc(struct man *m, int line, int pos, int tok)
264 struct man_node *p;
266 p = man_node_alloc(line, pos, MAN_ELEM, tok);
267 if (NULL == p)
268 return(0);
269 if ( ! man_node_append(m, p))
270 return(0);
271 m->next = MAN_NEXT_CHILD;
272 return(1);
277 man_head_alloc(struct man *m, int line, int pos, int tok)
279 struct man_node *p;
281 p = man_node_alloc(line, pos, MAN_HEAD, tok);
282 if (NULL == p)
283 return(0);
284 if ( ! man_node_append(m, p))
285 return(0);
286 m->next = MAN_NEXT_CHILD;
287 return(1);
292 man_body_alloc(struct man *m, int line, int pos, int tok)
294 struct man_node *p;
296 p = man_node_alloc(line, pos, MAN_BODY, tok);
297 if (NULL == p)
298 return(0);
299 if ( ! man_node_append(m, p))
300 return(0);
301 m->next = MAN_NEXT_CHILD;
302 return(1);
307 man_block_alloc(struct man *m, int line, int pos, int tok)
309 struct man_node *p;
311 p = man_node_alloc(line, pos, MAN_BLOCK, tok);
312 if (NULL == p)
313 return(0);
314 if ( ! man_node_append(m, p))
315 return(0);
316 m->next = MAN_NEXT_CHILD;
317 return(1);
321 static int
322 pstring(struct man *m, int line, int pos,
323 const char *p, size_t len)
325 struct man_node *n;
326 size_t sv;
328 n = man_node_alloc(line, pos, MAN_TEXT, -1);
329 if (NULL == n)
330 return(0);
332 n->string = malloc(len + 1);
333 if (NULL == n->string) {
334 free(n);
335 return(0);
338 sv = strlcpy(n->string, p, len + 1);
340 /* Prohibit truncation. */
341 assert(sv < len + 1);
343 if ( ! man_node_append(m, n))
344 return(0);
345 m->next = MAN_NEXT_SIBLING;
346 return(1);
351 man_word_alloc(struct man *m, int line, int pos, const char *word)
354 return(pstring(m, line, pos, word, strlen(word)));
358 void
359 man_node_free(struct man_node *p)
362 if (p->string)
363 free(p->string);
364 if (p->parent)
365 p->parent->nchild--;
366 free(p);
370 void
371 man_node_freelist(struct man_node *p)
373 struct man_node *n;
375 if (p->child)
376 man_node_freelist(p->child);
377 assert(0 == p->nchild);
378 n = p->next;
379 man_node_free(p);
380 if (n)
381 man_node_freelist(n);
385 static int
386 man_ptext(struct man *m, int line, char *buf)
388 int i, j;
390 /* Literal free-form text whitespace is preserved. */
392 if (MAN_LITERAL & m->flags) {
393 if ( ! man_word_alloc(m, line, 0, buf))
394 return(0);
395 goto descope;
398 /* First de-chunk and allocate words. */
400 for (i = 0; ' ' == buf[i]; i++)
401 /* Skip leading whitespace. */ ;
402 if (0 == buf[i]) {
403 if ( ! pstring(m, line, 0, &buf[i], 0))
404 return(0);
405 goto descope;
408 for (j = i; buf[i]; i++) {
409 if (' ' != buf[i])
410 continue;
412 /* Escaped whitespace. */
413 if (i && ' ' == buf[i] && '\\' == buf[i - 1])
414 continue;
416 buf[i++] = 0;
417 if ( ! pstring(m, line, j, &buf[j], (size_t)(i - j)))
418 return(0);
420 for ( ; ' ' == buf[i]; i++)
421 /* Skip trailing whitespace. */ ;
423 j = i;
424 if (0 == buf[i])
425 break;
428 if (j != i && ! pstring(m, line, j, &buf[j], (size_t)(i - j)))
429 return(0);
431 descope:
434 * Co-ordinate what happens with having a next-line scope open:
435 * first close out the element scope (if applicable), then close
436 * out the block scope (also if applicable).
439 if (MAN_ELINE & m->flags) {
440 m->flags &= ~MAN_ELINE;
441 if ( ! man_unscope(m, m->last->parent))
442 return(0);
445 if ( ! (MAN_BLINE & m->flags))
446 return(1);
447 m->flags &= ~MAN_BLINE;
449 if ( ! man_unscope(m, m->last->parent))
450 return(0);
451 return(man_body_alloc(m, line, 0, m->last->tok));
455 static int
456 macrowarn(struct man *m, int ln, const char *buf)
458 if ( ! (MAN_IGN_MACRO & m->pflags))
459 return(man_verr(m, ln, 0,
460 "unknown macro: %s%s",
461 buf, strlen(buf) > 3 ? "..." : ""));
462 return(man_vwarn(m, ln, 0, "unknown macro: %s%s",
463 buf, strlen(buf) > 3 ? "..." : ""));
468 man_pmacro(struct man *m, int ln, char *buf)
470 int i, j, c, ppos, fl;
471 char mac[5];
472 struct man_node *n;
474 /* Comments and empties are quickly ignored. */
476 fl = m->flags;
478 if (0 == buf[1])
479 goto out;
481 i = 1;
483 if (' ' == buf[i]) {
484 i++;
485 while (buf[i] && ' ' == buf[i])
486 i++;
487 if (0 == buf[i])
488 goto out;
491 ppos = i;
493 /* Copy the first word into a nil-terminated buffer. */
495 for (j = 0; j < 4; j++, i++) {
496 if (0 == (mac[j] = buf[i]))
497 break;
498 else if (' ' == buf[i])
499 break;
501 /* Check for invalid characters. */
503 if (isgraph((u_char)buf[i]))
504 continue;
505 return(man_perr(m, ln, i, WNPRINT));
508 mac[j] = 0;
510 if (j == 4 || j < 1) {
511 if ( ! (MAN_IGN_MACRO & m->pflags)) {
512 (void)man_perr(m, ln, ppos, WMACROFORM);
513 goto err;
515 if ( ! man_pwarn(m, ln, ppos, WMACROFORM))
516 goto err;
517 return(1);
520 if (MAN_MAX == (c = man_hash_find(mac))) {
521 if ( ! macrowarn(m, ln, mac))
522 goto err;
523 return(1);
526 /* The macro is sane. Jump to the next word. */
528 while (buf[i] && ' ' == buf[i])
529 i++;
531 /* Remove prior ELINE macro, if applicable. */
533 if (m->flags & MAN_ELINE) {
534 n = m->last;
535 assert(NULL == n->child);
536 assert(0 == n->nchild);
537 if ( ! man_nwarn(m, n, WLNSCOPE))
538 return(0);
540 if (n->prev) {
541 assert(n != n->parent->child);
542 assert(n == n->prev->next);
543 n->prev->next = NULL;
544 m->last = n->prev;
545 m->next = MAN_NEXT_SIBLING;
546 } else {
547 assert(n == n->parent->child);
548 n->parent->child = NULL;
549 m->last = n->parent;
550 m->next = MAN_NEXT_CHILD;
553 man_node_free(n);
554 m->flags &= ~MAN_ELINE;
557 /* Begin recursive parse sequence. */
559 assert(man_macros[c].fp);
561 if ( ! (*man_macros[c].fp)(m, c, ln, ppos, &i, buf))
562 goto err;
564 out:
565 if ( ! (MAN_BLINE & fl))
566 return(1);
569 * If we've opened a new next-line element scope, then return
570 * now, as the next line will close out the block scope.
573 if (MAN_ELINE & m->flags)
574 return(1);
576 /* Close out the block scope opened in the prior line. */
578 assert(MAN_BLINE & m->flags);
579 m->flags &= ~MAN_BLINE;
581 if ( ! man_unscope(m, m->last->parent))
582 return(0);
583 return(man_body_alloc(m, ln, 0, m->last->tok));
585 err: /* Error out. */
587 m->flags |= MAN_HALT;
588 return(0);
593 man_verr(struct man *man, int ln, int pos, const char *fmt, ...)
595 char buf[256];
596 va_list ap;
598 if (NULL == man->cb.man_err)
599 return(0);
601 va_start(ap, fmt);
602 (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
603 va_end(ap);
604 return((*man->cb.man_err)(man->data, ln, pos, buf));
609 man_vwarn(struct man *man, int ln, int pos, const char *fmt, ...)
611 char buf[256];
612 va_list ap;
614 if (NULL == man->cb.man_warn)
615 return(0);
617 va_start(ap, fmt);
618 (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
619 va_end(ap);
620 return((*man->cb.man_warn)(man->data, ln, pos, buf));
625 man_err(struct man *m, int line, int pos, int iserr, enum merr type)
627 const char *p;
629 p = __man_merrnames[(int)type];
630 assert(p);
632 if (iserr)
633 return(man_verr(m, line, pos, p));
635 return(man_vwarn(m, line, pos, p));