]>
git.cameronkatri.com Git - mandoc.git/blob - tbl_layout.c
1 /* $Id: tbl_layout.c,v 1.28 2014/10/07 14:07:03 schwarze Exp $ */
3 * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2012, 2014 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.
20 #include <sys/types.h>
28 #include "mandoc_aux.h"
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
);
68 mods(struct tbl_node
*tbl
, struct tbl_cell
*cp
,
69 int ln
, const char *p
, int *pos
)
74 /* Not all types accept modifiers. */
89 * XXX: since, at least for now, modifiers are non-conflicting
90 * (are separable by value, regardless of position), we let
91 * modifiers come in any order. The existing tbl doesn't let
111 /* Throw away parenthesised expression. */
113 if ('(' == p
[*pos
]) {
115 while (p
[*pos
] && ')' != p
[*pos
])
117 if (')' == p
[*pos
]) {
121 mandoc_msg(MANDOCERR_TBLLAYOUT
, tbl
->parse
,
126 /* Parse numerical spacing from modifier string. */
128 if (isdigit((unsigned char)p
[*pos
])) {
129 for (i
= 0; i
< 4; i
++) {
130 if ( ! isdigit((unsigned char)p
[*pos
+ i
]))
132 buf
[i
] = p
[*pos
+ i
];
136 /* No greater than 4 digits. */
139 mandoc_msg(MANDOCERR_TBLLAYOUT
,
140 tbl
->parse
, ln
, *pos
, NULL
);
145 cp
->spacing
= (size_t)atoi(buf
);
151 /* TODO: GNU has many more extensions. */
153 switch (tolower((unsigned char)p
[(*pos
)++])) {
155 cp
->flags
|= TBL_CELL_WIGN
;
158 cp
->flags
|= TBL_CELL_UP
;
161 cp
->flags
|= TBL_CELL_EQUAL
;
164 cp
->flags
|= TBL_CELL_TALIGN
;
167 cp
->flags
|= TBL_CELL_BALIGN
;
169 case 'w': /* XXX for now, ignore minimal column width */
181 mandoc_msg(MANDOCERR_TBLLAYOUT
, tbl
->parse
,
186 switch (tolower((unsigned char)p
[(*pos
)++])) {
190 cp
->flags
|= TBL_CELL_BOLD
;
195 cp
->flags
|= TBL_CELL_ITALIC
;
204 if (isalnum((unsigned char)p
[*pos
- 1])) {
205 mandoc_vmsg(MANDOCERR_FT_BAD
, tbl
->parse
,
206 ln
, *pos
- 1, "TS f%c", p
[*pos
- 1]);
210 mandoc_msg(MANDOCERR_TBLLAYOUT
, tbl
->parse
,
216 cell(struct tbl_node
*tbl
, struct tbl_row
*rp
,
217 int ln
, const char *p
, int *pos
)
222 /* Handle vertical lines. */
224 for (vert
= 0; '|' == p
[*pos
]; ++*pos
)
226 while (' ' == p
[*pos
])
229 /* Handle trailing vertical lines */
231 if ('.' == p
[*pos
] || '\0' == p
[*pos
]) {
236 /* Parse the column position (`c', `l', `r', ...). */
238 for (i
= 0; i
< KEYS_MAX
; i
++)
239 if (tolower((unsigned char)p
[*pos
]) == keys
[i
].name
)
243 mandoc_msg(MANDOCERR_TBLLAYOUT
, tbl
->parse
,
251 * If a span cell is found first, raise a warning and abort the
252 * parse. If a span cell is found and the last layout element
253 * isn't a "normal" layout, bail.
255 * FIXME: recover from this somehow?
258 if (TBL_CELL_SPAN
== c
) {
259 if (NULL
== rp
->first
) {
260 mandoc_msg(MANDOCERR_TBLLAYOUT
, tbl
->parse
,
264 switch (rp
->last
->pos
) {
267 case TBL_CELL_DHORIZ
:
268 mandoc_msg(MANDOCERR_TBLLAYOUT
,
269 tbl
->parse
, ln
, *pos
, NULL
);
277 * If a vertical spanner is found, we may not be in the first
281 if (TBL_CELL_DOWN
== c
&& rp
== tbl
->first_row
) {
282 mandoc_msg(MANDOCERR_TBLLAYOUT
, tbl
->parse
, ln
, *pos
, NULL
);
288 /* Disallow adjacent spacers. */
291 mandoc_msg(MANDOCERR_TBLLAYOUT
, tbl
->parse
, ln
, *pos
- 1, NULL
);
295 /* Allocate cell then parse its modifiers. */
297 return(mods(tbl
, cell_alloc(tbl
, rp
, c
, vert
), ln
, p
, pos
));
301 row(struct tbl_node
*tbl
, int ln
, const char *p
, int *pos
)
306 * EBNF describing this section:
308 * row ::= row_list [:space:]* [.]?[\n]
309 * row_list ::= [:space:]* row_elem row_tail
310 * row_tail ::= [:space:]*[,] row_list |
312 * row_elem ::= [\t\ ]*[:alpha:]+
315 rp
= mandoc_calloc(1, sizeof(struct tbl_row
));
317 tbl
->last_row
->next
= rp
;
323 while (isspace((unsigned char)p
[*pos
]))
326 /* Safely exit layout context. */
328 if ('.' == p
[*pos
]) {
329 tbl
->part
= TBL_PART_DATA
;
330 if (NULL
== tbl
->first_row
)
331 mandoc_msg(MANDOCERR_TBLNOLAYOUT
,
332 tbl
->parse
, ln
, *pos
, NULL
);
337 /* End (and possibly restart) a row. */
339 if (',' == p
[*pos
]) {
342 } else if ('\0' == p
[*pos
])
345 if ( ! cell(tbl
, rp
, ln
, p
, pos
))
353 tbl_layout(struct tbl_node
*tbl
, int ln
, const char *p
)
358 row(tbl
, ln
, p
, &pos
);
360 /* Always succeed. */
364 static struct tbl_cell
*
365 cell_alloc(struct tbl_node
*tbl
, struct tbl_row
*rp
, enum tbl_cellt pos
,
368 struct tbl_cell
*p
, *pp
;
369 struct tbl_head
*h
, *hp
;
371 p
= mandoc_calloc(1, sizeof(struct tbl_cell
));
373 if (NULL
!= (pp
= rp
->last
)) {
392 hp
= mandoc_calloc(1, sizeof(struct tbl_head
));
393 hp
->ident
= tbl
->opts
.cols
++;
396 if (tbl
->last_head
) {
397 hp
->prev
= tbl
->last_head
;
398 tbl
->last_head
->next
= hp
;
400 tbl
->first_head
= hp
;