Annotation of mandoc/mdoc.c, Revision 1.30
1.30 ! kristaps 1: /* $Id: mdoc.c,v 1.29 2009/01/14 11:58:24 kristaps Exp $ */
1.1 kristaps 2: /*
3: * Copyright (c) 2008 Kristaps Dzonsons <kristaps@kth.se>
4: *
5: * Permission to use, copy, modify, and distribute this software for any
6: * purpose with or without fee is hereby granted, provided that the
7: * above copyright notice and this permission notice appear in all
8: * copies.
9: *
10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
11: * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
12: * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
13: * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14: * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15: * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
16: * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17: * PERFORMANCE OF THIS SOFTWARE.
18: */
19: #include <assert.h>
20: #include <ctype.h>
21: #include <err.h>
22: #include <stdarg.h>
23: #include <stdlib.h>
24: #include <stdio.h>
25: #include <string.h>
26:
27: #include "private.h"
28:
29: const char *const __mdoc_macronames[MDOC_MAX] = {
30: "\\\"", "Dd", "Dt", "Os",
31: "Sh", "Ss", "Pp", "D1",
32: "Dl", "Bd", "Ed", "Bl",
33: "El", "It", "Ad", "An",
34: "Ar", "Cd", "Cm", "Dv",
35: "Er", "Ev", "Ex", "Fa",
36: "Fd", "Fl", "Fn", "Ft",
37: "Ic", "In", "Li", "Nd",
38: "Nm", "Op", "Ot", "Pa",
39: "Rv", "St", "Va", "Vt",
40: /* LINTED */
41: "Xr", "\%A", "\%B", "\%D",
42: /* LINTED */
43: "\%I", "\%J", "\%N", "\%O",
44: /* LINTED */
45: "\%P", "\%R", "\%T", "\%V",
46: "Ac", "Ao", "Aq", "At",
47: "Bc", "Bf", "Bo", "Bq",
48: "Bsx", "Bx", "Db", "Dc",
49: "Do", "Dq", "Ec", "Ef",
50: "Em", "Eo", "Fx", "Ms",
51: "No", "Ns", "Nx", "Ox",
52: "Pc", "Pf", "Po", "Pq",
53: "Qc", "Ql", "Qo", "Qq",
54: "Re", "Rs", "Sc", "So",
55: "Sq", "Sm", "Sx", "Sy",
56: "Tn", "Ux", "Xc", "Xo",
57: "Fo", "Fc", "Oo", "Oc",
58: "Bk", "Ek", "Bt", "Hf",
59: "Fr", "Ud",
60: };
61:
62: const char *const __mdoc_argnames[MDOC_ARG_MAX] = {
63: "split", "nosplit", "ragged",
64: "unfilled", "literal", "file",
65: "offset", "bullet", "dash",
66: "hyphen", "item", "enum",
67: "tag", "diag", "hang",
68: "ohang", "inset", "column",
69: "width", "compact", "std",
70: "p1003.1-88", "p1003.1-90", "p1003.1-96",
71: "p1003.1-2001", "p1003.1-2004", "p1003.1",
72: "p1003.1b", "p1003.1b-93", "p1003.1c-95",
73: "p1003.1g-2000", "p1003.2-92", "p1387.2-95",
74: "p1003.2", "p1387.2", "isoC-90",
75: "isoC-amd1", "isoC-tcor1", "isoC-tcor2",
76: "isoC-99", "ansiC", "ansiC-89",
77: "ansiC-99", "ieee754", "iso8802-3",
78: "xpg3", "xpg4", "xpg4.2",
79: "xpg4.3", "xbd5", "xcu5",
80: "xsh5", "xns5", "xns5.2d2.0",
81: "xcurses4.2", "susv2", "susv3",
82: "svid4", "filled", "words",
1.14 kristaps 83: "emphasis", "symbolic",
1.1 kristaps 84: };
85:
86: const struct mdoc_macro __mdoc_macros[MDOC_MAX] = {
87: { NULL, 0 }, /* \" */
1.24 kristaps 88: { macro_constant, MDOC_PROLOGUE }, /* Dd */
89: { macro_constant, MDOC_PROLOGUE }, /* Dt */
90: { macro_constant, MDOC_PROLOGUE }, /* Os */
1.13 kristaps 91: { macro_scoped, 0 }, /* Sh */
92: { macro_scoped, 0 }, /* Ss */
1.5 kristaps 93: { macro_text, 0 }, /* Pp */
1.19 kristaps 94: { macro_scoped_line, MDOC_PARSED }, /* D1 */
95: { macro_scoped_line, MDOC_PARSED }, /* Dl */
1.13 kristaps 96: { macro_scoped, MDOC_EXPLICIT }, /* Bd */
1.29 kristaps 97: { macro_scoped_close, MDOC_EXPLICIT }, /* Ed */
1.13 kristaps 98: { macro_scoped, MDOC_EXPLICIT }, /* Bl */
1.29 kristaps 99: { macro_scoped_close, MDOC_EXPLICIT }, /* El */
100: { macro_scoped, MDOC_PARSED | MDOC_TABSEP}, /* It */
1.19 kristaps 101: { macro_text, MDOC_CALLABLE | MDOC_PARSED }, /* Ad */
102: { macro_constant, MDOC_PARSED }, /* An */
103: { macro_text, MDOC_CALLABLE | MDOC_PARSED }, /* Ar */
1.12 kristaps 104: { macro_constant, MDOC_QUOTABLE }, /* Cd */
1.19 kristaps 105: { macro_text, MDOC_CALLABLE | MDOC_PARSED }, /* Cm */
106: { macro_text, MDOC_CALLABLE | MDOC_PARSED }, /* Dv */
107: { macro_text, MDOC_CALLABLE | MDOC_PARSED }, /* Er */
108: { macro_text, MDOC_CALLABLE | MDOC_PARSED }, /* Ev */
1.13 kristaps 109: { macro_constant, 0 }, /* Ex */
1.19 kristaps 110: { macro_text, MDOC_CALLABLE | MDOC_QUOTABLE | MDOC_PARSED }, /* Fa */
1.13 kristaps 111: { macro_constant, 0 }, /* Fd */
1.19 kristaps 112: { macro_text, MDOC_CALLABLE | MDOC_PARSED }, /* Fl */
113: { macro_text, MDOC_CALLABLE | MDOC_QUOTABLE | MDOC_PARSED }, /* Fn */
114: { macro_text, MDOC_PARSED }, /* Ft */
115: { macro_text, MDOC_CALLABLE | MDOC_PARSED }, /* Ic */
1.13 kristaps 116: { macro_constant, 0 }, /* In */
1.19 kristaps 117: { macro_text, MDOC_CALLABLE | MDOC_PARSED }, /* Li */
1.10 kristaps 118: { macro_constant, 0 }, /* Nd */
1.19 kristaps 119: { macro_text, MDOC_CALLABLE | MDOC_PARSED }, /* Nm */
120: { macro_scoped_line, MDOC_CALLABLE | MDOC_PARSED }, /* Op */
1.13 kristaps 121: { macro_obsolete, 0 }, /* Ot */
1.19 kristaps 122: { macro_text, MDOC_CALLABLE | MDOC_PARSED }, /* Pa */
1.13 kristaps 123: { macro_constant, 0 }, /* Rv */
1.22 kristaps 124: /* XXX - .St supposed to be (but isn't) callable. */
1.19 kristaps 125: { macro_constant_delimited, MDOC_PARSED }, /* St */
126: { macro_text, MDOC_CALLABLE | MDOC_PARSED }, /* Va */
127: { macro_text, MDOC_CALLABLE | MDOC_PARSED }, /* Vt */
128: { macro_text, MDOC_CALLABLE | MDOC_PARSED }, /* Xr */
1.23 kristaps 129: { macro_constant, MDOC_QUOTABLE }, /* %A */
130: { macro_constant, MDOC_QUOTABLE }, /* %B */
1.13 kristaps 131: { macro_constant, MDOC_QUOTABLE }, /* %D */
1.23 kristaps 132: { macro_constant, MDOC_QUOTABLE }, /* %I */
133: { macro_constant, MDOC_QUOTABLE }, /* %J */
1.13 kristaps 134: { macro_constant, MDOC_QUOTABLE }, /* %N */
135: { macro_constant, MDOC_QUOTABLE }, /* %O */
136: { macro_constant, MDOC_QUOTABLE }, /* %P */
137: { macro_constant, MDOC_QUOTABLE }, /* %R */
1.23 kristaps 138: { macro_constant, MDOC_QUOTABLE }, /* %T */
1.13 kristaps 139: { macro_constant, MDOC_QUOTABLE }, /* %V */
1.29 kristaps 140: { macro_scoped_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Ac */
141: { macro_constant_scoped, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Ao */
1.19 kristaps 142: { macro_scoped_line, MDOC_CALLABLE | MDOC_PARSED }, /* Aq */
1.9 kristaps 143: { macro_constant, 0 }, /* At */
1.29 kristaps 144: { macro_scoped_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Bc */
1.16 kristaps 145: { macro_scoped, MDOC_EXPLICIT }, /* Bf */
1.29 kristaps 146: { macro_constant_scoped, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Bo */
1.19 kristaps 147: { macro_scoped_line, MDOC_CALLABLE | MDOC_PARSED }, /* Bq */
148: { macro_constant_delimited, MDOC_PARSED }, /* Bsx */
149: { macro_constant_delimited, MDOC_PARSED }, /* Bx */
1.13 kristaps 150: { macro_constant, 0 }, /* Db */
1.29 kristaps 151: { macro_scoped_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Dc */
152: { macro_constant_scoped, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Do */
1.19 kristaps 153: { macro_scoped_line, MDOC_CALLABLE | MDOC_PARSED }, /* Dq */
1.29 kristaps 154: { macro_scoped_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Ec */
155: { macro_scoped_close, MDOC_EXPLICIT }, /* Ef */
1.19 kristaps 156: { macro_text, MDOC_CALLABLE | MDOC_PARSED }, /* Em */
1.29 kristaps 157: { macro_constant_scoped, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Eo */
1.19 kristaps 158: { macro_constant_delimited, MDOC_PARSED }, /* Fx */
159: { macro_text, MDOC_PARSED }, /* Ms */
160: { macro_constant_delimited, MDOC_CALLABLE | MDOC_PARSED }, /* No */
161: { macro_constant_delimited, MDOC_CALLABLE | MDOC_PARSED }, /* Ns */
162: { macro_constant_delimited, MDOC_PARSED }, /* Nx */
163: { macro_constant_delimited, MDOC_PARSED }, /* Ox */
1.29 kristaps 164: { macro_scoped_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Pc */
1.19 kristaps 165: { macro_constant, MDOC_PARSED }, /* Pf */
1.29 kristaps 166: { macro_constant_scoped, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Po */
1.19 kristaps 167: { macro_scoped_line, MDOC_CALLABLE | MDOC_PARSED }, /* Pq */
1.29 kristaps 168: { macro_scoped_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Qc */
1.19 kristaps 169: { macro_scoped_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ql */
1.29 kristaps 170: { macro_constant_scoped, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Qo */
1.19 kristaps 171: { macro_scoped_line, MDOC_CALLABLE | MDOC_PARSED }, /* Qq */
1.29 kristaps 172: { macro_scoped_close, MDOC_EXPLICIT }, /* Re */
1.13 kristaps 173: { macro_scoped, MDOC_EXPLICIT }, /* Rs */
1.29 kristaps 174: { macro_scoped_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Sc */
175: { macro_constant_scoped, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* So */
1.19 kristaps 176: { macro_scoped_line, MDOC_CALLABLE | MDOC_PARSED }, /* Sq */
1.13 kristaps 177: { macro_constant, 0 }, /* Sm */
1.19 kristaps 178: { macro_text, MDOC_CALLABLE | MDOC_PARSED }, /* Sx */
179: { macro_text, MDOC_CALLABLE | MDOC_PARSED }, /* Sy */
180: { macro_text, MDOC_CALLABLE | MDOC_PARSED }, /* Tn */
181: { macro_constant_delimited, MDOC_PARSED }, /* Ux */
1.29 kristaps 182: { macro_scoped_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Xc */
183: { macro_constant_scoped, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Xo */
1.19 kristaps 184: /* XXX - .Fo supposed to be (but isn't) callable. */
185: { macro_scoped, MDOC_EXPLICIT | MDOC_PARSED }, /* Fo */
186: /* XXX - .Fc supposed to be (but isn't) callable. */
1.29 kristaps 187: { macro_scoped_close, MDOC_EXPLICIT | MDOC_PARSED }, /* Fc */
188: { macro_constant_scoped, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Oo */
189: { macro_scoped_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Oc */
1.17 kristaps 190: { macro_scoped, MDOC_EXPLICIT }, /* Bk */
1.29 kristaps 191: { macro_scoped_close, MDOC_EXPLICIT }, /* Ek */
1.9 kristaps 192: { macro_constant, 0 }, /* Bt */
1.10 kristaps 193: { macro_constant, 0 }, /* Hf */
1.12 kristaps 194: { macro_obsolete, 0 }, /* Fr */
1.9 kristaps 195: { macro_constant, 0 }, /* Ud */
1.1 kristaps 196: };
197:
198: const char * const *mdoc_macronames = __mdoc_macronames;
199: const char * const *mdoc_argnames = __mdoc_argnames;
200: const struct mdoc_macro * const mdoc_macros = __mdoc_macros;
201:
202:
203: static struct mdoc_arg *argdup(size_t, const struct mdoc_arg *);
204: static void argfree(size_t, struct mdoc_arg *);
205: static void argcpy(struct mdoc_arg *,
206: const struct mdoc_arg *);
207:
208: static void mdoc_node_freelist(struct mdoc_node *);
1.23 kristaps 209: static int mdoc_node_append(struct mdoc *,
1.1 kristaps 210: struct mdoc_node *);
211: static void mdoc_elem_free(struct mdoc_elem *);
212: static void mdoc_text_free(struct mdoc_text *);
213:
214:
215: const struct mdoc_node *
216: mdoc_result(struct mdoc *mdoc)
217: {
218:
219: return(mdoc->first);
220: }
221:
222:
223: void
224: mdoc_free(struct mdoc *mdoc)
225: {
226:
227: if (mdoc->first)
228: mdoc_node_freelist(mdoc->first);
229: if (mdoc->htab)
1.4 kristaps 230: mdoc_tokhash_free(mdoc->htab);
1.1 kristaps 231:
232: free(mdoc);
233: }
234:
235:
236: struct mdoc *
237: mdoc_alloc(void *data, const struct mdoc_cb *cb)
238: {
239: struct mdoc *p;
240:
241: p = xcalloc(1, sizeof(struct mdoc));
242:
243: p->data = data;
244: (void)memcpy(&p->cb, cb, sizeof(struct mdoc_cb));
245:
1.25 kristaps 246: p->last = xcalloc(1, sizeof(struct mdoc_node));
247: p->last->type = MDOC_ROOT;
248: p->first = p->last;
249:
250: p->next = MDOC_NEXT_CHILD;
1.4 kristaps 251: p->htab = mdoc_tokhash_alloc();
1.25 kristaps 252:
1.1 kristaps 253: return(p);
254: }
255:
256:
257: int
1.20 kristaps 258: mdoc_endparse(struct mdoc *mdoc)
259: {
260:
261: if (MDOC_HALT & mdoc->flags)
262: return(0);
263: if (NULL == mdoc->first)
264: return(1);
265:
266: assert(mdoc->last);
267: if ( ! macro_end(mdoc)) {
268: mdoc->flags |= MDOC_HALT;
269: return(0);
270: }
271: return(1);
272: }
273:
274:
275: int
276: mdoc_parseln(struct mdoc *mdoc, int line, char *buf)
1.1 kristaps 277: {
278: int c, i;
279: char tmp[5];
280:
1.20 kristaps 281: if (MDOC_HALT & mdoc->flags)
282: return(0);
283:
1.5 kristaps 284: if ('.' != *buf) {
1.23 kristaps 285: if (SEC_PROLOGUE != mdoc->sec_lastn) {
1.26 kristaps 286: if ( ! mdoc_word_alloc(mdoc, line, 0, buf))
287: return(0);
1.23 kristaps 288: mdoc->next = MDOC_NEXT_SIBLING;
289: return(1);
290: }
291: return(mdoc_perr(mdoc, line, 0, ERR_SYNTAX_NOTEXT));
1.1 kristaps 292: }
293:
294: if (buf[1] && '\\' == buf[1])
295: if (buf[2] && '\"' == buf[2])
296: return(1);
297:
298: i = 1;
299: while (buf[i] && ! isspace(buf[i]) && i < (int)sizeof(tmp))
300: i++;
301:
1.20 kristaps 302: if (i == (int)sizeof(tmp)) {
303: mdoc->flags |= MDOC_HALT;
1.23 kristaps 304: return(mdoc_perr(mdoc, line, 1, ERR_MACRO_NOTSUP));
1.20 kristaps 305: } else if (i <= 2) {
306: mdoc->flags |= MDOC_HALT;
1.23 kristaps 307: return(mdoc_perr(mdoc, line, 1, ERR_MACRO_NOTSUP));
1.20 kristaps 308: }
1.1 kristaps 309:
310: i--;
311:
1.2 kristaps 312: (void)memcpy(tmp, buf + 1, (size_t)i);
1.1 kristaps 313: tmp[i++] = 0;
314:
1.20 kristaps 315: if (MDOC_MAX == (c = mdoc_find(mdoc, tmp))) {
316: mdoc->flags |= MDOC_HALT;
1.23 kristaps 317: return(mdoc_perr(mdoc, line, 1, ERR_MACRO_NOTSUP));
1.20 kristaps 318: }
1.1 kristaps 319:
320: while (buf[i] && isspace(buf[i]))
321: i++;
322:
1.21 kristaps 323: if ( ! mdoc_macro(mdoc, c, line, 1, &i, buf)) {
1.20 kristaps 324: mdoc->flags |= MDOC_HALT;
325: return(0);
326: }
327: return(1);
1.1 kristaps 328: }
329:
330:
331: void
1.23 kristaps 332: mdoc_msg(struct mdoc *mdoc, const char *fmt, ...)
1.1 kristaps 333: {
1.23 kristaps 334: struct mdoc_node *n;
335: va_list ap;
336: char buf[256];
1.1 kristaps 337:
338: if (NULL == mdoc->cb.mdoc_msg)
339: return;
340:
1.23 kristaps 341: n = mdoc->last;
342: assert(n);
343:
1.1 kristaps 344: va_start(ap, fmt);
345: (void)vsnprintf(buf, sizeof(buf), fmt, ap);
346: va_end(ap);
347:
1.23 kristaps 348: (*mdoc->cb.mdoc_msg)(mdoc->data, n->line, n->pos, buf);
1.1 kristaps 349: }
350:
351:
352: int
1.23 kristaps 353: mdoc_perr(struct mdoc *mdoc,
354: int line, int pos, enum mdoc_err type)
1.1 kristaps 355: {
356:
357: if (NULL == mdoc->cb.mdoc_err)
358: return(0);
1.23 kristaps 359: return((*mdoc->cb.mdoc_err)(mdoc->data, line, pos, type));
1.1 kristaps 360: }
361:
362:
363: int
1.23 kristaps 364: mdoc_pwarn(struct mdoc *mdoc,
365: int line, int pos, enum mdoc_warn type)
1.1 kristaps 366: {
367:
368: if (NULL == mdoc->cb.mdoc_warn)
369: return(0);
1.23 kristaps 370: return((*mdoc->cb.mdoc_warn)(mdoc->data, line, pos, type));
1.1 kristaps 371: }
372:
373:
374: int
1.21 kristaps 375: mdoc_macro(struct mdoc *mdoc, int tok,
1.23 kristaps 376: int ln, int ppos, int *pos, char *buf)
1.1 kristaps 377: {
378:
1.17 kristaps 379: if ( ! (MDOC_PROLOGUE & mdoc_macros[tok].flags) &&
1.13 kristaps 380: SEC_PROLOGUE == mdoc->sec_lastn)
1.23 kristaps 381: return(mdoc_perr(mdoc, ln, ppos, ERR_SEC_PROLOGUE));
1.13 kristaps 382:
1.23 kristaps 383: if (NULL == (mdoc_macros[tok].fp))
384: return(mdoc_perr(mdoc, ln, ppos, ERR_MACRO_NOTSUP));
1.3 kristaps 385:
1.23 kristaps 386: if (1 != ppos && ! (MDOC_CALLABLE & mdoc_macros[tok].flags))
387: return(mdoc_perr(mdoc, ln, ppos, ERR_MACRO_NOTCALL));
1.1 kristaps 388:
1.23 kristaps 389: return((*mdoc_macros[tok].fp)(mdoc, tok, ln, ppos, pos, buf));
1.1 kristaps 390: }
391:
392:
1.23 kristaps 393: static int
394: mdoc_node_append(struct mdoc *mdoc, struct mdoc_node *p)
1.1 kristaps 395: {
1.30 ! kristaps 396: const char *nn, *nt, *on, *ot, *act;
1.1 kristaps 397:
1.25 kristaps 398: assert(mdoc->last);
399: assert(mdoc->first);
400: assert(MDOC_ROOT != p->type);
1.1 kristaps 401:
1.27 kristaps 402: if (MDOC_TEXT == mdoc->last->type)
403: on = "<text>";
404: else if (MDOC_ROOT == mdoc->last->type)
405: on = "<root>";
406: else
407: on = mdoc_macronames[mdoc->last->tok];
408:
1.30 ! kristaps 409: if (MDOC_TEXT == p->type)
! 410: nn = "<text>";
! 411: else if (MDOC_ROOT == p->type)
! 412: nn = "<root>";
! 413: else
! 414: nn = mdoc_macronames[p->tok];
! 415:
! 416: ot = mdoc_type2a(mdoc->last->type);
! 417: nt = mdoc_type2a(p->type);
1.1 kristaps 418:
1.13 kristaps 419: switch (mdoc->next) {
420: case (MDOC_NEXT_SIBLING):
1.6 kristaps 421: mdoc->last->next = p;
422: p->prev = mdoc->last;
1.13 kristaps 423: p->parent = mdoc->last->parent;
1.6 kristaps 424: act = "sibling";
1.1 kristaps 425: break;
1.13 kristaps 426: case (MDOC_NEXT_CHILD):
427: mdoc->last->child = p;
1.1 kristaps 428: p->parent = mdoc->last;
429: act = "child";
430: break;
431: default:
1.13 kristaps 432: abort();
433: /* NOTREACHED */
1.1 kristaps 434: }
435:
1.23 kristaps 436: if ( ! mdoc_valid_pre(mdoc, p))
437: return(0);
438: if ( ! mdoc_action_pre(mdoc, p))
439: return(0);
1.27 kristaps 440:
441: switch (p->type) {
442: case (MDOC_HEAD):
443: assert(MDOC_BLOCK == p->parent->type);
444: p->parent->data.block.head = p;
445: break;
446: case (MDOC_TAIL):
447: assert(MDOC_BLOCK == p->parent->type);
448: p->parent->data.block.tail = p;
449: break;
450: case (MDOC_BODY):
451: assert(MDOC_BLOCK == p->parent->type);
452: p->parent->data.block.body = p;
453: break;
454: default:
455: break;
456: }
457:
1.1 kristaps 458: mdoc->last = p;
1.30 ! kristaps 459: mdoc_msg(mdoc, "parse: %s `%s' %s of %s `%s'",
! 460: nt, nn, act, ot, on);
1.23 kristaps 461: return(1);
1.1 kristaps 462: }
463:
464:
1.23 kristaps 465: int
1.22 kristaps 466: mdoc_tail_alloc(struct mdoc *mdoc, int line, int pos, int tok)
1.17 kristaps 467: {
468: struct mdoc_node *p;
469:
470: assert(mdoc->first);
471: assert(mdoc->last);
472:
473: p = xcalloc(1, sizeof(struct mdoc_node));
474:
1.22 kristaps 475: p->line = line;
476: p->pos = pos;
1.17 kristaps 477: p->type = MDOC_TAIL;
1.27 kristaps 478: p->tok = tok;
1.17 kristaps 479:
1.23 kristaps 480: return(mdoc_node_append(mdoc, p));
1.17 kristaps 481: }
482:
483:
1.23 kristaps 484: int
1.22 kristaps 485: mdoc_head_alloc(struct mdoc *mdoc, int line, int pos, int tok)
1.1 kristaps 486: {
487: struct mdoc_node *p;
488:
489: assert(mdoc->first);
490: assert(mdoc->last);
491:
492: p = xcalloc(1, sizeof(struct mdoc_node));
1.13 kristaps 493:
1.22 kristaps 494: p->line = line;
495: p->pos = pos;
1.1 kristaps 496: p->type = MDOC_HEAD;
1.27 kristaps 497: p->tok = tok;
1.1 kristaps 498:
1.23 kristaps 499: return(mdoc_node_append(mdoc, p));
1.1 kristaps 500: }
501:
502:
1.23 kristaps 503: int
1.22 kristaps 504: mdoc_body_alloc(struct mdoc *mdoc, int line, int pos, int tok)
1.1 kristaps 505: {
506: struct mdoc_node *p;
507:
508: assert(mdoc->first);
509: assert(mdoc->last);
510:
511: p = xcalloc(1, sizeof(struct mdoc_node));
512:
1.22 kristaps 513: p->line = line;
514: p->pos = pos;
1.1 kristaps 515: p->type = MDOC_BODY;
1.27 kristaps 516: p->tok = tok;
1.1 kristaps 517:
1.23 kristaps 518: return(mdoc_node_append(mdoc, p));
1.1 kristaps 519: }
520:
521:
1.23 kristaps 522: int
1.25 kristaps 523: mdoc_root_alloc(struct mdoc *mdoc)
524: {
525: struct mdoc_node *p;
526:
527: p = xcalloc(1, sizeof(struct mdoc_node));
528:
529: p->type = MDOC_ROOT;
530:
531: return(mdoc_node_append(mdoc, p));
532: }
533:
534:
535: int
1.22 kristaps 536: mdoc_block_alloc(struct mdoc *mdoc, int line, int pos,
537: int tok, size_t argsz, const struct mdoc_arg *args)
1.1 kristaps 538: {
539: struct mdoc_node *p;
540:
541: p = xcalloc(1, sizeof(struct mdoc_node));
542:
1.22 kristaps 543: p->pos = pos;
544: p->line = line;
1.1 kristaps 545: p->type = MDOC_BLOCK;
1.27 kristaps 546: p->tok = tok;
1.1 kristaps 547: p->data.block.argc = argsz;
548: p->data.block.argv = argdup(argsz, args);
549:
1.23 kristaps 550: return(mdoc_node_append(mdoc, p));
1.1 kristaps 551: }
552:
553:
1.23 kristaps 554: int
1.22 kristaps 555: mdoc_elem_alloc(struct mdoc *mdoc, int line, int pos,
556: int tok, size_t argsz, const struct mdoc_arg *args)
1.1 kristaps 557: {
558: struct mdoc_node *p;
559:
560: p = xcalloc(1, sizeof(struct mdoc_node));
1.22 kristaps 561:
562: p->line = line;
563: p->pos = pos;
1.1 kristaps 564: p->type = MDOC_ELEM;
1.27 kristaps 565: p->tok = tok;
1.1 kristaps 566: p->data.elem.argc = argsz;
567: p->data.elem.argv = argdup(argsz, args);
568:
1.23 kristaps 569: return(mdoc_node_append(mdoc, p));
1.1 kristaps 570: }
571:
572:
1.23 kristaps 573: int
1.22 kristaps 574: mdoc_word_alloc(struct mdoc *mdoc,
575: int line, int pos, const char *word)
1.1 kristaps 576: {
577: struct mdoc_node *p;
578:
579: p = xcalloc(1, sizeof(struct mdoc_node));
1.22 kristaps 580: p->line = line;
581: p->pos = pos;
1.1 kristaps 582: p->type = MDOC_TEXT;
583: p->data.text.string = xstrdup(word);
584:
1.23 kristaps 585: return(mdoc_node_append(mdoc, p));
1.1 kristaps 586: }
587:
588:
589: static void
590: argfree(size_t sz, struct mdoc_arg *p)
591: {
1.2 kristaps 592: int i, j;
1.1 kristaps 593:
594: if (0 == sz)
595: return;
596:
597: assert(p);
1.2 kristaps 598: /* LINTED */
599: for (i = 0; i < (int)sz; i++)
1.1 kristaps 600: if (p[i].sz > 0) {
601: assert(p[i].value);
1.2 kristaps 602: /* LINTED */
603: for (j = 0; j < (int)p[i].sz; j++)
1.1 kristaps 604: free(p[i].value[j]);
1.19 kristaps 605: free(p[i].value);
1.1 kristaps 606: }
607: free(p);
608: }
609:
610:
611: static void
612: mdoc_elem_free(struct mdoc_elem *p)
613: {
614:
615: argfree(p->argc, p->argv);
616: }
617:
618:
619: static void
620: mdoc_block_free(struct mdoc_block *p)
621: {
622:
623: argfree(p->argc, p->argv);
624: }
625:
626:
627: static void
628: mdoc_text_free(struct mdoc_text *p)
629: {
630:
631: if (p->string)
632: free(p->string);
633: }
634:
635:
636: void
637: mdoc_node_free(struct mdoc_node *p)
638: {
639:
640: switch (p->type) {
641: case (MDOC_TEXT):
642: mdoc_text_free(&p->data.text);
643: break;
644: case (MDOC_ELEM):
645: mdoc_elem_free(&p->data.elem);
646: break;
647: case (MDOC_BLOCK):
648: mdoc_block_free(&p->data.block);
649: break;
650: default:
651: break;
652: }
653:
654: free(p);
655: }
656:
657:
658: static void
659: mdoc_node_freelist(struct mdoc_node *p)
660: {
661:
662: if (p->child)
663: mdoc_node_freelist(p->child);
664: if (p->next)
665: mdoc_node_freelist(p->next);
666:
667: mdoc_node_free(p);
668: }
669:
670:
671: int
672: mdoc_find(const struct mdoc *mdoc, const char *key)
673: {
674:
1.4 kristaps 675: return(mdoc_tokhash_find(mdoc->htab, key));
1.1 kristaps 676: }
677:
678:
679: static void
680: argcpy(struct mdoc_arg *dst, const struct mdoc_arg *src)
681: {
1.2 kristaps 682: int i;
1.1 kristaps 683:
1.26 kristaps 684: dst->line = src->line;
685: dst->pos = src->pos;
1.1 kristaps 686: dst->arg = src->arg;
687: if (0 == (dst->sz = src->sz))
688: return;
689: dst->value = xcalloc(dst->sz, sizeof(char *));
1.2 kristaps 690: for (i = 0; i < (int)dst->sz; i++)
1.1 kristaps 691: dst->value[i] = xstrdup(src->value[i]);
692: }
693:
694:
695: static struct mdoc_arg *
696: argdup(size_t argsz, const struct mdoc_arg *args)
697: {
698: struct mdoc_arg *pp;
1.2 kristaps 699: int i;
1.1 kristaps 700:
701: if (0 == argsz)
702: return(NULL);
703:
704: pp = xcalloc((size_t)argsz, sizeof(struct mdoc_arg));
1.2 kristaps 705: for (i = 0; i < (int)argsz; i++)
1.1 kristaps 706: argcpy(&pp[i], &args[i]);
707:
708: return(pp);
709: }
710:
CVSweb