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