Annotation of src/usr.bin/mandoc/mdoc.c, Revision 1.11
1.11 ! schwarze 1: /* $Id: mdoc.c,v 1.10 2009/06/23 23:02:54 schwarze Exp $ */
1.1 kristaps 2: /*
1.3 schwarze 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.3 schwarze 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.3 schwarze 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 <stdio.h>
21: #include <stdlib.h>
22: #include <string.h>
23:
24: #include "libmdoc.h"
25:
26: enum merr {
27: ENOCALL,
28: EBODYPROL,
29: EPROLBODY,
30: ESPACE,
31: ETEXTPROL,
32: ENOBLANK,
33: EMALLOC
34: };
35:
36: const char *const __mdoc_macronames[MDOC_MAX] = {
1.7 schwarze 37: "Ap", "Dd", "Dt", "Os",
1.1 kristaps 38: "Sh", "Ss", "Pp", "D1",
39: "Dl", "Bd", "Ed", "Bl",
40: "El", "It", "Ad", "An",
41: "Ar", "Cd", "Cm", "Dv",
42: "Er", "Ev", "Ex", "Fa",
43: "Fd", "Fl", "Fn", "Ft",
44: "Ic", "In", "Li", "Nd",
45: "Nm", "Op", "Ot", "Pa",
46: "Rv", "St", "Va", "Vt",
47: /* LINTED */
48: "Xr", "\%A", "\%B", "\%D",
49: /* LINTED */
50: "\%I", "\%J", "\%N", "\%O",
51: /* LINTED */
52: "\%P", "\%R", "\%T", "\%V",
53: "Ac", "Ao", "Aq", "At",
54: "Bc", "Bf", "Bo", "Bq",
55: "Bsx", "Bx", "Db", "Dc",
56: "Do", "Dq", "Ec", "Ef",
57: "Em", "Eo", "Fx", "Ms",
58: "No", "Ns", "Nx", "Ox",
59: "Pc", "Pf", "Po", "Pq",
60: "Qc", "Ql", "Qo", "Qq",
61: "Re", "Rs", "Sc", "So",
62: "Sq", "Sm", "Sx", "Sy",
63: "Tn", "Ux", "Xc", "Xo",
64: "Fo", "Fc", "Oo", "Oc",
65: "Bk", "Ek", "Bt", "Hf",
1.7 schwarze 66: "Fr", "Ud", "Lb", "Lp",
67: "Lk", "Mt", "Brq", "Bro",
1.1 kristaps 68: /* LINTED */
1.7 schwarze 69: "Brc", "\%C", "Es", "En",
1.1 kristaps 70: /* LINTED */
1.7 schwarze 71: "Dx", "\%Q"
1.1 kristaps 72: };
73:
74: const char *const __mdoc_argnames[MDOC_ARG_MAX] = {
75: "split", "nosplit", "ragged",
76: "unfilled", "literal", "file",
77: "offset", "bullet", "dash",
78: "hyphen", "item", "enum",
79: "tag", "diag", "hang",
80: "ohang", "inset", "column",
81: "width", "compact", "std",
82: "filled", "words", "emphasis",
83: "symbolic", "nested"
84: };
85:
86: const char * const *mdoc_macronames = __mdoc_macronames;
87: const char * const *mdoc_argnames = __mdoc_argnames;
88:
89: static void mdoc_free1(struct mdoc *);
90: static int mdoc_alloc1(struct mdoc *);
91: static struct mdoc_node *node_alloc(struct mdoc *, int, int,
92: int, enum mdoc_type);
93: static int node_append(struct mdoc *,
94: struct mdoc_node *);
95: static int parsetext(struct mdoc *, int, char *);
96: static int parsemacro(struct mdoc *, int, char *);
97: static int macrowarn(struct mdoc *, int, const char *);
98: static int perr(struct mdoc *, int, int, enum merr);
99:
100: const struct mdoc_node *
101: mdoc_node(const struct mdoc *m)
102: {
103:
104: return(MDOC_HALT & m->flags ? NULL : m->first);
105: }
106:
107:
108: const struct mdoc_meta *
109: mdoc_meta(const struct mdoc *m)
110: {
111:
112: return(MDOC_HALT & m->flags ? NULL : &m->meta);
113: }
114:
115:
1.8 schwarze 116: /*
117: * Frees volatile resources (parse tree, meta-data, fields).
118: */
1.1 kristaps 119: static void
120: mdoc_free1(struct mdoc *mdoc)
121: {
122:
123: if (mdoc->first)
124: mdoc_node_freelist(mdoc->first);
125: if (mdoc->meta.title)
126: free(mdoc->meta.title);
127: if (mdoc->meta.os)
128: free(mdoc->meta.os);
129: if (mdoc->meta.name)
130: free(mdoc->meta.name);
131: if (mdoc->meta.arch)
132: free(mdoc->meta.arch);
133: if (mdoc->meta.vol)
134: free(mdoc->meta.vol);
135: }
136:
137:
1.8 schwarze 138: /*
139: * Allocate all volatile resources (parse tree, meta-data, fields).
140: */
1.1 kristaps 141: static int
142: mdoc_alloc1(struct mdoc *mdoc)
143: {
144:
145: bzero(&mdoc->meta, sizeof(struct mdoc_meta));
146: mdoc->flags = 0;
1.9 schwarze 147: mdoc->lastnamed = mdoc->lastsec = SEC_NONE;
1.1 kristaps 148: mdoc->last = calloc(1, sizeof(struct mdoc_node));
149: if (NULL == mdoc->last)
150: return(0);
151:
152: mdoc->first = mdoc->last;
153: mdoc->last->type = MDOC_ROOT;
154: mdoc->next = MDOC_NEXT_CHILD;
155: return(1);
156: }
157:
158:
159: /*
1.8 schwarze 160: * Free up volatile resources (see mdoc_free1()) then re-initialises the
161: * data with mdoc_alloc1(). After invocation, parse data has been reset
162: * and the parser is ready for re-invocation on a new tree; however,
163: * cross-parse non-volatile data is kept intact.
1.1 kristaps 164: */
165: int
166: mdoc_reset(struct mdoc *mdoc)
167: {
168:
169: mdoc_free1(mdoc);
170: return(mdoc_alloc1(mdoc));
171: }
172:
173:
174: /*
1.8 schwarze 175: * Completely free up all volatile and non-volatile parse resources.
176: * After invocation, the pointer is no longer usable.
1.1 kristaps 177: */
178: void
179: mdoc_free(struct mdoc *mdoc)
180: {
181:
182: mdoc_free1(mdoc);
183: if (mdoc->htab)
184: mdoc_hash_free(mdoc->htab);
185: free(mdoc);
186: }
187:
188:
1.8 schwarze 189: /*
190: * Allocate volatile and non-volatile parse resources.
191: */
1.1 kristaps 192: struct mdoc *
193: mdoc_alloc(void *data, int pflags, const struct mdoc_cb *cb)
194: {
195: struct mdoc *p;
196:
197: if (NULL == (p = calloc(1, sizeof(struct mdoc))))
198: return(NULL);
199: if (cb)
200: (void)memcpy(&p->cb, cb, sizeof(struct mdoc_cb));
201:
202: p->data = data;
203: p->pflags = pflags;
204:
205: if (NULL == (p->htab = mdoc_hash_alloc())) {
206: free(p);
207: return(NULL);
208: } else if (mdoc_alloc1(p))
209: return(p);
210:
211: free(p);
212: return(NULL);
213: }
214:
215:
216: /*
217: * Climb back up the parse tree, validating open scopes. Mostly calls
1.8 schwarze 218: * through to macro_end() in macro.c.
1.1 kristaps 219: */
220: int
221: mdoc_endparse(struct mdoc *m)
222: {
223:
224: if (MDOC_HALT & m->flags)
225: return(0);
226: else if (mdoc_macroend(m))
227: return(1);
228: m->flags |= MDOC_HALT;
229: return(0);
230: }
231:
232:
233: /*
234: * Main parse routine. Parses a single line -- really just hands off to
1.8 schwarze 235: * the macro (parsemacro()) or text parser (parsetext()).
1.1 kristaps 236: */
237: int
238: mdoc_parseln(struct mdoc *m, int ln, char *buf)
239: {
240:
241: if (MDOC_HALT & m->flags)
242: return(0);
243:
244: return('.' == *buf ? parsemacro(m, ln, buf) :
245: parsetext(m, ln, buf));
246: }
247:
248:
249: int
1.4 schwarze 250: mdoc_verr(struct mdoc *mdoc, int ln, int pos,
251: const char *fmt, ...)
1.1 kristaps 252: {
253: char buf[256];
254: va_list ap;
255:
256: if (NULL == mdoc->cb.mdoc_err)
257: return(0);
258:
259: va_start(ap, fmt);
260: (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
261: va_end(ap);
262: return((*mdoc->cb.mdoc_err)(mdoc->data, ln, pos, buf));
263: }
264:
265:
266: int
267: mdoc_vwarn(struct mdoc *mdoc, int ln, int pos,
268: enum mdoc_warn type, const char *fmt, ...)
269: {
270: char buf[256];
271: va_list ap;
272:
273: if (NULL == mdoc->cb.mdoc_warn)
274: return(0);
275:
276: va_start(ap, fmt);
277: (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
278: va_end(ap);
1.11 ! schwarze 279: return((*mdoc->cb.mdoc_warn)(mdoc->data, ln, pos, buf));
1.5 schwarze 280: }
281:
282:
283: int
1.8 schwarze 284: mdoc_nerr(struct mdoc *mdoc, const struct mdoc_node *node,
285: const char *fmt, ...)
1.5 schwarze 286: {
287: char buf[256];
288: va_list ap;
289:
290: if (NULL == mdoc->cb.mdoc_err)
291: return(0);
292:
293: va_start(ap, fmt);
294: (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
295: va_end(ap);
1.8 schwarze 296: return((*mdoc->cb.mdoc_err)(mdoc->data,
297: node->line, node->pos, buf));
1.5 schwarze 298: }
299:
300:
301: int
1.8 schwarze 302: mdoc_warn(struct mdoc *mdoc, enum mdoc_warn type,
303: const char *fmt, ...)
1.5 schwarze 304: {
305: char buf[256];
306: va_list ap;
307:
308: if (NULL == mdoc->cb.mdoc_warn)
309: return(0);
310:
311: va_start(ap, fmt);
312: (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
313: va_end(ap);
314: return((*mdoc->cb.mdoc_warn)(mdoc->data, mdoc->last->line,
1.11 ! schwarze 315: mdoc->last->pos, buf));
1.5 schwarze 316: }
317:
318:
319: int
320: mdoc_err(struct mdoc *mdoc, const char *fmt, ...)
321: {
322: char buf[256];
323: va_list ap;
324:
325: if (NULL == mdoc->cb.mdoc_err)
326: return(0);
327:
328: va_start(ap, fmt);
329: (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
330: va_end(ap);
331: return((*mdoc->cb.mdoc_err)(mdoc->data, mdoc->last->line,
1.8 schwarze 332: mdoc->last->pos, buf));
1.5 schwarze 333: }
334:
335:
336: int
337: mdoc_pwarn(struct mdoc *mdoc, int line, int pos, enum mdoc_warn type,
338: const char *fmt, ...)
339: {
340: char buf[256];
341: va_list ap;
342:
343: if (NULL == mdoc->cb.mdoc_warn)
344: return(0);
345:
346: va_start(ap, fmt);
347: (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
348: va_end(ap);
1.11 ! schwarze 349: return((*mdoc->cb.mdoc_warn)(mdoc->data, line, pos, buf));
1.5 schwarze 350: }
351:
352: int
353: mdoc_perr(struct mdoc *mdoc, int line, int pos, const char *fmt, ...)
354: {
355: char buf[256];
356: va_list ap;
357:
358: if (NULL == mdoc->cb.mdoc_err)
359: return(0);
360:
361: va_start(ap, fmt);
362: (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
363: va_end(ap);
364: return((*mdoc->cb.mdoc_err)(mdoc->data, line, pos, buf));
1.2 miod 365: }
366:
367:
368: int
1.1 kristaps 369: mdoc_macro(struct mdoc *m, int tok,
370: int ln, int pp, int *pos, char *buf)
371: {
372:
373: if (MDOC_PROLOGUE & mdoc_macros[tok].flags &&
1.9 schwarze 374: MDOC_PBODY & m->flags)
1.1 kristaps 375: return(perr(m, ln, pp, EPROLBODY));
376: if ( ! (MDOC_PROLOGUE & mdoc_macros[tok].flags) &&
1.9 schwarze 377: ! (MDOC_PBODY & m->flags))
1.1 kristaps 378: return(perr(m, ln, pp, EBODYPROL));
379:
380: if (1 != pp && ! (MDOC_CALLABLE & mdoc_macros[tok].flags))
381: return(perr(m, ln, pp, ENOCALL));
382:
383: return((*mdoc_macros[tok].fp)(m, tok, ln, pp, pos, buf));
384: }
385:
386:
387: static int
388: perr(struct mdoc *m, int line, int pos, enum merr type)
389: {
390: char *p;
391:
392: p = NULL;
393: switch (type) {
394: case (ENOCALL):
395: p = "not callable";
396: break;
397: case (EPROLBODY):
398: p = "macro disallowed in document body";
399: break;
400: case (EBODYPROL):
401: p = "macro disallowed in document prologue";
402: break;
403: case (EMALLOC):
404: p = "memory exhausted";
405: break;
406: case (ETEXTPROL):
407: p = "text disallowed in document prologue";
408: break;
409: case (ENOBLANK):
410: p = "blank lines disallowed in non-literal contexts";
411: break;
412: case (ESPACE):
413: p = "whitespace disallowed after delimiter";
414: break;
415: }
416: assert(p);
417: return(mdoc_perr(m, line, pos, p));
418: }
419:
420:
421: static int
422: node_append(struct mdoc *mdoc, struct mdoc_node *p)
423: {
424:
425: assert(mdoc->last);
426: assert(mdoc->first);
427: assert(MDOC_ROOT != p->type);
428:
429: switch (mdoc->next) {
430: case (MDOC_NEXT_SIBLING):
431: mdoc->last->next = p;
432: p->prev = mdoc->last;
433: p->parent = mdoc->last->parent;
434: break;
435: case (MDOC_NEXT_CHILD):
436: mdoc->last->child = p;
437: p->parent = mdoc->last;
438: break;
439: default:
440: abort();
441: /* NOTREACHED */
442: }
443:
1.10 schwarze 444: p->parent->nchild++;
445:
1.1 kristaps 446: if ( ! mdoc_valid_pre(mdoc, p))
447: return(0);
448: if ( ! mdoc_action_pre(mdoc, p))
449: return(0);
450:
451: switch (p->type) {
452: case (MDOC_HEAD):
453: assert(MDOC_BLOCK == p->parent->type);
454: p->parent->head = p;
455: break;
456: case (MDOC_TAIL):
457: assert(MDOC_BLOCK == p->parent->type);
458: p->parent->tail = p;
459: break;
460: case (MDOC_BODY):
461: assert(MDOC_BLOCK == p->parent->type);
462: p->parent->body = p;
463: break;
464: default:
465: break;
466: }
467:
468: mdoc->last = p;
469:
470: switch (p->type) {
471: case (MDOC_TEXT):
472: if ( ! mdoc_valid_post(mdoc))
473: return(0);
474: if ( ! mdoc_action_post(mdoc))
475: return(0);
476: break;
477: default:
478: break;
479: }
480:
481: return(1);
482: }
483:
484:
485: static struct mdoc_node *
486: node_alloc(struct mdoc *mdoc, int line,
487: int pos, int tok, enum mdoc_type type)
488: {
489: struct mdoc_node *p;
490:
491: if (NULL == (p = calloc(1, sizeof(struct mdoc_node)))) {
1.8 schwarze 492: (void)perr(mdoc, (mdoc)->last->line,
493: (mdoc)->last->pos, EMALLOC);
1.1 kristaps 494: return(NULL);
495: }
496:
497: p->sec = mdoc->lastsec;
498: p->line = line;
499: p->pos = pos;
500: p->tok = tok;
501: if (MDOC_TEXT != (p->type = type))
502: assert(p->tok >= 0);
503:
504: return(p);
505: }
506:
507:
508: int
509: mdoc_tail_alloc(struct mdoc *mdoc, int line, int pos, int tok)
510: {
511: struct mdoc_node *p;
512:
513: p = node_alloc(mdoc, line, pos, tok, MDOC_TAIL);
514: if (NULL == p)
515: return(0);
516: return(node_append(mdoc, p));
517: }
518:
519:
520: int
521: mdoc_head_alloc(struct mdoc *mdoc, int line, int pos, int tok)
522: {
523: struct mdoc_node *p;
524:
525: assert(mdoc->first);
526: assert(mdoc->last);
527:
528: p = node_alloc(mdoc, line, pos, tok, MDOC_HEAD);
529: if (NULL == p)
530: return(0);
531: return(node_append(mdoc, p));
532: }
533:
534:
535: int
536: mdoc_body_alloc(struct mdoc *mdoc, int line, int pos, int tok)
537: {
538: struct mdoc_node *p;
539:
540: p = node_alloc(mdoc, line, pos, tok, MDOC_BODY);
541: if (NULL == p)
542: return(0);
543: return(node_append(mdoc, p));
544: }
545:
546:
547: int
548: mdoc_block_alloc(struct mdoc *mdoc, int line, int pos,
549: int tok, struct mdoc_arg *args)
550: {
551: struct mdoc_node *p;
552:
553: p = node_alloc(mdoc, line, pos, tok, MDOC_BLOCK);
554: if (NULL == p)
555: return(0);
1.4 schwarze 556: p->args = args;
557: if (p->args)
1.1 kristaps 558: (args->refcnt)++;
559: return(node_append(mdoc, p));
560: }
561:
562:
563: int
564: mdoc_elem_alloc(struct mdoc *mdoc, int line, int pos,
565: int tok, struct mdoc_arg *args)
566: {
567: struct mdoc_node *p;
568:
569: p = node_alloc(mdoc, line, pos, tok, MDOC_ELEM);
570: if (NULL == p)
571: return(0);
1.4 schwarze 572: p->args = args;
573: if (p->args)
1.1 kristaps 574: (args->refcnt)++;
575: return(node_append(mdoc, p));
576: }
577:
578:
579: int
580: mdoc_word_alloc(struct mdoc *mdoc,
581: int line, int pos, const char *word)
582: {
583: struct mdoc_node *p;
584:
585: p = node_alloc(mdoc, line, pos, -1, MDOC_TEXT);
586: if (NULL == p)
587: return(0);
588: if (NULL == (p->string = strdup(word))) {
1.8 schwarze 589: (void)perr(mdoc, (mdoc)->last->line,
590: (mdoc)->last->pos, EMALLOC);
1.1 kristaps 591: return(0);
592: }
593: return(node_append(mdoc, p));
594: }
595:
596:
597: void
598: mdoc_node_free(struct mdoc_node *p)
599: {
600:
1.10 schwarze 601: if (p->parent)
602: p->parent->nchild--;
1.1 kristaps 603: if (p->string)
604: free(p->string);
605: if (p->args)
606: mdoc_argv_free(p->args);
607: free(p);
608: }
609:
610:
611: void
612: mdoc_node_freelist(struct mdoc_node *p)
613: {
614:
615: if (p->child)
616: mdoc_node_freelist(p->child);
617: if (p->next)
618: mdoc_node_freelist(p->next);
619:
1.10 schwarze 620: assert(0 == p->nchild);
1.1 kristaps 621: mdoc_node_free(p);
622: }
623:
624:
625: /*
626: * Parse free-form text, that is, a line that does not begin with the
627: * control character.
628: */
629: static int
630: parsetext(struct mdoc *m, int line, char *buf)
631: {
632:
1.9 schwarze 633: if (SEC_NONE == m->lastnamed)
1.1 kristaps 634: return(perr(m, line, 0, ETEXTPROL));
635:
636: if (0 == buf[0] && ! (MDOC_LITERAL & m->flags))
637: return(perr(m, line, 0, ENOBLANK));
638:
639: if ( ! mdoc_word_alloc(m, line, 0, buf))
640: return(0);
641:
642: m->next = MDOC_NEXT_SIBLING;
643: return(1);
644: }
645:
646:
647: static int
648: macrowarn(struct mdoc *m, int ln, const char *buf)
649: {
650: if ( ! (MDOC_IGN_MACRO & m->pflags))
651: return(mdoc_perr(m, ln, 1,
652: "unknown macro: %s%s",
653: buf, strlen(buf) > 3 ? "..." : ""));
654: return(mdoc_pwarn(m, ln, 1, WARN_SYNTAX,
655: "unknown macro: %s%s",
656: buf, strlen(buf) > 3 ? "..." : ""));
657: }
658:
659:
660: /*
661: * Parse a macro line, that is, a line beginning with the control
662: * character.
663: */
664: int
665: parsemacro(struct mdoc *m, int ln, char *buf)
666: {
667: int i, c;
668: char mac[5];
669:
1.7 schwarze 670: /* Empty lines are ignored. */
1.1 kristaps 671:
672: if (0 == buf[1])
673: return(1);
674:
675: if (' ' == buf[1]) {
676: i = 2;
677: while (buf[i] && ' ' == buf[i])
678: i++;
679: if (0 == buf[i])
680: return(1);
681: return(perr(m, ln, 1, ESPACE));
682: }
683:
684: /* Copy the first word into a nil-terminated buffer. */
685:
686: for (i = 1; i < 5; i++) {
687: if (0 == (mac[i - 1] = buf[i]))
688: break;
689: else if (' ' == buf[i])
690: break;
691: }
692:
693: mac[i - 1] = 0;
694:
695: if (i == 5 || i <= 2) {
696: if ( ! macrowarn(m, ln, mac))
697: goto err;
698: return(1);
699: }
700:
701: if (MDOC_MAX == (c = mdoc_hash_find(m->htab, mac))) {
702: if ( ! macrowarn(m, ln, mac))
703: goto err;
704: return(1);
705: }
706:
707: /* The macro is sane. Jump to the next word. */
708:
709: while (buf[i] && ' ' == buf[i])
710: i++;
711:
712: /* Begin recursive parse sequence. */
713:
714: if ( ! mdoc_macro(m, c, ln, 1, &i, buf))
715: goto err;
716:
717: return(1);
718:
719: err: /* Error out. */
720:
721: m->flags |= MDOC_HALT;
722: return(0);
723: }