]>
git.cameronkatri.com Git - mandoc.git/blob - tbl_layout.c
1 /* $Id: tbl_layout.c,v 1.10 2011/01/04 23:48:39 schwarze 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
;
129 case ('w'): /* XXX for now, ignore minimal column width */
139 TBL_MSG(tbl
, MANDOCERR_TBLLAYOUT
, ln
, *pos
- 1);
143 switch (tolower(p
[(*pos
)++])) {
145 cp
->flags
|= TBL_CELL_BOLD
;
148 cp
->flags
|= TBL_CELL_ITALIC
;
154 TBL_MSG(tbl
, MANDOCERR_TBLLAYOUT
, ln
, *pos
- 1);
159 cell(struct tbl_node
*tbl
, struct tbl_row
*rp
,
160 int ln
, const char *p
, int *pos
)
165 /* Parse the column position (`r', `R', `|', ...). */
167 for (i
= 0; i
< KEYS_MAX
; i
++)
168 if (tolower(p
[*pos
]) == keys
[i
].name
)
172 TBL_MSG(tbl
, MANDOCERR_TBLLAYOUT
, ln
, *pos
);
179 /* Extra check for the double-vertical. */
181 if (TBL_CELL_VERT
== c
&& '|' == p
[*pos
]) {
186 /* Disallow adjacent spacers. */
188 if (rp
->last
&& (TBL_CELL_VERT
== c
|| TBL_CELL_DVERT
== c
) &&
189 (TBL_CELL_VERT
== rp
->last
->pos
||
190 TBL_CELL_DVERT
== rp
->last
->pos
)) {
191 TBL_MSG(tbl
, MANDOCERR_TBLLAYOUT
, ln
, *pos
- 1);
195 /* Allocate cell then parse its modifiers. */
197 return(mods(tbl
, cell_alloc(tbl
, rp
, c
), ln
, p
, pos
));
202 row(struct tbl_node
*tbl
, int ln
, const char *p
, int *pos
)
207 * EBNF describing this section:
209 * row ::= row_list [:space:]* [.]?[\n]
210 * row_list ::= [:space:]* row_elem row_tail
211 * row_tail ::= [:space:]*[,] row_list |
213 * row_elem ::= [\t\ ]*[:alpha:]+
216 rp
= mandoc_calloc(1, sizeof(struct tbl_row
));
218 tbl
->last_row
->next
= rp
;
221 tbl
->last_row
= tbl
->first_row
= rp
;
224 while (isspace((unsigned char)p
[*pos
]))
227 /* Safely exit layout context. */
229 if ('.' == p
[*pos
]) {
230 tbl
->part
= TBL_PART_DATA
;
231 if (NULL
== tbl
->first_row
)
232 TBL_MSG(tbl
, MANDOCERR_TBLNOLAYOUT
, ln
, *pos
);
237 /* End (and possibly restart) a row. */
239 if (',' == p
[*pos
]) {
242 } else if ('\0' == p
[*pos
])
245 if ( ! cell(tbl
, rp
, ln
, p
, pos
))
253 tbl_layout(struct tbl_node
*tbl
, int ln
, const char *p
)
258 row(tbl
, ln
, p
, &pos
);
260 /* Always succeed. */
264 static struct tbl_cell
*
265 cell_alloc(struct tbl_node
*tbl
, struct tbl_row
*rp
, enum tbl_cellt pos
)
267 struct tbl_cell
*p
, *pp
;
268 struct tbl_head
*h
, *hp
;
270 p
= mandoc_calloc(1, sizeof(struct tbl_cell
));
272 if (NULL
!= (pp
= rp
->last
)) {
276 rp
->last
= rp
->first
= p
;
281 * This is a little bit complicated. Here we determine the
282 * header the corresponds to a cell. We add headers dynamically
283 * when need be or re-use them, otherwise. As an example, given
291 * We first add the new headers (as there are none) in (1); then
292 * in (2) we insert the first spanner (as it doesn't match up
293 * with the header); then we re-use the prior data headers,
294 * skipping over the spanners; then we re-use everything and add
295 * a last spanner. Note that VERT headers are made into DVERT
299 h
= pp
? pp
->head
->next
: tbl
->first_head
;
302 /* Re-use data header. */
303 if (TBL_HEAD_DATA
== h
->pos
&&
304 (TBL_CELL_VERT
!= p
->pos
&&
305 TBL_CELL_DVERT
!= p
->pos
)) {
310 /* Re-use spanner header. */
311 if (TBL_HEAD_DATA
!= h
->pos
&&
312 (TBL_CELL_VERT
== p
->pos
||
313 TBL_CELL_DVERT
== p
->pos
)) {
319 /* Right-shift headers with a new spanner. */
320 if (TBL_HEAD_DATA
== h
->pos
&&
321 (TBL_CELL_VERT
== p
->pos
||
322 TBL_CELL_DVERT
== p
->pos
)) {
323 hp
= mandoc_calloc(1, sizeof(struct tbl_head
));
324 hp
->ident
= tbl
->opts
.cols
++;
328 if (h
== tbl
->first_head
)
329 tbl
->first_head
= hp
;
337 if (NULL
!= (h
= h
->next
)) {
343 /* Fall through to default case... */
346 hp
= mandoc_calloc(1, sizeof(struct tbl_head
));
347 hp
->ident
= tbl
->opts
.cols
++;
349 if (tbl
->last_head
) {
350 hp
->prev
= tbl
->last_head
;
351 tbl
->last_head
->next
= hp
;
354 tbl
->last_head
= tbl
->first_head
= hp
;
362 head_adjust(const struct tbl_cell
*cell
, struct tbl_head
*head
)
364 if (TBL_CELL_VERT
!= cell
->pos
&&
365 TBL_CELL_DVERT
!= cell
->pos
) {
366 head
->pos
= TBL_HEAD_DATA
;
370 if (TBL_CELL_VERT
== cell
->pos
)
371 if (TBL_HEAD_DVERT
!= head
->pos
)
372 head
->pos
= TBL_HEAD_VERT
;
374 if (TBL_CELL_DVERT
== cell
->pos
)
375 head
->pos
= TBL_HEAD_DVERT
;