Annotation of src/usr.bin/mandoc/tbl_data.c, Revision 1.4
1.4 ! schwarze 1: /* $Id: tbl_data.c,v 1.1 2010/12/29 16:44:23 kristaps Exp $ */
1.1 schwarze 2: /*
1.4 ! schwarze 3: * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.1 schwarze 4: *
5: * Permission to use, copy, modify, and distribute this software for any
6: * purpose with or without fee is hereby granted, provided that the above
7: * copyright notice and this permission notice appear in all copies.
8: *
9: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16: */
17: #include <assert.h>
18: #include <ctype.h>
19: #include <stdlib.h>
20: #include <string.h>
1.4 ! schwarze 21: #include <time.h>
1.1 schwarze 22:
1.4 ! schwarze 23: #include "mandoc.h"
! 24: #include "libmandoc.h"
! 25: #include "libroff.h"
! 26:
! 27: static int data(struct tbl_node *, struct tbl_span *,
! 28: int, const char *, int *);
! 29:
! 30: static int
! 31: data(struct tbl_node *tbl, struct tbl_span *dp,
! 32: int ln, const char *p, int *pos)
1.1 schwarze 33: {
1.4 ! schwarze 34: struct tbl_dat *dat;
! 35: struct tbl_cell *cp;
! 36: int sv;
! 37:
! 38: cp = NULL;
! 39: if (dp->last && dp->last->layout)
! 40: cp = dp->last->layout->next;
! 41: else if (NULL == dp->last)
! 42: cp = dp->layout->first;
! 43:
! 44: /* Skip over spanners to data formats. */
! 45:
! 46: while (cp && (TBL_CELL_VERT == cp->pos ||
! 47: TBL_CELL_DVERT == cp->pos))
! 48: cp = cp->next;
! 49:
! 50: dat = mandoc_calloc(1, sizeof(struct tbl_dat));
! 51: dat->layout = cp;
! 52: dat->pos = TBL_DATA_NONE;
! 53:
! 54: if (NULL == dat->layout)
! 55: TBL_MSG(tbl, MANDOCERR_TBLEXTRADAT, ln, *pos);
! 56:
! 57: if (dp->last) {
! 58: dp->last->next = dat;
! 59: dp->last = dat;
! 60: } else
! 61: dp->last = dp->first = dat;
! 62:
! 63: sv = *pos;
! 64: while (p[*pos] && p[*pos] != tbl->opts.tab)
! 65: (*pos)++;
! 66:
! 67: /*
! 68: * Check for a continued-data scope opening. This consists of a
! 69: * trailing `T{' at the end of the line. Subsequent lines,
! 70: * until a standalone `T}', are included in our cell.
! 71: */
1.1 schwarze 72:
1.4 ! schwarze 73: if (*pos - sv == 2 && 'T' == p[sv] && '{' == p[sv + 1]) {
! 74: tbl->part = TBL_PART_CDATA;
1.1 schwarze 75: return(0);
1.4 ! schwarze 76: }
1.1 schwarze 77:
1.4 ! schwarze 78: dat->string = mandoc_malloc(*pos - sv + 1);
! 79: memcpy(dat->string, &p[sv], *pos - sv);
! 80: dat->string[*pos - sv] = '\0';
1.1 schwarze 81:
1.4 ! schwarze 82: if (p[*pos])
! 83: (*pos)++;
1.1 schwarze 84:
85: if ( ! strcmp(dat->string, "_"))
1.4 ! schwarze 86: dat->pos = TBL_DATA_HORIZ;
1.1 schwarze 87: else if ( ! strcmp(dat->string, "="))
1.4 ! schwarze 88: dat->pos = TBL_DATA_DHORIZ;
1.1 schwarze 89: else if ( ! strcmp(dat->string, "\\_"))
1.4 ! schwarze 90: dat->pos = TBL_DATA_NHORIZ;
1.1 schwarze 91: else if ( ! strcmp(dat->string, "\\="))
1.4 ! schwarze 92: dat->pos = TBL_DATA_NDHORIZ;
1.1 schwarze 93: else
1.4 ! schwarze 94: dat->pos = TBL_DATA_DATA;
! 95:
! 96: if (NULL == dat->layout)
1.1 schwarze 97: return(1);
98:
1.4 ! schwarze 99: if (TBL_CELL_HORIZ == dat->layout->pos ||
! 100: TBL_CELL_DHORIZ == dat->layout->pos)
! 101: if (TBL_DATA_DATA == dat->pos && '\0' != *dat->string)
! 102: TBL_MSG(tbl, MANDOCERR_TBLIGNDATA, ln, sv);
! 103:
1.1 schwarze 104: return(1);
105: }
106:
1.4 ! schwarze 107: int
! 108: tbl_cdata(struct tbl_node *tbl, int ln, const char *p)
! 109: {
! 110: struct tbl_dat *dat;
! 111: size_t sz;
! 112:
! 113: if (0 == strcmp(p, "T}")) {
! 114: tbl->part = TBL_PART_DATA;
! 115: return(1);
! 116: }
! 117:
! 118: dat = tbl->last_span->last;
! 119: dat->pos = TBL_DATA_DATA;
! 120:
! 121: if (dat->string) {
! 122: sz = strlen(p) + strlen(dat->string) + 2;
! 123: dat->string = mandoc_realloc(dat->string, sz);
! 124: strlcat(dat->string, " ", sz);
! 125: strlcat(dat->string, p, sz);
! 126: } else
! 127: dat->string = mandoc_strdup(p);
! 128:
! 129: return(0);
! 130: }
1.1 schwarze 131:
132: int
1.4 ! schwarze 133: tbl_data(struct tbl_node *tbl, int ln, const char *p)
1.1 schwarze 134: {
135: struct tbl_span *dp;
1.4 ! schwarze 136: struct tbl_row *rp;
! 137: int pos;
! 138:
! 139: pos = 0;
1.1 schwarze 140:
1.4 ! schwarze 141: if ('\0' == p[pos]) {
! 142: TBL_MSG(tbl, MANDOCERR_TBL, ln, pos);
! 143: return(0);
1.1 schwarze 144: }
145:
1.4 ! schwarze 146: /*
! 147: * Choose a layout row: take the one following the last parsed
! 148: * span's. If that doesn't exist, use the last parsed span's.
! 149: * If there's no last parsed span, use the first row. This can
! 150: * be NULL!
! 151: */
! 152:
! 153: if (tbl->last_span) {
! 154: assert(tbl->last_span->layout);
! 155: rp = tbl->last_span->layout->next;
! 156: if (NULL == rp)
! 157: rp = tbl->last_span->layout;
! 158: } else
! 159: rp = tbl->first_row;
! 160:
! 161: dp = mandoc_calloc(1, sizeof(struct tbl_span));
! 162: dp->tbl = &tbl->opts;
! 163: dp->layout = rp;
! 164: dp->head = tbl->first_head;
! 165:
! 166: if (tbl->last_span) {
! 167: tbl->last_span->next = dp;
! 168: tbl->last_span = dp;
! 169: } else {
! 170: tbl->last_span = tbl->first_span = dp;
! 171: dp->flags |= TBL_SPAN_FIRST;
! 172: }
1.1 schwarze 173:
174: if ( ! strcmp(p, "_")) {
1.4 ! schwarze 175: dp->pos = TBL_SPAN_HORIZ;
1.1 schwarze 176: return(1);
177: } else if ( ! strcmp(p, "=")) {
1.4 ! schwarze 178: dp->pos = TBL_SPAN_DHORIZ;
1.1 schwarze 179: return(1);
180: }
181:
1.4 ! schwarze 182: dp->pos = TBL_SPAN_DATA;
1.1 schwarze 183:
1.4 ! schwarze 184: /* This returns 0 when TBL_PART_CDATA is entered. */
1.1 schwarze 185:
1.4 ! schwarze 186: while ('\0' != p[pos])
! 187: if ( ! data(tbl, dp, ln, p, &pos))
! 188: return(0);
1.1 schwarze 189:
190: return(1);
191: }