From ecd22486800b060f9e6011718b0b9c5fb5734e2b Mon Sep 17 00:00:00 2001 From: schwarze Date: Thu, 8 Jun 2017 12:54:40 +0000 Subject: [PATCH] make the internal a2roffsu() interface more powerful by returning a pointer to the end of the parsed data, making it easier to parse subsequent bytes --- usr.bin/mandoc/html.c | 7 +++++-- usr.bin/mandoc/man_html.c | 8 ++------ usr.bin/mandoc/man_term.c | 14 +++++++------- usr.bin/mandoc/mdoc_man.c | 24 +++++++++++++++--------- usr.bin/mandoc/mdoc_term.c | 6 ++++-- usr.bin/mandoc/out.c | 15 +++++++-------- usr.bin/mandoc/out.h | 5 +++-- usr.bin/mandoc/roff_html.c | 4 ++-- usr.bin/mandoc/roff_term.c | 6 +++--- usr.bin/mandoc/term.c | 19 +++++-------------- usr.bin/mandoc/term_tab.c | 4 ++-- 11 files changed, 55 insertions(+), 57 deletions(-) diff --git a/usr.bin/mandoc/html.c b/usr.bin/mandoc/html.c index 4c6a3d9416d..f25c21da748 100644 --- a/usr.bin/mandoc/html.c +++ b/usr.bin/mandoc/html.c @@ -1,4 +1,4 @@ -/* $OpenBSD: html.c,v 1.82 2017/05/14 12:26:59 schwarze Exp $ */ +/* $OpenBSD: html.c,v 1.83 2017/06/08 12:54:40 schwarze Exp $ */ /* * Copyright (c) 2008-2011, 2014 Kristaps Dzonsons * Copyright (c) 2011-2015, 2017 Ingo Schwarze @@ -948,7 +948,10 @@ print_word(struct html *h, const char *cp) static void a2width(const char *p, struct roffsu *su) { - if (a2roffsu(p, su, SCALE_MAX) < 2) { + const char *end; + + end = a2roffsu(p, su, SCALE_MAX); + if (end == NULL || *end != '\0') { su->unit = SCALE_EN; su->scale = html_strlen(p); } else if (su->scale < 0.0) diff --git a/usr.bin/mandoc/man_html.c b/usr.bin/mandoc/man_html.c index 20fd4253102..f2b98b84592 100644 --- a/usr.bin/mandoc/man_html.c +++ b/usr.bin/mandoc/man_html.c @@ -1,4 +1,4 @@ -/* $OpenBSD: man_html.c,v 1.95 2017/05/09 14:09:37 schwarze Exp $ */ +/* $OpenBSD: man_html.c,v 1.96 2017/06/08 12:54:40 schwarze Exp $ */ /* * Copyright (c) 2008-2012, 2014 Kristaps Dzonsons * Copyright (c) 2013, 2014, 2015, 2017 Ingo Schwarze @@ -356,13 +356,9 @@ fillmode(struct html *h, int want) static int a2width(const struct roff_node *n, struct roffsu *su) { - if (n->type != ROFFT_TEXT) return 0; - if (a2roffsu(n->string, su, SCALE_EN)) - return 1; - - return 0; + return a2roffsu(n->string, su, SCALE_EN) != NULL; } static void diff --git a/usr.bin/mandoc/man_term.c b/usr.bin/mandoc/man_term.c index 70d57c5d252..f43d1bd2a34 100644 --- a/usr.bin/mandoc/man_term.c +++ b/usr.bin/mandoc/man_term.c @@ -1,4 +1,4 @@ -/* $OpenBSD: man_term.c,v 1.156 2017/06/07 17:38:08 schwarze Exp $ */ +/* $OpenBSD: man_term.c,v 1.157 2017/06/08 12:54:40 schwarze Exp $ */ /* * Copyright (c) 2008-2012 Kristaps Dzonsons * Copyright (c) 2010-2015, 2017 Ingo Schwarze @@ -258,7 +258,7 @@ pre_PD(DECL_ARGS) return 0; } assert(n->type == ROFFT_TEXT); - if (a2roffsu(n->string, &su, SCALE_VS)) + if (a2roffsu(n->string, &su, SCALE_VS) != NULL) mt->pardist = term_vspan(p, &su); return 0; } @@ -372,7 +372,7 @@ pre_in(DECL_ARGS) else cp--; - if ( ! a2roffsu(++cp, &su, SCALE_EN)) + if (a2roffsu(++cp, &su, SCALE_EN) == NULL) return 0; v = (term_hspan(p, &su) + 11) / 24; @@ -423,7 +423,7 @@ pre_HP(DECL_ARGS) /* Calculate offset. */ if ((nn = n->parent->head->child) != NULL && - a2roffsu(nn->string, &su, SCALE_EN)) { + a2roffsu(nn->string, &su, SCALE_EN) != NULL) { len = term_hspan(p, &su) / 24; if (len < 0 && (size_t)(-len) > mt->offset) len = -mt->offset; @@ -508,7 +508,7 @@ pre_IP(DECL_ARGS) /* Calculate the offset from the optional second argument. */ if ((nn = n->parent->head->child) != NULL && (nn = nn->next) != NULL && - a2roffsu(nn->string, &su, SCALE_EN)) { + a2roffsu(nn->string, &su, SCALE_EN) != NULL) { len = term_hspan(p, &su) / 24; if (len < 0 && (size_t)(-len) > mt->offset) len = -mt->offset; @@ -590,7 +590,7 @@ pre_TP(DECL_ARGS) if ((nn = n->parent->head->child) != NULL && nn->string != NULL && ! (NODE_LINE & nn->flags) && - a2roffsu(nn->string, &su, SCALE_EN)) { + a2roffsu(nn->string, &su, SCALE_EN) != NULL) { len = term_hspan(p, &su) / 24; if (len < 0 && (size_t)(-len) > mt->offset) len = -mt->offset; @@ -794,7 +794,7 @@ pre_RS(DECL_ARGS) n->aux = SHRT_MAX + 1; if (n->child == NULL) n->aux = mt->lmargin[mt->lmargincur]; - else if (a2roffsu(n->child->string, &su, SCALE_EN)) + else if (a2roffsu(n->child->string, &su, SCALE_EN) != NULL) n->aux = term_hspan(p, &su) / 24; if (n->aux < 0 && (size_t)(-n->aux) > mt->offset) n->aux = -mt->offset; diff --git a/usr.bin/mandoc/mdoc_man.c b/usr.bin/mandoc/mdoc_man.c index 8e14c4c8607..25505ee32da 100644 --- a/usr.bin/mandoc/mdoc_man.c +++ b/usr.bin/mandoc/mdoc_man.c @@ -1,4 +1,4 @@ -/* $OpenBSD: mdoc_man.c,v 1.116 2017/06/06 15:00:56 schwarze Exp $ */ +/* $OpenBSD: mdoc_man.c,v 1.117 2017/06/08 12:54:40 schwarze Exp $ */ /* * Copyright (c) 2011-2017 Ingo Schwarze * @@ -474,6 +474,7 @@ print_offs(const char *v, int keywords) { char buf[24]; struct roffsu su; + const char *end; int sz; print_line(".RS", MMAN_Bk_susp); @@ -485,8 +486,11 @@ print_offs(const char *v, int keywords) sz = 6; else if (keywords && !strcmp(v, "indent-two")) sz = 12; - else if (a2roffsu(v, &su, SCALE_EN) > 1) { - if (SCALE_EN == su.unit) + else { + end = a2roffsu(v, &su, SCALE_EN); + if (end == NULL || *end != '\0') + sz = man_strlen(v); + else if (SCALE_EN == su.unit) sz = su.scale; else { /* @@ -500,8 +504,7 @@ print_offs(const char *v, int keywords) outflags |= MMAN_nl; return; } - } else - sz = man_strlen(v); + } /* * We are inside an enclosing list. @@ -523,6 +526,7 @@ print_width(const struct mdoc_bl *bl, const struct roff_node *child) { char buf[24]; struct roffsu su; + const char *end; int numeric, remain, sz, chsz; numeric = 1; @@ -531,15 +535,17 @@ print_width(const struct mdoc_bl *bl, const struct roff_node *child) /* Convert the width into a number (of characters). */ if (bl->width == NULL) sz = (bl->type == LIST_hang) ? 6 : 0; - else if (a2roffsu(bl->width, &su, SCALE_MAX) > 1) { - if (SCALE_EN == su.unit) + else { + end = a2roffsu(bl->width, &su, SCALE_MAX); + if (end == NULL || *end != '\0') + sz = man_strlen(bl->width); + else if (SCALE_EN == su.unit) sz = su.scale; else { sz = 0; numeric = 0; } - } else - sz = man_strlen(bl->width); + } /* XXX Rough estimation, might have multiple parts. */ if (bl->type == LIST_enum) diff --git a/usr.bin/mandoc/mdoc_term.c b/usr.bin/mandoc/mdoc_term.c index 2426f311c43..d91ad6ef46f 100644 --- a/usr.bin/mandoc/mdoc_term.c +++ b/usr.bin/mandoc/mdoc_term.c @@ -1,4 +1,4 @@ -/* $OpenBSD: mdoc_term.c,v 1.261 2017/06/07 17:38:08 schwarze Exp $ */ +/* $OpenBSD: mdoc_term.c,v 1.262 2017/06/08 12:54:40 schwarze Exp $ */ /* * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons * Copyright (c) 2010, 2012-2017 Ingo Schwarze @@ -531,8 +531,10 @@ static int a2width(const struct termp *p, const char *v) { struct roffsu su; + const char *end; - if (a2roffsu(v, &su, SCALE_MAX) < 2) { + end = a2roffsu(v, &su, SCALE_MAX); + if (end == NULL || *end != '\0') { SCALE_HS_INIT(&su, term_strlen(p, v)); su.scale /= term_strlen(p, "0"); } diff --git a/usr.bin/mandoc/out.c b/usr.bin/mandoc/out.c index 8d483a6f3bb..9cf5c97c4dd 100644 --- a/usr.bin/mandoc/out.c +++ b/usr.bin/mandoc/out.c @@ -1,4 +1,4 @@ -/* $OpenBSD: out.c,v 1.35 2017/05/01 20:53:58 schwarze Exp $ */ +/* $OpenBSD: out.c,v 1.36 2017/06/08 12:54:40 schwarze Exp $ */ /* * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons * Copyright (c) 2011, 2014, 2015, 2017 Ingo Schwarze @@ -38,10 +38,10 @@ static void tblcalc_number(struct rofftbl *, struct roffcol *, * 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 2 on complete success, 1 when a conversion was done, - * but there was trailing garbage, and 0 on total failure. + * 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) { char *endptr; @@ -49,7 +49,7 @@ a2roffsu(const char *src, struct roffsu *dst, enum roffscale def) dst->unit = def == SCALE_MAX ? SCALE_BU : def; dst->scale = strtod(src, &endptr); if (endptr == src) - return 0; + return NULL; switch (*endptr++) { case 'c': @@ -87,12 +87,11 @@ a2roffsu(const char *src, struct roffsu *dst, enum roffscale def) /* FALLTHROUGH */ default: if (SCALE_MAX == def) - return 0; + return NULL; dst->unit = def; break; } - - return *endptr == '\0' ? 2 : 1; + return endptr; } /* diff --git a/usr.bin/mandoc/out.h b/usr.bin/mandoc/out.h index 82645e89282..4856dfbd08a 100644 --- a/usr.bin/mandoc/out.h +++ b/usr.bin/mandoc/out.h @@ -1,6 +1,7 @@ -/* $OpenBSD: out.h,v 1.18 2015/11/07 13:57:55 schwarze Exp $ */ +/* $OpenBSD: out.h,v 1.19 2017/06/08 12:54:40 schwarze Exp $ */ /* * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons + * Copyright (c) 2014, 2017 Ingo Schwarze * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -63,6 +64,6 @@ struct rofftbl { struct tbl_span; -int a2roffsu(const char *, struct roffsu *, enum roffscale); +const char *a2roffsu(const char *, struct roffsu *, enum roffscale); void tblcalc(struct rofftbl *tbl, const struct tbl_span *, size_t); diff --git a/usr.bin/mandoc/roff_html.c b/usr.bin/mandoc/roff_html.c index 641ade8355c..32e2b158d93 100644 --- a/usr.bin/mandoc/roff_html.c +++ b/usr.bin/mandoc/roff_html.c @@ -1,4 +1,4 @@ -/* $OpenBSD: roff_html.c,v 1.7 2017/06/06 15:00:56 schwarze Exp $ */ +/* $OpenBSD: roff_html.c,v 1.8 2017/06/08 12:54:40 schwarze Exp $ */ /* * Copyright (c) 2010 Kristaps Dzonsons * Copyright (c) 2014, 2017 Ingo Schwarze @@ -83,7 +83,7 @@ roff_html_pre_sp(ROFF_HTML_ARGS) SCALE_VS_INIT(&su, 1); if ((n = n->child) != NULL) { - if (a2roffsu(n->string, &su, SCALE_VS) == 0) + if (a2roffsu(n->string, &su, SCALE_VS) == NULL) su.scale = 1.0; else if (su.scale < 0.0) su.scale = 0.0; diff --git a/usr.bin/mandoc/roff_term.c b/usr.bin/mandoc/roff_term.c index f3a97e26a3e..a22238dde13 100644 --- a/usr.bin/mandoc/roff_term.c +++ b/usr.bin/mandoc/roff_term.c @@ -1,4 +1,4 @@ -/* $OpenBSD: roff_term.c,v 1.9 2017/06/07 17:38:08 schwarze Exp $ */ +/* $OpenBSD: roff_term.c,v 1.10 2017/06/08 12:54:40 schwarze Exp $ */ /* * Copyright (c) 2010, 2014, 2015, 2017 Ingo Schwarze * @@ -157,7 +157,7 @@ roff_term_pre_sp(ROFF_TERM_ARGS) int len; if (n->child != NULL) { - if (a2roffsu(n->child->string, &su, SCALE_VS) == 0) + if (a2roffsu(n->child->string, &su, SCALE_VS) == NULL) su.scale = 1.0; len = term_vspan(p, &su); } else @@ -201,7 +201,7 @@ roff_term_pre_ti(ROFF_TERM_ARGS) } else sign = 0; - if (a2roffsu(cp, &su, SCALE_EM) == 0) + if (a2roffsu(cp, &su, SCALE_EM) == NULL) return; len = term_hspan(p, &su) / 24; diff --git a/usr.bin/mandoc/term.c b/usr.bin/mandoc/term.c index 2196eb37e67..9aa69c7befe 100644 --- a/usr.bin/mandoc/term.c +++ b/usr.bin/mandoc/term.c @@ -1,4 +1,4 @@ -/* $OpenBSD: term.c,v 1.127 2017/06/07 20:01:07 schwarze Exp $ */ +/* $OpenBSD: term.c,v 1.128 2017/06/08 12:54:40 schwarze Exp $ */ /* * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons * Copyright (c) 2010-2017 Ingo Schwarze @@ -477,7 +477,7 @@ term_word(struct termp *p, const char *word) p->flags |= (TERMP_NOSPACE | TERMP_NONEWLINE); continue; case ESCAPE_HORIZ: - if (a2roffsu(seq, &su, SCALE_EM) == 0) + if (a2roffsu(seq, &su, SCALE_EM) == NULL) continue; uc = term_hspan(p, &su) / 24; if (uc > 0) @@ -498,7 +498,7 @@ term_word(struct termp *p, const char *word) } continue; case ESCAPE_HLINE: - if (a2roffsu(seq, &su, SCALE_EM) == 0) + if ((seq = a2roffsu(seq, &su, SCALE_EM)) == NULL) continue; uc = term_hspan(p, &su) / 24; if (uc <= 0) { @@ -507,16 +507,7 @@ term_word(struct termp *p, const char *word) lsz = p->tcol->rmargin - p->tcol->offset; } else lsz = uc; - while (sz && - strchr(" %&()*+-./0123456789:<=>", *seq)) { - seq++; - sz--; - } - if (sz && strchr("cifMmnPpuv", *seq)) { - seq++; - sz--; - } - if (sz == 0) + if (*seq == '\0') uc = -1; else if (*seq == '\\') { seq++; @@ -737,7 +728,7 @@ term_setwidth(struct termp *p, const char *wstr) default: break; } - if (a2roffsu(wstr, &su, SCALE_MAX)) + if (a2roffsu(wstr, &su, SCALE_MAX) != NULL) width = term_hspan(p, &su); else iop = 0; diff --git a/usr.bin/mandoc/term_tab.c b/usr.bin/mandoc/term_tab.c index 9d9e7812a7e..326a112c7f3 100644 --- a/usr.bin/mandoc/term_tab.c +++ b/usr.bin/mandoc/term_tab.c @@ -1,4 +1,4 @@ -/* $OpenBSD: term_tab.c,v 1.1 2017/05/07 17:30:58 schwarze Exp $ */ +/* $OpenBSD: term_tab.c,v 1.2 2017/06/08 12:54:40 schwarze Exp $ */ /* * Copyright (c) 2017 Ingo Schwarze * @@ -68,7 +68,7 @@ term_tab_set(const struct termp *p, const char *arg) arg++; } else add = 0; - if (a2roffsu(arg, &su, SCALE_EM) == 0) + if (a2roffsu(arg, &su, SCALE_EM) == NULL) return; /* Select the list, and extend it if it is full. */ -- 2.20.1