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