aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Makefile
diff options
context:
space:
mode:
authorIngo Schwarze <schwarze@openbsd.org>2022-05-19 15:37:47 +0000
committerIngo Schwarze <schwarze@openbsd.org>2022-05-19 15:37:47 +0000
commit45ff6553e28b53a3cb099d8c98af357ba4046b67 (patch)
treeeda57407816126a2dab814a60e08767aec501fc9 /Makefile
parent0946b71520648fe93598f7df2ced38730b6ad7bb (diff)
downloadmandoc-45ff6553e28b53a3cb099d8c98af357ba4046b67.tar.gz
mandoc-45ff6553e28b53a3cb099d8c98af357ba4046b67.tar.zst
mandoc-45ff6553e28b53a3cb099d8c98af357ba4046b67.zip
Make roff_expand() parse left-to-right rather than right-to-left.
Some escape sequences have side effects on global state, implying that the order of evaluation matters. For example, this fixes the long-standing bug that "\n+x\n+x\n+x" after ".nr x 0 1" used to print "321"; now it correctly prints "123". Right-to-left parsing was convenient because it implicitly handled nested escape sequences. With correct left-to-right parsing, nesting now requires an explicit implementation, here solved as follows: 1. Handle nested expanding escape sequences iteratively. When finding one, expand it, then retry parsing the enclosing escape sequence from the beginning, which will ultimately succeed as soon as it no longer contains any nested expanding escape sequences. 2. Handle nested non-expanding escape sequences recursively. When finding one, the escape sequence parser calls itself to find the end of the inner sequence, then continues parsing the outer sequence after that point. This requires the mandoc_escape() function to operate in two different modes. The roff(7) parser uses it in a mode where it generates diagnostics and may return an expansion request instead of a parse result. All other callers, in particular the formatters, use it in a simpler mode that never generates diagnostics and always returns a definite parsing result, but that requires all expanding escape sequences to already have been expanded earlier. The bulk of the code is the same for both modes. Since this required a major rewrite of the function anyway, move it into its own new file roff_escape.c and out of the file mandoc.c, which was misnamed in the first place and lacks a clear focus. As a side benefit, this also fixes a number of assertion failures that tb@ found with afl(1), for example "\n\\\\*0", "\v\-\\*0", and "\w\-\\\\\$0*0". As another side benefit, it also resolves some code duplication between mandoc_escape() and roff_expand() and centralizes all handling of escape sequences (except for expansion) in roff_escape.c, hopefully easing maintenance and feature improvements in the future. While here, also move end-of-input handling out of the complicated function roff_expand() and into the simpler function roff_parse_comment(), making the logic easier to understand. Since this is a major reorganization of a central component of mandoc(1), stability of the program might slightly suffer for a few weeks, but i believe that's not a problem at this point of the release cycle. The new code already satisfies the regression suite, but more tweaking and regression testing to further improve the handling of various escape sequences will likely follow in the near future.
Diffstat (limited to 'Makefile')
-rw-r--r--Makefile4
1 files changed, 3 insertions, 1 deletions
diff --git a/Makefile b/Makefile
index 8acd5db5..e3b3c6c2 100644
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,4 @@
-# $Id: Makefile,v 1.541 2022/04/14 16:43:43 schwarze Exp $
+# $Id: Makefile,v 1.542 2022/05/19 15:37:47 schwarze Exp $
#
# Copyright (c) 2011, 2013-2022 Ingo Schwarze <schwarze@openbsd.org>
# Copyright (c) 2010, 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv>
@@ -122,6 +122,7 @@ SRCS = arch.c \
preconv.c \
read.c \
roff.c \
+ roff_escape.c \
roff_html.c \
roff_term.c \
roff_validate.c \
@@ -235,6 +236,7 @@ LIBMDOC_OBJS = att.o \
LIBROFF_OBJS = eqn.o \
roff.o \
+ roff_escape.o \
roff_validate.o \
tbl.o \
tbl_data.o \