$NetBSD: patch-issue-525,v 1.1 2022/12/13 18:41:52 rhialto Exp $

Cherry-pick from
https://github.com/SDL-Hercules-390/hyperion/commit/2e2e942fb38f43842e8a9e2800aa42c346bfb050.patch

From 2e2e942fb38f43842e8a9e2800aa42c346bfb050 Mon Sep 17 00:00:00 2001
From: Bill Lewis <wrljet@gmail.com>
Date: Sat, 10 Dec 2022 12:15:20 -0500
Subject: [PATCH] Fix for Issue #525

Don't issue messages when setting the CODEPAGE from Hercifc, as they can wind up in the stream, corrupting data exchange with Hercules.
---
 codepage.c | 36 ++++++++++++++++++++++++++----------
 codepage.h |  1 +
 hercifc.c  |  2 +-
 tuntap.c   |  2 +-
 4 files changed, 29 insertions(+), 12 deletions(-)

diff --git a/codepage.c b/codepage.c
index 4c3325ad1..7e507e245 100644
--- codepage.c
+++ codepage.c
@@ -1076,7 +1076,10 @@ DLL_EXPORT bool valid_codepage_name( const char* name )
     return false;
 }
 
-DLL_EXPORT void set_codepage( const char* name )
+#define SETCODEPAGE_SILENT   1
+#define SETCODEPAGE_VERBOSE  2
+
+static void set_codepage_internal( const char* name, int msgflag )
 {
     int dflt = FALSE;
 
@@ -1089,7 +1092,7 @@ DLL_EXPORT void set_codepage( const char* name )
 
     if( strcasecmp(name,"user") == 0 && user_in_use == FALSE )
     {
-        WRMSG( HHC01477, "W" );
+        if( msgflag == SETCODEPAGE_VERBOSE) WRMSG( HHC01477, "W" );
         name = "default";
     }
 
@@ -1100,18 +1103,31 @@ DLL_EXPORT void set_codepage( const char* name )
     if( codepage_conv->name && strcasecmp(codepage_conv->name,"user") == 0 && user_in_use == FALSE )
         codepage_conv++;
 
-    if(codepage_conv->name)
-    {
-        if (!dflt)
-            WRMSG(HHC01474, "I", "internal", name);
-    }
-    else
+    if( msgflag == SETCODEPAGE_VERBOSE )
     {
-        if (!dflt)
-            WRMSG (HHC01475, "E", name);
+        if(codepage_conv->name)
+        {
+            if (!dflt)
+                WRMSG(HHC01474, "I", "internal", name);
+        }
+        else
+        {
+            if (!dflt)
+                WRMSG (HHC01475, "E", name);
+        }
     }
 }
 
+DLL_EXPORT void set_codepage( const char* name )
+{
+    set_codepage_internal( name, SETCODEPAGE_VERBOSE );
+}
+
+DLL_EXPORT void set_codepage_no_msgs( const char* name )
+{
+    set_codepage_internal( name, SETCODEPAGE_SILENT );
+}
+
 DLL_EXPORT int update_codepage(int argc, char *argv[], char *cmd )
 {
     int rc = 0;
diff --git a/codepage.h b/codepage.h
index 7e4edbc98..a44a58d14 100644
--- codepage.h
+++ codepage.h
@@ -14,6 +14,7 @@
 COD_DLL_IMPORT const char* query_codepage();
 COD_DLL_IMPORT bool valid_codepage_name( const char* name );
 COD_DLL_IMPORT void set_codepage( const char *name);
+COD_DLL_IMPORT void set_codepage_no_msgs( const char* name );
 COD_DLL_IMPORT int update_codepage(int argc, char *argv[], char *table );
 COD_DLL_IMPORT unsigned char host_to_guest (unsigned char byte);
 COD_DLL_IMPORT unsigned char guest_to_host (unsigned char byte);
diff --git a/hercifc.c b/hercifc.c
index d8260d247..1e2ceebc9 100644
--- hercifc.c
+++ hercifc.c
@@ -62,7 +62,7 @@ int main( int argc, char **argv )
         sysblk.msglvl = atoi( argv[1] );
 
         if (argv[2])
-            set_codepage( argv[2] );
+            set_codepage_no_msgs( argv[2] );
     }
 
     // Must not be run from the commandline
diff --git a/tuntap.c b/tuntap.c
index 90708f087..11ddae78a 100644
--- tuntap.c
+++ tuntap.c
@@ -222,7 +222,7 @@ static int TUNTAP_SetMode (int fd, struct hifr *hifr, int iFlags)
 // Output:
 //      pfd            Pointer to receive the file descriptor of the
 //                       TUN/TAP interface.
-//      pszNetDevName  Pointer to receive the name if the interface.
+//      pszNetDevName  Pointer to receive the name of the interface.
 
 int             TUNTAP_CreateInterface( char* pszTUNDevice,
                                         int   iFlags,
