Home | History | Annotate | Line # | Download | only in dist
tbl.c revision 1.7.12.1
      1  1.7.12.1  pgoyette /*	Id: tbl.c,v 1.42 2017/07/08 17:52:50 schwarze Exp  */
      2       1.1     joerg /*
      3       1.3     joerg  * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps (at) bsd.lv>
      4       1.6  christos  * Copyright (c) 2011, 2015 Ingo Schwarze <schwarze (at) openbsd.org>
      5       1.1     joerg  *
      6       1.1     joerg  * Permission to use, copy, modify, and distribute this software for any
      7       1.1     joerg  * purpose with or without fee is hereby granted, provided that the above
      8       1.1     joerg  * copyright notice and this permission notice appear in all copies.
      9       1.1     joerg  *
     10       1.1     joerg  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     11       1.1     joerg  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     12       1.1     joerg  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     13       1.1     joerg  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     14       1.1     joerg  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     15       1.1     joerg  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     16       1.1     joerg  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     17       1.1     joerg  */
     18       1.2  nakayama #include "config.h"
     19       1.6  christos 
     20       1.6  christos #include <sys/types.h>
     21       1.2  nakayama 
     22       1.1     joerg #include <assert.h>
     23       1.1     joerg #include <stdio.h>
     24       1.1     joerg #include <stdlib.h>
     25       1.1     joerg #include <string.h>
     26       1.1     joerg #include <time.h>
     27       1.1     joerg 
     28       1.1     joerg #include "mandoc.h"
     29       1.6  christos #include "mandoc_aux.h"
     30       1.1     joerg #include "libmandoc.h"
     31       1.1     joerg #include "libroff.h"
     32       1.1     joerg 
     33       1.6  christos 
     34  1.7.12.1  pgoyette void
     35       1.6  christos tbl_read(struct tbl_node *tbl, int ln, const char *p, int pos)
     36       1.1     joerg {
     37       1.1     joerg 	const char	*cp;
     38       1.6  christos 	int		 active;
     39       1.1     joerg 
     40       1.1     joerg 	/*
     41       1.6  christos 	 * In the options section, proceed to the layout section
     42       1.6  christos 	 * after a semicolon, or right away if there is no semicolon.
     43       1.6  christos 	 * Ignore semicolons in arguments.
     44       1.1     joerg 	 */
     45       1.1     joerg 
     46       1.6  christos 	if (tbl->part == TBL_PART_OPTS) {
     47       1.6  christos 		tbl->part = TBL_PART_LAYOUT;
     48       1.6  christos 		active = 1;
     49       1.6  christos 		for (cp = p + pos; *cp != '\0'; cp++) {
     50       1.6  christos 			switch (*cp) {
     51       1.6  christos 			case '(':
     52       1.6  christos 				active = 0;
     53       1.6  christos 				continue;
     54       1.6  christos 			case ')':
     55       1.6  christos 				active = 1;
     56       1.6  christos 				continue;
     57       1.6  christos 			case ';':
     58       1.6  christos 				if (active)
     59       1.6  christos 					break;
     60       1.6  christos 				continue;
     61       1.6  christos 			default:
     62       1.6  christos 				continue;
     63       1.6  christos 			}
     64       1.6  christos 			break;
     65       1.6  christos 		}
     66       1.6  christos 		if (*cp == ';') {
     67       1.6  christos 			tbl_option(tbl, ln, p, &pos);
     68       1.6  christos 			if (p[pos] == '\0')
     69  1.7.12.1  pgoyette 				return;
     70       1.6  christos 		}
     71       1.6  christos 	}
     72       1.1     joerg 
     73       1.6  christos 	/* Process the other section types.  */
     74       1.1     joerg 
     75       1.1     joerg 	switch (tbl->part) {
     76       1.6  christos 	case TBL_PART_LAYOUT:
     77       1.6  christos 		tbl_layout(tbl, ln, p, pos);
     78  1.7.12.1  pgoyette 		break;
     79       1.6  christos 	case TBL_PART_CDATA:
     80  1.7.12.1  pgoyette 		tbl_cdata(tbl, ln, p, pos);
     81  1.7.12.1  pgoyette 		break;
     82       1.1     joerg 	default:
     83  1.7.12.1  pgoyette 		tbl_data(tbl, ln, p, pos);
     84       1.1     joerg 		break;
     85       1.1     joerg 	}
     86       1.1     joerg }
     87       1.1     joerg 
     88       1.1     joerg struct tbl_node *
     89       1.3     joerg tbl_alloc(int pos, int line, struct mparse *parse)
     90       1.1     joerg {
     91       1.5     joerg 	struct tbl_node	*tbl;
     92       1.1     joerg 
     93       1.6  christos 	tbl = mandoc_calloc(1, sizeof(*tbl));
     94       1.5     joerg 	tbl->line = line;
     95       1.5     joerg 	tbl->pos = pos;
     96       1.5     joerg 	tbl->parse = parse;
     97       1.5     joerg 	tbl->part = TBL_PART_OPTS;
     98       1.5     joerg 	tbl->opts.tab = '\t';
     99       1.5     joerg 	tbl->opts.decimal = '.';
    100       1.7  christos 	return tbl;
    101       1.1     joerg }
    102       1.1     joerg 
    103       1.1     joerg void
    104       1.5     joerg tbl_free(struct tbl_node *tbl)
    105       1.1     joerg {
    106       1.1     joerg 	struct tbl_row	*rp;
    107       1.1     joerg 	struct tbl_cell	*cp;
    108       1.1     joerg 	struct tbl_span	*sp;
    109       1.1     joerg 	struct tbl_dat	*dp;
    110       1.1     joerg 
    111       1.6  christos 	while ((rp = tbl->first_row) != NULL) {
    112       1.5     joerg 		tbl->first_row = rp->next;
    113       1.6  christos 		while (rp->first != NULL) {
    114       1.1     joerg 			cp = rp->first;
    115       1.1     joerg 			rp->first = cp->next;
    116  1.7.12.1  pgoyette 			free(cp->wstr);
    117       1.1     joerg 			free(cp);
    118       1.1     joerg 		}
    119       1.1     joerg 		free(rp);
    120       1.1     joerg 	}
    121       1.1     joerg 
    122       1.6  christos 	while ((sp = tbl->first_span) != NULL) {
    123       1.5     joerg 		tbl->first_span = sp->next;
    124       1.6  christos 		while (sp->first != NULL) {
    125       1.1     joerg 			dp = sp->first;
    126       1.1     joerg 			sp->first = dp->next;
    127       1.6  christos 			free(dp->string);
    128       1.1     joerg 			free(dp);
    129       1.1     joerg 		}
    130       1.1     joerg 		free(sp);
    131       1.1     joerg 	}
    132       1.1     joerg 
    133       1.5     joerg 	free(tbl);
    134       1.1     joerg }
    135       1.1     joerg 
    136       1.1     joerg void
    137       1.1     joerg tbl_restart(int line, int pos, struct tbl_node *tbl)
    138       1.1     joerg {
    139       1.6  christos 	if (tbl->part == TBL_PART_CDATA)
    140       1.6  christos 		mandoc_msg(MANDOCERR_TBLDATA_BLK, tbl->parse,
    141       1.6  christos 		    line, pos, "T&");
    142       1.1     joerg 
    143       1.1     joerg 	tbl->part = TBL_PART_LAYOUT;
    144       1.1     joerg 	tbl->line = line;
    145       1.1     joerg 	tbl->pos = pos;
    146       1.1     joerg }
    147       1.1     joerg 
    148       1.1     joerg const struct tbl_span *
    149       1.3     joerg tbl_span(struct tbl_node *tbl)
    150       1.1     joerg {
    151       1.3     joerg 	struct tbl_span	 *span;
    152       1.1     joerg 
    153       1.1     joerg 	assert(tbl);
    154       1.3     joerg 	span = tbl->current_span ? tbl->current_span->next
    155       1.3     joerg 				 : tbl->first_span;
    156       1.3     joerg 	if (span)
    157       1.3     joerg 		tbl->current_span = span;
    158       1.7  christos 	return span;
    159       1.1     joerg }
    160       1.1     joerg 
    161       1.6  christos int
    162  1.7.12.1  pgoyette tbl_end(struct tbl_node *tbl)
    163       1.1     joerg {
    164       1.6  christos 	struct tbl_span *sp;
    165       1.4     joerg 
    166       1.6  christos 	if (tbl->part == TBL_PART_CDATA)
    167       1.6  christos 		mandoc_msg(MANDOCERR_TBLDATA_BLK, tbl->parse,
    168       1.6  christos 		    tbl->line, tbl->pos, "TE");
    169       1.6  christos 
    170       1.6  christos 	sp = tbl->first_span;
    171       1.6  christos 	while (sp != NULL && sp->first == NULL)
    172       1.6  christos 		sp = sp->next;
    173       1.6  christos 	if (sp == NULL) {
    174       1.6  christos 		mandoc_msg(MANDOCERR_TBLDATA_NONE, tbl->parse,
    175       1.6  christos 		    tbl->line, tbl->pos, NULL);
    176       1.7  christos 		return 0;
    177       1.6  christos 	}
    178       1.7  christos 	return 1;
    179       1.1     joerg }
    180