10 # include <sys/time.h>
11 # include <sys/types.h>
18 # define fopen( file, mode ) _fsopen( file, mode, _SH_DENYNO )
21 #if defined(NO_STDOUT) || defined(WIN32_PIPE)
22 static int out_board0( FILE *pf, int piece, int i, int ito, int ifrom );
23 # define OutBoard0(a,b,c,d,e,f) out_board0(a,b,c,d,e)
25 static int out_board0( FILE *pf, int piece, int i, int ito, int ifrom,
27 # define OutBoard0(a,b,c,d,e,f) out_board0(a,b,c,d,e,f)
30 static void CONV out_file( FILE *pf, const char *format, ... );
31 static int CONV check_input_buffer( void );
32 static int CONV read_command( char **pstr_line_end );
33 static void CONV out_hand( FILE *pf, unsigned int hand,
34 const char *str_prefix );
35 static void CONV out_hand0( FILE *pf, int n, const char *str_prefix,
38 #if ! ( defined(NO_STDOUT) && defined(NO_LOGGING) )
40 out( const char *format, ... )
44 # if ! defined(NO_STDOUT)
45 if ( !( game_status & flag_nostdout ) )
47 va_start( arg, format );
48 vprintf( format, arg );
54 # if ! defined(NO_LOGGING)
55 if ( ( strchr( format, '\n' ) != NULL || strchr( format, '\r' ) == NULL )
58 va_start( arg, format );
59 vfprintf( pf_log, format, arg );
70 usi_out( const char *format, ... )
74 va_start( arg, format );
75 vprintf( format, arg );
79 # if ! defined(NO_LOGGING)
80 if ( ( strchr( format, '\n' ) != NULL || strchr( format, '\r' ) == NULL )
83 fprintf( pf_log, "OUT: " );
84 va_start( arg, format );
85 vfprintf( pf_log, format, arg );
96 out_csashogi( const char *format, ... )
100 va_start( arg, format );
101 vprintf( format, arg );
110 out_warning( const char *format, ... )
114 #if defined(TLP) || defined(DFPN_CLIENT)
118 if ( !( game_status & flag_nostdout ) )
120 fprintf( stderr, "\n%s", str_warning );
121 va_start( arg, format );
122 vfprintf( stderr, format, arg );
124 fprintf( stderr, "\n\n" );
128 #if ! defined(NO_LOGGING)
129 if ( pf_log != NULL )
131 fprintf( pf_log, "\n%s", str_warning );
132 va_start( arg, format );
133 vfprintf( pf_log, format, arg );
135 fprintf( pf_log, "\n\n" );
140 #if defined(TLP) || defined(DFPN_CLIENT)
147 out_error( const char *format, ... )
151 if ( !( game_status & flag_nostdout ) )
153 fprintf( stderr, "\nERROR: " );
154 va_start( arg, format );
155 vfprintf( stderr, format, arg );
157 fprintf( stderr, "\n\n" );
161 #if ! defined(NO_LOGGING)
162 if ( pf_log != NULL )
164 fprintf( pf_log, "\nERROR: " );
165 va_start( arg, format );
166 vfprintf( pf_log, format, arg );
168 fprintf( pf_log, "\n\n" );
177 file_open( const char *str_file, const char *str_mode )
181 pf = fopen( str_file, str_mode );
184 snprintf( str_message, SIZE_MESSAGE,
185 "%s, %s", str_fopen_error, str_file );
186 str_error = str_message;
195 file_close( FILE *pf )
197 if ( pf == NULL ) { return 1; }
201 str_error = str_io_error;
206 str_error = str_io_error;
217 if ( game_status & flag_noprompt ) { return; }
219 if ( game_status & flag_drawn ) { Out( "Drawn> " ); }
220 else if ( game_status & flag_mated )
222 if ( root_turn ) { Out( "Black Mated> " ); }
223 else { Out( "White Mated> " ); }
225 else if ( game_status & flag_resigned )
227 if ( root_turn ) { Out( "White Resigned> " ); }
228 else { Out( "Black Resigned> " ); }
230 else if ( game_status & flag_suspend )
232 if ( root_turn ) { Out( "White Suspend> " ); }
233 else { Out( "Black Suspend> " ); }
235 else if ( root_turn ) { Out( "White %d> ", record_game.moves+1 ); }
241 else { Out( SIGN "Black %d> ", record_game.moves+1 ); }
246 open_history( const char *str_name1, const char *str_name2 )
248 #if defined(NO_LOGGING)
249 char str_file[SIZE_FILENAME];
252 iret = record_close( &record_game );
253 if ( iret < 0 ) { return -1; }
255 iret = record_open( &record_game, "game.csa", mode_read_write,
256 str_name1, str_name2 );
257 if ( iret < 0 ) { return -1; }
263 char str_file[SIZE_FILENAME];
265 if ( record_game.pf != NULL && ! record_game.moves )
267 record_game.str_name1[0] = '\0';
268 record_game.str_name2[0] = '\0';
272 strncpy( record_game.str_name1, str_name1, SIZE_PLAYERNAME-1 );
273 record_game.str_name1[SIZE_PLAYERNAME-1] = '\0';
278 strncpy( record_game.str_name2, str_name2, SIZE_PLAYERNAME-1 );
279 record_game.str_name2[SIZE_PLAYERNAME-1] = '\0';
284 if ( ( ( game_status & flag_nonewlog )
286 || usi_mode != usi_off
288 ) && 0 <= record_num )
290 iret = record_close( &record_game );
291 if ( iret < 0 ) { return -1; }
293 snprintf( str_file, SIZE_FILENAME, "%s/game%03d.csa",
294 str_dir_logs, record_num );
295 iret = record_open( &record_game, str_file, mode_read_write,
296 str_name1, str_name2 );
297 if ( iret < 0 ) { return -1; }
301 iret = file_close( pf_log );
302 if ( iret < 0 ) { return -1; }
304 iret = record_close( &record_game );
305 if ( iret < 0 ) { return -1; }
307 for ( i = 0; i < 999; i++ )
309 snprintf( str_file, SIZE_FILENAME, "%s/game%03d.csa",
311 pf = file_open( str_file, "r" );
312 if ( pf == NULL ) { break; }
313 iret = file_close( pf );
314 if ( iret < 0 ) { return -1; }
318 snprintf( str_file, SIZE_FILENAME, "%s/n%03d.log",
320 pf_log = file_open( str_file, "w" );
321 if ( pf_log == NULL ) { return -1; }
323 snprintf( str_file, SIZE_FILENAME, "%s/game%03d.csa",
325 iret = record_open( &record_game, str_file, mode_read_write,
326 str_name1, str_name2 );
327 if ( iret < 0 ) { return -1; }
336 out_board( const tree_t * restrict ptree, FILE *pf, unsigned int move,
339 int irank, ifile, i, iret, ito, ifrom;
341 #if ! defined(WIN32_PIPE)
345 if ( game_status & flag_nostdout ) { return 1; }
347 if ( ! is_strict && move )
350 ifrom = I2From( move );
351 #if ! defined(NO_STDOUT) && ! defined(WIN32_PIPE)
352 is_promote = I2IsPromote( move );
356 ito = ifrom = nsquare;
357 #if ! defined(NO_STDOUT) && ! defined(WIN32_PIPE)
362 if ( ( game_status & flag_reverse ) && ! is_strict )
364 fprintf( pf, " <reversed> \n" );
365 fprintf( pf, "' 1 2 3 4 5 6 7 8 9\n" );
367 for ( irank = rank9; irank >= rank1; irank-- )
369 fprintf( pf, "P%d", irank + 1 );
371 for ( ifile = file9; ifile >= file1; ifile-- )
373 i = irank * nfile + ifile;
374 iret = OutBoard0( pf, BOARD[i], i, ito, ifrom, is_promote );
375 if ( iret < 0 ) { return iret; }
381 fprintf( pf, "' 9 8 7 6 5 4 3 2 1\n" );
383 for ( irank = rank1; irank <= rank9; irank++ )
385 fprintf( pf, "P%d", irank + 1 );
387 for ( ifile = file1; ifile <= file9; ifile++ )
389 i = irank * nfile + ifile;
390 iret = OutBoard0( pf, BOARD[i], i, ito, ifrom, is_promote );
391 if ( iret < 0 ) { return iret; }
397 out_hand( pf, HAND_B, "P+" );
398 out_hand( pf, HAND_W, "P-" );
406 next_cmdline( int is_wait )
412 str_line_end = strchr( str_buffer_cmdline, '\n' );
414 if ( ! str_line_end )
419 iret = read_command( & str_line_end );
420 if ( iret < 0 ) { return iret; }
421 } while ( ! str_line_end && iret );
424 game_status |= flag_quit;
429 iret = check_input_buffer();
430 if ( iret <= 0 ) { return iret; }
432 iret = read_command( & str_line_end );
433 if ( iret < 0 ) { return iret; }
436 game_status |= flag_quit;
439 if ( ! str_line_end ) { return 0; }
443 if ( str_line_end - str_buffer_cmdline + 1 >= SIZE_CMDLINE )
445 str_error = str_ovrflw_line;
446 memmove( str_buffer_cmdline, str_line_end + 1,
447 strlen( str_line_end + 1 ) + 1 );
451 size = str_line_end - str_buffer_cmdline;
452 memcpy( str_cmdline, str_buffer_cmdline, size );
453 *( str_cmdline + size ) = '\0';
457 out_file( NULL, "%s\n", str_cmdline );
458 memmove( str_buffer_cmdline, str_line_end + 1,
459 strlen( str_line_end + 1 ) + 1 );
467 #if defined(NO_STDOUT) || defined(WIN32_PIPE)
468 out_board0( FILE *pf, int piece, int i, int ito, int ifrom )
470 out_board0( FILE *pf, int piece, int i, int ito, int ifrom, int is_promote )
474 #if ! ( defined(NO_STDOUT) || defined(WIN32_PIPE) )
480 ch = piece < 0 ? '-' : '+';
481 #if ! ( defined(NO_STDOUT) || defined(WIN32_PIPE) )
482 if ( i == ito && pf == stdout && ! ( game_status & flag_nostress ) )
484 iret = StdoutStress( is_promote, ifrom );
492 fprintf( pf, "%c%s", ch, astr_table_piece[ abs( piece ) ] );
493 #if ! ( defined(NO_STDOUT) || defined(WIN32_PIPE) )
494 if ( i == ito && pf == stdout && ! ( game_status & flag_nostress ) )
496 iret = StdoutNormal();
508 || ( adirec[ito][ifrom]
509 && adirec[ito][ifrom] == adirec[ito][i]
510 && ( ( ito < i && i <= ifrom )
511 || ( ito > i && i >= ifrom ) ) ) ) )
515 else { fprintf( pf, " * " ); }
522 #if ! defined(NO_STDOUT) && ! defined(WIN32_PIPE)
527 if ( game_status & flag_nobeep ) { return; }
530 if ( ! MessageBeep( MB_OK ) )
532 out_warning( "Beep is not available." );
541 stdout_normal( void )
543 # if defined(_MSC_VER)
547 hStdout = GetStdHandle( STD_OUTPUT_HANDLE );
548 if ( hStdout == INVALID_HANDLE_VALUE )
550 str_error = "GetStdHandle() faild";
554 wAttributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
555 if ( ! SetConsoleTextAttribute( hStdout, wAttributes ) )
557 str_error = "SetConsoleTextAttribute() faild";
569 stdout_stress( int is_promote, int ifrom )
571 # if defined(_MSC_VER)
575 hStdout = GetStdHandle( STD_OUTPUT_HANDLE );
576 if ( hStdout == INVALID_HANDLE_VALUE )
578 str_error = "GetStdHandle() faild";
584 wAttributes = BACKGROUND_RED | BACKGROUND_INTENSITY;
586 else if ( ifrom >= nsquare )
588 wAttributes = BACKGROUND_BLUE | BACKGROUND_INTENSITY;
591 wAttributes = ( BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE
592 | BACKGROUND_INTENSITY );
594 if ( ! SetConsoleTextAttribute( hStdout, wAttributes ) )
596 str_error = "SetConsoleTextAttribute() faild";
600 if ( is_promote ) { printf( "\033[7;31m" ); }
601 else if ( ifrom >= nsquare ) { printf( "\033[7;34m" ); }
602 else { printf( "\033[7m" ); }
608 #endif /* no NO_STDOUT and no WIN32_PIPE */
612 out_hand( FILE *pf, unsigned int hand, const char *str_prefix )
614 out_hand0( pf, (int)I2HandPawn(hand), str_prefix, "00FU" );
615 out_hand0( pf, (int)I2HandLance(hand), str_prefix, "00KY" );
616 out_hand0( pf, (int)I2HandKnight(hand), str_prefix, "00KE" );
617 out_hand0( pf, (int)I2HandSilver(hand), str_prefix, "00GI" );
618 out_hand0( pf, (int)I2HandGold(hand), str_prefix, "00KI" );
619 out_hand0( pf, (int)I2HandBishop(hand), str_prefix, "00KA" );
620 out_hand0( pf, (int)I2HandRook(hand), str_prefix, "00HI" );
625 out_hand0( FILE *pf, int n, const char *str_prefix, const char *str )
631 fprintf( pf, str_prefix );
632 for ( i = 0; i < n; i++ ) { fprintf( pf, str ); }
639 read_command( char ** pstr_line_end )
642 int count_byte, count_cmdbuff;
644 count_cmdbuff = (int)strlen( str_buffer_cmdline );
645 str_end = str_buffer_cmdline + count_cmdbuff;
648 if ( sckt_csa != SCKT_NULL )
650 count_byte = sckt_in( sckt_csa, str_end, SIZE_CMDLINE-1-count_cmdbuff );
651 if ( count_byte < 0 ) { return count_byte; }
657 if ( sckt_mnj != SCKT_NULL )
659 count_byte = sckt_in( sckt_mnj, str_end, SIZE_CMDLINE-1-count_cmdbuff );
660 if ( count_byte < 0 ) { return count_byte; }
666 if ( dfpn_sckt != SCKT_NULL )
668 count_byte = sckt_in( dfpn_sckt, str_end, SIZE_CMDLINE-1-count_cmdbuff );
669 if ( count_byte < 0 ) { return count_byte; }
674 do { count_byte = (int)read( 0, str_end, SIZE_CMDBUFFER-1-count_cmdbuff ); }
675 while ( count_byte < 0 && errno == EINTR );
677 if ( count_byte < 0 )
679 str_error = "read() faild.";
682 *( str_end + count_byte ) = '\0';
684 #if defined(CSA_LAN) || defined(MNJ_LAN) || defined(DFPN)
689 if ( usi_mode != usi_off ) { Out( "IN: %s[END]\n", str_end );}
692 *pstr_line_end = strchr( str_buffer_cmdline, '\n' );
693 if ( *pstr_line_end == NULL
694 && count_byte + count_cmdbuff + 1 >= SIZE_CMDLINE )
696 *str_buffer_cmdline = '\0';
697 str_error = str_ovrflw_line;
706 check_input_buffer( void )
709 if ( sckt_csa != SCKT_NULL ) { return sckt_check( sckt_csa ); }
713 if ( sckt_mnj != SCKT_NULL ) { return sckt_check( sckt_mnj ); }
717 if ( dfpn_sckt != SCKT_NULL ) { return sckt_check( dfpn_sckt ); }
721 #if defined(_WIN32) && defined(WIN32_PIPE)
724 DWORD dwBytesRead, dwTotalBytesAvail, dwBytesLeftThisMessage;
727 hHandle = GetStdHandle( STD_INPUT_HANDLE );
728 if ( hHandle == INVALID_HANDLE_VALUE )
730 str_error = "GetStdHandle() faild.";
733 bSuccess = PeekNamedPipe( hHandle, buf, 1, &dwBytesRead,
734 &dwTotalBytesAvail, &dwBytesLeftThisMessage );
737 if( GetLastError() == 6)
741 Out("peek error %d\n", GetLastError());
742 str_error = "PeekNamedPipe() faild.";
745 if ( dwBytesRead ) { return 1; }
748 #elif defined(_WIN32)
759 # pragma warning(disable:279)
760 # pragma warning(disable:593)
761 # pragma warning(disable:1469)
768 iret = select( 1, &readfds, NULL, NULL, &tv );
771 str_error = "select() faild.";
777 # pragma warning(default:279)
778 # pragma warning(default:593)
779 # pragma warning(default:1469)
781 #endif /* no _WIN32 */
787 out_file( FILE *pf, const char *format, ... )
793 va_start( arg, format );
794 vfprintf( pf, format, arg );
799 #if ! defined(NO_LOGGING)
800 if ( pf_log != NULL )
802 va_start( arg, format );
803 vfprintf( pf_log, format, arg );