ref: b6fac629b65e7769dfaf2b9252221e99cd4568dc
parent: 8bf0eab2238dbe21bccc8e079dd35285b2dc9a66
author: Ali Gholami Rudi <ali@rudi.ir>
date: Mon Apr 15 14:53:39 EDT 2013
cp: support \n+x and \n-x
--- a/cp.c
+++ b/cp.c
@@ -22,9 +22,15 @@
static void cp_num(void)
{
- char *buf = num_get(regid());
- if (buf)
- in_push(buf, NULL);
+ int id;
+ int c = cp_next();
+ if (c != '-' && c != '+')
+ cp_back(c);
+ id = regid();
+ if (c == '-' || c == '+')
+ num_get(id, c == '+' ? 1 : -1);
+ if (num_str(id))
+ in_push(num_str(id), NULL);
}
static void cp_str(void)
--- a/reg.c
+++ b/reg.c
@@ -13,6 +13,7 @@
};
static int nregs[NREGS]; /* global number registers */
+static int nregs_inc[NREGS]; /* number register auto-increment size */
static char *sregs[NREGS]; /* global string registers */
static void *sregs_dat[NREGS]; /* builtin function data */
static struct env envs[3]; /* environments */
@@ -52,7 +53,7 @@
}
/* the contents of a number register (returns a static buffer) */
-char *num_get(int id)
+char *num_str(int id)
{
static char numbuf[128];
numbuf[0] = '\0';
@@ -74,6 +75,25 @@
sprintf(numbuf, "%d", *nreg(id));
}
return numbuf;
+}
+
+void num_set(int id, int val, int inc)
+{
+ *nreg(id) = val;
+ nregs_inc[id] = inc;
+}
+
+void num_del(int id)
+{
+ *nreg(id) = 0;
+ nregs_inc[id] = 0;
+}
+
+int num_get(int id, int inc)
+{
+ if (inc)
+ *nreg(id) += inc > 0 ? nregs_inc[id] : -nregs_inc[id];
+ return *nreg(id);
}
void str_set(int id, char *s)
--- a/tr.c
+++ b/tr.c
@@ -40,9 +40,18 @@
if (!args[2])
return;
id = REG(args[1][0], args[1][1]);
- *nreg(id) = eval(args[2], *nreg(id), 'u');
+ num_set(id, eval(args[2], num_get(id, 0), 'u'),
+ args[3] ? eval(args[3], 0, 'u') : 0);
}
+static void tr_rr(char **args)
+{
+ int i;
+ for (i = 1; i <= NARGS; i++)
+ if (args[i])
+ num_del(REG(args[i][0], args[i][1]));
+}
+
static void tr_ds(char **args)
{
if (!args[2])
@@ -476,6 +485,7 @@
{"ps", tr_ps},
{"rm", tr_rm},
{"rn", tr_rn},
+ {"rr", tr_rr},
{"so", tr_so},
{"sp", tr_sp},
{"ti", tr_ti},
--- a/xroff.h
+++ b/xroff.h
@@ -25,7 +25,10 @@
#define LEN(a) (sizeof(a) / sizeof((a)[0]))
/* number registers */
-char *num_get(int id);
+int num_get(int id, int inc);
+void num_set(int id, int val, int inc);
+void num_del(int id);
+char *num_str(int id);
int *nreg(int id);
int eval(char *s, int orig, int unit);