1 |
|
|
/* $OpenBSD: tbl.c,v 1.23 2017/07/08 17:52:42 schwarze Exp $ */ |
2 |
|
|
/* |
3 |
|
|
* Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv> |
4 |
|
|
* Copyright (c) 2011, 2015 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 <stdio.h> |
22 |
|
|
#include <stdlib.h> |
23 |
|
|
#include <string.h> |
24 |
|
|
#include <time.h> |
25 |
|
|
|
26 |
|
|
#include "mandoc.h" |
27 |
|
|
#include "mandoc_aux.h" |
28 |
|
|
#include "libmandoc.h" |
29 |
|
|
#include "libroff.h" |
30 |
|
|
|
31 |
|
|
|
32 |
|
|
void |
33 |
|
|
tbl_read(struct tbl_node *tbl, int ln, const char *p, int pos) |
34 |
|
|
{ |
35 |
|
|
const char *cp; |
36 |
|
|
int active; |
37 |
|
|
|
38 |
|
|
/* |
39 |
|
|
* In the options section, proceed to the layout section |
40 |
|
|
* after a semicolon, or right away if there is no semicolon. |
41 |
|
|
* Ignore semicolons in arguments. |
42 |
|
|
*/ |
43 |
|
|
|
44 |
✓✓ |
19114 |
if (tbl->part == TBL_PART_OPTS) { |
45 |
|
2314 |
tbl->part = TBL_PART_LAYOUT; |
46 |
|
|
active = 1; |
47 |
✓✓ |
61502 |
for (cp = p + pos; *cp != '\0'; cp++) { |
48 |
✓✓✓✓
|
35011 |
switch (*cp) { |
49 |
|
|
case '(': |
50 |
|
|
active = 0; |
51 |
|
2164 |
continue; |
52 |
|
|
case ')': |
53 |
|
|
active = 1; |
54 |
|
2164 |
continue; |
55 |
|
|
case ';': |
56 |
✗✓ |
2246 |
if (active) |
57 |
|
|
break; |
58 |
|
|
continue; |
59 |
|
|
default: |
60 |
|
|
continue; |
61 |
|
|
} |
62 |
|
|
break; |
63 |
|
|
} |
64 |
✓✓ |
2314 |
if (*cp == ';') { |
65 |
|
2246 |
tbl_option(tbl, ln, p, &pos); |
66 |
✓✓ |
2246 |
if (p[pos] == '\0') |
67 |
|
2174 |
return; |
68 |
|
|
} |
69 |
|
|
} |
70 |
|
|
|
71 |
|
|
/* Process the other section types. */ |
72 |
|
|
|
73 |
✓✓✓ |
16940 |
switch (tbl->part) { |
74 |
|
|
case TBL_PART_LAYOUT: |
75 |
|
6872 |
tbl_layout(tbl, ln, p, pos); |
76 |
|
6872 |
break; |
77 |
|
|
case TBL_PART_CDATA: |
78 |
|
1140 |
tbl_cdata(tbl, ln, p, pos); |
79 |
|
1140 |
break; |
80 |
|
|
default: |
81 |
|
8928 |
tbl_data(tbl, ln, p, pos); |
82 |
|
8928 |
break; |
83 |
|
|
} |
84 |
|
36054 |
} |
85 |
|
|
|
86 |
|
|
struct tbl_node * |
87 |
|
|
tbl_alloc(int pos, int line, struct mparse *parse) |
88 |
|
|
{ |
89 |
|
|
struct tbl_node *tbl; |
90 |
|
|
|
91 |
|
4628 |
tbl = mandoc_calloc(1, sizeof(*tbl)); |
92 |
|
2314 |
tbl->line = line; |
93 |
|
2314 |
tbl->pos = pos; |
94 |
|
2314 |
tbl->parse = parse; |
95 |
|
2314 |
tbl->part = TBL_PART_OPTS; |
96 |
|
2314 |
tbl->opts.tab = '\t'; |
97 |
|
2314 |
tbl->opts.decimal = '.'; |
98 |
|
2314 |
return tbl; |
99 |
|
|
} |
100 |
|
|
|
101 |
|
|
void |
102 |
|
|
tbl_free(struct tbl_node *tbl) |
103 |
|
|
{ |
104 |
|
|
struct tbl_row *rp; |
105 |
|
|
struct tbl_cell *cp; |
106 |
|
|
struct tbl_span *sp; |
107 |
|
|
struct tbl_dat *dp; |
108 |
|
|
|
109 |
✓✓ |
20692 |
while ((rp = tbl->first_row) != NULL) { |
110 |
|
6875 |
tbl->first_row = rp->next; |
111 |
✓✓ |
49284 |
while (rp->first != NULL) { |
112 |
|
|
cp = rp->first; |
113 |
|
17767 |
rp->first = cp->next; |
114 |
|
17767 |
free(cp->wstr); |
115 |
|
17767 |
free(cp); |
116 |
|
|
} |
117 |
|
6875 |
free(rp); |
118 |
|
|
} |
119 |
|
|
|
120 |
✓✓ |
22512 |
while ((sp = tbl->first_span) != NULL) { |
121 |
|
8942 |
tbl->first_span = sp->next; |
122 |
✓✓ |
64244 |
while (sp->first != NULL) { |
123 |
|
|
dp = sp->first; |
124 |
|
23180 |
sp->first = dp->next; |
125 |
|
23180 |
free(dp->string); |
126 |
|
23180 |
free(dp); |
127 |
|
|
} |
128 |
|
8942 |
free(sp); |
129 |
|
|
} |
130 |
|
|
|
131 |
|
2314 |
free(tbl); |
132 |
|
2314 |
} |
133 |
|
|
|
134 |
|
|
void |
135 |
|
|
tbl_restart(int line, int pos, struct tbl_node *tbl) |
136 |
|
|
{ |
137 |
✓✗ |
12 |
if (tbl->part == TBL_PART_CDATA) |
138 |
|
6 |
mandoc_msg(MANDOCERR_TBLDATA_BLK, tbl->parse, |
139 |
|
|
line, pos, "T&"); |
140 |
|
|
|
141 |
|
6 |
tbl->part = TBL_PART_LAYOUT; |
142 |
|
6 |
tbl->line = line; |
143 |
|
6 |
tbl->pos = pos; |
144 |
|
6 |
} |
145 |
|
|
|
146 |
|
|
const struct tbl_span * |
147 |
|
|
tbl_span(struct tbl_node *tbl) |
148 |
|
|
{ |
149 |
|
|
struct tbl_span *span; |
150 |
|
|
|
151 |
✗✓ |
56112 |
assert(tbl); |
152 |
✓✓ |
56112 |
span = tbl->current_span ? tbl->current_span->next |
153 |
|
11348 |
: tbl->first_span; |
154 |
✓✓ |
28056 |
if (span) |
155 |
|
8942 |
tbl->current_span = span; |
156 |
|
28056 |
return span; |
157 |
|
|
} |
158 |
|
|
|
159 |
|
|
int |
160 |
|
|
tbl_end(struct tbl_node *tbl) |
161 |
|
|
{ |
162 |
|
|
struct tbl_span *sp; |
163 |
|
|
|
164 |
✓✓ |
4628 |
if (tbl->part == TBL_PART_CDATA) |
165 |
|
12 |
mandoc_msg(MANDOCERR_TBLDATA_BLK, tbl->parse, |
166 |
|
6 |
tbl->line, tbl->pos, "TE"); |
167 |
|
|
|
168 |
|
2314 |
sp = tbl->first_span; |
169 |
✓✓✓✓
|
9406 |
while (sp != NULL && sp->first == NULL) |
170 |
|
39 |
sp = sp->next; |
171 |
✓✓ |
2314 |
if (sp == NULL) { |
172 |
|
12 |
mandoc_msg(MANDOCERR_TBLDATA_NONE, tbl->parse, |
173 |
|
6 |
tbl->line, tbl->pos, NULL); |
174 |
|
6 |
return 0; |
175 |
|
|
} |
176 |
|
2308 |
return 1; |
177 |
|
2314 |
} |