Annotation of src/usr.bin/mandoc/mdoc.c, Revision 1.75
1.75 ! schwarze 1: /* $Id: mdoc.c,v 1.74 2010/12/26 21:04:19 schwarze Exp $ */
1.1 kristaps 2: /*
1.61 schwarze 3: * Copyright (c) 2008, 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
4: * Copyright (c) 2010 Ingo Schwarze <schwarze@openbsd.org>
1.1 kristaps 5: *
6: * Permission to use, copy, modify, and distribute this software for any
1.3 schwarze 7: * purpose with or without fee is hereby granted, provided that the above
8: * copyright notice and this permission notice appear in all copies.
1.1 kristaps 9: *
1.3 schwarze 10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1 kristaps 17: */
1.28 schwarze 18: #include <sys/types.h>
19:
1.1 kristaps 20: #include <assert.h>
21: #include <stdarg.h>
22: #include <stdio.h>
23: #include <stdlib.h>
24: #include <string.h>
1.39 schwarze 25: #include <time.h>
1.1 kristaps 26:
1.54 schwarze 27: #include "mandoc.h"
1.1 kristaps 28: #include "libmdoc.h"
1.32 schwarze 29: #include "libmandoc.h"
1.1 kristaps 30:
1.67 schwarze 31: #include "out.h"
32: #include "term.h"
33: #include "tbl.h"
34:
1.1 kristaps 35: const char *const __mdoc_macronames[MDOC_MAX] = {
1.7 schwarze 36: "Ap", "Dd", "Dt", "Os",
1.1 kristaps 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 */
1.33 schwarze 47: "Xr", "%A", "%B", "%D",
1.1 kristaps 48: /* LINTED */
1.33 schwarze 49: "%I", "%J", "%N", "%O",
1.1 kristaps 50: /* LINTED */
1.33 schwarze 51: "%P", "%R", "%T", "%V",
1.1 kristaps 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",
1.7 schwarze 65: "Fr", "Ud", "Lb", "Lp",
66: "Lk", "Mt", "Brq", "Bro",
1.1 kristaps 67: /* LINTED */
1.33 schwarze 68: "Brc", "%C", "Es", "En",
1.1 kristaps 69: /* LINTED */
1.33 schwarze 70: "Dx", "%Q", "br", "sp",
1.31 schwarze 71: /* LINTED */
1.67 schwarze 72: "%U", "Ta", "TS", "TE"
1.1 kristaps 73: };
74:
75: const char *const __mdoc_argnames[MDOC_ARG_MAX] = {
76: "split", "nosplit", "ragged",
77: "unfilled", "literal", "file",
78: "offset", "bullet", "dash",
79: "hyphen", "item", "enum",
80: "tag", "diag", "hang",
81: "ohang", "inset", "column",
82: "width", "compact", "std",
83: "filled", "words", "emphasis",
1.30 schwarze 84: "symbolic", "nested", "centered"
1.1 kristaps 85: };
86:
87: const char * const *mdoc_macronames = __mdoc_macronames;
88: const char * const *mdoc_argnames = __mdoc_argnames;
89:
1.40 schwarze 90: static void mdoc_node_free(struct mdoc_node *);
91: static void mdoc_node_unlink(struct mdoc *,
92: struct mdoc_node *);
1.1 kristaps 93: static void mdoc_free1(struct mdoc *);
1.32 schwarze 94: static void mdoc_alloc1(struct mdoc *);
1.1 kristaps 95: static struct mdoc_node *node_alloc(struct mdoc *, int, int,
1.37 schwarze 96: enum mdoct, enum mdoc_type);
1.1 kristaps 97: static int node_append(struct mdoc *,
98: struct mdoc_node *);
1.53 schwarze 99: static int mdoc_ptext(struct mdoc *, int, char *, int);
100: static int mdoc_pmacro(struct mdoc *, int, char *, int);
1.46 schwarze 101:
1.1 kristaps 102:
103: const struct mdoc_node *
104: mdoc_node(const struct mdoc *m)
105: {
106:
107: return(MDOC_HALT & m->flags ? NULL : m->first);
108: }
109:
110:
111: const struct mdoc_meta *
112: mdoc_meta(const struct mdoc *m)
113: {
114:
115: return(MDOC_HALT & m->flags ? NULL : &m->meta);
116: }
117:
118:
1.8 schwarze 119: /*
120: * Frees volatile resources (parse tree, meta-data, fields).
121: */
1.1 kristaps 122: static void
123: mdoc_free1(struct mdoc *mdoc)
124: {
125:
126: if (mdoc->first)
1.40 schwarze 127: mdoc_node_delete(mdoc, mdoc->first);
1.1 kristaps 128: if (mdoc->meta.title)
129: free(mdoc->meta.title);
130: if (mdoc->meta.os)
131: free(mdoc->meta.os);
132: if (mdoc->meta.name)
133: free(mdoc->meta.name);
134: if (mdoc->meta.arch)
135: free(mdoc->meta.arch);
136: if (mdoc->meta.vol)
137: free(mdoc->meta.vol);
1.50 schwarze 138: if (mdoc->meta.msec)
139: free(mdoc->meta.msec);
1.1 kristaps 140: }
141:
142:
1.8 schwarze 143: /*
144: * Allocate all volatile resources (parse tree, meta-data, fields).
145: */
1.32 schwarze 146: static void
1.1 kristaps 147: mdoc_alloc1(struct mdoc *mdoc)
148: {
149:
1.32 schwarze 150: memset(&mdoc->meta, 0, sizeof(struct mdoc_meta));
1.1 kristaps 151: mdoc->flags = 0;
1.9 schwarze 152: mdoc->lastnamed = mdoc->lastsec = SEC_NONE;
1.32 schwarze 153: mdoc->last = mandoc_calloc(1, sizeof(struct mdoc_node));
1.1 kristaps 154: mdoc->first = mdoc->last;
155: mdoc->last->type = MDOC_ROOT;
156: mdoc->next = MDOC_NEXT_CHILD;
157: }
158:
159:
160: /*
1.8 schwarze 161: * Free up volatile resources (see mdoc_free1()) then re-initialises the
162: * data with mdoc_alloc1(). After invocation, parse data has been reset
163: * and the parser is ready for re-invocation on a new tree; however,
164: * cross-parse non-volatile data is kept intact.
1.1 kristaps 165: */
1.32 schwarze 166: void
1.1 kristaps 167: mdoc_reset(struct mdoc *mdoc)
168: {
169:
170: mdoc_free1(mdoc);
1.32 schwarze 171: mdoc_alloc1(mdoc);
1.1 kristaps 172: }
173:
174:
175: /*
1.8 schwarze 176: * Completely free up all volatile and non-volatile parse resources.
177: * After invocation, the pointer is no longer usable.
1.1 kristaps 178: */
179: void
180: mdoc_free(struct mdoc *mdoc)
181: {
182:
183: mdoc_free1(mdoc);
184: free(mdoc);
185: }
186:
187:
1.8 schwarze 188: /*
189: * Allocate volatile and non-volatile parse resources.
190: */
1.1 kristaps 191: struct mdoc *
1.65 schwarze 192: mdoc_alloc(struct regset *regs, void *data, mandocmsg msg)
1.1 kristaps 193: {
194: struct mdoc *p;
195:
1.32 schwarze 196: p = mandoc_calloc(1, sizeof(struct mdoc));
197:
1.54 schwarze 198: p->msg = msg;
1.1 kristaps 199: p->data = data;
1.58 schwarze 200: p->regs = regs;
1.1 kristaps 201:
1.32 schwarze 202: mdoc_hash_init();
203: mdoc_alloc1(p);
204: return(p);
1.1 kristaps 205: }
206:
207:
208: /*
209: * Climb back up the parse tree, validating open scopes. Mostly calls
1.8 schwarze 210: * through to macro_end() in macro.c.
1.1 kristaps 211: */
212: int
213: mdoc_endparse(struct mdoc *m)
214: {
215:
216: if (MDOC_HALT & m->flags)
217: return(0);
218: else if (mdoc_macroend(m))
219: return(1);
220: m->flags |= MDOC_HALT;
221: return(0);
222: }
223:
224:
225: /*
226: * Main parse routine. Parses a single line -- really just hands off to
1.45 schwarze 227: * the macro (mdoc_pmacro()) or text parser (mdoc_ptext()).
1.1 kristaps 228: */
229: int
1.53 schwarze 230: mdoc_parseln(struct mdoc *m, int ln, char *buf, int offs)
1.1 kristaps 231: {
1.67 schwarze 232: struct mdoc_node *n;
1.1 kristaps 233:
234: if (MDOC_HALT & m->flags)
235: return(0);
236:
1.47 schwarze 237: m->flags |= MDOC_NEWLINE;
1.60 schwarze 238:
1.67 schwarze 239: n = m->last;
240:
241: if (n && MDOC_TS == n->tok && MDOC_BODY == n->type &&
242: strncmp(buf+offs, ".TE", 3)) {
243: n = n->parent;
1.75 ! schwarze 244: if ( ! tbl_read(n->norm->TS, "mdoc tbl parser",
1.68 schwarze 245: ln, buf+offs, strlen(buf+offs)))
246: mdoc_nmsg(m, n, MANDOCERR_TBL);
247: return(1);
1.67 schwarze 248: }
249:
1.60 schwarze 250: /*
251: * Let the roff nS register switch SYNOPSIS mode early,
252: * such that the parser knows at all times
253: * whether this mode is on or off.
254: * Note that this mode is also switched by the Sh macro.
255: */
256: if (m->regs->regs[(int)REG_nS].set) {
257: if (m->regs->regs[(int)REG_nS].v.u)
258: m->flags |= MDOC_SYNOPSIS;
259: else
260: m->flags &= ~MDOC_SYNOPSIS;
261: }
262:
1.53 schwarze 263: return(('.' == buf[offs] || '\'' == buf[offs]) ?
264: mdoc_pmacro(m, ln, buf, offs) :
265: mdoc_ptext(m, ln, buf, offs));
1.1 kristaps 266: }
267:
268:
269: int
1.54 schwarze 270: mdoc_vmsg(struct mdoc *mdoc, enum mandocerr t,
271: int ln, int pos, const char *fmt, ...)
1.1 kristaps 272: {
273: char buf[256];
274: va_list ap;
275:
276: va_start(ap, fmt);
1.54 schwarze 277: vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
1.1 kristaps 278: va_end(ap);
1.17 schwarze 279:
1.54 schwarze 280: return((*mdoc->msg)(t, mdoc->data, ln, pos, buf));
1.5 schwarze 281: }
282:
283:
284: int
1.58 schwarze 285: mdoc_macro(MACRO_PROT_ARGS)
1.1 kristaps 286: {
1.46 schwarze 287: assert(tok < MDOC_MAX);
288:
289: /* If we're in the body, deny prologue calls. */
1.37 schwarze 290:
1.1 kristaps 291: if (MDOC_PROLOGUE & mdoc_macros[tok].flags &&
1.9 schwarze 292: MDOC_PBODY & m->flags)
1.58 schwarze 293: return(mdoc_pmsg(m, line, ppos, MANDOCERR_BADBODY));
1.46 schwarze 294:
295: /* If we're in the prologue, deny "body" macros. */
296:
1.1 kristaps 297: if ( ! (MDOC_PROLOGUE & mdoc_macros[tok].flags) &&
1.39 schwarze 298: ! (MDOC_PBODY & m->flags)) {
1.58 schwarze 299: if ( ! mdoc_pmsg(m, line, ppos, MANDOCERR_BADPROLOG))
1.39 schwarze 300: return(0);
1.71 schwarze 301: if (NULL == m->meta.msec)
302: m->meta.msec = mandoc_strdup("1");
1.39 schwarze 303: if (NULL == m->meta.title)
1.56 schwarze 304: m->meta.title = mandoc_strdup("UNKNOWN");
1.39 schwarze 305: if (NULL == m->meta.vol)
1.56 schwarze 306: m->meta.vol = mandoc_strdup("LOCAL");
1.39 schwarze 307: if (NULL == m->meta.os)
1.56 schwarze 308: m->meta.os = mandoc_strdup("LOCAL");
1.39 schwarze 309: if (0 == m->meta.date)
310: m->meta.date = time(NULL);
311: m->flags |= MDOC_PBODY;
312: }
1.1 kristaps 313:
1.58 schwarze 314: return((*mdoc_macros[tok].fp)(m, tok, line, ppos, pos, buf));
1.1 kristaps 315: }
316:
317:
318: static int
319: node_append(struct mdoc *mdoc, struct mdoc_node *p)
320: {
321:
322: assert(mdoc->last);
323: assert(mdoc->first);
324: assert(MDOC_ROOT != p->type);
325:
326: switch (mdoc->next) {
327: case (MDOC_NEXT_SIBLING):
328: mdoc->last->next = p;
329: p->prev = mdoc->last;
330: p->parent = mdoc->last->parent;
331: break;
332: case (MDOC_NEXT_CHILD):
333: mdoc->last->child = p;
334: p->parent = mdoc->last;
335: break;
336: default:
337: abort();
338: /* NOTREACHED */
339: }
340:
1.10 schwarze 341: p->parent->nchild++;
342:
1.75 ! schwarze 343: /*
! 344: * Copy over the normalised-data pointer of our parent. Not
! 345: * everybody has one, but copying a null pointer is fine.
! 346: */
! 347:
! 348: switch (p->type) {
! 349: case (MDOC_BODY):
! 350: /* FALLTHROUGH */
! 351: case (MDOC_TAIL):
! 352: /* FALLTHROUGH */
! 353: case (MDOC_HEAD):
! 354: p->norm = p->parent->norm;
! 355: break;
! 356: default:
! 357: break;
! 358: }
! 359:
1.1 kristaps 360: if ( ! mdoc_valid_pre(mdoc, p))
361: return(0);
362:
363: switch (p->type) {
364: case (MDOC_HEAD):
365: assert(MDOC_BLOCK == p->parent->type);
366: p->parent->head = p;
367: break;
368: case (MDOC_TAIL):
369: assert(MDOC_BLOCK == p->parent->type);
370: p->parent->tail = p;
371: break;
372: case (MDOC_BODY):
1.59 schwarze 373: if (p->end)
374: break;
1.1 kristaps 375: assert(MDOC_BLOCK == p->parent->type);
376: p->parent->body = p;
377: break;
378: default:
379: break;
380: }
381:
382: mdoc->last = p;
383:
384: switch (p->type) {
385: case (MDOC_TEXT):
386: if ( ! mdoc_valid_post(mdoc))
387: return(0);
388: break;
389: default:
390: break;
391: }
392:
393: return(1);
394: }
395:
396:
397: static struct mdoc_node *
1.37 schwarze 398: node_alloc(struct mdoc *m, int line, int pos,
399: enum mdoct tok, enum mdoc_type type)
1.1 kristaps 400: {
401: struct mdoc_node *p;
402:
1.32 schwarze 403: p = mandoc_calloc(1, sizeof(struct mdoc_node));
1.19 schwarze 404: p->sec = m->lastsec;
1.1 kristaps 405: p->line = line;
406: p->pos = pos;
407: p->tok = tok;
1.37 schwarze 408: p->type = type;
1.58 schwarze 409:
410: /* Flag analysis. */
411:
1.60 schwarze 412: if (MDOC_SYNOPSIS & m->flags)
413: p->flags |= MDOC_SYNPRETTY;
414: else
415: p->flags &= ~MDOC_SYNPRETTY;
1.47 schwarze 416: if (MDOC_NEWLINE & m->flags)
417: p->flags |= MDOC_LINE;
418: m->flags &= ~MDOC_NEWLINE;
1.58 schwarze 419:
1.1 kristaps 420: return(p);
421: }
422:
423:
424: int
1.37 schwarze 425: mdoc_tail_alloc(struct mdoc *m, int line, int pos, enum mdoct tok)
1.1 kristaps 426: {
427: struct mdoc_node *p;
428:
1.19 schwarze 429: p = node_alloc(m, line, pos, tok, MDOC_TAIL);
1.25 schwarze 430: if ( ! node_append(m, p))
431: return(0);
432: m->next = MDOC_NEXT_CHILD;
433: return(1);
1.1 kristaps 434: }
435:
436:
437: int
1.37 schwarze 438: mdoc_head_alloc(struct mdoc *m, int line, int pos, enum mdoct tok)
1.1 kristaps 439: {
440: struct mdoc_node *p;
441:
1.19 schwarze 442: assert(m->first);
443: assert(m->last);
1.1 kristaps 444:
1.19 schwarze 445: p = node_alloc(m, line, pos, tok, MDOC_HEAD);
1.25 schwarze 446: if ( ! node_append(m, p))
447: return(0);
448: m->next = MDOC_NEXT_CHILD;
449: return(1);
1.1 kristaps 450: }
451:
452:
453: int
1.37 schwarze 454: mdoc_body_alloc(struct mdoc *m, int line, int pos, enum mdoct tok)
1.1 kristaps 455: {
456: struct mdoc_node *p;
457:
1.19 schwarze 458: p = node_alloc(m, line, pos, tok, MDOC_BODY);
1.25 schwarze 459: if ( ! node_append(m, p))
460: return(0);
461: m->next = MDOC_NEXT_CHILD;
1.59 schwarze 462: return(1);
463: }
464:
465:
466: int
467: mdoc_endbody_alloc(struct mdoc *m, int line, int pos, enum mdoct tok,
468: struct mdoc_node *body, enum mdoc_endbody end)
469: {
470: struct mdoc_node *p;
471:
472: p = node_alloc(m, line, pos, tok, MDOC_BODY);
473: p->pending = body;
474: p->end = end;
475: if ( ! node_append(m, p))
476: return(0);
477: m->next = MDOC_NEXT_SIBLING;
1.25 schwarze 478: return(1);
1.1 kristaps 479: }
480:
481:
482: int
1.19 schwarze 483: mdoc_block_alloc(struct mdoc *m, int line, int pos,
1.37 schwarze 484: enum mdoct tok, struct mdoc_arg *args)
1.1 kristaps 485: {
486: struct mdoc_node *p;
487:
1.19 schwarze 488: p = node_alloc(m, line, pos, tok, MDOC_BLOCK);
1.4 schwarze 489: p->args = args;
490: if (p->args)
1.1 kristaps 491: (args->refcnt)++;
1.75 ! schwarze 492:
! 493: switch (tok) {
! 494: case (MDOC_Bd):
! 495: /* FALLTHROUGH */
! 496: case (MDOC_Bf):
! 497: /* FALLTHROUGH */
! 498: case (MDOC_Bl):
! 499: /* FALLTHROUGH */
! 500: case (MDOC_Rs):
! 501: /* FALLTHROUGH */
! 502: case (MDOC_TS):
! 503: p->norm = mandoc_calloc(1, sizeof(union mdoc_data));
! 504: break;
! 505: default:
! 506: break;
! 507: }
! 508:
1.25 schwarze 509: if ( ! node_append(m, p))
510: return(0);
511: m->next = MDOC_NEXT_CHILD;
512: return(1);
1.1 kristaps 513: }
514:
515:
516: int
1.19 schwarze 517: mdoc_elem_alloc(struct mdoc *m, int line, int pos,
1.37 schwarze 518: enum mdoct tok, struct mdoc_arg *args)
1.1 kristaps 519: {
520: struct mdoc_node *p;
521:
1.19 schwarze 522: p = node_alloc(m, line, pos, tok, MDOC_ELEM);
1.4 schwarze 523: p->args = args;
524: if (p->args)
1.1 kristaps 525: (args->refcnt)++;
1.75 ! schwarze 526:
! 527: switch (tok) {
! 528: case (MDOC_An):
! 529: p->norm = mandoc_calloc(1, sizeof(union mdoc_data));
! 530: break;
! 531: default:
! 532: break;
! 533: }
! 534:
1.25 schwarze 535: if ( ! node_append(m, p))
536: return(0);
537: m->next = MDOC_NEXT_CHILD;
538: return(1);
1.1 kristaps 539: }
540:
541:
1.46 schwarze 542: int
543: mdoc_word_alloc(struct mdoc *m, int line, int pos, const char *p)
1.1 kristaps 544: {
1.19 schwarze 545: struct mdoc_node *n;
1.46 schwarze 546: size_t sv, len;
547:
548: len = strlen(p);
1.1 kristaps 549:
1.46 schwarze 550: n = node_alloc(m, line, pos, MDOC_MAX, MDOC_TEXT);
1.32 schwarze 551: n->string = mandoc_malloc(len + 1);
1.19 schwarze 552: sv = strlcpy(n->string, p, len + 1);
553:
554: /* Prohibit truncation. */
555: assert(sv < len + 1);
556:
1.25 schwarze 557: if ( ! node_append(m, n))
558: return(0);
1.46 schwarze 559:
1.25 schwarze 560: m->next = MDOC_NEXT_SIBLING;
561: return(1);
1.19 schwarze 562: }
563:
564:
1.61 schwarze 565: static void
1.1 kristaps 566: mdoc_node_free(struct mdoc_node *p)
567: {
568:
1.67 schwarze 569: if (MDOC_TS == p->tok && MDOC_BLOCK == p->type)
1.75 ! schwarze 570: if (p->norm->TS)
! 571: tbl_free(p->norm->TS);
1.61 schwarze 572:
1.75 ! schwarze 573: if (MDOC_BLOCK == p->type || MDOC_ELEM == p->type)
! 574: free(p->norm);
1.1 kristaps 575: if (p->string)
576: free(p->string);
577: if (p->args)
578: mdoc_argv_free(p->args);
579: free(p);
580: }
581:
582:
1.40 schwarze 583: static void
584: mdoc_node_unlink(struct mdoc *m, struct mdoc_node *n)
585: {
586:
587: /* Adjust siblings. */
588:
589: if (n->prev)
590: n->prev->next = n->next;
591: if (n->next)
592: n->next->prev = n->prev;
593:
594: /* Adjust parent. */
595:
596: if (n->parent) {
597: n->parent->nchild--;
598: if (n->parent->child == n)
599: n->parent->child = n->prev ? n->prev : n->next;
1.72 schwarze 600: if (n->parent->last == n)
601: n->parent->last = n->prev ? n->prev : NULL;
1.40 schwarze 602: }
603:
604: /* Adjust parse point, if applicable. */
605:
606: if (m && m->last == n) {
607: if (n->prev) {
608: m->last = n->prev;
609: m->next = MDOC_NEXT_SIBLING;
610: } else {
611: m->last = n->parent;
612: m->next = MDOC_NEXT_CHILD;
613: }
614: }
615:
616: if (m && m->first == n)
617: m->first = NULL;
618: }
619:
620:
1.1 kristaps 621: void
1.40 schwarze 622: mdoc_node_delete(struct mdoc *m, struct mdoc_node *p)
1.1 kristaps 623: {
624:
1.40 schwarze 625: while (p->child) {
626: assert(p->nchild);
627: mdoc_node_delete(m, p->child);
628: }
629: assert(0 == p->nchild);
1.1 kristaps 630:
1.40 schwarze 631: mdoc_node_unlink(m, p);
1.1 kristaps 632: mdoc_node_free(p);
633: }
634:
635:
636: /*
637: * Parse free-form text, that is, a line that does not begin with the
638: * control character.
639: */
640: static int
1.53 schwarze 641: mdoc_ptext(struct mdoc *m, int line, char *buf, int offs)
1.1 kristaps 642: {
1.56 schwarze 643: char *c, *ws, *end;
644: struct mdoc_node *n;
1.44 schwarze 645:
646: /* Ignore bogus comments. */
647:
1.53 schwarze 648: if ('\\' == buf[offs] &&
649: '.' == buf[offs + 1] &&
650: '"' == buf[offs + 2])
1.54 schwarze 651: return(mdoc_pmsg(m, line, offs, MANDOCERR_BADCOMMENT));
1.1 kristaps 652:
1.46 schwarze 653: /* No text before an initial macro. */
654:
1.9 schwarze 655: if (SEC_NONE == m->lastnamed)
1.54 schwarze 656: return(mdoc_pmsg(m, line, offs, MANDOCERR_NOTEXT));
1.46 schwarze 657:
1.56 schwarze 658: assert(m->last);
659: n = m->last;
660:
661: /*
662: * Divert directly to list processing if we're encountering a
663: * columnar MDOC_BLOCK with or without a prior MDOC_BLOCK entry
664: * (a MDOC_BODY means it's already open, in which case we should
665: * process within its context in the normal way).
666: */
667:
668: if (MDOC_Bl == n->tok && MDOC_BODY == n->type &&
1.75 ! schwarze 669: LIST_column == n->norm->Bl.type) {
1.56 schwarze 670: /* `Bl' is open without any children. */
671: m->flags |= MDOC_FREECOL;
672: return(mdoc_macro(m, MDOC_It, line, offs, &offs, buf));
673: }
674:
675: if (MDOC_It == n->tok && MDOC_BLOCK == n->type &&
676: NULL != n->parent &&
677: MDOC_Bl == n->parent->tok &&
1.75 ! schwarze 678: LIST_column == n->parent->norm->Bl.type) {
1.56 schwarze 679: /* `Bl' has block-level `It' children. */
680: m->flags |= MDOC_FREECOL;
681: return(mdoc_macro(m, MDOC_It, line, offs, &offs, buf));
682: }
683:
1.52 schwarze 684: /*
685: * Search for the beginning of unescaped trailing whitespace (ws)
686: * and for the first character not to be output (end).
687: */
1.56 schwarze 688:
689: /* FIXME: replace with strcspn(). */
1.52 schwarze 690: ws = NULL;
1.53 schwarze 691: for (c = end = buf + offs; *c; c++) {
1.52 schwarze 692: switch (*c) {
1.55 schwarze 693: case '-':
694: if (mandoc_hyph(buf + offs, c))
695: *c = ASCII_HYPH;
1.56 schwarze 696: ws = NULL;
1.55 schwarze 697: break;
1.52 schwarze 698: case ' ':
699: if (NULL == ws)
700: ws = c;
701: continue;
702: case '\t':
703: /*
704: * Always warn about trailing tabs,
705: * even outside literal context,
706: * where they should be put on the next line.
707: */
708: if (NULL == ws)
709: ws = c;
710: /*
711: * Strip trailing tabs in literal context only;
712: * outside, they affect the next line.
713: */
714: if (MDOC_LITERAL & m->flags)
715: continue;
716: break;
717: case '\\':
718: /* Skip the escaped character, too, if any. */
719: if (c[1])
720: c++;
721: /* FALLTHROUGH */
722: default:
723: ws = NULL;
724: break;
725: }
726: end = c + 1;
727: }
728: *end = '\0';
1.19 schwarze 729:
1.52 schwarze 730: if (ws)
1.54 schwarze 731: if ( ! mdoc_pmsg(m, line, (int)(ws-buf), MANDOCERR_EOLNSPACE))
1.52 schwarze 732: return(0);
1.34 schwarze 733:
1.53 schwarze 734: if ('\0' == buf[offs] && ! (MDOC_LITERAL & m->flags)) {
1.54 schwarze 735: if ( ! mdoc_pmsg(m, line, (int)(c-buf), MANDOCERR_NOBLANKLN))
1.38 schwarze 736: return(0);
1.46 schwarze 737:
1.40 schwarze 738: /*
1.66 schwarze 739: * Insert a `sp' in the case of a blank line. Technically,
1.46 schwarze 740: * blank lines aren't allowed, but enough manuals assume this
741: * behaviour that we want to work around it.
1.40 schwarze 742: */
1.66 schwarze 743: if ( ! mdoc_elem_alloc(m, line, offs, MDOC_sp, NULL))
1.42 schwarze 744: return(0);
1.46 schwarze 745:
1.42 schwarze 746: m->next = MDOC_NEXT_SIBLING;
747: return(1);
1.38 schwarze 748: }
1.1 kristaps 749:
1.53 schwarze 750: if ( ! mdoc_word_alloc(m, line, offs, buf+offs))
1.1 kristaps 751: return(0);
752:
1.52 schwarze 753: if (MDOC_LITERAL & m->flags)
754: return(1);
755:
1.35 schwarze 756: /*
1.48 schwarze 757: * End-of-sentence check. If the last character is an unescaped
758: * EOS character, then flag the node as being the end of a
759: * sentence. The front-end will know how to interpret this.
1.35 schwarze 760: */
761:
1.52 schwarze 762: assert(buf < end);
1.48 schwarze 763:
1.62 schwarze 764: if (mandoc_eos(buf+offs, (size_t)(end-buf-offs), 0))
1.48 schwarze 765: m->last->flags |= MDOC_EOS;
766:
1.1 kristaps 767: return(1);
768: }
1.19 schwarze 769:
1.1 kristaps 770:
771: /*
772: * Parse a macro line, that is, a line beginning with the control
773: * character.
774: */
1.61 schwarze 775: static int
1.53 schwarze 776: mdoc_pmacro(struct mdoc *m, int ln, char *buf, int offs)
1.1 kristaps 777: {
1.56 schwarze 778: enum mdoct tok;
779: int i, j, sv;
780: char mac[5];
781: struct mdoc_node *n;
1.1 kristaps 782:
1.7 schwarze 783: /* Empty lines are ignored. */
1.1 kristaps 784:
1.53 schwarze 785: offs++;
786:
787: if ('\0' == buf[offs])
1.1 kristaps 788: return(1);
789:
1.53 schwarze 790: i = offs;
1.24 schwarze 791:
1.63 schwarze 792: /* Accept tabs/whitespace after the initial control char. */
1.24 schwarze 793:
1.63 schwarze 794: if (' ' == buf[i] || '\t' == buf[i]) {
1.24 schwarze 795: i++;
1.63 schwarze 796: while (buf[i] && (' ' == buf[i] || '\t' == buf[i]))
1.1 kristaps 797: i++;
1.34 schwarze 798: if ('\0' == buf[i])
1.1 kristaps 799: return(1);
800: }
801:
1.47 schwarze 802: sv = i;
803:
1.63 schwarze 804: /*
1.64 schwarze 805: * Copy the first word into a nil-terminated buffer.
806: * Stop copying when a tab, space, or eoln is encountered.
1.63 schwarze 807: */
1.1 kristaps 808:
1.64 schwarze 809: j = 0;
810: while (j < 4 && '\0' != buf[i] && ' ' != buf[i] && '\t' != buf[i])
811: mac[j++] = buf[i++];
1.53 schwarze 812: mac[j] = '\0';
1.1 kristaps 813:
1.65 schwarze 814: tok = (j > 1 || j < 4) ? mdoc_hash_find(mac) : MDOC_MAX;
815: if (MDOC_MAX == tok) {
1.69 schwarze 816: mdoc_vmsg(m, MANDOCERR_MACRO, ln, sv, "%s", buf + sv - 1);
1.1 kristaps 817: return(1);
818: }
819:
1.63 schwarze 820: /* Disregard the first trailing tab, if applicable. */
821:
822: if ('\t' == buf[i])
823: i++;
824:
825: /* Jump to the next non-whitespace word. */
1.1 kristaps 826:
827: while (buf[i] && ' ' == buf[i])
828: i++;
1.34 schwarze 829:
1.46 schwarze 830: /*
831: * Trailing whitespace. Note that tabs are allowed to be passed
832: * into the parser as "text", so we only warn about spaces here.
833: */
1.34 schwarze 834:
835: if ('\0' == buf[i] && ' ' == buf[i - 1])
1.54 schwarze 836: if ( ! mdoc_pmsg(m, ln, i - 1, MANDOCERR_EOLNSPACE))
1.34 schwarze 837: goto err;
1.1 kristaps 838:
1.56 schwarze 839: /*
840: * If an initial macro or a list invocation, divert directly
841: * into macro processing.
842: */
843:
844: if (NULL == m->last || MDOC_It == tok || MDOC_El == tok) {
845: if ( ! mdoc_macro(m, tok, ln, sv, &i, buf))
846: goto err;
847: return(1);
848: }
849:
850: n = m->last;
851: assert(m->last);
852:
853: /*
854: * If the first macro of a `Bl -column', open an `It' block
855: * context around the parsed macro.
856: */
857:
858: if (MDOC_Bl == n->tok && MDOC_BODY == n->type &&
1.75 ! schwarze 859: LIST_column == n->norm->Bl.type) {
1.56 schwarze 860: m->flags |= MDOC_FREECOL;
1.58 schwarze 861: if ( ! mdoc_macro(m, MDOC_It, ln, sv, &sv, buf))
1.56 schwarze 862: goto err;
863: return(1);
864: }
865:
866: /*
867: * If we're following a block-level `It' within a `Bl -column'
868: * context (perhaps opened in the above block or in ptext()),
869: * then open an `It' block context around the parsed macro.
1.24 schwarze 870: */
1.56 schwarze 871:
872: if (MDOC_It == n->tok && MDOC_BLOCK == n->type &&
873: NULL != n->parent &&
874: MDOC_Bl == n->parent->tok &&
1.75 ! schwarze 875: LIST_column == n->parent->norm->Bl.type) {
1.56 schwarze 876: m->flags |= MDOC_FREECOL;
877: if ( ! mdoc_macro(m, MDOC_It, ln, sv, &sv, buf))
878: goto err;
879: return(1);
880: }
881:
882: /* Normal processing of a macro. */
883:
1.47 schwarze 884: if ( ! mdoc_macro(m, tok, ln, sv, &i, buf))
1.1 kristaps 885: goto err;
886:
887: return(1);
888:
889: err: /* Error out. */
890:
891: m->flags |= MDOC_HALT;
892: return(0);
893: }
1.24 schwarze 894:
895: