]>
git.cameronkatri.com Git - mandoc.git/blob - tbl_layout.c
1 /* $Id: tbl_layout.c,v 1.1 2010/12/29 14:38:14 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.
23 #include "libmandoc.h"
33 static const struct tbl_phrase keys
[KEYS_MAX
] = {
34 { 'c', TBL_CELL_CENTRE
},
35 { 'C', TBL_CELL_CENTRE
},
36 { 'r', TBL_CELL_RIGHT
},
37 { 'R', TBL_CELL_RIGHT
},
38 { 'l', TBL_CELL_LEFT
},
39 { 'L', TBL_CELL_LEFT
},
40 { 'n', TBL_CELL_NUMBER
},
41 { 'N', TBL_CELL_NUMBER
},
42 { 's', TBL_CELL_SPAN
},
43 { 'S', TBL_CELL_SPAN
},
44 { 'a', TBL_CELL_LONG
},
45 { 'A', TBL_CELL_LONG
},
46 { '^', TBL_CELL_DOWN
},
47 { '-', TBL_CELL_HORIZ
},
48 { '_', TBL_CELL_HORIZ
},
49 { '=', TBL_CELL_DHORIZ
},
50 { '|', TBL_CELL_VERT
}
53 static int mods(struct tbl
*, struct tbl_cell
*,
54 int, const char *, int *);
55 static int cell(struct tbl
*, struct tbl_row
*,
56 int, const char *, int *);
57 static void row(struct tbl
*, int, const char *, int *);
60 mods(struct tbl
*tbl
, struct tbl_cell
*cp
,
61 int ln
, const char *p
, int *pos
)
68 * XXX: since, at least for now, modifiers are non-conflicting
69 * (are separable by value, regardless of position), we let
70 * modifiers come in any order. The existing tbl doesn't let
88 /* Parse numerical spacing from modifier string. */
90 if (isdigit((unsigned char)p
[*pos
])) {
91 for (i
= 0; i
< 4; i
++) {
92 if ( ! isdigit((unsigned char)p
[*pos
+ i
]))
98 /* No greater than 4 digits. */
101 TBL_MSG(tbl
, MANDOCERR_TBLLAYOUT
, ln
, *pos
);
106 cp
->spacing
= atoi(buf
);
112 /* TODO: GNU has many more extensions. */
114 switch (p
[(*pos
)++]) {
118 cp
->flags
|= TBL_CELL_WIGN
;
123 cp
->flags
|= TBL_CELL_UP
;
128 cp
->flags
|= TBL_CELL_EQUAL
;
133 cp
->flags
|= TBL_CELL_TALIGN
;
138 cp
->flags
|= TBL_CELL_BALIGN
;
151 TBL_MSG(tbl
, MANDOCERR_TBLLAYOUT
, ln
, *pos
- 1);
155 switch (p
[(*pos
)++]) {
159 cp
->flags
|= TBL_CELL_BOLD
;
164 cp
->flags
|= TBL_CELL_ITALIC
;
170 TBL_MSG(tbl
, MANDOCERR_TBLLAYOUT
, ln
, *pos
- 1);
175 cell(struct tbl
*tbl
, struct tbl_row
*rp
,
176 int ln
, const char *p
, int *pos
)
182 /* Parse the column position (`r', `R', `|', ...). */
184 for (i
= 0; i
< KEYS_MAX
; i
++)
185 if (p
[*pos
] == keys
[i
].name
)
189 TBL_MSG(tbl
, MANDOCERR_TBLLAYOUT
, ln
, *pos
);
196 /* Extra check for the double-vertical. */
198 if (TBL_CELL_VERT
== c
&& '|' == p
[*pos
]) {
203 /* Disallow adjacent spacers. */
205 if (rp
->last
&& (TBL_CELL_VERT
== c
|| TBL_CELL_DVERT
== c
) &&
206 (TBL_CELL_VERT
== rp
->last
->pos
||
207 TBL_CELL_DVERT
== rp
->last
->pos
)) {
208 TBL_MSG(tbl
, MANDOCERR_TBLLAYOUT
, ln
, *pos
- 1);
212 /* Allocate cell then parse its modifiers. */
214 cp
= mandoc_calloc(1, sizeof(struct tbl_cell
));
221 rp
->last
= rp
->first
= cp
;
223 return(mods(tbl
, cp
, ln
, p
, pos
));
228 row(struct tbl
*tbl
, int ln
, const char *p
, int *pos
)
233 * EBNF describing this section:
235 * row ::= row_list [:space:]* [.]?[\n]
236 * row_list ::= [:space:]* row_elem row_tail
237 * row_tail ::= [:space:]*[,] row_list |
239 * row_elem ::= [\t\ ]*[:alpha:]+
242 rp
= mandoc_calloc(1, sizeof(struct tbl_row
));
244 tbl
->last
->next
= rp
;
247 tbl
->last
= tbl
->first
= rp
;
250 while (isspace((unsigned char)p
[*pos
]))
253 /* Safely exit layout context. */
255 if ('.' == p
[*pos
]) {
256 tbl
->part
= TBL_PART_DATA
;
257 if (NULL
== tbl
->first
)
258 TBL_MSG(tbl
, MANDOCERR_TBLNOLAYOUT
, ln
, *pos
);
263 /* End (and possibly restart) a row. */
265 if (',' == p
[*pos
]) {
268 } else if ('\0' == p
[*pos
])
271 if ( ! cell(tbl
, rp
, ln
, p
, pos
))
280 tbl_layout(struct tbl
*tbl
, int ln
, const char *p
)
285 row(tbl
, ln
, p
, &pos
);
287 /* Always succeed. */