shithub: neatroff

Download patch

ref: 4e0c1829c7b5e17414f453fc22e6533978632d4c
parent: 867f1772c21359c2577857c6ca22bf3365271a34
author: Ali Gholami Rudi <[email protected]>
date: Mon Oct 14 07:19:22 EDT 2013

tr: use cdef_*() for char definition and cmap_*() for char translation

--- a/dev.c
+++ b/dev.c
@@ -133,7 +133,7 @@
 		c++;
 	if (c[0] == c_ec && c[1] == '(')
 		c += 2;
-	c = tr_map(c);
+	c = cmap_map(c);
 	g = font_find(fn_font[fn], c);
 	if (g)
 		return g;
--- a/out.c
+++ b/out.c
@@ -148,7 +148,7 @@
 			}
 			if (c[0] == '\t' || c[0] == '' || !strcmp(c_hc, c))
 				continue;
-			outc(tr_map(c));
+			outc(cmap_map(c));
 			continue;
 		}
 		switch (t) {
--- a/ren.c
+++ b/ren.c
@@ -730,13 +730,14 @@
 			strcpy(c, g ? g->name : "cnull");
 		}
 	}
+	if (!ren_div && cdef_map(c)) {			/* .char characters */
+		wb_putexpand(wb, c);
+		return;
+	}
 	if (!n_lg || ren_div || wb_lig(wb, c)) {
 		if (n_kn && !ren_div)
 			wb_kern(wb, c);
-		if (ren_div)
-			wb_put(wb, c);	/* disable .char for diverted text */
-		else
-			wb_putexpand(wb, c);
+		wb_put(wb, c);
 	}
 }
 
@@ -899,28 +900,21 @@
 	wb_done(&t);
 }
 
-static int ren_expanding;	/* expanding the definition of a character */
-
-/* expand the given defined character */
-int ren_expand(struct wb *wb, char *n)
+/* parse characters and troff requests of s and append them to wb */
+int ren_parse(struct wb *wb, char *s)
 {
-	char *s = chdef_map(n);
 	int c;
-	if (!s || ren_expanding)
-		return 1;
-	ren_expanding = 1;
 	odiv_beg();
 	sstr_push(s);
 	c = sstr_next();
 	while (c >= 0) {
 		sstr_back(c);
-		if (ren_chardel(wb, sstr_next, sstr_back, NULL, NULL))
+		if (ren_char(wb, sstr_next, sstr_back))
 			break;
 		c = sstr_next();
 	}
 	sstr_pop();
 	odiv_end();
-	ren_expanding = 0;
 	return 0;
 }
 
--- a/roff.h
+++ b/roff.h
@@ -24,12 +24,12 @@
 #define NTRAPS		1024	/* number of traps per page */
 #define NIES		128	/* number of nested .ie commands */
 #define NTABS		16	/* number of tab stops */
-#define NTR		512	/* number of character translations (.tr) */
+#define NCMAPS		512	/* number of character translations (.tr) */
 #define NSSTR		32	/* number of nested sstr_push() calls */
 #define NFIELDS		32	/* number of fields */
 #define MAXFRAC		100000	/* maximum value of the fractional part */
 #define LIGLEN		4	/* length of ligatures */
-#define NCHDEF		128	/* number of character definitions (.char) */
+#define NCDEFS		128	/* number of character definitions (.char) */
 
 /* escape sequences */
 #define ESC_Q	"bCDhHlLNoSvwxX"	/* \X'ccc' quoted escape sequences */
@@ -175,12 +175,6 @@
 #define cp_back		in_back	/* cp.c is stateless */
 void tr_first(void);		/* read until the first non-command line */
 
-/* character translation (.tr) */
-void tr_add(char *c1, char *c2);
-char *tr_map(char *c);
-/* character definition (.char) */
-char *chdef_map(char *c);
-
 /* variable length string buffer */
 struct sbuf {
 	char *s;		/* allocated buffer */
@@ -242,6 +236,13 @@
 int wb_lig(struct wb *wb, char *c);
 int wb_kern(struct wb *wb, char *c);
 
+/* character translation (.tr) */
+void cmap_add(char *c1, char *c2);
+char *cmap_map(char *c);
+/* character definition (.char) */
+char *cdef_map(char *c);
+int cdef_expand(struct wb *wb, char *c);
+
 /* hyphenation flags */
 #define HY_MASK		0x0f	/* enable hyphenation */
 #define HY_LAST		0x02	/* do not hyphenate last lines */
@@ -275,6 +276,7 @@
 
 /* rendering */
 int render(void);				/* the main loop */
+int ren_parse(struct wb *wb, char *c);
 int ren_char(struct wb *wb, int (*next)(void), void (*back)(int));
 int ren_wid(int (*next)(void), void (*back)(int));
 void ren_tl(int (*next)(void), void (*back)(int));
@@ -284,7 +286,6 @@
 void ren_bcmd(struct wb *wb, char *arg);	/* \b */
 void ren_ocmd(struct wb *wb, char *arg);	/* \o */
 void ren_dcmd(struct wb *wb, char *arg);	/* \D */
-int ren_expand(struct wb *wb, char *c);		/* .char expansion */
 
 /* out.c */
 void out_line(char *s);				/* output rendered line */
--- a/tr.c
+++ b/tr.c
@@ -493,35 +493,35 @@
 		in_lf(args[2], eval(args[1], 0));
 }
 
