Force iteration to start at 1 in analyze mode
[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 cmd_book( char **lasts );
25 #else
26 #  define CmdBook(x,y) cmd_book(x,y);
27 static int cmd_learn( tree_t * restrict ptree, char **lasts );
28 static int cmd_book( tree_t * restrict ptree, char **lasts );
29 #endif
30
31 #if ! defined(NO_STDOUT)
32 static int cmd_stress( char **lasts );
33 #endif
34
35 #if defined(DEKUNOBOU)
36 static int cmd_dek( char **lasts );
37 #endif
38
39 #if defined(CSA_LAN)
40 static int proce_csalan( tree_t * restrict ptree );
41 static int cmd_connect( tree_t * restrict ptree, char **lasts );
42 #endif
43
44 #if defined(MNJ_LAN)
45 static int proce_mnj( tree_t * restrict ptree );
46 static int cmd_mnj( tree_t * restrict ptree, char **lasts );
47 static int cmd_mnjmove( tree_t * restrict ptree, char **lasts, int is_alter );
48 #endif
49
50 #if defined(TLP)
51 static int cmd_thread( char **lasts );
52 #endif
53
54 #if defined(MPV)
55 static int cmd_mpv( tree_t * restrict ptree, char **lasts );
56 #endif
57
58 static int proce_cui( tree_t * restrict ptree );
59 static int cmd_usrmove( tree_t * restrict ptree, const char *str_move,
60                         char **last );
61 static int cmd_move_now( void );
62 static int cmd_ponder( char **lasts );
63 static int cmd_limit( char **lasts );
64 static int cmd_quit( void );
65 static int cmd_beep( char **lasts );
66 static int cmd_peek( char **lasts );
67 static int cmd_hash( char **lasts );
68 static int cmd_ping( void );
69 static int cmd_suspend( void );
70 static int cmd_problem( tree_t * restrict ptree, char **lasts );
71 static int cmd_display( tree_t * restrict ptree, char **lasts );
72 static int cmd_move( tree_t * restrict ptree, char **lasts );
73 static int cmd_new( tree_t * restrict ptree, char **lasts );
74 static int cmd_read( tree_t * restrict ptree, char **lasts );
75 static int cmd_resign( tree_t * restrict ptree, char **lasts );
76 static int cmd_time( char **lasts );
77 static int cmd_undo( tree_t * restrict ptree );    // [HGM] undo
78 static int cmd_analyze( tree_t * restrict ptree ); // [HGM] analyze
79 static int cmd_exit( void );
80 static int is_move( const char *str );
81
82
83 int
84 procedure( tree_t * restrict ptree )
85 {
86 #if defined(CSA_LAN)
87   if ( sckt_csa != SCKT_NULL ) { return proce_csalan( ptree ); }
88 #endif
89 #if defined(MNJ_LAN)
90   if ( sckt_mnj != SCKT_NULL ) { return proce_mnj( ptree ); }
91 #endif
92
93   return proce_cui( ptree );
94 }
95
96 char *start_pos, start_data[512]; // [HGM] undo: for remembering start position
97 int move_list[1024], move_ptr;
98 char analyze_mode;
99 int exclude_list[MAX_LEGAL_MOVES], all_moves[MAX_LEGAL_MOVES];
100
101 #ifdef XBOARD
102 #define IF(X) else if(!strcmp(command, X))
103
104 int myTime, hisTime, movesPerSession, inc, plyNr;
105 char xboard_mode;
106
107 int
108 xboard_to_CSA( tree_t * restrict ptree, char *in, char *out, int status )
109 {
110   char fromX=in[0], fromY=in[1], toX=in[2], toY=in[3], promo=in[4];
111   int piece=0, bonanza_move=0;
112   if(fromY == '@') { // drop (contains all info needed to convert it)
113     if(fromX >= 'a') fromX += 'A' - 'a';
114     switch(fromX) { // encode piece
115       case 'P': piece = pawn;   break;
116       case 'L': piece = lance;  break;
117       case 'N': piece = knight; break;
118       case 'S': piece = silver; break;
119       case 'G': piece = gold;   break;
120       case 'B': piece = bishop; break;
121       case 'R': piece = rook;   break;
122     }
123     bonanza_move = Drop2Move(piece) | To2Move( ('9' - toY)*9 + (toX - 'a') );
124     sprintf(out, "00%c%c%s", 'a'+'9'-toX, '1'+'9'-toY, astr_table_piece[piece]);
125   } else { // board move (need to figure out moved piece)
126     int from = ('9' - fromY)*9 + (fromX - 'a');
127     int flag = (promo == '+' ? FLAG_PROMO : 0);
128     piece = abs( BOARD[from] ); // this only works when not searching!
129 printf("# piece from board: %d\n", piece);fflush(stdout);
130     if( status & ( flag_pondering | flag_puzzling | flag_thinking ) ) {
131       int i, to = ('9' - toY)*9 + (toX - 'a'), l = (status == (flag_pondering | flag_thinking));
132       piece = 0; // kludge to force illegal CSA move
133       for( i = 0; l ? all_moves[i] : i < root_nmove; i++ ) { // determine the piece from the move list
134         int move = (l ? all_moves[i] : root_move_list[i].move);
135         if( I2To(move) != to ) continue;
136         if( I2From(move) != from ) continue;
137         if( (move & FLAG_PROMO) != flag ) continue;
138         piece = I2PieceMove( move ); // we found the move; take the piece from it
139         bonanza_move = move;
140         break;
141       }
142 printf("# piece corrected to %d\n", piece);fflush(stdout);
143     }
144     if( promo ) piece += promote;
145     sprintf(out, "%c%c%c%c%s", 'a'+'9'-fromX, '1'+'9'-fromY, 'a'+'9'-toX, '1'+'9'-toY, astr_table_piece[piece]);
146   }
147   return bonanza_move;
148 }
149
150 static void
151 update_exclude_list( tree_t * restrict ptree, int add, char *move_str )
152 { // [HGM] exclude: manage list of excluded moves
153   char dummy[20];
154   int i;
155   if( ! exclude_list[0] ) { // nothing is excluded yet; make a copy of root move list;
156     for( i = 0; i < root_nmove; i++) all_moves[i] = root_move_list[i].move;
157     all_moves[i] = 0;
158   }
159   if ( ! strcmp(move_str, "all") ) { // all moves
160     if( add ) { // copy entire list of legal moves
161       for( i = 0; i < root_nmove; i++ ) exclude_list[i] = all_moves[i];
162     } else exclude_list[0] = 0; // clear list
163   } else { // single move
164     int move = xboard_to_CSA( ptree, move_str, dummy, flag_pondering | flag_thinking); // kludge with impossible flag
165     for( i = 0; exclude_list[i]; i++ ) if( move == exclude_list[i] ) break;
166     if( add ) { // we must add the move
167       if( exclude_list[i] ) return; // but it was already in list
168       if( i >= MAX_LEGAL_MOVES - 2 ) return; // overflow
169       exclude_list[i] = move; exclude_list[i+1] = 0; // append move
170     } else { // we must delete the move
171       if( exclude_list[i] == 0 ) return; // but it was not there
172       while( (exclude_list[i] = exclude_list[i+1]) ) i++; // squeeze it out
173     }
174   }
175 }
176
177 static void
178 SetTimes(void)
179 { // set white and black times from own and opponent time.
180   int moves;
181   if(movesPerSession <= 0) moves = 35; else {
182     moves = - plyNr/2;
183     while(moves <= 0) moves += movesPerSession;
184   }
185   time_limit = (myTime-inc-30)/(moves+2) + inc;
186   time_max_limit = 3*time_limit;
187   if(time_max_limit > myTime - 30)time_max_limit = myTime - 30; // keep 0.3 sec margin, as Bonanza reads the clock infrequently
188   time_limit *= 10; // msec
189   time_max_limit *= 10;
190 Out("# moves=%d, time=%d inc=%d t=%d, max=%d\n", moves, myTime, inc, time_limit, time_max_limit);
191 }
192
193 static int
194 proce_xboard(char *line, const char *command, tree_t * restrict ptree)
195 { // [HGM] added to make Bonanza.exe a native WinBoard engine
196   int value = -100000;
197   static char forceMode = 0;
198   sscanf(line + strlen(command) + 1, "%d", &value);
199 Out("# command = '%s'\n", line);
200   if(0) ;
201   IF("protover") {
202 #if defined(MPV)
203                    Out("feature option=\"MultiPV -spin 1 1 100\"\n");
204                    Out("feature option=\"centi-Pawn margin -spin 200 0 25000\"\n");
205 #endif
206                    Out("feature variants=\"shogi\" usermove=1 myname=\"Bonanza " BNZ_VER
207                        "\" memory=1 smp=1 debug=1 colors=0 setboard=1 ping=1 sigint=0 exclude=1 done=1\n");
208                  }
209   IF("new")      { forceMode = plyNr = 0; SetTimes(); return 0; }
210   IF("easy")     { strcpy(line, "ponder off"); return 0; }
211   IF("hard")     { strcpy(line, "ponder on");  return 0; }
212   IF("post")     { ; }
213   IF("nopost")   { ; }
214   IF("time")     { sscanf(line+5, "%d", &myTime); }
215   IF("otim")     { sscanf(line+5, "%d", &hisTime); }
216   IF("force")    { forceMode = 1; }
217   IF("go")       { forceMode = 0; SetTimes(); plyNr++; strcpy(line, "move"); return 0; }
218   IF("memory")   { ; }
219   IF("cores")    { sprintf(line, "tlp num %d", value); return 0; }
220   IF("sd")       { sprintf(line, "limit depth %d", value); return 0; }
221   IF("st")       { ; }
222   IF("quit")     { return 0; }
223   IF("analyze")  { return 0; }
224   IF("exit")     { return 0; }
225   IF("variant")  { /* ignore, since it must be Shogi */; }
226   IF("setboard") { ; }
227   IF("option")   {
228                    if(sscanf(line+7, "MultiPV=%d", &value) == 1) { sprintf(line, "mpv num %d", value); return 0; }
229                    if(sscanf(line+7, "centi-Pawn margin=%d", &value) == 1) { sprintf(line, "mpv width %d", value); return 0; }
230                  }
231   IF("level")    { int min, sec; float fsec=0.;
232                    if(sscanf(line+6, "%d %d:%d %f", &movesPerSession, &min, &sec, &fsec) != 4)
233                       sscanf(line+6, "%d %d %f", &movesPerSession, &min, &fsec);
234                    min = 60*min + sec; myTime = hisTime = 100*min; inc = 100 * fsec;
235                  }
236   IF("usermove") { char buf[20];
237                    xboard_to_CSA( ptree, line+9, buf, game_status );
238                    if(forceMode || analyze_mode) strcpy(line, "move "), line += 5; else plyNr++, SetTimes();
239                    strcpy( line, buf );
240                    plyNr++;
241                    return 0;
242                  }
243   IF("undo")     { return 0; }
244   IF("remove")   { ; }
245   IF("ping")     { Out("pong %d\n", value); }
246   IF("exclude")  { update_exclude_list( ptree, 1, line+8 ); strcpy(line, "analyze"); return 0; }
247   IF("include")  { update_exclude_list( ptree, 0, line+8 ); strcpy(line, "analyze"); return 0; }
248   return 1;
249 }
250 #endif
251
252 static int
253 proce_cui( tree_t * restrict ptree )
254 {
255   const char *token;
256   char *last;
257
258   token = strtok_r( str_cmdline, str_delimiters, &last );
259
260   if ( token == NULL || *token == '#' ) { return 1; }
261 #ifdef XBOARD
262   { 
263     if(xboard_mode) {
264       if( proce_xboard(str_cmdline, token, ptree) )  return 1; // command already processed
265       Out("# translated command '%s'\n", str_cmdline);         // command translated for processing by Bonanza
266       token = strtok_r( str_cmdline, str_delimiters, &last );  // redo parsing
267     } else
268     if ( ! strcmp( token, "xboard" ) )  { xboard_mode = 1; game_status |= flag_noprompt; return 1; }
269   }
270 #endif
271   if ( ! strcmp( token, "analyze" ) )   { return cmd_analyze( ptree ); } // [HGM] analyze
272   if ( ! strcmp( token, "exit" ) )      { return cmd_exit(); }           // [HGM] analyze
273   if ( ! strcmp( token, "move" ) )      { return cmd_move( ptree, &last ); }
274   if ( ! strcmp( token, "undo" ) )      { return cmd_undo( ptree ); }    // [HGM] undo
275 #if defined(MPV)
276   if ( ! strcmp( token, "mpv" ) )       { return cmd_mpv( ptree, &last ); }
277 #endif
278   analyze_mode = 0; // [HGM] analyze: all other commands terminate analysis
279   if ( is_move( token ) ) { return cmd_usrmove( ptree, token, &last ); }
280   if ( ! strcmp( token, "s" ) )         { return cmd_move_now(); }
281   if ( ! strcmp( token, "beep" ) )      { return cmd_beep( &last); }
282   if ( ! strcmp( token, "book" ) )      { return CmdBook( ptree, &last ); }
283   if ( ! strcmp( token, "display" ) )   { return cmd_display( ptree, &last ); }
284   if ( ! strcmp( token, "hash" ) )      { return cmd_hash( &last ); }
285   if ( ! strcmp( token, "limit" ) )     { return cmd_limit( &last ); }
286   if ( ! strcmp( token, "new" ) )       { return cmd_new( ptree, &last ); }
287   if ( ! strcmp( token, "peek" ) )      { return cmd_peek( &last ); }
288   if ( ! strcmp( token, "ping" ) )      { return cmd_ping(); }
289   if ( ! strcmp( token, "ponder" ) )    { return cmd_ponder( &last ); }
290   if ( ! strcmp( token, "problem" ) )   { return cmd_problem( ptree, &last ); }
291   if ( ! strcmp( token, "quit" ) )      { return cmd_quit(); }
292   if ( ! strcmp( token, "read" ) )      { return cmd_read( ptree, &last ); }
293   if ( ! strcmp( token, "resign" ) )    { return cmd_resign( ptree, &last ); }
294   if ( ! strcmp( token, "suspend" ) )   { return cmd_suspend(); }
295   if ( ! strcmp( token, "time" ) )      { return cmd_time( &last ); }
296 #if defined(CSA_LAN)
297   if ( ! strcmp( token, "connect" ) )   { return cmd_connect( ptree, &last ); }
298 #endif
299 #if defined(MNJ_LAN)
300   if ( ! strcmp( token, "mnj" ) )       { return cmd_mnj( ptree, &last ); }
301 #endif
302 #if defined(DEKUNOBOU)
303   if ( ! strcmp( token, "dekunobou" ) ) { return cmd_dek( &last ); }
304 #endif
305 #if defined(TLP)
306   if ( ! strcmp( token, "tlp" ) )       { return cmd_thread( &last ); }
307 #endif
308 #if ! defined(NO_STDOUT)
309   if ( ! strcmp( token, "stress" ) )    { return cmd_stress( &last ); }
310 #endif
311 #if ! defined(MINIMUM)
312   if ( ! strcmp( token, "learn" ) )     { return cmd_learn( ptree, &last ); }
313 #endif
314
315   str_error = str_bad_cmdline;
316   return -2;
317 }
318
319
320 #if defined(CSA_LAN)
321 static int
322 proce_csalan( tree_t * restrict ptree )
323 {
324   const char *token;
325   char *last;
326
327   token = strtok_r( str_cmdline, str_delimiters, &last );
328     
329   if ( token == NULL ) { return 1; }
330   if ( *token == ach_turn[client_turn] && is_move( token+1 ) )
331     {
332       char *ptr;
333       long l;
334
335       token = strtok_r( NULL, str_delimiters, &last );
336       if ( token == NULL || *token != 'T' )
337         {
338           str_error = str_bad_cmdline;
339           return -1;
340         }
341       
342       l = strtol( token+1, &ptr, 0 );
343       if ( token+1 == ptr || l == LONG_MAX || l < 1 )
344         {
345           str_error = str_bad_cmdline;
346           return -1;
347         }
348
349       adjust_time( (unsigned int)l, client_turn );
350       Out( "  elapsed: b%u, w%u\n", sec_b_total, sec_w_total );
351       return 1;
352     }
353   if ( *token == ach_turn[Flip(client_turn)] && is_move( token+1 ) )
354     {
355       return cmd_usrmove( ptree, token+1, &last );
356     }
357   if ( ! strcmp( token, str_resign ) ) { return cmd_resign( ptree, &last ); }
358   if ( ! strcmp( token, "#WIN" )
359        || ! strcmp( token, "#LOSE" )
360        || ! strcmp( token, "#DRAW" )
361        || ! strcmp( token, "#CHUDAN" ) )
362     {
363       if ( game_status & ( flag_thinking | flag_pondering | flag_puzzling ) )
364         {
365           game_status |= flag_suspend;
366           return 2;
367         }
368       
369       ShutdownClient;
370       
371       if ( client_ngame == client_max_game ) { return cmd_quit(); }
372
373       return client_next_game( ptree, client_str_addr, (int)client_port );
374     }
375   
376   return 1;
377 }
378 #endif
379
380
381 #if defined(MNJ_LAN)
382 static int
383 proce_mnj( tree_t * restrict ptree )
384 {
385   const char *token;
386   char *last;
387   int iret;
388
389   token = strtok_r( str_cmdline, str_delimiters, &last );
390   if ( token == NULL ) { return 1; }
391
392   if ( ! strcmp( token, "new" ) )
393     {
394       iret = cmd_suspend();
395       if ( iret != 1 ) { return iret; }
396
397       mnj_posi_id = 0;
398       iret = cmd_new( ptree, &last );
399       if ( iret < 0 ) { return iret; }
400
401       return analyze( ptree );
402     }
403   if ( ! strcmp( token, "idle" ) )  { return cmd_suspend(); }
404   if ( ! strcmp( token, "alter" ) ) { return cmd_mnjmove( ptree, &last, 1 ); }
405   if ( ! strcmp( token, "move" ) )  { return cmd_mnjmove( ptree, &last, 0 ); }
406
407   str_error = str_bad_cmdline;
408   return -2;
409 }
410
411
412 static int
413 cmd_mnjmove( tree_t * restrict ptree, char **lasts, int is_alter )
414 {
415   const char *str1 = strtok_r( NULL, str_delimiters, lasts );
416   const char *str2 = strtok_r( NULL, str_delimiters, lasts );
417   char *ptr;
418   long lid;
419   unsigned int move;
420   int iret;
421
422   if ( sckt_mnj == SCKT_NULL ||  str1 == NULL || str2 == NULL )
423     {
424       str_error = str_bad_cmdline;
425       return -1;
426     }
427
428   lid = strtol( str2, &ptr, 0 );
429   if ( ptr == str2 || lid == LONG_MAX || lid < 1 )
430     {
431       str_error = str_bad_cmdline;
432       return -1;
433     }
434
435   AbortDifficultCommand;
436  
437   if ( is_alter ) { unmake_move_root( ptree, mnj_move_last ); };
438
439   iret = interpret_CSA_move( ptree, &move, str1 );
440   if ( iret < 0 ) { return iret; }
441     
442   iret = get_elapsed( &time_turn_start );
443   if ( iret < 0 ) { return iret; }
444
445   mnj_posi_id   = (int)lid;
446   mnj_move_last = move;
447
448   iret = make_move_root( ptree, move, ( flag_history | flag_time | flag_rep
449                                         | flag_detect_hang
450                                         | flag_rejections ) );
451   if ( iret < 0 ) { return iret; }
452   
453 #  if ! defined(NO_STDOUT)
454   iret = out_board( ptree, stdout, 0, 0 );
455   if ( iret < 0 ) { return iret; }
456 #  endif
457
458   return analyze( ptree );
459 }
460 #endif
461
462
463 static int
464 do_analyze( tree_t * restrict ptree )
465 { // [HGM] analyze: do a ponder search on the current position
466   int iret;
467   if ( get_elapsed( &time_start ) < 0 ) { return -1; }
468   time_limit = time_max_limit = 1e9; // kludge: use huge time to mimic infinity
469 #ifdef XBOARD
470   Out("1 0 0 0 New Search\n"); // make sure lower depth is emitted, so XBoard undestand new search started
471 #endif
472   game_status |= flag_pondering;
473   iret         = iterate( ptree, flag_refer_rest );
474   game_status &= ~flag_pondering;
475   return iret;
476 }
477
478
479 static int
480 cmd_undo( tree_t * restrict ptree )
481 { // [HGM] undo: restart the game, and feed all moves except the last
482   int i, last = move_ptr;
483   char *p = start_data;
484   if( move_ptr <= 0 ) {
485     str_error = "undo past start of game ignored";
486     return -2;
487   }
488
489   AbortDifficultCommand;
490
491   last--;
492   cmd_new( ptree, &p );
493   for(i=0; i<last; i++) {
494     make_move_root( ptree, move_list[i], 0);
495   }
496
497   exclude_list[0] = 0; // [HGM] exclude: exclude list cleared for new position
498   if ( analyze_mode ) return do_analyze ( ptree ); // [HGM] analyze: analysis should continue after undo
499
500   return 1;
501 }
502
503
504 static int
505 cmd_analyze( tree_t * restrict ptree )
506 { // [HGM] analyze: switch on analyze mode, and start analyzing (also used to force a restart)
507   AbortDifficultCommand;
508
509   analyze_mode = 1;
510   return do_analyze( ptree );
511 }
512
513
514 static int
515 cmd_exit( void )
516 { // [HGM] analyze: switch off analysis mode
517   if ( !analyze_mode ) {
518     str_error = "was not analyzing";
519     return -2;
520   }
521
522   if ( game_status & flag_pondering ) { game_status |= flag_quit_ponder; return 2; }
523   analyze_mode = 0;
524   exclude_list[0] = 0; // [HGM] exclude: exclude list cleared after analysis
525
526   return 1;
527 }
528
529
530 static int
531 is_move( const char *str )
532 {
533   if ( isdigit( (int)str[0] ) && isdigit( (int)str[1] )
534        && isdigit( (int)str[2] ) && isdigit( (int)str[3] )
535        && isupper( (int)str[4] ) && isupper( (int)str[5] )
536        && str[6] == '\0' ) { return 1; }
537
538   return 0;
539 }
540
541
542 static int
543 cmd_move_now( void )
544 {
545   if ( game_status & flag_thinking ) { game_status |= flag_move_now; }
546
547   return 1;
548 }
549
550
551 static int
552 cmd_usrmove( tree_t * restrict ptree, const char *str_move, char **lasts )
553 {
554   const char *str;
555   char *ptr;
556   long lelapsed;
557   unsigned int move;
558   int iret;
559
560   if ( game_status & mask_game_end )
561     {
562       str_error = str_game_ended;
563       return -2;
564     }
565   
566   if ( game_status & flag_thinking )
567     {
568       str_error = str_busy_think;
569       return -2;
570     }
571
572   str = strtok_r( NULL, str_delimiters, lasts );
573   if ( str == NULL ) { lelapsed = 0; }
574   else {
575     if ( *str != 'T' )
576       {
577         str_error = str_bad_cmdline;
578         return -2;
579       }
580     str += 1;
581     lelapsed = strtol( str, &ptr, 0 );
582     if ( ptr == str || lelapsed == LONG_MAX || lelapsed < 1 )
583       {
584         str_error = str_bad_cmdline;
585         return -2;
586       }
587   }
588
589   if ( game_status & ( flag_pondering | flag_puzzling ) )
590     {
591       int i;
592
593       for ( i = 0; i < ponder_nmove; i++ )
594         {
595           if ( ! strcmp( str_move, str_CSA_move(ponder_move_list[i]) ) )
596             {
597               break;
598             }
599         }
600       if ( i == ponder_nmove )
601         {
602 #if defined(CSA_LAN)
603           if ( sckt_csa != SCKT_NULL ) { AbortDifficultCommand; }
604 #endif
605
606 #if defined(DEKUNOBOU)
607           if ( dek_ngame ) { AbortDifficultCommand; }
608 #endif
609
610 #if defined(CSASHOGI)
611           AbortDifficultCommand;
612 #else
613           str_error = str_illegal_move;
614           return -2;
615 #endif
616         }
617
618       if ( ( game_status & flag_puzzling )
619            || strcmp( str_move, str_CSA_move(ponder_move) ) )
620         {
621           ponder_move  = MOVE_PONDER_FAILED;
622           game_status |= flag_quit_ponder;
623           return 2;
624         }
625       else {
626         iret = renovate_time( Flip(root_turn) );
627         if ( iret < 0 ) { return iret; }
628         if ( lelapsed )
629           {
630             adjust_time( (unsigned int)lelapsed, Flip(root_turn) );
631           }
632
633         history_book_learn[ record_game.moves ].move_played = ponder_move;
634         history_book_learn[ record_game.moves ].hand_played
635           = ptree->rep_hand_list[ root_nrep-1 ];
636         history_book_learn[ record_game.moves ].key_played
637           = (unsigned int)ptree->rep_board_list[ root_nrep-1 ];
638
639         out_CSA( ptree, &record_game, ponder_move );
640
641         game_status      &= ~flag_pondering;
642         game_status      |= flag_thinking;
643         n_nobook_move    += 1;
644 #ifdef XBOARD
645       if(!xboard_mode)
646 #endif
647         set_search_limit_time( root_turn );
648
649         OutCsaShogi( "info ponder end\n" );
650
651         str = str_time_symple( time_turn_start - time_start );
652         Out( "    %6s          MOVE PREDICTION HIT\n"
653              "  elapsed: b%u, w%u\n", str, sec_b_total, sec_w_total );
654         return 1;
655       }
656     }
657
658   iret = interpret_CSA_move( ptree, &move, str_move );
659   if ( iret < 0 ) { return iret; }
660   move_evasion_pchk = 0;
661   iret = make_move_root( ptree, move, ( flag_rep | flag_history | flag_time
662                                         | flag_rejections
663                                         | flag_detect_hang ) );
664   if ( iret < 0 )
665       {
666
667 #if defined(CSA_LAN)
668         if ( sckt_csa != SCKT_NULL )
669           {
670             if ( move_evasion_pchk )
671               {
672                 str  = str_CSA_move( move_evasion_pchk );
673                 iret = sckt_out( sckt_csa, "%c%s\n",
674                                  ach_turn[Flip(root_turn)], str );
675                 if ( iret < 0 ) { return iret; }
676               }
677             return cmd_suspend();
678           }
679 #endif
680
681 #if defined(DEKUNOBOU)
682         if ( dek_ngame )
683           {
684             if ( move_evasion_pchk )
685               {
686                 dek_win += 1;
687                 OutDek( "%%TORYO\n" );
688               }
689             return cmd_suspend();
690           }
691 #endif
692
693         if ( move_evasion_pchk )
694           {
695             str = str_CSA_move( move_evasion_pchk );
696 #if defined(CSASHOGI)
697             OutCsaShogi( "move%s\n", str );
698             return cmd_suspend();
699 #else
700             snprintf( str_message, SIZE_MESSAGE, "perpetual check (%c%s)",
701                       ach_turn[Flip(root_turn)], str );
702             str_error = str_message;
703             return -2;
704 #endif
705           }
706
707         return iret;
708       }
709
710   if ( lelapsed ) { adjust_time( (unsigned int)lelapsed, Flip(root_turn) ); }
711   Out( "  elapsed: b%u, w%u\n", sec_b_total, sec_w_total );
712
713 #if defined(CSA_LAN)
714   if ( sckt_csa != SCKT_NULL && ( game_status & flag_mated ) )
715     {
716       iret = sckt_out( sckt_csa, "%%TORYO\n" );
717       if ( iret < 0 ) { return iret; }
718     }
719 #endif
720
721 #if defined(DEKUNOBOU)
722   if ( dek_ngame && ( game_status & flag_drawn ) ) { OutDek( "%%TORYO\n" ); }
723 #endif
724
725   if ( ! ( game_status & mask_game_end ) )
726     {
727       iret = com_turn_start( ptree, 0 );
728       if ( iret < 0 ) { return iret; }
729     }
730
731   return 1;
732 }
733
734
735 static int
736 cmd_beep( char **lasts )
737 {
738   const char *str = strtok_r( NULL, str_delimiters, lasts );
739   if ( str == NULL )
740     {
741       str_error = str_bad_cmdline;
742       return -2;
743     }
744
745   if      ( ! strcmp( str, str_on )  ) {  game_status &= ~flag_nobeep; }
746   else if ( ! strcmp( str, str_off ) ) {  game_status |=  flag_nobeep; }
747   else {
748     str_error = str_bad_cmdline;
749     return -2;
750   }
751
752   return 1;
753 }
754
755
756 static int
757 cmd_peek( char **lasts )
758 {
759   const char *str = strtok_r( NULL, str_delimiters, lasts );
760
761   if ( str == NULL )
762     {
763       str_error = str_bad_cmdline;
764       return -2;
765     }
766
767   if      ( ! strcmp( str, str_on )  ) {  game_status &= ~flag_nopeek; }
768   else if ( ! strcmp( str, str_off ) ) {  game_status |=  flag_nopeek; }
769   else {
770     str_error = str_bad_cmdline;
771     return -2;
772   }
773
774   return 1;
775 }
776
777
778 static int
779 cmd_ponder( char **lasts )
780 {
781   const char *str = strtok_r( NULL, str_delimiters, lasts );
782
783   if ( str == NULL )
784     {
785       str_error = str_bad_cmdline;
786       return -2;
787     }
788
789   if      ( ! strcmp( str, str_on )  ) {  game_status &= ~flag_noponder; }
790   else if ( ! strcmp( str, str_off ) )
791     {
792       if ( game_status & ( flag_pondering | flag_puzzling ) )
793         {
794           game_status |= flag_quit_ponder;
795         }
796       game_status |= flag_noponder;
797     }
798   else {
799     str_error = str_bad_cmdline;
800     return -2;
801   }
802
803   return 1;
804 }
805
806
807 #if ! defined(NO_STDOUT)
808 static int
809 cmd_stress( char **lasts )
810 {
811   const char *str = strtok_r( NULL, str_delimiters, lasts );
812
813   if ( str == NULL )
814     {
815       str_error = str_bad_cmdline;
816       return -2;
817     }
818
819   if      ( ! strcmp( str, str_on  ) ) { game_status &= ~flag_nostress; }
820   else if ( ! strcmp( str, str_off ) ) { game_status |= flag_nostress; }
821   else {
822     str_error = str_bad_cmdline;
823     return -2;
824   }
825
826   return 1;
827 }
828 #endif
829
830
831 static int
832 #if defined(MINIMUM)
833 cmd_book( char **lasts )
834 #else
835 cmd_book( tree_t * restrict ptree, char **lasts )
836 #endif
837 {
838   const char *str = strtok_r( NULL, str_delimiters, lasts );
839   int iret = 1;
840
841   if ( str == NULL )
842     {
843       str_error = str_bad_cmdline;
844       return -2;
845     }
846   if      ( ! strcmp( str, str_on ) )   { iret = book_on(); }
847   else if ( ! strcmp( str, str_off ) )  { iret = book_off(); }
848   else if ( ! strcmp( str, "narrow" ) ) { game_status |= flag_narrow_book; }
849   else if ( ! strcmp( str, "wide" ) )   { game_status &= ~flag_narrow_book; }
850 #if ! defined(MINIMUM)
851   else if ( ! strcmp( str, "create" ) )
852     {
853       AbortDifficultCommand;
854
855       iret = book_create( ptree );
856       if ( iret < 0 ) { return iret; }
857
858       iret = ini_game( ptree, &min_posi_no_handicap, flag_history,
859                        NULL, NULL );
860       if ( iret < 0 ) { return iret; }
861
862       iret = get_elapsed( &time_turn_start );
863     }
864 #endif
865   else {
866     str_error = str_bad_cmdline;
867     iret = -2;
868   }
869
870   return iret;
871 }
872
873
874 static int
875 cmd_display( tree_t * restrict ptree, char **lasts )
876 {
877   const char *str = strtok_r( NULL, str_delimiters, lasts );
878   char *ptr;
879   long l;
880   int iret;
881
882   if ( str != NULL )
883     {
884       l = strtol( str, &ptr, 0 );
885       if ( ptr == str )
886         {
887           str_error = str_bad_cmdline;
888           return -2;
889         }
890       if      ( l == 1 ) { game_status &= ~flag_reverse; }
891       else if ( l == 2 ) { game_status |= flag_reverse; }
892       else {
893         str_error = str_bad_cmdline;
894         return -2;
895       }
896     }
897   
898   Out( "\n" );
899   iret = out_board( ptree, stdout, 0, 0 );
900   if ( iret < 0 ) { return iret; }
901 #if ! defined(NO_LOGGING)
902   iret = out_board( ptree, pf_log, 0, 0 );
903   if ( iret < 0 ) { return iret; }
904 #endif
905   Out( "\n" );
906
907   return 1;
908 }
909
910
911 static int
912 cmd_ping( void )
913 {
914   OutCsaShogi( "pong\n" );
915   Out( "pong\n" );
916   return 1;
917 }
918
919
920 static int
921 cmd_hash( char **lasts )
922 {
923   const char *str = strtok_r( NULL, str_delimiters, lasts );
924   char *ptr;
925   long l;
926
927   if ( str == NULL )
928     {
929       str_error = str_bad_cmdline;
930       return -2;
931     }
932
933   if ( ! strcmp( str, "learn" ) )
934     {
935       str = strtok_r( NULL, str_delimiters, lasts );
936       if ( str != NULL && ! strcmp( str, str_on ) )
937         {
938           return hash_learn_on();
939         }
940       else if ( str != NULL && ! strcmp( str, str_off ) )
941         {
942           return hash_learn_off();
943         }
944 #if ! defined(MINIMUM)
945       else if ( str != NULL && ! strcmp( str, "create" ) )
946         {
947           return hash_learn_create();
948         }
949 #endif
950       else {
951         str_error = str_bad_cmdline;
952         return -2;
953       }
954     }
955
956   l = strtol( str, &ptr, 0 );
957   if ( ptr == str || l == LONG_MAX || l < 1 || l > 31 )
958     {
959       str_error = str_bad_cmdline;
960       return -2;
961     }
962   
963   AbortDifficultCommand;
964   
965   log2_ntrans_table = (int)l;
966   memory_free( (void *)ptrans_table_orig );
967   return ini_trans_table();
968 }
969
970
971 static int
972 cmd_limit( char **lasts )
973 {
974   const char *str = strtok_r( NULL, str_delimiters, lasts );
975   char *ptr;
976   long l1, l2, l3;
977
978   if ( str == NULL )
979     {
980       str_error = str_bad_cmdline;
981       return -2;
982     }
983
984   AbortDifficultCommand;
985
986   if ( ! strcmp( str, "depth" ) )
987     {
988       str = strtok_r( NULL, str_delimiters, lasts );
989       if ( str == NULL )
990         {
991           str_error = str_bad_cmdline;
992           return -2;
993         }
994       l1 = strtol( str, &ptr, 0 );
995       if ( ptr == str || l1 == LONG_MAX || l1 < 1 )
996         {
997           str_error = str_bad_cmdline;
998           return -2;
999         }
1000       sec_limit_up = UINT_MAX;
1001       node_limit   = UINT64_MAX;
1002       depth_limit  = (int)l1;
1003     }
1004   else if ( ! strcmp( str, "nodes" ) )
1005     {
1006       str = strtok_r( NULL, str_delimiters, lasts );
1007       if ( str == NULL )
1008         {
1009           str_error = str_bad_cmdline;
1010           return -2;
1011         }
1012       l1 = strtol( str, &ptr, 0 );
1013       if ( ptr == str || l1 == LONG_MAX || l1 < 1 )
1014         {
1015           str_error = str_bad_cmdline;
1016           return -2;
1017         }
1018       sec_limit_up = UINT_MAX;
1019       depth_limit  = PLY_MAX;
1020       node_limit   = (uint64_t)l1;
1021     }
1022   else if ( ! strcmp( str, "time" ) )
1023     {
1024       str = strtok_r( NULL, str_delimiters, lasts );
1025       if ( str == NULL )
1026         {
1027           str_error = str_bad_cmdline;
1028           return -2;
1029         }
1030
1031       if ( ! strcmp( str, "extendable" ) )
1032         {
1033           game_status |= flag_time_extendable;
1034         }
1035       else if ( ! strcmp( str, "strict" ) )
1036         {
1037           game_status &= ~flag_time_extendable;
1038         }
1039       else {
1040         l1 = strtol( str, &ptr, 0 );
1041         if ( ptr == str || l1 == LONG_MAX || l1 < 0 )
1042           {
1043             str_error = str_bad_cmdline;
1044             return -2;
1045           }
1046
1047         str = strtok_r( NULL, str_delimiters, lasts );
1048         if ( str == NULL )
1049           {
1050             str_error = str_bad_cmdline;
1051             return -2;
1052           }
1053         l2 = strtol( str, &ptr, 0 );
1054         if ( ptr == str || l2 == LONG_MAX || l2 < 0 )
1055           {
1056             str_error = str_bad_cmdline;
1057             return -2;
1058           }
1059
1060         str = strtok_r( NULL, str_delimiters, lasts );
1061         if ( ! str ) { l3 = -1; }
1062         else {
1063           l3 = strtol( str, &ptr, 0 );
1064           if ( ptr == str || l3 >= PLY_MAX || l3 < -1 )
1065             {
1066               str_error = str_bad_cmdline;
1067               return -2;
1068             }
1069         }
1070
1071         if ( ! ( l1 | l2 ) ) { l2 = 1; }
1072
1073         depth_limit  = PLY_MAX;
1074         node_limit   = UINT64_MAX;
1075         sec_limit    = (unsigned int)l1 * 60U;
1076         sec_limit_up = (unsigned int)l2;
1077         if ( l3 == -1 ) { sec_limit_depth = UINT_MAX; }
1078         else            { sec_limit_depth = (unsigned int)l3; }
1079       }
1080     }
1081   else {
1082     str_error = str_bad_cmdline;
1083     return -2;
1084   }
1085
1086   return 1;
1087 }
1088
1089
1090 static int
1091 cmd_read( tree_t * restrict ptree, char **lasts )
1092 {
1093   const char *str1 = strtok_r( NULL, str_delimiters, lasts );
1094   const char *str2 = strtok_r( NULL, str_delimiters, lasts );
1095   const char *str3 = strtok_r( NULL, str_delimiters, lasts );
1096   const char *str_tmp;
1097   FILE *pf_src, *pf_dest;
1098   char str_file[SIZE_FILENAME];
1099   char *ptr;
1100   unsigned int moves;
1101   long l;
1102   int iret, flag, c;
1103
1104   flag    = flag_history | flag_rep | flag_detect_hang | flag_rejections;
1105   moves   = UINT_MAX;
1106   str_tmp = NULL;
1107
1108   if ( str1 == NULL )
1109     {
1110       str_error = str_bad_cmdline;
1111       return -2;
1112     }
1113
1114   if ( str2 != NULL )
1115     {
1116       if ( ! strcmp( str2, "t" ) ) { flag |= flag_time; }
1117       else if ( strcmp( str2, "nil" ) )
1118         {
1119           str_error = str_bad_cmdline;
1120           return -2;
1121         }
1122     }
1123
1124   if ( str3 != NULL )
1125     {
1126       l = strtol( str3, &ptr, 0 );
1127       if ( ptr == str3 || l == LONG_MAX || l < 1 )
1128         {
1129           str_error = str_bad_cmdline;
1130           return -2;
1131         }
1132       moves = (unsigned int)l - 1U;
1133     }
1134
1135   AbortDifficultCommand;
1136
1137   if ( ! strcmp( str1, "." ) )
1138     {
1139       str_tmp = "game.cs_";
1140
1141 #if defined(NO_LOGGING)
1142       strncpy( str_file, "game.csa", SIZE_FILENAME-1 );
1143 #else
1144       snprintf( str_file, SIZE_FILENAME, "%s/game%03d.csa",
1145                 str_dir_logs, irecord_game );
1146 #endif
1147       pf_dest = file_open( str_tmp, "w" );
1148       if ( pf_dest == NULL ) { return -2; }
1149
1150       pf_src = file_open( str_file, "r" );
1151       if ( pf_src == NULL )
1152         {
1153           file_close( pf_dest );
1154           return -2;
1155         }
1156
1157       while ( ( c = getc(pf_src) ) != EOF ) { putc( c, pf_dest ); }
1158
1159       iret = file_close( pf_src );
1160       if ( iret < 0 )
1161         {
1162           file_close( pf_dest );
1163           return iret;
1164         }
1165
1166       iret = file_close( pf_dest );
1167       if ( iret < 0 ) { return iret; }
1168
1169       flag |= flag_time;
1170       str1  = str_tmp;
1171     }
1172
1173   iret = read_record( ptree, str1, moves, flag );
1174   if ( iret < 0 ) { return iret; }
1175
1176   iret = get_elapsed( &time_turn_start );
1177   if ( iret < 0 ) { return iret; }
1178
1179   if ( str_tmp && remove( str_tmp ) )
1180     {
1181       out_warning( "remove() failed." );
1182       return -2;
1183     }
1184
1185   return 1;
1186 }
1187
1188
1189 static int
1190 cmd_resign( tree_t * restrict ptree, char **lasts )
1191 {
1192   const char *str = strtok_r( NULL, str_delimiters, lasts );
1193   char *ptr;
1194   long l;
1195
1196   if ( str == NULL || *str == 'T' )
1197     {
1198       AbortDifficultCommand;
1199
1200       if ( game_status & mask_game_end ) { return 1; }
1201
1202 #if defined(DEKUNOBOU)
1203       if ( dek_ngame && record_game.moves < 2 )
1204         {
1205           str_error = "ignore resignation";
1206           return -2;
1207         }
1208 #endif
1209
1210       game_status |= flag_resigned;
1211       renovate_time( root_turn );
1212       out_CSA( ptree, &record_game, MOVE_RESIGN );
1213     }
1214   else {
1215     l = strtol( str, &ptr, 0 );
1216     if ( ptr == str || l == LONG_MAX || l < MT_CAP_PAWN )
1217       {
1218         str_error = str_bad_cmdline;
1219         return -2;
1220       }
1221     resign_threshold = (int)l;
1222   }
1223
1224   return 1;
1225 }
1226
1227
1228 static int
1229 cmd_move( tree_t * restrict ptree, char **lasts )
1230 {
1231   const char *str = strtok_r( NULL, str_delimiters, lasts );
1232   unsigned int move;
1233   int iret;
1234
1235   if ( game_status & mask_game_end )
1236     {
1237       str_error = str_game_ended;
1238       return -2;
1239     }
1240   
1241   AbortDifficultCommand;
1242
1243   if ( str == NULL )
1244     {
1245       if ( analyze_mode ) // [HGM] analyze: in analysis mode we cannot set the engine thinking (but perhaps play PV move?)
1246           {
1247             str_error = str_bad_cmdline;
1248             return -2;
1249           }
1250
1251       iret = get_elapsed( &time_turn_start );
1252       if ( iret < 0 ) { return iret; }
1253       
1254       iret = com_turn_start( ptree, 0 );
1255       if ( iret < 0 ) { return iret; }
1256     }
1257   else if ( ! strcmp( str, "restraint" ) )
1258     {
1259       if ( analyze_mode ) // [HGM] analyze: in analysis mode we cannot set the engine thinking
1260           {
1261             str_error = str_bad_cmdline;
1262             return -2;
1263           }
1264
1265       iret = get_elapsed( &time_turn_start );
1266       if ( iret < 0 ) { return iret; }
1267       
1268       iret = com_turn_start( ptree, flag_refer_rest );
1269       if ( iret < 0 ) { return iret; }
1270     }
1271   else {
1272
1273     iret = interpret_CSA_move( ptree, &move, str );
1274     if ( iret < 0 ) { return iret; }
1275     
1276     iret = get_elapsed( &time_turn_start );
1277     if ( iret < 0 ) { return iret; }
1278     
1279 #if defined(MNJ_LAN)
1280     if ( sckt_mnj != SCKT_NULL )
1281       {
1282         const char *str2 = strtok_r( NULL, str_delimiters, lasts );
1283         char *ptr;
1284         long l;
1285         if ( str2 ) { l = strtol( str2, &ptr, 0 ); }
1286         if ( ! str2 || ptr == str || l == LONG_MAX || l < 1 )
1287           {
1288             str_error = str_bad_cmdline;
1289             return -2;
1290           }
1291         mnj_posi_id   = (int)l;
1292         mnj_move_last = move;
1293       }
1294 #endif
1295
1296     iret = make_move_root( ptree, move, ( flag_history | flag_time | flag_rep
1297                                           | flag_detect_hang
1298                                           | flag_rejections ) );
1299     if ( iret < 0 ) { return iret; }
1300
1301     exclude_list[0] = 0; // [HGM] exclude: exclude list cleared for new position
1302     if ( analyze_mode ) return do_analyze ( ptree ); // [HGM] analyze: analysis should continue after feeding moves
1303   }
1304   
1305   return 1;
1306 }
1307
1308
1309 static int
1310 cmd_new( tree_t * restrict ptree, char **lasts )
1311 {
1312   const char *str1 = strtok_r( NULL, str_delimiters, lasts );
1313   const char *str2 = strtok_r( NULL, str_delimiters, lasts );
1314   const min_posi_t *pmp;
1315   min_posi_t min_posi;
1316   int iret;
1317
1318   AbortDifficultCommand;
1319
1320   start_pos = *lasts; move_ptr = 0; // [HGM] undo: remember start position
1321   exclude_list[0] = 0; // [HGM] exclude: exclude list cleared for new position
1322
1323   if ( str1 != NULL )
1324     {
1325       strncpy(start_data, str1, 511); // [HGM] undo: remember start position
1326       memset( &min_posi.asquare, empty, nsquare );
1327       min_posi.hand_black = min_posi.hand_white = 0;
1328       iret = read_board_rep1( str1, &min_posi );
1329       if ( iret < 0 ) { return iret; }
1330
1331       if ( str2 != NULL )
1332         {
1333           if      ( ! strcmp( str2, "-" ) ) { min_posi.turn_to_move = white; }
1334           else if ( ! strcmp( str2, "+" ) ) { min_posi.turn_to_move = black; }
1335           else {
1336             str_error = str_bad_cmdline;
1337             return -2;
1338           }
1339         }
1340       else { min_posi.turn_to_move = black; }
1341
1342       pmp = &min_posi;
1343     }
1344   else { pmp = &min_posi_no_handicap; }
1345
1346   iret = ini_game( ptree, pmp, flag_history, NULL, NULL );
1347   if ( iret < 0 ) { return iret; }
1348
1349   return get_elapsed( &time_turn_start );
1350 }
1351
1352
1353 static int
1354 cmd_problem( tree_t * restrict ptree, char **lasts )
1355 {
1356   const char *str = strtok_r( NULL, str_delimiters, lasts );
1357   char *ptr;
1358   long l;
1359   unsigned int nposition;
1360   int iret;
1361
1362   if ( str != NULL )
1363     {
1364       l = strtol( str, &ptr, 0 );
1365       if ( ptr == str || l == LONG_MAX || l < 1 )
1366         {
1367           str_error = str_bad_cmdline;
1368           return -2;
1369         }
1370       nposition = (unsigned int)l;
1371     }
1372   else { nposition = UINT_MAX; }
1373
1374   AbortDifficultCommand;
1375
1376   iret = record_open( &record_problems, "problem.csa", mode_read, NULL, NULL );
1377   if ( iret < 0 ) { return iret; }
1378
1379   iret = solve_problems( ptree, nposition );
1380   if ( iret < 0 )
1381     {
1382       record_close( &record_problems );
1383       return iret;
1384     }
1385
1386   iret = record_close( &record_problems );
1387   if ( iret < 0 ) { return iret; }
1388
1389   iret = ini_game( ptree, &min_posi_no_handicap, flag_history, NULL, NULL );
1390   if ( iret < 0 ) { return iret; }
1391
1392   return get_elapsed( &time_turn_start );
1393 }
1394
1395
1396 static int
1397 cmd_quit( void )
1398 {
1399   game_status |= flag_quit;
1400   return 1;
1401 }
1402
1403
1404 static int
1405 cmd_suspend( void )
1406 {
1407   if ( game_status & ( flag_pondering | flag_puzzling ) )
1408     {
1409       game_status |= flag_quit_ponder;
1410       return 2;
1411     }
1412   
1413   game_status |= flag_suspend;
1414   return 1;
1415 }
1416
1417
1418 static int
1419 cmd_time( char **lasts )
1420 {
1421   const char *str = strtok_r( NULL, str_delimiters, lasts );
1422   char *ptr;
1423
1424   if ( str == NULL )
1425     {
1426       str_error = str_bad_cmdline;
1427       return -2;
1428     }
1429   else if ( ! strcmp( str, "response" ) )
1430     {
1431       long l;
1432       str = strtok_r( NULL, str_delimiters, lasts );
1433       if ( str == NULL )
1434         {
1435           str_error = str_bad_cmdline;
1436           return -2;
1437         }
1438       l = strtol( str, &ptr, 0 );
1439       if ( ptr == str || l == LONG_MAX || l < 0 || l > 1000 )
1440         {
1441           str_error = str_bad_cmdline;
1442           return -2;
1443         }
1444       time_response = (unsigned int)l;
1445       return 1;
1446     }
1447   else if ( ! strcmp( str, "remain" ) )
1448     {
1449       long l1, l2;
1450       
1451       str = strtok_r( NULL, str_delimiters, lasts );
1452       if ( str == NULL )
1453         {
1454           str_error = str_bad_cmdline;
1455           return -2;
1456         }
1457       l1 = strtol( str, &ptr, 0 );
1458       if ( ptr == str || l1 == LONG_MAX || l1 < 0 )
1459         {
1460           str_error = str_bad_cmdline;
1461           return -2;
1462         }
1463
1464       str = strtok_r( NULL, str_delimiters, lasts );
1465       if ( str == NULL )
1466         {
1467           str_error = str_bad_cmdline;
1468           return -2;
1469         }
1470       l2 = strtol( str, &ptr, 0 );
1471       if ( ptr == str || l2 == LONG_MAX || l2 < 0 )
1472         {
1473           str_error = str_bad_cmdline;
1474           return -2;
1475         }
1476
1477       if ( sec_limit_up == UINT_MAX )
1478         {
1479           str_error = str_bad_cmdline;
1480           return -2;
1481         }
1482
1483       return reset_time( (unsigned int)l1, (unsigned int)l2 );
1484     }
1485
1486   str_error = str_bad_cmdline;
1487   return -2;
1488 }
1489
1490
1491 #if !defined(MINIMUM)
1492 /* learn (ini|no-ini) steps games iterations tlp1 tlp2 */
1493 static int
1494 cmd_learn( tree_t * restrict ptree, char **lasts )
1495 {
1496   const char *str1 = strtok_r( NULL, str_delimiters, lasts );
1497   const char *str2 = strtok_r( NULL, str_delimiters, lasts );
1498   const char *str3 = strtok_r( NULL, str_delimiters, lasts );
1499   const char *str4 = strtok_r( NULL, str_delimiters, lasts );
1500 #  if defined(TLP)
1501   const char *str5 = strtok_r( NULL, str_delimiters, lasts );
1502   const char *str6 = strtok_r( NULL, str_delimiters, lasts );
1503 #  endif
1504   char *ptr;
1505   long l;
1506   unsigned int max_games;
1507   int is_ini, nsteps, max_iterations, nworker1, nworker2, iret;
1508
1509   if ( str1 == NULL )
1510     {
1511       str_error = str_bad_cmdline;
1512       return -2;
1513     }
1514   if      ( ! strcmp( str1, "ini" ) )    { is_ini = 1; }
1515   else if ( ! strcmp( str1, "no-ini" ) ) { is_ini = 0; }
1516   else {
1517     str_error = str_bad_cmdline;
1518     return -2;
1519   }
1520
1521   max_games      = UINT_MAX;
1522   max_iterations = INT_MAX;
1523   nworker1 = nworker2 = nsteps = 1;
1524
1525   if ( str2 != NULL )
1526     {
1527       l = strtol( str2, &ptr, 0 );
1528       if ( ptr == str2 || l == LONG_MAX || l < 1 )
1529         {
1530           str_error = str_bad_cmdline;
1531           return -2;
1532         }
1533       nsteps = (int)l;
1534     }
1535
1536   if ( str3 != NULL )
1537     {
1538       l = strtol( str3, &ptr, 0 );
1539       if ( ptr == str3 || l == LONG_MAX || l == LONG_MIN )
1540         {
1541           str_error = str_bad_cmdline;
1542           return -2;
1543         }
1544       if ( l > 0 ) { max_games = (unsigned int)l; }
1545     }
1546
1547   if ( str4 != NULL )
1548     {
1549       l = strtol( str4, &ptr, 0 );
1550       if ( ptr == str4 || l == LONG_MAX || l == LONG_MIN )
1551         {
1552           str_error = str_bad_cmdline;
1553           return -2;
1554         }
1555       if ( l > 0 ) { max_iterations = (int)l; }
1556     }
1557
1558 #  if defined(TLP)
1559   if ( str5 != NULL )
1560     {
1561       l = strtol( str5, &ptr, 0 );
1562       if ( ptr == str5 || l > TLP_MAX_THREADS || l < 1 )
1563         {
1564           str_error = str_bad_cmdline;
1565           return -2;
1566         }
1567       nworker1 = (int)l;
1568     }
1569
1570   if ( str6 != NULL )
1571     {
1572       l = strtol( str6, &ptr, 0 );
1573       if ( ptr == str6 || l > TLP_MAX_THREADS || l < 1 )
1574         {
1575           str_error = str_bad_cmdline;
1576           return -2;
1577         }
1578       nworker2 = (int)l;
1579     }
1580 #  endif
1581
1582   AbortDifficultCommand;
1583
1584   log2_ntrans_table = 12;
1585
1586   memory_free( (void *)ptrans_table_orig );
1587
1588   iret = ini_trans_table();
1589   if ( iret < 0 ) { return iret; }
1590
1591   iret = learn( ptree, is_ini, nsteps, max_games, max_iterations,
1592                 nworker1, nworker2 );
1593   if ( iret < 0 ) { return -1; }
1594
1595   iret = ini_game( ptree, &min_posi_no_handicap, flag_history, NULL, NULL );
1596   if ( iret < 0 ) { return -1; }
1597
1598   iret = get_elapsed( &time_turn_start );
1599   if ( iret < 0 ) { return iret; }
1600
1601   return 1;
1602 }
1603 #endif /* MINIMUM */
1604
1605
1606 #if defined(MPV)
1607 static int
1608 cmd_mpv( tree_t * restrict ptree, char **lasts )
1609 {
1610   const char *str = strtok_r( NULL, str_delimiters, lasts );
1611   char *ptr;
1612   long l;
1613
1614   if ( str == NULL )
1615     {
1616       str_error = str_bad_cmdline;
1617       return -2;
1618     }
1619   else if ( ! strcmp( str, "num" ) )
1620     {
1621       str = strtok_r( NULL, str_delimiters, lasts );
1622       if ( str == NULL )
1623         {
1624           str_error = str_bad_cmdline;
1625           return -2;
1626         }
1627       l = strtol( str, &ptr, 0 );
1628       if ( ptr == str || l == LONG_MAX || l < 1 || l > MPV_MAX_PV )
1629         {
1630           str_error = str_bad_cmdline;
1631           return -2;
1632         }
1633
1634       AbortDifficultCommand;
1635
1636       mpv_num = (int)l;
1637
1638       if ( analyze_mode ) return do_analyze ( ptree ); // [HGM] analyze: analysis should continue changing num
1639
1640       return 1;
1641     }
1642   else if ( ! strcmp( str, "width" ) )
1643     {
1644       str = strtok_r( NULL, str_delimiters, lasts );
1645       if ( str == NULL )
1646         {
1647           str_error = str_bad_cmdline;
1648           return -2;
1649         }
1650       l = strtol( str, &ptr, 0 );
1651       if ( ptr == str || l == LONG_MAX || l < MT_CAP_PAWN )
1652         {
1653           str_error = str_bad_cmdline;
1654           return -2;
1655         }
1656
1657       AbortDifficultCommand;
1658
1659       mpv_width = (int)l;
1660
1661       if ( analyze_mode ) return do_analyze ( ptree ); // [HGM] analyze: analysis should continue after changing width
1662
1663       return 1;
1664     }
1665
1666   str_error = str_bad_cmdline;
1667   return -2;
1668 }
1669 #endif
1670
1671
1672 #if defined(TLP)
1673 static int
1674 cmd_thread( char **lasts )
1675 {
1676   const char *str = strtok_r( NULL, str_delimiters, lasts );
1677
1678   if ( str == NULL )
1679     {
1680       str_error = str_bad_cmdline;
1681       return -2;
1682     }
1683   else if ( ! strcmp( str, "num" ) )
1684     {
1685       char *ptr;
1686       long l;
1687
1688       str = strtok_r( NULL, str_delimiters, lasts );
1689       if ( str == NULL )
1690         {
1691           str_error = str_bad_cmdline;
1692           return -2;
1693         }
1694       l = strtol( str, &ptr, 0 );
1695       if ( ptr == str || l == LONG_MAX || l < 1 || l > TLP_MAX_THREADS )
1696         {
1697           str_error = str_bad_cmdline;
1698           return -2;
1699         }
1700
1701       TlpEnd();
1702
1703       tlp_max = (int)l;
1704
1705       if ( game_status & ( flag_thinking | flag_pondering | flag_puzzling ) )
1706         {
1707           return tlp_start();
1708         }
1709       return 1;
1710     }
1711
1712   str_error = str_bad_cmdline;
1713   return -2;
1714 }
1715 #endif
1716
1717
1718 #if defined(CSA_LAN)
1719 static int
1720 cmd_connect( tree_t * restrict ptree, char **lasts )
1721 {
1722   const char *str;
1723   char *ptr;
1724   long max_games;
1725
1726   str = strtok_r( NULL, str_delimiters, lasts );
1727   if ( ! str || ! strcmp( str, "." ) ) { str = "gserver.computer-shogi.org"; }
1728   strncpy( client_str_addr, str, 255 );
1729   client_str_addr[255] = '\0';
1730
1731   str = strtok_r( NULL, str_delimiters, lasts );
1732   if ( ! str || ! strcmp( str, "." ) ) { str = "4081"; }
1733   client_port = strtol( str, &ptr, 0 );
1734   if ( ptr == str || client_port == LONG_MAX || client_port < 0
1735        || client_port > USHRT_MAX )
1736     {
1737       str_error = str_bad_cmdline;
1738       return -2;
1739     }
1740
1741   str = strtok_r( NULL, str_delimiters, lasts );
1742   if ( ! str || ! strcmp( str, "." ) ) { str = "bonanza_test"; }
1743   strncpy( client_str_id, str, 255 );
1744   client_str_id[255] = '\0';
1745
1746   str = strtok_r( NULL, " \t", lasts );
1747   if ( ! str || ! strcmp( str, "." ) ) { str = "bonanza_test"; }
1748   strncpy( client_str_pwd, str, 255 );
1749   client_str_pwd[255] = '\0';
1750
1751   str = strtok_r( NULL, str_delimiters, lasts );
1752   if ( ! str || ! strcmp( str, "." ) ) { client_max_game = INT_MAX; }
1753   else {
1754     max_games = strtol( str, &ptr, 0 );
1755     if ( ptr == str || max_games == LONG_MAX || max_games < 1 )
1756     {
1757       str_error = str_bad_cmdline;
1758       return -2;
1759     }
1760     client_max_game = max_games;
1761   }
1762
1763   AbortDifficultCommand;
1764
1765   client_ngame          = 0;
1766
1767   return client_next_game( ptree, client_str_addr, (int)client_port );
1768 }
1769 #endif
1770
1771
1772 #if defined(MNJ_LAN)
1773 static int
1774 cmd_mnj( tree_t * restrict ptree, char **lasts )
1775 {
1776   char client_str_addr[256];
1777   char client_str_id[256];
1778   const char *str;
1779   char *ptr;
1780   unsigned int seed;
1781   int sd;
1782   long l;
1783   int client_port;
1784
1785   str = strtok_r( NULL, str_delimiters, lasts );
1786   if ( ! str )
1787     {
1788       str_error = str_bad_cmdline;
1789       return -2;
1790     }
1791   l = strtol( str, &ptr, 0 );
1792   if ( ptr == str || l == LONG_MAX || l < 0 )
1793     {
1794       str_error = str_bad_cmdline;
1795       return -2;
1796     }
1797   sd = (int)l;
1798
1799
1800   str = strtok_r( NULL, str_delimiters, lasts );
1801   if ( ! str )
1802     {
1803       str_error = str_bad_cmdline;
1804       return -2;
1805     }
1806   l = strtol( str, &ptr, 0 );
1807   if ( ptr == str || l == LONG_MAX || l < 0 )
1808     {
1809       str_error = str_bad_cmdline;
1810       return -2;
1811     }
1812   seed = (unsigned int)l;
1813
1814
1815   str = strtok_r( NULL, str_delimiters, lasts );
1816   if ( ! str || ! strcmp( str, "." ) ) { str = "localhost"; }
1817   strncpy( client_str_addr, str, 255 );
1818   client_str_addr[255] = '\0';
1819
1820
1821   str = strtok_r( NULL, str_delimiters, lasts );
1822   if ( ! str || ! strcmp( str, "." ) ) { str = "4082"; }
1823   l = strtol( str, &ptr, 0 );
1824   if ( ptr == str || l == LONG_MAX || l < 0 || l > USHRT_MAX )
1825     {
1826       str_error = str_bad_cmdline;
1827       return -2;
1828     }
1829   client_port = (int)l;
1830
1831
1832   str = strtok_r( NULL, str_delimiters, lasts );
1833   if ( ! str || ! strcmp( str, "." ) ) { str = "bonanza1"; }
1834   strncpy( client_str_id, str, 255 );
1835   client_str_id[255] = '\0';
1836
1837   AbortDifficultCommand;
1838
1839   resign_threshold  = 65535;
1840   game_status      |= ( flag_noponder | flag_noprompt );
1841   if ( mnj_reset_tbl( sd, seed ) < 0 ) { return -1; }
1842
1843   sckt_mnj = sckt_connect( client_str_addr, (int)client_port );
1844   if ( sckt_mnj == SCKT_NULL ) { return -2; }
1845
1846   str_buffer_cmdline[0] = '\0';
1847
1848   Out( "Sending my name %s", client_str_id );
1849   sckt_out( sckt_mnj, "%s\n", client_str_id );
1850
1851   return analyze( ptree );
1852 }
1853 #endif
1854
1855
1856 #if defined(DEKUNOBOU)
1857
1858 static int
1859 cmd_dek( char **lasts )
1860 {
1861   const char *str = strtok_r( NULL, str_delimiters, lasts );
1862   char *ptr;
1863   long l1, l2;
1864   int iret;
1865
1866   if ( str == NULL )
1867     {
1868       str_error = str_bad_cmdline;
1869       return -2;
1870     }
1871   strncpy( str_message, str, SIZE_MESSAGE-1 );
1872   str_message[SIZE_MESSAGE-1] = '\0';
1873   dek_ul_addr = inet_addr( str );
1874
1875   str = strtok_r( NULL, str_delimiters, lasts );
1876   if ( str == NULL )
1877     {
1878       str_error = str_bad_cmdline;
1879       return -2;
1880     }
1881   l1 = strtol( str, &ptr, 0 );
1882   if ( ptr == str || l1 == LONG_MAX || l1 < 0 || l1 > USHRT_MAX )
1883     {
1884       str_error = str_bad_cmdline;
1885       return -2;
1886     }
1887
1888   str = strtok_r( NULL, str_delimiters, lasts );
1889   if ( str == NULL )
1890     {
1891       str_error = str_bad_cmdline;
1892       return -2;
1893     }
1894   l2 = strtol( str, &ptr, 0 );
1895   if ( ptr == str || l2 == LONG_MAX || l2 < 0 || l2 > USHRT_MAX )
1896     {
1897       str_error = str_bad_cmdline;
1898       return -2;
1899     }
1900
1901   AbortDifficultCommand;
1902
1903   iret = dek_start( str_message, (int)l1, (int)l2 );
1904   if ( iret < 0 ) { return iret; }
1905
1906   Out( "\n- in communication with Dekunobou...\n" );
1907
1908   str_buffer_cmdline[0] = '\0';
1909   dek_ngame    = 1;
1910   dek_lost     = 0;
1911   dek_win      = 0;
1912   dek_turn     = 1;
1913   game_status |= flag_resigned;
1914   
1915   return 1;
1916 }
1917
1918 #endif
1919