1 /* $Id: tbl_layout.c,v 1.42 2017/06/08 18:11:22 schwarze Exp $ */
3 * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2012, 2014, 2015, 2017 Ingo Schwarze <schwarze@openbsd.org>
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.
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.
20 #include <sys/types.h>
28 #include "mandoc_aux.h"
29 #include "libmandoc.h"
37 static const struct tbl_phrase keys[] = {
38 { 'c', TBL_CELL_CENTRE },
39 { 'r', TBL_CELL_RIGHT },
40 { 'l', TBL_CELL_LEFT },
41 { 'n', TBL_CELL_NUMBER },
42 { 's', TBL_CELL_SPAN },
43 { 'a', TBL_CELL_LONG },
44 { '^', TBL_CELL_DOWN },
45 { '-', TBL_CELL_HORIZ },
46 { '_', TBL_CELL_HORIZ },
47 { '=', TBL_CELL_DHORIZ }
50 #define KEYS_MAX ((int)(sizeof(keys)/sizeof(keys[0])))
52 static void mods(struct tbl_node *, struct tbl_cell *,
53 int, const char *, int *);
54 static void cell(struct tbl_node *, struct tbl_row *,
55 int, const char *, int *);
56 static struct tbl_cell *cell_alloc(struct tbl_node *, struct tbl_row *,
61 mods(struct tbl_node *tbl, struct tbl_cell *cp,
62 int ln, const char *p, int *pos)
68 while (p[*pos] == ' ' || p[*pos] == '\t')
71 /* Row delimiters and cell specifiers end modifier lists. */
73 if (strchr(".,-=^_ACLNRSaclnrs", p[*pos]) != NULL)
76 /* Throw away parenthesised expression. */
80 while (p[*pos] && ')' != p[*pos])
86 mandoc_msg(MANDOCERR_TBLLAYOUT_PAR, tbl->parse,
91 /* Parse numerical spacing from modifier string. */
93 if (isdigit((unsigned char)p[*pos])) {
94 cp->spacing = strtoull(p + *pos, &endptr, 10);
99 switch (tolower((unsigned char)p[(*pos)++])) {
101 cp->flags |= TBL_CELL_BOLD;
104 cp->flags |= TBL_CELL_BALIGN;
107 cp->flags |= TBL_CELL_EQUAL;
112 cp->flags |= TBL_CELL_ITALIC;
115 mandoc_msg(MANDOCERR_TBLLAYOUT_MOD, tbl->parse,
120 if (p[*pos] == '-' || p[*pos] == '+')
122 while (isdigit((unsigned char)p[*pos]))
126 cp->flags |= TBL_CELL_TALIGN;
129 cp->flags |= TBL_CELL_UP;
133 if (p[*pos] == '(') {
135 while (p[*pos + sz] != '\0' && p[*pos + sz] != ')')
138 while (isdigit((unsigned char)p[*pos + sz]))
142 cp->wstr = mandoc_strndup(p + *pos, sz);
149 cp->flags |= TBL_CELL_WMAX;
152 cp->flags |= TBL_CELL_WIGN;
158 mandoc_msg(MANDOCERR_TBLLAYOUT_VERT,
159 tbl->parse, ln, *pos - 1, NULL);
162 mandoc_vmsg(MANDOCERR_TBLLAYOUT_CHAR, tbl->parse,
163 ln, *pos - 1, "%c", p[*pos - 1]);
167 /* Ignore parenthised font names for now. */
172 /* Support only one-character font-names for now. */
174 if (p[*pos] == '\0' || (p[*pos + 1] != ' ' && p[*pos + 1] != '.')) {
175 mandoc_vmsg(MANDOCERR_FT_BAD, tbl->parse,
176 ln, *pos, "TS %s", p + *pos - 1);
184 switch (p[(*pos)++]) {
187 cp->flags |= TBL_CELL_BOLD;
191 cp->flags |= TBL_CELL_ITALIC;
197 mandoc_vmsg(MANDOCERR_FT_BAD, tbl->parse,
198 ln, *pos - 1, "TS f%c", p[*pos - 1]);
204 cell(struct tbl_node *tbl, struct tbl_row *rp,
205 int ln, const char *p, int *pos)
210 /* Handle leading vertical lines */
212 while (p[*pos] == ' ' || p[*pos] == '\t' || p[*pos] == '|') {
213 if (p[*pos] == '|') {
217 mandoc_msg(MANDOCERR_TBLLAYOUT_VERT,
218 tbl->parse, ln, *pos, NULL);
224 while (p[*pos] == ' ' || p[*pos] == '\t')
227 if (p[*pos] == '.' || p[*pos] == '\0')
230 /* Parse the column position (`c', `l', `r', ...). */
232 for (i = 0; i < KEYS_MAX; i++)
233 if (tolower((unsigned char)p[*pos]) == keys[i].name)
237 mandoc_vmsg(MANDOCERR_TBLLAYOUT_CHAR, tbl->parse,
238 ln, *pos, "%c", p[*pos]);
244 /* Special cases of spanners. */
246 if (c == TBL_CELL_SPAN) {
247 if (rp->last == NULL)
248 mandoc_msg(MANDOCERR_TBLLAYOUT_SPAN,
249 tbl->parse, ln, *pos, NULL);
250 else if (rp->last->pos == TBL_CELL_HORIZ ||
251 rp->last->pos == TBL_CELL_DHORIZ)
253 } else if (c == TBL_CELL_DOWN && rp == tbl->first_row)
254 mandoc_msg(MANDOCERR_TBLLAYOUT_DOWN,
255 tbl->parse, ln, *pos, NULL);
259 /* Allocate cell then parse its modifiers. */
261 mods(tbl, cell_alloc(tbl, rp, c), ln, p, pos);
265 tbl_layout(struct tbl_node *tbl, int ln, const char *p, int pos)
271 /* Skip whitespace before and after each cell. */
273 while (p[pos] == ' ' || p[pos] == '\t')
277 case ',': /* Next row on this input line. */
281 case '\0': /* Next row on next input line. */
283 case '.': /* End of layout. */
285 tbl->part = TBL_PART_DATA;
288 * When the layout is completely empty,
289 * default to one left-justified column.
292 if (tbl->first_row == NULL) {
293 tbl->first_row = tbl->last_row =
294 mandoc_calloc(1, sizeof(*rp));
296 if (tbl->first_row->first == NULL) {
297 mandoc_msg(MANDOCERR_TBLLAYOUT_NONE,
298 tbl->parse, ln, pos, NULL);
299 cell_alloc(tbl, tbl->first_row,
305 * Search for the widest line
306 * along the left and right margins.
309 for (rp = tbl->first_row; rp; rp = rp->next) {
310 if (tbl->opts.lvert < rp->vert)
311 tbl->opts.lvert = rp->vert;
312 if (rp->last != NULL &&
313 rp->last->col + 1 == tbl->opts.cols &&
314 tbl->opts.rvert < rp->last->vert)
315 tbl->opts.rvert = rp->last->vert;
317 /* If the last line is empty, drop it. */
319 if (rp->next != NULL &&
320 rp->next->first == NULL) {
332 * If the last line had at least one cell,
333 * start a new one; otherwise, continue it.
337 if (tbl->last_row == NULL ||
338 tbl->last_row->first != NULL) {
339 rp = mandoc_calloc(1, sizeof(*rp));
341 tbl->last_row->next = rp;
348 cell(tbl, rp, ln, p, &pos);
352 static struct tbl_cell *
353 cell_alloc(struct tbl_node *tbl, struct tbl_row *rp, enum tbl_cellt pos)
355 struct tbl_cell *p, *pp;
357 p = mandoc_calloc(1, sizeof(*p));
360 if ((pp = rp->last) != NULL) {
362 p->col = pp->col + 1;
367 if (tbl->opts.cols <= p->col)
368 tbl->opts.cols = p->col + 1;