ref: fd962f5a493d28eb11420019e7a0fa007440c8db
parent: 04a4132728a2f5141703d160f4d91a31de231b54
author: Ali Gholami Rudi <ali@rudi.ir>
date: Tue Apr 23 19:38:49 EDT 2013
cp: move \w request handling to cp.c tr.c can disable it using cp_wid().
--- a/cp.c
+++ b/cp.c
@@ -8,6 +8,7 @@
static int cp_backed; /* number of pushed characters */
static int cp_nblk; /* input block depth (text in \{ and \}) */
static int cp_sblk[NIES]; /* skip \} escape at this depth, if set */
+static int cp_widreq = 1; /* inline \w requests */
static int regid(void)
{
@@ -52,6 +53,13 @@
in_push(arg, NULL);
}
+static void cp_width(void)
+{
+ char wid[16];
+ sprintf(wid, "%d", ren_wid(cp_next, cp_back));
+ in_push(wid, NULL);
+}
+
static int cp_raw(void)
{
int c;
@@ -86,15 +94,18 @@
if (c == '"') {
while (c >= 0 && c != '\n')
c = cp_raw();
+ } else if (c == 'w' && cp_widreq) {
+ cp_width();
+ c = cp_next();
} else if (c == 'n') {
cp_num();
- c = cp_raw();
+ c = cp_next();
} else if (c == '*') {
cp_str();
- c = cp_raw();
+ c = cp_next();
} else if (c == '$') {
cp_arg();
- c = cp_raw();
+ c = cp_next();
} else {
cp_back(c);
c = '\\';
@@ -135,4 +146,9 @@
}
while (skip && c != '\n') /* skip until the end of the line */
c = cp_raw();
+}
+
+void cp_wid(int enable)
+{
+ cp_widreq = enable;
}
--- a/eval.c
+++ b/eval.c
@@ -64,18 +64,6 @@
return **s == '.' || isdigit(**s);
}
-static char **wid_s;
-
-static int wid_next(void)
-{
- return (unsigned char) *(*wid_s)++;
-}
-
-static void wid_back(int c)
-{
- (*wid_s)--;
-}
-
static int evalexpr(char **s);
static int evalatom(char **s);
@@ -93,13 +81,6 @@
if (!evaljmp(s, '(')) {
ret = evalexpr(s);
evaljmp(s, ')');
- return ret;
- }
- if ((*s)[0] == '\\' && (*s)[1] == 'w') {
- *s += 2;
- wid_s = s;
- ret = ren_wid(wid_next, wid_back);
- readunit(**s && strchr(SCHAR, **s) ? *(*s)++ : defunit, ret);
return ret;
}
return 0;
--- a/tr.c
+++ b/tr.c
@@ -103,6 +103,7 @@
{
char buf[4];
int i, c;
+ cp_wid(0); /* copy-mode; disable \w handling */
while ((c = cp_next()) >= 0) {
if (sbuf)
sbuf_add(sbuf, c);
@@ -125,6 +126,7 @@
sbuf_add(sbuf, c);
}
}
+ cp_wid(1);
}
static void tr_de(char **args)
@@ -511,7 +513,9 @@
mkargs_req(args + 1, buf, sizeof(buf));
req->f(args);
} else {
+ cp_wid(0);
mkargs(args + 1, buf, sizeof(buf));
+ cp_wid(1);
if (str_get(REG(cmd[1], cmd[2])))
in_push(str_get(REG(cmd[1], cmd[2])), args + 1);
}
--- a/xroff.h
+++ b/xroff.h
@@ -109,6 +109,7 @@
char *in_filename(void); /* current filename */
void cp_back(int c); /* push back copy-mode character */
void cp_blk(int skip); /* skip or read the next line or block */
+void cp_wid(int enable); /* control inlining \w requests */
/* rendering */
void render(void); /* read from in.c and print the output */