]>
git.cameronkatri.com Git - mandoc.git/blob - tbl_layout.c
1 /* $Id: tbl_layout.c,v 1.33 2015/01/27 05:21:45 schwarze Exp $ */
3 * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2012, 2014, 2015 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"
37 static const struct tbl_phrase keys
[] = {
38 { 'c', TBL_CELL_CENTRE
},
39 { 'r', TBL_CELL_RIGHT
},
40 { 'l', TBL_CELL_LEFT
},
41 { 'n', TBL_CELL_NUMBER
},
42 { 's', TBL_CELL_SPAN
},
43 { 'a', TBL_CELL_LONG
},
44 { '^', TBL_CELL_DOWN
},
45 { '-', TBL_CELL_HORIZ
},
46 { '_', TBL_CELL_HORIZ
},
47 { '=', TBL_CELL_DHORIZ
}
50 #define KEYS_MAX ((int)(sizeof(keys)/sizeof(keys[0])))
52 static void mods(struct tbl_node
*, struct tbl_cell
*,
53 int, const char *, int *);
54 static void cell(struct tbl_node
*, struct tbl_row
*,
55 int, const char *, int *);
56 static struct tbl_cell
*cell_alloc(struct tbl_node
*, struct tbl_row
*,
61 mods(struct tbl_node
*tbl
, struct tbl_cell
*cp
,
62 int ln
, const char *p
, int *pos
)
67 while (p
[*pos
] == ' ' || p
[*pos
] == '\t')
70 /* Row delimiters and cell specifiers end modifier lists. */
72 if (strchr(".,-=^_ACLNRSaclnrs", p
[*pos
]) != NULL
)
75 /* Throw away parenthesised expression. */
79 while (p
[*pos
] && ')' != p
[*pos
])
85 mandoc_msg(MANDOCERR_TBLLAYOUT_PAR
, tbl
->parse
,
90 /* Parse numerical spacing from modifier string. */
92 if (isdigit((unsigned char)p
[*pos
])) {
93 cp
->spacing
= strtoull(p
+ *pos
, &endptr
, 10);
98 switch (tolower((unsigned char)p
[(*pos
)++])) {
107 cp
->flags
|= TBL_CELL_BALIGN
;
110 cp
->flags
|= TBL_CELL_EQUAL
;
115 mandoc_msg(MANDOCERR_TBLLAYOUT_MOD
, tbl
->parse
,
121 if (p
[*pos
] == '-' || p
[*pos
] == '+')
123 while (isdigit((unsigned char)p
[*pos
]))
127 cp
->flags
|= TBL_CELL_TALIGN
;
130 cp
->flags
|= TBL_CELL_UP
;
132 case 'w': /* XXX for now, ignore minimal column width */
135 cp
->flags
|= TBL_CELL_WMAX
;
138 cp
->flags
|= TBL_CELL_WIGN
;
144 mandoc_msg(MANDOCERR_TBLLAYOUT_VERT
,
145 tbl
->parse
, ln
, *pos
- 1, NULL
);
148 mandoc_vmsg(MANDOCERR_TBLLAYOUT_CHAR
, tbl
->parse
,
149 ln
, *pos
- 1, "%c", p
[*pos
- 1]);
153 switch (tolower((unsigned char)p
[(*pos
)++])) {
157 cp
->flags
|= TBL_CELL_BOLD
;
162 cp
->flags
|= TBL_CELL_ITALIC
;
169 mandoc_vmsg(MANDOCERR_FT_BAD
, tbl
->parse
,
170 ln
, *pos
- 1, "TS f%c", p
[*pos
- 1]);
176 cell(struct tbl_node
*tbl
, struct tbl_row
*rp
,
177 int ln
, const char *p
, int *pos
)
182 /* Handle leading vertical lines */
184 while (p
[*pos
] == ' ' || p
[*pos
] == '\t' || p
[*pos
] == '|') {
185 if (p
[*pos
] == '|') {
189 mandoc_msg(MANDOCERR_TBLLAYOUT_VERT
,
190 tbl
->parse
, ln
, *pos
, NULL
);
196 while (p
[*pos
] == ' ' || p
[*pos
] == '\t')
199 if (p
[*pos
] == '.' || p
[*pos
] == '\0')
202 /* Parse the column position (`c', `l', `r', ...). */
204 for (i
= 0; i
< KEYS_MAX
; i
++)
205 if (tolower((unsigned char)p
[*pos
]) == keys
[i
].name
)
209 mandoc_vmsg(MANDOCERR_TBLLAYOUT_CHAR
, tbl
->parse
,
210 ln
, *pos
, "%c", p
[*pos
]);
216 /* Special cases of spanners. */
218 if (c
== TBL_CELL_SPAN
) {
219 if (rp
->last
== NULL
)
220 mandoc_msg(MANDOCERR_TBLLAYOUT_SPAN
,
221 tbl
->parse
, ln
, *pos
, NULL
);
222 else if (rp
->last
->pos
== TBL_CELL_HORIZ
||
223 rp
->last
->pos
== TBL_CELL_DHORIZ
)
225 } else if (c
== TBL_CELL_DOWN
&& rp
== tbl
->first_row
)
226 mandoc_msg(MANDOCERR_TBLLAYOUT_DOWN
,
227 tbl
->parse
, ln
, *pos
, NULL
);
231 /* Allocate cell then parse its modifiers. */
233 mods(tbl
, cell_alloc(tbl
, rp
, c
), ln
, p
, pos
);
237 tbl_layout(struct tbl_node
*tbl
, int ln
, const char *p
)
246 /* Skip whitespace before and after each cell. */
248 while (p
[pos
] == ' ' || p
[pos
] == '\t')
252 case ',': /* Next row on this input line. */
256 case '\0': /* Next row on next input line. */
258 case '.': /* End of layout. */
260 tbl
->part
= TBL_PART_DATA
;
263 * When the layout is completely empty,
264 * default to one left-justified column.
267 if (tbl
->first_row
== NULL
) {
268 mandoc_msg(MANDOCERR_TBLLAYOUT_NONE
,
269 tbl
->parse
, ln
, pos
, NULL
);
270 rp
= mandoc_calloc(1, sizeof(*rp
));
271 cell_alloc(tbl
, rp
, TBL_CELL_LEFT
);
272 tbl
->first_row
= tbl
->last_row
= rp
;
277 * Search for the widest line
278 * along the left and right margins.
281 for (rp
= tbl
->first_row
; rp
; rp
= rp
->next
) {
282 if (tbl
->opts
.lvert
< rp
->vert
)
283 tbl
->opts
.lvert
= rp
->vert
;
284 if (rp
->last
!= NULL
&&
285 rp
->last
->head
== tbl
->last_head
&&
286 tbl
->opts
.rvert
< rp
->last
->vert
)
287 tbl
->opts
.rvert
= rp
->last
->vert
;
294 if (rp
== NULL
) { /* First cell on this line. */
295 rp
= mandoc_calloc(1, sizeof(*rp
));
297 tbl
->last_row
->next
= rp
;
302 cell(tbl
, rp
, ln
, p
, &pos
);
306 static struct tbl_cell
*
307 cell_alloc(struct tbl_node
*tbl
, struct tbl_row
*rp
, enum tbl_cellt pos
)
309 struct tbl_cell
*p
, *pp
;
310 struct tbl_head
*h
, *hp
;
312 p
= mandoc_calloc(1, sizeof(struct tbl_cell
));
314 if (NULL
!= (pp
= rp
->last
)) {
332 hp
= mandoc_calloc(1, sizeof(struct tbl_head
));
333 hp
->ident
= tbl
->opts
.cols
++;
335 if (tbl
->last_head
) {
336 hp
->prev
= tbl
->last_head
;
337 tbl
->last_head
->next
= hp
;
339 tbl
->first_head
= hp
;