]>
git.cameronkatri.com Git - mandoc.git/blob - tbl_layout.c
1 /* $Id: tbl_layout.c,v 1.49 2020/09/01 18:25:28 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>
29 #include "mandoc_aux.h"
32 #include "libmandoc.h"
40 static const struct tbl_phrase keys
[] = {
41 { 'c', TBL_CELL_CENTRE
},
42 { 'r', TBL_CELL_RIGHT
},
43 { 'l', TBL_CELL_LEFT
},
44 { 'n', TBL_CELL_NUMBER
},
45 { 's', TBL_CELL_SPAN
},
46 { 'a', TBL_CELL_LONG
},
47 { '^', TBL_CELL_DOWN
},
48 { '-', TBL_CELL_HORIZ
},
49 { '_', TBL_CELL_HORIZ
},
50 { '=', TBL_CELL_DHORIZ
}
53 #define KEYS_MAX ((int)(sizeof(keys)/sizeof(keys[0])))
55 static void mods(struct tbl_node
*, struct tbl_cell
*,
56 int, const char *, int *);
57 static void cell(struct tbl_node
*, struct tbl_row
*,
58 int, const char *, int *);
59 static struct tbl_cell
*cell_alloc(struct tbl_node
*, struct tbl_row
*,
64 mods(struct tbl_node
*tbl
, struct tbl_cell
*cp
,
65 int ln
, const char *p
, int *pos
)
68 unsigned long spacing
;
72 while (p
[*pos
] == ' ' || p
[*pos
] == '\t')
75 /* Row delimiters and cell specifiers end modifier lists. */
77 if (strchr(".,-=^_ACLNRSaclnrs", p
[*pos
]) != NULL
)
80 /* Throw away parenthesised expression. */
84 while (p
[*pos
] && ')' != p
[*pos
])
90 mandoc_msg(MANDOCERR_TBLLAYOUT_PAR
, ln
, *pos
, NULL
);
94 /* Parse numerical spacing from modifier string. */
96 if (isdigit((unsigned char)p
[*pos
])) {
97 if ((spacing
= strtoul(p
+ *pos
, &endptr
, 10)) > 9)
98 mandoc_msg(MANDOCERR_TBLLAYOUT_SPC
, ln
, *pos
,
101 cp
->spacing
= spacing
;
106 switch (tolower((unsigned char)p
[(*pos
)++])) {
108 cp
->flags
|= TBL_CELL_BOLD
;
111 cp
->flags
|= TBL_CELL_BALIGN
;
114 cp
->flags
|= TBL_CELL_EQUAL
;
119 cp
->flags
|= TBL_CELL_ITALIC
;
122 mandoc_msg(MANDOCERR_TBLLAYOUT_MOD
, ln
, *pos
, "m");
126 if (p
[*pos
] == '-' || p
[*pos
] == '+')
128 while (isdigit((unsigned char)p
[*pos
]))
132 cp
->flags
|= TBL_CELL_TALIGN
;
135 cp
->flags
|= TBL_CELL_UP
;
139 if (p
[*pos
] == '(') {
141 while (p
[*pos
+ sz
] != '\0' && p
[*pos
+ sz
] != ')')
144 while (isdigit((unsigned char)p
[*pos
+ sz
]))
148 cp
->wstr
= mandoc_strndup(p
+ *pos
, sz
);
155 cp
->flags
|= TBL_CELL_WMAX
;
158 cp
->flags
|= TBL_CELL_WIGN
;
164 mandoc_msg(MANDOCERR_TBLLAYOUT_VERT
,
168 mandoc_msg(MANDOCERR_TBLLAYOUT_CHAR
,
169 ln
, *pos
- 1, "%c", p
[*pos
- 1]);
173 /* Ignore parenthised font names for now. */
178 /* Support only one-character font-names for now. */
180 if (p
[*pos
] == '\0' || (p
[*pos
+ 1] != ' ' && p
[*pos
+ 1] != '.')) {
181 mandoc_msg(MANDOCERR_FT_BAD
,
182 ln
, *pos
, "TS %s", p
+ *pos
- 1);
190 switch (p
[(*pos
)++]) {
193 cp
->flags
|= TBL_CELL_BOLD
;
197 cp
->flags
|= TBL_CELL_ITALIC
;
203 mandoc_msg(MANDOCERR_FT_BAD
,
204 ln
, *pos
- 1, "TS f%c", p
[*pos
- 1]);
210 cell(struct tbl_node
*tbl
, struct tbl_row
*rp
,
211 int ln
, const char *p
, int *pos
)
216 /* Handle leading vertical lines */
218 while (p
[*pos
] == ' ' || p
[*pos
] == '\t' || p
[*pos
] == '|') {
219 if (p
[*pos
] == '|') {
223 mandoc_msg(MANDOCERR_TBLLAYOUT_VERT
,
230 while (p
[*pos
] == ' ' || p
[*pos
] == '\t')
233 if (p
[*pos
] == '.' || p
[*pos
] == '\0')
236 /* Parse the column position (`c', `l', `r', ...). */
238 for (i
= 0; i
< KEYS_MAX
; i
++)
239 if (tolower((unsigned char)p
[*pos
]) == keys
[i
].name
)
243 mandoc_msg(MANDOCERR_TBLLAYOUT_CHAR
,
244 ln
, *pos
, "%c", p
[*pos
]);
250 /* Special cases of spanners. */
252 if (c
== TBL_CELL_SPAN
) {
253 if (rp
->last
== NULL
)
254 mandoc_msg(MANDOCERR_TBLLAYOUT_SPAN
, ln
, *pos
, NULL
);
255 else if (rp
->last
->pos
== TBL_CELL_HORIZ
||
256 rp
->last
->pos
== TBL_CELL_DHORIZ
)
258 } else if (c
== TBL_CELL_DOWN
&& rp
== tbl
->first_row
)
259 mandoc_msg(MANDOCERR_TBLLAYOUT_DOWN
, ln
, *pos
, NULL
);
263 /* Allocate cell then parse its modifiers. */
265 mods(tbl
, cell_alloc(tbl
, rp
, c
), ln
, p
, pos
);
269 tbl_layout(struct tbl_node
*tbl
, int ln
, const char *p
, int pos
)
275 /* Skip whitespace before and after each cell. */
277 while (p
[pos
] == ' ' || p
[pos
] == '\t')
281 case ',': /* Next row on this input line. */
285 case '\0': /* Next row on next input line. */
287 case '.': /* End of layout. */
289 tbl
->part
= TBL_PART_DATA
;
292 * When the layout is completely empty,
293 * default to one left-justified column.
296 if (tbl
->first_row
== NULL
) {
297 tbl
->first_row
= tbl
->last_row
=
298 mandoc_calloc(1, sizeof(*rp
));
300 if (tbl
->first_row
->first
== NULL
) {
301 mandoc_msg(MANDOCERR_TBLLAYOUT_NONE
,
303 cell_alloc(tbl
, tbl
->first_row
,
305 if (tbl
->opts
.lvert
< tbl
->first_row
->vert
)
306 tbl
->opts
.lvert
= tbl
->first_row
->vert
;
311 * Search for the widest line
312 * along the left and right margins.
315 for (rp
= tbl
->first_row
; rp
; rp
= rp
->next
) {
316 if (tbl
->opts
.lvert
< rp
->vert
)
317 tbl
->opts
.lvert
= rp
->vert
;
318 if (rp
->last
!= NULL
&&
319 rp
->last
->col
+ 1 == tbl
->opts
.cols
&&
320 tbl
->opts
.rvert
< rp
->last
->vert
)
321 tbl
->opts
.rvert
= rp
->last
->vert
;
323 /* If the last line is empty, drop it. */
325 if (rp
->next
!= NULL
&&
326 rp
->next
->first
== NULL
) {
338 * If the last line had at least one cell,
339 * start a new one; otherwise, continue it.
343 if (tbl
->last_row
== NULL
||
344 tbl
->last_row
->first
!= NULL
) {
345 rp
= mandoc_calloc(1, sizeof(*rp
));
347 tbl
->last_row
->next
= rp
;
354 cell(tbl
, rp
, ln
, p
, &pos
);
358 static struct tbl_cell
*
359 cell_alloc(struct tbl_node
*tbl
, struct tbl_row
*rp
, enum tbl_cellt pos
)
361 struct tbl_cell
*p
, *pp
;
363 p
= mandoc_calloc(1, sizeof(*p
));
364 p
->spacing
= SIZE_MAX
;
367 if ((pp
= rp
->last
) != NULL
) {
369 p
->col
= pp
->col
+ 1;
374 if (tbl
->opts
.cols
<= p
->col
)
375 tbl
->opts
.cols
= p
->col
+ 1;