shithub: neatroff

Download patch

ref: c0958e44843f678a014c3ede1b5f4d857fa10784
parent: 246326f41195d0ccf1f5c88c8e8884b2cacd1103
author: Ali Gholami Rudi <[email protected]>
date: Sat Sep 30 11:35:03 EDT 2017

dev: finding font position automatically for .fp request

In Neatroff, if instead of the position of the font to be mounted,
the second argument of .fp is a dash, the position of the font is
decided automatically: if a font with the same name is already
mounted, the same position is reused.  Otherwise the font is mounted
on the first available font position.

--- a/dev.c
+++ b/dev.c
@@ -37,6 +37,19 @@
 	out("x init\n");
 }
 
+/* find a position for the given font */
+static int dev_position(char *id)
+{
+	int i;
+	for (i = 1; i < NFONTS; i++)	/* already mounted */
+		if (!strcmp(fn_name[i], id))
+			return i;
+	for (i = 1; i < NFONTS; i++)	/* the first empty position */
+		if (!fn_font[i])
+			return i;
+	return 0;			/* no room left */
+}
+
 int dev_mnt(int pos, char *id, char *name)
 {
 	char path[PATHLEN];
@@ -50,6 +63,8 @@
 	fn = font_open(path);
 	if (!fn)
 		return -1;
+	if (pos < 0)
+		pos = dev_position(id);
 	if (fn_font[pos])
 		font_close(fn_font[pos]);
 	if (fn_name[pos] != name)	/* ignore if fn_name[pos] is passed */
--- a/ren.c
+++ b/ren.c
@@ -564,9 +564,11 @@
 
 void tr_fp(char **args)
 {
+	int pos;
 	if (!args[2])
 		return;
-	if (dev_mnt(atoi(args[1]), args[2], args[3] ? args[3] : args[2]) < 0)
+	pos = isdigit((unsigned char) args[1][0]) ? atoi(args[1]) : -1;
+	if (dev_mnt(pos, args[2], args[3] ? args[3] : args[2]) < 0)
 		errmsg("neatroff: failed to mount <%s>\n", args[2]);
 }