]>
git.cameronkatri.com Git - mandoc.git/blob - tbl_layout.c
1 /* $Id: tbl_layout.c,v 1.12 2011/01/07 14:59:52 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 TBL_MSG(tbl
, MANDOCERR_TBLLAYOUT
, ln
, *pos
);
107 /* Parse numerical spacing from modifier string. */
109 if (isdigit((unsigned char)p
[*pos
])) {
110 for (i
= 0; i
< 4; i
++) {
111 if ( ! isdigit((unsigned char)p
[*pos
+ i
]))
113 buf
[i
] = p
[*pos
+ i
];
117 /* No greater than 4 digits. */
120 TBL_MSG(tbl
, MANDOCERR_TBLLAYOUT
, ln
, *pos
);
125 cp
->spacing
= atoi(buf
);
131 /* TODO: GNU has many more extensions. */
133 switch (tolower(p
[(*pos
)++])) {
135 cp
->flags
|= TBL_CELL_WIGN
;
138 cp
->flags
|= TBL_CELL_UP
;
141 cp
->flags
|= TBL_CELL_EQUAL
;
144 cp
->flags
|= TBL_CELL_TALIGN
;
147 cp
->flags
|= TBL_CELL_BALIGN
;
149 case ('w'): /* XXX for now, ignore minimal column width */
159 TBL_MSG(tbl
, MANDOCERR_TBLLAYOUT
, ln
, *pos
- 1);
163 switch (tolower(p
[(*pos
)++])) {
165 cp
->flags
|= TBL_CELL_BOLD
;
168 cp
->flags
|= TBL_CELL_ITALIC
;
174 TBL_MSG(tbl
, MANDOCERR_TBLLAYOUT
, ln
, *pos
- 1);
179 cell(struct tbl_node
*tbl
, struct tbl_row
*rp
,
180 int ln
, const char *p
, int *pos
)
185 /* Parse the column position (`r', `R', `|', ...). */
187 for (i
= 0; i
< KEYS_MAX
; i
++)
188 if (tolower(p
[*pos
]) == keys
[i
].name
)
192 TBL_MSG(tbl
, MANDOCERR_TBLLAYOUT
, ln
, *pos
);
199 * If a span cell is found first, raise a warning and abort the
200 * parse. FIXME: recover from this somehow?
203 if (NULL
== rp
->first
&& TBL_CELL_SPAN
== c
) {
204 TBL_MSG(tbl
, MANDOCERR_TBLLAYOUT
, ln
, *pos
);
210 /* Extra check for the double-vertical. */
212 if (TBL_CELL_VERT
== c
&& '|' == p
[*pos
]) {
217 /* Disallow adjacent spacers. */
219 if (rp
->last
&& (TBL_CELL_VERT
== c
|| TBL_CELL_DVERT
== c
) &&
220 (TBL_CELL_VERT
== rp
->last
->pos
||
221 TBL_CELL_DVERT
== rp
->last
->pos
)) {
222 TBL_MSG(tbl
, MANDOCERR_TBLLAYOUT
, ln
, *pos
- 1);
226 /* Allocate cell then parse its modifiers. */
228 return(mods(tbl
, cell_alloc(tbl
, rp
, c
), ln
, p
, pos
));
233 row(struct tbl_node
*tbl
, int ln
, const char *p
, int *pos
)
238 * EBNF describing this section:
240 * row ::= row_list [:space:]* [.]?[\n]
241 * row_list ::= [:space:]* row_elem row_tail
242 * row_tail ::= [:space:]*[,] row_list |
244 * row_elem ::= [\t\ ]*[:alpha:]+
247 rp
= mandoc_calloc(1, sizeof(struct tbl_row
));
249 tbl
->last_row
->next
= rp
;
252 tbl
->last_row
= tbl
->first_row
= rp
;
255 while (isspace((unsigned char)p
[*pos
]))
258 /* Safely exit layout context. */
260 if ('.' == p
[*pos
]) {
261 tbl
->part
= TBL_PART_DATA
;
262 if (NULL
== tbl
->first_row
)
263 TBL_MSG(tbl
, MANDOCERR_TBLNOLAYOUT
, ln
, *pos
);
268 /* End (and possibly restart) a row. */
270 if (',' == p
[*pos
]) {
273 } else if ('\0' == p
[*pos
])
276 if ( ! cell(tbl
, rp
, ln
, p
, pos
))
284 tbl_layout(struct tbl_node
*tbl
, int ln
, const char *p
)
289 row(tbl
, ln
, p
, &pos
);
291 /* Always succeed. */
295 static struct tbl_cell
*
296 cell_alloc(struct tbl_node
*tbl
, struct tbl_row
*rp
, enum tbl_cellt pos
)
298 struct tbl_cell
*p
, *pp
;
299 struct tbl_head
*h
, *hp
;
301 p
= mandoc_calloc(1, sizeof(struct tbl_cell
));
303 if (NULL
!= (pp
= rp
->last
)) {
307 rp
->last
= rp
->first
= p
;
312 * This is a little bit complicated. Here we determine the
313 * header the corresponds to a cell. We add headers dynamically
314 * when need be or re-use them, otherwise. As an example, given
322 * We first add the new headers (as there are none) in (1); then
323 * in (2) we insert the first spanner (as it doesn't match up
324 * with the header); then we re-use the prior data headers,
325 * skipping over the spanners; then we re-use everything and add
326 * a last spanner. Note that VERT headers are made into DVERT
330 h
= pp
? pp
->head
->next
: tbl
->first_head
;
333 /* Re-use data header. */
334 if (TBL_HEAD_DATA
== h
->pos
&&
335 (TBL_CELL_VERT
!= p
->pos
&&
336 TBL_CELL_DVERT
!= p
->pos
)) {
341 /* Re-use spanner header. */
342 if (TBL_HEAD_DATA
!= h
->pos
&&
343 (TBL_CELL_VERT
== p
->pos
||
344 TBL_CELL_DVERT
== p
->pos
)) {
350 /* Right-shift headers with a new spanner. */
351 if (TBL_HEAD_DATA
== h
->pos
&&
352 (TBL_CELL_VERT
== p
->pos
||
353 TBL_CELL_DVERT
== p
->pos
)) {
354 hp
= mandoc_calloc(1, sizeof(struct tbl_head
));
355 hp
->ident
= tbl
->opts
.cols
++;
359 if (h
== tbl
->first_head
)
360 tbl
->first_head
= hp
;
368 if (NULL
!= (h
= h
->next
)) {
374 /* Fall through to default case... */
377 hp
= mandoc_calloc(1, sizeof(struct tbl_head
));
378 hp
->ident
= tbl
->opts
.cols
++;
380 if (tbl
->last_head
) {
381 hp
->prev
= tbl
->last_head
;
382 tbl
->last_head
->next
= hp
;
385 tbl
->last_head
= tbl
->first_head
= hp
;
393 head_adjust(const struct tbl_cell
*cell
, struct tbl_head
*head
)
395 if (TBL_CELL_VERT
!= cell
->pos
&&
396 TBL_CELL_DVERT
!= cell
->pos
) {
397 head
->pos
= TBL_HEAD_DATA
;
401 if (TBL_CELL_VERT
== cell
->pos
)
402 if (TBL_HEAD_DVERT
!= head
->pos
)
403 head
->pos
= TBL_HEAD_VERT
;
405 if (TBL_CELL_DVERT
== cell
->pos
)
406 head
->pos
= TBL_HEAD_DVERT
;