Annotation of src/usr.bin/mandoc/mdoc.c, Revision 1.9
1.9 ! schwarze 1: /* $Id: mdoc.c,v 1.8 2009/06/18 23:51:12 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);
279: return((*mdoc->cb.mdoc_warn)(mdoc->data, ln, pos, type, 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.8 schwarze 315: mdoc->last->pos, type, 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.8 schwarze 349: return((*mdoc->cb.mdoc_warn)(mdoc->data,
350: line, pos, type, buf));
1.5 schwarze 351: }
352:
353: int
354: mdoc_perr(struct mdoc *mdoc, int line, int pos, const char *fmt, ...)
355: {
356: char buf[256];
357: va_list ap;
358:
359: if (NULL == mdoc->cb.mdoc_err)
360: return(0);
361:
362: va_start(ap, fmt);
363: (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
364: va_end(ap);
365: return((*mdoc->cb.mdoc_err)(mdoc->data, line, pos, buf));
1.2 miod 366: }
367:
368:
369: int
1.1 kristaps 370: mdoc_macro(struct mdoc *m, int tok,
371: int ln, int pp, int *pos, char *buf)
372: {
373:
374: if (MDOC_PROLOGUE & mdoc_macros[tok].flags &&
1.9 ! schwarze 375: MDOC_PBODY & m->flags)
1.1 kristaps 376: return(perr(m, ln, pp, EPROLBODY));
377: if ( ! (MDOC_PROLOGUE & mdoc_macros[tok].flags) &&
1.9 ! schwarze 378: ! (MDOC_PBODY & m->flags))
1.1 kristaps 379: return(perr(m, ln, pp, EBODYPROL));
380:
381: if (1 != pp && ! (MDOC_CALLABLE & mdoc_macros[tok].flags))
382: return(perr(m, ln, pp, ENOCALL));
383:
384: return((*mdoc_macros[tok].fp)(m, tok, ln, pp, pos, buf));
385: }
386:
387:
388: static int
389: perr(struct mdoc *m, int line, int pos, enum merr type)
390: {
391: char *p;
392:
393: p = NULL;
394: switch (type) {
395: case (ENOCALL):
396: p = "not callable";
397: break;
398: case (EPROLBODY):
399: p = "macro disallowed in document body";
400: break;
401: case (EBODYPROL):
402: p = "macro disallowed in document prologue";
403: break;
404: case (EMALLOC):
405: p = "memory exhausted";
406: break;
407: case (ETEXTPROL):
408: p = "text disallowed in document prologue";
409: break;
410: case (ENOBLANK):
411: p = "blank lines disallowed in non-literal contexts";
412: break;
413: case (ESPACE):
414: p = "whitespace disallowed after delimiter";
415: break;
416: }
417: assert(p);
418: return(mdoc_perr(m, line, pos, p));
419: }
420:
421:
422: static int
423: node_append(struct mdoc *mdoc, struct mdoc_node *p)
424: {
425:
426: assert(mdoc->last);
427: assert(mdoc->first);
428: assert(MDOC_ROOT != p->type);
429:
430: switch (mdoc->next) {
431: case (MDOC_NEXT_SIBLING):
432: mdoc->last->next = p;
433: p->prev = mdoc->last;
434: p->parent = mdoc->last->parent;
435: break;
436: case (MDOC_NEXT_CHILD):
437: mdoc->last->child = p;
438: p->parent = mdoc->last;
439: break;
440: default:
441: abort();
442: /* NOTREACHED */
443: }
444:
445: if ( ! mdoc_valid_pre(mdoc, p))
446: return(0);
447: if ( ! mdoc_action_pre(mdoc, p))
448: return(0);
449:
450: switch (p->type) {
451: case (MDOC_HEAD):
452: assert(MDOC_BLOCK == p->parent->type);
453: p->parent->head = p;
454: break;
455: case (MDOC_TAIL):
456: assert(MDOC_BLOCK == p->parent->type);
457: p->parent->tail = p;
458: break;
459: case (MDOC_BODY):
460: assert(MDOC_BLOCK == p->parent->type);
461: p->parent->body = p;
462: break;
463: default:
464: break;
465: }
466:
467: mdoc->last = p;
468:
469: switch (p->type) {
470: case (MDOC_TEXT):
471: if ( ! mdoc_valid_post(mdoc))
472: return(0);
473: if ( ! mdoc_action_post(mdoc))
474: return(0);
475: break;
476: default:
477: break;
478: }
479:
480: return(1);
481: }
482:
483:
484: static struct mdoc_node *
485: node_alloc(struct mdoc *mdoc, int line,
486: int pos, int tok, enum mdoc_type type)
487: {
488: struct mdoc_node *p;
489:
490: if (NULL == (p = calloc(1, sizeof(struct mdoc_node)))) {
1.8 schwarze 491: (void)perr(mdoc, (mdoc)->last->line,
492: (mdoc)->last->pos, EMALLOC);
1.1 kristaps 493: return(NULL);
494: }
495:
496: p->sec = mdoc->lastsec;
497: p->line = line;
498: p->pos = pos;
499: p->tok = tok;
500: if (MDOC_TEXT != (p->type = type))
501: assert(p->tok >= 0);
502:
503: return(p);
504: }
505:
506:
507: int
508: mdoc_tail_alloc(struct mdoc *mdoc, int line, int pos, int tok)
509: {
510: struct mdoc_node *p;
511:
512: p = node_alloc(mdoc, line, pos, tok, MDOC_TAIL);
513: if (NULL == p)
514: return(0);
515: return(node_append(mdoc, p));
516: }
517:
518:
519: int
520: mdoc_head_alloc(struct mdoc *mdoc, int line, int pos, int tok)
521: {
522: struct mdoc_node *p;
523:
524: assert(mdoc->first);
525: assert(mdoc->last);
526:
527: p = node_alloc(mdoc, line, pos, tok, MDOC_HEAD);
528: if (NULL == p)
529: return(0);
530: return(node_append(mdoc, p));
531: }
532:
533:
534: int
535: mdoc_body_alloc(struct mdoc *mdoc, int line, int pos, int tok)
536: {
537: struct mdoc_node *p;
538:
539: p = node_alloc(mdoc, line, pos, tok, MDOC_BODY);
540: if (NULL == p)
541: return(0);
542: return(node_append(mdoc, p));
543: }
544:
545:
546: int
547: mdoc_block_alloc(struct mdoc *mdoc, int line, int pos,
548: int tok, struct mdoc_arg *args)
549: {
550: struct mdoc_node *p;
551:
552: p = node_alloc(mdoc, line, pos, tok, MDOC_BLOCK);
553: if (NULL == p)
554: return(0);
1.4 schwarze 555: p->args = args;
556: if (p->args)
1.1 kristaps 557: (args->refcnt)++;
558: return(node_append(mdoc, p));
559: }
560:
561:
562: int
563: mdoc_elem_alloc(struct mdoc *mdoc, int line, int pos,
564: int tok, struct mdoc_arg *args)
565: {
566: struct mdoc_node *p;
567:
568: p = node_alloc(mdoc, line, pos, tok, MDOC_ELEM);
569: if (NULL == p)
570: return(0);
1.4 schwarze 571: p->args = args;
572: if (p->args)
1.1 kristaps 573: (args->refcnt)++;
574: return(node_append(mdoc, p));
575: }
576:
577:
578: int
579: mdoc_word_alloc(struct mdoc *mdoc,
580: int line, int pos, const char *word)
581: {
582: struct mdoc_node *p;
583:
584: p = node_alloc(mdoc, line, pos, -1, MDOC_TEXT);
585: if (NULL == p)
586: return(0);
587: if (NULL == (p->string = strdup(word))) {
1.8 schwarze 588: (void)perr(mdoc, (mdoc)->last->line,
589: (mdoc)->last->pos, EMALLOC);
1.1 kristaps 590: return(0);
591: }
592: return(node_append(mdoc, p));
593: }
594:
595:
596: void
597: mdoc_node_free(struct mdoc_node *p)
598: {
599:
600: if (p->string)
601: free(p->string);
602: if (p->args)
603: mdoc_argv_free(p->args);
604: free(p);
605: }
606:
607:
608: void
609: mdoc_node_freelist(struct mdoc_node *p)
610: {
611:
612: if (p->child)
613: mdoc_node_freelist(p->child);
614: if (p->next)
615: mdoc_node_freelist(p->next);
616:
617: mdoc_node_free(p);
618: }
619:
620:
621: /*
622: * Parse free-form text, that is, a line that does not begin with the
623: * control character.
624: */
625: static int
626: parsetext(struct mdoc *m, int line, char *buf)
627: {
628:
1.9 ! schwarze 629: if (SEC_NONE == m->lastnamed)
1.1 kristaps 630: return(perr(m, line, 0, ETEXTPROL));
631:
632: if (0 == buf[0] && ! (MDOC_LITERAL & m->flags))
633: return(perr(m, line, 0, ENOBLANK));
634:
635: if ( ! mdoc_word_alloc(m, line, 0, buf))
636: return(0);
637:
638: m->next = MDOC_NEXT_SIBLING;
639: return(1);
640: }
641:
642:
643: static int
644: macrowarn(struct mdoc *m, int ln, const char *buf)
645: {
646: if ( ! (MDOC_IGN_MACRO & m->pflags))
647: return(mdoc_perr(m, ln, 1,
648: "unknown macro: %s%s",
649: buf, strlen(buf) > 3 ? "..." : ""));
650: return(mdoc_pwarn(m, ln, 1, WARN_SYNTAX,
651: "unknown macro: %s%s",
652: buf, strlen(buf) > 3 ? "..." : ""));
653: }
654:
655:
656: /*
657: * Parse a macro line, that is, a line beginning with the control
658: * character.
659: */
660: int
661: parsemacro(struct mdoc *m, int ln, char *buf)
662: {
663: int i, c;
664: char mac[5];
665:
1.7 schwarze 666: /* Empty lines are ignored. */
1.1 kristaps 667:
668: if (0 == buf[1])
669: return(1);
670:
671: if (' ' == buf[1]) {
672: i = 2;
673: while (buf[i] && ' ' == buf[i])
674: i++;
675: if (0 == buf[i])
676: return(1);
677: return(perr(m, ln, 1, ESPACE));
678: }
679:
680: /* Copy the first word into a nil-terminated buffer. */
681:
682: for (i = 1; i < 5; i++) {
683: if (0 == (mac[i - 1] = buf[i]))
684: break;
685: else if (' ' == buf[i])
686: break;
687: }
688:
689: mac[i - 1] = 0;
690:
691: if (i == 5 || i <= 2) {
692: if ( ! macrowarn(m, ln, mac))
693: goto err;
694: return(1);
695: }
696:
697: if (MDOC_MAX == (c = mdoc_hash_find(m->htab, mac))) {
698: if ( ! macrowarn(m, ln, mac))
699: goto err;
700: return(1);
701: }
702:
703: /* The macro is sane. Jump to the next word. */
704:
705: while (buf[i] && ' ' == buf[i])
706: i++;
707:
708: /* Begin recursive parse sequence. */
709:
710: if ( ! mdoc_macro(m, c, ln, 1, &i, buf))
711: goto err;
712:
713: return(1);
714:
715: err: /* Error out. */
716:
717: m->flags |= MDOC_HALT;
718: return(0);
719: }