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 int check_input_buffer( void );
31 static int read_command( char **pstr_line_end );
32 static void out_hand( FILE *pf, unsigned int hand, const char *str_prefix );
33 static void out_hand0( FILE *pf, int n, const char *str_prefix,
36 #if ! ( defined(NO_STDOUT) && defined(NO_LOGGING) )
38 out( const char *format, ... )
41 if ( game_status & flag_quiet ) { return; }
43 # if ! defined(NO_STDOUT)
44 va_start( arg, format );
45 vprintf( format, arg );
50 # if ! defined(NO_LOGGING)
51 if ( ( strchr( format, '\n' ) != NULL || strchr( format, '\r' ) == NULL )
54 va_start( arg, format );
55 vfprintf( pf_log, format, arg );
66 out_csashogi( const char *format, ... )
70 va_start( arg, format );
71 vprintf( format, arg );
80 out_file( FILE *pf, const char *format, ... )
86 va_start( arg, format );
87 vfprintf( pf, format, arg );
92 #if ! defined(NO_LOGGING)
95 va_start( arg, format );
96 vfprintf( pf_log, format, arg );
106 out_warning( const char *format, ... )
110 fprintf( stderr, "\n%s", str_warning );
111 va_start( arg, format );
112 vfprintf( stderr, format, arg );
114 fprintf( stderr, "\n\n" );
117 #if ! defined(NO_LOGGING)
118 if ( pf_log != NULL )
120 fprintf( pf_log, "\n%s", str_warning );
121 va_start( arg, format );
122 vfprintf( pf_log, format, arg );
124 fprintf( pf_log, "\n\n" );
133 out_error( const char *format, ... )
137 fprintf( stderr, "\nERROR: " );
138 va_start( arg, format );
139 vfprintf( stderr, format, arg );
141 fprintf( stderr, "\n\n" );
144 #if ! defined(NO_LOGGING)
145 if ( pf_log != NULL )
147 fprintf( pf_log, "\nERROR: " );
148 va_start( arg, format );
149 vfprintf( pf_log, format, arg );
151 fprintf( pf_log, "\n\n" );
160 file_open( const char *str_file, const char *str_mode )
164 pf = fopen( str_file, str_mode );
167 snprintf( str_message, SIZE_MESSAGE,
168 "%s, %s", str_fopen_error, str_file );
169 str_error = str_message;
178 file_close( FILE *pf )
180 if ( pf == NULL ) { return 1; }
184 str_error = str_io_error;
189 str_error = str_io_error;
200 if ( game_status & flag_noprompt ) { return; }
202 #if defined(DEKUNOBOU)
205 Out( "Won=%3d Lost=%3d Total=%4d/", dek_win, dek_lost, dek_ngame-1 );
209 if ( game_status & flag_drawn ) { Out( "Drawn> " ); }
210 else if ( game_status & flag_mated )
212 if ( root_turn ) { Out( "Black Mated> " ); }
213 else { Out( "White Mated> " ); }
215 else if ( game_status & flag_resigned )
217 if ( root_turn ) { Out( "White Resigned> " ); }
218 else { Out( "Black Resigned> " ); }
220 else if ( game_status & flag_suspend )
222 if ( root_turn ) { Out( "White Suspend> " ); }
223 else { Out( "Black Suspend> " ); }
225 else if ( root_turn ) { Out( "White %d> ", record_game.moves+1 ); }
231 else { Out( SIGN "Black %d> ", record_game.moves+1 ); }
236 open_history( const char *str_name1, const char *str_name2 )
238 #if defined(NO_LOGGING)
239 char str_file[SIZE_FILENAME];
242 iret = record_close( &record_game );
243 if ( iret < 0 ) { return -1; }
245 strncpy( str_file, "game.csa", SIZE_FILENAME-1 );
246 iret = record_open( &record_game, str_file, mode_read_write,
247 str_name1, str_name2 );
248 if ( iret < 0 ) { return -1; }
254 char str_file[SIZE_FILENAME];
256 if ( record_game.pf != NULL && ! record_game.moves )
258 record_game.str_name1[0] = '\0';
259 record_game.str_name2[0] = '\0';
263 strncpy( record_game.str_name1, str_name1, SIZE_PLAYERNAME-1 );
264 record_game.str_name1[SIZE_PLAYERNAME-1] = '\0';
269 strncpy( record_game.str_name2, str_name2, SIZE_PLAYERNAME-1 );
270 record_game.str_name2[SIZE_PLAYERNAME-1] = '\0';
275 iret = file_close( pf_log );
276 if ( iret < 0 ) { return -1; }
278 iret = record_close( &record_game );
279 if ( iret < 0 ) { return -1; }
281 for ( i = 0; i < 999; i++ )
283 snprintf( str_file, SIZE_FILENAME, "%s/game%03d.csa",
285 pf = file_open( str_file, "r" );
286 if ( pf == NULL ) { break; }
287 iret = file_close( pf );
288 if ( iret < 0 ) { return -1; }
292 snprintf( str_file, SIZE_FILENAME, "%s/n%03d.log",
294 pf_log = file_open( str_file, "w" );
295 if ( pf_log == NULL ) { return -1; }
297 snprintf( str_file, SIZE_FILENAME, "%s/game%03d.csa",
299 iret = record_open( &record_game, str_file, mode_read_write,
300 str_name1, str_name2 );
301 if ( iret < 0 ) { return -1; }
309 out_board( const tree_t * restrict ptree, FILE *pf, unsigned int move,
312 int irank, ifile, i, iret, ito, ifrom;
314 #if ! defined(WIN32_PIPE)
318 if ( ! is_strict && move )
321 ifrom = I2From( move );
322 #if ! defined(NO_STDOUT) && ! defined(WIN32_PIPE)
323 is_promote = I2IsPromote( move );
327 ito = ifrom = nsquare;
328 #if ! defined(NO_STDOUT) && ! defined(WIN32_PIPE)
333 if ( ( game_status & flag_reverse ) && ! is_strict )
335 fprintf( pf, " <reversed> \n" );
336 fprintf( pf, "' 1 2 3 4 5 6 7 8 9\n" );
338 for ( irank = rank9; irank >= rank1; irank-- )
340 fprintf( pf, "P%d", irank + 1 );
342 for ( ifile = file9; ifile >= file1; ifile-- )
344 i = irank * nfile + ifile;
345 iret = OutBoard0( pf, BOARD[i], i, ito, ifrom, is_promote );
346 if ( iret < 0 ) { return iret; }
352 fprintf( pf, "' 9 8 7 6 5 4 3 2 1\n" );
354 for ( irank = rank1; irank <= rank9; irank++ )
356 fprintf( pf, "P%d", irank + 1 );
358 for ( ifile = file1; ifile <= file9; ifile++ )
360 i = irank * nfile + ifile;
361 iret = OutBoard0( pf, BOARD[i], i, ito, ifrom, is_promote );
362 if ( iret < 0 ) { return iret; }
368 out_hand( pf, HAND_B, "P+" );
369 out_hand( pf, HAND_W, "P-" );
377 next_cmdline( int is_wait )
383 str_line_end = strchr( str_buffer_cmdline, '\n' );
385 if ( ! str_line_end )
390 iret = read_command( & str_line_end );
391 if ( iret < 0 ) { return iret; }
392 } while ( ! str_line_end && iret );
395 game_status |= flag_quit;
400 #if defined(DEKUNOBOU)
409 if ( sckt_csa != SCKT_NULL )
411 iret = sckt_check( sckt_csa );
417 if ( sckt_mnj != SCKT_NULL )
419 iret = sckt_check( sckt_mnj );
423 iret = check_input_buffer();
425 #if defined(DEKUNOBOU) || defined(CSA_LAN) || defined(MNJ_LAN)
428 if ( iret <= 0 ) { return iret; }
430 iret = read_command( & str_line_end );
431 if ( iret < 0 ) { return iret; }
434 game_status |= flag_quit;
437 if ( ! str_line_end ) { return 0; }
441 if ( str_line_end - str_buffer_cmdline + 1 >= SIZE_CMDLINE )
443 str_error = str_ovrflw_line;
444 memmove( str_buffer_cmdline, str_line_end + 1,
445 strlen( str_line_end + 1 ) + 1 );
449 size = str_line_end - str_buffer_cmdline;
450 memcpy( str_cmdline, str_buffer_cmdline, size );
451 *( str_cmdline + size ) = '\0';
453 #if defined(DEKUNOBOU)
456 iret = dek_parse( str_cmdline, SIZE_CMDLINE );
459 memmove( str_buffer_cmdline, str_line_end + 1,
460 strlen( str_line_end + 1 ) + 1 );
468 out_file( NULL, "%s\n", str_cmdline );
469 memmove( str_buffer_cmdline, str_line_end + 1,
470 strlen( str_line_end + 1 ) + 1 );
478 #if defined(NO_STDOUT) || defined(WIN32_PIPE)
479 out_board0( FILE *pf, int piece, int i, int ito, int ifrom )
481 out_board0( FILE *pf, int piece, int i, int ito, int ifrom, int is_promote )
485 #if ! ( defined(NO_STDOUT) || defined(WIN32_PIPE) )
491 ch = piece < 0 ? '-' : '+';
492 #if ! ( defined(NO_STDOUT) || defined(WIN32_PIPE) )
493 if ( i == ito && pf == stdout && ! ( game_status & flag_nostress ) )
495 iret = StdoutStress( is_promote, ifrom );
503 fprintf( pf, "%c%s", ch, astr_table_piece[ abs( piece ) ] );
504 #if ! ( defined(NO_STDOUT) || defined(WIN32_PIPE) )
505 if ( i == ito && pf == stdout && ! ( game_status & flag_nostress ) )
507 iret = StdoutNormal();
519 || ( adirec[ito][ifrom]
520 && adirec[ito][ifrom] == adirec[ito][i]
521 && ( ( ito < i && i <= ifrom )
522 || ( ito > i && i >= ifrom ) ) ) ) )
526 else { fprintf( pf, " * " ); }
533 #if ! defined(NO_STDOUT) && ! defined(WIN32_PIPE)
538 if ( game_status & flag_nobeep ) { return; }
541 if ( ! MessageBeep( MB_OK ) )
543 out_warning( "Beep is not available." );
552 stdout_normal( void )
558 hStdout = GetStdHandle( STD_OUTPUT_HANDLE );
559 if ( hStdout == INVALID_HANDLE_VALUE )
561 str_error = "GetStdHandle() faild";
565 wAttributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
566 if ( ! SetConsoleTextAttribute( hStdout, wAttributes ) )
568 str_error = "SetConsoleTextAttribute() faild";
580 stdout_stress( int is_promote, int ifrom )
586 hStdout = GetStdHandle( STD_OUTPUT_HANDLE );
587 if ( hStdout == INVALID_HANDLE_VALUE )
589 str_error = "GetStdHandle() faild";
595 wAttributes = BACKGROUND_RED | BACKGROUND_INTENSITY;
597 else if ( ifrom >= nsquare )
599 wAttributes = BACKGROUND_BLUE | BACKGROUND_INTENSITY;
602 wAttributes = ( BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE
603 | BACKGROUND_INTENSITY );
605 if ( ! SetConsoleTextAttribute( hStdout, wAttributes ) )
607 str_error = "SetConsoleTextAttribute() faild";
611 if ( is_promote ) { printf( "\033[7;31m" ); }
612 else if ( ifrom >= nsquare ) { printf( "\033[7;34m" ); }
613 else { printf( "\033[7m" ); }
619 #endif /* no NO_STDOUT and no WIN32_PIPE */
623 out_hand( FILE *pf, unsigned int hand, const char *str_prefix )
625 out_hand0( pf, (int)I2HandPawn(hand), str_prefix, "00FU" );
626 out_hand0( pf, (int)I2HandLance(hand), str_prefix, "00KY" );
627 out_hand0( pf, (int)I2HandKnight(hand), str_prefix, "00KE" );
628 out_hand0( pf, (int)I2HandSilver(hand), str_prefix, "00GI" );
629 out_hand0( pf, (int)I2HandGold(hand), str_prefix, "00KI" );
630 out_hand0( pf, (int)I2HandBishop(hand), str_prefix, "00KA" );
631 out_hand0( pf, (int)I2HandRook(hand), str_prefix, "00HI" );
636 out_hand0( FILE *pf, int n, const char *str_prefix, const char *str )
642 fprintf( pf, str_prefix );
643 for ( i = 0; i < n; i++ ) { fprintf( pf, str ); }
650 read_command( char ** pstr_line_end )
653 int count_byte, count_cmdbuff;
655 count_cmdbuff = (int)strlen( str_buffer_cmdline );
656 str_end = str_buffer_cmdline + count_cmdbuff;
658 #if defined(DEKUNOBOU)
661 count_byte = dek_in( str_end, SIZE_CMDLINE-1-count_cmdbuff );
662 if ( count_byte < 0 ) { return count_byte; }
668 if ( sckt_csa != SCKT_NULL )
670 count_byte = sckt_in( sckt_csa, str_end, SIZE_CMDLINE-1-count_cmdbuff );
671 if ( count_byte < 0 ) { return count_byte; }
677 if ( sckt_mnj != SCKT_NULL )
679 count_byte = sckt_in( sckt_mnj, str_end, SIZE_CMDLINE-1-count_cmdbuff );
680 if ( count_byte < 0 ) { return count_byte; }
685 do { count_byte = (int)read( 0, str_end, SIZE_CMDBUFFER-1-count_cmdbuff ); }
686 while ( count_byte < 0 && errno == EINTR );
688 if ( count_byte < 0 )
690 str_error = "read() faild.";
693 *( str_end + count_byte ) = '\0';
695 #if defined(DEKUNOBOU) || defined(CSA_LAN) || defined(MNJ_LAN)
700 *pstr_line_end = strchr( str_buffer_cmdline, '\n' );
701 if ( *pstr_line_end == NULL
702 && count_byte + count_cmdbuff + 1 >= SIZE_CMDLINE )
704 *str_buffer_cmdline = '\0';
705 str_error = str_ovrflw_line;
715 check_input_buffer( void )
717 # if defined(WIN32_PIPE)
720 DWORD dwBytesRead, dwTotalBytesAvail, dwBytesLeftThisMessage;
723 hHandle = GetStdHandle( STD_INPUT_HANDLE );
724 if ( hHandle == INVALID_HANDLE_VALUE )
726 str_error = "GetStdHandle() faild.";
729 bSuccess = PeekNamedPipe( hHandle, buf, 1, &dwBytesRead, &dwTotalBytesAvail,
730 &dwBytesLeftThisMessage );
733 str_error = "PeekNamedPipe() faild.";
736 if ( dwBytesRead ) { return 1; }
743 #else /* no _WIN32 */
746 check_input_buffer( void )
753 # pragma warning(disable:279)
754 # pragma warning(disable:593)
755 # pragma warning(disable:1469)
762 iret = select( 1, &readfds, NULL, NULL, &tv );
765 str_error = "select() faild.";
771 # pragma warning(default:279)
772 # pragma warning(default:593)
773 # pragma warning(default:1469)
777 #endif /* no _WIN32 */