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