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 ); }
226 else { Out( "Black %d> ", record_game.moves+1 ); }
231 open_history( const char *str_name1, const char *str_name2 )
233 #if defined(NO_LOGGING)
234 char str_file[SIZE_FILENAME];
237 iret = record_close( &record_game );
238 if ( iret < 0 ) { return -1; }
240 strncpy( str_file, "game.csa", SIZE_FILENAME-1 );
241 iret = record_open( &record_game, str_file, mode_read_write,
242 str_name1, str_name2 );
243 if ( iret < 0 ) { return -1; }
249 char str_file[SIZE_FILENAME];
251 if ( record_game.pf != NULL && ! record_game.moves )
253 record_game.str_name1[0] = '\0';
254 record_game.str_name2[0] = '\0';
258 strncpy( record_game.str_name1, str_name1, SIZE_PLAYERNAME-1 );
259 record_game.str_name1[SIZE_PLAYERNAME-1] = '\0';
264 strncpy( record_game.str_name2, str_name2, SIZE_PLAYERNAME-1 );
265 record_game.str_name2[SIZE_PLAYERNAME-1] = '\0';
270 iret = file_close( pf_log );
271 if ( iret < 0 ) { return -1; }
273 iret = record_close( &record_game );
274 if ( iret < 0 ) { return -1; }
276 for ( i = 0; i < 999; i++ )
278 snprintf( str_file, SIZE_FILENAME, "%s/game%03d.csa",
280 pf = file_open( str_file, "r" );
281 if ( pf == NULL ) { break; }
282 iret = file_close( pf );
283 if ( iret < 0 ) { return -1; }
287 snprintf( str_file, SIZE_FILENAME, "%s/n%03d.log",
289 pf_log = file_open( str_file, "w" );
290 if ( pf_log == NULL ) { return -1; }
292 snprintf( str_file, SIZE_FILENAME, "%s/game%03d.csa",
294 iret = record_open( &record_game, str_file, mode_read_write,
295 str_name1, str_name2 );
296 if ( iret < 0 ) { return -1; }
304 out_board( const tree_t * restrict ptree, FILE *pf, unsigned int move,
307 int irank, ifile, i, iret, ito, ifrom;
309 #if ! defined(WIN32_PIPE)
313 if ( ! is_strict && move )
316 ifrom = I2From( move );
317 #if ! defined(NO_STDOUT) && ! defined(WIN32_PIPE)
318 is_promote = I2IsPromote( move );
322 ito = ifrom = nsquare;
323 #if ! defined(NO_STDOUT) && ! defined(WIN32_PIPE)
328 if ( ( game_status & flag_reverse ) && ! is_strict )
330 fprintf( pf, " <reversed> \n" );
331 fprintf( pf, "' 1 2 3 4 5 6 7 8 9\n" );
333 for ( irank = rank9; irank >= rank1; irank-- )
335 fprintf( pf, "P%d", irank + 1 );
337 for ( ifile = file9; ifile >= file1; ifile-- )
339 i = irank * nfile + ifile;
340 iret = OutBoard0( pf, BOARD[i], i, ito, ifrom, is_promote );
341 if ( iret < 0 ) { return iret; }
347 fprintf( pf, "' 9 8 7 6 5 4 3 2 1\n" );
349 for ( irank = rank1; irank <= rank9; irank++ )
351 fprintf( pf, "P%d", irank + 1 );
353 for ( ifile = file1; ifile <= file9; ifile++ )
355 i = irank * nfile + ifile;
356 iret = OutBoard0( pf, BOARD[i], i, ito, ifrom, is_promote );
357 if ( iret < 0 ) { return iret; }
363 out_hand( pf, HAND_B, "P+" );
364 out_hand( pf, HAND_W, "P-" );
372 next_cmdline( int is_wait )
378 str_line_end = strchr( str_buffer_cmdline, '\n' );
380 if ( ! str_line_end )
385 iret = read_command( & str_line_end );
386 if ( iret < 0 ) { return iret; }
387 } while ( ! str_line_end && iret );
390 game_status |= flag_quit;
395 #if defined(DEKUNOBOU)
404 if ( sckt_csa != SCKT_NULL )
406 iret = sckt_check( sckt_csa );
412 if ( sckt_mnj != SCKT_NULL )
414 iret = sckt_check( sckt_mnj );
418 iret = check_input_buffer();
420 #if defined(DEKUNOBOU) || defined(CSA_LAN) || defined(MNJ_LAN)
423 if ( iret <= 0 ) { return iret; }
425 iret = read_command( & str_line_end );
426 if ( iret < 0 ) { return iret; }
429 game_status |= flag_quit;
432 if ( ! str_line_end ) { return 0; }
436 if ( str_line_end - str_buffer_cmdline + 1 >= SIZE_CMDLINE )
438 str_error = str_ovrflw_line;
439 memmove( str_buffer_cmdline, str_line_end + 1,
440 strlen( str_line_end + 1 ) + 1 );
444 size = str_line_end - str_buffer_cmdline;
445 memcpy( str_cmdline, str_buffer_cmdline, size );
446 *( str_cmdline + size ) = '\0';
448 #if defined(DEKUNOBOU)
451 iret = dek_parse( str_cmdline, SIZE_CMDLINE );
454 memmove( str_buffer_cmdline, str_line_end + 1,
455 strlen( str_line_end + 1 ) + 1 );
463 out_file( NULL, "%s\n", str_cmdline );
464 memmove( str_buffer_cmdline, str_line_end + 1,
465 strlen( str_line_end + 1 ) + 1 );
473 #if defined(NO_STDOUT) || defined(WIN32_PIPE)
474 out_board0( FILE *pf, int piece, int i, int ito, int ifrom )
476 out_board0( FILE *pf, int piece, int i, int ito, int ifrom, int is_promote )
480 #if ! ( defined(NO_STDOUT) || defined(WIN32_PIPE) )
486 ch = piece < 0 ? '-' : '+';
487 #if ! ( defined(NO_STDOUT) || defined(WIN32_PIPE) )
488 if ( i == ito && pf == stdout && ! ( game_status & flag_nostress ) )
490 iret = StdoutStress( is_promote, ifrom );
498 fprintf( pf, "%c%s", ch, astr_table_piece[ abs( piece ) ] );
499 #if ! ( defined(NO_STDOUT) || defined(WIN32_PIPE) )
500 if ( i == ito && pf == stdout && ! ( game_status & flag_nostress ) )
502 iret = StdoutNormal();
514 || ( adirec[ito][ifrom]
515 && adirec[ito][ifrom] == adirec[ito][i]
516 && ( ( ito < i && i <= ifrom )
517 || ( ito > i && i >= ifrom ) ) ) ) )
521 else { fprintf( pf, " * " ); }
528 #if ! defined(NO_STDOUT) && ! defined(WIN32_PIPE)
533 if ( game_status & flag_nobeep ) { return; }
536 if ( ! MessageBeep( MB_OK ) )
538 out_warning( "Beep is not available." );
547 stdout_normal( void )
553 hStdout = GetStdHandle( STD_OUTPUT_HANDLE );
554 if ( hStdout == INVALID_HANDLE_VALUE )
556 str_error = "GetStdHandle() faild";
560 wAttributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
561 if ( ! SetConsoleTextAttribute( hStdout, wAttributes ) )
563 str_error = "SetConsoleTextAttribute() faild";
575 stdout_stress( int is_promote, int ifrom )
581 hStdout = GetStdHandle( STD_OUTPUT_HANDLE );
582 if ( hStdout == INVALID_HANDLE_VALUE )
584 str_error = "GetStdHandle() faild";
590 wAttributes = BACKGROUND_RED | BACKGROUND_INTENSITY;
592 else if ( ifrom >= nsquare )
594 wAttributes = BACKGROUND_BLUE | BACKGROUND_INTENSITY;
597 wAttributes = ( BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE
598 | BACKGROUND_INTENSITY );
600 if ( ! SetConsoleTextAttribute( hStdout, wAttributes ) )
602 str_error = "SetConsoleTextAttribute() faild";
606 if ( is_promote ) { printf( "\033[7;31m" ); }
607 else if ( ifrom >= nsquare ) { printf( "\033[7;34m" ); }
608 else { printf( "\033[7m" ); }
614 #endif /* no NO_STDOUT and no WIN32_PIPE */
618 out_hand( FILE *pf, unsigned int hand, const char *str_prefix )
620 out_hand0( pf, (int)I2HandPawn(hand), str_prefix, "00FU" );
621 out_hand0( pf, (int)I2HandLance(hand), str_prefix, "00KY" );
622 out_hand0( pf, (int)I2HandKnight(hand), str_prefix, "00KE" );
623 out_hand0( pf, (int)I2HandSilver(hand), str_prefix, "00GI" );
624 out_hand0( pf, (int)I2HandGold(hand), str_prefix, "00KI" );
625 out_hand0( pf, (int)I2HandBishop(hand), str_prefix, "00KA" );
626 out_hand0( pf, (int)I2HandRook(hand), str_prefix, "00HI" );
631 out_hand0( FILE *pf, int n, const char *str_prefix, const char *str )
637 fprintf( pf, str_prefix );
638 for ( i = 0; i < n; i++ ) { fprintf( pf, str ); }
645 read_command( char ** pstr_line_end )
648 int count_byte, count_cmdbuff;
650 count_cmdbuff = (int)strlen( str_buffer_cmdline );
651 str_end = str_buffer_cmdline + count_cmdbuff;
653 #if defined(DEKUNOBOU)
656 count_byte = dek_in( str_end, SIZE_CMDLINE-1-count_cmdbuff );
657 if ( count_byte < 0 ) { return count_byte; }
663 if ( sckt_csa != SCKT_NULL )
665 count_byte = sckt_in( sckt_csa, str_end, SIZE_CMDLINE-1-count_cmdbuff );
666 if ( count_byte < 0 ) { return count_byte; }
672 if ( sckt_mnj != SCKT_NULL )
674 count_byte = sckt_in( sckt_mnj, str_end, SIZE_CMDLINE-1-count_cmdbuff );
675 if ( count_byte < 0 ) { return count_byte; }
680 do { count_byte = (int)read( 0, str_end, SIZE_CMDBUFFER-1-count_cmdbuff ); }
681 while ( count_byte < 0 && errno == EINTR );
683 if ( count_byte < 0 )
685 str_error = "read() faild.";
688 *( str_end + count_byte ) = '\0';
690 #if defined(DEKUNOBOU) || defined(CSA_LAN) || defined(MNJ_LAN)
695 *pstr_line_end = strchr( str_buffer_cmdline, '\n' );
696 if ( *pstr_line_end == NULL
697 && count_byte + count_cmdbuff + 1 >= SIZE_CMDLINE )
699 *str_buffer_cmdline = '\0';
700 str_error = str_ovrflw_line;
710 check_input_buffer( void )
712 # if defined(WIN32_PIPE)
715 DWORD dwBytesRead, dwTotalBytesAvail, dwBytesLeftThisMessage;
718 hHandle = GetStdHandle( STD_INPUT_HANDLE );
719 if ( hHandle == INVALID_HANDLE_VALUE )
721 str_error = "GetStdHandle() faild.";
724 bSuccess = PeekNamedPipe( hHandle, buf, 1, &dwBytesRead, &dwTotalBytesAvail,
725 &dwBytesLeftThisMessage );
728 str_error = "PeekNamedPipe() faild.";
731 if ( dwBytesRead ) { return 1; }
738 #else /* no _WIN32 */
741 check_input_buffer( void )
748 # pragma warning(disable:279)
749 # pragma warning(disable:593)
750 # pragma warning(disable:1469)
757 iret = select( 1, &readfds, NULL, NULL, &tv );
760 str_error = "select() faild.";
766 # pragma warning(default:279)
767 # pragma warning(default:593)
768 # pragma warning(default:1469)
772 #endif /* no _WIN32 */