]>
git.cameronkatri.com Git - mandoc.git/blob - tbl_layout.c
1 /* $Id: tbl_layout.c,v 1.19 2011/04/07 01:08:42 joerg 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
)
77 * XXX: since, at least for now, modifiers are non-conflicting
78 * (are separable by value, regardless of position), we let
79 * modifiers come in any order. The existing tbl doesn't let
97 /* Throw away parenthesised expression. */
101 while (p
[*pos
] && ')' != p
[*pos
])
103 if (')' == p
[*pos
]) {
107 mandoc_msg(MANDOCERR_TBLLAYOUT
,
108 tbl
->parse
, ln
, *pos
, NULL
);
112 /* Parse numerical spacing from modifier string. */
114 if (isdigit((unsigned char)p
[*pos
])) {
115 for (i
= 0; i
< 4; i
++) {
116 if ( ! isdigit((unsigned char)p
[*pos
+ i
]))
118 buf
[i
] = p
[*pos
+ i
];
122 /* No greater than 4 digits. */
125 mandoc_msg(MANDOCERR_TBLLAYOUT
, tbl
->parse
,
131 cp
->spacing
= (size_t)atoi(buf
);
137 /* TODO: GNU has many more extensions. */
139 switch (tolower((unsigned char)p
[(*pos
)++])) {
141 cp
->flags
|= TBL_CELL_WIGN
;
144 cp
->flags
|= TBL_CELL_UP
;
147 cp
->flags
|= TBL_CELL_EQUAL
;
150 cp
->flags
|= TBL_CELL_TALIGN
;
153 cp
->flags
|= TBL_CELL_BALIGN
;
155 case ('w'): /* XXX for now, ignore minimal column width */
165 mandoc_msg(MANDOCERR_TBLLAYOUT
, tbl
->parse
,
170 switch (tolower((unsigned char)p
[(*pos
)++])) {
172 cp
->flags
|= TBL_CELL_BOLD
;
175 cp
->flags
|= TBL_CELL_ITALIC
;
181 mandoc_msg(MANDOCERR_TBLLAYOUT
, tbl
->parse
,
187 cell(struct tbl_node
*tbl
, struct tbl_row
*rp
,
188 int ln
, const char *p
, int *pos
)
193 /* Parse the column position (`r', `R', `|', ...). */
195 for (i
= 0; i
< KEYS_MAX
; i
++)
196 if (tolower((unsigned char)p
[*pos
]) == keys
[i
].name
)
200 mandoc_msg(MANDOCERR_TBLLAYOUT
, tbl
->parse
,
208 * If a span cell is found first, raise a warning and abort the
209 * parse. If a span cell is found and the last layout element
210 * isn't a "normal" layout, bail.
212 * FIXME: recover from this somehow?
215 if (TBL_CELL_SPAN
== c
) {
216 if (NULL
== rp
->first
) {
217 mandoc_msg(MANDOCERR_TBLLAYOUT
, tbl
->parse
,
221 switch (rp
->last
->pos
) {
222 case (TBL_CELL_VERT
):
223 case (TBL_CELL_DVERT
):
224 case (TBL_CELL_HORIZ
):
225 case (TBL_CELL_DHORIZ
):
226 mandoc_msg(MANDOCERR_TBLLAYOUT
, tbl
->parse
,
235 * If a vertical spanner is found, we may not be in the first
239 if (TBL_CELL_DOWN
== c
&& rp
== tbl
->first_row
) {
240 mandoc_msg(MANDOCERR_TBLLAYOUT
, tbl
->parse
, ln
, *pos
, NULL
);
246 /* Extra check for the double-vertical. */
248 if (TBL_CELL_VERT
== c
&& '|' == p
[*pos
]) {
253 /* Disallow adjacent spacers. */
255 if (rp
->last
&& (TBL_CELL_VERT
== c
|| TBL_CELL_DVERT
== c
) &&
256 (TBL_CELL_VERT
== rp
->last
->pos
||
257 TBL_CELL_DVERT
== rp
->last
->pos
)) {
258 mandoc_msg(MANDOCERR_TBLLAYOUT
, tbl
->parse
, ln
, *pos
- 1, NULL
);
262 /* Allocate cell then parse its modifiers. */
264 return(mods(tbl
, cell_alloc(tbl
, rp
, c
), ln
, p
, pos
));
269 row(struct tbl_node
*tbl
, int ln
, const char *p
, int *pos
)
274 * EBNF describing this section:
276 * row ::= row_list [:space:]* [.]?[\n]
277 * row_list ::= [:space:]* row_elem row_tail
278 * row_tail ::= [:space:]*[,] row_list |
280 * row_elem ::= [\t\ ]*[:alpha:]+
283 rp
= mandoc_calloc(1, sizeof(struct tbl_row
));
285 tbl
->last_row
->next
= rp
;
288 tbl
->last_row
= tbl
->first_row
= rp
;
291 while (isspace((unsigned char)p
[*pos
]))
294 /* Safely exit layout context. */
296 if ('.' == p
[*pos
]) {
297 tbl
->part
= TBL_PART_DATA
;
298 if (NULL
== tbl
->first_row
)
299 mandoc_msg(MANDOCERR_TBLNOLAYOUT
, tbl
->parse
,
305 /* End (and possibly restart) a row. */
307 if (',' == p
[*pos
]) {
310 } else if ('\0' == p
[*pos
])
313 if ( ! cell(tbl
, rp
, ln
, p
, pos
))
321 tbl_layout(struct tbl_node
*tbl
, int ln
, const char *p
)
326 row(tbl
, ln
, p
, &pos
);
328 /* Always succeed. */
332 static struct tbl_cell
*
333 cell_alloc(struct tbl_node
*tbl
, struct tbl_row
*rp
, enum tbl_cellt pos
)
335 struct tbl_cell
*p
, *pp
;
336 struct tbl_head
*h
, *hp
;
338 p
= mandoc_calloc(1, sizeof(struct tbl_cell
));
340 if (NULL
!= (pp
= rp
->last
)) {
344 rp
->last
= rp
->first
= p
;
349 * This is a little bit complicated. Here we determine the
350 * header the corresponds to a cell. We add headers dynamically
351 * when need be or re-use them, otherwise. As an example, given
359 * We first add the new headers (as there are none) in (1); then
360 * in (2) we insert the first spanner (as it doesn't match up
361 * with the header); then we re-use the prior data headers,
362 * skipping over the spanners; then we re-use everything and add
363 * a last spanner. Note that VERT headers are made into DVERT
367 h
= pp
? pp
->head
->next
: tbl
->first_head
;
370 /* Re-use data header. */
371 if (TBL_HEAD_DATA
== h
->pos
&&
372 (TBL_CELL_VERT
!= p
->pos
&&
373 TBL_CELL_DVERT
!= p
->pos
)) {
378 /* Re-use spanner header. */
379 if (TBL_HEAD_DATA
!= h
->pos
&&
380 (TBL_CELL_VERT
== p
->pos
||
381 TBL_CELL_DVERT
== p
->pos
)) {
387 /* Right-shift headers with a new spanner. */
388 if (TBL_HEAD_DATA
== h
->pos
&&
389 (TBL_CELL_VERT
== p
->pos
||
390 TBL_CELL_DVERT
== p
->pos
)) {
391 hp
= mandoc_calloc(1, sizeof(struct tbl_head
));
392 hp
->ident
= tbl
->opts
.cols
++;
396 if (h
== tbl
->first_head
)
397 tbl
->first_head
= hp
;
405 if (NULL
!= (h
= h
->next
)) {
411 /* Fall through to default case... */
414 hp
= mandoc_calloc(1, sizeof(struct tbl_head
));
415 hp
->ident
= tbl
->opts
.cols
++;
417 if (tbl
->last_head
) {
418 hp
->prev
= tbl
->last_head
;
419 tbl
->last_head
->next
= hp
;
422 tbl
->last_head
= tbl
->first_head
= hp
;
430 head_adjust(const struct tbl_cell
*cellp
, struct tbl_head
*head
)
432 if (TBL_CELL_VERT
!= cellp
->pos
&&
433 TBL_CELL_DVERT
!= cellp
->pos
) {
434 head
->pos
= TBL_HEAD_DATA
;
438 if (TBL_CELL_VERT
== cellp
->pos
)
439 if (TBL_HEAD_DVERT
!= head
->pos
)
440 head
->pos
= TBL_HEAD_VERT
;
442 if (TBL_CELL_DVERT
== cellp
->pos
)
443 head
->pos
= TBL_HEAD_DVERT
;