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