]>
git.cameronkatri.com Git - mandoc.git/blob - tbl_layout.c
1 /* $Id: tbl_layout.c,v 1.16 2011/01/11 14:12:01 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
= (size_t)atoi(buf
);
131 /* TODO: GNU has many more extensions. */
133 switch (tolower((unsigned char)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((unsigned char)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((unsigned char)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. If a span cell is found and the last layout element
201 * isn't a "normal" layout, bail.
203 * FIXME: recover from this somehow?
206 if (TBL_CELL_SPAN
== c
) {
207 if (NULL
== rp
->first
) {
208 TBL_MSG(tbl
, MANDOCERR_TBLLAYOUT
, ln
, *pos
);
211 switch (rp
->last
->pos
) {
212 case (TBL_CELL_VERT
):
213 case (TBL_CELL_DVERT
):
214 case (TBL_CELL_HORIZ
):
215 case (TBL_CELL_DHORIZ
):
216 TBL_MSG(tbl
, MANDOCERR_TBLLAYOUT
, ln
, *pos
);
224 * If a vertical spanner is found, we may not be in the first
228 if (TBL_CELL_DOWN
== c
&& rp
== tbl
->first_row
) {
229 TBL_MSG(tbl
, MANDOCERR_TBLLAYOUT
, ln
, *pos
);
235 /* Extra check for the double-vertical. */
237 if (TBL_CELL_VERT
== c
&& '|' == p
[*pos
]) {
242 /* Disallow adjacent spacers. */
244 if (rp
->last
&& (TBL_CELL_VERT
== c
|| TBL_CELL_DVERT
== c
) &&
245 (TBL_CELL_VERT
== rp
->last
->pos
||
246 TBL_CELL_DVERT
== rp
->last
->pos
)) {
247 TBL_MSG(tbl
, MANDOCERR_TBLLAYOUT
, ln
, *pos
- 1);
251 /* Allocate cell then parse its modifiers. */
253 return(mods(tbl
, cell_alloc(tbl
, rp
, c
), ln
, p
, pos
));
258 row(struct tbl_node
*tbl
, int ln
, const char *p
, int *pos
)
263 * EBNF describing this section:
265 * row ::= row_list [:space:]* [.]?[\n]
266 * row_list ::= [:space:]* row_elem row_tail
267 * row_tail ::= [:space:]*[,] row_list |
269 * row_elem ::= [\t\ ]*[:alpha:]+
272 rp
= mandoc_calloc(1, sizeof(struct tbl_row
));
274 tbl
->last_row
->next
= rp
;
277 tbl
->last_row
= tbl
->first_row
= rp
;
280 while (isspace((unsigned char)p
[*pos
]))
283 /* Safely exit layout context. */
285 if ('.' == p
[*pos
]) {
286 tbl
->part
= TBL_PART_DATA
;
287 if (NULL
== tbl
->first_row
)
288 TBL_MSG(tbl
, MANDOCERR_TBLNOLAYOUT
, ln
, *pos
);
293 /* End (and possibly restart) a row. */
295 if (',' == p
[*pos
]) {
298 } else if ('\0' == p
[*pos
])
301 if ( ! cell(tbl
, rp
, ln
, p
, pos
))
309 tbl_layout(struct tbl_node
*tbl
, int ln
, const char *p
)
314 row(tbl
, ln
, p
, &pos
);
316 /* Always succeed. */
320 static struct tbl_cell
*
321 cell_alloc(struct tbl_node
*tbl
, struct tbl_row
*rp
, enum tbl_cellt pos
)
323 struct tbl_cell
*p
, *pp
;
324 struct tbl_head
*h
, *hp
;
326 p
= mandoc_calloc(1, sizeof(struct tbl_cell
));
328 if (NULL
!= (pp
= rp
->last
)) {
332 rp
->last
= rp
->first
= p
;
337 * This is a little bit complicated. Here we determine the
338 * header the corresponds to a cell. We add headers dynamically
339 * when need be or re-use them, otherwise. As an example, given
347 * We first add the new headers (as there are none) in (1); then
348 * in (2) we insert the first spanner (as it doesn't match up
349 * with the header); then we re-use the prior data headers,
350 * skipping over the spanners; then we re-use everything and add
351 * a last spanner. Note that VERT headers are made into DVERT
355 h
= pp
? pp
->head
->next
: tbl
->first_head
;
358 /* Re-use data header. */
359 if (TBL_HEAD_DATA
== h
->pos
&&
360 (TBL_CELL_VERT
!= p
->pos
&&
361 TBL_CELL_DVERT
!= p
->pos
)) {
366 /* Re-use spanner header. */
367 if (TBL_HEAD_DATA
!= h
->pos
&&
368 (TBL_CELL_VERT
== p
->pos
||
369 TBL_CELL_DVERT
== p
->pos
)) {
375 /* Right-shift headers with a new spanner. */
376 if (TBL_HEAD_DATA
== h
->pos
&&
377 (TBL_CELL_VERT
== p
->pos
||
378 TBL_CELL_DVERT
== p
->pos
)) {
379 hp
= mandoc_calloc(1, sizeof(struct tbl_head
));
380 hp
->ident
= tbl
->opts
.cols
++;
384 if (h
== tbl
->first_head
)
385 tbl
->first_head
= hp
;
393 if (NULL
!= (h
= h
->next
)) {
399 /* Fall through to default case... */
402 hp
= mandoc_calloc(1, sizeof(struct tbl_head
));
403 hp
->ident
= tbl
->opts
.cols
++;
405 if (tbl
->last_head
) {
406 hp
->prev
= tbl
->last_head
;
407 tbl
->last_head
->next
= hp
;
410 tbl
->last_head
= tbl
->first_head
= hp
;
418 head_adjust(const struct tbl_cell
*cell
, struct tbl_head
*head
)
420 if (TBL_CELL_VERT
!= cell
->pos
&&
421 TBL_CELL_DVERT
!= cell
->pos
) {
422 head
->pos
= TBL_HEAD_DATA
;
426 if (TBL_CELL_VERT
== cell
->pos
)
427 if (TBL_HEAD_DVERT
!= head
->pos
)
428 head
->pos
= TBL_HEAD_VERT
;
430 if (TBL_CELL_DVERT
== cell
->pos
)
431 head
->pos
= TBL_HEAD_DVERT
;