diff options
author | 2011-01-16 01:11:50 +0000 | |
---|---|---|
committer | 2011-01-16 01:11:50 +0000 | |
commit | 8351ebcfb42f0ea237898b42ca99166750f47920 (patch) | |
tree | 754cce4b712f468cf322b5fb93fc694663048e52 | |
parent | the serial number is now available in struct usb_device_info, so (diff) | |
download | wireguard-openbsd-8351ebcfb42f0ea237898b42ca99166750f47920.tar.xz wireguard-openbsd-8351ebcfb42f0ea237898b42ca99166750f47920.zip |
Various tbl improvements from kristaps@:
* horizontal lines do not consume layout lines
* skip excessive data cells
* prepare rendering of spanned cells
* support vertical spans
-rw-r--r-- | usr.bin/mandoc/main.c | 6 | ||||
-rw-r--r-- | usr.bin/mandoc/mandoc.h | 27 | ||||
-rw-r--r-- | usr.bin/mandoc/out.c | 30 | ||||
-rw-r--r-- | usr.bin/mandoc/tbl_data.c | 53 | ||||
-rw-r--r-- | usr.bin/mandoc/tbl_html.c | 16 | ||||
-rw-r--r-- | usr.bin/mandoc/tbl_layout.c | 33 | ||||
-rw-r--r-- | usr.bin/mandoc/tbl_term.c | 64 | ||||
-rw-r--r-- | usr.bin/mandoc/tree.c | 9 |
8 files changed, 160 insertions, 78 deletions
diff --git a/usr.bin/mandoc/main.c b/usr.bin/mandoc/main.c index 682a3b1bb03..5165557e587 100644 --- a/usr.bin/mandoc/main.c +++ b/usr.bin/mandoc/main.c @@ -1,4 +1,4 @@ -/* $Id: main.c,v 1.66 2011/01/10 23:53:32 schwarze Exp $ */ +/* $Id: main.c,v 1.67 2011/01/16 01:11:50 schwarze Exp $ */ /* * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv> * Copyright (c) 2010 Ingo Schwarze <schwarze@openbsd.org> @@ -163,9 +163,6 @@ static const char * const mandocerrs[MANDOCERR_MAX] = { "unknown escape sequence", "unterminated quoted string", - /* related to tables */ - "extra data cells", - "generic error", /* related to tables */ @@ -176,6 +173,7 @@ static const char * const mandocerrs[MANDOCERR_MAX] = { "no table data cells specified", "ignore data in cell", "data block still open", + "ignoring extra data cells", "input stack limit exceeded, infinite loop?", "skipping bad character", diff --git a/usr.bin/mandoc/mandoc.h b/usr.bin/mandoc/mandoc.h index bb2a7074f12..55dc636cbc5 100644 --- a/usr.bin/mandoc/mandoc.h +++ b/usr.bin/mandoc/mandoc.h @@ -1,4 +1,4 @@ -/* $Id: mandoc.h,v 1.29 2011/01/10 23:53:32 schwarze Exp $ */ +/* $Id: mandoc.h,v 1.30 2011/01/16 01:11:50 schwarze Exp $ */ /* * Copyright (c) 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv> * @@ -100,9 +100,6 @@ enum mandocerr { MANDOCERR_BADESCAPE, /* unknown escape sequence */ MANDOCERR_BADQUOTE, /* unterminated quoted string */ - /* related to tables */ - MANDOCERR_TBLEXTRADAT, /* extra data cells */ - MANDOCERR_ERROR, /* ===== start of errors ===== */ /* related to tables */ @@ -113,6 +110,7 @@ enum mandocerr { MANDOCERR_TBLNODATA, /* no table data cells specified */ MANDOCERR_TBLIGNDATA, /* ignore data in cell */ MANDOCERR_TBLBLOCK, /* data block still open */ + MANDOCERR_TBLEXTRADAT, /* ignoring extra data cells */ MANDOCERR_ROFFLOOP, /* input stack limit exceeded, infinite loop? */ MANDOCERR_BADCHAR, /* skipping bad character */ @@ -203,7 +201,7 @@ enum tbl_cellt { struct tbl_cell { struct tbl_cell *next; enum tbl_cellt pos; - int spacing; + size_t spacing; int flags; #define TBL_CELL_TALIGN (1 << 0) /* t, T */ #define TBL_CELL_BALIGN (1 << 1) /* d, D */ @@ -225,12 +223,12 @@ struct tbl_row { }; enum tbl_datt { - TBL_DATA_NONE, - TBL_DATA_DATA, - TBL_DATA_HORIZ, - TBL_DATA_DHORIZ, - TBL_DATA_NHORIZ, - TBL_DATA_NDHORIZ + TBL_DATA_NONE, /* has no data */ + TBL_DATA_DATA, /* consists of data/string */ + TBL_DATA_HORIZ, /* horizontal line */ + TBL_DATA_DHORIZ, /* double-horizontal line */ + TBL_DATA_NHORIZ, /* squeezed horizontal line */ + TBL_DATA_NDHORIZ /* squeezed double-horizontal line */ }; /* @@ -238,9 +236,10 @@ enum tbl_datt { * string value that's in the cell. The rest is layout. */ struct tbl_dat { - struct tbl_cell *layout; /* layout cell: CAN BE NULL */ + struct tbl_cell *layout; /* layout cell */ + int spans; /* how many spans follow */ struct tbl_dat *next; - char *string; + char *string; /* data (NULL if not TBL_DATA_DATA) */ enum tbl_datt pos; }; @@ -256,7 +255,7 @@ enum tbl_spant { struct tbl_span { struct tbl *tbl; struct tbl_head *head; - struct tbl_row *layout; /* layout row: CAN BE NULL */ + struct tbl_row *layout; /* layout row */ struct tbl_dat *first; struct tbl_dat *last; int flags; diff --git a/usr.bin/mandoc/out.c b/usr.bin/mandoc/out.c index 026a481aa8f..ffa06345be7 100644 --- a/usr.bin/mandoc/out.c +++ b/usr.bin/mandoc/out.c @@ -1,4 +1,4 @@ -/* $Id: out.c,v 1.9 2011/01/09 14:30:48 schwarze Exp $ */ +/* $Id: out.c,v 1.10 2011/01/16 01:11:50 schwarze Exp $ */ /* * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv> * @@ -395,8 +395,7 @@ tblcalc(struct rofftbl *tbl, const struct tbl_span *sp) * to data cells in the data section. */ for (dp = sp->first; dp; dp = dp->next) { - if (NULL == dp->layout) - continue; + assert(dp->layout); col = &tbl->cols[dp->layout->head->ident]; tblcalc_data(tbl, col, sp->tbl, dp); } @@ -450,6 +449,8 @@ tblcalc_data(struct rofftbl *tbl, struct roffcol *col, case (TBL_CELL_NUMBER): tblcalc_number(tbl, col, tp, dp); break; + case (TBL_CELL_DOWN): + break; default: abort(); /* NOTREACHED */ @@ -461,6 +462,7 @@ tblcalc_literal(struct rofftbl *tbl, struct roffcol *col, const struct tbl_dat *dp) { size_t sz, bufsz, spsz; + const char *str; /* * Calculate our width and use the spacing, with a minimum @@ -468,9 +470,11 @@ tblcalc_literal(struct rofftbl *tbl, struct roffcol *col, * either side, while right/left get a single adjacent space). */ - sz = bufsz = spsz = 0; - if (dp->string) - sz = (*tbl->slen)(dp->string, tbl->arg); + bufsz = spsz = 0; + str = dp->string ? dp->string : ""; + sz = (*tbl->slen)(str, tbl->arg); + + /* FIXME: TBL_DATA_HORIZ et al.? */ assert(dp->layout); switch (dp->layout->pos) { @@ -499,9 +503,9 @@ tblcalc_number(struct rofftbl *tbl, struct roffcol *col, const struct tbl *tp, const struct tbl_dat *dp) { int i; - size_t sz, psz, ssz, d, max; - char *cp; + size_t sz, psz, ssz, d; const char *str; + char *cp; char buf[2]; /* @@ -513,11 +517,11 @@ tblcalc_number(struct rofftbl *tbl, struct roffcol *col, * Finally, re-assign the stored values. */ - str = dp && dp->string ? dp->string : ""; - max = dp && dp->layout ? dp->layout->spacing : 0; - + str = dp->string ? dp->string : ""; sz = (*tbl->slen)(str, tbl->arg); + /* FIXME: TBL_DATA_HORIZ et al.? */ + buf[0] = tp->decimal; buf[1] = '\0'; @@ -553,8 +557,8 @@ tblcalc_number(struct rofftbl *tbl, struct roffcol *col, /* Adjust for stipulated width. */ - if (col->width < max) - col->width = max; + if (col->width < dp->layout->spacing) + col->width = dp->layout->spacing; } diff --git a/usr.bin/mandoc/tbl_data.c b/usr.bin/mandoc/tbl_data.c index d1d39755c38..c9b26b5aa1c 100644 --- a/usr.bin/mandoc/tbl_data.c +++ b/usr.bin/mandoc/tbl_data.c @@ -1,4 +1,4 @@ -/* $Id: tbl_data.c,v 1.5 2011/01/09 14:30:48 schwarze Exp $ */ +/* $Id: tbl_data.c,v 1.6 2011/01/16 01:11:50 schwarze Exp $ */ /* * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv> * @@ -33,7 +33,7 @@ data(struct tbl_node *tbl, struct tbl_span *dp, { struct tbl_dat *dat; struct tbl_cell *cp; - int sv; + int sv, spans; cp = NULL; if (dp->last && dp->last->layout) @@ -51,12 +51,32 @@ data(struct tbl_node *tbl, struct tbl_span *dp, TBL_CELL_SPAN == cp->pos)) cp = cp->next; + /* + * Stop processing when we reach the end of the available layout + * cells. This means that we have extra input. + */ + + if (NULL == cp) { + TBL_MSG(tbl, MANDOCERR_TBLEXTRADAT, ln, *pos); + /* Skip to the end... */ + while (p[*pos]) + (*pos)++; + return(1); + } + dat = mandoc_calloc(1, sizeof(struct tbl_dat)); dat->layout = cp; dat->pos = TBL_DATA_NONE; - if (NULL == dat->layout) - TBL_MSG(tbl, MANDOCERR_TBLEXTRADAT, ln, *pos); + assert(TBL_CELL_SPAN != cp->pos); + + for (spans = 0, cp = cp->next; cp; cp = cp->next) + if (TBL_CELL_SPAN == cp->pos) + spans++; + else + break; + + dat->spans = spans; if (dp->last) { dp->last->next = dat; @@ -97,11 +117,9 @@ data(struct tbl_node *tbl, struct tbl_span *dp, else dat->pos = TBL_DATA_DATA; - if (NULL == dat->layout) - return(1); - if (TBL_CELL_HORIZ == dat->layout->pos || - TBL_CELL_DHORIZ == dat->layout->pos) + TBL_CELL_DHORIZ == dat->layout->pos || + TBL_CELL_DOWN == dat->layout->pos) if (TBL_DATA_DATA == dat->pos && '\0' != *dat->string) TBL_MSG(tbl, MANDOCERR_TBLIGNDATA, ln, sv); @@ -119,7 +137,6 @@ tbl_cdata(struct tbl_node *tbl, int ln, const char *p) pos = 0; dat = tbl->last_span->last; - dat->pos = TBL_DATA_DATA; if (p[pos] == 'T' && p[pos + 1] == '}') { pos += 2; @@ -135,6 +152,8 @@ tbl_cdata(struct tbl_node *tbl, int ln, const char *p) /* Fallthrough: T} is part of a word. */ } + dat->pos = TBL_DATA_DATA; + if (dat->string) { sz = strlen(p) + strlen(dat->string) + 2; dat->string = mandoc_realloc(dat->string, sz); @@ -143,6 +162,9 @@ tbl_cdata(struct tbl_node *tbl, int ln, const char *p) } else dat->string = mandoc_strdup(p); + if (TBL_CELL_DOWN == dat->layout->pos) + TBL_MSG(tbl, MANDOCERR_TBLIGNDATA, ln, pos); + return(0); } @@ -163,18 +185,25 @@ tbl_data(struct tbl_node *tbl, int ln, const char *p) /* * 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. This can - * be NULL! + * 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). */ if (tbl->last_span) { assert(tbl->last_span->layout); - rp = tbl->last_span->layout->next; + if (tbl->last_span->pos == TBL_SPAN_DATA) + rp = tbl->last_span->layout->next; + else + rp = tbl->last_span->layout; + if (NULL == rp) rp = tbl->last_span->layout; } else rp = tbl->first_row; + assert(rp); + dp = mandoc_calloc(1, sizeof(struct tbl_span)); dp->tbl = &tbl->opts; dp->layout = rp; diff --git a/usr.bin/mandoc/tbl_html.c b/usr.bin/mandoc/tbl_html.c index 5c580cb7524..d4090201dfc 100644 --- a/usr.bin/mandoc/tbl_html.c +++ b/usr.bin/mandoc/tbl_html.c @@ -1,4 +1,4 @@ -/* $Id: tbl_html.c,v 1.2 2011/01/09 14:30:48 schwarze Exp $ */ +/* $Id: tbl_html.c,v 1.3 2011/01/16 01:11:50 schwarze Exp $ */ /* * Copyright (c) 2011 Kristaps Dzonsons <kristaps@kth.se> * @@ -104,10 +104,18 @@ print_tbl(struct html *h, const struct tbl_span *sp) PAIR_STYLE_INIT(&tag, h); tt = print_otag(h, TAG_TD, 1, &tag); - if (dp && dp->string) - print_text(h, dp->string); - if (dp) + if (dp) { + switch (dp->layout->pos) { + case (TBL_CELL_DOWN): + break; + default: + if (NULL == dp->string) + break; + print_text(h, dp->string); + break; + } dp = dp->next; + } print_tagq(h, tt); } diff --git a/usr.bin/mandoc/tbl_layout.c b/usr.bin/mandoc/tbl_layout.c index 7cd8d63d469..28a0d2766df 100644 --- a/usr.bin/mandoc/tbl_layout.c +++ b/usr.bin/mandoc/tbl_layout.c @@ -1,4 +1,4 @@ -/* $Id: tbl_layout.c,v 1.6 2011/01/09 14:30:48 schwarze Exp $ */ +/* $Id: tbl_layout.c,v 1.7 2011/01/16 01:11:50 schwarze Exp $ */ /* * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv> * @@ -122,7 +122,7 @@ mod: } *pos += i; - cp->spacing = atoi(buf); + cp->spacing = (size_t)atoi(buf); goto mod; /* NOTREACHED */ @@ -197,10 +197,35 @@ cell(struct tbl_node *tbl, struct tbl_row *rp, /* * If a span cell is found first, raise a warning and abort the - * parse. FIXME: recover from this somehow? + * parse. If a span cell is found and the last layout element + * isn't a "normal" layout, bail. + * + * FIXME: recover from this somehow? + */ + + if (TBL_CELL_SPAN == c) { + if (NULL == rp->first) { + TBL_MSG(tbl, MANDOCERR_TBLLAYOUT, ln, *pos); + return(0); + } else if (rp->last) + switch (rp->last->pos) { + case (TBL_CELL_VERT): + case (TBL_CELL_DVERT): + case (TBL_CELL_HORIZ): + case (TBL_CELL_DHORIZ): + TBL_MSG(tbl, MANDOCERR_TBLLAYOUT, ln, *pos); + return(0); + default: + break; + } + } + + /* + * If a vertical spanner is found, we may not be in the first + * row. */ - if (NULL == rp->first && TBL_CELL_SPAN == c) { + if (TBL_CELL_DOWN == c && rp == tbl->first_row) { TBL_MSG(tbl, MANDOCERR_TBLLAYOUT, ln, *pos); return(0); } diff --git a/usr.bin/mandoc/tbl_term.c b/usr.bin/mandoc/tbl_term.c index 8165028d6cf..53556e5ff24 100644 --- a/usr.bin/mandoc/tbl_term.c +++ b/usr.bin/mandoc/tbl_term.c @@ -1,4 +1,4 @@ -/* $Id: tbl_term.c,v 1.6 2011/01/09 14:30:48 schwarze Exp $ */ +/* $Id: tbl_term.c,v 1.7 2011/01/16 01:11:50 schwarze Exp $ */ /* * Copyright (c) 2009, 2011 Kristaps Dzonsons <kristaps@kth.se> * @@ -60,6 +60,7 @@ term_tbl(struct termp *tp, const struct tbl_span *sp) const struct tbl_head *hp; const struct tbl_dat *dp; struct roffcol *col; + int spans; size_t rmargin, maxrmargin; rmargin = tp->rmargin; @@ -111,23 +112,39 @@ term_tbl(struct termp *tp, const struct tbl_span *sp) case (TBL_SPAN_DATA): /* Iterate over template headers. */ dp = sp->first; + spans = 0; for (hp = sp->head; hp; hp = hp->next) { + /* + * If the current data header is invoked during + * a spanner ("spans" > 0), don't emit anything + * at all. + */ switch (hp->pos) { case (TBL_HEAD_VERT): /* FALLTHROUGH */ case (TBL_HEAD_DVERT): - tbl_vrule(tp, hp); + if (spans <= 0) + tbl_vrule(tp, hp); continue; case (TBL_HEAD_DATA): break; } + if (--spans >= 0) + continue; + col = &tp->tbl.cols[hp->ident]; tbl_data(tp, sp->tbl, dp, col); - /* Go to the next data cell. */ - if (dp) + /* + * Go to the next data cell and assign the + * number of subsequent spans, if applicable. + */ + + if (dp) { + spans = dp->spans; dp = dp->next; + } } break; } @@ -240,12 +257,12 @@ tbl_data(struct termp *tp, const struct tbl *tbl, const struct tbl_dat *dp, const struct roffcol *col) { - enum tbl_cellt pos; if (NULL == dp) { tbl_char(tp, ASCII_NBRSP, col->width); return; } + assert(dp->layout); switch (dp->pos) { case (TBL_DATA_NONE): @@ -265,9 +282,7 @@ tbl_data(struct termp *tp, const struct tbl *tbl, break; } - pos = dp && dp->layout ? dp->layout->pos : TBL_CELL_LEFT; - - switch (pos) { + switch (dp->layout->pos) { case (TBL_CELL_HORIZ): tbl_char(tp, '-', col->width); break; @@ -286,6 +301,9 @@ tbl_data(struct termp *tp, const struct tbl *tbl, case (TBL_CELL_NUMBER): tbl_number(tp, tbl, dp, col); break; + case (TBL_CELL_DOWN): + tbl_char(tp, ASCII_NBRSP, col->width); + break; default: abort(); /* NOTREACHED */ @@ -336,38 +354,35 @@ tbl_literal(struct termp *tp, const struct tbl_dat *dp, const struct roffcol *col) { size_t padl, padr, ssz; - enum tbl_cellt pos; - const char *str; padl = padr = 0; - pos = dp && dp->layout ? dp->layout->pos : TBL_CELL_LEFT; - str = dp && dp->string ? dp->string : ""; + assert(dp->string); ssz = term_len(tp, 1); - switch (pos) { + switch (dp->layout->pos) { case (TBL_CELL_LONG): padl = ssz; - padr = col->width - term_strlen(tp, str) - ssz; + padr = col->width - term_strlen(tp, dp->string) - ssz; break; case (TBL_CELL_CENTRE): - padl = col->width - term_strlen(tp, str); + padl = col->width - term_strlen(tp, dp->string); if (padl % 2) padr++; padl /= 2; padr += padl; break; case (TBL_CELL_RIGHT): - padl = col->width - term_strlen(tp, str); + padl = col->width - term_strlen(tp, dp->string); break; default: - padr = col->width - term_strlen(tp, str); + padr = col->width - term_strlen(tp, dp->string); break; } tbl_char(tp, ASCII_NBRSP, padl); - term_word(tp, str); + term_word(tp, dp->string); tbl_char(tp, ASCII_NBRSP, padr); } @@ -378,7 +393,6 @@ tbl_number(struct termp *tp, const struct tbl *tbl, { char *cp; char buf[2]; - const char *str; size_t sz, psz, ssz, d, padl; int i; @@ -387,19 +401,19 @@ tbl_number(struct termp *tp, const struct tbl *tbl, * and the maximum decimal; right-pad by the remaining amount. */ - str = dp && dp->string ? dp->string : ""; + assert(dp->string); - sz = term_strlen(tp, str); + sz = term_strlen(tp, dp->string); buf[0] = tbl->decimal; buf[1] = '\0'; psz = term_strlen(tp, buf); - if (NULL != (cp = strrchr(str, tbl->decimal))) { + if (NULL != (cp = strrchr(dp->string, tbl->decimal))) { buf[1] = '\0'; - for (ssz = 0, i = 0; cp != &str[i]; i++) { - buf[0] = str[i]; + for (ssz = 0, i = 0; cp != &dp->string[i]; i++) { + buf[0] = dp->string[i]; ssz += term_strlen(tp, buf); } d = ssz + psz; @@ -412,7 +426,7 @@ tbl_number(struct termp *tp, const struct tbl *tbl, padl = col->decimal - d; tbl_char(tp, ASCII_NBRSP, padl); - term_word(tp, str); + term_word(tp, dp->string); tbl_char(tp, ASCII_NBRSP, col->width - sz - padl); } diff --git a/usr.bin/mandoc/tree.c b/usr.bin/mandoc/tree.c index 959b0a5b744..5ad54dd2cb0 100644 --- a/usr.bin/mandoc/tree.c +++ b/usr.bin/mandoc/tree.c @@ -1,4 +1,4 @@ -/* $Id: tree.c,v 1.11 2011/01/04 22:28:17 schwarze Exp $ */ +/* $Id: tree.c,v 1.12 2011/01/16 01:11:50 schwarze Exp $ */ /* * Copyright (c) 2008, 2009, 2011 Kristaps Dzonsons <kristaps@bsd.lv> * @@ -278,7 +278,12 @@ print_span(const struct tbl_span *sp, int indent) default: break; } - printf("[%s%s]", dp->string, dp->layout ? "" : "*"); + printf("[\"%s\"", dp->string ? dp->string : ""); + if (dp->spans) + printf("(%d)", dp->spans); + if (NULL == dp->layout) + putchar('*'); + putchar(']'); if (dp->next) putchar(' '); } |