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