1 /* $Id: tbl_layout.c,v 1.50 2021/08/10 12:55:04 schwarze Exp $ */
3 * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2012, 2014, 2015, 2017, 2020, 2021
5 * Ingo Schwarze <schwarze@openbsd.org>
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 #include <sys/types.h>
30 #include "mandoc_aux.h"
33 #include "libmandoc.h"
41 static const struct tbl_phrase keys
[] = {
42 { 'c', TBL_CELL_CENTRE
},
43 { 'r', TBL_CELL_RIGHT
},
44 { 'l', TBL_CELL_LEFT
},
45 { 'n', TBL_CELL_NUMBER
},
46 { 's', TBL_CELL_SPAN
},
47 { 'a', TBL_CELL_LONG
},
48 { '^', TBL_CELL_DOWN
},
49 { '-', TBL_CELL_HORIZ
},
50 { '_', TBL_CELL_HORIZ
},
51 { '=', TBL_CELL_DHORIZ
}
54 #define KEYS_MAX ((int)(sizeof(keys)/sizeof(keys[0])))
56 static void mods(struct tbl_node
*, struct tbl_cell
*,
57 int, const char *, int *);
58 static void cell(struct tbl_node
*, struct tbl_row
*,
59 int, const char *, int *);
60 static struct tbl_cell
*cell_alloc(struct tbl_node
*, struct tbl_row
*,
65 mods(struct tbl_node
*tbl
, struct tbl_cell
*cp
,
66 int ln
, const char *p
, int *pos
)
69 unsigned long spacing
;
72 enum mandoc_esc fontesc
;
75 while (p
[*pos
] == ' ' || p
[*pos
] == '\t')
78 /* Row delimiters and cell specifiers end modifier lists. */
80 if (strchr(".,-=^_ACLNRSaclnrs", p
[*pos
]) != NULL
)
83 /* Throw away parenthesised expression. */
87 while (p
[*pos
] && ')' != p
[*pos
])
93 mandoc_msg(MANDOCERR_TBLLAYOUT_PAR
, ln
, *pos
, NULL
);
97 /* Parse numerical spacing from modifier string. */
99 if (isdigit((unsigned char)p
[*pos
])) {
100 if ((spacing
= strtoul(p
+ *pos
, &endptr
, 10)) > 9)
101 mandoc_msg(MANDOCERR_TBLLAYOUT_SPC
, ln
, *pos
,
104 cp
->spacing
= spacing
;
109 switch (tolower((unsigned char)p
[(*pos
)++])) {
111 cp
->font
= ESCAPE_FONTBOLD
;
114 cp
->flags
|= TBL_CELL_BALIGN
;
117 cp
->flags
|= TBL_CELL_EQUAL
;
122 cp
->font
= ESCAPE_FONTITALIC
;
125 mandoc_msg(MANDOCERR_TBLLAYOUT_MOD
, ln
, *pos
, "m");
129 if (p
[*pos
] == '-' || p
[*pos
] == '+')
131 while (isdigit((unsigned char)p
[*pos
]))
135 cp
->flags
|= TBL_CELL_TALIGN
;
138 cp
->flags
|= TBL_CELL_UP
;
142 if (p
[*pos
] == '(') {
144 while (p
[*pos
+ sz
] != '\0' && p
[*pos
+ sz
] != ')')
147 while (isdigit((unsigned char)p
[*pos
+ sz
]))
151 cp
->wstr
= mandoc_strndup(p
+ *pos
, sz
);
158 cp
->flags
|= TBL_CELL_WMAX
;
161 cp
->flags
|= TBL_CELL_WIGN
;
167 mandoc_msg(MANDOCERR_TBLLAYOUT_VERT
,
171 mandoc_msg(MANDOCERR_TBLLAYOUT_CHAR
,
172 ln
, *pos
- 1, "%c", p
[*pos
- 1]);
176 while (p
[*pos
] == ' ' || p
[*pos
] == '\t')
179 /* Ignore parenthised font names for now. */
187 if (strchr(" \t.", p
[*pos
+ isz
]) == NULL
)
190 fontesc
= mandoc_font(p
+ *pos
, isz
);
193 case ESCAPE_FONTPREV
:
195 mandoc_msg(MANDOCERR_FT_BAD
,
196 ln
, *pos
, "TS %s", p
+ *pos
- 1);
207 cell(struct tbl_node
*tbl
, struct tbl_row
*rp
,
208 int ln
, const char *p
, int *pos
)
213 /* Handle leading vertical lines */
215 while (p
[*pos
] == ' ' || p
[*pos
] == '\t' || p
[*pos
] == '|') {
216 if (p
[*pos
] == '|') {
220 mandoc_msg(MANDOCERR_TBLLAYOUT_VERT
,
227 while (p
[*pos
] == ' ' || p
[*pos
] == '\t')
230 if (p
[*pos
] == '.' || p
[*pos
] == '\0')
233 /* Parse the column position (`c', `l', `r', ...). */
235 for (i
= 0; i
< KEYS_MAX
; i
++)
236 if (tolower((unsigned char)p
[*pos
]) == keys
[i
].name
)
240 mandoc_msg(MANDOCERR_TBLLAYOUT_CHAR
,
241 ln
, *pos
, "%c", p
[*pos
]);
247 /* Special cases of spanners. */
249 if (c
== TBL_CELL_SPAN
) {
250 if (rp
->last
== NULL
)
251 mandoc_msg(MANDOCERR_TBLLAYOUT_SPAN
, ln
, *pos
, NULL
);
252 else if (rp
->last
->pos
== TBL_CELL_HORIZ
||
253 rp
->last
->pos
== TBL_CELL_DHORIZ
)
255 } else if (c
== TBL_CELL_DOWN
&& rp
== tbl
->first_row
)
256 mandoc_msg(MANDOCERR_TBLLAYOUT_DOWN
, ln
, *pos
, NULL
);
260 /* Allocate cell then parse its modifiers. */
262 mods(tbl
, cell_alloc(tbl
, rp
, c
), ln
, p
, pos
);
266 tbl_layout(struct tbl_node
*tbl
, int ln
, const char *p
, int pos
)
272 /* Skip whitespace before and after each cell. */
274 while (p
[pos
] == ' ' || p
[pos
] == '\t')
278 case ',': /* Next row on this input line. */
282 case '\0': /* Next row on next input line. */
284 case '.': /* End of layout. */
286 tbl
->part
= TBL_PART_DATA
;
289 * When the layout is completely empty,
290 * default to one left-justified column.
293 if (tbl
->first_row
== NULL
) {
294 tbl
->first_row
= tbl
->last_row
=
295 mandoc_calloc(1, sizeof(*rp
));
297 if (tbl
->first_row
->first
== NULL
) {
298 mandoc_msg(MANDOCERR_TBLLAYOUT_NONE
,
300 cell_alloc(tbl
, tbl
->first_row
,
302 if (tbl
->opts
.lvert
< tbl
->first_row
->vert
)
303 tbl
->opts
.lvert
= tbl
->first_row
->vert
;
308 * Search for the widest line
309 * along the left and right margins.
312 for (rp
= tbl
->first_row
; rp
; rp
= rp
->next
) {
313 if (tbl
->opts
.lvert
< rp
->vert
)
314 tbl
->opts
.lvert
= rp
->vert
;
315 if (rp
->last
!= NULL
&&
316 rp
->last
->col
+ 1 == tbl
->opts
.cols
&&
317 tbl
->opts
.rvert
< rp
->last
->vert
)
318 tbl
->opts
.rvert
= rp
->last
->vert
;
320 /* If the last line is empty, drop it. */
322 if (rp
->next
!= NULL
&&
323 rp
->next
->first
== NULL
) {
335 * If the last line had at least one cell,
336 * start a new one; otherwise, continue it.
340 if (tbl
->last_row
== NULL
||
341 tbl
->last_row
->first
!= NULL
) {
342 rp
= mandoc_calloc(1, sizeof(*rp
));
344 tbl
->last_row
->next
= rp
;
351 cell(tbl
, rp
, ln
, p
, &pos
);
355 static struct tbl_cell
*
356 cell_alloc(struct tbl_node
*tbl
, struct tbl_row
*rp
, enum tbl_cellt pos
)
358 struct tbl_cell
*p
, *pp
;
360 p
= mandoc_calloc(1, sizeof(*p
));
361 p
->spacing
= SIZE_MAX
;
362 p
->font
= ESCAPE_FONTROMAN
;
365 if ((pp
= rp
->last
) != NULL
) {
367 p
->col
= pp
->col
+ 1;
372 if (tbl
->opts
.cols
<= p
->col
)
373 tbl
->opts
.cols
= p
->col
+ 1;