# Patch #8 for x3270 3.2.18.
# Corrects the behavior of the font-finding logic when the server returns
# stray fonts to match wild-card specifications.

--- x3270-3.2/screen.c	2001/12/27 04:51:04	1.72
+++ x3270-3.2/screen.c	2002/02/07 03:51:21
@@ -2926,14 +2926,15 @@
 			char *cs_dup;
 			char *cs;
 			char *buf;
+			char *lasts;
 
 			buf = cs_dup = NewString(display_charset);
-			while (!font_found && (cs = strtok(buf, ",")) != CN) {
+			while (!font_found && (cs = strtok_r(buf, ",", &lasts)) != CN) {
 				char *wild;
 
 				buf = CN;
 				wild = xs_buffer("*-r-*-c-*-%s", cs);
-				lff = load_fixed_font(wild, display_charset);
+				lff = load_fixed_font(wild, cs);
 				if (lff != CN)
 					Free(lff);
 				else
@@ -2941,8 +2942,7 @@
 				Free(wild);
 				if (!font_found) {
 					wild = xs_buffer("*-r-*-m-*-%s", cs);
-					lff = load_fixed_font(wild,
-							      display_charset);
+					lff = load_fixed_font(wild, cs);
 					if (lff != CN)
 						Free(lff);
 					else
@@ -3049,16 +3049,24 @@
 {
 	unsigned long a_family_name, a_font_registry, a_font_encoding;
 	char *font_registry = CN, *font_encoding = CN;
-	const char *font_charset;
+	const char *font_charset = CN;
 	Boolean r = False;
 	char *csn0, *ptr, *csn;
+	char *lasts;
 
+#if defined(DEBUG_FONTPICK) /*[*/
+	printf("checking '%s' against %s\n", name, dcsname);
+#endif /*]*/
 	/* Check for scalability. */
 	*scalable = False;
 	if (!force) {
 		*scalable = seems_scalable(name);
-		if (*scalable)
+		if (*scalable) {
+#if defined(DEBUG_FONTPICK) /*[*/
+		    printf("'%s' seems scalable\n", name);
+#endif /*]*/
 		    return False;
+		}
 	}
 
 	if (XGetFontProperty(f, a_registry, &a_font_registry))
@@ -3085,9 +3093,13 @@
 	}
 
 	ptr = csn0 = NewString(dcsname);
-	while (!r && (csn = strtok(ptr, ",")) != CN) {
-		if (force || !strcasecmp(font_charset, csn))
+	while (!r && (csn = strtok_r(ptr, ",", &lasts)) != CN) {
+		if (force || !strcasecmp(font_charset, csn)) {
+#if defined(DEBUG_FONTPICK) /*[*/
+			printf("'%s' says it implements '%s' (want '%s')%s\n", name, font_charset, csn, force? " (forced)": "");
+#endif /*]*/
 			r = True;
+		}
 		ptr = CN;
 	}
 	Free(csn0);
@@ -3095,6 +3107,9 @@
 		Replace(efont_charset, font_charset);
 	} else
 		*wrong_csname = font_charset;
+#if defined(DEBUG_FONTPICK) /*[*/
+	printf("'%s' seems %s\n", name, r? "okay": "bad");
+#endif /*]*/
 	return r;
 }
 
@@ -3180,7 +3195,7 @@
 	if (matches == (char **)NULL)
 		return xs_buffer("Font %s\nnot found", name);
 #if defined(DEBUG_FONTPICK) /*[*/
-	printf("%d fonts found\n", count);
+	printf("%d fonts found for '%s'\n", count, name);
 #endif /*]*/
 	for (i = 0; i < count; i++) {
 #if defined(DEBUG_FONTPICK) /*[*/
@@ -3220,11 +3235,11 @@
 				best = i;
 				best_pixel_size = pixel_size;
 #if defined(DEBUG_FONTPICK) /*[*/
-				printf("best pixel size: %ld\n", pixel_size);
+				printf("best pixel size: %ld '%s'\n", pixel_size, matches[i]);
 #endif /*]*/
 			} else {
 #if defined(DEBUG_FONTPICK) /*[*/
-				printf("not so good: %ld\n", pixel_size);
+				printf("not so good: %ld '%s'\n", pixel_size, matches[i]);
 #endif /*]*/
 			}
 		}
@@ -3783,6 +3798,7 @@
 	int count, i;
 	char **names;
 	char *dupcsn, *csn, *buf;
+	char *lasts;
 
 	/* Clear the old lists. */
 	while (rsfonts != NULL) {
@@ -3863,7 +3879,7 @@
 
 	/* Then expand out the wild-cards. */
 	buf = dupcsn = NewString(charset_name);
-	while ((csn = strtok(buf, ",")) != CN) {
+	while ((csn = strtok_r(buf, ",", &lasts)) != CN) {
 		buf = CN;
 		wild = xs_buffer("*-r-*-c-*-%s", csn);
 		count = 0;
