]>
git.cameronkatri.com Git - mandoc.git/blob - tbl_layout.c
1 /* $Id: tbl_layout.c,v 1.9 2011/01/03 13:59:21 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"
34 static const struct tbl_phrase keys
[KEYS_MAX
] = {
35 { 'c', TBL_CELL_CENTRE
},
36 { 'r', TBL_CELL_RIGHT
},
37 { 'l', TBL_CELL_LEFT
},
38 { 'n', TBL_CELL_NUMBER
},
39 { 's', TBL_CELL_SPAN
},
40 { 'a', TBL_CELL_LONG
},
41 { '^', TBL_CELL_DOWN
},
42 { '-', TBL_CELL_HORIZ
},
43 { '_', TBL_CELL_HORIZ
},
44 { '=', TBL_CELL_DHORIZ
},
45 { '|', TBL_CELL_VERT
}
48 static int mods(struct tbl_node
*, struct tbl_cell
*,
49 int, const char *, int *);
50 static int cell(struct tbl_node
*, struct tbl_row
*,
51 int, const char *, int *);
52 static void row(struct tbl_node
*, int, const char *, int *);
53 static struct tbl_cell
*cell_alloc(struct tbl_node
*,
54 struct tbl_row
*, enum tbl_cellt
);
55 static void head_adjust(const struct tbl_cell
*,
59 mods(struct tbl_node
*tbl
, struct tbl_cell
*cp
,
60 int ln
, const char *p
, int *pos
)
67 * XXX: since, at least for now, modifiers are non-conflicting
68 * (are separable by value, regardless of position), we let
69 * modifiers come in any order. The existing tbl doesn't let
87 /* Parse numerical spacing from modifier string. */
89 if (isdigit((unsigned char)p
[*pos
])) {
90 for (i
= 0; i
< 4; i
++) {
91 if ( ! isdigit((unsigned char)p
[*pos
+ i
]))
97 /* No greater than 4 digits. */
100 TBL_MSG(tbl
, MANDOCERR_TBLLAYOUT
, ln
, *pos
);
105 cp
->spacing
= atoi(buf
);
111 /* TODO: GNU has many more extensions. */
113 switch (tolower(p
[(*pos
)++])) {
115 cp
->flags
|= TBL_CELL_WIGN
;
118 cp
->flags
|= TBL_CELL_UP
;
121 cp
->flags
|= TBL_CELL_EQUAL
;
124 cp
->flags
|= TBL_CELL_TALIGN
;
127 cp
->flags
|= TBL_CELL_BALIGN
;
137 TBL_MSG(tbl
, MANDOCERR_TBLLAYOUT
, ln
, *pos
- 1);
141 switch (tolower(p
[(*pos
)++])) {
143 cp
->flags
|= TBL_CELL_BOLD
;
146 cp
->flags
|= TBL_CELL_ITALIC
;
152 TBL_MSG(tbl
, MANDOCERR_TBLLAYOUT
, ln
, *pos
- 1);
157 cell(struct tbl_node
*tbl
, struct tbl_row
*rp
,
158 int ln
, const char *p
, int *pos
)
163 /* Parse the column position (`r', `R', `|', ...). */
165 for (i
= 0; i
< KEYS_MAX
; i
++)
166 if (tolower(p
[*pos
]) == keys
[i
].name
)
170 TBL_MSG(tbl
, MANDOCERR_TBLLAYOUT
, ln
, *pos
);
177 /* Extra check for the double-vertical. */
179 if (TBL_CELL_VERT
== c
&& '|' == p
[*pos
]) {
184 /* Disallow adjacent spacers. */
186 if (rp
->last
&& (TBL_CELL_VERT
== c
|| TBL_CELL_DVERT
== c
) &&
187 (TBL_CELL_VERT
== rp
->last
->pos
||
188 TBL_CELL_DVERT
== rp
->last
->pos
)) {
189 TBL_MSG(tbl
, MANDOCERR_TBLLAYOUT
, ln
, *pos
- 1);
193 /* Allocate cell then parse its modifiers. */
195 return(mods(tbl
, cell_alloc(tbl
, rp
, c
), ln
, p
, pos
));
200 row(struct tbl_node
*tbl
, int ln
, const char *p
, int *pos
)
205 * EBNF describing this section:
207 * row ::= row_list [:space:]* [.]?[\n]
208 * row_list ::= [:space:]* row_elem row_tail
209 * row_tail ::= [:space:]*[,] row_list |
211 * row_elem ::= [\t\ ]*[:alpha:]+
214 rp
= mandoc_calloc(1, sizeof(struct tbl_row
));
216 tbl
->last_row
->next
= rp
;
219 tbl
->last_row
= tbl
->first_row
= rp
;
222 while (isspace((unsigned char)p
[*pos
]))
225 /* Safely exit layout context. */
227 if ('.' == p
[*pos
]) {
228 tbl
->part
= TBL_PART_DATA
;
229 if (NULL
== tbl
->first_row
)
230 TBL_MSG(tbl
, MANDOCERR_TBLNOLAYOUT
, ln
, *pos
);
235 /* End (and possibly restart) a row. */
237 if (',' == p
[*pos
]) {
240 } else if ('\0' == p
[*pos
])
243 if ( ! cell(tbl
, rp
, ln
, p
, pos
))
251 tbl_layout(struct tbl_node
*tbl
, int ln
, const char *p
)
256 row(tbl
, ln
, p
, &pos
);
258 /* Always succeed. */
262 static struct tbl_cell
*
263 cell_alloc(struct tbl_node
*tbl
, struct tbl_row
*rp
, enum tbl_cellt pos
)
265 struct tbl_cell
*p
, *pp
;
266 struct tbl_head
*h
, *hp
;
268 p
= mandoc_calloc(1, sizeof(struct tbl_cell
));
270 if (NULL
!= (pp
= rp
->last
)) {
274 rp
->last
= rp
->first
= p
;
279 * This is a little bit complicated. Here we determine the
280 * header the corresponds to a cell. We add headers dynamically
281 * when need be or re-use them, otherwise. As an example, given
289 * We first add the new headers (as there are none) in (1); then
290 * in (2) we insert the first spanner (as it doesn't match up
291 * with the header); then we re-use the prior data headers,
292 * skipping over the spanners; then we re-use everything and add
293 * a last spanner. Note that VERT headers are made into DVERT
297 h
= pp
? pp
->head
->next
: tbl
->first_head
;
300 /* Re-use data header. */
301 if (TBL_HEAD_DATA
== h
->pos
&&
302 (TBL_CELL_VERT
!= p
->pos
&&
303 TBL_CELL_DVERT
!= p
->pos
)) {
308 /* Re-use spanner header. */
309 if (TBL_HEAD_DATA
!= h
->pos
&&
310 (TBL_CELL_VERT
== p
->pos
||
311 TBL_CELL_DVERT
== p
->pos
)) {
317 /* Right-shift headers with a new spanner. */
318 if (TBL_HEAD_DATA
== h
->pos
&&
319 (TBL_CELL_VERT
== p
->pos
||
320 TBL_CELL_DVERT
== p
->pos
)) {
321 hp
= mandoc_calloc(1, sizeof(struct tbl_head
));
322 hp
->ident
= tbl
->opts
.cols
++;
326 if (h
== tbl
->first_head
)
327 tbl
->first_head
= hp
;
335 if (NULL
!= (h
= h
->next
)) {
341 /* Fall through to default case... */
344 hp
= mandoc_calloc(1, sizeof(struct tbl_head
));
345 hp
->ident
= tbl
->opts
.cols
++;
347 if (tbl
->last_head
) {
348 hp
->prev
= tbl
->last_head
;
349 tbl
->last_head
->next
= hp
;
352 tbl
->last_head
= tbl
->first_head
= hp
;
360 head_adjust(const struct tbl_cell
*cell
, struct tbl_head
*head
)
362 if (TBL_CELL_VERT
!= cell
->pos
&&
363 TBL_CELL_DVERT
!= cell
->pos
) {
364 head
->pos
= TBL_HEAD_DATA
;
368 if (TBL_CELL_VERT
== cell
->pos
)
369 if (TBL_HEAD_DVERT
!= head
->pos
)
370 head
->pos
= TBL_HEAD_VERT
;
372 if (TBL_CELL_DVERT
== cell
->pos
)
373 head
->pos
= TBL_HEAD_DVERT
;