Annotation of mandoc/tbl.c, Revision 1.18
1.18 ! kristaps 1: /* $Id: tbl.c,v 1.17 2011/01/02 10:10:57 kristaps Exp $ */
1.1 kristaps 2: /*
1.4 kristaps 3: * Copyright (c) 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
1.1 kristaps 4: *
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.
8: *
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.
16: */
17: #include <assert.h>
1.3 kristaps 18: #include <stdio.h>
1.1 kristaps 19: #include <stdlib.h>
20: #include <string.h>
21: #include <time.h>
22:
23: #include "mandoc.h"
24: #include "roff.h"
25: #include "libmandoc.h"
26: #include "libroff.h"
27:
1.17 kristaps 28: static void tbl_calc(struct tbl_node *);
29: static void tbl_calc_data(struct tbl_node *, struct tbl_dat *);
1.16 kristaps 30: static void tbl_calc_data_literal(struct tbl_dat *);
1.17 kristaps 31: static void tbl_calc_data_number(struct tbl_node *, struct tbl_dat *);
1.16 kristaps 32: static void tbl_calc_data_spanner(struct tbl_dat *);
33:
1.1 kristaps 34: enum rofferr
1.17 kristaps 35: tbl_read(struct tbl_node *tbl, int ln, const char *p, int offs)
1.1 kristaps 36: {
37: int len;
38: const char *cp;
39:
40: cp = &p[offs];
41: len = (int)strlen(cp);
42:
1.5 kristaps 43: /*
44: * If we're in the options section and we don't have a
45: * terminating semicolon, assume we've moved directly into the
46: * layout section. No need to report a warning: this is,
47: * apparently, standard behaviour.
48: */
49:
50: if (TBL_PART_OPTS == tbl->part && len)
1.1 kristaps 51: if (';' != cp[len - 1])
52: tbl->part = TBL_PART_LAYOUT;
1.5 kristaps 53:
54: /* Now process each logical section of the table. */
55:
56: switch (tbl->part) {
57: case (TBL_PART_OPTS):
58: return(tbl_option(tbl, ln, p) ? ROFF_IGN : ROFF_ERR);
1.6 kristaps 59: case (TBL_PART_LAYOUT):
60: return(tbl_layout(tbl, ln, p) ? ROFF_IGN : ROFF_ERR);
1.8 kristaps 61: case (TBL_PART_DATA):
1.5 kristaps 62: break;
1.8 kristaps 63: }
64:
1.12 kristaps 65: /*
66: * This only returns zero if the line is empty, so we ignore it
67: * and continue on.
1.9 kristaps 68: */
1.12 kristaps 69: return(tbl_data(tbl, ln, p) ? ROFF_TBL : ROFF_IGN);
1.1 kristaps 70: }
71:
1.17 kristaps 72: struct tbl_node *
1.13 kristaps 73: tbl_alloc(int pos, int line, void *data, const mandocmsg msg)
1.1 kristaps 74: {
1.17 kristaps 75: struct tbl_node *p;
1.1 kristaps 76:
1.17 kristaps 77: p = mandoc_calloc(1, sizeof(struct tbl_node));
1.13 kristaps 78: p->line = line;
79: p->pos = pos;
1.5 kristaps 80: p->data = data;
81: p->msg = msg;
1.10 kristaps 82: p->part = TBL_PART_OPTS;
1.17 kristaps 83: p->opts.tab = '\t';
84: p->opts.linesize = 12;
85: p->opts.decimal = '.';
1.1 kristaps 86: return(p);
87: }
88:
89: void
1.17 kristaps 90: tbl_free(struct tbl_node *p)
1.1 kristaps 91: {
1.10 kristaps 92: struct tbl_row *rp;
93: struct tbl_cell *cp;
94: struct tbl_span *sp;
95: struct tbl_dat *dp;
1.15 kristaps 96: struct tbl_head *hp;
1.1 kristaps 97:
1.15 kristaps 98: while (NULL != (rp = p->first_row)) {
1.10 kristaps 99: p->first_row = rp->next;
100: while (rp->first) {
101: cp = rp->first;
102: rp->first = cp->next;
103: free(cp);
104: }
105: free(rp);
106: }
1.1 kristaps 107:
1.15 kristaps 108: while (NULL != (sp = p->first_span)) {
1.10 kristaps 109: p->first_span = sp->next;
110: while (sp->first) {
111: dp = sp->first;
112: sp->first = dp->next;
113: if (dp->string)
114: free(dp->string);
115: free(dp);
116: }
117: free(sp);
1.15 kristaps 118: }
119:
120: while (NULL != (hp = p->first_head)) {
121: p->first_head = hp->next;
122: free(hp);
1.10 kristaps 123: }
1.1 kristaps 124:
1.10 kristaps 125: free(p);
1.7 kristaps 126: }
127:
128: void
1.17 kristaps 129: tbl_restart(int line, int pos, struct tbl_node *tbl)
1.7 kristaps 130: {
131:
132: tbl->part = TBL_PART_LAYOUT;
1.14 kristaps 133: tbl->line = line;
134: tbl->pos = pos;
135:
136: if (NULL == tbl->first_span || NULL == tbl->first_span->first)
137: TBL_MSG(tbl, MANDOCERR_TBLNODATA, tbl->line, tbl->pos);
1.1 kristaps 138: }
1.3 kristaps 139:
1.11 kristaps 140: const struct tbl_span *
1.17 kristaps 141: tbl_span(const struct tbl_node *tbl)
1.11 kristaps 142: {
143:
144: assert(tbl);
145: return(tbl->last_span);
1.13 kristaps 146: }
147:
148: void
1.17 kristaps 149: tbl_end(struct tbl_node *tbl)
1.13 kristaps 150: {
151:
152: if (NULL == tbl->first_span || NULL == tbl->first_span->first)
153: TBL_MSG(tbl, MANDOCERR_TBLNODATA, tbl->line, tbl->pos);
1.16 kristaps 154: else
155: tbl_calc(tbl);
1.18 ! kristaps 156:
! 157: if (tbl->last_span)
! 158: tbl->last_span->flags |= TBL_SPAN_LAST;
1.16 kristaps 159: }
160:
161: static void
1.17 kristaps 162: tbl_calc(struct tbl_node *tbl)
1.16 kristaps 163: {
164: struct tbl_span *sp;
165: struct tbl_dat *dp;
166: struct tbl_head *hp;
167:
168: /* Calculate width as the max of column cells' widths. */
169:
170: for (sp = tbl->first_span; sp; sp = sp->next) {
171: switch (sp->pos) {
172: case (TBL_DATA_HORIZ):
173: /* FALLTHROUGH */
174: case (TBL_DATA_DHORIZ):
175: continue;
176: default:
177: break;
178: }
179: for (dp = sp->first; dp; dp = dp->next)
180: tbl_calc_data(tbl, dp);
181: }
182:
183: /* Calculate width as the simple spanner value. */
184:
185: for (hp = tbl->first_head; hp; hp = hp->next)
186: switch (hp->pos) {
187: case (TBL_HEAD_VERT):
188: hp->width = 1;
189: break;
190: case (TBL_HEAD_DVERT):
191: hp->width = 2;
192: break;
193: default:
194: break;
195: }
196: }
197:
198: static void
1.17 kristaps 199: tbl_calc_data(struct tbl_node *tbl, struct tbl_dat *data)
1.16 kristaps 200: {
201:
202: /*
203: * This is the case with overrunning cells...
204: */
205: if (NULL == data->layout)
206: return;
207:
208: /* Branch down into data sub-types. */
209:
210: switch (data->layout->pos) {
211: case (TBL_CELL_HORIZ):
212: /* FALLTHROUGH */
213: case (TBL_CELL_DHORIZ):
214: tbl_calc_data_spanner(data);
215: break;
216: case (TBL_CELL_LONG):
217: /* FALLTHROUGH */
218: case (TBL_CELL_CENTRE):
219: /* FALLTHROUGH */
220: case (TBL_CELL_LEFT):
221: /* FALLTHROUGH */
222: case (TBL_CELL_RIGHT):
223: tbl_calc_data_literal(data);
224: break;
225: case (TBL_CELL_NUMBER):
226: tbl_calc_data_number(tbl, data);
227: break;
228: default:
229: abort();
230: /* NOTREACHED */
231: }
232: }
233:
234: static void
235: tbl_calc_data_spanner(struct tbl_dat *data)
236: {
237:
238: /* N.B., these are horiz spanners (not vert) so always 1. */
239: data->layout->head->width = 1;
240: }
241:
242: static void
1.17 kristaps 243: tbl_calc_data_number(struct tbl_node *tbl, struct tbl_dat *data)
1.16 kristaps 244: {
245: int sz, d;
246: char *dp, pnt;
247:
248: /*
249: * First calculate number width and decimal place (last + 1 for
250: * no-decimal numbers). If the stored decimal is subsequent
251: * ours, make our size longer by that difference
252: * (right-"shifting"); similarly, if ours is subsequent the
253: * stored, then extend the stored size by the difference.
254: * Finally, re-assign the stored values.
255: */
256:
257: /* TODO: use spacing modifier. */
258:
259: assert(data->string);
260: sz = (int)strlen(data->string);
1.17 kristaps 261: pnt = tbl->opts.decimal;
1.16 kristaps 262:
263: if (NULL == (dp = strchr(data->string, pnt)))
264: d = sz + 1;
265: else
266: d = (int)(dp - data->string) + 1;
267:
268: sz += 2;
269:
270: if (data->layout->head->decimal > d) {
271: sz += data->layout->head->decimal - d;
272: d = data->layout->head->decimal;
273: } else
274: data->layout->head->width +=
275: d - data->layout->head->decimal;
276:
277: if (sz > data->layout->head->width)
278: data->layout->head->width = sz;
279: if (d > data->layout->head->decimal)
280: data->layout->head->decimal = d;
1.11 kristaps 281: }
1.16 kristaps 282:
283: static void
284: tbl_calc_data_literal(struct tbl_dat *data)
285: {
286: int sz, bufsz;
287:
288: /*
289: * Calculate our width and use the spacing, with a minimum
290: * spacing dictated by position (centre, e.g,. gets a space on
291: * either side, while right/left get a single adjacent space).
292: */
293:
294: assert(data->string);
295: sz = (int)strlen(data->string);
296:
297: switch (data->layout->pos) {
298: case (TBL_CELL_LONG):
299: /* FALLTHROUGH */
300: case (TBL_CELL_CENTRE):
301: bufsz = 2;
302: break;
303: default:
304: bufsz = 1;
305: break;
306: }
307:
308: if (data->layout->spacing)
309: bufsz = bufsz > data->layout->spacing ?
310: bufsz : data->layout->spacing;
311:
312: sz += bufsz;
313: if (data->layout->head->width < sz)
314: data->layout->head->width = sz;
315: }
316:
1.14 kristaps 317:
CVSweb