Annotation of mandoc/man.c, Revision 1.125
1.125 ! schwarze 1: /* $Id: man.c,v 1.124 2014/01/06 00:53:33 schwarze Exp $ */
1.1 kristaps 2: /*
1.102 schwarze 3: * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.123 schwarze 4: * Copyright (c) 2013, 2014 Ingo Schwarze <schwarze@openbsd.org>
1.124 schwarze 5: * Copyright (c) 2011 Joerg Sonnenberger <joerg@netbsd.org>
1.1 kristaps 6: *
7: * Permission to use, copy, modify, and distribute this software for any
1.18 kristaps 8: * purpose with or without fee is hereby granted, provided that the above
9: * copyright notice and this permission notice appear in all copies.
1.1 kristaps 10: *
1.18 kristaps 11: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1 kristaps 18: */
1.47 kristaps 19: #ifdef HAVE_CONFIG_H
20: #include "config.h"
21: #endif
22:
1.41 kristaps 23: #include <sys/types.h>
24:
1.1 kristaps 25: #include <assert.h>
26: #include <stdarg.h>
27: #include <stdlib.h>
28: #include <stdio.h>
29: #include <string.h>
30:
1.105 kristaps 31: #include "man.h"
1.74 kristaps 32: #include "mandoc.h"
1.125 ! schwarze 33: #include "mandoc_aux.h"
1.1 kristaps 34: #include "libman.h"
1.45 kristaps 35: #include "libmandoc.h"
1.1 kristaps 36:
37: const char *const __man_macronames[MAN_MAX] = {
1.21 kristaps 38: "br", "TH", "SH", "SS",
1.1 kristaps 39: "TP", "LP", "PP", "P",
40: "IP", "HP", "SM", "SB",
41: "BI", "IB", "BR", "RB",
1.11 kristaps 42: "R", "B", "I", "IR",
1.92 kristaps 43: "RI", "na", "sp", "nf",
44: "fi", "RE", "RS", "DT",
45: "UC", "PD", "AT", "in",
1.120 schwarze 46: "ft", "OP", "EX", "EE",
47: "UR", "UE"
1.1 kristaps 48: };
49:
50: const char * const *man_macronames = __man_macronames;
51:
1.97 kristaps 52: static struct man_node *man_node_alloc(struct man *, int, int,
1.53 kristaps 53: enum man_type, enum mant);
1.1 kristaps 54: static int man_node_append(struct man *,
55: struct man_node *);
1.54 kristaps 56: static void man_node_free(struct man_node *);
57: static void man_node_unlink(struct man *,
58: struct man_node *);
1.72 kristaps 59: static int man_ptext(struct man *, int, char *, int);
1.79 kristaps 60: static int man_pmacro(struct man *, int, char *, int);
1.2 kristaps 61: static void man_free1(struct man *);
1.45 kristaps 62: static void man_alloc1(struct man *);
1.94 kristaps 63: static int man_descope(struct man *, int, int);
1.1 kristaps 64:
65:
66: const struct man_node *
1.119 schwarze 67: man_node(const struct man *man)
1.1 kristaps 68: {
69:
1.119 schwarze 70: assert( ! (MAN_HALT & man->flags));
71: return(man->first);
1.1 kristaps 72: }
73:
74:
75: const struct man_meta *
1.119 schwarze 76: man_meta(const struct man *man)
1.1 kristaps 77: {
78:
1.119 schwarze 79: assert( ! (MAN_HALT & man->flags));
80: return(&man->meta);
1.1 kristaps 81: }
82:
83:
1.45 kristaps 84: void
1.1 kristaps 85: man_reset(struct man *man)
86: {
87:
1.2 kristaps 88: man_free1(man);
1.45 kristaps 89: man_alloc1(man);
1.1 kristaps 90: }
91:
92:
93: void
94: man_free(struct man *man)
95: {
96:
1.2 kristaps 97: man_free1(man);
1.1 kristaps 98: free(man);
99: }
100:
101:
102: struct man *
1.123 schwarze 103: man_alloc(struct roff *roff, struct mparse *parse, int quick)
1.1 kristaps 104: {
105: struct man *p;
106:
1.45 kristaps 107: p = mandoc_calloc(1, sizeof(struct man));
1.2 kristaps 108:
1.40 kristaps 109: man_hash_init();
1.104 kristaps 110: p->parse = parse;
1.123 schwarze 111: p->quick = quick;
1.108 kristaps 112: p->roff = roff;
1.45 kristaps 113:
114: man_alloc1(p);
1.1 kristaps 115: return(p);
116: }
117:
118:
119: int
1.119 schwarze 120: man_endparse(struct man *man)
1.1 kristaps 121: {
122:
1.119 schwarze 123: assert( ! (MAN_HALT & man->flags));
124: if (man_macroend(man))
1.3 kristaps 125: return(1);
1.119 schwarze 126: man->flags |= MAN_HALT;
1.3 kristaps 127: return(0);
1.1 kristaps 128: }
129:
130:
131: int
1.119 schwarze 132: man_parseln(struct man *man, int ln, char *buf, int offs)
1.1 kristaps 133: {
134:
1.119 schwarze 135: man->flags |= MAN_NEWLINE;
1.97 kristaps 136:
1.119 schwarze 137: assert( ! (MAN_HALT & man->flags));
1.107 kristaps 138:
1.119 schwarze 139: return (roff_getcontrol(man->roff, buf, &offs) ?
140: man_pmacro(man, ln, buf, offs) :
141: man_ptext(man, ln, buf, offs));
1.1 kristaps 142: }
143:
144:
1.2 kristaps 145: static void
146: man_free1(struct man *man)
147: {
148:
149: if (man->first)
1.54 kristaps 150: man_node_delete(man, man->first);
1.2 kristaps 151: if (man->meta.title)
152: free(man->meta.title);
1.6 kristaps 153: if (man->meta.source)
154: free(man->meta.source);
1.102 schwarze 155: if (man->meta.date)
156: free(man->meta.date);
1.2 kristaps 157: if (man->meta.vol)
158: free(man->meta.vol);
1.68 kristaps 159: if (man->meta.msec)
160: free(man->meta.msec);
1.2 kristaps 161: }
162:
163:
1.45 kristaps 164: static void
1.119 schwarze 165: man_alloc1(struct man *man)
1.2 kristaps 166: {
167:
1.119 schwarze 168: memset(&man->meta, 0, sizeof(struct man_meta));
169: man->flags = 0;
170: man->last = mandoc_calloc(1, sizeof(struct man_node));
171: man->first = man->last;
172: man->last->type = MAN_ROOT;
173: man->last->tok = MAN_MAX;
174: man->next = MAN_NEXT_CHILD;
1.2 kristaps 175: }
176:
177:
1.1 kristaps 178: static int
179: man_node_append(struct man *man, struct man_node *p)
180: {
181:
182: assert(man->last);
183: assert(man->first);
184: assert(MAN_ROOT != p->type);
185:
186: switch (man->next) {
187: case (MAN_NEXT_SIBLING):
188: man->last->next = p;
189: p->prev = man->last;
190: p->parent = man->last->parent;
191: break;
192: case (MAN_NEXT_CHILD):
193: man->last->child = p;
194: p->parent = man->last;
195: break;
196: default:
197: abort();
198: /* NOTREACHED */
199: }
1.22 kristaps 200:
1.54 kristaps 201: assert(p->parent);
1.22 kristaps 202: p->parent->nchild++;
1.1 kristaps 203:
1.29 kristaps 204: if ( ! man_valid_pre(man, p))
205: return(0);
206:
207: switch (p->type) {
208: case (MAN_HEAD):
209: assert(MAN_BLOCK == p->parent->type);
210: p->parent->head = p;
211: break;
1.106 kristaps 212: case (MAN_TAIL):
213: assert(MAN_BLOCK == p->parent->type);
214: p->parent->tail = p;
215: break;
1.29 kristaps 216: case (MAN_BODY):
217: assert(MAN_BLOCK == p->parent->type);
218: p->parent->body = p;
219: break;
220: default:
221: break;
222: }
223:
1.2 kristaps 224: man->last = p;
225:
1.1 kristaps 226: switch (p->type) {
1.94 kristaps 227: case (MAN_TBL):
228: /* FALLTHROUGH */
1.2 kristaps 229: case (MAN_TEXT):
230: if ( ! man_valid_post(man))
231: return(0);
1.1 kristaps 232: break;
233: default:
234: break;
235: }
236:
237: return(1);
238: }
239:
240:
241: static struct man_node *
1.119 schwarze 242: man_node_alloc(struct man *man, int line, int pos,
1.97 kristaps 243: enum man_type type, enum mant tok)
1.1 kristaps 244: {
245: struct man_node *p;
246:
1.45 kristaps 247: p = mandoc_calloc(1, sizeof(struct man_node));
1.1 kristaps 248: p->line = line;
249: p->pos = pos;
250: p->type = type;
1.16 kristaps 251: p->tok = tok;
1.97 kristaps 252:
1.119 schwarze 253: if (MAN_NEWLINE & man->flags)
1.97 kristaps 254: p->flags |= MAN_LINE;
1.119 schwarze 255: man->flags &= ~MAN_NEWLINE;
1.1 kristaps 256: return(p);
257: }
258:
259:
260: int
1.119 schwarze 261: man_elem_alloc(struct man *man, int line, int pos, enum mant tok)
1.1 kristaps 262: {
263: struct man_node *p;
264:
1.119 schwarze 265: p = man_node_alloc(man, line, pos, MAN_ELEM, tok);
266: if ( ! man_node_append(man, p))
1.106 kristaps 267: return(0);
1.119 schwarze 268: man->next = MAN_NEXT_CHILD;
1.106 kristaps 269: return(1);
270: }
271:
272:
273: int
1.119 schwarze 274: man_tail_alloc(struct man *man, int line, int pos, enum mant tok)
1.106 kristaps 275: {
276: struct man_node *p;
277:
1.119 schwarze 278: p = man_node_alloc(man, line, pos, MAN_TAIL, tok);
279: if ( ! man_node_append(man, p))
1.30 kristaps 280: return(0);
1.119 schwarze 281: man->next = MAN_NEXT_CHILD;
1.30 kristaps 282: return(1);
1.1 kristaps 283: }
284:
285:
286: int
1.119 schwarze 287: man_head_alloc(struct man *man, int line, int pos, enum mant tok)
1.29 kristaps 288: {
289: struct man_node *p;
290:
1.119 schwarze 291: p = man_node_alloc(man, line, pos, MAN_HEAD, tok);
292: if ( ! man_node_append(man, p))
1.29 kristaps 293: return(0);
1.119 schwarze 294: man->next = MAN_NEXT_CHILD;
1.29 kristaps 295: return(1);
296: }
297:
298:
299: int
1.119 schwarze 300: man_body_alloc(struct man *man, int line, int pos, enum mant tok)
1.29 kristaps 301: {
302: struct man_node *p;
303:
1.119 schwarze 304: p = man_node_alloc(man, line, pos, MAN_BODY, tok);
305: if ( ! man_node_append(man, p))
1.29 kristaps 306: return(0);
1.119 schwarze 307: man->next = MAN_NEXT_CHILD;
1.29 kristaps 308: return(1);
309: }
310:
311:
312: int
1.119 schwarze 313: man_block_alloc(struct man *man, int line, int pos, enum mant tok)
1.29 kristaps 314: {
315: struct man_node *p;
316:
1.119 schwarze 317: p = man_node_alloc(man, line, pos, MAN_BLOCK, tok);
318: if ( ! man_node_append(man, p))
1.29 kristaps 319: return(0);
1.119 schwarze 320: man->next = MAN_NEXT_CHILD;
1.29 kristaps 321: return(1);
322: }
323:
1.61 kristaps 324: int
1.119 schwarze 325: man_word_alloc(struct man *man, int line, int pos, const char *word)
1.1 kristaps 326: {
1.31 kristaps 327: struct man_node *n;
1.1 kristaps 328:
1.119 schwarze 329: n = man_node_alloc(man, line, pos, MAN_TEXT, MAN_MAX);
330: n->string = roff_strdup(man->roff, word);
1.31 kristaps 331:
1.119 schwarze 332: if ( ! man_node_append(man, n))
1.30 kristaps 333: return(0);
1.61 kristaps 334:
1.119 schwarze 335: man->next = MAN_NEXT_SIBLING;
1.30 kristaps 336: return(1);
1.1 kristaps 337: }
338:
339:
1.54 kristaps 340: /*
341: * Free all of the resources held by a node. This does NOT unlink a
342: * node from its context; for that, see man_node_unlink().
343: */
344: static void
1.1 kristaps 345: man_node_free(struct man_node *p)
346: {
347:
348: if (p->string)
349: free(p->string);
350: free(p);
351: }
352:
353:
354: void
1.119 schwarze 355: man_node_delete(struct man *man, struct man_node *p)
1.1 kristaps 356: {
357:
1.54 kristaps 358: while (p->child)
1.119 schwarze 359: man_node_delete(man, p->child);
1.54 kristaps 360:
1.119 schwarze 361: man_node_unlink(man, p);
1.1 kristaps 362: man_node_free(p);
363: }
364:
1.101 kristaps 365: int
1.119 schwarze 366: man_addeqn(struct man *man, const struct eqn *ep)
1.101 kristaps 367: {
368: struct man_node *n;
369:
1.119 schwarze 370: assert( ! (MAN_HALT & man->flags));
1.101 kristaps 371:
1.119 schwarze 372: n = man_node_alloc(man, ep->ln, ep->pos, MAN_EQN, MAN_MAX);
1.101 kristaps 373: n->eqn = ep;
374:
1.119 schwarze 375: if ( ! man_node_append(man, n))
1.101 kristaps 376: return(0);
377:
1.119 schwarze 378: man->next = MAN_NEXT_SIBLING;
379: return(man_descope(man, ep->ln, ep->pos));
1.101 kristaps 380: }
1.1 kristaps 381:
1.94 kristaps 382: int
1.119 schwarze 383: man_addspan(struct man *man, const struct tbl_span *sp)
1.94 kristaps 384: {
1.100 kristaps 385: struct man_node *n;
1.94 kristaps 386:
1.119 schwarze 387: assert( ! (MAN_HALT & man->flags));
1.100 kristaps 388:
1.119 schwarze 389: n = man_node_alloc(man, sp->line, 0, MAN_TBL, MAN_MAX);
1.100 kristaps 390: n->span = sp;
391:
1.119 schwarze 392: if ( ! man_node_append(man, n))
1.94 kristaps 393: return(0);
1.100 kristaps 394:
1.119 schwarze 395: man->next = MAN_NEXT_SIBLING;
396: return(man_descope(man, sp->line, 0));
1.94 kristaps 397: }
398:
399: static int
1.119 schwarze 400: man_descope(struct man *man, int line, int offs)
1.94 kristaps 401: {
402: /*
403: * Co-ordinate what happens with having a next-line scope open:
404: * first close out the element scope (if applicable), then close
405: * out the block scope (also if applicable).
406: */
407:
1.119 schwarze 408: if (MAN_ELINE & man->flags) {
409: man->flags &= ~MAN_ELINE;
410: if ( ! man_unscope(man, man->last->parent, MANDOCERR_MAX))
1.94 kristaps 411: return(0);
412: }
413:
1.119 schwarze 414: if ( ! (MAN_BLINE & man->flags))
1.94 kristaps 415: return(1);
1.119 schwarze 416: man->flags &= ~MAN_BLINE;
1.94 kristaps 417:
1.119 schwarze 418: if ( ! man_unscope(man, man->last->parent, MANDOCERR_MAX))
1.94 kristaps 419: return(0);
1.119 schwarze 420: return(man_body_alloc(man, line, offs, man->last->tok));
1.94 kristaps 421: }
422:
1.1 kristaps 423: static int
1.119 schwarze 424: man_ptext(struct man *man, int line, char *buf, int offs)
1.1 kristaps 425: {
1.61 kristaps 426: int i;
1.60 kristaps 427:
1.35 kristaps 428: /* Literal free-form text whitespace is preserved. */
429:
1.119 schwarze 430: if (MAN_LITERAL & man->flags) {
431: if ( ! man_word_alloc(man, line, offs, buf + offs))
1.35 kristaps 432: return(0);
1.119 schwarze 433: return(man_descope(man, line, offs));
1.35 kristaps 434: }
435:
1.72 kristaps 436: for (i = offs; ' ' == buf[i]; i++)
1.31 kristaps 437: /* Skip leading whitespace. */ ;
1.48 kristaps 438:
1.121 schwarze 439: /*
440: * Blank lines are ignored right after headings
441: * but add a single vertical space elsewhere.
442: */
443:
1.49 kristaps 444: if ('\0' == buf[i]) {
1.61 kristaps 445: /* Allocate a blank entry. */
1.121 schwarze 446: if (MAN_SH != man->last->tok &&
447: MAN_SS != man->last->tok) {
448: if ( ! man_elem_alloc(man, line, offs, MAN_sp))
449: return(0);
450: man->next = MAN_NEXT_SIBLING;
451: }
1.118 schwarze 452: return(1);
1.31 kristaps 453: }
454:
1.63 kristaps 455: /*
456: * Warn if the last un-escaped character is whitespace. Then
457: * strip away the remaining spaces (tabs stay!).
458: */
1.29 kristaps 459:
1.61 kristaps 460: i = (int)strlen(buf);
461: assert(i);
1.49 kristaps 462:
1.63 kristaps 463: if (' ' == buf[i - 1] || '\t' == buf[i - 1]) {
1.64 kristaps 464: if (i > 1 && '\\' != buf[i - 2])
1.119 schwarze 465: man_pmsg(man, line, i - 1, MANDOCERR_EOLNSPACE);
1.63 kristaps 466:
467: for (--i; i && ' ' == buf[i]; i--)
468: /* Spin back to non-space. */ ;
469:
470: /* Jump ahead of escaped whitespace. */
471: i += '\\' == buf[i] ? 2 : 1;
472:
473: buf[i] = '\0';
474: }
1.49 kristaps 475:
1.119 schwarze 476: if ( ! man_word_alloc(man, line, offs, buf + offs))
1.1 kristaps 477: return(0);
1.65 kristaps 478:
479: /*
480: * End-of-sentence check. If the last character is an unescaped
481: * EOS character, then flag the node as being the end of a
482: * sentence. The front-end will know how to interpret this.
483: */
1.67 kristaps 484:
1.65 kristaps 485: assert(i);
1.122 schwarze 486: if (mandoc_eos(buf, (size_t)i))
1.119 schwarze 487: man->last->flags |= MAN_EOS;
1.31 kristaps 488:
1.119 schwarze 489: return(man_descope(man, line, offs));
1.1 kristaps 490: }
491:
1.96 kristaps 492: static int
1.119 schwarze 493: man_pmacro(struct man *man, int ln, char *buf, int offs)
1.1 kristaps 494: {
1.107 kristaps 495: int i, ppos;
1.53 kristaps 496: enum mant tok;
1.34 kristaps 497: char mac[5];
498: struct man_node *n;
1.1 kristaps 499:
1.107 kristaps 500: if ('"' == buf[offs]) {
1.119 schwarze 501: man_pmsg(man, ln, offs, MANDOCERR_BADCOMMENT);
1.107 kristaps 502: return(1);
503: } else if ('\0' == buf[offs])
1.46 kristaps 504: return(1);
1.1 kristaps 505:
1.107 kristaps 506: ppos = offs;
1.9 kristaps 507:
1.56 kristaps 508: /*
1.107 kristaps 509: * Copy the first word into a nil-terminated buffer.
510: * Stop copying when a tab, space, or eoln is encountered.
1.56 kristaps 511: */
1.62 kristaps 512:
1.107 kristaps 513: i = 0;
514: while (i < 4 && '\0' != buf[offs] &&
515: ' ' != buf[offs] && '\t' != buf[offs])
516: mac[i++] = buf[offs++];
1.10 kristaps 517:
1.107 kristaps 518: mac[i] = '\0';
1.1 kristaps 519:
1.107 kristaps 520: tok = (i > 0 && i < 4) ? man_hash_find(mac) : MAN_MAX;
1.1 kristaps 521:
1.87 schwarze 522: if (MAN_MAX == tok) {
1.119 schwarze 523: mandoc_vmsg(MANDOCERR_MACRO, man->parse, ln,
1.104 kristaps 524: ppos, "%s", buf + ppos - 1);
1.12 kristaps 525: return(1);
1.1 kristaps 526: }
527:
528: /* The macro is sane. Jump to the next word. */
529:
1.107 kristaps 530: while (buf[offs] && ' ' == buf[offs])
531: offs++;
1.1 kristaps 532:
1.62 kristaps 533: /*
534: * Trailing whitespace. Note that tabs are allowed to be passed
535: * into the parser as "text", so we only warn about spaces here.
536: */
1.48 kristaps 537:
1.107 kristaps 538: if ('\0' == buf[offs] && ' ' == buf[offs - 1])
1.119 schwarze 539: man_pmsg(man, ln, offs - 1, MANDOCERR_EOLNSPACE);
1.48 kristaps 540:
1.50 kristaps 541: /*
1.90 kristaps 542: * Remove prior ELINE macro, as it's being clobbered by a new
1.51 kristaps 543: * macro. Note that NSCOPED macros do not close out ELINE
544: * macros---they don't print text---so we let those slip by.
1.50 kristaps 545: */
1.34 kristaps 546:
1.53 kristaps 547: if ( ! (MAN_NSCOPED & man_macros[tok].flags) &&
1.119 schwarze 548: man->flags & MAN_ELINE) {
549: n = man->last;
1.90 kristaps 550: assert(MAN_TEXT != n->type);
1.51 kristaps 551:
1.90 kristaps 552: /* Remove repeated NSCOPED macros causing ELINE. */
1.51 kristaps 553:
1.90 kristaps 554: if (MAN_NSCOPED & man_macros[n->tok].flags)
555: n = n->parent;
1.51 kristaps 556:
1.119 schwarze 557: mandoc_vmsg(MANDOCERR_LINESCOPE, man->parse, n->line,
1.113 schwarze 558: n->pos, "%s breaks %s", man_macronames[tok],
559: man_macronames[n->tok]);
1.34 kristaps 560:
1.119 schwarze 561: man_node_delete(man, n);
562: man->flags &= ~MAN_ELINE;
1.113 schwarze 563: }
564:
565: /*
566: * Remove prior BLINE macro that is being clobbered.
567: */
1.119 schwarze 568: if ((man->flags & MAN_BLINE) &&
1.113 schwarze 569: (MAN_BSCOPE & man_macros[tok].flags)) {
1.119 schwarze 570: n = man->last;
1.114 joerg 571:
572: /* Might be a text node like 8 in
573: * .TP 8
574: * .SH foo
575: */
576: if (MAN_TEXT == n->type)
577: n = n->parent;
1.113 schwarze 578:
579: /* Remove element that didn't end BLINE, if any. */
580: if ( ! (MAN_BSCOPE & man_macros[n->tok].flags))
581: n = n->parent;
582:
583: assert(MAN_HEAD == n->type);
584: n = n->parent;
585: assert(MAN_BLOCK == n->type);
586: assert(MAN_SCOPED & man_macros[n->tok].flags);
587:
1.119 schwarze 588: mandoc_vmsg(MANDOCERR_LINESCOPE, man->parse, n->line,
1.113 schwarze 589: n->pos, "%s breaks %s", man_macronames[tok],
590: man_macronames[n->tok]);
591:
1.119 schwarze 592: man_node_delete(man, n);
593: man->flags &= ~MAN_BLINE;
1.34 kristaps 594: }
595:
1.59 kristaps 596: /*
597: * Save the fact that we're in the next-line for a block. In
598: * this way, embedded roff instructions can "remember" state
599: * when they exit.
600: */
601:
1.119 schwarze 602: if (MAN_BLINE & man->flags)
603: man->flags |= MAN_BPLINE;
1.59 kristaps 604:
605: /* Call to handler... */
1.1 kristaps 606:
1.53 kristaps 607: assert(man_macros[tok].fp);
1.119 schwarze 608: if ( ! (*man_macros[tok].fp)(man, tok, ln, ppos, &offs, buf))
1.1 kristaps 609: goto err;
1.123 schwarze 610:
611: /* In quick mode (for mandocdb), abort after the NAME section. */
612:
613: if (man->quick && MAN_SH == tok &&
614: strcmp(man->last->prev->child->string, "NAME"))
615: return(2);
1.1 kristaps 616:
1.50 kristaps 617: /*
618: * We weren't in a block-line scope when entering the
619: * above-parsed macro, so return.
620: */
621:
1.119 schwarze 622: if ( ! (MAN_BPLINE & man->flags)) {
623: man->flags &= ~MAN_ILINE;
1.29 kristaps 624: return(1);
1.50 kristaps 625: }
1.119 schwarze 626: man->flags &= ~MAN_BPLINE;
1.50 kristaps 627:
628: /*
629: * If we're in a block scope, then allow this macro to slip by
630: * without closing scope around it.
631: */
632:
1.119 schwarze 633: if (MAN_ILINE & man->flags) {
634: man->flags &= ~MAN_ILINE;
1.50 kristaps 635: return(1);
636: }
1.29 kristaps 637:
638: /*
639: * If we've opened a new next-line element scope, then return
640: * now, as the next line will close out the block scope.
641: */
642:
1.119 schwarze 643: if (MAN_ELINE & man->flags)
1.29 kristaps 644: return(1);
645:
646: /* Close out the block scope opened in the prior line. */
1.11 kristaps 647:
1.119 schwarze 648: assert(MAN_BLINE & man->flags);
649: man->flags &= ~MAN_BLINE;
1.11 kristaps 650:
1.119 schwarze 651: if ( ! man_unscope(man, man->last->parent, MANDOCERR_MAX))
1.29 kristaps 652: return(0);
1.119 schwarze 653: return(man_body_alloc(man, ln, ppos, man->last->tok));
1.1 kristaps 654:
655: err: /* Error out. */
656:
1.119 schwarze 657: man->flags |= MAN_HALT;
1.1 kristaps 658: return(0);
659: }
1.51 kristaps 660:
1.54 kristaps 661: /*
1.119 schwarze 662: * Unlink a node from its context. If "man" is provided, the last parse
1.54 kristaps 663: * point will also be adjusted accordingly.
664: */
665: static void
1.119 schwarze 666: man_node_unlink(struct man *man, struct man_node *n)
1.51 kristaps 667: {
668:
1.54 kristaps 669: /* Adjust siblings. */
670:
671: if (n->prev)
1.51 kristaps 672: n->prev->next = n->next;
1.54 kristaps 673: if (n->next)
674: n->next->prev = n->prev;
675:
676: /* Adjust parent. */
677:
678: if (n->parent) {
679: n->parent->nchild--;
680: if (n->parent->child == n)
681: n->parent->child = n->prev ? n->prev : n->next;
682: }
683:
684: /* Adjust parse point, if applicable. */
685:
1.119 schwarze 686: if (man && man->last == n) {
1.54 kristaps 687: /*XXX: this can occur when bailing from validation. */
688: /*assert(NULL == n->next);*/
689: if (n->prev) {
1.119 schwarze 690: man->last = n->prev;
691: man->next = MAN_NEXT_SIBLING;
1.54 kristaps 692: } else {
1.119 schwarze 693: man->last = n->parent;
694: man->next = MAN_NEXT_CHILD;
1.51 kristaps 695: }
696: }
697:
1.119 schwarze 698: if (man && man->first == n)
699: man->first = NULL;
1.112 kristaps 700: }
701:
702: const struct mparse *
1.119 schwarze 703: man_mparse(const struct man *man)
1.112 kristaps 704: {
705:
1.119 schwarze 706: assert(man && man->parse);
707: return(man->parse);
1.23 kristaps 708: }
CVSweb