From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp1.rz.uni-karlsruhe.de (Debian-exim@smtp1.rz.uni-karlsruhe.de [129.13.185.217]) by krisdoz.my.domain (8.14.3/8.14.3) with ESMTP id p0AMqFlP018572 for ; Mon, 10 Jan 2011 17:52:16 -0500 (EST) Received: from hekate.usta.de (asta-nat.asta.uni-karlsruhe.de [172.22.63.82]) by smtp1.rz.uni-karlsruhe.de with esmtp (Exim 4.63 #1) id 1PcQb0-000571-HP; Mon, 10 Jan 2011 23:52:14 +0100 Received: from donnerwolke.usta.de ([172.24.96.3]) by hekate.usta.de with esmtp (Exim 4.72) (envelope-from ) id 1PcQb0-00062o-Fz for tech@mdocml.bsd.lv; Mon, 10 Jan 2011 23:52:14 +0100 Received: from iris.usta.de ([172.24.96.5] helo=usta.de) by donnerwolke.usta.de with esmtp (Exim 4.69) (envelope-from ) id 1PcQb0-0006js-FF for tech@mdocml.bsd.lv; Mon, 10 Jan 2011 23:52:14 +0100 Received: from schwarze by usta.de with local (Exim 4.72) (envelope-from ) id 1PcQb0-0006qU-5e for tech@mdocml.bsd.lv; Mon, 10 Jan 2011 23:52:14 +0100 Date: Mon, 10 Jan 2011 23:52:13 +0100 From: Ingo Schwarze To: tech@mdocml.bsd.lv Subject: [PATCH] uniform parsing of names in libroff Message-ID: <20110110225213.GA4964@iris.usta.de> X-Mailinglist: mdocml-tech Reply-To: tech@mdocml.bsd.lv MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.5.21 (2010-09-15) Hi, i just started to implement .rm (remove macro) in libroff, then realized that i would be introducing the third place where names (that is, to be precise, roff macro and string names) get parsed. The following patch moves this parsing into its own function, to be soon reused by roff_rm(). I put that function roff_getname() in libroff (and not in libmandoc) because i do not expect any macro package to deal with that kind of low-level stuff. The interface is similar to mandoc_getarg(), but much simpler, because quotes count as normal characters in this context, space characters cannot be escaped, and backslashes are only allowed in pairs. Yes, parsing of names has its own rules, and they are quite different from the rules for parsing of arguments. See, this is roff, right? As a bonus: - Both roff_ds and roff_nr become much shorter. - We get an ERROR when there are escapes in a name. OK? Yours, Ingo P.S. Maybe we can use this for roff_block() as well, but that is not as straighforward, so i haven't done it right away. Index: main.c =================================================================== RCS file: /cvs/src/usr.bin/mandoc/main.c,v retrieving revision 1.65 diff -u -p -r1.65 main.c --- main.c 9 Jan 2011 13:16:48 -0000 1.65 +++ main.c 10 Jan 2011 22:29:54 -0000 @@ -179,6 +179,7 @@ static const char * const mandocerrs[MAN "input stack limit exceeded, infinite loop?", "skipping bad character", + "escaped character not allowed in a name", "skipping text before the first section header", "skipping unknown macro", "NOT IMPLEMENTED: skipping request", Index: mandoc.h =================================================================== RCS file: /cvs/src/usr.bin/mandoc/mandoc.h,v retrieving revision 1.28 diff -u -p -r1.28 mandoc.h --- mandoc.h 9 Jan 2011 14:30:48 -0000 1.28 +++ mandoc.h 10 Jan 2011 22:29:54 -0000 @@ -116,6 +116,7 @@ enum mandocerr { MANDOCERR_ROFFLOOP, /* input stack limit exceeded, infinite loop? */ MANDOCERR_BADCHAR, /* skipping bad character */ + MANDOCERR_NAMESC, /* escaped character not allowed in a name */ MANDOCERR_NOTEXT, /* skipping text before the first section header */ MANDOCERR_MACRO, /* skipping unknown macro */ MANDOCERR_REQUEST, /* NOT IMPLEMENTED: skipping request */ Index: roff.c =================================================================== RCS file: /cvs/src/usr.bin/mandoc/roff.c,v retrieving revision 1.27 diff -u -p -r1.27 roff.c --- roff.c 4 Jan 2011 22:28:17 -0000 1.27 +++ roff.c 10 Jan 2011 22:29:54 -0000 @@ -130,6 +130,7 @@ static enum rofferr roff_cond_sub(ROFF_ static enum rofferr roff_ds(ROFF_ARGS); static enum roffrule roff_evalcond(const char *, int *); static void roff_freestr(struct roff *); +static char *roff_getname(struct roff *, char **, int, int); static const char *roff_getstrn(const struct roff *, const char *, size_t); static enum rofferr roff_line_ignore(ROFF_ARGS); @@ -1052,25 +1053,13 @@ roff_ds(ROFF_ARGS) * will have `bar " ' as its value. */ - name = *bufp + pos; + string = *bufp + pos; + name = roff_getname(r, &string, ln, pos); if ('\0' == *name) return(ROFF_IGN); - string = name; - /* Read until end of name. */ - while (*string && ' ' != *string) - string++; - - /* Nil-terminate name. */ - if (*string) - *(string++) = '\0'; - - /* Read past spaces. */ - while (*string && ' ' == *string) - string++; - - /* Read passed initial double-quote. */ - if (*string && '"' == *string) + /* Read past initial double-quote. */ + if ('"' == *string) string++; /* The rest is the value. */ @@ -1083,31 +1072,14 @@ roff_ds(ROFF_ARGS) static enum rofferr roff_nr(ROFF_ARGS) { - const char *key, *val; + const char *key; + char *val; struct reg *rg; - key = &(*bufp)[pos]; + val = *bufp + pos; + key = roff_getname(r, &val, ln, pos); rg = r->regs->regs; - /* Parse register request. */ - while ((*bufp)[pos] && ' ' != (*bufp)[pos]) - pos++; - - /* - * Set our nil terminator. Because this line is going to be - * ignored anyway, we can munge it as we please. - */ - if ((*bufp)[pos]) - (*bufp)[pos++] = '\0'; - - /* Skip whitespace to register token. */ - while ((*bufp)[pos] && ' ' == (*bufp)[pos]) - pos++; - - val = &(*bufp)[pos]; - - /* Process register token. */ - if (0 == strcmp(key, "nS")) { rg[(int)REG_nS].set = 1; if ( ! roff_parse_nat(val, &rg[(int)REG_nS].v.u)) @@ -1245,6 +1217,41 @@ roff_userdef(ROFF_ARGS) return(*szp > 1 && '\n' == (*bufp)[(int)*szp - 2] ? ROFF_REPARSE : ROFF_APPEND); } + + +static char * +roff_getname(struct roff *r, char **cpp, int ln, int pos) +{ + char *name, *cp; + + name = *cpp; + if ('\0' == *name) + return(name); + + /* Read until end of name. */ + for (cp = name; '\0' != *cp && ' ' != *cp; cp++) { + if ('\\' != *cp) + continue; + cp++; + if ('\\' == *cp) + continue; + (*r->msg)(MANDOCERR_NAMESC, r->data, ln, pos, NULL); + *cp = '\0'; + name = cp; + } + + /* Nil-terminate name. */ + if ('\0' != *cp) + *(cp++) = '\0'; + + /* Read past spaces. */ + while (' ' == *cp) + cp++; + + *cpp = cp; + return(name); +} + /* * Store *string into the user-defined string called *name. -- To unsubscribe send an email to tech+unsubscribe@mdocml.bsd.lv