GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/mandoc/eqn_term.c Lines: 88 92 95.7 %
Date: 2017-11-13 Branches: 109 126 86.5 %

Line Branch Exec Source
1
/*	$OpenBSD: eqn_term.c,v 1.13 2017/08/23 21:56:04 schwarze Exp $ */
2
/*
3
 * Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4
 * Copyright (c) 2014, 2015, 2017 Ingo Schwarze <schwarze@openbsd.org>
5
 *
6
 * Permission to use, copy, modify, and distribute this software for any
7
 * purpose with or without fee is hereby granted, provided that the above
8
 * copyright notice and this permission notice appear in all copies.
9
 *
10
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
 */
18
#include <sys/types.h>
19
20
#include <assert.h>
21
#include <ctype.h>
22
#include <stdio.h>
23
#include <stdlib.h>
24
#include <string.h>
25
26
#include "mandoc.h"
27
#include "out.h"
28
#include "term.h"
29
30
static	const enum termfont fontmap[EQNFONT__MAX] = {
31
	TERMFONT_NONE, /* EQNFONT_NONE */
32
	TERMFONT_NONE, /* EQNFONT_ROMAN */
33
	TERMFONT_BOLD, /* EQNFONT_BOLD */
34
	TERMFONT_BOLD, /* EQNFONT_FAT */
35
	TERMFONT_UNDER /* EQNFONT_ITALIC */
36
};
37
38
static void	eqn_box(struct termp *, const struct eqn_box *);
39
40
41
void
42
term_eqn(struct termp *p, const struct eqn_box *bp)
43
{
44
45
204
	eqn_box(p, bp);
46
102
	p->flags &= ~TERMP_NOSPACE;
47
102
}
48
49
static void
50
eqn_box(struct termp *p, const struct eqn_box *bp)
51
{
52
	const struct eqn_box *child;
53
	const char *cp;
54
	int delim;
55
56
	/* Delimiters around this box? */
57
58

2703
	if ((bp->type == EQN_LIST && bp->expectargs > 1) ||
59

1212
	    (bp->type == EQN_PILE && (bp->prev || bp->next)) ||
60

2268
	    (bp->parent != NULL && (bp->parent->pos == EQNPOS_SQRT ||
61
	    /* Diacritic followed by ^ or _. */
62

2094
	    ((bp->top != NULL || bp->bottom != NULL) &&
63
63
	     bp->parent->type == EQN_SUBEXPR &&
64

54
	     bp->parent->pos != EQNPOS_OVER && bp->next != NULL) ||
65
	    /* Nested over, sub, sup, from, to. */
66

1221
	    (bp->type == EQN_SUBEXPR && bp->pos != EQNPOS_SQRT &&
67

168
	     ((bp->parent->type == EQN_LIST && bp->expectargs == 1) ||
68
141
	      (bp->parent->type == EQN_SUBEXPR &&
69
39
	       bp->pos != EQNPOS_SQRT)))))) {
70

192
		if ((bp->parent->type == EQN_SUBEXPR && bp->prev != NULL) ||
71
72
		    (bp->type == EQN_LIST &&
72
48
		     bp->first != NULL &&
73
45
		     bp->first->type != EQN_PILE &&
74
42
		     bp->first->type != EQN_MATRIX &&
75
33
		     bp->prev != NULL &&
76
3
		     (bp->prev->type == EQN_LIST ||
77
		      (bp->prev->type == EQN_TEXT &&
78
		       (*bp->prev->text == '\\' ||
79
		        isalpha((unsigned char)*bp->prev->text))))))
80
42
			p->flags |= TERMP_NOSPACE;
81
237
		term_word(p, bp->left != NULL ? bp->left : "(");
82
111
		p->flags |= TERMP_NOSPACE;
83
		delim = 1;
84
111
	} else
85
		delim = 0;
86
87
	/* Handle Fonts and text. */
88
89
1233
	if (bp->font != EQNFONT_NONE)
90
867
		term_fontpush(p, fontmap[(int)bp->font]);
91
92
1233
	if (bp->text != NULL) {
93
750
		if (strchr("!\"'),.:;?]}", *bp->text) != NULL)
94
21
			p->flags |= TERMP_NOSPACE;
95
750
		term_word(p, bp->text);
96

804
		if ((cp = strchr(bp->text, '\0')) > bp->text &&
97
747
		    (strchr("\"'([{", cp[-1]) != NULL ||
98

1038
		     (bp->prev == NULL && (cp[-1] == '-' ||
99
291
		      (cp >= bp->text + 5 &&
100
54
		       strcmp(cp - 5, "\\[mi]") == 0)))))
101
			p->flags |= TERMP_NOSPACE;
102
	}
103
104
	/* Special box types. */
105
106
1233
	if (bp->pos == EQNPOS_SQRT) {
107
21
		term_word(p, "sqrt");
108
21
		if (bp->first != NULL) {
109
18
			p->flags |= TERMP_NOSPACE;
110
18
			eqn_box(p, bp->first);
111
18
		}
112
1212
	} else if (bp->type == EQN_SUBEXPR) {
113
		child = bp->first;
114
141
		eqn_box(p, child);
115
141
		p->flags |= TERMP_NOSPACE;
116
390
		term_word(p, bp->pos == EQNPOS_OVER ? "/" :
117
297
		    (bp->pos == EQNPOS_SUP ||
118
81
		     bp->pos == EQNPOS_TO) ? "^" : "_");
119
141
		child = child->next;
120
141
		if (child != NULL) {
121
129
			p->flags |= TERMP_NOSPACE;
122
129
			eqn_box(p, child);
123

243
			if (bp->pos == EQNPOS_FROMTO ||
124
114
			    bp->pos == EQNPOS_SUBSUP) {
125
36
				p->flags |= TERMP_NOSPACE;
126
36
				term_word(p, "^");
127
36
				p->flags |= TERMP_NOSPACE;
128
36
				child = child->next;
129
36
				if (child != NULL)
130
30
					eqn_box(p, child);
131
			}
132
		}
133
	} else {
134
		child = bp->first;
135

2148
		if (bp->type == EQN_MATRIX &&
136
1071
		    child != NULL &&
137
6
		    child->type == EQN_LIST &&
138
6
		    child->expectargs > 1)
139
6
			child = child->first;
140
3768
		while (child != NULL) {
141
813
			eqn_box(p,
142

2463
			    bp->type == EQN_PILE &&
143
24
			    child->type == EQN_LIST &&
144
24
			    child->expectargs > 1 &&
145
24
			    child->args == 1 ?
146
24
			    child->first : child);
147
813
			child = child->next;
148
		}
149
	}
150
151
	/* Handle Fonts and diacritics. */
152
153
1233
	if (bp->font != EQNFONT_NONE)
154
867
		term_fontpop(p);
155
1233
	if (bp->top != NULL) {
156
57
		p->flags |= TERMP_NOSPACE;
157
57
		term_word(p, bp->top);
158
57
	}
159
1233
	if (bp->bottom != NULL) {
160
9
		p->flags |= TERMP_NOSPACE;
161
9
		term_word(p, "_");
162
9
	}
163
164
	/* Right delimiter after this box? */
165
166
1233
	if (delim) {
167
111
		p->flags |= TERMP_NOSPACE;
168
237
		term_word(p, bp->right != NULL ? bp->right : ")");
169

192
		if (bp->parent->type == EQN_SUBEXPR && bp->next != NULL)
170
33
			p->flags |= TERMP_NOSPACE;
171
	}
172
1233
}