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