version 1.15, 2010/04/07 11:29:55 |
version 1.78, 2019/03/29 21:27:06 |
|
|
/* $Id$ */ |
/* $Id$ */ |
/* |
/* |
* Copyright (c) 2009 Kristaps Dzonsons <kristaps@kth.se> |
* Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv> |
|
* Copyright (c) 2011,2014,2015,2017,2018 Ingo Schwarze <schwarze@openbsd.org> |
* |
* |
* Permission to use, copy, modify, and distribute this software for any |
* Permission to use, copy, modify, and distribute this software for any |
* purpose with or without fee is hereby granted, provided that the above |
* purpose with or without fee is hereby granted, provided that the above |
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
*/ |
*/ |
#ifdef HAVE_CONFIG_H |
|
#include "config.h" |
#include "config.h" |
#endif |
|
|
|
#include <sys/types.h> |
#include <sys/types.h> |
|
|
#include <assert.h> |
#include <assert.h> |
#include <ctype.h> |
#include <ctype.h> |
#include <stdio.h> |
#include <stdint.h> |
#include <stdlib.h> |
#include <stdlib.h> |
#include <string.h> |
#include <string.h> |
#include <time.h> |
#include <time.h> |
|
|
|
#include "mandoc_aux.h" |
|
#include "tbl.h" |
#include "out.h" |
#include "out.h" |
|
|
/* See a2roffdeco(). */ |
struct tbl_colgroup { |
#define C2LIM(c, l) do { \ |
struct tbl_colgroup *next; |
(l) = 1; \ |
size_t wanted; |
if ('[' == (c) || '\'' == (c)) \ |
int startcol; |
(l) = 0; \ |
int endcol; |
else if ('(' == (c)) \ |
}; |
(l) = 2; } \ |
|
while (/* CONSTCOND */ 0) |
|
|
|
/* See a2roffdeco(). */ |
static size_t tblcalc_data(struct rofftbl *, struct roffcol *, |
#define C2TERM(c, t) do { \ |
const struct tbl_opts *, const struct tbl_dat *, |
(t) = 0; \ |
size_t); |
if ('\'' == (c)) \ |
static size_t tblcalc_literal(struct rofftbl *, struct roffcol *, |
(t) = 1; \ |
const struct tbl_dat *, size_t); |
else if ('[' == (c)) \ |
static size_t tblcalc_number(struct rofftbl *, struct roffcol *, |
(t) = 2; \ |
const struct tbl_opts *, const struct tbl_dat *); |
else if ('(' == (c)) \ |
|
(t) = 3; } \ |
|
while (/* CONSTCOND */ 0) |
|
|
|
/* |
|
* Convert a `scaling unit' to a consistent form, or fail. Scaling |
/* |
* units are documented in groff.7, mdoc.7, man.7. |
* Parse the *src string and store a scaling unit into *dst. |
|
* If the string doesn't specify the unit, use the default. |
|
* If no default is specified, fail. |
|
* Return a pointer to the byte after the last byte used, |
|
* or NULL on total failure. |
*/ |
*/ |
int |
const char * |
a2roffsu(const char *src, struct roffsu *dst, enum roffscale def) |
a2roffsu(const char *src, struct roffsu *dst, enum roffscale def) |
{ |
{ |
char buf[BUFSIZ], hasd; |
char *endptr; |
int i; |
|
enum roffscale unit; |
|
|
|
if ('\0' == *src) |
dst->unit = def == SCALE_MAX ? SCALE_BU : def; |
return(0); |
dst->scale = strtod(src, &endptr); |
|
if (endptr == src) |
|
return NULL; |
|
|
i = hasd = 0; |
switch (*endptr++) { |
|
case 'c': |
switch (*src) { |
dst->unit = SCALE_CM; |
case ('+'): |
|
src++; |
|
break; |
break; |
case ('-'): |
case 'i': |
buf[i++] = *src++; |
dst->unit = SCALE_IN; |
break; |
break; |
default: |
case 'f': |
|
dst->unit = SCALE_FS; |
break; |
break; |
} |
case 'M': |
|
dst->unit = SCALE_MM; |
if ('\0' == *src) |
|
return(0); |
|
|
|
while (i < BUFSIZ) { |
|
if ( ! isdigit((u_char)*src)) { |
|
if ('.' != *src) |
|
break; |
|
else if (hasd) |
|
break; |
|
else |
|
hasd = 1; |
|
} |
|
buf[i++] = *src++; |
|
} |
|
|
|
if (BUFSIZ == i || (*src && *(src + 1))) |
|
return(0); |
|
|
|
buf[i] = '\0'; |
|
|
|
switch (*src) { |
|
case ('c'): |
|
unit = SCALE_CM; |
|
break; |
break; |
case ('i'): |
case 'm': |
unit = SCALE_IN; |
dst->unit = SCALE_EM; |
break; |
break; |
case ('P'): |
case 'n': |
unit = SCALE_PC; |
dst->unit = SCALE_EN; |
break; |
break; |
case ('p'): |
case 'P': |
unit = SCALE_PT; |
dst->unit = SCALE_PC; |
break; |
break; |
case ('f'): |
case 'p': |
unit = SCALE_FS; |
dst->unit = SCALE_PT; |
break; |
break; |
case ('v'): |
case 'u': |
unit = SCALE_VS; |
dst->unit = SCALE_BU; |
break; |
break; |
case ('m'): |
case 'v': |
unit = SCALE_EM; |
dst->unit = SCALE_VS; |
break; |
break; |
case ('\0'): |
default: |
|
endptr--; |
if (SCALE_MAX == def) |
if (SCALE_MAX == def) |
return(0); |
return NULL; |
unit = SCALE_BU; |
dst->unit = def; |
break; |
break; |
case ('u'): |
|
unit = SCALE_BU; |
|
break; |
|
case ('M'): |
|
unit = SCALE_MM; |
|
break; |
|
case ('n'): |
|
unit = SCALE_EN; |
|
break; |
|
default: |
|
return(0); |
|
} |
} |
|
return endptr; |
if ((dst->scale = atof(buf)) < 0) |
|
dst->scale = 0; |
|
dst->unit = unit; |
|
dst->pt = hasd; |
|
|
|
return(1); |
|
} |
} |
|
|
|
|
/* |
/* |
* Correctly writes the time in nroff form, which differs from standard |
* Calculate the abstract widths and decimal positions of columns in a |
* form in that a space isn't printed in lieu of the extra %e field for |
* table. This routine allocates the columns structures then runs over |
* single-digit dates. |
* all rows and cells in the table. The function pointers in "tbl" are |
|
* used for the actual width calculations. |
*/ |
*/ |
void |
void |
time2a(time_t t, char *dst, size_t sz) |
tblcalc(struct rofftbl *tbl, const struct tbl_span *sp_first, |
|
size_t offset, size_t rmargin) |
{ |
{ |
struct tm tm; |
struct roffsu su; |
char buf[5]; |
const struct tbl_opts *opts; |
char *p; |
const struct tbl_span *sp; |
size_t nsz; |
const struct tbl_dat *dp; |
|
struct roffcol *col; |
|
struct tbl_colgroup *first_group, **gp, *g; |
|
size_t *colwidth; |
|
size_t ewidth, min1, min2, wanted, width, xwidth; |
|
int done, icol, maxcol, necol, nxcol, quirkcol; |
|
|
assert(sz > 1); |
/* |
localtime_r(&t, &tm); |
* Allocate the master column specifiers. These will hold the |
|
* widths and decimal positions for all cells in the column. It |
|
* must be freed and nullified by the caller. |
|
*/ |
|
|
p = dst; |
assert(tbl->cols == NULL); |
nsz = 0; |
tbl->cols = mandoc_calloc((size_t)sp_first->opts->cols, |
|
sizeof(struct roffcol)); |
|
opts = sp_first->opts; |
|
|
dst[0] = '\0'; |
maxcol = -1; |
|
first_group = NULL; |
|
for (sp = sp_first; sp != NULL; sp = sp->next) { |
|
if (sp->pos != TBL_SPAN_DATA) |
|
continue; |
|
|
if (0 == (nsz = strftime(p, sz, "%B ", &tm))) |
/* |
return; |
* Account for the data cells in the layout, matching it |
|
* to data cells in the data section. |
|
*/ |
|
|
p += (int)nsz; |
gp = &first_group; |
sz -= nsz; |
for (dp = sp->first; dp != NULL; dp = dp->next) { |
|
icol = dp->layout->col; |
|
while (maxcol < icol + dp->hspans) |
|
tbl->cols[++maxcol].spacing = SIZE_MAX; |
|
col = tbl->cols + icol; |
|
col->flags |= dp->layout->flags; |
|
if (dp->layout->flags & TBL_CELL_WIGN) |
|
continue; |
|
|
if (0 == strftime(buf, sizeof(buf), "%e, ", &tm)) |
/* Handle explicit width specifications. */ |
return; |
|
|
|
nsz = strlcat(p, buf + (' ' == buf[0] ? 1 : 0), sz); |
if (dp->layout->wstr != NULL && |
|
dp->layout->width == 0 && |
|
a2roffsu(dp->layout->wstr, &su, SCALE_EN) |
|
!= NULL) |
|
dp->layout->width = |
|
(*tbl->sulen)(&su, tbl->arg); |
|
if (col->width < dp->layout->width) |
|
col->width = dp->layout->width; |
|
if (dp->layout->spacing != SIZE_MAX && |
|
(col->spacing == SIZE_MAX || |
|
col->spacing < dp->layout->spacing)) |
|
col->spacing = dp->layout->spacing; |
|
|
if (nsz >= sz) |
/* |
return; |
* Calculate an automatic width. |
|
* Except for spanning cells, apply it. |
|
*/ |
|
|
p += (int)nsz; |
width = tblcalc_data(tbl, |
sz -= nsz; |
dp->hspans == 0 ? col : NULL, |
|
opts, dp, |
|
dp->block == 0 ? 0 : |
|
dp->layout->width ? dp->layout->width : |
|
rmargin ? (rmargin + sp->opts->cols / 2) |
|
/ (sp->opts->cols + 1) : 0); |
|
if (dp->hspans == 0) |
|
continue; |
|
|
(void)strftime(p, sz, "%Y", &tm); |
/* |
} |
* Build an ordered, singly linked list |
|
* of all groups of columns joined by spans, |
|
* recording the minimum width for each group. |
|
*/ |
|
|
|
while (*gp != NULL && ((*gp)->startcol < icol || |
|
(*gp)->endcol < icol + dp->hspans)) |
|
gp = &(*gp)->next; |
|
if (*gp == NULL || (*gp)->startcol > icol || |
|
(*gp)->endcol > icol + dp->hspans) { |
|
g = mandoc_malloc(sizeof(*g)); |
|
g->next = *gp; |
|
g->wanted = width; |
|
g->startcol = icol; |
|
g->endcol = icol + dp->hspans; |
|
*gp = g; |
|
} else if ((*gp)->wanted < width) |
|
(*gp)->wanted = width; |
|
} |
|
} |
|
|
/* |
/* |
* Returns length of parsed string (the leading "\" should NOT be |
* Column spacings are needed for span width calculations, |
* included). This can be zero if the current character is the nil |
* so set the default values now. |
* terminator. "d" is set to the type of parsed decorator, which may |
*/ |
* have an adjoining "word" of size "sz" (e.g., "(ab" -> "ab", 2). |
|
*/ |
|
int |
|
a2roffdeco(enum roffdeco *d, |
|
const char **word, size_t *sz) |
|
{ |
|
int j, term, lim; |
|
char set; |
|
const char *wp, *sp; |
|
|
|
*d = DECO_NONE; |
for (icol = 0; icol <= maxcol; icol++) |
wp = *word; |
if (tbl->cols[icol].spacing == SIZE_MAX || icol == maxcol) |
|
tbl->cols[icol].spacing = 3; |
|
|
switch ((set = *wp)) { |
/* |
case ('\0'): |
* Replace the minimum widths with the missing widths, |
return(0); |
* and dismiss groups that are already wide enough. |
|
*/ |
|
|
case ('('): |
gp = &first_group; |
if ('\0' == *(++wp)) |
while ((g = *gp) != NULL) { |
return(1); |
done = 0; |
if ('\0' == *(wp + 1)) |
for (icol = g->startcol; icol <= g->endcol; icol++) { |
return(2); |
width = tbl->cols[icol].width; |
|
if (icol < g->endcol) |
|
width += tbl->cols[icol].spacing; |
|
if (g->wanted <= width) { |
|
done = 1; |
|
break; |
|
} else |
|
(*gp)->wanted -= width; |
|
} |
|
if (done) { |
|
*gp = g->next; |
|
free(g); |
|
} else |
|
gp = &(*gp)->next; |
|
} |
|
|
*d = DECO_SPECIAL; |
colwidth = mandoc_reallocarray(NULL, maxcol + 1, sizeof(*colwidth)); |
*sz = 2; |
while (first_group != NULL) { |
*word = wp; |
|
return(3); |
|
|
|
case ('F'): |
|
/* FALLTHROUGH */ |
|
case ('f'): |
|
/* |
/* |
* FIXME: this needs work and consolidation (it should |
* Rebuild the array of the widths of all columns |
* follow the sequence that special characters do, for |
* participating in spans that require expansion. |
* one), but isn't a priority at the moment. Note, for |
|
* one, that in reality \fB != \FB, although here we let |
|
* these slip by. |
|
*/ |
*/ |
switch (*(++wp)) { |
|
case ('\0'): |
|
return(1); |
|
case ('3'): |
|
/* FALLTHROUGH */ |
|
case ('B'): |
|
*d = DECO_BOLD; |
|
return(2); |
|
case ('2'): |
|
/* FALLTHROUGH */ |
|
case ('I'): |
|
*d = DECO_ITALIC; |
|
return(2); |
|
case ('P'): |
|
*d = DECO_PREVIOUS; |
|
return(2); |
|
case ('1'): |
|
/* FALLTHROUGH */ |
|
case ('R'): |
|
*d = DECO_ROMAN; |
|
return(2); |
|
case ('('): |
|
if ('\0' == *(++wp)) |
|
return(2); |
|
if ('\0' == *(wp + 1)) |
|
return(3); |
|
|
|
*d = 'F' == set ? DECO_FFONT : DECO_FONT; |
for (icol = 0; icol <= maxcol; icol++) |
*sz = 2; |
colwidth[icol] = SIZE_MAX; |
*word = wp; |
for (g = first_group; g != NULL; g = g->next) |
return(4); |
for (icol = g->startcol; icol <= g->endcol; icol++) |
case ('['): |
colwidth[icol] = tbl->cols[icol].width; |
*word = ++wp; |
|
for (j = 0; *wp && ']' != *wp; wp++, j++) |
|
/* Loop... */ ; |
|
|
|
if ('\0' == *wp) |
/* |
return(j + 2); |
* Find the smallest and second smallest column width |
|
* among the columns which may need expamsion. |
|
*/ |
|
|
*d = 'F' == set ? DECO_FFONT : DECO_FONT; |
min1 = min2 = SIZE_MAX; |
*sz = (size_t)j; |
for (icol = 0; icol <= maxcol; icol++) { |
return(j + 3); |
if (min1 > colwidth[icol]) { |
default: |
min2 = min1; |
break; |
min1 = colwidth[icol]; |
|
} else if (min1 < colwidth[icol] && |
|
min2 > colwidth[icol]) |
|
min2 = colwidth[icol]; |
} |
} |
|
|
*d = 'F' == set ? DECO_FFONT : DECO_FONT; |
/* |
*sz = 1; |
* Find the minimum wanted width |
*word = wp; |
* for any one of the narrowest columns, |
return(2); |
* and mark the columns wanting that width. |
|
*/ |
|
|
case ('*'): |
wanted = min2; |
switch (*(++wp)) { |
for (g = first_group; g != NULL; g = g->next) { |
case ('\0'): |
necol = 0; |
return(1); |
for (icol = g->startcol; icol <= g->endcol; icol++) |
|
if (tbl->cols[icol].width == min1) |
|
necol++; |
|
if (necol == 0) |
|
continue; |
|
width = min1 + (g->wanted - 1) / necol + 1; |
|
if (width > min2) |
|
width = min2; |
|
if (wanted > width) |
|
wanted = width; |
|
for (icol = g->startcol; icol <= g->endcol; icol++) |
|
if (colwidth[icol] == min1 || |
|
(colwidth[icol] < min2 && |
|
colwidth[icol] > width)) |
|
colwidth[icol] = width; |
|
} |
|
|
case ('('): |
/* Record the effect of the widening on the group list. */ |
if ('\0' == *(++wp)) |
|
return(2); |
|
if ('\0' == *(wp + 1)) |
|
return(3); |
|
|
|
*d = DECO_RESERVED; |
gp = &first_group; |
*sz = 2; |
while ((g = *gp) != NULL) { |
*word = wp; |
done = 0; |
return(4); |
for (icol = g->startcol; icol <= g->endcol; icol++) { |
|
if (colwidth[icol] != wanted || |
|
tbl->cols[icol].width == wanted) |
|
continue; |
|
if (g->wanted <= wanted - min1) { |
|
done = 1; |
|
break; |
|
} |
|
g->wanted -= wanted - min1; |
|
} |
|
if (done) { |
|
*gp = g->next; |
|
free(g); |
|
} else |
|
gp = &(*gp)->next; |
|
} |
|
|
case ('['): |
/* Record the effect of the widening on the columns. */ |
*word = ++wp; |
|
for (j = 0; *wp && ']' != *wp; wp++, j++) |
|
/* Loop... */ ; |
|
|
|
if ('\0' == *wp) |
for (icol = 0; icol <= maxcol; icol++) |
return(j + 2); |
if (colwidth[icol] == wanted) |
|
tbl->cols[icol].width = wanted; |
|
} |
|
free(colwidth); |
|
|
*d = DECO_RESERVED; |
/* |
*sz = (size_t)j; |
* Align numbers with text. |
return(j + 3); |
* Count columns to equalize and columns to maximize. |
|
* Find maximum width of the columns to equalize. |
|
* Find total width of the columns *not* to maximize. |
|
*/ |
|
|
default: |
necol = nxcol = 0; |
break; |
ewidth = xwidth = 0; |
|
for (icol = 0; icol <= maxcol; icol++) { |
|
col = tbl->cols + icol; |
|
if (col->width > col->nwidth) |
|
col->decimal += (col->width - col->nwidth) / 2; |
|
else |
|
col->width = col->nwidth; |
|
if (col->flags & TBL_CELL_EQUAL) { |
|
necol++; |
|
if (ewidth < col->width) |
|
ewidth = col->width; |
} |
} |
|
if (col->flags & TBL_CELL_WMAX) |
|
nxcol++; |
|
else |
|
xwidth += col->width; |
|
} |
|
|
*d = DECO_RESERVED; |
/* |
*sz = 1; |
* Equalize columns, if requested for any of them. |
*word = wp; |
* Update total width of the columns not to maximize. |
return(2); |
*/ |
|
|
case ('s'): |
if (necol) { |
sp = wp; |
for (icol = 0; icol <= maxcol; icol++) { |
if ('\0' == *(++wp)) |
col = tbl->cols + icol; |
return(1); |
if ( ! (col->flags & TBL_CELL_EQUAL)) |
|
continue; |
|
if (col->width == ewidth) |
|
continue; |
|
if (nxcol && rmargin) |
|
xwidth += ewidth - col->width; |
|
col->width = ewidth; |
|
} |
|
} |
|
|
C2LIM(*wp, lim); |
/* |
C2TERM(*wp, term); |
* If there are any columns to maximize, find the total |
|
* available width, deducting 3n margins between columns. |
|
* Distribute the available width evenly. |
|
*/ |
|
|
if (term) |
if (nxcol && rmargin) { |
wp++; |
xwidth += 3*maxcol + |
|
(opts->opts & (TBL_OPT_BOX | TBL_OPT_DBOX) ? |
|
2 : !!opts->lvert + !!opts->rvert); |
|
if (rmargin <= offset + xwidth) |
|
return; |
|
xwidth = rmargin - offset - xwidth; |
|
|
*word = wp; |
/* |
|
* Emulate a bug in GNU tbl width calculation that |
|
* manifests itself for large numbers of x-columns. |
|
* Emulating it for 5 x-columns gives identical |
|
* behaviour for up to 6 x-columns. |
|
*/ |
|
|
if (*wp == '+' || *wp == '-') |
if (nxcol == 5) { |
++wp; |
quirkcol = xwidth % nxcol + 2; |
|
if (quirkcol != 3 && quirkcol != 4) |
|
quirkcol = -1; |
|
} else |
|
quirkcol = -1; |
|
|
switch (*wp) { |
necol = 0; |
case ('\''): |
ewidth = 0; |
/* FALLTHROUGH */ |
for (icol = 0; icol <= maxcol; icol++) { |
case ('['): |
col = tbl->cols + icol; |
/* FALLTHROUGH */ |
if ( ! (col->flags & TBL_CELL_WMAX)) |
case ('('): |
continue; |
if (term) |
col->width = (double)xwidth * ++necol / nxcol |
return((int)(wp - sp)); |
- ewidth + 0.4995; |
|
if (necol == quirkcol) |
C2LIM(*wp, lim); |
col->width--; |
C2TERM(*wp, term); |
ewidth += col->width; |
wp++; |
|
break; |
|
default: |
|
break; |
|
} |
} |
|
} |
|
} |
|
|
if ( ! isdigit((u_char)*wp)) |
static size_t |
return((int)(wp - sp)); |
tblcalc_data(struct rofftbl *tbl, struct roffcol *col, |
|
const struct tbl_opts *opts, const struct tbl_dat *dp, size_t mw) |
|
{ |
|
size_t sz; |
|
|
for (j = 0; isdigit((u_char)*wp); j++) { |
/* Branch down into data sub-types. */ |
if (lim && j >= lim) |
|
break; |
|
++wp; |
|
} |
|
|
|
if (term && term < 3) { |
switch (dp->layout->pos) { |
if (1 == term && *wp != '\'') |
case TBL_CELL_HORIZ: |
return((int)(wp - sp)); |
case TBL_CELL_DHORIZ: |
if (2 == term && *wp != ']') |
sz = (*tbl->len)(1, tbl->arg); |
return((int)(wp - sp)); |
if (col != NULL && col->width < sz) |
++wp; |
col->width = sz; |
|
return sz; |
|
case TBL_CELL_LONG: |
|
case TBL_CELL_CENTRE: |
|
case TBL_CELL_LEFT: |
|
case TBL_CELL_RIGHT: |
|
return tblcalc_literal(tbl, col, dp, mw); |
|
case TBL_CELL_NUMBER: |
|
return tblcalc_number(tbl, col, opts, dp); |
|
case TBL_CELL_DOWN: |
|
return 0; |
|
default: |
|
abort(); |
|
} |
|
} |
|
|
|
static size_t |
|
tblcalc_literal(struct rofftbl *tbl, struct roffcol *col, |
|
const struct tbl_dat *dp, size_t mw) |
|
{ |
|
const char *str; /* Beginning of the first line. */ |
|
const char *beg; /* Beginning of the current line. */ |
|
char *end; /* End of the current line. */ |
|
size_t lsz; /* Length of the current line. */ |
|
size_t wsz; /* Length of the current word. */ |
|
size_t msz; /* Length of the longest line. */ |
|
|
|
if (dp->string == NULL || *dp->string == '\0') |
|
return 0; |
|
str = mw ? mandoc_strdup(dp->string) : dp->string; |
|
msz = lsz = 0; |
|
for (beg = str; beg != NULL && *beg != '\0'; beg = end) { |
|
end = mw ? strchr(beg, ' ') : NULL; |
|
if (end != NULL) { |
|
*end++ = '\0'; |
|
while (*end == ' ') |
|
end++; |
} |
} |
|
wsz = (*tbl->slen)(beg, tbl->arg); |
|
if (mw && lsz && lsz + 1 + wsz <= mw) |
|
lsz += 1 + wsz; |
|
else |
|
lsz = wsz; |
|
if (msz < lsz) |
|
msz = lsz; |
|
} |
|
if (mw) |
|
free((void *)str); |
|
if (col != NULL && col->width < msz) |
|
col->width = msz; |
|
return msz; |
|
} |
|
|
*d = DECO_SIZE; |
static size_t |
return((int)(wp - sp)); |
tblcalc_number(struct rofftbl *tbl, struct roffcol *col, |
|
const struct tbl_opts *opts, const struct tbl_dat *dp) |
|
{ |
|
const char *cp, *lastdigit, *lastpoint; |
|
size_t intsz, totsz; |
|
char buf[2]; |
|
|
case ('['): |
if (dp->string == NULL || *dp->string == '\0') |
*word = ++wp; |
return 0; |
|
|
for (j = 0; *wp && ']' != *wp; wp++, j++) |
totsz = (*tbl->slen)(dp->string, tbl->arg); |
/* Loop... */ ; |
if (col == NULL) |
|
return totsz; |
|
|
if ('\0' == *wp) |
/* |
return(j + 1); |
* Find the last digit and |
|
* the last decimal point that is adjacent to a digit. |
|
* The alignment indicator "\&" overrides everything. |
|
*/ |
|
|
*d = DECO_SPECIAL; |
lastdigit = lastpoint = NULL; |
*sz = (size_t)j; |
for (cp = dp->string; cp[0] != '\0'; cp++) { |
return(j + 2); |
if (cp[0] == '\\' && cp[1] == '&') { |
|
lastdigit = lastpoint = cp; |
|
break; |
|
} else if (cp[0] == opts->decimal && |
|
(isdigit((unsigned char)cp[1]) || |
|
(cp > dp->string && isdigit((unsigned char)cp[-1])))) |
|
lastpoint = cp; |
|
else if (isdigit((unsigned char)cp[0])) |
|
lastdigit = cp; |
|
} |
|
|
case ('c'): |
/* Not a number, treat as a literal string. */ |
*d = DECO_NOSPACE; |
|
*sz = 1; |
|
return(1); |
|
|
|
default: |
if (lastdigit == NULL) { |
break; |
if (col != NULL && col->width < totsz) |
|
col->width = totsz; |
|
return totsz; |
} |
} |
|
|
*d = DECO_SPECIAL; |
/* Measure the width of the integer part. */ |
*word = wp; |
|
*sz = 1; |
if (lastpoint == NULL) |
return(1); |
lastpoint = lastdigit + 1; |
|
intsz = 0; |
|
buf[1] = '\0'; |
|
for (cp = dp->string; cp < lastpoint; cp++) { |
|
buf[0] = cp[0]; |
|
intsz += (*tbl->slen)(buf, tbl->arg); |
|
} |
|
|
|
/* |
|
* If this number has more integer digits than all numbers |
|
* seen on earlier lines, shift them all to the right. |
|
* If it has fewer, shift this number to the right. |
|
*/ |
|
|
|
if (intsz > col->decimal) { |
|
col->nwidth += intsz - col->decimal; |
|
col->decimal = intsz; |
|
} else |
|
totsz += col->decimal - intsz; |
|
|
|
/* Update the maximum total width seen so far. */ |
|
|
|
if (totsz > col->nwidth) |
|
col->nwidth = totsz; |
|
return totsz; |
} |
} |