aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorIngo Schwarze <schwarze@openbsd.org>2017-06-14 14:02:39 +0000
committerIngo Schwarze <schwarze@openbsd.org>2017-06-14 14:02:39 +0000
commit141881c887ec609a42b6025503a0082ee59cced0 (patch)
tree769552e2a43c2d4e8d362fa4ad1d4a349980c8a1
parentd7510635200bec5516c0a2f66d8a4fe42b453020 (diff)
downloadmandoc-141881c887ec609a42b6025503a0082ee59cced0.tar.gz
mandoc-141881c887ec609a42b6025503a0082ee59cced0.tar.zst
mandoc-141881c887ec609a42b6025503a0082ee59cced0.zip
implement the roff(7) d (macro or string defined) conditional
-rw-r--r--regress/roff/cond/Makefile2
-rw-r--r--regress/roff/cond/string.in29
-rw-r--r--regress/roff/cond/string.out_ascii20
-rw-r--r--roff.713
-rw-r--r--roff.c18
5 files changed, 72 insertions, 10 deletions
diff --git a/regress/roff/cond/Makefile b/regress/roff/cond/Makefile
index 68a88694..65d7be52 100644
--- a/regress/roff/cond/Makefile
+++ b/regress/roff/cond/Makefile
@@ -1,6 +1,6 @@
# $OpenBSD: Makefile,v 1.8 2015/05/31 23:12:17 schwarze Exp $
-REGRESS_TARGETS = if ie close numeric register strcmp before-Dd
+REGRESS_TARGETS = if ie close numeric register strcmp string before-Dd
LINT_TARGETS = if close
.include <bsd.regress.mk>
diff --git a/regress/roff/cond/string.in b/regress/roff/cond/string.in
new file mode 100644
index 00000000..6d19f4ce
--- /dev/null
+++ b/regress/roff/cond/string.in
@@ -0,0 +1,29 @@
+.TH STRING 1 "June 14, 2017" OpenBSD
+.SH NAME
+string \- conditional testing whether a string is defined
+.SH DESCRIPTION
+.ie d mystr OOPS
+.el mystr not yet defined
+.br
+.ds mystr mystrval
+.ie d mystr now defined
+.el OOPS
+.if !d mystr OOPS
+.PP
+.ie d mymac OOPS
+.el mymac not yet defined
+.br
+.de mymac
+mymacval
+..
+.ie dmymac now defined
+.el OOPS
+.if !d mymac OOPS
+.PP
+.ie d myren OOPS
+.el myren not yet defined
+.br
+.rn SM myren
+.ie d myren now defined
+.el OOPS
+.if !d myren OOPS
diff --git a/regress/roff/cond/string.out_ascii b/regress/roff/cond/string.out_ascii
new file mode 100644
index 00000000..8df491ac
--- /dev/null
+++ b/regress/roff/cond/string.out_ascii
@@ -0,0 +1,20 @@
+STRING(1) General Commands Manual STRING(1)
+
+
+
+NNAAMMEE
+ string - conditional testing whether a string is defined
+
+DDEESSCCRRIIPPTTIIOONN
+ mystr not yet defined
+ now defined
+
+ mymac not yet defined
+ now defined
+
+ myren not yet defined
+ now defined
+
+
+
+OpenBSD June 14, 2017 STRING(1)
diff --git a/roff.7 b/roff.7
index 85c5d6eb..f4c1df73 100644
--- a/roff.7
+++ b/roff.7
@@ -1,4 +1,4 @@
-.\" $Id: roff.7,v 1.89 2017/06/14 13:00:31 schwarze Exp $
+.\" $Id: roff.7,v 1.90 2017/06/14 14:02:39 schwarze Exp $
.\"
.\" Copyright (c) 2010, 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv>
.\" Copyright (c) 2010,2011,2013-2015,2017 Ingo Schwarze <schwarze@openbsd.org>
@@ -1045,8 +1045,6 @@ If the first character of
is
.Sq c
.Pq character available ,
-.Sq d
-.Pq string defined ,
.Sq e
.Pq even page ,
.Sq t
@@ -1059,6 +1057,15 @@ it evaluates to false.
If the first character of
.Ar condition
is
+.Sq d ,
+it evaluates to true if the rest of
+.Ar condition
+is the name of an existing user defined macro or string;
+otherwise, it evaluates to false.
+.It
+If the first character of
+.Ar condition
+is
.Sq r ,
it evaluates to true if the rest of
.Ar condition
diff --git a/roff.c b/roff.c
index c5f2815c..6cd2011d 100644
--- a/roff.c
+++ b/roff.c
@@ -1,4 +1,4 @@
-/* $Id: roff.c,v 1.309 2017/06/14 13:00:31 schwarze Exp $ */
+/* $Id: roff.c,v 1.310 2017/06/14 14:02:39 schwarze Exp $ */
/*
* Copyright (c) 2008-2012, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
* Copyright (c) 2010-2015, 2017 Ingo Schwarze <schwarze@openbsd.org>
@@ -2065,7 +2065,7 @@ roff_evalcond(struct roff *r, int ln, char *v, int *pos)
{
char *cp, *name;
size_t sz;
- int number, savepos, wanttrue;
+ int number, savepos, istrue, wanttrue;
if ('!' == v[*pos]) {
wanttrue = 0;
@@ -2081,17 +2081,23 @@ roff_evalcond(struct roff *r, int ln, char *v, int *pos)
(*pos)++;
return wanttrue;
case 'c':
- case 'd':
case 'e':
case 't':
case 'v':
(*pos)++;
return !wanttrue;
+ case 'd':
case 'r':
- cp = name = v + ++*pos;
- sz = roff_getname(r, &cp, ln, *pos);
+ cp = v + *pos + 1;
+ while (*cp == ' ')
+ cp++;
+ name = cp;
+ sz = roff_getname(r, &cp, ln, cp - v);
+ istrue = sz && (v[*pos] == 'r' ? roff_hasregn(r, name, sz) :
+ (roff_getstrn(r, name, sz) != NULL ||
+ roff_getrenn(r, name, sz) != NULL));
*pos = cp - v;
- return (sz && roff_hasregn(r, name, sz)) == wanttrue;
+ return istrue == wanttrue;
default:
break;
}