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