Annotation of mandoc/tbl.c, Revision 1.45
1.45 ! schwarze 1: /* $Id: tbl.c,v 1.44 2018/12/13 02:06:07 schwarze Exp $ */
1.1 kristaps 2: /*
1.22 schwarze 3: * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.33 schwarze 4: * Copyright (c) 2011, 2015 Ingo Schwarze <schwarze@openbsd.org>
1.1 kristaps 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.25 kristaps 18: #include "config.h"
1.30 schwarze 19:
20: #include <sys/types.h>
1.25 kristaps 21:
1.1 kristaps 22: #include <assert.h>
1.3 kristaps 23: #include <stdio.h>
1.1 kristaps 24: #include <stdlib.h>
25: #include <string.h>
26: #include <time.h>
27:
1.43 schwarze 28: #include "mandoc_aux.h"
1.1 kristaps 29: #include "mandoc.h"
1.43 schwarze 30: #include "tbl.h"
1.1 kristaps 31: #include "libmandoc.h"
1.44 schwarze 32: #include "tbl_parse.h"
33: #include "tbl_int.h"
1.1 kristaps 34:
1.29 schwarze 35:
1.42 schwarze 36: void
1.35 schwarze 37: tbl_read(struct tbl_node *tbl, int ln, const char *p, int pos)
1.1 kristaps 38: {
39: const char *cp;
1.33 schwarze 40: int active;
1.1 kristaps 41:
1.5 kristaps 42: /*
1.33 schwarze 43: * In the options section, proceed to the layout section
44: * after a semicolon, or right away if there is no semicolon.
45: * Ignore semicolons in arguments.
1.5 kristaps 46: */
47:
1.33 schwarze 48: if (tbl->part == TBL_PART_OPTS) {
49: tbl->part = TBL_PART_LAYOUT;
50: active = 1;
1.35 schwarze 51: for (cp = p + pos; *cp != '\0'; cp++) {
1.33 schwarze 52: switch (*cp) {
53: case '(':
54: active = 0;
55: continue;
56: case ')':
57: active = 1;
58: continue;
59: case ';':
60: if (active)
61: break;
62: continue;
63: default:
64: continue;
65: }
66: break;
67: }
68: if (*cp == ';') {
1.35 schwarze 69: tbl_option(tbl, ln, p, &pos);
70: if (p[pos] == '\0')
1.42 schwarze 71: return;
1.33 schwarze 72: }
73: }
1.5 kristaps 74:
1.33 schwarze 75: /* Process the other section types. */
1.5 kristaps 76:
77: switch (tbl->part) {
1.29 schwarze 78: case TBL_PART_LAYOUT:
1.35 schwarze 79: tbl_layout(tbl, ln, p, pos);
1.42 schwarze 80: break;
1.29 schwarze 81: case TBL_PART_CDATA:
1.42 schwarze 82: tbl_cdata(tbl, ln, p, pos);
83: break;
1.21 kristaps 84: default:
1.42 schwarze 85: tbl_data(tbl, ln, p, pos);
1.5 kristaps 86: break;
1.8 kristaps 87: }
1.1 kristaps 88: }
89:
1.17 kristaps 90: struct tbl_node *
1.44 schwarze 91: tbl_alloc(int pos, int line, struct mparse *parse, struct tbl_node *last_tbl)
1.1 kristaps 92: {
1.27 schwarze 93: struct tbl_node *tbl;
1.1 kristaps 94:
1.37 schwarze 95: tbl = mandoc_calloc(1, sizeof(*tbl));
1.44 schwarze 96: if (last_tbl != NULL)
97: last_tbl->next = tbl;
1.27 schwarze 98: tbl->line = line;
99: tbl->pos = pos;
100: tbl->parse = parse;
101: tbl->part = TBL_PART_OPTS;
102: tbl->opts.tab = '\t';
103: tbl->opts.decimal = '.';
1.40 schwarze 104: return tbl;
1.1 kristaps 105: }
106:
107: void
1.27 schwarze 108: tbl_free(struct tbl_node *tbl)
1.1 kristaps 109: {
1.44 schwarze 110: struct tbl_node *old_tbl;
1.10 kristaps 111: struct tbl_row *rp;
112: struct tbl_cell *cp;
113: struct tbl_span *sp;
114: struct tbl_dat *dp;
1.1 kristaps 115:
1.44 schwarze 116: while (tbl != NULL) {
117: while ((rp = tbl->first_row) != NULL) {
118: tbl->first_row = rp->next;
119: while (rp->first != NULL) {
120: cp = rp->first;
121: rp->first = cp->next;
122: free(cp->wstr);
123: free(cp);
124: }
125: free(rp);
1.10 kristaps 126: }
1.44 schwarze 127: while ((sp = tbl->first_span) != NULL) {
128: tbl->first_span = sp->next;
129: while (sp->first != NULL) {
130: dp = sp->first;
131: sp->first = dp->next;
132: free(dp->string);
133: free(dp);
134: }
135: free(sp);
1.10 kristaps 136: }
1.44 schwarze 137: old_tbl = tbl;
138: tbl = tbl->next;
139: free(old_tbl);
1.10 kristaps 140: }
1.7 kristaps 141: }
142:
143: void
1.17 kristaps 144: tbl_restart(int line, int pos, struct tbl_node *tbl)
1.7 kristaps 145: {
1.36 schwarze 146: if (tbl->part == TBL_PART_CDATA)
1.45 ! schwarze 147: mandoc_msg(MANDOCERR_TBLDATA_BLK, line, pos, "T&");
1.7 kristaps 148:
149: tbl->part = TBL_PART_LAYOUT;
1.14 kristaps 150: tbl->line = line;
151: tbl->pos = pos;
1.1 kristaps 152: }
1.3 kristaps 153:
1.44 schwarze 154: struct tbl_span *
1.22 schwarze 155: tbl_span(struct tbl_node *tbl)
1.11 kristaps 156: {
1.22 schwarze 157: struct tbl_span *span;
1.11 kristaps 158:
1.22 schwarze 159: span = tbl->current_span ? tbl->current_span->next
160: : tbl->first_span;
1.44 schwarze 161: if (span != NULL)
1.22 schwarze 162: tbl->current_span = span;
1.40 schwarze 163: return span;
1.13 kristaps 164: }
165:
1.36 schwarze 166: int
1.44 schwarze 167: tbl_end(struct tbl_node *tbl, int still_open)
1.13 kristaps 168: {
1.34 schwarze 169: struct tbl_span *sp;
1.13 kristaps 170:
1.44 schwarze 171: if (still_open)
1.45 ! schwarze 172: mandoc_msg(MANDOCERR_BLK_NOEND, tbl->line, tbl->pos, "TS");
1.44 schwarze 173: else if (tbl->part == TBL_PART_CDATA)
1.45 ! schwarze 174: mandoc_msg(MANDOCERR_TBLDATA_BLK, tbl->line, tbl->pos, "TE");
1.36 schwarze 175:
1.34 schwarze 176: sp = tbl->first_span;
177: while (sp != NULL && sp->first == NULL)
178: sp = sp->next;
1.36 schwarze 179: if (sp == NULL) {
1.45 ! schwarze 180: mandoc_msg(MANDOCERR_TBLDATA_NONE, tbl->line, tbl->pos, NULL);
1.40 schwarze 181: return 0;
1.36 schwarze 182: }
1.40 schwarze 183: return 1;
1.16 kristaps 184: }
CVSweb