Annotation of mandoc/tbl_data.c, Revision 1.42
1.42 ! schwarze 1: /* $Id: tbl_data.c,v 1.41 2015/10/06 18:32:20 schwarze Exp $ */
1.1 kristaps 2: /*
1.20 schwarze 3: * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.42 ! schwarze 4: * Copyright (c) 2011, 2015, 2017 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.11 kristaps 18: #include "config.h"
1.32 schwarze 19:
20: #include <sys/types.h>
1.11 kristaps 21:
1.1 kristaps 22: #include <assert.h>
23: #include <ctype.h>
24: #include <stdlib.h>
25: #include <string.h>
1.6 kristaps 26: #include <time.h>
1.1 kristaps 27:
28: #include "mandoc.h"
1.29 schwarze 29: #include "mandoc_aux.h"
1.1 kristaps 30: #include "libmandoc.h"
31: #include "libroff.h"
32:
1.33 schwarze 33: static void getdata(struct tbl_node *, struct tbl_span *,
1.22 kristaps 34: int, const char *, int *);
1.30 schwarze 35: static struct tbl_span *newspan(struct tbl_node *, int,
1.22 kristaps 36: struct tbl_row *);
1.1 kristaps 37:
1.30 schwarze 38:
1.33 schwarze 39: static void
1.30 schwarze 40: getdata(struct tbl_node *tbl, struct tbl_span *dp,
1.1 kristaps 41: int ln, const char *p, int *pos)
42: {
43: struct tbl_dat *dat;
1.3 kristaps 44: struct tbl_cell *cp;
1.37 schwarze 45: int sv;
1.1 kristaps 46:
1.38 schwarze 47: /* Advance to the next layout cell, skipping spanners. */
48:
1.37 schwarze 49: cp = dp->last == NULL ? dp->layout->first : dp->last->layout->next;
50: while (cp != NULL && cp->pos == TBL_CELL_SPAN)
1.3 kristaps 51: cp = cp->next;
52:
1.16 kristaps 53: /*
54: * Stop processing when we reach the end of the available layout
55: * cells. This means that we have extra input.
56: */
57:
1.37 schwarze 58: if (cp == NULL) {
1.36 schwarze 59: mandoc_msg(MANDOCERR_TBLDATA_EXTRA, tbl->parse,
60: ln, *pos, p + *pos);
1.16 kristaps 61: /* Skip to the end... */
62: while (p[*pos])
63: (*pos)++;
1.33 schwarze 64: return;
1.16 kristaps 65: }
66:
1.37 schwarze 67: dat = mandoc_calloc(1, sizeof(*dat));
1.3 kristaps 68: dat->layout = cp;
1.11 kristaps 69: dat->pos = TBL_DATA_NONE;
1.37 schwarze 70: dat->spans = 0;
71: for (cp = cp->next; cp != NULL; cp = cp->next)
72: if (cp->pos == TBL_CELL_SPAN)
73: dat->spans++;
1.16 kristaps 74: else
75: break;
1.30 schwarze 76:
1.37 schwarze 77: if (dp->last == NULL)
78: dp->first = dat;
79: else
1.1 kristaps 80: dp->last->next = dat;
1.37 schwarze 81: dp->last = dat;
1.1 kristaps 82:
83: sv = *pos;
1.8 kristaps 84: while (p[*pos] && p[*pos] != tbl->opts.tab)
1.1 kristaps 85: (*pos)++;
86:
1.11 kristaps 87: /*
88: * Check for a continued-data scope opening. This consists of a
89: * trailing `T{' at the end of the line. Subsequent lines,
90: * until a standalone `T}', are included in our cell.
91: */
92:
1.37 schwarze 93: if (*pos - sv == 2 && p[sv] == 'T' && p[sv + 1] == '{') {
1.11 kristaps 94: tbl->part = TBL_PART_CDATA;
1.33 schwarze 95: return;
1.11 kristaps 96: }
97:
1.37 schwarze 98: dat->string = mandoc_strndup(p + sv, *pos - sv);
1.1 kristaps 99:
100: if (p[*pos])
101: (*pos)++;
102:
103: if ( ! strcmp(dat->string, "_"))
1.5 kristaps 104: dat->pos = TBL_DATA_HORIZ;
1.1 kristaps 105: else if ( ! strcmp(dat->string, "="))
1.5 kristaps 106: dat->pos = TBL_DATA_DHORIZ;
1.1 kristaps 107: else if ( ! strcmp(dat->string, "\\_"))
1.5 kristaps 108: dat->pos = TBL_DATA_NHORIZ;
1.1 kristaps 109: else if ( ! strcmp(dat->string, "\\="))
1.5 kristaps 110: dat->pos = TBL_DATA_NDHORIZ;
111: else
112: dat->pos = TBL_DATA_DATA;
1.11 kristaps 113:
1.37 schwarze 114: if ((dat->layout->pos == TBL_CELL_HORIZ ||
115: dat->layout->pos == TBL_CELL_DHORIZ ||
116: dat->layout->pos == TBL_CELL_DOWN) &&
117: dat->pos == TBL_DATA_DATA && *dat->string != '\0')
118: mandoc_msg(MANDOCERR_TBLDATA_SPAN,
119: tbl->parse, ln, sv, dat->string);
1.11 kristaps 120: }
121:
122: int
1.35 schwarze 123: tbl_cdata(struct tbl_node *tbl, int ln, const char *p, int pos)
1.11 kristaps 124: {
125: struct tbl_dat *dat;
1.30 schwarze 126: size_t sz;
1.11 kristaps 127:
128: dat = tbl->last_span->last;
1.14 kristaps 129:
130: if (p[pos] == 'T' && p[pos + 1] == '}') {
131: pos += 2;
132: if (p[pos] == tbl->opts.tab) {
133: tbl->part = TBL_PART_DATA;
134: pos++;
1.40 schwarze 135: while (p[pos] != '\0')
136: getdata(tbl, tbl->last_span, ln, p, &pos);
1.41 schwarze 137: return 1;
1.37 schwarze 138: } else if (p[pos] == '\0') {
1.14 kristaps 139: tbl->part = TBL_PART_DATA;
1.41 schwarze 140: return 1;
1.14 kristaps 141: }
142:
143: /* Fallthrough: T} is part of a word. */
144: }
1.17 kristaps 145:
146: dat->pos = TBL_DATA_DATA;
1.42 ! schwarze 147: dat->block = 1;
1.11 kristaps 148:
1.37 schwarze 149: if (dat->string != NULL) {
1.36 schwarze 150: sz = strlen(p + pos) + strlen(dat->string) + 2;
1.11 kristaps 151: dat->string = mandoc_realloc(dat->string, sz);
1.31 schwarze 152: (void)strlcat(dat->string, " ", sz);
1.36 schwarze 153: (void)strlcat(dat->string, p + pos, sz);
1.11 kristaps 154: } else
1.36 schwarze 155: dat->string = mandoc_strdup(p + pos);
1.19 kristaps 156:
1.37 schwarze 157: if (dat->layout->pos == TBL_CELL_DOWN)
1.36 schwarze 158: mandoc_msg(MANDOCERR_TBLDATA_SPAN, tbl->parse,
159: ln, pos, dat->string);
1.11 kristaps 160:
1.41 schwarze 161: return 0;
1.1 kristaps 162: }
163:
1.20 schwarze 164: static struct tbl_span *
1.22 kristaps 165: newspan(struct tbl_node *tbl, int line, struct tbl_row *rp)
1.20 schwarze 166: {
167: struct tbl_span *dp;
168:
1.37 schwarze 169: dp = mandoc_calloc(1, sizeof(*dp));
1.22 kristaps 170: dp->line = line;
1.26 schwarze 171: dp->opts = &tbl->opts;
1.20 schwarze 172: dp->layout = rp;
1.34 schwarze 173: dp->prev = tbl->last_span;
1.20 schwarze 174:
1.34 schwarze 175: if (dp->prev == NULL) {
176: tbl->first_span = dp;
1.21 schwarze 177: tbl->current_span = NULL;
1.34 schwarze 178: } else
179: dp->prev->next = dp;
180: tbl->last_span = dp;
1.20 schwarze 181:
1.41 schwarze 182: return dp;
1.20 schwarze 183: }
184:
1.33 schwarze 185: void
1.35 schwarze 186: tbl_data(struct tbl_node *tbl, int ln, const char *p, int pos)
1.1 kristaps 187: {
188: struct tbl_span *dp;
1.3 kristaps 189: struct tbl_row *rp;
1.1 kristaps 190:
1.30 schwarze 191: /*
1.3 kristaps 192: * Choose a layout row: take the one following the last parsed
193: * span's. If that doesn't exist, use the last parsed span's.
1.15 kristaps 194: * If there's no last parsed span, use the first row. Lastly,
195: * if the last span was a horizontal line, use the same layout
196: * (it doesn't "consume" the layout).
1.3 kristaps 197: */
198:
1.37 schwarze 199: if (tbl->last_span != NULL) {
1.20 schwarze 200: if (tbl->last_span->pos == TBL_SPAN_DATA) {
201: for (rp = tbl->last_span->layout->next;
1.37 schwarze 202: rp != NULL && rp->first != NULL;
203: rp = rp->next) {
1.20 schwarze 204: switch (rp->first->pos) {
1.30 schwarze 205: case TBL_CELL_HORIZ:
1.22 kristaps 206: dp = newspan(tbl, ln, rp);
1.20 schwarze 207: dp->pos = TBL_SPAN_HORIZ;
208: continue;
1.30 schwarze 209: case TBL_CELL_DHORIZ:
1.22 kristaps 210: dp = newspan(tbl, ln, rp);
1.20 schwarze 211: dp->pos = TBL_SPAN_DHORIZ;
212: continue;
213: default:
214: break;
215: }
216: break;
217: }
218: } else
1.15 kristaps 219: rp = tbl->last_span->layout;
1.18 kristaps 220:
1.37 schwarze 221: if (rp == NULL)
1.3 kristaps 222: rp = tbl->last_span->layout;
223: } else
224: rp = tbl->first_row;
1.18 kristaps 225:
226: assert(rp);
1.3 kristaps 227:
1.22 kristaps 228: dp = newspan(tbl, ln, rp);
1.2 kristaps 229:
1.1 kristaps 230: if ( ! strcmp(p, "_")) {
1.5 kristaps 231: dp->pos = TBL_SPAN_HORIZ;
1.33 schwarze 232: return;
1.1 kristaps 233: } else if ( ! strcmp(p, "=")) {
1.5 kristaps 234: dp->pos = TBL_SPAN_DHORIZ;
1.33 schwarze 235: return;
1.1 kristaps 236: }
1.5 kristaps 237:
238: dp->pos = TBL_SPAN_DATA;
1.1 kristaps 239:
1.37 schwarze 240: while (p[pos] != '\0')
1.33 schwarze 241: getdata(tbl, dp, ln, p, &pos);
1.1 kristaps 242: }
CVSweb