Annotation of mandoc/mdoc.c, Revision 1.45
1.45 ! kristaps 1: /* $Id: mdoc.c,v 1.44 2009/01/21 11:35:26 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:
1.41 kristaps 29: /*
30: * Main caller in the libmdoc library. This begins the parsing routine,
31: * handles allocation of data, and so forth. Most of the "work" is done
32: * in macro.c, but this orchestrates who does what, when.
33: */
34:
1.1 kristaps 35: const char *const __mdoc_macronames[MDOC_MAX] = {
36: "\\\"", "Dd", "Dt", "Os",
37: "Sh", "Ss", "Pp", "D1",
38: "Dl", "Bd", "Ed", "Bl",
39: "El", "It", "Ad", "An",
40: "Ar", "Cd", "Cm", "Dv",
41: "Er", "Ev", "Ex", "Fa",
42: "Fd", "Fl", "Fn", "Ft",
43: "Ic", "In", "Li", "Nd",
44: "Nm", "Op", "Ot", "Pa",
45: "Rv", "St", "Va", "Vt",
46: /* LINTED */
47: "Xr", "\%A", "\%B", "\%D",
48: /* LINTED */
49: "\%I", "\%J", "\%N", "\%O",
50: /* LINTED */
51: "\%P", "\%R", "\%T", "\%V",
52: "Ac", "Ao", "Aq", "At",
53: "Bc", "Bf", "Bo", "Bq",
54: "Bsx", "Bx", "Db", "Dc",
55: "Do", "Dq", "Ec", "Ef",
56: "Em", "Eo", "Fx", "Ms",
57: "No", "Ns", "Nx", "Ox",
58: "Pc", "Pf", "Po", "Pq",
59: "Qc", "Ql", "Qo", "Qq",
60: "Re", "Rs", "Sc", "So",
61: "Sq", "Sm", "Sx", "Sy",
62: "Tn", "Ux", "Xc", "Xo",
63: "Fo", "Fc", "Oo", "Oc",
64: "Bk", "Ek", "Bt", "Hf",
65: "Fr", "Ud",
66: };
67:
68: const char *const __mdoc_argnames[MDOC_ARG_MAX] = {
69: "split", "nosplit", "ragged",
70: "unfilled", "literal", "file",
71: "offset", "bullet", "dash",
72: "hyphen", "item", "enum",
73: "tag", "diag", "hang",
74: "ohang", "inset", "column",
75: "width", "compact", "std",
76: "p1003.1-88", "p1003.1-90", "p1003.1-96",
77: "p1003.1-2001", "p1003.1-2004", "p1003.1",
78: "p1003.1b", "p1003.1b-93", "p1003.1c-95",
79: "p1003.1g-2000", "p1003.2-92", "p1387.2-95",
80: "p1003.2", "p1387.2", "isoC-90",
81: "isoC-amd1", "isoC-tcor1", "isoC-tcor2",
82: "isoC-99", "ansiC", "ansiC-89",
83: "ansiC-99", "ieee754", "iso8802-3",
84: "xpg3", "xpg4", "xpg4.2",
85: "xpg4.3", "xbd5", "xcu5",
86: "xsh5", "xns5", "xns5.2d2.0",
87: "xcurses4.2", "susv2", "susv3",
88: "svid4", "filled", "words",
1.14 kristaps 89: "emphasis", "symbolic",
1.1 kristaps 90: };
91:
1.41 kristaps 92: /* Central table of library: who gets parsed how. */
93:
1.1 kristaps 94: const struct mdoc_macro __mdoc_macros[MDOC_MAX] = {
95: { NULL, 0 }, /* \" */
1.39 kristaps 96: { macro_constant, MDOC_PROLOGUE }, /* Dd */
97: { macro_constant, MDOC_PROLOGUE }, /* Dt */
98: { macro_constant, MDOC_PROLOGUE }, /* Os */
1.13 kristaps 99: { macro_scoped, 0 }, /* Sh */
100: { macro_scoped, 0 }, /* Ss */
1.5 kristaps 101: { macro_text, 0 }, /* Pp */
1.19 kristaps 102: { macro_scoped_line, MDOC_PARSED }, /* D1 */
103: { macro_scoped_line, MDOC_PARSED }, /* Dl */
1.13 kristaps 104: { macro_scoped, MDOC_EXPLICIT }, /* Bd */
1.29 kristaps 105: { macro_scoped_close, MDOC_EXPLICIT }, /* Ed */
1.13 kristaps 106: { macro_scoped, MDOC_EXPLICIT }, /* Bl */
1.29 kristaps 107: { macro_scoped_close, MDOC_EXPLICIT }, /* El */
1.43 kristaps 108: { macro_scoped, MDOC_PARSED }, /* It */
1.19 kristaps 109: { macro_text, MDOC_CALLABLE | MDOC_PARSED }, /* Ad */
1.32 kristaps 110: { macro_text, MDOC_PARSED }, /* An */
1.19 kristaps 111: { macro_text, MDOC_CALLABLE | MDOC_PARSED }, /* Ar */
1.43 kristaps 112: { macro_constant, 0 }, /* Cd */
1.19 kristaps 113: { macro_text, MDOC_CALLABLE | MDOC_PARSED }, /* Cm */
114: { macro_text, MDOC_CALLABLE | MDOC_PARSED }, /* Dv */
115: { macro_text, MDOC_CALLABLE | MDOC_PARSED }, /* Er */
116: { macro_text, MDOC_CALLABLE | MDOC_PARSED }, /* Ev */
1.13 kristaps 117: { macro_constant, 0 }, /* Ex */
1.43 kristaps 118: { macro_text, MDOC_CALLABLE | MDOC_PARSED }, /* Fa */
1.13 kristaps 119: { macro_constant, 0 }, /* Fd */
1.19 kristaps 120: { macro_text, MDOC_CALLABLE | MDOC_PARSED }, /* Fl */
1.43 kristaps 121: { macro_text, MDOC_CALLABLE | MDOC_PARSED }, /* Fn */
122: { macro_text, MDOC_PARSED }, /* Ft */
1.19 kristaps 123: { macro_text, MDOC_CALLABLE | MDOC_PARSED }, /* Ic */
1.13 kristaps 124: { macro_constant, 0 }, /* In */
1.19 kristaps 125: { macro_text, MDOC_CALLABLE | MDOC_PARSED }, /* Li */
1.10 kristaps 126: { macro_constant, 0 }, /* Nd */
1.19 kristaps 127: { macro_text, MDOC_CALLABLE | MDOC_PARSED }, /* Nm */
128: { macro_scoped_line, MDOC_CALLABLE | MDOC_PARSED }, /* Op */
1.13 kristaps 129: { macro_obsolete, 0 }, /* Ot */
1.19 kristaps 130: { macro_text, MDOC_CALLABLE | MDOC_PARSED }, /* Pa */
1.13 kristaps 131: { macro_constant, 0 }, /* Rv */
1.22 kristaps 132: /* XXX - .St supposed to be (but isn't) callable. */
1.19 kristaps 133: { macro_constant_delimited, MDOC_PARSED }, /* St */
134: { macro_text, MDOC_CALLABLE | MDOC_PARSED }, /* Va */
135: { macro_text, MDOC_CALLABLE | MDOC_PARSED }, /* Vt */
136: { macro_text, MDOC_CALLABLE | MDOC_PARSED }, /* Xr */
1.43 kristaps 137: { macro_constant, 0 }, /* %A */
138: { macro_constant, 0 }, /* %B */
139: { macro_constant, 0 }, /* %D */
140: { macro_constant, 0 }, /* %I */
141: { macro_constant, 0 }, /* %J */
142: { macro_constant, 0 }, /* %N */
143: { macro_constant, 0 }, /* %O */
144: { macro_constant, 0 }, /* %P */
145: { macro_constant, 0 }, /* %R */
146: { macro_constant, 0 }, /* %T */
147: { macro_constant, 0 }, /* %V */
1.29 kristaps 148: { macro_scoped_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Ac */
149: { macro_constant_scoped, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Ao */
1.19 kristaps 150: { macro_scoped_line, MDOC_CALLABLE | MDOC_PARSED }, /* Aq */
1.34 kristaps 151: { macro_constant_delimited, 0 }, /* At */
1.29 kristaps 152: { macro_scoped_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Bc */
1.16 kristaps 153: { macro_scoped, MDOC_EXPLICIT }, /* Bf */
1.29 kristaps 154: { macro_constant_scoped, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Bo */
1.19 kristaps 155: { macro_scoped_line, MDOC_CALLABLE | MDOC_PARSED }, /* Bq */
156: { macro_constant_delimited, MDOC_PARSED }, /* Bsx */
157: { macro_constant_delimited, MDOC_PARSED }, /* Bx */
1.13 kristaps 158: { macro_constant, 0 }, /* Db */
1.29 kristaps 159: { macro_scoped_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Dc */
160: { macro_constant_scoped, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Do */
1.19 kristaps 161: { macro_scoped_line, MDOC_CALLABLE | MDOC_PARSED }, /* Dq */
1.29 kristaps 162: { macro_scoped_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Ec */
163: { macro_scoped_close, MDOC_EXPLICIT }, /* Ef */
1.19 kristaps 164: { macro_text, MDOC_CALLABLE | MDOC_PARSED }, /* Em */
1.29 kristaps 165: { macro_constant_scoped, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Eo */
1.19 kristaps 166: { macro_constant_delimited, MDOC_PARSED }, /* Fx */
167: { macro_text, MDOC_PARSED }, /* Ms */
168: { macro_constant_delimited, MDOC_CALLABLE | MDOC_PARSED }, /* No */
169: { macro_constant_delimited, MDOC_CALLABLE | MDOC_PARSED }, /* Ns */
170: { macro_constant_delimited, MDOC_PARSED }, /* Nx */
171: { macro_constant_delimited, MDOC_PARSED }, /* Ox */
1.29 kristaps 172: { macro_scoped_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Pc */
1.32 kristaps 173: { macro_constant_delimited, MDOC_PARSED }, /* Pf */
1.29 kristaps 174: { macro_constant_scoped, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Po */
1.19 kristaps 175: { macro_scoped_line, MDOC_CALLABLE | MDOC_PARSED }, /* Pq */
1.29 kristaps 176: { macro_scoped_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Qc */
1.19 kristaps 177: { macro_scoped_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ql */
1.29 kristaps 178: { macro_constant_scoped, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Qo */
1.19 kristaps 179: { macro_scoped_line, MDOC_CALLABLE | MDOC_PARSED }, /* Qq */
1.29 kristaps 180: { macro_scoped_close, MDOC_EXPLICIT }, /* Re */
1.13 kristaps 181: { macro_scoped, MDOC_EXPLICIT }, /* Rs */
1.29 kristaps 182: { macro_scoped_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Sc */
183: { macro_constant_scoped, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* So */
1.19 kristaps 184: { macro_scoped_line, MDOC_CALLABLE | MDOC_PARSED }, /* Sq */
1.13 kristaps 185: { macro_constant, 0 }, /* Sm */
1.19 kristaps 186: { macro_text, MDOC_CALLABLE | MDOC_PARSED }, /* Sx */
187: { macro_text, MDOC_CALLABLE | MDOC_PARSED }, /* Sy */
188: { macro_text, MDOC_CALLABLE | MDOC_PARSED }, /* Tn */
189: { macro_constant_delimited, MDOC_PARSED }, /* Ux */
1.29 kristaps 190: { macro_scoped_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Xc */
191: { macro_constant_scoped, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Xo */
1.19 kristaps 192: /* XXX - .Fo supposed to be (but isn't) callable. */
1.43 kristaps 193: { macro_scoped, MDOC_EXPLICIT }, /* Fo */
1.19 kristaps 194: /* XXX - .Fc supposed to be (but isn't) callable. */
1.43 kristaps 195: { macro_scoped_close, MDOC_EXPLICIT }, /* Fc */
1.29 kristaps 196: { macro_constant_scoped, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Oo */
197: { macro_scoped_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Oc */
1.17 kristaps 198: { macro_scoped, MDOC_EXPLICIT }, /* Bk */
1.29 kristaps 199: { macro_scoped_close, MDOC_EXPLICIT }, /* Ek */
1.9 kristaps 200: { macro_constant, 0 }, /* Bt */
1.10 kristaps 201: { macro_constant, 0 }, /* Hf */
1.12 kristaps 202: { macro_obsolete, 0 }, /* Fr */
1.9 kristaps 203: { macro_constant, 0 }, /* Ud */
1.1 kristaps 204: };
205:
206: const char * const *mdoc_macronames = __mdoc_macronames;
207: const char * const *mdoc_argnames = __mdoc_argnames;
208: const struct mdoc_macro * const mdoc_macros = __mdoc_macros;
209:
210: static struct mdoc_arg *argdup(size_t, const struct mdoc_arg *);
211: static void argfree(size_t, struct mdoc_arg *);
212: static void argcpy(struct mdoc_arg *,
213: const struct mdoc_arg *);
214:
1.45 ! kristaps 215: static struct mdoc_node *mdoc_node_alloc(void);
1.23 kristaps 216: static int mdoc_node_append(struct mdoc *,
1.1 kristaps 217: struct mdoc_node *);
218: static void mdoc_elem_free(struct mdoc_elem *);
219: static void mdoc_text_free(struct mdoc_text *);
220:
221:
1.45 ! kristaps 222:
1.1 kristaps 223: const struct mdoc_node *
1.37 kristaps 224: mdoc_node(struct mdoc *mdoc)
1.1 kristaps 225: {
226:
227: return(mdoc->first);
228: }
229:
230:
1.37 kristaps 231: const struct mdoc_meta *
232: mdoc_meta(struct mdoc *mdoc)
233: {
234:
235: return(&mdoc->meta);
236: }
237:
238:
1.1 kristaps 239: void
1.38 kristaps 240: mdoc_free(struct mdoc *mdoc)
1.34 kristaps 241: {
242:
1.38 kristaps 243: if (mdoc->first)
244: mdoc_node_freelist(mdoc->first);
245: if (mdoc->htab)
246: mdoc_tokhash_free(mdoc->htab);
1.34 kristaps 247: if (mdoc->meta.title)
248: free(mdoc->meta.title);
249: if (mdoc->meta.os)
250: free(mdoc->meta.os);
251: if (mdoc->meta.name)
252: free(mdoc->meta.name);
253:
1.1 kristaps 254: free(mdoc);
255: }
256:
257:
258: struct mdoc *
259: mdoc_alloc(void *data, const struct mdoc_cb *cb)
260: {
261: struct mdoc *p;
262:
263: p = xcalloc(1, sizeof(struct mdoc));
264:
265: p->data = data;
1.33 kristaps 266: if (cb)
267: (void)memcpy(&p->cb, cb, sizeof(struct mdoc_cb));
1.1 kristaps 268:
1.25 kristaps 269: p->last = xcalloc(1, sizeof(struct mdoc_node));
270: p->last->type = MDOC_ROOT;
271: p->first = p->last;
272:
273: p->next = MDOC_NEXT_CHILD;
1.4 kristaps 274: p->htab = mdoc_tokhash_alloc();
1.25 kristaps 275:
1.1 kristaps 276: return(p);
277: }
278:
279:
280: int
1.20 kristaps 281: mdoc_endparse(struct mdoc *mdoc)
282: {
283:
284: if (MDOC_HALT & mdoc->flags)
285: return(0);
286: if (NULL == mdoc->first)
287: return(1);
288:
289: assert(mdoc->last);
290: if ( ! macro_end(mdoc)) {
291: mdoc->flags |= MDOC_HALT;
292: return(0);
293: }
294: return(1);
295: }
296:
297:
298: int
299: mdoc_parseln(struct mdoc *mdoc, int line, char *buf)
1.1 kristaps 300: {
301: int c, i;
302: char tmp[5];
303:
1.20 kristaps 304: if (MDOC_HALT & mdoc->flags)
305: return(0);
306:
1.39 kristaps 307: mdoc->linetok = 0;
308:
1.5 kristaps 309: if ('.' != *buf) {
1.44 kristaps 310: if (SEC_PROLOGUE == mdoc->lastnamed)
311: return(mdoc_perr(mdoc, line, 0, "text disallowed in document prologue"));
1.39 kristaps 312: if ( ! mdoc_word_alloc(mdoc, line, 0, buf))
313: return(0);
314: mdoc->next = MDOC_NEXT_SIBLING;
315: return(1);
1.1 kristaps 316: }
317:
318: if (buf[1] && '\\' == buf[1])
319: if (buf[2] && '\"' == buf[2])
320: return(1);
321:
322: i = 1;
1.42 kristaps 323: while (buf[i] && ! isspace((int)buf[i]) && i < (int)sizeof(tmp))
1.1 kristaps 324: i++;
325:
1.20 kristaps 326: if (i == (int)sizeof(tmp)) {
327: mdoc->flags |= MDOC_HALT;
1.31 kristaps 328: return(mdoc_perr(mdoc, line, 1, "unknown macro"));
1.20 kristaps 329: } else if (i <= 2) {
330: mdoc->flags |= MDOC_HALT;
1.31 kristaps 331: return(mdoc_perr(mdoc, line, 1, "unknown macro"));
1.20 kristaps 332: }
1.1 kristaps 333:
334: i--;
335:
1.2 kristaps 336: (void)memcpy(tmp, buf + 1, (size_t)i);
1.1 kristaps 337: tmp[i++] = 0;
338:
1.20 kristaps 339: if (MDOC_MAX == (c = mdoc_find(mdoc, tmp))) {
340: mdoc->flags |= MDOC_HALT;
1.31 kristaps 341: return(mdoc_perr(mdoc, line, 1, "unknown macro"));
1.20 kristaps 342: }
1.1 kristaps 343:
1.42 kristaps 344: while (buf[i] && isspace((int)buf[i]))
1.1 kristaps 345: i++;
346:
1.21 kristaps 347: if ( ! mdoc_macro(mdoc, c, line, 1, &i, buf)) {
1.20 kristaps 348: mdoc->flags |= MDOC_HALT;
349: return(0);
350: }
351: return(1);
1.1 kristaps 352: }
353:
354:
355: void
1.31 kristaps 356: mdoc_vmsg(struct mdoc *mdoc, int ln, int pos, const char *fmt, ...)
1.1 kristaps 357: {
1.31 kristaps 358: char buf[256];
1.23 kristaps 359: va_list ap;
1.1 kristaps 360:
361: if (NULL == mdoc->cb.mdoc_msg)
362: return;
363:
364: va_start(ap, fmt);
1.31 kristaps 365: (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
1.1 kristaps 366: va_end(ap);
1.31 kristaps 367: (*mdoc->cb.mdoc_msg)(mdoc->data, ln, pos, buf);
1.1 kristaps 368: }
369:
370:
371: int
1.31 kristaps 372: mdoc_verr(struct mdoc *mdoc, int ln, int pos,
373: const char *fmt, ...)
1.1 kristaps 374: {
1.31 kristaps 375: char buf[256];
376: va_list ap;
1.1 kristaps 377:
378: if (NULL == mdoc->cb.mdoc_err)
379: return(0);
1.31 kristaps 380:
381: va_start(ap, fmt);
382: (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
383: va_end(ap);
384: return((*mdoc->cb.mdoc_err)(mdoc->data, ln, pos, buf));
1.1 kristaps 385: }
386:
387:
388: int
1.31 kristaps 389: mdoc_vwarn(struct mdoc *mdoc, int ln, int pos,
390: enum mdoc_warn type, const char *fmt, ...)
1.1 kristaps 391: {
1.31 kristaps 392: char buf[256];
393: va_list ap;
1.1 kristaps 394:
395: if (NULL == mdoc->cb.mdoc_warn)
396: return(0);
1.31 kristaps 397:
398: va_start(ap, fmt);
399: (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
400: va_end(ap);
401: return((*mdoc->cb.mdoc_warn)(mdoc->data, ln, pos, type, buf));
1.1 kristaps 402: }
403:
404:
405: int
1.21 kristaps 406: mdoc_macro(struct mdoc *mdoc, int tok,
1.23 kristaps 407: int ln, int ppos, int *pos, char *buf)
1.1 kristaps 408: {
409:
1.31 kristaps 410: assert(mdoc_macros[tok].fp);
411:
1.44 kristaps 412: if (MDOC_PROLOGUE & mdoc_macros[tok].flags &&
413: SEC_PROLOGUE != mdoc->lastnamed)
414: return(mdoc_perr(mdoc, ln, ppos, "macro disallowed in document body"));
415: if ( ! (MDOC_PROLOGUE & mdoc_macros[tok].flags) &&
416: SEC_PROLOGUE == mdoc->lastnamed)
417: return(mdoc_perr(mdoc, ln, ppos, "macro disallowed in document prologue"));
1.23 kristaps 418: if (1 != ppos && ! (MDOC_CALLABLE & mdoc_macros[tok].flags))
1.31 kristaps 419: return(mdoc_perr(mdoc, ln, ppos, "macro not callable"));
1.23 kristaps 420: return((*mdoc_macros[tok].fp)(mdoc, tok, ln, ppos, pos, buf));
1.1 kristaps 421: }
422:
423:
1.23 kristaps 424: static int
425: mdoc_node_append(struct mdoc *mdoc, struct mdoc_node *p)
1.1 kristaps 426: {
1.30 kristaps 427: const char *nn, *nt, *on, *ot, *act;
1.1 kristaps 428:
1.25 kristaps 429: assert(mdoc->last);
430: assert(mdoc->first);
431: assert(MDOC_ROOT != p->type);
1.1 kristaps 432:
1.39 kristaps 433: /* See if we exceed the suggest line-max. */
434:
435: switch (p->type) {
436: case (MDOC_TEXT):
437: /* FALLTHROUGH */
438: case (MDOC_ELEM):
439: /* FALLTHROUGH */
440: case (MDOC_BLOCK):
441: mdoc->linetok++;
442: break;
443: default:
444: break;
445: }
446:
1.40 kristaps 447: /* This sort-of works (re-opening of text macros...). */
1.39 kristaps 448: if (mdoc->linetok > MDOC_LINEARG_SOFTMAX)
449: if ( ! mdoc_nwarn(mdoc, p, WARN_COMPAT,
450: "suggested %d tokens per line exceeded (has %d)",
451: MDOC_LINEARG_SOFTMAX, mdoc->linetok))
452: return(0);
453:
1.27 kristaps 454: if (MDOC_TEXT == mdoc->last->type)
455: on = "<text>";
456: else if (MDOC_ROOT == mdoc->last->type)
457: on = "<root>";
458: else
459: on = mdoc_macronames[mdoc->last->tok];
460:
1.30 kristaps 461: if (MDOC_TEXT == p->type)
462: nn = "<text>";
463: else if (MDOC_ROOT == p->type)
464: nn = "<root>";
465: else
466: nn = mdoc_macronames[p->tok];
467:
468: ot = mdoc_type2a(mdoc->last->type);
469: nt = mdoc_type2a(p->type);
1.1 kristaps 470:
1.13 kristaps 471: switch (mdoc->next) {
472: case (MDOC_NEXT_SIBLING):
1.6 kristaps 473: mdoc->last->next = p;
474: p->prev = mdoc->last;
1.13 kristaps 475: p->parent = mdoc->last->parent;
1.6 kristaps 476: act = "sibling";
1.1 kristaps 477: break;
1.13 kristaps 478: case (MDOC_NEXT_CHILD):
479: mdoc->last->child = p;
1.1 kristaps 480: p->parent = mdoc->last;
481: act = "child";
482: break;
483: default:
1.13 kristaps 484: abort();
485: /* NOTREACHED */
1.1 kristaps 486: }
487:
1.23 kristaps 488: if ( ! mdoc_valid_pre(mdoc, p))
489: return(0);
1.27 kristaps 490:
491: switch (p->type) {
492: case (MDOC_HEAD):
493: assert(MDOC_BLOCK == p->parent->type);
494: p->parent->data.block.head = p;
495: break;
496: case (MDOC_TAIL):
497: assert(MDOC_BLOCK == p->parent->type);
498: p->parent->data.block.tail = p;
499: break;
500: case (MDOC_BODY):
501: assert(MDOC_BLOCK == p->parent->type);
502: p->parent->data.block.body = p;
503: break;
504: default:
505: break;
506: }
507:
1.1 kristaps 508: mdoc->last = p;
1.30 kristaps 509: mdoc_msg(mdoc, "parse: %s `%s' %s of %s `%s'",
510: nt, nn, act, ot, on);
1.23 kristaps 511: return(1);
1.1 kristaps 512: }
513:
514:
1.45 ! kristaps 515: static struct mdoc_node *
! 516: mdoc_node_alloc(void)
! 517: {
! 518:
! 519: return(xcalloc(1, sizeof(struct mdoc_node)));
! 520: }
! 521:
! 522:
1.23 kristaps 523: int
1.22 kristaps 524: mdoc_tail_alloc(struct mdoc *mdoc, int line, int pos, int tok)
1.17 kristaps 525: {
526: struct mdoc_node *p;
527:
528: assert(mdoc->first);
529: assert(mdoc->last);
530:
1.45 ! kristaps 531: p = mdoc_node_alloc();
1.17 kristaps 532:
1.22 kristaps 533: p->line = line;
534: p->pos = pos;
1.17 kristaps 535: p->type = MDOC_TAIL;
1.27 kristaps 536: p->tok = tok;
1.17 kristaps 537:
1.23 kristaps 538: return(mdoc_node_append(mdoc, p));
1.17 kristaps 539: }
540:
541:
1.23 kristaps 542: int
1.22 kristaps 543: mdoc_head_alloc(struct mdoc *mdoc, int line, int pos, int tok)
1.1 kristaps 544: {
545: struct mdoc_node *p;
546:
547: assert(mdoc->first);
548: assert(mdoc->last);
549:
1.45 ! kristaps 550: p = mdoc_node_alloc();
1.13 kristaps 551:
1.22 kristaps 552: p->line = line;
553: p->pos = pos;
1.1 kristaps 554: p->type = MDOC_HEAD;
1.27 kristaps 555: p->tok = tok;
1.1 kristaps 556:
1.23 kristaps 557: return(mdoc_node_append(mdoc, p));
1.1 kristaps 558: }
559:
560:
1.23 kristaps 561: int
1.22 kristaps 562: mdoc_body_alloc(struct mdoc *mdoc, int line, int pos, int tok)
1.1 kristaps 563: {
564: struct mdoc_node *p;
565:
566: assert(mdoc->first);
567: assert(mdoc->last);
568:
1.45 ! kristaps 569: p = mdoc_node_alloc();
1.1 kristaps 570:
1.22 kristaps 571: p->line = line;
572: p->pos = pos;
1.1 kristaps 573: p->type = MDOC_BODY;
1.27 kristaps 574: p->tok = tok;
1.1 kristaps 575:
1.23 kristaps 576: return(mdoc_node_append(mdoc, p));
1.1 kristaps 577: }
578:
579:
1.23 kristaps 580: int
1.25 kristaps 581: mdoc_root_alloc(struct mdoc *mdoc)
582: {
583: struct mdoc_node *p;
584:
1.45 ! kristaps 585: p = mdoc_node_alloc();
1.25 kristaps 586:
587: p->type = MDOC_ROOT;
588:
589: return(mdoc_node_append(mdoc, p));
590: }
591:
592:
593: int
1.22 kristaps 594: mdoc_block_alloc(struct mdoc *mdoc, int line, int pos,
595: int tok, size_t argsz, const struct mdoc_arg *args)
1.1 kristaps 596: {
597: struct mdoc_node *p;
598:
1.45 ! kristaps 599: p = mdoc_node_alloc();
1.1 kristaps 600:
1.22 kristaps 601: p->pos = pos;
602: p->line = line;
1.1 kristaps 603: p->type = MDOC_BLOCK;
1.27 kristaps 604: p->tok = tok;
1.1 kristaps 605: p->data.block.argc = argsz;
606: p->data.block.argv = argdup(argsz, args);
607:
1.23 kristaps 608: return(mdoc_node_append(mdoc, p));
1.1 kristaps 609: }
610:
611:
1.23 kristaps 612: int
1.22 kristaps 613: mdoc_elem_alloc(struct mdoc *mdoc, int line, int pos,
614: int tok, size_t argsz, const struct mdoc_arg *args)
1.1 kristaps 615: {
616: struct mdoc_node *p;
617:
1.45 ! kristaps 618: p = mdoc_node_alloc();
1.22 kristaps 619:
620: p->line = line;
621: p->pos = pos;
1.1 kristaps 622: p->type = MDOC_ELEM;
1.27 kristaps 623: p->tok = tok;
1.1 kristaps 624: p->data.elem.argc = argsz;
625: p->data.elem.argv = argdup(argsz, args);
626:
1.23 kristaps 627: return(mdoc_node_append(mdoc, p));
1.1 kristaps 628: }
629:
630:
1.23 kristaps 631: int
1.22 kristaps 632: mdoc_word_alloc(struct mdoc *mdoc,
633: int line, int pos, const char *word)
1.1 kristaps 634: {
635: struct mdoc_node *p;
636:
1.45 ! kristaps 637: p = mdoc_node_alloc();
! 638:
1.22 kristaps 639: p->line = line;
640: p->pos = pos;
1.1 kristaps 641: p->type = MDOC_TEXT;
642: p->data.text.string = xstrdup(word);
643:
1.23 kristaps 644: return(mdoc_node_append(mdoc, p));
1.1 kristaps 645: }
646:
647:
648: static void
649: argfree(size_t sz, struct mdoc_arg *p)
650: {
1.2 kristaps 651: int i, j;
1.1 kristaps 652:
653: if (0 == sz)
654: return;
655:
656: assert(p);
1.2 kristaps 657: /* LINTED */
658: for (i = 0; i < (int)sz; i++)
1.1 kristaps 659: if (p[i].sz > 0) {
660: assert(p[i].value);
1.2 kristaps 661: /* LINTED */
662: for (j = 0; j < (int)p[i].sz; j++)
1.1 kristaps 663: free(p[i].value[j]);
1.19 kristaps 664: free(p[i].value);
1.1 kristaps 665: }
666: free(p);
667: }
668:
669:
670: static void
671: mdoc_elem_free(struct mdoc_elem *p)
672: {
673:
674: argfree(p->argc, p->argv);
675: }
676:
677:
678: static void
679: mdoc_block_free(struct mdoc_block *p)
680: {
681:
682: argfree(p->argc, p->argv);
683: }
684:
685:
686: static void
687: mdoc_text_free(struct mdoc_text *p)
688: {
689:
690: if (p->string)
691: free(p->string);
692: }
693:
694:
695: void
696: mdoc_node_free(struct mdoc_node *p)
697: {
698:
699: switch (p->type) {
700: case (MDOC_TEXT):
701: mdoc_text_free(&p->data.text);
702: break;
703: case (MDOC_ELEM):
704: mdoc_elem_free(&p->data.elem);
705: break;
706: case (MDOC_BLOCK):
707: mdoc_block_free(&p->data.block);
708: break;
709: default:
710: break;
711: }
712:
713: free(p);
714: }
715:
716:
1.37 kristaps 717: void
1.1 kristaps 718: mdoc_node_freelist(struct mdoc_node *p)
719: {
720:
721: if (p->child)
722: mdoc_node_freelist(p->child);
723: if (p->next)
724: mdoc_node_freelist(p->next);
725:
726: mdoc_node_free(p);
727: }
728:
729:
730: int
731: mdoc_find(const struct mdoc *mdoc, const char *key)
732: {
733:
1.4 kristaps 734: return(mdoc_tokhash_find(mdoc->htab, key));
1.1 kristaps 735: }
736:
737:
738: static void
739: argcpy(struct mdoc_arg *dst, const struct mdoc_arg *src)
740: {
1.2 kristaps 741: int i;
1.1 kristaps 742:
1.26 kristaps 743: dst->line = src->line;
744: dst->pos = src->pos;
1.1 kristaps 745: dst->arg = src->arg;
746: if (0 == (dst->sz = src->sz))
747: return;
748: dst->value = xcalloc(dst->sz, sizeof(char *));
1.2 kristaps 749: for (i = 0; i < (int)dst->sz; i++)
1.1 kristaps 750: dst->value[i] = xstrdup(src->value[i]);
751: }
752:
753:
754: static struct mdoc_arg *
755: argdup(size_t argsz, const struct mdoc_arg *args)
756: {
757: struct mdoc_arg *pp;
1.2 kristaps 758: int i;
1.1 kristaps 759:
760: if (0 == argsz)
761: return(NULL);
762:
763: pp = xcalloc((size_t)argsz, sizeof(struct mdoc_arg));
1.2 kristaps 764: for (i = 0; i < (int)argsz; i++)
1.1 kristaps 765: argcpy(&pp[i], &args[i]);
766:
767: return(pp);
768: }
769:
1.35 kristaps 770:
1.36 kristaps 771: /* FIXME: deprecate. */
1.35 kristaps 772: char *
773: mdoc_node2a(struct mdoc_node *node)
774: {
775: static char buf[64];
776:
777: assert(node);
778:
779: buf[0] = 0;
780: (void)xstrlcat(buf, mdoc_type2a(node->type), 64);
781: if (MDOC_ROOT == node->type)
782: return(buf);
783: (void)xstrlcat(buf, " `", 64);
784: if (MDOC_TEXT == node->type)
785: (void)xstrlcat(buf, node->data.text.string, 64);
786: else
787: (void)xstrlcat(buf, mdoc_macronames[node->tok], 64);
1.36 kristaps 788: (void)xstrlcat(buf, "'", 64);
1.35 kristaps 789:
790: return(buf);
791: }
792:
793:
CVSweb