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