Annotation of mandoc/terminal.c, Revision 1.8
1.8 ! kristaps 1: /* $Id: terminal.c,v 1.7 2009/03/23 15:20:51 kristaps Exp $ */
1.1 kristaps 2: /*
3: * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@openbsd.org>
4: *
5: * Permission to use, copy, modify, and distribute this software for any
6: * purpose with or without fee is hereby granted, provided that the
7: * above copyright notice and this permission notice appear in all
8: * copies.
9: *
10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
11: * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
12: * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
13: * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14: * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15: * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
16: * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17: * PERFORMANCE OF THIS SOFTWARE.
18: */
19: #include <assert.h>
20: #include <err.h>
21: #include <stdio.h>
22: #include <stdlib.h>
23: #include <string.h>
24:
25: #include "term.h"
26:
1.2 kristaps 27: #ifdef __linux__
28: extern size_t strlcpy(char *, const char *, size_t);
29: extern size_t strlcat(char *, const char *, size_t);
30: #endif
31:
1.5 kristaps 32: static struct termp *term_alloc(enum termenc);
33: static void term_free(struct termp *);
34: static void term_body(struct termp *, struct termpair *,
1.1 kristaps 35: const struct mdoc_meta *,
36: const struct mdoc_node *);
1.5 kristaps 37: static void term_head(struct termp *,
1.1 kristaps 38: const struct mdoc_meta *);
1.5 kristaps 39: static void term_foot(struct termp *,
1.1 kristaps 40: const struct mdoc_meta *);
1.5 kristaps 41: static void term_pword(struct termp *, const char *, int);
42: static void term_pescape(struct termp *,
1.1 kristaps 43: const char *, int *, int);
1.5 kristaps 44: static void term_nescape(struct termp *,
1.1 kristaps 45: const char *, size_t);
1.5 kristaps 46: static void term_chara(struct termp *, char);
47: static void term_stringa(struct termp *,
1.1 kristaps 48: const char *, size_t);
1.6 kristaps 49: static int term_isopendelim(const char *, int);
50: static int term_isclosedelim(const char *, int);
1.1 kristaps 51: static void sanity(const struct mdoc_node *); /* XXX */
52:
53:
54: void *
55: ascii_alloc(void)
56: {
57:
1.5 kristaps 58: return(term_alloc(TERMENC_ASCII));
1.1 kristaps 59: }
60:
61:
62: int
1.7 kristaps 63: terminal_run(void *arg, const struct man *man,
64: const struct mdoc *mdoc)
1.1 kristaps 65: {
66: struct termp *p;
67:
1.7 kristaps 68: if (NULL == mdoc)
69: return(1);
70:
1.1 kristaps 71: p = (struct termp *)arg;
72:
73: if (NULL == p->symtab)
1.3 kristaps 74: p->symtab = term_ascii2htab();
1.1 kristaps 75:
1.5 kristaps 76: term_head(p, mdoc_meta(mdoc));
77: term_body(p, NULL, mdoc_meta(mdoc), mdoc_node(mdoc));
78: term_foot(p, mdoc_meta(mdoc));
1.1 kristaps 79:
80: return(1);
81: }
82:
83:
84: void
85: terminal_free(void *arg)
86: {
87:
1.5 kristaps 88: term_free((struct termp *)arg);
1.1 kristaps 89: }
90:
91:
92: static void
1.5 kristaps 93: term_free(struct termp *p)
1.1 kristaps 94: {
95:
96: if (p->buf)
97: free(p->buf);
98: if (TERMENC_ASCII == p->enc && p->symtab)
1.3 kristaps 99: term_asciifree(p->symtab);
1.1 kristaps 100:
101: free(p);
102: }
103:
104:
105: static struct termp *
1.5 kristaps 106: term_alloc(enum termenc enc)
1.1 kristaps 107: {
108: struct termp *p;
109:
110: if (NULL == (p = malloc(sizeof(struct termp))))
111: err(1, "malloc");
112: bzero(p, sizeof(struct termp));
113: p->maxrmargin = 78;
114: p->enc = enc;
115: return(p);
116: }
117:
118:
1.5 kristaps 119: static int
1.6 kristaps 120: term_isclosedelim(const char *p, int len)
1.5 kristaps 121: {
122:
123: if (1 != len)
124: return(0);
125:
126: switch (*p) {
127: case('.'):
128: /* FALLTHROUGH */
129: case(','):
130: /* FALLTHROUGH */
131: case(';'):
132: /* FALLTHROUGH */
133: case(':'):
134: /* FALLTHROUGH */
135: case('?'):
136: /* FALLTHROUGH */
137: case('!'):
138: /* FALLTHROUGH */
139: case(')'):
140: /* FALLTHROUGH */
141: case(']'):
142: /* FALLTHROUGH */
143: case('}'):
144: return(1);
145: default:
146: break;
147: }
148:
149: return(0);
150: }
151:
152:
153: static int
1.6 kristaps 154: term_isopendelim(const char *p, int len)
1.5 kristaps 155: {
156:
157: if (1 != len)
158: return(0);
159:
160: switch (*p) {
161: case('('):
162: /* FALLTHROUGH */
163: case('['):
164: /* FALLTHROUGH */
165: case('{'):
166: return(1);
167: default:
168: break;
169: }
170:
171: return(0);
172: }
173:
174:
1.1 kristaps 175: /*
176: * Flush a line of text. A "line" is loosely defined as being something
177: * that should be followed by a newline, regardless of whether it's
178: * broken apart by newlines getting there. A line can also be a
179: * fragment of a columnar list.
180: *
181: * Specifically, a line is whatever's in p->buf of length p->col, which
182: * is zeroed after this function returns.
183: *
184: * The variables TERMP_NOLPAD, TERMP_LITERAL and TERMP_NOBREAK are of
185: * critical importance here. Their behaviour follows:
186: *
187: * - TERMP_NOLPAD: when beginning to write the line, don't left-pad the
188: * offset value. This is useful when doing columnar lists where the
189: * prior column has right-padded.
190: *
191: * - TERMP_NOBREAK: this is the most important and is used when making
192: * columns. In short: don't print a newline and instead pad to the
193: * right margin. Used in conjunction with TERMP_NOLPAD.
194: *
195: * - TERMP_NONOBREAK: don't newline when TERMP_NOBREAK is specified.
196: *
197: * In-line line breaking:
198: *
199: * If TERMP_NOBREAK is specified and the line overruns the right
200: * margin, it will break and pad-right to the right margin after
201: * writing. If maxrmargin is violated, it will break and continue
202: * writing from the right-margin, which will lead to the above
203: * scenario upon exit.
204: *
205: * Otherwise, the line will break at the right margin. Extremely long
206: * lines will cause the system to emit a warning (TODO: hyphenate, if
207: * possible).
208: */
209: void
1.3 kristaps 210: term_flushln(struct termp *p)
1.1 kristaps 211: {
212: int i, j;
213: size_t vsz, vis, maxvis, mmax, bp;
214:
215: /*
216: * First, establish the maximum columns of "visible" content.
217: * This is usually the difference between the right-margin and
218: * an indentation, but can be, for tagged lists or columns, a
219: * small set of values.
220: */
221:
222: assert(p->offset < p->rmargin);
223: maxvis = p->rmargin - p->offset;
224: mmax = p->maxrmargin - p->offset;
225: bp = TERMP_NOBREAK & p->flags ? mmax : maxvis;
226: vis = 0;
227:
228: /*
229: * If in the standard case (left-justified), then begin with our
230: * indentation, otherwise (columns, etc.) just start spitting
231: * out text.
232: */
233:
234: if ( ! (p->flags & TERMP_NOLPAD))
235: /* LINTED */
236: for (j = 0; j < (int)p->offset; j++)
237: putchar(' ');
238:
239: for (i = 0; i < (int)p->col; i++) {
240: /*
241: * Count up visible word characters. Control sequences
242: * (starting with the CSI) aren't counted. A space
243: * generates a non-printing word, which is valid (the
244: * space is printed according to regular spacing rules).
245: */
246:
247: /* LINTED */
248: for (j = i, vsz = 0; j < (int)p->col; j++) {
249: if (' ' == p->buf[j])
250: break;
251: else if (8 == p->buf[j])
252: j += 1;
253: else
254: vsz++;
255: }
256:
257: /*
258: * Do line-breaking. If we're greater than our
259: * break-point and already in-line, break to the next
260: * line and start writing. If we're at the line start,
261: * then write out the word (TODO: hyphenate) and break
262: * in a subsequent loop invocation.
263: */
264:
265: if ( ! (TERMP_NOBREAK & p->flags)) {
266: if (vis && vis + vsz > bp) {
267: putchar('\n');
268: for (j = 0; j < (int)p->offset; j++)
269: putchar(' ');
270: vis = 0;
1.4 kristaps 271: }
272: } else if (vis && vis + vsz > bp) {
273: putchar('\n');
274: for (j = 0; j < (int)p->rmargin; j++)
275: putchar(' ');
276: vis = p->rmargin - p->offset;
1.1 kristaps 277: }
278:
279: /*
280: * Write out the word and a trailing space. Omit the
281: * space if we're the last word in the line or beyond
282: * our breakpoint.
283: */
284:
285: for ( ; i < (int)p->col; i++) {
286: if (' ' == p->buf[i])
287: break;
288: putchar(p->buf[i]);
289: }
290: vis += vsz;
291: if (i < (int)p->col && vis <= bp) {
292: putchar(' ');
293: vis++;
294: }
295: }
296:
297: /*
298: * If we've overstepped our maximum visible no-break space, then
299: * cause a newline and offset at the right margin.
300: */
301:
302: if ((TERMP_NOBREAK & p->flags) && vis >= maxvis) {
303: if ( ! (TERMP_NONOBREAK & p->flags)) {
304: putchar('\n');
305: for (i = 0; i < (int)p->rmargin; i++)
306: putchar(' ');
307: }
308: p->col = 0;
309: return;
310: }
311:
312: /*
313: * If we're not to right-marginalise it (newline), then instead
314: * pad to the right margin and stay off.
315: */
316:
317: if (p->flags & TERMP_NOBREAK) {
318: if ( ! (TERMP_NONOBREAK & p->flags))
319: for ( ; vis < maxvis; vis++)
320: putchar(' ');
321: } else
322: putchar('\n');
323:
324: p->col = 0;
325: }
326:
327:
328: /*
329: * A newline only breaks an existing line; it won't assert vertical
330: * space. All data in the output buffer is flushed prior to the newline
331: * assertion.
332: */
333: void
1.3 kristaps 334: term_newln(struct termp *p)
1.1 kristaps 335: {
336:
337: p->flags |= TERMP_NOSPACE;
338: if (0 == p->col) {
339: p->flags &= ~TERMP_NOLPAD;
340: return;
341: }
1.3 kristaps 342: term_flushln(p);
1.1 kristaps 343: p->flags &= ~TERMP_NOLPAD;
344: }
345:
346:
347: /*
348: * Asserts a vertical space (a full, empty line-break between lines).
349: * Note that if used twice, this will cause two blank spaces and so on.
350: * All data in the output buffer is flushed prior to the newline
351: * assertion.
352: */
353: void
1.3 kristaps 354: term_vspace(struct termp *p)
1.1 kristaps 355: {
356:
1.3 kristaps 357: term_newln(p);
1.1 kristaps 358: putchar('\n');
359: }
360:
361:
362: /*
363: * Break apart a word into "pwords" (partial-words, usually from
364: * breaking up a phrase into individual words) and, eventually, put them
365: * into the output buffer. If we're a literal word, then don't break up
366: * the word and put it verbatim into the output buffer.
367: */
368: void
1.3 kristaps 369: term_word(struct termp *p, const char *word)
1.1 kristaps 370: {
371: int i, j, len;
372:
1.3 kristaps 373: len = (int)strlen(word);
374:
1.1 kristaps 375: if (p->flags & TERMP_LITERAL) {
1.5 kristaps 376: term_pword(p, word, len);
1.1 kristaps 377: return;
378: }
379:
380: /* LINTED */
381: for (j = i = 0; i < len; i++) {
382: if (' ' != word[i]) {
383: j++;
384: continue;
385: }
386:
387: /* Escaped spaces don't delimit... */
388: if (i && ' ' == word[i] && '\\' == word[i - 1]) {
389: j++;
390: continue;
391: }
392:
393: if (0 == j)
394: continue;
395: assert(i >= j);
1.5 kristaps 396: term_pword(p, &word[i - j], j);
1.1 kristaps 397: j = 0;
398: }
399: if (j > 0) {
400: assert(i >= j);
1.5 kristaps 401: term_pword(p, &word[i - j], j);
1.1 kristaps 402: }
403: }
404:
405:
1.3 kristaps 406: static void
1.5 kristaps 407: term_body(struct termp *p, struct termpair *ppair,
1.3 kristaps 408: const struct mdoc_meta *meta,
409: const struct mdoc_node *node)
410: {
411:
412: term_node(p, ppair, meta, node);
413: if (node->next)
1.5 kristaps 414: term_body(p, ppair, meta, node->next);
1.3 kristaps 415: }
416:
417:
1.1 kristaps 418: /*
419: * This is the main function for printing out nodes. It's constituted
420: * of PRE and POST functions, which correspond to prefix and infix
421: * processing. The termpair structure allows data to persist between
422: * prefix and postfix invocations.
423: */
1.3 kristaps 424: void
425: term_node(struct termp *p, struct termpair *ppair,
1.1 kristaps 426: const struct mdoc_meta *meta,
427: const struct mdoc_node *node)
428: {
429: int dochild;
430: struct termpair pair;
431:
432: /* Some quick sanity-checking. */
433:
434: sanity(node);
435:
436: /* Pre-processing. */
437:
438: dochild = 1;
439: pair.ppair = ppair;
440: pair.type = 0;
441: pair.offset = pair.rmargin = 0;
442: pair.flag = 0;
443: pair.count = 0;
444:
445: if (MDOC_TEXT != node->type) {
446: if (termacts[node->tok].pre)
447: if ( ! (*termacts[node->tok].pre)(p, &pair, meta, node))
448: dochild = 0;
449: } else /* MDOC_TEXT == node->type */
1.3 kristaps 450: term_word(p, node->string);
1.1 kristaps 451:
452: /* Children. */
453:
454: if (TERMPAIR_FLAG & pair.type)
455: p->flags |= pair.flag;
456:
457: if (dochild && node->child)
1.5 kristaps 458: term_body(p, &pair, meta, node->child);
1.1 kristaps 459:
460: if (TERMPAIR_FLAG & pair.type)
461: p->flags &= ~pair.flag;
462:
463: /* Post-processing. */
464:
465: if (MDOC_TEXT != node->type)
466: if (termacts[node->tok].post)
467: (*termacts[node->tok].post)(p, &pair, meta, node);
468: }
469:
470:
471: static void
1.5 kristaps 472: term_foot(struct termp *p, const struct mdoc_meta *meta)
1.1 kristaps 473: {
474: struct tm *tm;
475: char *buf, *os;
476:
477: if (NULL == (buf = malloc(p->rmargin)))
478: err(1, "malloc");
479: if (NULL == (os = malloc(p->rmargin)))
480: err(1, "malloc");
481:
482: tm = localtime(&meta->date);
483:
484: #ifdef __OpenBSD__
485: if (NULL == strftime(buf, p->rmargin, "%B %d, %Y", tm))
486: #else
487: if (0 == strftime(buf, p->rmargin, "%B %d, %Y", tm))
488: #endif
489: err(1, "strftime");
490:
491: (void)strlcpy(os, meta->os, p->rmargin);
492:
493: /*
494: * This is /slightly/ different from regular groff output
495: * because we don't have page numbers. Print the following:
496: *
497: * OS MDOCDATE
498: */
499:
1.3 kristaps 500: term_vspace(p);
1.1 kristaps 501:
502: p->flags |= TERMP_NOSPACE | TERMP_NOBREAK;
503: p->rmargin = p->maxrmargin - strlen(buf);
504: p->offset = 0;
505:
1.3 kristaps 506: term_word(p, os);
507: term_flushln(p);
1.1 kristaps 508:
509: p->flags |= TERMP_NOLPAD | TERMP_NOSPACE;
510: p->offset = p->rmargin;
511: p->rmargin = p->maxrmargin;
512: p->flags &= ~TERMP_NOBREAK;
513:
1.3 kristaps 514: term_word(p, buf);
515: term_flushln(p);
1.1 kristaps 516:
517: free(buf);
518: free(os);
519: }
520:
521:
522: static void
1.5 kristaps 523: term_head(struct termp *p, const struct mdoc_meta *meta)
1.1 kristaps 524: {
525: char *buf, *title;
526:
527: p->rmargin = p->maxrmargin;
528: p->offset = 0;
529:
530: if (NULL == (buf = malloc(p->rmargin)))
531: err(1, "malloc");
532: if (NULL == (title = malloc(p->rmargin)))
533: err(1, "malloc");
534:
535: /*
536: * The header is strange. It has three components, which are
537: * really two with the first duplicated. It goes like this:
538: *
539: * IDENTIFIER TITLE IDENTIFIER
540: *
541: * The IDENTIFIER is NAME(SECTION), which is the command-name
542: * (if given, or "unknown" if not) followed by the manual page
543: * section. These are given in `Dt'. The TITLE is a free-form
544: * string depending on the manual volume. If not specified, it
545: * switches on the manual section.
546: */
547:
548: assert(meta->vol);
549: (void)strlcpy(buf, meta->vol, p->rmargin);
550:
551: if (meta->arch) {
552: (void)strlcat(buf, " (", p->rmargin);
553: (void)strlcat(buf, meta->arch, p->rmargin);
554: (void)strlcat(buf, ")", p->rmargin);
555: }
556:
557: (void)snprintf(title, p->rmargin, "%s(%d)",
558: meta->title, meta->msec);
559:
560: p->offset = 0;
561: p->rmargin = (p->maxrmargin - strlen(buf)) / 2;
562: p->flags |= TERMP_NOBREAK | TERMP_NOSPACE;
563:
1.3 kristaps 564: term_word(p, title);
565: term_flushln(p);
1.1 kristaps 566:
567: p->flags |= TERMP_NOLPAD | TERMP_NOSPACE;
568: p->offset = p->rmargin;
569: p->rmargin = p->maxrmargin - strlen(title);
570:
1.3 kristaps 571: term_word(p, buf);
572: term_flushln(p);
1.1 kristaps 573:
574: p->offset = p->rmargin;
575: p->rmargin = p->maxrmargin;
576: p->flags &= ~TERMP_NOBREAK;
577: p->flags |= TERMP_NOLPAD | TERMP_NOSPACE;
578:
1.3 kristaps 579: term_word(p, title);
580: term_flushln(p);
1.1 kristaps 581:
582: p->rmargin = p->maxrmargin;
583: p->offset = 0;
584: p->flags &= ~TERMP_NOSPACE;
585:
586: free(title);
587: free(buf);
588: }
589:
590:
591: /*
592: * Determine the symbol indicated by an escape sequences, that is, one
593: * starting with a backslash. Once done, we pass this value into the
594: * output buffer by way of the symbol table.
595: */
596: static void
1.5 kristaps 597: term_nescape(struct termp *p, const char *word, size_t len)
1.1 kristaps 598: {
599: const char *rhs;
600: size_t sz;
601:
1.3 kristaps 602: if (NULL == (rhs = term_a2ascii(p->symtab, word, len, &sz)))
1.1 kristaps 603: return;
1.5 kristaps 604: term_stringa(p, rhs, sz);
1.1 kristaps 605: }
606:
607:
608: /*
609: * Handle an escape sequence: determine its length and pass it to the
610: * escape-symbol look table. Note that we assume mdoc(3) has validated
611: * the escape sequence (we assert upon badly-formed escape sequences).
612: */
613: static void
1.5 kristaps 614: term_pescape(struct termp *p, const char *word, int *i, int len)
1.1 kristaps 615: {
616: int j;
617:
618: if (++(*i) >= len)
619: return;
620:
621: if ('(' == word[*i]) {
622: (*i)++;
623: if (*i + 1 >= len)
624: return;
625:
1.5 kristaps 626: term_nescape(p, &word[*i], 2);
1.1 kristaps 627: (*i)++;
628: return;
629:
630: } else if ('*' == word[*i]) {
631: (*i)++;
632: if (*i >= len)
633: return;
634:
635: switch (word[*i]) {
636: case ('('):
637: (*i)++;
638: if (*i + 1 >= len)
639: return;
640:
1.5 kristaps 641: term_nescape(p, &word[*i], 2);
1.1 kristaps 642: (*i)++;
643: return;
644: case ('['):
645: break;
646: default:
1.5 kristaps 647: term_nescape(p, &word[*i], 1);
1.1 kristaps 648: return;
649: }
650:
651: } else if ('[' != word[*i]) {
1.5 kristaps 652: term_nescape(p, &word[*i], 1);
1.1 kristaps 653: return;
654: }
655:
656: (*i)++;
657: for (j = 0; word[*i] && ']' != word[*i]; (*i)++, j++)
658: /* Loop... */ ;
659:
660: if (0 == word[*i])
661: return;
662:
1.5 kristaps 663: term_nescape(p, &word[*i - j], (size_t)j);
1.1 kristaps 664: }
665:
666:
667: /*
668: * Handle pwords, partial words, which may be either a single word or a
669: * phrase that cannot be broken down (such as a literal string). This
670: * handles word styling.
671: */
672: static void
1.5 kristaps 673: term_pword(struct termp *p, const char *word, int len)
1.1 kristaps 674: {
675: int i;
676:
1.5 kristaps 677: if (term_isclosedelim(word, len))
678: if ( ! (TERMP_IGNDELIM & p->flags))
679: p->flags |= TERMP_NOSPACE;
680:
1.3 kristaps 681: if ( ! (TERMP_NOSPACE & p->flags))
1.5 kristaps 682: term_chara(p, ' ');
1.1 kristaps 683:
684: if ( ! (p->flags & TERMP_NONOSPACE))
685: p->flags &= ~TERMP_NOSPACE;
686:
687: /*
688: * If ANSI (word-length styling), then apply our style now,
689: * before the word.
690: */
691:
692: for (i = 0; i < len; i++) {
693: if ('\\' == word[i]) {
1.5 kristaps 694: term_pescape(p, word, &i, len);
1.1 kristaps 695: continue;
696: }
697:
698: if (TERMP_STYLE & p->flags) {
699: if (TERMP_BOLD & p->flags) {
1.5 kristaps 700: term_chara(p, word[i]);
701: term_chara(p, 8);
1.1 kristaps 702: }
703: if (TERMP_UNDER & p->flags) {
1.5 kristaps 704: term_chara(p, '_');
705: term_chara(p, 8);
1.1 kristaps 706: }
707: }
708:
1.5 kristaps 709: term_chara(p, word[i]);
1.1 kristaps 710: }
1.5 kristaps 711:
712: if (term_isopendelim(word, len))
713: p->flags |= TERMP_NOSPACE;
1.1 kristaps 714: }
715:
716:
717: /*
1.5 kristaps 718: * Like term_chara() but for arbitrary-length buffers. Resize the
1.1 kristaps 719: * buffer by a factor of two (if the buffer is less than that) or the
720: * buffer's size.
721: */
722: static void
1.5 kristaps 723: term_stringa(struct termp *p, const char *c, size_t sz)
1.1 kristaps 724: {
725: size_t s;
726:
727: if (0 == sz)
728: return;
729:
730: assert(c);
731: if (p->col + sz >= p->maxcols) {
732: if (0 == p->maxcols)
733: p->maxcols = 256;
734: s = sz > p->maxcols * 2 ? sz : p->maxcols * 2;
735: p->buf = realloc(p->buf, s);
736: if (NULL == p->buf)
737: err(1, "realloc");
738: p->maxcols = s;
739: }
740:
741: (void)memcpy(&p->buf[(int)p->col], c, sz);
742: p->col += sz;
743: }
744:
745:
746: /*
747: * Insert a single character into the line-buffer. If the buffer's
748: * space is exceeded, then allocate more space by doubling the buffer
749: * size.
750: */
751: static void
1.5 kristaps 752: term_chara(struct termp *p, char c)
1.1 kristaps 753: {
754: size_t s;
755:
756: if (p->col + 1 >= p->maxcols) {
757: if (0 == p->maxcols)
758: p->maxcols = 256;
759: s = p->maxcols * 2;
760: p->buf = realloc(p->buf, s);
761: if (NULL == p->buf)
762: err(1, "realloc");
763: p->maxcols = s;
764: }
765: p->buf[(int)(p->col)++] = c;
766: }
767:
768:
769: static void
770: sanity(const struct mdoc_node *n)
771: {
772:
773: switch (n->type) {
774: case (MDOC_TEXT):
775: if (n->child)
776: errx(1, "regular form violated (1)");
777: if (NULL == n->parent)
778: errx(1, "regular form violated (2)");
779: if (NULL == n->string)
780: errx(1, "regular form violated (3)");
781: switch (n->parent->type) {
782: case (MDOC_TEXT):
783: /* FALLTHROUGH */
784: case (MDOC_ROOT):
785: errx(1, "regular form violated (4)");
786: /* NOTREACHED */
787: default:
788: break;
789: }
790: break;
791: case (MDOC_ELEM):
792: if (NULL == n->parent)
793: errx(1, "regular form violated (5)");
794: switch (n->parent->type) {
795: case (MDOC_TAIL):
796: /* FALLTHROUGH */
797: case (MDOC_BODY):
798: /* FALLTHROUGH */
799: case (MDOC_HEAD):
800: break;
801: default:
802: errx(1, "regular form violated (6)");
803: /* NOTREACHED */
804: }
805: if (n->child) switch (n->child->type) {
806: case (MDOC_TEXT):
807: break;
808: default:
809: errx(1, "regular form violated (7(");
810: /* NOTREACHED */
811: }
812: break;
813: case (MDOC_HEAD):
814: /* FALLTHROUGH */
815: case (MDOC_BODY):
816: /* FALLTHROUGH */
817: case (MDOC_TAIL):
818: if (NULL == n->parent)
819: errx(1, "regular form violated (8)");
820: if (MDOC_BLOCK != n->parent->type)
821: errx(1, "regular form violated (9)");
822: if (n->child) switch (n->child->type) {
823: case (MDOC_BLOCK):
824: /* FALLTHROUGH */
825: case (MDOC_ELEM):
826: /* FALLTHROUGH */
827: case (MDOC_TEXT):
828: break;
829: default:
830: errx(1, "regular form violated (a)");
831: /* NOTREACHED */
832: }
833: break;
834: case (MDOC_BLOCK):
835: if (NULL == n->parent)
836: errx(1, "regular form violated (b)");
837: if (NULL == n->child)
838: errx(1, "regular form violated (c)");
839: switch (n->parent->type) {
840: case (MDOC_ROOT):
841: /* FALLTHROUGH */
842: case (MDOC_HEAD):
843: /* FALLTHROUGH */
844: case (MDOC_BODY):
845: /* FALLTHROUGH */
846: case (MDOC_TAIL):
847: break;
848: default:
849: errx(1, "regular form violated (d)");
850: /* NOTREACHED */
851: }
852: switch (n->child->type) {
853: case (MDOC_ROOT):
854: /* FALLTHROUGH */
855: case (MDOC_ELEM):
856: errx(1, "regular form violated (e)");
857: /* NOTREACHED */
858: default:
859: break;
860: }
861: break;
862: case (MDOC_ROOT):
863: if (n->parent)
864: errx(1, "regular form violated (f)");
865: if (NULL == n->child)
866: errx(1, "regular form violated (10)");
867: switch (n->child->type) {
868: case (MDOC_BLOCK):
869: break;
870: default:
871: errx(1, "regular form violated (11)");
872: /* NOTREACHED */
873: }
874: break;
875: }
876: }
CVSweb