Implement undo command
[bonanza.git] / proce.c
1 #include <ctype.h>
2 #include <limits.h>
3 #include <math.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include "shogi.h"
8
9 /* unacceptable when the program is thinking, or quit pondering */
10 #define AbortDifficultCommand                                              \
11           if ( game_status & flag_thinking )                               \
12             {                                                              \
13               str_error = str_busy_think;                                  \
14               return -2;                                                   \
15             }                                                              \
16           else if ( game_status & ( flag_pondering | flag_puzzling ) )     \
17             {                                                              \
18               game_status |= flag_quit_ponder;                             \
19               return 2;                                                    \
20             }
21
22 #if defined(MINIMUM)
23 #  define CmdBook(x,y) cmd_book(y);
24 static int CONV cmd_book( char **lasts );
25 #else
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 );
29 #endif
30
31 #if ! defined(NO_STDOUT)
32 static int CONV cmd_stress( char **lasts );
33 #endif
34
35 #if defined(CSA_LAN)
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 );
39 #endif
40
41 #if defined(MNJ_LAN)
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,
46                              int num_alter );
47 #endif
48
49 #if defined(USI)
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 );
54 #endif
55
56 #if defined(TLP)
57 static int CONV cmd_thread( char **lasts );
58 #endif
59
60 #if defined(MPV)
61 static int CONV cmd_mpv( char **lasts );
62 #endif
63
64 #if defined(DFPN)
65 static int CONV cmd_dfpn( tree_t * restrict ptree, char **lasts );
66 #endif
67
68 #if defined(DFPN_CLIENT)
69 static int CONV cmd_dfpn_client( tree_t * restrict ptree, char **lasts );
70 #endif
71
72 static int CONV proce_cui( tree_t * restrict ptree );
73 static int CONV cmd_usrmove( tree_t * restrict ptree, const char *str_move,
74                              char **last );
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
96
97 int CONV is_move( const char *str )
98 {
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; }
103
104   return 0;
105 }
106
107
108 int CONV
109 procedure( tree_t * restrict ptree )
110 {
111 #if defined(CSA_LAN)
112   if ( sckt_csa != SCKT_NULL ) { return proce_csalan( ptree ); }
113 #endif
114
115 #if defined(MNJ_LAN)
116   if ( sckt_mnj != SCKT_NULL ) { return proce_mnj( ptree ); }
117 #endif
118
119 #if defined(USI)
120   if ( usi_mode != usi_off ) { return proce_usi( ptree ); }
121 #endif
122
123   return proce_cui( ptree );
124 }
125
126 char *start_pos, start_data[512]; // [HGM] undo: for remembering start position
127 int move_list[1024], move_ptr;
128
129 #ifdef XBOARD
130 #define IF(X) else if(!strcmp(command, X))
131
132 int myTime, hisTime, movesPerSession, inc, plyNr;
133 char xboard_mode;
134
135 static void
136 SetTimes(void)
137 { // set white and black times from own and opponent time.
138   int moves;
139   if(movesPerSession <= 0) moves = 35; else {
140     moves = - plyNr/2;
141     while(moves <= 0) moves += movesPerSession;
142   }
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);
149 }
150
151 static int
152 proce_xboard(char *line, const char *command, tree_t * restrict ptree)
153 { // [HGM] added to make Bonanza.exe a native WinBoard engine
154   int value = -100000;
155   static char forceMode = 0;
156   sscanf(line + strlen(command) + 1, "%d", &value);
157 Out("# command = '%s'\n", line);
158   if(0) ;
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; }
164   IF("post")     { ; }
165   IF("nopost")   { ; }
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; }
170   IF("memory")   { ; }
171   IF("cores")    { sprintf(line, "tlp num %d", value); return 0; }
172   IF("sd")       { sprintf(line, "limit depth %d", value); return 0; }
173   IF("st")       { ; }
174   IF("quit")     { return 0; }
175   IF("analyze")  { ; }
176   IF("exit")     { ; }
177   IF("variant")  { /* ignore, since it must be Shogi */; }
178   IF("setboard") { ; }
179   IF("option")   { ; }
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;
184                  }
185   IF("usermove") { char fromX=line[9], fromY=line[10], toX=line[11], toY=line[12], promo=line[13];
186                    int from;
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;
199                        }
200                        sprintf(line, "00%c%c%s", 'a'+'9'-toX, '1'+'9'-toY, astr_table_piece[(int)fromX]);
201                    } else {
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)]);
206                    }
207                    plyNr++;
208                    return 0;
209                  }
210   IF("undo")     { ; }
211   IF("remove")   { ; }
212   IF("ping")     { Out("pong %d\n", value); }
213   return 1;
214 }
215 #endif
216
217 static int CONV proce_cui( tree_t * restrict ptree )
218 {
219   const char *token;
220   char *last;
221
222   token = strtok_r( str_cmdline, str_delimiters, &last );
223
224   if ( token == NULL || *token == '#' ) { return 1; }
225 #ifdef XBOARD
226   { 
227     if(xboard_mode) {
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
231     } else
232     if ( ! strcmp( token, "xboard" ) )  { xboard_mode = 1; game_status |= flag_noprompt; return 1; }
233   }
234 #endif
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 ); }
257 #if defined(CSA_LAN)
258   if ( ! strcmp( token, "connect" ) )   { return cmd_connect( ptree, &last ); }
259   if ( ! strcmp( token, "sendpv" ) )    { return cmd_sendpv( &last ); }
260 #endif
261 #if defined(MNJ_LAN)
262   if ( ! strcmp( token, "mnj" ) )       { return cmd_mnj( &last ); }
263 #endif
264 #if defined(MPV)
265   if ( ! strcmp( token, "mpv" ) )       { return cmd_mpv( &last ); }
266 #endif
267 #if defined(DFPN)
268   if ( ! strcmp( token, "dfpn" ) )      { return cmd_dfpn( ptree, &last ); }
269 #endif
270 #if defined(DFPN_CLIENT)
271   if ( ! strcmp( token, "dfpn_client")) { return cmd_dfpn_client( ptree,
272                                                                   &last ); }
273 #endif
274 #if defined(TLP)
275   if ( ! strcmp( token, "tlp" ) )       { return cmd_thread( &last ); }
276 #endif
277 #if ! defined(NO_STDOUT)
278   if ( ! strcmp( token, "stress" ) )    { return cmd_stress( &last ); }
279 #endif
280 #if ! defined(MINIMUM)
281   if ( ! strcmp( token, "learn" ) )     { return cmd_learn( ptree, &last ); }
282 #endif
283
284   str_error = str_bad_cmdline;
285   return -2;
286 }
287
288
289 #if defined(CSA_LAN)
290 static int CONV proce_csalan( tree_t * restrict ptree )
291 {
292   const char *token;
293   char *last;
294
295   token = strtok_r( str_cmdline, str_delimiters, &last );
296     
297   if ( token == NULL ) { return 1; }
298   if ( *token == ach_turn[client_turn] && is_move( token+1 ) )
299     {
300       char *ptr;
301       long l;
302
303       token = strtok_r( NULL, str_delimiters, &last );
304       if ( token == NULL || *token != 'T' )
305         {
306           str_error = str_bad_cmdline;
307           return -1;
308         }
309       
310       l = strtol( token+1, &ptr, 0 );
311       if ( token+1 == ptr || l == LONG_MAX || l < 1 )
312         {
313           str_error = str_bad_cmdline;
314           return -1;
315         }
316
317       adjust_time( (unsigned int)l, client_turn );
318       Out( "  elapsed: b%u, w%u\n", sec_b_total, sec_w_total );
319       return 1;
320     }
321   if ( *token == ach_turn[Flip(client_turn)] && is_move( token+1 ) )
322     {
323       return cmd_usrmove( ptree, token+1, &last );
324     }
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" ) )
330     {
331       if ( game_status & ( flag_thinking | flag_pondering | flag_puzzling ) )
332         {
333           game_status |= flag_suspend;
334           return 2;
335         }
336
337       if ( sckt_out( sckt_csa, "LOGOUT\n" ) < 0 ) { return -1; }
338       if ( sckt_recv_all( sckt_csa )        < 0 ) { return -1; }
339
340       ShutdownAll();
341       
342       if ( client_ngame == client_max_game ) { return cmd_quit(); }
343
344       return client_next_game( ptree, client_str_addr, (int)client_port );
345     }
346   
347   return 1;
348 }
349 #endif
350
351
352 #if defined(MNJ_LAN)
353 static int CONV proce_mnj( tree_t * restrict ptree )
354 {
355   const char *token;
356   char *last;
357   int iret;
358
359   token = strtok_r( str_cmdline, str_delimiters, &last );
360   if ( token == NULL ) { return 1; }
361
362   if ( ! strcmp( token, "new" ) )
363     {
364       iret = cmd_suspend();
365       if ( iret != 1 ) { return iret; }
366
367       mnj_posi_id = 0;
368       iret = cmd_new( ptree, &last );
369       if ( iret < 0 ) { return iret; }
370
371       moves_ignore[0] = MOVE_NA;
372       return analyze( ptree );
373     }
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" ) )
378     {
379       long l;
380       char *ptr;
381       const char *str = strtok_r( NULL, str_delimiters, &last );
382       if ( str == NULL )
383         {
384           str_error = str_bad_cmdline;
385           return -1;
386         }
387       l = strtol( str, &ptr, 0 );
388       if ( ptr == str || (long)NUM_UNMAKE < l )
389         {
390           str_error = str_bad_cmdline;
391           return -1;
392         }
393       
394       return cmd_mnjmove( ptree, &last, (int)l );
395     }
396   if ( ! strcmp( token, "move" ) )  { return cmd_mnjmove( ptree, &last, 0 ); }
397
398   str_error = str_bad_cmdline;
399   return -2;
400 }
401
402
403 static int CONV
404 cmd_mnjignore( tree_t *restrict ptree, char **lasts )
405 {
406   const char *token;
407   char *ptr;
408   int i;
409   unsigned int move;
410   long lid;
411
412
413   token = strtok_r( NULL, str_delimiters, lasts );
414   if ( token == NULL )
415     {
416       str_error = str_bad_cmdline;
417       return -1;
418     }
419   lid = strtol( token, &ptr, 0 );
420   if ( ptr == token || lid == LONG_MAX || lid < 1 )
421     {
422       str_error = str_bad_cmdline;
423       return -1;
424     }
425
426   AbortDifficultCommand;
427
428   for ( i = 0; ; i += 1 )
429     {
430       token = strtok_r( NULL, str_delimiters, lasts );
431       if ( token == NULL ) { break; }
432
433       if ( interpret_CSA_move( ptree, &move, token ) < 0 ) { return -1; }
434
435       moves_ignore[i] = move;
436     }
437   if ( i == 0 )
438     {
439       str_error = str_bad_cmdline;
440       return -1;
441     }
442   mnj_posi_id     = (int)lid;
443   moves_ignore[i] = MOVE_NA;
444
445   return analyze( ptree );
446 }
447
448
449 static int CONV
450 cmd_mnjmove( tree_t * restrict ptree, char **lasts, int num_alter )
451 {
452   const char *str1 = strtok_r( NULL, str_delimiters, lasts );
453   const char *str2 = strtok_r( NULL, str_delimiters, lasts );
454   char *ptr;
455   long lid;
456   unsigned int move;
457   int iret;
458
459   if ( sckt_mnj == SCKT_NULL ||  str1 == NULL || str2 == NULL )
460     {
461       str_error = str_bad_cmdline;
462       return -1;
463     }
464
465   lid = strtol( str2, &ptr, 0 );
466   if ( ptr == str2 || lid == LONG_MAX || lid < 1 )
467     {
468       str_error = str_bad_cmdline;
469       return -1;
470     }
471
472   AbortDifficultCommand;
473  
474   while ( num_alter )
475     {
476       iret = unmake_move_root( ptree );
477       if ( iret < 0 ) { return iret; }
478
479       num_alter -= 1;
480     }
481
482   iret = interpret_CSA_move( ptree, &move, str1 );
483   if ( iret < 0 ) { return iret; }
484     
485   iret = get_elapsed( &time_turn_start );
486   if ( iret < 0 ) { return iret; }
487
488   mnj_posi_id = (int)lid;
489
490   iret = make_move_root( ptree, move, ( flag_time | flag_rep
491                                         | flag_detect_hang ) );
492   if ( iret < 0 ) { return iret; }
493   
494 #  if ! defined(NO_STDOUT)
495   iret = out_board( ptree, stdout, 0, 0 );
496   if ( iret < 0 ) { return iret; }
497 #  endif
498
499   moves_ignore[0] = MOVE_NA;
500   return analyze( ptree );
501 }
502 #endif
503
504
505 static int CONV
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";
512     return -2;
513   }
514
515   AbortDifficultCommand;
516
517   last--;
518   cmd_new( ptree, &p );
519   for(i=0; i<last; i++) {
520     make_move_root( ptree, move_list[i], 0);
521   }
522   return 1;
523 }
524
525
526 #if defined(USI)
527 static int CONV proce_usi( tree_t * restrict ptree )
528 {
529   const char *token;
530   char *lasts;
531   int iret;
532
533   token = strtok_r( str_cmdline, str_delimiters, &lasts );
534   if ( token == NULL ) { return 1; }
535
536   if ( ! strcmp( token, "usi" ) )
537     {
538       USIOut( "id name %s\n", str_myname );
539       USIOut( "id author Kunihito Hoki\n" );
540       USIOut( "usiok\n" );
541       return 1;
542     }
543
544   if ( ! strcmp( token, "isready" ) )
545     {
546       USIOut( "readyok\n", str_myname );
547       return 1;
548     }
549
550   if ( ! strcmp( token, "echo" ) )
551     {
552       USIOut( "%s\n", lasts );
553       return 1;
554     }
555
556   if ( ! strcmp( token, "ignore_moves" ) )
557     {
558       return usi_ignore( ptree, &lasts );
559     }
560
561   if ( ! strcmp( token, "genmove_probability" ) )
562     {
563       if ( get_elapsed( &time_start ) < 0 ) { return -1; }
564       return usi_root_list( ptree );
565     }
566
567   if ( ! strcmp( token, "go" ) )
568     {
569       iret = usi_go( ptree, &lasts );
570       moves_ignore[0] = MOVE_NA;
571       return iret;
572     }
573
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(); }
577   
578   str_error = str_bad_cmdline;
579   return -1;
580 }
581
582
583 static int CONV
584 usi_ignore( tree_t * restrict ptree, char **lasts )
585 {
586   const char *token;
587   char str_buf[7];
588   int i;
589   unsigned int move;
590
591   AbortDifficultCommand;
592
593   for ( i = 0; ; i += 1 )
594     {
595       token = strtok_r( NULL, str_delimiters, lasts );
596       if ( token == NULL ) { break; }
597       
598       if ( usi2csa( ptree, token, str_buf ) < 0 )            { return -1; }
599       if ( interpret_CSA_move( ptree, &move, str_buf ) < 0 ) { return -1; }
600
601       moves_ignore[i] = move;
602     }
603
604   moves_ignore[i] = MOVE_NA;
605
606   return 1;
607 }
608
609
610 static int CONV
611 usi_go( tree_t * restrict ptree, char **lasts )
612 {
613   const char *token;
614   char *ptr;
615   int iret;
616   long l;
617
618   AbortDifficultCommand;
619
620   if ( game_status & mask_game_end )
621     {
622       str_error = str_game_ended;
623       return -1;
624     }
625   
626   token = strtok_r( NULL, str_delimiters, lasts );
627
628   if ( ! strcmp( token, "book" ) )
629     {
630       AbortDifficultCommand;
631       if ( usi_book( ptree ) < 0 ) { return -1; }
632
633       return 1;
634     }
635
636
637   if ( ! strcmp( token, "infinite" ) )
638     {
639       usi_byoyomi     = UINT_MAX;
640       depth_limit     = PLY_MAX;
641       node_limit      = UINT64_MAX;
642       sec_limit_depth = UINT_MAX;
643     }
644   else if ( ! strcmp( token, "byoyomi" ) )
645     {
646       token = strtok_r( NULL, str_delimiters, lasts );
647       if ( token == NULL )
648         {
649           str_error = str_bad_cmdline;
650           return -1;
651         }
652
653       l = strtol( token, &ptr, 0 );
654       if ( ptr == token || l > UINT_MAX || l < 1 )
655         {
656           str_error = str_bad_cmdline;
657           return -1;
658         }
659       
660       usi_byoyomi     = (unsigned int)l;
661       depth_limit     = PLY_MAX;
662       node_limit      = UINT64_MAX;
663       sec_limit_depth = UINT_MAX;
664     }
665   else {
666     str_error = str_bad_cmdline;
667     return -1;
668   }
669
670       
671   if ( get_elapsed( &time_turn_start ) < 0 ) { return -1; }
672
673   iret = com_turn_start( ptree, 0 );
674   if ( iret < 0 ) {
675     if ( str_error == str_no_legal_move ) { USIOut( "bestmove resign\n" ); }
676     else                                  { return -1; }
677   }
678   
679   return 1;
680 }
681
682
683 static int CONV
684 usi_posi( tree_t * restrict ptree, char **lasts )
685 {
686   const char *token;
687   char str_buf[7];
688   unsigned int move;
689     
690   AbortDifficultCommand;
691     
692   moves_ignore[0] = MOVE_NA;
693
694   token = strtok_r( NULL, str_delimiters, lasts );
695   if ( strcmp( token, "startpos" ) )
696     {
697       str_error = str_bad_cmdline;
698       return -1;
699     }
700     
701   if ( ini_game( ptree, &min_posi_no_handicap,
702                  flag_history, NULL, NULL ) < 0 ) { return -1; }
703     
704   token = strtok_r( NULL, str_delimiters, lasts );
705   if ( token == NULL ) { return 1; }
706
707   if ( strcmp( token, "moves" ) )
708     {
709       str_error = str_bad_cmdline;
710       return -1;
711     }
712     
713   for ( ;; )  {
714
715     token = strtok_r( NULL, str_delimiters, lasts );
716     if ( token == NULL ) { break; }
717       
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
721                                         | flag_rep
722                                         | flag_detect_hang ) ) < 0 )
723       {
724         return -1;
725       }
726   }
727     
728   if ( get_elapsed( &time_turn_start ) < 0 ) { return -1; }
729   return 1;
730 }
731
732 #endif
733
734
735 static int CONV cmd_move_now( void )
736 {
737   if ( game_status & flag_thinking ) { game_status |= flag_move_now; }
738
739   return 1;
740 }
741
742
743 static int CONV
744 cmd_usrmove( tree_t * restrict ptree, const char *str_move, char **lasts )
745 {
746   const char *str;
747   char *ptr;
748   long lelapsed;
749   unsigned int move;
750   int iret;
751
752   if ( game_status & mask_game_end )
753     {
754       str_error = str_game_ended;
755       return -2;
756     }
757   
758   if ( game_status & flag_thinking )
759     {
760       str_error = str_busy_think;
761       return -2;
762     }
763
764   str = strtok_r( NULL, str_delimiters, lasts );
765   if ( str == NULL ) { lelapsed = 0; }
766   else {
767     if ( *str != 'T' )
768       {
769         str_error = str_bad_cmdline;
770         return -2;
771       }
772     str += 1;
773     lelapsed = strtol( str, &ptr, 0 );
774     if ( ptr == str || lelapsed == LONG_MAX || lelapsed < 1 )
775       {
776         str_error = str_bad_cmdline;
777         return -2;
778       }
779   }
780
781   if ( game_status & ( flag_pondering | flag_puzzling ) )
782     {
783       int i;
784
785       for ( i = 0; i < ponder_nmove; i++ )
786         {
787           if ( ! strcmp( str_move, str_CSA_move(ponder_move_list[i]) ) )
788             {
789               break;
790             }
791         }
792       if ( i == ponder_nmove )
793         {
794 #if defined(CSA_LAN)
795           if ( sckt_csa != SCKT_NULL ) { AbortDifficultCommand; }
796 #endif
797
798 #if defined(CSASHOGI)
799           AbortDifficultCommand;
800 #else
801           str_error = str_illegal_move;
802           return -2;
803 #endif
804         }
805
806       if ( ( game_status & flag_puzzling )
807            || strcmp( str_move, str_CSA_move(ponder_move) ) )
808         {
809           ponder_move  = MOVE_PONDER_FAILED;
810           game_status |= flag_quit_ponder;
811           return 2;
812         }
813       else {
814         iret = update_time( Flip(root_turn) );
815         if ( iret < 0 ) { return iret; }
816         if ( lelapsed )
817           {
818             adjust_time( (unsigned int)lelapsed, Flip(root_turn) );
819           }
820
821         out_CSA( ptree, &record_game, ponder_move );
822
823         game_status      &= ~flag_pondering;
824         game_status      |= flag_thinking;
825 #ifdef XBOARD
826       if(!xboard_mode)
827 #endif
828         set_search_limit_time( root_turn );
829
830         OutCsaShogi( "info ponder end\n" );
831
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 );
835         return 1;
836       }
837     }
838
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 ) );
844   if ( iret < 0 )
845       {
846
847 #if defined(CSA_LAN)
848         if ( sckt_csa != SCKT_NULL )
849           {
850             if ( move_evasion_pchk )
851               {
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; }
856               }
857             return cmd_suspend();
858           }
859 #endif
860
861         if ( move_evasion_pchk )
862           {
863             str = str_CSA_move( move_evasion_pchk );
864 #if defined(CSASHOGI)
865             OutCsaShogi( "move%s\n", str );
866             return cmd_suspend();
867 #else
868             snprintf( str_message, SIZE_MESSAGE, "perpetual check (%c%s)",
869                       ach_turn[Flip(root_turn)], str );
870             str_error = str_message;
871             return -2;
872 #endif
873           }
874
875         return iret;
876       }
877
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 );
880
881 #if defined(CSA_LAN)
882   if ( sckt_csa != SCKT_NULL && ( game_status & flag_mated ) )
883     {
884       iret = sckt_out( sckt_csa, "%%TORYO\n" );
885       if ( iret < 0 ) { return iret; }
886     }
887 #endif
888
889   if ( ! ( game_status & mask_game_end ) )
890     {
891       iret = com_turn_start( ptree, 0 );
892       if ( iret < 0 ) { return iret; }
893     }
894
895   return 1;
896 }
897
898
899 static int CONV cmd_beep( char **lasts )
900 {
901   const char *str = strtok_r( NULL, str_delimiters, lasts );
902   if ( str == NULL )
903     {
904       str_error = str_bad_cmdline;
905       return -2;
906     }
907
908   if      ( ! strcmp( str, str_on )  ) {  game_status &= ~flag_nobeep; }
909   else if ( ! strcmp( str, str_off ) ) {  game_status |=  flag_nobeep; }
910   else {
911     str_error = str_bad_cmdline;
912     return -2;
913   }
914
915   return 1;
916 }
917
918
919 static int CONV cmd_peek( char **lasts )
920 {
921   const char *str = strtok_r( NULL, str_delimiters, lasts );
922
923   if ( str == NULL )
924     {
925       str_error = str_bad_cmdline;
926       return -2;
927     }
928
929   if      ( ! strcmp( str, str_on )  ) {  game_status &= ~flag_nopeek; }
930   else if ( ! strcmp( str, str_off ) ) {  game_status |=  flag_nopeek; }
931   else {
932     str_error = str_bad_cmdline;
933     return -2;
934   }
935
936   return 1;
937 }
938
939
940 static int CONV cmd_stdout( char **lasts )
941 {
942   const char *str = strtok_r( NULL, str_delimiters, lasts );
943
944   if ( str == NULL )
945     {
946       str_error = str_bad_cmdline;
947       return -2;
948     }
949
950   if      ( ! strcmp( str, str_on )  ) {  game_status &= ~flag_nostdout; }
951   else if ( ! strcmp( str, str_off ) ) {  game_status |=  flag_nostdout; }
952   else {
953     str_error = str_bad_cmdline;
954     return -2;
955   }
956
957   return 1;
958 }
959
960
961 static int CONV cmd_newlog( char **lasts )
962 {
963   const char *str = strtok_r( NULL, str_delimiters, lasts );
964
965   if ( str == NULL )
966     {
967       str_error = str_bad_cmdline;
968       return -2;
969     }
970
971   if      ( ! strcmp( str, str_on )  ) { game_status &= ~flag_nonewlog; }
972   else if ( ! strcmp( str, str_off ) ) { game_status |=  flag_nonewlog; }
973   else {
974     str_error = str_bad_cmdline;
975     return -2;
976   }
977
978   return 1;
979 }
980
981
982 static int CONV cmd_ponder( char **lasts )
983 {
984   const char *str = strtok_r( NULL, str_delimiters, lasts );
985
986   if ( str == NULL )
987     {
988       str_error = str_bad_cmdline;
989       return -2;
990     }
991
992   if      ( ! strcmp( str, str_on )  ) { game_status &= ~flag_noponder; }
993   else if ( ! strcmp( str, str_off ) )
994     {
995       if ( game_status & ( flag_pondering | flag_puzzling ) )
996         {
997           game_status |= flag_quit_ponder;
998         }
999       game_status |= flag_noponder;
1000     }
1001   else {
1002     str_error = str_bad_cmdline;
1003     return -2;
1004   }
1005
1006   return 1;
1007 }
1008
1009
1010 #if ! defined(NO_STDOUT)
1011 static int CONV cmd_stress( char **lasts )
1012 {
1013   const char *str = strtok_r( NULL, str_delimiters, lasts );
1014
1015   if ( str == NULL )
1016     {
1017       str_error = str_bad_cmdline;
1018       return -2;
1019     }
1020
1021   if      ( ! strcmp( str, str_on  ) ) { game_status &= ~flag_nostress; }
1022   else if ( ! strcmp( str, str_off ) ) { game_status |= flag_nostress; }
1023   else {
1024     str_error = str_bad_cmdline;
1025     return -2;
1026   }
1027
1028   return 1;
1029 }
1030 #endif
1031
1032
1033 static int CONV
1034 #if defined(MINIMUM)
1035 cmd_book( char **lasts )
1036 #else
1037 cmd_book( tree_t * restrict ptree, char **lasts )
1038 #endif
1039 {
1040   const char *str = strtok_r( NULL, str_delimiters, lasts );
1041   int iret = 1;
1042
1043   if ( str == NULL )
1044     {
1045       str_error = str_bad_cmdline;
1046       return -2;
1047     }
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" ) )
1054     {
1055       AbortDifficultCommand;
1056
1057       iret = book_create( ptree );
1058       if ( iret < 0 ) { return iret; }
1059
1060       iret = ini_game( ptree, &min_posi_no_handicap, flag_history,
1061                        NULL, NULL );
1062       if ( iret < 0 ) { return iret; }
1063
1064       iret = get_elapsed( &time_turn_start );
1065     }
1066 #endif
1067   else {
1068     str_error = str_bad_cmdline;
1069     iret = -2;
1070   }
1071
1072   return iret;
1073 }
1074
1075
1076 static int CONV cmd_display( tree_t * restrict ptree, char **lasts )
1077 {
1078   const char *str = strtok_r( NULL, str_delimiters, lasts );
1079   char *ptr;
1080   long l;
1081   int iret;
1082
1083   if ( str != NULL )
1084     {
1085       l = strtol( str, &ptr, 0 );
1086       if ( ptr == str )
1087         {
1088           str_error = str_bad_cmdline;
1089           return -2;
1090         }
1091       if      ( l == 1 ) { game_status &= ~flag_reverse; }
1092       else if ( l == 2 ) { game_status |= flag_reverse; }
1093       else {
1094         str_error = str_bad_cmdline;
1095         return -2;
1096       }
1097     }
1098   
1099   Out( "\n" );
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; }
1105 #endif
1106   Out( "\n" );
1107
1108   return 1;
1109 }
1110
1111
1112 static int CONV cmd_ping( void )
1113 {
1114   OutCsaShogi( "pong\n" );
1115   Out( "pong\n" );
1116   return 1;
1117 }
1118
1119
1120 static int CONV cmd_hash( char **lasts )
1121 {
1122   const char *str = strtok_r( NULL, str_delimiters, lasts );
1123   char *ptr;
1124   long l;
1125
1126   if ( str == NULL )
1127     {
1128       str_error = str_bad_cmdline;
1129       return -2;
1130     }
1131
1132   l = strtol( str, &ptr, 0 );
1133   if ( ptr == str || l == LONG_MAX || l < 1 || l > 31 )
1134     {
1135       str_error = str_bad_cmdline;
1136       return -2;
1137     }
1138   
1139   AbortDifficultCommand;
1140   
1141   log2_ntrans_table = (int)l;
1142   memory_free( (void *)ptrans_table_orig );
1143   return ini_trans_table();
1144 }
1145
1146
1147 static int CONV cmd_limit( char **lasts )
1148 {
1149   const char *str = strtok_r( NULL, str_delimiters, lasts );
1150   char *ptr;
1151   long l1, l2, l3;
1152
1153   if ( str == NULL )
1154     {
1155       str_error = str_bad_cmdline;
1156       return -2;
1157     }
1158
1159   AbortDifficultCommand;
1160
1161   if ( ! strcmp( str, "depth" ) )
1162     {
1163       str = strtok_r( NULL, str_delimiters, lasts );
1164       if ( str == NULL )
1165         {
1166           str_error = str_bad_cmdline;
1167           return -2;
1168         }
1169       l1 = strtol( str, &ptr, 0 );
1170       if ( ptr == str || l1 == LONG_MAX || l1 < 1 )
1171         {
1172           str_error = str_bad_cmdline;
1173           return -2;
1174         }
1175       sec_limit_up = UINT_MAX;
1176       node_limit   = UINT64_MAX;
1177       depth_limit  = (int)l1;
1178     }
1179   else if ( ! strcmp( str, "nodes" ) )
1180     {
1181       str = strtok_r( NULL, str_delimiters, lasts );
1182       if ( str == NULL )
1183         {
1184           str_error = str_bad_cmdline;
1185           return -2;
1186         }
1187       l1 = strtol( str, &ptr, 0 );
1188       if ( ptr == str || l1 == LONG_MAX || l1 < 1 )
1189         {
1190           str_error = str_bad_cmdline;
1191           return -2;
1192         }
1193       sec_limit_up = UINT_MAX;
1194       depth_limit  = PLY_MAX;
1195       node_limit   = (uint64_t)l1;
1196     }
1197   else if ( ! strcmp( str, "time" ) )
1198     {
1199       str = strtok_r( NULL, str_delimiters, lasts );
1200       if ( str == NULL )
1201         {
1202           str_error = str_bad_cmdline;
1203           return -2;
1204         }
1205
1206       if ( ! strcmp( str, "extendable" ) )
1207         {
1208           game_status |= flag_time_extendable;
1209         }
1210       else if ( ! strcmp( str, "strict" ) )
1211         {
1212           game_status &= ~flag_time_extendable;
1213         }
1214       else {
1215         l1 = strtol( str, &ptr, 0 );
1216         if ( ptr == str || l1 == LONG_MAX || l1 < 0 )
1217           {
1218             str_error = str_bad_cmdline;
1219             return -2;
1220           }
1221
1222         str = strtok_r( NULL, str_delimiters, lasts );
1223         if ( str == NULL )
1224           {
1225             str_error = str_bad_cmdline;
1226             return -2;
1227           }
1228         l2 = strtol( str, &ptr, 0 );
1229         if ( ptr == str || l2 == LONG_MAX || l2 < 0 )
1230           {
1231             str_error = str_bad_cmdline;
1232             return -2;
1233           }
1234
1235         str = strtok_r( NULL, str_delimiters, lasts );
1236         if ( ! str ) { l3 = -1; }
1237         else {
1238           l3 = strtol( str, &ptr, 0 );
1239           if ( ptr == str || l3 >= PLY_MAX || l3 < -1 )
1240             {
1241               str_error = str_bad_cmdline;
1242               return -2;
1243             }
1244         }
1245
1246         if ( ! ( l1 | l2 ) ) { l2 = 1; }
1247
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; }
1254       }
1255     }
1256   else {
1257     str_error = str_bad_cmdline;
1258     return -2;
1259   }
1260
1261   return 1;
1262 }
1263
1264
1265 static int CONV
1266 cmd_read( tree_t * restrict ptree, char **lasts )
1267 {
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];
1274   char *ptr;
1275   unsigned int moves;
1276   long l;
1277   int iret, flag, c;
1278
1279   flag    = flag_history | flag_rep | flag_detect_hang;
1280   moves   = UINT_MAX;
1281   str_tmp = NULL;
1282
1283   if ( str1 == NULL )
1284     {
1285       str_error = str_bad_cmdline;
1286       return -2;
1287     }
1288
1289   if ( str2 != NULL )
1290     {
1291       if ( ! strcmp( str2, "t" ) ) { flag |= flag_time; }
1292       else if ( strcmp( str2, "nil" ) )
1293         {
1294           str_error = str_bad_cmdline;
1295           return -2;
1296         }
1297     }
1298
1299   if ( str3 != NULL )
1300     {
1301       l = strtol( str3, &ptr, 0 );
1302       if ( ptr == str3 || l == LONG_MAX || l < 1 )
1303         {
1304           str_error = str_bad_cmdline;
1305           return -2;
1306         }
1307       moves = (unsigned int)l - 1U;
1308     }
1309
1310   AbortDifficultCommand;
1311
1312   if ( ! strcmp( str1, "." ) )
1313     {
1314       str_tmp = "game.cs_";
1315
1316 #if defined(NO_LOGGING)
1317       strncpy( str_file, "game.csa", SIZE_FILENAME-1 );
1318 #else
1319       snprintf( str_file, SIZE_FILENAME, "%s/game%03d.csa",
1320                 str_dir_logs, record_num );
1321 #endif
1322       pf_dest = file_open( str_tmp, "w" );
1323       if ( pf_dest == NULL ) { return -2; }
1324
1325       pf_src = file_open( str_file, "r" );
1326       if ( pf_src == NULL )
1327         {
1328           file_close( pf_dest );
1329           return -2;
1330         }
1331
1332       while ( ( c = getc(pf_src) ) != EOF ) { putc( c, pf_dest ); }
1333
1334       iret = file_close( pf_src );
1335       if ( iret < 0 )
1336         {
1337           file_close( pf_dest );
1338           return iret;
1339         }
1340
1341       iret = file_close( pf_dest );
1342       if ( iret < 0 ) { return iret; }
1343
1344       flag |= flag_time;
1345       str1  = str_tmp;
1346     }
1347
1348   iret = read_record( ptree, str1, moves, flag );
1349   if ( iret < 0 ) { return iret; }
1350
1351   iret = get_elapsed( &time_turn_start );
1352   if ( iret < 0 ) { return iret; }
1353
1354   if ( str_tmp && remove( str_tmp ) )
1355     {
1356       out_warning( "remove() failed." );
1357       return -2;
1358     }
1359
1360   return 1;
1361 }
1362
1363
1364 static int CONV cmd_resign( tree_t * restrict ptree, char **lasts )
1365 {
1366   const char *str = strtok_r( NULL, str_delimiters, lasts );
1367   char *ptr;
1368   long l;
1369
1370   if ( str == NULL || *str == 'T' )
1371     {
1372       AbortDifficultCommand;
1373
1374       if ( game_status & mask_game_end ) { return 1; }
1375
1376       game_status |= flag_resigned;
1377       update_time( root_turn );
1378       out_CSA( ptree, &record_game, MOVE_RESIGN );
1379     }
1380   else {
1381     l = strtol( str, &ptr, 0 );
1382     if ( ptr == str || l == LONG_MAX || l < MT_CAP_PAWN )
1383       {
1384         str_error = str_bad_cmdline;
1385         return -2;
1386       }
1387     resign_threshold = (int)l;
1388   }
1389
1390   return 1;
1391 }
1392
1393
1394 static int CONV cmd_move( tree_t * restrict ptree, char **lasts )
1395 {
1396   const char *str = strtok_r( NULL, str_delimiters, lasts );
1397   char *ptr;
1398   long l;
1399   unsigned int move;
1400   int iret, i;
1401
1402   if ( game_status & mask_game_end )
1403     {
1404       str_error = str_game_ended;
1405       return -2;
1406     }
1407   
1408   AbortDifficultCommand;
1409
1410   if ( str == NULL )
1411     {
1412       iret = get_elapsed( &time_turn_start );
1413       if ( iret < 0 ) { return iret; }
1414       
1415       return com_turn_start( ptree, 0 );
1416     }
1417
1418   l = strtol( str, &ptr, 0 );
1419   if ( str != ptr && l != LONG_MAX && l >= 1 && *ptr == '\0' )
1420     {
1421       for ( i = 0; i < l; i += 1 )
1422         {
1423           if ( game_status & ( flag_move_now | mask_game_end ) ) { break; }
1424
1425           iret = get_elapsed( &time_turn_start );
1426           if ( iret < 0 ) { return iret; }
1427         
1428           iret = com_turn_start( ptree, 0 );
1429           if ( iret < 0 ) { return iret; }
1430         }
1431
1432       return 1;
1433     }
1434
1435   do {
1436     iret = interpret_CSA_move( ptree, &move, str );
1437     if ( iret < 0 ) { return iret; }
1438     
1439     iret = get_elapsed( &time_turn_start );
1440     if ( iret < 0 ) { return iret; }
1441     
1442     iret = make_move_root( ptree, move,
1443                            ( flag_history | flag_time | flag_rep
1444                              | flag_detect_hang ) );
1445     if ( iret < 0 ) { return iret; }
1446     
1447     str = strtok_r( NULL, str_delimiters, lasts );
1448
1449   } while ( str != NULL );
1450   
1451   return 1;
1452 }
1453
1454
1455 static int CONV cmd_new( tree_t * restrict ptree, char **lasts )
1456 {
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;
1461   int iret;
1462
1463   AbortDifficultCommand;
1464
1465   start_pos = *lasts; move_ptr = 0; // [HGM] undo: remember start position
1466
1467   if ( str1 != NULL )
1468     {
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; }
1474
1475       if ( str2 != NULL )
1476         {
1477           if      ( ! strcmp( str2, "-" ) ) { min_posi.turn_to_move = white; }
1478           else if ( ! strcmp( str2, "+" ) ) { min_posi.turn_to_move = black; }
1479           else {
1480             str_error = str_bad_cmdline;
1481             return -2;
1482           }
1483         }
1484       else { min_posi.turn_to_move = black; }
1485
1486       pmp = &min_posi;
1487     }
1488   else { pmp = &min_posi_no_handicap; }
1489
1490   iret = ini_game( ptree, pmp, flag_history, NULL, NULL );
1491   if ( iret < 0 ) { return iret; }
1492
1493   return get_elapsed( &time_turn_start );
1494 }
1495
1496
1497 static int CONV cmd_outmove( tree_t * restrict ptree )
1498 {
1499   const char *str_move;
1500   char buffer[256];
1501   unsigned int move_list[ MAX_LEGAL_MOVES ];
1502   int i, c, n;
1503
1504   AbortDifficultCommand;
1505
1506   if ( game_status & mask_game_end )
1507     {
1508       Out( "NO LEGAL MOVE\n" );
1509       DFPNOut( "NO LEGAL MOVE\n" );
1510       return 1;
1511     }
1512
1513   n = gen_legal_moves( ptree, move_list, 0 );
1514
1515   buffer[0]='\0';
1516   for ( c = i = 0; i < n; i += 1 )
1517     {
1518       str_move = str_CSA_move(move_list[i]);
1519
1520       if ( i && ( i % 10 ) == 0 )
1521         {
1522           Out( "%s\n", buffer );
1523           DFPNOut( "%s ", buffer );
1524           memcpy( buffer, str_move, 6 );
1525           c = 6;
1526         }
1527       else if ( i )
1528         {
1529           buffer[c] = ' ';
1530           memcpy( buffer + c + 1, str_move, 6 );
1531           c += 7;
1532         }
1533       else {
1534         memcpy( buffer + c, str_move, 6 );
1535         c += 6;
1536       }
1537       buffer[c] = '\0';
1538     }
1539   Out( "%s\n", buffer );
1540   DFPNOut( "%s\n", buffer );
1541
1542   return 1;
1543 }
1544
1545
1546 static int CONV cmd_problem( tree_t * restrict ptree, char **lasts )
1547 {
1548   const char *str = strtok_r( NULL, str_delimiters, lasts );
1549   char *ptr;
1550   long l;
1551   unsigned int nposition;
1552   int iret;
1553 #if defined(DFPN)
1554   int is_mate;
1555 #endif
1556
1557   AbortDifficultCommand;
1558
1559
1560 #if defined(DFPN)
1561   is_mate = 0;
1562   if ( str != NULL && ! strcmp( str, "mate" ) )
1563     {
1564       is_mate = 1;
1565       str     = strtok_r( NULL, str_delimiters, lasts );
1566     }
1567 #endif
1568
1569   if ( str != NULL )
1570     {
1571       l = strtol( str, &ptr, 0 );
1572       if ( ptr == str || l == LONG_MAX || l < 1 )
1573         {
1574           str_error = str_bad_cmdline;
1575           return -2;
1576         }
1577       nposition = (unsigned int)l;
1578     }
1579   else { nposition = UINT_MAX; }
1580
1581   
1582   iret = record_open( &record_problems, "problem.csa", mode_read, NULL, NULL );
1583   if ( iret < 0 ) { return iret; }
1584
1585 #if defined(DFPN)
1586   iret = is_mate ? solve_mate_problems( ptree, nposition )
1587                  : solve_problems( ptree, nposition );
1588 #else
1589   iret = solve_problems( ptree, nposition );
1590 #endif
1591
1592   if ( iret < 0 )
1593     {
1594       record_close( &record_problems );
1595       return iret;
1596     }
1597
1598   iret = record_close( &record_problems );
1599   if ( iret < 0 ) { return iret; }
1600
1601   return get_elapsed( &time_turn_start );
1602 }
1603
1604
1605 static int CONV cmd_quit( void )
1606 {
1607   game_status |= flag_quit;
1608   return 1;
1609 }
1610
1611
1612 static int CONV cmd_suspend( void )
1613 {
1614   if ( game_status & ( flag_pondering | flag_puzzling ) )
1615     {
1616       game_status |= flag_quit_ponder;
1617       return 2;
1618     }
1619   
1620   game_status |= flag_suspend;
1621   return 1;
1622 }
1623
1624
1625 static int CONV cmd_time( char **lasts )
1626 {
1627   const char *str = strtok_r( NULL, str_delimiters, lasts );
1628   char *ptr;
1629
1630   if ( str == NULL )
1631     {
1632       str_error = str_bad_cmdline;
1633       return -2;
1634     }
1635   else if ( ! strcmp( str, "response" ) )
1636     {
1637       long l;
1638       str = strtok_r( NULL, str_delimiters, lasts );
1639       if ( str == NULL )
1640         {
1641           str_error = str_bad_cmdline;
1642           return -2;
1643         }
1644       l = strtol( str, &ptr, 0 );
1645       if ( ptr == str || l == LONG_MAX || l < 0 || l > 1000 )
1646         {
1647           str_error = str_bad_cmdline;
1648           return -2;
1649         }
1650       time_response = (unsigned int)l;
1651       return 1;
1652     }
1653   else if ( ! strcmp( str, "remain" ) )
1654     {
1655       long l1, l2;
1656       
1657       str = strtok_r( NULL, str_delimiters, lasts );
1658       if ( str == NULL )
1659         {
1660           str_error = str_bad_cmdline;
1661           return -2;
1662         }
1663       l1 = strtol( str, &ptr, 0 );
1664       if ( ptr == str || l1 == LONG_MAX || l1 < 0 )
1665         {
1666           str_error = str_bad_cmdline;
1667           return -2;
1668         }
1669
1670       str = strtok_r( NULL, str_delimiters, lasts );
1671       if ( str == NULL )
1672         {
1673           str_error = str_bad_cmdline;
1674           return -2;
1675         }
1676       l2 = strtol( str, &ptr, 0 );
1677       if ( ptr == str || l2 == LONG_MAX || l2 < 0 )
1678         {
1679           str_error = str_bad_cmdline;
1680           return -2;
1681         }
1682
1683       if ( sec_limit_up == UINT_MAX )
1684         {
1685           str_error = str_bad_cmdline;
1686           return -2;
1687         }
1688
1689       return reset_time( (unsigned int)l1, (unsigned int)l2 );
1690     }
1691
1692   str_error = str_bad_cmdline;
1693   return -2;
1694 }
1695
1696
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 )
1700 {
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 );
1705 #  if defined(TLP)
1706   const char *str5 = strtok_r( NULL, str_delimiters, lasts );
1707   const char *str6 = strtok_r( NULL, str_delimiters, lasts );
1708 #  endif
1709   char *ptr;
1710   long l;
1711   unsigned int max_games;
1712   int is_ini, nsteps, max_iterations, nworker1, nworker2, iret;
1713
1714   if ( str1 == NULL )
1715     {
1716       str_error = str_bad_cmdline;
1717       return -2;
1718     }
1719   if      ( ! strcmp( str1, "ini" ) )    { is_ini = 1; }
1720   else if ( ! strcmp( str1, "no-ini" ) ) { is_ini = 0; }
1721   else {
1722     str_error = str_bad_cmdline;
1723     return -2;
1724   }
1725
1726   max_games      = UINT_MAX;
1727   max_iterations = INT_MAX;
1728   nworker1 = nworker2 = nsteps = 1;
1729
1730   if ( str2 != NULL )
1731     {
1732       l = strtol( str2, &ptr, 0 );
1733       if ( ptr == str2 || l == LONG_MAX || l < 1 )
1734         {
1735           str_error = str_bad_cmdline;
1736           return -2;
1737         }
1738       nsteps = (int)l;
1739     }
1740
1741   if ( str3 != NULL )
1742     {
1743       l = strtol( str3, &ptr, 0 );
1744       if ( ptr == str3 || l == LONG_MAX || l == LONG_MIN )
1745         {
1746           str_error = str_bad_cmdline;
1747           return -2;
1748         }
1749       if ( l > 0 ) { max_games = (unsigned int)l; }
1750     }
1751
1752   if ( str4 != NULL )
1753     {
1754       l = strtol( str4, &ptr, 0 );
1755       if ( ptr == str4 || l == LONG_MAX || l == LONG_MIN )
1756         {
1757           str_error = str_bad_cmdline;
1758           return -2;
1759         }
1760       if ( l > 0 ) { max_iterations = (int)l; }
1761     }
1762
1763 #  if defined(TLP)
1764   if ( str5 != NULL )
1765     {
1766       l = strtol( str5, &ptr, 0 );
1767       if ( ptr == str5 || l > TLP_MAX_THREADS || l < 1 )
1768         {
1769           str_error = str_bad_cmdline;
1770           return -2;
1771         }
1772       nworker1 = (int)l;
1773     }
1774
1775   if ( str6 != NULL )
1776     {
1777       l = strtol( str6, &ptr, 0 );
1778       if ( ptr == str6 || l > TLP_MAX_THREADS || l < 1 )
1779         {
1780           str_error = str_bad_cmdline;
1781           return -2;
1782         }
1783       nworker2 = (int)l;
1784     }
1785 #  endif
1786
1787   AbortDifficultCommand;
1788
1789   log2_ntrans_table = 12;
1790
1791   memory_free( (void *)ptrans_table_orig );
1792
1793   iret = ini_trans_table();
1794   if ( iret < 0 ) { return iret; }
1795
1796   iret = learn( ptree, is_ini, nsteps, max_games, max_iterations,
1797                 nworker1, nworker2 );
1798   if ( iret < 0 ) { return -1; }
1799
1800   iret = ini_game( ptree, &min_posi_no_handicap, flag_history, NULL, NULL );
1801   if ( iret < 0 ) { return -1; }
1802
1803   iret = get_elapsed( &time_turn_start );
1804   if ( iret < 0 ) { return iret; }
1805
1806   return 1;
1807 }
1808 #endif /* MINIMUM */
1809
1810
1811 #if defined(MPV)
1812 static int CONV cmd_mpv( char **lasts )
1813 {
1814   const char *str = strtok_r( NULL, str_delimiters, lasts );
1815   char *ptr;
1816   long l;
1817
1818   if ( str == NULL )
1819     {
1820       str_error = str_bad_cmdline;
1821       return -2;
1822     }
1823   else if ( ! strcmp( str, "num" ) )
1824     {
1825       str = strtok_r( NULL, str_delimiters, lasts );
1826       if ( str == NULL )
1827         {
1828           str_error = str_bad_cmdline;
1829           return -2;
1830         }
1831       l = strtol( str, &ptr, 0 );
1832       if ( ptr == str || l == LONG_MAX || l < 1 || l > MPV_MAX_PV )
1833         {
1834           str_error = str_bad_cmdline;
1835           return -2;
1836         }
1837
1838       AbortDifficultCommand;
1839
1840       mpv_num = (int)l;
1841
1842       return 1;
1843     }
1844   else if ( ! strcmp( str, "width" ) )
1845     {
1846       str = strtok_r( NULL, str_delimiters, lasts );
1847       if ( str == NULL )
1848         {
1849           str_error = str_bad_cmdline;
1850           return -2;
1851         }
1852       l = strtol( str, &ptr, 0 );
1853       if ( ptr == str || l == LONG_MAX || l < MT_CAP_PAWN )
1854         {
1855           str_error = str_bad_cmdline;
1856           return -2;
1857         }
1858
1859       AbortDifficultCommand;
1860
1861       mpv_width = (int)l;
1862
1863       return 1;
1864     }
1865
1866   str_error = str_bad_cmdline;
1867   return -2;
1868 }
1869 #endif
1870
1871
1872 #if defined(DFPN)
1873 static int CONV cmd_dfpn( tree_t * restrict ptree, char **lasts )
1874 {
1875   const char *str = strtok_r( NULL, str_delimiters, lasts );
1876
1877   if ( str == NULL )
1878     {
1879       str_error = str_bad_cmdline;
1880       return -2;
1881     }
1882   else if ( ! strcmp( str, "hash" ) )
1883     {
1884       char *ptr;
1885       long l;
1886
1887       str = strtok_r( NULL, str_delimiters, lasts );
1888       if ( str == NULL )
1889         {
1890           str_error = str_bad_cmdline;
1891           return -2;
1892         }
1893       l = strtol( str, &ptr, 0 );
1894       if ( ptr == str || l == LONG_MAX || l < 1 )
1895         {
1896           str_error = str_bad_cmdline;
1897           return -2;
1898         }
1899
1900       AbortDifficultCommand;
1901
1902       dfpn_hash_log2 = (unsigned int)l;
1903       return dfpn_ini_hash();
1904     }
1905   else if ( ! strcmp( str, "go" ) )
1906     {
1907       AbortDifficultCommand;
1908
1909       return dfpn( ptree, root_turn, 1 );
1910     }
1911   else if ( ! strcmp( str, "connect" ) )
1912     {
1913       char str_addr[256];
1914       char str_id[256];
1915       char *ptr;
1916       long l;
1917       int port;
1918
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';
1923
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 )
1928         {
1929           str_error = str_bad_cmdline;
1930           return -2;
1931         }
1932       port = (int)l;
1933
1934       str = strtok_r( NULL, str_delimiters, lasts );
1935       if ( ! str || ! strcmp( str, "." ) ) { str = "bonanza1"; }
1936       strncpy( str_id, str, 255 );
1937       str_id[255] = '\0';
1938
1939       AbortDifficultCommand;
1940       
1941       dfpn_sckt = sckt_connect( str_addr, port );
1942       if ( dfpn_sckt == SCKT_NULL ) { return -2; }
1943
1944       str_buffer_cmdline[0] = '\0';
1945       DFPNOut( "Worker: %s\n", str_id );
1946
1947       return 1;
1948     }
1949
1950   str_error = str_bad_cmdline;
1951   return -2;
1952 }
1953 #endif
1954
1955
1956 #if defined(TLP)
1957 static int CONV cmd_thread( char **lasts )
1958 {
1959   const char *str = strtok_r( NULL, str_delimiters, lasts );
1960
1961   if ( str == NULL )
1962     {
1963       str_error = str_bad_cmdline;
1964       return -2;
1965     }
1966   else if ( ! strcmp( str, "num" ) )
1967     {
1968       char *ptr;
1969       long l;
1970
1971       str = strtok_r( NULL, str_delimiters, lasts );
1972       if ( str == NULL )
1973         {
1974           str_error = str_bad_cmdline;
1975           return -2;
1976         }
1977       l = strtol( str, &ptr, 0 );
1978       if ( ptr == str || l == LONG_MAX || l < 1 || l > TLP_MAX_THREADS )
1979         {
1980           str_error = str_bad_cmdline;
1981           return -2;
1982         }
1983
1984       TlpEnd();
1985
1986       tlp_max = (int)l;
1987
1988       if ( game_status & ( flag_thinking | flag_pondering | flag_puzzling ) )
1989         {
1990           return tlp_start();
1991         }
1992       return 1;
1993     }
1994
1995   str_error = str_bad_cmdline;
1996   return -2;
1997 }
1998 #endif
1999
2000
2001 #if defined(DFPN_CLIENT)
2002 static int CONV cmd_dfpn_client( tree_t * restrict ptree, char **lasts )
2003 {
2004   const char *str;
2005   char *ptr;
2006   int iret;
2007
2008   AbortDifficultCommand;
2009
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';
2014
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 )
2020     {
2021       str_error = str_bad_cmdline;
2022       return -2;
2023     }
2024
2025   Out( "DFPN Server: %s %d\n", dfpn_client_str_addr, dfpn_client_port );
2026
2027   iret = ini_game( ptree, &min_posi_no_handicap, flag_history, NULL, NULL );
2028   if ( iret < 0 ) { return iret; }
2029
2030   if ( dfpn_client_sckt == SCKT_NULL )
2031     {
2032       str_error = "Check network status.";
2033       return -1;
2034     }
2035
2036   return get_elapsed( &time_turn_start );
2037 }
2038 #endif
2039
2040
2041 #if defined(CSA_LAN)
2042 static int CONV cmd_connect( tree_t * restrict ptree, char **lasts )
2043 {
2044   const char *str;
2045   char *ptr;
2046   long max_games;
2047
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';
2052
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 )
2058     {
2059       str_error = str_bad_cmdline;
2060       return -2;
2061     }
2062
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';
2067
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';
2072
2073   str = strtok_r( NULL, str_delimiters, lasts );
2074   if ( ! str || ! strcmp( str, "." ) ) { client_max_game = INT_MAX; }
2075   else {
2076     max_games = strtol( str, &ptr, 0 );
2077     if ( ptr == str || max_games == LONG_MAX || max_games < 1 )
2078     {
2079       str_error = str_bad_cmdline;
2080       return -2;
2081     }
2082     client_max_game = max_games;
2083   }
2084
2085   AbortDifficultCommand;
2086
2087   client_ngame          = 0;
2088
2089   return client_next_game( ptree, client_str_addr, (int)client_port );
2090 }
2091
2092
2093 static int CONV cmd_sendpv( char **lasts )
2094 {
2095   const char *str = strtok_r( NULL, str_delimiters, lasts );
2096
2097   if ( str == NULL )
2098     {
2099       str_error = str_bad_cmdline;
2100       return -2;
2101     }
2102
2103   if      ( ! strcmp( str, str_off ) ) {  game_status &= ~flag_sendpv; }
2104   else if ( ! strcmp( str, str_on ) )  {  game_status |=  flag_sendpv; }
2105   else {
2106     str_error = str_bad_cmdline;
2107     return -2;
2108   }
2109
2110   return 1;
2111 }
2112 #endif
2113
2114
2115 #if defined(MNJ_LAN)
2116 /* mnj sd seed addr port name factor stable_depth */
2117 static int CONV cmd_mnj( char **lasts )
2118 {
2119   char client_str_addr[256];
2120   char client_str_id[256];
2121   const char *str;
2122   char *ptr;
2123   unsigned int seed;
2124   long l;
2125   int client_port, sd;
2126   double factor;
2127
2128   str = strtok_r( NULL, str_delimiters, lasts );
2129   if ( ! str )
2130     {
2131       str_error = str_bad_cmdline;
2132       return -2;
2133     }
2134   l = strtol( str, &ptr, 0 );
2135   if ( ptr == str || l == LONG_MAX || l < 0 )
2136     {
2137       str_error = str_bad_cmdline;
2138       return -2;
2139     }
2140   sd = (int)l;
2141
2142
2143   str = strtok_r( NULL, str_delimiters, lasts );
2144   if ( ! str )
2145     {
2146       str_error = str_bad_cmdline;
2147       return -2;
2148     }
2149   l = strtol( str, &ptr, 0 );
2150   if ( ptr == str || l == LONG_MAX || l < 0 )
2151     {
2152       str_error = str_bad_cmdline;
2153       return -2;
2154     }
2155   seed = (unsigned int)l;
2156
2157
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';
2162
2163
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 )
2168     {
2169       str_error = str_bad_cmdline;
2170       return -2;
2171     }
2172   client_port = (int)l;
2173
2174
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';
2179
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 )
2184     {
2185       str_error = str_bad_cmdline;
2186       return -2;
2187     }
2188
2189   str = strtok_r( NULL, str_delimiters, lasts );
2190   if ( ! str || ! strcmp( str, "." ) ) { l = -1; }
2191   else {
2192     l = strtol( str, &ptr, 0 );
2193     if ( ptr == str || l == LONG_MAX )
2194       {
2195         str_error = str_bad_cmdline;
2196         return -2;
2197       }
2198   }
2199   if ( l <= 0 ) { mnj_depth_stable = INT_MAX; }
2200   else          { mnj_depth_stable = (int)l; }
2201
2202   AbortDifficultCommand;
2203
2204   resign_threshold  = 65535;
2205   game_status      |= ( flag_noponder | flag_noprompt );
2206   if ( mnj_reset_tbl( sd, seed ) < 0 ) { return -1; }
2207
2208   sckt_mnj = sckt_connect( client_str_addr, (int)client_port );
2209   if ( sckt_mnj == SCKT_NULL ) { return -2; }
2210
2211   str_buffer_cmdline[0] = '\0';
2212
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" );
2216
2217   return cmd_suspend();
2218 }
2219 #endif
2220
2221