9 /* unacceptable when the program is thinking, or quit pondering */
10 #define AbortDifficultCommand \
11 if ( game_status & flag_thinking ) \
13 str_error = str_busy_think; \
16 else if ( game_status & ( flag_pondering | flag_puzzling ) ) \
18 game_status |= flag_quit_ponder; \
23 # define CmdBook(x,y) cmd_book(y);
24 static int CONV cmd_book( char **lasts );
26 # define CmdBook(x,y) cmd_book(x,y);
27 static int CONV cmd_learn( tree_t * restrict ptree, char **lasts );
28 static int CONV cmd_book( tree_t * restrict ptree, char **lasts );
31 #if ! defined(NO_STDOUT)
32 static int CONV cmd_stress( char **lasts );
36 static int CONV proce_csalan( tree_t * restrict ptree );
37 static int CONV cmd_connect( tree_t * restrict ptree, char **lasts );
38 static int CONV cmd_sendpv( char **lasts );
42 static int CONV proce_mnj( tree_t * restrict ptree );
43 static int CONV cmd_mnjignore( tree_t *restrict ptree, char **lasts );
44 static int CONV cmd_mnj( char **lasts );
45 static int CONV cmd_mnjmove( tree_t * restrict ptree, char **lasts,
50 static int CONV proce_usi( tree_t * restrict ptree );
51 static int CONV usi_posi( tree_t * restrict ptree, char **lasts );
52 static int CONV usi_go( tree_t * restrict ptree, char **lasts );
53 static int CONV usi_ignore( tree_t * restrict ptree, char **lasts );
57 static int CONV cmd_thread( char **lasts );
61 static int CONV cmd_mpv( tree_t * restrict ptree, char **lasts );
65 static int CONV cmd_dfpn( tree_t * restrict ptree, char **lasts );
68 #if defined(DFPN_CLIENT)
69 static int CONV cmd_dfpn_client( tree_t * restrict ptree, char **lasts );
72 static int CONV proce_cui( tree_t * restrict ptree );
73 static int CONV cmd_usrmove( tree_t * restrict ptree, const char *str_move,
75 static int CONV cmd_outmove( tree_t * restrict ptree );
76 static int CONV cmd_move_now( void );
77 static int CONV cmd_ponder( char **lasts );
78 static int CONV cmd_limit( char **lasts );
79 static int CONV cmd_quit( void );
80 static int CONV cmd_beep( char **lasts );
81 static int CONV cmd_peek( char **lasts );
82 static int CONV cmd_stdout( char **lasts );
83 static int CONV cmd_newlog( char **lasts );
84 static int CONV cmd_hash( char **lasts );
85 static int CONV cmd_ping( void );
86 static int CONV cmd_suspend( void );
87 static int CONV cmd_problem( tree_t * restrict ptree, char **lasts );
88 static int CONV cmd_display( tree_t * restrict ptree, char **lasts );
89 static int CONV cmd_move( tree_t * restrict ptree, char **lasts );
90 static int CONV cmd_new( tree_t * restrict ptree, char **lasts );
91 static int CONV cmd_read( tree_t * restrict ptree, char **lasts );
92 static int CONV cmd_resign( tree_t * restrict ptree, char **lasts );
93 static int CONV cmd_undo( tree_t * restrict ptree ); // [HGM] undo
94 static int CONV cmd_time( char **lasts );
95 static int CONV cmd_undo( tree_t * restrict ptree ); // [HGM] undo
96 static int CONV cmd_analyze( tree_t * restrict ptree ); // [HGM] analyze
97 static int CONV cmd_exit( void );
100 int CONV is_move( const char *str )
102 if ( isdigit( (int)str[0] ) && isdigit( (int)str[1] )
103 && isdigit( (int)str[2] ) && isdigit( (int)str[3] )
104 && isupper( (int)str[4] ) && isupper( (int)str[5] )
105 && str[6] == '\0' ) { return 1; }
112 procedure( tree_t * restrict ptree )
115 if ( sckt_csa != SCKT_NULL ) { return proce_csalan( ptree ); }
119 if ( sckt_mnj != SCKT_NULL ) { return proce_mnj( ptree ); }
123 if ( usi_mode != usi_off ) { return proce_usi( ptree ); }
126 return proce_cui( ptree );
129 char *start_pos, start_data[512]; // [HGM] undo: for remembering start position
130 int move_list[1024], move_ptr;
133 #define IF(X) else if(!strcmp(command, X))
135 int myTime, hisTime, movesPerSession, inc, plyNr;
141 { // set white and black times from own and opponent time.
143 if(movesPerSession <= 0) moves = 35; else {
145 while(moves <= 0) moves += movesPerSession;
147 time_limit = (myTime-inc-30)/(moves+2) + inc;
148 time_max_limit = 3*time_limit;
149 if(time_max_limit > myTime - 30)time_max_limit = myTime - 30; // keep 0.3 sec margin, as Bonanza reads the clock infrequently
150 time_limit *= 10; // msec
151 time_max_limit *= 10;
152 Out("# moves=%d, time=%d inc=%d t=%d, max=%d\n", moves, myTime, inc, time_limit, time_max_limit);
156 proce_xboard(char *line, const char *command, tree_t * restrict ptree)
157 { // [HGM] added to make Bonanza.exe a native WinBoard engine
159 static char forceMode = 0;
160 sscanf(line + strlen(command) + 1, "%d", &value);
161 Out("# command = '%s'\n", line);
165 Out("feature option=\"MultiPV -spin 1 1 100\"\n");
166 Out("feature option=\"centi-Pawn margin -spin 200 0 25000\"\n");
168 Out("feature variants=\"shogi\" usermove=1 myname=\"Bonanza " BNZ_VER
169 "\" memory=1 smp=1 debug=1 colors=0 setboard=1 ping=1 done=1\n");
171 IF("new") { forceMode = plyNr = 0; SetTimes(); return 0; }
172 IF("easy") { strcpy(line, "ponder off"); return 0; }
173 IF("hard") { strcpy(line, "ponder on"); return 0; }
176 IF("time") { sscanf(line+5, "%d", &myTime); }
177 IF("otim") { sscanf(line+5, "%d", &hisTime); }
178 IF("force") { forceMode = 1; }
179 IF("go") { forceMode = 0; SetTimes(); plyNr++; strcpy(line, "move"); return 0; }
181 IF("cores") { sprintf(line, "tlp num %d", value); return 0; }
182 IF("sd") { sprintf(line, "limit depth %d", value); return 0; }
184 IF("quit") { return 0; }
185 IF("analyze") { return 0; }
186 IF("exit") { return 0; }
187 IF("variant") { /* ignore, since it must be Shogi */; }
190 if(sscanf(line+7, "MultiPV=%d", &value) == 1) { sprintf(line, "mpv num %d", value); return 0; }
191 if(sscanf(line+7, "centi-Pawn margin=%d", &value) == 1) { sprintf(line, "mpv width %d", value); return 0; }
193 IF("level") { int min, sec; float fsec=0.;
194 if(sscanf(line+6, "%d %d:%d %f", &movesPerSession, &min, &sec, &fsec) != 4)
195 sscanf(line+6, "%d %d %f", &movesPerSession, &min, &fsec);
196 min = 60*min + sec; myTime = hisTime = 100*min; inc = 100 * fsec;
198 IF("usermove") { char fromX=line[9], fromY=line[10], toX=line[11], toY=line[12], promo=line[13];
200 {int i,j;for(i=0;i<81;i+=9){printf("# ");for(j=0;j<9;j++)printf(" %3d", BOARD[i+j]);printf("\n");}}
201 if(forceMode || analyze_mode) strcpy(line, "move "), line += 5; else plyNr++, SetTimes();
202 if(fromY == '@') { // drop
203 if(fromX >= 'a') fromX += 'A' - 'a';
204 switch(fromX) { // encode piece
205 case 'P': fromX = pawn; break;
206 case 'L': fromX = lance; break;
207 case 'N': fromX = knight; break;
208 case 'S': fromX = silver; break;
209 case 'G': fromX = gold; break;
210 case 'B': fromX = bishop; break;
211 case 'R': fromX = rook; break;
213 sprintf(line, "00%c%c%s", 'a'+'9'-toX, '1'+'9'-toY, astr_table_piece[(int)fromX]);
215 from = ('9' - fromY)*9 + (fromX - 'a');
216 Out("# from=%d\n",from);
217 sprintf(line, "%c%c%c%c%s", 'a'+'9'-fromX, '1'+'9'-fromY, 'a'+'9'-toX, '1'+'9'-toY,
218 astr_table_piece[abs(BOARD[from]) + (promo == '+' ? promote : 0)]);
223 IF("undo") { return 0; }
225 IF("ping") { Out("pong %d\n", value); }
230 static int CONV proce_cui( tree_t * restrict ptree )
235 token = strtok_r( str_cmdline, str_delimiters, &last );
237 if ( token == NULL || *token == '#' ) { return 1; }
241 if( proce_xboard(str_cmdline, token, ptree) ) return 1; // command already processed
242 Out("# translated command '%s'\n", str_cmdline); // command translated for processing by Bonanza
243 token = strtok_r( str_cmdline, str_delimiters, &last ); // redo parsing
245 if ( ! strcmp( token, "xboard" ) ) { xboard_mode = 1; game_status |= flag_noprompt; return 1; }
248 if ( ! strcmp( token, "analyze" ) ) { return cmd_analyze( ptree ); } // [HGM] analyze
249 if ( ! strcmp( token, "exit" ) ) { return cmd_exit(); } // [HGM] analyze
250 if ( ! strcmp( token, "move" ) ) { return cmd_move( ptree, &last ); }
251 if ( ! strcmp( token, "undo" ) ) { return cmd_undo( ptree ); } // [HGM] undo
253 if ( ! strcmp( token, "mpv" ) ) { return cmd_mpv( ptree, &last ); }
255 analyze_mode = 0; // [HGM] analyze: all other commands terminate analysis
256 if ( is_move( token ) ) { return cmd_usrmove( ptree, token, &last ); }
257 if ( ! strcmp( token, "s" ) ) { return cmd_move_now(); }
258 if ( ! strcmp( token, "beep" ) ) { return cmd_beep( &last); }
259 if ( ! strcmp( token, "book" ) ) { return CmdBook( ptree, &last ); }
260 if ( ! strcmp( token, "display" ) ) { return cmd_display( ptree, &last ); }
261 if ( ! strcmp( token, "hash" ) ) { return cmd_hash( &last ); }
262 if ( ! strcmp( token, "limit" ) ) { return cmd_limit( &last ); }
263 if ( ! strcmp( token, "new" ) ) { return cmd_new( ptree, &last ); }
264 if ( ! strcmp( token, "outmove" ) ) { return cmd_outmove( ptree ); }
265 if ( ! strcmp( token, "peek" ) ) { return cmd_peek( &last ); }
266 if ( ! strcmp( token, "stdout" ) ) { return cmd_stdout( &last ); }
267 if ( ! strcmp( token, "ping" ) ) { return cmd_ping(); }
268 if ( ! strcmp( token, "ponder" ) ) { return cmd_ponder( &last ); }
269 if ( ! strcmp( token, "problem" ) ) { return cmd_problem( ptree, &last ); }
270 if ( ! strcmp( token, "quit" ) ) { return cmd_quit(); }
271 if ( ! strcmp( token, "read" ) ) { return cmd_read( ptree, &last ); }
272 if ( ! strcmp( token, "resign" ) ) { return cmd_resign( ptree, &last ); }
273 if ( ! strcmp( token, "suspend" ) ) { return cmd_suspend(); }
274 if ( ! strcmp( token, "time" ) ) { return cmd_time( &last ); }
275 if ( ! strcmp( token, "newlog" ) ) { return cmd_newlog( &last ); }
277 if ( ! strcmp( token, "connect" ) ) { return cmd_connect( ptree, &last ); }
278 if ( ! strcmp( token, "sendpv" ) ) { return cmd_sendpv( &last ); }
281 if ( ! strcmp( token, "mnj" ) ) { return cmd_mnj( &last ); }
284 if ( ! strcmp( token, "dfpn" ) ) { return cmd_dfpn( ptree, &last ); }
286 #if defined(DFPN_CLIENT)
287 if ( ! strcmp( token, "dfpn_client")) { return cmd_dfpn_client( ptree,
291 if ( ! strcmp( token, "tlp" ) ) { return cmd_thread( &last ); }
293 #if ! defined(NO_STDOUT)
294 if ( ! strcmp( token, "stress" ) ) { return cmd_stress( &last ); }
296 #if ! defined(MINIMUM)
297 if ( ! strcmp( token, "learn" ) ) { return cmd_learn( ptree, &last ); }
300 str_error = str_bad_cmdline;
306 static int CONV proce_csalan( tree_t * restrict ptree )
311 token = strtok_r( str_cmdline, str_delimiters, &last );
313 if ( token == NULL ) { return 1; }
314 if ( *token == ach_turn[client_turn] && is_move( token+1 ) )
319 token = strtok_r( NULL, str_delimiters, &last );
320 if ( token == NULL || *token != 'T' )
322 str_error = str_bad_cmdline;
326 l = strtol( token+1, &ptr, 0 );
327 if ( token+1 == ptr || l == LONG_MAX || l < 1 )
329 str_error = str_bad_cmdline;
333 adjust_time( (unsigned int)l, client_turn );
334 Out( " elapsed: b%u, w%u\n", sec_b_total, sec_w_total );
337 if ( *token == ach_turn[Flip(client_turn)] && is_move( token+1 ) )
339 return cmd_usrmove( ptree, token+1, &last );
341 if ( ! strcmp( token, str_resign ) ) { return cmd_resign( ptree, &last ); }
342 if ( ! strcmp( token, "#WIN" )
343 || ! strcmp( token, "#LOSE" )
344 || ! strcmp( token, "#DRAW" )
345 || ! strcmp( token, "#CHUDAN" ) )
347 if ( game_status & ( flag_thinking | flag_pondering | flag_puzzling ) )
349 game_status |= flag_suspend;
353 if ( sckt_out( sckt_csa, "LOGOUT\n" ) < 0 ) { return -1; }
354 if ( sckt_recv_all( sckt_csa ) < 0 ) { return -1; }
358 if ( client_ngame == client_max_game ) { return cmd_quit(); }
360 return client_next_game( ptree, client_str_addr, (int)client_port );
369 static int CONV proce_mnj( tree_t * restrict ptree )
375 token = strtok_r( str_cmdline, str_delimiters, &last );
376 if ( token == NULL ) { return 1; }
378 if ( ! strcmp( token, "new" ) )
380 iret = cmd_suspend();
381 if ( iret != 1 ) { return iret; }
384 iret = cmd_new( ptree, &last );
385 if ( iret < 0 ) { return iret; }
387 moves_ignore[0] = MOVE_NA;
388 return analyze( ptree );
390 if ( ! strcmp( token, "ignore" ) ) { return cmd_mnjignore( ptree, &last ); }
391 if ( ! strcmp( token, "idle" ) ) { return cmd_suspend(); }
392 if ( ! strcmp( token, "alter" ) ) { return cmd_mnjmove( ptree, &last, 1 ); }
393 if ( ! strcmp( token, "retract" ) )
397 const char *str = strtok_r( NULL, str_delimiters, &last );
400 str_error = str_bad_cmdline;
403 l = strtol( str, &ptr, 0 );
404 if ( ptr == str || (long)NUM_UNMAKE < l )
406 str_error = str_bad_cmdline;
410 return cmd_mnjmove( ptree, &last, (int)l );
412 if ( ! strcmp( token, "move" ) ) { return cmd_mnjmove( ptree, &last, 0 ); }
414 str_error = str_bad_cmdline;
420 cmd_mnjignore( tree_t *restrict ptree, char **lasts )
429 token = strtok_r( NULL, str_delimiters, lasts );
432 str_error = str_bad_cmdline;
435 lid = strtol( token, &ptr, 0 );
436 if ( ptr == token || lid == LONG_MAX || lid < 1 )
438 str_error = str_bad_cmdline;
442 AbortDifficultCommand;
444 for ( i = 0; ; i += 1 )
446 token = strtok_r( NULL, str_delimiters, lasts );
447 if ( token == NULL ) { break; }
449 if ( interpret_CSA_move( ptree, &move, token ) < 0 ) { return -1; }
451 moves_ignore[i] = move;
455 str_error = str_bad_cmdline;
458 mnj_posi_id = (int)lid;
459 moves_ignore[i] = MOVE_NA;
461 return analyze( ptree );
466 cmd_mnjmove( tree_t * restrict ptree, char **lasts, int num_alter )
468 const char *str1 = strtok_r( NULL, str_delimiters, lasts );
469 const char *str2 = strtok_r( NULL, str_delimiters, lasts );
475 if ( sckt_mnj == SCKT_NULL || str1 == NULL || str2 == NULL )
477 str_error = str_bad_cmdline;
481 lid = strtol( str2, &ptr, 0 );
482 if ( ptr == str2 || lid == LONG_MAX || lid < 1 )
484 str_error = str_bad_cmdline;
488 AbortDifficultCommand;
492 iret = unmake_move_root( ptree );
493 if ( iret < 0 ) { return iret; }
498 iret = interpret_CSA_move( ptree, &move, str1 );
499 if ( iret < 0 ) { return iret; }
501 iret = get_elapsed( &time_turn_start );
502 if ( iret < 0 ) { return iret; }
504 mnj_posi_id = (int)lid;
506 iret = make_move_root( ptree, move, ( flag_time | flag_rep
507 | flag_detect_hang ) );
508 if ( iret < 0 ) { return iret; }
510 # if ! defined(NO_STDOUT)
511 iret = out_board( ptree, stdout, 0, 0 );
512 if ( iret < 0 ) { return iret; }
515 moves_ignore[0] = MOVE_NA;
516 return analyze( ptree );
522 do_analyze( tree_t * restrict ptree )
523 { // [HGM] analyze: do a ponder search on the current position
525 if ( get_elapsed( &time_start ) < 0 ) { return -1; }
526 time_limit = time_max_limit = 1e9; // kludge: use huge time to mimic infinity
528 if(xboard_mode) Out("1 0 0 0 New Search\n"); // make sure lower depth is emitted, so XBoard undestand new search started
530 game_status |= flag_pondering;
531 iret = iterate( ptree );
532 game_status &= ~flag_pondering;
538 cmd_undo( tree_t * restrict ptree )
539 { // [HGM] undo: restart the game, and feed all moves except the last
540 int i, last = move_ptr;
541 char *p = start_data;
542 if( move_ptr <= 0 ) {
543 str_error = "undo past start of game ignored";
547 AbortDifficultCommand;
550 cmd_new( ptree, &p );
551 for(i=0; i<last; i++) {
552 make_move_root( ptree, move_list[i], 0);
555 if ( analyze_mode ) return do_analyze ( ptree ); // [HGM] analyze: analysis should continue after undo
562 cmd_analyze( tree_t * restrict ptree )
563 { // [HGM] analyze: switch on analyze mode, and start analyzing
564 AbortDifficultCommand;
567 return do_analyze( ptree );
573 { // [HGM] analyze: switch off analysis mode
574 if ( !analyze_mode ) {
575 str_error = "was not analyzing";
579 if ( game_status & flag_pondering ) { game_status |= flag_quit_ponder; return 2; }
587 static int CONV proce_usi( tree_t * restrict ptree )
593 token = strtok_r( str_cmdline, str_delimiters, &lasts );
594 if ( token == NULL ) { return 1; }
596 if ( ! strcmp( token, "usi" ) )
598 USIOut( "id name %s\n", str_myname );
599 USIOut( "id author Kunihito Hoki\n" );
604 if ( ! strcmp( token, "isready" ) )
606 USIOut( "readyok\n", str_myname );
610 if ( ! strcmp( token, "echo" ) )
612 USIOut( "%s\n", lasts );
616 if ( ! strcmp( token, "ignore_moves" ) )
618 return usi_ignore( ptree, &lasts );
621 if ( ! strcmp( token, "genmove_probability" ) )
623 if ( get_elapsed( &time_start ) < 0 ) { return -1; }
624 return usi_root_list( ptree );
627 if ( ! strcmp( token, "go" ) )
629 iret = usi_go( ptree, &lasts );
630 moves_ignore[0] = MOVE_NA;
634 if ( ! strcmp( token, "stop" ) ) { return cmd_move_now(); }
635 if ( ! strcmp( token, "position" ) ) { return usi_posi( ptree, &lasts ); }
636 if ( ! strcmp( token, "quit" ) ) { return cmd_quit(); }
638 str_error = str_bad_cmdline;
644 usi_ignore( tree_t * restrict ptree, char **lasts )
651 AbortDifficultCommand;
653 for ( i = 0; ; i += 1 )
655 token = strtok_r( NULL, str_delimiters, lasts );
656 if ( token == NULL ) { break; }
658 if ( usi2csa( ptree, token, str_buf ) < 0 ) { return -1; }
659 if ( interpret_CSA_move( ptree, &move, str_buf ) < 0 ) { return -1; }
661 moves_ignore[i] = move;
664 moves_ignore[i] = MOVE_NA;
671 usi_go( tree_t * restrict ptree, char **lasts )
678 AbortDifficultCommand;
680 if ( game_status & mask_game_end )
682 str_error = str_game_ended;
686 token = strtok_r( NULL, str_delimiters, lasts );
688 if ( ! strcmp( token, "book" ) )
690 AbortDifficultCommand;
691 if ( usi_book( ptree ) < 0 ) { return -1; }
697 if ( ! strcmp( token, "infinite" ) )
699 usi_byoyomi = UINT_MAX;
700 depth_limit = PLY_MAX;
701 node_limit = UINT64_MAX;
702 sec_limit_depth = UINT_MAX;
704 else if ( ! strcmp( token, "byoyomi" ) )
706 token = strtok_r( NULL, str_delimiters, lasts );
709 str_error = str_bad_cmdline;
713 l = strtol( token, &ptr, 0 );
714 if ( ptr == token || l > UINT_MAX || l < 1 )
716 str_error = str_bad_cmdline;
720 usi_byoyomi = (unsigned int)l;
721 depth_limit = PLY_MAX;
722 node_limit = UINT64_MAX;
723 sec_limit_depth = UINT_MAX;
726 str_error = str_bad_cmdline;
731 if ( get_elapsed( &time_turn_start ) < 0 ) { return -1; }
733 iret = com_turn_start( ptree, 0 );
735 if ( str_error == str_no_legal_move ) { USIOut( "bestmove resign\n" ); }
744 usi_posi( tree_t * restrict ptree, char **lasts )
750 AbortDifficultCommand;
752 moves_ignore[0] = MOVE_NA;
754 token = strtok_r( NULL, str_delimiters, lasts );
755 if ( strcmp( token, "startpos" ) )
757 str_error = str_bad_cmdline;
761 if ( ini_game( ptree, &min_posi_no_handicap,
762 flag_history, NULL, NULL ) < 0 ) { return -1; }
764 token = strtok_r( NULL, str_delimiters, lasts );
765 if ( token == NULL ) { return 1; }
767 if ( strcmp( token, "moves" ) )
769 str_error = str_bad_cmdline;
775 token = strtok_r( NULL, str_delimiters, lasts );
776 if ( token == NULL ) { break; }
778 if ( usi2csa( ptree, token, str_buf ) < 0 ) { return -1; }
779 if ( interpret_CSA_move( ptree, &move, str_buf ) < 0 ) { return -1; }
780 if ( make_move_root( ptree, move, ( flag_history | flag_time
782 | flag_detect_hang ) ) < 0 )
788 if ( get_elapsed( &time_turn_start ) < 0 ) { return -1; }
795 static int CONV cmd_move_now( void )
797 if ( game_status & flag_thinking ) { game_status |= flag_move_now; }
804 cmd_usrmove( tree_t * restrict ptree, const char *str_move, char **lasts )
812 if ( game_status & mask_game_end )
814 str_error = str_game_ended;
818 if ( game_status & flag_thinking )
820 str_error = str_busy_think;
824 str = strtok_r( NULL, str_delimiters, lasts );
825 if ( str == NULL ) { lelapsed = 0; }
829 str_error = str_bad_cmdline;
833 lelapsed = strtol( str, &ptr, 0 );
834 if ( ptr == str || lelapsed == LONG_MAX || lelapsed < 1 )
836 str_error = str_bad_cmdline;
841 if ( game_status & ( flag_pondering | flag_puzzling ) )
845 for ( i = 0; i < ponder_nmove; i++ )
847 if ( ! strcmp( str_move, str_CSA_move(ponder_move_list[i]) ) )
852 if ( i == ponder_nmove )
855 if ( sckt_csa != SCKT_NULL ) { AbortDifficultCommand; }
858 #if defined(CSASHOGI)
859 AbortDifficultCommand;
861 str_error = str_illegal_move;
866 if ( ( game_status & flag_puzzling )
867 || strcmp( str_move, str_CSA_move(ponder_move) ) )
869 ponder_move = MOVE_PONDER_FAILED;
870 game_status |= flag_quit_ponder;
874 iret = update_time( Flip(root_turn) );
875 if ( iret < 0 ) { return iret; }
878 adjust_time( (unsigned int)lelapsed, Flip(root_turn) );
881 out_CSA( ptree, &record_game, ponder_move );
883 game_status &= ~flag_pondering;
884 game_status |= flag_thinking;
888 set_search_limit_time( root_turn );
890 OutCsaShogi( "info ponder end\n" );
892 str = str_time_symple( time_turn_start - time_start );
893 Out( " %6s MOVE PREDICTION HIT\n"
894 " elapsed: b%u, w%u\n", str, sec_b_total, sec_w_total );
899 iret = interpret_CSA_move( ptree, &move, str_move );
900 if ( iret < 0 ) { return iret; }
901 move_evasion_pchk = 0;
902 iret = make_move_root( ptree, move, ( flag_rep | flag_history | flag_time
903 | flag_detect_hang ) );
908 if ( sckt_csa != SCKT_NULL )
910 if ( move_evasion_pchk )
912 str = str_CSA_move( move_evasion_pchk );
913 iret = sckt_out( sckt_csa, "%c%s\n",
914 ach_turn[Flip(root_turn)], str );
915 if ( iret < 0 ) { return iret; }
917 return cmd_suspend();
921 if ( move_evasion_pchk )
923 str = str_CSA_move( move_evasion_pchk );
924 #if defined(CSASHOGI)
925 OutCsaShogi( "move%s\n", str );
926 return cmd_suspend();
928 snprintf( str_message, SIZE_MESSAGE, "perpetual check (%c%s)",
929 ach_turn[Flip(root_turn)], str );
930 str_error = str_message;
938 if ( lelapsed ) { adjust_time( (unsigned int)lelapsed, Flip(root_turn) ); }
939 Out( " elapsed: b%u, w%u\n", sec_b_total, sec_w_total );
942 if ( sckt_csa != SCKT_NULL && ( game_status & flag_mated ) )
944 iret = sckt_out( sckt_csa, "%%TORYO\n" );
945 if ( iret < 0 ) { return iret; }
949 if ( ! ( game_status & mask_game_end ) )
951 iret = com_turn_start( ptree, 0 );
952 if ( iret < 0 ) { return iret; }
959 static int CONV cmd_beep( char **lasts )
961 const char *str = strtok_r( NULL, str_delimiters, lasts );
964 str_error = str_bad_cmdline;
968 if ( ! strcmp( str, str_on ) ) { game_status &= ~flag_nobeep; }
969 else if ( ! strcmp( str, str_off ) ) { game_status |= flag_nobeep; }
971 str_error = str_bad_cmdline;
979 static int CONV cmd_peek( char **lasts )
981 const char *str = strtok_r( NULL, str_delimiters, lasts );
985 str_error = str_bad_cmdline;
989 if ( ! strcmp( str, str_on ) ) { game_status &= ~flag_nopeek; }
990 else if ( ! strcmp( str, str_off ) ) { game_status |= flag_nopeek; }
992 str_error = str_bad_cmdline;
1000 static int CONV cmd_stdout( char **lasts )
1002 const char *str = strtok_r( NULL, str_delimiters, lasts );
1006 str_error = str_bad_cmdline;
1010 if ( ! strcmp( str, str_on ) ) { game_status &= ~flag_nostdout; }
1011 else if ( ! strcmp( str, str_off ) ) { game_status |= flag_nostdout; }
1013 str_error = str_bad_cmdline;
1021 static int CONV cmd_newlog( char **lasts )
1023 const char *str = strtok_r( NULL, str_delimiters, lasts );
1027 str_error = str_bad_cmdline;
1031 if ( ! strcmp( str, str_on ) ) { game_status &= ~flag_nonewlog; }
1032 else if ( ! strcmp( str, str_off ) ) { game_status |= flag_nonewlog; }
1034 str_error = str_bad_cmdline;
1042 static int CONV cmd_ponder( char **lasts )
1044 const char *str = strtok_r( NULL, str_delimiters, lasts );
1048 str_error = str_bad_cmdline;
1052 if ( ! strcmp( str, str_on ) ) { game_status &= ~flag_noponder; }
1053 else if ( ! strcmp( str, str_off ) )
1055 if ( game_status & ( flag_pondering | flag_puzzling ) )
1057 game_status |= flag_quit_ponder;
1059 game_status |= flag_noponder;
1062 str_error = str_bad_cmdline;
1070 #if ! defined(NO_STDOUT)
1071 static int CONV cmd_stress( char **lasts )
1073 const char *str = strtok_r( NULL, str_delimiters, lasts );
1077 str_error = str_bad_cmdline;
1081 if ( ! strcmp( str, str_on ) ) { game_status &= ~flag_nostress; }
1082 else if ( ! strcmp( str, str_off ) ) { game_status |= flag_nostress; }
1084 str_error = str_bad_cmdline;
1094 #if defined(MINIMUM)
1095 cmd_book( char **lasts )
1097 cmd_book( tree_t * restrict ptree, char **lasts )
1100 const char *str = strtok_r( NULL, str_delimiters, lasts );
1105 str_error = str_bad_cmdline;
1108 if ( ! strcmp( str, str_on ) ) { iret = book_on(); }
1109 else if ( ! strcmp( str, str_off ) ) { iret = book_off(); }
1110 else if ( ! strcmp( str, "narrow" ) ) { game_status |= flag_narrow_book; }
1111 else if ( ! strcmp( str, "wide" ) ) { game_status &= ~flag_narrow_book; }
1112 #if ! defined(MINIMUM)
1113 else if ( ! strcmp( str, "create" ) )
1115 AbortDifficultCommand;
1117 iret = book_create( ptree );
1118 if ( iret < 0 ) { return iret; }
1120 iret = ini_game( ptree, &min_posi_no_handicap, flag_history,
1122 if ( iret < 0 ) { return iret; }
1124 iret = get_elapsed( &time_turn_start );
1128 str_error = str_bad_cmdline;
1136 static int CONV cmd_display( tree_t * restrict ptree, char **lasts )
1138 const char *str = strtok_r( NULL, str_delimiters, lasts );
1145 l = strtol( str, &ptr, 0 );
1148 str_error = str_bad_cmdline;
1151 if ( l == 1 ) { game_status &= ~flag_reverse; }
1152 else if ( l == 2 ) { game_status |= flag_reverse; }
1154 str_error = str_bad_cmdline;
1160 iret = out_board( ptree, stdout, 0, 0 );
1161 if ( iret < 0 ) { return iret; }
1162 #if ! defined(NO_LOGGING)
1163 iret = out_board( ptree, pf_log, 0, 0 );
1164 if ( iret < 0 ) { return iret; }
1172 static int CONV cmd_ping( void )
1174 OutCsaShogi( "pong\n" );
1180 static int CONV cmd_hash( char **lasts )
1182 const char *str = strtok_r( NULL, str_delimiters, lasts );
1188 str_error = str_bad_cmdline;
1192 l = strtol( str, &ptr, 0 );
1193 if ( ptr == str || l == LONG_MAX || l < 1 || l > 31 )
1195 str_error = str_bad_cmdline;
1199 AbortDifficultCommand;
1201 log2_ntrans_table = (int)l;
1202 memory_free( (void *)ptrans_table_orig );
1203 return ini_trans_table();
1207 static int CONV cmd_limit( char **lasts )
1209 const char *str = strtok_r( NULL, str_delimiters, lasts );
1215 str_error = str_bad_cmdline;
1219 AbortDifficultCommand;
1221 if ( ! strcmp( str, "depth" ) )
1223 str = strtok_r( NULL, str_delimiters, lasts );
1226 str_error = str_bad_cmdline;
1229 l1 = strtol( str, &ptr, 0 );
1230 if ( ptr == str || l1 == LONG_MAX || l1 < 1 )
1232 str_error = str_bad_cmdline;
1235 sec_limit_up = UINT_MAX;
1236 node_limit = UINT64_MAX;
1237 depth_limit = (int)l1;
1239 else if ( ! strcmp( str, "nodes" ) )
1241 str = strtok_r( NULL, str_delimiters, lasts );
1244 str_error = str_bad_cmdline;
1247 l1 = strtol( str, &ptr, 0 );
1248 if ( ptr == str || l1 == LONG_MAX || l1 < 1 )
1250 str_error = str_bad_cmdline;
1253 sec_limit_up = UINT_MAX;
1254 depth_limit = PLY_MAX;
1255 node_limit = (uint64_t)l1;
1257 else if ( ! strcmp( str, "time" ) )
1259 str = strtok_r( NULL, str_delimiters, lasts );
1262 str_error = str_bad_cmdline;
1266 if ( ! strcmp( str, "extendable" ) )
1268 game_status |= flag_time_extendable;
1270 else if ( ! strcmp( str, "strict" ) )
1272 game_status &= ~flag_time_extendable;
1275 l1 = strtol( str, &ptr, 0 );
1276 if ( ptr == str || l1 == LONG_MAX || l1 < 0 )
1278 str_error = str_bad_cmdline;
1282 str = strtok_r( NULL, str_delimiters, lasts );
1285 str_error = str_bad_cmdline;
1288 l2 = strtol( str, &ptr, 0 );
1289 if ( ptr == str || l2 == LONG_MAX || l2 < 0 )
1291 str_error = str_bad_cmdline;
1295 str = strtok_r( NULL, str_delimiters, lasts );
1296 if ( ! str ) { l3 = -1; }
1298 l3 = strtol( str, &ptr, 0 );
1299 if ( ptr == str || l3 >= PLY_MAX || l3 < -1 )
1301 str_error = str_bad_cmdline;
1306 if ( ! ( l1 | l2 ) ) { l2 = 1; }
1308 depth_limit = PLY_MAX;
1309 node_limit = UINT64_MAX;
1310 sec_limit = (unsigned int)l1 * 60U;
1311 sec_limit_up = (unsigned int)l2;
1312 if ( l3 == -1 ) { sec_limit_depth = UINT_MAX; }
1313 else { sec_limit_depth = (unsigned int)l3; }
1317 str_error = str_bad_cmdline;
1326 cmd_read( tree_t * restrict ptree, char **lasts )
1328 const char *str1 = strtok_r( NULL, str_delimiters, lasts );
1329 const char *str2 = strtok_r( NULL, str_delimiters, lasts );
1330 const char *str3 = strtok_r( NULL, str_delimiters, lasts );
1331 const char *str_tmp;
1332 FILE *pf_src, *pf_dest;
1333 char str_file[SIZE_FILENAME];
1339 flag = flag_history | flag_rep | flag_detect_hang;
1345 str_error = str_bad_cmdline;
1351 if ( ! strcmp( str2, "t" ) ) { flag |= flag_time; }
1352 else if ( strcmp( str2, "nil" ) )
1354 str_error = str_bad_cmdline;
1361 l = strtol( str3, &ptr, 0 );
1362 if ( ptr == str3 || l == LONG_MAX || l < 1 )
1364 str_error = str_bad_cmdline;
1367 moves = (unsigned int)l - 1U;
1370 AbortDifficultCommand;
1372 if ( ! strcmp( str1, "." ) )
1374 str_tmp = "game.cs_";
1376 #if defined(NO_LOGGING)
1377 strncpy( str_file, "game.csa", SIZE_FILENAME-1 );
1379 snprintf( str_file, SIZE_FILENAME, "%s/game%03d.csa",
1380 str_dir_logs, record_num );
1382 pf_dest = file_open( str_tmp, "w" );
1383 if ( pf_dest == NULL ) { return -2; }
1385 pf_src = file_open( str_file, "r" );
1386 if ( pf_src == NULL )
1388 file_close( pf_dest );
1392 while ( ( c = getc(pf_src) ) != EOF ) { putc( c, pf_dest ); }
1394 iret = file_close( pf_src );
1397 file_close( pf_dest );
1401 iret = file_close( pf_dest );
1402 if ( iret < 0 ) { return iret; }
1408 iret = read_record( ptree, str1, moves, flag );
1409 if ( iret < 0 ) { return iret; }
1411 iret = get_elapsed( &time_turn_start );
1412 if ( iret < 0 ) { return iret; }
1414 if ( str_tmp && remove( str_tmp ) )
1416 out_warning( "remove() failed." );
1424 static int CONV cmd_resign( tree_t * restrict ptree, char **lasts )
1426 const char *str = strtok_r( NULL, str_delimiters, lasts );
1430 if ( str == NULL || *str == 'T' )
1432 AbortDifficultCommand;
1434 if ( game_status & mask_game_end ) { return 1; }
1436 game_status |= flag_resigned;
1437 update_time( root_turn );
1438 out_CSA( ptree, &record_game, MOVE_RESIGN );
1441 l = strtol( str, &ptr, 0 );
1442 if ( ptr == str || l == LONG_MAX || l < MT_CAP_PAWN )
1444 str_error = str_bad_cmdline;
1447 resign_threshold = (int)l;
1454 static int CONV cmd_move( tree_t * restrict ptree, char **lasts )
1456 const char *str = strtok_r( NULL, str_delimiters, lasts );
1462 if ( game_status & mask_game_end )
1464 str_error = str_game_ended;
1468 AbortDifficultCommand;
1472 if ( analyze_mode ) // [HGM] analyze: in analysis mode we cannot set the engine thinking (but perhaps play PV move?)
1474 str_error = str_bad_cmdline;
1478 iret = get_elapsed( &time_turn_start );
1479 if ( iret < 0 ) { return iret; }
1481 return com_turn_start( ptree, 0 );
1484 l = strtol( str, &ptr, 0 );
1485 if ( str != ptr && l != LONG_MAX && l >= 1 && *ptr == '\0' )
1487 if ( analyze_mode ) // [HGM] analyze: in analysis mode we cannot set the engine thinking
1489 str_error = str_bad_cmdline;
1493 for ( i = 0; i < l; i += 1 )
1495 if ( game_status & ( flag_move_now | mask_game_end ) ) { break; }
1497 iret = get_elapsed( &time_turn_start );
1498 if ( iret < 0 ) { return iret; }
1500 iret = com_turn_start( ptree, 0 );
1501 if ( iret < 0 ) { return iret; }
1508 iret = interpret_CSA_move( ptree, &move, str );
1509 if ( iret < 0 ) { return iret; }
1511 iret = get_elapsed( &time_turn_start );
1512 if ( iret < 0 ) { return iret; }
1514 iret = make_move_root( ptree, move,
1515 ( flag_history | flag_time | flag_rep
1516 | flag_detect_hang ) );
1517 if ( iret < 0 ) { return iret; }
1519 str = strtok_r( NULL, str_delimiters, lasts );
1521 } while ( str != NULL );
1523 if ( analyze_mode ) return do_analyze ( ptree ); // [HGM] analyze: analysis should continue after feeding moves
1529 static int CONV cmd_new( tree_t * restrict ptree, char **lasts )
1531 const char *str1 = strtok_r( NULL, str_delimiters, lasts );
1532 const char *str2 = strtok_r( NULL, str_delimiters, lasts );
1533 const min_posi_t *pmp;
1534 min_posi_t min_posi;
1537 AbortDifficultCommand;
1539 start_pos = *lasts; move_ptr = 0; // [HGM] undo: remember start position
1543 strncpy(start_data, str1, 511); // [HGM] undo: remember start position
1544 memset( &min_posi.asquare, empty, nsquare );
1545 min_posi.hand_black = min_posi.hand_white = 0;
1546 iret = read_board_rep1( str1, &min_posi );
1547 if ( iret < 0 ) { return iret; }
1551 if ( ! strcmp( str2, "-" ) ) { min_posi.turn_to_move = white; }
1552 else if ( ! strcmp( str2, "+" ) ) { min_posi.turn_to_move = black; }
1554 str_error = str_bad_cmdline;
1558 else { min_posi.turn_to_move = black; }
1562 else { pmp = &min_posi_no_handicap; }
1564 iret = ini_game( ptree, pmp, flag_history, NULL, NULL );
1565 if ( iret < 0 ) { return iret; }
1567 return get_elapsed( &time_turn_start );
1571 static int CONV cmd_outmove( tree_t * restrict ptree )
1573 const char *str_move;
1575 unsigned int move_list[ MAX_LEGAL_MOVES ];
1578 AbortDifficultCommand;
1580 if ( game_status & mask_game_end )
1582 Out( "NO LEGAL MOVE\n" );
1583 DFPNOut( "NO LEGAL MOVE\n" );
1587 n = gen_legal_moves( ptree, move_list, 0 );
1590 for ( c = i = 0; i < n; i += 1 )
1592 str_move = str_CSA_move(move_list[i]);
1594 if ( i && ( i % 10 ) == 0 )
1596 Out( "%s\n", buffer );
1597 DFPNOut( "%s ", buffer );
1598 memcpy( buffer, str_move, 6 );
1604 memcpy( buffer + c + 1, str_move, 6 );
1608 memcpy( buffer + c, str_move, 6 );
1613 Out( "%s\n", buffer );
1614 DFPNOut( "%s\n", buffer );
1620 static int CONV cmd_problem( tree_t * restrict ptree, char **lasts )
1622 const char *str = strtok_r( NULL, str_delimiters, lasts );
1625 unsigned int nposition;
1631 AbortDifficultCommand;
1636 if ( str != NULL && ! strcmp( str, "mate" ) )
1639 str = strtok_r( NULL, str_delimiters, lasts );
1645 l = strtol( str, &ptr, 0 );
1646 if ( ptr == str || l == LONG_MAX || l < 1 )
1648 str_error = str_bad_cmdline;
1651 nposition = (unsigned int)l;
1653 else { nposition = UINT_MAX; }
1656 iret = record_open( &record_problems, "problem.csa", mode_read, NULL, NULL );
1657 if ( iret < 0 ) { return iret; }
1660 iret = is_mate ? solve_mate_problems( ptree, nposition )
1661 : solve_problems( ptree, nposition );
1663 iret = solve_problems( ptree, nposition );
1668 record_close( &record_problems );
1672 iret = record_close( &record_problems );
1673 if ( iret < 0 ) { return iret; }
1675 return get_elapsed( &time_turn_start );
1679 static int CONV cmd_quit( void )
1681 game_status |= flag_quit;
1686 static int CONV cmd_suspend( void )
1688 if ( game_status & ( flag_pondering | flag_puzzling ) )
1690 game_status |= flag_quit_ponder;
1694 game_status |= flag_suspend;
1699 static int CONV cmd_time( char **lasts )
1701 const char *str = strtok_r( NULL, str_delimiters, lasts );
1706 str_error = str_bad_cmdline;
1709 else if ( ! strcmp( str, "response" ) )
1712 str = strtok_r( NULL, str_delimiters, lasts );
1715 str_error = str_bad_cmdline;
1718 l = strtol( str, &ptr, 0 );
1719 if ( ptr == str || l == LONG_MAX || l < 0 || l > 1000 )
1721 str_error = str_bad_cmdline;
1724 time_response = (unsigned int)l;
1727 else if ( ! strcmp( str, "remain" ) )
1731 str = strtok_r( NULL, str_delimiters, lasts );
1734 str_error = str_bad_cmdline;
1737 l1 = strtol( str, &ptr, 0 );
1738 if ( ptr == str || l1 == LONG_MAX || l1 < 0 )
1740 str_error = str_bad_cmdline;
1744 str = strtok_r( NULL, str_delimiters, lasts );
1747 str_error = str_bad_cmdline;
1750 l2 = strtol( str, &ptr, 0 );
1751 if ( ptr == str || l2 == LONG_MAX || l2 < 0 )
1753 str_error = str_bad_cmdline;
1757 if ( sec_limit_up == UINT_MAX )
1759 str_error = str_bad_cmdline;
1763 return reset_time( (unsigned int)l1, (unsigned int)l2 );
1766 str_error = str_bad_cmdline;
1771 #if !defined(MINIMUM)
1772 /* learn (ini|no-ini) steps games iterations tlp1 tlp2 */
1773 static int CONV cmd_learn( tree_t * restrict ptree, char **lasts )
1775 const char *str1 = strtok_r( NULL, str_delimiters, lasts );
1776 const char *str2 = strtok_r( NULL, str_delimiters, lasts );
1777 const char *str3 = strtok_r( NULL, str_delimiters, lasts );
1778 const char *str4 = strtok_r( NULL, str_delimiters, lasts );
1780 const char *str5 = strtok_r( NULL, str_delimiters, lasts );
1781 const char *str6 = strtok_r( NULL, str_delimiters, lasts );
1785 unsigned int max_games;
1786 int is_ini, nsteps, max_iterations, nworker1, nworker2, iret;
1790 str_error = str_bad_cmdline;
1793 if ( ! strcmp( str1, "ini" ) ) { is_ini = 1; }
1794 else if ( ! strcmp( str1, "no-ini" ) ) { is_ini = 0; }
1796 str_error = str_bad_cmdline;
1800 max_games = UINT_MAX;
1801 max_iterations = INT_MAX;
1802 nworker1 = nworker2 = nsteps = 1;
1806 l = strtol( str2, &ptr, 0 );
1807 if ( ptr == str2 || l == LONG_MAX || l < 1 )
1809 str_error = str_bad_cmdline;
1817 l = strtol( str3, &ptr, 0 );
1818 if ( ptr == str3 || l == LONG_MAX || l == LONG_MIN )
1820 str_error = str_bad_cmdline;
1823 if ( l > 0 ) { max_games = (unsigned int)l; }
1828 l = strtol( str4, &ptr, 0 );
1829 if ( ptr == str4 || l == LONG_MAX || l == LONG_MIN )
1831 str_error = str_bad_cmdline;
1834 if ( l > 0 ) { max_iterations = (int)l; }
1840 l = strtol( str5, &ptr, 0 );
1841 if ( ptr == str5 || l > TLP_MAX_THREADS || l < 1 )
1843 str_error = str_bad_cmdline;
1851 l = strtol( str6, &ptr, 0 );
1852 if ( ptr == str6 || l > TLP_MAX_THREADS || l < 1 )
1854 str_error = str_bad_cmdline;
1861 AbortDifficultCommand;
1863 log2_ntrans_table = 12;
1865 memory_free( (void *)ptrans_table_orig );
1867 iret = ini_trans_table();
1868 if ( iret < 0 ) { return iret; }
1870 iret = learn( ptree, is_ini, nsteps, max_games, max_iterations,
1871 nworker1, nworker2 );
1872 if ( iret < 0 ) { return -1; }
1874 iret = ini_game( ptree, &min_posi_no_handicap, flag_history, NULL, NULL );
1875 if ( iret < 0 ) { return -1; }
1877 iret = get_elapsed( &time_turn_start );
1878 if ( iret < 0 ) { return iret; }
1882 #endif /* MINIMUM */
1886 static int CONV cmd_mpv( tree_t * restrict ptree, char **lasts )
1888 const char *str = strtok_r( NULL, str_delimiters, lasts );
1894 str_error = str_bad_cmdline;
1897 else if ( ! strcmp( str, "num" ) )
1899 str = strtok_r( NULL, str_delimiters, lasts );
1902 str_error = str_bad_cmdline;
1905 l = strtol( str, &ptr, 0 );
1906 if ( ptr == str || l == LONG_MAX || l < 1 || l > MPV_MAX_PV )
1908 str_error = str_bad_cmdline;
1912 AbortDifficultCommand;
1916 if ( analyze_mode ) return do_analyze ( ptree ); // [HGM] analyze: analysis should continue changing num
1920 else if ( ! strcmp( str, "width" ) )
1922 str = strtok_r( NULL, str_delimiters, lasts );
1925 str_error = str_bad_cmdline;
1928 l = strtol( str, &ptr, 0 );
1929 if ( ptr == str || l == LONG_MAX || l < MT_CAP_PAWN )
1931 str_error = str_bad_cmdline;
1935 AbortDifficultCommand;
1939 if ( analyze_mode ) return do_analyze ( ptree ); // [HGM] analyze: analysis should continue after changing width
1944 str_error = str_bad_cmdline;
1951 static int CONV cmd_dfpn( tree_t * restrict ptree, char **lasts )
1953 const char *str = strtok_r( NULL, str_delimiters, lasts );
1957 str_error = str_bad_cmdline;
1960 else if ( ! strcmp( str, "hash" ) )
1965 str = strtok_r( NULL, str_delimiters, lasts );
1968 str_error = str_bad_cmdline;
1971 l = strtol( str, &ptr, 0 );
1972 if ( ptr == str || l == LONG_MAX || l < 1 )
1974 str_error = str_bad_cmdline;
1978 AbortDifficultCommand;
1980 dfpn_hash_log2 = (unsigned int)l;
1981 return dfpn_ini_hash();
1983 else if ( ! strcmp( str, "go" ) )
1985 AbortDifficultCommand;
1987 return dfpn( ptree, root_turn, 1 );
1989 else if ( ! strcmp( str, "connect" ) )
1997 str = strtok_r( NULL, str_delimiters, lasts );
1998 if ( ! str || ! strcmp( str, "." ) ) { str = "127.0.0.1"; }
1999 strncpy( str_addr, str, 255 );
2000 str_addr[255] = '\0';
2002 str = strtok_r( NULL, str_delimiters, lasts );
2003 if ( ! str || ! strcmp( str, "." ) ) { str = "4083"; }
2004 l = strtol( str, &ptr, 0 );
2005 if ( ptr == str || l == LONG_MAX || l < 0 || l > USHRT_MAX )
2007 str_error = str_bad_cmdline;
2012 str = strtok_r( NULL, str_delimiters, lasts );
2013 if ( ! str || ! strcmp( str, "." ) ) { str = "bonanza1"; }
2014 strncpy( str_id, str, 255 );
2017 AbortDifficultCommand;
2019 dfpn_sckt = sckt_connect( str_addr, port );
2020 if ( dfpn_sckt == SCKT_NULL ) { return -2; }
2022 str_buffer_cmdline[0] = '\0';
2023 DFPNOut( "Worker: %s\n", str_id );
2028 str_error = str_bad_cmdline;
2035 static int CONV cmd_thread( char **lasts )
2037 const char *str = strtok_r( NULL, str_delimiters, lasts );
2041 str_error = str_bad_cmdline;
2044 else if ( ! strcmp( str, "num" ) )
2049 str = strtok_r( NULL, str_delimiters, lasts );
2052 str_error = str_bad_cmdline;
2055 l = strtol( str, &ptr, 0 );
2056 if ( ptr == str || l == LONG_MAX || l < 1 || l > TLP_MAX_THREADS )
2058 str_error = str_bad_cmdline;
2066 if ( game_status & ( flag_thinking | flag_pondering | flag_puzzling ) )
2073 str_error = str_bad_cmdline;
2079 #if defined(DFPN_CLIENT)
2080 static int CONV cmd_dfpn_client( tree_t * restrict ptree, char **lasts )
2086 AbortDifficultCommand;
2088 str = strtok_r( NULL, str_delimiters, lasts );
2089 if ( ! str || ! strcmp( str, "." ) ) { str = "127.0.0.1"; }
2090 strncpy( dfpn_client_str_addr, str, 255 );
2091 dfpn_client_str_addr[255] = '\0';
2093 str = strtok_r( NULL, str_delimiters, lasts );
2094 if ( ! str || ! strcmp( str, "." ) ) { str = "4083"; }
2095 dfpn_client_port = strtol( str, &ptr, 0 );
2096 if ( ptr == str || dfpn_client_port == LONG_MAX || dfpn_client_port < 0
2097 || dfpn_client_port > USHRT_MAX )
2099 str_error = str_bad_cmdline;
2103 Out( "DFPN Server: %s %d\n", dfpn_client_str_addr, dfpn_client_port );
2105 iret = ini_game( ptree, &min_posi_no_handicap, flag_history, NULL, NULL );
2106 if ( iret < 0 ) { return iret; }
2108 if ( dfpn_client_sckt == SCKT_NULL )
2110 str_error = "Check network status.";
2114 return get_elapsed( &time_turn_start );
2119 #if defined(CSA_LAN)
2120 static int CONV cmd_connect( tree_t * restrict ptree, char **lasts )
2126 str = strtok_r( NULL, str_delimiters, lasts );
2127 if ( ! str || ! strcmp( str, "." ) ) { str = "gserver.computer-shogi.org"; }
2128 strncpy( client_str_addr, str, 255 );
2129 client_str_addr[255] = '\0';
2131 str = strtok_r( NULL, str_delimiters, lasts );
2132 if ( ! str || ! strcmp( str, "." ) ) { str = "4081"; }
2133 client_port = strtol( str, &ptr, 0 );
2134 if ( ptr == str || client_port == LONG_MAX || client_port < 0
2135 || client_port > USHRT_MAX )
2137 str_error = str_bad_cmdline;
2141 str = strtok_r( NULL, str_delimiters, lasts );
2142 if ( ! str || ! strcmp( str, "." ) ) { str = "bonanza_test"; }
2143 strncpy( client_str_id, str, 255 );
2144 client_str_id[255] = '\0';
2146 str = strtok_r( NULL, " \t", lasts );
2147 if ( ! str || ! strcmp( str, "." ) ) { str = "bonanza_test"; }
2148 strncpy( client_str_pwd, str, 255 );
2149 client_str_pwd[255] = '\0';
2151 str = strtok_r( NULL, str_delimiters, lasts );
2152 if ( ! str || ! strcmp( str, "." ) ) { client_max_game = INT_MAX; }
2154 max_games = strtol( str, &ptr, 0 );
2155 if ( ptr == str || max_games == LONG_MAX || max_games < 1 )
2157 str_error = str_bad_cmdline;
2160 client_max_game = max_games;
2163 AbortDifficultCommand;
2167 return client_next_game( ptree, client_str_addr, (int)client_port );
2171 static int CONV cmd_sendpv( char **lasts )
2173 const char *str = strtok_r( NULL, str_delimiters, lasts );
2177 str_error = str_bad_cmdline;
2181 if ( ! strcmp( str, str_off ) ) { game_status &= ~flag_sendpv; }
2182 else if ( ! strcmp( str, str_on ) ) { game_status |= flag_sendpv; }
2184 str_error = str_bad_cmdline;
2193 #if defined(MNJ_LAN)
2194 /* mnj sd seed addr port name factor stable_depth */
2195 static int CONV cmd_mnj( char **lasts )
2197 char client_str_addr[256];
2198 char client_str_id[256];
2203 int client_port, sd;
2206 str = strtok_r( NULL, str_delimiters, lasts );
2209 str_error = str_bad_cmdline;
2212 l = strtol( str, &ptr, 0 );
2213 if ( ptr == str || l == LONG_MAX || l < 0 )
2215 str_error = str_bad_cmdline;
2221 str = strtok_r( NULL, str_delimiters, lasts );
2224 str_error = str_bad_cmdline;
2227 l = strtol( str, &ptr, 0 );
2228 if ( ptr == str || l == LONG_MAX || l < 0 )
2230 str_error = str_bad_cmdline;
2233 seed = (unsigned int)l;
2236 str = strtok_r( NULL, str_delimiters, lasts );
2237 if ( ! str || ! strcmp( str, "." ) ) { str = "localhost"; }
2238 strncpy( client_str_addr, str, 255 );
2239 client_str_addr[255] = '\0';
2242 str = strtok_r( NULL, str_delimiters, lasts );
2243 if ( ! str || ! strcmp( str, "." ) ) { str = "4082"; }
2244 l = strtol( str, &ptr, 0 );
2245 if ( ptr == str || l == LONG_MAX || l < 0 || l > USHRT_MAX )
2247 str_error = str_bad_cmdline;
2250 client_port = (int)l;
2253 str = strtok_r( NULL, str_delimiters, lasts );
2254 if ( ! str || ! strcmp( str, "." ) ) { str = "bonanza1"; }
2255 strncpy( client_str_id, str, 255 );
2256 client_str_id[255] = '\0';
2258 str = strtok_r( NULL, str_delimiters, lasts );
2259 if ( ! str || ! strcmp( str, "." ) ) { str = "1.0"; }
2260 factor = strtod( str, &ptr );
2261 if ( ptr == str || factor < 0.0 )
2263 str_error = str_bad_cmdline;
2267 str = strtok_r( NULL, str_delimiters, lasts );
2268 if ( ! str || ! strcmp( str, "." ) ) { l = -1; }
2270 l = strtol( str, &ptr, 0 );
2271 if ( ptr == str || l == LONG_MAX )
2273 str_error = str_bad_cmdline;
2277 if ( l <= 0 ) { mnj_depth_stable = INT_MAX; }
2278 else { mnj_depth_stable = (int)l; }
2280 AbortDifficultCommand;
2282 resign_threshold = 65535;
2283 game_status |= ( flag_noponder | flag_noprompt );
2284 if ( mnj_reset_tbl( sd, seed ) < 0 ) { return -1; }
2286 sckt_mnj = sckt_connect( client_str_addr, (int)client_port );
2287 if ( sckt_mnj == SCKT_NULL ) { return -2; }
2289 str_buffer_cmdline[0] = '\0';
2291 Out( "Sending my name %s", client_str_id );
2292 MnjOut( "%s %g final%s\n", client_str_id, factor,
2293 ( mnj_depth_stable == INT_MAX ) ? "" : " stable" );
2295 return cmd_suspend();