source@mandoc.bsd.lv
 help / color / mirror / Atom feed
* mdocml: Use struct buf in libroff, it is very natural there and reduces
@ 2014-11-01  6:03 schwarze
  0 siblings, 0 replies; only message in thread
From: schwarze @ 2014-11-01  6:03 UTC (permalink / raw)
  To: source

Log Message:
-----------
Use struct buf in libroff, it is very natural there
and reduces the number of arguments of many functions.
While here, sprinkle some KNF.
No functional change.

Modified Files:
--------------
    mdocml:
        libmandoc.h
        read.c
        roff.c

Revision Data
-------------
Index: roff.c
===================================================================
RCS file: /usr/vhosts/mdocml.bsd.lv/cvs/mdocml/roff.c,v
retrieving revision 1.237
retrieving revision 1.238
diff -Lroff.c -Lroff.c -u -p -r1.237 -r1.238
--- roff.c
+++ roff.c
@@ -142,8 +142,7 @@ struct	roffnode {
 
 #define	ROFF_ARGS	 struct roff *r, /* parse ctx */ \
 			 enum rofft tok, /* tok of macro */ \
-			 char **bufp, /* input buffer */ \
-			 size_t *szp, /* size of input buffer */ \
+			 struct buf *buf, /* input buffer */ \
 			 int ln, /* parse line */ \
 			 int ppos, /* original pos in buffer */ \
 			 int pos, /* current pos in buffer */ \
@@ -185,8 +184,7 @@ static	enum rofferr	 roff_cond(ROFF_ARGS
 static	enum rofferr	 roff_cond_text(ROFF_ARGS);
 static	enum rofferr	 roff_cond_sub(ROFF_ARGS);
 static	enum rofferr	 roff_ds(ROFF_ARGS);
-static	enum rofferr	 roff_eqndelim(struct roff *,
-				char **, size_t *, int);
+static	enum rofferr	 roff_eqndelim(struct roff *, struct buf *, int);
 static	int		 roff_evalcond(struct roff *r, int,
 				const char *, int *);
 static	int		 roff_evalnum(struct roff *, int,
@@ -210,9 +208,8 @@ static	enum rofferr	 roff_line_ignore(RO
 static	enum rofferr	 roff_nr(ROFF_ARGS);
 static	enum rofft	 roff_parse(struct roff *, char *, int *,
 				int, int);
-static	enum rofferr	 roff_parsetext(char **, size_t *, int, int *);
-static	enum rofferr	 roff_res(struct roff *,
-				char **, size_t *, int, int);
+static	enum rofferr	 roff_parsetext(struct buf *, int, int *);
+static	enum rofferr	 roff_res(struct roff *, struct buf *, int, int);
 static	enum rofferr	 roff_rm(ROFF_ARGS);
 static	enum rofferr	 roff_rr(ROFF_ARGS);
 static	void		 roff_setstr(struct roff *,
@@ -499,7 +496,7 @@ roff_alloc(struct mparse *parse, const s
  * Also check the syntax of the remaining escape sequences.
  */
 static enum rofferr
-roff_res(struct roff *r, char **bufp, size_t *szp, int ln, int pos)
+roff_res(struct roff *r, struct buf *buf, int ln, int pos)
 {
 	char		 ubuf[24]; /* buffer to print the number */
 	const char	*start;	/* start of the string to process */
@@ -507,7 +504,7 @@ roff_res(struct roff *r, char **bufp, si
 	const char	*stnam;	/* start of the name, after "[(*" */
 	const char	*cp;	/* end of the name, e.g. before ']' */
 	const char	*res;	/* the string to be substituted */
-	char		*nbuf;	/* new buffer to copy bufp to */
+	char		*nbuf;	/* new buffer to copy buf->buf to */
 	size_t		 maxl;  /* expected length of the escape name */
 	size_t		 naml;	/* actual length of the escape name */
 	enum mandoc_esc	 esc;	/* type of the escape sequence */
@@ -518,22 +515,22 @@ roff_res(struct roff *r, char **bufp, si
 	char		 term;	/* character terminating the escape */
 
 	expand_count = 0;
-	start = *bufp + pos;
+	start = buf->buf + pos;
 	stesc = strchr(start, '\0') - 1;
 	while (stesc-- > start) {
 
 		/* Search backwards for the next backslash. */
 
-		if ('\\' != *stesc)
+		if (*stesc != '\\')
 			continue;
 
 		/* If it is escaped, skip it. */
 
 		for (cp = stesc - 1; cp >= start; cp--)
-			if ('\\' != *cp)
+			if (*cp != '\\')
 				break;
 
-		if (0 == (stesc - cp) % 2) {
+		if ((stesc - cp) % 2 == 0) {
 			stesc = (char *)cp;
 			continue;
 		}
@@ -560,14 +557,14 @@ roff_res(struct roff *r, char **bufp, si
 			    (esc == ESCAPE_SPECIAL &&
 			     mchars_spec2cp(r->mchars, stnam, inaml) < 0))
 				mandoc_vmsg(MANDOCERR_ESC_BAD,
-				    r->parse, ln, (int)(stesc - *bufp),
+				    r->parse, ln, (int)(stesc - buf->buf),
 				    "%.*s", (int)(cp - stesc), stesc);
 			continue;
 		}
 
 		if (EXPAND_LIMIT < ++expand_count) {
 			mandoc_msg(MANDOCERR_ROFFLOOP, r->parse,
-			    ln, (int)(stesc - *bufp), NULL);
+			    ln, (int)(stesc - buf->buf), NULL);
 			return(ROFF_IGN);
 		}
 
@@ -577,7 +574,7 @@ roff_res(struct roff *r, char **bufp, si
 		 * Save a pointer to the name.
 		 */
 
-		if ('\0' == term) {
+		if (term == '\0') {
 			switch (*++cp) {
 			case '\0':
 				maxl = 0;
@@ -604,14 +601,14 @@ roff_res(struct roff *r, char **bufp, si
 		/* Advance to the end of the name. */
 
 		arg_complete = 1;
-		for (naml = 0; 0 == maxl || naml < maxl; naml++, cp++) {
-			if ('\0' == *cp) {
+		for (naml = 0; maxl == 0 || naml < maxl; naml++, cp++) {
+			if (*cp == '\0') {
 				mandoc_msg(MANDOCERR_ESC_BAD, r->parse,
-				    ln, (int)(stesc - *bufp), stesc);
+				    ln, (int)(stesc - buf->buf), stesc);
 				arg_complete = 0;
 				break;
 			}
-			if (0 == maxl && *cp == term) {
+			if (maxl == 0 && *cp == term) {
 				cp++;
 				break;
 			}
@@ -648,9 +645,9 @@ roff_res(struct roff *r, char **bufp, si
 			break;
 		}
 
-		if (NULL == res) {
+		if (res == NULL) {
 			mandoc_vmsg(MANDOCERR_STR_UNDEF,
-			    r->parse, ln, (int)(stesc - *bufp),
+			    r->parse, ln, (int)(stesc - buf->buf),
 			    "%.*s", (int)naml, stnam);
 			res = "";
 		}
@@ -658,15 +655,15 @@ roff_res(struct roff *r, char **bufp, si
 		/* Replace the escape sequence by the string. */
 
 		*stesc = '\0';
-		*szp = mandoc_asprintf(&nbuf, "%s%s%s",
-		    *bufp, res, cp) + 1;
+		buf->sz = mandoc_asprintf(&nbuf, "%s%s%s",
+		    buf->buf, res, cp) + 1;
 
 		/* Prepare for the next replacement. */
 
 		start = nbuf + pos;
-		stesc = nbuf + (stesc - *bufp) + strlen(res);
-		free(*bufp);
-		*bufp = nbuf;
+		stesc = nbuf + (stesc - buf->buf) + strlen(res);
+		free(buf->buf);
+		buf->buf = nbuf;
 	}
 	return(ROFF_CONT);
 }
@@ -677,7 +674,7 @@ roff_res(struct roff *r, char **bufp, si
  * Decrement and spring input line trap.
  */
 static enum rofferr
-roff_parsetext(char **bufp, size_t *szp, int pos, int *offs)
+roff_parsetext(struct buf *buf, int pos, int *offs)
 {
 	size_t		 sz;
 	const char	*start;
@@ -685,20 +682,20 @@ roff_parsetext(char **bufp, size_t *szp,
 	int		 isz;
 	enum mandoc_esc	 esc;
 
-	start = p = *bufp + pos;
+	start = p = buf->buf + pos;
 
-	while ('\0' != *p) {
+	while (*p != '\0') {
 		sz = strcspn(p, "-\\");
 		p += sz;
 
-		if ('\0' == *p)
+		if (*p == '\0')
 			break;
 
-		if ('\\' == *p) {
+		if (*p == '\\') {
 			/* Skip over escapes. */
 			p++;
 			esc = mandoc_escape((const char **)&p, NULL, NULL);
-			if (ESCAPE_ERROR == esc)
+			if (esc == ESCAPE_ERROR)
 				break;
 			continue;
 		} else if (p == start) {
@@ -713,34 +710,37 @@ roff_parsetext(char **bufp, size_t *szp,
 	}
 
 	/* Spring the input line trap. */
-	if (1 == roffit_lines) {
-		isz = mandoc_asprintf(&p, "%s\n.%s", *bufp, roffit_macro);
-		free(*bufp);
-		*bufp = p;
-		*szp = isz + 1;
+	if (roffit_lines == 1) {
+		isz = mandoc_asprintf(&p, "%s\n.%s", buf->buf, roffit_macro);
+		free(buf->buf);
+		buf->buf = p;
+		buf->sz = isz + 1;
 		*offs = 0;
 		free(roffit_macro);
 		roffit_lines = 0;
 		return(ROFF_REPARSE);
-	} else if (1 < roffit_lines)
+	} else if (roffit_lines > 1)
 		--roffit_lines;
 	return(ROFF_CONT);
 }
 
 enum rofferr
-roff_parseln(struct roff *r, int ln, char **bufp,
-		size_t *szp, int pos, int *offs)
+roff_parseln(struct roff *r, int ln, struct buf *buf, int *offs)
 {
 	enum rofft	 t;
 	enum rofferr	 e;
-	int		 ppos, ctl;
+	int		 pos;	/* parse point */
+	int		 ppos;	/* original offset in buf->buf */
+	int		 ctl;	/* macro line (boolean) */
+
+	ppos = pos = *offs;
 
 	/* Handle in-line equation delimiters. */
 
 	if (r->tbl == NULL &&
 	    r->last_eqn != NULL && r->last_eqn->delim &&
 	    (r->eqn == NULL || r->eqn_inline)) {
-		e = roff_eqndelim(r, bufp, szp, pos);
+		e = roff_eqndelim(r, buf, pos);
 		if (e == ROFF_REPARSE)
 			return(e);
 		assert(e == ROFF_CONT);
@@ -748,13 +748,12 @@ roff_parseln(struct roff *r, int ln, cha
 
 	/* Expand some escape sequences. */
 
-	e = roff_res(r, bufp, szp, ln, pos);
-	if (ROFF_IGN == e)
+	e = roff_res(r, buf, ln, pos);
+	if (e == ROFF_IGN)
 		return(e);
-	assert(ROFF_CONT == e);
+	assert(e == ROFF_CONT);
 
-	ppos = pos;
-	ctl = roff_getcontrol(r, *bufp, &pos);
+	ctl = roff_getcontrol(r, buf->buf, &pos);
 
 	/*
 	 * First, if a scope is open and we're not a macro, pass the
@@ -767,26 +766,26 @@ roff_parseln(struct roff *r, int ln, cha
 	if (r->last && ! ctl) {
 		t = r->last->tok;
 		assert(roffs[t].text);
-		e = (*roffs[t].text)(r, t, bufp, szp, ln, pos, pos, offs);
-		assert(ROFF_IGN == e || ROFF_CONT == e);
-		if (ROFF_CONT != e)
+		e = (*roffs[t].text)(r, t, buf, ln, pos, pos, offs);
+		assert(e == ROFF_IGN || e == ROFF_CONT);
+		if (e != ROFF_CONT)
 			return(e);
 	}
 	if (r->eqn)
-		return(eqn_read(&r->eqn, ln, *bufp, ppos, offs));
+		return(eqn_read(&r->eqn, ln, buf->buf, ppos, offs));
 	if ( ! ctl) {
 		if (r->tbl)
-			return(tbl_read(r->tbl, ln, *bufp, pos));
-		return(roff_parsetext(bufp, szp, pos, offs));
+			return(tbl_read(r->tbl, ln, buf->buf, pos));
+		return(roff_parsetext(buf, pos, offs));
 	}
 
 	/* Skip empty request lines. */
 
-	if ((*bufp)[pos] == '"') {
+	if (buf->buf[pos] == '"') {
 		mandoc_msg(MANDOCERR_COMMENT_BAD, r->parse,
 		    ln, pos, NULL);
 		return(ROFF_IGN);
-	} else if ((*bufp)[pos] == '\0')
+	} else if (buf->buf[pos] == '\0')
 		return(ROFF_IGN);
 
 	/*
@@ -798,8 +797,7 @@ roff_parseln(struct roff *r, int ln, cha
 	if (r->last) {
 		t = r->last->tok;
 		assert(roffs[t].sub);
-		return((*roffs[t].sub)(r, t, bufp, szp,
-		    ln, ppos, pos, offs));
+		return((*roffs[t].sub)(r, t, buf, ln, ppos, pos, offs));
 	}
 
 	/*
@@ -808,11 +806,11 @@ roff_parseln(struct roff *r, int ln, cha
 	 * the compilers handle it.
 	 */
 
-	if (ROFF_MAX == (t = roff_parse(r, *bufp, &pos, ln, ppos)))
+	if ((t = roff_parse(r, buf->buf, &pos, ln, ppos)) == ROFF_MAX)
 		return(ROFF_CONT);
 
 	assert(roffs[t].proc);
-	return((*roffs[t].proc)(r, t, bufp, szp, ln, ppos, pos, offs));
+	return((*roffs[t].proc)(r, t, buf, ln, ppos, pos, offs));
 }
 
 void
@@ -875,7 +873,7 @@ roff_cblock(ROFF_ARGS)
 	 * ignore macro, otherwise raise a warning and just ignore it.
 	 */
 
-	if (NULL == r->last) {
+	if (r->last == NULL) {
 		mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse,
 		    ln, ppos, "..");
 		return(ROFF_IGN);
@@ -900,9 +898,9 @@ roff_cblock(ROFF_ARGS)
 		return(ROFF_IGN);
 	}
 
-	if ((*bufp)[pos])
+	if (buf->buf[pos] != '\0')
 		mandoc_vmsg(MANDOCERR_ARG_SKIP, r->parse, ln, pos,
-		    ".. %s", *bufp + pos);
+		    ".. %s", buf->buf + pos);
 
 	roffnode_pop(r);
 	roffnode_cleanscope(r);
@@ -964,15 +962,15 @@ roff_block(ROFF_ARGS)
 
 	/* Ignore groff compatibility mode for now. */
 
-	if (ROFF_de1 == tok)
+	if (tok == ROFF_de1)
 		tok = ROFF_de;
-	else if (ROFF_am1 == tok)
+	else if (tok == ROFF_am1)
 		tok = ROFF_am;
 
 	/* Parse the macro name argument. */
 
-	cp = *bufp + pos;
-	if (ROFF_ig == tok) {
+	cp = buf->buf + pos;
+	if (tok == ROFF_ig) {
 		iname = NULL;
 		namesz = 0;
 	} else {
@@ -983,10 +981,10 @@ roff_block(ROFF_ARGS)
 
 	/* Resolve the macro name argument if it is indirect. */
 
-	if (namesz && (ROFF_dei == tok || ROFF_ami == tok)) {
-		if (NULL == (name = roff_getstrn(r, iname, namesz))) {
+	if (namesz && (tok == ROFF_dei || tok == ROFF_ami)) {
+		if ((name = roff_getstrn(r, iname, namesz)) == NULL) {
 			mandoc_vmsg(MANDOCERR_STR_UNDEF,
-			    r->parse, ln, (int)(iname - *bufp),
+			    r->parse, ln, (int)(iname - buf->buf),
 			    "%.*s", (int)namesz, iname);
 			namesz = 0;
 		} else
@@ -994,7 +992,7 @@ roff_block(ROFF_ARGS)
 	} else
 		name = iname;
 
-	if (0 == namesz && ROFF_ig != tok) {
+	if (namesz == 0 && tok != ROFF_ig) {
 		mandoc_msg(MANDOCERR_REQ_EMPTY, r->parse,
 		    ln, ppos, roffs[tok].name);
 		return(ROFF_IGN);
@@ -1008,10 +1006,10 @@ roff_block(ROFF_ARGS)
 	 * appended from roff_block_text() in multiline mode.
 	 */
 
-	if (ROFF_de == tok || ROFF_dei == tok)
+	if (tok == ROFF_de || tok == ROFF_dei)
 		roff_setstrn(&r->strtab, name, namesz, "", 0, 0);
 
-	if ('\0' == *cp)
+	if (*cp == '\0')
 		return(ROFF_IGN);
 
 	/* Get the custom end marker. */
@@ -1021,10 +1019,10 @@ roff_block(ROFF_ARGS)
 
 	/* Resolve the end marker if it is indirect. */
 
-	if (namesz && (ROFF_dei == tok || ROFF_ami == tok)) {
-		if (NULL == (name = roff_getstrn(r, iname, namesz))) {
+	if (namesz && (tok == ROFF_dei || tok == ROFF_ami)) {
+		if ((name = roff_getstrn(r, iname, namesz)) == NULL) {
 			mandoc_vmsg(MANDOCERR_STR_UNDEF,
-			    r->parse, ln, (int)(iname - *bufp),
+			    r->parse, ln, (int)(iname - buf->buf),
 			    "%.*s", (int)namesz, iname);
 			namesz = 0;
 		} else
@@ -1035,7 +1033,7 @@ roff_block(ROFF_ARGS)
 	if (namesz)
 		r->last->end = mandoc_strndup(name, namesz);
 
-	if ('\0' != *cp)
+	if (*cp != '\0')
 		mandoc_vmsg(MANDOCERR_ARG_EXCESS, r->parse,
 		    ln, pos, ".%s ... %s", roffs[tok].name, cp);
 
@@ -1059,21 +1057,22 @@ roff_block_sub(ROFF_ARGS)
 
 	if (r->last->end) {
 		for (i = pos, j = 0; r->last->end[j]; j++, i++)
-			if ((*bufp)[i] != r->last->end[j])
+			if (buf->buf[i] != r->last->end[j])
 				break;
 
-		if ('\0' == r->last->end[j] &&
-		    ('\0' == (*bufp)[i] ||
-		     ' '  == (*bufp)[i] ||
-		     '\t' == (*bufp)[i])) {
+		if (r->last->end[j] == '\0' &&
+		    (buf->buf[i] == '\0' ||
+		     buf->buf[i] == ' ' ||
+		     buf->buf[i] == '\t')) {
 			roffnode_pop(r);
 			roffnode_cleanscope(r);
 
-			while (' ' == (*bufp)[i] || '\t' == (*bufp)[i])
+			while (buf->buf[i] == ' ' || buf->buf[i] == '\t')
 				i++;
 
 			pos = i;
-			if (ROFF_MAX != roff_parse(r, *bufp, &pos, ln, ppos))
+			if (roff_parse(r, buf->buf, &pos, ln, ppos) !=
+			    ROFF_MAX)
 				return(ROFF_RERUN);
 			return(ROFF_IGN);
 		}
@@ -1084,24 +1083,24 @@ roff_block_sub(ROFF_ARGS)
 	 * pulling it out of the hashtable.
 	 */
 
-	t = roff_parse(r, *bufp, &pos, ln, ppos);
+	t = roff_parse(r, buf->buf, &pos, ln, ppos);
 
-	if (ROFF_cblock != t) {
-		if (ROFF_ig != tok)
-			roff_setstr(r, r->last->name, *bufp + ppos, 2);
+	if (t != ROFF_cblock) {
+		if (tok != ROFF_ig)
+			roff_setstr(r, r->last->name, buf->buf + ppos, 2);
 		return(ROFF_IGN);
 	}
 
 	assert(roffs[t].proc);
-	return((*roffs[t].proc)(r, t, bufp, szp, ln, ppos, pos, offs));
+	return((*roffs[t].proc)(r, t, buf, ln, ppos, pos, offs));
 }
 
 static enum rofferr
 roff_block_text(ROFF_ARGS)
 {
 
-	if (ROFF_ig != tok)
-		roff_setstr(r, r->last->name, *bufp + pos, 2);
+	if (tok != ROFF_ig)
+		roff_setstr(r, r->last->name, buf->buf + pos, 2);
 
 	return(ROFF_IGN);
 }
@@ -1115,18 +1114,17 @@ roff_cond_sub(ROFF_ARGS)
 
 	rr = r->last->rule;
 	roffnode_cleanscope(r);
-	t = roff_parse(r, *bufp, &pos, ln, ppos);
+	t = roff_parse(r, buf->buf, &pos, ln, ppos);
 
 	/*
 	 * Fully handle known macros when they are structurally
 	 * required or when the conditional evaluated to true.
 	 */
 
-	if ((ROFF_MAX != t) &&
-	    (rr || ROFFMAC_STRUCT & roffs[t].flags)) {
+	if ((t != ROFF_MAX) &&
+	    (rr || roffs[t].flags & ROFFMAC_STRUCT)) {
 		assert(roffs[t].proc);
-		return((*roffs[t].proc)(r, t, bufp, szp,
-		    ln, ppos, pos, offs));
+		return((*roffs[t].proc)(r, t, buf, ln, ppos, pos, offs));
 	}
 
 	/*
@@ -1134,16 +1132,16 @@ roff_cond_sub(ROFF_ARGS)
 	 * drop the line completely.
 	 */
 
-	ep = *bufp + pos;
-	if ('\\' == ep[0] && '}' == ep[1])
+	ep = buf->buf + pos;
+	if (ep[0] == '\\' && ep[1] == '}')
 		rr = 0;
 
 	/* Always check for the closing delimiter `\}'. */
 
-	while (NULL != (ep = strchr(ep, '\\'))) {
-		if ('}' == *(++ep)) {
+	while ((ep = strchr(ep, '\\')) != NULL) {
+		if (*(++ep) == '}') {
 			*ep = '&';
-			roff_ccond(r, ln, ep - *bufp - 1);
+			roff_ccond(r, ln, ep - buf->buf - 1);
 		}
 		++ep;
 	}
@@ -1159,11 +1157,11 @@ roff_cond_text(ROFF_ARGS)
 	rr = r->last->rule;
 	roffnode_cleanscope(r);
 
-	ep = *bufp + pos;
-	while (NULL != (ep = strchr(ep, '\\'))) {
-		if ('}' == *(++ep)) {
+	ep = buf->buf + pos;
+	while ((ep = strchr(ep, '\\')) != NULL) {
+		if (*(++ep) == '}') {
 			*ep = '&';
-			roff_ccond(r, ln, ep - *bufp - 1);
+			roff_ccond(r, ln, ep - buf->buf - 1);
 		}
 		++ep;
 	}
@@ -1307,16 +1305,16 @@ roff_cond(ROFF_ARGS)
 	 * If we're not an `el', however, then evaluate the conditional.
 	 */
 
-	r->last->rule = ROFF_el == tok ?
+	r->last->rule = tok == ROFF_el ?
 	    (r->rstackpos < 0 ? 0 : r->rstack[r->rstackpos--]) :
-	    roff_evalcond(r, ln, *bufp, &pos);
+	    roff_evalcond(r, ln, buf->buf, &pos);
 
 	/*
 	 * An if-else will put the NEGATION of the current evaluated
 	 * conditional into the stack of rules.
 	 */
 
-	if (ROFF_ie == tok) {
+	if (tok == ROFF_ie) {
 		if (r->rstackpos + 1 == r->rstacksz) {
 			r->rstacksz += 16;
 			r->rstack = mandoc_reallocarray(r->rstack,
@@ -1336,17 +1334,17 @@ roff_cond(ROFF_ARGS)
 	 * not even whitespace, use next-line scope.
 	 */
 
-	if ('\0' == (*bufp)[pos]) {
+	if (buf->buf[pos] == '\0') {
 		r->last->endspan = 2;
 		goto out;
 	}
 
-	while (' ' == (*bufp)[pos])
+	while (buf->buf[pos] == ' ')
 		pos++;
 
 	/* An opening brace requests multiline scope. */
 
-	if ('\\' == (*bufp)[pos] && '{' == (*bufp)[pos + 1]) {
+	if (buf->buf[pos] == '\\' && buf->buf[pos + 1] == '{') {
 		r->last->endspan = -1;
 		pos += 2;
 		goto out;
@@ -1358,7 +1356,7 @@ roff_cond(ROFF_ARGS)
 	 * nothing but trailing whitespace.
 	 */
 
-	if ('\0' == (*bufp)[pos])
+	if (buf->buf[pos] == '\0')
 		mandoc_msg(MANDOCERR_COND_EMPTY, r->parse,
 		    ln, ppos, roffs[tok].name);
 
@@ -1382,16 +1380,16 @@ roff_ds(ROFF_ARGS)
 	 * abort the `ds' request without defining anything.
 	 */
 
-	name = string = *bufp + pos;
-	if ('\0' == *name)
+	name = string = buf->buf + pos;
+	if (*name == '\0')
 		return(ROFF_IGN);
 
 	namesz = roff_getname(r, &string, ln, pos);
-	if ('\\' == name[namesz])
+	if (name[namesz] == '\\')
 		return(ROFF_IGN);
 
 	/* Read past the initial double-quote, if any. */
-	if ('"' == *string)
+	if (*string == '"')
 		string++;
 
 	/* The rest is the value. */
@@ -1723,17 +1721,17 @@ roff_nr(ROFF_ARGS)
 	int		 iv;
 	char		 sign;
 
-	key = val = *bufp + pos;
-	if ('\0' == *key)
+	key = val = buf->buf + pos;
+	if (*key == '\0')
 		return(ROFF_IGN);
 
 	keysz = roff_getname(r, &val, ln, pos);
-	if ('\\' == key[keysz])
+	if (key[keysz] == '\\')
 		return(ROFF_IGN);
 	key[keysz] = '\0';
 
 	sign = *val;
-	if ('+' == sign || '-' == sign)
+	if (sign == '+' || sign == '-')
 		val++;
 
 	if (roff_evalnum(r, ln, val, NULL, &iv, 0))
@@ -1749,8 +1747,8 @@ roff_rr(ROFF_ARGS)
 	char		*name, *cp;
 	size_t		 namesz;
 
-	name = cp = *bufp + pos;
-	if ('\0' == *name)
+	name = cp = buf->buf + pos;
+	if (*name == '\0')
 		return(ROFF_IGN);
 	namesz = roff_getname(r, &cp, ln, pos);
 	name[namesz] = '\0';
@@ -1758,11 +1756,11 @@ roff_rr(ROFF_ARGS)
 	prev = &r->regtab;
 	while (1) {
 		reg = *prev;
-		if (NULL == reg || !strcmp(name, reg->key.p))
+		if (reg == NULL || !strcmp(name, reg->key.p))
 			break;
 		prev = &reg->next;
 	}
-	if (NULL != reg) {
+	if (reg != NULL) {
 		*prev = reg->next;
 		free(reg->key.p);
 		free(reg);
@@ -1777,12 +1775,12 @@ roff_rm(ROFF_ARGS)
 	char		 *cp;
 	size_t		  namesz;
 
-	cp = *bufp + pos;
-	while ('\0' != *cp) {
+	cp = buf->buf + pos;
+	while (*cp != '\0') {
 		name = cp;
-		namesz = roff_getname(r, &cp, ln, (int)(cp - *bufp));
+		namesz = roff_getname(r, &cp, ln, (int)(cp - buf->buf));
 		roff_setstrn(&r->strtab, name, namesz, NULL, 0, 0);
-		if ('\\' == name[namesz])
+		if (name[namesz] == '\\')
 			break;
 	}
 	return(ROFF_IGN);
@@ -1796,12 +1794,12 @@ roff_it(ROFF_ARGS)
 	int		 iv;
 
 	/* Parse the number of lines. */
-	cp = *bufp + pos;
+	cp = buf->buf + pos;
 	len = strcspn(cp, " \t");
 	cp[len] = '\0';
 	if ((iv = mandoc_strntoi(cp, len, 10)) <= 0) {
 		mandoc_msg(MANDOCERR_IT_NONUM, r->parse,
-		    ln, ppos, *bufp + 1);
+		    ln, ppos, buf->buf + 1);
 		return(ROFF_IGN);
 	}
 	cp += len + 1;
@@ -1872,7 +1870,7 @@ roff_T_(ROFF_ARGS)
  * Handle in-line equation delimiters.
  */
 static enum rofferr
-roff_eqndelim(struct roff *r, char **bufp, size_t *szp, int pos)
+roff_eqndelim(struct roff *r, struct buf *buf, int pos)
 {
 	char		*cp1, *cp2;
 	const char	*bef_pr, *bef_nl, *mac, *aft_nl, *aft_pr;
@@ -1884,7 +1882,7 @@ roff_eqndelim(struct roff *r, char **buf
 	 * so look for a closing delimiter.
 	 */
 
-	cp1 = *bufp + pos;
+	cp1 = buf->buf + pos;
 	cp2 = strchr(cp1, r->eqn == NULL ?
 	    r->last_eqn->odelim : r->last_eqn->cdelim);
 	if (cp2 == NULL)
@@ -1895,7 +1893,7 @@ roff_eqndelim(struct roff *r, char **buf
 
 	/* Handle preceding text, protecting whitespace. */
 
-	if (**bufp != '\0') {
+	if (*buf->buf != '\0') {
 		if (r->eqn == NULL)
 			bef_pr = "\\&";
 		bef_nl = "\n";
@@ -1923,10 +1921,10 @@ roff_eqndelim(struct roff *r, char **buf
 
 	/* Do the actual replacement. */
 
-	*szp = mandoc_asprintf(&cp1, "%s%s%s%s%s%s%s", *bufp,
+	buf->sz = mandoc_asprintf(&cp1, "%s%s%s%s%s%s%s", buf->buf,
 	    bef_pr, bef_nl, mac, aft_nl, aft_pr, cp2) + 1;
-	free(*bufp);
-	*bufp = cp1;
+	free(buf->buf);
+	buf->buf = cp1;
 
 	/* Toggle the in-line state of the eqn subsystem. */
 
@@ -1939,7 +1937,7 @@ roff_EQ(ROFF_ARGS)
 {
 	struct eqn_node *e;
 
-	assert(NULL == r->eqn);
+	assert(r->eqn == NULL);
 	e = eqn_alloc(ppos, ln, r->parse);
 
 	if (r->last_eqn) {
@@ -1952,9 +1950,9 @@ roff_EQ(ROFF_ARGS)
 
 	r->eqn = r->last_eqn = e;
 
-	if ((*bufp)[pos])
+	if (buf->buf[pos] != '\0')
 		mandoc_vmsg(MANDOCERR_ARG_SKIP, r->parse, ln, pos,
-		    ".EQ %s", *bufp + pos);
+		    ".EQ %s", buf->buf + pos);
 
 	return(ROFF_IGN);
 }
@@ -1994,12 +1992,12 @@ roff_cc(ROFF_ARGS)
 {
 	const char	*p;
 
-	p = *bufp + pos;
+	p = buf->buf + pos;
 
-	if ('\0' == *p || '.' == (r->control = *p++))
+	if (*p == '\0' || (r->control = *p++) == '.')
 		r->control = 0;
 
-	if ('\0' != *p)
+	if (*p != '\0')
 		mandoc_msg(MANDOCERR_ARGCOUNT, r->parse, ln, ppos, NULL);
 
 	return(ROFF_IGN);
@@ -2012,39 +2010,39 @@ roff_tr(ROFF_ARGS)
 	size_t		 fsz, ssz;
 	enum mandoc_esc	 esc;
 
-	p = *bufp + pos;
+	p = buf->buf + pos;
 
-	if ('\0' == *p) {
+	if (*p == '\0') {
 		mandoc_msg(MANDOCERR_ARGCOUNT, r->parse, ln, ppos, NULL);
 		return(ROFF_IGN);
 	}
 
-	while ('\0' != *p) {
+	while (*p != '\0') {
 		fsz = ssz = 1;
 
 		first = p++;
-		if ('\\' == *first) {
+		if (*first == '\\') {
 			esc = mandoc_escape(&p, NULL, NULL);
-			if (ESCAPE_ERROR == esc) {
+			if (esc == ESCAPE_ERROR) {
 				mandoc_msg(MANDOCERR_ESC_BAD, r->parse,
-				    ln, (int)(p - *bufp), first);
+				    ln, (int)(p - buf->buf), first);
 				return(ROFF_IGN);
 			}
 			fsz = (size_t)(p - first);
 		}
 
 		second = p++;
-		if ('\\' == *second) {
+		if (*second == '\\') {
 			esc = mandoc_escape(&p, NULL, NULL);
-			if (ESCAPE_ERROR == esc) {
+			if (esc == ESCAPE_ERROR) {
 				mandoc_msg(MANDOCERR_ESC_BAD, r->parse,
-				    ln, (int)(p - *bufp), second);
+				    ln, (int)(p - buf->buf), second);
 				return(ROFF_IGN);
 			}
 			ssz = (size_t)(p - second);
-		} else if ('\0' == *second) {
+		} else if (*second == '\0') {
 			mandoc_msg(MANDOCERR_ARGCOUNT, r->parse,
-			    ln, (int)(p - *bufp), NULL);
+			    ln, (int)(p - buf->buf), NULL);
 			second = " ";
 			p--;
 		}
@@ -2055,7 +2053,7 @@ roff_tr(ROFF_ARGS)
 			continue;
 		}
 
-		if (NULL == r->xtab)
+		if (r->xtab == NULL)
 			r->xtab = mandoc_calloc(128,
 			    sizeof(struct roffstr));
 
@@ -2072,7 +2070,7 @@ roff_so(ROFF_ARGS)
 {
 	char *name;
 
-	name = *bufp + pos;
+	name = buf->buf + pos;
 	mandoc_vmsg(MANDOCERR_SO, r->parse, ln, ppos, "so %s", name);
 
 	/*
@@ -2082,7 +2080,7 @@ roff_so(ROFF_ARGS)
 	 * or using absolute paths.
 	 */
 
-	if ('/' == *name || strstr(name, "../") || strstr(name, "/..")) {
+	if (*name == '/' || strstr(name, "../") || strstr(name, "/..")) {
 		mandoc_vmsg(MANDOCERR_SO_PATH, r->parse, ln, ppos,
 		    ".so %s", name);
 		return(ROFF_ERR);
@@ -2103,17 +2101,17 @@ roff_userdef(ROFF_ARGS)
 	 * Collect pointers to macro argument strings
 	 * and NUL-terminate them.
 	 */
-	cp = *bufp + pos;
+	cp = buf->buf + pos;
 	for (i = 0; i < 9; i++)
-		arg[i] = '\0' == *cp ? "" :
+		arg[i] = *cp == '\0' ? "" :
 		    mandoc_getarg(r->parse, &cp, ln, &pos);
 
 	/*
 	 * Expand macro arguments.
 	 */
-	*szp = 0;
+	buf->sz = 0;
 	n1 = cp = mandoc_strdup(r->current_string);
-	while (NULL != (cp = strstr(cp, "\\$"))) {
+	while ((cp = strstr(cp, "\\$")) != NULL) {
 		i = cp[2] - '1';
 		if (0 > i || 8 < i) {
 			/* Not an argument invocation. */
@@ -2121,7 +2119,7 @@ roff_userdef(ROFF_ARGS)
 			continue;
 		}
 		*cp = '\0';
-		*szp = mandoc_asprintf(&n2, "%s%s%s",
+		buf->sz = mandoc_asprintf(&n2, "%s%s%s",
 		    n1, arg[i], cp + 3) + 1;
 		cp = n2 + (cp - n1);
 		free(n1);
@@ -2132,12 +2130,12 @@ roff_userdef(ROFF_ARGS)
 	 * Replace the macro invocation
 	 * by the expanded macro.
 	 */
-	free(*bufp);
-	*bufp = n1;
-	if (0 == *szp)
-		*szp = strlen(*bufp) + 1;
+	free(buf->buf);
+	buf->buf = n1;
+	if (buf->sz == 0)
+		buf->sz = strlen(buf->buf) + 1;
 
-	return(*szp > 1 && '\n' == (*bufp)[(int)*szp - 2] ?
+	return(buf->sz > 1 && buf->buf[buf->sz - 2] == '\n' ?
 	   ROFF_REPARSE : ROFF_APPEND);
 }
 
Index: read.c
===================================================================
RCS file: /usr/vhosts/mdocml.bsd.lv/cvs/mdocml/read.c,v
retrieving revision 1.95
retrieving revision 1.96
diff -Lread.c -Lread.c -u -p -r1.95 -r1.96
--- read.c
+++ read.c
@@ -493,8 +493,7 @@ mparse_buf_r(struct mparse *curp, struct
 				[curp->secondary->sz] = '\0';
 		}
 rerun:
-		rr = roff_parseln(curp->roff, curp->line,
-		    &ln.buf, &ln.sz, of, &of);
+		rr = roff_parseln(curp->roff, curp->line, &ln, &of);
 
 		switch (rr) {
 		case ROFF_REPARSE:
Index: libmandoc.h
===================================================================
RCS file: /usr/vhosts/mdocml.bsd.lv/cvs/mdocml/libmandoc.h,v
retrieving revision 1.47
retrieving revision 1.48
diff -Llibmandoc.h -Llibmandoc.h -u -p -r1.47 -r1.48
--- libmandoc.h
+++ libmandoc.h
@@ -78,8 +78,7 @@ int		 preconv_encode(struct buf *, size_
 void		 roff_free(struct roff *);
 struct roff	*roff_alloc(struct mparse *, const struct mchars *, int);
 void		 roff_reset(struct roff *);
-enum rofferr	 roff_parseln(struct roff *, int,
-			char **, size_t *, int, int *);
+enum rofferr	 roff_parseln(struct roff *, int, struct buf *, int *);
 void		 roff_endparse(struct roff *);
 void		 roff_setreg(struct roff *, const char *, int, char sign);
 int		 roff_getreg(const struct roff *, const char *);
--
 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:[~2014-11-01  6:03 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-11-01  6:03 mdocml: Use struct buf in libroff, it is very natural there and reduces 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).