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