Annotation of mandoc/mdoc.c, Revision 1.79
1.79 ! kristaps 1: /* $Id: mdoc.c,v 1.78 2009/06/15 09:55:43 kristaps Exp $ */
1.1 kristaps 2: /*
1.76 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.75 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.75 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>
1.73 kristaps 20: #include <stdio.h>
1.1 kristaps 21: #include <stdlib.h>
22: #include <string.h>
23:
1.70 kristaps 24: #include "libmdoc.h"
1.1 kristaps 25:
1.73 kristaps 26: enum merr {
27: ENOCALL,
28: EBODYPROL,
29: EPROLBODY,
30: ESPACE,
31: ETEXTPROL,
32: ENOBLANK,
33: EMALLOC
34: };
1.41 kristaps 35:
1.1 kristaps 36: const char *const __mdoc_macronames[MDOC_MAX] = {
37: "\\\"", "Dd", "Dt", "Os",
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.57 kristaps 66: "Fr", "Ud", "Lb", "Ap",
1.61 kristaps 67: "Lp", "Lk", "Mt", "Brq",
1.64 kristaps 68: /* LINTED */
1.65 kristaps 69: "Bro", "Brc", "\%C", "Es",
1.69 kristaps 70: /* LINTED */
71: "En", "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",
1.52 kristaps 82: "filled", "words", "emphasis",
1.64 kristaps 83: "symbolic", "nested"
1.1 kristaps 84: };
85:
86: const char * const *mdoc_macronames = __mdoc_macronames;
87: const char * const *mdoc_argnames = __mdoc_argnames;
88:
1.73 kristaps 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 *,
1.71 kristaps 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 *);
1.73 kristaps 98: static int perr(struct mdoc *, int, int, enum merr);
1.71 kristaps 99:
1.73 kristaps 100: #define verr(m, t) perr((m), (m)->last->line, (m)->last->pos, (t))
1.45 kristaps 101:
1.68 kristaps 102: /*
103: * Get the first (root) node of the parse tree.
104: */
1.1 kristaps 105: const struct mdoc_node *
1.71 kristaps 106: mdoc_node(const struct mdoc *m)
1.1 kristaps 107: {
108:
1.71 kristaps 109: return(MDOC_HALT & m->flags ? NULL : m->first);
1.1 kristaps 110: }
111:
112:
1.37 kristaps 113: const struct mdoc_meta *
1.71 kristaps 114: mdoc_meta(const struct mdoc *m)
1.37 kristaps 115: {
116:
1.71 kristaps 117: return(MDOC_HALT & m->flags ? NULL : &m->meta);
1.37 kristaps 118: }
119:
120:
1.73 kristaps 121: static void
122: mdoc_free1(struct mdoc *mdoc)
1.67 kristaps 123: {
124:
125: if (mdoc->first)
126: mdoc_node_freelist(mdoc->first);
127: if (mdoc->meta.title)
128: free(mdoc->meta.title);
129: if (mdoc->meta.os)
130: free(mdoc->meta.os);
131: if (mdoc->meta.name)
132: free(mdoc->meta.name);
133: if (mdoc->meta.arch)
134: free(mdoc->meta.arch);
135: if (mdoc->meta.vol)
136: free(mdoc->meta.vol);
1.73 kristaps 137: }
138:
139:
140: static int
141: mdoc_alloc1(struct mdoc *mdoc)
142: {
1.67 kristaps 143:
144: bzero(&mdoc->meta, sizeof(struct mdoc_meta));
145: mdoc->flags = 0;
146: mdoc->lastnamed = mdoc->lastsec = 0;
1.70 kristaps 147: mdoc->last = calloc(1, sizeof(struct mdoc_node));
148: if (NULL == mdoc->last)
1.73 kristaps 149: return(0);
150:
1.70 kristaps 151: mdoc->first = mdoc->last;
1.67 kristaps 152: mdoc->last->type = MDOC_ROOT;
153: mdoc->next = MDOC_NEXT_CHILD;
1.73 kristaps 154: return(1);
155: }
156:
157:
158: /*
159: * Free up all resources contributed by a parse: the node tree,
160: * meta-data and so on. Then reallocate the root node for another
161: * parse.
162: */
163: int
164: mdoc_reset(struct mdoc *mdoc)
165: {
166:
167: mdoc_free1(mdoc);
168: return(mdoc_alloc1(mdoc));
1.67 kristaps 169: }
170:
171:
1.68 kristaps 172: /*
173: * Completely free up all resources.
174: */
1.67 kristaps 175: void
1.38 kristaps 176: mdoc_free(struct mdoc *mdoc)
1.34 kristaps 177: {
178:
1.73 kristaps 179: mdoc_free1(mdoc);
1.68 kristaps 180: if (mdoc->htab)
1.74 kristaps 181: mdoc_hash_free(mdoc->htab);
1.1 kristaps 182: free(mdoc);
183: }
184:
185:
186: struct mdoc *
1.55 kristaps 187: mdoc_alloc(void *data, int pflags, const struct mdoc_cb *cb)
1.1 kristaps 188: {
189: struct mdoc *p;
190:
1.70 kristaps 191: if (NULL == (p = calloc(1, sizeof(struct mdoc))))
1.73 kristaps 192: return(NULL);
1.74 kristaps 193: if (cb)
194: (void)memcpy(&p->cb, cb, sizeof(struct mdoc_cb));
1.1 kristaps 195:
196: p->data = data;
1.73 kristaps 197: p->pflags = pflags;
198:
1.74 kristaps 199: if (NULL == (p->htab = mdoc_hash_alloc())) {
200: free(p);
201: return(NULL);
202: } else if (mdoc_alloc1(p))
203: return(p);
1.1 kristaps 204:
1.73 kristaps 205: free(p);
206: return(NULL);
1.1 kristaps 207: }
208:
209:
1.68 kristaps 210: /*
211: * Climb back up the parse tree, validating open scopes. Mostly calls
212: * through to macro_end in macro.c.
213: */
1.1 kristaps 214: int
1.72 kristaps 215: mdoc_endparse(struct mdoc *m)
1.20 kristaps 216: {
217:
1.72 kristaps 218: if (MDOC_HALT & m->flags)
1.20 kristaps 219: return(0);
1.72 kristaps 220: else if (mdoc_macroend(m))
1.20 kristaps 221: return(1);
1.72 kristaps 222: m->flags |= MDOC_HALT;
223: return(0);
1.20 kristaps 224: }
225:
226:
1.50 kristaps 227: /*
1.53 kristaps 228: * Main parse routine. Parses a single line -- really just hands off to
229: * the macro or text parser.
1.50 kristaps 230: */
1.20 kristaps 231: int
1.53 kristaps 232: mdoc_parseln(struct mdoc *m, int ln, char *buf)
1.1 kristaps 233: {
234:
1.53 kristaps 235: /* If in error-mode, then we parse no more. */
1.50 kristaps 236:
1.53 kristaps 237: if (MDOC_HALT & m->flags)
1.20 kristaps 238: return(0);
1.50 kristaps 239:
1.53 kristaps 240: return('.' == *buf ? parsemacro(m, ln, buf) :
241: parsetext(m, ln, buf));
1.1 kristaps 242: }
243:
244:
245: void
1.31 kristaps 246: mdoc_vmsg(struct mdoc *mdoc, int ln, int pos, const char *fmt, ...)
1.1 kristaps 247: {
1.31 kristaps 248: char buf[256];
1.23 kristaps 249: va_list ap;
1.1 kristaps 250:
251: if (NULL == mdoc->cb.mdoc_msg)
252: return;
253:
254: va_start(ap, fmt);
1.31 kristaps 255: (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
1.1 kristaps 256: va_end(ap);
1.31 kristaps 257: (*mdoc->cb.mdoc_msg)(mdoc->data, ln, pos, buf);
1.1 kristaps 258: }
259:
260:
261: int
1.31 kristaps 262: mdoc_verr(struct mdoc *mdoc, int ln, int pos,
263: const char *fmt, ...)
1.1 kristaps 264: {
1.31 kristaps 265: char buf[256];
266: va_list ap;
1.1 kristaps 267:
268: if (NULL == mdoc->cb.mdoc_err)
269: return(0);
1.31 kristaps 270:
271: va_start(ap, fmt);
272: (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
273: va_end(ap);
274: return((*mdoc->cb.mdoc_err)(mdoc->data, ln, pos, buf));
1.1 kristaps 275: }
276:
277:
278: int
1.31 kristaps 279: mdoc_vwarn(struct mdoc *mdoc, int ln, int pos,
280: enum mdoc_warn type, const char *fmt, ...)
1.1 kristaps 281: {
1.31 kristaps 282: char buf[256];
283: va_list ap;
1.1 kristaps 284:
285: if (NULL == mdoc->cb.mdoc_warn)
286: return(0);
1.31 kristaps 287:
288: va_start(ap, fmt);
289: (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
290: va_end(ap);
291: return((*mdoc->cb.mdoc_warn)(mdoc->data, ln, pos, type, buf));
1.1 kristaps 292: }
293:
294:
295: int
1.78 kristaps 296: mdoc_nerr(struct mdoc *mdoc, const struct mdoc_node *node, const char *fmt, ...)
297: {
298: char buf[256];
299: va_list ap;
300:
301: if (NULL == mdoc->cb.mdoc_err)
302: return(0);
303:
304: va_start(ap, fmt);
305: (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
306: va_end(ap);
307: return((*mdoc->cb.mdoc_err)(mdoc->data, node->line, node->pos, buf));
308: }
309:
310:
311: int
312: mdoc_warn(struct mdoc *mdoc, enum mdoc_warn type, const char *fmt, ...)
313: {
314: char buf[256];
315: va_list ap;
316:
317: if (NULL == mdoc->cb.mdoc_warn)
318: return(0);
319:
320: va_start(ap, fmt);
321: (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
322: va_end(ap);
323: return((*mdoc->cb.mdoc_warn)(mdoc->data, mdoc->last->line,
324: mdoc->last->pos, type, buf));
325: }
326:
327:
328: int
329: mdoc_err(struct mdoc *mdoc, const char *fmt, ...)
330: {
331: char buf[256];
332: va_list ap;
333:
334: if (NULL == mdoc->cb.mdoc_err)
335: return(0);
336:
337: va_start(ap, fmt);
338: (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
339: va_end(ap);
340: return((*mdoc->cb.mdoc_err)(mdoc->data, mdoc->last->line,
341: mdoc->last->pos, buf));
342: }
343:
344:
345: void
346: mdoc_msg(struct mdoc *mdoc, const char *fmt, ...)
347: {
348: char buf[256];
349: va_list ap;
350:
351: if (NULL == mdoc->cb.mdoc_msg)
352: return;
353:
354: va_start(ap, fmt);
355: (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
356: va_end(ap);
357: (*mdoc->cb.mdoc_msg)(mdoc->data, mdoc->last->line, mdoc->last->pos,
358: buf);
359: }
360:
361:
362: void
363: mdoc_pmsg(struct mdoc *mdoc, int line, int pos, const char *fmt, ...)
364: {
365: char buf[256];
366: va_list ap;
367:
368: if (NULL == mdoc->cb.mdoc_msg)
369: return;
370:
371: va_start(ap, fmt);
372: (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
373: va_end(ap);
374: (*mdoc->cb.mdoc_msg)(mdoc->data, line, pos, buf);
375: }
376:
377:
378: int
379: mdoc_pwarn(struct mdoc *mdoc, int line, int pos, enum mdoc_warn type,
380: const char *fmt, ...)
381: {
382: char buf[256];
383: va_list ap;
384:
385: if (NULL == mdoc->cb.mdoc_warn)
386: return(0);
387:
388: va_start(ap, fmt);
389: (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
390: va_end(ap);
391: return((*mdoc->cb.mdoc_warn)(mdoc->data, line, pos, type, buf));
392: }
393:
394: int
395: mdoc_perr(struct mdoc *mdoc, int line, int pos, const char *fmt, ...)
396: {
397: char buf[256];
398: va_list ap;
399:
400: if (NULL == mdoc->cb.mdoc_err)
401: return(0);
402:
403: va_start(ap, fmt);
404: (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
405: va_end(ap);
406: return((*mdoc->cb.mdoc_err)(mdoc->data, line, pos, buf));
407: }
408:
409:
410: int
1.53 kristaps 411: mdoc_macro(struct mdoc *m, int tok,
412: int ln, int pp, int *pos, char *buf)
1.1 kristaps 413: {
414:
1.53 kristaps 415: /* FIXME - these should happen during validation. */
1.31 kristaps 416:
1.44 kristaps 417: if (MDOC_PROLOGUE & mdoc_macros[tok].flags &&
1.53 kristaps 418: SEC_PROLOGUE != m->lastnamed)
1.73 kristaps 419: return(perr(m, ln, pp, EPROLBODY));
1.53 kristaps 420:
1.44 kristaps 421: if ( ! (MDOC_PROLOGUE & mdoc_macros[tok].flags) &&
1.53 kristaps 422: SEC_PROLOGUE == m->lastnamed)
1.73 kristaps 423: return(perr(m, ln, pp, EBODYPROL));
1.53 kristaps 424:
425: if (1 != pp && ! (MDOC_CALLABLE & mdoc_macros[tok].flags))
1.73 kristaps 426: return(perr(m, ln, pp, ENOCALL));
1.53 kristaps 427:
428: return((*mdoc_macros[tok].fp)(m, tok, ln, pp, pos, buf));
1.1 kristaps 429: }
430:
431:
1.23 kristaps 432: static int
1.73 kristaps 433: perr(struct mdoc *m, int line, int pos, enum merr type)
434: {
435: char *p;
436:
437: p = NULL;
438: switch (type) {
439: case (ENOCALL):
440: p = "not callable";
441: break;
442: case (EPROLBODY):
443: p = "macro disallowed in document body";
444: break;
445: case (EBODYPROL):
446: p = "macro disallowed in document prologue";
447: break;
448: case (EMALLOC):
449: p = "memory exhausted";
450: break;
451: case (ETEXTPROL):
452: p = "text disallowed in document prologue";
453: break;
454: case (ENOBLANK):
455: p = "blank lines disallowed in non-literal contexts";
456: break;
457: case (ESPACE):
458: p = "whitespace disallowed after delimiter";
459: break;
460: }
461: assert(p);
462: return(mdoc_perr(m, line, pos, p));
463: }
464:
465:
466: static int
467: node_append(struct mdoc *mdoc, struct mdoc_node *p)
1.1 kristaps 468: {
469:
1.25 kristaps 470: assert(mdoc->last);
471: assert(mdoc->first);
472: assert(MDOC_ROOT != p->type);
1.1 kristaps 473:
1.13 kristaps 474: switch (mdoc->next) {
475: case (MDOC_NEXT_SIBLING):
1.6 kristaps 476: mdoc->last->next = p;
477: p->prev = mdoc->last;
1.13 kristaps 478: p->parent = mdoc->last->parent;
1.1 kristaps 479: break;
1.13 kristaps 480: case (MDOC_NEXT_CHILD):
481: mdoc->last->child = p;
1.1 kristaps 482: p->parent = mdoc->last;
483: break;
484: default:
1.13 kristaps 485: abort();
486: /* NOTREACHED */
1.1 kristaps 487: }
488:
1.23 kristaps 489: if ( ! mdoc_valid_pre(mdoc, p))
490: return(0);
1.68 kristaps 491: if ( ! mdoc_action_pre(mdoc, p))
492: return(0);
1.27 kristaps 493:
494: switch (p->type) {
495: case (MDOC_HEAD):
496: assert(MDOC_BLOCK == p->parent->type);
1.53 kristaps 497: p->parent->head = p;
1.27 kristaps 498: break;
499: case (MDOC_TAIL):
500: assert(MDOC_BLOCK == p->parent->type);
1.53 kristaps 501: p->parent->tail = p;
1.27 kristaps 502: break;
503: case (MDOC_BODY):
504: assert(MDOC_BLOCK == p->parent->type);
1.53 kristaps 505: p->parent->body = p;
1.27 kristaps 506: break;
507: default:
508: break;
509: }
510:
1.1 kristaps 511: mdoc->last = p;
1.71 kristaps 512:
513: switch (p->type) {
514: case (MDOC_TEXT):
515: if ( ! mdoc_valid_post(mdoc))
516: return(0);
517: if ( ! mdoc_action_post(mdoc))
518: return(0);
519: break;
520: default:
521: break;
522: }
523:
1.23 kristaps 524: return(1);
1.1 kristaps 525: }
526:
527:
1.45 kristaps 528: static struct mdoc_node *
1.73 kristaps 529: node_alloc(struct mdoc *mdoc, int line,
530: int pos, int tok, enum mdoc_type type)
1.45 kristaps 531: {
1.46 kristaps 532: struct mdoc_node *p;
533:
1.73 kristaps 534: if (NULL == (p = calloc(1, sizeof(struct mdoc_node)))) {
535: (void)verr(mdoc, EMALLOC);
536: return(NULL);
537: }
538:
1.46 kristaps 539: p->sec = mdoc->lastsec;
1.73 kristaps 540: p->line = line;
541: p->pos = pos;
542: p->tok = tok;
543: if (MDOC_TEXT != (p->type = type))
544: assert(p->tok >= 0);
1.45 kristaps 545:
1.46 kristaps 546: return(p);
1.45 kristaps 547: }
548:
549:
1.23 kristaps 550: int
1.22 kristaps 551: mdoc_tail_alloc(struct mdoc *mdoc, int line, int pos, int tok)
1.17 kristaps 552: {
553: struct mdoc_node *p;
554:
1.73 kristaps 555: p = node_alloc(mdoc, line, pos, tok, MDOC_TAIL);
556: if (NULL == p)
557: return(0);
558: return(node_append(mdoc, p));
1.17 kristaps 559: }
560:
561:
1.23 kristaps 562: int
1.22 kristaps 563: mdoc_head_alloc(struct mdoc *mdoc, int line, int pos, int tok)
1.1 kristaps 564: {
565: struct mdoc_node *p;
566:
567: assert(mdoc->first);
568: assert(mdoc->last);
569:
1.73 kristaps 570: p = node_alloc(mdoc, line, pos, tok, MDOC_HEAD);
571: if (NULL == p)
572: return(0);
573: return(node_append(mdoc, p));
1.1 kristaps 574: }
575:
576:
1.23 kristaps 577: int
1.22 kristaps 578: mdoc_body_alloc(struct mdoc *mdoc, int line, int pos, int tok)
1.1 kristaps 579: {
580: struct mdoc_node *p;
581:
1.73 kristaps 582: p = node_alloc(mdoc, line, pos, tok, MDOC_BODY);
583: if (NULL == p)
584: return(0);
585: return(node_append(mdoc, p));
1.1 kristaps 586: }
587:
588:
1.23 kristaps 589: int
1.22 kristaps 590: mdoc_block_alloc(struct mdoc *mdoc, int line, int pos,
1.53 kristaps 591: int tok, struct mdoc_arg *args)
1.1 kristaps 592: {
593: struct mdoc_node *p;
594:
1.73 kristaps 595: p = node_alloc(mdoc, line, pos, tok, MDOC_BLOCK);
596: if (NULL == p)
597: return(0);
1.77 kristaps 598: p->args = args;
599: if (p->args)
1.53 kristaps 600: (args->refcnt)++;
1.73 kristaps 601: return(node_append(mdoc, p));
1.1 kristaps 602: }
603:
604:
1.23 kristaps 605: int
1.22 kristaps 606: mdoc_elem_alloc(struct mdoc *mdoc, int line, int pos,
1.53 kristaps 607: int tok, struct mdoc_arg *args)
1.1 kristaps 608: {
609: struct mdoc_node *p;
610:
1.73 kristaps 611: p = node_alloc(mdoc, line, pos, tok, MDOC_ELEM);
612: if (NULL == p)
613: return(0);
1.77 kristaps 614: p->args = args;
615: if (p->args)
1.53 kristaps 616: (args->refcnt)++;
1.73 kristaps 617: return(node_append(mdoc, p));
1.1 kristaps 618: }
619:
620:
1.23 kristaps 621: int
1.22 kristaps 622: mdoc_word_alloc(struct mdoc *mdoc,
623: int line, int pos, const char *word)
1.1 kristaps 624: {
625: struct mdoc_node *p;
626:
1.73 kristaps 627: p = node_alloc(mdoc, line, pos, -1, MDOC_TEXT);
628: if (NULL == p)
629: return(0);
630: if (NULL == (p->string = strdup(word))) {
631: (void)verr(mdoc, EMALLOC);
632: return(0);
633: }
634: return(node_append(mdoc, p));
1.1 kristaps 635: }
636:
637:
1.53 kristaps 638: void
639: mdoc_node_free(struct mdoc_node *p)
1.1 kristaps 640: {
641:
1.53 kristaps 642: if (p->string)
643: free(p->string);
644: if (p->args)
645: mdoc_argv_free(p->args);
1.1 kristaps 646: free(p);
647: }
648:
649:
1.53 kristaps 650: void
651: mdoc_node_freelist(struct mdoc_node *p)
1.1 kristaps 652: {
653:
1.53 kristaps 654: if (p->child)
655: mdoc_node_freelist(p->child);
656: if (p->next)
657: mdoc_node_freelist(p->next);
1.1 kristaps 658:
1.53 kristaps 659: mdoc_node_free(p);
1.1 kristaps 660: }
661:
662:
1.53 kristaps 663: /*
664: * Parse free-form text, that is, a line that does not begin with the
665: * control character.
666: */
667: static int
1.68 kristaps 668: parsetext(struct mdoc *m, int line, char *buf)
1.1 kristaps 669: {
670:
1.68 kristaps 671: if (SEC_PROLOGUE == m->lastnamed)
1.73 kristaps 672: return(perr(m, line, 0, ETEXTPROL));
1.1 kristaps 673:
1.68 kristaps 674: if (0 == buf[0] && ! (MDOC_LITERAL & m->flags))
1.73 kristaps 675: return(perr(m, line, 0, ENOBLANK));
1.68 kristaps 676:
677: if ( ! mdoc_word_alloc(m, line, 0, buf))
1.53 kristaps 678: return(0);
1.1 kristaps 679:
1.68 kristaps 680: m->next = MDOC_NEXT_SIBLING;
1.53 kristaps 681: return(1);
1.1 kristaps 682: }
683:
684:
1.58 kristaps 685: static int
686: macrowarn(struct mdoc *m, int ln, const char *buf)
687: {
688: if ( ! (MDOC_IGN_MACRO & m->pflags))
1.73 kristaps 689: return(mdoc_perr(m, ln, 1,
690: "unknown macro: %s%s",
1.59 kristaps 691: buf, strlen(buf) > 3 ? "..." : ""));
1.58 kristaps 692: return(mdoc_pwarn(m, ln, 1, WARN_SYNTAX,
693: "unknown macro: %s%s",
1.59 kristaps 694: buf, strlen(buf) > 3 ? "..." : ""));
1.58 kristaps 695: }
696:
697:
698:
1.53 kristaps 699: /*
700: * Parse a macro line, that is, a line beginning with the control
701: * character.
702: */
703: int
704: parsemacro(struct mdoc *m, int ln, char *buf)
1.1 kristaps 705: {
1.53 kristaps 706: int i, c;
707: char mac[5];
1.1 kristaps 708:
1.63 kristaps 709: /* Comments and empties are quickly ignored. */
710:
711: if (0 == buf[1])
712: return(1);
713:
1.65 kristaps 714: if (' ' == buf[1]) {
1.63 kristaps 715: i = 2;
1.65 kristaps 716: while (buf[i] && ' ' == buf[i])
1.63 kristaps 717: i++;
718: if (0 == buf[i])
719: return(1);
1.73 kristaps 720: return(perr(m, ln, 1, ESPACE));
1.63 kristaps 721: }
1.1 kristaps 722:
1.53 kristaps 723: if (buf[1] && '\\' == buf[1])
724: if (buf[2] && '\"' == buf[2])
725: return(1);
1.1 kristaps 726:
1.53 kristaps 727: /* Copy the first word into a nil-terminated buffer. */
1.1 kristaps 728:
1.53 kristaps 729: for (i = 1; i < 5; i++) {
730: if (0 == (mac[i - 1] = buf[i]))
731: break;
1.65 kristaps 732: else if (' ' == buf[i])
1.53 kristaps 733: break;
734: }
1.1 kristaps 735:
1.53 kristaps 736: mac[i - 1] = 0;
1.1 kristaps 737:
1.53 kristaps 738: if (i == 5 || i <= 2) {
1.58 kristaps 739: if ( ! macrowarn(m, ln, mac))
740: goto err;
741: return(1);
1.53 kristaps 742: }
743:
1.74 kristaps 744: if (MDOC_MAX == (c = mdoc_hash_find(m->htab, mac))) {
1.58 kristaps 745: if ( ! macrowarn(m, ln, mac))
746: goto err;
747: return(1);
1.53 kristaps 748: }
1.1 kristaps 749:
1.53 kristaps 750: /* The macro is sane. Jump to the next word. */
1.1 kristaps 751:
1.65 kristaps 752: while (buf[i] && ' ' == buf[i])
1.53 kristaps 753: i++;
1.1 kristaps 754:
1.53 kristaps 755: /* Begin recursive parse sequence. */
1.1 kristaps 756:
1.53 kristaps 757: if ( ! mdoc_macro(m, c, ln, 1, &i, buf))
758: goto err;
1.1 kristaps 759:
1.53 kristaps 760: return(1);
1.1 kristaps 761:
1.53 kristaps 762: err: /* Error out. */
1.1 kristaps 763:
1.53 kristaps 764: m->flags |= MDOC_HALT;
765: return(0);
1.1 kristaps 766: }
CVSweb