]>
git.cameronkatri.com Git - mandoc.git/blob - tbl_layout.c
1 /* $Id: tbl_layout.c,v 1.2 2010/12/29 15:21:34 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 { 'r', TBL_CELL_RIGHT
},
36 { 'l', TBL_CELL_LEFT
},
37 { 'n', TBL_CELL_NUMBER
},
38 { 's', TBL_CELL_SPAN
},
39 { 'a', TBL_CELL_LONG
},
40 { '^', TBL_CELL_DOWN
},
41 { '-', TBL_CELL_HORIZ
},
42 { '_', TBL_CELL_HORIZ
},
43 { '=', TBL_CELL_DHORIZ
},
44 { '|', TBL_CELL_VERT
}
47 static int mods(struct tbl
*, struct tbl_cell
*,
48 int, const char *, int *);
49 static int cell(struct tbl
*, struct tbl_row
*,
50 int, const char *, int *);
51 static void row(struct tbl
*, int, const char *, int *);
54 mods(struct tbl
*tbl
, struct tbl_cell
*cp
,
55 int ln
, const char *p
, int *pos
)
62 * XXX: since, at least for now, modifiers are non-conflicting
63 * (are separable by value, regardless of position), we let
64 * modifiers come in any order. The existing tbl doesn't let
82 /* Parse numerical spacing from modifier string. */
84 if (isdigit((unsigned char)p
[*pos
])) {
85 for (i
= 0; i
< 4; i
++) {
86 if ( ! isdigit((unsigned char)p
[*pos
+ i
]))
92 /* No greater than 4 digits. */
95 TBL_MSG(tbl
, MANDOCERR_TBLLAYOUT
, ln
, *pos
);
100 cp
->spacing
= atoi(buf
);
106 /* TODO: GNU has many more extensions. */
108 switch (tolower(p
[(*pos
)++])) {
110 cp
->flags
|= TBL_CELL_WIGN
;
113 cp
->flags
|= TBL_CELL_UP
;
116 cp
->flags
|= TBL_CELL_EQUAL
;
119 cp
->flags
|= TBL_CELL_TALIGN
;
122 cp
->flags
|= TBL_CELL_BALIGN
;
132 TBL_MSG(tbl
, MANDOCERR_TBLLAYOUT
, ln
, *pos
- 1);
136 switch (tolower(p
[(*pos
)++])) {
138 cp
->flags
|= TBL_CELL_BOLD
;
141 cp
->flags
|= TBL_CELL_ITALIC
;
147 TBL_MSG(tbl
, MANDOCERR_TBLLAYOUT
, ln
, *pos
- 1);
152 cell(struct tbl
*tbl
, struct tbl_row
*rp
,
153 int ln
, const char *p
, int *pos
)
159 /* Parse the column position (`r', `R', `|', ...). */
161 for (i
= 0; i
< KEYS_MAX
; i
++)
162 if (tolower(p
[*pos
]) == keys
[i
].name
)
166 TBL_MSG(tbl
, MANDOCERR_TBLLAYOUT
, ln
, *pos
);
173 /* Extra check for the double-vertical. */
175 if (TBL_CELL_VERT
== c
&& '|' == p
[*pos
]) {
180 /* Disallow adjacent spacers. */
182 if (rp
->last
&& (TBL_CELL_VERT
== c
|| TBL_CELL_DVERT
== c
) &&
183 (TBL_CELL_VERT
== rp
->last
->pos
||
184 TBL_CELL_DVERT
== rp
->last
->pos
)) {
185 TBL_MSG(tbl
, MANDOCERR_TBLLAYOUT
, ln
, *pos
- 1);
189 /* Allocate cell then parse its modifiers. */
191 cp
= mandoc_calloc(1, sizeof(struct tbl_cell
));
198 rp
->last
= rp
->first
= cp
;
200 return(mods(tbl
, cp
, ln
, p
, pos
));
205 row(struct tbl
*tbl
, int ln
, const char *p
, int *pos
)
210 * EBNF describing this section:
212 * row ::= row_list [:space:]* [.]?[\n]
213 * row_list ::= [:space:]* row_elem row_tail
214 * row_tail ::= [:space:]*[,] row_list |
216 * row_elem ::= [\t\ ]*[:alpha:]+
219 rp
= mandoc_calloc(1, sizeof(struct tbl_row
));
221 tbl
->last
->next
= rp
;
224 tbl
->last
= tbl
->first
= rp
;
227 while (isspace((unsigned char)p
[*pos
]))
230 /* Safely exit layout context. */
232 if ('.' == p
[*pos
]) {
233 tbl
->part
= TBL_PART_DATA
;
234 if (NULL
== tbl
->first
)
235 TBL_MSG(tbl
, MANDOCERR_TBLNOLAYOUT
, ln
, *pos
);
240 /* End (and possibly restart) a row. */
242 if (',' == p
[*pos
]) {
245 } else if ('\0' == p
[*pos
])
248 if ( ! cell(tbl
, rp
, ln
, p
, pos
))
257 tbl_layout(struct tbl
*tbl
, int ln
, const char *p
)
262 row(tbl
, ln
, p
, &pos
);
264 /* Always succeed. */