1 /* $OpenBSD: tbl.c,v 1.28 2025/01/05 18:03:51 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_aux.h"
27 #include "mandoc.h"
28 #include "tbl.h"
29 #include "libmandoc.h"
30 #include "tbl_parse.h"
31 #include "tbl_int.h"
32
33
34 void
tbl_read(struct tbl_node * tbl,int ln,const char * p,int pos)35 tbl_read(struct tbl_node *tbl, int ln, const char *p, int pos)
36 {
37 const char *cp;
38 int active;
39
40 /*
41 * In the options section, proceed to the layout section
42 * after a semicolon, or right away if there is no semicolon.
43 * Ignore semicolons in arguments.
44 */
45
46 if (tbl->part == TBL_PART_OPTS) {
47 tbl->part = TBL_PART_LAYOUT;
48 active = 1;
49 for (cp = p + pos; *cp != '\0'; cp++) {
50 switch (*cp) {
51 case '(':
52 active = 0;
53 continue;
54 case ')':
55 active = 1;
56 continue;
57 case ';':
58 if (active)
59 break;
60 continue;
61 default:
62 continue;
63 }
64 break;
65 }
66 if (*cp == ';') {
67 tbl_option(tbl, ln, p, &pos);
68 if (p[pos] == '\0')
69 return;
70 }
71 }
72
73 /* Process the other section types. */
74
75 switch (tbl->part) {
76 case TBL_PART_LAYOUT:
77 tbl_layout(tbl, ln, p, pos);
78 break;
79 case TBL_PART_CDATA:
80 tbl_cdata(tbl, ln, p, pos);
81 break;
82 default:
83 tbl_data(tbl, ln, p, pos);
84 break;
85 }
86 }
87
88 struct tbl_node *
tbl_alloc(int pos,int line,struct tbl_node * last_tbl)89 tbl_alloc(int pos, int line, struct tbl_node *last_tbl)
90 {
91 struct tbl_node *tbl;
92
93 tbl = mandoc_calloc(1, sizeof(*tbl));
94 if (last_tbl != NULL)
95 last_tbl->next = tbl;
96 tbl->line = line;
97 tbl->pos = pos;
98 tbl->part = TBL_PART_OPTS;
99 tbl->opts.tab = '\t';
100 tbl->opts.decimal = '.';
101 return tbl;
102 }
103
104 void
tbl_free(struct tbl_node * tbl)105 tbl_free(struct tbl_node *tbl)
106 {
107 struct tbl_node *old_tbl;
108 struct tbl_row *rp;
109 struct tbl_cell *cp;
110 struct tbl_span *sp;
111 struct tbl_dat *dp;
112
113 while (tbl != NULL) {
114 while ((rp = tbl->first_row) != NULL) {
115 tbl->first_row = rp->next;
116 while (rp->first != NULL) {
117 cp = rp->first;
118 rp->first = cp->next;
119 free(cp);
120 }
121 free(rp);
122 }
123 while ((sp = tbl->first_span) != NULL) {
124 tbl->first_span = sp->next;
125 while (sp->first != NULL) {
126 dp = sp->first;
127 sp->first = dp->next;
128 free(dp->string);
129 free(dp);
130 }
131 free(sp);
132 }
133 old_tbl = tbl;
134 tbl = tbl->next;
135 free(old_tbl);
136 }
137 }
138
139 void
tbl_restart(int line,int pos,struct tbl_node * tbl)140 tbl_restart(int line, int pos, struct tbl_node *tbl)
141 {
142 if (tbl->part == TBL_PART_CDATA)
143 mandoc_msg(MANDOCERR_TBLDATA_BLK, line, pos, "T&");
144
145 tbl->part = TBL_PART_LAYOUT;
146 tbl->line = line;
147 tbl->pos = pos;
148 }
149
150 struct tbl_span *
tbl_span(struct tbl_node * tbl)151 tbl_span(struct tbl_node *tbl)
152 {
153 struct tbl_span *span;
154
155 span = tbl->current_span ? tbl->current_span->next
156 : tbl->first_span;
157 if (span != NULL)
158 tbl->current_span = span;
159 return span;
160 }
161
162 int
tbl_end(struct tbl_node * tbl,int still_open)163 tbl_end(struct tbl_node *tbl, int still_open)
164 {
165 struct tbl_span *sp;
166
167 if (still_open)
168 mandoc_msg(MANDOCERR_BLK_NOEND, tbl->line, tbl->pos, "TS");
169 else if (tbl->part == TBL_PART_CDATA)
170 mandoc_msg(MANDOCERR_TBLDATA_BLK, tbl->line, tbl->pos, "TE");
171
172 sp = tbl->first_span;
173 while (sp != NULL && sp->first == NULL)
174 sp = sp->next;
175 if (sp == NULL) {
176 mandoc_msg(MANDOCERR_TBLDATA_NONE, tbl->line, tbl->pos, NULL);
177 return 0;
178 }
179 return 1;
180 }
181