summaryrefslogtreecommitdiffstats
path: root/roff.c
diff options
context:
space:
mode:
authorIngo Schwarze <schwarze@openbsd.org>2023-10-23 20:25:02 +0000
committerIngo Schwarze <schwarze@openbsd.org>2023-10-23 20:25:02 +0000
commit18c87ef68c91b81051bd58c31019aa7c8983ce80 (patch)
treed7c614a3e44a7c6d290b14658dc49a19bea31d04 /roff.c
parentc886796204d02d3bd622259c550deba6d54e5544 (diff)
downloadmandoc-18c87ef68c91b81051bd58c31019aa7c8983ce80.tar.gz
Support some escape sequences, in particular character escape sequences,
inside \w arguments, and skip most other escape sequences when measuring the output length in this way because most escape sequences contribute little or nothing to text width: for example, consider font escapes in terminal output. This implementation is very rudimentary. In particular, it assumes that every character has the same width. No attempt is made to detect double-width or zero-width Unicode characters or to take dependencies on output devices or fonts into account. These limitations are hard to avoid because mandoc has to interpolate \w at the parsing stage when the output device is not yet known. I really do not want the content of the syntax tree to depend on the output device. Feature requested by Paul <Eggert at cs dot ucla dot edu>, who also submitted a patch, but i chose to commit this very different patch with almost the same functionality. His input was still very valuable because complete support for \w is out of the question, and consequently, the main task is identifying subsets of the feature that are needed for real-world manual pages and can be supported without uprooting the whole forest.
Diffstat (limited to 'roff.c')
-rw-r--r--roff.c33
1 files changed, 30 insertions, 3 deletions
diff --git a/roff.c b/roff.c
index 0005b999..53f9df46 100644
--- a/roff.c
+++ b/roff.c
@@ -1,6 +1,6 @@
/* $Id$ */
/*
- * Copyright (c) 2010-2015, 2017-2022 Ingo Schwarze <schwarze@openbsd.org>
+ * Copyright (c) 2010-2015, 2017-2023 Ingo Schwarze <schwarze@openbsd.org>
* Copyright (c) 2008-2012, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
*
* Permission to use, copy, modify, and distribute this software for any
@@ -1362,6 +1362,7 @@ roff_expand(struct roff *r, struct buf *buf, int ln, int pos, char ec)
const char *res; /* the string to be pasted */
const char *src; /* source for copying */
char *dst; /* destination for copying */
+ enum mandoc_esc subtype; /* return value from roff_escape */
int iesc; /* index of leading escape char */
int inam; /* index of the escape name */
int iarg; /* index beginning the argument */
@@ -1551,8 +1552,34 @@ roff_expand(struct roff *r, struct buf *buf, int ln, int pos, char ec)
res = ubuf;
break;
case 'w':
- (void)snprintf(ubuf, sizeof(ubuf),
- "%d", (iendarg - iarg) * 24);
+ rsz = 0;
+ subtype = ESCAPE_UNDEF;
+ while (iarg < iendarg) {
+ asz = subtype == ESCAPE_SKIPCHAR ? 0 : 1;
+ if (buf->buf[iarg] != '\\') {
+ rsz += asz;
+ iarg++;
+ continue;
+ }
+ switch ((subtype = roff_escape(buf->buf, 0,
+ iarg, NULL, NULL, NULL, NULL, &iarg))) {
+ case ESCAPE_SPECIAL:
+ case ESCAPE_NUMBERED:
+ case ESCAPE_UNICODE:
+ case ESCAPE_OVERSTRIKE:
+ case ESCAPE_UNDEF:
+ break;
+ case ESCAPE_DEVICE:
+ asz *= 8;
+ break;
+ case ESCAPE_EXPAND:
+ abort();
+ default:
+ continue;
+ }
+ rsz += asz;
+ }
+ (void)snprintf(ubuf, sizeof(ubuf), "%d", rsz * 24);
res = ubuf;
break;
default: