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( 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 );
97 int CONV is_move( const char *str )
99 if ( isdigit( (int)str[0] ) && isdigit( (int)str[1] )
100 && isdigit( (int)str[2] ) && isdigit( (int)str[3] )
101 && isupper( (int)str[4] ) && isupper( (int)str[5] )
102 && str[6] == '\0' ) { return 1; }
109 procedure( tree_t * restrict ptree )
112 if ( sckt_csa != SCKT_NULL ) { return proce_csalan( ptree ); }
116 if ( sckt_mnj != SCKT_NULL ) { return proce_mnj( ptree ); }
120 if ( usi_mode != usi_off ) { return proce_usi( ptree ); }
123 return proce_cui( ptree );
126 char *start_pos, start_data[512]; // [HGM] undo: for remembering start position
127 int move_list[1024], move_ptr;
130 #define IF(X) else if(!strcmp(command, X))
132 int myTime, hisTime, movesPerSession, inc, plyNr;
137 { // set white and black times from own and opponent time.
139 if(movesPerSession <= 0) moves = 35; else {
141 while(moves <= 0) moves += movesPerSession;
143 time_limit = (myTime-inc-30)/(moves+2) + inc;
144 time_max_limit = 3*time_limit;
145 if(time_max_limit > myTime - 30)time_max_limit = myTime - 30; // keep 0.3 sec margin, as Bonanza reads the clock infrequently
146 time_limit *= 10; // msec
147 time_max_limit *= 10;
148 Out("# moves=%d, time=%d inc=%d t=%d, max=%d\n", moves, myTime, inc, time_limit, time_max_limit);
152 proce_xboard(char *line, const char *command, tree_t * restrict ptree)
153 { // [HGM] added to make Bonanza.exe a native WinBoard engine
155 static char forceMode = 0;
156 sscanf(line + strlen(command) + 1, "%d", &value);
157 Out("# command = '%s'\n", line);
159 IF("protover") { Out("feature variants=\"shogi\" usermove=1 myname=\"Bonanza " BNZ_VER
160 "\" memory=1 smp=1 debug=1 colors=0 setboard=1 ping=1 done=1\n"); }
161 IF("new") { forceMode = plyNr = 0; SetTimes(); return 0; }
162 IF("easy") { strcpy(line, "ponder off"); return 0; }
163 IF("hard") { strcpy(line, "ponder on"); return 0; }
166 IF("time") { sscanf(line+5, "%d", &myTime); }
167 IF("otim") { sscanf(line+5, "%d", &hisTime); }
168 IF("force") { forceMode = 1; }
169 IF("go") { forceMode = 0; SetTimes(); plyNr++; strcpy(line, "move"); return 0; }
171 IF("cores") { sprintf(line, "tlp num %d", value); return 0; }
172 IF("sd") { sprintf(line, "limit depth %d", value); return 0; }
174 IF("quit") { return 0; }
177 IF("variant") { /* ignore, since it must be Shogi */; }
180 IF("level") { int min, sec; float fsec=0.;
181 if(sscanf(line+6, "%d %d:%d %f", &movesPerSession, &min, &sec, &fsec) != 4)
182 sscanf(line+6, "%d %d %f", &movesPerSession, &min, &fsec);
183 min = 60*min + sec; myTime = hisTime = 100*min; inc = 100 * fsec;
185 IF("usermove") { char fromX=line[9], fromY=line[10], toX=line[11], toY=line[12], promo=line[13];
187 {int i,j;for(i=0;i<81;i+=9){printf("# ");for(j=0;j<9;j++)printf(" %3d", BOARD[i+j]);printf("\n");}}
188 if(forceMode) strcpy(line, "move "), line += 5; else plyNr++, SetTimes();
189 if(fromY == '@') { // drop
190 if(fromX >= 'a') fromX += 'A' - 'a';
191 switch(fromX) { // encode piece
192 case 'P': fromX = pawn; break;
193 case 'L': fromX = lance; break;
194 case 'N': fromX = knight; break;
195 case 'S': fromX = silver; break;
196 case 'G': fromX = gold; break;
197 case 'B': fromX = bishop; break;
198 case 'R': fromX = rook; break;
200 sprintf(line, "00%c%c%s", 'a'+'9'-toX, '1'+'9'-toY, astr_table_piece[(int)fromX]);
202 from = ('9' - fromY)*9 + (fromX - 'a');
203 Out("# from=%d\n",from);
204 sprintf(line, "%c%c%c%c%s", 'a'+'9'-fromX, '1'+'9'-fromY, 'a'+'9'-toX, '1'+'9'-toY,
205 astr_table_piece[abs(BOARD[from]) + (promo == '+' ? promote : 0)]);
212 IF("ping") { Out("pong %d\n", value); }
217 static int CONV proce_cui( tree_t * restrict ptree )
222 token = strtok_r( str_cmdline, str_delimiters, &last );
224 if ( token == NULL || *token == '#' ) { return 1; }
228 if( proce_xboard(str_cmdline, token, ptree) ) return 1; // command already processed
229 Out("# translated command '%s'\n", str_cmdline); // command translated for processing by Bonanza
230 token = strtok_r( str_cmdline, str_delimiters, &last ); // redo parsing
232 if ( ! strcmp( token, "xboard" ) ) { xboard_mode = 1; game_status |= flag_noprompt; return 1; }
235 if ( ! strcmp( token, "undo" ) ) { return cmd_undo( ptree ); } // [HGM] undo
236 if ( is_move( token ) ) { return cmd_usrmove( ptree, token, &last ); }
237 if ( ! strcmp( token, "s" ) ) { return cmd_move_now(); }
238 if ( ! strcmp( token, "beep" ) ) { return cmd_beep( &last); }
239 if ( ! strcmp( token, "book" ) ) { return CmdBook( ptree, &last ); }
240 if ( ! strcmp( token, "display" ) ) { return cmd_display( ptree, &last ); }
241 if ( ! strcmp( token, "hash" ) ) { return cmd_hash( &last ); }
242 if ( ! strcmp( token, "limit" ) ) { return cmd_limit( &last ); }
243 if ( ! strcmp( token, "move" ) ) { return cmd_move( ptree, &last ); }
244 if ( ! strcmp( token, "new" ) ) { return cmd_new( ptree, &last ); }
245 if ( ! strcmp( token, "outmove" ) ) { return cmd_outmove( ptree ); }
246 if ( ! strcmp( token, "peek" ) ) { return cmd_peek( &last ); }
247 if ( ! strcmp( token, "stdout" ) ) { return cmd_stdout( &last ); }
248 if ( ! strcmp( token, "ping" ) ) { return cmd_ping(); }
249 if ( ! strcmp( token, "ponder" ) ) { return cmd_ponder( &last ); }
250 if ( ! strcmp( token, "problem" ) ) { return cmd_problem( ptree, &last ); }
251 if ( ! strcmp( token, "quit" ) ) { return cmd_quit(); }
252 if ( ! strcmp( token, "read" ) ) { return cmd_read( ptree, &last ); }
253 if ( ! strcmp( token, "resign" ) ) { return cmd_resign( ptree, &last ); }
254 if ( ! strcmp( token, "suspend" ) ) { return cmd_suspend(); }
255 if ( ! strcmp( token, "time" ) ) { return cmd_time( &last ); }
256 if ( ! strcmp( token, "newlog" ) ) { return cmd_newlog( &last ); }
258 if ( ! strcmp( token, "connect" ) ) { return cmd_connect( ptree, &last ); }
259 if ( ! strcmp( token, "sendpv" ) ) { return cmd_sendpv( &last ); }
262 if ( ! strcmp( token, "mnj" ) ) { return cmd_mnj( &last ); }
265 if ( ! strcmp( token, "mpv" ) ) { return cmd_mpv( &last ); }
268 if ( ! strcmp( token, "dfpn" ) ) { return cmd_dfpn( ptree, &last ); }
270 #if defined(DFPN_CLIENT)
271 if ( ! strcmp( token, "dfpn_client")) { return cmd_dfpn_client( ptree,
275 if ( ! strcmp( token, "tlp" ) ) { return cmd_thread( &last ); }
277 #if ! defined(NO_STDOUT)
278 if ( ! strcmp( token, "stress" ) ) { return cmd_stress( &last ); }
280 #if ! defined(MINIMUM)
281 if ( ! strcmp( token, "learn" ) ) { return cmd_learn( ptree, &last ); }
284 str_error = str_bad_cmdline;
290 static int CONV proce_csalan( tree_t * restrict ptree )
295 token = strtok_r( str_cmdline, str_delimiters, &last );
297 if ( token == NULL ) { return 1; }
298 if ( *token == ach_turn[client_turn] && is_move( token+1 ) )
303 token = strtok_r( NULL, str_delimiters, &last );
304 if ( token == NULL || *token != 'T' )
306 str_error = str_bad_cmdline;
310 l = strtol( token+1, &ptr, 0 );
311 if ( token+1 == ptr || l == LONG_MAX || l < 1 )
313 str_error = str_bad_cmdline;
317 adjust_time( (unsigned int)l, client_turn );
318 Out( " elapsed: b%u, w%u\n", sec_b_total, sec_w_total );
321 if ( *token == ach_turn[Flip(client_turn)] && is_move( token+1 ) )
323 return cmd_usrmove( ptree, token+1, &last );
325 if ( ! strcmp( token, str_resign ) ) { return cmd_resign( ptree, &last ); }
326 if ( ! strcmp( token, "#WIN" )
327 || ! strcmp( token, "#LOSE" )
328 || ! strcmp( token, "#DRAW" )
329 || ! strcmp( token, "#CHUDAN" ) )
331 if ( game_status & ( flag_thinking | flag_pondering | flag_puzzling ) )
333 game_status |= flag_suspend;
337 if ( sckt_out( sckt_csa, "LOGOUT\n" ) < 0 ) { return -1; }
338 if ( sckt_recv_all( sckt_csa ) < 0 ) { return -1; }
342 if ( client_ngame == client_max_game ) { return cmd_quit(); }
344 return client_next_game( ptree, client_str_addr, (int)client_port );
353 static int CONV proce_mnj( tree_t * restrict ptree )
359 token = strtok_r( str_cmdline, str_delimiters, &last );
360 if ( token == NULL ) { return 1; }
362 if ( ! strcmp( token, "new" ) )
364 iret = cmd_suspend();
365 if ( iret != 1 ) { return iret; }
368 iret = cmd_new( ptree, &last );
369 if ( iret < 0 ) { return iret; }
371 moves_ignore[0] = MOVE_NA;
372 return analyze( ptree );
374 if ( ! strcmp( token, "ignore" ) ) { return cmd_mnjignore( ptree, &last ); }
375 if ( ! strcmp( token, "idle" ) ) { return cmd_suspend(); }
376 if ( ! strcmp( token, "alter" ) ) { return cmd_mnjmove( ptree, &last, 1 ); }
377 if ( ! strcmp( token, "retract" ) )
381 const char *str = strtok_r( NULL, str_delimiters, &last );
384 str_error = str_bad_cmdline;
387 l = strtol( str, &ptr, 0 );
388 if ( ptr == str || (long)NUM_UNMAKE < l )
390 str_error = str_bad_cmdline;
394 return cmd_mnjmove( ptree, &last, (int)l );
396 if ( ! strcmp( token, "move" ) ) { return cmd_mnjmove( ptree, &last, 0 ); }
398 str_error = str_bad_cmdline;
404 cmd_mnjignore( tree_t *restrict ptree, char **lasts )
413 token = strtok_r( NULL, str_delimiters, lasts );
416 str_error = str_bad_cmdline;
419 lid = strtol( token, &ptr, 0 );
420 if ( ptr == token || lid == LONG_MAX || lid < 1 )
422 str_error = str_bad_cmdline;
426 AbortDifficultCommand;
428 for ( i = 0; ; i += 1 )
430 token = strtok_r( NULL, str_delimiters, lasts );
431 if ( token == NULL ) { break; }
433 if ( interpret_CSA_move( ptree, &move, token ) < 0 ) { return -1; }
435 moves_ignore[i] = move;
439 str_error = str_bad_cmdline;
442 mnj_posi_id = (int)lid;
443 moves_ignore[i] = MOVE_NA;
445 return analyze( ptree );
450 cmd_mnjmove( tree_t * restrict ptree, char **lasts, int num_alter )
452 const char *str1 = strtok_r( NULL, str_delimiters, lasts );
453 const char *str2 = strtok_r( NULL, str_delimiters, lasts );
459 if ( sckt_mnj == SCKT_NULL || str1 == NULL || str2 == NULL )
461 str_error = str_bad_cmdline;
465 lid = strtol( str2, &ptr, 0 );
466 if ( ptr == str2 || lid == LONG_MAX || lid < 1 )
468 str_error = str_bad_cmdline;
472 AbortDifficultCommand;
476 iret = unmake_move_root( ptree );
477 if ( iret < 0 ) { return iret; }
482 iret = interpret_CSA_move( ptree, &move, str1 );
483 if ( iret < 0 ) { return iret; }
485 iret = get_elapsed( &time_turn_start );
486 if ( iret < 0 ) { return iret; }
488 mnj_posi_id = (int)lid;
490 iret = make_move_root( ptree, move, ( flag_time | flag_rep
491 | flag_detect_hang ) );
492 if ( iret < 0 ) { return iret; }
494 # if ! defined(NO_STDOUT)
495 iret = out_board( ptree, stdout, 0, 0 );
496 if ( iret < 0 ) { return iret; }
499 moves_ignore[0] = MOVE_NA;
500 return analyze( ptree );
506 cmd_undo( tree_t * restrict ptree )
507 { // [HGM] undo: restart the game, and feed all moves except the last
508 int i, last = move_ptr;
509 char *p = start_data;
510 if( move_ptr <= 0 ) {
511 str_error = "undo past start of game ignored";
515 AbortDifficultCommand;
518 cmd_new( ptree, &p );
519 for(i=0; i<last; i++) {
520 make_move_root( ptree, move_list[i], 0);
527 static int CONV proce_usi( tree_t * restrict ptree )
533 token = strtok_r( str_cmdline, str_delimiters, &lasts );
534 if ( token == NULL ) { return 1; }
536 if ( ! strcmp( token, "usi" ) )
538 USIOut( "id name %s\n", str_myname );
539 USIOut( "id author Kunihito Hoki\n" );
544 if ( ! strcmp( token, "isready" ) )
546 USIOut( "readyok\n", str_myname );
550 if ( ! strcmp( token, "echo" ) )
552 USIOut( "%s\n", lasts );
556 if ( ! strcmp( token, "ignore_moves" ) )
558 return usi_ignore( ptree, &lasts );
561 if ( ! strcmp( token, "genmove_probability" ) )
563 if ( get_elapsed( &time_start ) < 0 ) { return -1; }
564 return usi_root_list( ptree );
567 if ( ! strcmp( token, "go" ) )
569 iret = usi_go( ptree, &lasts );
570 moves_ignore[0] = MOVE_NA;
574 if ( ! strcmp( token, "stop" ) ) { return cmd_move_now(); }
575 if ( ! strcmp( token, "position" ) ) { return usi_posi( ptree, &lasts ); }
576 if ( ! strcmp( token, "quit" ) ) { return cmd_quit(); }
578 str_error = str_bad_cmdline;
584 usi_ignore( tree_t * restrict ptree, char **lasts )
591 AbortDifficultCommand;
593 for ( i = 0; ; i += 1 )
595 token = strtok_r( NULL, str_delimiters, lasts );
596 if ( token == NULL ) { break; }
598 if ( usi2csa( ptree, token, str_buf ) < 0 ) { return -1; }
599 if ( interpret_CSA_move( ptree, &move, str_buf ) < 0 ) { return -1; }
601 moves_ignore[i] = move;
604 moves_ignore[i] = MOVE_NA;
611 usi_go( tree_t * restrict ptree, char **lasts )
618 AbortDifficultCommand;
620 if ( game_status & mask_game_end )
622 str_error = str_game_ended;
626 token = strtok_r( NULL, str_delimiters, lasts );
628 if ( ! strcmp( token, "book" ) )
630 AbortDifficultCommand;
631 if ( usi_book( ptree ) < 0 ) { return -1; }
637 if ( ! strcmp( token, "infinite" ) )
639 usi_byoyomi = UINT_MAX;
640 depth_limit = PLY_MAX;
641 node_limit = UINT64_MAX;
642 sec_limit_depth = UINT_MAX;
644 else if ( ! strcmp( token, "byoyomi" ) )
646 token = strtok_r( NULL, str_delimiters, lasts );
649 str_error = str_bad_cmdline;
653 l = strtol( token, &ptr, 0 );
654 if ( ptr == token || l > UINT_MAX || l < 1 )
656 str_error = str_bad_cmdline;
660 usi_byoyomi = (unsigned int)l;
661 depth_limit = PLY_MAX;
662 node_limit = UINT64_MAX;
663 sec_limit_depth = UINT_MAX;
666 str_error = str_bad_cmdline;
671 if ( get_elapsed( &time_turn_start ) < 0 ) { return -1; }
673 iret = com_turn_start( ptree, 0 );
675 if ( str_error == str_no_legal_move ) { USIOut( "bestmove resign\n" ); }
684 usi_posi( tree_t * restrict ptree, char **lasts )
690 AbortDifficultCommand;
692 moves_ignore[0] = MOVE_NA;
694 token = strtok_r( NULL, str_delimiters, lasts );
695 if ( strcmp( token, "startpos" ) )
697 str_error = str_bad_cmdline;
701 if ( ini_game( ptree, &min_posi_no_handicap,
702 flag_history, NULL, NULL ) < 0 ) { return -1; }
704 token = strtok_r( NULL, str_delimiters, lasts );
705 if ( token == NULL ) { return 1; }
707 if ( strcmp( token, "moves" ) )
709 str_error = str_bad_cmdline;
715 token = strtok_r( NULL, str_delimiters, lasts );
716 if ( token == NULL ) { break; }
718 if ( usi2csa( ptree, token, str_buf ) < 0 ) { return -1; }
719 if ( interpret_CSA_move( ptree, &move, str_buf ) < 0 ) { return -1; }
720 if ( make_move_root( ptree, move, ( flag_history | flag_time
722 | flag_detect_hang ) ) < 0 )
728 if ( get_elapsed( &time_turn_start ) < 0 ) { return -1; }
735 static int CONV cmd_move_now( void )
737 if ( game_status & flag_thinking ) { game_status |= flag_move_now; }
744 cmd_usrmove( tree_t * restrict ptree, const char *str_move, char **lasts )
752 if ( game_status & mask_game_end )
754 str_error = str_game_ended;
758 if ( game_status & flag_thinking )
760 str_error = str_busy_think;
764 str = strtok_r( NULL, str_delimiters, lasts );
765 if ( str == NULL ) { lelapsed = 0; }
769 str_error = str_bad_cmdline;
773 lelapsed = strtol( str, &ptr, 0 );
774 if ( ptr == str || lelapsed == LONG_MAX || lelapsed < 1 )
776 str_error = str_bad_cmdline;
781 if ( game_status & ( flag_pondering | flag_puzzling ) )
785 for ( i = 0; i < ponder_nmove; i++ )
787 if ( ! strcmp( str_move, str_CSA_move(ponder_move_list[i]) ) )
792 if ( i == ponder_nmove )
795 if ( sckt_csa != SCKT_NULL ) { AbortDifficultCommand; }
798 #if defined(CSASHOGI)
799 AbortDifficultCommand;
801 str_error = str_illegal_move;
806 if ( ( game_status & flag_puzzling )
807 || strcmp( str_move, str_CSA_move(ponder_move) ) )
809 ponder_move = MOVE_PONDER_FAILED;
810 game_status |= flag_quit_ponder;
814 iret = update_time( Flip(root_turn) );
815 if ( iret < 0 ) { return iret; }
818 adjust_time( (unsigned int)lelapsed, Flip(root_turn) );
821 out_CSA( ptree, &record_game, ponder_move );
823 game_status &= ~flag_pondering;
824 game_status |= flag_thinking;
828 set_search_limit_time( root_turn );
830 OutCsaShogi( "info ponder end\n" );
832 str = str_time_symple( time_turn_start - time_start );
833 Out( " %6s MOVE PREDICTION HIT\n"
834 " elapsed: b%u, w%u\n", str, sec_b_total, sec_w_total );
839 iret = interpret_CSA_move( ptree, &move, str_move );
840 if ( iret < 0 ) { return iret; }
841 move_evasion_pchk = 0;
842 iret = make_move_root( ptree, move, ( flag_rep | flag_history | flag_time
843 | flag_detect_hang ) );
848 if ( sckt_csa != SCKT_NULL )
850 if ( move_evasion_pchk )
852 str = str_CSA_move( move_evasion_pchk );
853 iret = sckt_out( sckt_csa, "%c%s\n",
854 ach_turn[Flip(root_turn)], str );
855 if ( iret < 0 ) { return iret; }
857 return cmd_suspend();
861 if ( move_evasion_pchk )
863 str = str_CSA_move( move_evasion_pchk );
864 #if defined(CSASHOGI)
865 OutCsaShogi( "move%s\n", str );
866 return cmd_suspend();
868 snprintf( str_message, SIZE_MESSAGE, "perpetual check (%c%s)",
869 ach_turn[Flip(root_turn)], str );
870 str_error = str_message;
878 if ( lelapsed ) { adjust_time( (unsigned int)lelapsed, Flip(root_turn) ); }
879 Out( " elapsed: b%u, w%u\n", sec_b_total, sec_w_total );
882 if ( sckt_csa != SCKT_NULL && ( game_status & flag_mated ) )
884 iret = sckt_out( sckt_csa, "%%TORYO\n" );
885 if ( iret < 0 ) { return iret; }
889 if ( ! ( game_status & mask_game_end ) )
891 iret = com_turn_start( ptree, 0 );
892 if ( iret < 0 ) { return iret; }
899 static int CONV cmd_beep( char **lasts )
901 const char *str = strtok_r( NULL, str_delimiters, lasts );
904 str_error = str_bad_cmdline;
908 if ( ! strcmp( str, str_on ) ) { game_status &= ~flag_nobeep; }
909 else if ( ! strcmp( str, str_off ) ) { game_status |= flag_nobeep; }
911 str_error = str_bad_cmdline;
919 static int CONV cmd_peek( char **lasts )
921 const char *str = strtok_r( NULL, str_delimiters, lasts );
925 str_error = str_bad_cmdline;
929 if ( ! strcmp( str, str_on ) ) { game_status &= ~flag_nopeek; }
930 else if ( ! strcmp( str, str_off ) ) { game_status |= flag_nopeek; }
932 str_error = str_bad_cmdline;
940 static int CONV cmd_stdout( char **lasts )
942 const char *str = strtok_r( NULL, str_delimiters, lasts );
946 str_error = str_bad_cmdline;
950 if ( ! strcmp( str, str_on ) ) { game_status &= ~flag_nostdout; }
951 else if ( ! strcmp( str, str_off ) ) { game_status |= flag_nostdout; }
953 str_error = str_bad_cmdline;
961 static int CONV cmd_newlog( char **lasts )
963 const char *str = strtok_r( NULL, str_delimiters, lasts );
967 str_error = str_bad_cmdline;
971 if ( ! strcmp( str, str_on ) ) { game_status &= ~flag_nonewlog; }
972 else if ( ! strcmp( str, str_off ) ) { game_status |= flag_nonewlog; }
974 str_error = str_bad_cmdline;
982 static int CONV cmd_ponder( char **lasts )
984 const char *str = strtok_r( NULL, str_delimiters, lasts );
988 str_error = str_bad_cmdline;
992 if ( ! strcmp( str, str_on ) ) { game_status &= ~flag_noponder; }
993 else if ( ! strcmp( str, str_off ) )
995 if ( game_status & ( flag_pondering | flag_puzzling ) )
997 game_status |= flag_quit_ponder;
999 game_status |= flag_noponder;
1002 str_error = str_bad_cmdline;
1010 #if ! defined(NO_STDOUT)
1011 static int CONV cmd_stress( char **lasts )
1013 const char *str = strtok_r( NULL, str_delimiters, lasts );
1017 str_error = str_bad_cmdline;
1021 if ( ! strcmp( str, str_on ) ) { game_status &= ~flag_nostress; }
1022 else if ( ! strcmp( str, str_off ) ) { game_status |= flag_nostress; }
1024 str_error = str_bad_cmdline;
1034 #if defined(MINIMUM)
1035 cmd_book( char **lasts )
1037 cmd_book( tree_t * restrict ptree, char **lasts )
1040 const char *str = strtok_r( NULL, str_delimiters, lasts );
1045 str_error = str_bad_cmdline;
1048 if ( ! strcmp( str, str_on ) ) { iret = book_on(); }
1049 else if ( ! strcmp( str, str_off ) ) { iret = book_off(); }
1050 else if ( ! strcmp( str, "narrow" ) ) { game_status |= flag_narrow_book; }
1051 else if ( ! strcmp( str, "wide" ) ) { game_status &= ~flag_narrow_book; }
1052 #if ! defined(MINIMUM)
1053 else if ( ! strcmp( str, "create" ) )
1055 AbortDifficultCommand;
1057 iret = book_create( ptree );
1058 if ( iret < 0 ) { return iret; }
1060 iret = ini_game( ptree, &min_posi_no_handicap, flag_history,
1062 if ( iret < 0 ) { return iret; }
1064 iret = get_elapsed( &time_turn_start );
1068 str_error = str_bad_cmdline;
1076 static int CONV cmd_display( tree_t * restrict ptree, char **lasts )
1078 const char *str = strtok_r( NULL, str_delimiters, lasts );
1085 l = strtol( str, &ptr, 0 );
1088 str_error = str_bad_cmdline;
1091 if ( l == 1 ) { game_status &= ~flag_reverse; }
1092 else if ( l == 2 ) { game_status |= flag_reverse; }
1094 str_error = str_bad_cmdline;
1100 iret = out_board( ptree, stdout, 0, 0 );
1101 if ( iret < 0 ) { return iret; }
1102 #if ! defined(NO_LOGGING)
1103 iret = out_board( ptree, pf_log, 0, 0 );
1104 if ( iret < 0 ) { return iret; }
1112 static int CONV cmd_ping( void )
1114 OutCsaShogi( "pong\n" );
1120 static int CONV cmd_hash( char **lasts )
1122 const char *str = strtok_r( NULL, str_delimiters, lasts );
1128 str_error = str_bad_cmdline;
1132 l = strtol( str, &ptr, 0 );
1133 if ( ptr == str || l == LONG_MAX || l < 1 || l > 31 )
1135 str_error = str_bad_cmdline;
1139 AbortDifficultCommand;
1141 log2_ntrans_table = (int)l;
1142 memory_free( (void *)ptrans_table_orig );
1143 return ini_trans_table();
1147 static int CONV cmd_limit( char **lasts )
1149 const char *str = strtok_r( NULL, str_delimiters, lasts );
1155 str_error = str_bad_cmdline;
1159 AbortDifficultCommand;
1161 if ( ! strcmp( str, "depth" ) )
1163 str = strtok_r( NULL, str_delimiters, lasts );
1166 str_error = str_bad_cmdline;
1169 l1 = strtol( str, &ptr, 0 );
1170 if ( ptr == str || l1 == LONG_MAX || l1 < 1 )
1172 str_error = str_bad_cmdline;
1175 sec_limit_up = UINT_MAX;
1176 node_limit = UINT64_MAX;
1177 depth_limit = (int)l1;
1179 else if ( ! strcmp( str, "nodes" ) )
1181 str = strtok_r( NULL, str_delimiters, lasts );
1184 str_error = str_bad_cmdline;
1187 l1 = strtol( str, &ptr, 0 );
1188 if ( ptr == str || l1 == LONG_MAX || l1 < 1 )
1190 str_error = str_bad_cmdline;
1193 sec_limit_up = UINT_MAX;
1194 depth_limit = PLY_MAX;
1195 node_limit = (uint64_t)l1;
1197 else if ( ! strcmp( str, "time" ) )
1199 str = strtok_r( NULL, str_delimiters, lasts );
1202 str_error = str_bad_cmdline;
1206 if ( ! strcmp( str, "extendable" ) )
1208 game_status |= flag_time_extendable;
1210 else if ( ! strcmp( str, "strict" ) )
1212 game_status &= ~flag_time_extendable;
1215 l1 = strtol( str, &ptr, 0 );
1216 if ( ptr == str || l1 == LONG_MAX || l1 < 0 )
1218 str_error = str_bad_cmdline;
1222 str = strtok_r( NULL, str_delimiters, lasts );
1225 str_error = str_bad_cmdline;
1228 l2 = strtol( str, &ptr, 0 );
1229 if ( ptr == str || l2 == LONG_MAX || l2 < 0 )
1231 str_error = str_bad_cmdline;
1235 str = strtok_r( NULL, str_delimiters, lasts );
1236 if ( ! str ) { l3 = -1; }
1238 l3 = strtol( str, &ptr, 0 );
1239 if ( ptr == str || l3 >= PLY_MAX || l3 < -1 )
1241 str_error = str_bad_cmdline;
1246 if ( ! ( l1 | l2 ) ) { l2 = 1; }
1248 depth_limit = PLY_MAX;
1249 node_limit = UINT64_MAX;
1250 sec_limit = (unsigned int)l1 * 60U;
1251 sec_limit_up = (unsigned int)l2;
1252 if ( l3 == -1 ) { sec_limit_depth = UINT_MAX; }
1253 else { sec_limit_depth = (unsigned int)l3; }
1257 str_error = str_bad_cmdline;
1266 cmd_read( tree_t * restrict ptree, char **lasts )
1268 const char *str1 = strtok_r( NULL, str_delimiters, lasts );
1269 const char *str2 = strtok_r( NULL, str_delimiters, lasts );
1270 const char *str3 = strtok_r( NULL, str_delimiters, lasts );
1271 const char *str_tmp;
1272 FILE *pf_src, *pf_dest;
1273 char str_file[SIZE_FILENAME];
1279 flag = flag_history | flag_rep | flag_detect_hang;
1285 str_error = str_bad_cmdline;
1291 if ( ! strcmp( str2, "t" ) ) { flag |= flag_time; }
1292 else if ( strcmp( str2, "nil" ) )
1294 str_error = str_bad_cmdline;
1301 l = strtol( str3, &ptr, 0 );
1302 if ( ptr == str3 || l == LONG_MAX || l < 1 )
1304 str_error = str_bad_cmdline;
1307 moves = (unsigned int)l - 1U;
1310 AbortDifficultCommand;
1312 if ( ! strcmp( str1, "." ) )
1314 str_tmp = "game.cs_";
1316 #if defined(NO_LOGGING)
1317 strncpy( str_file, "game.csa", SIZE_FILENAME-1 );
1319 snprintf( str_file, SIZE_FILENAME, "%s/game%03d.csa",
1320 str_dir_logs, record_num );
1322 pf_dest = file_open( str_tmp, "w" );
1323 if ( pf_dest == NULL ) { return -2; }
1325 pf_src = file_open( str_file, "r" );
1326 if ( pf_src == NULL )
1328 file_close( pf_dest );
1332 while ( ( c = getc(pf_src) ) != EOF ) { putc( c, pf_dest ); }
1334 iret = file_close( pf_src );
1337 file_close( pf_dest );
1341 iret = file_close( pf_dest );
1342 if ( iret < 0 ) { return iret; }
1348 iret = read_record( ptree, str1, moves, flag );
1349 if ( iret < 0 ) { return iret; }
1351 iret = get_elapsed( &time_turn_start );
1352 if ( iret < 0 ) { return iret; }
1354 if ( str_tmp && remove( str_tmp ) )
1356 out_warning( "remove() failed." );
1364 static int CONV cmd_resign( tree_t * restrict ptree, char **lasts )
1366 const char *str = strtok_r( NULL, str_delimiters, lasts );
1370 if ( str == NULL || *str == 'T' )
1372 AbortDifficultCommand;
1374 if ( game_status & mask_game_end ) { return 1; }
1376 game_status |= flag_resigned;
1377 update_time( root_turn );
1378 out_CSA( ptree, &record_game, MOVE_RESIGN );
1381 l = strtol( str, &ptr, 0 );
1382 if ( ptr == str || l == LONG_MAX || l < MT_CAP_PAWN )
1384 str_error = str_bad_cmdline;
1387 resign_threshold = (int)l;
1394 static int CONV cmd_move( tree_t * restrict ptree, char **lasts )
1396 const char *str = strtok_r( NULL, str_delimiters, lasts );
1402 if ( game_status & mask_game_end )
1404 str_error = str_game_ended;
1408 AbortDifficultCommand;
1412 iret = get_elapsed( &time_turn_start );
1413 if ( iret < 0 ) { return iret; }
1415 return com_turn_start( ptree, 0 );
1418 l = strtol( str, &ptr, 0 );
1419 if ( str != ptr && l != LONG_MAX && l >= 1 && *ptr == '\0' )
1421 for ( i = 0; i < l; i += 1 )
1423 if ( game_status & ( flag_move_now | mask_game_end ) ) { break; }
1425 iret = get_elapsed( &time_turn_start );
1426 if ( iret < 0 ) { return iret; }
1428 iret = com_turn_start( ptree, 0 );
1429 if ( iret < 0 ) { return iret; }
1436 iret = interpret_CSA_move( ptree, &move, str );
1437 if ( iret < 0 ) { return iret; }
1439 iret = get_elapsed( &time_turn_start );
1440 if ( iret < 0 ) { return iret; }
1442 iret = make_move_root( ptree, move,
1443 ( flag_history | flag_time | flag_rep
1444 | flag_detect_hang ) );
1445 if ( iret < 0 ) { return iret; }
1447 str = strtok_r( NULL, str_delimiters, lasts );
1449 } while ( str != NULL );
1455 static int CONV cmd_new( tree_t * restrict ptree, char **lasts )
1457 const char *str1 = strtok_r( NULL, str_delimiters, lasts );
1458 const char *str2 = strtok_r( NULL, str_delimiters, lasts );
1459 const min_posi_t *pmp;
1460 min_posi_t min_posi;
1463 AbortDifficultCommand;
1465 start_pos = *lasts; move_ptr = 0; // [HGM] undo: remember start position
1469 strncpy(start_data, str1, 511); // [HGM] undo: remember start position
1470 memset( &min_posi.asquare, empty, nsquare );
1471 min_posi.hand_black = min_posi.hand_white = 0;
1472 iret = read_board_rep1( str1, &min_posi );
1473 if ( iret < 0 ) { return iret; }
1477 if ( ! strcmp( str2, "-" ) ) { min_posi.turn_to_move = white; }
1478 else if ( ! strcmp( str2, "+" ) ) { min_posi.turn_to_move = black; }
1480 str_error = str_bad_cmdline;
1484 else { min_posi.turn_to_move = black; }
1488 else { pmp = &min_posi_no_handicap; }
1490 iret = ini_game( ptree, pmp, flag_history, NULL, NULL );
1491 if ( iret < 0 ) { return iret; }
1493 return get_elapsed( &time_turn_start );
1497 static int CONV cmd_outmove( tree_t * restrict ptree )
1499 const char *str_move;
1501 unsigned int move_list[ MAX_LEGAL_MOVES ];
1504 AbortDifficultCommand;
1506 if ( game_status & mask_game_end )
1508 Out( "NO LEGAL MOVE\n" );
1509 DFPNOut( "NO LEGAL MOVE\n" );
1513 n = gen_legal_moves( ptree, move_list, 0 );
1516 for ( c = i = 0; i < n; i += 1 )
1518 str_move = str_CSA_move(move_list[i]);
1520 if ( i && ( i % 10 ) == 0 )
1522 Out( "%s\n", buffer );
1523 DFPNOut( "%s ", buffer );
1524 memcpy( buffer, str_move, 6 );
1530 memcpy( buffer + c + 1, str_move, 6 );
1534 memcpy( buffer + c, str_move, 6 );
1539 Out( "%s\n", buffer );
1540 DFPNOut( "%s\n", buffer );
1546 static int CONV cmd_problem( tree_t * restrict ptree, char **lasts )
1548 const char *str = strtok_r( NULL, str_delimiters, lasts );
1551 unsigned int nposition;
1557 AbortDifficultCommand;
1562 if ( str != NULL && ! strcmp( str, "mate" ) )
1565 str = strtok_r( NULL, str_delimiters, lasts );
1571 l = strtol( str, &ptr, 0 );
1572 if ( ptr == str || l == LONG_MAX || l < 1 )
1574 str_error = str_bad_cmdline;
1577 nposition = (unsigned int)l;
1579 else { nposition = UINT_MAX; }
1582 iret = record_open( &record_problems, "problem.csa", mode_read, NULL, NULL );
1583 if ( iret < 0 ) { return iret; }
1586 iret = is_mate ? solve_mate_problems( ptree, nposition )
1587 : solve_problems( ptree, nposition );
1589 iret = solve_problems( ptree, nposition );
1594 record_close( &record_problems );
1598 iret = record_close( &record_problems );
1599 if ( iret < 0 ) { return iret; }
1601 return get_elapsed( &time_turn_start );
1605 static int CONV cmd_quit( void )
1607 game_status |= flag_quit;
1612 static int CONV cmd_suspend( void )
1614 if ( game_status & ( flag_pondering | flag_puzzling ) )
1616 game_status |= flag_quit_ponder;
1620 game_status |= flag_suspend;
1625 static int CONV cmd_time( char **lasts )
1627 const char *str = strtok_r( NULL, str_delimiters, lasts );
1632 str_error = str_bad_cmdline;
1635 else if ( ! strcmp( str, "response" ) )
1638 str = strtok_r( NULL, str_delimiters, lasts );
1641 str_error = str_bad_cmdline;
1644 l = strtol( str, &ptr, 0 );
1645 if ( ptr == str || l == LONG_MAX || l < 0 || l > 1000 )
1647 str_error = str_bad_cmdline;
1650 time_response = (unsigned int)l;
1653 else if ( ! strcmp( str, "remain" ) )
1657 str = strtok_r( NULL, str_delimiters, lasts );
1660 str_error = str_bad_cmdline;
1663 l1 = strtol( str, &ptr, 0 );
1664 if ( ptr == str || l1 == LONG_MAX || l1 < 0 )
1666 str_error = str_bad_cmdline;
1670 str = strtok_r( NULL, str_delimiters, lasts );
1673 str_error = str_bad_cmdline;
1676 l2 = strtol( str, &ptr, 0 );
1677 if ( ptr == str || l2 == LONG_MAX || l2 < 0 )
1679 str_error = str_bad_cmdline;
1683 if ( sec_limit_up == UINT_MAX )
1685 str_error = str_bad_cmdline;
1689 return reset_time( (unsigned int)l1, (unsigned int)l2 );
1692 str_error = str_bad_cmdline;
1697 #if !defined(MINIMUM)
1698 /* learn (ini|no-ini) steps games iterations tlp1 tlp2 */
1699 static int CONV cmd_learn( tree_t * restrict ptree, char **lasts )
1701 const char *str1 = strtok_r( NULL, str_delimiters, lasts );
1702 const char *str2 = strtok_r( NULL, str_delimiters, lasts );
1703 const char *str3 = strtok_r( NULL, str_delimiters, lasts );
1704 const char *str4 = strtok_r( NULL, str_delimiters, lasts );
1706 const char *str5 = strtok_r( NULL, str_delimiters, lasts );
1707 const char *str6 = strtok_r( NULL, str_delimiters, lasts );
1711 unsigned int max_games;
1712 int is_ini, nsteps, max_iterations, nworker1, nworker2, iret;
1716 str_error = str_bad_cmdline;
1719 if ( ! strcmp( str1, "ini" ) ) { is_ini = 1; }
1720 else if ( ! strcmp( str1, "no-ini" ) ) { is_ini = 0; }
1722 str_error = str_bad_cmdline;
1726 max_games = UINT_MAX;
1727 max_iterations = INT_MAX;
1728 nworker1 = nworker2 = nsteps = 1;
1732 l = strtol( str2, &ptr, 0 );
1733 if ( ptr == str2 || l == LONG_MAX || l < 1 )
1735 str_error = str_bad_cmdline;
1743 l = strtol( str3, &ptr, 0 );
1744 if ( ptr == str3 || l == LONG_MAX || l == LONG_MIN )
1746 str_error = str_bad_cmdline;
1749 if ( l > 0 ) { max_games = (unsigned int)l; }
1754 l = strtol( str4, &ptr, 0 );
1755 if ( ptr == str4 || l == LONG_MAX || l == LONG_MIN )
1757 str_error = str_bad_cmdline;
1760 if ( l > 0 ) { max_iterations = (int)l; }
1766 l = strtol( str5, &ptr, 0 );
1767 if ( ptr == str5 || l > TLP_MAX_THREADS || l < 1 )
1769 str_error = str_bad_cmdline;
1777 l = strtol( str6, &ptr, 0 );
1778 if ( ptr == str6 || l > TLP_MAX_THREADS || l < 1 )
1780 str_error = str_bad_cmdline;
1787 AbortDifficultCommand;
1789 log2_ntrans_table = 12;
1791 memory_free( (void *)ptrans_table_orig );
1793 iret = ini_trans_table();
1794 if ( iret < 0 ) { return iret; }
1796 iret = learn( ptree, is_ini, nsteps, max_games, max_iterations,
1797 nworker1, nworker2 );
1798 if ( iret < 0 ) { return -1; }
1800 iret = ini_game( ptree, &min_posi_no_handicap, flag_history, NULL, NULL );
1801 if ( iret < 0 ) { return -1; }
1803 iret = get_elapsed( &time_turn_start );
1804 if ( iret < 0 ) { return iret; }
1808 #endif /* MINIMUM */
1812 static int CONV cmd_mpv( char **lasts )
1814 const char *str = strtok_r( NULL, str_delimiters, lasts );
1820 str_error = str_bad_cmdline;
1823 else if ( ! strcmp( str, "num" ) )
1825 str = strtok_r( NULL, str_delimiters, lasts );
1828 str_error = str_bad_cmdline;
1831 l = strtol( str, &ptr, 0 );
1832 if ( ptr == str || l == LONG_MAX || l < 1 || l > MPV_MAX_PV )
1834 str_error = str_bad_cmdline;
1838 AbortDifficultCommand;
1844 else if ( ! strcmp( str, "width" ) )
1846 str = strtok_r( NULL, str_delimiters, lasts );
1849 str_error = str_bad_cmdline;
1852 l = strtol( str, &ptr, 0 );
1853 if ( ptr == str || l == LONG_MAX || l < MT_CAP_PAWN )
1855 str_error = str_bad_cmdline;
1859 AbortDifficultCommand;
1866 str_error = str_bad_cmdline;
1873 static int CONV cmd_dfpn( tree_t * restrict ptree, char **lasts )
1875 const char *str = strtok_r( NULL, str_delimiters, lasts );
1879 str_error = str_bad_cmdline;
1882 else if ( ! strcmp( str, "hash" ) )
1887 str = strtok_r( NULL, str_delimiters, lasts );
1890 str_error = str_bad_cmdline;
1893 l = strtol( str, &ptr, 0 );
1894 if ( ptr == str || l == LONG_MAX || l < 1 )
1896 str_error = str_bad_cmdline;
1900 AbortDifficultCommand;
1902 dfpn_hash_log2 = (unsigned int)l;
1903 return dfpn_ini_hash();
1905 else if ( ! strcmp( str, "go" ) )
1907 AbortDifficultCommand;
1909 return dfpn( ptree, root_turn, 1 );
1911 else if ( ! strcmp( str, "connect" ) )
1919 str = strtok_r( NULL, str_delimiters, lasts );
1920 if ( ! str || ! strcmp( str, "." ) ) { str = "127.0.0.1"; }
1921 strncpy( str_addr, str, 255 );
1922 str_addr[255] = '\0';
1924 str = strtok_r( NULL, str_delimiters, lasts );
1925 if ( ! str || ! strcmp( str, "." ) ) { str = "4083"; }
1926 l = strtol( str, &ptr, 0 );
1927 if ( ptr == str || l == LONG_MAX || l < 0 || l > USHRT_MAX )
1929 str_error = str_bad_cmdline;
1934 str = strtok_r( NULL, str_delimiters, lasts );
1935 if ( ! str || ! strcmp( str, "." ) ) { str = "bonanza1"; }
1936 strncpy( str_id, str, 255 );
1939 AbortDifficultCommand;
1941 dfpn_sckt = sckt_connect( str_addr, port );
1942 if ( dfpn_sckt == SCKT_NULL ) { return -2; }
1944 str_buffer_cmdline[0] = '\0';
1945 DFPNOut( "Worker: %s\n", str_id );
1950 str_error = str_bad_cmdline;
1957 static int CONV cmd_thread( char **lasts )
1959 const char *str = strtok_r( NULL, str_delimiters, lasts );
1963 str_error = str_bad_cmdline;
1966 else if ( ! strcmp( str, "num" ) )
1971 str = strtok_r( NULL, str_delimiters, lasts );
1974 str_error = str_bad_cmdline;
1977 l = strtol( str, &ptr, 0 );
1978 if ( ptr == str || l == LONG_MAX || l < 1 || l > TLP_MAX_THREADS )
1980 str_error = str_bad_cmdline;
1988 if ( game_status & ( flag_thinking | flag_pondering | flag_puzzling ) )
1995 str_error = str_bad_cmdline;
2001 #if defined(DFPN_CLIENT)
2002 static int CONV cmd_dfpn_client( tree_t * restrict ptree, char **lasts )
2008 AbortDifficultCommand;
2010 str = strtok_r( NULL, str_delimiters, lasts );
2011 if ( ! str || ! strcmp( str, "." ) ) { str = "127.0.0.1"; }
2012 strncpy( dfpn_client_str_addr, str, 255 );
2013 dfpn_client_str_addr[255] = '\0';
2015 str = strtok_r( NULL, str_delimiters, lasts );
2016 if ( ! str || ! strcmp( str, "." ) ) { str = "4083"; }
2017 dfpn_client_port = strtol( str, &ptr, 0 );
2018 if ( ptr == str || dfpn_client_port == LONG_MAX || dfpn_client_port < 0
2019 || dfpn_client_port > USHRT_MAX )
2021 str_error = str_bad_cmdline;
2025 Out( "DFPN Server: %s %d\n", dfpn_client_str_addr, dfpn_client_port );
2027 iret = ini_game( ptree, &min_posi_no_handicap, flag_history, NULL, NULL );
2028 if ( iret < 0 ) { return iret; }
2030 if ( dfpn_client_sckt == SCKT_NULL )
2032 str_error = "Check network status.";
2036 return get_elapsed( &time_turn_start );
2041 #if defined(CSA_LAN)
2042 static int CONV cmd_connect( tree_t * restrict ptree, char **lasts )
2048 str = strtok_r( NULL, str_delimiters, lasts );
2049 if ( ! str || ! strcmp( str, "." ) ) { str = "gserver.computer-shogi.org"; }
2050 strncpy( client_str_addr, str, 255 );
2051 client_str_addr[255] = '\0';
2053 str = strtok_r( NULL, str_delimiters, lasts );
2054 if ( ! str || ! strcmp( str, "." ) ) { str = "4081"; }
2055 client_port = strtol( str, &ptr, 0 );
2056 if ( ptr == str || client_port == LONG_MAX || client_port < 0
2057 || client_port > USHRT_MAX )
2059 str_error = str_bad_cmdline;
2063 str = strtok_r( NULL, str_delimiters, lasts );
2064 if ( ! str || ! strcmp( str, "." ) ) { str = "bonanza_test"; }
2065 strncpy( client_str_id, str, 255 );
2066 client_str_id[255] = '\0';
2068 str = strtok_r( NULL, " \t", lasts );
2069 if ( ! str || ! strcmp( str, "." ) ) { str = "bonanza_test"; }
2070 strncpy( client_str_pwd, str, 255 );
2071 client_str_pwd[255] = '\0';
2073 str = strtok_r( NULL, str_delimiters, lasts );
2074 if ( ! str || ! strcmp( str, "." ) ) { client_max_game = INT_MAX; }
2076 max_games = strtol( str, &ptr, 0 );
2077 if ( ptr == str || max_games == LONG_MAX || max_games < 1 )
2079 str_error = str_bad_cmdline;
2082 client_max_game = max_games;
2085 AbortDifficultCommand;
2089 return client_next_game( ptree, client_str_addr, (int)client_port );
2093 static int CONV cmd_sendpv( char **lasts )
2095 const char *str = strtok_r( NULL, str_delimiters, lasts );
2099 str_error = str_bad_cmdline;
2103 if ( ! strcmp( str, str_off ) ) { game_status &= ~flag_sendpv; }
2104 else if ( ! strcmp( str, str_on ) ) { game_status |= flag_sendpv; }
2106 str_error = str_bad_cmdline;
2115 #if defined(MNJ_LAN)
2116 /* mnj sd seed addr port name factor stable_depth */
2117 static int CONV cmd_mnj( char **lasts )
2119 char client_str_addr[256];
2120 char client_str_id[256];
2125 int client_port, sd;
2128 str = strtok_r( NULL, str_delimiters, lasts );
2131 str_error = str_bad_cmdline;
2134 l = strtol( str, &ptr, 0 );
2135 if ( ptr == str || l == LONG_MAX || l < 0 )
2137 str_error = str_bad_cmdline;
2143 str = strtok_r( NULL, str_delimiters, lasts );
2146 str_error = str_bad_cmdline;
2149 l = strtol( str, &ptr, 0 );
2150 if ( ptr == str || l == LONG_MAX || l < 0 )
2152 str_error = str_bad_cmdline;
2155 seed = (unsigned int)l;
2158 str = strtok_r( NULL, str_delimiters, lasts );
2159 if ( ! str || ! strcmp( str, "." ) ) { str = "localhost"; }
2160 strncpy( client_str_addr, str, 255 );
2161 client_str_addr[255] = '\0';
2164 str = strtok_r( NULL, str_delimiters, lasts );
2165 if ( ! str || ! strcmp( str, "." ) ) { str = "4082"; }
2166 l = strtol( str, &ptr, 0 );
2167 if ( ptr == str || l == LONG_MAX || l < 0 || l > USHRT_MAX )
2169 str_error = str_bad_cmdline;
2172 client_port = (int)l;
2175 str = strtok_r( NULL, str_delimiters, lasts );
2176 if ( ! str || ! strcmp( str, "." ) ) { str = "bonanza1"; }
2177 strncpy( client_str_id, str, 255 );
2178 client_str_id[255] = '\0';
2180 str = strtok_r( NULL, str_delimiters, lasts );
2181 if ( ! str || ! strcmp( str, "." ) ) { str = "1.0"; }
2182 factor = strtod( str, &ptr );
2183 if ( ptr == str || factor < 0.0 )
2185 str_error = str_bad_cmdline;
2189 str = strtok_r( NULL, str_delimiters, lasts );
2190 if ( ! str || ! strcmp( str, "." ) ) { l = -1; }
2192 l = strtol( str, &ptr, 0 );
2193 if ( ptr == str || l == LONG_MAX )
2195 str_error = str_bad_cmdline;
2199 if ( l <= 0 ) { mnj_depth_stable = INT_MAX; }
2200 else { mnj_depth_stable = (int)l; }
2202 AbortDifficultCommand;
2204 resign_threshold = 65535;
2205 game_status |= ( flag_noponder | flag_noprompt );
2206 if ( mnj_reset_tbl( sd, seed ) < 0 ) { return -1; }
2208 sckt_mnj = sckt_connect( client_str_addr, (int)client_port );
2209 if ( sckt_mnj == SCKT_NULL ) { return -2; }
2211 str_buffer_cmdline[0] = '\0';
2213 Out( "Sending my name %s", client_str_id );
2214 MnjOut( "%s %g final%s\n", client_str_id, factor,
2215 ( mnj_depth_stable == INT_MAX ) ? "" : " stable" );
2217 return cmd_suspend();