shithub: neatroff

Download patch

ref: 9d096e220d232fb127b4a5d7801929dd78f85c2d
parent: 9d3c377d7153fd7172aaa005874ca5c7376430b1
author: Ali Gholami Rudi <ali@rudi.ir>
date: Tue Mar 26 12:19:02 EDT 2013

tr: add .rn and .rm

--- a/reg.c
+++ b/reg.c
@@ -14,6 +14,7 @@
 
 static int nregs[NREGS];	/* global number registers */
 static char *sregs[NREGS];	/* global string registers */
+static void *sregs_dat[NREGS];	/* builtin function data */
 static struct env envs[3];	/* environments */
 static struct env *env;		/* current enviroment */
 static int eregs_idx[NREGS];	/* register environment index in eregs[] */
@@ -46,15 +47,6 @@
 	return numbuf;
 }
 
-void tr_nr(char **args)
-{
-	int id;
-	if (!args[2])
-		return;
-	id = REG(args[1][0], args[1][1]);
-	*nreg(id) = tr_int(args[2], *nreg(id), 'u');
-}
-
 void str_set(int id, char *s)
 {
 	int len = strlen(s) + 1;
@@ -67,6 +59,33 @@
 char *str_get(int id)
 {
 	return sregs[id];
+}
+
+void *str_dget(int id)
+{
+	return sregs_dat[id];
+}
+
+void str_dset(int id, void *d)
+{
+	sregs_dat[id] = d;
+}
+
+void str_rm(int id)
+{
+	if (sregs[id])
+		free(sregs[id]);
+	sregs[id] = NULL;
+	sregs_dat[id] = NULL;
+}
+
+void str_rn(int src, int dst)
+{
+	str_rm(dst);
+	sregs[dst] = sregs[src];
+	sregs_dat[dst] = sregs_dat[src];
+	sregs[src] = NULL;
+	sregs_dat[src] = NULL;
 }
 
 static void env_set(int id)
--- a/tr.c
+++ b/tr.c
@@ -99,6 +99,15 @@
 		n_p = tr_int(args[1], n_p, 'v');
 }
 
+static void tr_nr(char **args)
+{
+	int id;
+	if (!args[2])
+		return;
+	id = REG(args[1][0], args[1][1]);
+	*nreg(id) = tr_int(args[2], *nreg(id), 'u');
+}
+
 static void tr_ds(char **args)
 {
 	if (!args[2])
@@ -106,6 +115,20 @@
 	str_set(REG(args[1][0], args[1][1]), args[2]);
 }
 
+static void tr_rm(char **args)
+{
+	if (!args[1])
+		return;
+	str_rm(REG(args[1][0], args[1][1]));
+}
+
+static void tr_rn(char **args)
+{
+	if (!args[2])
+		return;
+	str_rn(REG(args[1][0], args[1][1]), REG(args[2][0], args[2][1]));
+}
+
 static char *arg_regname(char *s, int len);
 
 static void tr_de(char **args)
@@ -277,6 +300,8 @@
 	{"nr", tr_nr, mkargs_reg1},
 	{"pl", tr_pl},
 	{"ps", tr_ps},
+	{"rm", tr_rm},
+	{"rn", tr_rn},
 	{"sp", tr_sp},
 	{"vs", tr_vs},
 };
@@ -289,7 +314,6 @@
 	char cmd[RLEN];
 	char buf[LNLEN];
 	struct cmd *req;
-	int i;
 	while (tr_nl && (c == '.' || c == '\'')) {
 		nl = 1;
 		args[0] = cmd;
@@ -296,9 +320,7 @@
 		cmd[0] = c;
 		req = NULL;
 		arg_regname(cmd + 1, sizeof(cmd) - 1);
-		for (i = 0; i < LEN(cmds); i++)
-			if (!strcmp(cmd + 1, cmds[i].id))
-				req = &cmds[i];
+		req = str_dget(REG(cmd[1], cmd[2]));
 		if (req) {
 			if (req->args)
 				req->args(args + 1, buf, sizeof(buf));
@@ -314,4 +336,11 @@
 	}
 	tr_nl = nl;
 	return c;
+}
+
+void tr_init(void)
+{
+	int i;
+	for (i = 0; i < LEN(cmds); i++)
+		str_dset(REG(cmds[i].id[0], cmds[i].id[1]), &cmds[i]);
 }
--- a/xroff.c
+++ b/xroff.c
@@ -36,6 +36,7 @@
 {
 	dev_open("/root/troff/home/font/devutf");
 	env_init();
+	tr_init();
 	g_init();
 	compile();
 	env_free();
--- a/xroff.h
+++ b/xroff.h
@@ -28,7 +28,11 @@
 
 /* string registers */
 void str_set(int id, char *s);
+void str_dset(int id, void *d);
 char *str_get(int id);
+void *str_dget(int id);
+void str_rm(int id);
+void str_rn(int src, int dst);
 
 /* enviroments */
 void env_init(void);
@@ -104,9 +108,10 @@
 void tr_ft(char **args);
 void tr_in(char **args);
 void tr_nf(char **args);
-void tr_nr(char **args);
 void tr_ps(char **args);
 void tr_sp(char **args);
+
+void tr_init(void);
 
 /* helpers */
 void errmsg(char *msg, ...);