]>
git.cameronkatri.com Git - mandoc.git/blob - tbl_layout.c
1 /* $Id: tbl_layout.c,v 1.17 2011/03/20 16:02:05 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.
24 #include "libmandoc.h"
33 * FIXME: we can make this parse a lot nicer by, when an error is
34 * encountered in a layout key, bailing to the next key (i.e. to the
35 * next whitespace then continuing).
40 static const struct tbl_phrase keys
[KEYS_MAX
] = {
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
},
51 { '|', TBL_CELL_VERT
}
54 static int mods(struct tbl_node
*, struct tbl_cell
*,
55 int, const char *, int *);
56 static int cell(struct tbl_node
*, struct tbl_row
*,
57 int, const char *, int *);
58 static void row(struct tbl_node
*, int, const char *, int *);
59 static struct tbl_cell
*cell_alloc(struct tbl_node
*,
60 struct tbl_row
*, enum tbl_cellt
);
61 static void head_adjust(const struct tbl_cell
*,
65 mods(struct tbl_node
*tbl
, struct tbl_cell
*cp
,
66 int ln
, const char *p
, int *pos
)
73 * XXX: since, at least for now, modifiers are non-conflicting
74 * (are separable by value, regardless of position), we let
75 * modifiers come in any order. The existing tbl doesn't let
93 /* Throw away parenthesised expression. */
97 while (p
[*pos
] && ')' != p
[*pos
])
103 mandoc_msg(MANDOCERR_TBLLAYOUT
,
104 tbl
->parse
, ln
, *pos
, NULL
);
108 /* Parse numerical spacing from modifier string. */
110 if (isdigit((unsigned char)p
[*pos
])) {
111 for (i
= 0; i
< 4; i
++) {
112 if ( ! isdigit((unsigned char)p
[*pos
+ i
]))
114 buf
[i
] = p
[*pos
+ i
];
118 /* No greater than 4 digits. */
121 mandoc_msg(MANDOCERR_TBLLAYOUT
, tbl
->parse
,
127 cp
->spacing
= (size_t)atoi(buf
);
133 /* TODO: GNU has many more extensions. */
135 switch (tolower((unsigned char)p
[(*pos
)++])) {
137 cp
->flags
|= TBL_CELL_WIGN
;
140 cp
->flags
|= TBL_CELL_UP
;
143 cp
->flags
|= TBL_CELL_EQUAL
;
146 cp
->flags
|= TBL_CELL_TALIGN
;
149 cp
->flags
|= TBL_CELL_BALIGN
;
151 case ('w'): /* XXX for now, ignore minimal column width */
161 mandoc_msg(MANDOCERR_TBLLAYOUT
, tbl
->parse
,
166 switch (tolower((unsigned char)p
[(*pos
)++])) {
168 cp
->flags
|= TBL_CELL_BOLD
;
171 cp
->flags
|= TBL_CELL_ITALIC
;
177 mandoc_msg(MANDOCERR_TBLLAYOUT
, tbl
->parse
,
183 cell(struct tbl_node
*tbl
, struct tbl_row
*rp
,
184 int ln
, const char *p
, int *pos
)
189 /* Parse the column position (`r', `R', `|', ...). */
191 for (i
= 0; i
< KEYS_MAX
; i
++)
192 if (tolower((unsigned char)p
[*pos
]) == keys
[i
].name
)
196 mandoc_msg(MANDOCERR_TBLLAYOUT
, tbl
->parse
,
204 * If a span cell is found first, raise a warning and abort the
205 * parse. If a span cell is found and the last layout element
206 * isn't a "normal" layout, bail.
208 * FIXME: recover from this somehow?
211 if (TBL_CELL_SPAN
== c
) {
212 if (NULL
== rp
->first
) {
213 mandoc_msg(MANDOCERR_TBLLAYOUT
, tbl
->parse
,
217 switch (rp
->last
->pos
) {
218 case (TBL_CELL_VERT
):
219 case (TBL_CELL_DVERT
):
220 case (TBL_CELL_HORIZ
):
221 case (TBL_CELL_DHORIZ
):
222 mandoc_msg(MANDOCERR_TBLLAYOUT
, tbl
->parse
,
231 * If a vertical spanner is found, we may not be in the first
235 if (TBL_CELL_DOWN
== c
&& rp
== tbl
->first_row
) {
236 mandoc_msg(MANDOCERR_TBLLAYOUT
, tbl
->parse
, ln
, *pos
, NULL
);
242 /* Extra check for the double-vertical. */
244 if (TBL_CELL_VERT
== c
&& '|' == p
[*pos
]) {
249 /* Disallow adjacent spacers. */
251 if (rp
->last
&& (TBL_CELL_VERT
== c
|| TBL_CELL_DVERT
== c
) &&
252 (TBL_CELL_VERT
== rp
->last
->pos
||
253 TBL_CELL_DVERT
== rp
->last
->pos
)) {
254 mandoc_msg(MANDOCERR_TBLLAYOUT
, tbl
->parse
, ln
, *pos
- 1, NULL
);
258 /* Allocate cell then parse its modifiers. */
260 return(mods(tbl
, cell_alloc(tbl
, rp
, c
), ln
, p
, pos
));
265 row(struct tbl_node
*tbl
, int ln
, const char *p
, int *pos
)
270 * EBNF describing this section:
272 * row ::= row_list [:space:]* [.]?[\n]
273 * row_list ::= [:space:]* row_elem row_tail
274 * row_tail ::= [:space:]*[,] row_list |
276 * row_elem ::= [\t\ ]*[:alpha:]+
279 rp
= mandoc_calloc(1, sizeof(struct tbl_row
));
281 tbl
->last_row
->next
= rp
;
284 tbl
->last_row
= tbl
->first_row
= rp
;
287 while (isspace((unsigned char)p
[*pos
]))
290 /* Safely exit layout context. */
292 if ('.' == p
[*pos
]) {
293 tbl
->part
= TBL_PART_DATA
;
294 if (NULL
== tbl
->first_row
)
295 mandoc_msg(MANDOCERR_TBLNOLAYOUT
, tbl
->parse
,
301 /* End (and possibly restart) a row. */
303 if (',' == p
[*pos
]) {
306 } else if ('\0' == p
[*pos
])
309 if ( ! cell(tbl
, rp
, ln
, p
, pos
))
317 tbl_layout(struct tbl_node
*tbl
, int ln
, const char *p
)
322 row(tbl
, ln
, p
, &pos
);
324 /* Always succeed. */
328 static struct tbl_cell
*
329 cell_alloc(struct tbl_node
*tbl
, struct tbl_row
*rp
, enum tbl_cellt pos
)
331 struct tbl_cell
*p
, *pp
;
332 struct tbl_head
*h
, *hp
;
334 p
= mandoc_calloc(1, sizeof(struct tbl_cell
));
336 if (NULL
!= (pp
= rp
->last
)) {
340 rp
->last
= rp
->first
= p
;
345 * This is a little bit complicated. Here we determine the
346 * header the corresponds to a cell. We add headers dynamically
347 * when need be or re-use them, otherwise. As an example, given
355 * We first add the new headers (as there are none) in (1); then
356 * in (2) we insert the first spanner (as it doesn't match up
357 * with the header); then we re-use the prior data headers,
358 * skipping over the spanners; then we re-use everything and add
359 * a last spanner. Note that VERT headers are made into DVERT
363 h
= pp
? pp
->head
->next
: tbl
->first_head
;
366 /* Re-use data header. */
367 if (TBL_HEAD_DATA
== h
->pos
&&
368 (TBL_CELL_VERT
!= p
->pos
&&
369 TBL_CELL_DVERT
!= p
->pos
)) {
374 /* Re-use spanner header. */
375 if (TBL_HEAD_DATA
!= h
->pos
&&
376 (TBL_CELL_VERT
== p
->pos
||
377 TBL_CELL_DVERT
== p
->pos
)) {
383 /* Right-shift headers with a new spanner. */
384 if (TBL_HEAD_DATA
== h
->pos
&&
385 (TBL_CELL_VERT
== p
->pos
||
386 TBL_CELL_DVERT
== p
->pos
)) {
387 hp
= mandoc_calloc(1, sizeof(struct tbl_head
));
388 hp
->ident
= tbl
->opts
.cols
++;
392 if (h
== tbl
->first_head
)
393 tbl
->first_head
= hp
;
401 if (NULL
!= (h
= h
->next
)) {
407 /* Fall through to default case... */
410 hp
= mandoc_calloc(1, sizeof(struct tbl_head
));
411 hp
->ident
= tbl
->opts
.cols
++;
413 if (tbl
->last_head
) {
414 hp
->prev
= tbl
->last_head
;
415 tbl
->last_head
->next
= hp
;
418 tbl
->last_head
= tbl
->first_head
= hp
;
426 head_adjust(const struct tbl_cell
*cell
, struct tbl_head
*head
)
428 if (TBL_CELL_VERT
!= cell
->pos
&&
429 TBL_CELL_DVERT
!= cell
->pos
) {
430 head
->pos
= TBL_HEAD_DATA
;
434 if (TBL_CELL_VERT
== cell
->pos
)
435 if (TBL_HEAD_DVERT
!= head
->pos
)
436 head
->pos
= TBL_HEAD_VERT
;
438 if (TBL_CELL_DVERT
== cell
->pos
)
439 head
->pos
= TBL_HEAD_DVERT
;