Upgrade to Bonanza 6.0
[bonanza.git] / io.c
diff --git a/io.c b/io.c
index 14f841e..c0595f1 100644 (file)
--- a/io.c
+++ b/io.c
@@ -27,30 +27,60 @@ static int out_board0( FILE *pf, int piece, int i, int ito, int ifrom,
 #  define OutBoard0(a,b,c,d,e,f) out_board0(a,b,c,d,e,f)
 #endif
 
-static int check_input_buffer( void );
-static int read_command( char **pstr_line_end );
-static void out_hand( FILE *pf, unsigned int hand, const char *str_prefix );
-static void out_hand0( FILE *pf, int n, const char *str_prefix,
-                      const char *str );
+static void CONV out_file( FILE *pf, const char *format, ... );
+static int CONV check_input_buffer( void );
+static int CONV read_command( char **pstr_line_end );
+static void CONV out_hand( FILE *pf, unsigned int hand,
+                          const char *str_prefix );
+static void CONV out_hand0( FILE *pf, int n, const char *str_prefix,
+                           const char *str );
 
 #if ! ( defined(NO_STDOUT) && defined(NO_LOGGING) )
 void
 out( const char *format, ... )
 {
   va_list arg;
-  if ( game_status & flag_quiet ) { return; }
 
 #  if ! defined(NO_STDOUT)
+  if ( !( game_status & flag_nostdout ) )
+    {
+      va_start( arg, format );
+      vprintf( format, arg );
+      va_end( arg );
+      fflush( stdout );
+    }
+#  endif
+
+#  if ! defined(NO_LOGGING)
+  if ( ( strchr( format, '\n' ) != NULL || strchr( format, '\r' ) == NULL )
+       && pf_log != NULL )
+    {
+      va_start( arg, format );
+      vfprintf( pf_log, format, arg ); 
+      va_end( arg );
+      fflush( pf_log );
+    }
+#  endif
+}
+#endif
+
+
+#if defined(USI)
+void CONV
+usi_out( const char *format, ... )
+{
+  va_list arg;
+
   va_start( arg, format );
   vprintf( format, arg );
   va_end( arg );
   fflush( stdout );
-#  endif
 
 #  if ! defined(NO_LOGGING)
   if ( ( strchr( format, '\n' ) != NULL || strchr( format, '\r' ) == NULL )
        && pf_log != NULL )
     {
+      fprintf( pf_log, "OUT: " );
       va_start( arg, format );
       vfprintf( pf_log, format, arg ); 
       va_end( arg );
@@ -77,70 +107,57 @@ out_csashogi( const char *format, ... )
 
 
 void
-out_file( FILE *pf, const char *format, ... )
+out_warning( const char *format, ... )
 {
   va_list arg;
 
-  if ( pf != NULL )
+#if defined(TLP) || defined(DFPN_CLIENT)
+  lock( &io_lock );
+#endif
+
+  if ( !( game_status & flag_nostdout ) )
     {
+      fprintf( stderr, "\n%s", str_warning );
       va_start( arg, format );
-      vfprintf( pf, format, arg ); 
+      vfprintf( stderr, format, arg );
       va_end( arg );
-      fflush( pf );
+      fprintf( stderr, "\n\n" );
+      fflush( stderr );
     }
-  
+
 #if ! defined(NO_LOGGING)
   if ( pf_log != NULL )
     {
+      fprintf( pf_log, "\n%s", str_warning );
       va_start( arg, format );
       vfprintf( pf_log, format, arg ); 
       va_end( arg );
+      fprintf( pf_log, "\n\n" );
       fflush( pf_log );
     }
 #endif
 
+#if defined(TLP) || defined(DFPN_CLIENT)
+  unlock( &io_lock );
+#endif
 }
 
 
 void
-out_warning( const char *format, ... )
+out_error( const char *format, ... )
 {
   va_list arg;
   
-  fprintf( stderr, "\n%s", str_warning );
-  va_start( arg, format );
-  vfprintf( stderr, format, arg );
-  va_end( arg );
-  fprintf( stderr, "\n\n" );
-  fflush( stderr );
-  
-#if ! defined(NO_LOGGING)
-  if ( pf_log != NULL )
+  if ( !( game_status & flag_nostdout ) )
     {
-      fprintf( pf_log, "\n%s", str_warning );
+      fprintf( stderr, "\nERROR: " );
       va_start( arg, format );
-      vfprintf( pf_log, format, arg ); 
+      vfprintf( stderr, format, arg );
       va_end( arg );
-      fprintf( pf_log, "\n\n" );
-      fflush( pf_log );
+      fprintf( stderr, "\n\n" );
+      fflush( stderr );
     }
-#endif
-
-}
 
-
-void
-out_error( const char *format, ... )
-{
-  va_list arg;
-  
-  fprintf( stderr, "\nERROR: " );
-  va_start( arg, format );
-  vfprintf( stderr, format, arg );
-  va_end( arg );
-  fprintf( stderr, "\n\n" );
-  fflush( stderr );
-  
 #if ! defined(NO_LOGGING)
   if ( pf_log != NULL )
     {
@@ -199,13 +216,6 @@ show_prompt( void )
 {
   if ( game_status & flag_noprompt ) { return; }
   
-#if defined(DEKUNOBOU)
-  if ( dek_ngame )
-    {
-      Out( "Won=%3d Lost=%3d Total=%4d/", dek_win, dek_lost, dek_ngame-1 );
-    }
-#endif
-
   if ( game_status & flag_drawn ) { Out( "Drawn> " ); }
   else if ( game_status & flag_mated )
     {
@@ -237,8 +247,7 @@ open_history( const char *str_name1, const char *str_name2 )
   iret = record_close( &record_game );
   if ( iret < 0 ) { return -1; }
 
-  strncpy( str_file, "game.csa", SIZE_FILENAME-1 );
-  iret = record_open( &record_game, str_file, mode_read_write,
+  iret = record_open( &record_game, "game.csa", mode_read_write,
                      str_name1, str_name2 );
   if ( iret < 0 ) { return -1; }
 
@@ -267,34 +276,52 @@ open_history( const char *str_name1, const char *str_name2 )
       return 1;
     }
 
-  iret = file_close( pf_log );
-  if ( iret < 0 ) { return -1; }
-
-  iret = record_close( &record_game );
-  if ( iret < 0 ) { return -1; }
-  
-  for ( i = 0; i < 999; i++ )
+  if ( ( ( game_status & flag_nonewlog )
+#  if defined(USI)
+        ||  usi_mode != usi_off
+#  endif
+        ) && 0 <= record_num )
+    {
+      iret = record_close( &record_game );
+      if ( iret < 0 ) { return -1; }
+      
+      snprintf( str_file, SIZE_FILENAME, "%s/game%03d.csa",
+               str_dir_logs, record_num );
+      iret = record_open( &record_game, str_file, mode_read_write,
+                         str_name1, str_name2 );
+      if ( iret < 0 ) { return -1; }
+    }
+  else
     {
+      iret = file_close( pf_log );
+      if ( iret < 0 ) { return -1; }
+      
+      iret = record_close( &record_game );
+      if ( iret < 0 ) { return -1; }
+      
+      for ( i = 0; i < 999; i++ )
+       {
+         snprintf( str_file, SIZE_FILENAME, "%s/game%03d.csa",
+                   str_dir_logs, i );
+         pf = file_open( str_file, "r" );
+         if ( pf == NULL ) { break; }
+         iret = file_close( pf );
+         if ( iret < 0 ) { return -1; }
+       }
+      record_num = i;
+      
+      snprintf( str_file, SIZE_FILENAME, "%s/n%03d.log",
+               str_dir_logs, i );
+      pf_log = file_open( str_file, "w" );
+      if ( pf_log == NULL ) { return -1; }
+      
       snprintf( str_file, SIZE_FILENAME, "%s/game%03d.csa",
-              str_dir_logs, i );
-      pf = file_open( str_file, "r" );
-      if ( pf == NULL ) { break; }
-      iret = file_close( pf );
+               str_dir_logs, i );
+      iret = record_open( &record_game, str_file, mode_read_write,
+                         str_name1, str_name2 );
       if ( iret < 0 ) { return -1; }
     }
-  irecord_game = i;
-
-  snprintf( str_file, SIZE_FILENAME, "%s/n%03d.log",
-           str_dir_logs, i );
-  pf_log = file_open( str_file, "w" );
-  if ( pf_log == NULL ) { return -1; }
-
-  snprintf( str_file, SIZE_FILENAME, "%s/game%03d.csa",
-           str_dir_logs, i );
-  iret = record_open( &record_game, str_file, mode_read_write,
-                     str_name1, str_name2 );
-  if ( iret < 0 ) { return -1; }
-
+  
   return 1;
 #endif
 }
@@ -310,6 +337,8 @@ out_board( const tree_t * restrict ptree, FILE *pf, unsigned int move,
   int is_promote;
 #endif
 
+  if ( game_status & flag_nostdout ) { return 1; }
+
   if ( ! is_strict && move )
     {
       ito        = I2To( move );
@@ -392,34 +421,7 @@ next_cmdline( int is_wait )
            }
        }
       else {
-#if defined(DEKUNOBOU)
-       if ( dek_ngame )
-         {
-           iret = dek_check();
-           goto tag;
-         }
-#endif
-
-#if defined(CSA_LAN)
-       if ( sckt_csa != SCKT_NULL )
-         {
-           iret = sckt_check( sckt_csa );
-           goto tag;
-         }
-#endif
-
-#if defined(MNJ_LAN)
-       if ( sckt_mnj != SCKT_NULL )
-         {
-           iret = sckt_check( sckt_mnj );
-           goto tag;
-         }
-#endif
        iret = check_input_buffer();
-       
-#if defined(DEKUNOBOU) || defined(CSA_LAN) || defined(MNJ_LAN)
-    tag:
-#endif
        if ( iret <= 0 ) { return iret; }
 
        iret = read_command( & str_line_end );
@@ -445,19 +447,6 @@ next_cmdline( int is_wait )
   memcpy( str_cmdline, str_buffer_cmdline, size );
   *( str_cmdline + size ) = '\0';
   
-#if defined(DEKUNOBOU)
-  if ( dek_ngame )
-    {
-      iret = dek_parse( str_cmdline, SIZE_CMDLINE );
-      if ( iret < 0 )
-       {
-         memmove( str_buffer_cmdline, str_line_end + 1,
-                  strlen( str_line_end + 1 ) + 1 );
-         return iret;
-       }
-    }
-#endif
-
   if ( is_wait )
     {
       out_file( NULL, "%s\n", str_cmdline );
@@ -614,7 +603,7 @@ stdout_stress( int is_promote, int ifrom )
 #endif /* no NO_STDOUT and no WIN32_PIPE */
 
 
-static void
+static void CONV
 out_hand( FILE *pf, unsigned int hand, const char *str_prefix )
 {
   out_hand0( pf, (int)I2HandPawn(hand),   str_prefix, "00FU" );
@@ -627,7 +616,7 @@ out_hand( FILE *pf, unsigned int hand, const char *str_prefix )
 }
 
 
-static void
+static void CONV
 out_hand0( FILE *pf, int n, const char *str_prefix, const char *str )
 {
   int i;
@@ -641,7 +630,7 @@ out_hand0( FILE *pf, int n, const char *str_prefix, const char *str )
 }
 
 
-static int
+static int CONV
 read_command( char ** pstr_line_end )
 {
   char *str_end;
@@ -650,15 +639,6 @@ read_command( char ** pstr_line_end )
   count_cmdbuff = (int)strlen( str_buffer_cmdline );
   str_end       = str_buffer_cmdline + count_cmdbuff;
 
-#if defined(DEKUNOBOU)
-  if ( dek_ngame )
-    {
-      count_byte = dek_in( str_end, SIZE_CMDLINE-1-count_cmdbuff );
-      if ( count_byte < 0 ) { return count_byte; }
-      goto tag;
-    }
-#endif
-
 #if defined(CSA_LAN)
   if ( sckt_csa != SCKT_NULL )
     {
@@ -677,6 +657,15 @@ read_command( char ** pstr_line_end )
     }
 #endif
 
+#if defined(DFPN)
+  if ( dfpn_sckt != SCKT_NULL )
+    {
+      count_byte = sckt_in( dfpn_sckt, str_end, SIZE_CMDLINE-1-count_cmdbuff );
+      if ( count_byte < 0 ) { return count_byte; }
+      goto tag;
+    }
+#endif
+
   do { count_byte = (int)read( 0, str_end, SIZE_CMDBUFFER-1-count_cmdbuff ); }
   while ( count_byte < 0 && errno == EINTR );
   
@@ -687,10 +676,13 @@ read_command( char ** pstr_line_end )
     }
   *( str_end + count_byte ) = '\0';
 
-#if defined(DEKUNOBOU) || defined(CSA_LAN) || defined(MNJ_LAN)
+#if defined(CSA_LAN) || defined(MNJ_LAN) || defined(DFPN)
  tag:
 #endif
 
+#if defined(USI)
+  if ( usi_mode != usi_off ) { Out( "IN: %s[END]\n", str_end );}
+#endif
 
   *pstr_line_end = strchr( str_buffer_cmdline, '\n' );
   if ( *pstr_line_end == NULL
@@ -704,69 +696,104 @@ read_command( char ** pstr_line_end )
   return count_byte;
 }
 
-#if defined(_WIN32)
 
-static int
+static int CONV
 check_input_buffer( void )
 {
-#  if defined(WIN32_PIPE)
-  BOOL bSuccess;
-  HANDLE hHandle;
-  DWORD dwBytesRead, dwTotalBytesAvail, dwBytesLeftThisMessage;
-  char buf[1];
+#if defined(CSA_LAN)
+  if ( sckt_csa != SCKT_NULL ) { return sckt_check( sckt_csa ); }
+#endif
+  
+#if defined(MNJ_LAN)
+  if ( sckt_mnj != SCKT_NULL ) { return sckt_check( sckt_mnj ); }
+#endif
 
-  hHandle = GetStdHandle( STD_INPUT_HANDLE );
-  if ( hHandle == INVALID_HANDLE_VALUE )
-    {
-      str_error = "GetStdHandle() faild.";
-      return -1;
-    }
-  bSuccess = PeekNamedPipe( hHandle, buf, 1, &dwBytesRead, &dwTotalBytesAvail,
-                           &dwBytesLeftThisMessage );
-  if ( ! bSuccess )
-    {
-      str_error = "PeekNamedPipe() faild.";
-      return -1;
-    }
-  if ( dwBytesRead ) { return 1; }
-  return 0;
-#  else
-  return _kbhit();
+#if defined(DFPN)
+  if ( dfpn_sckt != SCKT_NULL ) { return sckt_check( dfpn_sckt ); }
+#endif
+
+  {
+#if defined(_WIN32) && defined(WIN32_PIPE)
+    BOOL bSuccess;
+    HANDLE hHandle;
+    DWORD dwBytesRead, dwTotalBytesAvail, dwBytesLeftThisMessage;
+    char buf[1];
+
+    hHandle = GetStdHandle( STD_INPUT_HANDLE );
+    if ( hHandle == INVALID_HANDLE_VALUE )
+      {
+       str_error = "GetStdHandle() faild.";
+       return -1;
+      }
+    bSuccess = PeekNamedPipe( hHandle, buf, 1, &dwBytesRead,
+                             &dwTotalBytesAvail, &dwBytesLeftThisMessage );
+    if ( ! bSuccess )
+      {
+       str_error = "PeekNamedPipe() faild.";
+       return -1;
+      }
+    if ( dwBytesRead ) { return 1; }
+    return 0;
+
+#elif defined(_WIN32)
+
+    return _kbhit();
+
+#else
+
+    fd_set readfds;
+    struct timeval tv;
+    int iret;
+
+#  if defined(__ICC)
+#    pragma warning(disable:279)
+#    pragma warning(disable:593)
+#    pragma warning(disable:1469)
+#  endif
+    
+    FD_ZERO(&readfds);
+    FD_SET(0, &readfds);
+    tv.tv_sec  = 0;
+    tv.tv_usec = 0;
+    iret       = select( 1, &readfds, NULL, NULL, &tv );
+    if ( iret == -1 )
+      {
+       str_error = "select() faild.";
+       return -1;
+      }
+    return iret;
+    
+#  if defined(__ICC)
+#    pragma warning(default:279)
+#    pragma warning(default:593)
+#    pragma warning(default:1469)
 #  endif
+#endif /* no _WIN32 */
+  }
 }
 
-#else /* no _WIN32 */
 
-static int
-check_input_buffer( void )
+static void CONV
+out_file( FILE *pf, const char *format, ... )
 {
-  fd_set readfds;
-  struct timeval tv;
-  int iret;
-
-#if defined(__ICC)
-#  pragma warning(disable:279)
-#  pragma warning(disable:593)
-#  pragma warning(disable:1469)
-#endif
+  va_list arg;
 
-  FD_ZERO(&readfds);
-  FD_SET(0, &readfds);
-  tv.tv_sec  = 0;
-  tv.tv_usec = 0;
-  iret       = select( 1, &readfds, NULL, NULL, &tv );
-  if ( iret == -1 )
+  if ( pf != NULL )
     {
-      str_error = "select() faild.";
-      return -1;
+      va_start( arg, format );
+      vfprintf( pf, format, arg ); 
+      va_end( arg );
+      fflush( pf );
+    }
+  
+#if ! defined(NO_LOGGING)
+  if ( pf_log != NULL )
+    {
+      va_start( arg, format );
+      vfprintf( pf_log, format, arg ); 
+      va_end( arg );
+      fflush( pf_log );
     }
-  return iret;
-
-#if defined(__ICC)
-#  pragma warning(default:279)
-#  pragma warning(default:593)
-#  pragma warning(default:1469)
 #endif
-}
 
-#endif /* no _WIN32 */
+}