* mdocml: make the internal a2roffsu() interface more powerful by
@ 2017-06-08 12:55 schwarze
0 siblings, 0 replies; only message in thread
From: schwarze @ 2017-06-08 12:55 UTC (permalink / raw)
To: source
Log Message:
-----------
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
Modified Files:
--------------
mdocml:
html.c
man_html.c
man_term.c
mdoc_man.c
mdoc_term.c
out.c
out.h
roff_html.c
roff_term.c
term.c
term_tab.c
Revision Data
-------------
Index: mdoc_term.c
===================================================================
RCS file: /home/cvs/mdocml/mdocml/mdoc_term.c,v
retrieving revision 1.362
retrieving revision 1.363
diff -Lmdoc_term.c -Lmdoc_term.c -u -p -r1.362 -r1.363
--- mdoc_term.c
+++ mdoc_term.c
@@ -533,8 +533,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");
}
Index: man_html.c
===================================================================
RCS file: /home/cvs/mdocml/mdocml/man_html.c,v
retrieving revision 1.142
retrieving revision 1.143
diff -Lman_html.c -Lman_html.c -u -p -r1.142 -r1.143
--- man_html.c
+++ man_html.c
@@ -358,13 +358,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
Index: out.h
===================================================================
RCS file: /home/cvs/mdocml/mdocml/out.h,v
retrieving revision 1.27
retrieving revision 1.28
diff -Lout.h -Lout.h -u -p -r1.27 -r1.28
--- out.h
+++ out.h
@@ -1,6 +1,7 @@
/* $Id$ */
/*
* Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
+ * Copyright (c) 2014, 2017 Ingo Schwarze <schwarze@openbsd.org>
*
* 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);
Index: term.c
===================================================================
RCS file: /home/cvs/mdocml/mdocml/term.c,v
retrieving revision 1.267
retrieving revision 1.268
diff -Lterm.c -Lterm.c -u -p -r1.267 -r1.268
--- term.c
+++ term.c
@@ -479,7 +479,7 @@ term_word(struct termp *p, const char *w
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)
@@ -500,7 +500,7 @@ term_word(struct termp *p, const char *w
}
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) {
@@ -509,16 +509,7 @@ term_word(struct termp *p, const char *w
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++;
@@ -739,7 +730,7 @@ term_setwidth(struct termp *p, const cha
default:
break;
}
- if (a2roffsu(wstr, &su, SCALE_MAX))
+ if (a2roffsu(wstr, &su, SCALE_MAX) != NULL)
width = term_hspan(p, &su);
else
iop = 0;
Index: term_tab.c
===================================================================
RCS file: /home/cvs/mdocml/mdocml/term_tab.c,v
retrieving revision 1.1
retrieving revision 1.2
diff -Lterm_tab.c -Lterm_tab.c -u -p -r1.1 -r1.2
--- term_tab.c
+++ term_tab.c
@@ -68,7 +68,7 @@ term_tab_set(const struct termp *p, cons
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. */
Index: out.c
===================================================================
RCS file: /home/cvs/mdocml/mdocml/out.c,v
retrieving revision 1.63
retrieving revision 1.64
diff -Lout.c -Lout.c -u -p -r1.63 -r1.64
--- out.c
+++ out.c
@@ -40,10 +40,10 @@ static void tblcalc_number(struct rofftb
* 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;
@@ -51,7 +51,7 @@ a2roffsu(const char *src, struct roffsu
dst->unit = def == SCALE_MAX ? SCALE_BU : def;
dst->scale = strtod(src, &endptr);
if (endptr == src)
- return 0;
+ return NULL;
switch (*endptr++) {
case 'c':
@@ -89,12 +89,11 @@ a2roffsu(const char *src, struct roffsu
/* FALLTHROUGH */
default:
if (SCALE_MAX == def)
- return 0;
+ return NULL;
dst->unit = def;
break;
}
-
- return *endptr == '\0' ? 2 : 1;
+ return endptr;
}
/*
Index: html.c
===================================================================
RCS file: /home/cvs/mdocml/mdocml/html.c,v
retrieving revision 1.212
retrieving revision 1.213
diff -Lhtml.c -Lhtml.c -u -p -r1.212 -r1.213
--- html.c
+++ html.c
@@ -950,7 +950,10 @@ print_word(struct html *h, const char *c
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)
Index: roff_html.c
===================================================================
RCS file: /home/cvs/mdocml/mdocml/roff_html.c,v
retrieving revision 1.7
retrieving revision 1.8
diff -Lroff_html.c -Lroff_html.c -u -p -r1.7 -r1.8
--- roff_html.c
+++ roff_html.c
@@ -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;
Index: man_term.c
===================================================================
RCS file: /home/cvs/mdocml/mdocml/man_term.c,v
retrieving revision 1.203
retrieving revision 1.204
diff -Lman_term.c -Lman_term.c -u -p -r1.203 -r1.204
--- man_term.c
+++ man_term.c
@@ -260,7 +260,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;
}
@@ -374,7 +374,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;
@@ -425,7 +425,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;
@@ -510,7 +510,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;
@@ -592,7 +592,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;
@@ -796,7 +796,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;
Index: mdoc_man.c
===================================================================
RCS file: /home/cvs/mdocml/mdocml/mdoc_man.c,v
retrieving revision 1.118
retrieving revision 1.119
diff -Lmdoc_man.c -Lmdoc_man.c -u -p -r1.118 -r1.119
--- mdoc_man.c
+++ mdoc_man.c
@@ -476,6 +476,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);
@@ -487,8 +488,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 {
/*
@@ -502,8 +506,7 @@ print_offs(const char *v, int keywords)
outflags |= MMAN_nl;
return;
}
- } else
- sz = man_strlen(v);
+ }
/*
* We are inside an enclosing list.
@@ -525,6 +528,7 @@ print_width(const struct mdoc_bl *bl, co
{
char buf[24];
struct roffsu su;
+ const char *end;
int numeric, remain, sz, chsz;
numeric = 1;
@@ -533,15 +537,17 @@ print_width(const struct mdoc_bl *bl, co
/* 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)
Index: roff_term.c
===================================================================
RCS file: /home/cvs/mdocml/mdocml/roff_term.c,v
retrieving revision 1.9
retrieving revision 1.10
diff -Lroff_term.c -Lroff_term.c -u -p -r1.9 -r1.10
--- roff_term.c
+++ roff_term.c
@@ -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;
--
To unsubscribe send an email to source+unsubscribe@mdocml.bsd.lv
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2017-06-08 12:55 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-06-08 12:55 mdocml: make the internal a2roffsu() interface more powerful by schwarze
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).