From bc80934db614d28c7b53a0b013dd68c4e6234c7e Mon Sep 17 00:00:00 2001 From: Kristaps Dzonsons Date: Sat, 14 May 2011 16:06:08 +0000 Subject: [PATCH] Move roff.c's strtol into libmandoc.h for use by other parts of the code (which will come). --- libmandoc.h | 3 ++- mandoc.c | 36 +++++++++++++++++++++++++++++++++++- roff.c | 34 ++++++---------------------------- 3 files changed, 43 insertions(+), 30 deletions(-) diff --git a/libmandoc.h b/libmandoc.h index eafcb2c6..5f8379af 100644 --- a/libmandoc.h +++ b/libmandoc.h @@ -1,4 +1,4 @@ -/* $Id: libmandoc.h,v 1.20 2011/04/19 16:38:48 kristaps Exp $ */ +/* $Id: libmandoc.h,v 1.21 2011/05/14 16:06:09 kristaps Exp $ */ /* * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons * @@ -79,6 +79,7 @@ char *mandoc_normdate(struct mparse *, char *, int, int); int mandoc_eos(const char *, size_t, int); int mandoc_hyph(const char *, const char *); int mandoc_getcontrol(const char *, int *); +int mandoc_strntou(const char *, size_t, int); void mdoc_free(struct mdoc *); struct mdoc *mdoc_alloc(struct regset *, struct mparse *); diff --git a/mandoc.c b/mandoc.c index c0ffa585..249a0dea 100644 --- a/mandoc.c +++ b/mandoc.c @@ -1,4 +1,4 @@ -/* $Id: mandoc.c,v 1.49 2011/04/30 10:18:24 kristaps Exp $ */ +/* $Id: mandoc.c,v 1.50 2011/05/14 16:06:09 kristaps Exp $ */ /* * Copyright (c) 2008, 2009, 2010 Kristaps Dzonsons * Copyright (c) 2011 Ingo Schwarze @@ -23,6 +23,8 @@ #include #include +#include +#include #include #include #include @@ -686,3 +688,35 @@ mandoc_getcontrol(const char *cp, int *ppos) *ppos = pos; return(1); } + +/* + * Convert a string to a long that may not be <0. + * If the string is invalid, or is less than 0, return -1. + */ +int +mandoc_strntou(const char *p, size_t sz, int base) +{ + char buf[32]; + char *ep; + long v; + + if (sz > 31) + return(-1); + + memcpy(buf, p, sz); + buf[sz] = '\0'; + + errno = 0; + v = strtol(buf, &ep, base); + + if (buf[0] == '\0' || *ep != '\0') + return(-1); + + if ((errno == ERANGE && + (v == LONG_MAX || v == LONG_MIN)) || + (v > INT_MAX || v < 0)) + return(-1); + + return((int)v); +} + diff --git a/roff.c b/roff.c index bb612385..e36138c8 100644 --- a/roff.c +++ b/roff.c @@ -1,4 +1,4 @@ -/* $Id: roff.c,v 1.137 2011/04/24 23:51:17 schwarze Exp $ */ +/* $Id: roff.c,v 1.138 2011/05/14 16:06:08 kristaps Exp $ */ /* * Copyright (c) 2010, 2011 Kristaps Dzonsons * Copyright (c) 2010, 2011 Ingo Schwarze @@ -20,12 +20,9 @@ #endif #include -#include #include -#include #include #include -#include #include "mandoc.h" #include "libroff.h" @@ -206,7 +203,6 @@ static void roffnode_push(struct roff *, enum rofft, const char *, int, int); static void roffnode_pop(struct roff *); static enum rofft roff_parse(struct roff *, const char *, int *); -static int roff_parse_nat(const char *, unsigned int *); /* See roff_hash_find() */ #define ROFF_HASH(p) (p[0] - ASCII_LO) @@ -593,27 +589,6 @@ roff_parse(struct roff *r, const char *buf, int *pos) return(t); } - -static int -roff_parse_nat(const char *buf, unsigned int *res) -{ - char *ep; - long lval; - - errno = 0; - lval = strtol(buf, &ep, 10); - if (buf[0] == '\0' || *ep != '\0') - return(0); - if ((errno == ERANGE && - (lval == LONG_MAX || lval == LONG_MIN)) || - (lval > INT_MAX || lval < 0)) - return(0); - - *res = (unsigned int)lval; - return(1); -} - - /* ARGSUSED */ static enum rofferr roff_cblock(ROFF_ARGS) @@ -1090,6 +1065,7 @@ roff_nr(ROFF_ARGS) { const char *key; char *val; + int iv; struct reg *rg; val = *bufp + pos; @@ -1098,8 +1074,10 @@ roff_nr(ROFF_ARGS) if (0 == strcmp(key, "nS")) { rg[(int)REG_nS].set = 1; - if ( ! roff_parse_nat(val, &rg[(int)REG_nS].v.u)) - rg[(int)REG_nS].v.u = 0; + if ((iv = mandoc_strntou(val, strlen(val), 10)) >= 0) + rg[REG_nS].v.u = (unsigned)iv; + else + rg[(int)REG_nS].v.u = 0u; } return(ROFF_IGN); -- 2.47.1