Annotation of mandoc/man.c, Revision 1.34
1.34 ! kristaps 1: /* $Id: man.c,v 1.33 2009/08/20 11:51:07 kristaps Exp $ */
1.1 kristaps 2: /*
1.19 kristaps 3: * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
1.1 kristaps 4: *
5: * Permission to use, copy, modify, and distribute this software for any
1.18 kristaps 6: * purpose with or without fee is hereby granted, provided that the above
7: * copyright notice and this permission notice appear in all copies.
1.1 kristaps 8: *
1.18 kristaps 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.
1.1 kristaps 16: */
17: #include <assert.h>
18: #include <ctype.h>
19: #include <stdarg.h>
20: #include <stdlib.h>
21: #include <stdio.h>
22: #include <string.h>
23:
24: #include "libman.h"
25:
1.27 kristaps 26: const char *const __man_merrnames[WERRMAX] = {
27: "invalid character", /* WNPRINT */
28: "system: malloc error", /* WNMEM */
29: "invalid manual section", /* WMSEC */
30: "invalid date format", /* WDATE */
31: "scope of prior line violated", /* WLNSCOPE */
32: "trailing whitespace", /* WTSPACE */
33: "unterminated quoted parameter", /* WTQUOTE */
34: "document has no body", /* WNODATA */
35: "document has no title/section", /* WNOTITLE */
36: "invalid escape sequence", /* WESCAPE */
1.28 kristaps 37: "invalid number format", /* WNUMFMT */
1.29 kristaps 38: "expected block head arguments", /* WHEADARGS */
39: "expected block body arguments", /* WBODYARGS */
40: "expected empty block head", /* WNHEADARGS */
41: "unknown macro", /* WMACRO */
42: "ill-formed macro", /* WMACROFORM */
1.30 kristaps 43: "scope open on exit", /* WEXITSCOPE */
44: "no scope context" /* WNOSCOPE */
1.27 kristaps 45: };
46:
1.1 kristaps 47: const char *const __man_macronames[MAN_MAX] = {
1.21 kristaps 48: "br", "TH", "SH", "SS",
1.1 kristaps 49: "TP", "LP", "PP", "P",
50: "IP", "HP", "SM", "SB",
51: "BI", "IB", "BR", "RB",
1.11 kristaps 52: "R", "B", "I", "IR",
1.29 kristaps 53: "RI", "na", "i", "sp",
1.30 kristaps 54: "nf", "fi", "r", "RE",
1.33 kristaps 55: "RS", "DT"
1.1 kristaps 56: };
57:
58: const char * const *man_macronames = __man_macronames;
59:
1.16 kristaps 60: static struct man_node *man_node_alloc(int, int,
61: enum man_type, int);
1.1 kristaps 62: static int man_node_append(struct man *,
63: struct man_node *);
64: static int man_ptext(struct man *, int, char *);
65: static int man_pmacro(struct man *, int, char *);
1.2 kristaps 66: static void man_free1(struct man *);
1.16 kristaps 67: static int man_alloc1(struct man *);
1.31 kristaps 68: static int pstring(struct man *, int, int,
69: const char *, size_t);
1.1 kristaps 70:
1.32 kristaps 71: #ifdef __linux__
72: extern size_t strlcpy(char *, const char *, size_t);
73: #endif
74:
1.1 kristaps 75:
76: const struct man_node *
1.2 kristaps 77: man_node(const struct man *m)
1.1 kristaps 78: {
79:
1.2 kristaps 80: return(MAN_HALT & m->flags ? NULL : m->first);
1.1 kristaps 81: }
82:
83:
84: const struct man_meta *
1.2 kristaps 85: man_meta(const struct man *m)
1.1 kristaps 86: {
87:
1.2 kristaps 88: return(MAN_HALT & m->flags ? NULL : &m->meta);
1.1 kristaps 89: }
90:
91:
1.15 kristaps 92: int
1.1 kristaps 93: man_reset(struct man *man)
94: {
95:
1.2 kristaps 96: man_free1(man);
1.16 kristaps 97: return(man_alloc1(man));
1.1 kristaps 98: }
99:
100:
101: void
102: man_free(struct man *man)
103: {
104:
1.2 kristaps 105: man_free1(man);
106:
1.1 kristaps 107: if (man->htab)
108: man_hash_free(man->htab);
109: free(man);
110: }
111:
112:
113: struct man *
1.7 kristaps 114: man_alloc(void *data, int pflags, const struct man_cb *cb)
1.1 kristaps 115: {
116: struct man *p;
117:
1.16 kristaps 118: if (NULL == (p = calloc(1, sizeof(struct man))))
119: return(NULL);
1.2 kristaps 120:
1.16 kristaps 121: if ( ! man_alloc1(p)) {
122: free(p);
123: return(NULL);
124: }
1.1 kristaps 125:
1.4 kristaps 126: p->data = data;
1.7 kristaps 127: p->pflags = pflags;
1.16 kristaps 128: (void)memcpy(&p->cb, cb, sizeof(struct man_cb));
1.7 kristaps 129:
1.16 kristaps 130: if (NULL == (p->htab = man_hash_alloc())) {
131: free(p);
132: return(NULL);
133: }
1.1 kristaps 134: return(p);
135: }
136:
137:
138: int
139: man_endparse(struct man *m)
140: {
141:
1.3 kristaps 142: if (MAN_HALT & m->flags)
143: return(0);
144: else if (man_macroend(m))
145: return(1);
146: m->flags |= MAN_HALT;
147: return(0);
1.1 kristaps 148: }
149:
150:
151: int
152: man_parseln(struct man *m, int ln, char *buf)
153: {
154:
155: return('.' == *buf ?
156: man_pmacro(m, ln, buf) :
157: man_ptext(m, ln, buf));
158: }
159:
160:
1.2 kristaps 161: static void
162: man_free1(struct man *man)
163: {
164:
165: if (man->first)
166: man_node_freelist(man->first);
167: if (man->meta.title)
168: free(man->meta.title);
1.6 kristaps 169: if (man->meta.source)
170: free(man->meta.source);
1.2 kristaps 171: if (man->meta.vol)
172: free(man->meta.vol);
173: }
174:
175:
1.16 kristaps 176: static int
1.2 kristaps 177: man_alloc1(struct man *m)
178: {
179:
180: bzero(&m->meta, sizeof(struct man_meta));
181: m->flags = 0;
182: m->last = calloc(1, sizeof(struct man_node));
183: if (NULL == m->last)
1.16 kristaps 184: return(0);
1.2 kristaps 185: m->first = m->last;
186: m->last->type = MAN_ROOT;
187: m->next = MAN_NEXT_CHILD;
1.16 kristaps 188: return(1);
1.2 kristaps 189: }
190:
191:
1.1 kristaps 192: static int
193: man_node_append(struct man *man, struct man_node *p)
194: {
195:
196: assert(man->last);
197: assert(man->first);
198: assert(MAN_ROOT != p->type);
199:
200: switch (man->next) {
201: case (MAN_NEXT_SIBLING):
202: man->last->next = p;
203: p->prev = man->last;
204: p->parent = man->last->parent;
205: break;
206: case (MAN_NEXT_CHILD):
207: man->last->child = p;
208: p->parent = man->last;
209: break;
210: default:
211: abort();
212: /* NOTREACHED */
213: }
1.22 kristaps 214:
215: p->parent->nchild++;
1.1 kristaps 216:
1.29 kristaps 217: if ( ! man_valid_pre(man, p))
218: return(0);
219:
220: switch (p->type) {
221: case (MAN_HEAD):
222: assert(MAN_BLOCK == p->parent->type);
223: p->parent->head = p;
224: break;
225: case (MAN_BODY):
226: assert(MAN_BLOCK == p->parent->type);
227: p->parent->body = p;
228: break;
229: default:
230: break;
231: }
232:
1.2 kristaps 233: man->last = p;
234:
1.1 kristaps 235: switch (p->type) {
1.2 kristaps 236: case (MAN_TEXT):
237: if ( ! man_valid_post(man))
238: return(0);
239: if ( ! man_action_post(man))
240: return(0);
1.1 kristaps 241: break;
242: default:
243: break;
244: }
245:
246: return(1);
247: }
248:
249:
250: static struct man_node *
1.16 kristaps 251: man_node_alloc(int line, int pos, enum man_type type, int tok)
1.1 kristaps 252: {
253: struct man_node *p;
254:
1.16 kristaps 255: p = calloc(1, sizeof(struct man_node));
256: if (NULL == p)
257: return(NULL);
258:
1.1 kristaps 259: p->line = line;
260: p->pos = pos;
261: p->type = type;
1.16 kristaps 262: p->tok = tok;
1.1 kristaps 263: return(p);
264: }
265:
266:
267: int
1.30 kristaps 268: man_elem_alloc(struct man *m, int line, int pos, int tok)
1.1 kristaps 269: {
270: struct man_node *p;
271:
1.16 kristaps 272: p = man_node_alloc(line, pos, MAN_ELEM, tok);
273: if (NULL == p)
274: return(0);
1.30 kristaps 275: if ( ! man_node_append(m, p))
276: return(0);
277: m->next = MAN_NEXT_CHILD;
278: return(1);
1.1 kristaps 279: }
280:
281:
282: int
1.29 kristaps 283: man_head_alloc(struct man *m, int line, int pos, int tok)
284: {
285: struct man_node *p;
286:
287: p = man_node_alloc(line, pos, MAN_HEAD, tok);
288: if (NULL == p)
289: return(0);
290: if ( ! man_node_append(m, p))
291: return(0);
292: m->next = MAN_NEXT_CHILD;
293: return(1);
294: }
295:
296:
297: int
298: man_body_alloc(struct man *m, int line, int pos, int tok)
299: {
300: struct man_node *p;
301:
302: p = man_node_alloc(line, pos, MAN_BODY, tok);
303: if (NULL == p)
304: return(0);
305: if ( ! man_node_append(m, p))
306: return(0);
307: m->next = MAN_NEXT_CHILD;
308: return(1);
309: }
310:
311:
312: int
313: man_block_alloc(struct man *m, int line, int pos, int tok)
314: {
315: struct man_node *p;
316:
317: p = man_node_alloc(line, pos, MAN_BLOCK, tok);
318: if (NULL == p)
319: return(0);
320: if ( ! man_node_append(m, p))
321: return(0);
322: m->next = MAN_NEXT_CHILD;
323: return(1);
324: }
325:
326:
1.31 kristaps 327: static int
328: pstring(struct man *m, int line, int pos,
329: const char *p, size_t len)
1.1 kristaps 330: {
1.31 kristaps 331: struct man_node *n;
332: size_t sv;
1.1 kristaps 333:
1.31 kristaps 334: n = man_node_alloc(line, pos, MAN_TEXT, -1);
335: if (NULL == n)
1.16 kristaps 336: return(0);
1.31 kristaps 337:
338: n->string = malloc(len + 1);
339: if (NULL == n->string) {
340: free(n);
1.16 kristaps 341: return(0);
1.31 kristaps 342: }
343:
344: sv = strlcpy(n->string, p, len + 1);
345:
346: /* Prohibit truncation. */
347: assert(sv < len + 1);
348:
349: if ( ! man_node_append(m, n))
1.30 kristaps 350: return(0);
351: m->next = MAN_NEXT_SIBLING;
352: return(1);
1.1 kristaps 353: }
354:
355:
1.31 kristaps 356: int
357: man_word_alloc(struct man *m, int line, int pos, const char *word)
358: {
359:
360: return(pstring(m, line, pos, word, strlen(word)));
361: }
362:
363:
1.1 kristaps 364: void
365: man_node_free(struct man_node *p)
366: {
367:
368: if (p->string)
369: free(p->string);
1.24 kristaps 370: if (p->parent)
371: p->parent->nchild--;
1.1 kristaps 372: free(p);
373: }
374:
375:
376: void
377: man_node_freelist(struct man_node *p)
378: {
379:
380: if (p->child)
381: man_node_freelist(p->child);
382: if (p->next)
383: man_node_freelist(p->next);
384:
1.24 kristaps 385: assert(0 == p->nchild);
1.1 kristaps 386: man_node_free(p);
387: }
388:
389:
390: static int
391: man_ptext(struct man *m, int line, char *buf)
392: {
1.31 kristaps 393: int i, j;
394:
395: /* First de-chunk and allocate words. */
396:
397: for (i = 0; ' ' == buf[i]; i++)
398: /* Skip leading whitespace. */ ;
399: if (0 == buf[i]) {
400: if ( ! pstring(m, line, 0, &buf[i], 0))
401: return(0);
402: goto descope;
403: }
404:
405: for (j = i; buf[i]; i++) {
406: if (' ' != buf[i])
407: continue;
408:
409: /* Escaped whitespace. */
410: if (i && ' ' == buf[i] && '\\' == buf[i - 1])
411: continue;
1.1 kristaps 412:
1.31 kristaps 413: buf[i++] = 0;
414: if ( ! pstring(m, line, j, &buf[j], (size_t)(i - j)))
415: return(0);
1.29 kristaps 416:
1.31 kristaps 417: for ( ; ' ' == buf[i]; i++)
418: /* Skip trailing whitespace. */ ;
1.30 kristaps 419:
1.31 kristaps 420: j = i;
421: if (0 == buf[i])
422: break;
423: }
424:
425: if (j != i && ! pstring(m, line, j, &buf[j], (size_t)(i - j)))
1.1 kristaps 426: return(0);
1.31 kristaps 427:
428: descope:
1.11 kristaps 429:
430: /*
1.29 kristaps 431: * Co-ordinate what happens with having a next-line scope open:
432: * first close out the element scope (if applicable), then close
433: * out the block scope (also if applicable).
1.11 kristaps 434: */
435:
1.29 kristaps 436: /* XXX - this should be in man_action.c. */
437:
438: if (MAN_ELINE & m->flags) {
439: m->flags &= ~MAN_ELINE;
440: if ( ! man_unscope(m, m->last->parent))
441: return(0);
442: }
443:
444: if ( ! (MAN_BLINE & m->flags))
1.11 kristaps 445: return(1);
1.29 kristaps 446: m->flags &= ~MAN_BLINE;
1.11 kristaps 447:
1.29 kristaps 448: if ( ! man_unscope(m, m->last->parent))
1.11 kristaps 449: return(0);
1.29 kristaps 450: return(man_body_alloc(m, line, 0, m->last->tok));
1.1 kristaps 451: }
452:
453:
454: int
455: man_pmacro(struct man *m, int ln, char *buf)
456: {
1.34 ! kristaps 457: int i, j, c, ppos, fl;
! 458: char mac[5];
! 459: struct man_node *n;
1.1 kristaps 460:
461: /* Comments and empties are quickly ignored. */
462:
1.29 kristaps 463: fl = m->flags;
1.11 kristaps 464:
1.1 kristaps 465: if (0 == buf[1])
1.11 kristaps 466: goto out;
1.1 kristaps 467:
1.9 kristaps 468: i = 1;
469:
470: if (' ' == buf[i]) {
471: i++;
1.1 kristaps 472: while (buf[i] && ' ' == buf[i])
473: i++;
474: if (0 == buf[i])
1.11 kristaps 475: goto out;
1.1 kristaps 476: }
477:
1.10 kristaps 478: ppos = i;
479:
1.1 kristaps 480: /* Copy the first word into a nil-terminated buffer. */
481:
1.10 kristaps 482: for (j = 0; j < 4; j++, i++) {
483: if (0 == (mac[j] = buf[i]))
1.1 kristaps 484: break;
1.10 kristaps 485: else if (' ' == buf[i])
1.1 kristaps 486: break;
487: }
488:
1.9 kristaps 489: mac[j] = 0;
1.1 kristaps 490:
1.9 kristaps 491: if (j == 4 || j < 1) {
1.7 kristaps 492: if ( ! (MAN_IGN_MACRO & m->pflags)) {
1.29 kristaps 493: (void)man_perr(m, ln, ppos, WMACROFORM);
1.7 kristaps 494: goto err;
495: }
1.29 kristaps 496: if ( ! man_pwarn(m, ln, ppos, WMACROFORM))
1.7 kristaps 497: goto err;
1.12 kristaps 498: return(1);
1.7 kristaps 499: }
1.1 kristaps 500:
501: if (MAN_MAX == (c = man_hash_find(m->htab, mac))) {
1.7 kristaps 502: if ( ! (MAN_IGN_MACRO & m->pflags)) {
1.29 kristaps 503: (void)man_perr(m, ln, ppos, WMACRO);
1.7 kristaps 504: goto err;
505: }
1.29 kristaps 506: if ( ! man_pwarn(m, ln, ppos, WMACRO))
1.7 kristaps 507: goto err;
1.12 kristaps 508: return(1);
1.1 kristaps 509: }
510:
511: /* The macro is sane. Jump to the next word. */
512:
513: while (buf[i] && ' ' == buf[i])
514: i++;
515:
1.34 ! kristaps 516: /* Remove prior ELINE macro, if applicable. */
! 517:
! 518: if (m->flags & MAN_ELINE) {
! 519: n = m->last;
! 520: assert(NULL == n->child);
! 521: if ( ! man_nwarn(m, n, WLNSCOPE))
! 522: return(0);
! 523:
! 524: if (n->prev) {
! 525: assert(n != n->parent->child);
! 526: assert(n == n->prev->next);
! 527: n->prev->next = NULL;
! 528: m->last = n->prev;
! 529: } else {
! 530: assert(n == n->parent->child);
! 531: n->parent->child = NULL;
! 532: m->last = n->parent;
! 533: }
! 534:
! 535: man_node_free(n);
! 536: m->flags &= ~MAN_ELINE;
! 537: }
! 538:
1.1 kristaps 539: /* Begin recursive parse sequence. */
540:
1.29 kristaps 541: assert(man_macros[c].fp);
542:
543: if ( ! (*man_macros[c].fp)(m, c, ln, ppos, &i, buf))
1.1 kristaps 544: goto err;
545:
1.11 kristaps 546: out:
1.29 kristaps 547: if ( ! (MAN_BLINE & fl))
548: return(1);
549:
550: /*
551: * If we've opened a new next-line element scope, then return
552: * now, as the next line will close out the block scope.
553: */
554:
555: if (MAN_ELINE & m->flags)
556: return(1);
557:
558: /* Close out the block scope opened in the prior line. */
1.11 kristaps 559:
1.29 kristaps 560: assert(MAN_BLINE & m->flags);
561: m->flags &= ~MAN_BLINE;
1.11 kristaps 562:
1.29 kristaps 563: if ( ! man_unscope(m, m->last->parent))
564: return(0);
565: return(man_body_alloc(m, ln, 0, m->last->tok));
1.1 kristaps 566:
567: err: /* Error out. */
568:
1.2 kristaps 569: m->flags |= MAN_HALT;
1.1 kristaps 570: return(0);
571: }
1.3 kristaps 572:
1.4 kristaps 573:
574: int
575: man_verr(struct man *man, int ln, int pos, const char *fmt, ...)
576: {
577: char buf[256];
578: va_list ap;
579:
580: if (NULL == man->cb.man_err)
581: return(0);
582:
583: va_start(ap, fmt);
584: (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
585: va_end(ap);
586: return((*man->cb.man_err)(man->data, ln, pos, buf));
587: }
588:
589:
590: int
591: man_vwarn(struct man *man, int ln, int pos, const char *fmt, ...)
592: {
593: char buf[256];
594: va_list ap;
595:
596: if (NULL == man->cb.man_warn)
597: return(0);
598:
599: va_start(ap, fmt);
600: (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
601: va_end(ap);
602: return((*man->cb.man_warn)(man->data, ln, pos, buf));
603: }
604:
605:
1.23 kristaps 606: int
1.27 kristaps 607: man_err(struct man *m, int line, int pos, int iserr, enum merr type)
1.23 kristaps 608: {
609: const char *p;
610:
1.27 kristaps 611: p = __man_merrnames[(int)type];
1.23 kristaps 612: assert(p);
613:
614: if (iserr)
615: return(man_verr(m, line, pos, p));
616:
617: return(man_vwarn(m, line, pos, p));
618: }
CVSweb