2016-01-15 23:08:59 +00:00
|
|
|
/* $Id: tbl_data.c,v 1.41 2015/10/06 18:32:20 schwarze Exp $ */
|
2012-10-18 09:55:16 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
|
2015-03-02 16:45:41 +00:00
|
|
|
* Copyright (c) 2011, 2015 Ingo Schwarze <schwarze@openbsd.org>
|
2012-10-18 09:55:16 +00:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
#include "config.h"
|
2014-12-02 07:34:06 +00:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
2012-10-18 09:55:16 +00:00
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
#include "mandoc.h"
|
2014-11-22 18:08:25 +00:00
|
|
|
#include "mandoc_aux.h"
|
2012-10-18 09:55:16 +00:00
|
|
|
#include "libmandoc.h"
|
|
|
|
#include "libroff.h"
|
|
|
|
|
2015-03-02 16:45:41 +00:00
|
|
|
static void getdata(struct tbl_node *, struct tbl_span *,
|
2012-10-18 09:55:16 +00:00
|
|
|
int, const char *, int *);
|
2014-11-22 18:08:25 +00:00
|
|
|
static struct tbl_span *newspan(struct tbl_node *, int,
|
2012-10-18 09:55:16 +00:00
|
|
|
struct tbl_row *);
|
|
|
|
|
2014-11-22 18:08:25 +00:00
|
|
|
|
2015-03-02 16:45:41 +00:00
|
|
|
static void
|
2014-11-22 18:08:25 +00:00
|
|
|
getdata(struct tbl_node *tbl, struct tbl_span *dp,
|
2012-10-18 09:55:16 +00:00
|
|
|
int ln, const char *p, int *pos)
|
|
|
|
{
|
|
|
|
struct tbl_dat *dat;
|
|
|
|
struct tbl_cell *cp;
|
2015-03-02 16:45:41 +00:00
|
|
|
int sv;
|
2012-10-18 09:55:16 +00:00
|
|
|
|
2015-03-02 16:45:41 +00:00
|
|
|
/* Advance to the next layout cell, skipping spanners. */
|
2012-10-18 09:55:16 +00:00
|
|
|
|
2015-03-02 16:45:41 +00:00
|
|
|
cp = dp->last == NULL ? dp->layout->first : dp->last->layout->next;
|
|
|
|
while (cp != NULL && cp->pos == TBL_CELL_SPAN)
|
2012-10-18 09:55:16 +00:00
|
|
|
cp = cp->next;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Stop processing when we reach the end of the available layout
|
|
|
|
* cells. This means that we have extra input.
|
|
|
|
*/
|
|
|
|
|
2015-03-02 16:45:41 +00:00
|
|
|
if (cp == NULL) {
|
|
|
|
mandoc_msg(MANDOCERR_TBLDATA_EXTRA, tbl->parse,
|
|
|
|
ln, *pos, p + *pos);
|
2012-10-18 09:55:16 +00:00
|
|
|
/* Skip to the end... */
|
|
|
|
while (p[*pos])
|
|
|
|
(*pos)++;
|
2015-03-02 16:45:41 +00:00
|
|
|
return;
|
2012-10-18 09:55:16 +00:00
|
|
|
}
|
|
|
|
|
2015-03-02 16:45:41 +00:00
|
|
|
dat = mandoc_calloc(1, sizeof(*dat));
|
2012-10-18 09:55:16 +00:00
|
|
|
dat->layout = cp;
|
|
|
|
dat->pos = TBL_DATA_NONE;
|
2015-03-02 16:45:41 +00:00
|
|
|
dat->spans = 0;
|
|
|
|
for (cp = cp->next; cp != NULL; cp = cp->next)
|
|
|
|
if (cp->pos == TBL_CELL_SPAN)
|
|
|
|
dat->spans++;
|
2012-10-18 09:55:16 +00:00
|
|
|
else
|
|
|
|
break;
|
2014-11-22 18:08:25 +00:00
|
|
|
|
2015-03-02 16:45:41 +00:00
|
|
|
if (dp->last == NULL)
|
|
|
|
dp->first = dat;
|
|
|
|
else
|
2012-10-18 09:55:16 +00:00
|
|
|
dp->last->next = dat;
|
2015-03-02 16:45:41 +00:00
|
|
|
dp->last = dat;
|
2012-10-18 09:55:16 +00:00
|
|
|
|
|
|
|
sv = *pos;
|
|
|
|
while (p[*pos] && p[*pos] != tbl->opts.tab)
|
|
|
|
(*pos)++;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check for a continued-data scope opening. This consists of a
|
|
|
|
* trailing `T{' at the end of the line. Subsequent lines,
|
|
|
|
* until a standalone `T}', are included in our cell.
|
|
|
|
*/
|
|
|
|
|
2015-03-02 16:45:41 +00:00
|
|
|
if (*pos - sv == 2 && p[sv] == 'T' && p[sv + 1] == '{') {
|
2012-10-18 09:55:16 +00:00
|
|
|
tbl->part = TBL_PART_CDATA;
|
2015-03-02 16:45:41 +00:00
|
|
|
return;
|
2012-10-18 09:55:16 +00:00
|
|
|
}
|
|
|
|
|
2015-03-02 16:45:41 +00:00
|
|
|
dat->string = mandoc_strndup(p + sv, *pos - sv);
|
2012-10-18 09:55:16 +00:00
|
|
|
|
|
|
|
if (p[*pos])
|
|
|
|
(*pos)++;
|
|
|
|
|
|
|
|
if ( ! strcmp(dat->string, "_"))
|
|
|
|
dat->pos = TBL_DATA_HORIZ;
|
|
|
|
else if ( ! strcmp(dat->string, "="))
|
|
|
|
dat->pos = TBL_DATA_DHORIZ;
|
|
|
|
else if ( ! strcmp(dat->string, "\\_"))
|
|
|
|
dat->pos = TBL_DATA_NHORIZ;
|
|
|
|
else if ( ! strcmp(dat->string, "\\="))
|
|
|
|
dat->pos = TBL_DATA_NDHORIZ;
|
|
|
|
else
|
|
|
|
dat->pos = TBL_DATA_DATA;
|
|
|
|
|
2015-03-02 16:45:41 +00:00
|
|
|
if ((dat->layout->pos == TBL_CELL_HORIZ ||
|
|
|
|
dat->layout->pos == TBL_CELL_DHORIZ ||
|
|
|
|
dat->layout->pos == TBL_CELL_DOWN) &&
|
|
|
|
dat->pos == TBL_DATA_DATA && *dat->string != '\0')
|
|
|
|
mandoc_msg(MANDOCERR_TBLDATA_SPAN,
|
|
|
|
tbl->parse, ln, sv, dat->string);
|
2012-10-18 09:55:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2015-03-02 16:45:41 +00:00
|
|
|
tbl_cdata(struct tbl_node *tbl, int ln, const char *p, int pos)
|
2012-10-18 09:55:16 +00:00
|
|
|
{
|
|
|
|
struct tbl_dat *dat;
|
2014-11-22 18:08:25 +00:00
|
|
|
size_t sz;
|
2012-10-18 09:55:16 +00:00
|
|
|
|
|
|
|
dat = tbl->last_span->last;
|
|
|
|
|
|
|
|
if (p[pos] == 'T' && p[pos + 1] == '}') {
|
|
|
|
pos += 2;
|
|
|
|
if (p[pos] == tbl->opts.tab) {
|
|
|
|
tbl->part = TBL_PART_DATA;
|
|
|
|
pos++;
|
2016-01-15 23:08:59 +00:00
|
|
|
while (p[pos] != '\0')
|
|
|
|
getdata(tbl, tbl->last_span, ln, p, &pos);
|
|
|
|
return 1;
|
2015-03-02 16:45:41 +00:00
|
|
|
} else if (p[pos] == '\0') {
|
2012-10-18 09:55:16 +00:00
|
|
|
tbl->part = TBL_PART_DATA;
|
2016-01-15 23:08:59 +00:00
|
|
|
return 1;
|
2012-10-18 09:55:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Fallthrough: T} is part of a word. */
|
|
|
|
}
|
|
|
|
|
|
|
|
dat->pos = TBL_DATA_DATA;
|
|
|
|
|
2015-03-02 16:45:41 +00:00
|
|
|
if (dat->string != NULL) {
|
|
|
|
sz = strlen(p + pos) + strlen(dat->string) + 2;
|
2012-10-18 09:55:16 +00:00
|
|
|
dat->string = mandoc_realloc(dat->string, sz);
|
2014-11-22 18:08:25 +00:00
|
|
|
(void)strlcat(dat->string, " ", sz);
|
2015-03-02 16:45:41 +00:00
|
|
|
(void)strlcat(dat->string, p + pos, sz);
|
2012-10-18 09:55:16 +00:00
|
|
|
} else
|
2015-03-02 16:45:41 +00:00
|
|
|
dat->string = mandoc_strdup(p + pos);
|
2012-10-18 09:55:16 +00:00
|
|
|
|
2015-03-02 16:45:41 +00:00
|
|
|
if (dat->layout->pos == TBL_CELL_DOWN)
|
|
|
|
mandoc_msg(MANDOCERR_TBLDATA_SPAN, tbl->parse,
|
|
|
|
ln, pos, dat->string);
|
2012-10-18 09:55:16 +00:00
|
|
|
|
2016-01-15 23:08:59 +00:00
|
|
|
return 0;
|
2012-10-18 09:55:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct tbl_span *
|
|
|
|
newspan(struct tbl_node *tbl, int line, struct tbl_row *rp)
|
|
|
|
{
|
|
|
|
struct tbl_span *dp;
|
|
|
|
|
2015-03-02 16:45:41 +00:00
|
|
|
dp = mandoc_calloc(1, sizeof(*dp));
|
2012-10-18 09:55:16 +00:00
|
|
|
dp->line = line;
|
2014-01-31 19:59:03 +00:00
|
|
|
dp->opts = &tbl->opts;
|
2012-10-18 09:55:16 +00:00
|
|
|
dp->layout = rp;
|
2015-03-02 16:45:41 +00:00
|
|
|
dp->prev = tbl->last_span;
|
2012-10-18 09:55:16 +00:00
|
|
|
|
2015-03-02 16:45:41 +00:00
|
|
|
if (dp->prev == NULL) {
|
|
|
|
tbl->first_span = dp;
|
2012-10-18 09:55:16 +00:00
|
|
|
tbl->current_span = NULL;
|
2015-03-02 16:45:41 +00:00
|
|
|
} else
|
|
|
|
dp->prev->next = dp;
|
|
|
|
tbl->last_span = dp;
|
2012-10-18 09:55:16 +00:00
|
|
|
|
2016-01-15 23:08:59 +00:00
|
|
|
return dp;
|
2012-10-18 09:55:16 +00:00
|
|
|
}
|
|
|
|
|
2015-03-02 16:45:41 +00:00
|
|
|
void
|
|
|
|
tbl_data(struct tbl_node *tbl, int ln, const char *p, int pos)
|
2012-10-18 09:55:16 +00:00
|
|
|
{
|
|
|
|
struct tbl_span *dp;
|
|
|
|
struct tbl_row *rp;
|
|
|
|
|
2014-11-22 18:08:25 +00:00
|
|
|
/*
|
2012-10-18 09:55:16 +00:00
|
|
|
* Choose a layout row: take the one following the last parsed
|
|
|
|
* span's. If that doesn't exist, use the last parsed span's.
|
|
|
|
* If there's no last parsed span, use the first row. Lastly,
|
|
|
|
* if the last span was a horizontal line, use the same layout
|
|
|
|
* (it doesn't "consume" the layout).
|
|
|
|
*/
|
|
|
|
|
2015-03-02 16:45:41 +00:00
|
|
|
if (tbl->last_span != NULL) {
|
2012-10-18 09:55:16 +00:00
|
|
|
if (tbl->last_span->pos == TBL_SPAN_DATA) {
|
|
|
|
for (rp = tbl->last_span->layout->next;
|
2015-03-02 16:45:41 +00:00
|
|
|
rp != NULL && rp->first != NULL;
|
|
|
|
rp = rp->next) {
|
2012-10-18 09:55:16 +00:00
|
|
|
switch (rp->first->pos) {
|
2014-11-22 18:08:25 +00:00
|
|
|
case TBL_CELL_HORIZ:
|
2012-10-18 09:55:16 +00:00
|
|
|
dp = newspan(tbl, ln, rp);
|
|
|
|
dp->pos = TBL_SPAN_HORIZ;
|
|
|
|
continue;
|
2014-11-22 18:08:25 +00:00
|
|
|
case TBL_CELL_DHORIZ:
|
2012-10-18 09:55:16 +00:00
|
|
|
dp = newspan(tbl, ln, rp);
|
|
|
|
dp->pos = TBL_SPAN_DHORIZ;
|
|
|
|
continue;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
rp = tbl->last_span->layout;
|
|
|
|
|
2015-03-02 16:45:41 +00:00
|
|
|
if (rp == NULL)
|
2012-10-18 09:55:16 +00:00
|
|
|
rp = tbl->last_span->layout;
|
|
|
|
} else
|
|
|
|
rp = tbl->first_row;
|
|
|
|
|
|
|
|
assert(rp);
|
|
|
|
|
|
|
|
dp = newspan(tbl, ln, rp);
|
|
|
|
|
|
|
|
if ( ! strcmp(p, "_")) {
|
|
|
|
dp->pos = TBL_SPAN_HORIZ;
|
2015-03-02 16:45:41 +00:00
|
|
|
return;
|
2012-10-18 09:55:16 +00:00
|
|
|
} else if ( ! strcmp(p, "=")) {
|
|
|
|
dp->pos = TBL_SPAN_DHORIZ;
|
2015-03-02 16:45:41 +00:00
|
|
|
return;
|
2012-10-18 09:55:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dp->pos = TBL_SPAN_DATA;
|
|
|
|
|
2015-03-02 16:45:41 +00:00
|
|
|
while (p[pos] != '\0')
|
|
|
|
getdata(tbl, dp, ln, p, &pos);
|
2012-10-18 09:55:16 +00:00
|
|
|
}
|