1 /* Id: tbl_layout.c,v 1.23 2012/05/27 17:54:54 schwarze Exp */
3 * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2012 Ingo Schwarze <schwarze@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
29 #include "libmandoc.h"
38 * FIXME: we can make this parse a lot nicer by, when an error is
39 * encountered in a layout key, bailing to the next key (i.e. to the
40 * next whitespace then continuing).
45 static const struct tbl_phrase keys
[KEYS_MAX
] = {
46 { 'c', TBL_CELL_CENTRE
},
47 { 'r', TBL_CELL_RIGHT
},
48 { 'l', TBL_CELL_LEFT
},
49 { 'n', TBL_CELL_NUMBER
},
50 { 's', TBL_CELL_SPAN
},
51 { 'a', TBL_CELL_LONG
},
52 { '^', TBL_CELL_DOWN
},
53 { '-', TBL_CELL_HORIZ
},
54 { '_', TBL_CELL_HORIZ
},
55 { '=', TBL_CELL_DHORIZ
}
58 static int mods(struct tbl_node
*, struct tbl_cell
*,
59 int, const char *, int *);
60 static int cell(struct tbl_node
*, struct tbl_row
*,
61 int, const char *, int *);
62 static void row(struct tbl_node
*, int, const char *, int *);
63 static struct tbl_cell
*cell_alloc(struct tbl_node
*, struct tbl_row
*,
64 enum tbl_cellt
, int vert
);
67 mods(struct tbl_node
*tbl
, struct tbl_cell
*cp
,
68 int ln
, const char *p
, int *pos
)
73 /* Not all types accept modifiers. */
78 case (TBL_CELL_HORIZ
):
80 case (TBL_CELL_DHORIZ
):
88 * XXX: since, at least for now, modifiers are non-conflicting
89 * (are separable by value, regardless of position), we let
90 * modifiers come in any order. The existing tbl doesn't let
108 /* Throw away parenthesised expression. */
110 if ('(' == p
[*pos
]) {
112 while (p
[*pos
] && ')' != p
[*pos
])
114 if (')' == p
[*pos
]) {
118 mandoc_msg(MANDOCERR_TBLLAYOUT
,
119 tbl
->parse
, ln
, *pos
, NULL
);
123 /* Parse numerical spacing from modifier string. */
125 if (isdigit((unsigned char)p
[*pos
])) {
126 for (i
= 0; i
< 4; i
++) {
127 if ( ! isdigit((unsigned char)p
[*pos
+ i
]))
129 buf
[i
] = p
[*pos
+ i
];
133 /* No greater than 4 digits. */
136 mandoc_msg(MANDOCERR_TBLLAYOUT
, tbl
->parse
,
142 cp
->spacing
= (size_t)atoi(buf
);
148 /* TODO: GNU has many more extensions. */
150 switch (tolower((unsigned char)p
[(*pos
)++])) {
152 cp
->flags
|= TBL_CELL_WIGN
;
155 cp
->flags
|= TBL_CELL_UP
;
158 cp
->flags
|= TBL_CELL_EQUAL
;
161 cp
->flags
|= TBL_CELL_TALIGN
;
164 cp
->flags
|= TBL_CELL_BALIGN
;
166 case ('w'): /* XXX for now, ignore minimal column width */
178 mandoc_msg(MANDOCERR_TBLLAYOUT
, tbl
->parse
,
183 switch (tolower((unsigned char)p
[(*pos
)++])) {
187 cp
->flags
|= TBL_CELL_BOLD
;
192 cp
->flags
|= TBL_CELL_ITALIC
;
202 mandoc_msg(MANDOCERR_TBLLAYOUT
, tbl
->parse
,
208 cell(struct tbl_node
*tbl
, struct tbl_row
*rp
,
209 int ln
, const char *p
, int *pos
)
214 /* Handle vertical lines. */
216 for (vert
= 0; '|' == p
[*pos
]; ++*pos
)
218 while (' ' == p
[*pos
])
221 /* Parse the column position (`c', `l', `r', ...). */
223 for (i
= 0; i
< KEYS_MAX
; i
++)
224 if (tolower((unsigned char)p
[*pos
]) == keys
[i
].name
)
228 mandoc_msg(MANDOCERR_TBLLAYOUT
, tbl
->parse
,
236 * If a span cell is found first, raise a warning and abort the
237 * parse. If a span cell is found and the last layout element
238 * isn't a "normal" layout, bail.
240 * FIXME: recover from this somehow?
243 if (TBL_CELL_SPAN
== c
) {
244 if (NULL
== rp
->first
) {
245 mandoc_msg(MANDOCERR_TBLLAYOUT
, tbl
->parse
,
249 switch (rp
->last
->pos
) {
250 case (TBL_CELL_HORIZ
):
251 case (TBL_CELL_DHORIZ
):
252 mandoc_msg(MANDOCERR_TBLLAYOUT
, tbl
->parse
,
261 * If a vertical spanner is found, we may not be in the first
265 if (TBL_CELL_DOWN
== c
&& rp
== tbl
->first_row
) {
266 mandoc_msg(MANDOCERR_TBLLAYOUT
, tbl
->parse
, ln
, *pos
, NULL
);
272 /* Disallow adjacent spacers. */
275 mandoc_msg(MANDOCERR_TBLLAYOUT
, tbl
->parse
, ln
, *pos
- 1, NULL
);
279 /* Allocate cell then parse its modifiers. */
281 return(mods(tbl
, cell_alloc(tbl
, rp
, c
, vert
), ln
, p
, pos
));
286 row(struct tbl_node
*tbl
, int ln
, const char *p
, int *pos
)
291 * EBNF describing this section:
293 * row ::= row_list [:space:]* [.]?[\n]
294 * row_list ::= [:space:]* row_elem row_tail
295 * row_tail ::= [:space:]*[,] row_list |
297 * row_elem ::= [\t\ ]*[:alpha:]+
300 rp
= mandoc_calloc(1, sizeof(struct tbl_row
));
302 tbl
->last_row
->next
= rp
;
308 while (isspace((unsigned char)p
[*pos
]))
311 /* Safely exit layout context. */
313 if ('.' == p
[*pos
]) {
314 tbl
->part
= TBL_PART_DATA
;
315 if (NULL
== tbl
->first_row
)
316 mandoc_msg(MANDOCERR_TBLNOLAYOUT
, tbl
->parse
,
322 /* End (and possibly restart) a row. */
324 if (',' == p
[*pos
]) {
327 } else if ('\0' == p
[*pos
])
330 if ( ! cell(tbl
, rp
, ln
, p
, pos
))
338 tbl_layout(struct tbl_node
*tbl
, int ln
, const char *p
)
343 row(tbl
, ln
, p
, &pos
);
345 /* Always succeed. */
349 static struct tbl_cell
*
350 cell_alloc(struct tbl_node
*tbl
, struct tbl_row
*rp
, enum tbl_cellt pos
,
353 struct tbl_cell
*p
, *pp
;
354 struct tbl_head
*h
, *hp
;
356 p
= mandoc_calloc(1, sizeof(struct tbl_cell
));
358 if (NULL
!= (pp
= rp
->last
)) {
377 hp
= mandoc_calloc(1, sizeof(struct tbl_head
));
378 hp
->ident
= tbl
->opts
.cols
++;
381 if (tbl
->last_head
) {
382 hp
->prev
= tbl
->last_head
;
383 tbl
->last_head
->next
= hp
;
385 tbl
->first_head
= hp
;