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