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;
132 int all_moves[MAX_LEGAL_MOVES];
135 #define IF(X) else if(!strcmp(command, X))
137 int myTime, hisTime, movesPerSession, inc, plyNr;
141 xboard_to_CSA( tree_t * restrict ptree, char *in, char *out, int status )
143 char fromX=in[0], fromY=in[1], toX=in[2], toY=in[3], promo=in[4];
144 int piece=0, bonanza_move=0;
145 if(fromY == '@') { // drop (contains all info needed to convert it)
146 if(fromX >= 'a') fromX += 'A' - 'a';
147 switch(fromX) { // encode piece
148 case 'P': piece = pawn; break;
149 case 'L': piece = lance; break;
150 case 'N': piece = knight; break;
151 case 'S': piece = silver; break;
152 case 'G': piece = gold; break;
153 case 'B': piece = bishop; break;
154 case 'R': piece = rook; break;
156 bonanza_move = Drop2Move(piece) | To2Move( ('9' - toY)*9 + (toX - 'a') );
157 sprintf(out, "00%c%c%s", 'a'+'9'-toX, '1'+'9'-toY, astr_table_piece[piece]);
158 } else { // board move (need to figure out moved piece)
159 int from = ('9' - fromY)*9 + (fromX - 'a');
160 int flag = (promo == '+' ? FLAG_PROMO : 0);
161 piece = abs( BOARD[from] ); // this only works when not searching!
162 printf("# piece from board: %d\n", piece);fflush(stdout);
163 if( status & ( flag_pondering | flag_puzzling | flag_thinking ) ) {
164 int i, to = ('9' - toY)*9 + (toX - 'a'), l = (status == (flag_pondering | flag_thinking));
165 piece = 0; // kludge to force illegal CSA move
166 for( i = 0; l ? all_moves[i] : i < root_nmove; i++ ) { // determine the piece from the move list
167 int move = (l ? all_moves[i] : root_move_list[i].move);
168 if( I2To(move) != to ) continue;
169 if( I2From(move) != from ) continue;
170 if( (move & FLAG_PROMO) != flag ) continue;
171 piece = I2PieceMove( move ); // we found the move; take the piece from it
175 printf("# piece corrected to %d\n", piece);fflush(stdout);
177 if( promo ) piece += promote;
178 sprintf(out, "%c%c%c%c%s", 'a'+'9'-fromX, '1'+'9'-fromY, 'a'+'9'-toX, '1'+'9'-toY, astr_table_piece[piece]);
184 update_exclude_list( tree_t * restrict ptree, int add, char *move_str )
185 { // [HGM] exclude: manage list of excluded moves
188 if( moves_ignore[0] == MOVE_NA ) { // nothing is excluded yet; make a copy of root move list;
189 for( i = 0; i < root_nmove; i++) all_moves[i] = root_move_list[i].move;
192 if ( ! strcmp(move_str, "all") ) { // all moves
193 if( add ) { // copy entire list of legal moves
194 for( i = 0; i < root_nmove; i++ ) moves_ignore[i] = all_moves[i];
195 } else moves_ignore[0] = MOVE_NA; // clear list
196 } else { // single move
197 int move = xboard_to_CSA( ptree, move_str, dummy, flag_pondering | flag_thinking); // kludge with impossible flag
198 for( i = 0; moves_ignore[i] != MOVE_NA; i++ ) if( move == moves_ignore[i] ) break;
199 if( add ) { // we must add the move
200 if( moves_ignore[i] != MOVE_NA ) return; // but it was already in list
201 if( i >= MAX_LEGAL_MOVES - 2 ) return; // overflow
202 moves_ignore[i] = move; moves_ignore[i+1] = MOVE_NA; // append move
203 } else { // we must delete the move
204 if( moves_ignore[i] == MOVE_NA ) return; // but it was not there
205 while( (moves_ignore[i] = moves_ignore[i+1]) ) i++; // squeeze it out
212 { // set white and black times from own and opponent time.
214 if(movesPerSession <= 0) moves = 35; else {
216 while(moves <= 0) moves += movesPerSession;
218 time_limit = (myTime-inc-30)/(moves+2) + inc;
219 time_max_limit = 3*time_limit;
220 if(time_max_limit > myTime - 30)time_max_limit = myTime - 30; // keep 0.3 sec margin, as Bonanza reads the clock infrequently
221 time_limit *= 10; // msec
222 time_max_limit *= 10;
223 Out("# moves=%d, time=%d inc=%d t=%d, max=%d\n", moves, myTime, inc, time_limit, time_max_limit);
227 proce_xboard(char *line, const char *command, tree_t * restrict ptree)
228 { // [HGM] added to make Bonanza.exe a native WinBoard engine
230 static char forceMode = 0;
231 sscanf(line + strlen(command) + 1, "%d", &value);
232 Out("# command = '%s'\n", line);
236 Out("feature option=\"MultiPV -spin 1 1 100\"\n");
237 Out("feature option=\"centi-Pawn margin -spin 200 0 25000\"\n");
239 Out("feature variants=\"shogi\" usermove=1 myname=\"Bonanza " BNZ_VER
240 "\" memory=1 smp=1 debug=1 colors=0 setboard=1 ping=1 sigint=0 exclude=1 done=1\n");
242 IF("new") { forceMode = plyNr = 0; SetTimes(); return 0; }
243 IF("easy") { strcpy(line, "ponder off"); return 0; }
244 IF("hard") { strcpy(line, "ponder on"); return 0; }
247 IF("time") { sscanf(line+5, "%d", &myTime); }
248 IF("otim") { sscanf(line+5, "%d", &hisTime); }
249 IF("force") { forceMode = 1; }
250 IF("go") { forceMode = 0; SetTimes(); plyNr++; strcpy(line, "move"); return 0; }
252 IF("cores") { sprintf(line, "tlp num %d", value); return 0; }
253 IF("sd") { sprintf(line, "limit depth %d", value); return 0; }
255 IF("quit") { return 0; }
256 IF("analyze") { return 0; }
257 IF("exit") { return 0; }
258 IF("variant") { /* ignore, since it must be Shogi */; }
261 if(sscanf(line+7, "MultiPV=%d", &value) == 1) { sprintf(line, "mpv num %d", value); return 0; }
262 if(sscanf(line+7, "centi-Pawn margin=%d", &value) == 1) { sprintf(line, "mpv width %d", value); return 0; }
264 IF("level") { int min, sec; float fsec=0.;
265 if(sscanf(line+6, "%d %d:%d %f", &movesPerSession, &min, &sec, &fsec) != 4)
266 sscanf(line+6, "%d %d %f", &movesPerSession, &min, &fsec);
267 min = 60*min + sec; myTime = hisTime = 100*min; inc = 100 * fsec;
269 IF("usermove") { char buf[20];
270 xboard_to_CSA( ptree, line+9, buf, game_status );
271 if(forceMode || analyze_mode) strcpy(line, "move "), line += 5; else plyNr++, SetTimes();
276 IF("undo") { return 0; }
278 IF("ping") { Out("pong %d\n", value); }
279 IF("exclude") { update_exclude_list( ptree, 1, line+8 ); strcpy(line, "analyze"); return 0; }
280 IF("include") { update_exclude_list( ptree, 0, line+8 ); strcpy(line, "analyze"); return 0; }
285 static int CONV proce_cui( tree_t * restrict ptree )
290 token = strtok_r( str_cmdline, str_delimiters, &last );
292 if ( token == NULL || *token == '#' ) { return 1; }
296 if( proce_xboard(str_cmdline, token, ptree) ) return 1; // command already processed
297 Out("# translated command '%s'\n", str_cmdline); // command translated for processing by Bonanza
298 token = strtok_r( str_cmdline, str_delimiters, &last ); // redo parsing
300 if ( ! strcmp( token, "xboard" ) ) { xboard_mode = 1; game_status |= flag_noprompt; return 1; }
303 if ( ! strcmp( token, "analyze" ) ) { return cmd_analyze( ptree ); } // [HGM] analyze
304 if ( ! strcmp( token, "exit" ) ) { return cmd_exit(); } // [HGM] analyze
305 if ( ! strcmp( token, "move" ) ) { return cmd_move( ptree, &last ); }
306 if ( ! strcmp( token, "undo" ) ) { return cmd_undo( ptree ); } // [HGM] undo
308 if ( ! strcmp( token, "mpv" ) ) { return cmd_mpv( ptree, &last ); }
310 analyze_mode = 0; // [HGM] analyze: all other commands terminate analysis
311 if ( is_move( token ) ) { return cmd_usrmove( ptree, token, &last ); }
312 if ( ! strcmp( token, "s" ) ) { return cmd_move_now(); }
313 if ( ! strcmp( token, "beep" ) ) { return cmd_beep( &last); }
314 if ( ! strcmp( token, "book" ) ) { return CmdBook( ptree, &last ); }
315 if ( ! strcmp( token, "display" ) ) { return cmd_display( ptree, &last ); }
316 if ( ! strcmp( token, "hash" ) ) { return cmd_hash( &last ); }
317 if ( ! strcmp( token, "limit" ) ) { return cmd_limit( &last ); }
318 if ( ! strcmp( token, "new" ) ) { return cmd_new( ptree, &last ); }
319 if ( ! strcmp( token, "outmove" ) ) { return cmd_outmove( ptree ); }
320 if ( ! strcmp( token, "peek" ) ) { return cmd_peek( &last ); }
321 if ( ! strcmp( token, "stdout" ) ) { return cmd_stdout( &last ); }
322 if ( ! strcmp( token, "ping" ) ) { return cmd_ping(); }
323 if ( ! strcmp( token, "ponder" ) ) { return cmd_ponder( &last ); }
324 if ( ! strcmp( token, "problem" ) ) { return cmd_problem( ptree, &last ); }
325 if ( ! strcmp( token, "quit" ) ) { return cmd_quit(); }
326 if ( ! strcmp( token, "read" ) ) { return cmd_read( ptree, &last ); }
327 if ( ! strcmp( token, "resign" ) ) { return cmd_resign( ptree, &last ); }
328 if ( ! strcmp( token, "suspend" ) ) { return cmd_suspend(); }
329 if ( ! strcmp( token, "time" ) ) { return cmd_time( &last ); }
330 if ( ! strcmp( token, "newlog" ) ) { return cmd_newlog( &last ); }
332 if ( ! strcmp( token, "connect" ) ) { return cmd_connect( ptree, &last ); }
333 if ( ! strcmp( token, "sendpv" ) ) { return cmd_sendpv( &last ); }
336 if ( ! strcmp( token, "mnj" ) ) { return cmd_mnj( &last ); }
339 if ( ! strcmp( token, "dfpn" ) ) { return cmd_dfpn( ptree, &last ); }
341 #if defined(DFPN_CLIENT)
342 if ( ! strcmp( token, "dfpn_client")) { return cmd_dfpn_client( ptree,
346 if ( ! strcmp( token, "tlp" ) ) { return cmd_thread( &last ); }
348 #if ! defined(NO_STDOUT)
349 if ( ! strcmp( token, "stress" ) ) { return cmd_stress( &last ); }
351 #if ! defined(MINIMUM)
352 if ( ! strcmp( token, "learn" ) ) { return cmd_learn( ptree, &last ); }
355 str_error = str_bad_cmdline;
361 static int CONV proce_csalan( tree_t * restrict ptree )
366 token = strtok_r( str_cmdline, str_delimiters, &last );
368 if ( token == NULL ) { return 1; }
369 if ( *token == ach_turn[client_turn] && is_move( token+1 ) )
374 token = strtok_r( NULL, str_delimiters, &last );
375 if ( token == NULL || *token != 'T' )
377 str_error = str_bad_cmdline;
381 l = strtol( token+1, &ptr, 0 );
382 if ( token+1 == ptr || l == LONG_MAX || l < 1 )
384 str_error = str_bad_cmdline;
388 adjust_time( (unsigned int)l, client_turn );
389 Out( " elapsed: b%u, w%u\n", sec_b_total, sec_w_total );
392 if ( *token == ach_turn[Flip(client_turn)] && is_move( token+1 ) )
394 return cmd_usrmove( ptree, token+1, &last );
396 if ( ! strcmp( token, str_resign ) ) { return cmd_resign( ptree, &last ); }
397 if ( ! strcmp( token, "#WIN" )
398 || ! strcmp( token, "#LOSE" )
399 || ! strcmp( token, "#DRAW" )
400 || ! strcmp( token, "#CHUDAN" ) )
402 if ( game_status & ( flag_thinking | flag_pondering | flag_puzzling ) )
404 game_status |= flag_suspend;
408 if ( sckt_out( sckt_csa, "LOGOUT\n" ) < 0 ) { return -1; }
409 if ( sckt_recv_all( sckt_csa ) < 0 ) { return -1; }
413 if ( client_ngame == client_max_game ) { return cmd_quit(); }
415 return client_next_game( ptree, client_str_addr, (int)client_port );
424 static int CONV proce_mnj( tree_t * restrict ptree )
430 token = strtok_r( str_cmdline, str_delimiters, &last );
431 if ( token == NULL ) { return 1; }
433 if ( ! strcmp( token, "new" ) )
435 iret = cmd_suspend();
436 if ( iret != 1 ) { return iret; }
439 iret = cmd_new( ptree, &last );
440 if ( iret < 0 ) { return iret; }
442 moves_ignore[0] = MOVE_NA;
443 return analyze( ptree );
445 if ( ! strcmp( token, "ignore" ) ) { return cmd_mnjignore( ptree, &last ); }
446 if ( ! strcmp( token, "idle" ) ) { return cmd_suspend(); }
447 if ( ! strcmp( token, "alter" ) ) { return cmd_mnjmove( ptree, &last, 1 ); }
448 if ( ! strcmp( token, "retract" ) )
452 const char *str = strtok_r( NULL, str_delimiters, &last );
455 str_error = str_bad_cmdline;
458 l = strtol( str, &ptr, 0 );
459 if ( ptr == str || (long)NUM_UNMAKE < l )
461 str_error = str_bad_cmdline;
465 return cmd_mnjmove( ptree, &last, (int)l );
467 if ( ! strcmp( token, "move" ) ) { return cmd_mnjmove( ptree, &last, 0 ); }
469 str_error = str_bad_cmdline;
475 cmd_mnjignore( tree_t *restrict ptree, char **lasts )
484 token = strtok_r( NULL, str_delimiters, lasts );
487 str_error = str_bad_cmdline;
490 lid = strtol( token, &ptr, 0 );
491 if ( ptr == token || lid == LONG_MAX || lid < 1 )
493 str_error = str_bad_cmdline;
497 AbortDifficultCommand;
499 for ( i = 0; ; i += 1 )
501 token = strtok_r( NULL, str_delimiters, lasts );
502 if ( token == NULL ) { break; }
504 if ( interpret_CSA_move( ptree, &move, token ) < 0 ) { return -1; }
506 moves_ignore[i] = move;
510 str_error = str_bad_cmdline;
513 mnj_posi_id = (int)lid;
514 moves_ignore[i] = MOVE_NA;
516 return analyze( ptree );
521 cmd_mnjmove( tree_t * restrict ptree, char **lasts, int num_alter )
523 const char *str1 = strtok_r( NULL, str_delimiters, lasts );
524 const char *str2 = strtok_r( NULL, str_delimiters, lasts );
530 if ( sckt_mnj == SCKT_NULL || str1 == NULL || str2 == NULL )
532 str_error = str_bad_cmdline;
536 lid = strtol( str2, &ptr, 0 );
537 if ( ptr == str2 || lid == LONG_MAX || lid < 1 )
539 str_error = str_bad_cmdline;
543 AbortDifficultCommand;
547 iret = unmake_move_root( ptree );
548 if ( iret < 0 ) { return iret; }
553 iret = interpret_CSA_move( ptree, &move, str1 );
554 if ( iret < 0 ) { return iret; }
556 iret = get_elapsed( &time_turn_start );
557 if ( iret < 0 ) { return iret; }
559 mnj_posi_id = (int)lid;
561 iret = make_move_root( ptree, move, ( flag_time | flag_rep
562 | flag_detect_hang ) );
563 if ( iret < 0 ) { return iret; }
565 # if ! defined(NO_STDOUT)
566 iret = out_board( ptree, stdout, 0, 0 );
567 if ( iret < 0 ) { return iret; }
570 moves_ignore[0] = MOVE_NA;
571 return analyze( ptree );
577 do_analyze( tree_t * restrict ptree )
578 { // [HGM] analyze: do a ponder search on the current position
580 if ( get_elapsed( &time_start ) < 0 ) { return -1; }
581 time_limit = time_max_limit = 1e9; // kludge: use huge time to mimic infinity
583 if(xboard_mode) Out("1 0 0 0 New Search\n"); // make sure lower depth is emitted, so XBoard undestand new search started
585 game_status |= flag_pondering;
586 iret = iterate( ptree );
587 game_status &= ~flag_pondering;
593 cmd_undo( tree_t * restrict ptree )
594 { // [HGM] undo: restart the game, and feed all moves except the last
595 int i, last = move_ptr;
596 char *p = start_data;
597 if( move_ptr <= 0 ) {
598 str_error = "undo past start of game ignored";
602 AbortDifficultCommand;
605 cmd_new( ptree, &p );
606 for(i=0; i<last; i++) {
607 make_move_root( ptree, move_list[i], 0);
610 moves_ignore[0] = MOVE_NA; // [HGM] exclude: exclude list cleared for new position
611 if ( analyze_mode ) return do_analyze ( ptree ); // [HGM] analyze: analysis should continue after undo
618 cmd_analyze( tree_t * restrict ptree )
619 { // [HGM] analyze: switch on analyze mode, and start analyzing (also used to force a restart)
620 AbortDifficultCommand;
623 return do_analyze( ptree );
629 { // [HGM] analyze: switch off analysis mode
630 if ( !analyze_mode ) {
631 str_error = "was not analyzing";
635 if ( game_status & flag_pondering ) { game_status |= flag_quit_ponder; return 2; }
637 moves_ignore[0] = MOVE_NA; // [HGM] exclude: exclude list cleared after analysis
644 static int CONV proce_usi( tree_t * restrict ptree )
650 token = strtok_r( str_cmdline, str_delimiters, &lasts );
651 if ( token == NULL ) { return 1; }
653 if ( ! strcmp( token, "usi" ) )
655 USIOut( "id name %s\n", str_myname );
656 USIOut( "id author Kunihito Hoki\n" );
661 if ( ! strcmp( token, "isready" ) )
663 USIOut( "readyok\n", str_myname );
667 if ( ! strcmp( token, "echo" ) )
669 USIOut( "%s\n", lasts );
673 if ( ! strcmp( token, "ignore_moves" ) )
675 return usi_ignore( ptree, &lasts );
678 if ( ! strcmp( token, "genmove_probability" ) )
680 if ( get_elapsed( &time_start ) < 0 ) { return -1; }
681 return usi_root_list( ptree );
684 if ( ! strcmp( token, "go" ) )
686 iret = usi_go( ptree, &lasts );
687 moves_ignore[0] = MOVE_NA;
691 if ( ! strcmp( token, "stop" ) ) { return cmd_move_now(); }
692 if ( ! strcmp( token, "position" ) ) { return usi_posi( ptree, &lasts ); }
693 if ( ! strcmp( token, "quit" ) ) { return cmd_quit(); }
695 str_error = str_bad_cmdline;
701 usi_ignore( tree_t * restrict ptree, char **lasts )
708 AbortDifficultCommand;
710 for ( i = 0; ; i += 1 )
712 token = strtok_r( NULL, str_delimiters, lasts );
713 if ( token == NULL ) { break; }
715 if ( usi2csa( ptree, token, str_buf ) < 0 ) { return -1; }
716 if ( interpret_CSA_move( ptree, &move, str_buf ) < 0 ) { return -1; }
718 moves_ignore[i] = move;
721 moves_ignore[i] = MOVE_NA;
728 usi_go( tree_t * restrict ptree, char **lasts )
735 AbortDifficultCommand;
737 if ( game_status & mask_game_end )
739 str_error = str_game_ended;
743 token = strtok_r( NULL, str_delimiters, lasts );
745 if ( ! strcmp( token, "book" ) )
747 AbortDifficultCommand;
748 if ( usi_book( ptree ) < 0 ) { return -1; }
754 if ( ! strcmp( token, "infinite" ) )
756 usi_byoyomi = UINT_MAX;
757 depth_limit = PLY_MAX;
758 node_limit = UINT64_MAX;
759 sec_limit_depth = UINT_MAX;
761 else if ( ! strcmp( token, "byoyomi" ) )
763 token = strtok_r( NULL, str_delimiters, lasts );
766 str_error = str_bad_cmdline;
770 l = strtol( token, &ptr, 0 );
771 if ( ptr == token || l > UINT_MAX || l < 1 )
773 str_error = str_bad_cmdline;
777 usi_byoyomi = (unsigned int)l;
778 depth_limit = PLY_MAX;
779 node_limit = UINT64_MAX;
780 sec_limit_depth = UINT_MAX;
783 str_error = str_bad_cmdline;
788 if ( get_elapsed( &time_turn_start ) < 0 ) { return -1; }
790 iret = com_turn_start( ptree, 0 );
792 if ( str_error == str_no_legal_move ) { USIOut( "bestmove resign\n" ); }
801 usi_posi( tree_t * restrict ptree, char **lasts )
807 AbortDifficultCommand;
809 moves_ignore[0] = MOVE_NA;
811 token = strtok_r( NULL, str_delimiters, lasts );
812 if ( strcmp( token, "startpos" ) )
814 str_error = str_bad_cmdline;
818 if ( ini_game( ptree, &min_posi_no_handicap,
819 flag_history, NULL, NULL ) < 0 ) { return -1; }
821 token = strtok_r( NULL, str_delimiters, lasts );
822 if ( token == NULL ) { return 1; }
824 if ( strcmp( token, "moves" ) )
826 str_error = str_bad_cmdline;
832 token = strtok_r( NULL, str_delimiters, lasts );
833 if ( token == NULL ) { break; }
835 if ( usi2csa( ptree, token, str_buf ) < 0 ) { return -1; }
836 if ( interpret_CSA_move( ptree, &move, str_buf ) < 0 ) { return -1; }
837 if ( make_move_root( ptree, move, ( flag_history | flag_time
839 | flag_detect_hang ) ) < 0 )
845 if ( get_elapsed( &time_turn_start ) < 0 ) { return -1; }
852 static int CONV cmd_move_now( void )
854 if ( game_status & flag_thinking ) { game_status |= flag_move_now; }
861 cmd_usrmove( tree_t * restrict ptree, const char *str_move, char **lasts )
869 if ( game_status & mask_game_end )
871 str_error = str_game_ended;
875 if ( game_status & flag_thinking )
877 str_error = str_busy_think;
881 str = strtok_r( NULL, str_delimiters, lasts );
882 if ( str == NULL ) { lelapsed = 0; }
886 str_error = str_bad_cmdline;
890 lelapsed = strtol( str, &ptr, 0 );
891 if ( ptr == str || lelapsed == LONG_MAX || lelapsed < 1 )
893 str_error = str_bad_cmdline;
898 if ( game_status & ( flag_pondering | flag_puzzling ) )
902 for ( i = 0; i < ponder_nmove; i++ )
904 if ( ! strcmp( str_move, str_CSA_move(ponder_move_list[i]) ) )
909 if ( i == ponder_nmove )
912 if ( sckt_csa != SCKT_NULL ) { AbortDifficultCommand; }
915 #if defined(CSASHOGI)
916 AbortDifficultCommand;
918 str_error = str_illegal_move;
923 if ( ( game_status & flag_puzzling )
924 || strcmp( str_move, str_CSA_move(ponder_move) ) )
926 ponder_move = MOVE_PONDER_FAILED;
927 game_status |= flag_quit_ponder;
931 iret = update_time( Flip(root_turn) );
932 if ( iret < 0 ) { return iret; }
935 adjust_time( (unsigned int)lelapsed, Flip(root_turn) );
938 out_CSA( ptree, &record_game, ponder_move );
940 game_status &= ~flag_pondering;
941 game_status |= flag_thinking;
945 set_search_limit_time( root_turn );
947 OutCsaShogi( "info ponder end\n" );
949 str = str_time_symple( time_turn_start - time_start );
950 Out( " %6s MOVE PREDICTION HIT\n"
951 " elapsed: b%u, w%u\n", str, sec_b_total, sec_w_total );
956 iret = interpret_CSA_move( ptree, &move, str_move );
957 if ( iret < 0 ) { return iret; }
958 move_evasion_pchk = 0;
959 iret = make_move_root( ptree, move, ( flag_rep | flag_history | flag_time
960 | flag_detect_hang ) );
965 if ( sckt_csa != SCKT_NULL )
967 if ( move_evasion_pchk )
969 str = str_CSA_move( move_evasion_pchk );
970 iret = sckt_out( sckt_csa, "%c%s\n",
971 ach_turn[Flip(root_turn)], str );
972 if ( iret < 0 ) { return iret; }
974 return cmd_suspend();
978 if ( move_evasion_pchk )
980 str = str_CSA_move( move_evasion_pchk );
981 #if defined(CSASHOGI)
982 OutCsaShogi( "move%s\n", str );
983 return cmd_suspend();
985 snprintf( str_message, SIZE_MESSAGE, "perpetual check (%c%s)",
986 ach_turn[Flip(root_turn)], str );
987 str_error = str_message;
995 if ( lelapsed ) { adjust_time( (unsigned int)lelapsed, Flip(root_turn) ); }
996 Out( " elapsed: b%u, w%u\n", sec_b_total, sec_w_total );
999 if ( sckt_csa != SCKT_NULL && ( game_status & flag_mated ) )
1001 iret = sckt_out( sckt_csa, "%%TORYO\n" );
1002 if ( iret < 0 ) { return iret; }
1006 if ( ! ( game_status & mask_game_end ) )
1008 iret = com_turn_start( ptree, 0 );
1009 if ( iret < 0 ) { return iret; }
1016 static int CONV cmd_beep( char **lasts )
1018 const char *str = strtok_r( NULL, str_delimiters, lasts );
1021 str_error = str_bad_cmdline;
1025 if ( ! strcmp( str, str_on ) ) { game_status &= ~flag_nobeep; }
1026 else if ( ! strcmp( str, str_off ) ) { game_status |= flag_nobeep; }
1028 str_error = str_bad_cmdline;
1036 static int CONV cmd_peek( char **lasts )
1038 const char *str = strtok_r( NULL, str_delimiters, lasts );
1042 str_error = str_bad_cmdline;
1046 if ( ! strcmp( str, str_on ) ) { game_status &= ~flag_nopeek; }
1047 else if ( ! strcmp( str, str_off ) ) { game_status |= flag_nopeek; }
1049 str_error = str_bad_cmdline;
1057 static int CONV cmd_stdout( char **lasts )
1059 const char *str = strtok_r( NULL, str_delimiters, lasts );
1063 str_error = str_bad_cmdline;
1067 if ( ! strcmp( str, str_on ) ) { game_status &= ~flag_nostdout; }
1068 else if ( ! strcmp( str, str_off ) ) { game_status |= flag_nostdout; }
1070 str_error = str_bad_cmdline;
1078 static int CONV cmd_newlog( char **lasts )
1080 const char *str = strtok_r( NULL, str_delimiters, lasts );
1084 str_error = str_bad_cmdline;
1088 if ( ! strcmp( str, str_on ) ) { game_status &= ~flag_nonewlog; }
1089 else if ( ! strcmp( str, str_off ) ) { game_status |= flag_nonewlog; }
1091 str_error = str_bad_cmdline;
1099 static int CONV cmd_ponder( char **lasts )
1101 const char *str = strtok_r( NULL, str_delimiters, lasts );
1105 str_error = str_bad_cmdline;
1109 if ( ! strcmp( str, str_on ) ) { game_status &= ~flag_noponder; }
1110 else if ( ! strcmp( str, str_off ) )
1112 if ( game_status & ( flag_pondering | flag_puzzling ) )
1114 game_status |= flag_quit_ponder;
1116 game_status |= flag_noponder;
1119 str_error = str_bad_cmdline;
1127 #if ! defined(NO_STDOUT)
1128 static int CONV cmd_stress( char **lasts )
1130 const char *str = strtok_r( NULL, str_delimiters, lasts );
1134 str_error = str_bad_cmdline;
1138 if ( ! strcmp( str, str_on ) ) { game_status &= ~flag_nostress; }
1139 else if ( ! strcmp( str, str_off ) ) { game_status |= flag_nostress; }
1141 str_error = str_bad_cmdline;
1151 #if defined(MINIMUM)
1152 cmd_book( char **lasts )
1154 cmd_book( tree_t * restrict ptree, char **lasts )
1157 const char *str = strtok_r( NULL, str_delimiters, lasts );
1162 str_error = str_bad_cmdline;
1165 if ( ! strcmp( str, str_on ) ) { iret = book_on(); }
1166 else if ( ! strcmp( str, str_off ) ) { iret = book_off(); }
1167 else if ( ! strcmp( str, "narrow" ) ) { game_status |= flag_narrow_book; }
1168 else if ( ! strcmp( str, "wide" ) ) { game_status &= ~flag_narrow_book; }
1169 #if ! defined(MINIMUM)
1170 else if ( ! strcmp( str, "create" ) )
1172 AbortDifficultCommand;
1174 iret = book_create( ptree );
1175 if ( iret < 0 ) { return iret; }
1177 iret = ini_game( ptree, &min_posi_no_handicap, flag_history,
1179 if ( iret < 0 ) { return iret; }
1181 iret = get_elapsed( &time_turn_start );
1185 str_error = str_bad_cmdline;
1193 static int CONV cmd_display( tree_t * restrict ptree, char **lasts )
1195 const char *str = strtok_r( NULL, str_delimiters, lasts );
1202 l = strtol( str, &ptr, 0 );
1205 str_error = str_bad_cmdline;
1208 if ( l == 1 ) { game_status &= ~flag_reverse; }
1209 else if ( l == 2 ) { game_status |= flag_reverse; }
1211 str_error = str_bad_cmdline;
1217 iret = out_board( ptree, stdout, 0, 0 );
1218 if ( iret < 0 ) { return iret; }
1219 #if ! defined(NO_LOGGING)
1220 iret = out_board( ptree, pf_log, 0, 0 );
1221 if ( iret < 0 ) { return iret; }
1229 static int CONV cmd_ping( void )
1231 OutCsaShogi( "pong\n" );
1237 static int CONV cmd_hash( char **lasts )
1239 const char *str = strtok_r( NULL, str_delimiters, lasts );
1245 str_error = str_bad_cmdline;
1249 l = strtol( str, &ptr, 0 );
1250 if ( ptr == str || l == LONG_MAX || l < 1 || l > 31 )
1252 str_error = str_bad_cmdline;
1256 AbortDifficultCommand;
1258 log2_ntrans_table = (int)l;
1259 memory_free( (void *)ptrans_table_orig );
1260 return ini_trans_table();
1264 static int CONV cmd_limit( char **lasts )
1266 const char *str = strtok_r( NULL, str_delimiters, lasts );
1272 str_error = str_bad_cmdline;
1276 AbortDifficultCommand;
1278 if ( ! strcmp( str, "depth" ) )
1280 str = strtok_r( NULL, str_delimiters, lasts );
1283 str_error = str_bad_cmdline;
1286 l1 = strtol( str, &ptr, 0 );
1287 if ( ptr == str || l1 == LONG_MAX || l1 < 1 )
1289 str_error = str_bad_cmdline;
1292 sec_limit_up = UINT_MAX;
1293 node_limit = UINT64_MAX;
1294 depth_limit = (int)l1;
1296 else if ( ! strcmp( str, "nodes" ) )
1298 str = strtok_r( NULL, str_delimiters, lasts );
1301 str_error = str_bad_cmdline;
1304 l1 = strtol( str, &ptr, 0 );
1305 if ( ptr == str || l1 == LONG_MAX || l1 < 1 )
1307 str_error = str_bad_cmdline;
1310 sec_limit_up = UINT_MAX;
1311 depth_limit = PLY_MAX;
1312 node_limit = (uint64_t)l1;
1314 else if ( ! strcmp( str, "time" ) )
1316 str = strtok_r( NULL, str_delimiters, lasts );
1319 str_error = str_bad_cmdline;
1323 if ( ! strcmp( str, "extendable" ) )
1325 game_status |= flag_time_extendable;
1327 else if ( ! strcmp( str, "strict" ) )
1329 game_status &= ~flag_time_extendable;
1332 l1 = strtol( str, &ptr, 0 );
1333 if ( ptr == str || l1 == LONG_MAX || l1 < 0 )
1335 str_error = str_bad_cmdline;
1339 str = strtok_r( NULL, str_delimiters, lasts );
1342 str_error = str_bad_cmdline;
1345 l2 = strtol( str, &ptr, 0 );
1346 if ( ptr == str || l2 == LONG_MAX || l2 < 0 )
1348 str_error = str_bad_cmdline;
1352 str = strtok_r( NULL, str_delimiters, lasts );
1353 if ( ! str ) { l3 = -1; }
1355 l3 = strtol( str, &ptr, 0 );
1356 if ( ptr == str || l3 >= PLY_MAX || l3 < -1 )
1358 str_error = str_bad_cmdline;
1363 if ( ! ( l1 | l2 ) ) { l2 = 1; }
1365 depth_limit = PLY_MAX;
1366 node_limit = UINT64_MAX;
1367 sec_limit = (unsigned int)l1 * 60U;
1368 sec_limit_up = (unsigned int)l2;
1369 if ( l3 == -1 ) { sec_limit_depth = UINT_MAX; }
1370 else { sec_limit_depth = (unsigned int)l3; }
1374 str_error = str_bad_cmdline;
1383 cmd_read( tree_t * restrict ptree, char **lasts )
1385 const char *str1 = strtok_r( NULL, str_delimiters, lasts );
1386 const char *str2 = strtok_r( NULL, str_delimiters, lasts );
1387 const char *str3 = strtok_r( NULL, str_delimiters, lasts );
1388 const char *str_tmp;
1389 FILE *pf_src, *pf_dest;
1390 char str_file[SIZE_FILENAME];
1396 flag = flag_history | flag_rep | flag_detect_hang;
1402 str_error = str_bad_cmdline;
1408 if ( ! strcmp( str2, "t" ) ) { flag |= flag_time; }
1409 else if ( strcmp( str2, "nil" ) )
1411 str_error = str_bad_cmdline;
1418 l = strtol( str3, &ptr, 0 );
1419 if ( ptr == str3 || l == LONG_MAX || l < 1 )
1421 str_error = str_bad_cmdline;
1424 moves = (unsigned int)l - 1U;
1427 AbortDifficultCommand;
1429 if ( ! strcmp( str1, "." ) )
1431 str_tmp = "game.cs_";
1433 #if defined(NO_LOGGING)
1434 strncpy( str_file, "game.csa", SIZE_FILENAME-1 );
1436 snprintf( str_file, SIZE_FILENAME, "%s/game%03d.csa",
1437 str_dir_logs, record_num );
1439 pf_dest = file_open( str_tmp, "w" );
1440 if ( pf_dest == NULL ) { return -2; }
1442 pf_src = file_open( str_file, "r" );
1443 if ( pf_src == NULL )
1445 file_close( pf_dest );
1449 while ( ( c = getc(pf_src) ) != EOF ) { putc( c, pf_dest ); }
1451 iret = file_close( pf_src );
1454 file_close( pf_dest );
1458 iret = file_close( pf_dest );
1459 if ( iret < 0 ) { return iret; }
1465 iret = read_record( ptree, str1, moves, flag );
1466 if ( iret < 0 ) { return iret; }
1468 iret = get_elapsed( &time_turn_start );
1469 if ( iret < 0 ) { return iret; }
1471 if ( str_tmp && remove( str_tmp ) )
1473 out_warning( "remove() failed." );
1481 static int CONV cmd_resign( tree_t * restrict ptree, char **lasts )
1483 const char *str = strtok_r( NULL, str_delimiters, lasts );
1487 if ( str == NULL || *str == 'T' )
1489 AbortDifficultCommand;
1491 if ( game_status & mask_game_end ) { return 1; }
1493 game_status |= flag_resigned;
1494 update_time( root_turn );
1495 out_CSA( ptree, &record_game, MOVE_RESIGN );
1498 l = strtol( str, &ptr, 0 );
1499 if ( ptr == str || l == LONG_MAX || l < MT_CAP_PAWN )
1501 str_error = str_bad_cmdline;
1504 resign_threshold = (int)l;
1511 static int CONV cmd_move( tree_t * restrict ptree, char **lasts )
1513 const char *str = strtok_r( NULL, str_delimiters, lasts );
1519 if ( game_status & mask_game_end )
1521 str_error = str_game_ended;
1525 AbortDifficultCommand;
1529 if ( analyze_mode ) // [HGM] analyze: in analysis mode we cannot set the engine thinking (but perhaps play PV move?)
1531 str_error = str_bad_cmdline;
1535 iret = get_elapsed( &time_turn_start );
1536 if ( iret < 0 ) { return iret; }
1538 return com_turn_start( ptree, 0 );
1541 l = strtol( str, &ptr, 0 );
1542 if ( str != ptr && l != LONG_MAX && l >= 1 && *ptr == '\0' )
1544 if ( analyze_mode ) // [HGM] analyze: in analysis mode we cannot set the engine thinking
1546 str_error = str_bad_cmdline;
1550 for ( i = 0; i < l; i += 1 )
1552 if ( game_status & ( flag_move_now | mask_game_end ) ) { break; }
1554 iret = get_elapsed( &time_turn_start );
1555 if ( iret < 0 ) { return iret; }
1557 iret = com_turn_start( ptree, 0 );
1558 if ( iret < 0 ) { return iret; }
1565 iret = interpret_CSA_move( ptree, &move, str );
1566 if ( iret < 0 ) { return iret; }
1568 iret = get_elapsed( &time_turn_start );
1569 if ( iret < 0 ) { return iret; }
1571 iret = make_move_root( ptree, move,
1572 ( flag_history | flag_time | flag_rep
1573 | flag_detect_hang ) );
1574 if ( iret < 0 ) { return iret; }
1576 str = strtok_r( NULL, str_delimiters, lasts );
1578 } while ( str != NULL );
1580 moves_ignore[0] = MOVE_NA; // [HGM] exclude: exclude list cleared for new position
1581 if ( analyze_mode ) return do_analyze ( ptree ); // [HGM] analyze: analysis should continue after feeding moves
1587 static int CONV cmd_new( tree_t * restrict ptree, char **lasts )
1589 const char *str1 = strtok_r( NULL, str_delimiters, lasts );
1590 const char *str2 = strtok_r( NULL, str_delimiters, lasts );
1591 const min_posi_t *pmp;
1592 min_posi_t min_posi;
1595 AbortDifficultCommand;
1597 start_pos = *lasts; move_ptr = 0; // [HGM] undo: remember start position
1598 moves_ignore[0] = MOVE_NA; // [HGM] exclude: exclude list cleared for new position
1602 strncpy(start_data, str1, 511); // [HGM] undo: remember start position
1603 memset( &min_posi.asquare, empty, nsquare );
1604 min_posi.hand_black = min_posi.hand_white = 0;
1605 iret = read_board_rep1( str1, &min_posi );
1606 if ( iret < 0 ) { return iret; }
1610 if ( ! strcmp( str2, "-" ) ) { min_posi.turn_to_move = white; }
1611 else if ( ! strcmp( str2, "+" ) ) { min_posi.turn_to_move = black; }
1613 str_error = str_bad_cmdline;
1617 else { min_posi.turn_to_move = black; }
1621 else { pmp = &min_posi_no_handicap; }
1623 iret = ini_game( ptree, pmp, flag_history, NULL, NULL );
1624 if ( iret < 0 ) { return iret; }
1626 return get_elapsed( &time_turn_start );
1630 static int CONV cmd_outmove( tree_t * restrict ptree )
1632 const char *str_move;
1634 unsigned int move_list[ MAX_LEGAL_MOVES ];
1637 AbortDifficultCommand;
1639 if ( game_status & mask_game_end )
1641 Out( "NO LEGAL MOVE\n" );
1642 DFPNOut( "NO LEGAL MOVE\n" );
1646 n = gen_legal_moves( ptree, move_list, 0 );
1649 for ( c = i = 0; i < n; i += 1 )
1651 str_move = str_CSA_move(move_list[i]);
1653 if ( i && ( i % 10 ) == 0 )
1655 Out( "%s\n", buffer );
1656 DFPNOut( "%s ", buffer );
1657 memcpy( buffer, str_move, 6 );
1663 memcpy( buffer + c + 1, str_move, 6 );
1667 memcpy( buffer + c, str_move, 6 );
1672 Out( "%s\n", buffer );
1673 DFPNOut( "%s\n", buffer );
1679 static int CONV cmd_problem( tree_t * restrict ptree, char **lasts )
1681 const char *str = strtok_r( NULL, str_delimiters, lasts );
1684 unsigned int nposition;
1690 AbortDifficultCommand;
1695 if ( str != NULL && ! strcmp( str, "mate" ) )
1698 str = strtok_r( NULL, str_delimiters, lasts );
1704 l = strtol( str, &ptr, 0 );
1705 if ( ptr == str || l == LONG_MAX || l < 1 )
1707 str_error = str_bad_cmdline;
1710 nposition = (unsigned int)l;
1712 else { nposition = UINT_MAX; }
1715 iret = record_open( &record_problems, "problem.csa", mode_read, NULL, NULL );
1716 if ( iret < 0 ) { return iret; }
1719 iret = is_mate ? solve_mate_problems( ptree, nposition )
1720 : solve_problems( ptree, nposition );
1722 iret = solve_problems( ptree, nposition );
1727 record_close( &record_problems );
1731 iret = record_close( &record_problems );
1732 if ( iret < 0 ) { return iret; }
1734 return get_elapsed( &time_turn_start );
1738 static int CONV cmd_quit( void )
1740 game_status |= flag_quit;
1745 static int CONV cmd_suspend( void )
1747 if ( game_status & ( flag_pondering | flag_puzzling ) )
1749 game_status |= flag_quit_ponder;
1753 game_status |= flag_suspend;
1758 static int CONV cmd_time( char **lasts )
1760 const char *str = strtok_r( NULL, str_delimiters, lasts );
1765 str_error = str_bad_cmdline;
1768 else if ( ! strcmp( str, "response" ) )
1771 str = strtok_r( NULL, str_delimiters, lasts );
1774 str_error = str_bad_cmdline;
1777 l = strtol( str, &ptr, 0 );
1778 if ( ptr == str || l == LONG_MAX || l < 0 || l > 1000 )
1780 str_error = str_bad_cmdline;
1783 time_response = (unsigned int)l;
1786 else if ( ! strcmp( str, "remain" ) )
1790 str = strtok_r( NULL, str_delimiters, lasts );
1793 str_error = str_bad_cmdline;
1796 l1 = strtol( str, &ptr, 0 );
1797 if ( ptr == str || l1 == LONG_MAX || l1 < 0 )
1799 str_error = str_bad_cmdline;
1803 str = strtok_r( NULL, str_delimiters, lasts );
1806 str_error = str_bad_cmdline;
1809 l2 = strtol( str, &ptr, 0 );
1810 if ( ptr == str || l2 == LONG_MAX || l2 < 0 )
1812 str_error = str_bad_cmdline;
1816 if ( sec_limit_up == UINT_MAX )
1818 str_error = str_bad_cmdline;
1822 return reset_time( (unsigned int)l1, (unsigned int)l2 );
1825 str_error = str_bad_cmdline;
1830 #if !defined(MINIMUM)
1831 /* learn (ini|no-ini) steps games iterations tlp1 tlp2 */
1832 static int CONV cmd_learn( tree_t * restrict ptree, char **lasts )
1834 const char *str1 = strtok_r( NULL, str_delimiters, lasts );
1835 const char *str2 = strtok_r( NULL, str_delimiters, lasts );
1836 const char *str3 = strtok_r( NULL, str_delimiters, lasts );
1837 const char *str4 = strtok_r( NULL, str_delimiters, lasts );
1839 const char *str5 = strtok_r( NULL, str_delimiters, lasts );
1840 const char *str6 = strtok_r( NULL, str_delimiters, lasts );
1844 unsigned int max_games;
1845 int is_ini, nsteps, max_iterations, nworker1, nworker2, iret;
1849 str_error = str_bad_cmdline;
1852 if ( ! strcmp( str1, "ini" ) ) { is_ini = 1; }
1853 else if ( ! strcmp( str1, "no-ini" ) ) { is_ini = 0; }
1855 str_error = str_bad_cmdline;
1859 max_games = UINT_MAX;
1860 max_iterations = INT_MAX;
1861 nworker1 = nworker2 = nsteps = 1;
1865 l = strtol( str2, &ptr, 0 );
1866 if ( ptr == str2 || l == LONG_MAX || l < 1 )
1868 str_error = str_bad_cmdline;
1876 l = strtol( str3, &ptr, 0 );
1877 if ( ptr == str3 || l == LONG_MAX || l == LONG_MIN )
1879 str_error = str_bad_cmdline;
1882 if ( l > 0 ) { max_games = (unsigned int)l; }
1887 l = strtol( str4, &ptr, 0 );
1888 if ( ptr == str4 || l == LONG_MAX || l == LONG_MIN )
1890 str_error = str_bad_cmdline;
1893 if ( l > 0 ) { max_iterations = (int)l; }
1899 l = strtol( str5, &ptr, 0 );
1900 if ( ptr == str5 || l > TLP_MAX_THREADS || l < 1 )
1902 str_error = str_bad_cmdline;
1910 l = strtol( str6, &ptr, 0 );
1911 if ( ptr == str6 || l > TLP_MAX_THREADS || l < 1 )
1913 str_error = str_bad_cmdline;
1920 AbortDifficultCommand;
1922 log2_ntrans_table = 12;
1924 memory_free( (void *)ptrans_table_orig );
1926 iret = ini_trans_table();
1927 if ( iret < 0 ) { return iret; }
1929 iret = learn( ptree, is_ini, nsteps, max_games, max_iterations,
1930 nworker1, nworker2 );
1931 if ( iret < 0 ) { return -1; }
1933 iret = ini_game( ptree, &min_posi_no_handicap, flag_history, NULL, NULL );
1934 if ( iret < 0 ) { return -1; }
1936 iret = get_elapsed( &time_turn_start );
1937 if ( iret < 0 ) { return iret; }
1941 #endif /* MINIMUM */
1945 static int CONV cmd_mpv( tree_t * restrict ptree, char **lasts )
1947 const char *str = strtok_r( NULL, str_delimiters, lasts );
1953 str_error = str_bad_cmdline;
1956 else if ( ! strcmp( str, "num" ) )
1958 str = strtok_r( NULL, str_delimiters, lasts );
1961 str_error = str_bad_cmdline;
1964 l = strtol( str, &ptr, 0 );
1965 if ( ptr == str || l == LONG_MAX || l < 1 || l > MPV_MAX_PV )
1967 str_error = str_bad_cmdline;
1971 AbortDifficultCommand;
1975 if ( analyze_mode ) return do_analyze ( ptree ); // [HGM] analyze: analysis should continue changing num
1979 else if ( ! strcmp( str, "width" ) )
1981 str = strtok_r( NULL, str_delimiters, lasts );
1984 str_error = str_bad_cmdline;
1987 l = strtol( str, &ptr, 0 );
1988 if ( ptr == str || l == LONG_MAX || l < MT_CAP_PAWN )
1990 str_error = str_bad_cmdline;
1994 AbortDifficultCommand;
1998 if ( analyze_mode ) return do_analyze ( ptree ); // [HGM] analyze: analysis should continue after changing width
2003 str_error = str_bad_cmdline;
2010 static int CONV cmd_dfpn( tree_t * restrict ptree, char **lasts )
2012 const char *str = strtok_r( NULL, str_delimiters, lasts );
2016 str_error = str_bad_cmdline;
2019 else if ( ! strcmp( str, "hash" ) )
2024 str = strtok_r( NULL, str_delimiters, lasts );
2027 str_error = str_bad_cmdline;
2030 l = strtol( str, &ptr, 0 );
2031 if ( ptr == str || l == LONG_MAX || l < 1 )
2033 str_error = str_bad_cmdline;
2037 AbortDifficultCommand;
2039 dfpn_hash_log2 = (unsigned int)l;
2040 return dfpn_ini_hash();
2042 else if ( ! strcmp( str, "go" ) )
2044 AbortDifficultCommand;
2046 return dfpn( ptree, root_turn, 1 );
2048 else if ( ! strcmp( str, "connect" ) )
2056 str = strtok_r( NULL, str_delimiters, lasts );
2057 if ( ! str || ! strcmp( str, "." ) ) { str = "127.0.0.1"; }
2058 strncpy( str_addr, str, 255 );
2059 str_addr[255] = '\0';
2061 str = strtok_r( NULL, str_delimiters, lasts );
2062 if ( ! str || ! strcmp( str, "." ) ) { str = "4083"; }
2063 l = strtol( str, &ptr, 0 );
2064 if ( ptr == str || l == LONG_MAX || l < 0 || l > USHRT_MAX )
2066 str_error = str_bad_cmdline;
2071 str = strtok_r( NULL, str_delimiters, lasts );
2072 if ( ! str || ! strcmp( str, "." ) ) { str = "bonanza1"; }
2073 strncpy( str_id, str, 255 );
2076 AbortDifficultCommand;
2078 dfpn_sckt = sckt_connect( str_addr, port );
2079 if ( dfpn_sckt == SCKT_NULL ) { return -2; }
2081 str_buffer_cmdline[0] = '\0';
2082 DFPNOut( "Worker: %s\n", str_id );
2087 str_error = str_bad_cmdline;
2094 static int CONV cmd_thread( char **lasts )
2096 const char *str = strtok_r( NULL, str_delimiters, lasts );
2100 str_error = str_bad_cmdline;
2103 else if ( ! strcmp( str, "num" ) )
2108 str = strtok_r( NULL, str_delimiters, lasts );
2111 str_error = str_bad_cmdline;
2114 l = strtol( str, &ptr, 0 );
2115 if ( ptr == str || l == LONG_MAX || l < 1 || l > TLP_MAX_THREADS )
2117 str_error = str_bad_cmdline;
2125 if ( game_status & ( flag_thinking | flag_pondering | flag_puzzling ) )
2132 str_error = str_bad_cmdline;
2138 #if defined(DFPN_CLIENT)
2139 static int CONV cmd_dfpn_client( tree_t * restrict ptree, char **lasts )
2145 AbortDifficultCommand;
2147 str = strtok_r( NULL, str_delimiters, lasts );
2148 if ( ! str || ! strcmp( str, "." ) ) { str = "127.0.0.1"; }
2149 strncpy( dfpn_client_str_addr, str, 255 );
2150 dfpn_client_str_addr[255] = '\0';
2152 str = strtok_r( NULL, str_delimiters, lasts );
2153 if ( ! str || ! strcmp( str, "." ) ) { str = "4083"; }
2154 dfpn_client_port = strtol( str, &ptr, 0 );
2155 if ( ptr == str || dfpn_client_port == LONG_MAX || dfpn_client_port < 0
2156 || dfpn_client_port > USHRT_MAX )
2158 str_error = str_bad_cmdline;
2162 Out( "DFPN Server: %s %d\n", dfpn_client_str_addr, dfpn_client_port );
2164 iret = ini_game( ptree, &min_posi_no_handicap, flag_history, NULL, NULL );
2165 if ( iret < 0 ) { return iret; }
2167 if ( dfpn_client_sckt == SCKT_NULL )
2169 str_error = "Check network status.";
2173 return get_elapsed( &time_turn_start );
2178 #if defined(CSA_LAN)
2179 static int CONV cmd_connect( tree_t * restrict ptree, char **lasts )
2185 str = strtok_r( NULL, str_delimiters, lasts );
2186 if ( ! str || ! strcmp( str, "." ) ) { str = "gserver.computer-shogi.org"; }
2187 strncpy( client_str_addr, str, 255 );
2188 client_str_addr[255] = '\0';
2190 str = strtok_r( NULL, str_delimiters, lasts );
2191 if ( ! str || ! strcmp( str, "." ) ) { str = "4081"; }
2192 client_port = strtol( str, &ptr, 0 );
2193 if ( ptr == str || client_port == LONG_MAX || client_port < 0
2194 || client_port > USHRT_MAX )
2196 str_error = str_bad_cmdline;
2200 str = strtok_r( NULL, str_delimiters, lasts );
2201 if ( ! str || ! strcmp( str, "." ) ) { str = "bonanza_test"; }
2202 strncpy( client_str_id, str, 255 );
2203 client_str_id[255] = '\0';
2205 str = strtok_r( NULL, " \t", lasts );
2206 if ( ! str || ! strcmp( str, "." ) ) { str = "bonanza_test"; }
2207 strncpy( client_str_pwd, str, 255 );
2208 client_str_pwd[255] = '\0';
2210 str = strtok_r( NULL, str_delimiters, lasts );
2211 if ( ! str || ! strcmp( str, "." ) ) { client_max_game = INT_MAX; }
2213 max_games = strtol( str, &ptr, 0 );
2214 if ( ptr == str || max_games == LONG_MAX || max_games < 1 )
2216 str_error = str_bad_cmdline;
2219 client_max_game = max_games;
2222 AbortDifficultCommand;
2226 return client_next_game( ptree, client_str_addr, (int)client_port );
2230 static int CONV cmd_sendpv( char **lasts )
2232 const char *str = strtok_r( NULL, str_delimiters, lasts );
2236 str_error = str_bad_cmdline;
2240 if ( ! strcmp( str, str_off ) ) { game_status &= ~flag_sendpv; }
2241 else if ( ! strcmp( str, str_on ) ) { game_status |= flag_sendpv; }
2243 str_error = str_bad_cmdline;
2252 #if defined(MNJ_LAN)
2253 /* mnj sd seed addr port name factor stable_depth */
2254 static int CONV cmd_mnj( char **lasts )
2256 char client_str_addr[256];
2257 char client_str_id[256];
2262 int client_port, sd;
2265 str = strtok_r( NULL, str_delimiters, lasts );
2268 str_error = str_bad_cmdline;
2271 l = strtol( str, &ptr, 0 );
2272 if ( ptr == str || l == LONG_MAX || l < 0 )
2274 str_error = str_bad_cmdline;
2280 str = strtok_r( NULL, str_delimiters, lasts );
2283 str_error = str_bad_cmdline;
2286 l = strtol( str, &ptr, 0 );
2287 if ( ptr == str || l == LONG_MAX || l < 0 )
2289 str_error = str_bad_cmdline;
2292 seed = (unsigned int)l;
2295 str = strtok_r( NULL, str_delimiters, lasts );
2296 if ( ! str || ! strcmp( str, "." ) ) { str = "localhost"; }
2297 strncpy( client_str_addr, str, 255 );
2298 client_str_addr[255] = '\0';
2301 str = strtok_r( NULL, str_delimiters, lasts );
2302 if ( ! str || ! strcmp( str, "." ) ) { str = "4082"; }
2303 l = strtol( str, &ptr, 0 );
2304 if ( ptr == str || l == LONG_MAX || l < 0 || l > USHRT_MAX )
2306 str_error = str_bad_cmdline;
2309 client_port = (int)l;
2312 str = strtok_r( NULL, str_delimiters, lasts );
2313 if ( ! str || ! strcmp( str, "." ) ) { str = "bonanza1"; }
2314 strncpy( client_str_id, str, 255 );
2315 client_str_id[255] = '\0';
2317 str = strtok_r( NULL, str_delimiters, lasts );
2318 if ( ! str || ! strcmp( str, "." ) ) { str = "1.0"; }
2319 factor = strtod( str, &ptr );
2320 if ( ptr == str || factor < 0.0 )
2322 str_error = str_bad_cmdline;
2326 str = strtok_r( NULL, str_delimiters, lasts );
2327 if ( ! str || ! strcmp( str, "." ) ) { l = -1; }
2329 l = strtol( str, &ptr, 0 );
2330 if ( ptr == str || l == LONG_MAX )
2332 str_error = str_bad_cmdline;
2336 if ( l <= 0 ) { mnj_depth_stable = INT_MAX; }
2337 else { mnj_depth_stable = (int)l; }
2339 AbortDifficultCommand;
2341 resign_threshold = 65535;
2342 game_status |= ( flag_noponder | flag_noprompt );
2343 if ( mnj_reset_tbl( sd, seed ) < 0 ) { return -1; }
2345 sckt_mnj = sckt_connect( client_str_addr, (int)client_port );
2346 if ( sckt_mnj == SCKT_NULL ) { return -2; }
2348 str_buffer_cmdline[0] = '\0';
2350 Out( "Sending my name %s", client_str_id );
2351 MnjOut( "%s %g final%s\n", client_str_id, factor,
2352 ( mnj_depth_stable == INT_MAX ) ? "" : " stable" );
2354 return cmd_suspend();