summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIngo Schwarze <schwarze@cvs.openbsd.org>2012-05-28 22:45:35 +0000
committerIngo Schwarze <schwarze@cvs.openbsd.org>2012-05-28 22:45:35 +0000
commite181695f9fdea338faa03af3a4db2abbb523b0f8 (patch)
treeaddbb4ae1bc5fb0b960a10a03dc2ac40edc6d331
parent0318623ea5e73c19426dd575773ed902966ef2b9 (diff)
While i already got my fingers dirty on mandoc_escape(),
profit of the occasion to pull out some spaghetti, that is, three confusing variables and fourteen pointless assignments among them; instead, always operate on the official pointers **start, **end, and *sz, each of which conveys an obvious meaning. No functional change intended, and the new tests confirm that everything still (err...) "works", as far as that word can be applied to the kind of roff(7) mock-up code i'm polishing here.
-rw-r--r--regress/usr.bin/mandoc/roff/esc/Makefile4
-rw-r--r--regress/usr.bin/mandoc/roff/esc/c.in13
-rw-r--r--regress/usr.bin/mandoc/roff/esc/c.out_ascii9
-rw-r--r--regress/usr.bin/mandoc/roff/esc/f.in12
-rw-r--r--regress/usr.bin/mandoc/roff/esc/f.out_ascii11
-rw-r--r--regress/usr.bin/mandoc/roff/esc/ignore.in12
-rw-r--r--regress/usr.bin/mandoc/roff/esc/ignore.out_ascii11
-rw-r--r--regress/usr.bin/mandoc/roff/esc/multi.in10
-rw-r--r--regress/usr.bin/mandoc/roff/esc/multi.out_ascii10
-rw-r--r--regress/usr.bin/mandoc/roff/esc/one.in14
-rw-r--r--regress/usr.bin/mandoc/roff/esc/one.out_ascii12
-rw-r--r--regress/usr.bin/mandoc/roff/esc/two.in75
-rw-r--r--regress/usr.bin/mandoc/roff/esc/two.out_ascii25
-rw-r--r--usr.bin/mandoc/mandoc.c133
14 files changed, 281 insertions, 70 deletions
diff --git a/regress/usr.bin/mandoc/roff/esc/Makefile b/regress/usr.bin/mandoc/roff/esc/Makefile
index f8e95e9161c..e9705972a80 100644
--- a/regress/usr.bin/mandoc/roff/esc/Makefile
+++ b/regress/usr.bin/mandoc/roff/esc/Makefile
@@ -1,6 +1,6 @@
-# $OpenBSD: Makefile,v 1.2 2012/05/28 17:08:48 schwarze Exp $
+# $OpenBSD: Makefile,v 1.3 2012/05/28 22:45:34 schwarze Exp $
-REGRESS_TARGETS=h z
+REGRESS_TARGETS=one two multi c f h z ignore
# Postprocessing to remove "character backspace" sequences
# unless they are foolowed by the same character again.
diff --git a/regress/usr.bin/mandoc/roff/esc/c.in b/regress/usr.bin/mandoc/roff/esc/c.in
new file mode 100644
index 00000000000..be95f2bcd03
--- /dev/null
+++ b/regress/usr.bin/mandoc/roff/esc/c.in
@@ -0,0 +1,13 @@
+.Dd May 28, 2012
+.Dt ESC-C 1
+.Os OpenBSD
+.Sh NAME
+.Nm esc-c
+.Nd the roff escape c sequence: remove trailing space
+.Sh DESCRIPTION
+No space between
+.Dq one
+and
+.Dq word :
+one\c
+word
diff --git a/regress/usr.bin/mandoc/roff/esc/c.out_ascii b/regress/usr.bin/mandoc/roff/esc/c.out_ascii
new file mode 100644
index 00000000000..90ed2c99605
--- /dev/null
+++ b/regress/usr.bin/mandoc/roff/esc/c.out_ascii
@@ -0,0 +1,9 @@
+ESC-C(1) OpenBSD Reference Manual ESC-C(1)
+
+NNAAMMEE
+ eesscc--cc - the roff escape c sequence: remove trailing space
+
+DDEESSCCRRIIPPTTIIOONN
+ No space between ``one'' and ``word'': oneword
+
+OpenBSD May 28, 2012 OpenBSD
diff --git a/regress/usr.bin/mandoc/roff/esc/f.in b/regress/usr.bin/mandoc/roff/esc/f.in
new file mode 100644
index 00000000000..c6de2c297a8
--- /dev/null
+++ b/regress/usr.bin/mandoc/roff/esc/f.in
@@ -0,0 +1,12 @@
+.Dd May 28, 2012
+.Dt ESC-F 1
+.Os OpenBSD
+.Sh NAME
+.Nm esc-f
+.Nd the roff escape f sequence: font changes
+.Sh DESCRIPTION
+numbers: \f3bold\f2italic\f1roman
+.br
+letters: \fBbold\fIitalic\fPback\fRroman
+.br
+multiletter: \f[B]bold\f[I]italic\f[P]back\f[R]roman
diff --git a/regress/usr.bin/mandoc/roff/esc/f.out_ascii b/regress/usr.bin/mandoc/roff/esc/f.out_ascii
new file mode 100644
index 00000000000..594c3a17625
--- /dev/null
+++ b/regress/usr.bin/mandoc/roff/esc/f.out_ascii
@@ -0,0 +1,11 @@
+ESC-F(1) OpenBSD Reference Manual ESC-F(1)
+
+NNAAMMEE
+ eesscc--ff - the roff escape f sequence: font changes
+
+DDEESSCCRRIIPPTTIIOONN
+ numbers: bboolldd_i_t_a_l_i_croman
+ letters: bboolldd_i_t_a_l_i_cbbaacckkroman
+ multiletter: bboolldd_i_t_a_l_i_cbbaacckkroman
+
+OpenBSD May 28, 2012 OpenBSD
diff --git a/regress/usr.bin/mandoc/roff/esc/ignore.in b/regress/usr.bin/mandoc/roff/esc/ignore.in
new file mode 100644
index 00000000000..a7350b87cc1
--- /dev/null
+++ b/regress/usr.bin/mandoc/roff/esc/ignore.in
@@ -0,0 +1,12 @@
+.Dd May 28, 2012
+.Dt ESC-IGNORE 1
+.Os OpenBSD
+.Sh NAME
+.Nm esc-ignore
+.Nd ignored roff escape sequences
+.Sh DESCRIPTION
+multiform: a\kxb\k(xyc\k[xyz]d
+.br
+quoted: a\R'myreg 0'b\R'myreg \A'y'0'c
+.br
+sizes: a\s0b\s(12c\s[123]d\s'123'e\s'1\w'xy'2'f
diff --git a/regress/usr.bin/mandoc/roff/esc/ignore.out_ascii b/regress/usr.bin/mandoc/roff/esc/ignore.out_ascii
new file mode 100644
index 00000000000..9a936163d89
--- /dev/null
+++ b/regress/usr.bin/mandoc/roff/esc/ignore.out_ascii
@@ -0,0 +1,11 @@
+ESC-IGNORE(1) OpenBSD Reference Manual ESC-IGNORE(1)
+
+NNAAMMEE
+ eesscc--iiggnnoorree - ignored roff escape sequences
+
+DDEESSCCRRIIPPTTIIOONN
+ multiform: abcd
+ quoted: abc
+ sizes: abcdef
+
+OpenBSD May 28, 2012 OpenBSD
diff --git a/regress/usr.bin/mandoc/roff/esc/multi.in b/regress/usr.bin/mandoc/roff/esc/multi.in
new file mode 100644
index 00000000000..a2a76db1e01
--- /dev/null
+++ b/regress/usr.bin/mandoc/roff/esc/multi.in
@@ -0,0 +1,10 @@
+.Dd May 28, 2012
+.Dt ESC-MULTI 1
+.Os OpenBSD
+.Sh NAME
+.Nm esc-multi
+.Nd roff multi-character escape sequences
+.Sh DESCRIPTION
+\[tno] \[t+-] \[tmu] \[tdi] \[12] \[14] \[34]
+.br
+\C'tno' \C't+-' \C'tmu' \C'tdi' \C'12' \C'14' \C'34'
diff --git a/regress/usr.bin/mandoc/roff/esc/multi.out_ascii b/regress/usr.bin/mandoc/roff/esc/multi.out_ascii
new file mode 100644
index 00000000000..eb4ad133a28
--- /dev/null
+++ b/regress/usr.bin/mandoc/roff/esc/multi.out_ascii
@@ -0,0 +1,10 @@
+ESC-MULTI(1) OpenBSD Reference Manual ESC-MULTI(1)
+
+NNAAMMEE
+ eesscc--mmuullttii - roff multi-character escape sequences
+
+DDEESSCCRRIIPPTTIIOONN
+ ~ +- x -:- 1/2 1/4 3/4
+ ~ +- x -:- 1/2 1/4 3/4
+
+OpenBSD May 28, 2012 OpenBSD
diff --git a/regress/usr.bin/mandoc/roff/esc/one.in b/regress/usr.bin/mandoc/roff/esc/one.in
new file mode 100644
index 00000000000..caed6260cd6
--- /dev/null
+++ b/regress/usr.bin/mandoc/roff/esc/one.in
@@ -0,0 +1,14 @@
+.Dd May 28, 2012
+.Dt ESC-ONE 1
+.Os OpenBSD
+.Sh NAME
+.Nm esc-one
+.Nd roff one-character escape sequences
+.Sh DESCRIPTION
+backslash: >\e<
+.br
+minus: >\-<
+.br
+acute: >\'<
+.br
+grave: >\`<
diff --git a/regress/usr.bin/mandoc/roff/esc/one.out_ascii b/regress/usr.bin/mandoc/roff/esc/one.out_ascii
new file mode 100644
index 00000000000..8fc54112b4b
--- /dev/null
+++ b/regress/usr.bin/mandoc/roff/esc/one.out_ascii
@@ -0,0 +1,12 @@
+ESC-ONE(1) OpenBSD Reference Manual ESC-ONE(1)
+
+NNAAMMEE
+ eesscc--oonnee - roff one-character escape sequences
+
+DDEESSCCRRIIPPTTIIOONN
+ backslash: >\<
+ minus: >-<
+ acute: >'<
+ grave: >`<
+
+OpenBSD May 28, 2012 OpenBSD
diff --git a/regress/usr.bin/mandoc/roff/esc/two.in b/regress/usr.bin/mandoc/roff/esc/two.in
new file mode 100644
index 00000000000..6a5d222ccbb
--- /dev/null
+++ b/regress/usr.bin/mandoc/roff/esc/two.in
@@ -0,0 +1,75 @@
+.Dd May 28, 2012
+.Dt ESC-TWO 1
+.Os OpenBSD
+.Sh NAME
+.Nm esc-two
+.Nd roff two-character escape sequences
+.Sh DESCRIPTION
+lines: \(ba \(br \(ul \(bb \(sl \(rs
+.\" groff doesn't know \(rl
+.br
+markers: \(bu \(lz \(sq \(ps \(sc \(lh \(rh \(at \(sh \(CR
+.\" the circle \(ci differs
+.\" the daggers \(dd and \(dg use backspace
+.\" groff doesn't know \(OK
+.br
+legal: \(co \(rg \(tm
+.br
+punctuation: \(em \(en \(hy
+.\" the inverted punctuation \(r! and \(r? use backspace
+.br
+quotes: \(Bq \(bq \(oq \(cq \(aq \(dq \(Fo \(Fc \(fo \(fc
+.\" the double quotes \(lq and \(rq differ
+.br
+brackets: \(lB \(rB \(lC \(rC \(la \(ra \(bv \(lt \(lk \(rt \(rk \(rb
+.\" the left bottom \(lb differs
+.br
+arrows: \(<- \(-> \(lA \(rA \(hA
+.\" the left-right arrow \(<> differs
+.\" groff doesn't know \(va and \(vA
+.\" the vertical arrows \(da, \(ua, \(uA, \(dA use backspace
+.br
+logical: \(AN \(OR \(no \(te \(st \(tf \(3d \(or
+.\" the universal quantifier \(fa uses backspace
+.br
+mathematical: \(pl \(mi \(-+ \(+- \(pc \(mu \(di \(f/ \(**
+\(<= \(>= \(<< \(>> \(eq \(!= \(== \(ne \(=~ \(ap \(~~ \(~= \(pt
+\(es \(mo \(sb \(sp \(ca \(cu
+\(sr \(lc \(rc \(lf \(rf \(if \(Ah \(Im \(Re \(pd
+.\" groff doesn't know \(-~, \(nb, \(nc, \(-h
+.\" these differ: \(nm \(ib \(ip \(/_ \(pp \(gr
+.\" these use backspace: \(c* \(c+ \(is
+.br
+ligatures: \(ff \(fi \(fl \(Fi \(Fl \(AE \(ae \(OE \(oe \(IJ \(ij
+.\" the German eszett \(ss differs
+.br
+accents: \(a" \(a^ \(aa \(ga \(ac \(ad \(ah \(ao \(a~ \(ho \(ha \(ti
+.\" the macron \(a- differs
+.\" groff doesn't know \(a.
+.\" the breve \(ab uses backspace
+.br
+.\" accented and special letters all use backspace:
+.\" \('A \('E \('I \('O \('U \('a \('e \('i \('o \('u
+.\" \(`A \(`E \(`I \(`O \(`U \(`a \(`e \(`i \(`o \(`u
+.\" \(~A \(~N \(~O \(~a \(~n \(~o
+.\" \(:A \(:E \(:I \(:O \(:U \(:a \(:e \(:i \(:o \(:u \(:y
+.\" \(^A \(^E \(^I \(^O \(^U \(^a \(^e \(^i \(^o \(^u
+.\" \(,C \(,c \(/L \(/l \(/O \(/o \(oA \(oa
+.\" \(-D \(Sd \(TP \(Tp
+.\" except:
+special letter: \(.i
+.\" groff doesn't know \(.j
+.br
+currency: \(Do \(Eu \(eu \(Fo
+.\" these use backspace: \(ct \(Ye \(Po \(Cs
+.br
+units: \(de \(fm
+.\" groff doesn't know \(%O, and \(sd and \(mc differ
+.br
+greek letters: \(*A \(*B \(*E \(*Z \(*Y \(*I \(*K \(*L
+\(*M \(*N \(*O \(*P \(*R \(*T \(*U \(*X
+\(*a \(*b \(*g \(*d \(*e \(*y \(*i \(*k
+\(*n \(*o \(*r \(*u \(*x \(*w \(+e \(ts
+.\" these differ: \(*G \(*S \(*F
+.\" these use backspace: \(*D \(*H \(*C \(*Q \(*W
+.\" \(*z \(*h \(*l \(*m \(*c \(*p \(*s \(*t \(*f \(*q \(+h \(+f \+p
diff --git a/regress/usr.bin/mandoc/roff/esc/two.out_ascii b/regress/usr.bin/mandoc/roff/esc/two.out_ascii
new file mode 100644
index 00000000000..d1960bc0bef
--- /dev/null
+++ b/regress/usr.bin/mandoc/roff/esc/two.out_ascii
@@ -0,0 +1,25 @@
+ESC-TWO(1) OpenBSD Reference Manual ESC-TWO(1)
+
+NNAAMMEE
+ eesscc--ttwwoo - roff two-character escape sequences
+
+DDEESSCCRRIIPPTTIIOONN
+ lines: | | _ | / \
+ markers: o <> [] 9| S <= => @ # _|
+ legal: (C) (R) tm
+ punctuation: -- - -
+ quotes: ,, , ` ' ' " << >> < >
+ brackets: [ ] { } < > | ,- { -. } -'
+ arrows: <- -> <= => <=>
+ logical: ^ v ~ 3 -) .:. .:. |
+ mathematical: + - -+ +- . x -:- / * <= >= << >> = != == !== =~ ~ ~~ ~= oc
+ {} E (= =) (^) U \/ |~ ~| |_ _| oo N I R a
+ ligatures: ff fi fl ffi ffl AE ae OE oe IJ ij
+ accents: " ^ ' ` , " v o ~ , ^ ~
+ special letter: i
+ currency: $ EUR EUR <<
+ units: o '
+ greek letters: A B E Z H I K /\ M N O TT P T Y X a B y d e n i k v o p u
+ x w e s
+
+OpenBSD May 28, 2012 OpenBSD
diff --git a/usr.bin/mandoc/mandoc.c b/usr.bin/mandoc/mandoc.c
index 8c3afd24a7c..2bfb58f0e77 100644
--- a/usr.bin/mandoc/mandoc.c
+++ b/usr.bin/mandoc/mandoc.c
@@ -1,4 +1,4 @@
-/* $Id: mandoc.c,v 1.33 2012/05/28 17:08:48 schwarze Exp $ */
+/* $Id: mandoc.c,v 1.34 2012/05/28 22:45:33 schwarze Exp $ */
/*
* Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
* Copyright (c) 2011, 2012 Ingo Schwarze <schwarze@openbsd.org>
@@ -38,20 +38,33 @@ static char *time2a(time_t);
enum mandoc_esc
mandoc_escape(const char **end, const char **start, int *sz)
{
- char c, term;
- int i, rlim;
- const char *cp, *rstart;
+ const char *local_start;
+ int local_sz;
+ char term;
enum mandoc_esc gly;
- cp = *end;
- rstart = cp;
- if (start)
- *start = rstart;
- i = rlim = 0;
+ /*
+ * When the caller doesn't provide return storage,
+ * use local storage.
+ */
+
+ if (NULL == start)
+ start = &local_start;
+ if (NULL == sz)
+ sz = &local_sz;
+
+ /*
+ * Beyond the backslash, at least one input character
+ * is part of the escape sequence. With one exception
+ * (see below), that character won't be returned.
+ */
+
gly = ESCAPE_ERROR;
+ *start = ++*end;
+ *sz = 0;
term = '\0';
- switch ((c = cp[i++])) {
+ switch ((*start)[-1]) {
/*
* First the glyphs. There are several different forms of
* these, but each eventually returns a substring of the glyph
@@ -59,7 +72,7 @@ mandoc_escape(const char **end, const char **start, int *sz)
*/
case ('('):
gly = ESCAPE_SPECIAL;
- rlim = 2;
+ *sz = 2;
break;
case ('['):
gly = ESCAPE_SPECIAL;
@@ -69,14 +82,15 @@ mandoc_escape(const char **end, const char **start, int *sz)
* Unicode codepoint. Here, however, only check whether
* it's not a zero-width escape.
*/
- if ('u' == cp[i] && ']' != cp[i + 1])
+ if ('u' == (*start)[0] && ']' != (*start)[1])
gly = ESCAPE_UNICODE;
term = ']';
break;
case ('C'):
- if ('\'' != cp[i])
+ if ('\'' != **start)
return(ESCAPE_ERROR);
gly = ESCAPE_SPECIAL;
+ *start = ++*end;
term = '\'';
break;
@@ -87,7 +101,6 @@ mandoc_escape(const char **end, const char **start, int *sz)
* let us just skip the next character.
*/
case ('z'):
- (*end)++;
return(ESCAPE_SKIPCHAR);
/*
@@ -114,21 +127,17 @@ mandoc_escape(const char **end, const char **start, int *sz)
case ('f'):
if (ESCAPE_ERROR == gly)
gly = ESCAPE_FONT;
-
- rstart= &cp[i];
- if (start)
- *start = rstart;
-
- switch (cp[i++]) {
+ switch (**start) {
case ('('):
- rlim = 2;
+ *start = ++*end;
+ *sz = 2;
break;
case ('['):
+ *start = ++*end;
term = ']';
break;
default:
- rlim = 1;
- i--;
+ *sz = 1;
break;
}
break;
@@ -150,9 +159,10 @@ mandoc_escape(const char **end, const char **start, int *sz)
case ('X'):
/* FALLTHROUGH */
case ('Z'):
- if ('\'' != cp[i++])
+ if ('\'' != **start)
return(ESCAPE_ERROR);
gly = ESCAPE_IGNORE;
+ *start = ++*end;
term = '\'';
break;
@@ -178,10 +188,11 @@ mandoc_escape(const char **end, const char **start, int *sz)
case ('w'):
/* FALLTHROUGH */
case ('x'):
+ if ('\'' != **start)
+ return(ESCAPE_ERROR);
if (ESCAPE_ERROR == gly)
gly = ESCAPE_IGNORE;
- if ('\'' != cp[i++])
- return(ESCAPE_ERROR);
+ *start = ++*end;
term = '\'';
break;
@@ -190,17 +201,17 @@ mandoc_escape(const char **end, const char **start, int *sz)
* XXX Do any other escapes need similar handling?
*/
case ('N'):
- if ('\0' == cp[i])
+ if ('\0' == **start)
return(ESCAPE_ERROR);
- *end = &cp[++i];
- if (isdigit((unsigned char)cp[i-1]))
+ (*end)++;
+ if (isdigit((unsigned char)**start)) {
+ *sz = 1;
return(ESCAPE_IGNORE);
+ }
+ (*start)++;
while (isdigit((unsigned char)**end))
(*end)++;
- if (start)
- *start = &cp[i];
- if (sz)
- *sz = *end - &cp[i];
+ *sz = *end - *start;
if ('\0' != **end)
(*end)++;
return(ESCAPE_NUMBERED);
@@ -211,54 +222,43 @@ mandoc_escape(const char **end, const char **start, int *sz)
case ('s'):
gly = ESCAPE_IGNORE;
- rstart = &cp[i];
- if (start)
- *start = rstart;
-
/* See +/- counts as a sign. */
- c = cp[i];
- if ('+' == c || '-' == c || ASCII_HYPH == c)
- ++i;
+ if ('+' == **end || '-' == **end || ASCII_HYPH == **end)
+ (*end)++;
- switch (cp[i++]) {
+ switch (**end) {
case ('('):
- rlim = 2;
+ *start = ++*end;
+ *sz = 2;
break;
case ('['):
+ *start = ++*end;
term = ']';
break;
case ('\''):
+ *start = ++*end;
term = '\'';
break;
default:
- rlim = 1;
- i--;
+ *sz = 1;
break;
}
- /* See +/- counts as a sign. */
- c = cp[i];
- if ('+' == c || '-' == c || ASCII_HYPH == c)
- ++i;
-
break;
/*
* Anything else is assumed to be a glyph.
+ * In this case, pass back the character after the backslash.
*/
default:
gly = ESCAPE_SPECIAL;
- rlim = 1;
- i--;
+ *start = --*end;
+ *sz = 1;
break;
}
assert(ESCAPE_ERROR != gly);
- *end = rstart = &cp[i];
- if (start)
- *start = rstart;
-
/*
* Read up to the terminating character,
* paying attention to nested escapes.
@@ -280,15 +280,13 @@ mandoc_escape(const char **end, const char **start, int *sz)
break;
}
}
- rlim = (*end)++ - rstart;
+ *sz = (*end)++ - *start;
} else {
- assert(rlim > 0);
- if ((size_t)rlim > strlen(rstart))
+ assert(*sz > 0);
+ if ((size_t)*sz > strlen(*start))
return(ESCAPE_ERROR);
- *end += rlim;
+ *end += *sz;
}
- if (sz)
- *sz = rlim;
/* Run post-processors. */
@@ -298,12 +296,13 @@ mandoc_escape(const char **end, const char **start, int *sz)
* Pretend that the constant-width font modes are the
* same as the regular font modes.
*/
- if (2 == rlim && 'C' == *rstart)
- rstart++;
- else if (1 != rlim)
+ if (2 == *sz && 'C' == **start) {
+ (*start)++;
+ (*sz)--;
+ } else if (1 != *sz)
break;
- switch (*rstart) {
+ switch (**start) {
case ('3'):
/* FALLTHROUGH */
case ('B'):
@@ -325,9 +324,7 @@ mandoc_escape(const char **end, const char **start, int *sz)
}
break;
case (ESCAPE_SPECIAL):
- if (1 != rlim)
- break;
- if ('c' == *rstart)
+ if (1 == *sz && 'c' == **start)
gly = ESCAPE_NOSPACE;
break;
default: