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_inex( tree_t * restrict ptree, int inex, char **last ); // [HGM] exclude
98 static int CONV cmd_exit( void );
101 int CONV is_move( const char *str )
103 if ( isdigit( (int)str[0] ) && isdigit( (int)str[1] )
104 && isdigit( (int)str[2] ) && isdigit( (int)str[3] )
105 && isupper( (int)str[4] ) && isupper( (int)str[5] )
106 && str[6] == '\0' ) { return 1; }
114 strtok_r( char *s, const char *t, char **next)
118 if( ! s ) return NULL;
119 while( *s && strchr(t, *s) ) s++;
120 if( ! *s ) return NULL;
122 while( *s && ! (p = strchr(t, *s)) ) s++;
124 if( ! *s ) *next = NULL;
131 procedure( tree_t * restrict ptree )
134 if ( sckt_csa != SCKT_NULL ) { return proce_csalan( ptree ); }
138 if ( sckt_mnj != SCKT_NULL ) { return proce_mnj( ptree ); }
142 if ( usi_mode != usi_off ) { return proce_usi( ptree ); }
145 return proce_cui( ptree );
148 char *start_pos, start_data[512]; // [HGM] undo: for remembering start position
149 int move_list[1024], move_ptr;
151 int all_moves[MAX_LEGAL_MOVES];
154 #define IF(X) else if(!strcmp(command, X))
156 int myTime, hisTime, movesPerSession, inc, plyNr;
158 int root_pos[nsquare];
161 bonanza_piece( char p )
164 if(p >= 'a') p += 'A' - 'a';
165 switch(p) { // encode piece
166 case 'P': piece = pawn; break;
167 case 'L': piece = lance; break;
168 case 'N': piece = knight; break;
169 case 'S': piece = silver; break;
170 case 'G': piece = gold; break;
171 case 'B': piece = bishop; break;
172 case 'R': piece = rook; break;
173 case 'K': piece = king; break;
181 static char fen[128];
183 char *q = fen, *start = p;
184 strncpy( fen, p+9 , 127);
185 strcpy( p, "new SU" ); p += 6;
186 for ( r = 1; r <= 9; r++) {
187 for ( f = 0; *q; q++ ) {
189 if( *q == '+' ) promoted = promote, q++;
190 if( isdigit( *q ) ) f += atoi( q ); else
191 if( isalpha( *q ) ) {
192 int piece = bonanza_piece( *q );
193 if( ( *q >= 'a' && *q <= 'z' ) != gote ) {
194 gote = !gote; // switch color
195 *p++ = ( gote ? '-' : '+' );
197 sprintf( p, "%d%d%s", 9-f, r, astr_table_piece[piece + promoted] );
199 } else { q++; break; }
202 while( q[-1] == ' ' ) q++;
203 if( q[-1] == '[' ) { // holdings
204 while( isalpha( *q ) ) {
205 int piece = bonanza_piece( *q );
206 if( ( *q >= 'a' && *q <= 'z' ) != gote ) {
207 gote = !gote; // switch color
208 *p++ = ( gote ? '-' : '+' );
210 sprintf( p, "00%s", astr_table_piece[piece] );
215 while( *q == ' ' ) q++;
217 sprintf(p, " %c", *q == 'w' ? '+' : '-' ); // side to move
221 xboard_to_CSA( tree_t * restrict ptree, char *in, char *out )
223 char fromX=in[0], fromY=in[1], toX=in[2], toY=in[3], promo=in[4];
225 if(fromY == '@') { // drop (contains all info needed to convert it)
226 piece = bonanza_piece( fromX );
227 sprintf(out, "00%c%c%s", 'a'+'9'-toX, '1'+'9'-toY, astr_table_piece[piece]);
228 } else { // board move (need to figure out moved piece)
229 int from = ('9' - fromY)*9 + (fromX - 'a');
230 int flag = (promo == '+' ? FLAG_PROMO : 0);
231 piece = abs( BOARD[from] ); // this only works when no search in progress!
232 printf("# piece from board: %d\n", piece);fflush(stdout);
233 if( game_status & flag_thinking ) {
234 int i, to = ('9' - toY)*9 + (toX - 'a');
235 piece = 0; // kludge to force illegal CSA move
236 for( i = 0; i < root_nmove; i++ ) { // determine the piece from the move list
237 int move = root_move_list[i].move;
238 if( I2To(move) != to ) continue;
239 if( I2From(move) != from ) continue;
240 if( (move & FLAG_PROMO) != flag ) continue;
241 piece = I2PieceMove( move ); // we found the move; take the piece from it
244 } else if( game_status & ( flag_pondering | flag_puzzling ) ) piece = abs( root_pos[from] ); // we have valid copy!
245 printf("# piece corrected to %d\n", piece);fflush(stdout);
246 if( promo == '+') piece += promote;
247 sprintf(out, "%c%c%c%c%s", 'a'+'9'-fromX, '1'+'9'-fromY, 'a'+'9'-toX, '1'+'9'-toY, astr_table_piece[piece]);
252 update_exclude_list( tree_t * restrict ptree, int add, unsigned int move )
253 { // [HGM] exclude: manage list of excluded moves
255 if( moves_ignore[0] == MOVE_NA ) { // nothing is excluded yet; make a copy of root move list;
256 for( i = 0; i < root_nmove; i++) all_moves[i] = root_move_list[i].move;
257 all_moves[i] = MOVE_NA;
259 if ( move == MOVE_NA ) { // all moves
260 if( add ) { // copy entire list of legal moves
261 for( i = 0; i < root_nmove; i++ ) moves_ignore[i] = all_moves[i];
262 } else moves_ignore[0] = MOVE_NA; // clear list
263 } else { // single move
264 for( i = 0; moves_ignore[i] != MOVE_NA; i++ ) if( move == moves_ignore[i] ) break;
265 if( add ) { // we must add the move
266 if( moves_ignore[i] != MOVE_NA ) return; // but it was already in list
267 if( i >= MAX_LEGAL_MOVES - 2 ) return; // overflow
268 moves_ignore[i] = move; moves_ignore[i+1] = MOVE_NA; // append move
269 } else { // we must delete the move
270 if( moves_ignore[i] == MOVE_NA ) return; // but it was not there
271 while( (moves_ignore[i] = moves_ignore[i+1]) ) i++; // squeeze it out
278 { // set white and black times from own and opponent time.
280 if(movesPerSession <= 0) moves = 35; else {
282 while(moves <= 0) moves += movesPerSession;
284 time_limit = (myTime-inc-30)/(moves+2) + inc;
285 time_max_limit = 3*time_limit;
286 if(time_max_limit > myTime - 30)time_max_limit = myTime - 30; // keep 0.3 sec margin, as Bonanza reads the clock infrequently
287 time_limit *= 10; // msec
288 time_max_limit *= 10;
289 Out("# moves=%d, time=%d inc=%d t=%d, max=%d\n", moves, myTime, inc, time_limit, time_max_limit);
293 proce_xboard(char *line, const char *command, tree_t * restrict ptree)
294 { // [HGM] added to make Bonanza.exe a native WinBoard engine
296 static char forceMode = 0;
297 sscanf(line + strlen(command) + 1, "%d", &value);
298 Out("# command = '%s'\n", line);
302 Out("feature option=\"MultiPV -spin 1 1 100\"\n");
303 Out("feature option=\"centi-Pawn margin -spin 200 0 25000\"\n");
305 Out("feature variants=\"shogi\" usermove=1 myname=\"Bonanza " BNZ_VER
306 "\" memory=1 smp=1 debug=1 colors=0 setboard=1 ping=1 sigint=0 exclude=1 done=1\n");
308 IF("new") { forceMode = plyNr = 0; SetTimes(); return 0; }
309 IF("easy") { strcpy(line, "ponder off"); return 0; }
310 IF("hard") { strcpy(line, "ponder on"); return 0; }
313 IF("time") { sscanf(line+5, "%d", &myTime); }
314 IF("otim") { sscanf(line+5, "%d", &hisTime); }
315 IF("force") { forceMode = 1; }
316 IF("go") { forceMode = 0; SetTimes(); plyNr++; strcpy(line, "move"); return 0; }
317 IF("memory") { sprintf(line, "hash %d", value); return 0; }
318 IF("cores") { sprintf(line, "tlp num %d", value); return 0; }
319 IF("sd") { sprintf(line, "limit depth %d", value); return 0; }
321 IF("quit") { return 0; }
322 IF("analyze") { return 0; }
323 IF("exit") { return 0; }
324 IF("variant") { /* ignore, since it must be Shogi */; }
325 IF("setboard") { forceMode = 1; plyNr = 0; read_fen( line ); return 0; }
327 if(sscanf(line+7, "MultiPV=%d", &value) == 1) { sprintf(line, "mpv num %d", value); return 0; }
328 if(sscanf(line+7, "centi-Pawn margin=%d", &value) == 1) { sprintf(line, "mpv width %d", value); return 0; }
330 IF("level") { int min, sec; float fsec=0.;
331 if(sscanf(line+6, "%d %d:%d %f", &movesPerSession, &min, &sec, &fsec) != 4)
332 sscanf(line+6, "%d %d %f", &movesPerSession, &min, &fsec);
333 min = 60*min + sec; myTime = hisTime = 100*min; inc = 100 * fsec;
335 IF("usermove") { char buf[20];
336 xboard_to_CSA( ptree, line+9, buf );
337 if(forceMode || analyze_mode) strcpy(line, "move "), line += 5; else plyNr++, SetTimes();
342 IF("undo") { return 0; }
344 IF("ping") { Out("pong %d\n", value); }
345 IF("exclude") { xboard_to_CSA( ptree, line+8, line+8 ); line[7] = ' '; return 0; }
346 IF("include") { xboard_to_CSA( ptree, line+8, line+8 ); line[7] = ' '; return 0; }
351 static int CONV proce_cui( tree_t * restrict ptree )
356 token = strtok_r( str_cmdline, str_delimiters, &last );
358 if ( token == NULL || *token == '#' ) { return 1; }
362 if( proce_xboard(str_cmdline, token, ptree) ) return 1; // command already processed
363 Out("# translated command '%s'\n", str_cmdline); // command translated for processing by Bonanza
364 token = strtok_r( str_cmdline, str_delimiters, &last ); // redo parsing
366 if ( ! strcmp( token, "xboard" ) ) { xboard_mode = 1; game_status |= flag_noprompt; return 1; }
369 if ( ! strcmp( token, "analyze" ) ) { return cmd_analyze( ptree ); } // [HGM] analyze
370 if ( ! strcmp( token, "exit" ) ) { return cmd_exit(); } // [HGM] analyze
371 if ( ! strcmp( token, "move" ) ) { return cmd_move( ptree, &last ); }
372 if ( ! strcmp( token, "undo" ) ) { return cmd_undo( ptree ); } // [HGM] undo
373 if ( ! strcmp( token, "include" ) ) { return cmd_inex( ptree , 0, &last); } // [HGM] exclude
374 if ( ! strcmp( token, "exclude" ) ) { return cmd_inex( ptree , 1, &last); } // [HGM] exclude
376 if ( ! strcmp( token, "mpv" ) ) { return cmd_mpv( ptree, &last ); }
378 analyze_mode = 0; // [HGM] analyze: all other commands terminate analysis
379 if ( is_move( token ) ) { return cmd_usrmove( ptree, token, &last ); }
380 if ( ! strcmp( token, "s" ) ) { return cmd_move_now(); }
381 if ( ! strcmp( token, "beep" ) ) { return cmd_beep( &last); }
382 if ( ! strcmp( token, "book" ) ) { return CmdBook( ptree, &last ); }
383 if ( ! strcmp( token, "display" ) ) { return cmd_display( ptree, &last ); }
384 if ( ! strcmp( token, "hash" ) ) { return cmd_hash( &last ); }
385 if ( ! strcmp( token, "limit" ) ) { return cmd_limit( &last ); }
386 if ( ! strcmp( token, "new" ) ) { return cmd_new( ptree, &last ); }
387 if ( ! strcmp( token, "outmove" ) ) { return cmd_outmove( ptree ); }
388 if ( ! strcmp( token, "peek" ) ) { return cmd_peek( &last ); }
389 if ( ! strcmp( token, "stdout" ) ) { return cmd_stdout( &last ); }
390 if ( ! strcmp( token, "ping" ) ) { return cmd_ping(); }
391 if ( ! strcmp( token, "ponder" ) ) { return cmd_ponder( &last ); }
392 if ( ! strcmp( token, "problem" ) ) { return cmd_problem( ptree, &last ); }
393 if ( ! strcmp( token, "quit" ) ) { return cmd_quit(); }
394 if ( ! strcmp( token, "read" ) ) { return cmd_read( ptree, &last ); }
395 if ( ! strcmp( token, "resign" ) ) { return cmd_resign( ptree, &last ); }
396 if ( ! strcmp( token, "suspend" ) ) { return cmd_suspend(); }
397 if ( ! strcmp( token, "time" ) ) { return cmd_time( &last ); }
398 if ( ! strcmp( token, "newlog" ) ) { return cmd_newlog( &last ); }
400 if ( ! strcmp( token, "connect" ) ) { return cmd_connect( ptree, &last ); }
401 if ( ! strcmp( token, "sendpv" ) ) { return cmd_sendpv( &last ); }
404 if ( ! strcmp( token, "mnj" ) ) { return cmd_mnj( &last ); }
407 if ( ! strcmp( token, "dfpn" ) ) { return cmd_dfpn( ptree, &last ); }
409 #if defined(DFPN_CLIENT)
410 if ( ! strcmp( token, "dfpn_client")) { return cmd_dfpn_client( ptree,
414 if ( ! strcmp( token, "tlp" ) ) { return cmd_thread( &last ); }
416 #if ! defined(NO_STDOUT)
417 if ( ! strcmp( token, "stress" ) ) { return cmd_stress( &last ); }
419 #if ! defined(MINIMUM)
420 if ( ! strcmp( token, "learn" ) ) { return cmd_learn( ptree, &last ); }
423 str_error = str_bad_cmdline;
429 static int CONV proce_csalan( tree_t * restrict ptree )
434 token = strtok_r( str_cmdline, str_delimiters, &last );
436 if ( token == NULL ) { return 1; }
437 if ( *token == ach_turn[client_turn] && is_move( token+1 ) )
442 token = strtok_r( NULL, str_delimiters, &last );
443 if ( token == NULL || *token != 'T' )
445 str_error = str_bad_cmdline;
449 l = strtol( token+1, &ptr, 0 );
450 if ( token+1 == ptr || l == LONG_MAX || l < 1 )
452 str_error = str_bad_cmdline;
456 adjust_time( (unsigned int)l, client_turn );
457 Out( " elapsed: b%u, w%u\n", sec_b_total, sec_w_total );
460 if ( *token == ach_turn[Flip(client_turn)] && is_move( token+1 ) )
462 return cmd_usrmove( ptree, token+1, &last );
464 if ( ! strcmp( token, str_resign ) ) { return cmd_resign( ptree, &last ); }
465 if ( ! strcmp( token, "#WIN" )
466 || ! strcmp( token, "#LOSE" )
467 || ! strcmp( token, "#DRAW" )
468 || ! strcmp( token, "#CHUDAN" ) )
470 if ( game_status & ( flag_thinking | flag_pondering | flag_puzzling ) )
472 game_status |= flag_suspend;
476 if ( sckt_out( sckt_csa, "LOGOUT\n" ) < 0 ) { return -1; }
477 if ( sckt_recv_all( sckt_csa ) < 0 ) { return -1; }
481 if ( client_ngame == client_max_game ) { return cmd_quit(); }
483 return client_next_game( ptree, client_str_addr, (int)client_port );
492 static int CONV proce_mnj( tree_t * restrict ptree )
498 token = strtok_r( str_cmdline, str_delimiters, &last );
499 if ( token == NULL ) { return 1; }
501 if ( ! strcmp( token, "new" ) )
503 iret = cmd_suspend();
504 if ( iret != 1 ) { return iret; }
507 iret = cmd_new( ptree, &last );
508 if ( iret < 0 ) { return iret; }
510 moves_ignore[0] = MOVE_NA;
511 return analyze( ptree );
513 if ( ! strcmp( token, "ignore" ) ) { return cmd_mnjignore( ptree, &last ); }
514 if ( ! strcmp( token, "idle" ) ) { return cmd_suspend(); }
515 if ( ! strcmp( token, "alter" ) ) { return cmd_mnjmove( ptree, &last, 1 ); }
516 if ( ! strcmp( token, "retract" ) )
520 const char *str = strtok_r( NULL, str_delimiters, &last );
523 str_error = str_bad_cmdline;
526 l = strtol( str, &ptr, 0 );
527 if ( ptr == str || (long)NUM_UNMAKE < l )
529 str_error = str_bad_cmdline;
533 return cmd_mnjmove( ptree, &last, (int)l );
535 if ( ! strcmp( token, "move" ) ) { return cmd_mnjmove( ptree, &last, 0 ); }
537 str_error = str_bad_cmdline;
543 cmd_mnjignore( tree_t *restrict ptree, char **lasts )
552 token = strtok_r( NULL, str_delimiters, lasts );
555 str_error = str_bad_cmdline;
558 lid = strtol( token, &ptr, 0 );
559 if ( ptr == token || lid == LONG_MAX || lid < 1 )
561 str_error = str_bad_cmdline;
565 AbortDifficultCommand;
567 for ( i = 0; ; i += 1 )
569 token = strtok_r( NULL, str_delimiters, lasts );
570 if ( token == NULL ) { break; }
572 if ( interpret_CSA_move( ptree, &move, token ) < 0 ) { return -1; }
574 moves_ignore[i] = move;
578 str_error = str_bad_cmdline;
581 mnj_posi_id = (int)lid;
582 moves_ignore[i] = MOVE_NA;
584 return analyze( ptree );
589 cmd_mnjmove( tree_t * restrict ptree, char **lasts, int num_alter )
591 const char *str1 = strtok_r( NULL, str_delimiters, lasts );
592 const char *str2 = strtok_r( NULL, str_delimiters, lasts );
598 if ( sckt_mnj == SCKT_NULL || str1 == NULL || str2 == NULL )
600 str_error = str_bad_cmdline;
604 lid = strtol( str2, &ptr, 0 );
605 if ( ptr == str2 || lid == LONG_MAX || lid < 1 )
607 str_error = str_bad_cmdline;
611 AbortDifficultCommand;
615 iret = unmake_move_root( ptree );
616 if ( iret < 0 ) { return iret; }
621 iret = interpret_CSA_move( ptree, &move, str1 );
622 if ( iret < 0 ) { return iret; }
624 iret = get_elapsed( &time_turn_start );
625 if ( iret < 0 ) { return iret; }
627 mnj_posi_id = (int)lid;
629 iret = make_move_root( ptree, move, ( flag_time | flag_rep
630 | flag_detect_hang ) );
631 if ( iret < 0 ) { return iret; }
633 # if ! defined(NO_STDOUT)
634 iret = out_board( ptree, stdout, 0, 0 );
635 if ( iret < 0 ) { return iret; }
638 moves_ignore[0] = MOVE_NA;
639 return analyze( ptree );
645 do_analyze( tree_t * restrict ptree )
646 { // [HGM] analyze: do a ponder search on the current position
648 if ( get_elapsed( &time_start ) < 0 ) { return -1; }
649 time_limit = time_max_limit = 1e9; // kludge: use huge time to mimic infinity
651 if(xboard_mode) Out("1 0 0 0 New Search\n"); // make sure lower depth is emitted, so XBoard undestand new search started
652 for( iret = 0; iret < nsquare; iret++ ) root_pos[iret] = BOARD[iret];
654 game_status |= flag_pondering;
655 iret = iterate( ptree );
656 game_status &= ~flag_pondering;
662 cmd_undo( tree_t * restrict ptree )
663 { // [HGM] undo: restart the game, and feed all moves except the last
664 int i, last = move_ptr;
665 char *p = start_data;
666 if( move_ptr <= 0 ) {
667 str_error = "undo past start of game ignored";
671 AbortDifficultCommand;
674 cmd_new( ptree, &p );
675 for(i=0; i<last; i++) {
676 make_move_root( ptree, move_list[i], 0);
679 moves_ignore[0] = MOVE_NA; // [HGM] exclude: exclude list cleared for new position
680 if ( analyze_mode ) return do_analyze ( ptree ); // [HGM] analyze: analysis should continue after undo
687 cmd_analyze( tree_t * restrict ptree )
688 { // [HGM] analyze: switch on analyze mode, and start analyzing (also used to force a restart)
689 AbortDifficultCommand;
692 return do_analyze( ptree );
697 cmd_inex( tree_t * restrict ptree, int inex, char **last )
698 { // [HGM] analyze: switch off analysis mode
699 const char *str = strtok_r( NULL, str_delimiters, last );
703 AbortDifficultCommand;
705 if( ! strcmp( str, "all" ) ) {
708 iret = interpret_CSA_move( ptree, &move, str );
709 if ( iret < 0 ) { return iret; }
712 update_exclude_list( ptree, inex, move );
714 if ( ! analyze_mode ) return 1;
715 return do_analyze( ptree );
721 { // [HGM] analyze: switch off analysis mode
722 if ( !analyze_mode ) {
723 str_error = "was not analyzing";
727 if ( game_status & flag_pondering ) { game_status |= flag_quit_ponder; return 2; }
729 moves_ignore[0] = MOVE_NA; // [HGM] exclude: exclude list cleared after analysis
736 static int CONV proce_usi( tree_t * restrict ptree )
742 token = strtok_r( str_cmdline, str_delimiters, &lasts );
743 if ( token == NULL ) { return 1; }
745 if ( ! strcmp( token, "usi" ) )
747 USIOut( "id name %s\n", str_myname );
748 USIOut( "id author Kunihito Hoki\n" );
753 if ( ! strcmp( token, "isready" ) )
755 USIOut( "readyok\n", str_myname );
759 if ( ! strcmp( token, "echo" ) )
761 USIOut( "%s\n", lasts );
765 if ( ! strcmp( token, "ignore_moves" ) )
767 return usi_ignore( ptree, &lasts );
770 if ( ! strcmp( token, "genmove_probability" ) )
772 if ( get_elapsed( &time_start ) < 0 ) { return -1; }
773 return usi_root_list( ptree );
776 if ( ! strcmp( token, "go" ) )
778 iret = usi_go( ptree, &lasts );
779 moves_ignore[0] = MOVE_NA;
783 if ( ! strcmp( token, "stop" ) ) { return cmd_move_now(); }
784 if ( ! strcmp( token, "position" ) ) { return usi_posi( ptree, &lasts ); }
785 if ( ! strcmp( token, "quit" ) ) { return cmd_quit(); }
787 str_error = str_bad_cmdline;
793 usi_ignore( tree_t * restrict ptree, char **lasts )
800 AbortDifficultCommand;
802 for ( i = 0; ; i += 1 )
804 token = strtok_r( NULL, str_delimiters, lasts );
805 if ( token == NULL ) { break; }
807 if ( usi2csa( ptree, token, str_buf ) < 0 ) { return -1; }
808 if ( interpret_CSA_move( ptree, &move, str_buf ) < 0 ) { return -1; }
810 moves_ignore[i] = move;
813 moves_ignore[i] = MOVE_NA;
820 usi_go( tree_t * restrict ptree, char **lasts )
827 AbortDifficultCommand;
829 if ( game_status & mask_game_end )
831 str_error = str_game_ended;
835 token = strtok_r( NULL, str_delimiters, lasts );
837 if ( ! strcmp( token, "book" ) )
839 AbortDifficultCommand;
840 if ( usi_book( ptree ) < 0 ) { return -1; }
846 if ( ! strcmp( token, "infinite" ) )
848 usi_byoyomi = UINT_MAX;
849 depth_limit = PLY_MAX;
850 node_limit = UINT64_MAX;
851 sec_limit_depth = UINT_MAX;
853 else if ( ! strcmp( token, "byoyomi" ) )
855 token = strtok_r( NULL, str_delimiters, lasts );
858 str_error = str_bad_cmdline;
862 l = strtol( token, &ptr, 0 );
863 if ( ptr == token || l > UINT_MAX || l < 1 )
865 str_error = str_bad_cmdline;
869 usi_byoyomi = (unsigned int)l;
870 depth_limit = PLY_MAX;
871 node_limit = UINT64_MAX;
872 sec_limit_depth = UINT_MAX;
875 str_error = str_bad_cmdline;
880 if ( get_elapsed( &time_turn_start ) < 0 ) { return -1; }
882 iret = com_turn_start( ptree, 0 );
884 if ( str_error == str_no_legal_move ) { USIOut( "bestmove resign\n" ); }
893 usi_posi( tree_t * restrict ptree, char **lasts )
899 AbortDifficultCommand;
901 moves_ignore[0] = MOVE_NA;
903 token = strtok_r( NULL, str_delimiters, lasts );
904 if ( strcmp( token, "startpos" ) )
906 str_error = str_bad_cmdline;
910 if ( ini_game( ptree, &min_posi_no_handicap,
911 flag_history, NULL, NULL ) < 0 ) { return -1; }
913 token = strtok_r( NULL, str_delimiters, lasts );
914 if ( token == NULL ) { return 1; }
916 if ( strcmp( token, "moves" ) )
918 str_error = str_bad_cmdline;
924 token = strtok_r( NULL, str_delimiters, lasts );
925 if ( token == NULL ) { break; }
927 if ( usi2csa( ptree, token, str_buf ) < 0 ) { return -1; }
928 if ( interpret_CSA_move( ptree, &move, str_buf ) < 0 ) { return -1; }
929 if ( make_move_root( ptree, move, ( flag_history | flag_time
931 | flag_detect_hang ) ) < 0 )
937 if ( get_elapsed( &time_turn_start ) < 0 ) { return -1; }
944 static int CONV cmd_move_now( void )
946 if ( game_status & flag_thinking ) { game_status |= flag_move_now; }
953 cmd_usrmove( tree_t * restrict ptree, const char *str_move, char **lasts )
961 if ( game_status & mask_game_end )
963 str_error = str_game_ended;
967 if ( game_status & flag_thinking )
969 str_error = str_busy_think;
973 str = strtok_r( NULL, str_delimiters, lasts );
974 if ( str == NULL ) { lelapsed = 0; }
978 str_error = str_bad_cmdline;
982 lelapsed = strtol( str, &ptr, 0 );
983 if ( ptr == str || lelapsed == LONG_MAX || lelapsed < 1 )
985 str_error = str_bad_cmdline;
990 if ( game_status & ( flag_pondering | flag_puzzling ) )
994 for ( i = 0; i < ponder_nmove; i++ )
996 if ( ! strcmp( str_move, str_CSA_move(ponder_move_list[i]) ) )
1001 if ( i == ponder_nmove )
1003 #if defined(CSA_LAN)
1004 if ( sckt_csa != SCKT_NULL ) { AbortDifficultCommand; }
1007 #if defined(CSASHOGI)
1008 AbortDifficultCommand;
1010 str_error = str_illegal_move;
1015 if ( ( game_status & flag_puzzling )
1016 || strcmp( str_move, str_CSA_move(ponder_move) ) )
1018 ponder_move = MOVE_PONDER_FAILED;
1019 game_status |= flag_quit_ponder;
1023 iret = update_time( Flip(root_turn) );
1024 if ( iret < 0 ) { return iret; }
1027 adjust_time( (unsigned int)lelapsed, Flip(root_turn) );
1030 out_CSA( ptree, &record_game, ponder_move );
1032 game_status &= ~flag_pondering;
1033 game_status |= flag_thinking;
1037 set_search_limit_time( root_turn );
1039 OutCsaShogi( "info ponder end\n" );
1041 str = str_time_symple( time_turn_start - time_start );
1042 Out( " %6s MOVE PREDICTION HIT\n"
1043 " elapsed: b%u, w%u\n", str, sec_b_total, sec_w_total );
1048 iret = interpret_CSA_move( ptree, &move, str_move );
1049 if ( iret < 0 ) { return iret; }
1050 move_evasion_pchk = 0;
1051 iret = make_move_root( ptree, move, ( flag_rep | flag_history | flag_time
1052 | flag_detect_hang ) );
1056 #if defined(CSA_LAN)
1057 if ( sckt_csa != SCKT_NULL )
1059 if ( move_evasion_pchk )
1061 str = str_CSA_move( move_evasion_pchk );
1062 iret = sckt_out( sckt_csa, "%c%s\n",
1063 ach_turn[Flip(root_turn)], str );
1064 if ( iret < 0 ) { return iret; }
1066 return cmd_suspend();
1070 if ( move_evasion_pchk )
1072 str = str_CSA_move( move_evasion_pchk );
1073 #if defined(CSASHOGI)
1074 OutCsaShogi( "move%s\n", str );
1075 return cmd_suspend();
1077 snprintf( str_message, SIZE_MESSAGE, "perpetual check (%c%s)",
1078 ach_turn[Flip(root_turn)], str );
1079 str_error = str_message;
1087 if ( lelapsed ) { adjust_time( (unsigned int)lelapsed, Flip(root_turn) ); }
1088 Out( " elapsed: b%u, w%u\n", sec_b_total, sec_w_total );
1090 #if defined(CSA_LAN)
1091 if ( sckt_csa != SCKT_NULL && ( game_status & flag_mated ) )
1093 iret = sckt_out( sckt_csa, "%%TORYO\n" );
1094 if ( iret < 0 ) { return iret; }
1098 if ( ! ( game_status & mask_game_end ) )
1100 iret = com_turn_start( ptree, 0 );
1101 if ( iret < 0 ) { return iret; }
1108 static int CONV cmd_beep( char **lasts )
1110 const char *str = strtok_r( NULL, str_delimiters, lasts );
1113 str_error = str_bad_cmdline;
1117 if ( ! strcmp( str, str_on ) ) { game_status &= ~flag_nobeep; }
1118 else if ( ! strcmp( str, str_off ) ) { game_status |= flag_nobeep; }
1120 str_error = str_bad_cmdline;
1128 static int CONV cmd_peek( 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_nopeek; }
1139 else if ( ! strcmp( str, str_off ) ) { game_status |= flag_nopeek; }
1141 str_error = str_bad_cmdline;
1149 static int CONV cmd_stdout( char **lasts )
1151 const char *str = strtok_r( NULL, str_delimiters, lasts );
1155 str_error = str_bad_cmdline;
1159 if ( ! strcmp( str, str_on ) ) { game_status &= ~flag_nostdout; }
1160 else if ( ! strcmp( str, str_off ) ) { game_status |= flag_nostdout; }
1162 str_error = str_bad_cmdline;
1170 static int CONV cmd_newlog( char **lasts )
1172 const char *str = strtok_r( NULL, str_delimiters, lasts );
1176 str_error = str_bad_cmdline;
1180 if ( ! strcmp( str, str_on ) ) { game_status &= ~flag_nonewlog; }
1181 else if ( ! strcmp( str, str_off ) ) { game_status |= flag_nonewlog; }
1183 str_error = str_bad_cmdline;
1191 static int CONV cmd_ponder( char **lasts )
1193 const char *str = strtok_r( NULL, str_delimiters, lasts );
1197 str_error = str_bad_cmdline;
1201 if ( ! strcmp( str, str_on ) ) { game_status &= ~flag_noponder; }
1202 else if ( ! strcmp( str, str_off ) )
1204 if ( game_status & ( flag_pondering | flag_puzzling ) )
1206 game_status |= flag_quit_ponder;
1208 game_status |= flag_noponder;
1211 str_error = str_bad_cmdline;
1219 #if ! defined(NO_STDOUT)
1220 static int CONV cmd_stress( char **lasts )
1222 const char *str = strtok_r( NULL, str_delimiters, lasts );
1226 str_error = str_bad_cmdline;
1230 if ( ! strcmp( str, str_on ) ) { game_status &= ~flag_nostress; }
1231 else if ( ! strcmp( str, str_off ) ) { game_status |= flag_nostress; }
1233 str_error = str_bad_cmdline;
1243 #if defined(MINIMUM)
1244 cmd_book( char **lasts )
1246 cmd_book( tree_t * restrict ptree, char **lasts )
1249 const char *str = strtok_r( NULL, str_delimiters, lasts );
1254 str_error = str_bad_cmdline;
1257 if ( ! strcmp( str, str_on ) ) { iret = book_on(); }
1258 else if ( ! strcmp( str, str_off ) ) { iret = book_off(); }
1259 else if ( ! strcmp( str, "narrow" ) ) { game_status |= flag_narrow_book; }
1260 else if ( ! strcmp( str, "wide" ) ) { game_status &= ~flag_narrow_book; }
1261 #if ! defined(MINIMUM)
1262 else if ( ! strcmp( str, "create" ) )
1264 AbortDifficultCommand;
1266 iret = book_create( ptree );
1267 if ( iret < 0 ) { return iret; }
1269 iret = ini_game( ptree, &min_posi_no_handicap, flag_history,
1271 if ( iret < 0 ) { return iret; }
1273 iret = get_elapsed( &time_turn_start );
1277 str_error = str_bad_cmdline;
1285 static int CONV cmd_display( tree_t * restrict ptree, char **lasts )
1287 const char *str = strtok_r( NULL, str_delimiters, lasts );
1294 l = strtol( str, &ptr, 0 );
1297 str_error = str_bad_cmdline;
1300 if ( l == 1 ) { game_status &= ~flag_reverse; }
1301 else if ( l == 2 ) { game_status |= flag_reverse; }
1303 str_error = str_bad_cmdline;
1309 iret = out_board( ptree, stdout, 0, 0 );
1310 if ( iret < 0 ) { return iret; }
1311 #if ! defined(NO_LOGGING)
1312 iret = out_board( ptree, pf_log, 0, 0 );
1313 if ( iret < 0 ) { return iret; }
1321 static int CONV cmd_ping( void )
1323 OutCsaShogi( "pong\n" );
1329 static int CONV cmd_hash( char **lasts )
1331 const char *str = strtok_r( NULL, str_delimiters, lasts );
1337 str_error = str_bad_cmdline;
1341 l = strtol( str, &ptr, 0 );
1342 if ( ptr == str || l == LONG_MAX || l < 1 || l > 31 )
1344 str_error = str_bad_cmdline;
1348 AbortDifficultCommand;
1350 log2_ntrans_table = (int)l;
1351 memory_free( (void *)ptrans_table_orig );
1352 return ini_trans_table();
1356 static int CONV cmd_limit( char **lasts )
1358 const char *str = strtok_r( NULL, str_delimiters, lasts );
1364 str_error = str_bad_cmdline;
1368 AbortDifficultCommand;
1370 if ( ! strcmp( str, "depth" ) )
1372 str = strtok_r( NULL, str_delimiters, lasts );
1375 str_error = str_bad_cmdline;
1378 l1 = strtol( str, &ptr, 0 );
1379 if ( ptr == str || l1 == LONG_MAX || l1 < 1 )
1381 str_error = str_bad_cmdline;
1384 sec_limit_up = UINT_MAX;
1385 node_limit = UINT64_MAX;
1386 depth_limit = (int)l1;
1388 else if ( ! strcmp( str, "nodes" ) )
1390 str = strtok_r( NULL, str_delimiters, lasts );
1393 str_error = str_bad_cmdline;
1396 l1 = strtol( str, &ptr, 0 );
1397 if ( ptr == str || l1 == LONG_MAX || l1 < 1 )
1399 str_error = str_bad_cmdline;
1402 sec_limit_up = UINT_MAX;
1403 depth_limit = PLY_MAX;
1404 node_limit = (uint64_t)l1;
1406 else if ( ! strcmp( str, "time" ) )
1408 str = strtok_r( NULL, str_delimiters, lasts );
1411 str_error = str_bad_cmdline;
1415 if ( ! strcmp( str, "extendable" ) )
1417 game_status |= flag_time_extendable;
1419 else if ( ! strcmp( str, "strict" ) )
1421 game_status &= ~flag_time_extendable;
1424 l1 = strtol( str, &ptr, 0 );
1425 if ( ptr == str || l1 == LONG_MAX || l1 < 0 )
1427 str_error = str_bad_cmdline;
1431 str = strtok_r( NULL, str_delimiters, lasts );
1434 str_error = str_bad_cmdline;
1437 l2 = strtol( str, &ptr, 0 );
1438 if ( ptr == str || l2 == LONG_MAX || l2 < 0 )
1440 str_error = str_bad_cmdline;
1444 str = strtok_r( NULL, str_delimiters, lasts );
1445 if ( ! str ) { l3 = -1; }
1447 l3 = strtol( str, &ptr, 0 );
1448 if ( ptr == str || l3 >= PLY_MAX || l3 < -1 )
1450 str_error = str_bad_cmdline;
1455 if ( ! ( l1 | l2 ) ) { l2 = 1; }
1457 depth_limit = PLY_MAX;
1458 node_limit = UINT64_MAX;
1459 sec_limit = (unsigned int)l1 * 60U;
1460 sec_limit_up = (unsigned int)l2;
1461 if ( l3 == -1 ) { sec_limit_depth = UINT_MAX; }
1462 else { sec_limit_depth = (unsigned int)l3; }
1466 str_error = str_bad_cmdline;
1475 cmd_read( tree_t * restrict ptree, char **lasts )
1477 const char *str1 = strtok_r( NULL, str_delimiters, lasts );
1478 const char *str2 = strtok_r( NULL, str_delimiters, lasts );
1479 const char *str3 = strtok_r( NULL, str_delimiters, lasts );
1480 const char *str_tmp;
1481 FILE *pf_src, *pf_dest;
1482 char str_file[SIZE_FILENAME];
1488 flag = flag_history | flag_rep | flag_detect_hang;
1494 str_error = str_bad_cmdline;
1500 if ( ! strcmp( str2, "t" ) ) { flag |= flag_time; }
1501 else if ( strcmp( str2, "nil" ) )
1503 str_error = str_bad_cmdline;
1510 l = strtol( str3, &ptr, 0 );
1511 if ( ptr == str3 || l == LONG_MAX || l < 1 )
1513 str_error = str_bad_cmdline;
1516 moves = (unsigned int)l - 1U;
1519 AbortDifficultCommand;
1521 if ( ! strcmp( str1, "." ) )
1523 str_tmp = "game.cs_";
1525 #if defined(NO_LOGGING)
1526 strncpy( str_file, "game.csa", SIZE_FILENAME-1 );
1528 snprintf( str_file, SIZE_FILENAME, "%s/game%03d.csa",
1529 str_dir_logs, record_num );
1531 pf_dest = file_open( str_tmp, "w" );
1532 if ( pf_dest == NULL ) { return -2; }
1534 pf_src = file_open( str_file, "r" );
1535 if ( pf_src == NULL )
1537 file_close( pf_dest );
1541 while ( ( c = getc(pf_src) ) != EOF ) { putc( c, pf_dest ); }
1543 iret = file_close( pf_src );
1546 file_close( pf_dest );
1550 iret = file_close( pf_dest );
1551 if ( iret < 0 ) { return iret; }
1557 iret = read_record( ptree, str1, moves, flag );
1558 if ( iret < 0 ) { return iret; }
1560 iret = get_elapsed( &time_turn_start );
1561 if ( iret < 0 ) { return iret; }
1563 if ( str_tmp && remove( str_tmp ) )
1565 out_warning( "remove() failed." );
1573 static int CONV cmd_resign( tree_t * restrict ptree, char **lasts )
1575 const char *str = strtok_r( NULL, str_delimiters, lasts );
1579 if ( str == NULL || *str == 'T' )
1581 AbortDifficultCommand;
1583 if ( game_status & mask_game_end ) { return 1; }
1585 game_status |= flag_resigned;
1586 update_time( root_turn );
1587 out_CSA( ptree, &record_game, MOVE_RESIGN );
1590 l = strtol( str, &ptr, 0 );
1591 if ( ptr == str || l == LONG_MAX || l < MT_CAP_PAWN )
1593 str_error = str_bad_cmdline;
1596 resign_threshold = (int)l;
1603 static int CONV cmd_move( tree_t * restrict ptree, char **lasts )
1605 const char *str = strtok_r( NULL, str_delimiters, lasts );
1611 if ( game_status & mask_game_end )
1613 str_error = str_game_ended;
1617 AbortDifficultCommand;
1621 if ( analyze_mode ) // [HGM] analyze: in analysis mode we cannot set the engine thinking (but perhaps play PV move?)
1623 str_error = str_bad_cmdline;
1627 iret = get_elapsed( &time_turn_start );
1628 if ( iret < 0 ) { return iret; }
1630 return com_turn_start( ptree, 0 );
1633 l = strtol( str, &ptr, 0 );
1634 if ( str != ptr && l != LONG_MAX && l >= 1 && *ptr == '\0' )
1636 if ( analyze_mode ) // [HGM] analyze: in analysis mode we cannot set the engine thinking
1638 str_error = str_bad_cmdline;
1642 for ( i = 0; i < l; i += 1 )
1644 if ( game_status & ( flag_move_now | mask_game_end ) ) { break; }
1646 iret = get_elapsed( &time_turn_start );
1647 if ( iret < 0 ) { return iret; }
1649 iret = com_turn_start( ptree, 0 );
1650 if ( iret < 0 ) { return iret; }
1657 iret = interpret_CSA_move( ptree, &move, str );
1658 if ( iret < 0 ) { return iret; }
1660 iret = get_elapsed( &time_turn_start );
1661 if ( iret < 0 ) { return iret; }
1663 iret = make_move_root( ptree, move,
1664 ( flag_history | flag_time | flag_rep
1665 | flag_detect_hang ) );
1666 if ( iret < 0 ) { return iret; }
1668 str = strtok_r( NULL, str_delimiters, lasts );
1670 } while ( str != NULL );
1672 moves_ignore[0] = MOVE_NA; // [HGM] exclude: exclude list cleared for new position
1673 if ( analyze_mode ) return do_analyze ( ptree ); // [HGM] analyze: analysis should continue after feeding moves
1679 static int CONV cmd_new( tree_t * restrict ptree, char **lasts )
1681 const char *str1 = strtok_r( NULL, str_delimiters, lasts );
1682 const char *str2 = strtok_r( NULL, str_delimiters, lasts );
1683 const min_posi_t *pmp;
1684 min_posi_t min_posi;
1687 AbortDifficultCommand;
1689 start_pos = *lasts; move_ptr = 0; // [HGM] undo: remember start position
1690 moves_ignore[0] = MOVE_NA; // [HGM] exclude: exclude list cleared for new position
1694 strncpy(start_data, str1, 511); // [HGM] undo: remember start position
1695 memset( &min_posi.asquare, empty, nsquare );
1696 min_posi.hand_black = min_posi.hand_white = 0;
1697 iret = read_board_rep1( str1, &min_posi );
1698 if ( iret < 0 ) { return iret; }
1702 if ( ! strcmp( str2, "-" ) ) { min_posi.turn_to_move = white; }
1703 else if ( ! strcmp( str2, "+" ) ) { min_posi.turn_to_move = black; }
1705 str_error = str_bad_cmdline;
1709 else { min_posi.turn_to_move = black; }
1713 else { pmp = &min_posi_no_handicap; }
1715 iret = ini_game( ptree, pmp, flag_history, NULL, NULL );
1716 if ( iret < 0 ) { return iret; }
1718 return get_elapsed( &time_turn_start );
1722 static int CONV cmd_outmove( tree_t * restrict ptree )
1724 const char *str_move;
1726 unsigned int move_list[ MAX_LEGAL_MOVES ];
1729 AbortDifficultCommand;
1731 if ( game_status & mask_game_end )
1733 Out( "NO LEGAL MOVE\n" );
1734 DFPNOut( "NO LEGAL MOVE\n" );
1738 n = gen_legal_moves( ptree, move_list, 0 );
1741 for ( c = i = 0; i < n; i += 1 )
1743 str_move = str_CSA_move(move_list[i]);
1745 if ( i && ( i % 10 ) == 0 )
1747 Out( "%s\n", buffer );
1748 DFPNOut( "%s ", buffer );
1749 memcpy( buffer, str_move, 6 );
1755 memcpy( buffer + c + 1, str_move, 6 );
1759 memcpy( buffer + c, str_move, 6 );
1764 Out( "%s\n", buffer );
1765 DFPNOut( "%s\n", buffer );
1771 static int CONV cmd_problem( tree_t * restrict ptree, char **lasts )
1773 const char *str = strtok_r( NULL, str_delimiters, lasts );
1776 unsigned int nposition;
1782 AbortDifficultCommand;
1787 if ( str != NULL && ! strcmp( str, "mate" ) )
1790 str = strtok_r( NULL, str_delimiters, lasts );
1796 l = strtol( str, &ptr, 0 );
1797 if ( ptr == str || l == LONG_MAX || l < 1 )
1799 str_error = str_bad_cmdline;
1802 nposition = (unsigned int)l;
1804 else { nposition = UINT_MAX; }
1807 iret = record_open( &record_problems, "problem.csa", mode_read, NULL, NULL );
1808 if ( iret < 0 ) { return iret; }
1811 iret = is_mate ? solve_mate_problems( ptree, nposition )
1812 : solve_problems( ptree, nposition );
1814 iret = solve_problems( ptree, nposition );
1819 record_close( &record_problems );
1823 iret = record_close( &record_problems );
1824 if ( iret < 0 ) { return iret; }
1826 return get_elapsed( &time_turn_start );
1830 static int CONV cmd_quit( void )
1832 game_status |= flag_quit;
1837 static int CONV cmd_suspend( void )
1839 if ( game_status & ( flag_pondering | flag_puzzling ) )
1841 game_status |= flag_quit_ponder;
1845 game_status |= flag_suspend;
1850 static int CONV cmd_time( char **lasts )
1852 const char *str = strtok_r( NULL, str_delimiters, lasts );
1857 str_error = str_bad_cmdline;
1860 else if ( ! strcmp( str, "response" ) )
1863 str = strtok_r( NULL, str_delimiters, lasts );
1866 str_error = str_bad_cmdline;
1869 l = strtol( str, &ptr, 0 );
1870 if ( ptr == str || l == LONG_MAX || l < 0 || l > 1000 )
1872 str_error = str_bad_cmdline;
1875 time_response = (unsigned int)l;
1878 else if ( ! strcmp( str, "remain" ) )
1882 str = strtok_r( NULL, str_delimiters, lasts );
1885 str_error = str_bad_cmdline;
1888 l1 = strtol( str, &ptr, 0 );
1889 if ( ptr == str || l1 == LONG_MAX || l1 < 0 )
1891 str_error = str_bad_cmdline;
1895 str = strtok_r( NULL, str_delimiters, lasts );
1898 str_error = str_bad_cmdline;
1901 l2 = strtol( str, &ptr, 0 );
1902 if ( ptr == str || l2 == LONG_MAX || l2 < 0 )
1904 str_error = str_bad_cmdline;
1908 if ( sec_limit_up == UINT_MAX )
1910 str_error = str_bad_cmdline;
1914 return reset_time( (unsigned int)l1, (unsigned int)l2 );
1917 str_error = str_bad_cmdline;
1922 #if !defined(MINIMUM)
1923 /* learn (ini|no-ini) steps games iterations tlp1 tlp2 */
1924 static int CONV cmd_learn( tree_t * restrict ptree, char **lasts )
1926 const char *str1 = strtok_r( NULL, str_delimiters, lasts );
1927 const char *str2 = strtok_r( NULL, str_delimiters, lasts );
1928 const char *str3 = strtok_r( NULL, str_delimiters, lasts );
1929 const char *str4 = strtok_r( NULL, str_delimiters, lasts );
1931 const char *str5 = strtok_r( NULL, str_delimiters, lasts );
1932 const char *str6 = strtok_r( NULL, str_delimiters, lasts );
1936 unsigned int max_games;
1937 int is_ini, nsteps, max_iterations, nworker1, nworker2, iret;
1941 str_error = str_bad_cmdline;
1944 if ( ! strcmp( str1, "ini" ) ) { is_ini = 1; }
1945 else if ( ! strcmp( str1, "no-ini" ) ) { is_ini = 0; }
1947 str_error = str_bad_cmdline;
1951 max_games = UINT_MAX;
1952 max_iterations = INT_MAX;
1953 nworker1 = nworker2 = nsteps = 1;
1957 l = strtol( str2, &ptr, 0 );
1958 if ( ptr == str2 || l == LONG_MAX || l < 1 )
1960 str_error = str_bad_cmdline;
1968 l = strtol( str3, &ptr, 0 );
1969 if ( ptr == str3 || l == LONG_MAX || l == LONG_MIN )
1971 str_error = str_bad_cmdline;
1974 if ( l > 0 ) { max_games = (unsigned int)l; }
1979 l = strtol( str4, &ptr, 0 );
1980 if ( ptr == str4 || l == LONG_MAX || l == LONG_MIN )
1982 str_error = str_bad_cmdline;
1985 if ( l > 0 ) { max_iterations = (int)l; }
1991 l = strtol( str5, &ptr, 0 );
1992 if ( ptr == str5 || l > TLP_MAX_THREADS || l < 1 )
1994 str_error = str_bad_cmdline;
2002 l = strtol( str6, &ptr, 0 );
2003 if ( ptr == str6 || l > TLP_MAX_THREADS || l < 1 )
2005 str_error = str_bad_cmdline;
2012 AbortDifficultCommand;
2014 log2_ntrans_table = 12;
2016 memory_free( (void *)ptrans_table_orig );
2018 iret = ini_trans_table();
2019 if ( iret < 0 ) { return iret; }
2021 iret = learn( ptree, is_ini, nsteps, max_games, max_iterations,
2022 nworker1, nworker2 );
2023 if ( iret < 0 ) { return -1; }
2025 iret = ini_game( ptree, &min_posi_no_handicap, flag_history, NULL, NULL );
2026 if ( iret < 0 ) { return -1; }
2028 iret = get_elapsed( &time_turn_start );
2029 if ( iret < 0 ) { return iret; }
2033 #endif /* MINIMUM */
2037 static int CONV cmd_mpv( tree_t * restrict ptree, char **lasts )
2039 const char *str = strtok_r( NULL, str_delimiters, lasts );
2045 str_error = str_bad_cmdline;
2048 else if ( ! strcmp( str, "num" ) )
2050 str = strtok_r( NULL, str_delimiters, lasts );
2053 str_error = str_bad_cmdline;
2056 l = strtol( str, &ptr, 0 );
2057 if ( ptr == str || l == LONG_MAX || l < 1 || l > MPV_MAX_PV )
2059 str_error = str_bad_cmdline;
2063 AbortDifficultCommand;
2067 if ( analyze_mode ) return do_analyze ( ptree ); // [HGM] analyze: analysis should continue changing num
2071 else if ( ! strcmp( str, "width" ) )
2073 str = strtok_r( NULL, str_delimiters, lasts );
2076 str_error = str_bad_cmdline;
2079 l = strtol( str, &ptr, 0 );
2080 if ( ptr == str || l == LONG_MAX || l < MT_CAP_PAWN )
2082 str_error = str_bad_cmdline;
2086 AbortDifficultCommand;
2090 if ( analyze_mode ) return do_analyze ( ptree ); // [HGM] analyze: analysis should continue after changing width
2095 str_error = str_bad_cmdline;
2102 static int CONV cmd_dfpn( tree_t * restrict ptree, char **lasts )
2104 const char *str = strtok_r( NULL, str_delimiters, lasts );
2108 str_error = str_bad_cmdline;
2111 else if ( ! strcmp( str, "hash" ) )
2116 str = strtok_r( NULL, str_delimiters, lasts );
2119 str_error = str_bad_cmdline;
2122 l = strtol( str, &ptr, 0 );
2123 if ( ptr == str || l == LONG_MAX || l < 1 )
2125 str_error = str_bad_cmdline;
2129 AbortDifficultCommand;
2131 dfpn_hash_log2 = (unsigned int)l;
2132 return dfpn_ini_hash();
2134 else if ( ! strcmp( str, "go" ) )
2136 AbortDifficultCommand;
2138 return dfpn( ptree, root_turn, 1 );
2140 else if ( ! strcmp( str, "connect" ) )
2148 str = strtok_r( NULL, str_delimiters, lasts );
2149 if ( ! str || ! strcmp( str, "." ) ) { str = "127.0.0.1"; }
2150 strncpy( str_addr, str, 255 );
2151 str_addr[255] = '\0';
2153 str = strtok_r( NULL, str_delimiters, lasts );
2154 if ( ! str || ! strcmp( str, "." ) ) { str = "4083"; }
2155 l = strtol( str, &ptr, 0 );
2156 if ( ptr == str || l == LONG_MAX || l < 0 || l > USHRT_MAX )
2158 str_error = str_bad_cmdline;
2163 str = strtok_r( NULL, str_delimiters, lasts );
2164 if ( ! str || ! strcmp( str, "." ) ) { str = "bonanza1"; }
2165 strncpy( str_id, str, 255 );
2168 AbortDifficultCommand;
2170 dfpn_sckt = sckt_connect( str_addr, port );
2171 if ( dfpn_sckt == SCKT_NULL ) { return -2; }
2173 str_buffer_cmdline[0] = '\0';
2174 DFPNOut( "Worker: %s\n", str_id );
2179 str_error = str_bad_cmdline;
2186 static int CONV cmd_thread( char **lasts )
2188 const char *str = strtok_r( NULL, str_delimiters, lasts );
2192 str_error = str_bad_cmdline;
2195 else if ( ! strcmp( str, "num" ) )
2200 str = strtok_r( NULL, str_delimiters, lasts );
2203 str_error = str_bad_cmdline;
2206 l = strtol( str, &ptr, 0 );
2207 if ( ptr == str || l == LONG_MAX || l < 1 || l > TLP_MAX_THREADS )
2209 str_error = str_bad_cmdline;
2217 if ( game_status & ( flag_thinking | flag_pondering | flag_puzzling ) )
2224 str_error = str_bad_cmdline;
2230 #if defined(DFPN_CLIENT)
2231 static int CONV cmd_dfpn_client( tree_t * restrict ptree, char **lasts )
2237 AbortDifficultCommand;
2239 str = strtok_r( NULL, str_delimiters, lasts );
2240 if ( ! str || ! strcmp( str, "." ) ) { str = "127.0.0.1"; }
2241 strncpy( dfpn_client_str_addr, str, 255 );
2242 dfpn_client_str_addr[255] = '\0';
2244 str = strtok_r( NULL, str_delimiters, lasts );
2245 if ( ! str || ! strcmp( str, "." ) ) { str = "4083"; }
2246 dfpn_client_port = strtol( str, &ptr, 0 );
2247 if ( ptr == str || dfpn_client_port == LONG_MAX || dfpn_client_port < 0
2248 || dfpn_client_port > USHRT_MAX )
2250 str_error = str_bad_cmdline;
2254 Out( "DFPN Server: %s %d\n", dfpn_client_str_addr, dfpn_client_port );
2256 iret = ini_game( ptree, &min_posi_no_handicap, flag_history, NULL, NULL );
2257 if ( iret < 0 ) { return iret; }
2259 if ( dfpn_client_sckt == SCKT_NULL )
2261 str_error = "Check network status.";
2265 return get_elapsed( &time_turn_start );
2270 #if defined(CSA_LAN)
2271 static int CONV cmd_connect( tree_t * restrict ptree, char **lasts )
2277 str = strtok_r( NULL, str_delimiters, lasts );
2278 if ( ! str || ! strcmp( str, "." ) ) { str = "gserver.computer-shogi.org"; }
2279 strncpy( client_str_addr, str, 255 );
2280 client_str_addr[255] = '\0';
2282 str = strtok_r( NULL, str_delimiters, lasts );
2283 if ( ! str || ! strcmp( str, "." ) ) { str = "4081"; }
2284 client_port = strtol( str, &ptr, 0 );
2285 if ( ptr == str || client_port == LONG_MAX || client_port < 0
2286 || client_port > USHRT_MAX )
2288 str_error = str_bad_cmdline;
2292 str = strtok_r( NULL, str_delimiters, lasts );
2293 if ( ! str || ! strcmp( str, "." ) ) { str = "bonanza_test"; }
2294 strncpy( client_str_id, str, 255 );
2295 client_str_id[255] = '\0';
2297 str = strtok_r( NULL, " \t", lasts );
2298 if ( ! str || ! strcmp( str, "." ) ) { str = "bonanza_test"; }
2299 strncpy( client_str_pwd, str, 255 );
2300 client_str_pwd[255] = '\0';
2302 str = strtok_r( NULL, str_delimiters, lasts );
2303 if ( ! str || ! strcmp( str, "." ) ) { client_max_game = INT_MAX; }
2305 max_games = strtol( str, &ptr, 0 );
2306 if ( ptr == str || max_games == LONG_MAX || max_games < 1 )
2308 str_error = str_bad_cmdline;
2311 client_max_game = max_games;
2314 AbortDifficultCommand;
2318 return client_next_game( ptree, client_str_addr, (int)client_port );
2322 static int CONV cmd_sendpv( char **lasts )
2324 const char *str = strtok_r( NULL, str_delimiters, lasts );
2328 str_error = str_bad_cmdline;
2332 if ( ! strcmp( str, str_off ) ) { game_status &= ~flag_sendpv; }
2333 else if ( ! strcmp( str, str_on ) ) { game_status |= flag_sendpv; }
2335 str_error = str_bad_cmdline;
2344 #if defined(MNJ_LAN)
2345 /* mnj sd seed addr port name factor stable_depth */
2346 static int CONV cmd_mnj( char **lasts )
2348 char client_str_addr[256];
2349 char client_str_id[256];
2354 int client_port, sd;
2357 str = strtok_r( NULL, str_delimiters, lasts );
2360 str_error = str_bad_cmdline;
2363 l = strtol( str, &ptr, 0 );
2364 if ( ptr == str || l == LONG_MAX || l < 0 )
2366 str_error = str_bad_cmdline;
2372 str = strtok_r( NULL, str_delimiters, lasts );
2375 str_error = str_bad_cmdline;
2378 l = strtol( str, &ptr, 0 );
2379 if ( ptr == str || l == LONG_MAX || l < 0 )
2381 str_error = str_bad_cmdline;
2384 seed = (unsigned int)l;
2387 str = strtok_r( NULL, str_delimiters, lasts );
2388 if ( ! str || ! strcmp( str, "." ) ) { str = "localhost"; }
2389 strncpy( client_str_addr, str, 255 );
2390 client_str_addr[255] = '\0';
2393 str = strtok_r( NULL, str_delimiters, lasts );
2394 if ( ! str || ! strcmp( str, "." ) ) { str = "4082"; }
2395 l = strtol( str, &ptr, 0 );
2396 if ( ptr == str || l == LONG_MAX || l < 0 || l > USHRT_MAX )
2398 str_error = str_bad_cmdline;
2401 client_port = (int)l;
2404 str = strtok_r( NULL, str_delimiters, lasts );
2405 if ( ! str || ! strcmp( str, "." ) ) { str = "bonanza1"; }
2406 strncpy( client_str_id, str, 255 );
2407 client_str_id[255] = '\0';
2409 str = strtok_r( NULL, str_delimiters, lasts );
2410 if ( ! str || ! strcmp( str, "." ) ) { str = "1.0"; }
2411 factor = strtod( str, &ptr );
2412 if ( ptr == str || factor < 0.0 )
2414 str_error = str_bad_cmdline;
2418 str = strtok_r( NULL, str_delimiters, lasts );
2419 if ( ! str || ! strcmp( str, "." ) ) { l = -1; }
2421 l = strtol( str, &ptr, 0 );
2422 if ( ptr == str || l == LONG_MAX )
2424 str_error = str_bad_cmdline;
2428 if ( l <= 0 ) { mnj_depth_stable = INT_MAX; }
2429 else { mnj_depth_stable = (int)l; }
2431 AbortDifficultCommand;
2433 resign_threshold = 65535;
2434 game_status |= ( flag_noponder | flag_noprompt );
2435 if ( mnj_reset_tbl( sd, seed ) < 0 ) { return -1; }
2437 sckt_mnj = sckt_connect( client_str_addr, (int)client_port );
2438 if ( sckt_mnj == SCKT_NULL ) { return -2; }
2440 str_buffer_cmdline[0] = '\0';
2442 Out( "Sending my name %s", client_str_id );
2443 MnjOut( "%s %g final%s\n", client_str_id, factor,
2444 ( mnj_depth_stable == INT_MAX ) ? "" : " stable" );
2446 return cmd_suspend();