1*ffb8ebfaSGarrett D'Amore /* $Id: tbl.c,v 1.27 2013/05/31 22:08:09 schwarze Exp $ */
232a712daSGarrett D'Amore /*
332a712daSGarrett D'Amore * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
432a712daSGarrett D'Amore * Copyright (c) 2011 Ingo Schwarze <schwarze@openbsd.org>
532a712daSGarrett D'Amore *
632a712daSGarrett D'Amore * Permission to use, copy, modify, and distribute this software for any
732a712daSGarrett D'Amore * purpose with or without fee is hereby granted, provided that the above
832a712daSGarrett D'Amore * copyright notice and this permission notice appear in all copies.
932a712daSGarrett D'Amore *
1032a712daSGarrett D'Amore * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1132a712daSGarrett D'Amore * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1232a712daSGarrett D'Amore * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1332a712daSGarrett D'Amore * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1432a712daSGarrett D'Amore * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1532a712daSGarrett D'Amore * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1632a712daSGarrett D'Amore * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1732a712daSGarrett D'Amore */
1832a712daSGarrett D'Amore #ifdef HAVE_CONFIG_H
1932a712daSGarrett D'Amore #include "config.h"
2032a712daSGarrett D'Amore #endif
2132a712daSGarrett D'Amore
2232a712daSGarrett D'Amore #include <assert.h>
2332a712daSGarrett D'Amore #include <stdio.h>
2432a712daSGarrett D'Amore #include <stdlib.h>
2532a712daSGarrett D'Amore #include <string.h>
2632a712daSGarrett D'Amore #include <time.h>
2732a712daSGarrett D'Amore
2832a712daSGarrett D'Amore #include "mandoc.h"
2932a712daSGarrett D'Amore #include "libmandoc.h"
3032a712daSGarrett D'Amore #include "libroff.h"
3132a712daSGarrett D'Amore
3232a712daSGarrett D'Amore enum rofferr
tbl_read(struct tbl_node * tbl,int ln,const char * p,int offs)3332a712daSGarrett D'Amore tbl_read(struct tbl_node *tbl, int ln, const char *p, int offs)
3432a712daSGarrett D'Amore {
3532a712daSGarrett D'Amore int len;
3632a712daSGarrett D'Amore const char *cp;
3732a712daSGarrett D'Amore
3832a712daSGarrett D'Amore cp = &p[offs];
3932a712daSGarrett D'Amore len = (int)strlen(cp);
4032a712daSGarrett D'Amore
4132a712daSGarrett D'Amore /*
4232a712daSGarrett D'Amore * If we're in the options section and we don't have a
4332a712daSGarrett D'Amore * terminating semicolon, assume we've moved directly into the
4432a712daSGarrett D'Amore * layout section. No need to report a warning: this is,
4532a712daSGarrett D'Amore * apparently, standard behaviour.
4632a712daSGarrett D'Amore */
4732a712daSGarrett D'Amore
4832a712daSGarrett D'Amore if (TBL_PART_OPTS == tbl->part && len)
4932a712daSGarrett D'Amore if (';' != cp[len - 1])
5032a712daSGarrett D'Amore tbl->part = TBL_PART_LAYOUT;
5132a712daSGarrett D'Amore
5232a712daSGarrett D'Amore /* Now process each logical section of the table. */
5332a712daSGarrett D'Amore
5432a712daSGarrett D'Amore switch (tbl->part) {
5532a712daSGarrett D'Amore case (TBL_PART_OPTS):
5632a712daSGarrett D'Amore return(tbl_option(tbl, ln, p) ? ROFF_IGN : ROFF_ERR);
5732a712daSGarrett D'Amore case (TBL_PART_LAYOUT):
5832a712daSGarrett D'Amore return(tbl_layout(tbl, ln, p) ? ROFF_IGN : ROFF_ERR);
5932a712daSGarrett D'Amore case (TBL_PART_CDATA):
6032a712daSGarrett D'Amore return(tbl_cdata(tbl, ln, p) ? ROFF_TBL : ROFF_IGN);
6132a712daSGarrett D'Amore default:
6232a712daSGarrett D'Amore break;
6332a712daSGarrett D'Amore }
6432a712daSGarrett D'Amore
6532a712daSGarrett D'Amore /*
6632a712daSGarrett D'Amore * This only returns zero if the line is empty, so we ignore it
6732a712daSGarrett D'Amore * and continue on.
6832a712daSGarrett D'Amore */
6932a712daSGarrett D'Amore return(tbl_data(tbl, ln, p) ? ROFF_TBL : ROFF_IGN);
7032a712daSGarrett D'Amore }
7132a712daSGarrett D'Amore
7232a712daSGarrett D'Amore struct tbl_node *
tbl_alloc(int pos,int line,struct mparse * parse)7332a712daSGarrett D'Amore tbl_alloc(int pos, int line, struct mparse *parse)
7432a712daSGarrett D'Amore {
75*ffb8ebfaSGarrett D'Amore struct tbl_node *tbl;
7632a712daSGarrett D'Amore
77*ffb8ebfaSGarrett D'Amore tbl = mandoc_calloc(1, sizeof(struct tbl_node));
78*ffb8ebfaSGarrett D'Amore tbl->line = line;
79*ffb8ebfaSGarrett D'Amore tbl->pos = pos;
80*ffb8ebfaSGarrett D'Amore tbl->parse = parse;
81*ffb8ebfaSGarrett D'Amore tbl->part = TBL_PART_OPTS;
82*ffb8ebfaSGarrett D'Amore tbl->opts.tab = '\t';
83*ffb8ebfaSGarrett D'Amore tbl->opts.linesize = 12;
84*ffb8ebfaSGarrett D'Amore tbl->opts.decimal = '.';
85*ffb8ebfaSGarrett D'Amore return(tbl);
8632a712daSGarrett D'Amore }
8732a712daSGarrett D'Amore
8832a712daSGarrett D'Amore void
tbl_free(struct tbl_node * tbl)89*ffb8ebfaSGarrett D'Amore tbl_free(struct tbl_node *tbl)
9032a712daSGarrett D'Amore {
9132a712daSGarrett D'Amore struct tbl_row *rp;
9232a712daSGarrett D'Amore struct tbl_cell *cp;
9332a712daSGarrett D'Amore struct tbl_span *sp;
9432a712daSGarrett D'Amore struct tbl_dat *dp;
9532a712daSGarrett D'Amore struct tbl_head *hp;
9632a712daSGarrett D'Amore
97*ffb8ebfaSGarrett D'Amore while (NULL != (rp = tbl->first_row)) {
98*ffb8ebfaSGarrett D'Amore tbl->first_row = rp->next;
9932a712daSGarrett D'Amore while (rp->first) {
10032a712daSGarrett D'Amore cp = rp->first;
10132a712daSGarrett D'Amore rp->first = cp->next;
10232a712daSGarrett D'Amore free(cp);
10332a712daSGarrett D'Amore }
10432a712daSGarrett D'Amore free(rp);
10532a712daSGarrett D'Amore }
10632a712daSGarrett D'Amore
107*ffb8ebfaSGarrett D'Amore while (NULL != (sp = tbl->first_span)) {
108*ffb8ebfaSGarrett D'Amore tbl->first_span = sp->next;
10932a712daSGarrett D'Amore while (sp->first) {
11032a712daSGarrett D'Amore dp = sp->first;
11132a712daSGarrett D'Amore sp->first = dp->next;
11232a712daSGarrett D'Amore if (dp->string)
11332a712daSGarrett D'Amore free(dp->string);
11432a712daSGarrett D'Amore free(dp);
11532a712daSGarrett D'Amore }
11632a712daSGarrett D'Amore free(sp);
11732a712daSGarrett D'Amore }
11832a712daSGarrett D'Amore
119*ffb8ebfaSGarrett D'Amore while (NULL != (hp = tbl->first_head)) {
120*ffb8ebfaSGarrett D'Amore tbl->first_head = hp->next;
12132a712daSGarrett D'Amore free(hp);
12232a712daSGarrett D'Amore }
12332a712daSGarrett D'Amore
124*ffb8ebfaSGarrett D'Amore free(tbl);
12532a712daSGarrett D'Amore }
12632a712daSGarrett D'Amore
12732a712daSGarrett D'Amore void
tbl_restart(int line,int pos,struct tbl_node * tbl)12832a712daSGarrett D'Amore tbl_restart(int line, int pos, struct tbl_node *tbl)
12932a712daSGarrett D'Amore {
13032a712daSGarrett D'Amore if (TBL_PART_CDATA == tbl->part)
13132a712daSGarrett D'Amore mandoc_msg(MANDOCERR_TBLBLOCK, tbl->parse,
13232a712daSGarrett D'Amore tbl->line, tbl->pos, NULL);
13332a712daSGarrett D'Amore
13432a712daSGarrett D'Amore tbl->part = TBL_PART_LAYOUT;
13532a712daSGarrett D'Amore tbl->line = line;
13632a712daSGarrett D'Amore tbl->pos = pos;
13732a712daSGarrett D'Amore
13832a712daSGarrett D'Amore if (NULL == tbl->first_span || NULL == tbl->first_span->first)
13932a712daSGarrett D'Amore mandoc_msg(MANDOCERR_TBLNODATA, tbl->parse,
14032a712daSGarrett D'Amore tbl->line, tbl->pos, NULL);
14132a712daSGarrett D'Amore }
14232a712daSGarrett D'Amore
14332a712daSGarrett D'Amore const struct tbl_span *
tbl_span(struct tbl_node * tbl)14432a712daSGarrett D'Amore tbl_span(struct tbl_node *tbl)
14532a712daSGarrett D'Amore {
14632a712daSGarrett D'Amore struct tbl_span *span;
14732a712daSGarrett D'Amore
14832a712daSGarrett D'Amore assert(tbl);
14932a712daSGarrett D'Amore span = tbl->current_span ? tbl->current_span->next
15032a712daSGarrett D'Amore : tbl->first_span;
15132a712daSGarrett D'Amore if (span)
15232a712daSGarrett D'Amore tbl->current_span = span;
15332a712daSGarrett D'Amore return(span);
15432a712daSGarrett D'Amore }
15532a712daSGarrett D'Amore
15632a712daSGarrett D'Amore void
tbl_end(struct tbl_node ** tblp)15732a712daSGarrett D'Amore tbl_end(struct tbl_node **tblp)
15832a712daSGarrett D'Amore {
15932a712daSGarrett D'Amore struct tbl_node *tbl;
16032a712daSGarrett D'Amore
16132a712daSGarrett D'Amore tbl = *tblp;
16232a712daSGarrett D'Amore *tblp = NULL;
16332a712daSGarrett D'Amore
16432a712daSGarrett D'Amore if (NULL == tbl->first_span || NULL == tbl->first_span->first)
16532a712daSGarrett D'Amore mandoc_msg(MANDOCERR_TBLNODATA, tbl->parse,
16632a712daSGarrett D'Amore tbl->line, tbl->pos, NULL);
16732a712daSGarrett D'Amore
16832a712daSGarrett D'Amore if (tbl->last_span)
16932a712daSGarrett D'Amore tbl->last_span->flags |= TBL_SPAN_LAST;
17032a712daSGarrett D'Amore
17132a712daSGarrett D'Amore if (TBL_PART_CDATA == tbl->part)
17232a712daSGarrett D'Amore mandoc_msg(MANDOCERR_TBLBLOCK, tbl->parse,
17332a712daSGarrett D'Amore tbl->line, tbl->pos, NULL);
17432a712daSGarrett D'Amore }
17532a712daSGarrett D'Amore
176