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 ); }
236 else { Out( "Black %d> ", record_game.moves+1 ); }
241 open_history( const char *str_name1, const char *str_name2 )
243 #if defined(NO_LOGGING)
244 char str_file[SIZE_FILENAME];
247 iret = record_close( &record_game );
248 if ( iret < 0 ) { return -1; }
250 iret = record_open( &record_game, "game.csa", mode_read_write,
251 str_name1, str_name2 );
252 if ( iret < 0 ) { return -1; }
258 char str_file[SIZE_FILENAME];
260 if ( record_game.pf != NULL && ! record_game.moves )
262 record_game.str_name1[0] = '\0';
263 record_game.str_name2[0] = '\0';
267 strncpy( record_game.str_name1, str_name1, SIZE_PLAYERNAME-1 );
268 record_game.str_name1[SIZE_PLAYERNAME-1] = '\0';
273 strncpy( record_game.str_name2, str_name2, SIZE_PLAYERNAME-1 );
274 record_game.str_name2[SIZE_PLAYERNAME-1] = '\0';
279 if ( ( ( game_status & flag_nonewlog )
281 || usi_mode != usi_off
283 ) && 0 <= record_num )
285 iret = record_close( &record_game );
286 if ( iret < 0 ) { return -1; }
288 snprintf( str_file, SIZE_FILENAME, "%s/game%03d.csa",
289 str_dir_logs, record_num );
290 iret = record_open( &record_game, str_file, mode_read_write,
291 str_name1, str_name2 );
292 if ( iret < 0 ) { return -1; }
296 iret = file_close( pf_log );
297 if ( iret < 0 ) { return -1; }
299 iret = record_close( &record_game );
300 if ( iret < 0 ) { return -1; }
302 for ( i = 0; i < 999; i++ )
304 snprintf( str_file, SIZE_FILENAME, "%s/game%03d.csa",
306 pf = file_open( str_file, "r" );
307 if ( pf == NULL ) { break; }
308 iret = file_close( pf );
309 if ( iret < 0 ) { return -1; }
313 snprintf( str_file, SIZE_FILENAME, "%s/n%03d.log",
315 pf_log = file_open( str_file, "w" );
316 if ( pf_log == NULL ) { return -1; }
318 snprintf( str_file, SIZE_FILENAME, "%s/game%03d.csa",
320 iret = record_open( &record_game, str_file, mode_read_write,
321 str_name1, str_name2 );
322 if ( iret < 0 ) { return -1; }
331 out_board( const tree_t * restrict ptree, FILE *pf, unsigned int move,
334 int irank, ifile, i, iret, ito, ifrom;
336 #if ! defined(WIN32_PIPE)
340 if ( game_status & flag_nostdout ) { return 1; }
342 if ( ! is_strict && move )
345 ifrom = I2From( move );
346 #if ! defined(NO_STDOUT) && ! defined(WIN32_PIPE)
347 is_promote = I2IsPromote( move );
351 ito = ifrom = nsquare;
352 #if ! defined(NO_STDOUT) && ! defined(WIN32_PIPE)
357 if ( ( game_status & flag_reverse ) && ! is_strict )
359 fprintf( pf, " <reversed> \n" );
360 fprintf( pf, "' 1 2 3 4 5 6 7 8 9\n" );
362 for ( irank = rank9; irank >= rank1; irank-- )
364 fprintf( pf, "P%d", irank + 1 );
366 for ( ifile = file9; ifile >= file1; ifile-- )
368 i = irank * nfile + ifile;
369 iret = OutBoard0( pf, BOARD[i], i, ito, ifrom, is_promote );
370 if ( iret < 0 ) { return iret; }
376 fprintf( pf, "' 9 8 7 6 5 4 3 2 1\n" );
378 for ( irank = rank1; irank <= rank9; irank++ )
380 fprintf( pf, "P%d", irank + 1 );
382 for ( ifile = file1; ifile <= file9; ifile++ )
384 i = irank * nfile + ifile;
385 iret = OutBoard0( pf, BOARD[i], i, ito, ifrom, is_promote );
386 if ( iret < 0 ) { return iret; }
392 out_hand( pf, HAND_B, "P+" );
393 out_hand( pf, HAND_W, "P-" );
401 next_cmdline( int is_wait )
407 str_line_end = strchr( str_buffer_cmdline, '\n' );
409 if ( ! str_line_end )
414 iret = read_command( & str_line_end );
415 if ( iret < 0 ) { return iret; }
416 } while ( ! str_line_end && iret );
419 game_status |= flag_quit;
424 iret = check_input_buffer();
425 if ( iret <= 0 ) { return iret; }
427 iret = read_command( & str_line_end );
428 if ( iret < 0 ) { return iret; }
431 game_status |= flag_quit;
434 if ( ! str_line_end ) { return 0; }
438 if ( str_line_end - str_buffer_cmdline + 1 >= SIZE_CMDLINE )
440 str_error = str_ovrflw_line;
441 memmove( str_buffer_cmdline, str_line_end + 1,
442 strlen( str_line_end + 1 ) + 1 );
446 size = str_line_end - str_buffer_cmdline;
447 memcpy( str_cmdline, str_buffer_cmdline, size );
448 *( str_cmdline + size ) = '\0';
452 out_file( NULL, "%s\n", str_cmdline );
453 memmove( str_buffer_cmdline, str_line_end + 1,
454 strlen( str_line_end + 1 ) + 1 );
462 #if defined(NO_STDOUT) || defined(WIN32_PIPE)
463 out_board0( FILE *pf, int piece, int i, int ito, int ifrom )
465 out_board0( FILE *pf, int piece, int i, int ito, int ifrom, int is_promote )
469 #if ! ( defined(NO_STDOUT) || defined(WIN32_PIPE) )
475 ch = piece < 0 ? '-' : '+';
476 #if ! ( defined(NO_STDOUT) || defined(WIN32_PIPE) )
477 if ( i == ito && pf == stdout && ! ( game_status & flag_nostress ) )
479 iret = StdoutStress( is_promote, ifrom );
487 fprintf( pf, "%c%s", ch, astr_table_piece[ abs( piece ) ] );
488 #if ! ( defined(NO_STDOUT) || defined(WIN32_PIPE) )
489 if ( i == ito && pf == stdout && ! ( game_status & flag_nostress ) )
491 iret = StdoutNormal();
503 || ( adirec[ito][ifrom]
504 && adirec[ito][ifrom] == adirec[ito][i]
505 && ( ( ito < i && i <= ifrom )
506 || ( ito > i && i >= ifrom ) ) ) ) )
510 else { fprintf( pf, " * " ); }
517 #if ! defined(NO_STDOUT) && ! defined(WIN32_PIPE)
522 if ( game_status & flag_nobeep ) { return; }
525 if ( ! MessageBeep( MB_OK ) )
527 out_warning( "Beep is not available." );
536 stdout_normal( void )
542 hStdout = GetStdHandle( STD_OUTPUT_HANDLE );
543 if ( hStdout == INVALID_HANDLE_VALUE )
545 str_error = "GetStdHandle() faild";
549 wAttributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
550 if ( ! SetConsoleTextAttribute( hStdout, wAttributes ) )
552 str_error = "SetConsoleTextAttribute() faild";
564 stdout_stress( int is_promote, int ifrom )
570 hStdout = GetStdHandle( STD_OUTPUT_HANDLE );
571 if ( hStdout == INVALID_HANDLE_VALUE )
573 str_error = "GetStdHandle() faild";
579 wAttributes = BACKGROUND_RED | BACKGROUND_INTENSITY;
581 else if ( ifrom >= nsquare )
583 wAttributes = BACKGROUND_BLUE | BACKGROUND_INTENSITY;
586 wAttributes = ( BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE
587 | BACKGROUND_INTENSITY );
589 if ( ! SetConsoleTextAttribute( hStdout, wAttributes ) )
591 str_error = "SetConsoleTextAttribute() faild";
595 if ( is_promote ) { printf( "\033[7;31m" ); }
596 else if ( ifrom >= nsquare ) { printf( "\033[7;34m" ); }
597 else { printf( "\033[7m" ); }
603 #endif /* no NO_STDOUT and no WIN32_PIPE */
607 out_hand( FILE *pf, unsigned int hand, const char *str_prefix )
609 out_hand0( pf, (int)I2HandPawn(hand), str_prefix, "00FU" );
610 out_hand0( pf, (int)I2HandLance(hand), str_prefix, "00KY" );
611 out_hand0( pf, (int)I2HandKnight(hand), str_prefix, "00KE" );
612 out_hand0( pf, (int)I2HandSilver(hand), str_prefix, "00GI" );
613 out_hand0( pf, (int)I2HandGold(hand), str_prefix, "00KI" );
614 out_hand0( pf, (int)I2HandBishop(hand), str_prefix, "00KA" );
615 out_hand0( pf, (int)I2HandRook(hand), str_prefix, "00HI" );
620 out_hand0( FILE *pf, int n, const char *str_prefix, const char *str )
626 fprintf( pf, str_prefix );
627 for ( i = 0; i < n; i++ ) { fprintf( pf, str ); }
634 read_command( char ** pstr_line_end )
637 int count_byte, count_cmdbuff;
639 count_cmdbuff = (int)strlen( str_buffer_cmdline );
640 str_end = str_buffer_cmdline + count_cmdbuff;
643 if ( sckt_csa != SCKT_NULL )
645 count_byte = sckt_in( sckt_csa, str_end, SIZE_CMDLINE-1-count_cmdbuff );
646 if ( count_byte < 0 ) { return count_byte; }
652 if ( sckt_mnj != SCKT_NULL )
654 count_byte = sckt_in( sckt_mnj, str_end, SIZE_CMDLINE-1-count_cmdbuff );
655 if ( count_byte < 0 ) { return count_byte; }
661 if ( dfpn_sckt != SCKT_NULL )
663 count_byte = sckt_in( dfpn_sckt, str_end, SIZE_CMDLINE-1-count_cmdbuff );
664 if ( count_byte < 0 ) { return count_byte; }
669 do { count_byte = (int)read( 0, str_end, SIZE_CMDBUFFER-1-count_cmdbuff ); }
670 while ( count_byte < 0 && errno == EINTR );
672 if ( count_byte < 0 )
674 str_error = "read() faild.";
677 *( str_end + count_byte ) = '\0';
679 #if defined(CSA_LAN) || defined(MNJ_LAN) || defined(DFPN)
684 if ( usi_mode != usi_off ) { Out( "IN: %s[END]\n", str_end );}
687 *pstr_line_end = strchr( str_buffer_cmdline, '\n' );
688 if ( *pstr_line_end == NULL
689 && count_byte + count_cmdbuff + 1 >= SIZE_CMDLINE )
691 *str_buffer_cmdline = '\0';
692 str_error = str_ovrflw_line;
701 check_input_buffer( void )
704 if ( sckt_csa != SCKT_NULL ) { return sckt_check( sckt_csa ); }
708 if ( sckt_mnj != SCKT_NULL ) { return sckt_check( sckt_mnj ); }
712 if ( dfpn_sckt != SCKT_NULL ) { return sckt_check( dfpn_sckt ); }
716 #if defined(_WIN32) && defined(WIN32_PIPE)
719 DWORD dwBytesRead, dwTotalBytesAvail, dwBytesLeftThisMessage;
722 hHandle = GetStdHandle( STD_INPUT_HANDLE );
723 if ( hHandle == INVALID_HANDLE_VALUE )
725 str_error = "GetStdHandle() faild.";
728 bSuccess = PeekNamedPipe( hHandle, buf, 1, &dwBytesRead,
729 &dwTotalBytesAvail, &dwBytesLeftThisMessage );
732 str_error = "PeekNamedPipe() faild.";
735 if ( dwBytesRead ) { return 1; }
738 #elif defined(_WIN32)
749 # pragma warning(disable:279)
750 # pragma warning(disable:593)
751 # pragma warning(disable:1469)
758 iret = select( 1, &readfds, NULL, NULL, &tv );
761 str_error = "select() faild.";
767 # pragma warning(default:279)
768 # pragma warning(default:593)
769 # pragma warning(default:1469)
771 #endif /* no _WIN32 */
777 out_file( FILE *pf, const char *format, ... )
783 va_start( arg, format );
784 vfprintf( pf, format, arg );
789 #if ! defined(NO_LOGGING)
790 if ( pf_log != NULL )
792 va_start( arg, format );
793 vfprintf( pf_log, format, arg );