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;
139 int root_pos[nsquare];
142 xboard_to_CSA( tree_t * restrict ptree, char *in, char *out, int status )
144 char fromX=in[0], fromY=in[1], toX=in[2], toY=in[3], promo=in[4];
145 int piece=0, bonanza_move=0;
146 if(fromY == '@') { // drop (contains all info needed to convert it)
147 if(fromX >= 'a') fromX += 'A' - 'a';
148 switch(fromX) { // encode piece
149 case 'P': piece = pawn; break;
150 case 'L': piece = lance; break;
151 case 'N': piece = knight; break;
152 case 'S': piece = silver; break;
153 case 'G': piece = gold; break;
154 case 'B': piece = bishop; break;
155 case 'R': piece = rook; break;
157 bonanza_move = Drop2Move(piece) | To2Move( ('9' - toY)*9 + (toX - 'a') );
158 sprintf(out, "00%c%c%s", 'a'+'9'-toX, '1'+'9'-toY, astr_table_piece[piece]);
159 } else { // board move (need to figure out moved piece)
160 int from = ('9' - fromY)*9 + (fromX - 'a');
161 int flag = (promo == '+' ? FLAG_PROMO : 0);
162 piece = abs( BOARD[from] ); // this only works when not searching!
163 printf("# piece from board: %d\n", piece);fflush(stdout);
164 if( status & flag_thinking ) {
165 int i, to = ('9' - toY)*9 + (toX - 'a'), l = (status == (flag_pondering | flag_thinking));
166 piece = 0; // kludge to force illegal CSA move
167 for( i = 0; l ? all_moves[i] : i < root_nmove; i++ ) { // determine the piece from the move list
168 int move = (l ? all_moves[i] : root_move_list[i].move);
169 if( I2To(move) != to ) continue;
170 if( I2From(move) != from ) continue;
171 if( (move & FLAG_PROMO) != flag ) continue;
172 piece = I2PieceMove( move ); // we found the move; take the piece from it
176 } else if( status & ( flag_pondering | flag_puzzling ) ) piece = abs( root_pos[from] ); // we have valid copy!
177 printf("# piece corrected to %d\n", piece);fflush(stdout);
178 if( promo ) piece += promote;
179 sprintf(out, "%c%c%c%c%s", 'a'+'9'-fromX, '1'+'9'-fromY, 'a'+'9'-toX, '1'+'9'-toY, astr_table_piece[piece]);
185 update_exclude_list( tree_t * restrict ptree, int add, char *move_str )
186 { // [HGM] exclude: manage list of excluded moves
189 if( moves_ignore[0] == MOVE_NA ) { // nothing is excluded yet; make a copy of root move list;
190 for( i = 0; i < root_nmove; i++) all_moves[i] = root_move_list[i].move;
193 if ( ! strcmp(move_str, "all") ) { // all moves
194 if( add ) { // copy entire list of legal moves
195 for( i = 0; i < root_nmove; i++ ) moves_ignore[i] = all_moves[i];
196 } else moves_ignore[0] = MOVE_NA; // clear list
197 } else { // single move
198 int move = xboard_to_CSA( ptree, move_str, dummy, flag_pondering | flag_thinking); // kludge with impossible flag
199 for( i = 0; moves_ignore[i] != MOVE_NA; i++ ) if( move == moves_ignore[i] ) break;
200 if( add ) { // we must add the move
201 if( moves_ignore[i] != MOVE_NA ) return; // but it was already in list
202 if( i >= MAX_LEGAL_MOVES - 2 ) return; // overflow
203 moves_ignore[i] = move; moves_ignore[i+1] = MOVE_NA; // append move
204 } else { // we must delete the move
205 if( moves_ignore[i] == MOVE_NA ) return; // but it was not there
206 while( (moves_ignore[i] = moves_ignore[i+1]) ) i++; // squeeze it out
213 { // set white and black times from own and opponent time.
215 if(movesPerSession <= 0) moves = 35; else {
217 while(moves <= 0) moves += movesPerSession;
219 time_limit = (myTime-inc-30)/(moves+2) + inc;
220 time_max_limit = 3*time_limit;
221 if(time_max_limit > myTime - 30)time_max_limit = myTime - 30; // keep 0.3 sec margin, as Bonanza reads the clock infrequently
222 time_limit *= 10; // msec
223 time_max_limit *= 10;
224 Out("# moves=%d, time=%d inc=%d t=%d, max=%d\n", moves, myTime, inc, time_limit, time_max_limit);
228 proce_xboard(char *line, const char *command, tree_t * restrict ptree)
229 { // [HGM] added to make Bonanza.exe a native WinBoard engine
231 static char forceMode = 0;
232 sscanf(line + strlen(command) + 1, "%d", &value);
233 Out("# command = '%s'\n", line);
237 Out("feature option=\"MultiPV -spin 1 1 100\"\n");
238 Out("feature option=\"centi-Pawn margin -spin 200 0 25000\"\n");
240 Out("feature variants=\"shogi\" usermove=1 myname=\"Bonanza " BNZ_VER
241 "\" memory=1 smp=1 debug=1 colors=0 setboard=1 ping=1 sigint=0 exclude=1 done=1\n");
243 IF("new") { forceMode = plyNr = 0; SetTimes(); return 0; }
244 IF("easy") { strcpy(line, "ponder off"); return 0; }
245 IF("hard") { strcpy(line, "ponder on"); return 0; }
248 IF("time") { sscanf(line+5, "%d", &myTime); }
249 IF("otim") { sscanf(line+5, "%d", &hisTime); }
250 IF("force") { forceMode = 1; }
251 IF("go") { forceMode = 0; SetTimes(); plyNr++; strcpy(line, "move"); return 0; }
253 IF("cores") { sprintf(line, "tlp num %d", value); return 0; }
254 IF("sd") { sprintf(line, "limit depth %d", value); return 0; }
256 IF("quit") { return 0; }
257 IF("analyze") { return 0; }
258 IF("exit") { return 0; }
259 IF("variant") { /* ignore, since it must be Shogi */; }
262 if(sscanf(line+7, "MultiPV=%d", &value) == 1) { sprintf(line, "mpv num %d", value); return 0; }
263 if(sscanf(line+7, "centi-Pawn margin=%d", &value) == 1) { sprintf(line, "mpv width %d", value); return 0; }
265 IF("level") { int min, sec; float fsec=0.;
266 if(sscanf(line+6, "%d %d:%d %f", &movesPerSession, &min, &sec, &fsec) != 4)
267 sscanf(line+6, "%d %d %f", &movesPerSession, &min, &fsec);
268 min = 60*min + sec; myTime = hisTime = 100*min; inc = 100 * fsec;
270 IF("usermove") { char buf[20];
271 xboard_to_CSA( ptree, line+9, buf, game_status );
272 if(forceMode || analyze_mode) strcpy(line, "move "), line += 5; else plyNr++, SetTimes();
277 IF("undo") { return 0; }
279 IF("ping") { Out("pong %d\n", value); }
280 IF("exclude") { update_exclude_list( ptree, 1, line+8 ); strcpy(line, "analyze"); return 0; }
281 IF("include") { update_exclude_list( ptree, 0, line+8 ); strcpy(line, "analyze"); return 0; }
286 static int CONV proce_cui( tree_t * restrict ptree )
291 token = strtok_r( str_cmdline, str_delimiters, &last );
293 if ( token == NULL || *token == '#' ) { return 1; }
297 if( proce_xboard(str_cmdline, token, ptree) ) return 1; // command already processed
298 Out("# translated command '%s'\n", str_cmdline); // command translated for processing by Bonanza
299 token = strtok_r( str_cmdline, str_delimiters, &last ); // redo parsing
301 if ( ! strcmp( token, "xboard" ) ) { xboard_mode = 1; game_status |= flag_noprompt; return 1; }
304 if ( ! strcmp( token, "analyze" ) ) { return cmd_analyze( ptree ); } // [HGM] analyze
305 if ( ! strcmp( token, "exit" ) ) { return cmd_exit(); } // [HGM] analyze
306 if ( ! strcmp( token, "move" ) ) { return cmd_move( ptree, &last ); }
307 if ( ! strcmp( token, "undo" ) ) { return cmd_undo( ptree ); } // [HGM] undo
309 if ( ! strcmp( token, "mpv" ) ) { return cmd_mpv( ptree, &last ); }
311 analyze_mode = 0; // [HGM] analyze: all other commands terminate analysis
312 if ( is_move( token ) ) { return cmd_usrmove( ptree, token, &last ); }
313 if ( ! strcmp( token, "s" ) ) { return cmd_move_now(); }
314 if ( ! strcmp( token, "beep" ) ) { return cmd_beep( &last); }
315 if ( ! strcmp( token, "book" ) ) { return CmdBook( ptree, &last ); }
316 if ( ! strcmp( token, "display" ) ) { return cmd_display( ptree, &last ); }
317 if ( ! strcmp( token, "hash" ) ) { return cmd_hash( &last ); }
318 if ( ! strcmp( token, "limit" ) ) { return cmd_limit( &last ); }
319 if ( ! strcmp( token, "new" ) ) { return cmd_new( ptree, &last ); }
320 if ( ! strcmp( token, "outmove" ) ) { return cmd_outmove( ptree ); }
321 if ( ! strcmp( token, "peek" ) ) { return cmd_peek( &last ); }
322 if ( ! strcmp( token, "stdout" ) ) { return cmd_stdout( &last ); }
323 if ( ! strcmp( token, "ping" ) ) { return cmd_ping(); }
324 if ( ! strcmp( token, "ponder" ) ) { return cmd_ponder( &last ); }
325 if ( ! strcmp( token, "problem" ) ) { return cmd_problem( ptree, &last ); }
326 if ( ! strcmp( token, "quit" ) ) { return cmd_quit(); }
327 if ( ! strcmp( token, "read" ) ) { return cmd_read( ptree, &last ); }
328 if ( ! strcmp( token, "resign" ) ) { return cmd_resign( ptree, &last ); }
329 if ( ! strcmp( token, "suspend" ) ) { return cmd_suspend(); }
330 if ( ! strcmp( token, "time" ) ) { return cmd_time( &last ); }
331 if ( ! strcmp( token, "newlog" ) ) { return cmd_newlog( &last ); }
333 if ( ! strcmp( token, "connect" ) ) { return cmd_connect( ptree, &last ); }
334 if ( ! strcmp( token, "sendpv" ) ) { return cmd_sendpv( &last ); }
337 if ( ! strcmp( token, "mnj" ) ) { return cmd_mnj( &last ); }
340 if ( ! strcmp( token, "dfpn" ) ) { return cmd_dfpn( ptree, &last ); }
342 #if defined(DFPN_CLIENT)
343 if ( ! strcmp( token, "dfpn_client")) { return cmd_dfpn_client( ptree,
347 if ( ! strcmp( token, "tlp" ) ) { return cmd_thread( &last ); }
349 #if ! defined(NO_STDOUT)
350 if ( ! strcmp( token, "stress" ) ) { return cmd_stress( &last ); }
352 #if ! defined(MINIMUM)
353 if ( ! strcmp( token, "learn" ) ) { return cmd_learn( ptree, &last ); }
356 str_error = str_bad_cmdline;
362 static int CONV proce_csalan( tree_t * restrict ptree )
367 token = strtok_r( str_cmdline, str_delimiters, &last );
369 if ( token == NULL ) { return 1; }
370 if ( *token == ach_turn[client_turn] && is_move( token+1 ) )
375 token = strtok_r( NULL, str_delimiters, &last );
376 if ( token == NULL || *token != 'T' )
378 str_error = str_bad_cmdline;
382 l = strtol( token+1, &ptr, 0 );
383 if ( token+1 == ptr || l == LONG_MAX || l < 1 )
385 str_error = str_bad_cmdline;
389 adjust_time( (unsigned int)l, client_turn );
390 Out( " elapsed: b%u, w%u\n", sec_b_total, sec_w_total );
393 if ( *token == ach_turn[Flip(client_turn)] && is_move( token+1 ) )
395 return cmd_usrmove( ptree, token+1, &last );
397 if ( ! strcmp( token, str_resign ) ) { return cmd_resign( ptree, &last ); }
398 if ( ! strcmp( token, "#WIN" )
399 || ! strcmp( token, "#LOSE" )
400 || ! strcmp( token, "#DRAW" )
401 || ! strcmp( token, "#CHUDAN" ) )
403 if ( game_status & ( flag_thinking | flag_pondering | flag_puzzling ) )
405 game_status |= flag_suspend;
409 if ( sckt_out( sckt_csa, "LOGOUT\n" ) < 0 ) { return -1; }
410 if ( sckt_recv_all( sckt_csa ) < 0 ) { return -1; }
414 if ( client_ngame == client_max_game ) { return cmd_quit(); }
416 return client_next_game( ptree, client_str_addr, (int)client_port );
425 static int CONV proce_mnj( tree_t * restrict ptree )
431 token = strtok_r( str_cmdline, str_delimiters, &last );
432 if ( token == NULL ) { return 1; }
434 if ( ! strcmp( token, "new" ) )
436 iret = cmd_suspend();
437 if ( iret != 1 ) { return iret; }
440 iret = cmd_new( ptree, &last );
441 if ( iret < 0 ) { return iret; }
443 moves_ignore[0] = MOVE_NA;
444 return analyze( ptree );
446 if ( ! strcmp( token, "ignore" ) ) { return cmd_mnjignore( ptree, &last ); }
447 if ( ! strcmp( token, "idle" ) ) { return cmd_suspend(); }
448 if ( ! strcmp( token, "alter" ) ) { return cmd_mnjmove( ptree, &last, 1 ); }
449 if ( ! strcmp( token, "retract" ) )
453 const char *str = strtok_r( NULL, str_delimiters, &last );
456 str_error = str_bad_cmdline;
459 l = strtol( str, &ptr, 0 );
460 if ( ptr == str || (long)NUM_UNMAKE < l )
462 str_error = str_bad_cmdline;
466 return cmd_mnjmove( ptree, &last, (int)l );
468 if ( ! strcmp( token, "move" ) ) { return cmd_mnjmove( ptree, &last, 0 ); }
470 str_error = str_bad_cmdline;
476 cmd_mnjignore( tree_t *restrict ptree, char **lasts )
485 token = strtok_r( NULL, str_delimiters, lasts );
488 str_error = str_bad_cmdline;
491 lid = strtol( token, &ptr, 0 );
492 if ( ptr == token || lid == LONG_MAX || lid < 1 )
494 str_error = str_bad_cmdline;
498 AbortDifficultCommand;
500 for ( i = 0; ; i += 1 )
502 token = strtok_r( NULL, str_delimiters, lasts );
503 if ( token == NULL ) { break; }
505 if ( interpret_CSA_move( ptree, &move, token ) < 0 ) { return -1; }
507 moves_ignore[i] = move;
511 str_error = str_bad_cmdline;
514 mnj_posi_id = (int)lid;
515 moves_ignore[i] = MOVE_NA;
517 return analyze( ptree );
522 cmd_mnjmove( tree_t * restrict ptree, char **lasts, int num_alter )
524 const char *str1 = strtok_r( NULL, str_delimiters, lasts );
525 const char *str2 = strtok_r( NULL, str_delimiters, lasts );
531 if ( sckt_mnj == SCKT_NULL || str1 == NULL || str2 == NULL )
533 str_error = str_bad_cmdline;
537 lid = strtol( str2, &ptr, 0 );
538 if ( ptr == str2 || lid == LONG_MAX || lid < 1 )
540 str_error = str_bad_cmdline;
544 AbortDifficultCommand;
548 iret = unmake_move_root( ptree );
549 if ( iret < 0 ) { return iret; }
554 iret = interpret_CSA_move( ptree, &move, str1 );
555 if ( iret < 0 ) { return iret; }
557 iret = get_elapsed( &time_turn_start );
558 if ( iret < 0 ) { return iret; }
560 mnj_posi_id = (int)lid;
562 iret = make_move_root( ptree, move, ( flag_time | flag_rep
563 | flag_detect_hang ) );
564 if ( iret < 0 ) { return iret; }
566 # if ! defined(NO_STDOUT)
567 iret = out_board( ptree, stdout, 0, 0 );
568 if ( iret < 0 ) { return iret; }
571 moves_ignore[0] = MOVE_NA;
572 return analyze( ptree );
578 do_analyze( tree_t * restrict ptree )
579 { // [HGM] analyze: do a ponder search on the current position
581 if ( get_elapsed( &time_start ) < 0 ) { return -1; }
582 time_limit = time_max_limit = 1e9; // kludge: use huge time to mimic infinity
584 if(xboard_mode) Out("1 0 0 0 New Search\n"); // make sure lower depth is emitted, so XBoard undestand new search started
586 game_status |= flag_pondering;
587 iret = iterate( ptree );
588 game_status &= ~flag_pondering;
594 cmd_undo( tree_t * restrict ptree )
595 { // [HGM] undo: restart the game, and feed all moves except the last
596 int i, last = move_ptr;
597 char *p = start_data;
598 if( move_ptr <= 0 ) {
599 str_error = "undo past start of game ignored";
603 AbortDifficultCommand;
606 cmd_new( ptree, &p );
607 for(i=0; i<last; i++) {
608 make_move_root( ptree, move_list[i], 0);
611 moves_ignore[0] = MOVE_NA; // [HGM] exclude: exclude list cleared for new position
612 if ( analyze_mode ) return do_analyze ( ptree ); // [HGM] analyze: analysis should continue after undo
619 cmd_analyze( tree_t * restrict ptree )
620 { // [HGM] analyze: switch on analyze mode, and start analyzing (also used to force a restart)
621 AbortDifficultCommand;
624 return do_analyze( ptree );
630 { // [HGM] analyze: switch off analysis mode
631 if ( !analyze_mode ) {
632 str_error = "was not analyzing";
636 if ( game_status & flag_pondering ) { game_status |= flag_quit_ponder; return 2; }
638 moves_ignore[0] = MOVE_NA; // [HGM] exclude: exclude list cleared after analysis
645 static int CONV proce_usi( tree_t * restrict ptree )
651 token = strtok_r( str_cmdline, str_delimiters, &lasts );
652 if ( token == NULL ) { return 1; }
654 if ( ! strcmp( token, "usi" ) )
656 USIOut( "id name %s\n", str_myname );
657 USIOut( "id author Kunihito Hoki\n" );
662 if ( ! strcmp( token, "isready" ) )
664 USIOut( "readyok\n", str_myname );
668 if ( ! strcmp( token, "echo" ) )
670 USIOut( "%s\n", lasts );
674 if ( ! strcmp( token, "ignore_moves" ) )
676 return usi_ignore( ptree, &lasts );
679 if ( ! strcmp( token, "genmove_probability" ) )
681 if ( get_elapsed( &time_start ) < 0 ) { return -1; }
682 return usi_root_list( ptree );
685 if ( ! strcmp( token, "go" ) )
687 iret = usi_go( ptree, &lasts );
688 moves_ignore[0] = MOVE_NA;
692 if ( ! strcmp( token, "stop" ) ) { return cmd_move_now(); }
693 if ( ! strcmp( token, "position" ) ) { return usi_posi( ptree, &lasts ); }
694 if ( ! strcmp( token, "quit" ) ) { return cmd_quit(); }
696 str_error = str_bad_cmdline;
702 usi_ignore( tree_t * restrict ptree, char **lasts )
709 AbortDifficultCommand;
711 for ( i = 0; ; i += 1 )
713 token = strtok_r( NULL, str_delimiters, lasts );
714 if ( token == NULL ) { break; }
716 if ( usi2csa( ptree, token, str_buf ) < 0 ) { return -1; }
717 if ( interpret_CSA_move( ptree, &move, str_buf ) < 0 ) { return -1; }
719 moves_ignore[i] = move;
722 moves_ignore[i] = MOVE_NA;
729 usi_go( tree_t * restrict ptree, char **lasts )
736 AbortDifficultCommand;
738 if ( game_status & mask_game_end )
740 str_error = str_game_ended;
744 token = strtok_r( NULL, str_delimiters, lasts );
746 if ( ! strcmp( token, "book" ) )
748 AbortDifficultCommand;
749 if ( usi_book( ptree ) < 0 ) { return -1; }
755 if ( ! strcmp( token, "infinite" ) )
757 usi_byoyomi = UINT_MAX;
758 depth_limit = PLY_MAX;
759 node_limit = UINT64_MAX;
760 sec_limit_depth = UINT_MAX;
762 else if ( ! strcmp( token, "byoyomi" ) )
764 token = strtok_r( NULL, str_delimiters, lasts );
767 str_error = str_bad_cmdline;
771 l = strtol( token, &ptr, 0 );
772 if ( ptr == token || l > UINT_MAX || l < 1 )
774 str_error = str_bad_cmdline;
778 usi_byoyomi = (unsigned int)l;
779 depth_limit = PLY_MAX;
780 node_limit = UINT64_MAX;
781 sec_limit_depth = UINT_MAX;
784 str_error = str_bad_cmdline;
789 if ( get_elapsed( &time_turn_start ) < 0 ) { return -1; }
791 iret = com_turn_start( ptree, 0 );
793 if ( str_error == str_no_legal_move ) { USIOut( "bestmove resign\n" ); }
802 usi_posi( tree_t * restrict ptree, char **lasts )
808 AbortDifficultCommand;
810 moves_ignore[0] = MOVE_NA;
812 token = strtok_r( NULL, str_delimiters, lasts );
813 if ( strcmp( token, "startpos" ) )
815 str_error = str_bad_cmdline;
819 if ( ini_game( ptree, &min_posi_no_handicap,
820 flag_history, NULL, NULL ) < 0 ) { return -1; }
822 token = strtok_r( NULL, str_delimiters, lasts );
823 if ( token == NULL ) { return 1; }
825 if ( strcmp( token, "moves" ) )
827 str_error = str_bad_cmdline;
833 token = strtok_r( NULL, str_delimiters, lasts );
834 if ( token == NULL ) { break; }
836 if ( usi2csa( ptree, token, str_buf ) < 0 ) { return -1; }
837 if ( interpret_CSA_move( ptree, &move, str_buf ) < 0 ) { return -1; }
838 if ( make_move_root( ptree, move, ( flag_history | flag_time
840 | flag_detect_hang ) ) < 0 )
846 if ( get_elapsed( &time_turn_start ) < 0 ) { return -1; }
853 static int CONV cmd_move_now( void )
855 if ( game_status & flag_thinking ) { game_status |= flag_move_now; }
862 cmd_usrmove( tree_t * restrict ptree, const char *str_move, char **lasts )
870 if ( game_status & mask_game_end )
872 str_error = str_game_ended;
876 if ( game_status & flag_thinking )
878 str_error = str_busy_think;
882 str = strtok_r( NULL, str_delimiters, lasts );
883 if ( str == NULL ) { lelapsed = 0; }
887 str_error = str_bad_cmdline;
891 lelapsed = strtol( str, &ptr, 0 );
892 if ( ptr == str || lelapsed == LONG_MAX || lelapsed < 1 )
894 str_error = str_bad_cmdline;
899 if ( game_status & ( flag_pondering | flag_puzzling ) )
903 for ( i = 0; i < ponder_nmove; i++ )
905 if ( ! strcmp( str_move, str_CSA_move(ponder_move_list[i]) ) )
910 if ( i == ponder_nmove )
913 if ( sckt_csa != SCKT_NULL ) { AbortDifficultCommand; }
916 #if defined(CSASHOGI)
917 AbortDifficultCommand;
919 str_error = str_illegal_move;
924 if ( ( game_status & flag_puzzling )
925 || strcmp( str_move, str_CSA_move(ponder_move) ) )
927 ponder_move = MOVE_PONDER_FAILED;
928 game_status |= flag_quit_ponder;
932 iret = update_time( Flip(root_turn) );
933 if ( iret < 0 ) { return iret; }
936 adjust_time( (unsigned int)lelapsed, Flip(root_turn) );
939 out_CSA( ptree, &record_game, ponder_move );
941 game_status &= ~flag_pondering;
942 game_status |= flag_thinking;
946 set_search_limit_time( root_turn );
948 OutCsaShogi( "info ponder end\n" );
950 str = str_time_symple( time_turn_start - time_start );
951 Out( " %6s MOVE PREDICTION HIT\n"
952 " elapsed: b%u, w%u\n", str, sec_b_total, sec_w_total );
957 iret = interpret_CSA_move( ptree, &move, str_move );
958 if ( iret < 0 ) { return iret; }
959 move_evasion_pchk = 0;
960 iret = make_move_root( ptree, move, ( flag_rep | flag_history | flag_time
961 | flag_detect_hang ) );
966 if ( sckt_csa != SCKT_NULL )
968 if ( move_evasion_pchk )
970 str = str_CSA_move( move_evasion_pchk );
971 iret = sckt_out( sckt_csa, "%c%s\n",
972 ach_turn[Flip(root_turn)], str );
973 if ( iret < 0 ) { return iret; }
975 return cmd_suspend();
979 if ( move_evasion_pchk )
981 str = str_CSA_move( move_evasion_pchk );
982 #if defined(CSASHOGI)
983 OutCsaShogi( "move%s\n", str );
984 return cmd_suspend();
986 snprintf( str_message, SIZE_MESSAGE, "perpetual check (%c%s)",
987 ach_turn[Flip(root_turn)], str );
988 str_error = str_message;
996 if ( lelapsed ) { adjust_time( (unsigned int)lelapsed, Flip(root_turn) ); }
997 Out( " elapsed: b%u, w%u\n", sec_b_total, sec_w_total );
1000 if ( sckt_csa != SCKT_NULL && ( game_status & flag_mated ) )
1002 iret = sckt_out( sckt_csa, "%%TORYO\n" );
1003 if ( iret < 0 ) { return iret; }
1007 if ( ! ( game_status & mask_game_end ) )
1009 iret = com_turn_start( ptree, 0 );
1010 if ( iret < 0 ) { return iret; }
1017 static int CONV cmd_beep( char **lasts )
1019 const char *str = strtok_r( NULL, str_delimiters, lasts );
1022 str_error = str_bad_cmdline;
1026 if ( ! strcmp( str, str_on ) ) { game_status &= ~flag_nobeep; }
1027 else if ( ! strcmp( str, str_off ) ) { game_status |= flag_nobeep; }
1029 str_error = str_bad_cmdline;
1037 static int CONV cmd_peek( char **lasts )
1039 const char *str = strtok_r( NULL, str_delimiters, lasts );
1043 str_error = str_bad_cmdline;
1047 if ( ! strcmp( str, str_on ) ) { game_status &= ~flag_nopeek; }
1048 else if ( ! strcmp( str, str_off ) ) { game_status |= flag_nopeek; }
1050 str_error = str_bad_cmdline;
1058 static int CONV cmd_stdout( char **lasts )
1060 const char *str = strtok_r( NULL, str_delimiters, lasts );
1064 str_error = str_bad_cmdline;
1068 if ( ! strcmp( str, str_on ) ) { game_status &= ~flag_nostdout; }
1069 else if ( ! strcmp( str, str_off ) ) { game_status |= flag_nostdout; }
1071 str_error = str_bad_cmdline;
1079 static int CONV cmd_newlog( char **lasts )
1081 const char *str = strtok_r( NULL, str_delimiters, lasts );
1085 str_error = str_bad_cmdline;
1089 if ( ! strcmp( str, str_on ) ) { game_status &= ~flag_nonewlog; }
1090 else if ( ! strcmp( str, str_off ) ) { game_status |= flag_nonewlog; }
1092 str_error = str_bad_cmdline;
1100 static int CONV cmd_ponder( char **lasts )
1102 const char *str = strtok_r( NULL, str_delimiters, lasts );
1106 str_error = str_bad_cmdline;
1110 if ( ! strcmp( str, str_on ) ) { game_status &= ~flag_noponder; }
1111 else if ( ! strcmp( str, str_off ) )
1113 if ( game_status & ( flag_pondering | flag_puzzling ) )
1115 game_status |= flag_quit_ponder;
1117 game_status |= flag_noponder;
1120 str_error = str_bad_cmdline;
1128 #if ! defined(NO_STDOUT)
1129 static int CONV cmd_stress( char **lasts )
1131 const char *str = strtok_r( NULL, str_delimiters, lasts );
1135 str_error = str_bad_cmdline;
1139 if ( ! strcmp( str, str_on ) ) { game_status &= ~flag_nostress; }
1140 else if ( ! strcmp( str, str_off ) ) { game_status |= flag_nostress; }
1142 str_error = str_bad_cmdline;
1152 #if defined(MINIMUM)
1153 cmd_book( char **lasts )
1155 cmd_book( tree_t * restrict ptree, char **lasts )
1158 const char *str = strtok_r( NULL, str_delimiters, lasts );
1163 str_error = str_bad_cmdline;
1166 if ( ! strcmp( str, str_on ) ) { iret = book_on(); }
1167 else if ( ! strcmp( str, str_off ) ) { iret = book_off(); }
1168 else if ( ! strcmp( str, "narrow" ) ) { game_status |= flag_narrow_book; }
1169 else if ( ! strcmp( str, "wide" ) ) { game_status &= ~flag_narrow_book; }
1170 #if ! defined(MINIMUM)
1171 else if ( ! strcmp( str, "create" ) )
1173 AbortDifficultCommand;
1175 iret = book_create( ptree );
1176 if ( iret < 0 ) { return iret; }
1178 iret = ini_game( ptree, &min_posi_no_handicap, flag_history,
1180 if ( iret < 0 ) { return iret; }
1182 iret = get_elapsed( &time_turn_start );
1186 str_error = str_bad_cmdline;
1194 static int CONV cmd_display( tree_t * restrict ptree, char **lasts )
1196 const char *str = strtok_r( NULL, str_delimiters, lasts );
1203 l = strtol( str, &ptr, 0 );
1206 str_error = str_bad_cmdline;
1209 if ( l == 1 ) { game_status &= ~flag_reverse; }
1210 else if ( l == 2 ) { game_status |= flag_reverse; }
1212 str_error = str_bad_cmdline;
1218 iret = out_board( ptree, stdout, 0, 0 );
1219 if ( iret < 0 ) { return iret; }
1220 #if ! defined(NO_LOGGING)
1221 iret = out_board( ptree, pf_log, 0, 0 );
1222 if ( iret < 0 ) { return iret; }
1230 static int CONV cmd_ping( void )
1232 OutCsaShogi( "pong\n" );
1238 static int CONV cmd_hash( char **lasts )
1240 const char *str = strtok_r( NULL, str_delimiters, lasts );
1246 str_error = str_bad_cmdline;
1250 l = strtol( str, &ptr, 0 );
1251 if ( ptr == str || l == LONG_MAX || l < 1 || l > 31 )
1253 str_error = str_bad_cmdline;
1257 AbortDifficultCommand;
1259 log2_ntrans_table = (int)l;
1260 memory_free( (void *)ptrans_table_orig );
1261 return ini_trans_table();
1265 static int CONV cmd_limit( char **lasts )
1267 const char *str = strtok_r( NULL, str_delimiters, lasts );
1273 str_error = str_bad_cmdline;
1277 AbortDifficultCommand;
1279 if ( ! strcmp( str, "depth" ) )
1281 str = strtok_r( NULL, str_delimiters, lasts );
1284 str_error = str_bad_cmdline;
1287 l1 = strtol( str, &ptr, 0 );
1288 if ( ptr == str || l1 == LONG_MAX || l1 < 1 )
1290 str_error = str_bad_cmdline;
1293 sec_limit_up = UINT_MAX;
1294 node_limit = UINT64_MAX;
1295 depth_limit = (int)l1;
1297 else if ( ! strcmp( str, "nodes" ) )
1299 str = strtok_r( NULL, str_delimiters, lasts );
1302 str_error = str_bad_cmdline;
1305 l1 = strtol( str, &ptr, 0 );
1306 if ( ptr == str || l1 == LONG_MAX || l1 < 1 )
1308 str_error = str_bad_cmdline;
1311 sec_limit_up = UINT_MAX;
1312 depth_limit = PLY_MAX;
1313 node_limit = (uint64_t)l1;
1315 else if ( ! strcmp( str, "time" ) )
1317 str = strtok_r( NULL, str_delimiters, lasts );
1320 str_error = str_bad_cmdline;
1324 if ( ! strcmp( str, "extendable" ) )
1326 game_status |= flag_time_extendable;
1328 else if ( ! strcmp( str, "strict" ) )
1330 game_status &= ~flag_time_extendable;
1333 l1 = strtol( str, &ptr, 0 );
1334 if ( ptr == str || l1 == LONG_MAX || l1 < 0 )
1336 str_error = str_bad_cmdline;
1340 str = strtok_r( NULL, str_delimiters, lasts );
1343 str_error = str_bad_cmdline;
1346 l2 = strtol( str, &ptr, 0 );
1347 if ( ptr == str || l2 == LONG_MAX || l2 < 0 )
1349 str_error = str_bad_cmdline;
1353 str = strtok_r( NULL, str_delimiters, lasts );
1354 if ( ! str ) { l3 = -1; }
1356 l3 = strtol( str, &ptr, 0 );
1357 if ( ptr == str || l3 >= PLY_MAX || l3 < -1 )
1359 str_error = str_bad_cmdline;
1364 if ( ! ( l1 | l2 ) ) { l2 = 1; }
1366 depth_limit = PLY_MAX;
1367 node_limit = UINT64_MAX;
1368 sec_limit = (unsigned int)l1 * 60U;
1369 sec_limit_up = (unsigned int)l2;
1370 if ( l3 == -1 ) { sec_limit_depth = UINT_MAX; }
1371 else { sec_limit_depth = (unsigned int)l3; }
1375 str_error = str_bad_cmdline;
1384 cmd_read( tree_t * restrict ptree, char **lasts )
1386 const char *str1 = strtok_r( NULL, str_delimiters, lasts );
1387 const char *str2 = strtok_r( NULL, str_delimiters, lasts );
1388 const char *str3 = strtok_r( NULL, str_delimiters, lasts );
1389 const char *str_tmp;
1390 FILE *pf_src, *pf_dest;
1391 char str_file[SIZE_FILENAME];
1397 flag = flag_history | flag_rep | flag_detect_hang;
1403 str_error = str_bad_cmdline;
1409 if ( ! strcmp( str2, "t" ) ) { flag |= flag_time; }
1410 else if ( strcmp( str2, "nil" ) )
1412 str_error = str_bad_cmdline;
1419 l = strtol( str3, &ptr, 0 );
1420 if ( ptr == str3 || l == LONG_MAX || l < 1 )
1422 str_error = str_bad_cmdline;
1425 moves = (unsigned int)l - 1U;
1428 AbortDifficultCommand;
1430 if ( ! strcmp( str1, "." ) )
1432 str_tmp = "game.cs_";
1434 #if defined(NO_LOGGING)
1435 strncpy( str_file, "game.csa", SIZE_FILENAME-1 );
1437 snprintf( str_file, SIZE_FILENAME, "%s/game%03d.csa",
1438 str_dir_logs, record_num );
1440 pf_dest = file_open( str_tmp, "w" );
1441 if ( pf_dest == NULL ) { return -2; }
1443 pf_src = file_open( str_file, "r" );
1444 if ( pf_src == NULL )
1446 file_close( pf_dest );
1450 while ( ( c = getc(pf_src) ) != EOF ) { putc( c, pf_dest ); }
1452 iret = file_close( pf_src );
1455 file_close( pf_dest );
1459 iret = file_close( pf_dest );
1460 if ( iret < 0 ) { return iret; }
1466 iret = read_record( ptree, str1, moves, flag );
1467 if ( iret < 0 ) { return iret; }
1469 iret = get_elapsed( &time_turn_start );
1470 if ( iret < 0 ) { return iret; }
1472 if ( str_tmp && remove( str_tmp ) )
1474 out_warning( "remove() failed." );
1482 static int CONV cmd_resign( tree_t * restrict ptree, char **lasts )
1484 const char *str = strtok_r( NULL, str_delimiters, lasts );
1488 if ( str == NULL || *str == 'T' )
1490 AbortDifficultCommand;
1492 if ( game_status & mask_game_end ) { return 1; }
1494 game_status |= flag_resigned;
1495 update_time( root_turn );
1496 out_CSA( ptree, &record_game, MOVE_RESIGN );
1499 l = strtol( str, &ptr, 0 );
1500 if ( ptr == str || l == LONG_MAX || l < MT_CAP_PAWN )
1502 str_error = str_bad_cmdline;
1505 resign_threshold = (int)l;
1512 static int CONV cmd_move( tree_t * restrict ptree, char **lasts )
1514 const char *str = strtok_r( NULL, str_delimiters, lasts );
1520 if ( game_status & mask_game_end )
1522 str_error = str_game_ended;
1526 AbortDifficultCommand;
1530 if ( analyze_mode ) // [HGM] analyze: in analysis mode we cannot set the engine thinking (but perhaps play PV move?)
1532 str_error = str_bad_cmdline;
1536 iret = get_elapsed( &time_turn_start );
1537 if ( iret < 0 ) { return iret; }
1539 return com_turn_start( ptree, 0 );
1542 l = strtol( str, &ptr, 0 );
1543 if ( str != ptr && l != LONG_MAX && l >= 1 && *ptr == '\0' )
1545 if ( analyze_mode ) // [HGM] analyze: in analysis mode we cannot set the engine thinking
1547 str_error = str_bad_cmdline;
1551 for ( i = 0; i < l; i += 1 )
1553 if ( game_status & ( flag_move_now | mask_game_end ) ) { break; }
1555 iret = get_elapsed( &time_turn_start );
1556 if ( iret < 0 ) { return iret; }
1558 iret = com_turn_start( ptree, 0 );
1559 if ( iret < 0 ) { return iret; }
1566 iret = interpret_CSA_move( ptree, &move, str );
1567 if ( iret < 0 ) { return iret; }
1569 iret = get_elapsed( &time_turn_start );
1570 if ( iret < 0 ) { return iret; }
1572 iret = make_move_root( ptree, move,
1573 ( flag_history | flag_time | flag_rep
1574 | flag_detect_hang ) );
1575 if ( iret < 0 ) { return iret; }
1577 str = strtok_r( NULL, str_delimiters, lasts );
1579 } while ( str != NULL );
1581 moves_ignore[0] = MOVE_NA; // [HGM] exclude: exclude list cleared for new position
1582 if ( analyze_mode ) return do_analyze ( ptree ); // [HGM] analyze: analysis should continue after feeding moves
1588 static int CONV cmd_new( tree_t * restrict ptree, char **lasts )
1590 const char *str1 = strtok_r( NULL, str_delimiters, lasts );
1591 const char *str2 = strtok_r( NULL, str_delimiters, lasts );
1592 const min_posi_t *pmp;
1593 min_posi_t min_posi;
1596 AbortDifficultCommand;
1598 start_pos = *lasts; move_ptr = 0; // [HGM] undo: remember start position
1599 moves_ignore[0] = MOVE_NA; // [HGM] exclude: exclude list cleared for new position
1603 strncpy(start_data, str1, 511); // [HGM] undo: remember start position
1604 memset( &min_posi.asquare, empty, nsquare );
1605 min_posi.hand_black = min_posi.hand_white = 0;
1606 iret = read_board_rep1( str1, &min_posi );
1607 if ( iret < 0 ) { return iret; }
1611 if ( ! strcmp( str2, "-" ) ) { min_posi.turn_to_move = white; }
1612 else if ( ! strcmp( str2, "+" ) ) { min_posi.turn_to_move = black; }
1614 str_error = str_bad_cmdline;
1618 else { min_posi.turn_to_move = black; }
1622 else { pmp = &min_posi_no_handicap; }
1624 iret = ini_game( ptree, pmp, flag_history, NULL, NULL );
1625 if ( iret < 0 ) { return iret; }
1627 return get_elapsed( &time_turn_start );
1631 static int CONV cmd_outmove( tree_t * restrict ptree )
1633 const char *str_move;
1635 unsigned int move_list[ MAX_LEGAL_MOVES ];
1638 AbortDifficultCommand;
1640 if ( game_status & mask_game_end )
1642 Out( "NO LEGAL MOVE\n" );
1643 DFPNOut( "NO LEGAL MOVE\n" );
1647 n = gen_legal_moves( ptree, move_list, 0 );
1650 for ( c = i = 0; i < n; i += 1 )
1652 str_move = str_CSA_move(move_list[i]);
1654 if ( i && ( i % 10 ) == 0 )
1656 Out( "%s\n", buffer );
1657 DFPNOut( "%s ", buffer );
1658 memcpy( buffer, str_move, 6 );
1664 memcpy( buffer + c + 1, str_move, 6 );
1668 memcpy( buffer + c, str_move, 6 );
1673 Out( "%s\n", buffer );
1674 DFPNOut( "%s\n", buffer );
1680 static int CONV cmd_problem( tree_t * restrict ptree, char **lasts )
1682 const char *str = strtok_r( NULL, str_delimiters, lasts );
1685 unsigned int nposition;
1691 AbortDifficultCommand;
1696 if ( str != NULL && ! strcmp( str, "mate" ) )
1699 str = strtok_r( NULL, str_delimiters, lasts );
1705 l = strtol( str, &ptr, 0 );
1706 if ( ptr == str || l == LONG_MAX || l < 1 )
1708 str_error = str_bad_cmdline;
1711 nposition = (unsigned int)l;
1713 else { nposition = UINT_MAX; }
1716 iret = record_open( &record_problems, "problem.csa", mode_read, NULL, NULL );
1717 if ( iret < 0 ) { return iret; }
1720 iret = is_mate ? solve_mate_problems( ptree, nposition )
1721 : solve_problems( ptree, nposition );
1723 iret = solve_problems( ptree, nposition );
1728 record_close( &record_problems );
1732 iret = record_close( &record_problems );
1733 if ( iret < 0 ) { return iret; }
1735 return get_elapsed( &time_turn_start );
1739 static int CONV cmd_quit( void )
1741 game_status |= flag_quit;
1746 static int CONV cmd_suspend( void )
1748 if ( game_status & ( flag_pondering | flag_puzzling ) )
1750 game_status |= flag_quit_ponder;
1754 game_status |= flag_suspend;
1759 static int CONV cmd_time( char **lasts )
1761 const char *str = strtok_r( NULL, str_delimiters, lasts );
1766 str_error = str_bad_cmdline;
1769 else if ( ! strcmp( str, "response" ) )
1772 str = strtok_r( NULL, str_delimiters, lasts );
1775 str_error = str_bad_cmdline;
1778 l = strtol( str, &ptr, 0 );
1779 if ( ptr == str || l == LONG_MAX || l < 0 || l > 1000 )
1781 str_error = str_bad_cmdline;
1784 time_response = (unsigned int)l;
1787 else if ( ! strcmp( str, "remain" ) )
1791 str = strtok_r( NULL, str_delimiters, lasts );
1794 str_error = str_bad_cmdline;
1797 l1 = strtol( str, &ptr, 0 );
1798 if ( ptr == str || l1 == LONG_MAX || l1 < 0 )
1800 str_error = str_bad_cmdline;
1804 str = strtok_r( NULL, str_delimiters, lasts );
1807 str_error = str_bad_cmdline;
1810 l2 = strtol( str, &ptr, 0 );
1811 if ( ptr == str || l2 == LONG_MAX || l2 < 0 )
1813 str_error = str_bad_cmdline;
1817 if ( sec_limit_up == UINT_MAX )
1819 str_error = str_bad_cmdline;
1823 return reset_time( (unsigned int)l1, (unsigned int)l2 );
1826 str_error = str_bad_cmdline;
1831 #if !defined(MINIMUM)
1832 /* learn (ini|no-ini) steps games iterations tlp1 tlp2 */
1833 static int CONV cmd_learn( tree_t * restrict ptree, char **lasts )
1835 const char *str1 = strtok_r( NULL, str_delimiters, lasts );
1836 const char *str2 = strtok_r( NULL, str_delimiters, lasts );
1837 const char *str3 = strtok_r( NULL, str_delimiters, lasts );
1838 const char *str4 = strtok_r( NULL, str_delimiters, lasts );
1840 const char *str5 = strtok_r( NULL, str_delimiters, lasts );
1841 const char *str6 = strtok_r( NULL, str_delimiters, lasts );
1845 unsigned int max_games;
1846 int is_ini, nsteps, max_iterations, nworker1, nworker2, iret;
1850 str_error = str_bad_cmdline;
1853 if ( ! strcmp( str1, "ini" ) ) { is_ini = 1; }
1854 else if ( ! strcmp( str1, "no-ini" ) ) { is_ini = 0; }
1856 str_error = str_bad_cmdline;
1860 max_games = UINT_MAX;
1861 max_iterations = INT_MAX;
1862 nworker1 = nworker2 = nsteps = 1;
1866 l = strtol( str2, &ptr, 0 );
1867 if ( ptr == str2 || l == LONG_MAX || l < 1 )
1869 str_error = str_bad_cmdline;
1877 l = strtol( str3, &ptr, 0 );
1878 if ( ptr == str3 || l == LONG_MAX || l == LONG_MIN )
1880 str_error = str_bad_cmdline;
1883 if ( l > 0 ) { max_games = (unsigned int)l; }
1888 l = strtol( str4, &ptr, 0 );
1889 if ( ptr == str4 || l == LONG_MAX || l == LONG_MIN )
1891 str_error = str_bad_cmdline;
1894 if ( l > 0 ) { max_iterations = (int)l; }
1900 l = strtol( str5, &ptr, 0 );
1901 if ( ptr == str5 || l > TLP_MAX_THREADS || l < 1 )
1903 str_error = str_bad_cmdline;
1911 l = strtol( str6, &ptr, 0 );
1912 if ( ptr == str6 || l > TLP_MAX_THREADS || l < 1 )
1914 str_error = str_bad_cmdline;
1921 AbortDifficultCommand;
1923 log2_ntrans_table = 12;
1925 memory_free( (void *)ptrans_table_orig );
1927 iret = ini_trans_table();
1928 if ( iret < 0 ) { return iret; }
1930 iret = learn( ptree, is_ini, nsteps, max_games, max_iterations,
1931 nworker1, nworker2 );
1932 if ( iret < 0 ) { return -1; }
1934 iret = ini_game( ptree, &min_posi_no_handicap, flag_history, NULL, NULL );
1935 if ( iret < 0 ) { return -1; }
1937 iret = get_elapsed( &time_turn_start );
1938 if ( iret < 0 ) { return iret; }
1942 #endif /* MINIMUM */
1946 static int CONV cmd_mpv( tree_t * restrict ptree, char **lasts )
1948 const char *str = strtok_r( NULL, str_delimiters, lasts );
1954 str_error = str_bad_cmdline;
1957 else if ( ! strcmp( str, "num" ) )
1959 str = strtok_r( NULL, str_delimiters, lasts );
1962 str_error = str_bad_cmdline;
1965 l = strtol( str, &ptr, 0 );
1966 if ( ptr == str || l == LONG_MAX || l < 1 || l > MPV_MAX_PV )
1968 str_error = str_bad_cmdline;
1972 AbortDifficultCommand;
1976 if ( analyze_mode ) return do_analyze ( ptree ); // [HGM] analyze: analysis should continue changing num
1980 else if ( ! strcmp( str, "width" ) )
1982 str = strtok_r( NULL, str_delimiters, lasts );
1985 str_error = str_bad_cmdline;
1988 l = strtol( str, &ptr, 0 );
1989 if ( ptr == str || l == LONG_MAX || l < MT_CAP_PAWN )
1991 str_error = str_bad_cmdline;
1995 AbortDifficultCommand;
1999 if ( analyze_mode ) return do_analyze ( ptree ); // [HGM] analyze: analysis should continue after changing width
2004 str_error = str_bad_cmdline;
2011 static int CONV cmd_dfpn( tree_t * restrict ptree, char **lasts )
2013 const char *str = strtok_r( NULL, str_delimiters, lasts );
2017 str_error = str_bad_cmdline;
2020 else if ( ! strcmp( str, "hash" ) )
2025 str = strtok_r( NULL, str_delimiters, lasts );
2028 str_error = str_bad_cmdline;
2031 l = strtol( str, &ptr, 0 );
2032 if ( ptr == str || l == LONG_MAX || l < 1 )
2034 str_error = str_bad_cmdline;
2038 AbortDifficultCommand;
2040 dfpn_hash_log2 = (unsigned int)l;
2041 return dfpn_ini_hash();
2043 else if ( ! strcmp( str, "go" ) )
2045 AbortDifficultCommand;
2047 return dfpn( ptree, root_turn, 1 );
2049 else if ( ! strcmp( str, "connect" ) )
2057 str = strtok_r( NULL, str_delimiters, lasts );
2058 if ( ! str || ! strcmp( str, "." ) ) { str = "127.0.0.1"; }
2059 strncpy( str_addr, str, 255 );
2060 str_addr[255] = '\0';
2062 str = strtok_r( NULL, str_delimiters, lasts );
2063 if ( ! str || ! strcmp( str, "." ) ) { str = "4083"; }
2064 l = strtol( str, &ptr, 0 );
2065 if ( ptr == str || l == LONG_MAX || l < 0 || l > USHRT_MAX )
2067 str_error = str_bad_cmdline;
2072 str = strtok_r( NULL, str_delimiters, lasts );
2073 if ( ! str || ! strcmp( str, "." ) ) { str = "bonanza1"; }
2074 strncpy( str_id, str, 255 );
2077 AbortDifficultCommand;
2079 dfpn_sckt = sckt_connect( str_addr, port );
2080 if ( dfpn_sckt == SCKT_NULL ) { return -2; }
2082 str_buffer_cmdline[0] = '\0';
2083 DFPNOut( "Worker: %s\n", str_id );
2088 str_error = str_bad_cmdline;
2095 static int CONV cmd_thread( char **lasts )
2097 const char *str = strtok_r( NULL, str_delimiters, lasts );
2101 str_error = str_bad_cmdline;
2104 else if ( ! strcmp( str, "num" ) )
2109 str = strtok_r( NULL, str_delimiters, lasts );
2112 str_error = str_bad_cmdline;
2115 l = strtol( str, &ptr, 0 );
2116 if ( ptr == str || l == LONG_MAX || l < 1 || l > TLP_MAX_THREADS )
2118 str_error = str_bad_cmdline;
2126 if ( game_status & ( flag_thinking | flag_pondering | flag_puzzling ) )
2133 str_error = str_bad_cmdline;
2139 #if defined(DFPN_CLIENT)
2140 static int CONV cmd_dfpn_client( tree_t * restrict ptree, char **lasts )
2146 AbortDifficultCommand;
2148 str = strtok_r( NULL, str_delimiters, lasts );
2149 if ( ! str || ! strcmp( str, "." ) ) { str = "127.0.0.1"; }
2150 strncpy( dfpn_client_str_addr, str, 255 );
2151 dfpn_client_str_addr[255] = '\0';
2153 str = strtok_r( NULL, str_delimiters, lasts );
2154 if ( ! str || ! strcmp( str, "." ) ) { str = "4083"; }
2155 dfpn_client_port = strtol( str, &ptr, 0 );
2156 if ( ptr == str || dfpn_client_port == LONG_MAX || dfpn_client_port < 0
2157 || dfpn_client_port > USHRT_MAX )
2159 str_error = str_bad_cmdline;
2163 Out( "DFPN Server: %s %d\n", dfpn_client_str_addr, dfpn_client_port );
2165 iret = ini_game( ptree, &min_posi_no_handicap, flag_history, NULL, NULL );
2166 if ( iret < 0 ) { return iret; }
2168 if ( dfpn_client_sckt == SCKT_NULL )
2170 str_error = "Check network status.";
2174 return get_elapsed( &time_turn_start );
2179 #if defined(CSA_LAN)
2180 static int CONV cmd_connect( tree_t * restrict ptree, char **lasts )
2186 str = strtok_r( NULL, str_delimiters, lasts );
2187 if ( ! str || ! strcmp( str, "." ) ) { str = "gserver.computer-shogi.org"; }
2188 strncpy( client_str_addr, str, 255 );
2189 client_str_addr[255] = '\0';
2191 str = strtok_r( NULL, str_delimiters, lasts );
2192 if ( ! str || ! strcmp( str, "." ) ) { str = "4081"; }
2193 client_port = strtol( str, &ptr, 0 );
2194 if ( ptr == str || client_port == LONG_MAX || client_port < 0
2195 || client_port > USHRT_MAX )
2197 str_error = str_bad_cmdline;
2201 str = strtok_r( NULL, str_delimiters, lasts );
2202 if ( ! str || ! strcmp( str, "." ) ) { str = "bonanza_test"; }
2203 strncpy( client_str_id, str, 255 );
2204 client_str_id[255] = '\0';
2206 str = strtok_r( NULL, " \t", lasts );
2207 if ( ! str || ! strcmp( str, "." ) ) { str = "bonanza_test"; }
2208 strncpy( client_str_pwd, str, 255 );
2209 client_str_pwd[255] = '\0';
2211 str = strtok_r( NULL, str_delimiters, lasts );
2212 if ( ! str || ! strcmp( str, "." ) ) { client_max_game = INT_MAX; }
2214 max_games = strtol( str, &ptr, 0 );
2215 if ( ptr == str || max_games == LONG_MAX || max_games < 1 )
2217 str_error = str_bad_cmdline;
2220 client_max_game = max_games;
2223 AbortDifficultCommand;
2227 return client_next_game( ptree, client_str_addr, (int)client_port );
2231 static int CONV cmd_sendpv( char **lasts )
2233 const char *str = strtok_r( NULL, str_delimiters, lasts );
2237 str_error = str_bad_cmdline;
2241 if ( ! strcmp( str, str_off ) ) { game_status &= ~flag_sendpv; }
2242 else if ( ! strcmp( str, str_on ) ) { game_status |= flag_sendpv; }
2244 str_error = str_bad_cmdline;
2253 #if defined(MNJ_LAN)
2254 /* mnj sd seed addr port name factor stable_depth */
2255 static int CONV cmd_mnj( char **lasts )
2257 char client_str_addr[256];
2258 char client_str_id[256];
2263 int client_port, sd;
2266 str = strtok_r( NULL, str_delimiters, lasts );
2269 str_error = str_bad_cmdline;
2272 l = strtol( str, &ptr, 0 );
2273 if ( ptr == str || l == LONG_MAX || l < 0 )
2275 str_error = str_bad_cmdline;
2281 str = strtok_r( NULL, str_delimiters, lasts );
2284 str_error = str_bad_cmdline;
2287 l = strtol( str, &ptr, 0 );
2288 if ( ptr == str || l == LONG_MAX || l < 0 )
2290 str_error = str_bad_cmdline;
2293 seed = (unsigned int)l;
2296 str = strtok_r( NULL, str_delimiters, lasts );
2297 if ( ! str || ! strcmp( str, "." ) ) { str = "localhost"; }
2298 strncpy( client_str_addr, str, 255 );
2299 client_str_addr[255] = '\0';
2302 str = strtok_r( NULL, str_delimiters, lasts );
2303 if ( ! str || ! strcmp( str, "." ) ) { str = "4082"; }
2304 l = strtol( str, &ptr, 0 );
2305 if ( ptr == str || l == LONG_MAX || l < 0 || l > USHRT_MAX )
2307 str_error = str_bad_cmdline;
2310 client_port = (int)l;
2313 str = strtok_r( NULL, str_delimiters, lasts );
2314 if ( ! str || ! strcmp( str, "." ) ) { str = "bonanza1"; }
2315 strncpy( client_str_id, str, 255 );
2316 client_str_id[255] = '\0';
2318 str = strtok_r( NULL, str_delimiters, lasts );
2319 if ( ! str || ! strcmp( str, "." ) ) { str = "1.0"; }
2320 factor = strtod( str, &ptr );
2321 if ( ptr == str || factor < 0.0 )
2323 str_error = str_bad_cmdline;
2327 str = strtok_r( NULL, str_delimiters, lasts );
2328 if ( ! str || ! strcmp( str, "." ) ) { l = -1; }
2330 l = strtol( str, &ptr, 0 );
2331 if ( ptr == str || l == LONG_MAX )
2333 str_error = str_bad_cmdline;
2337 if ( l <= 0 ) { mnj_depth_stable = INT_MAX; }
2338 else { mnj_depth_stable = (int)l; }
2340 AbortDifficultCommand;
2342 resign_threshold = 65535;
2343 game_status |= ( flag_noponder | flag_noprompt );
2344 if ( mnj_reset_tbl( sd, seed ) < 0 ) { return -1; }
2346 sckt_mnj = sckt_connect( client_str_addr, (int)client_port );
2347 if ( sckt_mnj == SCKT_NULL ) { return -2; }
2349 str_buffer_cmdline[0] = '\0';
2351 Out( "Sending my name %s", client_str_id );
2352 MnjOut( "%s %g final%s\n", client_str_id, factor,
2353 ( mnj_depth_stable == INT_MAX ) ? "" : " stable" );
2355 return cmd_suspend();