]>
git.cameronkatri.com Git - mandoc.git/blob - tbl_layout.c
1 /* $Id: tbl_layout.c,v 1.20 2011/05/17 13:11:40 kristaps Exp $ */
3 * Copyright (c) 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
28 #include "libmandoc.h"
37 * FIXME: we can make this parse a lot nicer by, when an error is
38 * encountered in a layout key, bailing to the next key (i.e. to the
39 * next whitespace then continuing).
44 static const struct tbl_phrase keys
[KEYS_MAX
] = {
45 { 'c', TBL_CELL_CENTRE
},
46 { 'r', TBL_CELL_RIGHT
},
47 { 'l', TBL_CELL_LEFT
},
48 { 'n', TBL_CELL_NUMBER
},
49 { 's', TBL_CELL_SPAN
},
50 { 'a', TBL_CELL_LONG
},
51 { '^', TBL_CELL_DOWN
},
52 { '-', TBL_CELL_HORIZ
},
53 { '_', TBL_CELL_HORIZ
},
54 { '=', TBL_CELL_DHORIZ
},
55 { '|', TBL_CELL_VERT
}
58 static int mods(struct tbl_node
*, struct tbl_cell
*,
59 int, const char *, int *);
60 static int cell(struct tbl_node
*, struct tbl_row
*,
61 int, const char *, int *);
62 static void row(struct tbl_node
*, int, const char *, int *);
63 static struct tbl_cell
*cell_alloc(struct tbl_node
*,
64 struct tbl_row
*, enum tbl_cellt
);
65 static void head_adjust(const struct tbl_cell
*,
69 mods(struct tbl_node
*tbl
, struct tbl_cell
*cp
,
70 int ln
, const char *p
, int *pos
)
75 /* Not all types accept modifiers. */
80 case (TBL_CELL_HORIZ
):
82 case (TBL_CELL_DHORIZ
):
86 case (TBL_CELL_DVERT
):
94 * XXX: since, at least for now, modifiers are non-conflicting
95 * (are separable by value, regardless of position), we let
96 * modifiers come in any order. The existing tbl doesn't let
114 /* Throw away parenthesised expression. */
116 if ('(' == p
[*pos
]) {
118 while (p
[*pos
] && ')' != p
[*pos
])
120 if (')' == p
[*pos
]) {
124 mandoc_msg(MANDOCERR_TBLLAYOUT
,
125 tbl
->parse
, ln
, *pos
, NULL
);
129 /* Parse numerical spacing from modifier string. */
131 if (isdigit((unsigned char)p
[*pos
])) {
132 for (i
= 0; i
< 4; i
++) {
133 if ( ! isdigit((unsigned char)p
[*pos
+ i
]))
135 buf
[i
] = p
[*pos
+ i
];
139 /* No greater than 4 digits. */
142 mandoc_msg(MANDOCERR_TBLLAYOUT
, tbl
->parse
,
148 cp
->spacing
= (size_t)atoi(buf
);
154 /* TODO: GNU has many more extensions. */
156 switch (tolower((unsigned char)p
[(*pos
)++])) {
158 cp
->flags
|= TBL_CELL_WIGN
;
161 cp
->flags
|= TBL_CELL_UP
;
164 cp
->flags
|= TBL_CELL_EQUAL
;
167 cp
->flags
|= TBL_CELL_TALIGN
;
170 cp
->flags
|= TBL_CELL_BALIGN
;
172 case ('w'): /* XXX for now, ignore minimal column width */
182 mandoc_msg(MANDOCERR_TBLLAYOUT
, tbl
->parse
,
187 switch (tolower((unsigned char)p
[(*pos
)++])) {
189 cp
->flags
|= TBL_CELL_BOLD
;
192 cp
->flags
|= TBL_CELL_ITALIC
;
198 mandoc_msg(MANDOCERR_TBLLAYOUT
, tbl
->parse
,
204 cell(struct tbl_node
*tbl
, struct tbl_row
*rp
,
205 int ln
, const char *p
, int *pos
)
210 /* Parse the column position (`r', `R', `|', ...). */
212 for (i
= 0; i
< KEYS_MAX
; i
++)
213 if (tolower((unsigned char)p
[*pos
]) == keys
[i
].name
)
217 mandoc_msg(MANDOCERR_TBLLAYOUT
, tbl
->parse
,
225 * If a span cell is found first, raise a warning and abort the
226 * parse. If a span cell is found and the last layout element
227 * isn't a "normal" layout, bail.
229 * FIXME: recover from this somehow?
232 if (TBL_CELL_SPAN
== c
) {
233 if (NULL
== rp
->first
) {
234 mandoc_msg(MANDOCERR_TBLLAYOUT
, tbl
->parse
,
238 switch (rp
->last
->pos
) {
239 case (TBL_CELL_VERT
):
240 case (TBL_CELL_DVERT
):
241 case (TBL_CELL_HORIZ
):
242 case (TBL_CELL_DHORIZ
):
243 mandoc_msg(MANDOCERR_TBLLAYOUT
, tbl
->parse
,
252 * If a vertical spanner is found, we may not be in the first
256 if (TBL_CELL_DOWN
== c
&& rp
== tbl
->first_row
) {
257 mandoc_msg(MANDOCERR_TBLLAYOUT
, tbl
->parse
, ln
, *pos
, NULL
);
263 /* Extra check for the double-vertical. */
265 if (TBL_CELL_VERT
== c
&& '|' == p
[*pos
]) {
270 /* Disallow adjacent spacers. */
272 if (rp
->last
&& (TBL_CELL_VERT
== c
|| TBL_CELL_DVERT
== c
) &&
273 (TBL_CELL_VERT
== rp
->last
->pos
||
274 TBL_CELL_DVERT
== rp
->last
->pos
)) {
275 mandoc_msg(MANDOCERR_TBLLAYOUT
, tbl
->parse
, ln
, *pos
- 1, NULL
);
279 /* Allocate cell then parse its modifiers. */
281 return(mods(tbl
, cell_alloc(tbl
, rp
, c
), ln
, p
, pos
));
286 row(struct tbl_node
*tbl
, int ln
, const char *p
, int *pos
)
291 * EBNF describing this section:
293 * row ::= row_list [:space:]* [.]?[\n]
294 * row_list ::= [:space:]* row_elem row_tail
295 * row_tail ::= [:space:]*[,] row_list |
297 * row_elem ::= [\t\ ]*[:alpha:]+
300 rp
= mandoc_calloc(1, sizeof(struct tbl_row
));
302 tbl
->last_row
->next
= rp
;
305 tbl
->last_row
= tbl
->first_row
= rp
;
308 while (isspace((unsigned char)p
[*pos
]))
311 /* Safely exit layout context. */
313 if ('.' == p
[*pos
]) {
314 tbl
->part
= TBL_PART_DATA
;
315 if (NULL
== tbl
->first_row
)
316 mandoc_msg(MANDOCERR_TBLNOLAYOUT
, tbl
->parse
,
322 /* End (and possibly restart) a row. */
324 if (',' == p
[*pos
]) {
327 } else if ('\0' == p
[*pos
])
330 if ( ! cell(tbl
, rp
, ln
, p
, pos
))
338 tbl_layout(struct tbl_node
*tbl
, int ln
, const char *p
)
343 row(tbl
, ln
, p
, &pos
);
345 /* Always succeed. */
349 static struct tbl_cell
*
350 cell_alloc(struct tbl_node
*tbl
, struct tbl_row
*rp
, enum tbl_cellt pos
)
352 struct tbl_cell
*p
, *pp
;
353 struct tbl_head
*h
, *hp
;
355 p
= mandoc_calloc(1, sizeof(struct tbl_cell
));
357 if (NULL
!= (pp
= rp
->last
)) {
361 rp
->last
= rp
->first
= p
;
366 * This is a little bit complicated. Here we determine the
367 * header the corresponds to a cell. We add headers dynamically
368 * when need be or re-use them, otherwise. As an example, given
376 * We first add the new headers (as there are none) in (1); then
377 * in (2) we insert the first spanner (as it doesn't match up
378 * with the header); then we re-use the prior data headers,
379 * skipping over the spanners; then we re-use everything and add
380 * a last spanner. Note that VERT headers are made into DVERT
384 h
= pp
? pp
->head
->next
: tbl
->first_head
;
387 /* Re-use data header. */
388 if (TBL_HEAD_DATA
== h
->pos
&&
389 (TBL_CELL_VERT
!= p
->pos
&&
390 TBL_CELL_DVERT
!= p
->pos
)) {
395 /* Re-use spanner header. */
396 if (TBL_HEAD_DATA
!= h
->pos
&&
397 (TBL_CELL_VERT
== p
->pos
||
398 TBL_CELL_DVERT
== p
->pos
)) {
404 /* Right-shift headers with a new spanner. */
405 if (TBL_HEAD_DATA
== h
->pos
&&
406 (TBL_CELL_VERT
== p
->pos
||
407 TBL_CELL_DVERT
== p
->pos
)) {
408 hp
= mandoc_calloc(1, sizeof(struct tbl_head
));
409 hp
->ident
= tbl
->opts
.cols
++;
413 if (h
== tbl
->first_head
)
414 tbl
->first_head
= hp
;
422 if (NULL
!= (h
= h
->next
)) {
428 /* Fall through to default case... */
431 hp
= mandoc_calloc(1, sizeof(struct tbl_head
));
432 hp
->ident
= tbl
->opts
.cols
++;
434 if (tbl
->last_head
) {
435 hp
->prev
= tbl
->last_head
;
436 tbl
->last_head
->next
= hp
;
439 tbl
->last_head
= tbl
->first_head
= hp
;
447 head_adjust(const struct tbl_cell
*cellp
, struct tbl_head
*head
)
449 if (TBL_CELL_VERT
!= cellp
->pos
&&
450 TBL_CELL_DVERT
!= cellp
->pos
) {
451 head
->pos
= TBL_HEAD_DATA
;
455 if (TBL_CELL_VERT
== cellp
->pos
)
456 if (TBL_HEAD_DVERT
!= head
->pos
)
457 head
->pos
= TBL_HEAD_VERT
;
459 if (TBL_CELL_DVERT
== cellp
->pos
)
460 head
->pos
= TBL_HEAD_DVERT
;