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