-/* character translation */
-static char tr_src[NTR][GNLEN];
-static char tr_dst[NTR][GNLEN];
-static int tr_n;
+/* character translation (.tr) */
+static char cmap_src[NCMAPS][GNLEN];	/* source character */
+static char cmap_dst[NCMAPS][GNLEN];	/* character mapping */
+static int cmap_n;			/* number of translated character */
 
 static int tr_find(char *c)
 {
 	int i;
-	for (i = 0; i < tr_n; i++)
-		if (!strcmp(c, tr_src[i]))
+	for (i = 0; i < cmap_n; i++)
+		if (!strcmp(c, cmap_src[i]))
 			return i;
 	return -1;
 }
 
-void tr_add(char *c1, char *c2)
+void cmap_add(char *c1, char *c2)
 {
 	int i = tr_find(c1);
-	if (i < 0 && tr_n < NTR)
-		i = tr_n++;
+	if (i < 0 && cmap_n < NCMAPS)
+		i = cmap_n++;
 	if (i >= 0) {
-		strcpy(tr_src[i], c1);
-		strcpy(tr_dst[i], c2);
+		strcpy(cmap_src[i], c1);
+		strcpy(cmap_dst[i], c2);
 	}
 }
 
-char *tr_map(char *c)
+char *cmap_map(char *c)
 {
 	int i = tr_find(c);
-	return i >= 0 ? tr_dst[i] : c;
+	return i >= 0 ? cmap_dst[i] : c;
 }
 
 static void tr_tr(char **args)
@@ -531,31 +531,43 @@
 	while (s && charread(&s, c1) >= 0) {
 		if (charread(&s, c2) < 0)
 			strcpy(c2, " ");
-		tr_add(c1, c2);
+		cmap_add(c1, c2);
 	}
 }
 
-/* character definitions */
-static char chdef_src[NCHDEF][GNLEN];
-static char *chdef_dst[NCHDEF];
-static int chdef_n;
+/* character definition (.char) */
+static char cdef_src[NCDEFS][GNLEN];	/* source character */
+static char *cdef_dst[NCDEFS];		/* character definition */
+static int cdef_n;			/* number of defined characters */
+static int cdef_expanding;		/* inside cdef_expand() call */
 
-static int chdef_find(char *c)
+static int cdef_find(char *c)
 {
 	int i;
-	for (i = 0; i < chdef_n; i++)
-		if (!strcmp(chdef_src[i], c))
+	for (i = 0; i < cdef_n; i++)
+		if (!strcmp(cdef_src[i], c))
 			return i;
 	return -1;
 }
 
 /* return the definition of the given character */
-char *chdef_map(char *c)
+char *cdef_map(char *c)
 {
-	int i = chdef_find(c);
-	return i >= 0 ? chdef_dst[i] : NULL;
+	int i = cdef_find(c);
+	return !cdef_expanding && i >= 0 ? cdef_dst[i] : NULL;
 }
 
+int cdef_expand(struct wb *wb, char *s)
+{
+	char *d = cdef_map(s);
+	if (!d)
+		return 1;
+	cdef_expanding = 1;
+	ren_parse(wb, d);
+	cdef_expanding = 0;
+	return 0;
+}
+
 static void tr_char(char **args)
 {
 	char c[GNLEN];
@@ -563,13 +575,13 @@
 	int i;
 	if (!args[2] || charread(&s, c) < 0)
 		return;
-	i = chdef_find(c);
-	if (i < 0 && chdef_n < NCHDEF)
-		i = chdef_n++;
+	i = cdef_find(c);
+	if (i < 0 && cdef_n < NCDEFS)
+		i = cdef_n++;
 	if (i >= 0) {
-		strncpy(chdef_src[i], c, sizeof(chdef_src[i]) - 1);
-		chdef_dst[i] = malloc(strlen(args[2]) + 1);
-		strcpy(chdef_dst[i], args[2]);
+		strncpy(cdef_src[i], c, sizeof(cdef_src[i]) - 1);
+		cdef_dst[i] = malloc(strlen(args[2]) + 1);
+		strcpy(cdef_dst[i], args[2]);
 	}
 }
 
@@ -581,9 +593,9 @@
 	for (i = 1; i <= NARGS; i++) {
 		s = args[i];
 		if (s && charread(&s, c) >= 0) {
-			if (chdef_find(c) >= 0) {
-				free(chdef_dst[chdef_find(c)]);
-				chdef_dst[chdef_find(c)] = NULL;
+			if (cdef_find(c) >= 0) {
+				free(cdef_dst[cdef_find(c)]);
+				cdef_dst[cdef_find(c)] = NULL;
 			}
 		}
 	}
--- a/wb.c
+++ b/wb.c
@@ -164,10 +164,10 @@
 	}
 }
 
-/* just like wb_put(), but call chdef_expand() if c is defined */
+/* just like wb_put(), but call cdef_expand() if c is defined */
 void wb_putexpand(struct wb *wb, char *c)
 {
-	if (ren_expand(wb, c))
+	if (cdef_expand(wb, c))
 		wb_put(wb, c);
 }