Home | History | Annotate | Line # | Download | only in dist
tbl.c revision 1.1.1.1.2.1
      1 /*	$Vendor-Id: tbl.c,v 1.21 2011/01/04 15:02:00 kristaps Exp $ */
      2 /*
      3  * Copyright (c) 2009, 2010 Kristaps Dzonsons <kristaps (at) bsd.lv>
      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 #ifdef HAVE_CONFIG_H
     18 #include "config.h"
     19 #endif
     20 
     21 #include <assert.h>
     22 #include <stdio.h>
     23 #include <stdlib.h>
     24 #include <string.h>
     25 #include <time.h>
     26 
     27 #include "mandoc.h"
     28 #include "roff.h"
     29 #include "libmandoc.h"
     30 #include "libroff.h"
     31 
     32 enum rofferr
     33 tbl_read(struct tbl_node *tbl, int ln, const char *p, int offs)
     34 {
     35 	int		 len;
     36 	const char	*cp;
     37 
     38 	cp = &p[offs];
     39 	len = (int)strlen(cp);
     40 
     41 	/*
     42 	 * If we're in the options section and we don't have a
     43 	 * terminating semicolon, assume we've moved directly into the
     44 	 * layout section.  No need to report a warning: this is,
     45 	 * apparently, standard behaviour.
     46 	 */
     47 
     48 	if (TBL_PART_OPTS == tbl->part && len)
     49 		if (';' != cp[len - 1])
     50 			tbl->part = TBL_PART_LAYOUT;
     51 
     52 	/* Now process each logical section of the table.  */
     53 
     54 	switch (tbl->part) {
     55 	case (TBL_PART_OPTS):
     56 		return(tbl_option(tbl, ln, p) ? ROFF_IGN : ROFF_ERR);
     57 	case (TBL_PART_LAYOUT):
     58 		return(tbl_layout(tbl, ln, p) ? ROFF_IGN : ROFF_ERR);
     59 	case (TBL_PART_CDATA):
     60 		return(tbl_cdata(tbl, ln, p) ? ROFF_TBL : ROFF_IGN);
     61 	default:
     62 		break;
     63 	}
     64 
     65 	/*
     66 	 * This only returns zero if the line is empty, so we ignore it
     67 	 * and continue on.
     68 	 */
     69 	return(tbl_data(tbl, ln, p) ? ROFF_TBL : ROFF_IGN);
     70 }
     71 
     72 struct tbl_node *
     73 tbl_alloc(int pos, int line, void *data, const mandocmsg msg)
     74 {
     75 	struct tbl_node	*p;
     76 
     77 	p = mandoc_calloc(1, sizeof(struct tbl_node));
     78 	p->line = line;
     79 	p->pos = pos;
     80 	p->data = data;
     81 	p->msg = msg;
     82 	p->part = TBL_PART_OPTS;
     83 	p->opts.tab = '\t';
     84 	p->opts.linesize = 12;
     85 	p->opts.decimal = '.';
     86 	return(p);
     87 }
     88 
     89 void
     90 tbl_free(struct tbl_node *p)
     91 {
     92 	struct tbl_row	*rp;
     93 	struct tbl_cell	*cp;
     94 	struct tbl_span	*sp;
     95 	struct tbl_dat	*dp;
     96 	struct tbl_head	*hp;
     97 
     98 	while (NULL != (rp = p->first_row)) {
     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 	}
    107 
    108 	while (NULL != (sp = p->first_span)) {
    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);
    118 	}
    119 
    120 	while (NULL != (hp = p->first_head)) {
    121 		p->first_head = hp->next;
    122 		free(hp);
    123 	}
    124 
    125 	free(p);
    126 }
    127 
    128 void
    129 tbl_restart(int line, int pos, struct tbl_node *tbl)
    130 {
    131 	if (TBL_PART_CDATA == tbl->part)
    132 		TBL_MSG(tbl, MANDOCERR_TBLBLOCK, tbl->line, tbl->pos);
    133 
    134 	tbl->part = TBL_PART_LAYOUT;
    135 	tbl->line = line;
    136 	tbl->pos = pos;
    137 
    138 	if (NULL == tbl->first_span || NULL == tbl->first_span->first)
    139 		TBL_MSG(tbl, MANDOCERR_TBLNODATA, tbl->line, tbl->pos);
    140 }
    141 
    142 const struct tbl_span *
    143 tbl_span(const struct tbl_node *tbl)
    144 {
    145 
    146 	assert(tbl);
    147 	return(tbl->last_span);
    148 }
    149 
    150 void
    151 tbl_end(struct tbl_node *tbl)
    152 {
    153 
    154 	if (NULL == tbl->first_span || NULL == tbl->first_span->first)
    155 		TBL_MSG(tbl, MANDOCERR_TBLNODATA, tbl->line, tbl->pos);
    156 
    157 	if (tbl->last_span)
    158 		tbl->last_span->flags |= TBL_SPAN_LAST;
    159 
    160 	if (TBL_PART_CDATA == tbl->part)
    161 		TBL_MSG(tbl, MANDOCERR_TBLBLOCK, tbl->line, tbl->pos);
    162 }
    163 
    164