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