Trivial-draws recognition improved in Xiangqi
[xboard.git] / backend.c
1 /*
2  * backend.c -- Common back end for X and Windows NT versions of
3  *
4  * Copyright 1991 by Digital Equipment Corporation, Maynard,
5  * Massachusetts.
6  *
7  * Enhancements Copyright 1992-2001, 2002, 2003, 2004, 2005, 2006,
8  * 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
9  *
10  * Enhancements Copyright 2005 Alessandro Scotti
11  *
12  * The following terms apply to Digital Equipment Corporation's copyright
13  * interest in XBoard:
14  * ------------------------------------------------------------------------
15  * All Rights Reserved
16  *
17  * Permission to use, copy, modify, and distribute this software and its
18  * documentation for any purpose and without fee is hereby granted,
19  * provided that the above copyright notice appear in all copies and that
20  * both that copyright notice and this permission notice appear in
21  * supporting documentation, and that the name of Digital not be
22  * used in advertising or publicity pertaining to distribution of the
23  * software without specific, written prior permission.
24  *
25  * DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
26  * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
27  * DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
28  * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
29  * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
30  * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
31  * SOFTWARE.
32  * ------------------------------------------------------------------------
33  *
34  * The following terms apply to the enhanced version of XBoard
35  * distributed by the Free Software Foundation:
36  * ------------------------------------------------------------------------
37  *
38  * GNU XBoard is free software: you can redistribute it and/or modify
39  * it under the terms of the GNU General Public License as published by
40  * the Free Software Foundation, either version 3 of the License, or (at
41  * your option) any later version.
42  *
43  * GNU XBoard is distributed in the hope that it will be useful, but
44  * WITHOUT ANY WARRANTY; without even the implied warranty of
45  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
46  * General Public License for more details.
47  *
48  * You should have received a copy of the GNU General Public License
49  * along with this program. If not, see http://www.gnu.org/licenses/.  *
50  *
51  *------------------------------------------------------------------------
52  ** See the file ChangeLog for a revision history.  */
53
54 /* [AS] Also useful here for debugging */
55 #ifdef WIN32
56 #include <windows.h>
57
58 #define DoSleep( n ) if( (n) != 0 ) Sleep( (n) );
59
60 #else
61
62 #define DoSleep( n ) if( (n) >= 0) sleep(n)
63
64 #endif
65
66 #include "config.h"
67
68 #include <assert.h>
69 #include <stdio.h>
70 #include <ctype.h>
71 #include <errno.h>
72 #include <sys/types.h>
73 #include <sys/stat.h>
74 #include <math.h>
75 #include <ctype.h>
76
77 #if STDC_HEADERS
78 # include <stdlib.h>
79 # include <string.h>
80 # include <stdarg.h>
81 #else /* not STDC_HEADERS */
82 # if HAVE_STRING_H
83 #  include <string.h>
84 # else /* not HAVE_STRING_H */
85 #  include <strings.h>
86 # endif /* not HAVE_STRING_H */
87 #endif /* not STDC_HEADERS */
88
89 #if HAVE_SYS_FCNTL_H
90 # include <sys/fcntl.h>
91 #else /* not HAVE_SYS_FCNTL_H */
92 # if HAVE_FCNTL_H
93 #  include <fcntl.h>
94 # endif /* HAVE_FCNTL_H */
95 #endif /* not HAVE_SYS_FCNTL_H */
96
97 #if TIME_WITH_SYS_TIME
98 # include <sys/time.h>
99 # include <time.h>
100 #else
101 # if HAVE_SYS_TIME_H
102 #  include <sys/time.h>
103 # else
104 #  include <time.h>
105 # endif
106 #endif
107
108 #if defined(_amigados) && !defined(__GNUC__)
109 struct timezone {
110     int tz_minuteswest;
111     int tz_dsttime;
112 };
113 extern int gettimeofday(struct timeval *, struct timezone *);
114 #endif
115
116 #if HAVE_UNISTD_H
117 # include <unistd.h>
118 #endif
119
120 #include "common.h"
121 #include "frontend.h"
122 #include "backend.h"
123 #include "parser.h"
124 #include "moves.h"
125 #if ZIPPY
126 # include "zippy.h"
127 #endif
128 #include "backendz.h"
129 #include "gettext.h" 
130  
131 #ifdef ENABLE_NLS 
132 # define _(s) gettext (s) 
133 # define N_(s) gettext_noop (s) 
134 #else 
135 # define _(s) (s) 
136 # define N_(s) s 
137 #endif 
138
139
140 /* A point in time */
141 typedef struct {
142     long sec;  /* Assuming this is >= 32 bits */
143     int ms;    /* Assuming this is >= 16 bits */
144 } TimeMark;
145
146 int establish P((void));
147 void read_from_player P((InputSourceRef isr, VOIDSTAR closure,
148                          char *buf, int count, int error));
149 void read_from_ics P((InputSourceRef isr, VOIDSTAR closure,
150                       char *buf, int count, int error));
151 void ics_printf P((char *format, ...));
152 void SendToICS P((char *s));
153 void SendToICSDelayed P((char *s, long msdelay));
154 void SendMoveToICS P((ChessMove moveType, int fromX, int fromY,
155                       int toX, int toY));
156 void HandleMachineMove P((char *message, ChessProgramState *cps));
157 int AutoPlayOneMove P((void));
158 int LoadGameOneMove P((ChessMove readAhead));
159 int LoadGameFromFile P((char *filename, int n, char *title, int useList));
160 int LoadPositionFromFile P((char *filename, int n, char *title));
161 int SavePositionToFile P((char *filename));
162 void ApplyMove P((int fromX, int fromY, int toX, int toY, int promoChar,
163                                                                                 Board board));
164 void MakeMove P((int fromX, int fromY, int toX, int toY, int promoChar));
165 void ShowMove P((int fromX, int fromY, int toX, int toY));
166 int FinishMove P((ChessMove moveType, int fromX, int fromY, int toX, int toY,
167                    /*char*/int promoChar));
168 void BackwardInner P((int target));
169 void ForwardInner P((int target));
170 int Adjudicate P((ChessProgramState *cps));
171 void GameEnds P((ChessMove result, char *resultDetails, int whosays));
172 void EditPositionDone P((Boolean fakeRights));
173 void PrintOpponents P((FILE *fp));
174 void PrintPosition P((FILE *fp, int move));
175 void StartChessProgram P((ChessProgramState *cps));
176 void SendToProgram P((char *message, ChessProgramState *cps));
177 void SendMoveToProgram P((int moveNum, ChessProgramState *cps));
178 void ReceiveFromProgram P((InputSourceRef isr, VOIDSTAR closure,
179                            char *buf, int count, int error));
180 void SendTimeControl P((ChessProgramState *cps,
181                         int mps, long tc, int inc, int sd, int st));
182 char *TimeControlTagValue P((void));
183 void Attention P((ChessProgramState *cps));
184 void FeedMovesToProgram P((ChessProgramState *cps, int upto));
185 void ResurrectChessProgram P((void));
186 void DisplayComment P((int moveNumber, char *text));
187 void DisplayMove P((int moveNumber));
188
189 void ParseGameHistory P((char *game));
190 void ParseBoard12 P((char *string));
191 void KeepAlive P((void));
192 void StartClocks P((void));
193 void SwitchClocks P((int nr));
194 void StopClocks P((void));
195 void ResetClocks P((void));
196 char *PGNDate P((void));
197 void SetGameInfo P((void));
198 Boolean ParseFEN P((Board board, int *blackPlaysFirst, char *fen));
199 int RegisterMove P((void));
200 void MakeRegisteredMove P((void));
201 void TruncateGame P((void));
202 int looking_at P((char *, int *, char *));
203 void CopyPlayerNameIntoFileName P((char **, char *));
204 char *SavePart P((char *));
205 int SaveGameOldStyle P((FILE *));
206 int SaveGamePGN P((FILE *));
207 void GetTimeMark P((TimeMark *));
208 long SubtractTimeMarks P((TimeMark *, TimeMark *));
209 int CheckFlags P((void));
210 long NextTickLength P((long));
211 void CheckTimeControl P((void));
212 void show_bytes P((FILE *, char *, int));
213 int string_to_rating P((char *str));
214 void ParseFeatures P((char* args, ChessProgramState *cps));
215 void InitBackEnd3 P((void));
216 void FeatureDone P((ChessProgramState* cps, int val));
217 void InitChessProgram P((ChessProgramState *cps, int setup));
218 void OutputKibitz(int window, char *text);
219 int PerpetualChase(int first, int last);
220 int EngineOutputIsUp();
221 void InitDrawingSizes(int x, int y);
222
223 #ifdef WIN32
224        extern void ConsoleCreate();
225 #endif
226
227 ChessProgramState *WhitePlayer();
228 void InsertIntoMemo P((int which, char *text)); // [HGM] kibitz: in engineo.c
229 int VerifyDisplayMode P(());
230
231 char *GetInfoFromComment( int, char * ); // [HGM] PV time: returns stripped comment
232 void InitEngineUCI( const char * iniDir, ChessProgramState * cps ); // [HGM] moved here from winboard.c
233 char *ProbeBook P((int moveNr, char *book)); // [HGM] book: returns a book move
234 char *SendMoveToBookUser P((int nr, ChessProgramState *cps, int initial)); // [HGM] book
235 void ics_update_width P((int new_width));
236 extern char installDir[MSG_SIZ];
237 VariantClass startVariant; /* [HGM] nicks: initial variant */
238
239 extern int tinyLayout, smallLayout;
240 ChessProgramStats programStats;
241 char lastPV[2][2*MSG_SIZ]; /* [HGM] pv: last PV in thinking output of each engine */
242 int endPV = -1;
243 static int exiting = 0; /* [HGM] moved to top */
244 static int setboardSpoiledMachineBlack = 0 /*, errorExitFlag = 0*/;
245 int startedFromPositionFile = FALSE; Board filePosition;       /* [HGM] loadPos */
246 Board partnerBoard;     /* [HGM] bughouse: for peeking at partner game          */
247 int partnerHighlight[2];
248 Boolean partnerBoardValid = 0;
249 char partnerStatus[MSG_SIZ];
250 Boolean partnerUp;
251 Boolean originalFlip;
252 Boolean twoBoards = 0;
253 char endingGame = 0;    /* [HGM] crash: flag to prevent recursion of GameEnds() */
254 int whiteNPS, blackNPS; /* [HGM] nps: for easily making clocks aware of NPS     */
255 VariantClass currentlyInitializedVariant; /* [HGM] variantswitch */
256 int lastIndex = 0;      /* [HGM] autoinc: last game/position used in match mode */
257 Boolean connectionAlive;/* [HGM] alive: ICS connection status from probing      */
258 int opponentKibitzes;
259 int lastSavedGame; /* [HGM] save: ID of game */
260 char chatPartner[MAX_CHAT][MSG_SIZ]; /* [HGM] chat: list of chatting partners */
261 extern int chatCount;
262 int chattingPartner;
263 char marker[BOARD_RANKS][BOARD_FILES]; /* [HGM] marks for target squares */
264
265 /* States for ics_getting_history */
266 #define H_FALSE 0
267 #define H_REQUESTED 1
268 #define H_GOT_REQ_HEADER 2
269 #define H_GOT_UNREQ_HEADER 3
270 #define H_GETTING_MOVES 4
271 #define H_GOT_UNWANTED_HEADER 5
272
273 /* whosays values for GameEnds */
274 #define GE_ICS 0
275 #define GE_ENGINE 1
276 #define GE_PLAYER 2
277 #define GE_FILE 3
278 #define GE_XBOARD 4
279 #define GE_ENGINE1 5
280 #define GE_ENGINE2 6
281
282 /* Maximum number of games in a cmail message */
283 #define CMAIL_MAX_GAMES 20
284
285 /* Different types of move when calling RegisterMove */
286 #define CMAIL_MOVE   0
287 #define CMAIL_RESIGN 1
288 #define CMAIL_DRAW   2
289 #define CMAIL_ACCEPT 3
290
291 /* Different types of result to remember for each game */
292 #define CMAIL_NOT_RESULT 0
293 #define CMAIL_OLD_RESULT 1
294 #define CMAIL_NEW_RESULT 2
295
296 /* Telnet protocol constants */
297 #define TN_WILL 0373
298 #define TN_WONT 0374
299 #define TN_DO   0375
300 #define TN_DONT 0376
301 #define TN_IAC  0377
302 #define TN_ECHO 0001
303 #define TN_SGA  0003
304 #define TN_PORT 23
305
306 /* [AS] */
307 static char * safeStrCpy( char * dst, const char * src, size_t count )
308 {
309     assert( dst != NULL );
310     assert( src != NULL );
311     assert( count > 0 );
312
313     strncpy( dst, src, count );
314     dst[ count-1 ] = '\0';
315     return dst;
316 }
317
318 /* Some compiler can't cast u64 to double
319  * This function do the job for us:
320
321  * We use the highest bit for cast, this only
322  * works if the highest bit is not
323  * in use (This should not happen)
324  *
325  * We used this for all compiler
326  */
327 double
328 u64ToDouble(u64 value)
329 {
330   double r;
331   u64 tmp = value & u64Const(0x7fffffffffffffff);
332   r = (double)(s64)tmp;
333   if (value & u64Const(0x8000000000000000))
334        r +=  9.2233720368547758080e18; /* 2^63 */
335  return r;
336 }
337
338 /* Fake up flags for now, as we aren't keeping track of castling
339    availability yet. [HGM] Change of logic: the flag now only
340    indicates the type of castlings allowed by the rule of the game.
341    The actual rights themselves are maintained in the array
342    castlingRights, as part of the game history, and are not probed
343    by this function.
344  */
345 int
346 PosFlags(index)
347 {
348   int flags = F_ALL_CASTLE_OK;
349   if ((index % 2) == 0) flags |= F_WHITE_ON_MOVE;
350   switch (gameInfo.variant) {
351   case VariantSuicide:
352     flags &= ~F_ALL_CASTLE_OK;
353   case VariantGiveaway:         // [HGM] moved this case label one down: seems Giveaway does have castling on ICC!
354     flags |= F_IGNORE_CHECK;
355   case VariantLosers:
356     flags |= F_MANDATORY_CAPTURE; //[HGM] losers: sets flag so TestLegality rejects non-capts if capts exist
357     break;
358   case VariantAtomic:
359     flags |= F_IGNORE_CHECK | F_ATOMIC_CAPTURE;
360     break;
361   case VariantKriegspiel:
362     flags |= F_KRIEGSPIEL_CAPTURE;
363     break;
364   case VariantCapaRandom: 
365   case VariantFischeRandom:
366     flags |= F_FRC_TYPE_CASTLING; /* [HGM] enable this through flag */
367   case VariantNoCastle:
368   case VariantShatranj:
369   case VariantCourier:
370   case VariantMakruk:
371     flags &= ~F_ALL_CASTLE_OK;
372     break;
373   default:
374     break;
375   }
376   return flags;
377 }
378
379 FILE *gameFileFP, *debugFP;
380
381 /* 
382     [AS] Note: sometimes, the sscanf() function is used to parse the input
383     into a fixed-size buffer. Because of this, we must be prepared to
384     receive strings as long as the size of the input buffer, which is currently
385     set to 4K for Windows and 8K for the rest.
386     So, we must either allocate sufficiently large buffers here, or
387     reduce the size of the input buffer in the input reading part.
388 */
389
390 char cmailMove[CMAIL_MAX_GAMES][MOVE_LEN], cmailMsg[MSG_SIZ];
391 char bookOutput[MSG_SIZ*10], thinkOutput[MSG_SIZ*10], lastHint[MSG_SIZ];
392 char thinkOutput1[MSG_SIZ*10];
393
394 ChessProgramState first, second;
395
396 /* premove variables */
397 int premoveToX = 0;
398 int premoveToY = 0;
399 int premoveFromX = 0;
400 int premoveFromY = 0;
401 int premovePromoChar = 0;
402 int gotPremove = 0;
403 Boolean alarmSounded;
404 /* end premove variables */
405
406 char *ics_prefix = "$";
407 int ics_type = ICS_GENERIC;
408
409 int currentMove = 0, forwardMostMove = 0, backwardMostMove = 0;
410 int pauseExamForwardMostMove = 0;
411 int nCmailGames = 0, nCmailResults = 0, nCmailMovesRegistered = 0;
412 int cmailMoveRegistered[CMAIL_MAX_GAMES], cmailResult[CMAIL_MAX_GAMES];
413 int cmailMsgLoaded = FALSE, cmailMailedMove = FALSE;
414 int cmailOldMove = -1, firstMove = TRUE, flipView = FALSE;
415 int blackPlaysFirst = FALSE, startedFromSetupPosition = FALSE;
416 int searchTime = 0, pausing = FALSE, pauseExamInvalid = FALSE;
417 int whiteFlag = FALSE, blackFlag = FALSE;
418 int userOfferedDraw = FALSE;
419 int ics_user_moved = 0, ics_gamenum = -1, ics_getting_history = H_FALSE;
420 int matchMode = FALSE, hintRequested = FALSE, bookRequested = FALSE;
421 int cmailMoveType[CMAIL_MAX_GAMES];
422 long ics_clock_paused = 0;
423 ProcRef icsPR = NoProc, cmailPR = NoProc;
424 InputSourceRef telnetISR = NULL, fromUserISR = NULL, cmailISR = NULL;
425 GameMode gameMode = BeginningOfGame;
426 char moveList[MAX_MOVES][MOVE_LEN], parseList[MAX_MOVES][MOVE_LEN * 2];
427 char *commentList[MAX_MOVES], *cmailCommentList[CMAIL_MAX_GAMES];
428 ChessProgramStats_Move pvInfoList[MAX_MOVES]; /* [AS] Info about engine thinking */
429 int hiddenThinkOutputState = 0; /* [AS] */
430 int adjudicateLossThreshold = 0; /* [AS] Automatic adjudication */
431 int adjudicateLossPlies = 6;
432 char white_holding[64], black_holding[64];
433 TimeMark lastNodeCountTime;
434 long lastNodeCount=0;
435 int have_sent_ICS_logon = 0;
436 int movesPerSession;
437 long whiteTimeRemaining, blackTimeRemaining, timeControl, timeIncrement;
438 long timeControl_2; /* [AS] Allow separate time controls */
439 char *fullTimeControlString = NULL; /* [HGM] secondary TC: merge of MPS, TC and inc */
440 long timeRemaining[2][MAX_MOVES];
441 int matchGame = 0;
442 TimeMark programStartTime;
443 char ics_handle[MSG_SIZ];
444 int have_set_title = 0;
445
446 /* animateTraining preserves the state of appData.animate
447  * when Training mode is activated. This allows the
448  * response to be animated when appData.animate == TRUE and
449  * appData.animateDragging == TRUE.
450  */
451 Boolean animateTraining;
452
453 GameInfo gameInfo;
454
455 AppData appData;
456
457 Board boards[MAX_MOVES];
458 /* [HGM] Following 7 needed for accurate legality tests: */
459 signed char  castlingRank[BOARD_FILES]; // and corresponding ranks
460 signed char  initialRights[BOARD_FILES];
461 int   nrCastlingRights; // For TwoKings, or to implement castling-unknown status
462 int   initialRulePlies, FENrulePlies;
463 FILE  *serverMoves = NULL; // next two for broadcasting (/serverMoves option)
464 int loadFlag = 0; 
465 int shuffleOpenings;
466 int mute; // mute all sounds
467
468 // [HGM] vari: next 12 to save and restore variations
469 #define MAX_VARIATIONS 10
470 int framePtr = MAX_MOVES-1; // points to free stack entry
471 int storedGames = 0;
472 int savedFirst[MAX_VARIATIONS];
473 int savedLast[MAX_VARIATIONS];
474 int savedFramePtr[MAX_VARIATIONS];
475 char *savedDetails[MAX_VARIATIONS];
476 ChessMove savedResult[MAX_VARIATIONS];
477
478 void PushTail P((int firstMove, int lastMove));
479 Boolean PopTail P((Boolean annotate));
480 void CleanupTail P((void));
481
482 ChessSquare  FIDEArray[2][BOARD_FILES] = {
483     { WhiteRook, WhiteKnight, WhiteBishop, WhiteQueen,
484         WhiteKing, WhiteBishop, WhiteKnight, WhiteRook },
485     { BlackRook, BlackKnight, BlackBishop, BlackQueen,
486         BlackKing, BlackBishop, BlackKnight, BlackRook }
487 };
488
489 ChessSquare twoKingsArray[2][BOARD_FILES] = {
490     { WhiteRook, WhiteKnight, WhiteBishop, WhiteQueen,
491         WhiteKing, WhiteKing, WhiteKnight, WhiteRook },
492     { BlackRook, BlackKnight, BlackBishop, BlackQueen,
493         BlackKing, BlackKing, BlackKnight, BlackRook }
494 };
495
496 ChessSquare  KnightmateArray[2][BOARD_FILES] = {
497     { WhiteRook, WhiteMan, WhiteBishop, WhiteQueen,
498         WhiteUnicorn, WhiteBishop, WhiteMan, WhiteRook },
499     { BlackRook, BlackMan, BlackBishop, BlackQueen,
500         BlackUnicorn, BlackBishop, BlackMan, BlackRook }
501 };
502
503 ChessSquare fairyArray[2][BOARD_FILES] = { /* [HGM] Queen side differs from King side */
504     { WhiteRook, WhiteKnight, WhiteBishop, WhiteQueen,
505         WhiteKing, WhiteBishop, WhiteKnight, WhiteRook },
506     { BlackLance, BlackAlfil, BlackMarshall, BlackAngel,
507         BlackKing, BlackMarshall, BlackAlfil, BlackLance }
508 };
509
510 ChessSquare ShatranjArray[2][BOARD_FILES] = { /* [HGM] (movGen knows about Shatranj Q and P) */
511     { WhiteRook, WhiteKnight, WhiteAlfil, WhiteKing,
512         WhiteFerz, WhiteAlfil, WhiteKnight, WhiteRook },
513     { BlackRook, BlackKnight, BlackAlfil, BlackKing,
514         BlackFerz, BlackAlfil, BlackKnight, BlackRook }
515 };
516
517 ChessSquare makrukArray[2][BOARD_FILES] = { /* [HGM] (movGen knows about Shatranj Q and P) */
518     { WhiteRook, WhiteKnight, WhiteMan, WhiteKing,
519         WhiteFerz, WhiteMan, WhiteKnight, WhiteRook },
520     { BlackRook, BlackKnight, BlackMan, BlackFerz,
521         BlackKing, BlackMan, BlackKnight, BlackRook }
522 };
523
524
525 #if (BOARD_FILES>=10)
526 ChessSquare ShogiArray[2][BOARD_FILES] = {
527     { WhiteQueen, WhiteKnight, WhiteFerz, WhiteWazir,
528         WhiteKing, WhiteWazir, WhiteFerz, WhiteKnight, WhiteQueen },
529     { BlackQueen, BlackKnight, BlackFerz, BlackWazir,
530         BlackKing, BlackWazir, BlackFerz, BlackKnight, BlackQueen }
531 };
532
533 ChessSquare XiangqiArray[2][BOARD_FILES] = {
534     { WhiteRook, WhiteKnight, WhiteAlfil, WhiteFerz,
535         WhiteWazir, WhiteFerz, WhiteAlfil, WhiteKnight, WhiteRook },
536     { BlackRook, BlackKnight, BlackAlfil, BlackFerz,
537         BlackWazir, BlackFerz, BlackAlfil, BlackKnight, BlackRook }
538 };
539
540 ChessSquare CapablancaArray[2][BOARD_FILES] = {
541     { WhiteRook, WhiteKnight, WhiteAngel, WhiteBishop, WhiteQueen, 
542         WhiteKing, WhiteBishop, WhiteMarshall, WhiteKnight, WhiteRook },
543     { BlackRook, BlackKnight, BlackAngel, BlackBishop, BlackQueen, 
544         BlackKing, BlackBishop, BlackMarshall, BlackKnight, BlackRook }
545 };
546
547 ChessSquare GreatArray[2][BOARD_FILES] = {
548     { WhiteDragon, WhiteKnight, WhiteAlfil, WhiteGrasshopper, WhiteKing, 
549         WhiteSilver, WhiteCardinal, WhiteAlfil, WhiteKnight, WhiteDragon },
550     { BlackDragon, BlackKnight, BlackAlfil, BlackGrasshopper, BlackKing, 
551         BlackSilver, BlackCardinal, BlackAlfil, BlackKnight, BlackDragon },
552 };
553
554 ChessSquare JanusArray[2][BOARD_FILES] = {
555     { WhiteRook, WhiteAngel, WhiteKnight, WhiteBishop, WhiteKing, 
556         WhiteQueen, WhiteBishop, WhiteKnight, WhiteAngel, WhiteRook },
557     { BlackRook, BlackAngel, BlackKnight, BlackBishop, BlackKing, 
558         BlackQueen, BlackBishop, BlackKnight, BlackAngel, BlackRook }
559 };
560
561 #ifdef GOTHIC
562 ChessSquare GothicArray[2][BOARD_FILES] = {
563     { WhiteRook, WhiteKnight, WhiteBishop, WhiteQueen, WhiteMarshall, 
564         WhiteKing, WhiteAngel, WhiteBishop, WhiteKnight, WhiteRook },
565     { BlackRook, BlackKnight, BlackBishop, BlackQueen, BlackMarshall, 
566         BlackKing, BlackAngel, BlackBishop, BlackKnight, BlackRook }
567 };
568 #else // !GOTHIC
569 #define GothicArray CapablancaArray
570 #endif // !GOTHIC
571
572 #ifdef FALCON
573 ChessSquare FalconArray[2][BOARD_FILES] = {
574     { WhiteRook, WhiteKnight, WhiteBishop, WhiteLance, WhiteQueen, 
575         WhiteKing, WhiteLance, WhiteBishop, WhiteKnight, WhiteRook },
576     { BlackRook, BlackKnight, BlackBishop, BlackLance, BlackQueen, 
577         BlackKing, BlackLance, BlackBishop, BlackKnight, BlackRook }
578 };
579 #else // !FALCON
580 #define FalconArray CapablancaArray
581 #endif // !FALCON
582
583 #else // !(BOARD_FILES>=10)
584 #define XiangqiPosition FIDEArray
585 #define CapablancaArray FIDEArray
586 #define GothicArray FIDEArray
587 #define GreatArray FIDEArray
588 #endif // !(BOARD_FILES>=10)
589
590 #if (BOARD_FILES>=12)
591 ChessSquare CourierArray[2][BOARD_FILES] = {
592     { WhiteRook, WhiteKnight, WhiteAlfil, WhiteBishop, WhiteMan, WhiteKing,
593         WhiteFerz, WhiteWazir, WhiteBishop, WhiteAlfil, WhiteKnight, WhiteRook },
594     { BlackRook, BlackKnight, BlackAlfil, BlackBishop, BlackMan, BlackKing,
595         BlackFerz, BlackWazir, BlackBishop, BlackAlfil, BlackKnight, BlackRook }
596 };
597 #else // !(BOARD_FILES>=12)
598 #define CourierArray CapablancaArray
599 #endif // !(BOARD_FILES>=12)
600
601
602 Board initialPosition;
603
604
605 /* Convert str to a rating. Checks for special cases of "----",
606
607    "++++", etc. Also strips ()'s */
608 int
609 string_to_rating(str)
610   char *str;
611 {
612   while(*str && !isdigit(*str)) ++str;
613   if (!*str)
614     return 0;   /* One of the special "no rating" cases */
615   else
616     return atoi(str);
617 }
618
619 void
620 ClearProgramStats()
621 {
622     /* Init programStats */
623     programStats.movelist[0] = 0;
624     programStats.depth = 0;
625     programStats.nr_moves = 0;
626     programStats.moves_left = 0;
627     programStats.nodes = 0;
628     programStats.time = -1;        // [HGM] PGNtime: make invalid to recognize engine output
629     programStats.score = 0;
630     programStats.got_only_move = 0;
631     programStats.got_fail = 0;
632     programStats.line_is_book = 0;
633 }
634
635 void
636 InitBackEnd1()
637 {
638     int matched, min, sec;
639
640     ShowThinkingEvent(); // [HGM] thinking: make sure post/nopost state is set according to options
641     startVariant = StringToVariant(appData.variant); // [HGM] nicks: remember original variant
642
643     GetTimeMark(&programStartTime);
644     srandom((programStartTime.ms + 1000*programStartTime.sec)*0x1001001); // [HGM] book: makes sure random is unpredictabe to msec level
645
646     ClearProgramStats();
647     programStats.ok_to_send = 1;
648     programStats.seen_stat = 0;
649
650     /*
651      * Initialize game list
652      */
653     ListNew(&gameList);
654
655
656     /*
657      * Internet chess server status
658      */
659     if (appData.icsActive) {
660         appData.matchMode = FALSE;
661         appData.matchGames = 0;
662 #if ZIPPY       
663         appData.noChessProgram = !appData.zippyPlay;
664 #else
665         appData.zippyPlay = FALSE;
666         appData.zippyTalk = FALSE;
667         appData.noChessProgram = TRUE;
668 #endif
669         if (*appData.icsHelper != NULLCHAR) {
670             appData.useTelnet = TRUE;
671             appData.telnetProgram = appData.icsHelper;
672         }
673     } else {
674         appData.zippyTalk = appData.zippyPlay = FALSE;
675     }
676
677     /* [AS] Initialize pv info list [HGM] and game state */
678     {
679         int i, j;
680
681         for( i=0; i<=framePtr; i++ ) {
682             pvInfoList[i].depth = -1;
683             boards[i][EP_STATUS] = EP_NONE;
684             for( j=0; j<BOARD_FILES-2; j++ ) boards[i][CASTLING][j] = NoRights;
685         }
686     }
687
688     /*
689      * Parse timeControl resource
690      */
691     if (!ParseTimeControl(appData.timeControl, appData.timeIncrement,
692                           appData.movesPerSession)) {
693         char buf[MSG_SIZ];
694         snprintf(buf, sizeof(buf), _("bad timeControl option %s"), appData.timeControl);
695         DisplayFatalError(buf, 0, 2);
696     }
697
698     /*
699      * Parse searchTime resource
700      */
701     if (*appData.searchTime != NULLCHAR) {
702         matched = sscanf(appData.searchTime, "%d:%d", &min, &sec);
703         if (matched == 1) {
704             searchTime = min * 60;
705         } else if (matched == 2) {
706             searchTime = min * 60 + sec;
707         } else {
708             char buf[MSG_SIZ];
709             snprintf(buf, sizeof(buf), _("bad searchTime option %s"), appData.searchTime);
710             DisplayFatalError(buf, 0, 2);
711         }
712     }
713
714     /* [AS] Adjudication threshold */
715     adjudicateLossThreshold = appData.adjudicateLossThreshold;
716     
717     first.which = "first";
718     second.which = "second";
719     first.maybeThinking = second.maybeThinking = FALSE;
720     first.pr = second.pr = NoProc;
721     first.isr = second.isr = NULL;
722     first.sendTime = second.sendTime = 2;
723     first.sendDrawOffers = 1;
724     if (appData.firstPlaysBlack) {
725         first.twoMachinesColor = "black\n";
726         second.twoMachinesColor = "white\n";
727     } else {
728         first.twoMachinesColor = "white\n";
729         second.twoMachinesColor = "black\n";
730     }
731     first.program = appData.firstChessProgram;
732     second.program = appData.secondChessProgram;
733     first.host = appData.firstHost;
734     second.host = appData.secondHost;
735     first.dir = appData.firstDirectory;
736     second.dir = appData.secondDirectory;
737     first.other = &second;
738     second.other = &first;
739     first.initString = appData.initString;
740     second.initString = appData.secondInitString;
741     first.computerString = appData.firstComputerString;
742     second.computerString = appData.secondComputerString;
743     first.useSigint = second.useSigint = TRUE;
744     first.useSigterm = second.useSigterm = TRUE;
745     first.reuse = appData.reuseFirst;
746     second.reuse = appData.reuseSecond;
747     first.nps = appData.firstNPS;   // [HGM] nps: copy nodes per second
748     second.nps = appData.secondNPS;
749     first.useSetboard = second.useSetboard = FALSE;
750     first.useSAN = second.useSAN = FALSE;
751     first.usePing = second.usePing = FALSE;
752     first.lastPing = second.lastPing = 0;
753     first.lastPong = second.lastPong = 0;
754     first.usePlayother = second.usePlayother = FALSE;
755     first.useColors = second.useColors = TRUE;
756     first.useUsermove = second.useUsermove = FALSE;
757     first.sendICS = second.sendICS = FALSE;
758     first.sendName = second.sendName = appData.icsActive;
759     first.sdKludge = second.sdKludge = FALSE;
760     first.stKludge = second.stKludge = FALSE;
761     TidyProgramName(first.program, first.host, first.tidy);
762     TidyProgramName(second.program, second.host, second.tidy);
763     first.matchWins = second.matchWins = 0;
764     strcpy(first.variants, appData.variant);
765     strcpy(second.variants, appData.variant);
766     first.analysisSupport = second.analysisSupport = 2; /* detect */
767     first.analyzing = second.analyzing = FALSE;
768     first.initDone = second.initDone = FALSE;
769
770     /* New features added by Tord: */
771     first.useFEN960 = FALSE; second.useFEN960 = FALSE;
772     first.useOOCastle = TRUE; second.useOOCastle = TRUE;
773     /* End of new features added by Tord. */
774     first.fenOverride  = appData.fenOverride1;
775     second.fenOverride = appData.fenOverride2;
776
777     /* [HGM] time odds: set factor for each machine */
778     first.timeOdds  = appData.firstTimeOdds;
779     second.timeOdds = appData.secondTimeOdds;
780     { float norm = 1;
781         if(appData.timeOddsMode) {
782             norm = first.timeOdds;
783             if(norm > second.timeOdds) norm = second.timeOdds;
784         }
785         first.timeOdds /= norm;
786         second.timeOdds /= norm;
787     }
788
789     /* [HGM] secondary TC: how to handle sessions that do not fit in 'level'*/
790     first.accumulateTC = appData.firstAccumulateTC;
791     second.accumulateTC = appData.secondAccumulateTC;
792     first.maxNrOfSessions = second.maxNrOfSessions = 1;
793
794     /* [HGM] debug */
795     first.debug = second.debug = FALSE;
796     first.supportsNPS = second.supportsNPS = UNKNOWN;
797
798     /* [HGM] options */
799     first.optionSettings  = appData.firstOptions;
800     second.optionSettings = appData.secondOptions;
801
802     first.scoreIsAbsolute = appData.firstScoreIsAbsolute; /* [AS] */
803     second.scoreIsAbsolute = appData.secondScoreIsAbsolute; /* [AS] */
804     first.isUCI = appData.firstIsUCI; /* [AS] */
805     second.isUCI = appData.secondIsUCI; /* [AS] */
806     first.hasOwnBookUCI = appData.firstHasOwnBookUCI; /* [AS] */
807     second.hasOwnBookUCI = appData.secondHasOwnBookUCI; /* [AS] */
808
809     if (appData.firstProtocolVersion > PROTOVER ||
810         appData.firstProtocolVersion < 1) {
811       char buf[MSG_SIZ];
812       sprintf(buf, _("protocol version %d not supported"),
813               appData.firstProtocolVersion);
814       DisplayFatalError(buf, 0, 2);
815     } else {
816       first.protocolVersion = appData.firstProtocolVersion;
817     }
818
819     if (appData.secondProtocolVersion > PROTOVER ||
820         appData.secondProtocolVersion < 1) {
821       char buf[MSG_SIZ];
822       sprintf(buf, _("protocol version %d not supported"),
823               appData.secondProtocolVersion);
824       DisplayFatalError(buf, 0, 2);
825     } else {
826       second.protocolVersion = appData.secondProtocolVersion;
827     }
828
829     if (appData.icsActive) {
830         appData.clockMode = TRUE;  /* changes dynamically in ICS mode */
831 //    } else if (*appData.searchTime != NULLCHAR || appData.noChessProgram) {
832     } else if (appData.noChessProgram) { // [HGM] st: searchTime mode now also is clockMode
833         appData.clockMode = FALSE;
834         first.sendTime = second.sendTime = 0;
835     }
836     
837 #if ZIPPY
838     /* Override some settings from environment variables, for backward
839        compatibility.  Unfortunately it's not feasible to have the env
840        vars just set defaults, at least in xboard.  Ugh.
841     */
842     if (appData.icsActive && (appData.zippyPlay || appData.zippyTalk)) {
843       ZippyInit();
844     }
845 #endif
846     
847     if (appData.noChessProgram) {
848         programVersion = (char*) malloc(5 + strlen(PACKAGE_STRING));
849         sprintf(programVersion, "%s", PACKAGE_STRING);
850     } else {
851       /* [HGM] tidy: use tidy name, in stead of full pathname (which was probably a bug due to / vs \ ) */
852       programVersion = (char*) malloc(8 + strlen(PACKAGE_STRING) + strlen(first.tidy));
853       sprintf(programVersion, "%s + %s", PACKAGE_STRING, first.tidy);
854     }
855
856     if (!appData.icsActive) {
857       char buf[MSG_SIZ];
858       /* Check for variants that are supported only in ICS mode,
859          or not at all.  Some that are accepted here nevertheless
860          have bugs; see comments below.
861       */
862       VariantClass variant = StringToVariant(appData.variant);
863       switch (variant) {
864       case VariantBughouse:     /* need four players and two boards */
865       case VariantKriegspiel:   /* need to hide pieces and move details */
866       /* case VariantFischeRandom: (Fabien: moved below) */
867         sprintf(buf, _("Variant %s supported only in ICS mode"), appData.variant);
868         DisplayFatalError(buf, 0, 2);
869         return;
870
871       case VariantUnknown:
872       case VariantLoadable:
873       case Variant29:
874       case Variant30:
875       case Variant31:
876       case Variant32:
877       case Variant33:
878       case Variant34:
879       case Variant35:
880       case Variant36:
881       default:
882         sprintf(buf, _("Unknown variant name %s"), appData.variant);
883         DisplayFatalError(buf, 0, 2);
884         return;
885
886       case VariantXiangqi:    /* [HGM] repetition rules not implemented */
887       case VariantFairy:      /* [HGM] TestLegality definitely off! */
888       case VariantGothic:     /* [HGM] should work */
889       case VariantCapablanca: /* [HGM] should work */
890       case VariantCourier:    /* [HGM] initial forced moves not implemented */
891       case VariantShogi:      /* [HGM] drops not tested for legality */
892       case VariantKnightmate: /* [HGM] should work */
893       case VariantCylinder:   /* [HGM] untested */
894       case VariantFalcon:     /* [HGM] untested */
895       case VariantCrazyhouse: /* holdings not shown, ([HGM] fixed that!)
896                                  offboard interposition not understood */
897       case VariantNormal:     /* definitely works! */
898       case VariantWildCastle: /* pieces not automatically shuffled */
899       case VariantNoCastle:   /* pieces not automatically shuffled */
900       case VariantFischeRandom: /* [HGM] works and shuffles pieces */
901       case VariantLosers:     /* should work except for win condition,
902                                  and doesn't know captures are mandatory */
903       case VariantSuicide:    /* should work except for win condition,
904                                  and doesn't know captures are mandatory */
905       case VariantGiveaway:   /* should work except for win condition,
906                                  and doesn't know captures are mandatory */
907       case VariantTwoKings:   /* should work */
908       case VariantAtomic:     /* should work except for win condition */
909       case Variant3Check:     /* should work except for win condition */
910       case VariantShatranj:   /* should work except for all win conditions */
911       case VariantMakruk:     /* should work except for daw countdown */
912       case VariantBerolina:   /* might work if TestLegality is off */
913       case VariantCapaRandom: /* should work */
914       case VariantJanus:      /* should work */
915       case VariantSuper:      /* experimental */
916       case VariantGreat:      /* experimental, requires legality testing to be off */
917         break;
918       }
919     }
920
921     InitEngineUCI( installDir, &first );  // [HGM] moved here from winboard.c, to make available in xboard
922     InitEngineUCI( installDir, &second );
923 }
924
925 int NextIntegerFromString( char ** str, long * value )
926 {
927     int result = -1;
928     char * s = *str;
929
930     while( *s == ' ' || *s == '\t' ) {
931         s++;
932     }
933
934     *value = 0;
935
936     if( *s >= '0' && *s <= '9' ) {
937         while( *s >= '0' && *s <= '9' ) {
938             *value = *value * 10 + (*s - '0');
939             s++;
940         }
941
942         result = 0;
943     }
944
945     *str = s;
946
947     return result;
948 }
949
950 int NextTimeControlFromString( char ** str, long * value )
951 {
952     long temp;
953     int result = NextIntegerFromString( str, &temp );
954
955     if( result == 0 ) {
956         *value = temp * 60; /* Minutes */
957         if( **str == ':' ) {
958             (*str)++;
959             result = NextIntegerFromString( str, &temp );
960             *value += temp; /* Seconds */
961         }
962     }
963
964     return result;
965 }
966
967 int NextSessionFromString( char ** str, int *moves, long * tc, long *inc)
968 {   /* [HGM] routine added to read '+moves/time' for secondary time control */
969     int result = -1; long temp, temp2;
970
971     if(**str != '+') return -1; // old params remain in force!
972     (*str)++;
973     if( NextTimeControlFromString( str, &temp ) ) return -1;
974
975     if(**str != '/') {
976         /* time only: incremental or sudden-death time control */
977         if(**str == '+') { /* increment follows; read it */
978             (*str)++;
979             if(result = NextIntegerFromString( str, &temp2)) return -1;
980             *inc = temp2 * 1000;
981         } else *inc = 0;
982         *moves = 0; *tc = temp * 1000; 
983         return 0;
984     } else if(temp % 60 != 0) return -1;     /* moves was given as min:sec */
985
986     (*str)++; /* classical time control */
987     result = NextTimeControlFromString( str, &temp2);
988     if(result == 0) {
989         *moves = temp/60;
990         *tc    = temp2 * 1000;
991         *inc   = 0;
992     }
993     return result;
994 }
995
996 int GetTimeQuota(int movenr)
997 {   /* [HGM] get time to add from the multi-session time-control string */
998     int moves=1; /* kludge to force reading of first session */
999     long time, increment;
1000     char *s = fullTimeControlString;
1001
1002     if(appData.debugMode) fprintf(debugFP, "TC string = '%s'\n", fullTimeControlString);
1003     do {
1004         if(moves) NextSessionFromString(&s, &moves, &time, &increment);
1005         if(appData.debugMode) fprintf(debugFP, "mps=%d tc=%d inc=%d\n", moves, (int) time, (int) increment);
1006         if(movenr == -1) return time;    /* last move before new session     */
1007         if(!moves) return increment;     /* current session is incremental   */
1008         if(movenr >= 0) movenr -= moves; /* we already finished this session */
1009     } while(movenr >= -1);               /* try again for next session       */
1010
1011     return 0; // no new time quota on this move
1012 }
1013
1014 int
1015 ParseTimeControl(tc, ti, mps)
1016      char *tc;
1017      int ti;
1018      int mps;
1019 {
1020   long tc1;
1021   long tc2;
1022   char buf[MSG_SIZ];
1023   
1024   if(ti >= 0 && !strchr(tc, '+') && !strchr(tc, '/') ) mps = 0;
1025   if(ti > 0) {
1026     if(mps)
1027       sprintf(buf, "+%d/%s+%d", mps, tc, ti);
1028     else sprintf(buf, "+%s+%d", tc, ti);
1029   } else {
1030     if(mps)
1031              sprintf(buf, "+%d/%s", mps, tc);
1032     else sprintf(buf, "+%s", tc);
1033   }
1034   fullTimeControlString = StrSave(buf);
1035   
1036   if( NextTimeControlFromString( &tc, &tc1 ) != 0 ) {
1037     return FALSE;
1038   }
1039   
1040   if( *tc == '/' ) {
1041     /* Parse second time control */
1042     tc++;
1043     
1044     if( NextTimeControlFromString( &tc, &tc2 ) != 0 ) {
1045       return FALSE;
1046     }
1047     
1048     if( tc2 == 0 ) {
1049       return FALSE;
1050     }
1051     
1052     timeControl_2 = tc2 * 1000;
1053   }
1054   else {
1055     timeControl_2 = 0;
1056   }
1057   
1058   if( tc1 == 0 ) {
1059     return FALSE;
1060   }
1061   
1062   timeControl = tc1 * 1000;
1063   
1064   if (ti >= 0) {
1065     timeIncrement = ti * 1000;  /* convert to ms */
1066     movesPerSession = 0;
1067   } else {
1068     timeIncrement = 0;
1069     movesPerSession = mps;
1070   }
1071   return TRUE;
1072 }
1073
1074 void
1075 InitBackEnd2()
1076 {
1077     if (appData.debugMode) {
1078         fprintf(debugFP, "%s\n", programVersion);
1079     }
1080
1081     set_cont_sequence(appData.wrapContSeq);
1082     if (appData.matchGames > 0) {
1083         appData.matchMode = TRUE;
1084     } else if (appData.matchMode) {
1085         appData.matchGames = 1;
1086     }
1087     if(appData.matchMode && appData.sameColorGames > 0) /* [HGM] alternate: overrule matchGames */
1088         appData.matchGames = appData.sameColorGames;
1089     if(appData.rewindIndex > 1) { /* [HGM] autoinc: rewind implies auto-increment and overrules given index */
1090         if(appData.loadPositionIndex >= 0) appData.loadPositionIndex = -1;
1091         if(appData.loadGameIndex >= 0) appData.loadGameIndex = -1;
1092     }
1093     Reset(TRUE, FALSE);
1094     if (appData.noChessProgram || first.protocolVersion == 1) {
1095       InitBackEnd3();
1096     } else {
1097       /* kludge: allow timeout for initial "feature" commands */
1098       FreezeUI();
1099       DisplayMessage("", _("Starting chess program"));
1100       ScheduleDelayedEvent(InitBackEnd3, FEATURE_TIMEOUT);
1101     }
1102 }
1103
1104 void
1105 InitBackEnd3 P((void))
1106 {
1107     GameMode initialMode;
1108     char buf[MSG_SIZ];
1109     int err;
1110
1111     InitChessProgram(&first, startedFromSetupPosition);
1112
1113     if(!appData.noChessProgram) {  /* [HGM] tidy: redo program version to use name from myname feature */
1114         free(programVersion);
1115         programVersion = (char*) malloc(8 + strlen(PACKAGE_STRING) + strlen(first.tidy));
1116         sprintf(programVersion, "%s + %s", PACKAGE_STRING, first.tidy);
1117     }
1118
1119     if (appData.icsActive) {
1120 #ifdef WIN32
1121         /* [DM] Make a console window if needed [HGM] merged ifs */
1122         ConsoleCreate(); 
1123 #endif
1124         err = establish();
1125         if (err != 0) {
1126             if (*appData.icsCommPort != NULLCHAR) {
1127                 sprintf(buf, _("Could not open comm port %s"),  
1128                         appData.icsCommPort);
1129             } else {
1130                 snprintf(buf, sizeof(buf), _("Could not connect to host %s, port %s"),  
1131                         appData.icsHost, appData.icsPort);
1132             }
1133             DisplayFatalError(buf, err, 1);
1134             return;
1135         }
1136         SetICSMode();
1137         telnetISR =
1138           AddInputSource(icsPR, FALSE, read_from_ics, &telnetISR);
1139         fromUserISR =
1140           AddInputSource(NoProc, FALSE, read_from_player, &fromUserISR);
1141         if(appData.keepAlive) // [HGM] alive: schedule sending of dummy 'date' command
1142             ScheduleDelayedEvent(KeepAlive, appData.keepAlive*60*1000);
1143     } else if (appData.noChessProgram) {
1144         SetNCPMode();
1145     } else {
1146         SetGNUMode();
1147     }
1148
1149     if (*appData.cmailGameName != NULLCHAR) {
1150         SetCmailMode();
1151         OpenLoopback(&cmailPR);
1152         cmailISR =
1153           AddInputSource(cmailPR, FALSE, CmailSigHandlerCallBack, &cmailISR);
1154     }
1155     
1156     ThawUI();
1157     DisplayMessage("", "");
1158     if (StrCaseCmp(appData.initialMode, "") == 0) {
1159       initialMode = BeginningOfGame;
1160     } else if (StrCaseCmp(appData.initialMode, "TwoMachines") == 0) {
1161       initialMode = TwoMachinesPlay;
1162     } else if (StrCaseCmp(appData.initialMode, "AnalyzeFile") == 0) {
1163       initialMode = AnalyzeFile; 
1164     } else if (StrCaseCmp(appData.initialMode, "Analysis") == 0) {
1165       initialMode = AnalyzeMode;
1166     } else if (StrCaseCmp(appData.initialMode, "MachineWhite") == 0) {
1167       initialMode = MachinePlaysWhite;
1168     } else if (StrCaseCmp(appData.initialMode, "MachineBlack") == 0) {
1169       initialMode = MachinePlaysBlack;
1170     } else if (StrCaseCmp(appData.initialMode, "EditGame") == 0) {
1171       initialMode = EditGame;
1172     } else if (StrCaseCmp(appData.initialMode, "EditPosition") == 0) {
1173       initialMode = EditPosition;
1174     } else if (StrCaseCmp(appData.initialMode, "Training") == 0) {
1175       initialMode = Training;
1176     } else {
1177       sprintf(buf, _("Unknown initialMode %s"), appData.initialMode);
1178       DisplayFatalError(buf, 0, 2);
1179       return;
1180     }
1181
1182     if (appData.matchMode) {
1183         /* Set up machine vs. machine match */
1184         if (appData.noChessProgram) {
1185             DisplayFatalError(_("Can't have a match with no chess programs"),
1186                               0, 2);
1187             return;
1188         }
1189         matchMode = TRUE;
1190         matchGame = 1;
1191         if (*appData.loadGameFile != NULLCHAR) {
1192             int index = appData.loadGameIndex; // [HGM] autoinc
1193             if(index<0) lastIndex = index = 1;
1194             if (!LoadGameFromFile(appData.loadGameFile,
1195                                   index,
1196                                   appData.loadGameFile, FALSE)) {
1197                 DisplayFatalError(_("Bad game file"), 0, 1);
1198                 return;
1199             }
1200         } else if (*appData.loadPositionFile != NULLCHAR) {
1201             int index = appData.loadPositionIndex; // [HGM] autoinc
1202             if(index<0) lastIndex = index = 1;
1203             if (!LoadPositionFromFile(appData.loadPositionFile,
1204                                       index,
1205                                       appData.loadPositionFile)) {
1206                 DisplayFatalError(_("Bad position file"), 0, 1);
1207                 return;
1208             }
1209         }
1210         TwoMachinesEvent();
1211     } else if (*appData.cmailGameName != NULLCHAR) {
1212         /* Set up cmail mode */
1213         ReloadCmailMsgEvent(TRUE);
1214     } else {
1215         /* Set up other modes */
1216         if (initialMode == AnalyzeFile) {
1217           if (*appData.loadGameFile == NULLCHAR) {
1218             DisplayFatalError(_("AnalyzeFile mode requires a game file"), 0, 1);
1219             return;
1220           }
1221         }
1222         if (*appData.loadGameFile != NULLCHAR) {
1223             (void) LoadGameFromFile(appData.loadGameFile,
1224                                     appData.loadGameIndex,
1225                                     appData.loadGameFile, TRUE);
1226         } else if (*appData.loadPositionFile != NULLCHAR) {
1227             (void) LoadPositionFromFile(appData.loadPositionFile,
1228                                         appData.loadPositionIndex,
1229                                         appData.loadPositionFile);
1230             /* [HGM] try to make self-starting even after FEN load */
1231             /* to allow automatic setup of fairy variants with wtm */
1232             if(initialMode == BeginningOfGame && !blackPlaysFirst) {
1233                 gameMode = BeginningOfGame;
1234                 setboardSpoiledMachineBlack = 1;
1235             }
1236             /* [HGM] loadPos: make that every new game uses the setup */
1237             /* from file as long as we do not switch variant          */
1238             if(!blackPlaysFirst) {
1239                 startedFromPositionFile = TRUE;
1240                 CopyBoard(filePosition, boards[0]);
1241             }
1242         }
1243         if (initialMode == AnalyzeMode) {
1244           if (appData.noChessProgram) {
1245             DisplayFatalError(_("Analysis mode requires a chess engine"), 0, 2);
1246             return;
1247           }
1248           if (appData.icsActive) {
1249             DisplayFatalError(_("Analysis mode does not work with ICS mode"),0,2);
1250             return;
1251           }
1252           AnalyzeModeEvent();
1253         } else if (initialMode == AnalyzeFile) {
1254           appData.showThinking = TRUE; // [HGM] thinking: moved out of ShowThinkingEvent
1255           ShowThinkingEvent();
1256           AnalyzeFileEvent();
1257           AnalysisPeriodicEvent(1);
1258         } else if (initialMode == MachinePlaysWhite) {
1259           if (appData.noChessProgram) {
1260             DisplayFatalError(_("MachineWhite mode requires a chess engine"),
1261                               0, 2);
1262             return;
1263           }
1264           if (appData.icsActive) {
1265             DisplayFatalError(_("MachineWhite mode does not work with ICS mode"),
1266                               0, 2);
1267             return;
1268           }
1269           MachineWhiteEvent();
1270         } else if (initialMode == MachinePlaysBlack) {
1271           if (appData.noChessProgram) {
1272             DisplayFatalError(_("MachineBlack mode requires a chess engine"),
1273                               0, 2);
1274             return;
1275           }
1276           if (appData.icsActive) {
1277             DisplayFatalError(_("MachineBlack mode does not work with ICS mode"),
1278                               0, 2);
1279             return;
1280           }
1281           MachineBlackEvent();
1282         } else if (initialMode == TwoMachinesPlay) {
1283           if (appData.noChessProgram) {
1284             DisplayFatalError(_("TwoMachines mode requires a chess engine"),
1285                               0, 2);
1286             return;
1287           }
1288           if (appData.icsActive) {
1289             DisplayFatalError(_("TwoMachines mode does not work with ICS mode"),
1290                               0, 2);
1291             return;
1292           }
1293           TwoMachinesEvent();
1294         } else if (initialMode == EditGame) {
1295           EditGameEvent();
1296         } else if (initialMode == EditPosition) {
1297           EditPositionEvent();
1298         } else if (initialMode == Training) {
1299           if (*appData.loadGameFile == NULLCHAR) {
1300             DisplayFatalError(_("Training mode requires a game file"), 0, 2);
1301             return;
1302           }
1303           TrainingEvent();
1304         }
1305     }
1306 }
1307
1308 /*
1309  * Establish will establish a contact to a remote host.port.
1310  * Sets icsPR to a ProcRef for a process (or pseudo-process)
1311  *  used to talk to the host.
1312  * Returns 0 if okay, error code if not.
1313  */
1314 int
1315 establish()
1316 {
1317     char buf[MSG_SIZ];
1318
1319     if (*appData.icsCommPort != NULLCHAR) {
1320         /* Talk to the host through a serial comm port */
1321         return OpenCommPort(appData.icsCommPort, &icsPR);
1322
1323     } else if (*appData.gateway != NULLCHAR) {
1324         if (*appData.remoteShell == NULLCHAR) {
1325             /* Use the rcmd protocol to run telnet program on a gateway host */
1326             snprintf(buf, sizeof(buf), "%s %s %s",
1327                     appData.telnetProgram, appData.icsHost, appData.icsPort);
1328             return OpenRcmd(appData.gateway, appData.remoteUser, buf, &icsPR);
1329
1330         } else {
1331             /* Use the rsh program to run telnet program on a gateway host */
1332             if (*appData.remoteUser == NULLCHAR) {
1333                 snprintf(buf, sizeof(buf), "%s %s %s %s %s", appData.remoteShell,
1334                         appData.gateway, appData.telnetProgram,
1335                         appData.icsHost, appData.icsPort);
1336             } else {
1337                 snprintf(buf, sizeof(buf), "%s %s -l %s %s %s %s",
1338                         appData.remoteShell, appData.gateway, 
1339                         appData.remoteUser, appData.telnetProgram,
1340                         appData.icsHost, appData.icsPort);
1341             }
1342             return StartChildProcess(buf, "", &icsPR);
1343
1344         }
1345     } else if (appData.useTelnet) {
1346         return OpenTelnet(appData.icsHost, appData.icsPort, &icsPR);
1347
1348     } else {
1349         /* TCP socket interface differs somewhat between
1350            Unix and NT; handle details in the front end.
1351            */
1352         return OpenTCP(appData.icsHost, appData.icsPort, &icsPR);
1353     }
1354 }
1355
1356 void EscapeExpand(char *p, char *q)
1357 {       // [HGM] initstring: routine to shape up string arguments
1358         while(*p++ = *q++) if(p[-1] == '\\')
1359             switch(*q++) {
1360                 case 'n': p[-1] = '\n'; break;
1361                 case 'r': p[-1] = '\r'; break;
1362                 case 't': p[-1] = '\t'; break;
1363                 case '\\': p[-1] = '\\'; break;
1364                 case 0: *p = 0; return;
1365                 default: p[-1] = q[-1]; break;
1366             }
1367 }
1368
1369 void
1370 show_bytes(fp, buf, count)
1371      FILE *fp;
1372      char *buf;
1373      int count;
1374 {
1375     while (count--) {
1376         if (*buf < 040 || *(unsigned char *) buf > 0177) {
1377             fprintf(fp, "\\%03o", *buf & 0xff);
1378         } else {
1379             putc(*buf, fp);
1380         }
1381         buf++;
1382     }
1383     fflush(fp);
1384 }
1385
1386 /* Returns an errno value */
1387 int
1388 OutputMaybeTelnet(pr, message, count, outError)
1389      ProcRef pr;
1390      char *message;
1391      int count;
1392      int *outError;
1393 {
1394     char buf[8192], *p, *q, *buflim;
1395     int left, newcount, outcount;
1396
1397     if (*appData.icsCommPort != NULLCHAR || appData.useTelnet ||
1398         *appData.gateway != NULLCHAR) {
1399         if (appData.debugMode) {
1400             fprintf(debugFP, ">ICS: ");
1401             show_bytes(debugFP, message, count);
1402             fprintf(debugFP, "\n");
1403         }
1404         return OutputToProcess(pr, message, count, outError);
1405     }
1406
1407     buflim = &buf[sizeof(buf)-1]; /* allow 1 byte for expanding last char */
1408     p = message;
1409     q = buf;
1410     left = count;
1411     newcount = 0;
1412     while (left) {
1413         if (q >= buflim) {
1414             if (appData.debugMode) {
1415                 fprintf(debugFP, ">ICS: ");
1416                 show_bytes(debugFP, buf, newcount);
1417                 fprintf(debugFP, "\n");
1418             }
1419             outcount = OutputToProcess(pr, buf, newcount, outError);
1420             if (outcount < newcount) return -1; /* to be sure */
1421             q = buf;
1422             newcount = 0;
1423         }
1424         if (*p == '\n') {
1425             *q++ = '\r';
1426             newcount++;
1427         } else if (((unsigned char) *p) == TN_IAC) {
1428             *q++ = (char) TN_IAC;
1429             newcount ++;
1430         }
1431         *q++ = *p++;
1432         newcount++;
1433         left--;
1434     }
1435     if (appData.debugMode) {
1436         fprintf(debugFP, ">ICS: ");
1437         show_bytes(debugFP, buf, newcount);
1438         fprintf(debugFP, "\n");
1439     }
1440     outcount = OutputToProcess(pr, buf, newcount, outError);
1441     if (outcount < newcount) return -1; /* to be sure */
1442     return count;
1443 }
1444
1445 void
1446 read_from_player(isr, closure, message, count, error)
1447      InputSourceRef isr;
1448      VOIDSTAR closure;
1449      char *message;
1450      int count;
1451      int error;
1452 {
1453     int outError, outCount;
1454     static int gotEof = 0;
1455
1456     /* Pass data read from player on to ICS */
1457     if (count > 0) {
1458         gotEof = 0;
1459         outCount = OutputMaybeTelnet(icsPR, message, count, &outError);
1460         if (outCount < count) {
1461             DisplayFatalError(_("Error writing to ICS"), outError, 1);
1462         }
1463     } else if (count < 0) {
1464         RemoveInputSource(isr);
1465         DisplayFatalError(_("Error reading from keyboard"), error, 1);
1466     } else if (gotEof++ > 0) {
1467         RemoveInputSource(isr);
1468         DisplayFatalError(_("Got end of file from keyboard"), 0, 0);
1469     }
1470 }
1471
1472 void
1473 KeepAlive()
1474 {   // [HGM] alive: periodically send dummy (date) command to ICS to prevent time-out
1475     if(!connectionAlive) DisplayFatalError("No response from ICS", 0, 1);
1476     connectionAlive = FALSE; // only sticks if no response to 'date' command.
1477     SendToICS("date\n");
1478     if(appData.keepAlive) ScheduleDelayedEvent(KeepAlive, appData.keepAlive*60*1000);
1479 }
1480
1481 /* added routine for printf style output to ics */
1482 void ics_printf(char *format, ...)
1483 {
1484     char buffer[MSG_SIZ];
1485     va_list args;
1486
1487     va_start(args, format);
1488     vsnprintf(buffer, sizeof(buffer), format, args);
1489     buffer[sizeof(buffer)-1] = '\0';
1490     SendToICS(buffer);
1491     va_end(args);
1492 }
1493
1494 void
1495 SendToICS(s)
1496      char *s;
1497 {
1498     int count, outCount, outError;
1499
1500     if (icsPR == NULL) return;
1501
1502     count = strlen(s);
1503     outCount = OutputMaybeTelnet(icsPR, s, count, &outError);
1504     if (outCount < count) {
1505         DisplayFatalError(_("Error writing to ICS"), outError, 1);
1506     }
1507 }
1508
1509 /* This is used for sending logon scripts to the ICS. Sending
1510    without a delay causes problems when using timestamp on ICC
1511    (at least on my machine). */
1512 void
1513 SendToICSDelayed(s,msdelay)
1514      char *s;
1515      long msdelay;
1516 {
1517     int count, outCount, outError;
1518
1519     if (icsPR == NULL) return;
1520
1521     count = strlen(s);
1522     if (appData.debugMode) {
1523         fprintf(debugFP, ">ICS: ");
1524         show_bytes(debugFP, s, count);
1525         fprintf(debugFP, "\n");
1526     }
1527     outCount = OutputToProcessDelayed(icsPR, s, count, &outError,
1528                                       msdelay);
1529     if (outCount < count) {
1530         DisplayFatalError(_("Error writing to ICS"), outError, 1);
1531     }
1532 }
1533
1534
1535 /* Remove all highlighting escape sequences in s
1536    Also deletes any suffix starting with '(' 
1537    */
1538 char *
1539 StripHighlightAndTitle(s)
1540      char *s;
1541 {
1542     static char retbuf[MSG_SIZ];
1543     char *p = retbuf;
1544
1545     while (*s != NULLCHAR) {
1546         while (*s == '\033') {
1547             while (*s != NULLCHAR && !isalpha(*s)) s++;
1548             if (*s != NULLCHAR) s++;
1549         }
1550         while (*s != NULLCHAR && *s != '\033') {
1551             if (*s == '(' || *s == '[') {
1552                 *p = NULLCHAR;
1553                 return retbuf;
1554             }
1555             *p++ = *s++;
1556         }
1557     }
1558     *p = NULLCHAR;
1559     return retbuf;
1560 }
1561
1562 /* Remove all highlighting escape sequences in s */
1563 char *
1564 StripHighlight(s)
1565      char *s;
1566 {
1567     static char retbuf[MSG_SIZ];
1568     char *p = retbuf;
1569
1570     while (*s != NULLCHAR) {
1571         while (*s == '\033') {
1572             while (*s != NULLCHAR && !isalpha(*s)) s++;
1573             if (*s != NULLCHAR) s++;
1574         }
1575         while (*s != NULLCHAR && *s != '\033') {
1576             *p++ = *s++;
1577         }
1578     }
1579     *p = NULLCHAR;
1580     return retbuf;
1581 }
1582
1583 char *variantNames[] = VARIANT_NAMES;
1584 char *
1585 VariantName(v)
1586      VariantClass v;
1587 {
1588     return variantNames[v];
1589 }
1590
1591
1592 /* Identify a variant from the strings the chess servers use or the
1593    PGN Variant tag names we use. */
1594 VariantClass
1595 StringToVariant(e)
1596      char *e;
1597 {
1598     char *p;
1599     int wnum = -1;
1600     VariantClass v = VariantNormal;
1601     int i, found = FALSE;
1602     char buf[MSG_SIZ];
1603
1604     if (!e) return v;
1605
1606     /* [HGM] skip over optional board-size prefixes */
1607     if( sscanf(e, "%dx%d_", &i, &i) == 2 ||
1608         sscanf(e, "%dx%d+%d_", &i, &i, &i) == 3 ) {
1609         while( *e++ != '_');
1610     }
1611
1612     if(StrCaseStr(e, "misc/")) { // [HGM] on FICS, misc/shogi is not shogi
1613         v = VariantNormal;
1614         found = TRUE;
1615     } else
1616     for (i=0; i<sizeof(variantNames)/sizeof(char*); i++) {
1617       if (StrCaseStr(e, variantNames[i])) {
1618         v = (VariantClass) i;
1619         found = TRUE;
1620         break;
1621       }
1622     }
1623
1624     if (!found) {
1625       if ((StrCaseStr(e, "fischer") && StrCaseStr(e, "random"))
1626           || StrCaseStr(e, "wild/fr") 
1627           || StrCaseStr(e, "frc") || StrCaseStr(e, "960")) {
1628         v = VariantFischeRandom;
1629       } else if ((i = 4, p = StrCaseStr(e, "wild")) ||
1630                  (i = 1, p = StrCaseStr(e, "w"))) {
1631         p += i;
1632         while (*p && (isspace(*p) || *p == '(' || *p == '/')) p++;
1633         if (isdigit(*p)) {
1634           wnum = atoi(p);
1635         } else {
1636           wnum = -1;
1637         }
1638         switch (wnum) {
1639         case 0: /* FICS only, actually */
1640         case 1:
1641           /* Castling legal even if K starts on d-file */
1642           v = VariantWildCastle;
1643           break;
1644         case 2:
1645         case 3:
1646         case 4:
1647           /* Castling illegal even if K & R happen to start in
1648              normal positions. */
1649           v = VariantNoCastle;
1650           break;
1651         case 5:
1652         case 7:
1653         case 8:
1654         case 10:
1655         case 11:
1656         case 12:
1657         case 13:
1658         case 14:
1659         case 15:
1660         case 18:
1661         case 19:
1662           /* Castling legal iff K & R start in normal positions */
1663           v = VariantNormal;
1664           break;
1665         case 6:
1666         case 20:
1667         case 21:
1668           /* Special wilds for position setup; unclear what to do here */
1669           v = VariantLoadable;
1670           break;
1671         case 9:
1672           /* Bizarre ICC game */
1673           v = VariantTwoKings;
1674           break;
1675         case 16:
1676           v = VariantKriegspiel;
1677           break;
1678         case 17:
1679           v = VariantLosers;
1680           break;
1681         case 22:
1682           v = VariantFischeRandom;
1683           break;
1684         case 23:
1685           v = VariantCrazyhouse;
1686           break;
1687         case 24:
1688           v = VariantBughouse;
1689           break;
1690         case 25:
1691           v = Variant3Check;
1692           break;
1693         case 26:
1694           /* Not quite the same as FICS suicide! */
1695           v = VariantGiveaway;
1696           break;
1697         case 27:
1698           v = VariantAtomic;
1699           break;
1700         case 28:
1701           v = VariantShatranj;
1702           break;
1703
1704         /* Temporary names for future ICC types.  The name *will* change in 
1705            the next xboard/WinBoard release after ICC defines it. */
1706         case 29:
1707           v = Variant29;
1708           break;
1709         case 30:
1710           v = Variant30;
1711           break;
1712         case 31:
1713           v = Variant31;
1714           break;
1715         case 32:
1716           v = Variant32;
1717           break;
1718         case 33:
1719           v = Variant33;
1720           break;
1721         case 34:
1722           v = Variant34;
1723           break;
1724         case 35:
1725           v = Variant35;
1726           break;
1727         case 36:
1728           v = Variant36;
1729           break;
1730         case 37:
1731           v = VariantShogi;
1732           break;
1733         case 38:
1734           v = VariantXiangqi;
1735           break;
1736         case 39:
1737           v = VariantCourier;
1738           break;
1739         case 40:
1740           v = VariantGothic;
1741           break;
1742         case 41:
1743           v = VariantCapablanca;
1744           break;
1745         case 42:
1746           v = VariantKnightmate;
1747           break;
1748         case 43:
1749           v = VariantFairy;
1750           break;
1751         case 44:
1752           v = VariantCylinder;
1753           break;
1754         case 45:
1755           v = VariantFalcon;
1756           break;
1757         case 46:
1758           v = VariantCapaRandom;
1759           break;
1760         case 47:
1761           v = VariantBerolina;
1762           break;
1763         case 48:
1764           v = VariantJanus;
1765           break;
1766         case 49:
1767           v = VariantSuper;
1768           break;
1769         case 50:
1770           v = VariantGreat;
1771           break;
1772         case -1:
1773           /* Found "wild" or "w" in the string but no number;
1774              must assume it's normal chess. */
1775           v = VariantNormal;
1776           break;
1777         default:
1778           sprintf(buf, _("Unknown wild type %d"), wnum);
1779           DisplayError(buf, 0);
1780           v = VariantUnknown;
1781           break;
1782         }
1783       }
1784     }
1785     if (appData.debugMode) {
1786       fprintf(debugFP, _("recognized '%s' (%d) as variant %s\n"),
1787               e, wnum, VariantName(v));
1788     }
1789     return v;
1790 }
1791
1792 static int leftover_start = 0, leftover_len = 0;
1793 char star_match[STAR_MATCH_N][MSG_SIZ];
1794
1795 /* Test whether pattern is present at &buf[*index]; if so, return TRUE,
1796    advance *index beyond it, and set leftover_start to the new value of
1797    *index; else return FALSE.  If pattern contains the character '*', it
1798    matches any sequence of characters not containing '\r', '\n', or the
1799    character following the '*' (if any), and the matched sequence(s) are
1800    copied into star_match.
1801    */
1802 int
1803 looking_at(buf, index, pattern)
1804      char *buf;
1805      int *index;
1806      char *pattern;
1807 {
1808     char *bufp = &buf[*index], *patternp = pattern;
1809     int star_count = 0;
1810     char *matchp = star_match[0];
1811     
1812     for (;;) {
1813         if (*patternp == NULLCHAR) {
1814             *index = leftover_start = bufp - buf;
1815             *matchp = NULLCHAR;
1816             return TRUE;
1817         }
1818         if (*bufp == NULLCHAR) return FALSE;
1819         if (*patternp == '*') {
1820             if (*bufp == *(patternp + 1)) {
1821                 *matchp = NULLCHAR;
1822                 matchp = star_match[++star_count];
1823                 patternp += 2;
1824                 bufp++;
1825                 continue;
1826             } else if (*bufp == '\n' || *bufp == '\r') {
1827                 patternp++;
1828                 if (*patternp == NULLCHAR)
1829                   continue;
1830                 else
1831                   return FALSE;
1832             } else {
1833                 *matchp++ = *bufp++;
1834                 continue;
1835             }
1836         }
1837         if (*patternp != *bufp) return FALSE;
1838         patternp++;
1839         bufp++;
1840     }
1841 }
1842
1843 void
1844 SendToPlayer(data, length)
1845      char *data;
1846      int length;
1847 {
1848     int error, outCount;
1849     outCount = OutputToProcess(NoProc, data, length, &error);
1850     if (outCount < length) {
1851         DisplayFatalError(_("Error writing to display"), error, 1);
1852     }
1853 }
1854
1855 void
1856 PackHolding(packed, holding)
1857      char packed[];
1858      char *holding;
1859 {
1860     char *p = holding;
1861     char *q = packed;
1862     int runlength = 0;
1863     int curr = 9999;
1864     do {
1865         if (*p == curr) {
1866             runlength++;
1867         } else {
1868             switch (runlength) {
1869               case 0:
1870                 break;
1871               case 1:
1872                 *q++ = curr;
1873                 break;
1874               case 2:
1875                 *q++ = curr;
1876                 *q++ = curr;
1877                 break;
1878               default:
1879                 sprintf(q, "%d", runlength);
1880                 while (*q) q++;
1881                 *q++ = curr;
1882                 break;
1883             }
1884             runlength = 1;
1885             curr = *p;
1886         }
1887     } while (*p++);
1888     *q = NULLCHAR;
1889 }
1890
1891 /* Telnet protocol requests from the front end */
1892 void
1893 TelnetRequest(ddww, option)
1894      unsigned char ddww, option;
1895 {
1896     unsigned char msg[3];
1897     int outCount, outError;
1898
1899     if (*appData.icsCommPort != NULLCHAR || appData.useTelnet) return;
1900
1901     if (appData.debugMode) {
1902         char buf1[8], buf2[8], *ddwwStr, *optionStr;
1903         switch (ddww) {
1904           case TN_DO:
1905             ddwwStr = "DO";
1906             break;
1907           case TN_DONT:
1908             ddwwStr = "DONT";
1909             break;
1910           case TN_WILL:
1911             ddwwStr = "WILL";
1912             break;
1913           case TN_WONT:
1914             ddwwStr = "WONT";
1915             break;
1916           default:
1917             ddwwStr = buf1;
1918             sprintf(buf1, "%d", ddww);
1919             break;
1920         }
1921         switch (option) {
1922           case TN_ECHO:
1923             optionStr = "ECHO";
1924             break;
1925           default:
1926             optionStr = buf2;
1927             sprintf(buf2, "%d", option);
1928             break;
1929         }
1930         fprintf(debugFP, ">%s %s ", ddwwStr, optionStr);
1931     }
1932     msg[0] = TN_IAC;
1933     msg[1] = ddww;
1934     msg[2] = option;
1935     outCount = OutputToProcess(icsPR, (char *)msg, 3, &outError);
1936     if (outCount < 3) {
1937         DisplayFatalError(_("Error writing to ICS"), outError, 1);
1938     }
1939 }
1940
1941 void
1942 DoEcho()
1943 {
1944     if (!appData.icsActive) return;
1945     TelnetRequest(TN_DO, TN_ECHO);
1946 }
1947
1948 void
1949 DontEcho()
1950 {
1951     if (!appData.icsActive) return;
1952     TelnetRequest(TN_DONT, TN_ECHO);
1953 }
1954
1955 void
1956 CopyHoldings(Board board, char *holdings, ChessSquare lowestPiece)
1957 {
1958     /* put the holdings sent to us by the server on the board holdings area */
1959     int i, j, holdingsColumn, holdingsStartRow, direction, countsColumn;
1960     char p;
1961     ChessSquare piece;
1962
1963     if(gameInfo.holdingsWidth < 2)  return;
1964     if(gameInfo.variant != VariantBughouse && board[HOLDINGS_SET])
1965         return; // prevent overwriting by pre-board holdings
1966
1967     if( (int)lowestPiece >= BlackPawn ) {
1968         holdingsColumn = 0;
1969         countsColumn = 1;
1970         holdingsStartRow = BOARD_HEIGHT-1;
1971         direction = -1;
1972     } else {
1973         holdingsColumn = BOARD_WIDTH-1;
1974         countsColumn = BOARD_WIDTH-2;
1975         holdingsStartRow = 0;
1976         direction = 1;
1977     }
1978
1979     for(i=0; i<BOARD_HEIGHT; i++) { /* clear holdings */
1980         board[i][holdingsColumn] = EmptySquare;
1981         board[i][countsColumn]   = (ChessSquare) 0;
1982     }
1983     while( (p=*holdings++) != NULLCHAR ) {
1984         piece = CharToPiece( ToUpper(p) );
1985         if(piece == EmptySquare) continue;
1986         /*j = (int) piece - (int) WhitePawn;*/
1987         j = PieceToNumber(piece);
1988         if(j >= gameInfo.holdingsSize) continue; /* ignore pieces that do not fit */
1989         if(j < 0) continue;               /* should not happen */
1990         piece = (ChessSquare) ( (int)piece + (int)lowestPiece );
1991         board[holdingsStartRow+j*direction][holdingsColumn] = piece;
1992         board[holdingsStartRow+j*direction][countsColumn]++;
1993     }
1994 }
1995
1996
1997 void
1998 VariantSwitch(Board board, VariantClass newVariant)
1999 {
2000    int newHoldingsWidth, newWidth = 8, newHeight = 8, i, j;
2001    static Board oldBoard;
2002
2003    startedFromPositionFile = FALSE;
2004    if(gameInfo.variant == newVariant) return;
2005
2006    /* [HGM] This routine is called each time an assignment is made to
2007     * gameInfo.variant during a game, to make sure the board sizes
2008     * are set to match the new variant. If that means adding or deleting
2009     * holdings, we shift the playing board accordingly
2010     * This kludge is needed because in ICS observe mode, we get boards
2011     * of an ongoing game without knowing the variant, and learn about the
2012     * latter only later. This can be because of the move list we requested,
2013     * in which case the game history is refilled from the beginning anyway,
2014     * but also when receiving holdings of a crazyhouse game. In the latter
2015     * case we want to add those holdings to the already received position.
2016     */
2017
2018    
2019    if (appData.debugMode) {
2020      fprintf(debugFP, "Switch board from %s to %s\n",
2021              VariantName(gameInfo.variant), VariantName(newVariant));
2022      setbuf(debugFP, NULL);
2023    }
2024    shuffleOpenings = 0;       /* [HGM] shuffle */
2025    gameInfo.holdingsSize = 5; /* [HGM] prepare holdings */
2026    switch(newVariant) 
2027      {
2028      case VariantShogi:
2029        newWidth = 9;  newHeight = 9;
2030        gameInfo.holdingsSize = 7;
2031      case VariantBughouse:
2032      case VariantCrazyhouse:
2033        newHoldingsWidth = 2; break;
2034      case VariantGreat:
2035        newWidth = 10;
2036      case VariantSuper:
2037        newHoldingsWidth = 2;
2038        gameInfo.holdingsSize = 8;
2039        break;
2040      case VariantGothic:
2041      case VariantCapablanca:
2042      case VariantCapaRandom:
2043        newWidth = 10;
2044      default:
2045        newHoldingsWidth = gameInfo.holdingsSize = 0;
2046      };
2047    
2048    if(newWidth  != gameInfo.boardWidth  ||
2049       newHeight != gameInfo.boardHeight ||
2050       newHoldingsWidth != gameInfo.holdingsWidth ) {
2051      
2052      /* shift position to new playing area, if needed */
2053      if(newHoldingsWidth > gameInfo.holdingsWidth) {
2054        for(i=0; i<BOARD_HEIGHT; i++) 
2055          for(j=BOARD_RGHT-1; j>=BOARD_LEFT; j--)
2056            board[i][j+newHoldingsWidth-gameInfo.holdingsWidth] =
2057              board[i][j];
2058        for(i=0; i<newHeight; i++) {
2059          board[i][0] = board[i][newWidth+2*newHoldingsWidth-1] = EmptySquare;
2060          board[i][1] = board[i][newWidth+2*newHoldingsWidth-2] = (ChessSquare) 0;
2061        }
2062      } else if(newHoldingsWidth < gameInfo.holdingsWidth) {
2063        for(i=0; i<BOARD_HEIGHT; i++)
2064          for(j=BOARD_LEFT; j<BOARD_RGHT; j++)
2065            board[i][j+newHoldingsWidth-gameInfo.holdingsWidth] =
2066              board[i][j];
2067      }
2068      gameInfo.boardWidth  = newWidth;
2069      gameInfo.boardHeight = newHeight;
2070      gameInfo.holdingsWidth = newHoldingsWidth;
2071      gameInfo.variant = newVariant;
2072      InitDrawingSizes(-2, 0);
2073    } else gameInfo.variant = newVariant;
2074    CopyBoard(oldBoard, board);   // remember correctly formatted board
2075      InitPosition(FALSE);          /* this sets up board[0], but also other stuff        */
2076    DrawPosition(TRUE, currentMove ? boards[currentMove] : oldBoard);
2077 }
2078
2079 static int loggedOn = FALSE;
2080
2081 /*-- Game start info cache: --*/
2082 int gs_gamenum;
2083 char gs_kind[MSG_SIZ];
2084 static char player1Name[128] = "";
2085 static char player2Name[128] = "";
2086 static char cont_seq[] = "\n\\   ";
2087 static int player1Rating = -1;
2088 static int player2Rating = -1;
2089 /*----------------------------*/
2090
2091 ColorClass curColor = ColorNormal;
2092 int suppressKibitz = 0;
2093
2094 // [HGM] seekgraph
2095 Boolean soughtPending = FALSE;
2096 Boolean seekGraphUp;
2097 #define MAX_SEEK_ADS 200
2098 #define SQUARE 0x80
2099 char *seekAdList[MAX_SEEK_ADS];
2100 int ratingList[MAX_SEEK_ADS], xList[MAX_SEEK_ADS], yList[MAX_SEEK_ADS], seekNrList[MAX_SEEK_ADS], zList[MAX_SEEK_ADS];
2101 float tcList[MAX_SEEK_ADS];
2102 char colorList[MAX_SEEK_ADS];
2103 int nrOfSeekAds = 0;
2104 int minRating = 1010, maxRating = 2800;
2105 int hMargin = 10, vMargin = 20, h, w;
2106 extern int squareSize, lineGap;
2107
2108 void
2109 PlotSeekAd(int i)
2110 {
2111         int x, y, color = 0, r = ratingList[i]; float tc = tcList[i];
2112         xList[i] = yList[i] = -100; // outside graph, so cannot be clicked
2113         if(r < minRating+100 && r >=0 ) r = minRating+100;
2114         if(r > maxRating) r = maxRating;
2115         if(tc < 1.) tc = 1.;
2116         if(tc > 95.) tc = 95.;
2117         x = (w-hMargin-squareSize/8-7)* log(tc)/log(95.) + hMargin;
2118         y = ((double)r - minRating)/(maxRating - minRating)
2119             * (h-vMargin-squareSize/8-1) + vMargin;
2120         if(ratingList[i] < 0) y = vMargin + squareSize/4;
2121         if(strstr(seekAdList[i], " u ")) color = 1;
2122         if(!strstr(seekAdList[i], "lightning") && // for now all wilds same color
2123            !strstr(seekAdList[i], "bullet") &&
2124            !strstr(seekAdList[i], "blitz") &&
2125            !strstr(seekAdList[i], "standard") ) color = 2;
2126         if(strstr(seekAdList[i], "(C) ")) color |= SQUARE; // plot computer seeks as squares
2127         DrawSeekDot(xList[i]=x+3*(color&~SQUARE), yList[i]=h-1-y, colorList[i]=color);
2128 }
2129
2130 void
2131 AddAd(char *handle, char *rating, int base, int inc,  char rated, char *type, int nr, Boolean plot)
2132 {
2133         char buf[MSG_SIZ], *ext = "";
2134         VariantClass v = StringToVariant(type);
2135         if(strstr(type, "wild")) {
2136             ext = type + 4; // append wild number
2137             if(v == VariantFischeRandom) type = "chess960"; else
2138             if(v == VariantLoadable) type = "setup"; else
2139             type = VariantName(v);
2140         }
2141         sprintf(buf, "%s (%s) %d %d %c %s%s", handle, rating, base, inc, rated, type, ext);
2142         if(nrOfSeekAds < MAX_SEEK_ADS-1) {
2143             if(seekAdList[nrOfSeekAds]) free(seekAdList[nrOfSeekAds]);
2144             ratingList[nrOfSeekAds] = -1; // for if seeker has no rating
2145             sscanf(rating, "%d", &ratingList[nrOfSeekAds]);
2146             tcList[nrOfSeekAds] = base + (2./3.)*inc;
2147             seekNrList[nrOfSeekAds] = nr;
2148             zList[nrOfSeekAds] = 0;
2149             seekAdList[nrOfSeekAds++] = StrSave(buf);
2150             if(plot) PlotSeekAd(nrOfSeekAds-1);
2151         }
2152 }
2153
2154 void
2155 EraseSeekDot(int i)
2156 {
2157     int x = xList[i], y = yList[i], d=squareSize/4, k;
2158     DrawSeekBackground(x-squareSize/8, y-squareSize/8, x+squareSize/8+1, y+squareSize/8+1);
2159     if(x < hMargin+d) DrawSeekAxis(hMargin, y-squareSize/8, hMargin, y+squareSize/8+1);
2160     // now replot every dot that overlapped
2161     for(k=0; k<nrOfSeekAds; k++) if(k != i) {
2162         int xx = xList[k], yy = yList[k];
2163         if(xx <= x+d && xx > x-d && yy <= y+d && yy > y-d)
2164             DrawSeekDot(xx, yy, colorList[k]);
2165     }
2166 }
2167
2168 void
2169 RemoveSeekAd(int nr)
2170 {
2171         int i;
2172         for(i=0; i<nrOfSeekAds; i++) if(seekNrList[i] == nr) {
2173             EraseSeekDot(i);
2174             if(seekAdList[i]) free(seekAdList[i]);
2175             seekAdList[i] = seekAdList[--nrOfSeekAds];
2176             seekNrList[i] = seekNrList[nrOfSeekAds];
2177             ratingList[i] = ratingList[nrOfSeekAds];
2178             colorList[i]  = colorList[nrOfSeekAds];
2179             tcList[i] = tcList[nrOfSeekAds];
2180             xList[i]  = xList[nrOfSeekAds];
2181             yList[i]  = yList[nrOfSeekAds];
2182             zList[i]  = zList[nrOfSeekAds];
2183             seekAdList[nrOfSeekAds] = NULL;
2184             break;
2185         }
2186 }
2187
2188 Boolean
2189 MatchSoughtLine(char *line)
2190 {
2191     char handle[MSG_SIZ], rating[MSG_SIZ], type[MSG_SIZ];
2192     int nr, base, inc, u=0; char dummy;
2193
2194     if(sscanf(line, "%d %s %s %d %d rated %s", &nr, rating, handle, &base, &inc, type) == 6 ||
2195        sscanf(line, "%d %s %s %s %d %d rated %c", &nr, rating, handle, type, &base, &inc, &dummy) == 7 ||
2196        (u=1) &&
2197        (sscanf(line, "%d %s %s %d %d unrated %s", &nr, rating, handle, &base, &inc, type) == 6 ||
2198         sscanf(line, "%d %s %s %s %d %d unrated %c", &nr, rating, handle, type, &base, &inc, &dummy) == 7)  ) {
2199         // match: compact and save the line
2200         AddAd(handle, rating, base, inc, u ? 'u' : 'r', type, nr, FALSE);
2201         return TRUE;
2202     }
2203     return FALSE;
2204 }
2205
2206 int
2207 DrawSeekGraph()
2208 {
2209     int i;
2210     if(!seekGraphUp) return FALSE;
2211     h = BOARD_HEIGHT * (squareSize + lineGap) + lineGap;
2212     w = BOARD_WIDTH  * (squareSize + lineGap) + lineGap;
2213
2214     DrawSeekBackground(0, 0, w, h);
2215     DrawSeekAxis(hMargin, h-1-vMargin, w-5, h-1-vMargin);
2216     DrawSeekAxis(hMargin, h-1-vMargin, hMargin, 5);
2217     for(i=0; i<4000; i+= 100) if(i>=minRating && i<maxRating) {
2218         int yy =((double)i - minRating)/(maxRating - minRating)*(h-vMargin-squareSize/8-1) + vMargin;
2219         yy = h-1-yy;
2220         DrawSeekAxis(hMargin+5*(i%500==0), yy, hMargin-5, yy); // rating ticks
2221         if(i%500 == 0) {
2222             char buf[MSG_SIZ];
2223             sprintf(buf, "%d", i);
2224             DrawSeekText(buf, hMargin+squareSize/8+7, yy);
2225         }
2226     }
2227     DrawSeekText("unrated", hMargin+squareSize/8+7, h-1-vMargin-squareSize/4);
2228     for(i=1; i<100; i+=(i<10?1:5)) {
2229         int xx = (w-hMargin-squareSize/8-7)* log((double)i)/log(95.) + hMargin;
2230         DrawSeekAxis(xx, h-1-vMargin, xx, h-6-vMargin-3*(i%10==0)); // TC ticks
2231         if(i<=5 || (i>40 ? i%20 : i%10) == 0) {
2232             char buf[MSG_SIZ];
2233             sprintf(buf, "%d", i);
2234             DrawSeekText(buf, xx-2-3*(i>9), h-1-vMargin/2);
2235         }
2236     }
2237     for(i=0; i<nrOfSeekAds; i++) PlotSeekAd(i);
2238     return TRUE;
2239 }
2240
2241 int SeekGraphClick(ClickType click, int x, int y, int moving)
2242 {
2243     static int lastDown = 0, displayed = 0, lastSecond;
2244     if(!seekGraphUp) { // initiate cration of seek graph by requesting seek-ad list
2245         if(click == Release || moving) return FALSE;
2246         nrOfSeekAds = 0;
2247         soughtPending = TRUE;
2248         SendToICS(ics_prefix);
2249         SendToICS("sought\n"); // should this be "sought all"?
2250     } else { // issue challenge based on clicked ad
2251         int dist = 10000; int i, closest = 0, second = 0;
2252         for(i=0; i<nrOfSeekAds; i++) {
2253             int d = (x-xList[i])*(x-xList[i]) +  (y-yList[i])*(y-yList[i]) + zList[i];
2254             if(d < dist) { dist = d; closest = i; }
2255             second += (d - zList[i] < 120); // count in-range ads
2256             if(click == Press && moving != 1 && zList[i]>0) zList[i] *= 0.8; // age priority
2257         }
2258         if(dist < 120) {
2259             char buf[MSG_SIZ];
2260             second = (second > 1);
2261             if(displayed != closest || second != lastSecond) {
2262                 DisplayMessage(second ? "!" : "", seekAdList[closest]);
2263                 lastSecond = second; displayed = closest;
2264             }
2265             if(click == Press) {
2266                 if(moving == 2) zList[closest] = 100; // right-click; push to back on press
2267                 lastDown = closest;
2268                 return TRUE;
2269             } // on press 'hit', only show info
2270             if(moving == 2) return TRUE; // ignore right up-clicks on dot
2271             sprintf(buf, "play %d\n", seekNrList[closest]);
2272             SendToICS(ics_prefix);
2273             SendToICS(buf);
2274             return TRUE; // let incoming board of started game pop down the graph
2275         } else if(click == Release) { // release 'miss' is ignored
2276             zList[lastDown] = 100; // make future selection of the rejected ad more difficult
2277             if(moving == 2) { // right up-click
2278                 nrOfSeekAds = 0; // refresh graph
2279                 soughtPending = TRUE;
2280                 SendToICS(ics_prefix);
2281                 SendToICS("sought\n"); // should this be "sought all"?
2282             }
2283             return TRUE;
2284         } else if(moving) { if(displayed >= 0) DisplayMessage("", ""); displayed = -1; return TRUE; }
2285         // press miss or release hit 'pop down' seek graph
2286         seekGraphUp = FALSE;
2287         DrawPosition(TRUE, NULL);
2288     }
2289     return TRUE;
2290 }
2291
2292 void
2293 read_from_ics(isr, closure, data, count, error)
2294      InputSourceRef isr;
2295      VOIDSTAR closure;
2296      char *data;
2297      int count;
2298      int error;
2299 {
2300 #define BUF_SIZE (16*1024) /* overflowed at 8K with "inchannel 1" on FICS? */
2301 #define STARTED_NONE 0
2302 #define STARTED_MOVES 1
2303 #define STARTED_BOARD 2
2304 #define STARTED_OBSERVE 3
2305 #define STARTED_HOLDINGS 4
2306 #define STARTED_CHATTER 5
2307 #define STARTED_COMMENT 6
2308 #define STARTED_MOVES_NOHIDE 7
2309     
2310     static int started = STARTED_NONE;
2311     static char parse[20000];
2312     static int parse_pos = 0;
2313     static char buf[BUF_SIZE + 1];
2314     static int firstTime = TRUE, intfSet = FALSE;
2315     static ColorClass prevColor = ColorNormal;
2316     static int savingComment = FALSE;
2317     static int cmatch = 0; // continuation sequence match
2318     char *bp;
2319     char str[500];
2320     int i, oldi;
2321     int buf_len;
2322     int next_out;
2323     int tkind;
2324     int backup;    /* [DM] For zippy color lines */
2325     char *p;
2326     char talker[MSG_SIZ]; // [HGM] chat
2327     int channel;
2328
2329     connectionAlive = TRUE; // [HGM] alive: I think, therefore I am...
2330
2331     if (appData.debugMode) {
2332       if (!error) {
2333         fprintf(debugFP, "<ICS: ");
2334         show_bytes(debugFP, data, count);
2335         fprintf(debugFP, "\n");
2336       }
2337     }
2338
2339     if (appData.debugMode) { int f = forwardMostMove;
2340         fprintf(debugFP, "ics input %d, castling = %d %d %d %d %d %d\n", f,
2341                 boards[f][CASTLING][0],boards[f][CASTLING][1],boards[f][CASTLING][2],
2342                 boards[f][CASTLING][3],boards[f][CASTLING][4],boards[f][CASTLING][5]);
2343     }
2344     if (count > 0) {
2345         /* If last read ended with a partial line that we couldn't parse,
2346            prepend it to the new read and try again. */
2347         if (leftover_len > 0) {
2348             for (i=0; i<leftover_len; i++)
2349               buf[i] = buf[leftover_start + i];
2350         }
2351
2352     /* copy new characters into the buffer */
2353     bp = buf + leftover_len;
2354     buf_len=leftover_len;
2355     for (i=0; i<count; i++)
2356     {
2357         // ignore these
2358         if (data[i] == '\r')
2359             continue;
2360
2361         // join lines split by ICS?
2362         if (!appData.noJoin)
2363         {
2364             /*
2365                 Joining just consists of finding matches against the
2366                 continuation sequence, and discarding that sequence
2367                 if found instead of copying it.  So, until a match
2368                 fails, there's nothing to do since it might be the
2369                 complete sequence, and thus, something we don't want
2370                 copied.
2371             */
2372             if (data[i] == cont_seq[cmatch])
2373             {
2374                 cmatch++;
2375                 if (cmatch == strlen(cont_seq))
2376                 {
2377                     cmatch = 0; // complete match.  just reset the counter
2378
2379                     /*
2380                         it's possible for the ICS to not include the space
2381                         at the end of the last word, making our [correct]
2382                         join operation fuse two separate words.  the server
2383                         does this when the space occurs at the width setting.
2384                     */
2385                     if (!buf_len || buf[buf_len-1] != ' ')
2386                     {
2387                         *bp++ = ' ';
2388                         buf_len++;
2389                     }
2390                 }
2391                 continue;
2392             }
2393             else if (cmatch)
2394             {
2395                 /*
2396                     match failed, so we have to copy what matched before
2397                     falling through and copying this character.  In reality,
2398                     this will only ever be just the newline character, but
2399                     it doesn't hurt to be precise.
2400                 */
2401                 strncpy(bp, cont_seq, cmatch);
2402                 bp += cmatch;
2403                 buf_len += cmatch;
2404                 cmatch = 0;
2405             }
2406         }
2407
2408         // copy this char
2409         *bp++ = data[i];
2410         buf_len++;
2411     }
2412
2413         buf[buf_len] = NULLCHAR;
2414 //      next_out = leftover_len; // [HGM] should we set this to 0, and not print it in advance?
2415         next_out = 0;
2416         leftover_start = 0;
2417         
2418         i = 0;
2419         while (i < buf_len) {
2420             /* Deal with part of the TELNET option negotiation
2421                protocol.  We refuse to do anything beyond the
2422                defaults, except that we allow the WILL ECHO option,
2423                which ICS uses to turn off password echoing when we are
2424                directly connected to it.  We reject this option
2425                if localLineEditing mode is on (always on in xboard)
2426                and we are talking to port 23, which might be a real
2427                telnet server that will try to keep WILL ECHO on permanently.
2428              */
2429             if (buf_len - i >= 3 && (unsigned char) buf[i] == TN_IAC) {
2430                 static int remoteEchoOption = FALSE; /* telnet ECHO option */
2431                 unsigned char option;
2432                 oldi = i;
2433                 switch ((unsigned char) buf[++i]) {
2434                   case TN_WILL:
2435                     if (appData.debugMode)
2436                       fprintf(debugFP, "\n<WILL ");
2437                     switch (option = (unsigned char) buf[++i]) {
2438                       case TN_ECHO:
2439                         if (appData.debugMode)
2440                           fprintf(debugFP, "ECHO ");
2441                         /* Reply only if this is a change, according
2442                            to the protocol rules. */
2443                         if (remoteEchoOption) break;
2444                         if (appData.localLineEditing &&
2445                             atoi(appData.icsPort) == TN_PORT) {
2446                             TelnetRequest(TN_DONT, TN_ECHO);
2447                         } else {
2448                             EchoOff();
2449                             TelnetRequest(TN_DO, TN_ECHO);
2450                             remoteEchoOption = TRUE;
2451                         }
2452                         break;
2453                       default:
2454                         if (appData.debugMode)
2455                           fprintf(debugFP, "%d ", option);
2456                         /* Whatever this is, we don't want it. */
2457                         TelnetRequest(TN_DONT, option);
2458                         break;
2459                     }
2460                     break;
2461                   case TN_WONT:
2462                     if (appData.debugMode)
2463                       fprintf(debugFP, "\n<WONT ");
2464                     switch (option = (unsigned char) buf[++i]) {
2465                       case TN_ECHO:
2466                         if (appData.debugMode)
2467                           fprintf(debugFP, "ECHO ");
2468                         /* Reply only if this is a change, according
2469                            to the protocol rules. */
2470                         if (!remoteEchoOption) break;
2471                         EchoOn();
2472                         TelnetRequest(TN_DONT, TN_ECHO);
2473                         remoteEchoOption = FALSE;
2474                         break;
2475                       default:
2476                         if (appData.debugMode)
2477                           fprintf(debugFP, "%d ", (unsigned char) option);
2478                         /* Whatever this is, it must already be turned
2479                            off, because we never agree to turn on
2480                            anything non-default, so according to the
2481                            protocol rules, we don't reply. */
2482                         break;
2483                     }
2484                     break;
2485                   case TN_DO:
2486                     if (appData.debugMode)
2487                       fprintf(debugFP, "\n<DO ");
2488                     switch (option = (unsigned char) buf[++i]) {
2489                       default:
2490                         /* Whatever this is, we refuse to do it. */
2491                         if (appData.debugMode)
2492                           fprintf(debugFP, "%d ", option);
2493                         TelnetRequest(TN_WONT, option);
2494                         break;
2495                     }
2496                     break;
2497                   case TN_DONT:
2498                     if (appData.debugMode)
2499                       fprintf(debugFP, "\n<DONT ");
2500                     switch (option = (unsigned char) buf[++i]) {
2501                       default:
2502                         if (appData.debugMode)
2503                           fprintf(debugFP, "%d ", option);
2504                         /* Whatever this is, we are already not doing
2505                            it, because we never agree to do anything
2506                            non-default, so according to the protocol
2507                            rules, we don't reply. */
2508                         break;
2509                     }
2510                     break;
2511                   case TN_IAC:
2512                     if (appData.debugMode)
2513                       fprintf(debugFP, "\n<IAC ");
2514                     /* Doubled IAC; pass it through */
2515                     i--;
2516                     break;
2517                   default:
2518                     if (appData.debugMode)
2519                       fprintf(debugFP, "\n<%d ", (unsigned char) buf[i]);
2520                     /* Drop all other telnet commands on the floor */
2521                     break;
2522                 }
2523                 if (oldi > next_out)
2524                   SendToPlayer(&buf[next_out], oldi - next_out);
2525                 if (++i > next_out)
2526                   next_out = i;
2527                 continue;
2528             }
2529                 
2530             /* OK, this at least will *usually* work */
2531             if (!loggedOn && looking_at(buf, &i, "ics%")) {
2532                 loggedOn = TRUE;
2533             }
2534             
2535             if (loggedOn && !intfSet) {
2536                 if (ics_type == ICS_ICC) {
2537                   sprintf(str,
2538                           "/set-quietly interface %s\n/set-quietly style 12\n",
2539                           programVersion);
2540                   if(appData.seekGraph && appData.autoRefresh) // [HGM] seekgraph
2541                       strcat(str, "/set-2 51 1\n/set seek 1\n");
2542                 } else if (ics_type == ICS_CHESSNET) {
2543                   sprintf(str, "/style 12\n");
2544                 } else {
2545                   strcpy(str, "alias $ @\n$set interface ");
2546                   strcat(str, programVersion);
2547                   strcat(str, "\n$iset startpos 1\n$iset ms 1\n");
2548                   if(appData.seekGraph && appData.autoRefresh) // [HGM] seekgraph
2549                       strcat(str, "$iset seekremove 1\n$set seek 1\n");
2550 #ifdef WIN32
2551                   strcat(str, "$iset nohighlight 1\n");
2552 #endif
2553                   strcat(str, "$iset lock 1\n$style 12\n");
2554                 }
2555                 SendToICS(str);
2556                 NotifyFrontendLogin();
2557                 intfSet = TRUE;
2558             }
2559
2560             if (started == STARTED_COMMENT) {
2561                 /* Accumulate characters in comment */
2562                 parse[parse_pos++] = buf[i];
2563                 if (buf[i] == '\n') {
2564                     parse[parse_pos] = NULLCHAR;
2565                     if(chattingPartner>=0) {
2566                         char mess[MSG_SIZ];
2567                         sprintf(mess, "%s%s", talker, parse);
2568                         OutputChatMessage(chattingPartner, mess);
2569                         chattingPartner = -1;
2570                         next_out = i+1; // [HGM] suppress printing in ICS window
2571                     } else
2572                     if(!suppressKibitz) // [HGM] kibitz
2573                         AppendComment(forwardMostMove, StripHighlight(parse), TRUE);
2574                     else { // [HGM kibitz: divert memorized engine kibitz to engine-output window
2575                         int nrDigit = 0, nrAlph = 0, j;
2576                         if(parse_pos > MSG_SIZ - 30) // defuse unreasonably long input
2577                         { parse_pos = MSG_SIZ-30; parse[parse_pos - 1] = '\n'; }
2578                         parse[parse_pos] = NULLCHAR;
2579                         // try to be smart: if it does not look like search info, it should go to
2580                         // ICS interaction window after all, not to engine-output window.
2581                         for(j=0; j<parse_pos; j++) { // count letters and digits
2582                             nrDigit += (parse[j] >= '0' && parse[j] <= '9');
2583                             nrAlph  += (parse[j] >= 'a' && parse[j] <= 'z');
2584                             nrAlph  += (parse[j] >= 'A' && parse[j] <= 'Z');
2585                         }
2586                         if(nrAlph < 9*nrDigit) { // if more than 10% digit we assume search info
2587                             int depth=0; float score;
2588                             if(sscanf(parse, "!!! %f/%d", &score, &depth) == 2 && depth>0) {
2589                                 // [HGM] kibitz: save kibitzed opponent info for PGN and eval graph
2590                                 pvInfoList[forwardMostMove-1].depth = depth;
2591                                 pvInfoList[forwardMostMove-1].score = 100*score;
2592                             }
2593                             OutputKibitz(suppressKibitz, parse);
2594                         } else {
2595                             char tmp[MSG_SIZ];
2596                             sprintf(tmp, _("your opponent kibitzes: %s"), parse);
2597                             SendToPlayer(tmp, strlen(tmp));
2598                         }
2599                         next_out = i+1; // [HGM] suppress printing in ICS window
2600                     }
2601                     started = STARTED_NONE;
2602                 } else {
2603                     /* Don't match patterns against characters in comment */
2604                     i++;
2605                     continue;
2606                 }
2607             }
2608             if (started == STARTED_CHATTER) {
2609                 if (buf[i] != '\n') {
2610                     /* Don't match patterns against characters in chatter */
2611                     i++;
2612                     continue;
2613                 }
2614                 started = STARTED_NONE;
2615                 if(suppressKibitz) next_out = i+1;
2616             }
2617
2618             /* Kludge to deal with rcmd protocol */
2619             if (firstTime && looking_at(buf, &i, "\001*")) {
2620                 DisplayFatalError(&buf[1], 0, 1);
2621                 continue;
2622             } else {
2623                 firstTime = FALSE;
2624             }
2625
2626             if (!loggedOn && looking_at(buf, &i, "chessclub.com")) {
2627                 ics_type = ICS_ICC;
2628                 ics_prefix = "/";
2629                 if (appData.debugMode)
2630                   fprintf(debugFP, "ics_type %d\n", ics_type);
2631                 continue;
2632             }
2633             if (!loggedOn && looking_at(buf, &i, "freechess.org")) {
2634                 ics_type = ICS_FICS;
2635                 ics_prefix = "$";
2636                 if (appData.debugMode)
2637                   fprintf(debugFP, "ics_type %d\n", ics_type);
2638                 continue;
2639             }
2640             if (!loggedOn && looking_at(buf, &i, "chess.net")) {
2641                 ics_type = ICS_CHESSNET;
2642                 ics_prefix = "/";
2643                 if (appData.debugMode)
2644                   fprintf(debugFP, "ics_type %d\n", ics_type);
2645                 continue;
2646             }
2647
2648             if (!loggedOn &&
2649                 (looking_at(buf, &i, "\"*\" is *a registered name") ||
2650                  looking_at(buf, &i, "Logging you in as \"*\"") ||
2651                  looking_at(buf, &i, "will be \"*\""))) {
2652               strcpy(ics_handle, star_match[0]);
2653               continue;
2654             }
2655
2656             if (loggedOn && !have_set_title && ics_handle[0] != NULLCHAR) {
2657               char buf[MSG_SIZ];
2658               snprintf(buf, sizeof(buf), "%s@%s", ics_handle, appData.icsHost);
2659               DisplayIcsInteractionTitle(buf);
2660               have_set_title = TRUE;
2661             }
2662
2663             /* skip finger notes */
2664             if (started == STARTED_NONE &&
2665                 ((buf[i] == ' ' && isdigit(buf[i+1])) ||
2666                  (buf[i] == '1' && buf[i+1] == '0')) &&
2667                 buf[i+2] == ':' && buf[i+3] == ' ') {
2668               started = STARTED_CHATTER;
2669               i += 3;
2670               continue;
2671             }
2672
2673             oldi = i;
2674             // [HGM] seekgraph: recognize sought lines and end-of-sought message
2675             if(appData.seekGraph) {
2676                 if(soughtPending && MatchSoughtLine(buf+i)) {
2677                     i = strstr(buf+i, "rated") - buf;
2678                     if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
2679                     next_out = leftover_start = i;
2680                     started = STARTED_CHATTER;
2681                     suppressKibitz = TRUE;
2682                     continue;
2683                 }
2684                 if((gameMode == IcsIdle || gameMode == BeginningOfGame)
2685                         && looking_at(buf, &i, "* ads displayed")) {
2686                     soughtPending = FALSE;
2687                     seekGraphUp = TRUE;
2688                     DrawSeekGraph();
2689                     continue;
2690                 }
2691                 if(appData.autoRefresh) {
2692                     if(looking_at(buf, &i, "* (*) seeking * * * * *\"play *\" to respond)\n")) {
2693                         int s = (ics_type == ICS_ICC); // ICC format differs
2694                         if(seekGraphUp)
2695                         AddAd(star_match[0], star_match[1], atoi(star_match[2+s]), atoi(star_match[3+s]), 
2696                               star_match[4+s][0], star_match[5-3*s], atoi(star_match[7]), TRUE);
2697                         looking_at(buf, &i, "*% "); // eat prompt
2698                         if(oldi > 0 && buf[oldi-1] == '\n') oldi--; // suppress preceding LF, if any
2699                         if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
2700                         next_out = i; // suppress
2701                         continue;
2702                     }
2703                     if(looking_at(buf, &i, "\nAds removed: *\n") || looking_at(buf, &i, "\031(51 * *\031)")) {
2704                         char *p = star_match[0];
2705                         while(*p) {
2706                             if(seekGraphUp) RemoveSeekAd(atoi(p));
2707                             while(*p && *p++ != ' '); // next
2708                         }
2709                         looking_at(buf, &i, "*% "); // eat prompt
2710                         if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
2711                         next_out = i;
2712                         continue;
2713                     }
2714                 }
2715             }
2716
2717             /* skip formula vars */
2718             if (started == STARTED_NONE &&
2719                 buf[i] == 'f' && isdigit(buf[i+1]) && buf[i+2] == ':') {
2720               started = STARTED_CHATTER;
2721               i += 3;
2722               continue;
2723             }
2724
2725             // [HGM] kibitz: try to recognize opponent engine-score kibitzes, to divert them to engine-output window
2726             if (appData.autoKibitz && started == STARTED_NONE && 
2727                 !appData.icsEngineAnalyze &&                     // [HGM] [DM] ICS analyze
2728                 (gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack || gameMode == IcsObserving)) {
2729                 if((looking_at(buf, &i, "* kibitzes: ") || looking_at(buf, &i, "* whispers: ")) &&
2730                    (StrStr(star_match[0], gameInfo.white) == star_match[0] || 
2731                     StrStr(star_match[0], gameInfo.black) == star_match[0]   )) { // kibitz of self or opponent
2732                         suppressKibitz = TRUE;
2733                         if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
2734                         next_out = i;
2735                         if((StrStr(star_match[0], gameInfo.white) == star_match[0]
2736                                 && (gameMode == IcsPlayingWhite)) ||
2737                            (StrStr(star_match[0], gameInfo.black) == star_match[0]
2738                                 && (gameMode == IcsPlayingBlack))   ) // opponent kibitz
2739                             started = STARTED_CHATTER; // own kibitz we simply discard
2740                         else {
2741                             started = STARTED_COMMENT; // make sure it will be collected in parse[]
2742                             parse_pos = 0; parse[0] = NULLCHAR;
2743                             savingComment = TRUE;
2744                             suppressKibitz = gameMode != IcsObserving ? 2 :
2745                                 (StrStr(star_match[0], gameInfo.white) == NULL) + 1;
2746                         } 
2747                         continue;
2748                 } else
2749                 if((looking_at(buf, &i, "\nkibitzed to *\n") || looking_at(buf, &i, "kibitzed to *\n") ||
2750                     looking_at(buf, &i, "\n(kibitzed to *\n") || looking_at(buf, &i, "(kibitzed to *\n"))
2751                          && atoi(star_match[0])) {
2752                     // suppress the acknowledgements of our own autoKibitz
2753                     char *p;
2754                     if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
2755                     if(p = strchr(star_match[0], ' ')) p[1] = NULLCHAR; // clip off "players)" on FICS
2756                     SendToPlayer(star_match[0], strlen(star_match[0]));
2757                     if(looking_at(buf, &i, "*% ")) // eat prompt
2758                         suppressKibitz = FALSE;
2759                     next_out = i;
2760                     continue;
2761                 }
2762             } // [HGM] kibitz: end of patch
2763
2764             // [HGM] chat: intercept tells by users for which we have an open chat window
2765             channel = -1;
2766             if(started == STARTED_NONE && (looking_at(buf, &i, "* tells you:") || looking_at(buf, &i, "* says:") || 
2767                                            looking_at(buf, &i, "* whispers:") ||
2768                                            looking_at(buf, &i, "* kibitzes:") ||
2769                                            looking_at(buf, &i, "* shouts:") ||
2770                                            looking_at(buf, &i, "* c-shouts:") ||
2771                                            looking_at(buf, &i, "--> * ") ||
2772                                            looking_at(buf, &i, "*(*):") && (sscanf(star_match[1], "%d", &channel),1) ||
2773                                            looking_at(buf, &i, "*(*)(*):") && (sscanf(star_match[2], "%d", &channel),1) ||
2774                                            looking_at(buf, &i, "*(*)(*)(*):") && (sscanf(star_match[3], "%d", &channel),1) ||
2775                                            looking_at(buf, &i, "*(*)(*)(*)(*):") && sscanf(star_match[4], "%d", &channel) == 1 )) {
2776                 int p;
2777                 sscanf(star_match[0], "%[^(]", talker+1); // strip (C) or (U) off ICS handle
2778                 chattingPartner = -1;
2779
2780                 if(channel >= 0) // channel broadcast; look if there is a chatbox for this channel
2781                 for(p=0; p<MAX_CHAT; p++) {
2782                     if(channel == atoi(chatPartner[p])) {
2783                     talker[0] = '['; strcat(talker, "] ");
2784                     Colorize(channel == 1 ? ColorChannel1 : ColorChannel, FALSE);
2785                     chattingPartner = p; break;
2786                     }
2787                 } else
2788                 if(buf[i-3] == 'e') // kibitz; look if there is a KIBITZ chatbox
2789                 for(p=0; p<MAX_CHAT; p++) {
2790                     if(!strcmp("kibitzes", chatPartner[p])) {
2791                         talker[0] = '['; strcat(talker, "] ");
2792                         chattingPartner = p; break;
2793                     }
2794                 } else
2795                 if(buf[i-3] == 'r') // whisper; look if there is a WHISPER chatbox
2796                 for(p=0; p<MAX_CHAT; p++) {
2797                     if(!strcmp("whispers", chatPartner[p])) {
2798                         talker[0] = '['; strcat(talker, "] ");
2799                         chattingPartner = p; break;
2800                     }
2801                 } else
2802                 if(buf[i-3] == 't' || buf[oldi+2] == '>') {// shout, c-shout or it; look if there is a 'shouts' chatbox
2803                   if(buf[i-8] == '-' && buf[i-3] == 't')
2804                   for(p=0; p<MAX_CHAT; p++) { // c-shout; check if dedicatesd c-shout box exists
2805                     if(!strcmp("c-shouts", chatPartner[p])) {
2806                         talker[0] = '('; strcat(talker, ") "); Colorize(ColorSShout, FALSE);
2807                         chattingPartner = p; break;
2808                     }
2809                   }
2810                   if(chattingPartner < 0)
2811                   for(p=0; p<MAX_CHAT; p++) {
2812                     if(!strcmp("shouts", chatPartner[p])) {
2813                         if(buf[oldi+2] == '>') { talker[0] = '<'; strcat(talker, "> "); Colorize(ColorShout, FALSE); }
2814                         else if(buf[i-8] == '-') { talker[0] = '('; strcat(talker, ") "); Colorize(ColorSShout, FALSE); }
2815                         else { talker[0] = '['; strcat(talker, "] "); Colorize(ColorShout, FALSE); }
2816                         chattingPartner = p; break;
2817                     }
2818                   }
2819                 }
2820                 if(chattingPartner<0) // if not, look if there is a chatbox for this indivdual
2821                 for(p=0; p<MAX_CHAT; p++) if(!StrCaseCmp(talker+1, chatPartner[p])) {
2822                     talker[0] = 0; Colorize(ColorTell, FALSE);
2823                     chattingPartner = p; break;
2824                 }
2825                 if(chattingPartner<0) i = oldi; else {
2826                     Colorize(curColor, TRUE); // undo the bogus colorations we just made to trigger the souds
2827                     if(oldi > 0 && buf[oldi-1] == '\n') oldi--;
2828                     if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
2829                     started = STARTED_COMMENT;
2830                     parse_pos = 0; parse[0] = NULLCHAR;
2831                     savingComment = 3 + chattingPartner; // counts as TRUE
2832                     suppressKibitz = TRUE;
2833                     continue;
2834                 }
2835             } // [HGM] chat: end of patch
2836
2837             if (appData.zippyTalk || appData.zippyPlay) {
2838                 /* [DM] Backup address for color zippy lines */
2839                 backup = i;
2840 #if ZIPPY
2841                if (loggedOn == TRUE)
2842                        if (ZippyControl(buf, &backup) || ZippyConverse(buf, &backup) ||
2843                           (appData.zippyPlay && ZippyMatch(buf, &backup)));
2844 #endif
2845             } // [DM] 'else { ' deleted
2846                 if (
2847                     /* Regular tells and says */
2848                     (tkind = 1, looking_at(buf, &i, "* tells you: ")) ||
2849                     looking_at(buf, &i, "* (your partner) tells you: ") ||
2850                     looking_at(buf, &i, "* says: ") ||
2851                     /* Don't color "message" or "messages" output */
2852                     (tkind = 5, looking_at(buf, &i, "*. * (*:*): ")) ||
2853                     looking_at(buf, &i, "*. * at *:*: ") ||
2854                     looking_at(buf, &i, "--* (*:*): ") ||
2855                     /* Message notifications (same color as tells) */
2856                     looking_at(buf, &i, "* has left a message ") ||
2857                     looking_at(buf, &i, "* just sent you a message:\n") ||
2858                     /* Whispers and kibitzes */
2859                     (tkind = 2, looking_at(buf, &i, "* whispers: ")) ||
2860                     looking_at(buf, &i, "* kibitzes: ") ||
2861                     /* Channel tells */
2862                     (tkind = 3, looking_at(buf, &i, "*(*: "))) {
2863
2864                   if (tkind == 1 && strchr(star_match[0], ':')) {
2865                       /* Avoid "tells you:" spoofs in channels */
2866                      tkind = 3;
2867                   }
2868                   if (star_match[0][0] == NULLCHAR ||
2869                       strchr(star_match[0], ' ') ||
2870                       (tkind == 3 && strchr(star_match[1], ' '))) {
2871                     /* Reject bogus matches */
2872                     i = oldi;
2873                   } else {
2874                     if (appData.colorize) {
2875                       if (oldi > next_out) {
2876                         SendToPlayer(&buf[next_out], oldi - next_out);
2877                         next_out = oldi;
2878                       }
2879                       switch (tkind) {
2880                       case 1:
2881                         Colorize(ColorTell, FALSE);
2882                         curColor = ColorTell;
2883                         break;
2884                       case 2:
2885                         Colorize(ColorKibitz, FALSE);
2886                         curColor = ColorKibitz;
2887                         break;
2888                       case 3:
2889                         p = strrchr(star_match[1], '(');
2890                         if (p == NULL) {
2891                           p = star_match[1];
2892                         } else {
2893                           p++;
2894                         }
2895                         if (atoi(p) == 1) {
2896                           Colorize(ColorChannel1, FALSE);
2897                           curColor = ColorChannel1;
2898                         } else {
2899                           Colorize(ColorChannel, FALSE);
2900                           curColor = ColorChannel;
2901                         }
2902                         break;
2903                       case 5:
2904                         curColor = ColorNormal;
2905                         break;
2906                       }
2907                     }
2908                     if (started == STARTED_NONE && appData.autoComment &&
2909                         (gameMode == IcsObserving ||
2910                          gameMode == IcsPlayingWhite ||
2911                          gameMode == IcsPlayingBlack)) {
2912                       parse_pos = i - oldi;
2913                       memcpy(parse, &buf[oldi], parse_pos);
2914                       parse[parse_pos] = NULLCHAR;
2915                       started = STARTED_COMMENT;
2916                       savingComment = TRUE;
2917                     } else {
2918                       started = STARTED_CHATTER;
2919                       savingComment = FALSE;
2920                     }
2921                     loggedOn = TRUE;
2922                     continue;
2923                   }
2924                 }
2925
2926                 if (looking_at(buf, &i, "* s-shouts: ") ||
2927                     looking_at(buf, &i, "* c-shouts: ")) {
2928                     if (appData.colorize) {
2929                         if (oldi > next_out) {
2930                             SendToPlayer(&buf[next_out], oldi - next_out);
2931                             next_out = oldi;
2932                         }
2933                         Colorize(ColorSShout, FALSE);
2934                         curColor = ColorSShout;
2935                     }
2936                     loggedOn = TRUE;
2937                     started = STARTED_CHATTER;
2938                     continue;
2939                 }
2940
2941                 if (looking_at(buf, &i, "--->")) {
2942                     loggedOn = TRUE;
2943                     continue;
2944                 }
2945
2946                 if (looking_at(buf, &i, "* shouts: ") ||
2947                     looking_at(buf, &i, "--> ")) {
2948                     if (appData.colorize) {
2949                         if (oldi > next_out) {
2950                             SendToPlayer(&buf[next_out], oldi - next_out);
2951                             next_out = oldi;
2952                         }
2953                         Colorize(ColorShout, FALSE);
2954                         curColor = ColorShout;
2955                     }
2956                     loggedOn = TRUE;
2957                     started = STARTED_CHATTER;
2958                     continue;
2959                 }
2960
2961                 if (looking_at( buf, &i, "Challenge:")) {
2962                     if (appData.colorize) {
2963                         if (oldi > next_out) {
2964                             SendToPlayer(&buf[next_out], oldi - next_out);
2965                             next_out = oldi;
2966                         }
2967                         Colorize(ColorChallenge, FALSE);
2968                         curColor = ColorChallenge;
2969                     }
2970                     loggedOn = TRUE;
2971                     continue;
2972                 }
2973
2974                 if (looking_at(buf, &i, "* offers you") ||
2975                     looking_at(buf, &i, "* offers to be") ||
2976                     looking_at(buf, &i, "* would like to") ||
2977                     looking_at(buf, &i, "* requests to") ||
2978                     looking_at(buf, &i, "Your opponent offers") ||
2979                     looking_at(buf, &i, "Your opponent requests")) {
2980
2981                     if (appData.colorize) {
2982                         if (oldi > next_out) {
2983                             SendToPlayer(&buf[next_out], oldi - next_out);
2984                             next_out = oldi;
2985                         }
2986                         Colorize(ColorRequest, FALSE);
2987                         curColor = ColorRequest;
2988                     }
2989                     continue;
2990                 }
2991
2992                 if (looking_at(buf, &i, "* (*) seeking")) {
2993                     if (appData.colorize) {
2994                         if (oldi > next_out) {
2995                             SendToPlayer(&buf[next_out], oldi - next_out);
2996                             next_out = oldi;
2997                         }
2998                         Colorize(ColorSeek, FALSE);
2999                         curColor = ColorSeek;
3000                     }
3001                     continue;
3002             }
3003
3004             if (looking_at(buf, &i, "\\   ")) {
3005                 if (prevColor != ColorNormal) {
3006                     if (oldi > next_out) {
3007                         SendToPlayer(&buf[next_out], oldi - next_out);
3008                         next_out = oldi;
3009                     }
3010                     Colorize(prevColor, TRUE);
3011                     curColor = prevColor;
3012                 }
3013                 if (savingComment) {
3014                     parse_pos = i - oldi;
3015                     memcpy(parse, &buf[oldi], parse_pos);
3016                     parse[parse_pos] = NULLCHAR;
3017                     started = STARTED_COMMENT;
3018                     if(savingComment >= 3) // [HGM] chat: continuation of line for chat box
3019                         chattingPartner = savingComment - 3; // kludge to remember the box
3020                 } else {
3021                     started = STARTED_CHATTER;
3022                 }
3023                 continue;
3024             }
3025
3026             if (looking_at(buf, &i, "Black Strength :") ||
3027                 looking_at(buf, &i, "<<< style 10 board >>>") ||
3028                 looking_at(buf, &i, "<10>") ||
3029                 looking_at(buf, &i, "#@#")) {
3030                 /* Wrong board style */
3031                 loggedOn = TRUE;
3032                 SendToICS(ics_prefix);
3033                 SendToICS("set style 12\n");
3034                 SendToICS(ics_prefix);
3035                 SendToICS("refresh\n");
3036                 continue;
3037             }
3038             
3039             if (!have_sent_ICS_logon && looking_at(buf, &i, "login:")) {
3040                 ICSInitScript();
3041                 have_sent_ICS_logon = 1;
3042                 continue;
3043             }
3044               
3045             if (ics_getting_history != H_GETTING_MOVES /*smpos kludge*/ && 
3046                 (looking_at(buf, &i, "\n<12> ") ||
3047                  looking_at(buf, &i, "<12> "))) {
3048                 loggedOn = TRUE;
3049                 if (oldi > next_out) {
3050                     SendToPlayer(&buf[next_out], oldi - next_out);
3051                 }
3052                 next_out = i;
3053                 started = STARTED_BOARD;
3054                 parse_pos = 0;
3055                 continue;
3056             }
3057
3058             if ((started == STARTED_NONE && looking_at(buf, &i, "\n<b1> ")) ||
3059                 looking_at(buf, &i, "<b1> ")) {
3060                 if (oldi > next_out) {
3061                     SendToPlayer(&buf[next_out], oldi - next_out);
3062                 }
3063                 next_out = i;
3064                 started = STARTED_HOLDINGS;
3065                 parse_pos = 0;
3066                 continue;
3067             }
3068
3069             if (looking_at(buf, &i, "* *vs. * *--- *")) {
3070                 loggedOn = TRUE;
3071                 /* Header for a move list -- first line */
3072
3073                 switch (ics_getting_history) {
3074                   case H_FALSE:
3075                     switch (gameMode) {
3076                       case IcsIdle:
3077                       case BeginningOfGame:
3078                         /* User typed "moves" or "oldmoves" while we
3079                            were idle.  Pretend we asked for these
3080                            moves and soak them up so user can step
3081                            through them and/or save them.
3082                            */
3083                         Reset(FALSE, TRUE);
3084                         gameMode = IcsObserving;
3085                         ModeHighlight();
3086                         ics_gamenum = -1;
3087                         ics_getting_history = H_GOT_UNREQ_HEADER;
3088                         break;
3089                       case EditGame: /*?*/
3090                       case EditPosition: /*?*/
3091                         /* Should above feature work in these modes too? */
3092                         /* For now it doesn't */
3093                         ics_getting_history = H_GOT_UNWANTED_HEADER;
3094                         break;
3095                       default:
3096                         ics_getting_history = H_GOT_UNWANTED_HEADER;
3097                         break;
3098                     }
3099                     break;
3100                   case H_REQUESTED:
3101                     /* Is this the right one? */
3102                     if (gameInfo.white && gameInfo.black &&
3103                         strcmp(gameInfo.white, star_match[0]) == 0 &&
3104                         strcmp(gameInfo.black, star_match[2]) == 0) {
3105                         /* All is well */
3106                         ics_getting_history = H_GOT_REQ_HEADER;
3107                     }
3108                     break;
3109                   case H_GOT_REQ_HEADER:
3110                   case H_GOT_UNREQ_HEADER:
3111                   case H_GOT_UNWANTED_HEADER:
3112                   case H_GETTING_MOVES:
3113                     /* Should not happen */
3114                     DisplayError(_("Error gathering move list: two headers"), 0);
3115                     ics_getting_history = H_FALSE;
3116                     break;
3117                 }
3118
3119                 /* Save player ratings into gameInfo if needed */
3120                 if ((ics_getting_history == H_GOT_REQ_HEADER ||
3121                      ics_getting_history == H_GOT_UNREQ_HEADER) &&
3122                     (gameInfo.whiteRating == -1 ||
3123                      gameInfo.blackRating == -1)) {
3124
3125                     gameInfo.whiteRating = string_to_rating(star_match[1]);
3126                     gameInfo.blackRating = string_to_rating(star_match[3]);
3127                     if (appData.debugMode)
3128                       fprintf(debugFP, _("Ratings from header: W %d, B %d\n"), 
3129                               gameInfo.whiteRating, gameInfo.blackRating);
3130                 }
3131                 continue;
3132             }
3133
3134             if (looking_at(buf, &i,
3135               "* * match, initial time: * minute*, increment: * second")) {
3136                 /* Header for a move list -- second line */
3137                 /* Initial board will follow if this is a wild game */
3138                 if (gameInfo.event != NULL) free(gameInfo.event);
3139                 sprintf(str, "ICS %s %s match", star_match[0], star_match[1]);
3140                 gameInfo.event = StrSave(str);
3141                 /* [HGM] we switched variant. Translate boards if needed. */
3142                 VariantSwitch(boards[currentMove], StringToVariant(gameInfo.event));
3143                 continue;
3144             }
3145
3146             if (looking_at(buf, &i, "Move  ")) {
3147                 /* Beginning of a move list */
3148                 switch (ics_getting_history) {
3149                   case H_FALSE:
3150                     /* Normally should not happen */
3151                     /* Maybe user hit reset while we were parsing */
3152                     break;
3153                   case H_REQUESTED:
3154                     /* Happens if we are ignoring a move list that is not
3155                      * the one we just requested.  Common if the user
3156                      * tries to observe two games without turning off
3157                      * getMoveList */
3158                     break;
3159                   case H_GETTING_MOVES:
3160                     /* Should not happen */
3161                     DisplayError(_("Error gathering move list: nested"), 0);
3162                     ics_getting_history = H_FALSE;
3163                     break;
3164                   case H_GOT_REQ_HEADER:
3165                     ics_getting_history = H_GETTING_MOVES;
3166                     started = STARTED_MOVES;
3167                     parse_pos = 0;
3168                     if (oldi > next_out) {
3169                         SendToPlayer(&buf[next_out], oldi - next_out);
3170                     }
3171                     break;
3172                   case H_GOT_UNREQ_HEADER:
3173                     ics_getting_history = H_GETTING_MOVES;
3174                     started = STARTED_MOVES_NOHIDE;
3175                     parse_pos = 0;
3176                     break;
3177                   case H_GOT_UNWANTED_HEADER:
3178                     ics_getting_history = H_FALSE;
3179                     break;
3180                 }
3181                 continue;
3182             }                           
3183             
3184             if (looking_at(buf, &i, "% ") ||
3185                 ((started == STARTED_MOVES || started == STARTED_MOVES_NOHIDE)
3186                  && looking_at(buf, &i, "}*"))) { char *bookHit = NULL; // [HGM] book
3187                 if(ics_type == ICS_ICC && soughtPending) { // [HGM] seekgraph: on ICC sought-list has no termination line
3188                     soughtPending = FALSE;
3189                     seekGraphUp = TRUE;
3190                     DrawSeekGraph();
3191                 }
3192                 if(suppressKibitz) next_out = i;
3193                 savingComment = FALSE;
3194                 suppressKibitz = 0;
3195                 switch (started) {
3196                   case STARTED_MOVES:
3197                   case STARTED_MOVES_NOHIDE:
3198                     memcpy(&parse[parse_pos], &buf[oldi], i - oldi);
3199                     parse[parse_pos + i - oldi] = NULLCHAR;
3200                     ParseGameHistory(parse);
3201 #if ZIPPY
3202                     if (appData.zippyPlay && first.initDone) {
3203                         FeedMovesToProgram(&first, forwardMostMove);
3204                         if (gameMode == IcsPlayingWhite) {
3205                             if (WhiteOnMove(forwardMostMove)) {
3206                                 if (first.sendTime) {
3207                                   if (first.useColors) {
3208                                     SendToProgram("black\n", &first); 
3209                                   }
3210                                   SendTimeRemaining(&first, TRUE);
3211                                 }
3212                                 if (first.useColors) {
3213                                   SendToProgram("white\n", &first); // [HGM] book: made sending of "go\n" book dependent
3214                                 }
3215                                 bookHit = SendMoveToBookUser(forwardMostMove-1, &first, TRUE); // [HGM] book: probe book for initial pos
3216                                 first.maybeThinking = TRUE;
3217                             } else {
3218                                 if (first.usePlayother) {
3219                                   if (first.sendTime) {
3220                                     SendTimeRemaining(&first, TRUE);
3221                                   }
3222                                   SendToProgram("playother\n", &first);
3223                                   firstMove = FALSE;
3224                                 } else {
3225                                   firstMove = TRUE;
3226                                 }
3227                             }
3228                         } else if (gameMode == IcsPlayingBlack) {
3229                             if (!WhiteOnMove(forwardMostMove)) {
3230                                 if (first.sendTime) {
3231                                   if (first.useColors) {
3232                                     SendToProgram("white\n", &first);
3233                                   }
3234                                   SendTimeRemaining(&first, FALSE);
3235                                 }
3236                                 if (first.useColors) {
3237                                   SendToProgram("black\n", &first);
3238                                 }
3239                                 bookHit = SendMoveToBookUser(forwardMostMove-1, &first, TRUE);
3240                                 first.maybeThinking = TRUE;
3241                             } else {
3242                                 if (first.usePlayother) {
3243                                   if (first.sendTime) {
3244                                     SendTimeRemaining(&first, FALSE);
3245                                   }
3246                                   SendToProgram("playother\n", &first);
3247                                   firstMove = FALSE;
3248                                 } else {
3249                                   firstMove = TRUE;
3250                                 }
3251                             }
3252                         }                       
3253                     }
3254 #endif
3255                     if (gameMode == IcsObserving && ics_gamenum == -1) {
3256                         /* Moves came from oldmoves or moves command
3257                            while we weren't doing anything else.
3258                            */
3259                         currentMove = forwardMostMove;
3260                         ClearHighlights();/*!!could figure this out*/
3261                         flipView = appData.flipView;
3262                         DrawPosition(TRUE, boards[currentMove]);
3263                         DisplayBothClocks();
3264                         sprintf(str, "%s vs. %s",
3265                                 gameInfo.white, gameInfo.black);
3266                         DisplayTitle(str);
3267                         gameMode = IcsIdle;
3268                     } else {
3269                         /* Moves were history of an active game */
3270                         if (gameInfo.resultDetails != NULL) {
3271                             free(gameInfo.resultDetails);
3272                             gameInfo.resultDetails = NULL;
3273                         }
3274                     }
3275                     HistorySet(parseList, backwardMostMove,
3276                                forwardMostMove, currentMove-1);
3277                     DisplayMove(currentMove - 1);
3278                     if (started == STARTED_MOVES) next_out = i;
3279                     started = STARTED_NONE;
3280                     ics_getting_history = H_FALSE;
3281                     break;
3282
3283                   case STARTED_OBSERVE:
3284                     started = STARTED_NONE;
3285                     SendToICS(ics_prefix);
3286                     SendToICS("refresh\n");
3287                     break;
3288
3289                   default:
3290                     break;
3291                 }
3292                 if(bookHit) { // [HGM] book: simulate book reply
3293                     static char bookMove[MSG_SIZ]; // a bit generous?
3294
3295                     programStats.nodes = programStats.depth = programStats.time = 
3296                     programStats.score = programStats.got_only_move = 0;
3297                     sprintf(programStats.movelist, "%s (xbook)", bookHit);
3298
3299                     strcpy(bookMove, "move ");
3300                     strcat(bookMove, bookHit);
3301                     HandleMachineMove(bookMove, &first);
3302                 }
3303                 continue;
3304             }
3305             
3306             if ((started == STARTED_MOVES || started == STARTED_BOARD ||
3307                  started == STARTED_HOLDINGS ||
3308                  started == STARTED_MOVES_NOHIDE) && i >= leftover_len) {
3309                 /* Accumulate characters in move list or board */
3310                 parse[parse_pos++] = buf[i];
3311             }
3312             
3313             /* Start of game messages.  Mostly we detect start of game
3314                when the first board image arrives.  On some versions
3315                of the ICS, though, we need to do a "refresh" after starting
3316                to observe in order to get the current board right away. */
3317             if (looking_at(buf, &i, "Adding game * to observation list")) {
3318                 started = STARTED_OBSERVE;
3319                 continue;
3320             }
3321
3322             /* Handle auto-observe */
3323             if (appData.autoObserve &&
3324                 (gameMode == IcsIdle || gameMode == BeginningOfGame) &&
3325                 looking_at(buf, &i, "Game notification: * (*) vs. * (*)")) {
3326                 char *player;
3327                 /* Choose the player that was highlighted, if any. */
3328                 if (star_match[0][0] == '\033' ||
3329                     star_match[1][0] != '\033') {
3330                     player = star_match[0];
3331                 } else {
3332                     player = star_match[2];
3333                 }
3334                 sprintf(str, "%sobserve %s\n",
3335                         ics_prefix, StripHighlightAndTitle(player));
3336                 SendToICS(str);
3337
3338                 /* Save ratings from notify string */
3339                 strcpy(player1Name, star_match[0]);
3340                 player1Rating = string_to_rating(star_match[1]);
3341                 strcpy(player2Name, star_match[2]);
3342                 player2Rating = string_to_rating(star_match[3]);
3343
3344                 if (appData.debugMode)
3345                   fprintf(debugFP, 
3346                           "Ratings from 'Game notification:' %s %d, %s %d\n",
3347                           player1Name, player1Rating,
3348                           player2Name, player2Rating);
3349
3350                 continue;
3351             }
3352
3353             /* Deal with automatic examine mode after a game,
3354                and with IcsObserving -> IcsExamining transition */
3355             if (looking_at(buf, &i, "Entering examine mode for game *") ||
3356                 looking_at(buf, &i, "has made you an examiner of game *")) {
3357
3358                 int gamenum = atoi(star_match[0]);
3359                 if ((gameMode == IcsIdle || gameMode == IcsObserving) &&
3360                     gamenum == ics_gamenum) {
3361                     /* We were already playing or observing this game;
3362                        no need to refetch history */
3363                     gameMode = IcsExamining;
3364                     if (pausing) {
3365                         pauseExamForwardMostMove = forwardMostMove;
3366                     } else if (currentMove < forwardMostMove) {
3367                         ForwardInner(forwardMostMove);
3368                     }
3369                 } else {
3370                     /* I don't think this case really can happen */
3371                     SendToICS(ics_prefix);
3372                     SendToICS("refresh\n");
3373                 }
3374                 continue;
3375             }    
3376             
3377             /* Error messages */
3378 //          if (ics_user_moved) {
3379             if (1) { // [HGM] old way ignored error after move type in; ics_user_moved is not set then!
3380                 if (looking_at(buf, &i, "Illegal move") ||
3381                     looking_at(buf, &i, "Not a legal move") ||
3382                     looking_at(buf, &i, "Your king is in check") ||
3383                     looking_at(buf, &i, "It isn't your turn") ||
3384                     looking_at(buf, &i, "It is not your move")) {
3385                     /* Illegal move */
3386                     if (ics_user_moved && forwardMostMove > backwardMostMove) { // only backup if we already moved
3387                         currentMove = forwardMostMove-1;
3388                         DisplayMove(currentMove - 1); /* before DMError */
3389                         DrawPosition(FALSE, boards[currentMove]);
3390                         SwitchClocks(forwardMostMove-1); // [HGM] race
3391                         DisplayBothClocks();
3392                     }
3393                     DisplayMoveError(_("Illegal move (rejected by ICS)")); // [HGM] but always relay error msg
3394                     ics_user_moved = 0;
3395                     continue;
3396                 }
3397             }
3398
3399             if (looking_at(buf, &i, "still have time") ||
3400                 looking_at(buf, &i, "not out of time") ||
3401                 looking_at(buf, &i, "either player is out of time") ||
3402                 looking_at(buf, &i, "has timeseal; checking")) {
3403                 /* We must have called his flag a little too soon */
3404                 whiteFlag = blackFlag = FALSE;
3405                 continue;
3406             }
3407
3408             if (looking_at(buf, &i, "added * seconds to") ||
3409                 looking_at(buf, &i, "seconds were added to")) {
3410                 /* Update the clocks */
3411                 SendToICS(ics_prefix);
3412                 SendToICS("refresh\n");
3413                 continue;
3414             }
3415
3416             if (!ics_clock_paused && looking_at(buf, &i, "clock paused")) {
3417                 ics_clock_paused = TRUE;
3418                 StopClocks();
3419                 continue;
3420             }
3421
3422             if (ics_clock_paused && looking_at(buf, &i, "clock resumed")) {
3423                 ics_clock_paused = FALSE;
3424                 StartClocks();
3425                 continue;
3426             }
3427
3428             /* Grab player ratings from the Creating: message.
3429                Note we have to check for the special case when
3430                the ICS inserts things like [white] or [black]. */
3431             if (looking_at(buf, &i, "Creating: * (*)* * (*)") ||
3432                 looking_at(buf, &i, "Creating: * (*) [*] * (*)")) {
3433                 /* star_matches:
3434                    0    player 1 name (not necessarily white)
3435                    1    player 1 rating
3436                    2    empty, white, or black (IGNORED)
3437                    3    player 2 name (not necessarily black)
3438                    4    player 2 rating
3439                    
3440                    The names/ratings are sorted out when the game
3441                    actually starts (below).
3442                 */
3443                 strcpy(player1Name, StripHighlightAndTitle(star_match[0]));
3444                 player1Rating = string_to_rating(star_match[1]);
3445                 strcpy(player2Name, StripHighlightAndTitle(star_match[3]));
3446                 player2Rating = string_to_rating(star_match[4]);
3447
3448                 if (appData.debugMode)
3449                   fprintf(debugFP, 
3450                           "Ratings from 'Creating:' %s %d, %s %d\n",
3451                           player1Name, player1Rating,
3452                           player2Name, player2Rating);
3453
3454                 continue;
3455             }
3456             
3457             /* Improved generic start/end-of-game messages */
3458             if ((tkind=0, looking_at(buf, &i, "{Game * (* vs. *) *}*")) ||
3459                 (tkind=1, looking_at(buf, &i, "{Game * (*(*) vs. *(*)) *}*"))){
3460                 /* If tkind == 0: */
3461                 /* star_match[0] is the game number */
3462                 /*           [1] is the white player's name */
3463                 /*           [2] is the black player's name */
3464                 /* For end-of-game: */
3465                 /*           [3] is the reason for the game end */
3466                 /*           [4] is a PGN end game-token, preceded by " " */
3467                 /* For start-of-game: */
3468                 /*           [3] begins with "Creating" or "Continuing" */
3469                 /*           [4] is " *" or empty (don't care). */
3470                 int gamenum = atoi(star_match[0]);
3471                 char *whitename, *blackname, *why, *endtoken;
3472                 ChessMove endtype = (ChessMove) 0;
3473
3474                 if (tkind == 0) {
3475                   whitename = star_match[1];
3476                   blackname = star_match[2];
3477                   why = star_match[3];
3478                   endtoken = star_match[4];
3479                 } else {
3480                   whitename = star_match[1];
3481                   blackname = star_match[3];
3482                   why = star_match[5];
3483                   endtoken = star_match[6];
3484                 }
3485
3486                 /* Game start messages */
3487                 if (strncmp(why, "Creating ", 9) == 0 ||
3488                     strncmp(why, "Continuing ", 11) == 0) {
3489                     gs_gamenum = gamenum;
3490                     strcpy(gs_kind, strchr(why, ' ') + 1);
3491                     VariantSwitch(boards[currentMove], StringToVariant(gs_kind)); // [HGM] variantswitch: even before we get first board
3492 #if ZIPPY
3493                     if (appData.zippyPlay) {
3494                         ZippyGameStart(whitename, blackname);
3495                     }
3496 #endif /*ZIPPY*/
3497                     partnerBoardValid = FALSE; // [HGM] bughouse
3498                     continue;
3499                 }
3500
3501                 /* Game end messages */
3502                 if (gameMode == IcsIdle || gameMode == BeginningOfGame ||
3503                     ics_gamenum != gamenum) {
3504                     continue;
3505                 }
3506                 while (endtoken[0] == ' ') endtoken++;
3507                 switch (endtoken[0]) {
3508                   case '*':
3509                   default:
3510                     endtype = GameUnfinished;
3511                     break;
3512                   case '0':
3513                     endtype = BlackWins;
3514                     break;
3515                   case '1':
3516                     if (endtoken[1] == '/')
3517                       endtype = GameIsDrawn;
3518                     else
3519                       endtype = WhiteWins;
3520                     break;
3521                 }
3522                 GameEnds(endtype, why, GE_ICS);
3523 #if ZIPPY
3524                 if (appData.zippyPlay && first.initDone) {
3525                     ZippyGameEnd(endtype, why);
3526                     if (first.pr == NULL) {
3527                       /* Start the next process early so that we'll
3528                          be ready for the next challenge */
3529                       StartChessProgram(&first);
3530                     }
3531                     /* Send "new" early, in case this command takes
3532                        a long time to finish, so that we'll be ready
3533                        for the next challenge. */
3534                     gameInfo.variant = VariantNormal; // [HGM] variantswitch: suppress sending of 'variant'
3535                     Reset(TRUE, TRUE);
3536                 }
3537 #endif /*ZIPPY*/
3538                 if(appData.bgObserve && partnerBoardValid) DrawPosition(TRUE, partnerBoard);
3539                 continue;
3540             }
3541
3542             if (looking_at(buf, &i, "Removing game * from observation") ||
3543                 looking_at(buf, &i, "no longer observing game *") ||
3544                 looking_at(buf, &i, "Game * (*) has no examiners")) {
3545                 if (gameMode == IcsObserving &&
3546                     atoi(star_match[0]) == ics_gamenum)
3547                   {
3548                       /* icsEngineAnalyze */
3549                       if (appData.icsEngineAnalyze) {
3550                             ExitAnalyzeMode();
3551                             ModeHighlight();
3552                       }
3553                       StopClocks();
3554                       gameMode = IcsIdle;
3555                       ics_gamenum = -1;
3556                       ics_user_moved = FALSE;
3557                   }
3558                 continue;
3559             }
3560
3561             if (looking_at(buf, &i, "no longer examining game *")) {
3562                 if (gameMode == IcsExamining &&
3563                     atoi(star_match[0]) == ics_gamenum)
3564                   {
3565                       gameMode = IcsIdle;
3566                       ics_gamenum = -1;
3567                       ics_user_moved = FALSE;
3568                   }
3569                 continue;
3570             }
3571
3572             /* Advance leftover_start past any newlines we find,
3573                so only partial lines can get reparsed */
3574             if (looking_at(buf, &i, "\n")) {
3575                 prevColor = curColor;
3576                 if (curColor != ColorNormal) {
3577                     if (oldi > next_out) {
3578                         SendToPlayer(&buf[next_out], oldi - next_out);
3579                         next_out = oldi;
3580                     }
3581                     Colorize(ColorNormal, FALSE);
3582                     curColor = ColorNormal;
3583                 }
3584                 if (started == STARTED_BOARD) {
3585                     started = STARTED_NONE;
3586                     parse[parse_pos] = NULLCHAR;
3587                     ParseBoard12(parse);
3588                     ics_user_moved = 0;
3589
3590                     /* Send premove here */
3591                     if (appData.premove) {
3592                       char str[MSG_SIZ];
3593                       if (currentMove == 0 &&
3594                           gameMode == IcsPlayingWhite &&
3595                           appData.premoveWhite) {
3596                         sprintf(str, "%s\n", appData.premoveWhiteText);
3597                         if (appData.debugMode)
3598                           fprintf(debugFP, "Sending premove:\n");
3599                         SendToICS(str);
3600                       } else if (currentMove == 1 &&
3601                                  gameMode == IcsPlayingBlack &&
3602                                  appData.premoveBlack) {
3603                         sprintf(str, "%s\n", appData.premoveBlackText);
3604                         if (appData.debugMode)
3605                           fprintf(debugFP, "Sending premove:\n");
3606                         SendToICS(str);
3607                       } else if (gotPremove) {
3608                         gotPremove = 0;
3609                         ClearPremoveHighlights();
3610                         if (appData.debugMode)
3611                           fprintf(debugFP, "Sending premove:\n");
3612                           UserMoveEvent(premoveFromX, premoveFromY, 
3613                                         premoveToX, premoveToY, 
3614                                         premovePromoChar);
3615                       }
3616                     }
3617
3618                     /* Usually suppress following prompt */
3619                     if (!(forwardMostMove == 0 && gameMode == IcsExamining)) {
3620                         while(looking_at(buf, &i, "\n")); // [HGM] skip empty lines
3621                         if (looking_at(buf, &i, "*% ")) {
3622                             savingComment = FALSE;
3623                             suppressKibitz = 0;
3624                         }
3625                     }
3626                     next_out = i;
3627                 } else if (started == STARTED_HOLDINGS) {
3628                     int gamenum;
3629                     char new_piece[MSG_SIZ];
3630                     started = STARTED_NONE;
3631                     parse[parse_pos] = NULLCHAR;
3632                     if (appData.debugMode)
3633                       fprintf(debugFP, "Parsing holdings: %s, currentMove = %d\n",
3634                                                         parse, currentMove);
3635                     if (sscanf(parse, " game %d", &gamenum) == 1) {
3636                       if(gamenum == ics_gamenum) { // [HGM] bughouse: old code if part of foreground game
3637                         if (gameInfo.variant == VariantNormal) {
3638                           /* [HGM] We seem to switch variant during a game!
3639                            * Presumably no holdings were displayed, so we have
3640                            * to move the position two files to the right to
3641                            * create room for them!
3642                            */
3643                           VariantClass newVariant;
3644                           switch(gameInfo.boardWidth) { // base guess on board width
3645                                 case 9:  newVariant = VariantShogi; break;
3646                                 case 10: newVariant = VariantGreat; break;
3647                                 default: newVariant = VariantCrazyhouse; break;
3648                           }
3649                           VariantSwitch(boards[currentMove], newVariant); /* temp guess */
3650                           /* Get a move list just to see the header, which
3651                              will tell us whether this is really bug or zh */
3652                           if (ics_getting_history == H_FALSE) {
3653                             ics_getting_history = H_REQUESTED;
3654                             sprintf(str, "%smoves %d\n", ics_prefix, gamenum);
3655                             SendToICS(str);
3656                           }
3657                         }
3658                         new_piece[0] = NULLCHAR;
3659                         sscanf(parse, "game %d white [%s black [%s <- %s",
3660                                &gamenum, white_holding, black_holding,
3661                                new_piece);
3662                         white_holding[strlen(white_holding)-1] = NULLCHAR;
3663                         black_holding[strlen(black_holding)-1] = NULLCHAR;
3664                         /* [HGM] copy holdings to board holdings area */
3665                         CopyHoldings(boards[forwardMostMove], white_holding, WhitePawn);
3666                         CopyHoldings(boards[forwardMostMove], black_holding, BlackPawn);
3667                         boards[forwardMostMove][HOLDINGS_SET] = 1; // flag holdings as set
3668 #if ZIPPY
3669                         if (appData.zippyPlay && first.initDone) {
3670                             ZippyHoldings(white_holding, black_holding,
3671                                           new_piece);
3672                         }
3673 #endif /*ZIPPY*/
3674                         if (tinyLayout || smallLayout) {
3675                             char wh[16], bh[16];
3676                             PackHolding(wh, white_holding);
3677                             PackHolding(bh, black_holding);
3678                             sprintf(str, "[%s-%s] %s-%s", wh, bh,
3679                                     gameInfo.white, gameInfo.black);
3680                         } else {
3681                             sprintf(str, "%s [%s] vs. %s [%s]",
3682                                     gameInfo.white, white_holding,
3683                                     gameInfo.black, black_holding);
3684                         }
3685                         if(!partnerUp) // [HGM] bughouse: when peeking at partner game we already know what he captured...
3686                         DrawPosition(FALSE, boards[currentMove]);
3687                         DisplayTitle(str);
3688                       } else if(appData.bgObserve) { // [HGM] bughouse: holdings of other game => background
3689                         sscanf(parse, "game %d white [%s black [%s <- %s",
3690                                &gamenum, white_holding, black_holding,
3691                                new_piece);
3692                         white_holding[strlen(white_holding)-1] = NULLCHAR;
3693                         black_holding[strlen(black_holding)-1] = NULLCHAR;
3694                         /* [HGM] copy holdings to partner-board holdings area */
3695                         CopyHoldings(partnerBoard, white_holding, WhitePawn);
3696                         CopyHoldings(partnerBoard, black_holding, BlackPawn);
3697                         if(twoBoards) { partnerUp = 1; flipView = !flipView; } // [HGM] dual: always draw
3698                         if(partnerUp) DrawPosition(FALSE, partnerBoard);
3699                         if(twoBoards) { partnerUp = 0; flipView = !flipView; }
3700                       }
3701                     }
3702                     /* Suppress following prompt */
3703                     if (looking_at(buf, &i, "*% ")) {
3704                         if(strchr(star_match[0], 7)) SendToPlayer("\007", 1); // Bell(); // FICS fuses bell for next board with prompt in zh captures
3705                         savingComment = FALSE;
3706                         suppressKibitz = 0;
3707                     }
3708                     next_out = i;
3709                 }
3710                 continue;
3711             }
3712
3713             i++;                /* skip unparsed character and loop back */
3714         }
3715         
3716         if (started != STARTED_MOVES && started != STARTED_BOARD && !suppressKibitz && // [HGM] kibitz
3717 //          started != STARTED_HOLDINGS && i > next_out) { // [HGM] should we compare to leftover_start in stead of i?
3718 //          SendToPlayer(&buf[next_out], i - next_out);
3719             started != STARTED_HOLDINGS && leftover_start > next_out) {
3720             SendToPlayer(&buf[next_out], leftover_start - next_out);
3721             next_out = i;
3722         }
3723         
3724         leftover_len = buf_len - leftover_start;
3725         /* if buffer ends with something we couldn't parse,
3726            reparse it after appending the next read */
3727         
3728     } else if (count == 0) {
3729         RemoveInputSource(isr);
3730         DisplayFatalError(_("Connection closed by ICS"), 0, 0);
3731     } else {
3732         DisplayFatalError(_("Error reading from ICS"), error, 1);
3733     }
3734 }
3735
3736
3737 /* Board style 12 looks like this:
3738    
3739    <12> r-b---k- pp----pp ---bP--- ---p---- q------- ------P- P--Q--BP -----R-K W -1 0 0 0 0 0 0 paf MaxII 0 2 12 21 25 234 174 24 Q/d7-a4 (0:06) Qxa4 0 0
3740    
3741  * The "<12> " is stripped before it gets to this routine.  The two
3742  * trailing 0's (flip state and clock ticking) are later addition, and
3743  * some chess servers may not have them, or may have only the first.
3744  * Additional trailing fields may be added in the future.  
3745  */
3746
3747 #define PATTERN "%c%d%d%d%d%d%d%d%s%s%d%d%d%d%d%d%d%d%s%s%s%d%d"
3748
3749 #define RELATION_OBSERVING_PLAYED    0
3750 #define RELATION_OBSERVING_STATIC   -2   /* examined, oldmoves, or smoves */
3751 #define RELATION_PLAYING_MYMOVE      1
3752 #define RELATION_PLAYING_NOTMYMOVE  -1
3753 #define RELATION_EXAMINING           2
3754 #define RELATION_ISOLATED_BOARD     -3
3755 #define RELATION_STARTING_POSITION  -4   /* FICS only */
3756
3757 void
3758 ParseBoard12(string)
3759      char *string;
3760
3761     GameMode newGameMode;
3762     int gamenum, newGame, newMove, relation, basetime, increment, ics_flip = 0, i;
3763     int j, k, n, moveNum, white_stren, black_stren, white_time, black_time, takeback;
3764     int double_push, castle_ws, castle_wl, castle_bs, castle_bl, irrev_count;
3765     char to_play, board_chars[200];
3766     char move_str[500], str[500], elapsed_time[500];
3767     char black[32], white[32];
3768     Board board;
3769     int prevMove = currentMove;
3770     int ticking = 2;
3771     ChessMove moveType;
3772     int fromX, fromY, toX, toY;
3773     char promoChar;
3774     int ranks=1, files=0; /* [HGM] ICS80: allow variable board size */
3775     char *bookHit = NULL; // [HGM] book
3776     Boolean weird = FALSE, reqFlag = FALSE;
3777
3778     fromX = fromY = toX = toY = -1;
3779     
3780     newGame = FALSE;
3781
3782     if (appData.debugMode)
3783       fprintf(debugFP, _("Parsing board: %s\n"), string);
3784
3785     move_str[0] = NULLCHAR;
3786     elapsed_time[0] = NULLCHAR;
3787     {   /* [HGM] figure out how many ranks and files the board has, for ICS extension used by Capablanca server */
3788         int  i = 0, j;
3789         while(i < 199 && (string[i] != ' ' || string[i+2] != ' ')) {
3790             if(string[i] == ' ') { ranks++; files = 0; }
3791             else files++;
3792             if(!strchr(" -pnbrqkPNBRQK" , string[i])) weird = TRUE; // test for fairies
3793             i++;
3794         }
3795         for(j = 0; j <i; j++) board_chars[j] = string[j];
3796         board_chars[i] = '\0';
3797         string += i + 1;
3798     }
3799     n = sscanf(string, PATTERN, &to_play, &double_push,
3800                &castle_ws, &castle_wl, &castle_bs, &castle_bl, &irrev_count,
3801                &gamenum, white, black, &relation, &basetime, &increment,
3802                &white_stren, &black_stren, &white_time, &black_time,
3803                &moveNum, str, elapsed_time, move_str, &ics_flip,
3804                &ticking);
3805
3806     if (n < 21) {
3807         snprintf(str, sizeof(str), _("Failed to parse board string:\n\"%s\""), string);
3808         DisplayError(str, 0);
3809         return;
3810     }
3811
3812     /* Convert the move number to internal form */
3813     moveNum = (moveNum - 1) * 2;
3814     if (to_play == 'B') moveNum++;
3815     if (moveNum > framePtr) { // [HGM] vari: do not run into saved variations
3816       DisplayFatalError(_("Game too long; increase MAX_MOVES and recompile"),
3817                         0, 1);
3818       return;
3819     }
3820     
3821     switch (relation) {
3822       case RELATION_OBSERVING_PLAYED:
3823       case RELATION_OBSERVING_STATIC:
3824         if (gamenum == -1) {
3825             /* Old ICC buglet */
3826             relation = RELATION_OBSERVING_STATIC;
3827         }
3828         newGameMode = IcsObserving;
3829         break;
3830       case RELATION_PLAYING_MYMOVE:
3831       case RELATION_PLAYING_NOTMYMOVE:
3832         newGameMode =
3833           ((relation == RELATION_PLAYING_MYMOVE) == (to_play == 'W')) ?
3834             IcsPlayingWhite : IcsPlayingBlack;
3835         break;
3836       case RELATION_EXAMINING:
3837         newGameMode = IcsExamining;
3838         break;
3839       case RELATION_ISOLATED_BOARD:
3840       default:
3841         /* Just display this board.  If user was doing something else,
3842            we will forget about it until the next board comes. */ 
3843         newGameMode = IcsIdle;
3844         break;
3845       case RELATION_STARTING_POSITION:
3846         newGameMode = gameMode;
3847         break;
3848     }
3849     
3850     if((gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack)
3851          && newGameMode == IcsObserving && gamenum != ics_gamenum && appData.bgObserve) {
3852       // [HGM] bughouse: don't act on alien boards while we play. Just parse the board and save it */
3853       char *toSqr;
3854       for (k = 0; k < ranks; k++) {
3855         for (j = 0; j < files; j++)
3856           board[k][j+gameInfo.holdingsWidth] = CharToPiece(board_chars[(ranks-1-k)*(files+1) + j]);
3857         if(gameInfo.holdingsWidth > 1) {
3858              board[k][0] = board[k][BOARD_WIDTH-1] = EmptySquare;
3859              board[k][1] = board[k][BOARD_WIDTH-2] = (ChessSquare) 0;;
3860         }
3861       }
3862       CopyBoard(partnerBoard, board);
3863       if(toSqr = strchr(str, '/')) { // extract highlights from long move
3864         partnerBoard[EP_STATUS-3] = toSqr[1] - AAA; // kludge: hide highlighting info in board
3865         partnerBoard[EP_STATUS-4] = toSqr[2] - ONE;
3866       } else partnerBoard[EP_STATUS-4] = partnerBoard[EP_STATUS-3] = -1;
3867       if(toSqr = strchr(str, '-')) {
3868         partnerBoard[EP_STATUS-1] = toSqr[1] - AAA;
3869         partnerBoard[EP_STATUS-2] = toSqr[2] - ONE;
3870       } else partnerBoard[EP_STATUS-1] = partnerBoard[EP_STATUS-2] = -1;
3871       if(appData.dualBoard && !twoBoards) { twoBoards = 1; InitDrawingSizes(-2,0); }
3872       if(twoBoards) { partnerUp = 1; flipView = !flipView; } // [HGM] dual
3873       if(partnerUp) DrawPosition(FALSE, partnerBoard);
3874       if(twoBoards) { partnerUp = 0; flipView = !flipView; } // [HGM] dual
3875       sprintf(partnerStatus, "W: %d:%02d B: %d:%02d (%d-%d) %c", white_time/60000, (white_time%60000)/1000,
3876                  (black_time/60000), (black_time%60000)/1000, white_stren, black_stren, to_play);
3877       DisplayMessage(partnerStatus, "");
3878         partnerBoardValid = TRUE;
3879       return;
3880     }
3881
3882     /* Modify behavior for initial board display on move listing
3883        of wild games.
3884        */
3885     switch (ics_getting_history) {
3886       case H_FALSE:
3887       case H_REQUESTED:
3888         break;
3889       case H_GOT_REQ_HEADER:
3890       case H_GOT_UNREQ_HEADER:
3891         /* This is the initial position of the current game */
3892         gamenum = ics_gamenum;
3893         moveNum = 0;            /* old ICS bug workaround */
3894         if (to_play == 'B') {
3895           startedFromSetupPosition = TRUE;
3896           blackPlaysFirst = TRUE;
3897           moveNum = 1;
3898           if (forwardMostMove == 0) forwardMostMove = 1;
3899           if (backwardMostMove == 0) backwardMostMove = 1;
3900           if (currentMove == 0) currentMove = 1;
3901         }
3902         newGameMode = gameMode;
3903         relation = RELATION_STARTING_POSITION; /* ICC needs this */
3904         break;
3905       case H_GOT_UNWANTED_HEADER:
3906         /* This is an initial board that we don't want */
3907         return;
3908       case H_GETTING_MOVES:
3909         /* Should not happen */
3910         DisplayError(_("Error gathering move list: extra board"), 0);
3911         ics_getting_history = H_FALSE;
3912         return;
3913     }
3914
3915    if (gameInfo.boardHeight != ranks || gameInfo.boardWidth != files || 
3916                                         weird && (int)gameInfo.variant <= (int)VariantShogi) {
3917      /* [HGM] We seem to have switched variant unexpectedly
3918       * Try to guess new variant from board size
3919       */
3920           VariantClass newVariant = VariantFairy; // if 8x8, but fairies present
3921           if(ranks == 8 && files == 10) newVariant = VariantCapablanca; else
3922           if(ranks == 10 && files == 9) newVariant = VariantXiangqi; else
3923           if(ranks == 8 && files == 12) newVariant = VariantCourier; else
3924           if(ranks == 9 && files == 9)  newVariant = VariantShogi; else
3925           if(!weird) newVariant = VariantNormal;
3926           VariantSwitch(boards[currentMove], newVariant); /* temp guess */
3927           /* Get a move list just to see the header, which
3928              will tell us whether this is really bug or zh */
3929           if (ics_getting_history == H_FALSE) {
3930             ics_getting_history = H_REQUESTED; reqFlag = TRUE;
3931             sprintf(str, "%smoves %d\n", ics_prefix, gamenum);
3932             SendToICS(str);
3933           }
3934     }
3935     
3936     /* Take action if this is the first board of a new game, or of a
3937        different game than is currently being displayed.  */
3938     if (gamenum != ics_gamenum || newGameMode != gameMode ||
3939         relation == RELATION_ISOLATED_BOARD) {
3940         
3941         /* Forget the old game and get the history (if any) of the new one */
3942         if (gameMode != BeginningOfGame) {
3943           Reset(TRUE, TRUE);
3944         }
3945         newGame = TRUE;
3946         if (appData.autoRaiseBoard) BoardToTop();
3947         prevMove = -3;
3948         if (gamenum == -1) {
3949             newGameMode = IcsIdle;
3950         } else if ((moveNum > 0 || newGameMode == IcsObserving) && newGameMode != IcsIdle &&
3951                    appData.getMoveList && !reqFlag) {
3952             /* Need to get game history */
3953             ics_getting_history = H_REQUESTED;
3954             sprintf(str, "%smoves %d\n", ics_prefix, gamenum);
3955             SendToICS(str);
3956         }
3957         
3958         /* Initially flip the board to have black on the bottom if playing
3959            black or if the ICS flip flag is set, but let the user change
3960            it with the Flip View button. */
3961         flipView = appData.autoFlipView ? 
3962           (newGameMode == IcsPlayingBlack) || ics_flip :
3963           appData.flipView;
3964         
3965         /* Done with values from previous mode; copy in new ones */
3966         gameMode = newGameMode;
3967         ModeHighlight();
3968         ics_gamenum = gamenum;
3969         if (gamenum == gs_gamenum) {
3970             int klen = strlen(gs_kind);
3971             if (gs_kind[klen - 1] == '.') gs_kind[klen - 1] = NULLCHAR;
3972             sprintf(str, "ICS %s", gs_kind);
3973             gameInfo.event = StrSave(str);
3974         } else {
3975             gameInfo.event = StrSave("ICS game");
3976         }
3977         gameInfo.site = StrSave(appData.icsHost);
3978         gameInfo.date = PGNDate();
3979         gameInfo.round = StrSave("-");
3980         gameInfo.white = StrSave(white);
3981         gameInfo.black = StrSave(black);
3982         timeControl = basetime * 60 * 1000;
3983         timeControl_2 = 0;
3984         timeIncrement = increment * 1000;
3985         movesPerSession = 0;
3986         gameInfo.timeControl = TimeControlTagValue();
3987         VariantSwitch(boards[currentMove], StringToVariant(gameInfo.event) );
3988   if (appData.debugMode) {
3989     fprintf(debugFP, "ParseBoard says variant = '%s'\n", gameInfo.event);
3990     fprintf(debugFP, "recognized as %s\n", VariantName(gameInfo.variant));
3991     setbuf(debugFP, NULL);
3992   }
3993
3994         gameInfo.outOfBook = NULL;
3995         
3996         /* Do we have the ratings? */
3997         if (strcmp(player1Name, white) == 0 &&
3998             strcmp(player2Name, black) == 0) {
3999             if (appData.debugMode)
4000               fprintf(debugFP, "Remembered ratings: W %d, B %d\n",
4001                       player1Rating, player2Rating);
4002             gameInfo.whiteRating = player1Rating;
4003             gameInfo.blackRating = player2Rating;
4004         } else if (strcmp(player2Name, white) == 0 &&
4005                    strcmp(player1Name, black) == 0) {
4006             if (appData.debugMode)
4007               fprintf(debugFP, "Remembered ratings: W %d, B %d\n",
4008                       player2Rating, player1Rating);
4009             gameInfo.whiteRating = player2Rating;
4010             gameInfo.blackRating = player1Rating;
4011         }
4012         player1Name[0] = player2Name[0] = NULLCHAR;
4013
4014         /* Silence shouts if requested */
4015         if (appData.quietPlay &&
4016             (gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack)) {
4017             SendToICS(ics_prefix);
4018             SendToICS("set shout 0\n");
4019         }
4020     }
4021     
4022     /* Deal with midgame name changes */
4023     if (!newGame) {
4024         if (!gameInfo.white || strcmp(gameInfo.white, white) != 0) {
4025             if (gameInfo.white) free(gameInfo.white);
4026             gameInfo.white = StrSave(white);
4027         }
4028         if (!gameInfo.black || strcmp(gameInfo.black, black) != 0) {
4029             if (gameInfo.black) free(gameInfo.black);
4030             gameInfo.black = StrSave(black);
4031         }
4032     }
4033     
4034     /* Throw away game result if anything actually changes in examine mode */
4035     if (gameMode == IcsExamining && !newGame) {
4036         gameInfo.result = GameUnfinished;
4037         if (gameInfo.resultDetails != NULL) {
4038             free(gameInfo.resultDetails);
4039             gameInfo.resultDetails = NULL;
4040         }
4041     }
4042     
4043     /* In pausing && IcsExamining mode, we ignore boards coming
4044        in if they are in a different variation than we are. */
4045     if (pauseExamInvalid) return;
4046     if (pausing && gameMode == IcsExamining) {
4047         if (moveNum <= pauseExamForwardMostMove) {
4048             pauseExamInvalid = TRUE;
4049             forwardMostMove = pauseExamForwardMostMove;
4050             return;
4051         }
4052     }
4053     
4054   if (appData.debugMode) {
4055     fprintf(debugFP, "load %dx%d board\n", files, ranks);
4056   }
4057     /* Parse the board */
4058     for (k = 0; k < ranks; k++) {
4059       for (j = 0; j < files; j++)
4060         board[k][j+gameInfo.holdingsWidth] = CharToPiece(board_chars[(ranks-1-k)*(files+1) + j]);
4061       if(gameInfo.holdingsWidth > 1) {
4062            board[k][0] = board[k][BOARD_WIDTH-1] = EmptySquare;
4063            board[k][1] = board[k][BOARD_WIDTH-2] = (ChessSquare) 0;;
4064       }
4065     }
4066     CopyBoard(boards[moveNum], board);
4067     boards[moveNum][HOLDINGS_SET] = 0; // [HGM] indicate holdings not set
4068     if (moveNum == 0) {
4069         startedFromSetupPosition =
4070           !CompareBoards(board, initialPosition);
4071         if(startedFromSetupPosition)
4072             initialRulePlies = irrev_count; /* [HGM] 50-move counter offset */
4073     }
4074
4075     /* [HGM] Set castling rights. Take the outermost Rooks,
4076        to make it also work for FRC opening positions. Note that board12
4077        is really defective for later FRC positions, as it has no way to
4078        indicate which Rook can castle if they are on the same side of King.
4079        For the initial position we grant rights to the outermost Rooks,
4080        and remember thos rights, and we then copy them on positions
4081        later in an FRC game. This means WB might not recognize castlings with
4082        Rooks that have moved back to their original position as illegal,
4083        but in ICS mode that is not its job anyway.
4084     */
4085     if(moveNum == 0 || gameInfo.variant != VariantFischeRandom)
4086     { int i, j; ChessSquare wKing = WhiteKing, bKing = BlackKing;
4087
4088         for(i=BOARD_LEFT, j=NoRights; i<BOARD_RGHT; i++)
4089             if(board[0][i] == WhiteRook) j = i;
4090         initialRights[0] = boards[moveNum][CASTLING][0] = (castle_ws == 0 && gameInfo.variant != VariantFischeRandom ? NoRights : j);
4091         for(i=BOARD_RGHT-1, j=NoRights; i>=BOARD_LEFT; i--)
4092             if(board[0][i] == WhiteRook) j = i;
4093         initialRights[1] = boards[moveNum][CASTLING][1] = (castle_wl == 0 && gameInfo.variant != VariantFischeRandom ? NoRights : j);
4094         for(i=BOARD_LEFT, j=NoRights; i<BOARD_RGHT; i++)
4095             if(board[BOARD_HEIGHT-1][i] == BlackRook) j = i;
4096         initialRights[3] = boards[moveNum][CASTLING][3] = (castle_bs == 0 && gameInfo.variant != VariantFischeRandom ? NoRights : j);
4097         for(i=BOARD_RGHT-1, j=NoRights; i>=BOARD_LEFT; i--)
4098             if(board[BOARD_HEIGHT-1][i] == BlackRook) j = i;
4099         initialRights[4] = boards[moveNum][CASTLING][4] = (castle_bl == 0 && gameInfo.variant != VariantFischeRandom ? NoRights : j);
4100
4101         if(gameInfo.variant == VariantKnightmate) { wKing = WhiteUnicorn; bKing = BlackUnicorn; }
4102         for(k=BOARD_LEFT; k<BOARD_RGHT; k++)
4103             if(board[0][k] == wKing) initialRights[2] = boards[moveNum][CASTLING][2] = k;
4104         for(k=BOARD_LEFT; k<BOARD_RGHT; k++)
4105             if(board[BOARD_HEIGHT-1][k] == bKing)
4106                 initialRights[5] = boards[moveNum][CASTLING][5] = k;
4107         if(gameInfo.variant == VariantTwoKings) {
4108             // In TwoKings looking for a King does not work, so always give castling rights to a King on e1/e8
4109             if(board[0][4] == wKing) initialRights[2] = boards[moveNum][CASTLING][2] = 4;
4110             if(board[BOARD_HEIGHT-1][4] == bKing) initialRights[5] = boards[moveNum][CASTLING][5] = 4;
4111         }
4112     } else { int r;
4113         r = boards[moveNum][CASTLING][0] = initialRights[0];
4114         if(board[0][r] != WhiteRook) boards[moveNum][CASTLING][0] = NoRights;
4115         r = boards[moveNum][CASTLING][1] = initialRights[1];
4116         if(board[0][r] != WhiteRook) boards[moveNum][CASTLING][1] = NoRights;
4117         r = boards[moveNum][CASTLING][3] = initialRights[3];
4118         if(board[BOARD_HEIGHT-1][r] != BlackRook) boards[moveNum][CASTLING][3] = NoRights;
4119         r = boards[moveNum][CASTLING][4] = initialRights[4];
4120         if(board[BOARD_HEIGHT-1][r] != BlackRook) boards[moveNum][CASTLING][4] = NoRights;
4121         /* wildcastle kludge: always assume King has rights */
4122         r = boards[moveNum][CASTLING][2] = initialRights[2];
4123         r = boards[moveNum][CASTLING][5] = initialRights[5];
4124     }
4125     /* [HGM] e.p. rights. Assume that ICS sends file number here? */
4126     boards[moveNum][EP_STATUS] = double_push == -1 ? EP_NONE : double_push + BOARD_LEFT;
4127
4128     
4129     if (ics_getting_history == H_GOT_REQ_HEADER ||
4130         ics_getting_history == H_GOT_UNREQ_HEADER) {
4131         /* This was an initial position from a move list, not
4132            the current position */
4133         return;
4134     }
4135     
4136     /* Update currentMove and known move number limits */
4137     newMove = newGame || moveNum > forwardMostMove;
4138
4139     if (newGame) {
4140         forwardMostMove = backwardMostMove = currentMove = moveNum;
4141         if (gameMode == IcsExamining && moveNum == 0) {
4142           /* Workaround for ICS limitation: we are not told the wild
4143              type when starting to examine a game.  But if we ask for
4144              the move list, the move list header will tell us */
4145             ics_getting_history = H_REQUESTED;
4146             sprintf(str, "%smoves %d\n", ics_prefix, gamenum);
4147             SendToICS(str);
4148         }
4149     } else if (moveNum == forwardMostMove + 1 || moveNum == forwardMostMove
4150                || (moveNum < forwardMostMove && moveNum >= backwardMostMove)) {
4151 #if ZIPPY
4152         /* [DM] If we found takebacks during icsEngineAnalyze try send to engine */
4153         /* [HGM] applied this also to an engine that is silently watching        */
4154         if (appData.zippyPlay && moveNum < forwardMostMove && first.initDone &&
4155             (gameMode == IcsObserving || gameMode == IcsExamining) &&
4156             gameInfo.variant == currentlyInitializedVariant) {
4157           takeback = forwardMostMove - moveNum;
4158           for (i = 0; i < takeback; i++) {
4159             if (appData.debugMode) fprintf(debugFP, "take back move\n");
4160             SendToProgram("undo\n", &first);
4161           }
4162         }
4163 #endif
4164
4165         forwardMostMove = moveNum;
4166         if (!pausing || currentMove > forwardMostMove)
4167           currentMove = forwardMostMove;
4168     } else {
4169         /* New part of history that is not contiguous with old part */ 
4170         if (pausing && gameMode == IcsExamining) {
4171             pauseExamInvalid = TRUE;
4172             forwardMostMove = pauseExamForwardMostMove;
4173             return;
4174         }
4175         if (gameMode == IcsExamining && moveNum > 0 && appData.getMoveList) {
4176 #if ZIPPY
4177             if(appData.zippyPlay && forwardMostMove > 0 && first.initDone) {
4178                 // [HGM] when we will receive the move list we now request, it will be
4179                 // fed to the engine from the first move on. So if the engine is not
4180                 // in the initial position now, bring it there.
4181                 InitChessProgram(&first, 0);
4182             }
4183 #endif
4184             ics_getting_history = H_REQUESTED;
4185             sprintf(str, "%smoves %d\n", ics_prefix, gamenum);
4186             SendToICS(str);
4187         }
4188         forwardMostMove = backwardMostMove = currentMove = moveNum;
4189     }
4190     
4191     /* Update the clocks */
4192     if (strchr(elapsed_time, '.')) {
4193       /* Time is in ms */
4194       timeRemaining[0][moveNum] = whiteTimeRemaining = white_time;
4195       timeRemaining[1][moveNum] = blackTimeRemaining = black_time;
4196     } else {
4197       /* Time is in seconds */
4198       timeRemaining[0][moveNum] = whiteTimeRemaining = white_time * 1000;
4199       timeRemaining[1][moveNum] = blackTimeRemaining = black_time * 1000;
4200     }
4201       
4202
4203 #if ZIPPY
4204     if (appData.zippyPlay && newGame &&
4205         gameMode != IcsObserving && gameMode != IcsIdle &&
4206         gameMode != IcsExamining)
4207       ZippyFirstBoard(moveNum, basetime, increment);
4208 #endif
4209     
4210     /* Put the move on the move list, first converting
4211        to canonical algebraic form. */
4212     if (moveNum > 0) {
4213   if (appData.debugMode) {
4214     if (appData.debugMode) { int f = forwardMostMove;
4215         fprintf(debugFP, "parseboard %d, castling = %d %d %d %d %d %d\n", f,
4216                 boards[f][CASTLING][0],boards[f][CASTLING][1],boards[f][CASTLING][2],
4217                 boards[f][CASTLING][3],boards[f][CASTLING][4],boards[f][CASTLING][5]);
4218     }
4219     fprintf(debugFP, "accepted move %s from ICS, parse it.\n", move_str);
4220     fprintf(debugFP, "moveNum = %d\n", moveNum);
4221     fprintf(debugFP, "board = %d-%d x %d\n", BOARD_LEFT, BOARD_RGHT, BOARD_HEIGHT);
4222     setbuf(debugFP, NULL);
4223   }
4224         if (moveNum <= backwardMostMove) {
4225             /* We don't know what the board looked like before
4226                this move.  Punt. */
4227             strcpy(parseList[moveNum - 1], move_str);
4228             strcat(parseList[moveNum - 1], " ");
4229             strcat(parseList[moveNum - 1], elapsed_time);
4230             moveList[moveNum - 1][0] = NULLCHAR;
4231         } else if (strcmp(move_str, "none") == 0) {
4232             // [HGM] long SAN: swapped order; test for 'none' before parsing move
4233             /* Again, we don't know what the board looked like;
4234                this is really the start of the game. */
4235             parseList[moveNum - 1][0] = NULLCHAR;
4236             moveList[moveNum - 1][0] = NULLCHAR;
4237             backwardMostMove = moveNum;
4238             startedFromSetupPosition = TRUE;
4239             fromX = fromY = toX = toY = -1;
4240         } else {
4241           // [HGM] long SAN: if legality-testing is off, disambiguation might not work or give wrong move. 
4242           //                 So we parse the long-algebraic move string in stead of the SAN move
4243           int valid; char buf[MSG_SIZ], *prom;
4244
4245           // str looks something like "Q/a1-a2"; kill the slash
4246           if(str[1] == '/') 
4247                 sprintf(buf, "%c%s", str[0], str+2);
4248           else  strcpy(buf, str); // might be castling
4249           if((prom = strstr(move_str, "=")) && !strstr(buf, "=")) 
4250                 strcat(buf, prom); // long move lacks promo specification!
4251           if(!appData.testLegality && move_str[1] != '@') { // drops never ambiguous (parser chokes on long form!)
4252                 if(appData.debugMode) 
4253                         fprintf(debugFP, "replaced ICS move '%s' by '%s'\n", move_str, buf);
4254                 strcpy(move_str, buf);
4255           }
4256           valid = ParseOneMove(move_str, moveNum - 1, &moveType,
4257                                 &fromX, &fromY, &toX, &toY, &promoChar)
4258                || ParseOneMove(buf, moveNum - 1, &moveType,
4259                                 &fromX, &fromY, &toX, &toY, &promoChar);
4260           // end of long SAN patch
4261           if (valid) {
4262             (void) CoordsToAlgebraic(boards[moveNum - 1],
4263                                      PosFlags(moveNum - 1),
4264                                      fromY, fromX, toY, toX, promoChar,
4265                                      parseList[moveNum-1]);
4266             switch (MateTest(boards[moveNum], PosFlags(moveNum)) ) {
4267               case MT_NONE:
4268               case MT_STALEMATE:
4269               default:
4270                 break;
4271               case MT_CHECK:
4272                 if(gameInfo.variant != VariantShogi)
4273                     strcat(parseList[moveNum - 1], "+");
4274                 break;
4275               case MT_CHECKMATE:
4276               case MT_STAINMATE: // [HGM] xq: for notation stalemate that wins counts as checkmate
4277                 strcat(parseList[moveNum - 1], "#");
4278                 break;
4279             }
4280             strcat(parseList[moveNum - 1], " ");
4281             strcat(parseList[moveNum - 1], elapsed_time);
4282             /* currentMoveString is set as a side-effect of ParseOneMove */
4283             strcpy(moveList[moveNum - 1], currentMoveString);
4284             strcat(moveList[moveNum - 1], "\n");
4285           } else {
4286             /* Move from ICS was illegal!?  Punt. */
4287   if (appData.debugMode) {
4288     fprintf(debugFP, "Illegal move from ICS '%s'\n", move_str);
4289     fprintf(debugFP, "board L=%d, R=%d, H=%d, holdings=%d\n", BOARD_LEFT, BOARD_RGHT, BOARD_HEIGHT, gameInfo.holdingsWidth);
4290   }
4291             strcpy(parseList[moveNum - 1], move_str);
4292             strcat(parseList[moveNum - 1], " ");
4293             strcat(parseList[moveNum - 1], elapsed_time);
4294             moveList[moveNum - 1][0] = NULLCHAR;
4295             fromX = fromY = toX = toY = -1;
4296           }
4297         }
4298   if (appData.debugMode) {
4299     fprintf(debugFP, "Move parsed to '%s'\n", parseList[moveNum - 1]);
4300     setbuf(debugFP, NULL);
4301   }
4302
4303 #if ZIPPY
4304         /* Send move to chess program (BEFORE animating it). */
4305         if (appData.zippyPlay && !newGame && newMove && 
4306            (!appData.getMoveList || backwardMostMove == 0) && first.initDone) {
4307
4308             if ((gameMode == IcsPlayingWhite && WhiteOnMove(moveNum)) ||
4309                 (gameMode == IcsPlayingBlack && !WhiteOnMove(moveNum))) {
4310                 if (moveList[moveNum - 1][0] == NULLCHAR) {
4311                     sprintf(str, _("Couldn't parse move \"%s\" from ICS"),
4312                             move_str);
4313                     DisplayError(str, 0);
4314                 } else {
4315                     if (first.sendTime) {
4316                         SendTimeRemaining(&first, gameMode == IcsPlayingWhite);
4317                     }
4318                     bookHit = SendMoveToBookUser(moveNum - 1, &first, FALSE); // [HGM] book
4319                     if (firstMove && !bookHit) {
4320                         firstMove = FALSE;
4321                         if (first.useColors) {
4322                           SendToProgram(gameMode == IcsPlayingWhite ?
4323                                         "white\ngo\n" :
4324                                         "black\ngo\n", &first);
4325                         } else {
4326                           SendToProgram("go\n", &first);
4327                         }
4328                         first.maybeThinking = TRUE;
4329                     }
4330                 }
4331             } else if (gameMode == IcsObserving || gameMode == IcsExamining) {
4332               if (moveList[moveNum - 1][0] == NULLCHAR) {
4333                 sprintf(str, _("Couldn't parse move \"%s\" from ICS"), move_str);
4334                 DisplayError(str, 0);
4335               } else {
4336                 if(gameInfo.variant == currentlyInitializedVariant) // [HGM] refrain sending moves engine can't understand!
4337                 SendMoveToProgram(moveNum - 1, &first);
4338               }
4339             }
4340         }
4341 #endif
4342     }
4343
4344     if (moveNum > 0 && !gotPremove && !appData.noGUI) {
4345         /* If move comes from a remote source, animate it.  If it
4346            isn't remote, it will have already been animated. */
4347         if (!pausing && !ics_user_moved && prevMove == moveNum - 1) {
4348             AnimateMove(boards[moveNum - 1], fromX, fromY, toX, toY);
4349         }
4350         if (!pausing && appData.highlightLastMove) {
4351             SetHighlights(fromX, fromY, toX, toY);
4352         }
4353     }
4354     
4355     /* Start the clocks */
4356     whiteFlag = blackFlag = FALSE;
4357     appData.clockMode = !(basetime == 0 && increment == 0);
4358     if (ticking == 0) {
4359       ics_clock_paused = TRUE;
4360       StopClocks();
4361     } else if (ticking == 1) {
4362       ics_clock_paused = FALSE;
4363     }
4364     if (gameMode == IcsIdle ||
4365         relation == RELATION_OBSERVING_STATIC ||
4366         relation == RELATION_EXAMINING ||
4367         ics_clock_paused)
4368       DisplayBothClocks();
4369     else
4370       StartClocks();
4371     
4372     /* Display opponents and material strengths */
4373     if (gameInfo.variant != VariantBughouse &&
4374         gameInfo.variant != VariantCrazyhouse && !appData.noGUI) {
4375         if (tinyLayout || smallLayout) {
4376             if(gameInfo.variant == VariantNormal)
4377                 sprintf(str, "%s(%d) %s(%d) {%d %d}", 
4378                     gameInfo.white, white_stren, gameInfo.black, black_stren,
4379                     basetime, increment);
4380             else
4381                 sprintf(str, "%s(%d) %s(%d) {%d %d w%d}", 
4382                     gameInfo.white, white_stren, gameInfo.black, black_stren,
4383                     basetime, increment, (int) gameInfo.variant);
4384         } else {
4385             if(gameInfo.variant == VariantNormal)
4386                 sprintf(str, "%s (%d) vs. %s (%d) {%d %d}", 
4387                     gameInfo.white, white_stren, gameInfo.black, black_stren,
4388                     basetime, increment);
4389             else
4390                 sprintf(str, "%s (%d) vs. %s (%d) {%d %d %s}", 
4391                     gameInfo.white, white_stren, gameInfo.black, black_stren,
4392                     basetime, increment, VariantName(gameInfo.variant));
4393         }
4394         DisplayTitle(str);
4395   if (appData.debugMode) {
4396     fprintf(debugFP, "Display title '%s, gameInfo.variant = %d'\n", str, gameInfo.variant);
4397   }
4398     }
4399
4400
4401     /* Display the board */
4402     if (!pausing && !appData.noGUI) {
4403       
4404       if (appData.premove)
4405           if (!gotPremove || 
4406              ((gameMode == IcsPlayingWhite) && (WhiteOnMove(currentMove))) ||
4407              ((gameMode == IcsPlayingBlack) && (!WhiteOnMove(currentMove))))
4408               ClearPremoveHighlights();
4409
4410       j = seekGraphUp; seekGraphUp = FALSE; // [HGM] seekgraph: when we draw a board, it overwrites the seek graph
4411         if(partnerUp) { flipView = originalFlip; partnerUp = FALSE; j = TRUE; } // [HGM] bughouse: restore view
4412       DrawPosition(j, boards[currentMove]);
4413
4414       DisplayMove(moveNum - 1);
4415       if (appData.ringBellAfterMoves && /*!ics_user_moved*/ // [HGM] use absolute method to recognize own move
4416             !((gameMode == IcsPlayingWhite) && (!WhiteOnMove(moveNum)) ||
4417               (gameMode == IcsPlayingBlack) &&  (WhiteOnMove(moveNum))   ) ) {
4418         if(newMove) RingBell(); else PlayIcsUnfinishedSound();
4419       }
4420     }
4421
4422     HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove-1);
4423 #if ZIPPY
4424     if(bookHit) { // [HGM] book: simulate book reply
4425         static char bookMove[MSG_SIZ]; // a bit generous?
4426
4427         programStats.nodes = programStats.depth = programStats.time = 
4428         programStats.score = programStats.got_only_move = 0;
4429         sprintf(programStats.movelist, "%s (xbook)", bookHit);
4430
4431         strcpy(bookMove, "move ");
4432         strcat(bookMove, bookHit);
4433         HandleMachineMove(bookMove, &first);
4434     }
4435 #endif
4436 }
4437
4438 void
4439 GetMoveListEvent()
4440 {
4441     char buf[MSG_SIZ];
4442     if (appData.icsActive && gameMode != IcsIdle && ics_gamenum > 0) {
4443         ics_getting_history = H_REQUESTED;
4444         sprintf(buf, "%smoves %d\n", ics_prefix, ics_gamenum);
4445         SendToICS(buf);
4446     }
4447 }
4448
4449 void
4450 AnalysisPeriodicEvent(force)
4451      int force;
4452 {
4453     if (((programStats.ok_to_send == 0 || programStats.line_is_book)
4454          && !force) || !appData.periodicUpdates)
4455       return;
4456
4457     /* Send . command to Crafty to collect stats */
4458     SendToProgram(".\n", &first);
4459
4460     /* Don't send another until we get a response (this makes
4461        us stop sending to old Crafty's which don't understand
4462        the "." command (sending illegal cmds resets node count & time,
4463        which looks bad)) */
4464     programStats.ok_to_send = 0;
4465 }
4466
4467 void ics_update_width(new_width)
4468         int new_width;
4469 {
4470         ics_printf("set width %d\n", new_width);
4471 }
4472
4473 void
4474 SendMoveToProgram(moveNum, cps)
4475      int moveNum;
4476      ChessProgramState *cps;
4477 {
4478     char buf[MSG_SIZ];
4479
4480     if (cps->useUsermove) {
4481       SendToProgram("usermove ", cps);
4482     }
4483     if (cps->useSAN) {
4484       char *space;
4485       if ((space = strchr(parseList[moveNum], ' ')) != NULL) {
4486         int len = space - parseList[moveNum];
4487         memcpy(buf, parseList[moveNum], len);
4488         buf[len++] = '\n';
4489         buf[len] = NULLCHAR;
4490       } else {
4491         sprintf(buf, "%s\n", parseList[moveNum]);
4492       }
4493       SendToProgram(buf, cps);
4494     } else {
4495       if(cps->alphaRank) { /* [HGM] shogi: temporarily convert to shogi coordinates before sending */
4496         AlphaRank(moveList[moveNum], 4);
4497         SendToProgram(moveList[moveNum], cps);
4498         AlphaRank(moveList[moveNum], 4); // and back
4499       } else
4500       /* Added by Tord: Send castle moves in "O-O" in FRC games if required by
4501        * the engine. It would be nice to have a better way to identify castle 
4502        * moves here. */
4503       if((gameInfo.variant == VariantFischeRandom || gameInfo.variant == VariantCapaRandom)
4504                                                                          && cps->useOOCastle) {
4505         int fromX = moveList[moveNum][0] - AAA; 
4506         int fromY = moveList[moveNum][1] - ONE;
4507         int toX = moveList[moveNum][2] - AAA; 
4508         int toY = moveList[moveNum][3] - ONE;
4509         if((boards[moveNum][fromY][fromX] == WhiteKing 
4510             && boards[moveNum][toY][toX] == WhiteRook)
4511            || (boards[moveNum][fromY][fromX] == BlackKing 
4512                && boards[moveNum][toY][toX] == BlackRook)) {
4513           if(toX > fromX) SendToProgram("O-O\n", cps);
4514           else SendToProgram("O-O-O\n", cps);
4515         }
4516         else SendToProgram(moveList[moveNum], cps);
4517       }
4518       else SendToProgram(moveList[moveNum], cps);
4519       /* End of additions by Tord */
4520     }
4521
4522     /* [HGM] setting up the opening has brought engine in force mode! */
4523     /*       Send 'go' if we are in a mode where machine should play. */
4524     if( (moveNum == 0 && setboardSpoiledMachineBlack && cps == &first) &&
4525         (gameMode == TwoMachinesPlay   ||
4526 #if ZIPPY
4527          gameMode == IcsPlayingBlack     || gameMode == IcsPlayingWhite ||
4528 #endif
4529          gameMode == MachinePlaysBlack || gameMode == MachinePlaysWhite) ) {
4530         SendToProgram("go\n", cps);
4531   if (appData.debugMode) {
4532     fprintf(debugFP, "(extra)\n");
4533   }
4534     }
4535     setboardSpoiledMachineBlack = 0;
4536 }
4537
4538 void
4539 SendMoveToICS(moveType, fromX, fromY, toX, toY)
4540      ChessMove moveType;
4541      int fromX, fromY, toX, toY;
4542 {
4543     char user_move[MSG_SIZ];
4544
4545     switch (moveType) {
4546       default:
4547         sprintf(user_move, _("say Internal error; bad moveType %d (%d,%d-%d,%d)"),
4548                 (int)moveType, fromX, fromY, toX, toY);
4549         DisplayError(user_move + strlen("say "), 0);
4550         break;
4551       case WhiteKingSideCastle:
4552       case BlackKingSideCastle:
4553       case WhiteQueenSideCastleWild:
4554       case BlackQueenSideCastleWild:
4555       /* PUSH Fabien */
4556       case WhiteHSideCastleFR:
4557       case BlackHSideCastleFR:
4558       /* POP Fabien */
4559         sprintf(user_move, "o-o\n");
4560         break;
4561       case WhiteQueenSideCastle:
4562       case BlackQueenSideCastle:
4563       case WhiteKingSideCastleWild:
4564       case BlackKingSideCastleWild:
4565       /* PUSH Fabien */
4566       case WhiteASideCastleFR:
4567       case BlackASideCastleFR:
4568       /* POP Fabien */
4569         sprintf(user_move, "o-o-o\n");
4570         break;
4571       case WhitePromotionQueen:
4572       case BlackPromotionQueen:
4573       case WhitePromotionRook:
4574       case BlackPromotionRook:
4575       case WhitePromotionBishop:
4576       case BlackPromotionBishop:
4577       case WhitePromotionKnight:
4578       case BlackPromotionKnight:
4579       case WhitePromotionKing:
4580       case BlackPromotionKing:
4581       case WhitePromotionChancellor:
4582       case BlackPromotionChancellor:
4583       case WhitePromotionArchbishop:
4584       case BlackPromotionArchbishop:
4585         if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantCourier || gameInfo.variant == VariantMakruk)
4586             sprintf(user_move, "%c%c%c%c=%c\n",
4587                 AAA + fromX, ONE + fromY, AAA + toX, ONE + toY,
4588                 PieceToChar(WhiteFerz));
4589         else if(gameInfo.variant == VariantGreat)
4590             sprintf(user_move, "%c%c%c%c=%c\n",
4591                 AAA + fromX, ONE + fromY, AAA + toX, ONE + toY,
4592                 PieceToChar(WhiteMan));
4593         else
4594             sprintf(user_move, "%c%c%c%c=%c\n",
4595                 AAA + fromX, ONE + fromY, AAA + toX, ONE + toY,
4596                 PieceToChar(PromoPiece(moveType)));
4597         break;
4598       case WhiteDrop:
4599       case BlackDrop:
4600         sprintf(user_move, "%c@%c%c\n",
4601                 ToUpper(PieceToChar((ChessSquare) fromX)),
4602                 AAA + toX, ONE + toY);
4603         break;
4604       case NormalMove:
4605       case WhiteCapturesEnPassant:
4606       case BlackCapturesEnPassant:
4607       case IllegalMove:  /* could be a variant we don't quite understand */
4608         sprintf(user_move, "%c%c%c%c\n",
4609                 AAA + fromX, ONE + fromY, AAA + toX, ONE + toY);
4610         break;
4611     }
4612     SendToICS(user_move);
4613     if(appData.keepAlive) // [HGM] alive: schedule sending of dummy 'date' command
4614         ScheduleDelayedEvent(KeepAlive, appData.keepAlive*60*1000);
4615 }
4616
4617 void
4618 UploadGameEvent()
4619 {   // [HGM] upload: send entire stored game to ICS as long-algebraic moves.
4620     int i, last = forwardMostMove; // make sure ICS reply cannot pre-empt us by clearing fmm
4621     static char *castlingStrings[4] = { "none", "kside", "qside", "both" };
4622     if(gameMode == IcsObserving || gameMode == IcsPlayingBlack || gameMode == IcsPlayingWhite) {
4623         DisplayError("You cannot do this while you are playing or observing", 0);
4624         return;
4625     }
4626     if(gameMode != IcsExamining) { // is this ever not the case?
4627         char buf[MSG_SIZ], *p, *fen, command[MSG_SIZ], bsetup = 0;
4628
4629         if(ics_type == ICS_ICC) { // on ICC match ourselves in applicable variant
4630             sprintf(command, "match %s", ics_handle);
4631         } else { // on FICS we must first go to general examine mode
4632             strcpy(command, "examine\nbsetup"); // and specify variant within it with bsetups
4633         }
4634         if(gameInfo.variant != VariantNormal) {
4635             // try figure out wild number, as xboard names are not always valid on ICS
4636             for(i=1; i<=36; i++) {
4637                 sprintf(buf, "wild/%d", i);
4638                 if(StringToVariant(buf) == gameInfo.variant) break;
4639             }
4640             if(i<=36 && ics_type == ICS_ICC) sprintf(buf, "%s w%d\n", command, i);
4641             else if(i == 22) sprintf(buf, "%s fr\n", command);
4642             else sprintf(buf, "%s %s\n", command, VariantName(gameInfo.variant));
4643         } else sprintf(buf, "%s\n", ics_type == ICS_ICC ? command : "examine\n"); // match yourself or examine
4644         SendToICS(ics_prefix);
4645         SendToICS(buf);
4646         if(startedFromSetupPosition || backwardMostMove != 0) {
4647           fen = PositionToFEN(backwardMostMove, NULL);
4648           if(ics_type == ICS_ICC) { // on ICC we can simply send a complete FEN to set everything
4649             sprintf(buf, "loadfen %s\n", fen);
4650             SendToICS(buf);
4651           } else { // FICS: everything has to set by separate bsetup commands
4652             p = strchr(fen, ' '); p[0] = NULLCHAR; // cut after board
4653             sprintf(buf, "bsetup fen %s\n", fen);
4654             SendToICS(buf);
4655             if(!WhiteOnMove(backwardMostMove)) {
4656                 SendToICS("bsetup tomove black\n");
4657             }
4658             i = (strchr(p+3, 'K') != NULL) + 2*(strchr(p+3, 'Q') != NULL);
4659             sprintf(buf, "bsetup wcastle %s\n", castlingStrings[i]);
4660             SendToICS(buf);
4661             i = (strchr(p+3, 'k') != NULL) + 2*(strchr(p+3, 'q') != NULL);
4662             sprintf(buf, "bsetup bcastle %s\n", castlingStrings[i]);
4663             SendToICS(buf);
4664             i = boards[backwardMostMove][EP_STATUS];
4665             if(i >= 0) { // set e.p.
4666                 sprintf(buf, "bsetup eppos %c\n", i+AAA);
4667                 SendToICS(buf);
4668             }
4669             bsetup++;
4670           }
4671         }
4672       if(bsetup || ics_type != ICS_ICC && gameInfo.variant != VariantNormal)
4673             SendToICS("bsetup done\n"); // switch to normal examining.
4674     }
4675     for(i = backwardMostMove; i<last; i++) {
4676         char buf[20];
4677         sprintf(buf, "%s\n", parseList[i]);
4678         SendToICS(buf);
4679     }
4680     SendToICS(ics_prefix);
4681     SendToICS(ics_type == ICS_ICC ? "tag result Game in progress\n" : "commit\n");
4682 }
4683
4684 void
4685 CoordsToComputerAlgebraic(rf, ff, rt, ft, promoChar, move)
4686      int rf, ff, rt, ft;
4687      char promoChar;
4688      char move[7];
4689 {
4690     if (rf == DROP_RANK) {
4691         sprintf(move, "%c@%c%c\n",
4692                 ToUpper(PieceToChar((ChessSquare) ff)), AAA + ft, ONE + rt);
4693     } else {
4694         if (promoChar == 'x' || promoChar == NULLCHAR) {
4695             sprintf(move, "%c%c%c%c\n",
4696                     AAA + ff, ONE + rf, AAA + ft, ONE + rt);
4697         } else {
4698             sprintf(move, "%c%c%c%c%c\n",
4699                     AAA + ff, ONE + rf, AAA + ft, ONE + rt, promoChar);
4700         }
4701     }
4702 }
4703
4704 void
4705 ProcessICSInitScript(f)
4706      FILE *f;
4707 {
4708     char buf[MSG_SIZ];
4709
4710     while (fgets(buf, MSG_SIZ, f)) {
4711         SendToICSDelayed(buf,(long)appData.msLoginDelay);
4712     }
4713
4714     fclose(f);
4715 }
4716
4717
4718 /* [HGM] Shogi move preprocessor: swap digits for letters, vice versa */
4719 void
4720 AlphaRank(char *move, int n)
4721 {
4722 //    char *p = move, c; int x, y;
4723
4724     if (appData.debugMode) {
4725         fprintf(debugFP, "alphaRank(%s,%d)\n", move, n);
4726     }
4727
4728     if(move[1]=='*' && 
4729        move[2]>='0' && move[2]<='9' &&
4730        move[3]>='a' && move[3]<='x'    ) {
4731         move[1] = '@';
4732         move[2] = BOARD_RGHT  -1 - (move[2]-'1') + AAA;
4733         move[3] = BOARD_HEIGHT-1 - (move[3]-'a') + ONE;
4734     } else
4735     if(move[0]>='0' && move[0]<='9' &&
4736        move[1]>='a' && move[1]<='x' &&
4737        move[2]>='0' && move[2]<='9' &&
4738        move[3]>='a' && move[3]<='x'    ) {
4739         /* input move, Shogi -> normal */
4740         move[0] = BOARD_RGHT  -1 - (move[0]-'1') + AAA;
4741         move[1] = BOARD_HEIGHT-1 - (move[1]-'a') + ONE;
4742         move[2] = BOARD_RGHT  -1 - (move[2]-'1') + AAA;
4743         move[3] = BOARD_HEIGHT-1 - (move[3]-'a') + ONE;
4744     } else
4745     if(move[1]=='@' &&
4746        move[3]>='0' && move[3]<='9' &&
4747        move[2]>='a' && move[2]<='x'    ) {
4748         move[1] = '*';
4749         move[2] = BOARD_RGHT - 1 - (move[2]-AAA) + '1';
4750         move[3] = BOARD_HEIGHT-1 - (move[3]-ONE) + 'a';
4751     } else
4752     if(
4753        move[0]>='a' && move[0]<='x' &&
4754        move[3]>='0' && move[3]<='9' &&
4755        move[2]>='a' && move[2]<='x'    ) {
4756          /* output move, normal -> Shogi */
4757         move[0] = BOARD_RGHT - 1 - (move[0]-AAA) + '1';
4758         move[1] = BOARD_HEIGHT-1 - (move[1]-ONE) + 'a';
4759         move[2] = BOARD_RGHT - 1 - (move[2]-AAA) + '1';
4760         move[3] = BOARD_HEIGHT-1 - (move[3]-ONE) + 'a';
4761         if(move[4] == PieceToChar(BlackQueen)) move[4] = '+';
4762     }
4763     if (appData.debugMode) {
4764         fprintf(debugFP, "   out = '%s'\n", move);
4765     }
4766 }
4767
4768 char yy_textstr[8000];
4769
4770 /* Parser for moves from gnuchess, ICS, or user typein box */
4771 Boolean
4772 ParseOneMove(move, moveNum, moveType, fromX, fromY, toX, toY, promoChar)
4773      char *move;
4774      int moveNum;
4775      ChessMove *moveType;
4776      int *fromX, *fromY, *toX, *toY;
4777      char *promoChar;
4778 {       
4779     if (appData.debugMode) {
4780         fprintf(debugFP, "move to parse: %s\n", move);
4781     }
4782     *moveType = yylexstr(moveNum, move, yy_textstr, sizeof yy_textstr);
4783
4784     switch (*moveType) {
4785       case WhitePromotionChancellor:
4786       case BlackPromotionChancellor:
4787       case WhitePromotionArchbishop:
4788       case BlackPromotionArchbishop:
4789       case WhitePromotionQueen:
4790       case BlackPromotionQueen:
4791       case WhitePromotionRook:
4792       case BlackPromotionRook:
4793       case WhitePromotionBishop:
4794       case BlackPromotionBishop:
4795       case WhitePromotionKnight:
4796       case BlackPromotionKnight:
4797       case WhitePromotionKing:
4798       case BlackPromotionKing:
4799       case NormalMove:
4800       case WhiteCapturesEnPassant:
4801       case BlackCapturesEnPassant:
4802       case WhiteKingSideCastle:
4803       case WhiteQueenSideCastle:
4804       case BlackKingSideCastle:
4805       case BlackQueenSideCastle:
4806       case WhiteKingSideCastleWild:
4807       case WhiteQueenSideCastleWild:
4808       case BlackKingSideCastleWild:
4809       case BlackQueenSideCastleWild:
4810       /* Code added by Tord: */
4811       case WhiteHSideCastleFR:
4812       case WhiteASideCastleFR:
4813       case BlackHSideCastleFR:
4814       case BlackASideCastleFR:
4815       /* End of code added by Tord */
4816       case IllegalMove:         /* bug or odd chess variant */
4817         *fromX = currentMoveString[0] - AAA;
4818         *fromY = currentMoveString[1] - ONE;
4819         *toX = currentMoveString[2] - AAA;
4820         *toY = currentMoveString[3] - ONE;
4821         *promoChar = currentMoveString[4];
4822         if (*fromX < BOARD_LEFT || *fromX >= BOARD_RGHT || *fromY < 0 || *fromY >= BOARD_HEIGHT ||
4823             *toX < BOARD_LEFT || *toX >= BOARD_RGHT || *toY < 0 || *toY >= BOARD_HEIGHT) {
4824     if (appData.debugMode) {
4825         fprintf(debugFP, "Off-board move (%d,%d)-(%d,%d)%c, type = %d\n", *fromX, *fromY, *toX, *toY, *promoChar, *moveType);
4826     }
4827             *fromX = *fromY = *toX = *toY = 0;
4828             return FALSE;
4829         }
4830         if (appData.testLegality) {
4831           return (*moveType != IllegalMove);
4832         } else {
4833           return !(*fromX == *toX && *fromY == *toY) && boards[moveNum][*fromY][*fromX] != EmptySquare && 
4834                         WhiteOnMove(moveNum) == (boards[moveNum][*fromY][*fromX] < BlackPawn);
4835         }
4836
4837       case WhiteDrop:
4838       case BlackDrop:
4839         *fromX = *moveType == WhiteDrop ?
4840           (int) CharToPiece(ToUpper(currentMoveString[0])) :
4841           (int) CharToPiece(ToLower(currentMoveString[0]));
4842         *fromY = DROP_RANK;
4843         *toX = currentMoveString[2] - AAA;
4844         *toY = currentMoveString[3] - ONE;
4845         *promoChar = NULLCHAR;
4846         return TRUE;
4847
4848       case AmbiguousMove:
4849       case ImpossibleMove:
4850       case (ChessMove) 0:       /* end of file */
4851       case ElapsedTime:
4852       case Comment:
4853       case PGNTag:
4854       case NAG:
4855       case WhiteWins:
4856       case BlackWins:
4857       case GameIsDrawn:
4858       default:
4859     if (appData.debugMode) {
4860         fprintf(debugFP, "Impossible move %s, type = %d\n", currentMoveString, *moveType);
4861     }
4862         /* bug? */
4863         *fromX = *fromY = *toX = *toY = 0;
4864         *promoChar = NULLCHAR;
4865         return FALSE;
4866     }
4867 }
4868
4869
4870 void
4871 ParsePV(char *pv, Boolean storeComments)
4872 { // Parse a string of PV moves, and append to current game, behind forwardMostMove
4873   int fromX, fromY, toX, toY; char promoChar;
4874   ChessMove moveType;
4875   Boolean valid;
4876   int nr = 0;
4877
4878   endPV = forwardMostMove;
4879   do {
4880     while(*pv == ' ' || *pv == '\n' || *pv == '\t') pv++; // must still read away whitespace
4881     if(nr == 0 && !storeComments && *pv == '(') pv++; // first (ponder) move can be in parentheses
4882     valid = ParseOneMove(pv, endPV, &moveType, &fromX, &fromY, &toX, &toY, &promoChar);
4883 if(appData.debugMode){
4884 fprintf(debugFP,"parsePV: %d %c%c%c%c yy='%s'\nPV = '%s'\n", valid, fromX+AAA, fromY+ONE, toX+AAA, toY+ONE, yy_textstr, pv);
4885 }
4886     if(!valid && nr == 0 &&
4887        ParseOneMove(pv, endPV-1, &moveType, &fromX, &fromY, &toX, &toY, &promoChar)){ 
4888         nr++; moveType = Comment; // First move has been played; kludge to make sure we continue
4889         // Hande case where played move is different from leading PV move
4890         CopyBoard(boards[endPV+1], boards[endPV-1]); // tentatively unplay last game move
4891         CopyBoard(boards[endPV+2], boards[endPV-1]); // and play first move of PV
4892         ApplyMove(fromX, fromY, toX, toY, promoChar, boards[endPV+2]);
4893         if(!CompareBoards(boards[endPV], boards[endPV+2])) {
4894           endPV += 2; // if position different, keep this
4895           moveList[endPV-1][0] = fromX + AAA;
4896           moveList[endPV-1][1] = fromY + ONE;
4897           moveList[endPV-1][2] = toX + AAA;
4898           moveList[endPV-1][3] = toY + ONE;
4899           parseList[endPV-1][0] = NULLCHAR;
4900           strcpy(moveList[endPV-2], "_0_0"); // suppress premove highlight on takeback move
4901         }
4902       }
4903     pv = strstr(pv, yy_textstr) + strlen(yy_textstr); // skip what we parsed
4904     if(nr == 0 && !storeComments && *pv == ')') pv++; // closing parenthesis of ponder move;
4905     if(moveType == Comment && storeComments) AppendComment(endPV, yy_textstr, FALSE);
4906     if(moveType == Comment || moveType == NAG || moveType == ElapsedTime) {
4907         valid++; // allow comments in PV
4908         continue;
4909     }
4910     nr++;
4911     if(endPV+1 > framePtr) break; // no space, truncate
4912     if(!valid) break;
4913     endPV++;
4914     CopyBoard(boards[endPV], boards[endPV-1]);
4915     ApplyMove(fromX, fromY, toX, toY, promoChar, boards[endPV]);
4916     moveList[endPV-1][0] = fromX + AAA;
4917     moveList[endPV-1][1] = fromY + ONE;
4918     moveList[endPV-1][2] = toX + AAA;
4919     moveList[endPV-1][3] = toY + ONE;
4920     if(storeComments)
4921         CoordsToAlgebraic(boards[endPV - 1],
4922                              PosFlags(endPV - 1),
4923                              fromY, fromX, toY, toX, promoChar,
4924                              parseList[endPV - 1]);
4925     else
4926         parseList[endPV-1][0] = NULLCHAR;
4927   } while(valid);
4928   currentMove = endPV;
4929   if(currentMove == forwardMostMove) ClearPremoveHighlights(); else
4930   SetPremoveHighlights(moveList[currentMove-1][0]-AAA, moveList[currentMove-1][1]-ONE,
4931                        moveList[currentMove-1][2]-AAA, moveList[currentMove-1][3]-ONE);
4932   DrawPosition(TRUE, boards[currentMove]);
4933 }
4934
4935 static int lastX, lastY;
4936
4937 Boolean
4938 LoadMultiPV(int x, int y, char *buf, int index, int *start, int *end)
4939 {
4940         int startPV;
4941         char *p;
4942
4943         if(index < 0 || index >= strlen(buf)) return FALSE; // sanity
4944         lastX = x; lastY = y;
4945         while(index > 0 && buf[index-1] != '\n') index--; // beginning of line
4946         startPV = index;
4947         while(buf[index] != '\n') if(buf[index++] == '\t') startPV = index;
4948         if(index == startPV && (p = StrCaseStr(buf+index, "PV="))) startPV = p - buf + 3;
4949         index = startPV;
4950         do{ while(buf[index] && buf[index] != '\n') index++;
4951         } while(buf[index] == '\n' && buf[index+1] == '\\' && buf[index+2] == ' ' && index++); // join kibitzed PV continuation line
4952         buf[index] = 0;
4953         ParsePV(buf+startPV, FALSE);
4954         *start = startPV; *end = index-1;
4955         return TRUE;
4956 }
4957
4958 Boolean
4959 LoadPV(int x, int y)
4960 { // called on right mouse click to load PV
4961   int which = gameMode == TwoMachinesPlay && (WhiteOnMove(forwardMostMove) == (second.twoMachinesColor[0] == 'w'));
4962   lastX = x; lastY = y;
4963   ParsePV(lastPV[which], FALSE); // load the PV of the thinking engine in the boards array.
4964   return TRUE;
4965 }
4966
4967 void
4968 UnLoadPV()
4969 {
4970   if(endPV < 0) return;
4971   endPV = -1;
4972   currentMove = forwardMostMove;
4973   ClearPremoveHighlights();
4974   DrawPosition(TRUE, boards[currentMove]);
4975 }
4976
4977 void
4978 MovePV(int x, int y, int h)
4979 { // step through PV based on mouse coordinates (called on mouse move)
4980   int margin = h>>3, step = 0;
4981
4982   if(endPV < 0) return;
4983   // we must somehow check if right button is still down (might be released off board!)
4984   if(y < margin && (abs(x - lastX) > 6 || abs(y - lastY) > 6)) step = 1; else
4985   if(y > h - margin && (abs(x - lastX) > 6 || abs(y - lastY) > 6)) step = -1; else
4986   if( y > lastY + 6 ) step = -1; else if(y < lastY - 6) step = 1;
4987   if(!step) return;
4988   lastX = x; lastY = y;
4989   if(currentMove + step > endPV || currentMove + step < forwardMostMove) step = 0;
4990   currentMove += step;
4991   if(currentMove == forwardMostMove) ClearPremoveHighlights(); else
4992   SetPremoveHighlights(moveList[currentMove-1][0]-AAA, moveList[currentMove-1][1]-ONE,
4993                        moveList[currentMove-1][2]-AAA, moveList[currentMove-1][3]-ONE);
4994   DrawPosition(FALSE, boards[currentMove]);
4995 }
4996
4997
4998 // [HGM] shuffle: a general way to suffle opening setups, applicable to arbitrary variants.
4999 // All positions will have equal probability, but the current method will not provide a unique
5000 // numbering scheme for arrays that contain 3 or more pieces of the same kind.
5001 #define DARK 1
5002 #define LITE 2
5003 #define ANY 3
5004
5005 int squaresLeft[4];
5006 int piecesLeft[(int)BlackPawn];
5007 int seed, nrOfShuffles;
5008
5009 void GetPositionNumber()
5010 {       // sets global variable seed
5011         int i;
5012
5013         seed = appData.defaultFrcPosition;
5014         if(seed < 0) { // randomize based on time for negative FRC position numbers
5015                 for(i=0; i<50; i++) seed += random();
5016                 seed = random() ^ random() >> 8 ^ random() << 8;
5017                 if(seed<0) seed = -seed;
5018         }
5019 }
5020
5021 int put(Board board, int pieceType, int rank, int n, int shade)
5022 // put the piece on the (n-1)-th empty squares of the given shade
5023 {
5024         int i;
5025
5026         for(i=BOARD_LEFT; i<BOARD_RGHT; i++) {
5027                 if( (((i-BOARD_LEFT)&1)+1) & shade && board[rank][i] == EmptySquare && n-- == 0) {
5028                         board[rank][i] = (ChessSquare) pieceType;
5029                         squaresLeft[((i-BOARD_LEFT)&1) + 1]--;
5030                         squaresLeft[ANY]--;
5031                         piecesLeft[pieceType]--; 
5032                         return i;
5033                 }
5034         }
5035         return -1;
5036 }
5037
5038
5039 void AddOnePiece(Board board, int pieceType, int rank, int shade)
5040 // calculate where the next piece goes, (any empty square), and put it there
5041 {
5042         int i;
5043
5044         i = seed % squaresLeft[shade];
5045         nrOfShuffles *= squaresLeft[shade];
5046         seed /= squaresLeft[shade];
5047         put(board, pieceType, rank, i, shade);
5048 }
5049
5050 void AddTwoPieces(Board board, int pieceType, int rank)
5051 // calculate where the next 2 identical pieces go, (any empty square), and put it there
5052 {
5053         int i, n=squaresLeft[ANY], j=n-1, k;
5054
5055         k = n*(n-1)/2; // nr of possibilities, not counting permutations
5056         i = seed % k;  // pick one
5057         nrOfShuffles *= k;
5058         seed /= k;
5059         while(i >= j) i -= j--;
5060         j = n - 1 - j; i += j;
5061         put(board, pieceType, rank, j, ANY);
5062         put(board, pieceType, rank, i, ANY);
5063 }
5064
5065 void SetUpShuffle(Board board, int number)
5066 {
5067         int i, p, first=1;
5068
5069         GetPositionNumber(); nrOfShuffles = 1;
5070
5071         squaresLeft[DARK] = (BOARD_RGHT - BOARD_LEFT + 1)/2;
5072         squaresLeft[ANY]  = BOARD_RGHT - BOARD_LEFT;
5073         squaresLeft[LITE] = squaresLeft[ANY] - squaresLeft[DARK];
5074
5075         for(p = 0; p<=(int)WhiteKing; p++) piecesLeft[p] = 0;
5076
5077         for(i=BOARD_LEFT; i<BOARD_RGHT; i++) { // count pieces and clear board
5078             p = (int) board[0][i];
5079             if(p < (int) BlackPawn) piecesLeft[p] ++;
5080             board[0][i] = EmptySquare;
5081         }
5082
5083         if(PosFlags(0) & F_ALL_CASTLE_OK) {
5084             // shuffles restricted to allow normal castling put KRR first
5085             if(piecesLeft[(int)WhiteKing]) // King goes rightish of middle
5086                 put(board, WhiteKing, 0, (gameInfo.boardWidth+1)/2, ANY);
5087             else if(piecesLeft[(int)WhiteUnicorn]) // in Knightmate Unicorn castles
5088                 put(board, WhiteUnicorn, 0, (gameInfo.boardWidth+1)/2, ANY);
5089             if(piecesLeft[(int)WhiteRook]) // First supply a Rook for K-side castling
5090                 put(board, WhiteRook, 0, gameInfo.boardWidth-2, ANY);
5091             if(piecesLeft[(int)WhiteRook]) // Then supply a Rook for Q-side castling
5092                 put(board, WhiteRook, 0, 0, ANY);
5093             // in variants with super-numerary Kings and Rooks, we leave these for the shuffle
5094         }
5095
5096         if(((BOARD_RGHT-BOARD_LEFT) & 1) == 0)
5097             // only for even boards make effort to put pairs of colorbound pieces on opposite colors
5098             for(p = (int) WhiteKing; p > (int) WhitePawn; p--) {
5099                 if(p != (int) WhiteBishop && p != (int) WhiteFerz && p != (int) WhiteAlfil) continue;
5100                 while(piecesLeft[p] >= 2) {
5101                     AddOnePiece(board, p, 0, LITE);
5102                     AddOnePiece(board, p, 0, DARK);
5103                 }
5104                 // Odd color-bound pieces are shuffled with the rest (to not run out of paired squares)
5105             }
5106
5107         for(p = (int) WhiteKing - 2; p > (int) WhitePawn; p--) {
5108             // Remaining pieces (non-colorbound, or odd color bound) can be put anywhere
5109             // but we leave King and Rooks for last, to possibly obey FRC restriction
5110             if(p == (int)WhiteRook) continue;
5111             while(piecesLeft[p] >= 2) AddTwoPieces(board, p, 0); // add in pairs, for not counting permutations
5112             if(piecesLeft[p]) AddOnePiece(board, p, 0, ANY);     // add the odd piece
5113         }
5114
5115         // now everything is placed, except perhaps King (Unicorn) and Rooks
5116
5117         if(PosFlags(0) & F_FRC_TYPE_CASTLING) {
5118             // Last King gets castling rights
5119             while(piecesLeft[(int)WhiteUnicorn]) {
5120                 i = put(board, WhiteUnicorn, 0, piecesLeft[(int)WhiteRook]/2, ANY);
5121                 initialRights[2]  = initialRights[5]  = board[CASTLING][2] = board[CASTLING][5] = i;
5122             }
5123
5124             while(piecesLeft[(int)WhiteKing]) {
5125                 i = put(board, WhiteKing, 0, piecesLeft[(int)WhiteRook]/2, ANY);
5126                 initialRights[2]  = initialRights[5]  = board[CASTLING][2] = board[CASTLING][5] = i;
5127             }
5128
5129
5130         } else {
5131             while(piecesLeft[(int)WhiteKing])    AddOnePiece(board, WhiteKing, 0, ANY);
5132             while(piecesLeft[(int)WhiteUnicorn]) AddOnePiece(board, WhiteUnicorn, 0, ANY);
5133         }
5134
5135         // Only Rooks can be left; simply place them all
5136         while(piecesLeft[(int)WhiteRook]) {
5137                 i = put(board, WhiteRook, 0, 0, ANY);
5138                 if(PosFlags(0) & F_FRC_TYPE_CASTLING) { // first and last Rook get FRC castling rights
5139                         if(first) {
5140                                 first=0;
5141                                 initialRights[1]  = initialRights[4]  = board[CASTLING][1] = board[CASTLING][4] = i;
5142                         }
5143                         initialRights[0]  = initialRights[3]  = board[CASTLING][0] = board[CASTLING][3] = i;
5144                 }
5145         }
5146         for(i=BOARD_LEFT; i<BOARD_RGHT; i++) { // copy black from white
5147             board[BOARD_HEIGHT-1][i] =  (int) board[0][i] < BlackPawn ? WHITE_TO_BLACK board[0][i] : EmptySquare;
5148         }
5149
5150         if(number >= 0) appData.defaultFrcPosition %= nrOfShuffles; // normalize
5151 }
5152
5153 int SetCharTable( char *table, const char * map )
5154 /* [HGM] moved here from winboard.c because of its general usefulness */
5155 /*       Basically a safe strcpy that uses the last character as King */
5156 {
5157     int result = FALSE; int NrPieces;
5158
5159     if( map != NULL && (NrPieces=strlen(map)) <= (int) EmptySquare 
5160                     && NrPieces >= 12 && !(NrPieces&1)) {
5161         int i; /* [HGM] Accept even length from 12 to 34 */
5162
5163         for( i=0; i<(int) EmptySquare; i++ ) table[i] = '.';
5164         for( i=0; i<NrPieces/2-1; i++ ) {
5165             table[i] = map[i];
5166             table[i + (int)BlackPawn - (int) WhitePawn] = map[i+NrPieces/2];
5167         }
5168         table[(int) WhiteKing]  = map[NrPieces/2-1];
5169         table[(int) BlackKing]  = map[NrPieces-1];
5170
5171         result = TRUE;
5172     }
5173
5174     return result;
5175 }
5176
5177 void Prelude(Board board)
5178 {       // [HGM] superchess: random selection of exo-pieces
5179         int i, j, k; ChessSquare p; 
5180         static ChessSquare exoPieces[4] = { WhiteAngel, WhiteMarshall, WhiteSilver, WhiteLance };
5181
5182         GetPositionNumber(); // use FRC position number
5183
5184         if(appData.pieceToCharTable != NULL) { // select pieces to participate from given char table
5185             SetCharTable(pieceToChar, appData.pieceToCharTable);
5186             for(i=(int)WhiteQueen+1, j=0; i<(int)WhiteKing && j<4; i++) 
5187                 if(PieceToChar((ChessSquare)i) != '.') exoPieces[j++] = (ChessSquare) i;
5188         }
5189
5190         j = seed%4;                 seed /= 4; 
5191         p = board[0][BOARD_LEFT+j];   board[0][BOARD_LEFT+j] = EmptySquare; k = PieceToNumber(p);
5192         board[k][BOARD_WIDTH-1] = p;  board[k][BOARD_WIDTH-2]++;
5193         board[BOARD_HEIGHT-1-k][0] = WHITE_TO_BLACK p;  board[BOARD_HEIGHT-1-k][1]++;
5194         j = seed%3 + (seed%3 >= j); seed /= 3; 
5195         p = board[0][BOARD_LEFT+j];   board[0][BOARD_LEFT+j] = EmptySquare; k = PieceToNumber(p);
5196         board[k][BOARD_WIDTH-1] = p;  board[k][BOARD_WIDTH-2]++;
5197         board[BOARD_HEIGHT-1-k][0] = WHITE_TO_BLACK p;  board[BOARD_HEIGHT-1-k][1]++;
5198         j = seed%3;                 seed /= 3; 
5199         p = board[0][BOARD_LEFT+j+5]; board[0][BOARD_LEFT+j+5] = EmptySquare; k = PieceToNumber(p);
5200         board[k][BOARD_WIDTH-1] = p;  board[k][BOARD_WIDTH-2]++;
5201         board[BOARD_HEIGHT-1-k][0] = WHITE_TO_BLACK p;  board[BOARD_HEIGHT-1-k][1]++;
5202         j = seed%2 + (seed%2 >= j); seed /= 2; 
5203         p = board[0][BOARD_LEFT+j+5]; board[0][BOARD_LEFT+j+5] = EmptySquare; k = PieceToNumber(p);
5204         board[k][BOARD_WIDTH-1] = p;  board[k][BOARD_WIDTH-2]++;
5205         board[BOARD_HEIGHT-1-k][0] = WHITE_TO_BLACK p;  board[BOARD_HEIGHT-1-k][1]++;
5206         j = seed%4; seed /= 4; put(board, exoPieces[3],    0, j, ANY);
5207         j = seed%3; seed /= 3; put(board, exoPieces[2],   0, j, ANY);
5208         j = seed%2; seed /= 2; put(board, exoPieces[1], 0, j, ANY);
5209         put(board, exoPieces[0],    0, 0, ANY);
5210         for(i=BOARD_LEFT; i<BOARD_RGHT; i++) board[BOARD_HEIGHT-1][i] = WHITE_TO_BLACK board[0][i];
5211 }
5212
5213 void
5214 InitPosition(redraw)
5215      int redraw;
5216 {
5217     ChessSquare (* pieces)[BOARD_FILES];
5218     int i, j, pawnRow, overrule,
5219     oldx = gameInfo.boardWidth,
5220     oldy = gameInfo.boardHeight,
5221     oldh = gameInfo.holdingsWidth,
5222     oldv = gameInfo.variant;
5223
5224     if(appData.icsActive) shuffleOpenings = FALSE; // [HGM] shuffle: in ICS mode, only shuffle on ICS request
5225
5226     /* [AS] Initialize pv info list [HGM] and game status */
5227     {
5228         for( i=0; i<=framePtr; i++ ) { // [HGM] vari: spare saved variations
5229             pvInfoList[i].depth = 0;
5230             boards[i][EP_STATUS] = EP_NONE;
5231             for( j=0; j<BOARD_FILES-2; j++ ) boards[i][CASTLING][j] = NoRights;
5232         }
5233
5234         initialRulePlies = 0; /* 50-move counter start */
5235
5236         castlingRank[0] = castlingRank[1] = castlingRank[2] = 0;
5237         castlingRank[3] = castlingRank[4] = castlingRank[5] = BOARD_HEIGHT-1;
5238     }
5239
5240     
5241     /* [HGM] logic here is completely changed. In stead of full positions */
5242     /* the initialized data only consist of the two backranks. The switch */
5243     /* selects which one we will use, which is than copied to the Board   */
5244     /* initialPosition, which for the rest is initialized by Pawns and    */
5245     /* empty squares. This initial position is then copied to boards[0],  */
5246     /* possibly after shuffling, so that it remains available.            */
5247
5248     gameInfo.holdingsWidth = 0; /* default board sizes */
5249     gameInfo.boardWidth    = 8;
5250     gameInfo.boardHeight   = 8;
5251     gameInfo.holdingsSize  = 0;
5252     nrCastlingRights = -1; /* [HGM] Kludge to indicate default should be used */
5253     for(i=0; i<BOARD_FILES-2; i++)
5254       initialPosition[CASTLING][i] = initialRights[i] = NoRights; /* but no rights yet */
5255     initialPosition[EP_STATUS] = EP_NONE;
5256     SetCharTable(pieceToChar, "PNBRQ...........Kpnbrq...........k");
5257     if(startVariant == gameInfo.variant) // [HGM] nicks: enable nicknames in original variant
5258          SetCharTable(pieceNickName, appData.pieceNickNames);
5259     else SetCharTable(pieceNickName, "............");
5260
5261     switch (gameInfo.variant) {
5262     case VariantFischeRandom:
5263       shuffleOpenings = TRUE;
5264     default:
5265       pieces = FIDEArray;
5266       break;
5267     case VariantShatranj:
5268       pieces = ShatranjArray;
5269       nrCastlingRights = 0;
5270       SetCharTable(pieceToChar, "PN.R.QB...Kpn.r.qb...k"); 
5271       break;
5272     case VariantMakruk:
5273       pieces = makrukArray;
5274       nrCastlingRights = 0;
5275       startedFromSetupPosition = TRUE;
5276       SetCharTable(pieceToChar, "PN.R.M....SKpn.r.m....sk"); 
5277       break;
5278     case VariantTwoKings:
5279       pieces = twoKingsArray;
5280       break;
5281     case VariantCapaRandom:
5282       shuffleOpenings = TRUE;
5283     case VariantCapablanca:
5284       pieces = CapablancaArray;
5285       gameInfo.boardWidth = 10;
5286       SetCharTable(pieceToChar, "PNBRQ..ACKpnbrq..ack"); 
5287       break;
5288     case VariantGothic:
5289       pieces = GothicArray;
5290       gameInfo.boardWidth = 10;
5291       SetCharTable(pieceToChar, "PNBRQ..ACKpnbrq..ack"); 
5292       break;
5293     case VariantJanus:
5294       pieces = JanusArray;
5295       gameInfo.boardWidth = 10;
5296       SetCharTable(pieceToChar, "PNBRQ..JKpnbrq..jk"); 
5297       nrCastlingRights = 6;
5298         initialPosition[CASTLING][0] = initialRights[0] = BOARD_RGHT-1;
5299         initialPosition[CASTLING][1] = initialRights[1] = BOARD_LEFT;
5300         initialPosition[CASTLING][2] = initialRights[2] =(BOARD_WIDTH-1)>>1;
5301         initialPosition[CASTLING][3] = initialRights[3] = BOARD_RGHT-1;
5302         initialPosition[CASTLING][4] = initialRights[4] = BOARD_LEFT;
5303         initialPosition[CASTLING][5] = initialRights[5] =(BOARD_WIDTH-1)>>1;
5304       break;
5305     case VariantFalcon:
5306       pieces = FalconArray;
5307       gameInfo.boardWidth = 10;
5308       SetCharTable(pieceToChar, "PNBRQ.............FKpnbrq.............fk"); 
5309       break;
5310     case VariantXiangqi:
5311       pieces = XiangqiArray;
5312       gameInfo.boardWidth  = 9;
5313       gameInfo.boardHeight = 10;
5314       nrCastlingRights = 0;
5315       SetCharTable(pieceToChar, "PH.R.AE..K.C.ph.r.ae..k.c."); 
5316       break;
5317     case VariantShogi:
5318       pieces = ShogiArray;
5319       gameInfo.boardWidth  = 9;
5320       gameInfo.boardHeight = 9;
5321       gameInfo.holdingsSize = 7;
5322       nrCastlingRights = 0;
5323       SetCharTable(pieceToChar, "PNBRLS...G.++++++Kpnbrls...g.++++++k"); 
5324       break;
5325     case VariantCourier:
5326       pieces = CourierArray;
5327       gameInfo.boardWidth  = 12;
5328       nrCastlingRights = 0;
5329       SetCharTable(pieceToChar, "PNBR.FE..WMKpnbr.fe..wmk"); 
5330       break;
5331     case VariantKnightmate:
5332       pieces = KnightmateArray;
5333       SetCharTable(pieceToChar, "P.BRQ.....M.........K.p.brq.....m.........k."); 
5334       break;
5335     case VariantFairy:
5336       pieces = fairyArray;
5337       SetCharTable(pieceToChar, "PNBRQFEACWMOHIJGDVLSUKpnbrqfeacwmohijgdvlsuk"); 
5338       break;
5339     case VariantGreat:
5340       pieces = GreatArray;
5341       gameInfo.boardWidth = 10;
5342       SetCharTable(pieceToChar, "PN....E...S..HWGMKpn....e...s..hwgmk");
5343       gameInfo.holdingsSize = 8;
5344       break;
5345     case VariantSuper:
5346       pieces = FIDEArray;
5347       SetCharTable(pieceToChar, "PNBRQ..SE.......V.AKpnbrq..se.......v.ak");
5348       gameInfo.holdingsSize = 8;
5349       startedFromSetupPosition = TRUE;
5350       break;
5351     case VariantCrazyhouse:
5352     case VariantBughouse:
5353       pieces = FIDEArray;
5354       SetCharTable(pieceToChar, "PNBRQ.......~~~~Kpnbrq.......~~~~k"); 
5355       gameInfo.holdingsSize = 5;
5356       break;
5357     case VariantWildCastle:
5358       pieces = FIDEArray;
5359       /* !!?shuffle with kings guaranteed to be on d or e file */
5360       shuffleOpenings = 1;
5361       break;
5362     case VariantNoCastle:
5363       pieces = FIDEArray;
5364       nrCastlingRights = 0;
5365       /* !!?unconstrained back-rank shuffle */
5366       shuffleOpenings = 1;
5367       break;
5368     }
5369
5370     overrule = 0;
5371     if(appData.NrFiles >= 0) {
5372         if(gameInfo.boardWidth != appData.NrFiles) overrule++;
5373         gameInfo.boardWidth = appData.NrFiles;
5374     }
5375     if(appData.NrRanks >= 0) {
5376         gameInfo.boardHeight = appData.NrRanks;
5377     }
5378     if(appData.holdingsSize >= 0) {
5379         i = appData.holdingsSize;
5380         if(i > gameInfo.boardHeight) i = gameInfo.boardHeight;
5381         gameInfo.holdingsSize = i;
5382     }
5383     if(gameInfo.holdingsSize) gameInfo.holdingsWidth = 2;
5384     if(BOARD_HEIGHT > BOARD_RANKS || BOARD_WIDTH > BOARD_FILES)
5385         DisplayFatalError(_("Recompile to support this BOARD_RANKS or BOARD_FILES!"), 0, 2);
5386
5387     pawnRow = gameInfo.boardHeight - 7; /* seems to work in all common variants */
5388     if(pawnRow < 1) pawnRow = 1;
5389     if(gameInfo.variant == VariantMakruk) pawnRow = 2;
5390
5391     /* User pieceToChar list overrules defaults */
5392     if(appData.pieceToCharTable != NULL)
5393         SetCharTable(pieceToChar, appData.pieceToCharTable);
5394
5395     for( j=0; j<BOARD_WIDTH; j++ ) { ChessSquare s = EmptySquare;
5396
5397         if(j==BOARD_LEFT-1 || j==BOARD_RGHT)
5398             s = (ChessSquare) 0; /* account holding counts in guard band */
5399         for( i=0; i<BOARD_HEIGHT; i++ )
5400             initialPosition[i][j] = s;
5401
5402         if(j < BOARD_LEFT || j >= BOARD_RGHT || overrule) continue;
5403         initialPosition[0][j] = pieces[0][j-gameInfo.holdingsWidth];
5404         initialPosition[pawnRow][j] = WhitePawn;
5405         initialPosition[BOARD_HEIGHT-pawnRow-1][j] = BlackPawn;
5406         if(gameInfo.variant == VariantXiangqi) {
5407             if(j&1) {
5408                 initialPosition[pawnRow][j] = 
5409                 initialPosition[BOARD_HEIGHT-pawnRow-1][j] = EmptySquare;
5410                 if(j==BOARD_LEFT+1 || j>=BOARD_RGHT-2) {
5411                    initialPosition[2][j] = WhiteCannon;
5412                    initialPosition[BOARD_HEIGHT-3][j] = BlackCannon;
5413                 }
5414             }
5415         }
5416         initialPosition[BOARD_HEIGHT-1][j] =  pieces[1][j-gameInfo.holdingsWidth];
5417     }
5418     if( (gameInfo.variant == VariantShogi) && !overrule ) {
5419
5420             j=BOARD_LEFT+1;
5421             initialPosition[1][j] = WhiteBishop;
5422             initialPosition[BOARD_HEIGHT-2][j] = BlackRook;
5423             j=BOARD_RGHT-2;
5424             initialPosition[1][j] = WhiteRook;
5425             initialPosition[BOARD_HEIGHT-2][j] = BlackBishop;
5426     }
5427
5428     if( nrCastlingRights == -1) {
5429         /* [HGM] Build normal castling rights (must be done after board sizing!) */
5430         /*       This sets default castling rights from none to normal corners   */
5431         /* Variants with other castling rights must set them themselves above    */
5432         nrCastlingRights = 6;
5433        
5434         initialPosition[CASTLING][0] = initialRights[0] = BOARD_RGHT-1;
5435         initialPosition[CASTLING][1] = initialRights[1] = BOARD_LEFT;
5436         initialPosition[CASTLING][2] = initialRights[2] = BOARD_WIDTH>>1;
5437         initialPosition[CASTLING][3] = initialRights[3] = BOARD_RGHT-1;
5438         initialPosition[CASTLING][4] = initialRights[4] = BOARD_LEFT;
5439         initialPosition[CASTLING][5] = initialRights[5] = BOARD_WIDTH>>1;
5440      }
5441
5442      if(gameInfo.variant == VariantSuper) Prelude(initialPosition);
5443      if(gameInfo.variant == VariantGreat) { // promotion commoners
5444         initialPosition[PieceToNumber(WhiteMan)][BOARD_WIDTH-1] = WhiteMan;
5445         initialPosition[PieceToNumber(WhiteMan)][BOARD_WIDTH-2] = 9;
5446         initialPosition[BOARD_HEIGHT-1-PieceToNumber(WhiteMan)][0] = BlackMan;
5447         initialPosition[BOARD_HEIGHT-1-PieceToNumber(WhiteMan)][1] = 9;
5448      }
5449   if (appData.debugMode) {
5450     fprintf(debugFP, "shuffleOpenings = %d\n", shuffleOpenings);
5451   }
5452     if(shuffleOpenings) {
5453         SetUpShuffle(initialPosition, appData.defaultFrcPosition);
5454         startedFromSetupPosition = TRUE;
5455     }
5456     if(startedFromPositionFile) {
5457       /* [HGM] loadPos: use PositionFile for every new game */
5458       CopyBoard(initialPosition, filePosition);
5459       for(i=0; i<nrCastlingRights; i++)
5460           initialRights[i] = filePosition[CASTLING][i];
5461       startedFromSetupPosition = TRUE;
5462     }
5463
5464     CopyBoard(boards[0], initialPosition);
5465
5466     if(oldx != gameInfo.boardWidth ||
5467        oldy != gameInfo.boardHeight ||
5468        oldh != gameInfo.holdingsWidth
5469 #ifdef GOTHIC
5470        || oldv == VariantGothic ||        // For licensing popups
5471        gameInfo.variant == VariantGothic
5472 #endif
5473 #ifdef FALCON
5474        || oldv == VariantFalcon ||
5475        gameInfo.variant == VariantFalcon
5476 #endif
5477                                          )
5478             InitDrawingSizes(-2 ,0);
5479
5480     if (redraw)
5481       DrawPosition(TRUE, boards[currentMove]);
5482 }
5483
5484 void
5485 SendBoard(cps, moveNum)
5486      ChessProgramState *cps;
5487      int moveNum;
5488 {
5489     char message[MSG_SIZ];
5490     
5491     if (cps->useSetboard) {
5492       char* fen = PositionToFEN(moveNum, cps->fenOverride);
5493       sprintf(message, "setboard %s\n", fen);
5494       SendToProgram(message, cps);
5495       free(fen);
5496
5497     } else {
5498       ChessSquare *bp;
5499       int i, j;
5500       /* Kludge to set black to move, avoiding the troublesome and now
5501        * deprecated "black" command.
5502        */
5503       if (!WhiteOnMove(moveNum)) SendToProgram("a2a3\n", cps);
5504
5505       SendToProgram("edit\n", cps);
5506       SendToProgram("#\n", cps);
5507       for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
5508         bp = &boards[moveNum][i][BOARD_LEFT];
5509         for (j = BOARD_LEFT; j < BOARD_RGHT; j++, bp++) {
5510           if ((int) *bp < (int) BlackPawn) {
5511             sprintf(message, "%c%c%c\n", PieceToChar(*bp), 
5512                     AAA + j, ONE + i);
5513             if(message[0] == '+' || message[0] == '~') {
5514                 sprintf(message, "%c%c%c+\n",
5515                         PieceToChar((ChessSquare)(DEMOTED *bp)),
5516                         AAA + j, ONE + i);
5517             }
5518             if(cps->alphaRank) { /* [HGM] shogi: translate coords */
5519                 message[1] = BOARD_RGHT   - 1 - j + '1';
5520                 message[2] = BOARD_HEIGHT - 1 - i + 'a';
5521             }
5522             SendToProgram(message, cps);
5523           }
5524         }
5525       }
5526     
5527       SendToProgram("c\n", cps);
5528       for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
5529         bp = &boards[moveNum][i][BOARD_LEFT];
5530         for (j = BOARD_LEFT; j < BOARD_RGHT; j++, bp++) {
5531           if (((int) *bp != (int) EmptySquare)
5532               && ((int) *bp >= (int) BlackPawn)) {
5533             sprintf(message, "%c%c%c\n", ToUpper(PieceToChar(*bp)),
5534                     AAA + j, ONE + i);
5535             if(message[0] == '+' || message[0] == '~') {
5536                 sprintf(message, "%c%c%c+\n",
5537                         PieceToChar((ChessSquare)(DEMOTED *bp)),
5538                         AAA + j, ONE + i);
5539             }
5540             if(cps->alphaRank) { /* [HGM] shogi: translate coords */
5541                 message[1] = BOARD_RGHT   - 1 - j + '1';
5542                 message[2] = BOARD_HEIGHT - 1 - i + 'a';
5543             }
5544             SendToProgram(message, cps);
5545           }
5546         }
5547       }
5548     
5549       SendToProgram(".\n", cps);
5550     }
5551     setboardSpoiledMachineBlack = 0; /* [HGM] assume WB 4.2.7 already solves this after sending setboard */
5552 }
5553
5554 static int autoQueen; // [HGM] oneclick
5555
5556 int
5557 HasPromotionChoice(int fromX, int fromY, int toX, int toY, char *promoChoice)
5558 {
5559     /* [HGM] rewritten IsPromotion to only flag promotions that offer a choice */
5560     /* [HGM] add Shogi promotions */
5561     int promotionZoneSize=1, highestPromotingPiece = (int)WhitePawn;
5562     ChessSquare piece;
5563     ChessMove moveType;
5564     Boolean premove;
5565
5566     if(fromX < BOARD_LEFT || fromX >= BOARD_RGHT) return FALSE; // drop
5567     if(toX   < BOARD_LEFT || toX   >= BOARD_RGHT) return FALSE; // move into holdings
5568
5569     if(gameMode == EditPosition || gameInfo.variant == VariantXiangqi || // no promotions
5570       !(fromX >=0 && fromY >= 0 && toX >= 0 && toY >= 0) ) // invalid move
5571         return FALSE;
5572
5573     piece = boards[currentMove][fromY][fromX];
5574     if(gameInfo.variant == VariantShogi) {
5575         promotionZoneSize = 3;
5576         highestPromotingPiece = (int)WhiteFerz;
5577     } else if(gameInfo.variant == VariantMakruk) {
5578         promotionZoneSize = 3;
5579     }
5580
5581     // next weed out all moves that do not touch the promotion zone at all
5582     if((int)piece >= BlackPawn) {
5583         if(toY >= promotionZoneSize && fromY >= promotionZoneSize)
5584              return FALSE;
5585         highestPromotingPiece = WHITE_TO_BLACK highestPromotingPiece;
5586     } else {
5587         if(  toY < BOARD_HEIGHT - promotionZoneSize &&
5588            fromY < BOARD_HEIGHT - promotionZoneSize) return FALSE;
5589     }
5590
5591     if( (int)piece > highestPromotingPiece ) return FALSE; // non-promoting piece
5592
5593     // weed out mandatory Shogi promotions
5594     if(gameInfo.variant == VariantShogi) {
5595         if(piece >= BlackPawn) {
5596             if(toY == 0 && piece == BlackPawn ||
5597                toY == 0 && piece == BlackQueen ||
5598                toY <= 1 && piece == BlackKnight) {
5599                 *promoChoice = '+';
5600                 return FALSE;
5601             }
5602         } else {
5603             if(toY == BOARD_HEIGHT-1 && piece == WhitePawn ||
5604                toY == BOARD_HEIGHT-1 && piece == WhiteQueen ||
5605                toY >= BOARD_HEIGHT-2 && piece == WhiteKnight) {
5606                 *promoChoice = '+';
5607                 return FALSE;
5608             }
5609         }
5610     }
5611
5612     // weed out obviously illegal Pawn moves
5613     if(appData.testLegality  && (piece == WhitePawn || piece == BlackPawn) ) {
5614         if(toX > fromX+1 || toX < fromX-1) return FALSE; // wide
5615         if(piece == WhitePawn && toY != fromY+1) return FALSE; // deep
5616         if(piece == BlackPawn && toY != fromY-1) return FALSE; // deep
5617         if(fromX != toX && gameInfo.variant == VariantShogi) return FALSE;
5618         // note we are not allowed to test for valid (non-)capture, due to premove
5619     }
5620
5621     // we either have a choice what to promote to, or (in Shogi) whether to promote
5622     if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantCourier || gameInfo.variant == VariantMakruk) {
5623         *promoChoice = PieceToChar(BlackFerz);  // no choice
5624         return FALSE;
5625     }
5626     if(autoQueen) { // predetermined
5627         if(gameInfo.variant == VariantSuicide || gameInfo.variant == VariantLosers)
5628              *promoChoice = PieceToChar(BlackKing); // in Suicide Q is the last thing we want
5629         else *promoChoice = PieceToChar(BlackQueen);
5630         return FALSE;
5631     }
5632
5633     // suppress promotion popup on illegal moves that are not premoves
5634     premove = gameMode == IcsPlayingWhite && !WhiteOnMove(currentMove) ||
5635               gameMode == IcsPlayingBlack &&  WhiteOnMove(currentMove);
5636     if(appData.testLegality && !premove) {
5637         moveType = LegalityTest(boards[currentMove], PosFlags(currentMove),
5638                         fromY, fromX, toY, toX, NULLCHAR);
5639         if(moveType != WhitePromotionQueen && moveType  != BlackPromotionQueen &&
5640            moveType != WhitePromotionKnight && moveType != BlackPromotionKnight)
5641             return FALSE;
5642     }
5643
5644     return TRUE;
5645 }
5646
5647 int
5648 InPalace(row, column)
5649      int row, column;
5650 {   /* [HGM] for Xiangqi */
5651     if( (row < 3 || row > BOARD_HEIGHT-4) &&
5652          column < (BOARD_WIDTH + 4)/2 &&
5653          column > (BOARD_WIDTH - 5)/2 ) return TRUE;
5654     return FALSE;
5655 }
5656
5657 int
5658 PieceForSquare (x, y)
5659      int x;
5660      int y;
5661 {
5662   if (x < 0 || x >= BOARD_WIDTH || y < 0 || y >= BOARD_HEIGHT)
5663      return -1;
5664   else
5665      return boards[currentMove][y][x];
5666 }
5667
5668 int
5669 OKToStartUserMove(x, y)
5670      int x, y;
5671 {
5672     ChessSquare from_piece;
5673     int white_piece;
5674
5675     if (matchMode) return FALSE;
5676     if (gameMode == EditPosition) return TRUE;
5677
5678     if (x >= 0 && y >= 0)
5679       from_piece = boards[currentMove][y][x];
5680     else
5681       from_piece = EmptySquare;
5682
5683     if (from_piece == EmptySquare) return FALSE;
5684
5685     white_piece = (int)from_piece >= (int)WhitePawn &&
5686       (int)from_piece < (int)BlackPawn; /* [HGM] can be > King! */
5687
5688     switch (gameMode) {
5689       case PlayFromGameFile:
5690       case AnalyzeFile:
5691       case TwoMachinesPlay:
5692       case EndOfGame:
5693         return FALSE;
5694
5695       case IcsObserving:
5696       case IcsIdle:
5697         return FALSE;
5698
5699       case MachinePlaysWhite:
5700       case IcsPlayingBlack:
5701         if (appData.zippyPlay) return FALSE;
5702         if (white_piece) {
5703             DisplayMoveError(_("You are playing Black"));
5704             return FALSE;
5705         }
5706         break;
5707
5708       case MachinePlaysBlack:
5709       case IcsPlayingWhite:
5710         if (appData.zippyPlay) return FALSE;
5711         if (!white_piece) {
5712             DisplayMoveError(_("You are playing White"));
5713             return FALSE;
5714         }
5715         break;
5716
5717       case EditGame:
5718         if (!white_piece && WhiteOnMove(currentMove)) {
5719             DisplayMoveError(_("It is White's turn"));
5720             return FALSE;
5721         }           
5722         if (white_piece && !WhiteOnMove(currentMove)) {
5723             DisplayMoveError(_("It is Black's turn"));
5724             return FALSE;
5725         }           
5726         if (cmailMsgLoaded && (currentMove < cmailOldMove)) {
5727             /* Editing correspondence game history */
5728             /* Could disallow this or prompt for confirmation */
5729             cmailOldMove = -1;
5730         }
5731         break;
5732
5733       case BeginningOfGame:
5734         if (appData.icsActive) return FALSE;
5735         if (!appData.noChessProgram) {
5736             if (!white_piece) {
5737                 DisplayMoveError(_("You are playing White"));
5738                 return FALSE;
5739             }
5740         }
5741         break;
5742         
5743       case Training:
5744         if (!white_piece && WhiteOnMove(currentMove)) {
5745             DisplayMoveError(_("It is White's turn"));
5746             return FALSE;
5747         }           
5748         if (white_piece && !WhiteOnMove(currentMove)) {
5749             DisplayMoveError(_("It is Black's turn"));
5750             return FALSE;
5751         }           
5752         break;
5753
5754       default:
5755       case IcsExamining:
5756         break;
5757     }
5758     if (currentMove != forwardMostMove && gameMode != AnalyzeMode
5759         && gameMode != EditGame // [HGM] vari: treat as AnalyzeMode
5760         && gameMode != AnalyzeFile && gameMode != Training) {
5761         DisplayMoveError(_("Displayed position is not current"));
5762         return FALSE;
5763     }
5764     return TRUE;
5765 }
5766
5767 Boolean
5768 OnlyMove(int *x, int *y, Boolean captures) {
5769     DisambiguateClosure cl;
5770     if (appData.zippyPlay) return FALSE;
5771     switch(gameMode) {
5772       case MachinePlaysBlack:
5773       case IcsPlayingWhite:
5774       case BeginningOfGame:
5775         if(!WhiteOnMove(currentMove)) return FALSE;
5776         break;
5777       case MachinePlaysWhite:
5778       case IcsPlayingBlack:
5779         if(WhiteOnMove(currentMove)) return FALSE;
5780         break;
5781       default:
5782         return FALSE;
5783     }
5784     cl.pieceIn = EmptySquare; 
5785     cl.rfIn = *y;
5786     cl.ffIn = *x;
5787     cl.rtIn = -1;
5788     cl.ftIn = -1;
5789     cl.promoCharIn = NULLCHAR;
5790     Disambiguate(boards[currentMove], PosFlags(currentMove), &cl);
5791     if( cl.kind == NormalMove ||
5792         cl.kind == AmbiguousMove && captures && cl.captures == 1 ||
5793         cl.kind == WhitePromotionQueen || cl.kind == BlackPromotionQueen ||
5794         cl.kind == WhitePromotionKnight || cl.kind == BlackPromotionKnight ||
5795         cl.kind == WhiteCapturesEnPassant || cl.kind == BlackCapturesEnPassant) {
5796       fromX = cl.ff;
5797       fromY = cl.rf;
5798       *x = cl.ft;
5799       *y = cl.rt;
5800       return TRUE;
5801     }
5802     if(cl.kind != ImpossibleMove) return FALSE;
5803     cl.pieceIn = EmptySquare;
5804     cl.rfIn = -1;
5805     cl.ffIn = -1;
5806     cl.rtIn = *y;
5807     cl.ftIn = *x;
5808     cl.promoCharIn = NULLCHAR;
5809     Disambiguate(boards[currentMove], PosFlags(currentMove), &cl);
5810     if( cl.kind == NormalMove ||
5811         cl.kind == AmbiguousMove && captures && cl.captures == 1 ||
5812         cl.kind == WhitePromotionQueen || cl.kind == BlackPromotionQueen ||
5813         cl.kind == WhitePromotionKnight || cl.kind == BlackPromotionKnight ||
5814         cl.kind == WhiteCapturesEnPassant || cl.kind == BlackCapturesEnPassant) {
5815       fromX = cl.ff;
5816       fromY = cl.rf;
5817       *x = cl.ft;
5818       *y = cl.rt;
5819       autoQueen = TRUE; // act as if autoQueen on when we click to-square
5820       return TRUE;
5821     }
5822     return FALSE;
5823 }
5824
5825 FILE *lastLoadGameFP = NULL, *lastLoadPositionFP = NULL;
5826 int lastLoadGameNumber = 0, lastLoadPositionNumber = 0;
5827 int lastLoadGameUseList = FALSE;
5828 char lastLoadGameTitle[MSG_SIZ], lastLoadPositionTitle[MSG_SIZ];
5829 ChessMove lastLoadGameStart = (ChessMove) 0;
5830
5831 ChessMove
5832 UserMoveTest(fromX, fromY, toX, toY, promoChar, captureOwn)
5833      int fromX, fromY, toX, toY;
5834      int promoChar;
5835      Boolean captureOwn;
5836 {
5837     ChessMove moveType;
5838     ChessSquare pdown, pup;
5839
5840     /* Check if the user is playing in turn.  This is complicated because we
5841        let the user "pick up" a piece before it is his turn.  So the piece he
5842        tried to pick up may have been captured by the time he puts it down!
5843        Therefore we use the color the user is supposed to be playing in this
5844        test, not the color of the piece that is currently on the starting
5845        square---except in EditGame mode, where the user is playing both
5846        sides; fortunately there the capture race can't happen.  (It can
5847        now happen in IcsExamining mode, but that's just too bad.  The user
5848        will get a somewhat confusing message in that case.)
5849        */
5850
5851     switch (gameMode) {
5852       case PlayFromGameFile:
5853       case AnalyzeFile:
5854       case TwoMachinesPlay:
5855       case EndOfGame:
5856       case IcsObserving:
5857       case IcsIdle:
5858         /* We switched into a game mode where moves are not accepted,
5859            perhaps while the mouse button was down. */
5860         return ImpossibleMove;
5861
5862       case MachinePlaysWhite:
5863         /* User is moving for Black */
5864         if (WhiteOnMove(currentMove)) {
5865             DisplayMoveError(_("It is White's turn"));
5866             return ImpossibleMove;
5867         }
5868         break;
5869
5870       case MachinePlaysBlack:
5871         /* User is moving for White */
5872         if (!WhiteOnMove(currentMove)) {
5873             DisplayMoveError(_("It is Black's turn"));
5874             return ImpossibleMove;
5875         }
5876         break;
5877
5878       case EditGame:
5879       case IcsExamining:
5880       case BeginningOfGame:
5881       case AnalyzeMode:
5882       case Training:
5883         if ((int) boards[currentMove][fromY][fromX] >= (int) BlackPawn &&
5884             (int) boards[currentMove][fromY][fromX] < (int) EmptySquare) {
5885             /* User is moving for Black */
5886             if (WhiteOnMove(currentMove)) {
5887                 DisplayMoveError(_("It is White's turn"));
5888                 return ImpossibleMove;
5889             }
5890         } else {
5891             /* User is moving for White */
5892             if (!WhiteOnMove(currentMove)) {
5893                 DisplayMoveError(_("It is Black's turn"));
5894                 return ImpossibleMove;
5895             }
5896         }
5897         break;
5898
5899       case IcsPlayingBlack:
5900         /* User is moving for Black */
5901         if (WhiteOnMove(currentMove)) {
5902             if (!appData.premove) {
5903                 DisplayMoveError(_("It is White's turn"));
5904             } else if (toX >= 0 && toY >= 0) {
5905                 premoveToX = toX;
5906                 premoveToY = toY;
5907                 premoveFromX = fromX;
5908                 premoveFromY = fromY;
5909                 premovePromoChar = promoChar;
5910                 gotPremove = 1;
5911                 if (appData.debugMode) 
5912                     fprintf(debugFP, "Got premove: fromX %d,"
5913                             "fromY %d, toX %d, toY %d\n",
5914                             fromX, fromY, toX, toY);
5915             }
5916             return ImpossibleMove;
5917         }
5918         break;
5919
5920       case IcsPlayingWhite:
5921         /* User is moving for White */
5922         if (!WhiteOnMove(currentMove)) {
5923             if (!appData.premove) {
5924                 DisplayMoveError(_("It is Black's turn"));
5925             } else if (toX >= 0 && toY >= 0) {
5926                 premoveToX = toX;
5927                 premoveToY = toY;
5928                 premoveFromX = fromX;
5929                 premoveFromY = fromY;
5930                 premovePromoChar = promoChar;
5931                 gotPremove = 1;
5932                 if (appData.debugMode) 
5933                     fprintf(debugFP, "Got premove: fromX %d,"
5934                             "fromY %d, toX %d, toY %d\n",
5935                             fromX, fromY, toX, toY);
5936             }
5937             return ImpossibleMove;
5938         }
5939         break;
5940
5941       default:
5942         break;
5943
5944       case EditPosition:
5945         /* EditPosition, empty square, or different color piece;
5946            click-click move is possible */
5947         if (toX == -2 || toY == -2) {
5948             boards[0][fromY][fromX] = EmptySquare;
5949             return AmbiguousMove;
5950         } else if (toX >= 0 && toY >= 0) {
5951             boards[0][toY][toX] = boards[0][fromY][fromX];
5952             if(fromX == BOARD_LEFT-2) { // handle 'moves' out of holdings
5953                 if(boards[0][fromY][0] != EmptySquare) {
5954                     if(boards[0][fromY][1]) boards[0][fromY][1]--;
5955                     if(boards[0][fromY][1] == 0)  boards[0][fromY][0] = EmptySquare; 
5956                 }
5957             } else
5958             if(fromX == BOARD_RGHT+1) {
5959                 if(boards[0][fromY][BOARD_WIDTH-1] != EmptySquare) {
5960                     if(boards[0][fromY][BOARD_WIDTH-2]) boards[0][fromY][BOARD_WIDTH-2]--;
5961                     if(boards[0][fromY][BOARD_WIDTH-2] == 0)  boards[0][fromY][BOARD_WIDTH-1] = EmptySquare; 
5962                 }
5963             } else
5964             boards[0][fromY][fromX] = EmptySquare;
5965             return AmbiguousMove;
5966         }
5967         return ImpossibleMove;
5968     }
5969
5970     if(toX < 0 || toY < 0) return ImpossibleMove;
5971     pdown = boards[currentMove][fromY][fromX];
5972     pup = boards[currentMove][toY][toX];
5973
5974     /* [HGM] If move started in holdings, it means a drop */
5975     if( fromX == BOARD_LEFT-2 || fromX == BOARD_RGHT+1) { 
5976          if( pup != EmptySquare ) return ImpossibleMove;
5977          if(appData.testLegality) {
5978              /* it would be more logical if LegalityTest() also figured out
5979               * which drops are legal. For now we forbid pawns on back rank.
5980               * Shogi is on its own here...
5981               */
5982              if( (pdown == WhitePawn || pdown == BlackPawn) &&
5983                  (toY == 0 || toY == BOARD_HEIGHT -1 ) )
5984                  return(ImpossibleMove); /* no pawn drops on 1st/8th */
5985          }
5986          return WhiteDrop; /* Not needed to specify white or black yet */
5987     }
5988
5989     /* [HGM] always test for legality, to get promotion info */
5990     moveType = LegalityTest(boards[currentMove], PosFlags(currentMove),
5991                                          fromY, fromX, toY, toX, promoChar);
5992     /* [HGM] but possibly ignore an IllegalMove result */
5993     if (appData.testLegality) {
5994         if (moveType == IllegalMove || moveType == ImpossibleMove) {
5995             DisplayMoveError(_("Illegal move"));
5996             return ImpossibleMove;
5997         }
5998     }
5999
6000     return moveType;
6001     /* [HGM] <popupFix> in stead of calling FinishMove directly, this
6002        function is made into one that returns an OK move type if FinishMove
6003        should be called. This to give the calling driver routine the
6004        opportunity to finish the userMove input with a promotion popup,
6005        without bothering the user with this for invalid or illegal moves */
6006
6007 /*    FinishMove(moveType, fromX, fromY, toX, toY, promoChar); */
6008 }
6009
6010 /* Common tail of UserMoveEvent and DropMenuEvent */
6011 int
6012 FinishMove(moveType, fromX, fromY, toX, toY, promoChar)
6013      ChessMove moveType;
6014      int fromX, fromY, toX, toY;
6015      /*char*/int promoChar;
6016 {
6017     char *bookHit = 0;
6018
6019     if((gameInfo.variant == VariantSuper || gameInfo.variant == VariantGreat) && promoChar != NULLCHAR) { 
6020         // [HGM] superchess: suppress promotions to non-available piece
6021         int k = PieceToNumber(CharToPiece(ToUpper(promoChar)));
6022         if(WhiteOnMove(currentMove)) {
6023             if(!boards[currentMove][k][BOARD_WIDTH-2]) return 0;
6024         } else {
6025             if(!boards[currentMove][BOARD_HEIGHT-1-k][1]) return 0;
6026         }
6027     }
6028
6029     /* [HGM] <popupFix> kludge to avoid having to know the exact promotion
6030        move type in caller when we know the move is a legal promotion */
6031     if(moveType == NormalMove && promoChar)
6032         moveType = PromoCharToMoveType(WhiteOnMove(currentMove), promoChar);
6033
6034     /* [HGM] convert drag-and-drop piece drops to standard form */
6035     if( (fromX == BOARD_LEFT-2 || fromX == BOARD_RGHT+1) && fromY != DROP_RANK ){
6036          moveType = WhiteOnMove(currentMove) ? WhiteDrop : BlackDrop;
6037            if(appData.debugMode) fprintf(debugFP, "Drop move %d, curr=%d, x=%d,y=%d, p=%d\n", 
6038                 moveType, currentMove, fromX, fromY, boards[currentMove][fromY][fromX]);
6039            // holdings might not be sent yet in ICS play; we have to figure out which piece belongs here
6040            if(fromX == 0) fromY = BOARD_HEIGHT-1 - fromY; // black holdings upside-down
6041            fromX = fromX ? WhitePawn : BlackPawn; // first piece type in selected holdings
6042            while(PieceToChar(fromX) == '.' || PieceToNumber(fromX) != fromY && fromX != (int) EmptySquare) fromX++; 
6043          fromY = DROP_RANK;
6044     }
6045
6046     /* [HGM] <popupFix> The following if has been moved here from
6047        UserMoveEvent(). Because it seemed to belong here (why not allow
6048        piece drops in training games?), and because it can only be
6049        performed after it is known to what we promote. */
6050     if (gameMode == Training) {
6051       /* compare the move played on the board to the next move in the
6052        * game. If they match, display the move and the opponent's response. 
6053        * If they don't match, display an error message.
6054        */
6055       int saveAnimate;
6056       Board testBoard;
6057       CopyBoard(testBoard, boards[currentMove]);
6058       ApplyMove(fromX, fromY, toX, toY, promoChar, testBoard);
6059
6060       if (CompareBoards(testBoard, boards[currentMove+1])) {
6061         ForwardInner(currentMove+1);
6062
6063         /* Autoplay the opponent's response.
6064          * if appData.animate was TRUE when Training mode was entered,
6065          * the response will be animated.
6066          */
6067         saveAnimate = appData.animate;
6068         appData.animate = animateTraining;
6069         ForwardInner(currentMove+1);
6070         appData.animate = saveAnimate;
6071
6072         /* check for the end of the game */
6073         if (currentMove >= forwardMostMove) {
6074           gameMode = PlayFromGameFile;
6075           ModeHighlight();
6076           SetTrainingModeOff();
6077           DisplayInformation(_("End of game"));
6078         }
6079       } else {
6080         DisplayError(_("Incorrect move"), 0);
6081       }
6082       return 1;
6083     }
6084
6085   /* Ok, now we know that the move is good, so we can kill
6086      the previous line in Analysis Mode */
6087   if ((gameMode == AnalyzeMode || gameMode == EditGame) 
6088                                 && currentMove < forwardMostMove) {
6089     PushTail(currentMove, forwardMostMove); // [HGM] vari: save tail of game
6090   }
6091
6092   /* If we need the chess program but it's dead, restart it */
6093   ResurrectChessProgram();
6094
6095   /* A user move restarts a paused game*/
6096   if (pausing)
6097     PauseEvent();
6098
6099   thinkOutput[0] = NULLCHAR;
6100
6101   MakeMove(fromX, fromY, toX, toY, promoChar); /*updates forwardMostMove*/
6102
6103   if(Adjudicate(NULL)) return 1; // [HGM] adjudicate: take care of automtic game end
6104
6105   if (gameMode == BeginningOfGame) {
6106     if (appData.noChessProgram) {
6107       gameMode = EditGame;
6108       SetGameInfo();
6109     } else {
6110       char buf[MSG_SIZ];
6111       gameMode = MachinePlaysBlack;
6112       StartClocks();
6113       SetGameInfo();
6114       sprintf(buf, "%s vs. %s", gameInfo.white, gameInfo.black);
6115       DisplayTitle(buf);
6116       if (first.sendName) {
6117         sprintf(buf, "name %s\n", gameInfo.white);
6118         SendToProgram(buf, &first);
6119       }
6120       StartClocks();
6121     }
6122     ModeHighlight();
6123   }
6124
6125   /* Relay move to ICS or chess engine */
6126   if (appData.icsActive) {
6127     if (gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack ||
6128         gameMode == IcsExamining) {
6129       if(userOfferedDraw && (signed char)boards[forwardMostMove][EP_STATUS] <= EP_DRAWS) {
6130         SendToICS(ics_prefix); // [HGM] drawclaim: send caim and move on one line for FICS
6131         SendToICS("draw ");
6132         SendMoveToICS(moveType, fromX, fromY, toX, toY);
6133       }
6134       // also send plain move, in case ICS does not understand atomic claims
6135       SendMoveToICS(moveType, fromX, fromY, toX, toY);
6136       ics_user_moved = 1;
6137     }
6138   } else {
6139     if (first.sendTime && (gameMode == BeginningOfGame ||
6140                            gameMode == MachinePlaysWhite ||
6141                            gameMode == MachinePlaysBlack)) {
6142       SendTimeRemaining(&first, gameMode != MachinePlaysBlack);
6143     }
6144     if (gameMode != EditGame && gameMode != PlayFromGameFile) {
6145          // [HGM] book: if program might be playing, let it use book
6146         bookHit = SendMoveToBookUser(forwardMostMove-1, &first, FALSE);
6147         first.maybeThinking = TRUE;
6148     } else SendMoveToProgram(forwardMostMove-1, &first);
6149     if (currentMove == cmailOldMove + 1) {
6150       cmailMoveType[lastLoadGameNumber - 1] = CMAIL_MOVE;
6151     }
6152   }
6153
6154   ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
6155
6156   switch (gameMode) {
6157   case EditGame:
6158     switch (MateTest(boards[currentMove], PosFlags(currentMove)) ) {
6159     case MT_NONE:
6160     case MT_CHECK:
6161       break;
6162     case MT_CHECKMATE:
6163     case MT_STAINMATE:
6164       if (WhiteOnMove(currentMove)) {
6165         GameEnds(BlackWins, "Black mates", GE_PLAYER);
6166       } else {
6167         GameEnds(WhiteWins, "White mates", GE_PLAYER);
6168       }
6169       break;
6170     case MT_STALEMATE:
6171       GameEnds(GameIsDrawn, "Stalemate", GE_PLAYER);
6172       break;
6173     }
6174     break;
6175     
6176   case MachinePlaysBlack:
6177   case MachinePlaysWhite:
6178     /* disable certain menu options while machine is thinking */
6179     SetMachineThinkingEnables();
6180     break;
6181
6182   default:
6183     break;
6184   }
6185
6186   userOfferedDraw = FALSE; // [HGM] drawclaim: after move made, and tested for claimable draw
6187         
6188   if(bookHit) { // [HGM] book: simulate book reply
6189         static char bookMove[MSG_SIZ]; // a bit generous?
6190
6191         programStats.nodes = programStats.depth = programStats.time = 
6192         programStats.score = programStats.got_only_move = 0;
6193         sprintf(programStats.movelist, "%s (xbook)", bookHit);
6194
6195         strcpy(bookMove, "move ");
6196         strcat(bookMove, bookHit);
6197         HandleMachineMove(bookMove, &first);
6198   }
6199   return 1;
6200 }
6201
6202 void
6203 UserMoveEvent(fromX, fromY, toX, toY, promoChar)
6204      int fromX, fromY, toX, toY;
6205      int promoChar;
6206 {
6207     /* [HGM] This routine was added to allow calling of its two logical
6208        parts from other modules in the old way. Before, UserMoveEvent()
6209        automatically called FinishMove() if the move was OK, and returned
6210        otherwise. I separated the two, in order to make it possible to
6211        slip a promotion popup in between. But that it always needs two
6212        calls, to the first part, (now called UserMoveTest() ), and to
6213        FinishMove if the first part succeeded. Calls that do not need
6214        to do anything in between, can call this routine the old way. 
6215     */
6216     ChessMove moveType = UserMoveTest(fromX, fromY, toX, toY, promoChar, FALSE);
6217 if(appData.debugMode) fprintf(debugFP, "moveType 4 = %d, promochar = %x\n", moveType, promoChar);
6218     if(moveType == AmbiguousMove)
6219         DrawPosition(FALSE, boards[currentMove]);
6220     else if(moveType != ImpossibleMove && moveType != Comment)
6221         FinishMove(moveType, fromX, fromY, toX, toY, promoChar);
6222 }
6223
6224 void
6225 Mark(board, flags, kind, rf, ff, rt, ft, closure)
6226      Board board;
6227      int flags;
6228      ChessMove kind;
6229      int rf, ff, rt, ft;
6230      VOIDSTAR closure;
6231 {
6232     typedef char Markers[BOARD_RANKS][BOARD_FILES];
6233     Markers *m = (Markers *) closure;
6234     if(rf == fromY && ff == fromX)
6235         (*m)[rt][ft] = 1 + (board[rt][ft] != EmptySquare
6236                          || kind == WhiteCapturesEnPassant
6237                          || kind == BlackCapturesEnPassant);
6238     else if(flags & F_MANDATORY_CAPTURE && board[rt][ft] != EmptySquare) (*m)[rt][ft] = 3;
6239 }
6240
6241 void
6242 MarkTargetSquares(int clear)
6243 {
6244   int x, y;
6245   if(!appData.markers || !appData.highlightDragging || 
6246      !appData.testLegality || gameMode == EditPosition) return;
6247   if(clear) {
6248     for(x=0; x<BOARD_WIDTH; x++) for(y=0; y<BOARD_HEIGHT; y++) marker[y][x] = 0;
6249   } else {
6250     int capt = 0;
6251     GenLegal(boards[currentMove], PosFlags(currentMove), Mark, (void*) marker);
6252     if(PosFlags(0) & F_MANDATORY_CAPTURE) {
6253       for(x=0; x<BOARD_WIDTH; x++) for(y=0; y<BOARD_HEIGHT; y++) if(marker[y][x]>1) capt++;
6254       if(capt)
6255       for(x=0; x<BOARD_WIDTH; x++) for(y=0; y<BOARD_HEIGHT; y++) if(marker[y][x] == 1) marker[y][x] = 0;
6256     }
6257   }
6258   DrawPosition(TRUE, NULL);
6259 }
6260
6261 void LeftClick(ClickType clickType, int xPix, int yPix)
6262 {
6263     int x, y;
6264     Boolean saveAnimate;
6265     static int second = 0, promotionChoice = 0, dragging = 0;
6266     char promoChoice = NULLCHAR;
6267
6268     if(appData.seekGraph && appData.icsActive && loggedOn &&
6269         (gameMode == BeginningOfGame || gameMode == IcsIdle)) {
6270         SeekGraphClick(clickType, xPix, yPix, 0);
6271         return;
6272     }
6273
6274     if (clickType == Press) ErrorPopDown();
6275     MarkTargetSquares(1);
6276
6277     x = EventToSquare(xPix, BOARD_WIDTH);
6278     y = EventToSquare(yPix, BOARD_HEIGHT);
6279     if (!flipView && y >= 0) {
6280         y = BOARD_HEIGHT - 1 - y;
6281     }
6282     if (flipView && x >= 0) {
6283         x = BOARD_WIDTH - 1 - x;
6284     }
6285
6286     if(promotionChoice) { // we are waiting for a click to indicate promotion piece
6287         if(clickType == Release) return; // ignore upclick of click-click destination
6288         promotionChoice = FALSE; // only one chance: if click not OK it is interpreted as cancel
6289         if(appData.debugMode) fprintf(debugFP, "promotion click, x=%d, y=%d\n", x, y);
6290         if(gameInfo.holdingsWidth && 
6291                 (WhiteOnMove(currentMove) 
6292                         ? x == BOARD_WIDTH-1 && y < gameInfo.holdingsSize && y > 0
6293                         : x == 0 && y >= BOARD_HEIGHT - gameInfo.holdingsSize && y < BOARD_HEIGHT-1) ) {
6294             // click in right holdings, for determining promotion piece
6295             ChessSquare p = boards[currentMove][y][x];
6296             if(appData.debugMode) fprintf(debugFP, "square contains %d\n", (int)p);
6297             if(p != EmptySquare) {
6298                 FinishMove(NormalMove, fromX, fromY, toX, toY, ToLower(PieceToChar(p)));
6299                 fromX = fromY = -1;
6300                 return;
6301             }
6302         }
6303         DrawPosition(FALSE, boards[currentMove]);
6304         return;
6305     }
6306
6307     /* [HGM] holdings: next 5 lines: ignore all clicks between board and holdings */
6308     if(clickType == Press
6309             && ( x == BOARD_LEFT-1 || x == BOARD_RGHT
6310               || x == BOARD_LEFT-2 && y < BOARD_HEIGHT-gameInfo.holdingsSize
6311               || x == BOARD_RGHT+1 && y >= gameInfo.holdingsSize) )
6312         return;
6313
6314     autoQueen = appData.alwaysPromoteToQueen;
6315
6316     if (fromX == -1) {
6317       if(!appData.oneClick || !OnlyMove(&x, &y, FALSE)) {
6318         if (clickType == Press) {
6319             /* First square */
6320             if (OKToStartUserMove(x, y)) {
6321                 fromX = x;
6322                 fromY = y;
6323                 second = 0;
6324                 MarkTargetSquares(0);
6325                 DragPieceBegin(xPix, yPix); dragging = 1;
6326                 if (appData.highlightDragging) {
6327                     SetHighlights(x, y, -1, -1);
6328                 }
6329             }
6330         } else if(dragging) { // [HGM] from-square must have been reset due to game end since last press
6331             DragPieceEnd(xPix, yPix); dragging = 0;
6332             DrawPosition(FALSE, NULL);
6333         }
6334         return;
6335       }
6336     }
6337
6338     /* fromX != -1 */
6339     if (clickType == Press && gameMode != EditPosition) {
6340         ChessSquare fromP;
6341         ChessSquare toP;
6342         int frc;
6343
6344         // ignore off-board to clicks
6345         if(y < 0 || x < 0) return;
6346
6347         /* Check if clicking again on the same color piece */
6348         fromP = boards[currentMove][fromY][fromX];
6349         toP = boards[currentMove][y][x];
6350         frc = gameInfo.variant == VariantFischeRandom || gameInfo.variant == VariantCapaRandom;
6351         if ((WhitePawn <= fromP && fromP <= WhiteKing &&
6352              WhitePawn <= toP && toP <= WhiteKing &&
6353              !(fromP == WhiteKing && toP == WhiteRook && frc) &&
6354              !(fromP == WhiteRook && toP == WhiteKing && frc)) ||
6355             (BlackPawn <= fromP && fromP <= BlackKing && 
6356              BlackPawn <= toP && toP <= BlackKing &&
6357              !(fromP == BlackRook && toP == BlackKing && frc) && // allow also RxK as FRC castling
6358              !(fromP == BlackKing && toP == BlackRook && frc))) {
6359             /* Clicked again on same color piece -- changed his mind */
6360             second = (x == fromX && y == fromY);
6361            if(!second || appData.oneClick && !OnlyMove(&x, &y, TRUE)) {
6362             if (appData.highlightDragging) {
6363                 SetHighlights(x, y, -1, -1);
6364             } else {
6365                 ClearHighlights();
6366             }
6367             if (OKToStartUserMove(x, y)) {
6368                 fromX = x;
6369                 fromY = y; dragging = 1;
6370                 MarkTargetSquares(0);
6371                 DragPieceBegin(xPix, yPix);
6372             }
6373             return;
6374            }
6375         }
6376         // ignore clicks on holdings
6377         if(x < BOARD_LEFT || x >= BOARD_RGHT) return;
6378     }
6379
6380     if (clickType == Release && x == fromX && y == fromY) {
6381         DragPieceEnd(xPix, yPix); dragging = 0;
6382         if (appData.animateDragging) {
6383             /* Undo animation damage if any */
6384             DrawPosition(FALSE, NULL);
6385         }
6386         if (second) {
6387             /* Second up/down in same square; just abort move */
6388             second = 0;
6389             fromX = fromY = -1;
6390             ClearHighlights();
6391             gotPremove = 0;
6392             ClearPremoveHighlights();
6393         } else {
6394             /* First upclick in same square; start click-click mode */
6395             SetHighlights(x, y, -1, -1);
6396         }
6397         return;
6398     }
6399
6400     /* we now have a different from- and (possibly off-board) to-square */
6401     /* Completed move */
6402     toX = x;
6403     toY = y;
6404     saveAnimate = appData.animate;
6405     if (clickType == Press) {
6406         /* Finish clickclick move */
6407         if (appData.animate || appData.highlightLastMove) {
6408             SetHighlights(fromX, fromY, toX, toY);
6409         } else {
6410             ClearHighlights();
6411         }
6412     } else {
6413         /* Finish drag move */
6414         if (appData.highlightLastMove) {
6415             SetHighlights(fromX, fromY, toX, toY);
6416         } else {
6417             ClearHighlights();
6418         }
6419         DragPieceEnd(xPix, yPix); dragging = 0;
6420         /* Don't animate move and drag both */
6421         appData.animate = FALSE;
6422     }
6423
6424     // moves into holding are invalid for now (except in EditPosition, adapting to-square)
6425     if(x >= 0 && x < BOARD_LEFT || x >= BOARD_RGHT) {
6426         ChessSquare piece = boards[currentMove][fromY][fromX];
6427         if(gameMode == EditPosition && piece != EmptySquare &&
6428            fromX >= BOARD_LEFT && fromX < BOARD_RGHT) {
6429             int n;
6430              
6431             if(x == BOARD_LEFT-2 && piece >= BlackPawn) {
6432                 n = PieceToNumber(piece - (int)BlackPawn);
6433                 if(n >= gameInfo.holdingsSize) { n = 0; piece = BlackPawn; }
6434                 boards[currentMove][BOARD_HEIGHT-1 - n][0] = piece;
6435                 boards[currentMove][BOARD_HEIGHT-1 - n][1]++;
6436             } else
6437             if(x == BOARD_RGHT+1 && piece < BlackPawn) {
6438                 n = PieceToNumber(piece);
6439                 if(n >= gameInfo.holdingsSize) { n = 0; piece = WhitePawn; }
6440                 boards[currentMove][n][BOARD_WIDTH-1] = piece;
6441                 boards[currentMove][n][BOARD_WIDTH-2]++;
6442             }
6443             boards[currentMove][fromY][fromX] = EmptySquare;
6444         }
6445         ClearHighlights();
6446         fromX = fromY = -1;
6447         DrawPosition(TRUE, boards[currentMove]);
6448         return;
6449     }
6450
6451     // off-board moves should not be highlighted
6452     if(x < 0 || x < 0) ClearHighlights();
6453
6454     if (HasPromotionChoice(fromX, fromY, toX, toY, &promoChoice)) {
6455         SetHighlights(fromX, fromY, toX, toY);
6456         if(gameInfo.variant == VariantSuper || gameInfo.variant == VariantGreat) {
6457             // [HGM] super: promotion to captured piece selected from holdings
6458             ChessSquare p = boards[currentMove][fromY][fromX], q = boards[currentMove][toY][toX];
6459             promotionChoice = TRUE;
6460             // kludge follows to temporarily execute move on display, without promoting yet
6461             boards[currentMove][fromY][fromX] = EmptySquare; // move Pawn to 8th rank
6462             boards[currentMove][toY][toX] = p;
6463             DrawPosition(FALSE, boards[currentMove]);
6464             boards[currentMove][fromY][fromX] = p; // take back, but display stays
6465             boards[currentMove][toY][toX] = q;
6466             DisplayMessage("Click in holdings to choose piece", "");
6467             return;
6468         }
6469         PromotionPopUp();
6470     } else {
6471         UserMoveEvent(fromX, fromY, toX, toY, promoChoice);
6472         if (!appData.highlightLastMove || gotPremove) ClearHighlights();
6473         if (gotPremove) SetPremoveHighlights(fromX, fromY, toX, toY);
6474         fromX = fromY = -1;
6475     }
6476     appData.animate = saveAnimate;
6477     if (appData.animate || appData.animateDragging) {
6478         /* Undo animation damage if needed */
6479         DrawPosition(FALSE, NULL);
6480     }
6481 }
6482
6483 int RightClick(ClickType action, int x, int y, int *fromX, int *fromY)
6484 {   // front-end-free part taken out of PieceMenuPopup
6485     int whichMenu; int xSqr, ySqr;
6486
6487     if(seekGraphUp) { // [HGM] seekgraph
6488         if(action == Press)   SeekGraphClick(Press, x, y, 2); // 2 indicates right-click: no pop-down on miss
6489         if(action == Release) SeekGraphClick(Release, x, y, 2); // and no challenge on hit
6490         return -2;
6491     }
6492
6493     if((gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack)
6494          && !appData.zippyPlay && appData.bgObserve) { // [HGM] bughouse: show background game
6495         if(!partnerBoardValid) return -2; // suppress display of uninitialized boards
6496         if( appData.dualBoard) return -2; // [HGM] dual: is already displayed
6497         if(action == Press)   {
6498             originalFlip = flipView;
6499             flipView = !flipView; // temporarily flip board to see game from partners perspective
6500             DrawPosition(TRUE, partnerBoard);
6501             DisplayMessage(partnerStatus, "");
6502             partnerUp = TRUE;
6503         } else if(action == Release) {
6504             flipView = originalFlip;
6505             DrawPosition(TRUE, boards[currentMove]);
6506             partnerUp = FALSE;
6507         }
6508         return -2;
6509     }
6510
6511     xSqr = EventToSquare(x, BOARD_WIDTH);
6512     ySqr = EventToSquare(y, BOARD_HEIGHT);
6513     if (action == Release) UnLoadPV(); // [HGM] pv
6514     if (action != Press) return -2; // return code to be ignored
6515     switch (gameMode) {
6516       case IcsExamining:
6517         if(xSqr < BOARD_LEFT || xSqr >= BOARD_RGHT) return -1;\r
6518       case EditPosition:
6519         if (xSqr == BOARD_LEFT-1 || xSqr == BOARD_RGHT) return -1;\r
6520         if (xSqr < 0 || ySqr < 0) return -1;\r
6521         whichMenu = 0; // edit-position menu
6522         break;
6523       case IcsObserving:
6524         if(!appData.icsEngineAnalyze) return -1;
6525       case IcsPlayingWhite:
6526       case IcsPlayingBlack:
6527         if(!appData.zippyPlay) goto noZip;
6528       case AnalyzeMode:
6529       case AnalyzeFile:
6530       case MachinePlaysWhite:
6531       case MachinePlaysBlack:
6532       case TwoMachinesPlay: // [HGM] pv: use for showing PV
6533         if (!appData.dropMenu) {
6534           LoadPV(x, y);
6535           return 2; // flag front-end to grab mouse events
6536         }
6537         if(gameMode == TwoMachinesPlay || gameMode == AnalyzeMode ||
6538            gameMode == AnalyzeFile || gameMode == IcsObserving) return -1;
6539       case EditGame:
6540       noZip:
6541         if (xSqr < 0 || ySqr < 0) return -1;
6542         if (!appData.dropMenu || appData.testLegality &&
6543             gameInfo.variant != VariantBughouse &&
6544             gameInfo.variant != VariantCrazyhouse) return -1;
6545         whichMenu = 1; // drop menu
6546         break;
6547       default:
6548         return -1;
6549     }
6550
6551     if (((*fromX = xSqr) < 0) ||
6552         ((*fromY = ySqr) < 0)) {
6553         *fromX = *fromY = -1;
6554         return -1;
6555     }
6556     if (flipView)
6557       *fromX = BOARD_WIDTH - 1 - *fromX;
6558     else
6559       *fromY = BOARD_HEIGHT - 1 - *fromY;
6560
6561     return whichMenu;
6562 }
6563
6564 void SendProgramStatsToFrontend( ChessProgramState * cps, ChessProgramStats * cpstats )
6565 {
6566 //    char * hint = lastHint;
6567     FrontEndProgramStats stats;
6568
6569     stats.which = cps == &first ? 0 : 1;
6570     stats.depth = cpstats->depth;
6571     stats.nodes = cpstats->nodes;
6572     stats.score = cpstats->score;
6573     stats.time = cpstats->time;
6574     stats.pv = cpstats->movelist;
6575     stats.hint = lastHint;
6576     stats.an_move_index = 0;
6577     stats.an_move_count = 0;
6578
6579     if( gameMode == AnalyzeMode || gameMode == AnalyzeFile ) {
6580         stats.hint = cpstats->move_name;
6581         stats.an_move_index = cpstats->nr_moves - cpstats->moves_left;
6582         stats.an_move_count = cpstats->nr_moves;
6583     }
6584
6585     if(stats.pv && stats.pv[0]) strcpy(lastPV[stats.which], stats.pv); // [HGM] pv: remember last PV of each
6586
6587     SetProgramStats( &stats );
6588 }
6589
6590 void
6591 Count(Board board, int pCnt[], int *nW, int *nB, int *wStale, int *bStale, int *bishopColor)
6592 {       // count all piece types
6593         int p, f, r;
6594         *nB = *nW = *wStale = *bStale = *bishopColor = 0;
6595         for(p=WhitePawn; p<=EmptySquare; p++) pCnt[p] = 0;
6596         for(r=0; r<BOARD_HEIGHT; r++) for(f=BOARD_LEFT; f<BOARD_RGHT; f++) {
6597                 p = board[r][f];
6598                 pCnt[p]++;
6599                 if(p == WhitePawn && r == BOARD_HEIGHT-1) (*wStale)++; else
6600                 if(p == BlackPawn && r == 0) (*bStale)++; // count last-Rank Pawns (XQ) separately
6601                 if(p <= WhiteKing) (*nW)++; else if(p <= BlackKing) (*nB)++;
6602                 if(p == WhiteBishop || p == WhiteFerz || p == WhiteAlfil ||
6603                    p == BlackBishop || p == BlackFerz || p == BlackAlfil   )
6604                         *bishopColor |= 1 << ((f^r)&1); // track square color of color-bound pieces
6605         }
6606 }
6607
6608 int
6609 SufficientDefence(int pCnt[], int side, int nMine, int nHis)
6610 {
6611         int myPawns = pCnt[WhitePawn+side]; // my total Pawn count;
6612         int majorDefense = pCnt[BlackRook-side] + pCnt[BlackCannon-side] + pCnt[BlackKnight-side];
6613                    
6614         nMine -= pCnt[WhiteFerz+side] + pCnt[WhiteAlfil+side]; // discount defenders
6615         if(nMine - myPawns > 2) return FALSE; // no trivial draws with more than 1 major
6616         if(myPawns == 2 && nMine == 3) // KPP
6617             return majorDefense || pCnt[BlackFerz-side] + pCnt[BlackAlfil-side] >= 3;
6618         if(myPawns == 1 && nMine == 2) // KP
6619             return majorDefense || pCnt[BlackFerz-side] + pCnt[BlackAlfil-side]  + pCnt[BlackPawn-side] >= 1;
6620         if(myPawns == 1 && nMine == 3 && pCnt[WhiteKnight+side]) // KHP
6621             return majorDefense || pCnt[BlackFerz-side] + pCnt[BlackAlfil-side]*2 >= 5;
6622         if(myPawns) return FALSE;
6623         if(pCnt[WhiteRook+side])
6624             return pCnt[BlackRook-side] || 
6625                    pCnt[BlackCannon-side] && (pCnt[BlackFerz-side] >= 2 || pCnt[BlackAlfil-side] >= 2) ||
6626                    pCnt[BlackKnight-side] && pCnt[BlackFerz-side] + pCnt[BlackAlfil-side] > 2 ||
6627                    pCnt[BlackFerz-side] + pCnt[BlackAlfil-side] >= 4;
6628         if(pCnt[WhiteCannon+side]) {
6629             if(pCnt[WhiteFerz+side] + myPawns == 0) return TRUE; // Cannon needs platform
6630             return majorDefense || pCnt[BlackAlfil-side] >= 2;
6631         }
6632         if(pCnt[WhiteKnight+side])
6633             return majorDefense || pCnt[BlackFerz-side] >= 2 || pCnt[BlackAlfil-side] + pCnt[BlackPawn-side] >= 1;
6634         return FALSE;
6635 }
6636
6637 int
6638 MatingPotential(int pCnt[], int side, int nMine, int nHis, int stale, int bisColor)
6639 {
6640         VariantClass v = gameInfo.variant;
6641
6642         if(v == VariantShogi || v == VariantCrazyhouse || v == VariantBughouse) return TRUE; // drop games always winnable
6643         if(v == VariantShatranj) return TRUE; // always winnable through baring
6644         if(v == VariantLosers || v == VariantSuicide || v == VariantGiveaway) return TRUE;
6645         if(v == Variant3Check || v == VariantAtomic) return nMine > 1; // can win through checking / exploding King
6646
6647         if(v == VariantXiangqi) {
6648                 int majors = 5*pCnt[BlackKnight-side] + 7*pCnt[BlackCannon-side] + 7*pCnt[BlackRook-side];
6649
6650                 nMine -= pCnt[WhiteFerz+side] + pCnt[WhiteAlfil+side] + stale; // discount defensive pieces and back-rank Pawns
6651                 if(nMine + stale == 1) return (pCnt[BlackFerz-side] > 1 && pCnt[BlackKnight-side] > 0); // bare K can stalemate KHAA (!)
6652                 if(nMine > 2) return TRUE; // if we don't have P, H or R, we must have CC
6653                 if(nMine == 2 && pCnt[WhiteCannon+side] == 0) return TRUE; // We have at least one P, H or R
6654                 // if we get here, we must have KC... or KP..., possibly with additional A, E or last-rank P
6655                 if(stale) // we have at least one last-rank P plus perhaps C
6656                     return majors // KPKX
6657                         || pCnt[BlackFerz-side] && pCnt[BlackFerz-side] + pCnt[WhiteCannon+side] + stale > 2; // KPKAA, KPPKA and KCPKA
6658                 else // KCA*E*
6659                     return pCnt[WhiteFerz+side] // KCAK
6660                         || pCnt[WhiteAlfil+side] && pCnt[BlackRook-side] + pCnt[BlackCannon-side] + pCnt[BlackFerz-side] // KCEKA, KCEKX (X!=H)
6661                         || majors + (12*pCnt[BlackFerz-side] | 6*pCnt[BlackAlfil-side]) > 16; // KCKAA, KCKAX, KCKEEX, KCKEXX (XX!=HH), KCKXXX
6662                 // TO DO: cases wih an unpromoted f-Pawn acting as platform for an opponent Cannon
6663
6664         } else if(pCnt[WhiteKing] == 1 && pCnt[BlackKing] == 1) { // other variants with orthodox Kings
6665                 int nBishops = pCnt[WhiteBishop+side] + pCnt[WhiteFerz+side];
6666                 
6667                 if(nMine == 1) return FALSE; // bare King
6668                 if(nBishops && bisColor == 3) return TRUE; // There must be a second B/A/F, which can either block (his) or attack (mine) the escape square
6669                 nMine += (nBishops > 0) - nBishops; // By now all Bishops (and Ferz) on like-colored squares, so count as one
6670                 if(nMine > 2 && nMine != pCnt[WhiteAlfil+side] + 1) return TRUE; // At least two pieces, not all Alfils
6671                 // by now we have King + 1 piece (or multiple Bishops on the same color)
6672                 if(pCnt[WhiteKnight+side])
6673                         return (pCnt[BlackKnight-side] + pCnt[BlackBishop-side] + pCnt[BlackMan-side] + 
6674                                 pCnt[BlackWazir-side] + pCnt[BlackSilver-side] + bisColor // KNKN, KNKB, KNKF, KNKE, KNKW, KNKM, KNKS
6675                              || nHis > 3); // be sure to cover suffocation mates in corner (e.g. KNKQCA)
6676                 if(nBishops)
6677                         return (pCnt[BlackKnight-side]); // KBKN, KFKN
6678                 if(pCnt[WhiteAlfil+side])
6679                         return (nHis > 2); // Alfils can in general not reach a corner square, but there might be edge (suffocation) mates
6680                 if(pCnt[WhiteWazir+side])
6681                         return (pCnt[BlackKnight-side] + pCnt[BlackWazir-side] + pCnt[BlackAlfil-side]); // KWKN, KWKW, KWKE
6682         }
6683
6684         return TRUE;
6685 }
6686
6687 int
6688 Adjudicate(ChessProgramState *cps)
6689 {       // [HGM] some adjudications useful with buggy engines
6690         // [HGM] adjudicate: made into separate routine, which now can be called after every move
6691         //       In any case it determnes if the game is a claimable draw (filling in EP_STATUS).
6692         //       Actually ending the game is now based on the additional internal condition canAdjudicate.
6693         //       Only when the game is ended, and the opponent is a computer, this opponent gets the move relayed.
6694         int k, count = 0; static int bare = 1;
6695         ChessProgramState *engineOpponent = (gameMode == TwoMachinesPlay ? cps->other : (cps ? NULL : &first));
6696         Boolean canAdjudicate = !appData.icsActive;
6697
6698         // most tests only when we understand the game, i.e. legality-checking on, and (for the time being) no piece drops
6699         if(gameInfo.holdingsSize == 0 || gameInfo.variant == VariantSuper || gameInfo.variant == VariantGreat) {
6700             if( appData.testLegality )
6701             {   /* [HGM] Some more adjudications for obstinate engines */
6702                 int nrW, nrB, bishopColor, staleW, staleB, nr[EmptySquare+1], i;
6703                 static int moveCount = 6;
6704                 ChessMove result;
6705                 char *reason = NULL;
6706
6707                 /* Count what is on board. */
6708                 Count(boards[forwardMostMove], nr, &nrW, &nrB, &staleW, &staleB, &bishopColor);
6709
6710                 /* Some material-based adjudications that have to be made before stalemate test */
6711                 if(gameInfo.variant == VariantAtomic && nr[WhiteKing] + nr[BlackKing] < 2) {
6712                     // [HGM] atomic: stm must have lost his King on previous move, as destroying own K is illegal
6713                      boards[forwardMostMove][EP_STATUS] = EP_CHECKMATE; // make claimable as if stm is checkmated
6714                      if(canAdjudicate && appData.checkMates) {
6715                          if(engineOpponent)
6716                            SendMoveToProgram(forwardMostMove-1, engineOpponent); // make sure opponent gets move
6717                          ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
6718                          GameEnds( WhiteOnMove(forwardMostMove) ? BlackWins : WhiteWins, 
6719                                                         "Xboard adjudication: King destroyed", GE_XBOARD );
6720                          return 1;
6721                      }
6722                 }
6723
6724                 /* Bare King in Shatranj (loses) or Losers (wins) */
6725                 if( nrW == 1 || nrB == 1) {
6726                   if( gameInfo.variant == VariantLosers) { // [HGM] losers: bare King wins (stm must have it first)
6727                      boards[forwardMostMove][EP_STATUS] = EP_WINS;  // mark as win, so it becomes claimable
6728                      if(canAdjudicate && appData.checkMates) {
6729                          if(engineOpponent)
6730                            SendMoveToProgram(forwardMostMove-1, engineOpponent); // make sure opponent gets to see move
6731                          ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
6732                          GameEnds( WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins, 
6733                                                         "Xboard adjudication: Bare king", GE_XBOARD );
6734                          return 1;
6735                      }
6736                   } else
6737                   if( gameInfo.variant == VariantShatranj && --bare < 0)
6738                   {    /* bare King */
6739                         boards[forwardMostMove][EP_STATUS] = EP_WINS; // make claimable as win for stm
6740                         if(canAdjudicate && appData.checkMates) {
6741                             /* but only adjudicate if adjudication enabled */
6742                             if(engineOpponent)
6743                               SendMoveToProgram(forwardMostMove-1, engineOpponent); // make sure opponent gets move
6744                             ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
6745                             GameEnds( nrW > 1 ? WhiteWins : nrB > 1 ? BlackWins : GameIsDrawn, 
6746                                                         "Xboard adjudication: Bare king", GE_XBOARD );
6747                             return 1;
6748                         }
6749                   }
6750                 } else bare = 1;
6751
6752
6753             // don't wait for engine to announce game end if we can judge ourselves
6754             switch (MateTest(boards[forwardMostMove], PosFlags(forwardMostMove)) ) {
6755               case MT_CHECK:
6756                 if(gameInfo.variant == Variant3Check) { // [HGM] 3check: when in check, test if 3rd time
6757                     int i, checkCnt = 0;    // (should really be done by making nr of checks part of game state)
6758                     for(i=forwardMostMove-2; i>=backwardMostMove; i-=2) {
6759                         if(MateTest(boards[i], PosFlags(i)) == MT_CHECK)
6760                             checkCnt++;
6761                         if(checkCnt >= 2) {
6762                             reason = "Xboard adjudication: 3rd check";
6763                             boards[forwardMostMove][EP_STATUS] = EP_CHECKMATE;
6764                             break;
6765                         }
6766                     }
6767                 }
6768               case MT_NONE:
6769               default:
6770                 break;
6771               case MT_STALEMATE:
6772               case MT_STAINMATE:
6773                 reason = "Xboard adjudication: Stalemate";
6774                 if((signed char)boards[forwardMostMove][EP_STATUS] != EP_CHECKMATE) { // [HGM] don't touch win through baring or K-capt
6775                     boards[forwardMostMove][EP_STATUS] = EP_STALEMATE;   // default result for stalemate is draw
6776                     if(gameInfo.variant == VariantLosers  || gameInfo.variant == VariantGiveaway) // [HGM] losers:
6777                         boards[forwardMostMove][EP_STATUS] = EP_WINS;    // in these variants stalemated is always a win
6778                     else if(gameInfo.variant == VariantSuicide) // in suicide it depends
6779                         boards[forwardMostMove][EP_STATUS] = nrW == nrB ? EP_STALEMATE :
6780                                                    ((nrW < nrB) != WhiteOnMove(forwardMostMove) ?
6781                                                                         EP_CHECKMATE : EP_WINS);
6782                     else if(gameInfo.variant == VariantShatranj || gameInfo.variant == VariantXiangqi)
6783                         boards[forwardMostMove][EP_STATUS] = EP_CHECKMATE; // and in these variants being stalemated loses
6784                 }
6785                 break;
6786               case MT_CHECKMATE:
6787                 reason = "Xboard adjudication: Checkmate";
6788                 boards[forwardMostMove][EP_STATUS] = (gameInfo.variant == VariantLosers ? EP_WINS : EP_CHECKMATE);
6789                 break;
6790             }
6791
6792                 switch(i = (signed char)boards[forwardMostMove][EP_STATUS]) {
6793                     case EP_STALEMATE:
6794                         result = GameIsDrawn; break;
6795                     case EP_CHECKMATE:
6796                         result = WhiteOnMove(forwardMostMove) ? BlackWins : WhiteWins; break;
6797                     case EP_WINS:
6798                         result = WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins; break;
6799                     default:
6800                         result = (ChessMove) 0;
6801                 }
6802                 if(canAdjudicate && appData.checkMates && result) { // [HGM] mates: adjudicate finished games if requested
6803                     if(engineOpponent)
6804                       SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
6805                     ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
6806                     GameEnds( result, reason, GE_XBOARD );
6807                     return 1;
6808                 }
6809
6810                 /* Next absolutely insufficient mating material. */
6811                 if(!MatingPotential(nr, WhitePawn, nrW, nrB, staleW, bishopColor) &&
6812                    !MatingPotential(nr, BlackPawn, nrB, nrW, staleB, bishopColor))
6813                 {    /* includes KBK, KNK, KK of KBKB with like Bishops */
6814
6815                      /* always flag draws, for judging claims */
6816                      boards[forwardMostMove][EP_STATUS] = EP_INSUF_DRAW;
6817
6818                      if(canAdjudicate && appData.materialDraws) {
6819                          /* but only adjudicate them if adjudication enabled */
6820                          if(engineOpponent) {
6821                            SendToProgram("force\n", engineOpponent); // suppress reply
6822                            SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see last move */
6823                          }
6824                          ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
6825                          GameEnds( GameIsDrawn, "Xboard adjudication: Insufficient mating material", GE_XBOARD );
6826                          return 1;
6827                      }
6828                 }
6829
6830                 /* Then some trivial draws (only adjudicate, cannot be claimed) */
6831                 if(gameInfo.variant == VariantXiangqi ?
6832                        SufficientDefence(nr, WhitePawn, nrW, nrB) && SufficientDefence(nr, BlackPawn, nrB, nrW)
6833                  : nrW + nrB == 4 && 
6834                    (   nr[WhiteRook] == 1 && nr[BlackRook] == 1 /* KRKR */
6835                    || nr[WhiteQueen] && nr[BlackQueen]==1     /* KQKQ */
6836                    || nr[WhiteKnight]==2 || nr[BlackKnight]==2     /* KNNK */
6837                    || nr[WhiteKnight]+nr[WhiteBishop] == 1 && nr[BlackKnight]+nr[BlackBishop] == 1 /* KBKN, KBKB, KNKN */
6838                    ) ) {
6839                      if(--moveCount < 0 && appData.trivialDraws && canAdjudicate)
6840                      {    /* if the first 3 moves do not show a tactical win, declare draw */
6841                           if(engineOpponent) {
6842                             SendToProgram("force\n", engineOpponent); // suppress reply
6843                             SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
6844                           }
6845                           ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
6846                           GameEnds( GameIsDrawn, "Xboard adjudication: Trivial draw", GE_XBOARD );
6847                           return 1;
6848                      }
6849                 } else moveCount = 6;
6850             }
6851         }
6852           
6853         if (appData.debugMode) { int i;
6854             fprintf(debugFP, "repeat test fmm=%d bmm=%d ep=%d, reps=%d\n",
6855                     forwardMostMove, backwardMostMove, boards[backwardMostMove][EP_STATUS],
6856                     appData.drawRepeats);
6857             for( i=forwardMostMove; i>=backwardMostMove; i-- )
6858               fprintf(debugFP, "%d ep=%d\n", i, (signed char)boards[i][EP_STATUS]);
6859             
6860         }
6861
6862         // Repetition draws and 50-move rule can be applied independently of legality testing
6863
6864                 /* Check for rep-draws */
6865                 count = 0;
6866                 for(k = forwardMostMove-2;
6867                     k>=backwardMostMove && k>=forwardMostMove-100 &&
6868                         (signed char)boards[k][EP_STATUS] < EP_UNKNOWN &&
6869                         (signed char)boards[k+2][EP_STATUS] <= EP_NONE && (signed char)boards[k+1][EP_STATUS] <= EP_NONE;
6870                     k-=2)
6871                 {   int rights=0;
6872                     if(CompareBoards(boards[k], boards[forwardMostMove])) {
6873                         /* compare castling rights */
6874                         if( boards[forwardMostMove][CASTLING][2] != boards[k][CASTLING][2] &&
6875                              (boards[k][CASTLING][0] != NoRights || boards[k][CASTLING][1] != NoRights) )
6876                                 rights++; /* King lost rights, while rook still had them */
6877                         if( boards[forwardMostMove][CASTLING][2] != NoRights ) { /* king has rights */
6878                             if( boards[forwardMostMove][CASTLING][0] != boards[k][CASTLING][0] ||
6879                                 boards[forwardMostMove][CASTLING][1] != boards[k][CASTLING][1] )
6880                                    rights++; /* but at least one rook lost them */
6881                         }
6882                         if( boards[forwardMostMove][CASTLING][5] != boards[k][CASTLING][5] &&
6883                              (boards[k][CASTLING][3] != NoRights || boards[k][CASTLING][4] != NoRights) )
6884                                 rights++; 
6885                         if( boards[forwardMostMove][CASTLING][5] != NoRights ) {
6886                             if( boards[forwardMostMove][CASTLING][3] != boards[k][CASTLING][3] ||
6887                                 boards[forwardMostMove][CASTLING][4] != boards[k][CASTLING][4] )
6888                                    rights++;
6889                         }
6890                         if( rights == 0 && ++count > appData.drawRepeats-2 && canAdjudicate
6891                             && appData.drawRepeats > 1) {
6892                              /* adjudicate after user-specified nr of repeats */
6893                              int result = GameIsDrawn;
6894                              char *details = "XBoard adjudication: repetition draw";
6895                              if(gameInfo.variant == VariantXiangqi && appData.testLegality) { 
6896                                 // [HGM] xiangqi: check for forbidden perpetuals
6897                                 int m, ourPerpetual = 1, hisPerpetual = 1;
6898                                 for(m=forwardMostMove; m>k; m-=2) {
6899                                     if(MateTest(boards[m], PosFlags(m)) != MT_CHECK)
6900                                         ourPerpetual = 0; // the current mover did not always check
6901                                     if(MateTest(boards[m-1], PosFlags(m-1)) != MT_CHECK)
6902                                         hisPerpetual = 0; // the opponent did not always check
6903                                 }
6904                                 if(appData.debugMode) fprintf(debugFP, "XQ perpetual test, our=%d, his=%d\n",
6905                                                                         ourPerpetual, hisPerpetual);
6906                                 if(ourPerpetual && !hisPerpetual) { // we are actively checking him: forfeit
6907                                     result = WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins;
6908                                     details = "Xboard adjudication: perpetual checking";
6909                                 } else
6910                                 if(hisPerpetual && !ourPerpetual) { // he is checking us, but did not repeat yet
6911                                     break; // (or we would have caught him before). Abort repetition-checking loop.
6912                                 } else
6913                                 // Now check for perpetual chases
6914                                 if(!ourPerpetual && !hisPerpetual) { // no perpetual check, test for chase
6915                                     hisPerpetual = PerpetualChase(k, forwardMostMove);
6916                                     ourPerpetual = PerpetualChase(k+1, forwardMostMove);
6917                                     if(ourPerpetual && !hisPerpetual) { // we are actively chasing him: forfeit
6918                                         result = WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins;
6919                                         details = "Xboard adjudication: perpetual chasing";
6920                                     } else
6921                                     if(hisPerpetual && !ourPerpetual)   // he is chasing us, but did not repeat yet
6922                                         break; // Abort repetition-checking loop.
6923                                 }
6924                                 // if neither of us is checking or chasing all the time, or both are, it is draw
6925                              }
6926                              if(engineOpponent) {
6927                                SendToProgram("force\n", engineOpponent); // suppress reply
6928                                SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
6929                              }
6930                              ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
6931                              GameEnds( result, details, GE_XBOARD );
6932                              return 1;
6933                         }
6934                         if( rights == 0 && count > 1 ) /* occurred 2 or more times before */
6935                              boards[forwardMostMove][EP_STATUS] = EP_REP_DRAW;
6936                     }
6937                 }
6938
6939                 /* Now we test for 50-move draws. Determine ply count */
6940                 count = forwardMostMove;
6941                 /* look for last irreversble move */
6942                 while( (signed char)boards[count][EP_STATUS] <= EP_NONE && count > backwardMostMove )
6943                     count--;
6944                 /* if we hit starting position, add initial plies */
6945                 if( count == backwardMostMove )
6946                     count -= initialRulePlies;
6947                 count = forwardMostMove - count; 
6948                 if(gameInfo.variant == VariantXiangqi && ( count >= 100 || count >= 2*appData.ruleMoves ) ) {
6949                         // adjust reversible move counter for checks in Xiangqi
6950                         int i = forwardMostMove - count, inCheck = 0, lastCheck;
6951                         if(i < backwardMostMove) i = backwardMostMove;
6952                         while(i <= forwardMostMove) {
6953                                 lastCheck = inCheck; // check evasion does not count
6954                                 inCheck = (MateTest(boards[i], PosFlags(i)) == MT_CHECK);
6955                                 if(inCheck || lastCheck) count--; // check does not count
6956                                 i++;
6957                         }
6958                 }
6959                 if( count >= 100)
6960                          boards[forwardMostMove][EP_STATUS] = EP_RULE_DRAW;
6961                          /* this is used to judge if draw claims are legal */
6962                 if(canAdjudicate && appData.ruleMoves > 0 && count >= 2*appData.ruleMoves) {
6963                          if(engineOpponent) {
6964                            SendToProgram("force\n", engineOpponent); // suppress reply
6965                            SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
6966                          }
6967                          ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
6968                          GameEnds( GameIsDrawn, "Xboard adjudication: 50-move rule", GE_XBOARD );
6969                          return 1;
6970                 }
6971
6972                 /* if draw offer is pending, treat it as a draw claim
6973                  * when draw condition present, to allow engines a way to
6974                  * claim draws before making their move to avoid a race
6975                  * condition occurring after their move
6976                  */
6977                 if((gameMode == TwoMachinesPlay ? second.offeredDraw : userOfferedDraw) || first.offeredDraw ) {
6978                          char *p = NULL;
6979                          if((signed char)boards[forwardMostMove][EP_STATUS] == EP_RULE_DRAW)
6980                              p = "Draw claim: 50-move rule";
6981                          if((signed char)boards[forwardMostMove][EP_STATUS] == EP_REP_DRAW)
6982                              p = "Draw claim: 3-fold repetition";
6983                          if((signed char)boards[forwardMostMove][EP_STATUS] == EP_INSUF_DRAW)
6984                              p = "Draw claim: insufficient mating material";
6985                          if( p != NULL && canAdjudicate) {
6986                              if(engineOpponent) {
6987                                SendToProgram("force\n", engineOpponent); // suppress reply
6988                                SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
6989                              }
6990                              ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
6991                              GameEnds( GameIsDrawn, p, GE_XBOARD );
6992                              return 1;
6993                          }
6994                 }
6995
6996                 if( canAdjudicate && appData.adjudicateDrawMoves > 0 && forwardMostMove > (2*appData.adjudicateDrawMoves) ) {
6997                     if(engineOpponent) {
6998                       SendToProgram("force\n", engineOpponent); // suppress reply
6999                       SendMoveToProgram(forwardMostMove-1, engineOpponent); /* make sure opponent gets to see move */
7000                     }
7001                     ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
7002                     GameEnds( GameIsDrawn, "Xboard adjudication: long game", GE_XBOARD );
7003                     return 1;
7004                 }
7005         return 0;
7006 }
7007
7008 char *SendMoveToBookUser(int moveNr, ChessProgramState *cps, int initial)
7009 {   // [HGM] book: this routine intercepts moves to simulate book replies
7010     char *bookHit = NULL;
7011
7012     //first determine if the incoming move brings opponent into his book
7013     if(appData.usePolyglotBook && (cps == &first ? !appData.firstHasOwnBookUCI : !appData.secondHasOwnBookUCI))
7014         bookHit = ProbeBook(moveNr+1, appData.polyglotBook); // returns move
7015     if(appData.debugMode) fprintf(debugFP, "book hit = %s\n", bookHit ? bookHit : "(NULL)");
7016     if(bookHit != NULL && !cps->bookSuspend) {
7017         // make sure opponent is not going to reply after receiving move to book position
7018         SendToProgram("force\n", cps);
7019         cps->bookSuspend = TRUE; // flag indicating it has to be restarted
7020     }
7021     if(!initial) SendMoveToProgram(moveNr, cps); // with hit on initial position there is no move
7022     // now arrange restart after book miss
7023     if(bookHit) {
7024         // after a book hit we never send 'go', and the code after the call to this routine
7025         // has '&& !bookHit' added to suppress potential sending there (based on 'firstMove').
7026         char buf[MSG_SIZ];
7027         sprintf(buf, "%s%s\n", (cps->useUsermove ? "usermove " : ""), bookHit); // force book move into program supposed to play it
7028         SendToProgram(buf, cps);
7029         if(!initial) firstMove = FALSE; // normally we would clear the firstMove condition after return & sending 'go'
7030     } else if(initial) { // 'go' was needed irrespective of firstMove, and it has to be done in this routine
7031         SendToProgram("go\n", cps);
7032         cps->bookSuspend = FALSE; // after a 'go' we are never suspended
7033     } else { // 'go' might be sent based on 'firstMove' after this routine returns
7034         if(cps->bookSuspend && !firstMove) // 'go' needed, and it will not be done after we return
7035             SendToProgram("go\n", cps); 
7036         cps->bookSuspend = FALSE; // anyhow, we will not be suspended after a miss
7037     }
7038     return bookHit; // notify caller of hit, so it can take action to send move to opponent
7039 }
7040
7041 char *savedMessage;
7042 ChessProgramState *savedState;
7043 void DeferredBookMove(void)
7044 {
7045         if(savedState->lastPing != savedState->lastPong)
7046                     ScheduleDelayedEvent(DeferredBookMove, 10);
7047         else
7048         HandleMachineMove(savedMessage, savedState);
7049 }
7050
7051 void
7052 HandleMachineMove(message, cps)
7053      char *message;
7054      ChessProgramState *cps;
7055 {
7056     char machineMove[MSG_SIZ], buf1[MSG_SIZ*10], buf2[MSG_SIZ];
7057     char realname[MSG_SIZ];
7058     int fromX, fromY, toX, toY;
7059     ChessMove moveType;
7060     char promoChar;
7061     char *p;
7062     int machineWhite;
7063     char *bookHit;
7064
7065     cps->userError = 0;
7066
7067 FakeBookMove: // [HGM] book: we jump here to simulate machine moves after book hit
7068     /*
7069      * Kludge to ignore BEL characters
7070      */
7071     while (*message == '\007') message++;
7072
7073     /*
7074      * [HGM] engine debug message: ignore lines starting with '#' character
7075      */
7076     if(cps->debug && *message == '#') return;
7077
7078     /*
7079      * Look for book output
7080      */
7081     if (cps == &first && bookRequested) {
7082         if (message[0] == '\t' || message[0] == ' ') {
7083             /* Part of the book output is here; append it */
7084             strcat(bookOutput, message);
7085             strcat(bookOutput, "  \n");
7086             return;
7087         } else if (bookOutput[0] != NULLCHAR) {
7088             /* All of book output has arrived; display it */
7089             char *p = bookOutput;
7090             while (*p != NULLCHAR) {
7091                 if (*p == '\t') *p = ' ';
7092                 p++;
7093             }
7094             DisplayInformation(bookOutput);
7095             bookRequested = FALSE;
7096             /* Fall through to parse the current output */
7097         }
7098     }
7099
7100     /*
7101      * Look for machine move.
7102      */
7103     if ((sscanf(message, "%s %s %s", buf1, buf2, machineMove) == 3 && strcmp(buf2, "...") == 0) ||
7104         (sscanf(message, "%s %s", buf1, machineMove) == 2 && strcmp(buf1, "move") == 0)) 
7105     {
7106         /* This method is only useful on engines that support ping */
7107         if (cps->lastPing != cps->lastPong) {
7108           if (gameMode == BeginningOfGame) {
7109             /* Extra move from before last new; ignore */
7110             if (appData.debugMode) {
7111                 fprintf(debugFP, "Ignoring extra move from %s\n", cps->which);
7112             }
7113           } else {
7114             if (appData.debugMode) {
7115                 fprintf(debugFP, "Undoing extra move from %s, gameMode %d\n",
7116                         cps->which, gameMode);
7117             }
7118
7119             SendToProgram("undo\n", cps);
7120           }
7121           return;
7122         }
7123
7124         switch (gameMode) {
7125           case BeginningOfGame:
7126             /* Extra move from before last reset; ignore */
7127             if (appData.debugMode) {
7128                 fprintf(debugFP, "Ignoring extra move from %s\n", cps->which);
7129             }
7130             return;
7131
7132           case EndOfGame:
7133           case IcsIdle:
7134           default:
7135             /* Extra move after we tried to stop.  The mode test is
7136                not a reliable way of detecting this problem, but it's
7137                the best we can do on engines that don't support ping.
7138             */
7139             if (appData.debugMode) {
7140                 fprintf(debugFP, "Undoing extra move from %s, gameMode %d\n",
7141                         cps->which, gameMode);
7142             }
7143             SendToProgram("undo\n", cps);
7144             return;
7145
7146           case MachinePlaysWhite:
7147           case IcsPlayingWhite:
7148             machineWhite = TRUE;
7149             break;
7150
7151           case MachinePlaysBlack:
7152           case IcsPlayingBlack:
7153             machineWhite = FALSE;
7154             break;
7155
7156           case TwoMachinesPlay:
7157             machineWhite = (cps->twoMachinesColor[0] == 'w');
7158             break;
7159         }
7160         if (WhiteOnMove(forwardMostMove) != machineWhite) {
7161             if (appData.debugMode) {
7162                 fprintf(debugFP,
7163                         "Ignoring move out of turn by %s, gameMode %d"
7164                         ", forwardMost %d\n",
7165                         cps->which, gameMode, forwardMostMove);
7166             }
7167             return;
7168         }
7169
7170     if (appData.debugMode) { int f = forwardMostMove;
7171         fprintf(debugFP, "machine move %d, castling = %d %d %d %d %d %d\n", f,
7172                 boards[f][CASTLING][0],boards[f][CASTLING][1],boards[f][CASTLING][2],
7173                 boards[f][CASTLING][3],boards[f][CASTLING][4],boards[f][CASTLING][5]);
7174     }
7175         if(cps->alphaRank) AlphaRank(machineMove, 4);
7176         if (!ParseOneMove(machineMove, forwardMostMove, &moveType,
7177                               &fromX, &fromY, &toX, &toY, &promoChar)) {
7178             /* Machine move could not be parsed; ignore it. */
7179             sprintf(buf1, _("Illegal move \"%s\" from %s machine"),
7180                     machineMove, cps->which);
7181             DisplayError(buf1, 0);
7182             sprintf(buf1, "Xboard: Forfeit due to invalid move: %s (%c%c%c%c) res=%d",
7183                     machineMove, fromX+AAA, fromY+ONE, toX+AAA, toY+ONE, moveType);
7184             if (gameMode == TwoMachinesPlay) {
7185               GameEnds(machineWhite ? BlackWins : WhiteWins,
7186                        buf1, GE_XBOARD);
7187             }
7188             return;
7189         }
7190
7191         /* [HGM] Apparently legal, but so far only tested with EP_UNKOWN */
7192         /* So we have to redo legality test with true e.p. status here,  */
7193         /* to make sure an illegal e.p. capture does not slip through,   */
7194         /* to cause a forfeit on a justified illegal-move complaint      */
7195         /* of the opponent.                                              */
7196         if( gameMode==TwoMachinesPlay && appData.testLegality
7197             && fromY != DROP_RANK /* [HGM] temporary; should still add legality test for drops */
7198                                                               ) {
7199            ChessMove moveType;
7200            moveType = LegalityTest(boards[forwardMostMove], PosFlags(forwardMostMove),
7201                              fromY, fromX, toY, toX, promoChar);
7202             if (appData.debugMode) {
7203                 int i;
7204                 for(i=0; i< nrCastlingRights; i++) fprintf(debugFP, "(%d,%d) ",
7205                     boards[forwardMostMove][CASTLING][i], castlingRank[i]);
7206                 fprintf(debugFP, "castling rights\n");
7207             }
7208             if(moveType == IllegalMove) {
7209                 sprintf(buf1, "Xboard: Forfeit due to illegal move: %s (%c%c%c%c)%c",
7210                         machineMove, fromX+AAA, fromY+ONE, toX+AAA, toY+ONE, 0);
7211                 GameEnds(machineWhite ? BlackWins : WhiteWins,
7212                            buf1, GE_XBOARD);
7213                 return;
7214            } else if(gameInfo.variant != VariantFischeRandom && gameInfo.variant != VariantCapaRandom)
7215            /* [HGM] Kludge to handle engines that send FRC-style castling
7216               when they shouldn't (like TSCP-Gothic) */
7217            switch(moveType) {
7218              case WhiteASideCastleFR:
7219              case BlackASideCastleFR:
7220                toX+=2;
7221                currentMoveString[2]++;
7222                break;
7223              case WhiteHSideCastleFR:
7224              case BlackHSideCastleFR:
7225                toX--;
7226                currentMoveString[2]--;
7227                break;
7228              default: ; // nothing to do, but suppresses warning of pedantic compilers
7229            }
7230         }
7231         hintRequested = FALSE;
7232         lastHint[0] = NULLCHAR;
7233         bookRequested = FALSE;
7234         /* Program may be pondering now */
7235         cps->maybeThinking = TRUE;
7236         if (cps->sendTime == 2) cps->sendTime = 1;
7237         if (cps->offeredDraw) cps->offeredDraw--;
7238
7239         /* currentMoveString is set as a side-effect of ParseOneMove */
7240         strcpy(machineMove, currentMoveString);
7241         strcat(machineMove, "\n");
7242         strcpy(moveList[forwardMostMove], machineMove);
7243
7244         /* [AS] Save move info*/
7245         pvInfoList[ forwardMostMove ].score = programStats.score;
7246         pvInfoList[ forwardMostMove ].depth = programStats.depth;
7247         pvInfoList[ forwardMostMove ].time =  programStats.time; // [HGM] PGNtime: take time from engine stats
7248
7249         MakeMove(fromX, fromY, toX, toY, promoChar);/*updates forwardMostMove*/
7250
7251         /* [AS] Adjudicate game if needed (note: remember that forwardMostMove now points past the last move) */
7252         if( gameMode == TwoMachinesPlay && adjudicateLossThreshold != 0 && forwardMostMove >= adjudicateLossPlies ) {
7253             int count = 0;
7254
7255             while( count < adjudicateLossPlies ) {
7256                 int score = pvInfoList[ forwardMostMove - count - 1 ].score;
7257
7258                 if( count & 1 ) {
7259                     score = -score; /* Flip score for winning side */
7260                 }
7261
7262                 if( score > adjudicateLossThreshold ) {
7263                     break;
7264                 }
7265
7266                 count++;
7267             }
7268
7269             if( count >= adjudicateLossPlies ) {
7270                 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
7271
7272                 GameEnds( WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins, 
7273                     "Xboard adjudication", 
7274                     GE_XBOARD );
7275
7276                 return;
7277             }
7278         }
7279
7280         if(Adjudicate(cps)) return; // [HGM] adjudicate: for all automatic game ends
7281
7282 #if ZIPPY
7283         if ((gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack) &&
7284             first.initDone) {
7285           if(cps->offeredDraw && (signed char)boards[forwardMostMove][EP_STATUS] <= EP_DRAWS) {
7286                 SendToICS(ics_prefix); // [HGM] drawclaim: send caim and move on one line for FICS
7287                 SendToICS("draw ");
7288                 SendMoveToICS(moveType, fromX, fromY, toX, toY);
7289           }
7290           SendMoveToICS(moveType, fromX, fromY, toX, toY);
7291           ics_user_moved = 1;
7292           if(appData.autoKibitz && !appData.icsEngineAnalyze ) { /* [HGM] kibitz: send most-recent PV info to ICS */
7293                 char buf[3*MSG_SIZ];
7294
7295                 sprintf(buf, "kibitz !!! %+.2f/%d (%.2f sec, %u nodes, %.0f knps) PV=%s\n",
7296                         programStats.score / 100.,
7297                         programStats.depth,
7298                         programStats.time / 100.,
7299                         (unsigned int)programStats.nodes,
7300                         (unsigned int)programStats.nodes / (10*abs(programStats.time) + 1.),
7301                         programStats.movelist);
7302                 SendToICS(buf);
7303 if(appData.debugMode) fprintf(debugFP, "nodes = %d, %lld\n", (int) programStats.nodes, programStats.nodes);
7304           }
7305         }
7306 #endif
7307
7308         /* [AS] Clear stats for next move */
7309         ClearProgramStats();
7310         thinkOutput[0] = NULLCHAR;
7311         hiddenThinkOutputState = 0;
7312
7313         bookHit = NULL;
7314         if (gameMode == TwoMachinesPlay) {
7315             /* [HGM] relaying draw offers moved to after reception of move */
7316             /* and interpreting offer as claim if it brings draw condition */
7317             if (cps->offeredDraw == 1 && cps->other->sendDrawOffers) {
7318                 SendToProgram("draw\n", cps->other);
7319             }
7320             if (cps->other->sendTime) {
7321                 SendTimeRemaining(cps->other,
7322                                   cps->other->twoMachinesColor[0] == 'w');
7323             }
7324             bookHit = SendMoveToBookUser(forwardMostMove-1, cps->other, FALSE);
7325             if (firstMove && !bookHit) {
7326                 firstMove = FALSE;
7327                 if (cps->other->useColors) {
7328                   SendToProgram(cps->other->twoMachinesColor, cps->other);
7329                 }
7330                 SendToProgram("go\n", cps->other);
7331             }
7332             cps->other->maybeThinking = TRUE;
7333         }
7334
7335         ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
7336         
7337         if (!pausing && appData.ringBellAfterMoves) {
7338             RingBell();
7339         }
7340
7341         /* 
7342          * Reenable menu items that were disabled while
7343          * machine was thinking
7344          */
7345         if (gameMode != TwoMachinesPlay)
7346             SetUserThinkingEnables();
7347
7348         // [HGM] book: after book hit opponent has received move and is now in force mode
7349         // force the book reply into it, and then fake that it outputted this move by jumping
7350         // back to the beginning of HandleMachineMove, with cps toggled and message set to this move
7351         if(bookHit) {
7352                 static char bookMove[MSG_SIZ]; // a bit generous?
7353
7354                 strcpy(bookMove, "move ");
7355                 strcat(bookMove, bookHit);
7356                 message = bookMove;
7357                 cps = cps->other;
7358                 programStats.nodes = programStats.depth = programStats.time = 
7359                 programStats.score = programStats.got_only_move = 0;
7360                 sprintf(programStats.movelist, "%s (xbook)", bookHit);
7361
7362                 if(cps->lastPing != cps->lastPong) {
7363                     savedMessage = message; // args for deferred call
7364                     savedState = cps;
7365                     ScheduleDelayedEvent(DeferredBookMove, 10);
7366                     return;
7367                 }
7368                 goto FakeBookMove;
7369         }
7370
7371         return;
7372     }
7373
7374     /* Set special modes for chess engines.  Later something general
7375      *  could be added here; for now there is just one kludge feature,
7376      *  needed because Crafty 15.10 and earlier don't ignore SIGINT
7377      *  when "xboard" is given as an interactive command.
7378      */
7379     if (strncmp(message, "kibitz Hello from Crafty", 24) == 0) {
7380         cps->useSigint = FALSE;
7381         cps->useSigterm = FALSE;
7382     }
7383     if (strncmp(message, "feature ", 8) == 0) { // [HGM] moved forward to pre-empt non-compliant commands
7384       ParseFeatures(message+8, cps);
7385       return; // [HGM] This return was missing, causing option features to be recognized as non-compliant commands!
7386     }
7387
7388     /* [HGM] Allow engine to set up a position. Don't ask me why one would
7389      * want this, I was asked to put it in, and obliged.
7390      */
7391     if (!strncmp(message, "setboard ", 9)) {
7392         Board initial_position;
7393
7394         GameEnds(GameUnfinished, "Engine aborts game", GE_XBOARD);
7395
7396         if (!ParseFEN(initial_position, &blackPlaysFirst, message + 9)) {
7397             DisplayError(_("Bad FEN received from engine"), 0);
7398             return ;
7399         } else {
7400            Reset(TRUE, FALSE);
7401            CopyBoard(boards[0], initial_position);
7402            initialRulePlies = FENrulePlies;
7403            if(blackPlaysFirst) gameMode = MachinePlaysWhite;
7404            else gameMode = MachinePlaysBlack;                 
7405            DrawPosition(FALSE, boards[currentMove]);
7406         }
7407         return;
7408     }
7409
7410     /*
7411      * Look for communication commands
7412      */
7413     if (!strncmp(message, "telluser ", 9)) {
7414         EscapeExpand(message+9, message+9); // [HGM] esc: allow escape sequences in popup box
7415         DisplayNote(message + 9);
7416         return;
7417     }
7418     if (!strncmp(message, "tellusererror ", 14)) {
7419         cps->userError = 1;
7420         EscapeExpand(message+14, message+14); // [HGM] esc: allow escape sequences in popup box
7421         DisplayError(message + 14, 0);
7422         return;
7423     }
7424     if (!strncmp(message, "tellopponent ", 13)) {
7425       if (appData.icsActive) {
7426         if (loggedOn) {
7427           snprintf(buf1, sizeof(buf1), "%ssay %s\n", ics_prefix, message + 13);
7428           SendToICS(buf1);
7429         }
7430       } else {
7431         DisplayNote(message + 13);
7432       }
7433       return;
7434     }
7435     if (!strncmp(message, "tellothers ", 11)) {
7436       if (appData.icsActive) {
7437         if (loggedOn) {
7438           snprintf(buf1, sizeof(buf1), "%swhisper %s\n", ics_prefix, message + 11);
7439           SendToICS(buf1);
7440         }
7441       }
7442       return;
7443     }
7444     if (!strncmp(message, "tellall ", 8)) {
7445       if (appData.icsActive) {
7446         if (loggedOn) {
7447           snprintf(buf1, sizeof(buf1), "%skibitz %s\n", ics_prefix, message + 8);
7448           SendToICS(buf1);
7449         }
7450       } else {
7451         DisplayNote(message + 8);
7452       }
7453       return;
7454     }
7455     if (strncmp(message, "warning", 7) == 0) {
7456         /* Undocumented feature, use tellusererror in new code */
7457         DisplayError(message, 0);
7458         return;
7459     }
7460     if (sscanf(message, "askuser %s %[^\n]", buf1, buf2) == 2) {
7461         strcpy(realname, cps->tidy);
7462         strcat(realname, " query");
7463         AskQuestion(realname, buf2, buf1, cps->pr);
7464         return;
7465     }
7466     /* Commands from the engine directly to ICS.  We don't allow these to be 
7467      *  sent until we are logged on. Crafty kibitzes have been known to 
7468      *  interfere with the login process.
7469      */
7470     if (loggedOn) {
7471         if (!strncmp(message, "tellics ", 8)) {
7472             SendToICS(message + 8);
7473             SendToICS("\n");
7474             return;
7475         }
7476         if (!strncmp(message, "tellicsnoalias ", 15)) {
7477             SendToICS(ics_prefix);
7478             SendToICS(message + 15);
7479             SendToICS("\n");
7480             return;
7481         }
7482         /* The following are for backward compatibility only */
7483         if (!strncmp(message,"whisper",7) || !strncmp(message,"kibitz",6) ||
7484             !strncmp(message,"draw",4) || !strncmp(message,"tell",3)) {
7485             SendToICS(ics_prefix);
7486             SendToICS(message);
7487             SendToICS("\n");
7488             return;
7489         }
7490     }
7491     if (sscanf(message, "pong %d", &cps->lastPong) == 1) {
7492         return;
7493     }
7494     /*
7495      * If the move is illegal, cancel it and redraw the board.
7496      * Also deal with other error cases.  Matching is rather loose
7497      * here to accommodate engines written before the spec.
7498      */
7499     if (strncmp(message + 1, "llegal move", 11) == 0 ||
7500         strncmp(message, "Error", 5) == 0) {
7501         if (StrStr(message, "name") || 
7502             StrStr(message, "rating") || StrStr(message, "?") ||
7503             StrStr(message, "result") || StrStr(message, "board") ||
7504             StrStr(message, "bk") || StrStr(message, "computer") ||
7505             StrStr(message, "variant") || StrStr(message, "hint") ||
7506             StrStr(message, "random") || StrStr(message, "depth") ||
7507             StrStr(message, "accepted")) {
7508             return;
7509         }
7510         if (StrStr(message, "protover")) {
7511           /* Program is responding to input, so it's apparently done
7512              initializing, and this error message indicates it is
7513              protocol version 1.  So we don't need to wait any longer
7514              for it to initialize and send feature commands. */
7515           FeatureDone(cps, 1);
7516           cps->protocolVersion = 1;
7517           return;
7518         }
7519         cps->maybeThinking = FALSE;
7520
7521         if (StrStr(message, "draw")) {
7522             /* Program doesn't have "draw" command */
7523             cps->sendDrawOffers = 0;
7524             return;
7525         }
7526         if (cps->sendTime != 1 &&
7527             (StrStr(message, "time") || StrStr(message, "otim"))) {
7528           /* Program apparently doesn't have "time" or "otim" command */
7529           cps->sendTime = 0;
7530           return;
7531         }
7532         if (StrStr(message, "analyze")) {
7533             cps->analysisSupport = FALSE;
7534             cps->analyzing = FALSE;
7535             Reset(FALSE, TRUE);
7536             sprintf(buf2, _("%s does not support analysis"), cps->tidy);
7537             DisplayError(buf2, 0);
7538             return;
7539         }
7540         if (StrStr(message, "(no matching move)st")) {
7541           /* Special kludge for GNU Chess 4 only */
7542           cps->stKludge = TRUE;
7543           SendTimeControl(cps, movesPerSession, timeControl,
7544                           timeIncrement, appData.searchDepth,
7545                           searchTime);
7546           return;
7547         }
7548         if (StrStr(message, "(no matching move)sd")) {
7549           /* Special kludge for GNU Chess 4 only */
7550           cps->sdKludge = TRUE;
7551           SendTimeControl(cps, movesPerSession, timeControl,
7552                           timeIncrement, appData.searchDepth,
7553                           searchTime);
7554           return;
7555         }
7556         if (!StrStr(message, "llegal")) {
7557             return;
7558         }
7559         if (gameMode == BeginningOfGame || gameMode == EndOfGame ||
7560             gameMode == IcsIdle) return;
7561         if (forwardMostMove <= backwardMostMove) return;
7562         if (pausing) PauseEvent();
7563       if(appData.forceIllegal) {
7564             // [HGM] illegal: machine refused move; force position after move into it
7565           SendToProgram("force\n", cps);
7566           if(!cps->useSetboard) { // hideous kludge on kludge, because SendBoard sucks.
7567                 // we have a real problem now, as SendBoard will use the a2a3 kludge
7568                 // when black is to move, while there might be nothing on a2 or black
7569                 // might already have the move. So send the board as if white has the move.
7570                 // But first we must change the stm of the engine, as it refused the last move
7571                 SendBoard(cps, 0); // always kludgeless, as white is to move on boards[0]
7572                 if(WhiteOnMove(forwardMostMove)) {
7573                     SendToProgram("a7a6\n", cps); // for the engine black still had the move
7574                     SendBoard(cps, forwardMostMove); // kludgeless board
7575                 } else {
7576                     SendToProgram("a2a3\n", cps); // for the engine white still had the move
7577                     CopyBoard(boards[forwardMostMove+1], boards[forwardMostMove]);
7578                     SendBoard(cps, forwardMostMove+1); // kludgeless board
7579                 }
7580           } else SendBoard(cps, forwardMostMove); // FEN case, also sets stm properly
7581             if(gameMode == MachinePlaysWhite || gameMode == MachinePlaysBlack ||
7582                  gameMode == TwoMachinesPlay)
7583               SendToProgram("go\n", cps);
7584             return;
7585       } else
7586         if (gameMode == PlayFromGameFile) {
7587             /* Stop reading this game file */
7588             gameMode = EditGame;
7589             ModeHighlight();
7590         }
7591         currentMove = forwardMostMove-1;
7592         DisplayMove(currentMove-1); /* before DisplayMoveError */
7593         SwitchClocks(forwardMostMove-1); // [HGM] race
7594         DisplayBothClocks();
7595         sprintf(buf1, _("Illegal move \"%s\" (rejected by %s chess program)"),
7596                 parseList[currentMove], cps->which);
7597         DisplayMoveError(buf1);
7598         DrawPosition(FALSE, boards[currentMove]);
7599
7600         /* [HGM] illegal-move claim should forfeit game when Xboard */
7601         /* only passes fully legal moves                            */
7602         if( appData.testLegality && gameMode == TwoMachinesPlay ) {
7603             GameEnds( cps->twoMachinesColor[0] == 'w' ? BlackWins : WhiteWins,
7604                                 "False illegal-move claim", GE_XBOARD );
7605         }
7606         return;
7607     }
7608     if (strncmp(message, "time", 4) == 0 && StrStr(message, "Illegal")) {
7609         /* Program has a broken "time" command that
7610            outputs a string not ending in newline.
7611            Don't use it. */
7612         cps->sendTime = 0;
7613     }
7614     
7615     /*
7616      * If chess program startup fails, exit with an error message.
7617      * Attempts to recover here are futile.
7618      */
7619     if ((StrStr(message, "unknown host") != NULL)
7620         || (StrStr(message, "No remote directory") != NULL)
7621         || (StrStr(message, "not found") != NULL)
7622         || (StrStr(message, "No such file") != NULL)
7623         || (StrStr(message, "can't alloc") != NULL)
7624         || (StrStr(message, "Permission denied") != NULL)) {
7625
7626         cps->maybeThinking = FALSE;
7627         snprintf(buf1, sizeof(buf1), _("Failed to start %s chess program %s on %s: %s\n"),
7628                 cps->which, cps->program, cps->host, message);
7629         RemoveInputSource(cps->isr);
7630         DisplayFatalError(buf1, 0, 1);
7631         return;
7632     }
7633     
7634     /* 
7635      * Look for hint output
7636      */
7637     if (sscanf(message, "Hint: %s", buf1) == 1) {
7638         if (cps == &first && hintRequested) {
7639             hintRequested = FALSE;
7640             if (ParseOneMove(buf1, forwardMostMove, &moveType,
7641                                  &fromX, &fromY, &toX, &toY, &promoChar)) {
7642                 (void) CoordsToAlgebraic(boards[forwardMostMove],
7643                                     PosFlags(forwardMostMove),
7644                                     fromY, fromX, toY, toX, promoChar, buf1);
7645                 snprintf(buf2, sizeof(buf2), _("Hint: %s"), buf1);
7646                 DisplayInformation(buf2);
7647             } else {
7648                 /* Hint move could not be parsed!? */
7649               snprintf(buf2, sizeof(buf2),
7650                         _("Illegal hint move \"%s\"\nfrom %s chess program"),
7651                         buf1, cps->which);
7652                 DisplayError(buf2, 0);
7653             }
7654         } else {
7655             strcpy(lastHint, buf1);
7656         }
7657         return;
7658     }
7659
7660     /*
7661      * Ignore other messages if game is not in progress
7662      */
7663     if (gameMode == BeginningOfGame || gameMode == EndOfGame ||
7664         gameMode == IcsIdle || cps->lastPing != cps->lastPong) return;
7665
7666     /*
7667      * look for win, lose, draw, or draw offer
7668      */
7669     if (strncmp(message, "1-0", 3) == 0) {
7670         char *p, *q, *r = "";
7671         p = strchr(message, '{');
7672         if (p) {
7673             q = strchr(p, '}');
7674             if (q) {
7675                 *q = NULLCHAR;
7676                 r = p + 1;
7677             }
7678         }
7679         GameEnds(WhiteWins, r, GE_ENGINE1 + (cps != &first)); /* [HGM] pass claimer indication for claim test */
7680         return;
7681     } else if (strncmp(message, "0-1", 3) == 0) {
7682         char *p, *q, *r = "";
7683         p = strchr(message, '{');
7684         if (p) {
7685             q = strchr(p, '}');
7686             if (q) {
7687                 *q = NULLCHAR;
7688                 r = p + 1;
7689             }
7690         }
7691         /* Kludge for Arasan 4.1 bug */
7692         if (strcmp(r, "Black resigns") == 0) {
7693             GameEnds(WhiteWins, r, GE_ENGINE1 + (cps != &first));
7694             return;
7695         }
7696         GameEnds(BlackWins, r, GE_ENGINE1 + (cps != &first));
7697         return;
7698     } else if (strncmp(message, "1/2", 3) == 0) {
7699         char *p, *q, *r = "";
7700         p = strchr(message, '{');
7701         if (p) {
7702             q = strchr(p, '}');
7703             if (q) {
7704                 *q = NULLCHAR;
7705                 r = p + 1;
7706             }
7707         }
7708             
7709         GameEnds(GameIsDrawn, r, GE_ENGINE1 + (cps != &first));
7710         return;
7711
7712     } else if (strncmp(message, "White resign", 12) == 0) {
7713         GameEnds(BlackWins, "White resigns", GE_ENGINE1 + (cps != &first));
7714         return;
7715     } else if (strncmp(message, "Black resign", 12) == 0) {
7716         GameEnds(WhiteWins, "Black resigns", GE_ENGINE1 + (cps != &first));
7717         return;
7718     } else if (strncmp(message, "White matches", 13) == 0 ||
7719                strncmp(message, "Black matches", 13) == 0   ) {
7720         /* [HGM] ignore GNUShogi noises */
7721         return;
7722     } else if (strncmp(message, "White", 5) == 0 &&
7723                message[5] != '(' &&
7724                StrStr(message, "Black") == NULL) {
7725         GameEnds(WhiteWins, "White mates", GE_ENGINE1 + (cps != &first));
7726         return;
7727     } else if (strncmp(message, "Black", 5) == 0 &&
7728                message[5] != '(') {
7729         GameEnds(BlackWins, "Black mates", GE_ENGINE1 + (cps != &first));
7730         return;
7731     } else if (strcmp(message, "resign") == 0 ||
7732                strcmp(message, "computer resigns") == 0) {
7733         switch (gameMode) {
7734           case MachinePlaysBlack:
7735           case IcsPlayingBlack:
7736             GameEnds(WhiteWins, "Black resigns", GE_ENGINE);
7737             break;
7738           case MachinePlaysWhite:
7739           case IcsPlayingWhite:
7740             GameEnds(BlackWins, "White resigns", GE_ENGINE);
7741             break;
7742           case TwoMachinesPlay:
7743             if (cps->twoMachinesColor[0] == 'w')
7744               GameEnds(BlackWins, "White resigns", GE_ENGINE1 + (cps != &first));
7745             else
7746               GameEnds(WhiteWins, "Black resigns", GE_ENGINE1 + (cps != &first));
7747             break;
7748           default:
7749             /* can't happen */
7750             break;
7751         }
7752         return;
7753     } else if (strncmp(message, "opponent mates", 14) == 0) {
7754         switch (gameMode) {
7755           case MachinePlaysBlack:
7756           case IcsPlayingBlack:
7757             GameEnds(WhiteWins, "White mates", GE_ENGINE);
7758             break;
7759           case MachinePlaysWhite:
7760           case IcsPlayingWhite:
7761             GameEnds(BlackWins, "Black mates", GE_ENGINE);
7762             break;
7763           case TwoMachinesPlay:
7764             if (cps->twoMachinesColor[0] == 'w')
7765               GameEnds(BlackWins, "Black mates", GE_ENGINE1 + (cps != &first));
7766             else
7767               GameEnds(WhiteWins, "White mates", GE_ENGINE1 + (cps != &first));
7768             break;
7769           default:
7770             /* can't happen */
7771             break;
7772         }
7773         return;
7774     } else if (strncmp(message, "computer mates", 14) == 0) {
7775         switch (gameMode) {
7776           case MachinePlaysBlack:
7777           case IcsPlayingBlack:
7778             GameEnds(BlackWins, "Black mates", GE_ENGINE1);
7779             break;
7780           case MachinePlaysWhite:
7781           case IcsPlayingWhite:
7782             GameEnds(WhiteWins, "White mates", GE_ENGINE);
7783             break;
7784           case TwoMachinesPlay:
7785             if (cps->twoMachinesColor[0] == 'w')
7786               GameEnds(WhiteWins, "White mates", GE_ENGINE1 + (cps != &first));
7787             else
7788               GameEnds(BlackWins, "Black mates", GE_ENGINE1 + (cps != &first));
7789             break;
7790           default:
7791             /* can't happen */
7792             break;
7793         }
7794         return;
7795     } else if (strncmp(message, "checkmate", 9) == 0) {
7796         if (WhiteOnMove(forwardMostMove)) {
7797             GameEnds(BlackWins, "Black mates", GE_ENGINE1 + (cps != &first));
7798         } else {
7799             GameEnds(WhiteWins, "White mates", GE_ENGINE1 + (cps != &first));
7800         }
7801         return;
7802     } else if (strstr(message, "Draw") != NULL ||
7803                strstr(message, "game is a draw") != NULL) {
7804         GameEnds(GameIsDrawn, "Draw", GE_ENGINE1 + (cps != &first));
7805         return;
7806     } else if (strstr(message, "offer") != NULL &&
7807                strstr(message, "draw") != NULL) {
7808 #if ZIPPY
7809         if (appData.zippyPlay && first.initDone) {
7810             /* Relay offer to ICS */
7811             SendToICS(ics_prefix);
7812             SendToICS("draw\n");
7813         }
7814 #endif
7815         cps->offeredDraw = 2; /* valid until this engine moves twice */
7816         if (gameMode == TwoMachinesPlay) {
7817             if (cps->other->offeredDraw) {
7818                 GameEnds(GameIsDrawn, "Draw agreed", GE_XBOARD);
7819             /* [HGM] in two-machine mode we delay relaying draw offer      */
7820             /* until after we also have move, to see if it is really claim */
7821             }
7822         } else if (gameMode == MachinePlaysWhite ||
7823                    gameMode == MachinePlaysBlack) {
7824           if (userOfferedDraw) {
7825             DisplayInformation(_("Machine accepts your draw offer"));
7826             GameEnds(GameIsDrawn, "Draw agreed", GE_XBOARD);
7827           } else {
7828             DisplayInformation(_("Machine offers a draw\nSelect Action / Draw to agree"));
7829           }
7830         }
7831     }
7832
7833     
7834     /*
7835      * Look for thinking output
7836      */
7837     if ( appData.showThinking // [HGM] thinking: test all options that cause this output
7838           || !appData.hideThinkingFromHuman || appData.adjudicateLossThreshold != 0 || EngineOutputIsUp()
7839                                 ) {
7840         int plylev, mvleft, mvtot, curscore, time;
7841         char mvname[MOVE_LEN];
7842         u64 nodes; // [DM]
7843         char plyext;
7844         int ignore = FALSE;
7845         int prefixHint = FALSE;
7846         mvname[0] = NULLCHAR;
7847
7848         switch (gameMode) {
7849           case MachinePlaysBlack:
7850           case IcsPlayingBlack:
7851             if (WhiteOnMove(forwardMostMove)) prefixHint = TRUE;
7852             break;
7853           case MachinePlaysWhite:
7854           case IcsPlayingWhite:
7855             if (!WhiteOnMove(forwardMostMove)) prefixHint = TRUE;
7856             break;
7857           case AnalyzeMode:
7858           case AnalyzeFile:
7859             break;
7860           case IcsObserving: /* [DM] icsEngineAnalyze */
7861             if (!appData.icsEngineAnalyze) ignore = TRUE;
7862             break;
7863           case TwoMachinesPlay:
7864             if ((cps->twoMachinesColor[0] == 'w') != WhiteOnMove(forwardMostMove)) {
7865                 ignore = TRUE;
7866             }
7867             break;
7868           default:
7869             ignore = TRUE;
7870             break;
7871         }
7872
7873         if (!ignore) {
7874             ChessProgramStats tempStats = programStats; // [HGM] info: filter out info lines
7875             buf1[0] = NULLCHAR;
7876             if (sscanf(message, "%d%c %d %d " u64Display " %[^\n]\n",
7877                        &plylev, &plyext, &curscore, &time, &nodes, buf1) >= 5) {
7878
7879                 if (plyext != ' ' && plyext != '\t') {
7880                     time *= 100;
7881                 }
7882
7883                 /* [AS] Negate score if machine is playing black and reporting absolute scores */
7884                 if( cps->scoreIsAbsolute && 
7885                     ( gameMode == MachinePlaysBlack ||
7886                       gameMode == TwoMachinesPlay && cps->twoMachinesColor[0] == 'b' ||
7887                       gameMode == IcsPlayingBlack ||     // [HGM] also add other situations where engine should report black POV
7888                      (gameMode == AnalyzeMode || gameMode == AnalyzeFile || gameMode == IcsObserving && appData.icsEngineAnalyze) &&
7889                      !WhiteOnMove(currentMove)
7890                     ) )
7891                 {
7892                     curscore = -curscore;
7893                 }
7894
7895
7896                 tempStats.depth = plylev;
7897                 tempStats.nodes = nodes;
7898                 tempStats.time = time;
7899                 tempStats.score = curscore;
7900                 tempStats.got_only_move = 0;
7901
7902                 if(cps->nps >= 0) { /* [HGM] nps: use engine nodes or time to decrement clock */
7903                         int ticklen;
7904
7905                         if(cps->nps == 0) ticklen = 10*time;                    // use engine reported time
7906                         else ticklen = (1000. * u64ToDouble(nodes)) / cps->nps; // convert node count to time
7907                         if(WhiteOnMove(forwardMostMove) && (gameMode == MachinePlaysWhite ||
7908                                                 gameMode == TwoMachinesPlay && cps->twoMachinesColor[0] == 'w')) 
7909                              whiteTimeRemaining = timeRemaining[0][forwardMostMove] - ticklen;
7910                         if(!WhiteOnMove(forwardMostMove) && (gameMode == MachinePlaysBlack ||
7911                                                 gameMode == TwoMachinesPlay && cps->twoMachinesColor[0] == 'b')) 
7912                              blackTimeRemaining = timeRemaining[1][forwardMostMove] - ticklen;
7913                 }
7914
7915                 /* Buffer overflow protection */
7916                 if (buf1[0] != NULLCHAR) {
7917                     if (strlen(buf1) >= sizeof(tempStats.movelist)
7918                         && appData.debugMode) {
7919                         fprintf(debugFP,
7920                                 "PV is too long; using the first %u bytes.\n",
7921                                 (unsigned) sizeof(tempStats.movelist) - 1);
7922                     }
7923
7924                     safeStrCpy( tempStats.movelist, buf1, sizeof(tempStats.movelist) );
7925                 } else {
7926                     sprintf(tempStats.movelist, " no PV\n");
7927                 }
7928
7929                 if (tempStats.seen_stat) {
7930                     tempStats.ok_to_send = 1;
7931                 }
7932
7933                 if (strchr(tempStats.movelist, '(') != NULL) {
7934                     tempStats.line_is_book = 1;
7935                     tempStats.nr_moves = 0;
7936                     tempStats.moves_left = 0;
7937                 } else {
7938                     tempStats.line_is_book = 0;
7939                 }
7940
7941                     if(tempStats.score != 0 || tempStats.nodes != 0 || tempStats.time != 0)
7942                         programStats = tempStats; // [HGM] info: only set stats if genuine PV and not an info line
7943
7944                 SendProgramStatsToFrontend( cps, &tempStats );
7945
7946                 /* 
7947                     [AS] Protect the thinkOutput buffer from overflow... this
7948                     is only useful if buf1 hasn't overflowed first!
7949                 */
7950                 sprintf(thinkOutput, "[%d]%c%+.2f %s%s",
7951                         plylev, 
7952                         (gameMode == TwoMachinesPlay ?
7953                          ToUpper(cps->twoMachinesColor[0]) : ' '),
7954                         ((double) curscore) / 100.0,
7955                         prefixHint ? lastHint : "",
7956                         prefixHint ? " " : "" );
7957
7958                 if( buf1[0] != NULLCHAR ) {
7959                     unsigned max_len = sizeof(thinkOutput) - strlen(thinkOutput) - 1;
7960
7961                     if( strlen(buf1) > max_len ) {
7962                         if( appData.debugMode) {
7963                             fprintf(debugFP,"PV is too long for thinkOutput, truncating.\n");
7964                         }
7965                         buf1[max_len+1] = '\0';
7966                     }
7967
7968                     strcat( thinkOutput, buf1 );
7969                 }
7970
7971                 if (currentMove == forwardMostMove || gameMode == AnalyzeMode
7972                         || gameMode == AnalyzeFile || appData.icsEngineAnalyze) {
7973                     DisplayMove(currentMove - 1);
7974                 }
7975                 return;
7976
7977             } else if ((p=StrStr(message, "(only move)")) != NULL) {
7978                 /* crafty (9.25+) says "(only move) <move>"
7979                  * if there is only 1 legal move
7980                  */
7981                 sscanf(p, "(only move) %s", buf1);
7982                 sprintf(thinkOutput, "%s (only move)", buf1);
7983                 sprintf(programStats.movelist, "%s (only move)", buf1);
7984                 programStats.depth = 1;
7985                 programStats.nr_moves = 1;
7986                 programStats.moves_left = 1;
7987                 programStats.nodes = 1;
7988                 programStats.time = 1;
7989                 programStats.got_only_move = 1;
7990
7991                 /* Not really, but we also use this member to
7992                    mean "line isn't going to change" (Crafty
7993                    isn't searching, so stats won't change) */
7994                 programStats.line_is_book = 1;
7995
7996                 SendProgramStatsToFrontend( cps, &programStats );
7997                 
7998                 if (currentMove == forwardMostMove || gameMode==AnalyzeMode || 
7999                            gameMode == AnalyzeFile || appData.icsEngineAnalyze) {
8000                     DisplayMove(currentMove - 1);
8001                 }
8002                 return;
8003             } else if (sscanf(message,"stat01: %d " u64Display " %d %d %d %s",
8004                               &time, &nodes, &plylev, &mvleft,
8005                               &mvtot, mvname) >= 5) {
8006                 /* The stat01: line is from Crafty (9.29+) in response
8007                    to the "." command */
8008                 programStats.seen_stat = 1;
8009                 cps->maybeThinking = TRUE;
8010
8011                 if (programStats.got_only_move || !appData.periodicUpdates)
8012                   return;
8013
8014                 programStats.depth = plylev;
8015                 programStats.time = time;
8016                 programStats.nodes = nodes;
8017                 programStats.moves_left = mvleft;
8018                 programStats.nr_moves = mvtot;
8019                 strcpy(programStats.move_name, mvname);
8020                 programStats.ok_to_send = 1;
8021                 programStats.movelist[0] = '\0';
8022
8023                 SendProgramStatsToFrontend( cps, &programStats );
8024
8025                 return;
8026
8027             } else if (strncmp(message,"++",2) == 0) {
8028                 /* Crafty 9.29+ outputs this */
8029                 programStats.got_fail = 2;
8030                 return;
8031
8032             } else if (strncmp(message,"--",2) == 0) {
8033                 /* Crafty 9.29+ outputs this */
8034                 programStats.got_fail = 1;
8035                 return;
8036
8037             } else if (thinkOutput[0] != NULLCHAR &&
8038                        strncmp(message, "    ", 4) == 0) {
8039                 unsigned message_len;
8040
8041                 p = message;
8042                 while (*p && *p == ' ') p++;
8043
8044                 message_len = strlen( p );
8045
8046                 /* [AS] Avoid buffer overflow */
8047                 if( sizeof(thinkOutput) - strlen(thinkOutput) - 1 > message_len ) {
8048                     strcat(thinkOutput, " ");
8049                     strcat(thinkOutput, p);
8050                 }
8051
8052                 if( sizeof(programStats.movelist) - strlen(programStats.movelist) - 1 > message_len ) {
8053                     strcat(programStats.movelist, " ");
8054                     strcat(programStats.movelist, p);
8055                 }
8056
8057                 if (currentMove == forwardMostMove || gameMode==AnalyzeMode ||
8058                            gameMode == AnalyzeFile || appData.icsEngineAnalyze) {
8059                     DisplayMove(currentMove - 1);
8060                 }
8061                 return;
8062             }
8063         }
8064         else {
8065             buf1[0] = NULLCHAR;
8066
8067             if (sscanf(message, "%d%c %d %d " u64Display " %[^\n]\n",
8068                        &plylev, &plyext, &curscore, &time, &nodes, buf1) >= 5) 
8069             {
8070                 ChessProgramStats cpstats;
8071
8072                 if (plyext != ' ' && plyext != '\t') {
8073                     time *= 100;
8074                 }
8075
8076                 /* [AS] Negate score if machine is playing black and reporting absolute scores */
8077                 if( cps->scoreIsAbsolute && ((gameMode == MachinePlaysBlack) || (gameMode == TwoMachinesPlay && cps->twoMachinesColor[0] == 'b')) ) {
8078                     curscore = -curscore;
8079                 }
8080
8081                 cpstats.depth = plylev;
8082                 cpstats.nodes = nodes;
8083                 cpstats.time = time;
8084                 cpstats.score = curscore;
8085                 cpstats.got_only_move = 0;
8086                 cpstats.movelist[0] = '\0';
8087
8088                 if (buf1[0] != NULLCHAR) {
8089                     safeStrCpy( cpstats.movelist, buf1, sizeof(cpstats.movelist) );
8090                 }
8091
8092                 cpstats.ok_to_send = 0;
8093                 cpstats.line_is_book = 0;
8094                 cpstats.nr_moves = 0;
8095                 cpstats.moves_left = 0;
8096
8097                 SendProgramStatsToFrontend( cps, &cpstats );
8098             }
8099         }
8100     }
8101 }
8102
8103
8104 /* Parse a game score from the character string "game", and
8105    record it as the history of the current game.  The game
8106    score is NOT assumed to start from the standard position. 
8107    The display is not updated in any way.
8108    */
8109 void
8110 ParseGameHistory(game)
8111      char *game;
8112 {
8113     ChessMove moveType;
8114     int fromX, fromY, toX, toY, boardIndex;
8115     char promoChar;
8116     char *p, *q;
8117     char buf[MSG_SIZ];
8118
8119     if (appData.debugMode)
8120       fprintf(debugFP, "Parsing game history: %s\n", game);
8121
8122     if (gameInfo.event == NULL) gameInfo.event = StrSave("ICS game");
8123     gameInfo.site = StrSave(appData.icsHost);
8124     gameInfo.date = PGNDate();
8125     gameInfo.round = StrSave("-");
8126
8127     /* Parse out names of players */
8128     while (*game == ' ') game++;
8129     p = buf;
8130     while (*game != ' ') *p++ = *game++;
8131     *p = NULLCHAR;
8132     gameInfo.white = StrSave(buf);
8133     while (*game == ' ') game++;
8134     p = buf;
8135     while (*game != ' ' && *game != '\n') *p++ = *game++;
8136     *p = NULLCHAR;
8137     gameInfo.black = StrSave(buf);
8138
8139     /* Parse moves */
8140     boardIndex = blackPlaysFirst ? 1 : 0;
8141     yynewstr(game);
8142     for (;;) {
8143         yyboardindex = boardIndex;
8144         moveType = (ChessMove) yylex();
8145         switch (moveType) {
8146           case IllegalMove:             /* maybe suicide chess, etc. */
8147   if (appData.debugMode) {
8148     fprintf(debugFP, "Illegal move from ICS: '%s'\n", yy_text);
8149     fprintf(debugFP, "board L=%d, R=%d, H=%d, holdings=%d\n", BOARD_LEFT, BOARD_RGHT, BOARD_HEIGHT, gameInfo.holdingsWidth);
8150     setbuf(debugFP, NULL);
8151   }
8152           case WhitePromotionChancellor:
8153           case BlackPromotionChancellor:
8154           case WhitePromotionArchbishop:
8155           case BlackPromotionArchbishop:
8156           case WhitePromotionQueen:
8157           case BlackPromotionQueen:
8158           case WhitePromotionRook:
8159           case BlackPromotionRook:
8160           case WhitePromotionBishop:
8161           case BlackPromotionBishop:
8162           case WhitePromotionKnight:
8163           case BlackPromotionKnight:
8164           case WhitePromotionKing:
8165           case BlackPromotionKing:
8166           case NormalMove:
8167           case WhiteCapturesEnPassant:
8168           case BlackCapturesEnPassant:
8169           case WhiteKingSideCastle:
8170           case WhiteQueenSideCastle:
8171           case BlackKingSideCastle:
8172           case BlackQueenSideCastle:
8173           case WhiteKingSideCastleWild:
8174           case WhiteQueenSideCastleWild:
8175           case BlackKingSideCastleWild:
8176           case BlackQueenSideCastleWild:
8177           /* PUSH Fabien */
8178           case WhiteHSideCastleFR:
8179           case WhiteASideCastleFR:
8180           case BlackHSideCastleFR:
8181           case BlackASideCastleFR:
8182           /* POP Fabien */
8183             fromX = currentMoveString[0] - AAA;
8184             fromY = currentMoveString[1] - ONE;
8185             toX = currentMoveString[2] - AAA;
8186             toY = currentMoveString[3] - ONE;
8187             promoChar = currentMoveString[4];
8188             break;
8189           case WhiteDrop:
8190           case BlackDrop:
8191             fromX = moveType == WhiteDrop ?
8192               (int) CharToPiece(ToUpper(currentMoveString[0])) :
8193             (int) CharToPiece(ToLower(currentMoveString[0]));
8194             fromY = DROP_RANK;
8195             toX = currentMoveString[2] - AAA;
8196             toY = currentMoveString[3] - ONE;
8197             promoChar = NULLCHAR;
8198             break;
8199           case AmbiguousMove:
8200             /* bug? */
8201             sprintf(buf, _("Ambiguous move in ICS output: \"%s\""), yy_text);
8202   if (appData.debugMode) {
8203     fprintf(debugFP, "Ambiguous move from ICS: '%s'\n", yy_text);
8204     fprintf(debugFP, "board L=%d, R=%d, H=%d, holdings=%d\n", BOARD_LEFT, BOARD_RGHT, BOARD_HEIGHT, gameInfo.holdingsWidth);
8205     setbuf(debugFP, NULL);
8206   }
8207             DisplayError(buf, 0);
8208             return;
8209           case ImpossibleMove:
8210             /* bug? */
8211             sprintf(buf, _("Illegal move in ICS output: \"%s\""), yy_text);
8212   if (appData.debugMode) {
8213     fprintf(debugFP, "Impossible move from ICS: '%s'\n", yy_text);
8214     fprintf(debugFP, "board L=%d, R=%d, H=%d, holdings=%d\n", BOARD_LEFT, BOARD_RGHT, BOARD_HEIGHT, gameInfo.holdingsWidth);
8215     setbuf(debugFP, NULL);
8216   }
8217             DisplayError(buf, 0);
8218             return;
8219           case (ChessMove) 0:   /* end of file */
8220             if (boardIndex < backwardMostMove) {
8221                 /* Oops, gap.  How did that happen? */
8222                 DisplayError(_("Gap in move list"), 0);
8223                 return;
8224             }
8225             backwardMostMove =  blackPlaysFirst ? 1 : 0;
8226             if (boardIndex > forwardMostMove) {
8227                 forwardMostMove = boardIndex;
8228             }
8229             return;
8230           case ElapsedTime:
8231             if (boardIndex > (blackPlaysFirst ? 1 : 0)) {
8232                 strcat(parseList[boardIndex-1], " ");
8233                 strcat(parseList[boardIndex-1], yy_text);
8234             }
8235             continue;
8236           case Comment:
8237           case PGNTag:
8238           case NAG:
8239           default:
8240             /* ignore */
8241             continue;
8242           case WhiteWins:
8243           case BlackWins:
8244           case GameIsDrawn:
8245           case GameUnfinished:
8246             if (gameMode == IcsExamining) {
8247                 if (boardIndex < backwardMostMove) {
8248                     /* Oops, gap.  How did that happen? */
8249                     return;
8250                 }
8251                 backwardMostMove = blackPlaysFirst ? 1 : 0;
8252                 return;
8253             }
8254             gameInfo.result = moveType;
8255             p = strchr(yy_text, '{');
8256             if (p == NULL) p = strchr(yy_text, '(');
8257             if (p == NULL) {
8258                 p = yy_text;
8259                 if (p[0] == '0' || p[0] == '1' || p[0] == '*') p = "";
8260             } else {
8261                 q = strchr(p, *p == '{' ? '}' : ')');
8262                 if (q != NULL) *q = NULLCHAR;
8263                 p++;
8264             }
8265             gameInfo.resultDetails = StrSave(p);
8266             continue;
8267         }
8268         if (boardIndex >= forwardMostMove &&
8269             !(gameMode == IcsObserving && ics_gamenum == -1)) {
8270             backwardMostMove = blackPlaysFirst ? 1 : 0;
8271             return;
8272         }
8273         (void) CoordsToAlgebraic(boards[boardIndex], PosFlags(boardIndex),
8274                                  fromY, fromX, toY, toX, promoChar,
8275                                  parseList[boardIndex]);
8276         CopyBoard(boards[boardIndex + 1], boards[boardIndex]);
8277         /* currentMoveString is set as a side-effect of yylex */
8278         strcpy(moveList[boardIndex], currentMoveString);
8279         strcat(moveList[boardIndex], "\n");
8280         boardIndex++;
8281         ApplyMove(fromX, fromY, toX, toY, promoChar, boards[boardIndex]);
8282         switch (MateTest(boards[boardIndex], PosFlags(boardIndex)) ) {
8283           case MT_NONE:
8284           case MT_STALEMATE:
8285           default:
8286             break;
8287           case MT_CHECK:
8288             if(gameInfo.variant != VariantShogi)
8289                 strcat(parseList[boardIndex - 1], "+");
8290             break;
8291           case MT_CHECKMATE:
8292           case MT_STAINMATE:
8293             strcat(parseList[boardIndex - 1], "#");
8294             break;
8295         }
8296     }
8297 }
8298
8299
8300 /* Apply a move to the given board  */
8301 void
8302 ApplyMove(fromX, fromY, toX, toY, promoChar, board)
8303      int fromX, fromY, toX, toY;
8304      int promoChar;
8305      Board board;
8306 {
8307   ChessSquare captured = board[toY][toX], piece, king; int p, oldEP = EP_NONE, berolina = 0;
8308   int promoRank = gameInfo.variant == VariantMakruk ? 3 : 1;
8309
8310     /* [HGM] compute & store e.p. status and castling rights for new position */
8311     /* we can always do that 'in place', now pointers to these rights are passed to ApplyMove */
8312     { int i;
8313
8314       if(gameInfo.variant == VariantBerolina) berolina = EP_BEROLIN_A;
8315       oldEP = (signed char)board[EP_STATUS];
8316       board[EP_STATUS] = EP_NONE;
8317
8318       if( board[toY][toX] != EmptySquare ) 
8319            board[EP_STATUS] = EP_CAPTURE;  
8320
8321       if( board[fromY][fromX] == WhitePawn ) {
8322            if(fromY != toY) // [HGM] Xiangqi sideway Pawn moves should not count as 50-move breakers
8323                board[EP_STATUS] = EP_PAWN_MOVE;
8324            if( toY-fromY==2) {
8325                if(toX>BOARD_LEFT   && board[toY][toX-1] == BlackPawn &&
8326                         gameInfo.variant != VariantBerolina || toX < fromX)
8327                       board[EP_STATUS] = toX | berolina;
8328                if(toX<BOARD_RGHT-1 && board[toY][toX+1] == BlackPawn &&
8329                         gameInfo.variant != VariantBerolina || toX > fromX) 
8330                       board[EP_STATUS] = toX;
8331            }
8332       } else 
8333       if( board[fromY][fromX] == BlackPawn ) {
8334            if(fromY != toY) // [HGM] Xiangqi sideway Pawn moves should not count as 50-move breakers
8335                board[EP_STATUS] = EP_PAWN_MOVE; 
8336            if( toY-fromY== -2) {
8337                if(toX>BOARD_LEFT   && board[toY][toX-1] == WhitePawn &&
8338                         gameInfo.variant != VariantBerolina || toX < fromX)
8339                       board[EP_STATUS] = toX | berolina;
8340                if(toX<BOARD_RGHT-1 && board[toY][toX+1] == WhitePawn &&
8341                         gameInfo.variant != VariantBerolina || toX > fromX) 
8342                       board[EP_STATUS] = toX;
8343            }
8344        }
8345
8346        for(i=0; i<nrCastlingRights; i++) {
8347            if(board[CASTLING][i] == fromX && castlingRank[i] == fromY ||
8348               board[CASTLING][i] == toX   && castlingRank[i] == toY   
8349              ) board[CASTLING][i] = NoRights; // revoke for moved or captured piece
8350        }
8351
8352     }
8353
8354   /* [HGM] In Shatranj and Courier all promotions are to Ferz */
8355   if((gameInfo.variant==VariantShatranj || gameInfo.variant==VariantCourier || gameInfo.variant == VariantMakruk)
8356        && promoChar != 0) promoChar = PieceToChar(WhiteFerz);
8357          
8358   if (fromX == toX && fromY == toY) return;
8359
8360   if (fromY == DROP_RANK) {
8361         /* must be first */
8362         piece = board[toY][toX] = (ChessSquare) fromX;
8363   } else {
8364      piece = board[fromY][fromX]; /* [HGM] remember, for Shogi promotion */
8365      king = piece < (int) BlackPawn ? WhiteKing : BlackKing; /* [HGM] Knightmate simplify testing for castling */
8366      if(gameInfo.variant == VariantKnightmate)
8367          king += (int) WhiteUnicorn - (int) WhiteKing;
8368
8369     /* Code added by Tord: */
8370     /* FRC castling assumed when king captures friendly rook. */
8371     if (board[fromY][fromX] == WhiteKing &&
8372              board[toY][toX] == WhiteRook) {
8373       board[fromY][fromX] = EmptySquare;
8374       board[toY][toX] = EmptySquare;
8375       if(toX > fromX) {
8376         board[0][BOARD_RGHT-2] = WhiteKing; board[0][BOARD_RGHT-3] = WhiteRook;
8377       } else {
8378         board[0][BOARD_LEFT+2] = WhiteKing; board[0][BOARD_LEFT+3] = WhiteRook;
8379       }
8380     } else if (board[fromY][fromX] == BlackKing &&
8381                board[toY][toX] == BlackRook) {
8382       board[fromY][fromX] = EmptySquare;
8383       board[toY][toX] = EmptySquare;
8384       if(toX > fromX) {
8385         board[BOARD_HEIGHT-1][BOARD_RGHT-2] = BlackKing; board[BOARD_HEIGHT-1][BOARD_RGHT-3] = BlackRook;
8386       } else {
8387         board[BOARD_HEIGHT-1][BOARD_LEFT+2] = BlackKing; board[BOARD_HEIGHT-1][BOARD_LEFT+3] = BlackRook;
8388       }
8389     /* End of code added by Tord */
8390
8391     } else if (board[fromY][fromX] == king
8392         && fromX != BOARD_LEFT && fromX != BOARD_RGHT-1 // [HGM] cylinder */
8393         && toY == fromY && toX > fromX+1) {
8394         board[fromY][fromX] = EmptySquare;
8395         board[toY][toX] = king;
8396         board[toY][toX-1] = board[fromY][BOARD_RGHT-1];
8397         board[fromY][BOARD_RGHT-1] = EmptySquare;
8398     } else if (board[fromY][fromX] == king
8399         && fromX != BOARD_LEFT && fromX != BOARD_RGHT-1 // [HGM] cylinder */
8400                && toY == fromY && toX < fromX-1) {
8401         board[fromY][fromX] = EmptySquare;
8402         board[toY][toX] = king;
8403         board[toY][toX+1] = board[fromY][BOARD_LEFT];
8404         board[fromY][BOARD_LEFT] = EmptySquare;
8405     } else if (board[fromY][fromX] == WhitePawn
8406                && toY >= BOARD_HEIGHT-promoRank
8407                && gameInfo.variant != VariantXiangqi
8408                ) {
8409         /* white pawn promotion */
8410         board[toY][toX] = CharToPiece(ToUpper(promoChar));
8411         if (board[toY][toX] == EmptySquare) {
8412             board[toY][toX] = WhiteQueen;
8413         }
8414         if(gameInfo.variant==VariantBughouse ||
8415            gameInfo.variant==VariantCrazyhouse) /* [HGM] use shadow piece */
8416             board[toY][toX] = (ChessSquare) (PROMOTED board[toY][toX]);
8417         board[fromY][fromX] = EmptySquare;
8418     } else if ((fromY == BOARD_HEIGHT-4)
8419                && (toX != fromX)
8420                && gameInfo.variant != VariantXiangqi
8421                && gameInfo.variant != VariantBerolina
8422                && (board[fromY][fromX] == WhitePawn)
8423                && (board[toY][toX] == EmptySquare)) {
8424         board[fromY][fromX] = EmptySquare;
8425         board[toY][toX] = WhitePawn;
8426         captured = board[toY - 1][toX];
8427         board[toY - 1][toX] = EmptySquare;
8428     } else if ((fromY == BOARD_HEIGHT-4)
8429                && (toX == fromX)
8430                && gameInfo.variant == VariantBerolina
8431                && (board[fromY][fromX] == WhitePawn)
8432                && (board[toY][toX] == EmptySquare)) {
8433         board[fromY][fromX] = EmptySquare;
8434         board[toY][toX] = WhitePawn;
8435         if(oldEP & EP_BEROLIN_A) {
8436                 captured = board[fromY][fromX-1];
8437                 board[fromY][fromX-1] = EmptySquare;
8438         }else{  captured = board[fromY][fromX+1];
8439                 board[fromY][fromX+1] = EmptySquare;
8440         }
8441     } else if (board[fromY][fromX] == king
8442         && fromX != BOARD_LEFT && fromX != BOARD_RGHT-1 // [HGM] cylinder */
8443                && toY == fromY && toX > fromX+1) {
8444         board[fromY][fromX] = EmptySquare;
8445         board[toY][toX] = king;
8446         board[toY][toX-1] = board[fromY][BOARD_RGHT-1];
8447         board[fromY][BOARD_RGHT-1] = EmptySquare;
8448     } else if (board[fromY][fromX] == king
8449         && fromX != BOARD_LEFT && fromX != BOARD_RGHT-1 // [HGM] cylinder */
8450                && toY == fromY && toX < fromX-1) {
8451         board[fromY][fromX] = EmptySquare;
8452         board[toY][toX] = king;
8453         board[toY][toX+1] = board[fromY][BOARD_LEFT];
8454         board[fromY][BOARD_LEFT] = EmptySquare;
8455     } else if (fromY == 7 && fromX == 3
8456                && board[fromY][fromX] == BlackKing
8457                && toY == 7 && toX == 5) {
8458         board[fromY][fromX] = EmptySquare;
8459         board[toY][toX] = BlackKing;
8460         board[fromY][7] = EmptySquare;
8461         board[toY][4] = BlackRook;
8462     } else if (fromY == 7 && fromX == 3
8463                && board[fromY][fromX] == BlackKing
8464                && toY == 7 && toX == 1) {
8465         board[fromY][fromX] = EmptySquare;
8466         board[toY][toX] = BlackKing;
8467         board[fromY][0] = EmptySquare;
8468         board[toY][2] = BlackRook;
8469     } else if (board[fromY][fromX] == BlackPawn
8470                && toY < promoRank
8471                && gameInfo.variant != VariantXiangqi
8472                ) {
8473         /* black pawn promotion */
8474         board[toY][toX] = CharToPiece(ToLower(promoChar));
8475         if (board[toY][toX] == EmptySquare) {
8476             board[toY][toX] = BlackQueen;
8477         }
8478         if(gameInfo.variant==VariantBughouse ||
8479            gameInfo.variant==VariantCrazyhouse) /* [HGM] use shadow piece */
8480             board[toY][toX] = (ChessSquare) (PROMOTED board[toY][toX]);
8481         board[fromY][fromX] = EmptySquare;
8482     } else if ((fromY == 3)
8483                && (toX != fromX)
8484                && gameInfo.variant != VariantXiangqi
8485                && gameInfo.variant != VariantBerolina
8486                && (board[fromY][fromX] == BlackPawn)
8487                && (board[toY][toX] == EmptySquare)) {
8488         board[fromY][fromX] = EmptySquare;
8489         board[toY][toX] = BlackPawn;
8490         captured = board[toY + 1][toX];
8491         board[toY + 1][toX] = EmptySquare;
8492     } else if ((fromY == 3)
8493                && (toX == fromX)
8494                && gameInfo.variant == VariantBerolina
8495                && (board[fromY][fromX] == BlackPawn)
8496                && (board[toY][toX] == EmptySquare)) {
8497         board[fromY][fromX] = EmptySquare;
8498         board[toY][toX] = BlackPawn;
8499         if(oldEP & EP_BEROLIN_A) {
8500                 captured = board[fromY][fromX-1];
8501                 board[fromY][fromX-1] = EmptySquare;
8502         }else{  captured = board[fromY][fromX+1];
8503                 board[fromY][fromX+1] = EmptySquare;
8504         }
8505     } else {
8506         board[toY][toX] = board[fromY][fromX];
8507         board[fromY][fromX] = EmptySquare;
8508     }
8509
8510     /* [HGM] now we promote for Shogi, if needed */
8511     if(gameInfo.variant == VariantShogi && promoChar == 'q')
8512         board[toY][toX] = (ChessSquare) (PROMOTED piece);
8513   }
8514
8515     if (gameInfo.holdingsWidth != 0) {
8516
8517       /* !!A lot more code needs to be written to support holdings  */
8518       /* [HGM] OK, so I have written it. Holdings are stored in the */
8519       /* penultimate board files, so they are automaticlly stored   */
8520       /* in the game history.                                       */
8521       if (fromY == DROP_RANK) {
8522         /* Delete from holdings, by decreasing count */
8523         /* and erasing image if necessary            */
8524         p = (int) fromX;
8525         if(p < (int) BlackPawn) { /* white drop */
8526              p -= (int)WhitePawn;
8527                  p = PieceToNumber((ChessSquare)p);
8528              if(p >= gameInfo.holdingsSize) p = 0;
8529              if(--board[p][BOARD_WIDTH-2] <= 0)
8530                   board[p][BOARD_WIDTH-1] = EmptySquare;
8531              if((int)board[p][BOARD_WIDTH-2] < 0)
8532                         board[p][BOARD_WIDTH-2] = 0;
8533         } else {                  /* black drop */
8534              p -= (int)BlackPawn;
8535                  p = PieceToNumber((ChessSquare)p);
8536              if(p >= gameInfo.holdingsSize) p = 0;
8537              if(--board[BOARD_HEIGHT-1-p][1] <= 0)
8538                   board[BOARD_HEIGHT-1-p][0] = EmptySquare;
8539              if((int)board[BOARD_HEIGHT-1-p][1] < 0)
8540                         board[BOARD_HEIGHT-1-p][1] = 0;
8541         }
8542       }
8543       if (captured != EmptySquare && gameInfo.holdingsSize > 0
8544           && gameInfo.variant != VariantBughouse        ) {
8545         /* [HGM] holdings: Add to holdings, if holdings exist */
8546         if(gameInfo.variant == VariantSuper || gameInfo.variant == VariantGreat) { 
8547                 // [HGM] superchess: suppress flipping color of captured pieces by reverse pre-flip
8548                 captured = (int) captured >= (int) BlackPawn ? BLACK_TO_WHITE captured : WHITE_TO_BLACK captured;
8549         }
8550         p = (int) captured;
8551         if (p >= (int) BlackPawn) {
8552           p -= (int)BlackPawn;
8553           if(gameInfo.variant == VariantShogi && DEMOTED p >= 0) {
8554                   /* in Shogi restore piece to its original  first */
8555                   captured = (ChessSquare) (DEMOTED captured);
8556                   p = DEMOTED p;
8557           }
8558           p = PieceToNumber((ChessSquare)p);
8559           if(p >= gameInfo.holdingsSize) { p = 0; captured = BlackPawn; }
8560           board[p][BOARD_WIDTH-2]++;
8561           board[p][BOARD_WIDTH-1] = BLACK_TO_WHITE captured;
8562         } else {
8563           p -= (int)WhitePawn;
8564           if(gameInfo.variant == VariantShogi && DEMOTED p >= 0) {
8565                   captured = (ChessSquare) (DEMOTED captured);
8566                   p = DEMOTED p;
8567           }
8568           p = PieceToNumber((ChessSquare)p);
8569           if(p >= gameInfo.holdingsSize) { p = 0; captured = WhitePawn; }
8570           board[BOARD_HEIGHT-1-p][1]++;
8571           board[BOARD_HEIGHT-1-p][0] = WHITE_TO_BLACK captured;
8572         }
8573       }
8574     } else if (gameInfo.variant == VariantAtomic) {
8575       if (captured != EmptySquare) {
8576         int y, x;
8577         for (y = toY-1; y <= toY+1; y++) {
8578           for (x = toX-1; x <= toX+1; x++) {
8579             if (y >= 0 && y < BOARD_HEIGHT && x >= BOARD_LEFT && x < BOARD_RGHT &&
8580                 board[y][x] != WhitePawn && board[y][x] != BlackPawn) {
8581               board[y][x] = EmptySquare;
8582             }
8583           }
8584         }
8585         board[toY][toX] = EmptySquare;
8586       }
8587     }
8588     if(gameInfo.variant == VariantShogi && promoChar != NULLCHAR && promoChar != '=') {
8589         /* [HGM] Shogi promotions */
8590         board[toY][toX] = (ChessSquare) (PROMOTED piece);
8591     }
8592
8593     if((gameInfo.variant == VariantSuper || gameInfo.variant == VariantGreat) 
8594                 && promoChar != NULLCHAR && gameInfo.holdingsSize) { 
8595         // [HGM] superchess: take promotion piece out of holdings
8596         int k = PieceToNumber(CharToPiece(ToUpper(promoChar)));
8597         if((int)piece < (int)BlackPawn) { // determine stm from piece color
8598             if(!--board[k][BOARD_WIDTH-2])
8599                 board[k][BOARD_WIDTH-1] = EmptySquare;
8600         } else {
8601             if(!--board[BOARD_HEIGHT-1-k][1])
8602                 board[BOARD_HEIGHT-1-k][0] = EmptySquare;
8603         }
8604     }
8605
8606 }
8607
8608 /* Updates forwardMostMove */
8609 void
8610 MakeMove(fromX, fromY, toX, toY, promoChar)
8611      int fromX, fromY, toX, toY;
8612      int promoChar;
8613 {
8614 //    forwardMostMove++; // [HGM] bare: moved downstream
8615
8616     if(serverMoves != NULL) { /* [HGM] write moves on file for broadcasting (should be separate routine, really) */
8617         int timeLeft; static int lastLoadFlag=0; int king, piece;
8618         piece = boards[forwardMostMove][fromY][fromX];
8619         king = piece < (int) BlackPawn ? WhiteKing : BlackKing;
8620         if(gameInfo.variant == VariantKnightmate)
8621             king += (int) WhiteUnicorn - (int) WhiteKing;
8622         if(forwardMostMove == 0) {
8623             if(blackPlaysFirst) 
8624                 fprintf(serverMoves, "%s;", second.tidy);
8625             fprintf(serverMoves, "%s;", first.tidy);
8626             if(!blackPlaysFirst) 
8627                 fprintf(serverMoves, "%s;", second.tidy);
8628         } else fprintf(serverMoves, loadFlag|lastLoadFlag ? ":" : ";");
8629         lastLoadFlag = loadFlag;
8630         // print base move
8631         fprintf(serverMoves, "%c%c:%c%c", AAA+fromX, ONE+fromY, AAA+toX, ONE+toY);
8632         // print castling suffix
8633         if( toY == fromY && piece == king ) {
8634             if(toX-fromX > 1)
8635                 fprintf(serverMoves, ":%c%c:%c%c", AAA+BOARD_RGHT-1, ONE+fromY, AAA+toX-1,ONE+toY);
8636             if(fromX-toX >1)
8637                 fprintf(serverMoves, ":%c%c:%c%c", AAA+BOARD_LEFT, ONE+fromY, AAA+toX+1,ONE+toY);
8638         }
8639         // e.p. suffix
8640         if( (boards[forwardMostMove][fromY][fromX] == WhitePawn ||
8641              boards[forwardMostMove][fromY][fromX] == BlackPawn   ) &&
8642              boards[forwardMostMove][toY][toX] == EmptySquare
8643              && fromX != toX && fromY != toY)
8644                 fprintf(serverMoves, ":%c%c:%c%c", AAA+fromX, ONE+fromY, AAA+toX, ONE+fromY);
8645         // promotion suffix
8646         if(promoChar != NULLCHAR)
8647                 fprintf(serverMoves, ":%c:%c%c", promoChar, AAA+toX, ONE+toY);
8648         if(!loadFlag) {
8649             fprintf(serverMoves, "/%d/%d",
8650                pvInfoList[forwardMostMove].depth, pvInfoList[forwardMostMove].score);
8651             if(forwardMostMove+1 & 1) timeLeft = whiteTimeRemaining/1000;
8652             else                      timeLeft = blackTimeRemaining/1000;
8653             fprintf(serverMoves, "/%d", timeLeft);
8654         }
8655         fflush(serverMoves);
8656     }
8657
8658     if (forwardMostMove+1 > framePtr) { // [HGM] vari: do not run into saved variations
8659       DisplayFatalError(_("Game too long; increase MAX_MOVES and recompile"),
8660                         0, 1);
8661       return;
8662     }
8663     UnLoadPV(); // [HGM] pv: if we are looking at a PV, abort this
8664     if (commentList[forwardMostMove+1] != NULL) {
8665         free(commentList[forwardMostMove+1]);
8666         commentList[forwardMostMove+1] = NULL;
8667     }
8668     CopyBoard(boards[forwardMostMove+1], boards[forwardMostMove]);
8669     ApplyMove(fromX, fromY, toX, toY, promoChar, boards[forwardMostMove+1]);
8670     // forwardMostMove++; // [HGM] bare: moved to after ApplyMove, to make sure clock interrupt finds complete board
8671     SwitchClocks(forwardMostMove+1); // [HGM] race: incrementing move nr inside
8672     timeRemaining[0][forwardMostMove] = whiteTimeRemaining;
8673     timeRemaining[1][forwardMostMove] = blackTimeRemaining;
8674     gameInfo.result = GameUnfinished;
8675     if (gameInfo.resultDetails != NULL) {
8676         free(gameInfo.resultDetails);
8677         gameInfo.resultDetails = NULL;
8678     }
8679     CoordsToComputerAlgebraic(fromY, fromX, toY, toX, promoChar,
8680                               moveList[forwardMostMove - 1]);
8681     (void) CoordsToAlgebraic(boards[forwardMostMove - 1],
8682                              PosFlags(forwardMostMove - 1),
8683                              fromY, fromX, toY, toX, promoChar,
8684                              parseList[forwardMostMove - 1]);
8685     switch (MateTest(boards[forwardMostMove], PosFlags(forwardMostMove)) ) {
8686       case MT_NONE:
8687       case MT_STALEMATE:
8688       default:
8689         break;
8690       case MT_CHECK:
8691         if(gameInfo.variant != VariantShogi)
8692             strcat(parseList[forwardMostMove - 1], "+");
8693         break;
8694       case MT_CHECKMATE:
8695       case MT_STAINMATE:
8696         strcat(parseList[forwardMostMove - 1], "#");
8697         break;
8698     }
8699     if (appData.debugMode) {
8700         fprintf(debugFP, "move: %s, parse: %s (%c)\n", moveList[forwardMostMove-1], parseList[forwardMostMove-1], moveList[forwardMostMove-1][4]);
8701     }
8702
8703 }
8704
8705 /* Updates currentMove if not pausing */
8706 void
8707 ShowMove(fromX, fromY, toX, toY)
8708 {
8709     int instant = (gameMode == PlayFromGameFile) ?
8710         (matchMode || (appData.timeDelay == 0 && !pausing)) : pausing;
8711     if(appData.noGUI) return;
8712     if (!pausing || gameMode == PlayFromGameFile || gameMode == AnalyzeFile) {
8713         if (!instant) {
8714             if (forwardMostMove == currentMove + 1) {
8715                 AnimateMove(boards[forwardMostMove - 1],
8716                             fromX, fromY, toX, toY);
8717             }
8718             if (appData.highlightLastMove) {
8719                 SetHighlights(fromX, fromY, toX, toY);
8720             }
8721         }
8722         currentMove = forwardMostMove;
8723     }
8724
8725     if (instant) return;
8726
8727     DisplayMove(currentMove - 1);
8728     DrawPosition(FALSE, boards[currentMove]);
8729     DisplayBothClocks();
8730     HistorySet(parseList,backwardMostMove,forwardMostMove,currentMove-1);
8731 }
8732
8733 void SendEgtPath(ChessProgramState *cps)
8734 {       /* [HGM] EGT: match formats given in feature with those given by user, and send info for each match */
8735         char buf[MSG_SIZ], name[MSG_SIZ], *p;
8736
8737         if((p = cps->egtFormats) == NULL || appData.egtFormats == NULL) return;
8738
8739         while(*p) {
8740             char c, *q = name+1, *r, *s;
8741
8742             name[0] = ','; // extract next format name from feature and copy with prefixed ','
8743             while(*p && *p != ',') *q++ = *p++;
8744             *q++ = ':'; *q = 0;
8745             if( appData.defaultPathEGTB && appData.defaultPathEGTB[0] && 
8746                 strcmp(name, ",nalimov:") == 0 ) {
8747                 // take nalimov path from the menu-changeable option first, if it is defined
8748                 sprintf(buf, "egtpath nalimov %s\n", appData.defaultPathEGTB);
8749                 SendToProgram(buf,cps);     // send egtbpath command for nalimov
8750             } else
8751             if( (s = StrStr(appData.egtFormats, name+1)) == appData.egtFormats ||
8752                 (s = StrStr(appData.egtFormats, name)) != NULL) {
8753                 // format name occurs amongst user-supplied formats, at beginning or immediately after comma
8754                 s = r = StrStr(s, ":") + 1; // beginning of path info
8755                 while(*r && *r != ',') r++; // path info is everything upto next ';' or end of string
8756                 c = *r; *r = 0;             // temporarily null-terminate path info
8757                     *--q = 0;               // strip of trailig ':' from name
8758                     sprintf(buf, "egtpath %s %s\n", name+1, s);
8759                 *r = c;
8760                 SendToProgram(buf,cps);     // send egtbpath command for this format
8761             }
8762             if(*p == ',') p++; // read away comma to position for next format name
8763         }
8764 }
8765
8766 void
8767 InitChessProgram(cps, setup)
8768      ChessProgramState *cps;
8769      int setup; /* [HGM] needed to setup FRC opening position */
8770 {
8771     char buf[MSG_SIZ], b[MSG_SIZ]; int overruled;
8772     if (appData.noChessProgram) return;
8773     hintRequested = FALSE;
8774     bookRequested = FALSE;
8775
8776     /* [HGM] some new WB protocol commands to configure engine are sent now, if engine supports them */
8777     /*       moved to before sending initstring in 4.3.15, so Polyglot can delay UCI 'isready' to recepton of 'new' */
8778     if(cps->memSize) { /* [HGM] memory */
8779         sprintf(buf, "memory %d\n", appData.defaultHashSize + appData.defaultCacheSizeEGTB);
8780         SendToProgram(buf, cps);
8781     }
8782     SendEgtPath(cps); /* [HGM] EGT */
8783     if(cps->maxCores) { /* [HGM] SMP: (protocol specified must be last settings command before new!) */
8784         sprintf(buf, "cores %d\n", appData.smpCores);
8785         SendToProgram(buf, cps);
8786     }
8787
8788     SendToProgram(cps->initString, cps);
8789     if (gameInfo.variant != VariantNormal &&
8790         gameInfo.variant != VariantLoadable
8791         /* [HGM] also send variant if board size non-standard */
8792         || gameInfo.boardWidth != 8 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 0
8793                                             ) {
8794       char *v = VariantName(gameInfo.variant);
8795       if (cps->protocolVersion != 1 && StrStr(cps->variants, v) == NULL) {
8796         /* [HGM] in protocol 1 we have to assume all variants valid */
8797         sprintf(buf, _("Variant %s not supported by %s"), v, cps->tidy);
8798         DisplayFatalError(buf, 0, 1);
8799         return;
8800       }
8801
8802       /* [HGM] make prefix for non-standard board size. Awkward testing... */
8803       overruled = gameInfo.boardWidth != 8 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 0;
8804       if( gameInfo.variant == VariantXiangqi )
8805            overruled = gameInfo.boardWidth != 9 || gameInfo.boardHeight != 10 || gameInfo.holdingsSize != 0;
8806       if( gameInfo.variant == VariantShogi )
8807            overruled = gameInfo.boardWidth != 9 || gameInfo.boardHeight != 9 || gameInfo.holdingsSize != 7;
8808       if( gameInfo.variant == VariantBughouse || gameInfo.variant == VariantCrazyhouse )
8809            overruled = gameInfo.boardWidth != 8 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 5;
8810       if( gameInfo.variant == VariantCapablanca || gameInfo.variant == VariantCapaRandom || 
8811                                gameInfo.variant == VariantGothic  || gameInfo.variant == VariantFalcon )
8812            overruled = gameInfo.boardWidth != 10 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 0;
8813       if( gameInfo.variant == VariantCourier )
8814            overruled = gameInfo.boardWidth != 12 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 0;
8815       if( gameInfo.variant == VariantSuper )
8816            overruled = gameInfo.boardWidth != 8 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 8;
8817       if( gameInfo.variant == VariantGreat )
8818            overruled = gameInfo.boardWidth != 10 || gameInfo.boardHeight != 8 || gameInfo.holdingsSize != 8;
8819
8820       if(overruled) {
8821            sprintf(b, "%dx%d+%d_%s", gameInfo.boardWidth, gameInfo.boardHeight, 
8822                                gameInfo.holdingsSize, VariantName(gameInfo.variant)); // cook up sized variant name
8823            /* [HGM] varsize: try first if this defiant size variant is specifically known */
8824            if(StrStr(cps->variants, b) == NULL) { 
8825                // specific sized variant not known, check if general sizing allowed
8826                if (cps->protocolVersion != 1) { // for protocol 1 we cannot check and hope for the best
8827                    if(StrStr(cps->variants, "boardsize") == NULL) {
8828                        sprintf(buf, "Board size %dx%d+%d not supported by %s",
8829                             gameInfo.boardWidth, gameInfo.boardHeight, gameInfo.holdingsSize, cps->tidy);
8830                        DisplayFatalError(buf, 0, 1);
8831                        return;
8832                    }
8833                    /* [HGM] here we really should compare with the maximum supported board size */
8834                }
8835            }
8836       } else sprintf(b, "%s", VariantName(gameInfo.variant));
8837       sprintf(buf, "variant %s\n", b);
8838       SendToProgram(buf, cps);
8839     }
8840     currentlyInitializedVariant = gameInfo.variant;
8841
8842     /* [HGM] send opening position in FRC to first engine */
8843     if(setup) {
8844           SendToProgram("force\n", cps);
8845           SendBoard(cps, 0);
8846           /* engine is now in force mode! Set flag to wake it up after first move. */
8847           setboardSpoiledMachineBlack = 1;
8848     }
8849
8850     if (cps->sendICS) {
8851       snprintf(buf, sizeof(buf), "ics %s\n", appData.icsActive ? appData.icsHost : "-");
8852       SendToProgram(buf, cps);
8853     }
8854     cps->maybeThinking = FALSE;
8855     cps->offeredDraw = 0;
8856     if (!appData.icsActive) {
8857         SendTimeControl(cps, movesPerSession, timeControl,
8858                         timeIncrement, appData.searchDepth,
8859                         searchTime);
8860     }
8861     if (appData.showThinking 
8862         // [HGM] thinking: four options require thinking output to be sent
8863         || !appData.hideThinkingFromHuman || appData.adjudicateLossThreshold != 0 || EngineOutputIsUp()
8864                                 ) {
8865         SendToProgram("post\n", cps);
8866     }
8867     SendToProgram("hard\n", cps);
8868     if (!appData.ponderNextMove) {
8869         /* Warning: "easy" is a toggle in GNU Chess, so don't send
8870            it without being sure what state we are in first.  "hard"
8871            is not a toggle, so that one is OK.
8872          */
8873         SendToProgram("easy\n", cps);
8874     }
8875     if (cps->usePing) {
8876       sprintf(buf, "ping %d\n", ++cps->lastPing);
8877       SendToProgram(buf, cps);
8878     }
8879     cps->initDone = TRUE;
8880 }   
8881
8882
8883 void
8884 StartChessProgram(cps)
8885      ChessProgramState *cps;
8886 {
8887     char buf[MSG_SIZ];
8888     int err;
8889
8890     if (appData.noChessProgram) return;
8891     cps->initDone = FALSE;
8892
8893     if (strcmp(cps->host, "localhost") == 0) {
8894         err = StartChildProcess(cps->program, cps->dir, &cps->pr);
8895     } else if (*appData.remoteShell == NULLCHAR) {
8896         err = OpenRcmd(cps->host, appData.remoteUser, cps->program, &cps->pr);
8897     } else {
8898         if (*appData.remoteUser == NULLCHAR) {
8899           snprintf(buf, sizeof(buf), "%s %s %s", appData.remoteShell, cps->host,
8900                     cps->program);
8901         } else {
8902           snprintf(buf, sizeof(buf), "%s %s -l %s %s", appData.remoteShell,
8903                     cps->host, appData.remoteUser, cps->program);
8904         }
8905         err = StartChildProcess(buf, "", &cps->pr);
8906     }
8907     
8908     if (err != 0) {
8909         sprintf(buf, _("Startup failure on '%s'"), cps->program);
8910         DisplayFatalError(buf, err, 1);
8911         cps->pr = NoProc;
8912         cps->isr = NULL;
8913         return;
8914     }
8915     
8916     cps->isr = AddInputSource(cps->pr, TRUE, ReceiveFromProgram, cps);
8917     if (cps->protocolVersion > 1) {
8918       sprintf(buf, "xboard\nprotover %d\n", cps->protocolVersion);
8919       cps->nrOptions = 0; // [HGM] options: clear all engine-specific options
8920       cps->comboCnt = 0;  //                and values of combo boxes
8921       SendToProgram(buf, cps);
8922     } else {
8923       SendToProgram("xboard\n", cps);
8924     }
8925 }
8926
8927
8928 void
8929 TwoMachinesEventIfReady P((void))
8930 {
8931   if (first.lastPing != first.lastPong) {
8932     DisplayMessage("", _("Waiting for first chess program"));
8933     ScheduleDelayedEvent(TwoMachinesEventIfReady, 10); // [HGM] fast: lowered from 1000
8934     return;
8935   }
8936   if (second.lastPing != second.lastPong) {
8937     DisplayMessage("", _("Waiting for second chess program"));
8938     ScheduleDelayedEvent(TwoMachinesEventIfReady, 10); // [HGM] fast: lowered from 1000
8939     return;
8940   }
8941   ThawUI();
8942   TwoMachinesEvent();
8943 }
8944
8945 void
8946 NextMatchGame P((void))
8947 {
8948     int index; /* [HGM] autoinc: step load index during match */
8949     Reset(FALSE, TRUE);
8950     if (*appData.loadGameFile != NULLCHAR) {
8951         index = appData.loadGameIndex;
8952         if(index < 0) { // [HGM] autoinc
8953             lastIndex = index = (index == -2 && first.twoMachinesColor[0] == 'b') ? lastIndex : lastIndex+1;
8954             if(appData.rewindIndex > 0 && index > appData.rewindIndex) lastIndex = index = 1;
8955         } 
8956         LoadGameFromFile(appData.loadGameFile,
8957                          index,
8958                          appData.loadGameFile, FALSE);
8959     } else if (*appData.loadPositionFile != NULLCHAR) {
8960         index = appData.loadPositionIndex;
8961         if(index < 0) { // [HGM] autoinc
8962             lastIndex = index = (index == -2 && first.twoMachinesColor[0] == 'b') ? lastIndex : lastIndex+1;
8963             if(appData.rewindIndex > 0 && index > appData.rewindIndex) lastIndex = index = 1;
8964         } 
8965         LoadPositionFromFile(appData.loadPositionFile,
8966                              index,
8967                              appData.loadPositionFile);
8968     }
8969     TwoMachinesEventIfReady();
8970 }
8971
8972 void UserAdjudicationEvent( int result )
8973 {
8974     ChessMove gameResult = GameIsDrawn;
8975
8976     if( result > 0 ) {
8977         gameResult = WhiteWins;
8978     }
8979     else if( result < 0 ) {
8980         gameResult = BlackWins;
8981     }
8982
8983     if( gameMode == TwoMachinesPlay ) {
8984         GameEnds( gameResult, "User adjudication", GE_XBOARD );
8985     }
8986 }
8987
8988
8989 // [HGM] save: calculate checksum of game to make games easily identifiable
8990 int StringCheckSum(char *s)
8991 {
8992         int i = 0;
8993         if(s==NULL) return 0;
8994         while(*s) i = i*259 + *s++;
8995         return i;
8996 }
8997
8998 int GameCheckSum()
8999 {
9000         int i, sum=0;
9001         for(i=backwardMostMove; i<forwardMostMove; i++) {
9002                 sum += pvInfoList[i].depth;
9003                 sum += StringCheckSum(parseList[i]);
9004                 sum += StringCheckSum(commentList[i]);
9005                 sum *= 261;
9006         }
9007         if(i>1 && sum==0) sum++; // make sure never zero for non-empty game
9008         return sum + StringCheckSum(commentList[i]);
9009 } // end of save patch
9010
9011 void
9012 GameEnds(result, resultDetails, whosays)
9013      ChessMove result;
9014      char *resultDetails;
9015      int whosays;
9016 {
9017     GameMode nextGameMode;
9018     int isIcsGame;
9019     char buf[MSG_SIZ], popupRequested = 0;
9020
9021     if(endingGame) return; /* [HGM] crash: forbid recursion */
9022     endingGame = 1;
9023     if(twoBoards) { // [HGM] dual: switch back to one board
9024         twoBoards = partnerUp = 0; InitDrawingSizes(-2, 0);
9025         DrawPosition(TRUE, partnerBoard); // observed game becomes foreground
9026     }
9027     if (appData.debugMode) {
9028       fprintf(debugFP, "GameEnds(%d, %s, %d)\n",
9029               result, resultDetails ? resultDetails : "(null)", whosays);
9030     }
9031
9032     fromX = fromY = -1; // [HGM] abort any move the user is entering.
9033
9034     if (appData.icsActive && (whosays == GE_ENGINE || whosays >= GE_ENGINE1)) {
9035         /* If we are playing on ICS, the server decides when the
9036            game is over, but the engine can offer to draw, claim 
9037            a draw, or resign. 
9038          */
9039 #if ZIPPY
9040         if (appData.zippyPlay && first.initDone) {
9041             if (result == GameIsDrawn) {
9042                 /* In case draw still needs to be claimed */
9043                 SendToICS(ics_prefix);
9044                 SendToICS("draw\n");
9045             } else if (StrCaseStr(resultDetails, "resign")) {
9046                 SendToICS(ics_prefix);
9047                 SendToICS("resign\n");
9048             }
9049         }
9050 #endif
9051         endingGame = 0; /* [HGM] crash */
9052         return;
9053     }
9054
9055     /* If we're loading the game from a file, stop */
9056     if (whosays == GE_FILE) {
9057       (void) StopLoadGameTimer();
9058       gameFileFP = NULL;
9059     }
9060
9061     /* Cancel draw offers */
9062     first.offeredDraw = second.offeredDraw = 0;
9063
9064     /* If this is an ICS game, only ICS can really say it's done;
9065        if not, anyone can. */
9066     isIcsGame = (gameMode == IcsPlayingWhite || 
9067                  gameMode == IcsPlayingBlack || 
9068                  gameMode == IcsObserving    || 
9069                  gameMode == IcsExamining);
9070
9071     if (!isIcsGame || whosays == GE_ICS) {
9072         /* OK -- not an ICS game, or ICS said it was done */
9073         StopClocks();
9074         if (!isIcsGame && !appData.noChessProgram) 
9075           SetUserThinkingEnables();
9076     
9077         /* [HGM] if a machine claims the game end we verify this claim */
9078         if(gameMode == TwoMachinesPlay && appData.testClaims) {
9079             if(appData.testLegality && whosays >= GE_ENGINE1 ) {
9080                 char claimer;
9081                 ChessMove trueResult = (ChessMove) -1;
9082
9083                 claimer = whosays == GE_ENGINE1 ?      /* color of claimer */
9084                                             first.twoMachinesColor[0] :
9085                                             second.twoMachinesColor[0] ;
9086
9087                 // [HGM] losers: because the logic is becoming a bit hairy, determine true result first
9088                 if((signed char)boards[forwardMostMove][EP_STATUS] == EP_CHECKMATE) {
9089                     /* [HGM] verify: engine mate claims accepted if they were flagged */
9090                     trueResult = WhiteOnMove(forwardMostMove) ? BlackWins : WhiteWins;
9091                 } else
9092                 if((signed char)boards[forwardMostMove][EP_STATUS] == EP_WINS) { // added code for games where being mated is a win
9093                     /* [HGM] verify: engine mate claims accepted if they were flagged */
9094                     trueResult = WhiteOnMove(forwardMostMove) ? WhiteWins : BlackWins;
9095                 } else
9096                 if((signed char)boards[forwardMostMove][EP_STATUS] == EP_STALEMATE) { // only used to indicate draws now
9097                     trueResult = GameIsDrawn; // default; in variants where stalemate loses, Status is CHECKMATE
9098                 }
9099
9100                 // now verify win claims, but not in drop games, as we don't understand those yet
9101                 if( (gameInfo.holdingsWidth == 0 || gameInfo.variant == VariantSuper
9102                                                  || gameInfo.variant == VariantGreat) &&
9103                     (result == WhiteWins && claimer == 'w' ||
9104                      result == BlackWins && claimer == 'b'   ) ) { // case to verify: engine claims own win
9105                       if (appData.debugMode) {
9106                         fprintf(debugFP, "result=%d sp=%d move=%d\n",
9107                                 result, (signed char)boards[forwardMostMove][EP_STATUS], forwardMostMove);
9108                       }
9109                       if(result != trueResult) {
9110                               sprintf(buf, "False win claim: '%s'", resultDetails);
9111                               result = claimer == 'w' ? BlackWins : WhiteWins;
9112                               resultDetails = buf;
9113                       }
9114                 } else
9115                 if( result == GameIsDrawn && (signed char)boards[forwardMostMove][EP_STATUS] > EP_DRAWS
9116                     && (forwardMostMove <= backwardMostMove ||
9117                         (signed char)boards[forwardMostMove-1][EP_STATUS] > EP_DRAWS ||
9118                         (claimer=='b')==(forwardMostMove&1))
9119                                                                                   ) {
9120                       /* [HGM] verify: draws that were not flagged are false claims */
9121                       sprintf(buf, "False draw claim: '%s'", resultDetails);
9122                       result = claimer == 'w' ? BlackWins : WhiteWins;
9123                       resultDetails = buf;
9124                 }
9125                 /* (Claiming a loss is accepted no questions asked!) */
9126             }
9127             /* [HGM] bare: don't allow bare King to win */
9128             if((gameInfo.holdingsWidth == 0 || gameInfo.variant == VariantSuper || gameInfo.variant == VariantGreat)
9129                && gameInfo.variant != VariantLosers && gameInfo.variant != VariantGiveaway 
9130                && gameInfo.variant != VariantSuicide // [HGM] losers: except in losers, of course...
9131                && result != GameIsDrawn)
9132             {   int i, j, k=0, color = (result==WhiteWins ? (int)WhitePawn : (int)BlackPawn);
9133                 for(j=BOARD_LEFT; j<BOARD_RGHT; j++) for(i=0; i<BOARD_HEIGHT; i++) {
9134                         int p = (signed char)boards[forwardMostMove][i][j] - color;
9135                         if(p >= 0 && p <= (int)WhiteKing) k++;
9136                 }
9137                 if (appData.debugMode) {
9138                      fprintf(debugFP, "GE(%d, %s, %d) bare king k=%d color=%d\n",
9139                         result, resultDetails ? resultDetails : "(null)", whosays, k, color);
9140                 }
9141                 if(k <= 1) {
9142                         result = GameIsDrawn;
9143                         sprintf(buf, "%s but bare king", resultDetails);
9144                         resultDetails = buf;
9145                 }
9146             }
9147         }
9148
9149
9150         if(serverMoves != NULL && !loadFlag) { char c = '=';
9151             if(result==WhiteWins) c = '+';
9152             if(result==BlackWins) c = '-';
9153             if(resultDetails != NULL)
9154                 fprintf(serverMoves, ";%c;%s\n", c, resultDetails);
9155         }
9156         if (resultDetails != NULL) {
9157             gameInfo.result = result;
9158             gameInfo.resultDetails = StrSave(resultDetails);
9159
9160             /* display last move only if game was not loaded from file */
9161             if ((whosays != GE_FILE) && (currentMove == forwardMostMove))
9162                 DisplayMove(currentMove - 1);
9163     
9164             if (forwardMostMove != 0) {
9165                 if (gameMode != PlayFromGameFile && gameMode != EditGame
9166                     && lastSavedGame != GameCheckSum() // [HGM] save: suppress duplicates
9167                                                                 ) {
9168                     if (*appData.saveGameFile != NULLCHAR) {
9169                         SaveGameToFile(appData.saveGameFile, TRUE);
9170                     } else if (appData.autoSaveGames) {
9171                         AutoSaveGame();
9172                     }
9173                     if (*appData.savePositionFile != NULLCHAR) {
9174                         SavePositionToFile(appData.savePositionFile);
9175                     }
9176                 }
9177             }
9178
9179             /* Tell program how game ended in case it is learning */
9180             /* [HGM] Moved this to after saving the PGN, just in case */
9181             /* engine died and we got here through time loss. In that */
9182             /* case we will get a fatal error writing the pipe, which */
9183             /* would otherwise lose us the PGN.                       */
9184             /* [HGM] crash: not needed anymore, but doesn't hurt;     */
9185             /* output during GameEnds should never be fatal anymore   */
9186             if (gameMode == MachinePlaysWhite ||
9187                 gameMode == MachinePlaysBlack ||
9188                 gameMode == TwoMachinesPlay ||
9189                 gameMode == IcsPlayingWhite ||
9190                 gameMode == IcsPlayingBlack ||
9191                 gameMode == BeginningOfGame) {
9192                 char buf[MSG_SIZ];
9193                 sprintf(buf, "result %s {%s}\n", PGNResult(result),
9194                         resultDetails);
9195                 if (first.pr != NoProc) {
9196                     SendToProgram(buf, &first);
9197                 }
9198                 if (second.pr != NoProc &&
9199                     gameMode == TwoMachinesPlay) {
9200                     SendToProgram(buf, &second);
9201                 }
9202             }
9203         }
9204
9205         if (appData.icsActive) {
9206             if (appData.quietPlay &&
9207                 (gameMode == IcsPlayingWhite ||
9208                  gameMode == IcsPlayingBlack)) {
9209                 SendToICS(ics_prefix);
9210                 SendToICS("set shout 1\n");
9211             }
9212             nextGameMode = IcsIdle;
9213             ics_user_moved = FALSE;
9214             /* clean up premove.  It's ugly when the game has ended and the
9215              * premove highlights are still on the board.
9216              */
9217             if (gotPremove) {
9218               gotPremove = FALSE;
9219               ClearPremoveHighlights();
9220               DrawPosition(FALSE, boards[currentMove]);
9221             }
9222             if (whosays == GE_ICS) {
9223                 switch (result) {
9224                 case WhiteWins:
9225                     if (gameMode == IcsPlayingWhite)
9226                         PlayIcsWinSound();
9227                     else if(gameMode == IcsPlayingBlack)
9228                         PlayIcsLossSound();
9229                     break;
9230                 case BlackWins:
9231                     if (gameMode == IcsPlayingBlack)
9232                         PlayIcsWinSound();
9233                     else if(gameMode == IcsPlayingWhite)
9234                         PlayIcsLossSound();
9235                     break;
9236                 case GameIsDrawn:
9237                     PlayIcsDrawSound();
9238                     break;
9239                 default:
9240                     PlayIcsUnfinishedSound();
9241                 }
9242             }
9243         } else if (gameMode == EditGame ||
9244                    gameMode == PlayFromGameFile || 
9245                    gameMode == AnalyzeMode || 
9246                    gameMode == AnalyzeFile) {
9247             nextGameMode = gameMode;
9248         } else {
9249             nextGameMode = EndOfGame;
9250         }
9251         pausing = FALSE;
9252         ModeHighlight();
9253     } else {
9254         nextGameMode = gameMode;
9255     }
9256
9257     if (appData.noChessProgram) {
9258         gameMode = nextGameMode;
9259         ModeHighlight();
9260         endingGame = 0; /* [HGM] crash */
9261         return;
9262     }
9263
9264     if (first.reuse) {
9265         /* Put first chess program into idle state */
9266         if (first.pr != NoProc &&
9267             (gameMode == MachinePlaysWhite ||
9268              gameMode == MachinePlaysBlack ||
9269              gameMode == TwoMachinesPlay ||
9270              gameMode == IcsPlayingWhite ||
9271              gameMode == IcsPlayingBlack ||
9272              gameMode == BeginningOfGame)) {
9273             SendToProgram("force\n", &first);
9274             if (first.usePing) {
9275               char buf[MSG_SIZ];
9276               sprintf(buf, "ping %d\n", ++first.lastPing);
9277               SendToProgram(buf, &first);
9278             }
9279         }
9280     } else if (result != GameUnfinished || nextGameMode == IcsIdle) {
9281         /* Kill off first chess program */
9282         if (first.isr != NULL)
9283           RemoveInputSource(first.isr);
9284         first.isr = NULL;
9285     
9286         if (first.pr != NoProc) {
9287             ExitAnalyzeMode();
9288             DoSleep( appData.delayBeforeQuit );
9289             SendToProgram("quit\n", &first);
9290             DoSleep( appData.delayAfterQuit );
9291             DestroyChildProcess(first.pr, first.useSigterm);
9292         }
9293         first.pr = NoProc;
9294     }
9295     if (second.reuse) {
9296         /* Put second chess program into idle state */
9297         if (second.pr != NoProc &&
9298             gameMode == TwoMachinesPlay) {
9299             SendToProgram("force\n", &second);
9300             if (second.usePing) {
9301               char buf[MSG_SIZ];
9302               sprintf(buf, "ping %d\n", ++second.lastPing);
9303               SendToProgram(buf, &second);
9304             }
9305         }
9306     } else if (result != GameUnfinished || nextGameMode == IcsIdle) {
9307         /* Kill off second chess program */
9308         if (second.isr != NULL)
9309           RemoveInputSource(second.isr);
9310         second.isr = NULL;
9311     
9312         if (second.pr != NoProc) {
9313             DoSleep( appData.delayBeforeQuit );
9314             SendToProgram("quit\n", &second);
9315             DoSleep( appData.delayAfterQuit );
9316             DestroyChildProcess(second.pr, second.useSigterm);
9317         }
9318         second.pr = NoProc;
9319     }
9320
9321     if (matchMode && gameMode == TwoMachinesPlay) {
9322         switch (result) {
9323         case WhiteWins:
9324           if (first.twoMachinesColor[0] == 'w') {
9325             first.matchWins++;
9326           } else {
9327             second.matchWins++;
9328           }
9329           break;
9330         case BlackWins:
9331           if (first.twoMachinesColor[0] == 'b') {
9332             first.matchWins++;
9333           } else {
9334             second.matchWins++;
9335           }
9336           break;
9337         default:
9338           break;
9339         }
9340         if (matchGame < appData.matchGames) {
9341             char *tmp;
9342             if(appData.sameColorGames <= 1) { /* [HGM] alternate: suppress color swap */
9343                 tmp = first.twoMachinesColor;
9344                 first.twoMachinesColor = second.twoMachinesColor;
9345                 second.twoMachinesColor = tmp;
9346             }
9347             gameMode = nextGameMode;
9348             matchGame++;
9349             if(appData.matchPause>10000 || appData.matchPause<10)
9350                 appData.matchPause = 10000; /* [HGM] make pause adjustable */
9351             ScheduleDelayedEvent(NextMatchGame, appData.matchPause);
9352             endingGame = 0; /* [HGM] crash */
9353             return;
9354         } else {
9355             gameMode = nextGameMode;
9356             sprintf(buf, _("Match %s vs. %s: final score %d-%d-%d"),
9357                     first.tidy, second.tidy,
9358                     first.matchWins, second.matchWins,
9359                     appData.matchGames - (first.matchWins + second.matchWins));
9360             popupRequested++; // [HGM] crash: postpone to after resetting endingGame
9361         }
9362     }
9363     if ((gameMode == AnalyzeMode || gameMode == AnalyzeFile) &&
9364         !(nextGameMode == AnalyzeMode || nextGameMode == AnalyzeFile))
9365       ExitAnalyzeMode();
9366     gameMode = nextGameMode;
9367     ModeHighlight();
9368     endingGame = 0;  /* [HGM] crash */
9369     if(popupRequested) DisplayFatalError(buf, 0, 0); // [HGM] crash: this call GameEnds recursively through ExitEvent! Make it a harmless tail recursion.
9370 }
9371
9372 /* Assumes program was just initialized (initString sent).
9373    Leaves program in force mode. */
9374 void
9375 FeedMovesToProgram(cps, upto) 
9376      ChessProgramState *cps;
9377      int upto;
9378 {
9379     int i;
9380     
9381     if (appData.debugMode)
9382       fprintf(debugFP, "Feeding %smoves %d through %d to %s chess program\n",
9383               startedFromSetupPosition ? "position and " : "",
9384               backwardMostMove, upto, cps->which);
9385     if(currentlyInitializedVariant != gameInfo.variant) { char buf[MSG_SIZ];
9386         // [HGM] variantswitch: make engine aware of new variant
9387         if(cps->protocolVersion > 1 && StrStr(cps->variants, VariantName(gameInfo.variant)) == NULL)
9388                 return; // [HGM] refrain from feeding moves altogether if variant is unsupported!
9389         sprintf(buf, "variant %s\n", VariantName(gameInfo.variant));
9390         SendToProgram(buf, cps);
9391         currentlyInitializedVariant = gameInfo.variant;
9392     }
9393     SendToProgram("force\n", cps);
9394     if (startedFromSetupPosition) {
9395         SendBoard(cps, backwardMostMove);
9396     if (appData.debugMode) {
9397         fprintf(debugFP, "feedMoves\n");
9398     }
9399     }
9400     for (i = backwardMostMove; i < upto; i++) {
9401         SendMoveToProgram(i, cps);
9402     }
9403 }
9404
9405
9406 void
9407 ResurrectChessProgram()
9408 {
9409      /* The chess program may have exited.
9410         If so, restart it and feed it all the moves made so far. */
9411
9412     if (appData.noChessProgram || first.pr != NoProc) return;
9413     
9414     StartChessProgram(&first);
9415     InitChessProgram(&first, FALSE);
9416     FeedMovesToProgram(&first, currentMove);
9417
9418     if (!first.sendTime) {
9419         /* can't tell gnuchess what its clock should read,
9420            so we bow to its notion. */
9421         ResetClocks();
9422         timeRemaining[0][currentMove] = whiteTimeRemaining;
9423         timeRemaining[1][currentMove] = blackTimeRemaining;
9424     }
9425
9426     if ((gameMode == AnalyzeMode || gameMode == AnalyzeFile ||
9427                 appData.icsEngineAnalyze) && first.analysisSupport) {
9428       SendToProgram("analyze\n", &first);
9429       first.analyzing = TRUE;
9430     }
9431 }
9432
9433 /*
9434  * Button procedures
9435  */
9436 void
9437 Reset(redraw, init)
9438      int redraw, init;
9439 {
9440     int i;
9441
9442     if (appData.debugMode) {
9443         fprintf(debugFP, "Reset(%d, %d) from gameMode %d\n",
9444                 redraw, init, gameMode);
9445     }
9446     CleanupTail(); // [HGM] vari: delete any stored variations
9447     pausing = pauseExamInvalid = FALSE;
9448     startedFromSetupPosition = blackPlaysFirst = FALSE;
9449     firstMove = TRUE;
9450     whiteFlag = blackFlag = FALSE;
9451     userOfferedDraw = FALSE;
9452     hintRequested = bookRequested = FALSE;
9453     first.maybeThinking = FALSE;
9454     second.maybeThinking = FALSE;
9455     first.bookSuspend = FALSE; // [HGM] book
9456     second.bookSuspend = FALSE;
9457     thinkOutput[0] = NULLCHAR;
9458     lastHint[0] = NULLCHAR;
9459     ClearGameInfo(&gameInfo);
9460     gameInfo.variant = StringToVariant(appData.variant);
9461     ics_user_moved = ics_clock_paused = FALSE;
9462     ics_getting_history = H_FALSE;
9463     ics_gamenum = -1;
9464     white_holding[0] = black_holding[0] = NULLCHAR;
9465     ClearProgramStats();
9466     opponentKibitzes = FALSE; // [HGM] kibitz: do not reserve space in engine-output window in zippy mode
9467     
9468     ResetFrontEnd();
9469     ClearHighlights();
9470     flipView = appData.flipView;
9471     ClearPremoveHighlights();
9472     gotPremove = FALSE;
9473     alarmSounded = FALSE;
9474
9475     GameEnds((ChessMove) 0, NULL, GE_PLAYER);
9476     if(appData.serverMovesName != NULL) {
9477         /* [HGM] prepare to make moves file for broadcasting */
9478         clock_t t = clock();
9479         if(serverMoves != NULL) fclose(serverMoves);
9480         serverMoves = fopen(appData.serverMovesName, "r");
9481         if(serverMoves != NULL) {
9482             fclose(serverMoves);
9483             /* delay 15 sec before overwriting, so all clients can see end */
9484             while(clock()-t < appData.serverPause*CLOCKS_PER_SEC);
9485         }
9486         serverMoves = fopen(appData.serverMovesName, "w");
9487     }
9488
9489     ExitAnalyzeMode();
9490     gameMode = BeginningOfGame;
9491     ModeHighlight();
9492     if(appData.icsActive) gameInfo.variant = VariantNormal;
9493     currentMove = forwardMostMove = backwardMostMove = 0;
9494     InitPosition(redraw);
9495     for (i = 0; i < MAX_MOVES; i++) {
9496         if (commentList[i] != NULL) {
9497             free(commentList[i]);
9498             commentList[i] = NULL;
9499         }
9500     }
9501     ResetClocks();
9502     timeRemaining[0][0] = whiteTimeRemaining;
9503     timeRemaining[1][0] = blackTimeRemaining;
9504     if (first.pr == NULL) {
9505         StartChessProgram(&first);
9506     }
9507     if (init) {
9508             InitChessProgram(&first, startedFromSetupPosition);
9509     }
9510     DisplayTitle("");
9511     DisplayMessage("", "");
9512     HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove-1);
9513     lastSavedGame = 0; // [HGM] save: make sure next game counts as unsaved
9514 }
9515
9516 void
9517 AutoPlayGameLoop()
9518 {
9519     for (;;) {
9520         if (!AutoPlayOneMove())
9521           return;
9522         if (matchMode || appData.timeDelay == 0)
9523           continue;
9524         if (appData.timeDelay < 0 || gameMode == AnalyzeFile)
9525           return;
9526         StartLoadGameTimer((long)(1000.0 * appData.timeDelay));
9527         break;
9528     }
9529 }
9530
9531
9532 int
9533 AutoPlayOneMove()
9534 {
9535     int fromX, fromY, toX, toY;
9536
9537     if (appData.debugMode) {
9538       fprintf(debugFP, "AutoPlayOneMove(): current %d\n", currentMove);
9539     }
9540
9541     if (gameMode != PlayFromGameFile)
9542       return FALSE;
9543
9544     if (currentMove >= forwardMostMove) {
9545       gameMode = EditGame;
9546       ModeHighlight();
9547
9548       /* [AS] Clear current move marker at the end of a game */
9549       /* HistorySet(parseList, backwardMostMove, forwardMostMove, -1); */
9550
9551       return FALSE;
9552     }
9553     
9554     toX = moveList[currentMove][2] - AAA;
9555     toY = moveList[currentMove][3] - ONE;
9556
9557     if (moveList[currentMove][1] == '@') {
9558         if (appData.highlightLastMove) {
9559             SetHighlights(-1, -1, toX, toY);
9560         }
9561     } else {
9562         fromX = moveList[currentMove][0] - AAA;
9563         fromY = moveList[currentMove][1] - ONE;
9564
9565         HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove); /* [AS] */
9566
9567         AnimateMove(boards[currentMove], fromX, fromY, toX, toY);
9568
9569         if (appData.highlightLastMove) {
9570             SetHighlights(fromX, fromY, toX, toY);
9571         }
9572     }
9573     DisplayMove(currentMove);
9574     SendMoveToProgram(currentMove++, &first);
9575     DisplayBothClocks();
9576     DrawPosition(FALSE, boards[currentMove]);
9577     // [HGM] PV info: always display, routine tests if empty
9578     DisplayComment(currentMove - 1, commentList[currentMove]);
9579     return TRUE;
9580 }
9581
9582
9583 int
9584 LoadGameOneMove(readAhead)
9585      ChessMove readAhead;
9586 {
9587     int fromX = 0, fromY = 0, toX = 0, toY = 0, done;
9588     char promoChar = NULLCHAR;
9589     ChessMove moveType;
9590     char move[MSG_SIZ];
9591     char *p, *q;
9592     
9593     if (gameMode != PlayFromGameFile && gameMode != AnalyzeFile && 
9594         gameMode != AnalyzeMode && gameMode != Training) {
9595         gameFileFP = NULL;
9596         return FALSE;
9597     }
9598     
9599     yyboardindex = forwardMostMove;
9600     if (readAhead != (ChessMove)0) {
9601       moveType = readAhead;
9602     } else {
9603       if (gameFileFP == NULL)
9604           return FALSE;
9605       moveType = (ChessMove) yylex();
9606     }
9607     
9608     done = FALSE;
9609     switch (moveType) {
9610       case Comment:
9611         if (appData.debugMode) 
9612           fprintf(debugFP, "Parsed Comment: %s\n", yy_text);
9613         p = yy_text;
9614
9615         /* append the comment but don't display it */
9616         AppendComment(currentMove, p, FALSE);
9617         return TRUE;
9618
9619       case WhiteCapturesEnPassant:
9620       case BlackCapturesEnPassant:
9621       case WhitePromotionChancellor:
9622       case BlackPromotionChancellor:
9623       case WhitePromotionArchbishop:
9624       case BlackPromotionArchbishop:
9625       case WhitePromotionCentaur:
9626       case BlackPromotionCentaur:
9627       case WhitePromotionQueen:
9628       case BlackPromotionQueen:
9629       case WhitePromotionRook:
9630       case BlackPromotionRook:
9631       case WhitePromotionBishop:
9632       case BlackPromotionBishop:
9633       case WhitePromotionKnight:
9634       case BlackPromotionKnight:
9635       case WhitePromotionKing:
9636       case BlackPromotionKing:
9637       case NormalMove:
9638       case WhiteKingSideCastle:
9639       case WhiteQueenSideCastle:
9640       case BlackKingSideCastle:
9641       case BlackQueenSideCastle:
9642       case WhiteKingSideCastleWild:
9643       case WhiteQueenSideCastleWild:
9644       case BlackKingSideCastleWild:
9645       case BlackQueenSideCastleWild:
9646       /* PUSH Fabien */
9647       case WhiteHSideCastleFR:
9648       case WhiteASideCastleFR:
9649       case BlackHSideCastleFR:
9650       case BlackASideCastleFR:
9651       /* POP Fabien */
9652         if (appData.debugMode)
9653           fprintf(debugFP, "Parsed %s into %s\n", yy_text, currentMoveString);
9654         fromX = currentMoveString[0] - AAA;
9655         fromY = currentMoveString[1] - ONE;
9656         toX = currentMoveString[2] - AAA;
9657         toY = currentMoveString[3] - ONE;
9658         promoChar = currentMoveString[4];
9659         break;
9660
9661       case WhiteDrop:
9662       case BlackDrop:
9663         if (appData.debugMode)
9664           fprintf(debugFP, "Parsed %s into %s\n", yy_text, currentMoveString);
9665         fromX = moveType == WhiteDrop ?
9666           (int) CharToPiece(ToUpper(currentMoveString[0])) :
9667         (int) CharToPiece(ToLower(currentMoveString[0]));
9668         fromY = DROP_RANK;
9669         toX = currentMoveString[2] - AAA;
9670         toY = currentMoveString[3] - ONE;
9671         break;
9672
9673       case WhiteWins:
9674       case BlackWins:
9675       case GameIsDrawn:
9676       case GameUnfinished:
9677         if (appData.debugMode)
9678           fprintf(debugFP, "Parsed game end: %s\n", yy_text);
9679         p = strchr(yy_text, '{');
9680         if (p == NULL) p = strchr(yy_text, '(');
9681         if (p == NULL) {
9682             p = yy_text;
9683             if (p[0] == '0' || p[0] == '1' || p[0] == '*') p = "";
9684         } else {
9685             q = strchr(p, *p == '{' ? '}' : ')');
9686             if (q != NULL) *q = NULLCHAR;
9687             p++;
9688         }
9689         GameEnds(moveType, p, GE_FILE);
9690         done = TRUE;
9691         if (cmailMsgLoaded) {
9692             ClearHighlights();
9693             flipView = WhiteOnMove(currentMove);
9694             if (moveType == GameUnfinished) flipView = !flipView;
9695             if (appData.debugMode)
9696               fprintf(debugFP, "Setting flipView to %d\n", flipView) ;
9697         }
9698         break;
9699
9700       case (ChessMove) 0:       /* end of file */
9701         if (appData.debugMode)
9702           fprintf(debugFP, "Parser hit end of file\n");
9703         switch (MateTest(boards[currentMove], PosFlags(currentMove)) ) {
9704           case MT_NONE:
9705           case MT_CHECK:
9706             break;
9707           case MT_CHECKMATE:
9708           case MT_STAINMATE:
9709             if (WhiteOnMove(currentMove)) {
9710                 GameEnds(BlackWins, "Black mates", GE_FILE);
9711             } else {
9712                 GameEnds(WhiteWins, "White mates", GE_FILE);
9713             }
9714             break;
9715           case MT_STALEMATE:
9716             GameEnds(GameIsDrawn, "Stalemate", GE_FILE);
9717             break;
9718         }
9719         done = TRUE;
9720         break;
9721
9722       case MoveNumberOne:
9723         if (lastLoadGameStart == GNUChessGame) {
9724             /* GNUChessGames have numbers, but they aren't move numbers */
9725             if (appData.debugMode)
9726               fprintf(debugFP, "Parser ignoring: '%s' (%d)\n",
9727                       yy_text, (int) moveType);
9728             return LoadGameOneMove((ChessMove)0); /* tail recursion */
9729         }
9730         /* else fall thru */
9731
9732       case XBoardGame:
9733       case GNUChessGame:
9734       case PGNTag:
9735         /* Reached start of next game in file */
9736         if (appData.debugMode)
9737           fprintf(debugFP, "Parsed start of next game: %s\n", yy_text);
9738         switch (MateTest(boards[currentMove], PosFlags(currentMove)) ) {
9739           case MT_NONE:
9740           case MT_CHECK:
9741             break;
9742           case MT_CHECKMATE:
9743           case MT_STAINMATE:
9744             if (WhiteOnMove(currentMove)) {
9745                 GameEnds(BlackWins, "Black mates", GE_FILE);
9746             } else {
9747                 GameEnds(WhiteWins, "White mates", GE_FILE);
9748             }
9749             break;
9750           case MT_STALEMATE:
9751             GameEnds(GameIsDrawn, "Stalemate", GE_FILE);
9752             break;
9753         }
9754         done = TRUE;
9755         break;
9756
9757       case PositionDiagram:     /* should not happen; ignore */
9758       case ElapsedTime:         /* ignore */
9759       case NAG:                 /* ignore */
9760         if (appData.debugMode)
9761           fprintf(debugFP, "Parser ignoring: '%s' (%d)\n",
9762                   yy_text, (int) moveType);
9763         return LoadGameOneMove((ChessMove)0); /* tail recursion */
9764
9765       case IllegalMove:
9766         if (appData.testLegality) {
9767             if (appData.debugMode)
9768               fprintf(debugFP, "Parsed IllegalMove: %s\n", yy_text);
9769             sprintf(move, _("Illegal move: %d.%s%s"),
9770                     (forwardMostMove / 2) + 1,
9771                     WhiteOnMove(forwardMostMove) ? " " : ".. ", yy_text);
9772             DisplayError(move, 0);
9773             done = TRUE;
9774         } else {
9775             if (appData.debugMode)
9776               fprintf(debugFP, "Parsed %s into IllegalMove %s\n",
9777                       yy_text, currentMoveString);
9778             fromX = currentMoveString[0] - AAA;
9779             fromY = currentMoveString[1] - ONE;
9780             toX = currentMoveString[2] - AAA;
9781             toY = currentMoveString[3] - ONE;
9782             promoChar = currentMoveString[4];
9783         }
9784         break;
9785
9786       case AmbiguousMove:
9787         if (appData.debugMode)
9788           fprintf(debugFP, "Parsed AmbiguousMove: %s\n", yy_text);
9789         sprintf(move, _("Ambiguous move: %d.%s%s"),
9790                 (forwardMostMove / 2) + 1,
9791                 WhiteOnMove(forwardMostMove) ? " " : ".. ", yy_text);
9792         DisplayError(move, 0);
9793         done = TRUE;
9794         break;
9795
9796       default:
9797       case ImpossibleMove:
9798         if (appData.debugMode)
9799           fprintf(debugFP, "Parsed ImpossibleMove (type = %d): %s\n", moveType, yy_text);
9800         sprintf(move, _("Illegal move: %d.%s%s"),
9801                 (forwardMostMove / 2) + 1,
9802                 WhiteOnMove(forwardMostMove) ? " " : ".. ", yy_text);
9803         DisplayError(move, 0);
9804         done = TRUE;
9805         break;
9806     }
9807
9808     if (done) {
9809         if (appData.matchMode || (appData.timeDelay == 0 && !pausing)) {
9810             DrawPosition(FALSE, boards[currentMove]);
9811             DisplayBothClocks();
9812             if (!appData.matchMode) // [HGM] PV info: routine tests if empty
9813               DisplayComment(currentMove - 1, commentList[currentMove]);
9814         }
9815         (void) StopLoadGameTimer();
9816         gameFileFP = NULL;
9817         cmailOldMove = forwardMostMove;
9818         return FALSE;
9819     } else {
9820         /* currentMoveString is set as a side-effect of yylex */
9821         strcat(currentMoveString, "\n");
9822         strcpy(moveList[forwardMostMove], currentMoveString);
9823         
9824         thinkOutput[0] = NULLCHAR;
9825         MakeMove(fromX, fromY, toX, toY, promoChar);
9826         currentMove = forwardMostMove;
9827         return TRUE;
9828     }
9829 }
9830
9831 /* Load the nth game from the given file */
9832 int
9833 LoadGameFromFile(filename, n, title, useList)
9834      char *filename;
9835      int n;
9836      char *title;
9837      /*Boolean*/ int useList;
9838 {
9839     FILE *f;
9840     char buf[MSG_SIZ];
9841
9842     if (strcmp(filename, "-") == 0) {
9843         f = stdin;
9844         title = "stdin";
9845     } else {
9846         f = fopen(filename, "rb");
9847         if (f == NULL) {
9848           snprintf(buf, sizeof(buf),  _("Can't open \"%s\""), filename);
9849             DisplayError(buf, errno);
9850             return FALSE;
9851         }
9852     }
9853     if (fseek(f, 0, 0) == -1) {
9854         /* f is not seekable; probably a pipe */
9855         useList = FALSE;
9856     }
9857     if (useList && n == 0) {
9858         int error = GameListBuild(f);
9859         if (error) {
9860             DisplayError(_("Cannot build game list"), error);
9861         } else if (!ListEmpty(&gameList) &&
9862                    ((ListGame *) gameList.tailPred)->number > 1) {
9863             GameListPopUp(f, title);
9864             return TRUE;
9865         }
9866         GameListDestroy();
9867         n = 1;
9868     }
9869     if (n == 0) n = 1;
9870     return LoadGame(f, n, title, FALSE);
9871 }
9872
9873
9874 void
9875 MakeRegisteredMove()
9876 {
9877     int fromX, fromY, toX, toY;
9878     char promoChar;
9879     if (cmailMoveRegistered[lastLoadGameNumber - 1]) {
9880         switch (cmailMoveType[lastLoadGameNumber - 1]) {
9881           case CMAIL_MOVE:
9882           case CMAIL_DRAW:
9883             if (appData.debugMode)
9884               fprintf(debugFP, "Restoring %s for game %d\n",
9885                       cmailMove[lastLoadGameNumber - 1], lastLoadGameNumber);
9886     
9887             thinkOutput[0] = NULLCHAR;
9888             strcpy(moveList[currentMove], cmailMove[lastLoadGameNumber - 1]);
9889             fromX = cmailMove[lastLoadGameNumber - 1][0] - AAA;
9890             fromY = cmailMove[lastLoadGameNumber - 1][1] - ONE;
9891             toX = cmailMove[lastLoadGameNumber - 1][2] - AAA;
9892             toY = cmailMove[lastLoadGameNumber - 1][3] - ONE;
9893             promoChar = cmailMove[lastLoadGameNumber - 1][4];
9894             MakeMove(fromX, fromY, toX, toY, promoChar);
9895             ShowMove(fromX, fromY, toX, toY);
9896               
9897             switch (MateTest(boards[currentMove], PosFlags(currentMove)) ) {
9898               case MT_NONE:
9899               case MT_CHECK:
9900                 break;
9901                 
9902               case MT_CHECKMATE:
9903               case MT_STAINMATE:
9904                 if (WhiteOnMove(currentMove)) {
9905                     GameEnds(BlackWins, "Black mates", GE_PLAYER);
9906                 } else {
9907                     GameEnds(WhiteWins, "White mates", GE_PLAYER);
9908                 }
9909                 break;
9910                 
9911               case MT_STALEMATE:
9912                 GameEnds(GameIsDrawn, "Stalemate", GE_PLAYER);
9913                 break;
9914             }
9915
9916             break;
9917             
9918           case CMAIL_RESIGN:
9919             if (WhiteOnMove(currentMove)) {
9920                 GameEnds(BlackWins, "White resigns", GE_PLAYER);
9921             } else {
9922                 GameEnds(WhiteWins, "Black resigns", GE_PLAYER);
9923             }
9924             break;
9925             
9926           case CMAIL_ACCEPT:
9927             GameEnds(GameIsDrawn, "Draw agreed", GE_PLAYER);
9928             break;
9929               
9930           default:
9931             break;
9932         }
9933     }
9934
9935     return;
9936 }
9937
9938 /* Wrapper around LoadGame for use when a Cmail message is loaded */
9939 int
9940 CmailLoadGame(f, gameNumber, title, useList)
9941      FILE *f;
9942      int gameNumber;
9943      char *title;
9944      int useList;
9945 {
9946     int retVal;
9947
9948     if (gameNumber > nCmailGames) {
9949         DisplayError(_("No more games in this message"), 0);
9950         return FALSE;
9951     }
9952     if (f == lastLoadGameFP) {
9953         int offset = gameNumber - lastLoadGameNumber;
9954         if (offset == 0) {
9955             cmailMsg[0] = NULLCHAR;
9956             if (cmailMoveRegistered[lastLoadGameNumber - 1]) {
9957                 cmailMoveRegistered[lastLoadGameNumber - 1] = FALSE;
9958                 nCmailMovesRegistered--;
9959             }
9960             cmailMoveType[lastLoadGameNumber - 1] = CMAIL_MOVE;
9961             if (cmailResult[lastLoadGameNumber - 1] == CMAIL_NEW_RESULT) {
9962                 cmailResult[lastLoadGameNumber - 1] = CMAIL_NOT_RESULT;
9963             }
9964         } else {
9965             if (! RegisterMove()) return FALSE;
9966         }
9967     }
9968
9969     retVal = LoadGame(f, gameNumber, title, useList);
9970
9971     /* Make move registered during previous look at this game, if any */
9972     MakeRegisteredMove();
9973
9974     if (cmailCommentList[lastLoadGameNumber - 1] != NULL) {
9975         commentList[currentMove]
9976           = StrSave(cmailCommentList[lastLoadGameNumber - 1]);
9977         DisplayComment(currentMove - 1, commentList[currentMove]);
9978     }
9979
9980     return retVal;
9981 }
9982
9983 /* Support for LoadNextGame, LoadPreviousGame, ReloadSameGame */
9984 int
9985 ReloadGame(offset)
9986      int offset;
9987 {
9988     int gameNumber = lastLoadGameNumber + offset;
9989     if (lastLoadGameFP == NULL) {
9990         DisplayError(_("No game has been loaded yet"), 0);
9991         return FALSE;
9992     }
9993     if (gameNumber <= 0) {
9994         DisplayError(_("Can't back up any further"), 0);
9995         return FALSE;
9996     }
9997     if (cmailMsgLoaded) {
9998         return CmailLoadGame(lastLoadGameFP, gameNumber,
9999                              lastLoadGameTitle, lastLoadGameUseList);
10000     } else {
10001         return LoadGame(lastLoadGameFP, gameNumber,
10002                         lastLoadGameTitle, lastLoadGameUseList);
10003     }
10004 }
10005
10006
10007
10008 /* Load the nth game from open file f */
10009 int
10010 LoadGame(f, gameNumber, title, useList)
10011      FILE *f;
10012      int gameNumber;
10013      char *title;
10014      int useList;
10015 {
10016     ChessMove cm;
10017     char buf[MSG_SIZ];
10018     int gn = gameNumber;
10019     ListGame *lg = NULL;
10020     int numPGNTags = 0;
10021     int err;
10022     GameMode oldGameMode;
10023     VariantClass oldVariant = gameInfo.variant; /* [HGM] PGNvariant */
10024
10025     if (appData.debugMode) 
10026         fprintf(debugFP, "LoadGame(): on entry, gameMode %d\n", gameMode);
10027
10028     if (gameMode == Training )
10029         SetTrainingModeOff();
10030
10031     oldGameMode = gameMode;
10032     if (gameMode != BeginningOfGame) {
10033       Reset(FALSE, TRUE);
10034     }
10035
10036     gameFileFP = f;
10037     if (lastLoadGameFP != NULL && lastLoadGameFP != f) {
10038         fclose(lastLoadGameFP);
10039     }
10040
10041     if (useList) {
10042         lg = (ListGame *) ListElem(&gameList, gameNumber-1);
10043         
10044         if (lg) {
10045             fseek(f, lg->offset, 0);
10046             GameListHighlight(gameNumber);
10047             gn = 1;
10048         }
10049         else {
10050             DisplayError(_("Game number out of range"), 0);
10051             return FALSE;
10052         }
10053     } else {
10054         GameListDestroy();
10055         if (fseek(f, 0, 0) == -1) {
10056             if (f == lastLoadGameFP ?
10057                 gameNumber == lastLoadGameNumber + 1 :
10058                 gameNumber == 1) {
10059                 gn = 1;
10060             } else {
10061                 DisplayError(_("Can't seek on game file"), 0);
10062                 return FALSE;
10063             }
10064         }
10065     }
10066     lastLoadGameFP = f;
10067     lastLoadGameNumber = gameNumber;
10068     strcpy(lastLoadGameTitle, title);
10069     lastLoadGameUseList = useList;
10070
10071     yynewfile(f);
10072
10073     if (lg && lg->gameInfo.white && lg->gameInfo.black) {
10074       snprintf(buf, sizeof(buf), "%s vs. %s", lg->gameInfo.white,
10075                 lg->gameInfo.black);
10076             DisplayTitle(buf);
10077     } else if (*title != NULLCHAR) {
10078         if (gameNumber > 1) {
10079             sprintf(buf, "%s %d", title, gameNumber);
10080             DisplayTitle(buf);
10081         } else {
10082             DisplayTitle(title);
10083         }
10084     }
10085
10086     if (gameMode != AnalyzeFile && gameMode != AnalyzeMode) {
10087         gameMode = PlayFromGameFile;
10088         ModeHighlight();
10089     }
10090
10091     currentMove = forwardMostMove = backwardMostMove = 0;
10092     CopyBoard(boards[0], initialPosition);
10093     StopClocks();
10094
10095     /*
10096      * Skip the first gn-1 games in the file.
10097      * Also skip over anything that precedes an identifiable 
10098      * start of game marker, to avoid being confused by 
10099      * garbage at the start of the file.  Currently 
10100      * recognized start of game markers are the move number "1",
10101      * the pattern "gnuchess .* game", the pattern
10102      * "^[#;%] [^ ]* game file", and a PGN tag block.  
10103      * A game that starts with one of the latter two patterns
10104      * will also have a move number 1, possibly
10105      * following a position diagram.
10106      * 5-4-02: Let's try being more lenient and allowing a game to
10107      * start with an unnumbered move.  Does that break anything?
10108      */
10109     cm = lastLoadGameStart = (ChessMove) 0;
10110     while (gn > 0) {
10111         yyboardindex = forwardMostMove;
10112         cm = (ChessMove) yylex();
10113         switch (cm) {
10114           case (ChessMove) 0:
10115             if (cmailMsgLoaded) {
10116                 nCmailGames = CMAIL_MAX_GAMES - gn;
10117             } else {
10118                 Reset(TRUE, TRUE);
10119                 DisplayError(_("Game not found in file"), 0);
10120             }
10121             return FALSE;
10122
10123           case GNUChessGame:
10124           case XBoardGame:
10125             gn--;
10126             lastLoadGameStart = cm;
10127             break;
10128             
10129           case MoveNumberOne:
10130             switch (lastLoadGameStart) {
10131               case GNUChessGame:
10132               case XBoardGame:
10133               case PGNTag:
10134                 break;
10135               case MoveNumberOne:
10136               case (ChessMove) 0:
10137                 gn--;           /* count this game */
10138                 lastLoadGameStart = cm;
10139                 break;
10140               default:
10141                 /* impossible */
10142                 break;
10143             }
10144             break;
10145
10146           case PGNTag:
10147             switch (lastLoadGameStart) {
10148               case GNUChessGame:
10149               case PGNTag:
10150               case MoveNumberOne:
10151               case (ChessMove) 0:
10152                 gn--;           /* count this game */
10153                 lastLoadGameStart = cm;
10154                 break;
10155               case XBoardGame:
10156                 lastLoadGameStart = cm; /* game counted already */
10157                 break;
10158               default:
10159                 /* impossible */
10160                 break;
10161             }
10162             if (gn > 0) {
10163                 do {
10164                     yyboardindex = forwardMostMove;
10165                     cm = (ChessMove) yylex();
10166                 } while (cm == PGNTag || cm == Comment);
10167             }
10168             break;
10169
10170           case WhiteWins:
10171           case BlackWins:
10172           case GameIsDrawn:
10173             if (cmailMsgLoaded && (CMAIL_MAX_GAMES == lastLoadGameNumber)) {
10174                 if (   cmailResult[CMAIL_MAX_GAMES - gn - 1]
10175                     != CMAIL_OLD_RESULT) {
10176                     nCmailResults ++ ;
10177                     cmailResult[  CMAIL_MAX_GAMES
10178                                 - gn - 1] = CMAIL_OLD_RESULT;
10179                 }
10180             }
10181             break;
10182
10183           case NormalMove:
10184             /* Only a NormalMove can be at the start of a game
10185              * without a position diagram. */
10186             if (lastLoadGameStart == (ChessMove) 0) {
10187               gn--;
10188               lastLoadGameStart = MoveNumberOne;
10189             }
10190             break;
10191
10192           default:
10193             break;
10194         }
10195     }
10196     
10197     if (appData.debugMode)
10198       fprintf(debugFP, "Parsed game start '%s' (%d)\n", yy_text, (int) cm);
10199
10200     if (cm == XBoardGame) {
10201         /* Skip any header junk before position diagram and/or move 1 */
10202         for (;;) {
10203             yyboardindex = forwardMostMove;
10204             cm = (ChessMove) yylex();
10205
10206             if (cm == (ChessMove) 0 ||
10207                 cm == GNUChessGame || cm == XBoardGame) {
10208                 /* Empty game; pretend end-of-file and handle later */
10209                 cm = (ChessMove) 0;
10210                 break;
10211             }
10212
10213             if (cm == MoveNumberOne || cm == PositionDiagram ||
10214                 cm == PGNTag || cm == Comment)
10215               break;
10216         }
10217     } else if (cm == GNUChessGame) {
10218         if (gameInfo.event != NULL) {
10219             free(gameInfo.event);
10220         }
10221         gameInfo.event = StrSave(yy_text);
10222     }   
10223
10224     startedFromSetupPosition = FALSE;
10225     while (cm == PGNTag) {
10226         if (appData.debugMode) 
10227           fprintf(debugFP, "Parsed PGNTag: %s\n", yy_text);
10228         err = ParsePGNTag(yy_text, &gameInfo);
10229         if (!err) numPGNTags++;
10230
10231         /* [HGM] PGNvariant: automatically switch to variant given in PGN tag */
10232         if(gameInfo.variant != oldVariant) {
10233             startedFromPositionFile = FALSE; /* [HGM] loadPos: variant switch likely makes position invalid */
10234             ResetFrontEnd(); // [HGM] might need other bitmaps. Cannot use Reset() because it clears gameInfo :-(
10235             InitPosition(TRUE);
10236             oldVariant = gameInfo.variant;
10237             if (appData.debugMode) 
10238               fprintf(debugFP, "New variant %d\n", (int) oldVariant);
10239         }
10240
10241
10242         if (gameInfo.fen != NULL) {
10243           Board initial_position;
10244           startedFromSetupPosition = TRUE;
10245           if (!ParseFEN(initial_position, &blackPlaysFirst, gameInfo.fen)) {
10246             Reset(TRUE, TRUE);
10247             DisplayError(_("Bad FEN position in file"), 0);
10248             return FALSE;
10249           }
10250           CopyBoard(boards[0], initial_position);
10251           if (blackPlaysFirst) {
10252             currentMove = forwardMostMove = backwardMostMove = 1;
10253             CopyBoard(boards[1], initial_position);
10254             strcpy(moveList[0], "");
10255             strcpy(parseList[0], "");
10256             timeRemaining[0][1] = whiteTimeRemaining;
10257             timeRemaining[1][1] = blackTimeRemaining;
10258             if (commentList[0] != NULL) {
10259               commentList[1] = commentList[0];
10260               commentList[0] = NULL;
10261             }
10262           } else {
10263             currentMove = forwardMostMove = backwardMostMove = 0;
10264           }
10265           /* [HGM] copy FEN attributes as well. Bugfix 4.3.14m and 4.3.15e: moved to after 'blackPlaysFirst' */
10266           {   int i;
10267               initialRulePlies = FENrulePlies;
10268               for( i=0; i< nrCastlingRights; i++ )
10269                   initialRights[i] = initial_position[CASTLING][i];
10270           }
10271           yyboardindex = forwardMostMove;
10272           free(gameInfo.fen);
10273           gameInfo.fen = NULL;
10274         }
10275
10276         yyboardindex = forwardMostMove;
10277         cm = (ChessMove) yylex();
10278
10279         /* Handle comments interspersed among the tags */
10280         while (cm == Comment) {
10281             char *p;
10282             if (appData.debugMode) 
10283               fprintf(debugFP, "Parsed Comment: %s\n", yy_text);
10284             p = yy_text;
10285             AppendComment(currentMove, p, FALSE);
10286             yyboardindex = forwardMostMove;
10287             cm = (ChessMove) yylex();
10288         }
10289     }
10290
10291     /* don't rely on existence of Event tag since if game was
10292      * pasted from clipboard the Event tag may not exist
10293      */
10294     if (numPGNTags > 0){
10295         char *tags;
10296         if (gameInfo.variant == VariantNormal) {
10297           VariantClass v = StringToVariant(gameInfo.event);
10298           // [HGM] do not recognize variants from event tag that were introduced after supporting variant tag
10299           if(v < VariantShogi) gameInfo.variant = v;
10300         }
10301         if (!matchMode) {
10302           if( appData.autoDisplayTags ) {
10303             tags = PGNTags(&gameInfo);
10304             TagsPopUp(tags, CmailMsg());
10305             free(tags);
10306           }
10307         }
10308     } else {
10309         /* Make something up, but don't display it now */
10310         SetGameInfo();
10311         TagsPopDown();
10312     }
10313
10314     if (cm == PositionDiagram) {
10315         int i, j;
10316         char *p;
10317         Board initial_position;
10318
10319         if (appData.debugMode)
10320           fprintf(debugFP, "Parsed PositionDiagram: %s\n", yy_text);
10321
10322         if (!startedFromSetupPosition) {
10323             p = yy_text;
10324             for (i = BOARD_HEIGHT - 1; i >= 0; i--)
10325               for (j = BOARD_LEFT; j < BOARD_RGHT; p++)
10326                 switch (*p) {
10327                   case '[':
10328                   case '-':
10329                   case ' ':
10330                   case '\t':
10331                   case '\n':
10332                   case '\r':
10333                     break;
10334                   default:
10335                     initial_position[i][j++] = CharToPiece(*p);
10336                     break;
10337                 }
10338             while (*p == ' ' || *p == '\t' ||
10339                    *p == '\n' || *p == '\r') p++;
10340         
10341             if (strncmp(p, "black", strlen("black"))==0)
10342               blackPlaysFirst = TRUE;
10343             else
10344               blackPlaysFirst = FALSE;
10345             startedFromSetupPosition = TRUE;
10346         
10347             CopyBoard(boards[0], initial_position);
10348             if (blackPlaysFirst) {
10349                 currentMove = forwardMostMove = backwardMostMove = 1;
10350                 CopyBoard(boards[1], initial_position);
10351                 strcpy(moveList[0], "");
10352                 strcpy(parseList[0], "");
10353                 timeRemaining[0][1] = whiteTimeRemaining;
10354                 timeRemaining[1][1] = blackTimeRemaining;
10355                 if (commentList[0] != NULL) {
10356                     commentList[1] = commentList[0];
10357                     commentList[0] = NULL;
10358                 }
10359             } else {
10360                 currentMove = forwardMostMove = backwardMostMove = 0;
10361             }
10362         }
10363         yyboardindex = forwardMostMove;
10364         cm = (ChessMove) yylex();
10365     }
10366
10367     if (first.pr == NoProc) {
10368         StartChessProgram(&first);
10369     }
10370     InitChessProgram(&first, FALSE);
10371     SendToProgram("force\n", &first);
10372     if (startedFromSetupPosition) {
10373         SendBoard(&first, forwardMostMove);
10374     if (appData.debugMode) {
10375         fprintf(debugFP, "Load Game\n");
10376     }
10377         DisplayBothClocks();
10378     }      
10379
10380     /* [HGM] server: flag to write setup moves in broadcast file as one */
10381     loadFlag = appData.suppressLoadMoves;
10382
10383     while (cm == Comment) {
10384         char *p;
10385         if (appData.debugMode) 
10386           fprintf(debugFP, "Parsed Comment: %s\n", yy_text);
10387         p = yy_text;
10388         AppendComment(currentMove, p, FALSE);
10389         yyboardindex = forwardMostMove;
10390         cm = (ChessMove) yylex();
10391     }
10392
10393     if ((cm == (ChessMove) 0 && lastLoadGameStart != (ChessMove) 0) ||
10394         cm == WhiteWins || cm == BlackWins ||
10395         cm == GameIsDrawn || cm == GameUnfinished) {
10396         DisplayMessage("", _("No moves in game"));
10397         if (cmailMsgLoaded) {
10398             if (appData.debugMode)
10399               fprintf(debugFP, "Setting flipView to %d.\n", FALSE);
10400             ClearHighlights();
10401             flipView = FALSE;
10402         }
10403         DrawPosition(FALSE, boards[currentMove]);
10404         DisplayBothClocks();
10405         gameMode = EditGame;
10406         ModeHighlight();
10407         gameFileFP = NULL;
10408         cmailOldMove = 0;
10409         return TRUE;
10410     }
10411
10412     // [HGM] PV info: routine tests if comment empty
10413     if (!matchMode && (pausing || appData.timeDelay != 0)) {
10414         DisplayComment(currentMove - 1, commentList[currentMove]);
10415     }
10416     if (!matchMode && appData.timeDelay != 0) 
10417       DrawPosition(FALSE, boards[currentMove]);
10418
10419     if (gameMode == AnalyzeFile || gameMode == AnalyzeMode) {
10420       programStats.ok_to_send = 1;
10421     }
10422
10423     /* if the first token after the PGN tags is a move
10424      * and not move number 1, retrieve it from the parser 
10425      */
10426     if (cm != MoveNumberOne)
10427         LoadGameOneMove(cm);
10428
10429     /* load the remaining moves from the file */
10430     while (LoadGameOneMove((ChessMove)0)) {
10431       timeRemaining[0][forwardMostMove] = whiteTimeRemaining;
10432       timeRemaining[1][forwardMostMove] = blackTimeRemaining;
10433     }
10434
10435     /* rewind to the start of the game */
10436     currentMove = backwardMostMove;
10437
10438     HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove-1);
10439
10440     if (oldGameMode == AnalyzeFile ||
10441         oldGameMode == AnalyzeMode) {
10442       AnalyzeFileEvent();
10443     }
10444
10445     if (matchMode || appData.timeDelay == 0) {
10446       ToEndEvent();
10447       gameMode = EditGame;
10448       ModeHighlight();
10449     } else if (appData.timeDelay > 0) {
10450       AutoPlayGameLoop();
10451     }
10452
10453     if (appData.debugMode) 
10454         fprintf(debugFP, "LoadGame(): on exit, gameMode %d\n", gameMode);
10455
10456     loadFlag = 0; /* [HGM] true game starts */
10457     return TRUE;
10458 }
10459
10460 /* Support for LoadNextPosition, LoadPreviousPosition, ReloadSamePosition */
10461 int
10462 ReloadPosition(offset)
10463      int offset;
10464 {
10465     int positionNumber = lastLoadPositionNumber + offset;
10466     if (lastLoadPositionFP == NULL) {
10467         DisplayError(_("No position has been loaded yet"), 0);
10468         return FALSE;
10469     }
10470     if (positionNumber <= 0) {
10471         DisplayError(_("Can't back up any further"), 0);
10472         return FALSE;
10473     }
10474     return LoadPosition(lastLoadPositionFP, positionNumber,
10475                         lastLoadPositionTitle);
10476 }
10477
10478 /* Load the nth position from the given file */
10479 int
10480 LoadPositionFromFile(filename, n, title)
10481      char *filename;
10482      int n;
10483      char *title;
10484 {
10485     FILE *f;
10486     char buf[MSG_SIZ];
10487
10488     if (strcmp(filename, "-") == 0) {
10489         return LoadPosition(stdin, n, "stdin");
10490     } else {
10491         f = fopen(filename, "rb");
10492         if (f == NULL) {
10493             snprintf(buf, sizeof(buf), _("Can't open \"%s\""), filename);
10494             DisplayError(buf, errno);
10495             return FALSE;
10496         } else {
10497             return LoadPosition(f, n, title);
10498         }
10499     }
10500 }
10501
10502 /* Load the nth position from the given open file, and close it */
10503 int
10504 LoadPosition(f, positionNumber, title)
10505      FILE *f;
10506      int positionNumber;
10507      char *title;
10508 {
10509     char *p, line[MSG_SIZ];
10510     Board initial_position;
10511     int i, j, fenMode, pn;
10512     
10513     if (gameMode == Training )
10514         SetTrainingModeOff();
10515
10516     if (gameMode != BeginningOfGame) {
10517         Reset(FALSE, TRUE);
10518     }
10519     if (lastLoadPositionFP != NULL && lastLoadPositionFP != f) {
10520         fclose(lastLoadPositionFP);
10521     }
10522     if (positionNumber == 0) positionNumber = 1;
10523     lastLoadPositionFP = f;
10524     lastLoadPositionNumber = positionNumber;
10525     strcpy(lastLoadPositionTitle, title);
10526     if (first.pr == NoProc) {
10527       StartChessProgram(&first);
10528       InitChessProgram(&first, FALSE);
10529     }    
10530     pn = positionNumber;
10531     if (positionNumber < 0) {
10532         /* Negative position number means to seek to that byte offset */
10533         if (fseek(f, -positionNumber, 0) == -1) {
10534             DisplayError(_("Can't seek on position file"), 0);
10535             return FALSE;
10536         };
10537         pn = 1;
10538     } else {
10539         if (fseek(f, 0, 0) == -1) {
10540             if (f == lastLoadPositionFP ?
10541                 positionNumber == lastLoadPositionNumber + 1 :
10542                 positionNumber == 1) {
10543                 pn = 1;
10544             } else {
10545                 DisplayError(_("Can't seek on position file"), 0);
10546                 return FALSE;
10547             }
10548         }
10549     }
10550     /* See if this file is FEN or old-style xboard */
10551     if (fgets(line, MSG_SIZ, f) == NULL) {
10552         DisplayError(_("Position not found in file"), 0);
10553         return FALSE;
10554     }
10555     // [HGM] FEN can begin with digit, any piece letter valid in this variant, or a + for Shogi promoted pieces
10556     fenMode = line[0] >= '0' && line[0] <= '9' || line[0] == '+' || CharToPiece(line[0]) != EmptySquare;
10557
10558     if (pn >= 2) {
10559         if (fenMode || line[0] == '#') pn--;
10560         while (pn > 0) {
10561             /* skip positions before number pn */
10562             if (fgets(line, MSG_SIZ, f) == NULL) {
10563                 Reset(TRUE, TRUE);
10564                 DisplayError(_("Position not found in file"), 0);
10565                 return FALSE;
10566             }
10567             if (fenMode || line[0] == '#') pn--;
10568         }
10569     }
10570
10571     if (fenMode) {
10572         if (!ParseFEN(initial_position, &blackPlaysFirst, line)) {
10573             DisplayError(_("Bad FEN position in file"), 0);
10574             return FALSE;
10575         }
10576     } else {
10577         (void) fgets(line, MSG_SIZ, f);
10578         (void) fgets(line, MSG_SIZ, f);
10579     
10580         for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
10581             (void) fgets(line, MSG_SIZ, f);
10582             for (p = line, j = BOARD_LEFT; j < BOARD_RGHT; p++) {
10583                 if (*p == ' ')
10584                   continue;
10585                 initial_position[i][j++] = CharToPiece(*p);
10586             }
10587         }
10588     
10589         blackPlaysFirst = FALSE;
10590         if (!feof(f)) {
10591             (void) fgets(line, MSG_SIZ, f);
10592             if (strncmp(line, "black", strlen("black"))==0)
10593               blackPlaysFirst = TRUE;
10594         }
10595     }
10596     startedFromSetupPosition = TRUE;
10597     
10598     SendToProgram("force\n", &first);
10599     CopyBoard(boards[0], initial_position);
10600     if (blackPlaysFirst) {
10601         currentMove = forwardMostMove = backwardMostMove = 1;
10602         strcpy(moveList[0], "");
10603         strcpy(parseList[0], "");
10604         CopyBoard(boards[1], initial_position);
10605         DisplayMessage("", _("Black to play"));
10606     } else {
10607         currentMove = forwardMostMove = backwardMostMove = 0;
10608         DisplayMessage("", _("White to play"));
10609     }
10610     initialRulePlies = FENrulePlies; /* [HGM] copy FEN attributes as well */
10611     SendBoard(&first, forwardMostMove);
10612     if (appData.debugMode) {
10613 int i, j;
10614   for(i=0;i<2;i++){for(j=0;j<6;j++)fprintf(debugFP, " %d", boards[i][CASTLING][j]);fprintf(debugFP,"\n");}
10615   for(j=0;j<6;j++)fprintf(debugFP, " %d", initialRights[j]);fprintf(debugFP,"\n");
10616         fprintf(debugFP, "Load Position\n");
10617     }
10618
10619     if (positionNumber > 1) {
10620         sprintf(line, "%s %d", title, positionNumber);
10621         DisplayTitle(line);
10622     } else {
10623         DisplayTitle(title);
10624     }
10625     gameMode = EditGame;
10626     ModeHighlight();
10627     ResetClocks();
10628     timeRemaining[0][1] = whiteTimeRemaining;
10629     timeRemaining[1][1] = blackTimeRemaining;
10630     DrawPosition(FALSE, boards[currentMove]);
10631    
10632     return TRUE;
10633 }
10634
10635
10636 void
10637 CopyPlayerNameIntoFileName(dest, src)
10638      char **dest, *src;
10639 {
10640     while (*src != NULLCHAR && *src != ',') {
10641         if (*src == ' ') {
10642             *(*dest)++ = '_';
10643             src++;
10644         } else {
10645             *(*dest)++ = *src++;
10646         }
10647     }
10648 }
10649
10650 char *DefaultFileName(ext)
10651      char *ext;
10652 {
10653     static char def[MSG_SIZ];
10654     char *p;
10655
10656     if (gameInfo.white != NULL && gameInfo.white[0] != '-') {
10657         p = def;
10658         CopyPlayerNameIntoFileName(&p, gameInfo.white);
10659         *p++ = '-';
10660         CopyPlayerNameIntoFileName(&p, gameInfo.black);
10661         *p++ = '.';
10662         strcpy(p, ext);
10663     } else {
10664         def[0] = NULLCHAR;
10665     }
10666     return def;
10667 }
10668
10669 /* Save the current game to the given file */
10670 int
10671 SaveGameToFile(filename, append)
10672      char *filename;
10673      int append;
10674 {
10675     FILE *f;
10676     char buf[MSG_SIZ];
10677
10678     if (strcmp(filename, "-") == 0) {
10679         return SaveGame(stdout, 0, NULL);
10680     } else {
10681         f = fopen(filename, append ? "a" : "w");
10682         if (f == NULL) {
10683             snprintf(buf, sizeof(buf), _("Can't open \"%s\""), filename);
10684             DisplayError(buf, errno);
10685             return FALSE;
10686         } else {
10687             return SaveGame(f, 0, NULL);
10688         }
10689     }
10690 }
10691
10692 char *
10693 SavePart(str)
10694      char *str;
10695 {
10696     static char buf[MSG_SIZ];
10697     char *p;
10698     
10699     p = strchr(str, ' ');
10700     if (p == NULL) return str;
10701     strncpy(buf, str, p - str);
10702     buf[p - str] = NULLCHAR;
10703     return buf;
10704 }
10705
10706 #define PGN_MAX_LINE 75
10707
10708 #define PGN_SIDE_WHITE  0
10709 #define PGN_SIDE_BLACK  1
10710
10711 /* [AS] */
10712 static int FindFirstMoveOutOfBook( int side )
10713 {
10714     int result = -1;
10715
10716     if( backwardMostMove == 0 && ! startedFromSetupPosition) {
10717         int index = backwardMostMove;
10718         int has_book_hit = 0;
10719
10720         if( (index % 2) != side ) {
10721             index++;
10722         }
10723
10724         while( index < forwardMostMove ) {
10725             /* Check to see if engine is in book */
10726             int depth = pvInfoList[index].depth;
10727             int score = pvInfoList[index].score;
10728             int in_book = 0;
10729
10730             if( depth <= 2 ) {
10731                 in_book = 1;
10732             }
10733             else if( score == 0 && depth == 63 ) {
10734                 in_book = 1; /* Zappa */
10735             }
10736             else if( score == 2 && depth == 99 ) {
10737                 in_book = 1; /* Abrok */
10738             }
10739
10740             has_book_hit += in_book;
10741
10742             if( ! in_book ) {
10743                 result = index;
10744
10745                 break;
10746             }
10747
10748             index += 2;
10749         }
10750     }
10751
10752     return result;
10753 }
10754
10755 /* [AS] */
10756 void GetOutOfBookInfo( char * buf )
10757 {
10758     int oob[2];
10759     int i;
10760     int offset = backwardMostMove & (~1L); /* output move numbers start at 1 */
10761
10762     oob[0] = FindFirstMoveOutOfBook( PGN_SIDE_WHITE );
10763     oob[1] = FindFirstMoveOutOfBook( PGN_SIDE_BLACK );
10764
10765     *buf = '\0';
10766
10767     if( oob[0] >= 0 || oob[1] >= 0 ) {
10768         for( i=0; i<2; i++ ) {
10769             int idx = oob[i];
10770
10771             if( idx >= 0 ) {
10772                 if( i > 0 && oob[0] >= 0 ) {
10773                     strcat( buf, "   " );
10774                 }
10775
10776                 sprintf( buf+strlen(buf), "%d%s. ", (idx - offset)/2 + 1, idx & 1 ? ".." : "" );
10777                 sprintf( buf+strlen(buf), "%s%.2f", 
10778                     pvInfoList[idx].score >= 0 ? "+" : "",
10779                     pvInfoList[idx].score / 100.0 );
10780             }
10781         }
10782     }
10783 }
10784
10785 /* Save game in PGN style and close the file */
10786 int
10787 SaveGamePGN(f)
10788      FILE *f;
10789 {
10790     int i, offset, linelen, newblock;
10791     time_t tm;
10792 //    char *movetext;
10793     char numtext[32];
10794     int movelen, numlen, blank;
10795     char move_buffer[100]; /* [AS] Buffer for move+PV info */
10796
10797     offset = backwardMostMove & (~1L); /* output move numbers start at 1 */
10798     
10799     tm = time((time_t *) NULL);
10800     
10801     PrintPGNTags(f, &gameInfo);
10802     
10803     if (backwardMostMove > 0 || startedFromSetupPosition) {
10804         char *fen = PositionToFEN(backwardMostMove, NULL);
10805         fprintf(f, "[FEN \"%s\"]\n[SetUp \"1\"]\n", fen);
10806         fprintf(f, "\n{--------------\n");
10807         PrintPosition(f, backwardMostMove);
10808         fprintf(f, "--------------}\n");
10809         free(fen);
10810     }
10811     else {
10812         /* [AS] Out of book annotation */
10813         if( appData.saveOutOfBookInfo ) {
10814             char buf[64];
10815
10816             GetOutOfBookInfo( buf );
10817
10818             if( buf[0] != '\0' ) {
10819                 fprintf( f, "[%s \"%s\"]\n", PGN_OUT_OF_BOOK, buf ); 
10820             }
10821         }
10822
10823         fprintf(f, "\n");
10824     }
10825
10826     i = backwardMostMove;
10827     linelen = 0;
10828     newblock = TRUE;
10829
10830     while (i < forwardMostMove) {
10831         /* Print comments preceding this move */
10832         if (commentList[i] != NULL) {
10833             if (linelen > 0) fprintf(f, "\n");
10834             fprintf(f, "%s", commentList[i]);
10835             linelen = 0;
10836             newblock = TRUE;
10837         }
10838
10839         /* Format move number */
10840         if ((i % 2) == 0) {
10841             sprintf(numtext, "%d.", (i - offset)/2 + 1);
10842         } else {
10843             if (newblock) {
10844                 sprintf(numtext, "%d...", (i - offset)/2 + 1);
10845             } else {
10846                 numtext[0] = NULLCHAR;
10847             }
10848         }
10849         numlen = strlen(numtext);
10850         newblock = FALSE;
10851
10852         /* Print move number */
10853         blank = linelen > 0 && numlen > 0;
10854         if (linelen + (blank ? 1 : 0) + numlen > PGN_MAX_LINE) {
10855             fprintf(f, "\n");
10856             linelen = 0;
10857             blank = 0;
10858         }
10859         if (blank) {
10860             fprintf(f, " ");
10861             linelen++;
10862         }
10863         fprintf(f, "%s", numtext);
10864         linelen += numlen;
10865
10866         /* Get move */
10867         strcpy(move_buffer, SavePart(parseList[i])); // [HGM] pgn: print move via buffer, so it can be edited
10868         movelen = strlen(move_buffer); /* [HGM] pgn: line-break point before move */
10869
10870         /* Print move */
10871         blank = linelen > 0 && movelen > 0;
10872         if (linelen + (blank ? 1 : 0) + movelen > PGN_MAX_LINE) {
10873             fprintf(f, "\n");
10874             linelen = 0;
10875             blank = 0;
10876         }
10877         if (blank) {
10878             fprintf(f, " ");
10879             linelen++;
10880         }
10881         fprintf(f, "%s", move_buffer);
10882         linelen += movelen;
10883
10884         /* [AS] Add PV info if present */
10885         if( i >= 0 && appData.saveExtendedInfoInPGN && pvInfoList[i].depth > 0 ) {
10886             /* [HGM] add time */
10887             char buf[MSG_SIZ]; int seconds;
10888
10889             seconds = (pvInfoList[i].time+5)/10; // deci-seconds, rounded to nearest
10890
10891             if( seconds <= 0) buf[0] = 0; else
10892             if( seconds < 30 ) sprintf(buf, " %3.1f%c", seconds/10., 0); else {
10893                 seconds = (seconds + 4)/10; // round to full seconds
10894                 if( seconds < 60 ) sprintf(buf, " %d%c", seconds, 0); else
10895                                    sprintf(buf, " %d:%02d%c", seconds/60, seconds%60, 0);
10896             }
10897
10898             sprintf( move_buffer, "{%s%.2f/%d%s}", 
10899                 pvInfoList[i].score >= 0 ? "+" : "",
10900                 pvInfoList[i].score / 100.0,
10901                 pvInfoList[i].depth,
10902                 buf );
10903
10904             movelen = strlen(move_buffer); /* [HGM] pgn: line-break point after move */
10905
10906             /* Print score/depth */
10907             blank = linelen > 0 && movelen > 0;
10908             if (linelen + (blank ? 1 : 0) + movelen > PGN_MAX_LINE) {
10909                 fprintf(f, "\n");
10910                 linelen = 0;
10911                 blank = 0;
10912             }
10913             if (blank) {
10914                 fprintf(f, " ");
10915                 linelen++;
10916             }
10917             fprintf(f, "%s", move_buffer);
10918             linelen += movelen;
10919         }
10920
10921         i++;
10922     }
10923     
10924     /* Start a new line */
10925     if (linelen > 0) fprintf(f, "\n");
10926
10927     /* Print comments after last move */
10928     if (commentList[i] != NULL) {
10929         fprintf(f, "%s\n", commentList[i]);
10930     }
10931
10932     /* Print result */
10933     if (gameInfo.resultDetails != NULL &&
10934         gameInfo.resultDetails[0] != NULLCHAR) {
10935         fprintf(f, "{%s} %s\n\n", gameInfo.resultDetails,
10936                 PGNResult(gameInfo.result));
10937     } else {
10938         fprintf(f, "%s\n\n", PGNResult(gameInfo.result));
10939     }
10940
10941     fclose(f);
10942     lastSavedGame = GameCheckSum(); // [HGM] save: remember ID of last saved game to prevent double saving
10943     return TRUE;
10944 }
10945
10946 /* Save game in old style and close the file */
10947 int
10948 SaveGameOldStyle(f)
10949      FILE *f;
10950 {
10951     int i, offset;
10952     time_t tm;
10953     
10954     tm = time((time_t *) NULL);
10955     
10956     fprintf(f, "# %s game file -- %s", programName, ctime(&tm));
10957     PrintOpponents(f);
10958     
10959     if (backwardMostMove > 0 || startedFromSetupPosition) {
10960         fprintf(f, "\n[--------------\n");
10961         PrintPosition(f, backwardMostMove);
10962         fprintf(f, "--------------]\n");
10963     } else {
10964         fprintf(f, "\n");
10965     }
10966
10967     i = backwardMostMove;
10968     offset = backwardMostMove & (~1L); /* output move numbers start at 1 */
10969
10970     while (i < forwardMostMove) {
10971         if (commentList[i] != NULL) {
10972             fprintf(f, "[%s]\n", commentList[i]);
10973         }
10974
10975         if ((i % 2) == 1) {
10976             fprintf(f, "%d. ...  %s\n", (i - offset)/2 + 1, parseList[i]);
10977             i++;
10978         } else {
10979             fprintf(f, "%d. %s  ", (i - offset)/2 + 1, parseList[i]);
10980             i++;
10981             if (commentList[i] != NULL) {
10982                 fprintf(f, "\n");
10983                 continue;
10984             }
10985             if (i >= forwardMostMove) {
10986                 fprintf(f, "\n");
10987                 break;
10988             }
10989             fprintf(f, "%s\n", parseList[i]);
10990             i++;
10991         }
10992     }
10993     
10994     if (commentList[i] != NULL) {
10995         fprintf(f, "[%s]\n", commentList[i]);
10996     }
10997
10998     /* This isn't really the old style, but it's close enough */
10999     if (gameInfo.resultDetails != NULL &&
11000         gameInfo.resultDetails[0] != NULLCHAR) {
11001         fprintf(f, "%s (%s)\n\n", PGNResult(gameInfo.result),
11002                 gameInfo.resultDetails);
11003     } else {
11004         fprintf(f, "%s\n\n", PGNResult(gameInfo.result));
11005     }
11006
11007     fclose(f);
11008     return TRUE;
11009 }
11010
11011 /* Save the current game to open file f and close the file */
11012 int
11013 SaveGame(f, dummy, dummy2)
11014      FILE *f;
11015      int dummy;
11016      char *dummy2;
11017 {
11018     if (gameMode == EditPosition) EditPositionDone(TRUE);
11019     lastSavedGame = GameCheckSum(); // [HGM] save: remember ID of last saved game to prevent double saving
11020     if (appData.oldSaveStyle)
11021       return SaveGameOldStyle(f);
11022     else
11023       return SaveGamePGN(f);
11024 }
11025
11026 /* Save the current position to the given file */
11027 int
11028 SavePositionToFile(filename)
11029      char *filename;
11030 {
11031     FILE *f;
11032     char buf[MSG_SIZ];
11033
11034     if (strcmp(filename, "-") == 0) {
11035         return SavePosition(stdout, 0, NULL);
11036     } else {
11037         f = fopen(filename, "a");
11038         if (f == NULL) {
11039             snprintf(buf, sizeof(buf), _("Can't open \"%s\""), filename);
11040             DisplayError(buf, errno);
11041             return FALSE;
11042         } else {
11043             SavePosition(f, 0, NULL);
11044             return TRUE;
11045         }
11046     }
11047 }
11048
11049 /* Save the current position to the given open file and close the file */
11050 int
11051 SavePosition(f, dummy, dummy2)
11052      FILE *f;
11053      int dummy;
11054      char *dummy2;
11055 {
11056     time_t tm;
11057     char *fen;
11058     
11059     if (gameMode == EditPosition) EditPositionDone(TRUE);
11060     if (appData.oldSaveStyle) {
11061         tm = time((time_t *) NULL);
11062     
11063         fprintf(f, "# %s position file -- %s", programName, ctime(&tm));
11064         PrintOpponents(f);
11065         fprintf(f, "[--------------\n");
11066         PrintPosition(f, currentMove);
11067         fprintf(f, "--------------]\n");
11068     } else {
11069         fen = PositionToFEN(currentMove, NULL);
11070         fprintf(f, "%s\n", fen);
11071         free(fen);
11072     }
11073     fclose(f);
11074     return TRUE;
11075 }
11076
11077 void
11078 ReloadCmailMsgEvent(unregister)
11079      int unregister;
11080 {
11081 #if !WIN32
11082     static char *inFilename = NULL;
11083     static char *outFilename;
11084     int i;
11085     struct stat inbuf, outbuf;
11086     int status;
11087     
11088     /* Any registered moves are unregistered if unregister is set, */
11089     /* i.e. invoked by the signal handler */
11090     if (unregister) {
11091         for (i = 0; i < CMAIL_MAX_GAMES; i ++) {
11092             cmailMoveRegistered[i] = FALSE;
11093             if (cmailCommentList[i] != NULL) {
11094                 free(cmailCommentList[i]);
11095                 cmailCommentList[i] = NULL;
11096             }
11097         }
11098         nCmailMovesRegistered = 0;
11099     }
11100
11101     for (i = 0; i < CMAIL_MAX_GAMES; i ++) {
11102         cmailResult[i] = CMAIL_NOT_RESULT;
11103     }
11104     nCmailResults = 0;
11105
11106     if (inFilename == NULL) {
11107         /* Because the filenames are static they only get malloced once  */
11108         /* and they never get freed                                      */
11109         inFilename = (char *) malloc(strlen(appData.cmailGameName) + 9);
11110         sprintf(inFilename, "%s.game.in", appData.cmailGameName);
11111
11112         outFilename = (char *) malloc(strlen(appData.cmailGameName) + 5);
11113         sprintf(outFilename, "%s.out", appData.cmailGameName);
11114     }
11115     
11116     status = stat(outFilename, &outbuf);
11117     if (status < 0) {
11118         cmailMailedMove = FALSE;
11119     } else {
11120         status = stat(inFilename, &inbuf);
11121         cmailMailedMove = (inbuf.st_mtime < outbuf.st_mtime);
11122     }
11123     
11124     /* LoadGameFromFile(CMAIL_MAX_GAMES) with cmailMsgLoaded == TRUE
11125        counts the games, notes how each one terminated, etc.
11126        
11127        It would be nice to remove this kludge and instead gather all
11128        the information while building the game list.  (And to keep it
11129        in the game list nodes instead of having a bunch of fixed-size
11130        parallel arrays.)  Note this will require getting each game's
11131        termination from the PGN tags, as the game list builder does
11132        not process the game moves.  --mann
11133        */
11134     cmailMsgLoaded = TRUE;
11135     LoadGameFromFile(inFilename, CMAIL_MAX_GAMES, "", FALSE);
11136     
11137     /* Load first game in the file or popup game menu */
11138     LoadGameFromFile(inFilename, 0, appData.cmailGameName, TRUE);
11139
11140 #endif /* !WIN32 */
11141     return;
11142 }
11143
11144 int
11145 RegisterMove()
11146 {
11147     FILE *f;
11148     char string[MSG_SIZ];
11149
11150     if (   cmailMailedMove
11151         || (cmailResult[lastLoadGameNumber - 1] == CMAIL_OLD_RESULT)) {
11152         return TRUE;            /* Allow free viewing  */
11153     }
11154
11155     /* Unregister move to ensure that we don't leave RegisterMove        */
11156     /* with the move registered when the conditions for registering no   */
11157     /* longer hold                                                       */
11158     if (cmailMoveRegistered[lastLoadGameNumber - 1]) {
11159         cmailMoveRegistered[lastLoadGameNumber - 1] = FALSE;
11160         nCmailMovesRegistered --;
11161
11162         if (cmailCommentList[lastLoadGameNumber - 1] != NULL) 
11163           {
11164               free(cmailCommentList[lastLoadGameNumber - 1]);
11165               cmailCommentList[lastLoadGameNumber - 1] = NULL;
11166           }
11167     }
11168
11169     if (cmailOldMove == -1) {
11170         DisplayError(_("You have edited the game history.\nUse Reload Same Game and make your move again."), 0);
11171         return FALSE;
11172     }
11173
11174     if (currentMove > cmailOldMove + 1) {
11175         DisplayError(_("You have entered too many moves.\nBack up to the correct position and try again."), 0);
11176         return FALSE;
11177     }
11178
11179     if (currentMove < cmailOldMove) {
11180         DisplayError(_("Displayed position is not current.\nStep forward to the correct position and try again."), 0);
11181         return FALSE;
11182     }
11183
11184     if (forwardMostMove > currentMove) {
11185         /* Silently truncate extra moves */
11186         TruncateGame();
11187     }
11188
11189     if (   (currentMove == cmailOldMove + 1)
11190         || (   (currentMove == cmailOldMove)
11191             && (   (cmailMoveType[lastLoadGameNumber - 1] == CMAIL_ACCEPT)
11192                 || (cmailMoveType[lastLoadGameNumber - 1] == CMAIL_RESIGN)))) {
11193         if (gameInfo.result != GameUnfinished) {
11194             cmailResult[lastLoadGameNumber - 1] = CMAIL_NEW_RESULT;
11195         }
11196
11197         if (commentList[currentMove] != NULL) {
11198             cmailCommentList[lastLoadGameNumber - 1]
11199               = StrSave(commentList[currentMove]);
11200         }
11201         strcpy(cmailMove[lastLoadGameNumber - 1], moveList[currentMove - 1]);
11202
11203         if (appData.debugMode)
11204           fprintf(debugFP, "Saving %s for game %d\n",
11205                   cmailMove[lastLoadGameNumber - 1], lastLoadGameNumber);
11206
11207         sprintf(string,
11208                 "%s.game.out.%d", appData.cmailGameName, lastLoadGameNumber);
11209         
11210         f = fopen(string, "w");
11211         if (appData.oldSaveStyle) {
11212             SaveGameOldStyle(f); /* also closes the file */
11213             
11214             sprintf(string, "%s.pos.out", appData.cmailGameName);
11215             f = fopen(string, "w");
11216             SavePosition(f, 0, NULL); /* also closes the file */
11217         } else {
11218             fprintf(f, "{--------------\n");
11219             PrintPosition(f, currentMove);
11220             fprintf(f, "--------------}\n\n");
11221             
11222             SaveGame(f, 0, NULL); /* also closes the file*/
11223         }
11224         
11225         cmailMoveRegistered[lastLoadGameNumber - 1] = TRUE;
11226         nCmailMovesRegistered ++;
11227     } else if (nCmailGames == 1) {
11228         DisplayError(_("You have not made a move yet"), 0);
11229         return FALSE;
11230     }
11231
11232     return TRUE;
11233 }
11234
11235 void
11236 MailMoveEvent()
11237 {
11238 #if !WIN32
11239     static char *partCommandString = "cmail -xv%s -remail -game %s 2>&1";
11240     FILE *commandOutput;
11241     char buffer[MSG_SIZ], msg[MSG_SIZ], string[MSG_SIZ];
11242     int nBytes = 0;             /*  Suppress warnings on uninitialized variables    */
11243     int nBuffers;
11244     int i;
11245     int archived;
11246     char *arcDir;
11247
11248     if (! cmailMsgLoaded) {
11249         DisplayError(_("The cmail message is not loaded.\nUse Reload CMail Message and make your move again."), 0);
11250         return;
11251     }
11252
11253     if (nCmailGames == nCmailResults) {
11254         DisplayError(_("No unfinished games"), 0);
11255         return;
11256     }
11257
11258 #if CMAIL_PROHIBIT_REMAIL
11259     if (cmailMailedMove) {
11260         sprintf(msg, _("You have already mailed a move.\nWait until a move arrives from your opponent.\nTo resend the same move, type\n\"cmail -remail -game %s\"\non the command line."), appData.cmailGameName);
11261         DisplayError(msg, 0);
11262         return;
11263     }
11264 #endif
11265
11266     if (! (cmailMailedMove || RegisterMove())) return;
11267     
11268     if (   cmailMailedMove
11269         || (nCmailMovesRegistered + nCmailResults == nCmailGames)) {
11270         sprintf(string, partCommandString,
11271                 appData.debugMode ? " -v" : "", appData.cmailGameName);
11272         commandOutput = popen(string, "r");
11273
11274         if (commandOutput == NULL) {
11275             DisplayError(_("Failed to invoke cmail"), 0);
11276         } else {
11277             for (nBuffers = 0; (! feof(commandOutput)); nBuffers ++) {
11278                 nBytes = fread(buffer, 1, MSG_SIZ - 1, commandOutput);
11279             }
11280             if (nBuffers > 1) {
11281                 (void) memcpy(msg, buffer + nBytes, MSG_SIZ - nBytes - 1);
11282                 (void) memcpy(msg + MSG_SIZ - nBytes - 1, buffer, nBytes);
11283                 nBytes = MSG_SIZ - 1;
11284             } else {
11285                 (void) memcpy(msg, buffer, nBytes);
11286             }
11287             *(msg + nBytes) = '\0'; /* \0 for end-of-string*/
11288
11289             if(StrStr(msg, "Mailed cmail message to ") != NULL) {
11290                 cmailMailedMove = TRUE; /* Prevent >1 moves    */
11291
11292                 archived = TRUE;
11293                 for (i = 0; i < nCmailGames; i ++) {
11294                     if (cmailResult[i] == CMAIL_NOT_RESULT) {
11295                         archived = FALSE;
11296                     }
11297                 }
11298                 if (   archived
11299                     && (   (arcDir = (char *) getenv("CMAIL_ARCDIR"))
11300                         != NULL)) {
11301                     sprintf(buffer, "%s/%s.%s.archive",
11302                             arcDir,
11303                             appData.cmailGameName,
11304                             gameInfo.date);
11305                     LoadGameFromFile(buffer, 1, buffer, FALSE);
11306                     cmailMsgLoaded = FALSE;
11307                 }
11308             }
11309
11310             DisplayInformation(msg);
11311             pclose(commandOutput);
11312         }
11313     } else {
11314         if ((*cmailMsg) != '\0') {
11315             DisplayInformation(cmailMsg);
11316         }
11317     }
11318
11319     return;
11320 #endif /* !WIN32 */
11321 }
11322
11323 char *
11324 CmailMsg()
11325 {
11326 #if WIN32
11327     return NULL;
11328 #else
11329     int  prependComma = 0;
11330     char number[5];
11331     char string[MSG_SIZ];       /* Space for game-list */
11332     int  i;
11333     
11334     if (!cmailMsgLoaded) return "";
11335
11336     if (cmailMailedMove) {
11337         sprintf(cmailMsg, _("Waiting for reply from opponent\n"));
11338     } else {
11339         /* Create a list of games left */
11340         sprintf(string, "[");
11341         for (i = 0; i < nCmailGames; i ++) {
11342             if (! (   cmailMoveRegistered[i]
11343                    || (cmailResult[i] == CMAIL_OLD_RESULT))) {
11344                 if (prependComma) {
11345                     sprintf(number, ",%d", i + 1);
11346                 } else {
11347                     sprintf(number, "%d", i + 1);
11348                     prependComma = 1;
11349                 }
11350                 
11351                 strcat(string, number);
11352             }
11353         }
11354         strcat(string, "]");
11355
11356         if (nCmailMovesRegistered + nCmailResults == 0) {
11357             switch (nCmailGames) {
11358               case 1:
11359                 sprintf(cmailMsg,
11360                         _("Still need to make move for game\n"));
11361                 break;
11362                 
11363               case 2:
11364                 sprintf(cmailMsg,
11365                         _("Still need to make moves for both games\n"));
11366                 break;
11367                 
11368               default:
11369                 sprintf(cmailMsg,
11370                         _("Still need to make moves for all %d games\n"),
11371                         nCmailGames);
11372                 break;
11373             }
11374         } else {
11375             switch (nCmailGames - nCmailMovesRegistered - nCmailResults) {
11376               case 1:
11377                 sprintf(cmailMsg,
11378                         _("Still need to make a move for game %s\n"),
11379                         string);
11380                 break;
11381                 
11382               case 0:
11383                 if (nCmailResults == nCmailGames) {
11384                     sprintf(cmailMsg, _("No unfinished games\n"));
11385                 } else {
11386                     sprintf(cmailMsg, _("Ready to send mail\n"));
11387                 }
11388                 break;
11389                 
11390               default:
11391                 sprintf(cmailMsg,
11392                         _("Still need to make moves for games %s\n"),
11393                         string);
11394             }
11395         }
11396     }
11397     return cmailMsg;
11398 #endif /* WIN32 */
11399 }
11400
11401 void
11402 ResetGameEvent()
11403 {
11404     if (gameMode == Training)
11405       SetTrainingModeOff();
11406
11407     Reset(TRUE, TRUE);
11408     cmailMsgLoaded = FALSE;
11409     if (appData.icsActive) {
11410       SendToICS(ics_prefix);
11411       SendToICS("refresh\n");
11412     }
11413 }
11414
11415 void
11416 ExitEvent(status)
11417      int status;
11418 {
11419     exiting++;
11420     if (exiting > 2) {
11421       /* Give up on clean exit */
11422       exit(status);
11423     }
11424     if (exiting > 1) {
11425       /* Keep trying for clean exit */
11426       return;
11427     }
11428
11429     if (appData.icsActive && appData.colorize) Colorize(ColorNone, FALSE);
11430
11431     if (telnetISR != NULL) {
11432       RemoveInputSource(telnetISR);
11433     }
11434     if (icsPR != NoProc) {
11435       DestroyChildProcess(icsPR, TRUE);
11436     }
11437
11438     /* [HGM] crash: leave writing PGN and position entirely to GameEnds() */
11439     GameEnds(gameInfo.result, gameInfo.resultDetails==NULL ? "xboard exit" : gameInfo.resultDetails, GE_PLAYER);
11440
11441     /* [HGM] crash: the above GameEnds() is a dud if another one was running */
11442     /* make sure this other one finishes before killing it!                  */
11443     if(endingGame) { int count = 0;
11444         if(appData.debugMode) fprintf(debugFP, "ExitEvent() during GameEnds(), wait\n");
11445         while(endingGame && count++ < 10) DoSleep(1);
11446         if(appData.debugMode && endingGame) fprintf(debugFP, "GameEnds() seems stuck, proceed exiting\n");
11447     }
11448
11449     /* Kill off chess programs */
11450     if (first.pr != NoProc) {
11451         ExitAnalyzeMode();
11452         
11453         DoSleep( appData.delayBeforeQuit );
11454         SendToProgram("quit\n", &first);
11455         DoSleep( appData.delayAfterQuit );
11456         DestroyChildProcess(first.pr, 10 /* [AS] first.useSigterm */ );
11457     }
11458     if (second.pr != NoProc) {
11459         DoSleep( appData.delayBeforeQuit );
11460         SendToProgram("quit\n", &second);
11461         DoSleep( appData.delayAfterQuit );
11462         DestroyChildProcess(second.pr, 10 /* [AS] second.useSigterm */ );
11463     }
11464     if (first.isr != NULL) {
11465         RemoveInputSource(first.isr);
11466     }
11467     if (second.isr != NULL) {
11468         RemoveInputSource(second.isr);
11469     }
11470
11471     ShutDownFrontEnd();
11472     exit(status);
11473 }
11474
11475 void
11476 PauseEvent()
11477 {
11478     if (appData.debugMode)
11479         fprintf(debugFP, "PauseEvent(): pausing %d\n", pausing);
11480     if (pausing) {
11481         pausing = FALSE;
11482         ModeHighlight();
11483         if (gameMode == MachinePlaysWhite ||
11484             gameMode == MachinePlaysBlack) {
11485             StartClocks();
11486         } else {
11487             DisplayBothClocks();
11488         }
11489         if (gameMode == PlayFromGameFile) {
11490             if (appData.timeDelay >= 0) 
11491                 AutoPlayGameLoop();
11492         } else if (gameMode == IcsExamining && pauseExamInvalid) {
11493             Reset(FALSE, TRUE);
11494             SendToICS(ics_prefix);
11495             SendToICS("refresh\n");
11496         } else if (currentMove < forwardMostMove) {
11497             ForwardInner(forwardMostMove);
11498         }
11499         pauseExamInvalid = FALSE;
11500     } else {
11501         switch (gameMode) {
11502           default:
11503             return;
11504           case IcsExamining:
11505             pauseExamForwardMostMove = forwardMostMove;
11506             pauseExamInvalid = FALSE;
11507             /* fall through */
11508           case IcsObserving:
11509           case IcsPlayingWhite:
11510           case IcsPlayingBlack:
11511             pausing = TRUE;
11512             ModeHighlight();
11513             return;
11514           case PlayFromGameFile:
11515             (void) StopLoadGameTimer();
11516             pausing = TRUE;
11517             ModeHighlight();
11518             break;
11519           case BeginningOfGame:
11520             if (appData.icsActive) return;
11521             /* else fall through */
11522           case MachinePlaysWhite:
11523           case MachinePlaysBlack:
11524           case TwoMachinesPlay:
11525             if (forwardMostMove == 0)
11526               return;           /* don't pause if no one has moved */
11527             if ((gameMode == MachinePlaysWhite &&
11528                  !WhiteOnMove(forwardMostMove)) ||
11529                 (gameMode == MachinePlaysBlack &&
11530                  WhiteOnMove(forwardMostMove))) {
11531                 StopClocks();
11532             }
11533             pausing = TRUE;
11534             ModeHighlight();
11535             break;
11536         }
11537     }
11538 }
11539
11540 void
11541 EditCommentEvent()
11542 {
11543     char title[MSG_SIZ];
11544
11545     if (currentMove < 1 || parseList[currentMove - 1][0] == NULLCHAR) {
11546         strcpy(title, _("Edit comment"));
11547     } else {
11548         sprintf(title, _("Edit comment on %d.%s%s"), (currentMove - 1) / 2 + 1,
11549                 WhiteOnMove(currentMove - 1) ? " " : ".. ",
11550                 parseList[currentMove - 1]);
11551     }
11552
11553     EditCommentPopUp(currentMove, title, commentList[currentMove]);
11554 }
11555
11556
11557 void
11558 EditTagsEvent()
11559 {
11560     char *tags = PGNTags(&gameInfo);
11561     EditTagsPopUp(tags);
11562     free(tags);
11563 }
11564
11565 void
11566 AnalyzeModeEvent()
11567 {
11568     if (appData.noChessProgram || gameMode == AnalyzeMode)
11569       return;
11570
11571     if (gameMode != AnalyzeFile) {
11572         if (!appData.icsEngineAnalyze) {
11573                EditGameEvent();
11574                if (gameMode != EditGame) return;
11575         }
11576         ResurrectChessProgram();
11577         SendToProgram("analyze\n", &first);
11578         first.analyzing = TRUE;
11579         /*first.maybeThinking = TRUE;*/
11580         first.maybeThinking = FALSE; /* avoid killing GNU Chess */
11581         EngineOutputPopUp();
11582     }
11583     if (!appData.icsEngineAnalyze) gameMode = AnalyzeMode;
11584     pausing = FALSE;
11585     ModeHighlight();
11586     SetGameInfo();
11587
11588     StartAnalysisClock();
11589     GetTimeMark(&lastNodeCountTime);
11590     lastNodeCount = 0;
11591 }
11592
11593 void
11594 AnalyzeFileEvent()
11595 {
11596     if (appData.noChessProgram || gameMode == AnalyzeFile)
11597       return;
11598
11599     if (gameMode != AnalyzeMode) {
11600         EditGameEvent();
11601         if (gameMode != EditGame) return;
11602         ResurrectChessProgram();
11603         SendToProgram("analyze\n", &first);
11604         first.analyzing = TRUE;
11605         /*first.maybeThinking = TRUE;*/
11606         first.maybeThinking = FALSE; /* avoid killing GNU Chess */
11607         EngineOutputPopUp();
11608     }
11609     gameMode = AnalyzeFile;
11610     pausing = FALSE;
11611     ModeHighlight();
11612     SetGameInfo();
11613
11614     StartAnalysisClock();
11615     GetTimeMark(&lastNodeCountTime);
11616     lastNodeCount = 0;
11617 }
11618
11619 void
11620 MachineWhiteEvent()
11621 {
11622     char buf[MSG_SIZ];
11623     char *bookHit = NULL;
11624
11625     if (appData.noChessProgram || (gameMode == MachinePlaysWhite))
11626       return;
11627
11628
11629     if (gameMode == PlayFromGameFile || 
11630         gameMode == TwoMachinesPlay  || 
11631         gameMode == Training         || 
11632         gameMode == AnalyzeMode      || 
11633         gameMode == EndOfGame)
11634         EditGameEvent();
11635
11636     if (gameMode == EditPosition) 
11637         EditPositionDone(TRUE);
11638
11639     if (!WhiteOnMove(currentMove)) {
11640         DisplayError(_("It is not White's turn"), 0);
11641         return;
11642     }
11643   
11644     if (gameMode == AnalyzeMode || gameMode == AnalyzeFile)
11645       ExitAnalyzeMode();
11646
11647     if (gameMode == EditGame || gameMode == AnalyzeMode || 
11648         gameMode == AnalyzeFile)
11649         TruncateGame();
11650
11651     ResurrectChessProgram();    /* in case it isn't running */
11652     if(gameMode == BeginningOfGame) { /* [HGM] time odds: to get right odds in human mode */
11653         gameMode = MachinePlaysWhite;
11654         ResetClocks();
11655     } else
11656     gameMode = MachinePlaysWhite;
11657     pausing = FALSE;
11658     ModeHighlight();
11659     SetGameInfo();
11660     sprintf(buf, "%s vs. %s", gameInfo.white, gameInfo.black);
11661     DisplayTitle(buf);
11662     if (first.sendName) {
11663       sprintf(buf, "name %s\n", gameInfo.black);
11664       SendToProgram(buf, &first);
11665     }
11666     if (first.sendTime) {
11667       if (first.useColors) {
11668         SendToProgram("black\n", &first); /*gnu kludge*/
11669       }
11670       SendTimeRemaining(&first, TRUE);
11671     }
11672     if (first.useColors) {
11673       SendToProgram("white\n", &first); // [HGM] book: send 'go' separately
11674     }
11675     bookHit = SendMoveToBookUser(forwardMostMove-1, &first, TRUE); // [HGM] book: send go or retrieve book move
11676     SetMachineThinkingEnables();
11677     first.maybeThinking = TRUE;
11678     StartClocks();
11679     firstMove = FALSE;
11680
11681     if (appData.autoFlipView && !flipView) {
11682       flipView = !flipView;
11683       DrawPosition(FALSE, NULL);
11684       DisplayBothClocks();       // [HGM] logo: clocks might have to be exchanged;
11685     }
11686
11687     if(bookHit) { // [HGM] book: simulate book reply
11688         static char bookMove[MSG_SIZ]; // a bit generous?
11689
11690         programStats.nodes = programStats.depth = programStats.time = 
11691         programStats.score = programStats.got_only_move = 0;
11692         sprintf(programStats.movelist, "%s (xbook)", bookHit);
11693
11694         strcpy(bookMove, "move ");
11695         strcat(bookMove, bookHit);
11696         HandleMachineMove(bookMove, &first);
11697     }
11698 }
11699
11700 void
11701 MachineBlackEvent()
11702 {
11703     char buf[MSG_SIZ];
11704    char *bookHit = NULL;
11705
11706     if (appData.noChessProgram || (gameMode == MachinePlaysBlack))
11707         return;
11708
11709
11710     if (gameMode == PlayFromGameFile || 
11711         gameMode == TwoMachinesPlay  || 
11712         gameMode == Training         || 
11713         gameMode == AnalyzeMode      || 
11714         gameMode == EndOfGame)
11715         EditGameEvent();
11716
11717     if (gameMode == EditPosition) 
11718         EditPositionDone(TRUE);
11719
11720     if (WhiteOnMove(currentMove)) {
11721         DisplayError(_("It is not Black's turn"), 0);
11722         return;
11723     }
11724     
11725     if (gameMode == AnalyzeMode || gameMode == AnalyzeFile)
11726       ExitAnalyzeMode();
11727
11728     if (gameMode == EditGame || gameMode == AnalyzeMode || 
11729         gameMode == AnalyzeFile)
11730         TruncateGame();
11731
11732     ResurrectChessProgram();    /* in case it isn't running */
11733     gameMode = MachinePlaysBlack;
11734     pausing = FALSE;
11735     ModeHighlight();
11736     SetGameInfo();
11737     sprintf(buf, "%s vs. %s", gameInfo.white, gameInfo.black);
11738     DisplayTitle(buf);
11739     if (first.sendName) {
11740       sprintf(buf, "name %s\n", gameInfo.white);
11741       SendToProgram(buf, &first);
11742     }
11743     if (first.sendTime) {
11744       if (first.useColors) {
11745         SendToProgram("white\n", &first); /*gnu kludge*/
11746       }
11747       SendTimeRemaining(&first, FALSE);
11748     }
11749     if (first.useColors) {
11750       SendToProgram("black\n", &first); // [HGM] book: 'go' sent separately
11751     }
11752     bookHit = SendMoveToBookUser(forwardMostMove-1, &first, TRUE); // [HGM] book: send go or retrieve book move
11753     SetMachineThinkingEnables();
11754     first.maybeThinking = TRUE;
11755     StartClocks();
11756
11757     if (appData.autoFlipView && flipView) {
11758       flipView = !flipView;
11759       DrawPosition(FALSE, NULL);
11760       DisplayBothClocks();       // [HGM] logo: clocks might have to be exchanged;
11761     }
11762     if(bookHit) { // [HGM] book: simulate book reply
11763         static char bookMove[MSG_SIZ]; // a bit generous?
11764
11765         programStats.nodes = programStats.depth = programStats.time = 
11766         programStats.score = programStats.got_only_move = 0;
11767         sprintf(programStats.movelist, "%s (xbook)", bookHit);
11768
11769         strcpy(bookMove, "move ");
11770         strcat(bookMove, bookHit);
11771         HandleMachineMove(bookMove, &first);
11772     }
11773 }
11774
11775
11776 void
11777 DisplayTwoMachinesTitle()
11778 {
11779     char buf[MSG_SIZ];
11780     if (appData.matchGames > 0) {
11781         if (first.twoMachinesColor[0] == 'w') {
11782             sprintf(buf, "%s vs. %s (%d-%d-%d)",
11783                     gameInfo.white, gameInfo.black,
11784                     first.matchWins, second.matchWins,
11785                     matchGame - 1 - (first.matchWins + second.matchWins));
11786         } else {
11787             sprintf(buf, "%s vs. %s (%d-%d-%d)",
11788                     gameInfo.white, gameInfo.black,
11789                     second.matchWins, first.matchWins,
11790                     matchGame - 1 - (first.matchWins + second.matchWins));
11791         }
11792     } else {
11793         sprintf(buf, "%s vs. %s", gameInfo.white, gameInfo.black);
11794     }
11795     DisplayTitle(buf);
11796 }
11797
11798 void
11799 TwoMachinesEvent P((void))
11800 {
11801     int i;
11802     char buf[MSG_SIZ];
11803     ChessProgramState *onmove;
11804     char *bookHit = NULL;
11805     
11806     if (appData.noChessProgram) return;
11807
11808     switch (gameMode) {
11809       case TwoMachinesPlay:
11810         return;
11811       case MachinePlaysWhite:
11812       case MachinePlaysBlack:
11813         if (WhiteOnMove(forwardMostMove) == (gameMode == MachinePlaysWhite)) {
11814             DisplayError(_("Wait until your turn,\nor select Move Now"), 0);
11815             return;
11816         }
11817         /* fall through */
11818       case BeginningOfGame:
11819       case PlayFromGameFile:
11820       case EndOfGame:
11821         EditGameEvent();
11822         if (gameMode != EditGame) return;
11823         break;
11824       case EditPosition:
11825         EditPositionDone(TRUE);
11826         break;
11827       case AnalyzeMode:
11828       case AnalyzeFile:
11829         ExitAnalyzeMode();
11830         break;
11831       case EditGame:
11832       default:
11833         break;
11834     }
11835
11836 //    forwardMostMove = currentMove;
11837     TruncateGame(); // [HGM] vari: MachineWhite and MachineBlack do this...
11838     ResurrectChessProgram();    /* in case first program isn't running */
11839
11840     if (second.pr == NULL) {
11841         StartChessProgram(&second);
11842         if (second.protocolVersion == 1) {
11843           TwoMachinesEventIfReady();
11844         } else {
11845           /* kludge: allow timeout for initial "feature" command */
11846           FreezeUI();
11847           DisplayMessage("", _("Starting second chess program"));
11848           ScheduleDelayedEvent(TwoMachinesEventIfReady, FEATURE_TIMEOUT);
11849         }
11850         return;
11851     }
11852     DisplayMessage("", "");
11853     InitChessProgram(&second, FALSE);
11854     SendToProgram("force\n", &second);
11855     if (startedFromSetupPosition) {
11856         SendBoard(&second, backwardMostMove);
11857     if (appData.debugMode) {
11858         fprintf(debugFP, "Two Machines\n");
11859     }
11860     }
11861     for (i = backwardMostMove; i < forwardMostMove; i++) {
11862         SendMoveToProgram(i, &second);
11863     }
11864
11865     gameMode = TwoMachinesPlay;
11866     pausing = FALSE;
11867     ModeHighlight();
11868     SetGameInfo();
11869     DisplayTwoMachinesTitle();
11870     firstMove = TRUE;
11871     if ((first.twoMachinesColor[0] == 'w') == WhiteOnMove(forwardMostMove)) {
11872         onmove = &first;
11873     } else {
11874         onmove = &second;
11875     }
11876
11877     SendToProgram(first.computerString, &first);
11878     if (first.sendName) {
11879       sprintf(buf, "name %s\n", second.tidy);
11880       SendToProgram(buf, &first);
11881     }
11882     SendToProgram(second.computerString, &second);
11883     if (second.sendName) {
11884       sprintf(buf, "name %s\n", first.tidy);
11885       SendToProgram(buf, &second);
11886     }
11887
11888     ResetClocks();
11889     if (!first.sendTime || !second.sendTime) {
11890         timeRemaining[0][forwardMostMove] = whiteTimeRemaining;
11891         timeRemaining[1][forwardMostMove] = blackTimeRemaining;
11892     }
11893     if (onmove->sendTime) {
11894       if (onmove->useColors) {
11895         SendToProgram(onmove->other->twoMachinesColor, onmove); /*gnu kludge*/
11896       }
11897       SendTimeRemaining(onmove, WhiteOnMove(forwardMostMove));
11898     }
11899     if (onmove->useColors) {
11900       SendToProgram(onmove->twoMachinesColor, onmove);
11901     }
11902     bookHit = SendMoveToBookUser(forwardMostMove-1, onmove, TRUE); // [HGM] book: send go or retrieve book move
11903 //    SendToProgram("go\n", onmove);
11904     onmove->maybeThinking = TRUE;
11905     SetMachineThinkingEnables();
11906
11907     StartClocks();
11908
11909     if(bookHit) { // [HGM] book: simulate book reply
11910         static char bookMove[MSG_SIZ]; // a bit generous?
11911
11912         programStats.nodes = programStats.depth = programStats.time = 
11913         programStats.score = programStats.got_only_move = 0;
11914         sprintf(programStats.movelist, "%s (xbook)", bookHit);
11915
11916         strcpy(bookMove, "move ");
11917         strcat(bookMove, bookHit);
11918         savedMessage = bookMove; // args for deferred call
11919         savedState = onmove;
11920         ScheduleDelayedEvent(DeferredBookMove, 1);
11921     }
11922 }
11923
11924 void
11925 TrainingEvent()
11926 {
11927     if (gameMode == Training) {
11928       SetTrainingModeOff();
11929       gameMode = PlayFromGameFile;
11930       DisplayMessage("", _("Training mode off"));
11931     } else {
11932       gameMode = Training;
11933       animateTraining = appData.animate;
11934
11935       /* make sure we are not already at the end of the game */
11936       if (currentMove < forwardMostMove) {
11937         SetTrainingModeOn();
11938         DisplayMessage("", _("Training mode on"));
11939       } else {
11940         gameMode = PlayFromGameFile;
11941         DisplayError(_("Already at end of game"), 0);
11942       }
11943     }
11944     ModeHighlight();
11945 }
11946
11947 void
11948 IcsClientEvent()
11949 {
11950     if (!appData.icsActive) return;
11951     switch (gameMode) {
11952       case IcsPlayingWhite:
11953       case IcsPlayingBlack:
11954       case IcsObserving:
11955       case IcsIdle:
11956       case BeginningOfGame:
11957       case IcsExamining:
11958         return;
11959
11960       case EditGame:
11961         break;
11962
11963       case EditPosition:
11964         EditPositionDone(TRUE);
11965         break;
11966
11967       case AnalyzeMode:
11968       case AnalyzeFile:
11969         ExitAnalyzeMode();
11970         break;
11971         
11972       default:
11973         EditGameEvent();
11974         break;
11975     }
11976
11977     gameMode = IcsIdle;
11978     ModeHighlight();
11979     return;
11980 }
11981
11982
11983 void
11984 EditGameEvent()
11985 {
11986     int i;
11987
11988     switch (gameMode) {
11989       case Training:
11990         SetTrainingModeOff();
11991         break;
11992       case MachinePlaysWhite:
11993       case MachinePlaysBlack:
11994       case BeginningOfGame:
11995         SendToProgram("force\n", &first);
11996         SetUserThinkingEnables();
11997         break;
11998       case PlayFromGameFile:
11999         (void) StopLoadGameTimer();
12000         if (gameFileFP != NULL) {
12001             gameFileFP = NULL;
12002         }
12003         break;
12004       case EditPosition:
12005         EditPositionDone(TRUE);
12006         break;
12007       case AnalyzeMode:
12008       case AnalyzeFile:
12009         ExitAnalyzeMode();
12010         SendToProgram("force\n", &first);
12011         break;
12012       case TwoMachinesPlay:
12013         GameEnds((ChessMove) 0, NULL, GE_PLAYER);
12014         ResurrectChessProgram();
12015         SetUserThinkingEnables();
12016         break;
12017       case EndOfGame:
12018         ResurrectChessProgram();
12019         break;
12020       case IcsPlayingBlack:
12021       case IcsPlayingWhite:
12022         DisplayError(_("Warning: You are still playing a game"), 0);
12023         break;
12024       case IcsObserving:
12025         DisplayError(_("Warning: You are still observing a game"), 0);
12026         break;
12027       case IcsExamining:
12028         DisplayError(_("Warning: You are still examining a game"), 0);
12029         break;
12030       case IcsIdle:
12031         break;
12032       case EditGame:
12033       default:
12034         return;
12035     }
12036     
12037     pausing = FALSE;
12038     StopClocks();
12039     first.offeredDraw = second.offeredDraw = 0;
12040
12041     if (gameMode == PlayFromGameFile) {
12042         whiteTimeRemaining = timeRemaining[0][currentMove];
12043         blackTimeRemaining = timeRemaining[1][currentMove];
12044         DisplayTitle("");
12045     }
12046
12047     if (gameMode == MachinePlaysWhite ||
12048         gameMode == MachinePlaysBlack ||
12049         gameMode == TwoMachinesPlay ||
12050         gameMode == EndOfGame) {
12051         i = forwardMostMove;
12052         while (i > currentMove) {
12053             SendToProgram("undo\n", &first);
12054             i--;
12055         }
12056         whiteTimeRemaining = timeRemaining[0][currentMove];
12057         blackTimeRemaining = timeRemaining[1][currentMove];
12058         DisplayBothClocks();
12059         if (whiteFlag || blackFlag) {
12060             whiteFlag = blackFlag = 0;
12061         }
12062         DisplayTitle("");
12063     }           
12064     
12065     gameMode = EditGame;
12066     ModeHighlight();
12067     SetGameInfo();
12068 }
12069
12070
12071 void
12072 EditPositionEvent()
12073 {
12074     if (gameMode == EditPosition) {
12075         EditGameEvent();
12076         return;
12077     }
12078     
12079     EditGameEvent();
12080     if (gameMode != EditGame) return;
12081     
12082     gameMode = EditPosition;
12083     ModeHighlight();
12084     SetGameInfo();
12085     if (currentMove > 0)
12086       CopyBoard(boards[0], boards[currentMove]);
12087     
12088     blackPlaysFirst = !WhiteOnMove(currentMove);
12089     ResetClocks();
12090     currentMove = forwardMostMove = backwardMostMove = 0;
12091     HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove-1);
12092     DisplayMove(-1);
12093 }
12094
12095 void
12096 ExitAnalyzeMode()
12097 {
12098     /* [DM] icsEngineAnalyze - possible call from other functions */
12099     if (appData.icsEngineAnalyze) {
12100         appData.icsEngineAnalyze = FALSE;
12101
12102         DisplayMessage("",_("Close ICS engine analyze..."));
12103     }
12104     if (first.analysisSupport && first.analyzing) {
12105       SendToProgram("exit\n", &first);
12106       first.analyzing = FALSE;
12107     }
12108     thinkOutput[0] = NULLCHAR;
12109 }
12110
12111 void
12112 EditPositionDone(Boolean fakeRights)
12113 {
12114     int king = gameInfo.variant == VariantKnightmate ? WhiteUnicorn : WhiteKing;
12115
12116     startedFromSetupPosition = TRUE;
12117     InitChessProgram(&first, FALSE);
12118     if(fakeRights) { // [HGM] suppress this if we just pasted a FEN.
12119       boards[0][EP_STATUS] = EP_NONE;
12120       boards[0][CASTLING][2] = boards[0][CASTLING][5] = BOARD_WIDTH>>1;
12121     if(boards[0][0][BOARD_WIDTH>>1] == king) {
12122         boards[0][CASTLING][1] = boards[0][0][BOARD_LEFT] == WhiteRook ? 0 : NoRights;
12123         boards[0][CASTLING][0] = boards[0][0][BOARD_RGHT-1] == WhiteRook ? BOARD_RGHT-1 : NoRights;
12124       } else boards[0][CASTLING][2] = NoRights;
12125     if(boards[0][BOARD_HEIGHT-1][BOARD_WIDTH>>1] == WHITE_TO_BLACK king) {
12126         boards[0][CASTLING][4] = boards[0][BOARD_HEIGHT-1][BOARD_LEFT] == BlackRook ? 0 : NoRights;
12127         boards[0][CASTLING][3] = boards[0][BOARD_HEIGHT-1][BOARD_RGHT-1] == BlackRook ? BOARD_RGHT-1 : NoRights;
12128       } else boards[0][CASTLING][5] = NoRights;
12129     }
12130     SendToProgram("force\n", &first);
12131     if (blackPlaysFirst) {
12132         strcpy(moveList[0], "");
12133         strcpy(parseList[0], "");
12134         currentMove = forwardMostMove = backwardMostMove = 1;
12135         CopyBoard(boards[1], boards[0]);
12136     } else {
12137         currentMove = forwardMostMove = backwardMostMove = 0;
12138     }
12139     SendBoard(&first, forwardMostMove);
12140     if (appData.debugMode) {
12141         fprintf(debugFP, "EditPosDone\n");
12142     }
12143     DisplayTitle("");
12144     timeRemaining[0][forwardMostMove] = whiteTimeRemaining;
12145     timeRemaining[1][forwardMostMove] = blackTimeRemaining;
12146     gameMode = EditGame;
12147     ModeHighlight();
12148     HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove-1);
12149     ClearHighlights(); /* [AS] */
12150 }
12151
12152 /* Pause for `ms' milliseconds */
12153 /* !! Ugh, this is a kludge. Fix it sometime. --tpm */
12154 void
12155 TimeDelay(ms)
12156      long ms;
12157 {
12158     TimeMark m1, m2;
12159
12160     GetTimeMark(&m1);
12161     do {
12162         GetTimeMark(&m2);
12163     } while (SubtractTimeMarks(&m2, &m1) < ms);
12164 }
12165
12166 /* !! Ugh, this is a kludge. Fix it sometime. --tpm */
12167 void
12168 SendMultiLineToICS(buf)
12169      char *buf;
12170 {
12171     char temp[MSG_SIZ+1], *p;
12172     int len;
12173
12174     len = strlen(buf);
12175     if (len > MSG_SIZ)
12176       len = MSG_SIZ;
12177   
12178     strncpy(temp, buf, len);
12179     temp[len] = 0;
12180
12181     p = temp;
12182     while (*p) {
12183         if (*p == '\n' || *p == '\r')
12184           *p = ' ';
12185         ++p;
12186     }
12187
12188     strcat(temp, "\n");
12189     SendToICS(temp);
12190     SendToPlayer(temp, strlen(temp));
12191 }
12192
12193 void
12194 SetWhiteToPlayEvent()
12195 {
12196     if (gameMode == EditPosition) {
12197         blackPlaysFirst = FALSE;
12198         DisplayBothClocks();    /* works because currentMove is 0 */
12199     } else if (gameMode == IcsExamining) {
12200         SendToICS(ics_prefix);
12201         SendToICS("tomove white\n");
12202     }
12203 }
12204
12205 void
12206 SetBlackToPlayEvent()
12207 {
12208     if (gameMode == EditPosition) {
12209         blackPlaysFirst = TRUE;
12210         currentMove = 1;        /* kludge */
12211         DisplayBothClocks();
12212         currentMove = 0;
12213     } else if (gameMode == IcsExamining) {
12214         SendToICS(ics_prefix);
12215         SendToICS("tomove black\n");
12216     }
12217 }
12218
12219 void
12220 EditPositionMenuEvent(selection, x, y)
12221      ChessSquare selection;
12222      int x, y;
12223 {
12224     char buf[MSG_SIZ];
12225     ChessSquare piece = boards[0][y][x];
12226
12227     if (gameMode != EditPosition && gameMode != IcsExamining) return;
12228
12229     switch (selection) {
12230       case ClearBoard:
12231         if (gameMode == IcsExamining && ics_type == ICS_FICS) {
12232             SendToICS(ics_prefix);
12233             SendToICS("bsetup clear\n");
12234         } else if (gameMode == IcsExamining && ics_type == ICS_ICC) {
12235             SendToICS(ics_prefix);
12236             SendToICS("clearboard\n");
12237         } else {
12238             for (x = 0; x < BOARD_WIDTH; x++) { ChessSquare p = EmptySquare;
12239                 if(x == BOARD_LEFT-1 || x == BOARD_RGHT) p = (ChessSquare) 0; /* [HGM] holdings */
12240                 for (y = 0; y < BOARD_HEIGHT; y++) {
12241                     if (gameMode == IcsExamining) {
12242                         if (boards[currentMove][y][x] != EmptySquare) {
12243                             sprintf(buf, "%sx@%c%c\n", ics_prefix,
12244                                     AAA + x, ONE + y);
12245                             SendToICS(buf);
12246                         }
12247                     } else {
12248                         boards[0][y][x] = p;
12249                     }
12250                 }
12251             }
12252         }
12253         if (gameMode == EditPosition) {
12254             DrawPosition(FALSE, boards[0]);
12255         }
12256         break;
12257
12258       case WhitePlay:
12259         SetWhiteToPlayEvent();
12260         break;
12261
12262       case BlackPlay:
12263         SetBlackToPlayEvent();
12264         break;
12265
12266       case EmptySquare:
12267         if (gameMode == IcsExamining) {
12268             if (x < BOARD_LEFT || x >= BOARD_RGHT) break; // [HGM] holdings
12269             sprintf(buf, "%sx@%c%c\n", ics_prefix, AAA + x, ONE + y);
12270             SendToICS(buf);
12271         } else {
12272             if(x < BOARD_LEFT || x >= BOARD_RGHT) {
12273                 if(x == BOARD_LEFT-2) {
12274                     if(y < BOARD_HEIGHT-1-gameInfo.holdingsSize) break;
12275                     boards[0][y][1] = 0;
12276                 } else
12277                 if(x == BOARD_RGHT+1) {
12278                     if(y >= gameInfo.holdingsSize) break;
12279                     boards[0][y][BOARD_WIDTH-2] = 0;
12280                 } else break;
12281             }
12282             boards[0][y][x] = EmptySquare;
12283             DrawPosition(FALSE, boards[0]);
12284         }
12285         break;
12286
12287       case PromotePiece:
12288         if(piece >= (int)WhitePawn && piece < (int)WhiteMan ||
12289            piece >= (int)BlackPawn && piece < (int)BlackMan   ) {
12290             selection = (ChessSquare) (PROMOTED piece);
12291         } else if(piece == EmptySquare) selection = WhiteSilver;
12292         else selection = (ChessSquare)((int)piece - 1);
12293         goto defaultlabel;
12294
12295       case DemotePiece:
12296         if(piece > (int)WhiteMan && piece <= (int)WhiteKing ||
12297            piece > (int)BlackMan && piece <= (int)BlackKing   ) {
12298             selection = (ChessSquare) (DEMOTED piece);
12299         } else if(piece == EmptySquare) selection = BlackSilver;
12300         else selection = (ChessSquare)((int)piece + 1);       
12301         goto defaultlabel;
12302
12303       case WhiteQueen:
12304       case BlackQueen:
12305         if(gameInfo.variant == VariantShatranj ||
12306            gameInfo.variant == VariantXiangqi  ||
12307            gameInfo.variant == VariantCourier  ||
12308            gameInfo.variant == VariantMakruk     )
12309             selection = (ChessSquare)((int)selection - (int)WhiteQueen + (int)WhiteFerz);
12310         goto defaultlabel;
12311
12312       case WhiteKing:
12313       case BlackKing:
12314         if(gameInfo.variant == VariantXiangqi)
12315             selection = (ChessSquare)((int)selection - (int)WhiteKing + (int)WhiteWazir);
12316         if(gameInfo.variant == VariantKnightmate)
12317             selection = (ChessSquare)((int)selection - (int)WhiteKing + (int)WhiteUnicorn);
12318       default:
12319         defaultlabel:
12320         if (gameMode == IcsExamining) {
12321             if (x < BOARD_LEFT || x >= BOARD_RGHT) break; // [HGM] holdings
12322             sprintf(buf, "%s%c@%c%c\n", ics_prefix,
12323                     PieceToChar(selection), AAA + x, ONE + y);
12324             SendToICS(buf);
12325         } else {
12326             if(x < BOARD_LEFT || x >= BOARD_RGHT) {
12327                 int n;
12328                 if(x == BOARD_LEFT-2 && selection >= BlackPawn) {
12329                     n = PieceToNumber(selection - BlackPawn);
12330                     if(n >= gameInfo.holdingsSize) { n = 0; selection = BlackPawn; }
12331                     boards[0][BOARD_HEIGHT-1-n][0] = selection;
12332                     boards[0][BOARD_HEIGHT-1-n][1]++;
12333                 } else
12334                 if(x == BOARD_RGHT+1 && selection < BlackPawn) {
12335                     n = PieceToNumber(selection);
12336                     if(n >= gameInfo.holdingsSize) { n = 0; selection = WhitePawn; }
12337                     boards[0][n][BOARD_WIDTH-1] = selection;
12338                     boards[0][n][BOARD_WIDTH-2]++;
12339                 }
12340             } else
12341             boards[0][y][x] = selection;
12342             DrawPosition(TRUE, boards[0]);
12343         }
12344         break;
12345     }
12346 }
12347
12348
12349 void
12350 DropMenuEvent(selection, x, y)
12351      ChessSquare selection;
12352      int x, y;
12353 {
12354     ChessMove moveType;
12355
12356     switch (gameMode) {
12357       case IcsPlayingWhite:
12358       case MachinePlaysBlack:
12359         if (!WhiteOnMove(currentMove)) {
12360             DisplayMoveError(_("It is Black's turn"));
12361             return;
12362         }
12363         moveType = WhiteDrop;
12364         break;
12365       case IcsPlayingBlack:
12366       case MachinePlaysWhite:
12367         if (WhiteOnMove(currentMove)) {
12368             DisplayMoveError(_("It is White's turn"));
12369             return;
12370         }
12371         moveType = BlackDrop;
12372         break;
12373       case EditGame:
12374         moveType = WhiteOnMove(currentMove) ? WhiteDrop : BlackDrop;
12375         break;
12376       default:
12377         return;
12378     }
12379
12380     if (moveType == BlackDrop && selection < BlackPawn) {
12381       selection = (ChessSquare) ((int) selection
12382                                  + (int) BlackPawn - (int) WhitePawn);
12383     }
12384     if (boards[currentMove][y][x] != EmptySquare) {
12385         DisplayMoveError(_("That square is occupied"));
12386         return;
12387     }
12388
12389     FinishMove(moveType, (int) selection, DROP_RANK, x, y, NULLCHAR);
12390 }
12391
12392 void
12393 AcceptEvent()
12394 {
12395     /* Accept a pending offer of any kind from opponent */
12396     
12397     if (appData.icsActive) {
12398         SendToICS(ics_prefix);
12399         SendToICS("accept\n");
12400     } else if (cmailMsgLoaded) {
12401         if (currentMove == cmailOldMove &&
12402             commentList[cmailOldMove] != NULL &&
12403             StrStr(commentList[cmailOldMove], WhiteOnMove(cmailOldMove) ?
12404                    "Black offers a draw" : "White offers a draw")) {
12405             TruncateGame();
12406             GameEnds(GameIsDrawn, "Draw agreed", GE_PLAYER);
12407             cmailMoveType[lastLoadGameNumber - 1] = CMAIL_ACCEPT;
12408         } else {
12409             DisplayError(_("There is no pending offer on this move"), 0);
12410             cmailMoveType[lastLoadGameNumber - 1] = CMAIL_MOVE;
12411         }
12412     } else {
12413         /* Not used for offers from chess program */
12414     }
12415 }
12416
12417 void
12418 DeclineEvent()
12419 {
12420     /* Decline a pending offer of any kind from opponent */
12421     
12422     if (appData.icsActive) {
12423         SendToICS(ics_prefix);
12424         SendToICS("decline\n");
12425     } else if (cmailMsgLoaded) {
12426         if (currentMove == cmailOldMove &&
12427             commentList[cmailOldMove] != NULL &&
12428             StrStr(commentList[cmailOldMove], WhiteOnMove(cmailOldMove) ?
12429                    "Black offers a draw" : "White offers a draw")) {
12430 #ifdef NOTDEF
12431             AppendComment(cmailOldMove, "Draw declined", TRUE);
12432             DisplayComment(cmailOldMove - 1, "Draw declined");
12433 #endif /*NOTDEF*/
12434         } else {
12435             DisplayError(_("There is no pending offer on this move"), 0);
12436         }
12437     } else {
12438         /* Not used for offers from chess program */
12439     }
12440 }
12441
12442 void
12443 RematchEvent()
12444 {
12445     /* Issue ICS rematch command */
12446     if (appData.icsActive) {
12447         SendToICS(ics_prefix);
12448         SendToICS("rematch\n");
12449     }
12450 }
12451
12452 void
12453 CallFlagEvent()
12454 {
12455     /* Call your opponent's flag (claim a win on time) */
12456     if (appData.icsActive) {
12457         SendToICS(ics_prefix);
12458         SendToICS("flag\n");
12459     } else {
12460         switch (gameMode) {
12461           default:
12462             return;
12463           case MachinePlaysWhite:
12464             if (whiteFlag) {
12465                 if (blackFlag)
12466                   GameEnds(GameIsDrawn, "Both players ran out of time",
12467                            GE_PLAYER);
12468                 else
12469                   GameEnds(BlackWins, "Black wins on time", GE_PLAYER);
12470             } else {
12471                 DisplayError(_("Your opponent is not out of time"), 0);
12472             }
12473             break;
12474           case MachinePlaysBlack:
12475             if (blackFlag) {
12476                 if (whiteFlag)
12477                   GameEnds(GameIsDrawn, "Both players ran out of time",
12478                            GE_PLAYER);
12479                 else
12480                   GameEnds(WhiteWins, "White wins on time", GE_PLAYER);
12481             } else {
12482                 DisplayError(_("Your opponent is not out of time"), 0);
12483             }
12484             break;
12485         }
12486     }
12487 }
12488
12489 void
12490 DrawEvent()
12491 {
12492     /* Offer draw or accept pending draw offer from opponent */
12493     
12494     if (appData.icsActive) {
12495         /* Note: tournament rules require draw offers to be
12496            made after you make your move but before you punch
12497            your clock.  Currently ICS doesn't let you do that;
12498            instead, you immediately punch your clock after making
12499            a move, but you can offer a draw at any time. */
12500         
12501         SendToICS(ics_prefix);
12502         SendToICS("draw\n");
12503         userOfferedDraw = TRUE; // [HGM] drawclaim: also set flag in ICS play
12504     } else if (cmailMsgLoaded) {
12505         if (currentMove == cmailOldMove &&
12506             commentList[cmailOldMove] != NULL &&
12507             StrStr(commentList[cmailOldMove], WhiteOnMove(cmailOldMove) ?
12508                    "Black offers a draw" : "White offers a draw")) {
12509             GameEnds(GameIsDrawn, "Draw agreed", GE_PLAYER);
12510             cmailMoveType[lastLoadGameNumber - 1] = CMAIL_ACCEPT;
12511         } else if (currentMove == cmailOldMove + 1) {
12512             char *offer = WhiteOnMove(cmailOldMove) ?
12513               "White offers a draw" : "Black offers a draw";
12514             AppendComment(currentMove, offer, TRUE);
12515             DisplayComment(currentMove - 1, offer);
12516             cmailMoveType[lastLoadGameNumber - 1] = CMAIL_DRAW;
12517         } else {
12518             DisplayError(_("You must make your move before offering a draw"), 0);
12519             cmailMoveType[lastLoadGameNumber - 1] = CMAIL_MOVE;
12520         }
12521     } else if (first.offeredDraw) {
12522         GameEnds(GameIsDrawn, "Draw agreed", GE_XBOARD);
12523     } else {
12524         if (first.sendDrawOffers) {
12525             SendToProgram("draw\n", &first);
12526             userOfferedDraw = TRUE;
12527         }
12528     }
12529 }
12530
12531 void
12532 AdjournEvent()
12533 {
12534     /* Offer Adjourn or accept pending Adjourn offer from opponent */
12535     
12536     if (appData.icsActive) {
12537         SendToICS(ics_prefix);
12538         SendToICS("adjourn\n");
12539     } else {
12540         /* Currently GNU Chess doesn't offer or accept Adjourns */
12541     }
12542 }
12543
12544
12545 void
12546 AbortEvent()
12547 {
12548     /* Offer Abort or accept pending Abort offer from opponent */
12549     
12550     if (appData.icsActive) {
12551         SendToICS(ics_prefix);
12552         SendToICS("abort\n");
12553     } else {
12554         GameEnds(GameUnfinished, "Game aborted", GE_PLAYER);
12555     }
12556 }
12557
12558 void
12559 ResignEvent()
12560 {
12561     /* Resign.  You can do this even if it's not your turn. */
12562     
12563     if (appData.icsActive) {
12564         SendToICS(ics_prefix);
12565         SendToICS("resign\n");
12566     } else {
12567         switch (gameMode) {
12568           case MachinePlaysWhite:
12569             GameEnds(WhiteWins, "Black resigns", GE_PLAYER);
12570             break;
12571           case MachinePlaysBlack:
12572             GameEnds(BlackWins, "White resigns", GE_PLAYER);
12573             break;
12574           case EditGame:
12575             if (cmailMsgLoaded) {
12576                 TruncateGame();
12577                 if (WhiteOnMove(cmailOldMove)) {
12578                     GameEnds(BlackWins, "White resigns", GE_PLAYER);
12579                 } else {
12580                     GameEnds(WhiteWins, "Black resigns", GE_PLAYER);
12581                 }
12582                 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_RESIGN;
12583             }
12584             break;
12585           default:
12586             break;
12587         }
12588     }
12589 }
12590
12591
12592 void
12593 StopObservingEvent()
12594 {
12595     /* Stop observing current games */
12596     SendToICS(ics_prefix);
12597     SendToICS("unobserve\n");
12598 }
12599
12600 void
12601 StopExaminingEvent()
12602 {
12603     /* Stop observing current game */
12604     SendToICS(ics_prefix);
12605     SendToICS("unexamine\n");
12606 }
12607
12608 void
12609 ForwardInner(target)
12610      int target;
12611 {
12612     int limit;
12613
12614     if (appData.debugMode)
12615         fprintf(debugFP, "ForwardInner(%d), current %d, forward %d\n",
12616                 target, currentMove, forwardMostMove);
12617
12618     if (gameMode == EditPosition)
12619       return;
12620
12621     if (gameMode == PlayFromGameFile && !pausing)
12622       PauseEvent();
12623     
12624     if (gameMode == IcsExamining && pausing)
12625       limit = pauseExamForwardMostMove;
12626     else
12627       limit = forwardMostMove;
12628     
12629     if (target > limit) target = limit;
12630
12631     if (target > 0 && moveList[target - 1][0]) {
12632         int fromX, fromY, toX, toY;
12633         toX = moveList[target - 1][2] - AAA;
12634         toY = moveList[target - 1][3] - ONE;
12635         if (moveList[target - 1][1] == '@') {
12636             if (appData.highlightLastMove) {
12637                 SetHighlights(-1, -1, toX, toY);
12638             }
12639         } else {
12640             fromX = moveList[target - 1][0] - AAA;
12641             fromY = moveList[target - 1][1] - ONE;
12642             if (target == currentMove + 1) {
12643                 AnimateMove(boards[currentMove], fromX, fromY, toX, toY);
12644             }
12645             if (appData.highlightLastMove) {
12646                 SetHighlights(fromX, fromY, toX, toY);
12647             }
12648         }
12649     }
12650     if (gameMode == EditGame || gameMode == AnalyzeMode || 
12651         gameMode == Training || gameMode == PlayFromGameFile || 
12652         gameMode == AnalyzeFile) {
12653         while (currentMove < target) {
12654             SendMoveToProgram(currentMove++, &first);
12655         }
12656     } else {
12657         currentMove = target;
12658     }
12659     
12660     if (gameMode == EditGame || gameMode == EndOfGame) {
12661         whiteTimeRemaining = timeRemaining[0][currentMove];
12662         blackTimeRemaining = timeRemaining[1][currentMove];
12663     }
12664     DisplayBothClocks();
12665     DisplayMove(currentMove - 1);
12666     DrawPosition(FALSE, boards[currentMove]);
12667     HistorySet(parseList,backwardMostMove,forwardMostMove,currentMove-1);
12668     if ( !matchMode && gameMode != Training) { // [HGM] PV info: routine tests if empty
12669         DisplayComment(currentMove - 1, commentList[currentMove]);
12670     }
12671 }
12672
12673
12674 void
12675 ForwardEvent()
12676 {
12677     if (gameMode == IcsExamining && !pausing) {
12678         SendToICS(ics_prefix);
12679         SendToICS("forward\n");
12680     } else {
12681         ForwardInner(currentMove + 1);
12682     }
12683 }
12684
12685 void
12686 ToEndEvent()
12687 {
12688     if (gameMode == AnalyzeMode || gameMode == AnalyzeFile) {
12689         /* to optimze, we temporarily turn off analysis mode while we feed
12690          * the remaining moves to the engine. Otherwise we get analysis output
12691          * after each move.
12692          */ 
12693         if (first.analysisSupport) {
12694           SendToProgram("exit\nforce\n", &first);
12695           first.analyzing = FALSE;
12696         }
12697     }
12698         
12699     if (gameMode == IcsExamining && !pausing) {
12700         SendToICS(ics_prefix);
12701         SendToICS("forward 999999\n");
12702     } else {
12703         ForwardInner(forwardMostMove);
12704     }
12705
12706     if (gameMode == AnalyzeMode || gameMode == AnalyzeFile) {
12707         /* we have fed all the moves, so reactivate analysis mode */
12708         SendToProgram("analyze\n", &first);
12709         first.analyzing = TRUE;
12710         /*first.maybeThinking = TRUE;*/
12711         first.maybeThinking = FALSE; /* avoid killing GNU Chess */
12712     }
12713 }
12714
12715 void
12716 BackwardInner(target)
12717      int target;
12718 {
12719     int full_redraw = TRUE; /* [AS] Was FALSE, had to change it! */
12720
12721     if (appData.debugMode)
12722         fprintf(debugFP, "BackwardInner(%d), current %d, forward %d\n",
12723                 target, currentMove, forwardMostMove);
12724
12725     if (gameMode == EditPosition) return;
12726     if (currentMove <= backwardMostMove) {
12727         ClearHighlights();
12728         DrawPosition(full_redraw, boards[currentMove]);
12729         return;
12730     }
12731     if (gameMode == PlayFromGameFile && !pausing)
12732       PauseEvent();
12733     
12734     if (moveList[target][0]) {
12735         int fromX, fromY, toX, toY;
12736         toX = moveList[target][2] - AAA;
12737         toY = moveList[target][3] - ONE;
12738         if (moveList[target][1] == '@') {
12739             if (appData.highlightLastMove) {
12740                 SetHighlights(-1, -1, toX, toY);
12741             }
12742         } else {
12743             fromX = moveList[target][0] - AAA;
12744             fromY = moveList[target][1] - ONE;
12745             if (target == currentMove - 1) {
12746                 AnimateMove(boards[currentMove], toX, toY, fromX, fromY);
12747             }
12748             if (appData.highlightLastMove) {
12749                 SetHighlights(fromX, fromY, toX, toY);
12750             }
12751         }
12752     }
12753     if (gameMode == EditGame || gameMode==AnalyzeMode ||
12754         gameMode == PlayFromGameFile || gameMode == AnalyzeFile) {
12755         while (currentMove > target) {
12756             SendToProgram("undo\n", &first);
12757             currentMove--;
12758         }
12759     } else {
12760         currentMove = target;
12761     }
12762     
12763     if (gameMode == EditGame || gameMode == EndOfGame) {
12764         whiteTimeRemaining = timeRemaining[0][currentMove];
12765         blackTimeRemaining = timeRemaining[1][currentMove];
12766     }
12767     DisplayBothClocks();
12768     DisplayMove(currentMove - 1);
12769     DrawPosition(full_redraw, boards[currentMove]);
12770     HistorySet(parseList,backwardMostMove,forwardMostMove,currentMove-1);
12771     // [HGM] PV info: routine tests if comment empty
12772     DisplayComment(currentMove - 1, commentList[currentMove]);
12773 }
12774
12775 void
12776 BackwardEvent()
12777 {
12778     if (gameMode == IcsExamining && !pausing) {
12779         SendToICS(ics_prefix);
12780         SendToICS("backward\n");
12781     } else {
12782         BackwardInner(currentMove - 1);
12783     }
12784 }
12785
12786 void
12787 ToStartEvent()
12788 {
12789     if (gameMode == AnalyzeMode || gameMode == AnalyzeFile) {
12790         /* to optimize, we temporarily turn off analysis mode while we undo
12791          * all the moves. Otherwise we get analysis output after each undo.
12792          */ 
12793         if (first.analysisSupport) {
12794           SendToProgram("exit\nforce\n", &first);
12795           first.analyzing = FALSE;
12796         }
12797     }
12798
12799     if (gameMode == IcsExamining && !pausing) {
12800         SendToICS(ics_prefix);
12801         SendToICS("backward 999999\n");
12802     } else {
12803         BackwardInner(backwardMostMove);
12804     }
12805
12806     if (gameMode == AnalyzeMode || gameMode == AnalyzeFile) {
12807         /* we have fed all the moves, so reactivate analysis mode */
12808         SendToProgram("analyze\n", &first);
12809         first.analyzing = TRUE;
12810         /*first.maybeThinking = TRUE;*/
12811         first.maybeThinking = FALSE; /* avoid killing GNU Chess */
12812     }
12813 }
12814
12815 void
12816 ToNrEvent(int to)
12817 {
12818   if (gameMode == PlayFromGameFile && !pausing) PauseEvent();
12819   if (to >= forwardMostMove) to = forwardMostMove;
12820   if (to <= backwardMostMove) to = backwardMostMove;
12821   if (to < currentMove) {
12822     BackwardInner(to);
12823   } else {
12824     ForwardInner(to);
12825   }
12826 }
12827
12828 void
12829 RevertEvent(Boolean annotate)
12830 {
12831     if(PopTail(annotate)) { // [HGM] vari: restore old game tail
12832         return;
12833     }
12834     if (gameMode != IcsExamining) {
12835         DisplayError(_("You are not examining a game"), 0);
12836         return;
12837     }
12838     if (pausing) {
12839         DisplayError(_("You can't revert while pausing"), 0);
12840         return;
12841     }
12842     SendToICS(ics_prefix);
12843     SendToICS("revert\n");
12844 }
12845
12846 void
12847 RetractMoveEvent()
12848 {
12849     switch (gameMode) {
12850       case MachinePlaysWhite:
12851       case MachinePlaysBlack:
12852         if (WhiteOnMove(forwardMostMove) == (gameMode == MachinePlaysWhite)) {
12853             DisplayError(_("Wait until your turn,\nor select Move Now"), 0);
12854             return;
12855         }
12856         if (forwardMostMove < 2) return;
12857         currentMove = forwardMostMove = forwardMostMove - 2;
12858         whiteTimeRemaining = timeRemaining[0][currentMove];
12859         blackTimeRemaining = timeRemaining[1][currentMove];
12860         DisplayBothClocks();
12861         DisplayMove(currentMove - 1);
12862         ClearHighlights();/*!! could figure this out*/
12863         DrawPosition(TRUE, boards[currentMove]); /* [AS] Changed to full redraw! */
12864         SendToProgram("remove\n", &first);
12865         /*first.maybeThinking = TRUE;*/ /* GNU Chess does not ponder here */
12866         break;
12867
12868       case BeginningOfGame:
12869       default:
12870         break;
12871
12872       case IcsPlayingWhite:
12873       case IcsPlayingBlack:
12874         if (WhiteOnMove(forwardMostMove) == (gameMode == IcsPlayingWhite)) {
12875             SendToICS(ics_prefix);
12876             SendToICS("takeback 2\n");
12877         } else {
12878             SendToICS(ics_prefix);
12879             SendToICS("takeback 1\n");
12880         }
12881         break;
12882     }
12883 }
12884
12885 void
12886 MoveNowEvent()
12887 {
12888     ChessProgramState *cps;
12889
12890     switch (gameMode) {
12891       case MachinePlaysWhite:
12892         if (!WhiteOnMove(forwardMostMove)) {
12893             DisplayError(_("It is your turn"), 0);
12894             return;
12895         }
12896         cps = &first;
12897         break;
12898       case MachinePlaysBlack:
12899         if (WhiteOnMove(forwardMostMove)) {
12900             DisplayError(_("It is your turn"), 0);
12901             return;
12902         }
12903         cps = &first;
12904         break;
12905       case TwoMachinesPlay:
12906         if (WhiteOnMove(forwardMostMove) ==
12907             (first.twoMachinesColor[0] == 'w')) {
12908             cps = &first;
12909         } else {
12910             cps = &second;
12911         }
12912         break;
12913       case BeginningOfGame:
12914       default:
12915         return;
12916     }
12917     SendToProgram("?\n", cps);
12918 }
12919
12920 void
12921 TruncateGameEvent()
12922 {
12923     EditGameEvent();
12924     if (gameMode != EditGame) return;
12925     TruncateGame();
12926 }
12927
12928 void
12929 TruncateGame()
12930 {
12931     CleanupTail(); // [HGM] vari: only keep current variation if we explicitly truncate
12932     if (forwardMostMove > currentMove) {
12933         if (gameInfo.resultDetails != NULL) {
12934             free(gameInfo.resultDetails);
12935             gameInfo.resultDetails = NULL;
12936             gameInfo.result = GameUnfinished;
12937         }
12938         forwardMostMove = currentMove;
12939         HistorySet(parseList, backwardMostMove, forwardMostMove,
12940                    currentMove-1);
12941     }
12942 }
12943
12944 void
12945 HintEvent()
12946 {
12947     if (appData.noChessProgram) return;
12948     switch (gameMode) {
12949       case MachinePlaysWhite:
12950         if (WhiteOnMove(forwardMostMove)) {
12951             DisplayError(_("Wait until your turn"), 0);
12952             return;
12953         }
12954         break;
12955       case BeginningOfGame:
12956       case MachinePlaysBlack:
12957         if (!WhiteOnMove(forwardMostMove)) {
12958             DisplayError(_("Wait until your turn"), 0);
12959             return;
12960         }
12961         break;
12962       default:
12963         DisplayError(_("No hint available"), 0);
12964         return;
12965     }
12966     SendToProgram("hint\n", &first);
12967     hintRequested = TRUE;
12968 }
12969
12970 void
12971 BookEvent()
12972 {
12973     if (appData.noChessProgram) return;
12974     switch (gameMode) {
12975       case MachinePlaysWhite:
12976         if (WhiteOnMove(forwardMostMove)) {
12977             DisplayError(_("Wait until your turn"), 0);
12978             return;
12979         }
12980         break;
12981       case BeginningOfGame:
12982       case MachinePlaysBlack:
12983         if (!WhiteOnMove(forwardMostMove)) {
12984             DisplayError(_("Wait until your turn"), 0);
12985             return;
12986         }
12987         break;
12988       case EditPosition:
12989         EditPositionDone(TRUE);
12990         break;
12991       case TwoMachinesPlay:
12992         return;
12993       default:
12994         break;
12995     }
12996     SendToProgram("bk\n", &first);
12997     bookOutput[0] = NULLCHAR;
12998     bookRequested = TRUE;
12999 }
13000
13001 void
13002 AboutGameEvent()
13003 {
13004     char *tags = PGNTags(&gameInfo);
13005     TagsPopUp(tags, CmailMsg());
13006     free(tags);
13007 }
13008
13009 /* end button procedures */
13010
13011 void
13012 PrintPosition(fp, move)
13013      FILE *fp;
13014      int move;
13015 {
13016     int i, j;
13017     
13018     for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
13019         for (j = BOARD_LEFT; j < BOARD_RGHT; j++) {
13020             char c = PieceToChar(boards[move][i][j]);
13021             fputc(c == 'x' ? '.' : c, fp);
13022             fputc(j == BOARD_RGHT - 1 ? '\n' : ' ', fp);
13023         }
13024     }
13025     if ((gameMode == EditPosition) ? !blackPlaysFirst : (move % 2 == 0))
13026       fprintf(fp, "white to play\n");
13027     else
13028       fprintf(fp, "black to play\n");
13029 }
13030
13031 void
13032 PrintOpponents(fp)
13033      FILE *fp;
13034 {
13035     if (gameInfo.white != NULL) {
13036         fprintf(fp, "\t%s vs. %s\n", gameInfo.white, gameInfo.black);
13037     } else {
13038         fprintf(fp, "\n");
13039     }
13040 }
13041
13042 /* Find last component of program's own name, using some heuristics */
13043 void
13044 TidyProgramName(prog, host, buf)
13045      char *prog, *host, buf[MSG_SIZ];
13046 {
13047     char *p, *q;
13048     int local = (strcmp(host, "localhost") == 0);
13049     while (!local && (p = strchr(prog, ';')) != NULL) {
13050         p++;
13051         while (*p == ' ') p++;
13052         prog = p;
13053     }
13054     if (*prog == '"' || *prog == '\'') {
13055         q = strchr(prog + 1, *prog);
13056     } else {
13057         q = strchr(prog, ' ');
13058     }
13059     if (q == NULL) q = prog + strlen(prog);
13060     p = q;
13061     while (p >= prog && *p != '/' && *p != '\\') p--;
13062     p++;
13063     if(p == prog && *p == '"') p++;
13064     if (q - p >= 4 && StrCaseCmp(q - 4, ".exe") == 0) q -= 4;
13065     memcpy(buf, p, q - p);
13066     buf[q - p] = NULLCHAR;
13067     if (!local) {
13068         strcat(buf, "@");
13069         strcat(buf, host);
13070     }
13071 }
13072
13073 char *
13074 TimeControlTagValue()
13075 {
13076     char buf[MSG_SIZ];
13077     if (!appData.clockMode) {
13078         strcpy(buf, "-");
13079     } else if (movesPerSession > 0) {
13080         sprintf(buf, "%d/%ld", movesPerSession, timeControl/1000);
13081     } else if (timeIncrement == 0) {
13082         sprintf(buf, "%ld", timeControl/1000);
13083     } else {
13084         sprintf(buf, "%ld+%ld", timeControl/1000, timeIncrement/1000);
13085     }
13086     return StrSave(buf);
13087 }
13088
13089 void
13090 SetGameInfo()
13091 {
13092     /* This routine is used only for certain modes */
13093     VariantClass v = gameInfo.variant;
13094     ChessMove r = GameUnfinished;
13095     char *p = NULL;
13096
13097     if(gameMode == EditGame) { // [HGM] vari: do not erase result on EditGame
13098         r = gameInfo.result; 
13099         p = gameInfo.resultDetails; 
13100         gameInfo.resultDetails = NULL;
13101     }
13102     ClearGameInfo(&gameInfo);
13103     gameInfo.variant = v;
13104
13105     switch (gameMode) {
13106       case MachinePlaysWhite:
13107         gameInfo.event = StrSave( appData.pgnEventHeader );
13108         gameInfo.site = StrSave(HostName());
13109         gameInfo.date = PGNDate();
13110         gameInfo.round = StrSave("-");
13111         gameInfo.white = StrSave(first.tidy);
13112         gameInfo.black = StrSave(UserName());
13113         gameInfo.timeControl = TimeControlTagValue();
13114         break;
13115
13116       case MachinePlaysBlack:
13117         gameInfo.event = StrSave( appData.pgnEventHeader );
13118         gameInfo.site = StrSave(HostName());
13119         gameInfo.date = PGNDate();
13120         gameInfo.round = StrSave("-");
13121         gameInfo.white = StrSave(UserName());
13122         gameInfo.black = StrSave(first.tidy);
13123         gameInfo.timeControl = TimeControlTagValue();
13124         break;
13125
13126       case TwoMachinesPlay:
13127         gameInfo.event = StrSave( appData.pgnEventHeader );
13128         gameInfo.site = StrSave(HostName());
13129         gameInfo.date = PGNDate();
13130         if (matchGame > 0) {
13131             char buf[MSG_SIZ];
13132             sprintf(buf, "%d", matchGame);
13133             gameInfo.round = StrSave(buf);
13134         } else {
13135             gameInfo.round = StrSave("-");
13136         }
13137         if (first.twoMachinesColor[0] == 'w') {
13138             gameInfo.white = StrSave(first.tidy);
13139             gameInfo.black = StrSave(second.tidy);
13140         } else {
13141             gameInfo.white = StrSave(second.tidy);
13142             gameInfo.black = StrSave(first.tidy);
13143         }
13144         gameInfo.timeControl = TimeControlTagValue();
13145         break;
13146
13147       case EditGame:
13148         gameInfo.event = StrSave("Edited game");
13149         gameInfo.site = StrSave(HostName());
13150         gameInfo.date = PGNDate();
13151         gameInfo.round = StrSave("-");
13152         gameInfo.white = StrSave("-");
13153         gameInfo.black = StrSave("-");
13154         gameInfo.result = r;
13155         gameInfo.resultDetails = p;
13156         break;
13157
13158       case EditPosition:
13159         gameInfo.event = StrSave("Edited position");
13160         gameInfo.site = StrSave(HostName());
13161         gameInfo.date = PGNDate();
13162         gameInfo.round = StrSave("-");
13163         gameInfo.white = StrSave("-");
13164         gameInfo.black = StrSave("-");
13165         break;
13166
13167       case IcsPlayingWhite:
13168       case IcsPlayingBlack:
13169       case IcsObserving:
13170       case IcsExamining:
13171         break;
13172
13173       case PlayFromGameFile:
13174         gameInfo.event = StrSave("Game from non-PGN file");
13175         gameInfo.site = StrSave(HostName());
13176         gameInfo.date = PGNDate();
13177         gameInfo.round = StrSave("-");
13178         gameInfo.white = StrSave("?");
13179         gameInfo.black = StrSave("?");
13180         break;
13181
13182       default:
13183         break;
13184     }
13185 }
13186
13187 void
13188 ReplaceComment(index, text)
13189      int index;
13190      char *text;
13191 {
13192     int len;
13193
13194     while (*text == '\n') text++;
13195     len = strlen(text);
13196     while (len > 0 && text[len - 1] == '\n') len--;
13197
13198     if (commentList[index] != NULL)
13199       free(commentList[index]);
13200
13201     if (len == 0) {
13202         commentList[index] = NULL;
13203         return;
13204     }
13205   if( *text == '{' && strchr(text, '}') || // [HGM] braces: if certainy malformed, put braces
13206       *text == '[' && strchr(text, ']') || // otherwise hope the user knows what he is doing
13207       *text == '(' && strchr(text, ')')) { // (perhaps check if this parses as comment-only?)
13208     commentList[index] = (char *) malloc(len + 2);
13209     strncpy(commentList[index], text, len);
13210     commentList[index][len] = '\n';
13211     commentList[index][len + 1] = NULLCHAR;
13212   } else { 
13213     // [HGM] braces: if text does not start with known OK delimiter, put braces around it.
13214     char *p;
13215     commentList[index] = (char *) malloc(len + 6);
13216     strcpy(commentList[index], "{\n");
13217     strncpy(commentList[index]+2, text, len);
13218     commentList[index][len+2] = NULLCHAR;
13219     while(p = strchr(commentList[index], '}')) *p = ')'; // kill all } to make it one comment
13220     strcat(commentList[index], "\n}\n");
13221   }
13222 }
13223
13224 void
13225 CrushCRs(text)
13226      char *text;
13227 {
13228   char *p = text;
13229   char *q = text;
13230   char ch;
13231
13232   do {
13233     ch = *p++;
13234     if (ch == '\r') continue;
13235     *q++ = ch;
13236   } while (ch != '\0');
13237 }
13238
13239 void
13240 AppendComment(index, text, addBraces)
13241      int index;
13242      char *text;
13243      Boolean addBraces; // [HGM] braces: tells if we should add {}
13244 {
13245     int oldlen, len;
13246     char *old;
13247
13248 if(appData.debugMode) fprintf(debugFP, "Append: in='%s' %d\n", text, addBraces); fflush(debugFP);
13249     text = GetInfoFromComment( index, text ); /* [HGM] PV time: strip PV info from comment */
13250
13251     CrushCRs(text);
13252     while (*text == '\n') text++;
13253     len = strlen(text);
13254     while (len > 0 && text[len - 1] == '\n') len--;
13255
13256     if (len == 0) return;
13257
13258     if (commentList[index] != NULL) {
13259         old = commentList[index];
13260         oldlen = strlen(old);
13261         while(commentList[index][oldlen-1] ==  '\n')
13262           commentList[index][--oldlen] = NULLCHAR;
13263         commentList[index] = (char *) malloc(oldlen + len + 6); // might waste 4
13264         strcpy(commentList[index], old);
13265         free(old);
13266         // [HGM] braces: join "{A\n}\n" + "{\nB}" as "{A\nB\n}"
13267         if(commentList[index][oldlen-1] == '}' && (text[0] == '{' || addBraces)) {
13268           if(addBraces) addBraces = FALSE; else { text++; len--; }
13269           while (*text == '\n') { text++; len--; }
13270           commentList[index][--oldlen] = NULLCHAR;
13271       }
13272         if(addBraces) strcat(commentList[index], "\n{\n");
13273         else          strcat(commentList[index], "\n");
13274         strcat(commentList[index], text);
13275         if(addBraces) strcat(commentList[index], "\n}\n");
13276         else          strcat(commentList[index], "\n");
13277     } else {
13278         commentList[index] = (char *) malloc(len + 6); // perhaps wastes 4...
13279         if(addBraces)
13280              strcpy(commentList[index], "{\n");
13281         else commentList[index][0] = NULLCHAR;
13282         strcat(commentList[index], text);
13283         strcat(commentList[index], "\n");
13284         if(addBraces) strcat(commentList[index], "}\n");
13285     }
13286 }
13287
13288 static char * FindStr( char * text, char * sub_text )
13289 {
13290     char * result = strstr( text, sub_text );
13291
13292     if( result != NULL ) {
13293         result += strlen( sub_text );
13294     }
13295
13296     return result;
13297 }
13298
13299 /* [AS] Try to extract PV info from PGN comment */
13300 /* [HGM] PV time: and then remove it, to prevent it appearing twice */
13301 char *GetInfoFromComment( int index, char * text )
13302 {
13303     char * sep = text;
13304
13305     if( text != NULL && index > 0 ) {
13306         int score = 0;
13307         int depth = 0;
13308         int time = -1, sec = 0, deci;
13309         char * s_eval = FindStr( text, "[%eval " );
13310         char * s_emt = FindStr( text, "[%emt " );
13311
13312         if( s_eval != NULL || s_emt != NULL ) {
13313             /* New style */
13314             char delim;
13315
13316             if( s_eval != NULL ) {
13317                 if( sscanf( s_eval, "%d,%d%c", &score, &depth, &delim ) != 3 ) {
13318                     return text;
13319                 }
13320
13321                 if( delim != ']' ) {
13322                     return text;
13323                 }
13324             }
13325
13326             if( s_emt != NULL ) {
13327             }
13328                 return text;
13329         }
13330         else {
13331             /* We expect something like: [+|-]nnn.nn/dd */
13332             int score_lo = 0;
13333
13334             if(*text != '{') return text; // [HGM] braces: must be normal comment
13335
13336             sep = strchr( text, '/' );
13337             if( sep == NULL || sep < (text+4) ) {
13338                 return text;
13339             }
13340
13341             time = -1; sec = -1; deci = -1;
13342             if( sscanf( text+1, "%d.%d/%d %d:%d", &score, &score_lo, &depth, &time, &sec ) != 5 &&
13343                 sscanf( text+1, "%d.%d/%d %d.%d", &score, &score_lo, &depth, &time, &deci ) != 5 &&
13344                 sscanf( text+1, "%d.%d/%d %d", &score, &score_lo, &depth, &time ) != 4 &&
13345                 sscanf( text+1, "%d.%d/%d", &score, &score_lo, &depth ) != 3   ) {
13346                 return text;
13347             }
13348
13349             if( score_lo < 0 || score_lo >= 100 ) {
13350                 return text;
13351             }
13352
13353             if(sec >= 0) time = 600*time + 10*sec; else
13354             if(deci >= 0) time = 10*time + deci; else time *= 10; // deci-sec
13355
13356             score = score >= 0 ? score*100 + score_lo : score*100 - score_lo;
13357
13358             /* [HGM] PV time: now locate end of PV info */
13359             while( *++sep >= '0' && *sep <= '9'); // strip depth
13360             if(time >= 0)
13361             while( *++sep >= '0' && *sep <= '9'); // strip time
13362             if(sec >= 0)
13363             while( *++sep >= '0' && *sep <= '9'); // strip seconds
13364             if(deci >= 0)
13365             while( *++sep >= '0' && *sep <= '9'); // strip fractional seconds
13366             while(*sep == ' ') sep++;
13367         }
13368
13369         if( depth <= 0 ) {
13370             return text;
13371         }
13372
13373         if( time < 0 ) {
13374             time = -1;
13375         }
13376
13377         pvInfoList[index-1].depth = depth;
13378         pvInfoList[index-1].score = score;
13379         pvInfoList[index-1].time  = 10*time; // centi-sec
13380         if(*sep == '}') *sep = 0; else *--sep = '{';
13381     }
13382     return sep;
13383 }
13384
13385 void
13386 SendToProgram(message, cps)
13387      char *message;
13388      ChessProgramState *cps;
13389 {
13390     int count, outCount, error;
13391     char buf[MSG_SIZ];
13392
13393     if (cps->pr == NULL) return;
13394     Attention(cps);
13395     
13396     if (appData.debugMode) {
13397         TimeMark now;
13398         GetTimeMark(&now);
13399         fprintf(debugFP, "%ld >%-6s: %s", 
13400                 SubtractTimeMarks(&now, &programStartTime),
13401                 cps->which, message);
13402     }
13403     
13404     count = strlen(message);
13405     outCount = OutputToProcess(cps->pr, message, count, &error);
13406     if (outCount < count && !exiting 
13407                          && !endingGame) { /* [HGM] crash: to not hang GameEnds() writing to deceased engines */
13408         sprintf(buf, _("Error writing to %s chess program"), cps->which);
13409         if(gameInfo.resultDetails==NULL) { /* [HGM] crash: if game in progress, give reason for abort */
13410             if((signed char)boards[forwardMostMove][EP_STATUS] <= EP_DRAWS) {
13411                 gameInfo.result = GameIsDrawn; /* [HGM] accept exit as draw claim */
13412                 sprintf(buf, "%s program exits in draw position (%s)", cps->which, cps->program);
13413             } else {
13414                 gameInfo.result = cps->twoMachinesColor[0]=='w' ? BlackWins : WhiteWins;
13415             }
13416             gameInfo.resultDetails = StrSave(buf);
13417         }
13418         if(!cps->userError || !appData.popupExitMessage) DisplayFatalError(buf, error, 1); else errorExitStatus = 1;
13419     }
13420 }
13421
13422 void
13423 ReceiveFromProgram(isr, closure, message, count, error)
13424      InputSourceRef isr;
13425      VOIDSTAR closure;
13426      char *message;
13427      int count;
13428      int error;
13429 {
13430     char *end_str;
13431     char buf[MSG_SIZ];
13432     ChessProgramState *cps = (ChessProgramState *)closure;
13433
13434     if (isr != cps->isr) return; /* Killed intentionally */
13435     if (count <= 0) {
13436         if (count == 0) {
13437             sprintf(buf,
13438                     _("Error: %s chess program (%s) exited unexpectedly"),
13439                     cps->which, cps->program);
13440         if(gameInfo.resultDetails==NULL) { /* [HGM] crash: if game in progress, give reason for abort */
13441                 if((signed char)boards[forwardMostMove][EP_STATUS] <= EP_DRAWS) {
13442                     gameInfo.result = GameIsDrawn; /* [HGM] accept exit as draw claim */
13443                     sprintf(buf, _("%s program exits in draw position (%s)"), cps->which, cps->program);
13444                 } else {
13445                     gameInfo.result = cps->twoMachinesColor[0]=='w' ? BlackWins : WhiteWins;
13446                 }
13447                 gameInfo.resultDetails = StrSave(buf);
13448             }
13449             RemoveInputSource(cps->isr);
13450             if(!cps->userError || !appData.popupExitMessage) DisplayFatalError(buf, 0, 1); else errorExitStatus = 1;
13451         } else {
13452             sprintf(buf,
13453                     _("Error reading from %s chess program (%s)"),
13454                     cps->which, cps->program);
13455             RemoveInputSource(cps->isr);
13456
13457             /* [AS] Program is misbehaving badly... kill it */
13458             if( count == -2 ) {
13459                 DestroyChildProcess( cps->pr, 9 );
13460                 cps->pr = NoProc;
13461             }
13462
13463             if(!cps->userError || !appData.popupExitMessage) DisplayFatalError(buf, error, 1); else errorExitStatus = 1;
13464         }
13465         return;
13466     }
13467     
13468     if ((end_str = strchr(message, '\r')) != NULL)
13469       *end_str = NULLCHAR;
13470     if ((end_str = strchr(message, '\n')) != NULL)
13471       *end_str = NULLCHAR;
13472     
13473     if (appData.debugMode) {
13474         TimeMark now; int print = 1;
13475         char *quote = ""; char c; int i;
13476
13477         if(appData.engineComments != 1) { /* [HGM] debug: decide if protocol-violating output is written */
13478                 char start = message[0];
13479                 if(start >='A' && start <= 'Z') start += 'a' - 'A'; // be tolerant to capitalizing
13480                 if(sscanf(message, "%d%c%d%d%d", &i, &c, &i, &i, &i) != 5 && 
13481                    sscanf(message, "move %c", &c)!=1  && sscanf(message, "offer%c", &c)!=1 &&
13482                    sscanf(message, "resign%c", &c)!=1 && sscanf(message, "feature %c", &c)!=1 &&
13483                    sscanf(message, "error %c", &c)!=1 && sscanf(message, "illegal %c", &c)!=1 &&
13484                    sscanf(message, "tell%c", &c)!=1   && sscanf(message, "0-1 %c", &c)!=1 &&
13485                    sscanf(message, "1-0 %c", &c)!=1   && sscanf(message, "1/2-1/2 %c", &c)!=1 &&
13486                    sscanf(message, "pong %c", &c)!=1   && start != '#') {
13487                     quote = appData.engineComments == 2 ? "# " : "### NON-COMPLIANT! ### ";
13488                     print = (appData.engineComments >= 2);
13489                 }
13490                 message[0] = start; // restore original message
13491         }
13492         if(print) {
13493                 GetTimeMark(&now);
13494                 fprintf(debugFP, "%ld <%-6s: %s%s\n", 
13495                         SubtractTimeMarks(&now, &programStartTime), cps->which, 
13496                         quote,
13497                         message);
13498         }
13499     }
13500
13501     /* [DM] if icsEngineAnalyze is active we block all whisper and kibitz output, because nobody want to see this */
13502     if (appData.icsEngineAnalyze) {
13503         if (strstr(message, "whisper") != NULL ||
13504              strstr(message, "kibitz") != NULL || 
13505             strstr(message, "tellics") != NULL) return;
13506     }
13507
13508     HandleMachineMove(message, cps);
13509 }
13510
13511
13512 void
13513 SendTimeControl(cps, mps, tc, inc, sd, st)
13514      ChessProgramState *cps;
13515      int mps, inc, sd, st;
13516      long tc;
13517 {
13518     char buf[MSG_SIZ];
13519     int seconds;
13520
13521     if( timeControl_2 > 0 ) {
13522         if( (gameMode == MachinePlaysBlack) || (gameMode == TwoMachinesPlay && cps->twoMachinesColor[0] == 'b') ) {
13523             tc = timeControl_2;
13524         }
13525     }
13526     tc  /= cps->timeOdds; /* [HGM] time odds: apply before telling engine */
13527     inc /= cps->timeOdds;
13528     st  /= cps->timeOdds;
13529
13530     seconds = (tc / 1000) % 60; /* [HGM] displaced to after applying odds */
13531
13532     if (st > 0) {
13533       /* Set exact time per move, normally using st command */
13534       if (cps->stKludge) {
13535         /* GNU Chess 4 has no st command; uses level in a nonstandard way */
13536         seconds = st % 60;
13537         if (seconds == 0) {
13538           sprintf(buf, "level 1 %d\n", st/60);
13539         } else {
13540           sprintf(buf, "level 1 %d:%02d\n", st/60, seconds);
13541         }
13542       } else {
13543         sprintf(buf, "st %d\n", st);
13544       }
13545     } else {
13546       /* Set conventional or incremental time control, using level command */
13547       if (seconds == 0) {
13548         /* Note old gnuchess bug -- minutes:seconds used to not work.
13549            Fixed in later versions, but still avoid :seconds
13550            when seconds is 0. */
13551         sprintf(buf, "level %d %ld %d\n", mps, tc/60000, inc/1000);
13552       } else {
13553         sprintf(buf, "level %d %ld:%02d %d\n", mps, tc/60000,
13554                 seconds, inc/1000);
13555       }
13556     }
13557     SendToProgram(buf, cps);
13558
13559     /* Orthoganally (except for GNU Chess 4), limit time to st seconds */
13560     /* Orthogonally, limit search to given depth */
13561     if (sd > 0) {
13562       if (cps->sdKludge) {
13563         sprintf(buf, "depth\n%d\n", sd);
13564       } else {
13565         sprintf(buf, "sd %d\n", sd);
13566       }
13567       SendToProgram(buf, cps);
13568     }
13569
13570     if(cps->nps > 0) { /* [HGM] nps */
13571         if(cps->supportsNPS == FALSE) cps->nps = -1; // don't use if engine explicitly says not supported!
13572         else {
13573                 sprintf(buf, "nps %d\n", cps->nps);
13574               SendToProgram(buf, cps);
13575         }
13576     }
13577 }
13578
13579 ChessProgramState *WhitePlayer()
13580 /* [HGM] return pointer to 'first' or 'second', depending on who plays white */
13581 {
13582     if(gameMode == TwoMachinesPlay && first.twoMachinesColor[0] == 'b' || 
13583        gameMode == BeginningOfGame || gameMode == MachinePlaysBlack)
13584         return &second;
13585     return &first;
13586 }
13587
13588 void
13589 SendTimeRemaining(cps, machineWhite)
13590      ChessProgramState *cps;
13591      int /*boolean*/ machineWhite;
13592 {
13593     char message[MSG_SIZ];
13594     long time, otime;
13595
13596     /* Note: this routine must be called when the clocks are stopped
13597        or when they have *just* been set or switched; otherwise
13598        it will be off by the time since the current tick started.
13599     */
13600     if (machineWhite) {
13601         time = whiteTimeRemaining / 10;
13602         otime = blackTimeRemaining / 10;
13603     } else {
13604         time = blackTimeRemaining / 10;
13605         otime = whiteTimeRemaining / 10;
13606     }
13607     /* [HGM] translate opponent's time by time-odds factor */
13608     otime = (otime * cps->other->timeOdds) / cps->timeOdds;
13609     if (appData.debugMode) {
13610         fprintf(debugFP, "time odds: %f %f \n", cps->timeOdds, cps->other->timeOdds);
13611     }
13612
13613     if (time <= 0) time = 1;
13614     if (otime <= 0) otime = 1;
13615     
13616     sprintf(message, "time %ld\n", time);
13617     SendToProgram(message, cps);
13618
13619     sprintf(message, "otim %ld\n", otime);
13620     SendToProgram(message, cps);
13621 }
13622
13623 int
13624 BoolFeature(p, name, loc, cps)
13625      char **p;
13626      char *name;
13627      int *loc;
13628      ChessProgramState *cps;
13629 {
13630   char buf[MSG_SIZ];
13631   int len = strlen(name);
13632   int val;
13633   if (strncmp((*p), name, len) == 0 && (*p)[len] == '=') {
13634     (*p) += len + 1;
13635     sscanf(*p, "%d", &val);
13636     *loc = (val != 0);
13637     while (**p && **p != ' ') (*p)++;
13638     sprintf(buf, "accepted %s\n", name);
13639     SendToProgram(buf, cps);
13640     return TRUE;
13641   }
13642   return FALSE;
13643 }
13644
13645 int
13646 IntFeature(p, name, loc, cps)
13647      char **p;
13648      char *name;
13649      int *loc;
13650      ChessProgramState *cps;
13651 {
13652   char buf[MSG_SIZ];
13653   int len = strlen(name);
13654   if (strncmp((*p), name, len) == 0 && (*p)[len] == '=') {
13655     (*p) += len + 1;
13656     sscanf(*p, "%d", loc);
13657     while (**p && **p != ' ') (*p)++;
13658     sprintf(buf, "accepted %s\n", name);
13659     SendToProgram(buf, cps);
13660     return TRUE;
13661   }
13662   return FALSE;
13663 }
13664
13665 int
13666 StringFeature(p, name, loc, cps)
13667      char **p;
13668      char *name;
13669      char loc[];
13670      ChessProgramState *cps;
13671 {
13672   char buf[MSG_SIZ];
13673   int len = strlen(name);
13674   if (strncmp((*p), name, len) == 0
13675       && (*p)[len] == '=' && (*p)[len+1] == '\"') {
13676     (*p) += len + 2;
13677     sscanf(*p, "%[^\"]", loc);
13678     while (**p && **p != '\"') (*p)++;
13679     if (**p == '\"') (*p)++;
13680     sprintf(buf, "accepted %s\n", name);
13681     SendToProgram(buf, cps);
13682     return TRUE;
13683   }
13684   return FALSE;
13685 }
13686
13687 int 
13688 ParseOption(Option *opt, ChessProgramState *cps)
13689 // [HGM] options: process the string that defines an engine option, and determine
13690 // name, type, default value, and allowed value range
13691 {
13692         char *p, *q, buf[MSG_SIZ];
13693         int n, min = (-1)<<31, max = 1<<31, def;
13694
13695         if(p = strstr(opt->name, " -spin ")) {
13696             if((n = sscanf(p, " -spin %d %d %d", &def, &min, &max)) < 3 ) return FALSE;
13697             if(max < min) max = min; // enforce consistency
13698             if(def < min) def = min;
13699             if(def > max) def = max;
13700             opt->value = def;
13701             opt->min = min;
13702             opt->max = max;
13703             opt->type = Spin;
13704         } else if((p = strstr(opt->name, " -slider "))) {
13705             // for now -slider is a synonym for -spin, to already provide compatibility with future polyglots
13706             if((n = sscanf(p, " -slider %d %d %d", &def, &min, &max)) < 3 ) return FALSE;
13707             if(max < min) max = min; // enforce consistency
13708             if(def < min) def = min;
13709             if(def > max) def = max;
13710             opt->value = def;
13711             opt->min = min;
13712             opt->max = max;
13713             opt->type = Spin; // Slider;
13714         } else if((p = strstr(opt->name, " -string "))) {
13715             opt->textValue = p+9;
13716             opt->type = TextBox;
13717         } else if((p = strstr(opt->name, " -file "))) {
13718             // for now -file is a synonym for -string, to already provide compatibility with future polyglots
13719             opt->textValue = p+7;
13720             opt->type = TextBox; // FileName;
13721         } else if((p = strstr(opt->name, " -path "))) {
13722             // for now -file is a synonym for -string, to already provide compatibility with future polyglots
13723             opt->textValue = p+7;
13724             opt->type = TextBox; // PathName;
13725         } else if(p = strstr(opt->name, " -check ")) {
13726             if(sscanf(p, " -check %d", &def) < 1) return FALSE;
13727             opt->value = (def != 0);
13728             opt->type = CheckBox;
13729         } else if(p = strstr(opt->name, " -combo ")) {
13730             opt->textValue = (char*) (&cps->comboList[cps->comboCnt]); // cheat with pointer type
13731             cps->comboList[cps->comboCnt++] = q = p+8; // holds possible choices
13732             if(*q == '*') cps->comboList[cps->comboCnt-1]++;
13733             opt->value = n = 0;
13734             while(q = StrStr(q, " /// ")) {
13735                 n++; *q = 0;    // count choices, and null-terminate each of them
13736                 q += 5;
13737                 if(*q == '*') { // remember default, which is marked with * prefix
13738                     q++;
13739                     opt->value = n;
13740                 }
13741                 cps->comboList[cps->comboCnt++] = q;
13742             }
13743             cps->comboList[cps->comboCnt++] = NULL;
13744             opt->max = n + 1;
13745             opt->type = ComboBox;
13746         } else if(p = strstr(opt->name, " -button")) {
13747             opt->type = Button;
13748         } else if(p = strstr(opt->name, " -save")) {
13749             opt->type = SaveButton;
13750         } else return FALSE;
13751         *p = 0; // terminate option name
13752         // now look if the command-line options define a setting for this engine option.
13753         if(cps->optionSettings && cps->optionSettings[0])
13754             p = strstr(cps->optionSettings, opt->name); else p = NULL;
13755         if(p && (p == cps->optionSettings || p[-1] == ',')) {
13756                 sprintf(buf, "option %s", p);
13757                 if(p = strstr(buf, ",")) *p = 0;
13758                 strcat(buf, "\n");
13759                 SendToProgram(buf, cps);
13760         }
13761         return TRUE;
13762 }
13763
13764 void
13765 FeatureDone(cps, val)
13766      ChessProgramState* cps;
13767      int val;
13768 {
13769   DelayedEventCallback cb = GetDelayedEvent();
13770   if ((cb == InitBackEnd3 && cps == &first) ||
13771       (cb == TwoMachinesEventIfReady && cps == &second)) {
13772     CancelDelayedEvent();
13773     ScheduleDelayedEvent(cb, val ? 1 : 3600000);
13774   }
13775   cps->initDone = val;
13776 }
13777
13778 /* Parse feature command from engine */
13779 void
13780 ParseFeatures(args, cps)
13781      char* args;
13782      ChessProgramState *cps;  
13783 {
13784   char *p = args;
13785   char *q;
13786   int val;
13787   char buf[MSG_SIZ];
13788
13789   for (;;) {
13790     while (*p == ' ') p++;
13791     if (*p == NULLCHAR) return;
13792
13793     if (BoolFeature(&p, "setboard", &cps->useSetboard, cps)) continue;
13794     if (BoolFeature(&p, "time", &cps->sendTime, cps)) continue;    
13795     if (BoolFeature(&p, "draw", &cps->sendDrawOffers, cps)) continue;    
13796     if (BoolFeature(&p, "sigint", &cps->useSigint, cps)) continue;    
13797     if (BoolFeature(&p, "sigterm", &cps->useSigterm, cps)) continue;    
13798     if (BoolFeature(&p, "reuse", &val, cps)) {
13799       /* Engine can disable reuse, but can't enable it if user said no */
13800       if (!val) cps->reuse = FALSE;
13801       continue;
13802     }
13803     if (BoolFeature(&p, "analyze", &cps->analysisSupport, cps)) continue;
13804     if (StringFeature(&p, "myname", &cps->tidy, cps)) {
13805       if (gameMode == TwoMachinesPlay) {
13806         DisplayTwoMachinesTitle();
13807       } else {
13808         DisplayTitle("");
13809       }
13810       continue;
13811     }
13812     if (StringFeature(&p, "variants", &cps->variants, cps)) continue;
13813     if (BoolFeature(&p, "san", &cps->useSAN, cps)) continue;
13814     if (BoolFeature(&p, "ping", &cps->usePing, cps)) continue;
13815     if (BoolFeature(&p, "playother", &cps->usePlayother, cps)) continue;
13816     if (BoolFeature(&p, "colors", &cps->useColors, cps)) continue;
13817     if (BoolFeature(&p, "usermove", &cps->useUsermove, cps)) continue;
13818     if (BoolFeature(&p, "ics", &cps->sendICS, cps)) continue;
13819     if (BoolFeature(&p, "name", &cps->sendName, cps)) continue;
13820     if (BoolFeature(&p, "pause", &val, cps)) continue; /* unused at present */
13821     if (IntFeature(&p, "done", &val, cps)) {
13822       FeatureDone(cps, val);
13823       continue;
13824     }
13825     /* Added by Tord: */
13826     if (BoolFeature(&p, "fen960", &cps->useFEN960, cps)) continue;
13827     if (BoolFeature(&p, "oocastle", &cps->useOOCastle, cps)) continue;
13828     /* End of additions by Tord */
13829
13830     /* [HGM] added features: */
13831     if (BoolFeature(&p, "debug", &cps->debug, cps)) continue;
13832     if (BoolFeature(&p, "nps", &cps->supportsNPS, cps)) continue;
13833     if (IntFeature(&p, "level", &cps->maxNrOfSessions, cps)) continue;
13834     if (BoolFeature(&p, "memory", &cps->memSize, cps)) continue;
13835     if (BoolFeature(&p, "smp", &cps->maxCores, cps)) continue;
13836     if (StringFeature(&p, "egt", &cps->egtFormats, cps)) continue;
13837     if (StringFeature(&p, "option", &(cps->option[cps->nrOptions].name), cps)) {
13838         if(!ParseOption(&(cps->option[cps->nrOptions++]), cps)) { // [HGM] options: add option feature
13839             sprintf(buf, "rejected option %s\n", cps->option[--cps->nrOptions].name);
13840             SendToProgram(buf, cps);
13841             continue;
13842         }
13843         if(cps->nrOptions >= MAX_OPTIONS) {
13844             cps->nrOptions--;
13845             sprintf(buf, "%s engine has too many options\n", cps->which);
13846             DisplayError(buf, 0);
13847         }
13848         continue;
13849     }
13850     /* End of additions by HGM */
13851
13852     /* unknown feature: complain and skip */
13853     q = p;
13854     while (*q && *q != '=') q++;
13855     sprintf(buf, "rejected %.*s\n", (int)(q-p), p);
13856     SendToProgram(buf, cps);
13857     p = q;
13858     if (*p == '=') {
13859       p++;
13860       if (*p == '\"') {
13861         p++;
13862         while (*p && *p != '\"') p++;
13863         if (*p == '\"') p++;
13864       } else {
13865         while (*p && *p != ' ') p++;
13866       }
13867     }
13868   }
13869
13870 }
13871
13872 void
13873 PeriodicUpdatesEvent(newState)
13874      int newState;
13875 {
13876     if (newState == appData.periodicUpdates)
13877       return;
13878
13879     appData.periodicUpdates=newState;
13880
13881     /* Display type changes, so update it now */
13882 //    DisplayAnalysis();
13883
13884     /* Get the ball rolling again... */
13885     if (newState) {
13886         AnalysisPeriodicEvent(1);
13887         StartAnalysisClock();
13888     }
13889 }
13890
13891 void
13892 PonderNextMoveEvent(newState)
13893      int newState;
13894 {
13895     if (newState == appData.ponderNextMove) return;
13896     if (gameMode == EditPosition) EditPositionDone(TRUE);
13897     if (newState) {
13898         SendToProgram("hard\n", &first);
13899         if (gameMode == TwoMachinesPlay) {
13900             SendToProgram("hard\n", &second);
13901         }
13902     } else {
13903         SendToProgram("easy\n", &first);
13904         thinkOutput[0] = NULLCHAR;
13905         if (gameMode == TwoMachinesPlay) {
13906             SendToProgram("easy\n", &second);
13907         }
13908     }
13909     appData.ponderNextMove = newState;
13910 }
13911
13912 void
13913 NewSettingEvent(option, feature, command, value)
13914      char *command;
13915      int option, value, *feature;
13916 {
13917     char buf[MSG_SIZ];
13918
13919     if (gameMode == EditPosition) EditPositionDone(TRUE);
13920     sprintf(buf, "%s%s %d\n", (option ? "option ": ""), command, value);
13921     if(feature == NULL || *feature) SendToProgram(buf, &first);
13922     if (gameMode == TwoMachinesPlay) {
13923         if(feature == NULL || feature[(int*)&second - (int*)&first]) SendToProgram(buf, &second);
13924     }
13925 }
13926
13927 void
13928 ShowThinkingEvent()
13929 // [HGM] thinking: this routine is now also called from "Options -> Engine..." popup
13930 {
13931     static int oldState = 2; // kludge alert! Neither true nor fals, so first time oldState is always updated
13932     int newState = appData.showThinking
13933         // [HGM] thinking: other features now need thinking output as well
13934         || !appData.hideThinkingFromHuman || appData.adjudicateLossThreshold != 0 || EngineOutputIsUp();
13935     
13936     if (oldState == newState) return;
13937     oldState = newState;
13938     if (gameMode == EditPosition) EditPositionDone(TRUE);
13939     if (oldState) {
13940         SendToProgram("post\n", &first);
13941         if (gameMode == TwoMachinesPlay) {
13942             SendToProgram("post\n", &second);
13943         }
13944     } else {
13945         SendToProgram("nopost\n", &first);
13946         thinkOutput[0] = NULLCHAR;
13947         if (gameMode == TwoMachinesPlay) {
13948             SendToProgram("nopost\n", &second);
13949         }
13950     }
13951 //    appData.showThinking = newState; // [HGM] thinking: responsible option should already have be changed when calling this routine!
13952 }
13953
13954 void
13955 AskQuestionEvent(title, question, replyPrefix, which)
13956      char *title; char *question; char *replyPrefix; char *which;
13957 {
13958   ProcRef pr = (which[0] == '1') ? first.pr : second.pr;
13959   if (pr == NoProc) return;
13960   AskQuestion(title, question, replyPrefix, pr);
13961 }
13962
13963 void
13964 DisplayMove(moveNumber)
13965      int moveNumber;
13966 {
13967     char message[MSG_SIZ];
13968     char res[MSG_SIZ];
13969     char cpThinkOutput[MSG_SIZ];
13970
13971     if(appData.noGUI) return; // [HGM] fast: suppress display of moves
13972     
13973     if (moveNumber == forwardMostMove - 1 || 
13974         gameMode == AnalyzeMode || gameMode == AnalyzeFile) {
13975
13976         safeStrCpy(cpThinkOutput, thinkOutput, sizeof(cpThinkOutput));
13977
13978         if (strchr(cpThinkOutput, '\n')) {
13979             *strchr(cpThinkOutput, '\n') = NULLCHAR;
13980         }
13981     } else {
13982         *cpThinkOutput = NULLCHAR;
13983     }
13984
13985     /* [AS] Hide thinking from human user */
13986     if( appData.hideThinkingFromHuman && gameMode != TwoMachinesPlay ) {
13987         *cpThinkOutput = NULLCHAR;
13988         if( thinkOutput[0] != NULLCHAR ) {
13989             int i;
13990
13991             for( i=0; i<=hiddenThinkOutputState; i++ ) {
13992                 cpThinkOutput[i] = '.';
13993             }
13994             cpThinkOutput[i] = NULLCHAR;
13995             hiddenThinkOutputState = (hiddenThinkOutputState + 1) % 3;
13996         }
13997     }
13998
13999     if (moveNumber == forwardMostMove - 1 &&
14000         gameInfo.resultDetails != NULL) {
14001         if (gameInfo.resultDetails[0] == NULLCHAR) {
14002             sprintf(res, " %s", PGNResult(gameInfo.result));
14003         } else {
14004             sprintf(res, " {%s} %s",
14005                     gameInfo.resultDetails, PGNResult(gameInfo.result));
14006         }
14007     } else {
14008         res[0] = NULLCHAR;
14009     }
14010
14011     if (moveNumber < 0 || parseList[moveNumber][0] == NULLCHAR) {
14012         DisplayMessage(res, cpThinkOutput);
14013     } else {
14014         sprintf(message, "%d.%s%s%s", moveNumber / 2 + 1,
14015                 WhiteOnMove(moveNumber) ? " " : ".. ",
14016                 parseList[moveNumber], res);
14017         DisplayMessage(message, cpThinkOutput);
14018     }
14019 }
14020
14021 void
14022 DisplayComment(moveNumber, text)
14023      int moveNumber;
14024      char *text;
14025 {
14026     char title[MSG_SIZ];
14027     char buf[8000]; // comment can be long!
14028     int score, depth;
14029     
14030     if (moveNumber < 0 || parseList[moveNumber][0] == NULLCHAR) {
14031       strcpy(title, "Comment");
14032     } else {
14033       sprintf(title, "Comment on %d.%s%s", moveNumber / 2 + 1,
14034               WhiteOnMove(moveNumber) ? " " : ".. ",
14035               parseList[moveNumber]);
14036     }
14037     // [HGM] PV info: display PV info together with (or as) comment
14038     if(moveNumber >= 0 && (depth = pvInfoList[moveNumber].depth) > 0) {
14039       if(text == NULL) text = "";                                           
14040       score = pvInfoList[moveNumber].score;
14041       sprintf(buf, "%s%.2f/%d %d\n%s", score>0 ? "+" : "", score/100.,
14042               depth, (pvInfoList[moveNumber].time+50)/100, text);
14043       text = buf;
14044     }
14045     if (text != NULL && (appData.autoDisplayComment || commentUp))
14046         CommentPopUp(title, text);
14047 }
14048
14049 /* This routine sends a ^C interrupt to gnuchess, to awaken it if it
14050  * might be busy thinking or pondering.  It can be omitted if your
14051  * gnuchess is configured to stop thinking immediately on any user
14052  * input.  However, that gnuchess feature depends on the FIONREAD
14053  * ioctl, which does not work properly on some flavors of Unix.
14054  */
14055 void
14056 Attention(cps)
14057      ChessProgramState *cps;
14058 {
14059 #if ATTENTION
14060     if (!cps->useSigint) return;
14061     if (appData.noChessProgram || (cps->pr == NoProc)) return;
14062     switch (gameMode) {
14063       case MachinePlaysWhite:
14064       case MachinePlaysBlack:
14065       case TwoMachinesPlay:
14066       case IcsPlayingWhite:
14067       case IcsPlayingBlack:
14068       case AnalyzeMode:
14069       case AnalyzeFile:
14070         /* Skip if we know it isn't thinking */
14071         if (!cps->maybeThinking) return;
14072         if (appData.debugMode)
14073           fprintf(debugFP, "Interrupting %s\n", cps->which);
14074         InterruptChildProcess(cps->pr);
14075         cps->maybeThinking = FALSE;
14076         break;
14077       default:
14078         break;
14079     }
14080 #endif /*ATTENTION*/
14081 }
14082
14083 int
14084 CheckFlags()
14085 {
14086     if (whiteTimeRemaining <= 0) {
14087         if (!whiteFlag) {
14088             whiteFlag = TRUE;
14089             if (appData.icsActive) {
14090                 if (appData.autoCallFlag &&
14091                     gameMode == IcsPlayingBlack && !blackFlag) {
14092                   SendToICS(ics_prefix);
14093                   SendToICS("flag\n");
14094                 }
14095             } else {
14096                 if (blackFlag) {
14097                     if(gameMode != TwoMachinesPlay) DisplayTitle(_("Both flags fell"));
14098                 } else {
14099                     if(gameMode != TwoMachinesPlay) DisplayTitle(_("White's flag fell"));
14100                     if (appData.autoCallFlag) {
14101                         GameEnds(BlackWins, "Black wins on time", GE_XBOARD);
14102                         return TRUE;
14103                     }
14104                 }
14105             }
14106         }
14107     }
14108     if (blackTimeRemaining <= 0) {
14109         if (!blackFlag) {
14110             blackFlag = TRUE;
14111             if (appData.icsActive) {
14112                 if (appData.autoCallFlag &&
14113                     gameMode == IcsPlayingWhite && !whiteFlag) {
14114                   SendToICS(ics_prefix);
14115                   SendToICS("flag\n");
14116                 }
14117             } else {
14118                 if (whiteFlag) {
14119                     if(gameMode != TwoMachinesPlay) DisplayTitle(_("Both flags fell"));
14120                 } else {
14121                     if(gameMode != TwoMachinesPlay) DisplayTitle(_("Black's flag fell"));
14122                     if (appData.autoCallFlag) {
14123                         GameEnds(WhiteWins, "White wins on time", GE_XBOARD);
14124                         return TRUE;
14125                     }
14126                 }
14127             }
14128         }
14129     }
14130     return FALSE;
14131 }
14132
14133 void
14134 CheckTimeControl()
14135 {
14136     if (!appData.clockMode || appData.icsActive || searchTime || // [HGM] st: no inc in st mode
14137         gameMode == PlayFromGameFile || forwardMostMove == 0) return;
14138
14139     /*
14140      * add time to clocks when time control is achieved ([HGM] now also used for increment)
14141      */
14142     if ( !WhiteOnMove(forwardMostMove) )
14143         /* White made time control */
14144         whiteTimeRemaining += GetTimeQuota((forwardMostMove-1)/2)
14145         /* [HGM] time odds: correct new time quota for time odds! */
14146                                             / WhitePlayer()->timeOdds;
14147       else
14148         /* Black made time control */
14149         blackTimeRemaining += GetTimeQuota((forwardMostMove-1)/2)
14150                                             / WhitePlayer()->other->timeOdds;
14151 }
14152
14153 void
14154 DisplayBothClocks()
14155 {
14156     int wom = gameMode == EditPosition ?
14157       !blackPlaysFirst : WhiteOnMove(currentMove);
14158     DisplayWhiteClock(whiteTimeRemaining, wom);
14159     DisplayBlackClock(blackTimeRemaining, !wom);
14160 }
14161
14162
14163 /* Timekeeping seems to be a portability nightmare.  I think everyone
14164    has ftime(), but I'm really not sure, so I'm including some ifdefs
14165    to use other calls if you don't.  Clocks will be less accurate if
14166    you have neither ftime nor gettimeofday.
14167 */
14168
14169 /* VS 2008 requires the #include outside of the function */
14170 #if !HAVE_GETTIMEOFDAY && HAVE_FTIME
14171 #include <sys/timeb.h>
14172 #endif
14173
14174 /* Get the current time as a TimeMark */
14175 void
14176 GetTimeMark(tm)
14177      TimeMark *tm;
14178 {
14179 #if HAVE_GETTIMEOFDAY
14180
14181     struct timeval timeVal;
14182     struct timezone timeZone;
14183
14184     gettimeofday(&timeVal, &timeZone);
14185     tm->sec = (long) timeVal.tv_sec; 
14186     tm->ms = (int) (timeVal.tv_usec / 1000L);
14187
14188 #else /*!HAVE_GETTIMEOFDAY*/
14189 #if HAVE_FTIME
14190
14191 // include <sys/timeb.h> / moved to just above start of function
14192     struct timeb timeB;
14193
14194     ftime(&timeB);
14195     tm->sec = (long) timeB.time;
14196     tm->ms = (int) timeB.millitm;
14197
14198 #else /*!HAVE_FTIME && !HAVE_GETTIMEOFDAY*/
14199     tm->sec = (long) time(NULL);
14200     tm->ms = 0;
14201 #endif
14202 #endif
14203 }
14204
14205 /* Return the difference in milliseconds between two
14206    time marks.  We assume the difference will fit in a long!
14207 */
14208 long
14209 SubtractTimeMarks(tm2, tm1)
14210      TimeMark *tm2, *tm1;
14211 {
14212     return 1000L*(tm2->sec - tm1->sec) +
14213            (long) (tm2->ms - tm1->ms);
14214 }
14215
14216
14217 /*
14218  * Code to manage the game clocks.
14219  *
14220  * In tournament play, black starts the clock and then white makes a move.
14221  * We give the human user a slight advantage if he is playing white---the
14222  * clocks don't run until he makes his first move, so it takes zero time.
14223  * Also, we don't account for network lag, so we could get out of sync
14224  * with GNU Chess's clock -- but then, referees are always right.  
14225  */
14226
14227 static TimeMark tickStartTM;
14228 static long intendedTickLength;
14229
14230 long
14231 NextTickLength(timeRemaining)
14232      long timeRemaining;
14233 {
14234     long nominalTickLength, nextTickLength;
14235
14236     if (timeRemaining > 0L && timeRemaining <= 10000L)
14237       nominalTickLength = 100L;
14238     else
14239       nominalTickLength = 1000L;
14240     nextTickLength = timeRemaining % nominalTickLength;
14241     if (nextTickLength <= 0) nextTickLength += nominalTickLength;
14242
14243     return nextTickLength;
14244 }
14245
14246 /* Adjust clock one minute up or down */
14247 void
14248 AdjustClock(Boolean which, int dir)
14249 {
14250     if(which) blackTimeRemaining += 60000*dir;
14251     else      whiteTimeRemaining += 60000*dir;
14252     DisplayBothClocks();
14253 }
14254
14255 /* Stop clocks and reset to a fresh time control */
14256 void
14257 ResetClocks() 
14258 {
14259     (void) StopClockTimer();
14260     if (appData.icsActive) {
14261         whiteTimeRemaining = blackTimeRemaining = 0;
14262     } else if (searchTime) {
14263         whiteTimeRemaining = 1000*searchTime / WhitePlayer()->timeOdds;
14264         blackTimeRemaining = 1000*searchTime / WhitePlayer()->other->timeOdds;
14265     } else { /* [HGM] correct new time quote for time odds */
14266         whiteTimeRemaining = GetTimeQuota(-1) / WhitePlayer()->timeOdds;
14267         blackTimeRemaining = GetTimeQuota(-1) / WhitePlayer()->other->timeOdds;
14268     }
14269     if (whiteFlag || blackFlag) {
14270         DisplayTitle("");
14271         whiteFlag = blackFlag = FALSE;
14272     }
14273     DisplayBothClocks();
14274 }
14275
14276 #define FUDGE 25 /* 25ms = 1/40 sec; should be plenty even for 50 Hz clocks */
14277
14278 /* Decrement running clock by amount of time that has passed */
14279 void
14280 DecrementClocks()
14281 {
14282     long timeRemaining;
14283     long lastTickLength, fudge;
14284     TimeMark now;
14285
14286     if (!appData.clockMode) return;
14287     if (gameMode==AnalyzeMode || gameMode == AnalyzeFile) return;
14288         
14289     GetTimeMark(&now);
14290
14291     lastTickLength = SubtractTimeMarks(&now, &tickStartTM);
14292
14293     /* Fudge if we woke up a little too soon */
14294     fudge = intendedTickLength - lastTickLength;
14295     if (fudge < 0 || fudge > FUDGE) fudge = 0;
14296
14297     if (WhiteOnMove(forwardMostMove)) {
14298         if(whiteNPS >= 0) lastTickLength = 0;
14299         timeRemaining = whiteTimeRemaining -= lastTickLength;
14300         DisplayWhiteClock(whiteTimeRemaining - fudge,
14301                           WhiteOnMove(currentMove < forwardMostMove ? currentMove : forwardMostMove));
14302     } else {
14303         if(blackNPS >= 0) lastTickLength = 0;
14304         timeRemaining = blackTimeRemaining -= lastTickLength;
14305         DisplayBlackClock(blackTimeRemaining - fudge,
14306                           !WhiteOnMove(currentMove < forwardMostMove ? currentMove : forwardMostMove));
14307     }
14308
14309     if (CheckFlags()) return;
14310         
14311     tickStartTM = now;
14312     intendedTickLength = NextTickLength(timeRemaining - fudge) + fudge;
14313     StartClockTimer(intendedTickLength);
14314
14315     /* if the time remaining has fallen below the alarm threshold, sound the
14316      * alarm. if the alarm has sounded and (due to a takeback or time control
14317      * with increment) the time remaining has increased to a level above the
14318      * threshold, reset the alarm so it can sound again. 
14319      */
14320     
14321     if (appData.icsActive && appData.icsAlarm) {
14322
14323         /* make sure we are dealing with the user's clock */
14324         if (!( ((gameMode == IcsPlayingWhite) && WhiteOnMove(currentMove)) ||
14325                ((gameMode == IcsPlayingBlack) && !WhiteOnMove(currentMove))
14326            )) return;
14327
14328         if (alarmSounded && (timeRemaining > appData.icsAlarmTime)) {
14329             alarmSounded = FALSE;
14330         } else if (!alarmSounded && (timeRemaining <= appData.icsAlarmTime)) { 
14331             PlayAlarmSound();
14332             alarmSounded = TRUE;
14333         }
14334     }
14335 }
14336
14337
14338 /* A player has just moved, so stop the previously running
14339    clock and (if in clock mode) start the other one.
14340    We redisplay both clocks in case we're in ICS mode, because
14341    ICS gives us an update to both clocks after every move.
14342    Note that this routine is called *after* forwardMostMove
14343    is updated, so the last fractional tick must be subtracted
14344    from the color that is *not* on move now.
14345 */
14346 void
14347 SwitchClocks(int newMoveNr)
14348 {
14349     long lastTickLength;
14350     TimeMark now;
14351     int flagged = FALSE;
14352
14353     GetTimeMark(&now);
14354
14355     if (StopClockTimer() && appData.clockMode) {
14356         lastTickLength = SubtractTimeMarks(&now, &tickStartTM);
14357         if (!WhiteOnMove(forwardMostMove)) {
14358             if(blackNPS >= 0) lastTickLength = 0;
14359             blackTimeRemaining -= lastTickLength;
14360            /* [HGM] PGNtime: save time for PGN file if engine did not give it */
14361 //         if(pvInfoList[forwardMostMove-1].time == -1)
14362                  pvInfoList[forwardMostMove-1].time =               // use GUI time
14363                       (timeRemaining[1][forwardMostMove-1] - blackTimeRemaining)/10;
14364         } else {
14365            if(whiteNPS >= 0) lastTickLength = 0;
14366            whiteTimeRemaining -= lastTickLength;
14367            /* [HGM] PGNtime: save time for PGN file if engine did not give it */
14368 //         if(pvInfoList[forwardMostMove-1].time == -1)
14369                  pvInfoList[forwardMostMove-1].time = 
14370                       (timeRemaining[0][forwardMostMove-1] - whiteTimeRemaining)/10;
14371         }
14372         flagged = CheckFlags();
14373     }
14374     forwardMostMove = newMoveNr; // [HGM] race: change stm when no timer interrupt scheduled
14375     CheckTimeControl();
14376
14377     if (flagged || !appData.clockMode) return;
14378
14379     switch (gameMode) {
14380       case MachinePlaysBlack:
14381       case MachinePlaysWhite:
14382       case BeginningOfGame:
14383         if (pausing) return;
14384         break;
14385
14386       case EditGame:
14387       case PlayFromGameFile:
14388       case IcsExamining:
14389         return;
14390
14391       default:
14392         break;
14393     }
14394
14395     if (searchTime) { // [HGM] st: set clock of player that has to move to max time
14396         if(WhiteOnMove(forwardMostMove))
14397              whiteTimeRemaining = 1000*searchTime / WhitePlayer()->timeOdds;
14398         else blackTimeRemaining = 1000*searchTime / WhitePlayer()->other->timeOdds;
14399     }
14400
14401     tickStartTM = now;
14402     intendedTickLength = NextTickLength(WhiteOnMove(forwardMostMove) ?
14403       whiteTimeRemaining : blackTimeRemaining);
14404     StartClockTimer(intendedTickLength);
14405 }
14406         
14407
14408 /* Stop both clocks */
14409 void
14410 StopClocks()
14411 {       
14412     long lastTickLength;
14413     TimeMark now;
14414
14415     if (!StopClockTimer()) return;
14416     if (!appData.clockMode) return;
14417
14418     GetTimeMark(&now);
14419
14420     lastTickLength = SubtractTimeMarks(&now, &tickStartTM);
14421     if (WhiteOnMove(forwardMostMove)) {
14422         if(whiteNPS >= 0) lastTickLength = 0;
14423         whiteTimeRemaining -= lastTickLength;
14424         DisplayWhiteClock(whiteTimeRemaining, WhiteOnMove(currentMove));
14425     } else {
14426         if(blackNPS >= 0) lastTickLength = 0;
14427         blackTimeRemaining -= lastTickLength;
14428         DisplayBlackClock(blackTimeRemaining, !WhiteOnMove(currentMove));
14429     }
14430     CheckFlags();
14431 }
14432         
14433 /* Start clock of player on move.  Time may have been reset, so
14434    if clock is already running, stop and restart it. */
14435 void
14436 StartClocks()
14437 {
14438     (void) StopClockTimer(); /* in case it was running already */
14439     DisplayBothClocks();
14440     if (CheckFlags()) return;
14441
14442     if (!appData.clockMode) return;
14443     if (gameMode == AnalyzeMode || gameMode == AnalyzeFile) return;
14444
14445     GetTimeMark(&tickStartTM);
14446     intendedTickLength = NextTickLength(WhiteOnMove(forwardMostMove) ?
14447       whiteTimeRemaining : blackTimeRemaining);
14448
14449    /* [HGM] nps: figure out nps factors, by determining which engine plays white and/or black once and for all */
14450     whiteNPS = blackNPS = -1; 
14451     if(gameMode == MachinePlaysWhite || gameMode == TwoMachinesPlay && first.twoMachinesColor[0] == 'w'
14452        || appData.zippyPlay && gameMode == IcsPlayingBlack) // first (perhaps only) engine has white
14453         whiteNPS = first.nps;
14454     if(gameMode == MachinePlaysBlack || gameMode == TwoMachinesPlay && first.twoMachinesColor[0] == 'b'
14455        || appData.zippyPlay && gameMode == IcsPlayingWhite) // first (perhaps only) engine has black
14456         blackNPS = first.nps;
14457     if(gameMode == TwoMachinesPlay && first.twoMachinesColor[0] == 'b') // second only used in Two-Machines mode
14458         whiteNPS = second.nps;
14459     if(gameMode == TwoMachinesPlay && first.twoMachinesColor[0] == 'w')
14460         blackNPS = second.nps;
14461     if(appData.debugMode) fprintf(debugFP, "nps: w=%d, b=%d\n", whiteNPS, blackNPS);
14462
14463     StartClockTimer(intendedTickLength);
14464 }
14465
14466 char *
14467 TimeString(ms)
14468      long ms;
14469 {
14470     long second, minute, hour, day;
14471     char *sign = "";
14472     static char buf[32];
14473     
14474     if (ms > 0 && ms <= 9900) {
14475       /* convert milliseconds to tenths, rounding up */
14476       double tenths = floor( ((double)(ms + 99L)) / 100.00 );
14477
14478       sprintf(buf, " %03.1f ", tenths/10.0);
14479       return buf;
14480     }
14481
14482     /* convert milliseconds to seconds, rounding up */
14483     /* use floating point to avoid strangeness of integer division
14484        with negative dividends on many machines */
14485     second = (long) floor(((double) (ms + 999L)) / 1000.0);
14486
14487     if (second < 0) {
14488         sign = "-";
14489         second = -second;
14490     }
14491     
14492     day = second / (60 * 60 * 24);
14493     second = second % (60 * 60 * 24);
14494     hour = second / (60 * 60);
14495     second = second % (60 * 60);
14496     minute = second / 60;
14497     second = second % 60;
14498     
14499     if (day > 0)
14500       sprintf(buf, " %s%ld:%02ld:%02ld:%02ld ",
14501               sign, day, hour, minute, second);
14502     else if (hour > 0)
14503       sprintf(buf, " %s%ld:%02ld:%02ld ", sign, hour, minute, second);
14504     else
14505       sprintf(buf, " %s%2ld:%02ld ", sign, minute, second);
14506     
14507     return buf;
14508 }
14509
14510
14511 /*
14512  * This is necessary because some C libraries aren't ANSI C compliant yet.
14513  */
14514 char *
14515 StrStr(string, match)
14516      char *string, *match;
14517 {
14518     int i, length;
14519     
14520     length = strlen(match);
14521     
14522     for (i = strlen(string) - length; i >= 0; i--, string++)
14523       if (!strncmp(match, string, length))
14524         return string;
14525     
14526     return NULL;
14527 }
14528
14529 char *
14530 StrCaseStr(string, match)
14531      char *string, *match;
14532 {
14533     int i, j, length;
14534     
14535     length = strlen(match);
14536     
14537     for (i = strlen(string) - length; i >= 0; i--, string++) {
14538         for (j = 0; j < length; j++) {
14539             if (ToLower(match[j]) != ToLower(string[j]))
14540               break;
14541         }
14542         if (j == length) return string;
14543     }
14544
14545     return NULL;
14546 }
14547
14548 #ifndef _amigados
14549 int
14550 StrCaseCmp(s1, s2)
14551      char *s1, *s2;
14552 {
14553     char c1, c2;
14554     
14555     for (;;) {
14556         c1 = ToLower(*s1++);
14557         c2 = ToLower(*s2++);
14558         if (c1 > c2) return 1;
14559         if (c1 < c2) return -1;
14560         if (c1 == NULLCHAR) return 0;
14561     }
14562 }
14563
14564
14565 int
14566 ToLower(c)
14567      int c;
14568 {
14569     return isupper(c) ? tolower(c) : c;
14570 }
14571
14572
14573 int
14574 ToUpper(c)
14575      int c;
14576 {
14577     return islower(c) ? toupper(c) : c;
14578 }
14579 #endif /* !_amigados    */
14580
14581 char *
14582 StrSave(s)
14583      char *s;
14584 {
14585     char *ret;
14586
14587     if ((ret = (char *) malloc(strlen(s) + 1))) {
14588         strcpy(ret, s);
14589     }
14590     return ret;
14591 }
14592
14593 char *
14594 StrSavePtr(s, savePtr)
14595      char *s, **savePtr;
14596 {
14597     if (*savePtr) {
14598         free(*savePtr);
14599     }
14600     if ((*savePtr = (char *) malloc(strlen(s) + 1))) {
14601         strcpy(*savePtr, s);
14602     }
14603     return(*savePtr);
14604 }
14605
14606 char *
14607 PGNDate()
14608 {
14609     time_t clock;
14610     struct tm *tm;
14611     char buf[MSG_SIZ];
14612
14613     clock = time((time_t *)NULL);
14614     tm = localtime(&clock);
14615     sprintf(buf, "%04d.%02d.%02d",
14616             tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday);
14617     return StrSave(buf);
14618 }
14619
14620
14621 char *
14622 PositionToFEN(move, overrideCastling)
14623      int move;
14624      char *overrideCastling;
14625 {
14626     int i, j, fromX, fromY, toX, toY;
14627     int whiteToPlay;
14628     char buf[128];
14629     char *p, *q;
14630     int emptycount;
14631     ChessSquare piece;
14632
14633     whiteToPlay = (gameMode == EditPosition) ?
14634       !blackPlaysFirst : (move % 2 == 0);
14635     p = buf;
14636
14637     /* Piece placement data */
14638     for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
14639         emptycount = 0;
14640         for (j = BOARD_LEFT; j < BOARD_RGHT; j++) {
14641             if (boards[move][i][j] == EmptySquare) {
14642                 emptycount++;
14643             } else { ChessSquare piece = boards[move][i][j];
14644                 if (emptycount > 0) {
14645                     if(emptycount<10) /* [HGM] can be >= 10 */
14646                         *p++ = '0' + emptycount;
14647                     else { *p++ = '0' + emptycount/10; *p++ = '0' + emptycount%10; }
14648                     emptycount = 0;
14649                 }
14650                 if(PieceToChar(piece) == '+') {
14651                     /* [HGM] write promoted pieces as '+<unpromoted>' (Shogi) */
14652                     *p++ = '+';
14653                     piece = (ChessSquare)(DEMOTED piece);
14654                 } 
14655                 *p++ = PieceToChar(piece);
14656                 if(p[-1] == '~') {
14657                     /* [HGM] flag promoted pieces as '<promoted>~' (Crazyhouse) */
14658                     p[-1] = PieceToChar((ChessSquare)(DEMOTED piece));
14659                     *p++ = '~';
14660                 }
14661             }
14662         }
14663         if (emptycount > 0) {
14664             if(emptycount<10) /* [HGM] can be >= 10 */
14665                 *p++ = '0' + emptycount;
14666             else { *p++ = '0' + emptycount/10; *p++ = '0' + emptycount%10; }
14667             emptycount = 0;
14668         }
14669         *p++ = '/';
14670     }
14671     *(p - 1) = ' ';
14672
14673     /* [HGM] print Crazyhouse or Shogi holdings */
14674     if( gameInfo.holdingsWidth ) {
14675         *(p-1) = '['; /* if we wanted to support BFEN, this could be '/' */
14676         q = p;
14677         for(i=0; i<gameInfo.holdingsSize; i++) { /* white holdings */
14678             piece = boards[move][i][BOARD_WIDTH-1];
14679             if( piece != EmptySquare )
14680               for(j=0; j<(int) boards[move][i][BOARD_WIDTH-2]; j++)
14681                   *p++ = PieceToChar(piece);
14682         }
14683         for(i=0; i<gameInfo.holdingsSize; i++) { /* black holdings */
14684             piece = boards[move][BOARD_HEIGHT-i-1][0];
14685             if( piece != EmptySquare )
14686               for(j=0; j<(int) boards[move][BOARD_HEIGHT-i-1][1]; j++)
14687                   *p++ = PieceToChar(piece);
14688         }
14689
14690         if( q == p ) *p++ = '-';
14691         *p++ = ']';
14692         *p++ = ' ';
14693     }
14694
14695     /* Active color */
14696     *p++ = whiteToPlay ? 'w' : 'b';
14697     *p++ = ' ';
14698
14699   if(q = overrideCastling) { // [HGM] FRC: override castling & e.p fields for non-compliant engines
14700     while(*p++ = *q++); if(q != overrideCastling+1) p[-1] = ' '; else --p;
14701   } else {
14702   if(nrCastlingRights) {
14703      q = p;
14704      if(gameInfo.variant == VariantFischeRandom || gameInfo.variant == VariantCapaRandom) {
14705        /* [HGM] write directly from rights */
14706            if(boards[move][CASTLING][2] != NoRights &&
14707               boards[move][CASTLING][0] != NoRights   )
14708                 *p++ = boards[move][CASTLING][0] + AAA + 'A' - 'a';
14709            if(boards[move][CASTLING][2] != NoRights &&
14710               boards[move][CASTLING][1] != NoRights   )
14711                 *p++ = boards[move][CASTLING][1] + AAA + 'A' - 'a';
14712            if(boards[move][CASTLING][5] != NoRights &&
14713               boards[move][CASTLING][3] != NoRights   )
14714                 *p++ = boards[move][CASTLING][3] + AAA;
14715            if(boards[move][CASTLING][5] != NoRights &&
14716               boards[move][CASTLING][4] != NoRights   )
14717                 *p++ = boards[move][CASTLING][4] + AAA;
14718      } else {
14719
14720         /* [HGM] write true castling rights */
14721         if( nrCastlingRights == 6 ) {
14722             if(boards[move][CASTLING][0] == BOARD_RGHT-1 &&
14723                boards[move][CASTLING][2] != NoRights  ) *p++ = 'K';
14724             if(boards[move][CASTLING][1] == BOARD_LEFT &&
14725                boards[move][CASTLING][2] != NoRights  ) *p++ = 'Q';
14726             if(boards[move][CASTLING][3] == BOARD_RGHT-1 &&
14727                boards[move][CASTLING][5] != NoRights  ) *p++ = 'k';
14728             if(boards[move][CASTLING][4] == BOARD_LEFT &&
14729                boards[move][CASTLING][5] != NoRights  ) *p++ = 'q';
14730         }
14731      }
14732      if (q == p) *p++ = '-'; /* No castling rights */
14733      *p++ = ' ';
14734   }
14735
14736   if(gameInfo.variant != VariantShogi    && gameInfo.variant != VariantXiangqi &&
14737      gameInfo.variant != VariantShatranj && gameInfo.variant != VariantCourier && gameInfo.variant != VariantMakruk ) { 
14738     /* En passant target square */
14739     if (move > backwardMostMove) {
14740         fromX = moveList[move - 1][0] - AAA;
14741         fromY = moveList[move - 1][1] - ONE;
14742         toX = moveList[move - 1][2] - AAA;
14743         toY = moveList[move - 1][3] - ONE;
14744         if (fromY == (whiteToPlay ? BOARD_HEIGHT-2 : 1) &&
14745             toY == (whiteToPlay ? BOARD_HEIGHT-4 : 3) &&
14746             boards[move][toY][toX] == (whiteToPlay ? BlackPawn : WhitePawn) &&
14747             fromX == toX) {
14748             /* 2-square pawn move just happened */
14749             *p++ = toX + AAA;
14750             *p++ = whiteToPlay ? '6'+BOARD_HEIGHT-8 : '3';
14751         } else {
14752             *p++ = '-';
14753         }
14754     } else if(move == backwardMostMove) {
14755         // [HGM] perhaps we should always do it like this, and forget the above?
14756         if((signed char)boards[move][EP_STATUS] >= 0) {
14757             *p++ = boards[move][EP_STATUS] + AAA;
14758             *p++ = whiteToPlay ? '6'+BOARD_HEIGHT-8 : '3';
14759         } else {
14760             *p++ = '-';
14761         }
14762     } else {
14763         *p++ = '-';
14764     }
14765     *p++ = ' ';
14766   }
14767   }
14768
14769     /* [HGM] find reversible plies */
14770     {   int i = 0, j=move;
14771
14772         if (appData.debugMode) { int k;
14773             fprintf(debugFP, "write FEN 50-move: %d %d %d\n", initialRulePlies, forwardMostMove, backwardMostMove);
14774             for(k=backwardMostMove; k<=forwardMostMove; k++)
14775                 fprintf(debugFP, "e%d. p=%d\n", k, (signed char)boards[k][EP_STATUS]);
14776
14777         }
14778
14779         while(j > backwardMostMove && (signed char)boards[j][EP_STATUS] <= EP_NONE) j--,i++;
14780         if( j == backwardMostMove ) i += initialRulePlies;
14781         sprintf(p, "%d ", i);
14782         p += i>=100 ? 4 : i >= 10 ? 3 : 2;
14783     }
14784     /* Fullmove number */
14785     sprintf(p, "%d", (move / 2) + 1);
14786     
14787     return StrSave(buf);
14788 }
14789
14790 Boolean
14791 ParseFEN(board, blackPlaysFirst, fen)
14792     Board board;
14793      int *blackPlaysFirst;
14794      char *fen;
14795 {
14796     int i, j;
14797     char *p, c;
14798     int emptycount;
14799     ChessSquare piece;
14800
14801     p = fen;
14802
14803     /* [HGM] by default clear Crazyhouse holdings, if present */
14804     if(gameInfo.holdingsWidth) {
14805        for(i=0; i<BOARD_HEIGHT; i++) {
14806            board[i][0]             = EmptySquare; /* black holdings */
14807            board[i][BOARD_WIDTH-1] = EmptySquare; /* white holdings */
14808            board[i][1]             = (ChessSquare) 0; /* black counts */
14809            board[i][BOARD_WIDTH-2] = (ChessSquare) 0; /* white counts */
14810        }
14811     }
14812
14813     /* Piece placement data */
14814     for (i = BOARD_HEIGHT - 1; i >= 0; i--) {
14815         j = 0;
14816         for (;;) {
14817             if (*p == '/' || *p == ' ' || (*p == '[' && i == 0) ) {
14818                 if (*p == '/') p++;
14819                 emptycount = gameInfo.boardWidth - j;
14820                 while (emptycount--)
14821                         board[i][(j++)+gameInfo.holdingsWidth] = EmptySquare;
14822                 break;
14823 #if(BOARD_FILES >= 10)
14824             } else if(*p=='x' || *p=='X') { /* [HGM] X means 10 */
14825                 p++; emptycount=10;
14826                 if (j + emptycount > gameInfo.boardWidth) return FALSE;
14827                 while (emptycount--)
14828                         board[i][(j++)+gameInfo.holdingsWidth] = EmptySquare;
14829 #endif
14830             } else if (isdigit(*p)) {
14831                 emptycount = *p++ - '0';
14832                 while(isdigit(*p)) emptycount = 10*emptycount + *p++ - '0'; /* [HGM] allow > 9 */
14833                 if (j + emptycount > gameInfo.boardWidth) return FALSE;
14834                 while (emptycount--)
14835                         board[i][(j++)+gameInfo.holdingsWidth] = EmptySquare;
14836             } else if (*p == '+' || isalpha(*p)) {
14837                 if (j >= gameInfo.boardWidth) return FALSE;
14838                 if(*p=='+') {
14839                     piece = CharToPiece(*++p);
14840                     if(piece == EmptySquare) return FALSE; /* unknown piece */
14841                     piece = (ChessSquare) (PROMOTED piece ); p++;
14842                     if(PieceToChar(piece) != '+') return FALSE; /* unpromotable piece */
14843                 } else piece = CharToPiece(*p++);
14844
14845                 if(piece==EmptySquare) return FALSE; /* unknown piece */
14846                 if(*p == '~') { /* [HGM] make it a promoted piece for Crazyhouse */
14847                     piece = (ChessSquare) (PROMOTED piece);
14848                     if(PieceToChar(piece) != '~') return FALSE; /* cannot be a promoted piece */
14849                     p++;
14850                 }
14851                 board[i][(j++)+gameInfo.holdingsWidth] = piece;
14852             } else {
14853                 return FALSE;
14854             }
14855         }
14856     }
14857     while (*p == '/' || *p == ' ') p++;
14858
14859     /* [HGM] look for Crazyhouse holdings here */
14860     while(*p==' ') p++;
14861     if( gameInfo.holdingsWidth && p[-1] == '/' || *p == '[') {
14862         if(*p == '[') p++;
14863         if(*p == '-' ) *p++; /* empty holdings */ else {
14864             if( !gameInfo.holdingsWidth ) return FALSE; /* no room to put holdings! */
14865             /* if we would allow FEN reading to set board size, we would   */
14866             /* have to add holdings and shift the board read so far here   */
14867             while( (piece = CharToPiece(*p) ) != EmptySquare ) {
14868                 *p++;
14869                 if((int) piece >= (int) BlackPawn ) {
14870                     i = (int)piece - (int)BlackPawn;
14871                     i = PieceToNumber((ChessSquare)i);
14872                     if( i >= gameInfo.holdingsSize ) return FALSE;
14873                     board[BOARD_HEIGHT-1-i][0] = piece; /* black holdings */
14874                     board[BOARD_HEIGHT-1-i][1]++;       /* black counts   */
14875                 } else {
14876                     i = (int)piece - (int)WhitePawn;
14877                     i = PieceToNumber((ChessSquare)i);
14878                     if( i >= gameInfo.holdingsSize ) return FALSE;
14879                     board[i][BOARD_WIDTH-1] = piece;    /* white holdings */
14880                     board[i][BOARD_WIDTH-2]++;          /* black holdings */
14881                 }
14882             }
14883         }
14884         if(*p == ']') *p++;
14885     }
14886
14887     while(*p == ' ') p++;
14888
14889     /* Active color */
14890     c = *p++;
14891     if(appData.colorNickNames) {
14892       if( c == appData.colorNickNames[0] ) c = 'w'; else
14893       if( c == appData.colorNickNames[1] ) c = 'b';
14894     }
14895     switch (c) {
14896       case 'w':
14897         *blackPlaysFirst = FALSE;
14898         break;
14899       case 'b': 
14900         *blackPlaysFirst = TRUE;
14901         break;
14902       default:
14903         return FALSE;
14904     }
14905
14906     /* [HGM] We NO LONGER ignore the rest of the FEN notation */
14907     /* return the extra info in global variiables             */
14908
14909     /* set defaults in case FEN is incomplete */
14910     board[EP_STATUS] = EP_UNKNOWN;
14911     for(i=0; i<nrCastlingRights; i++ ) {
14912         board[CASTLING][i] =
14913             gameInfo.variant == VariantFischeRandom || gameInfo.variant == VariantCapaRandom ? NoRights : initialRights[i];
14914     }   /* assume possible unless obviously impossible */
14915     if(initialRights[0]!=NoRights && board[castlingRank[0]][initialRights[0]] != WhiteRook) board[CASTLING][0] = NoRights;
14916     if(initialRights[1]!=NoRights && board[castlingRank[1]][initialRights[1]] != WhiteRook) board[CASTLING][1] = NoRights;
14917     if(initialRights[2]!=NoRights && board[castlingRank[2]][initialRights[2]] != WhiteUnicorn
14918                                   && board[castlingRank[2]][initialRights[2]] != WhiteKing) board[CASTLING][2] = NoRights;
14919     if(initialRights[3]!=NoRights && board[castlingRank[3]][initialRights[3]] != BlackRook) board[CASTLING][3] = NoRights;
14920     if(initialRights[4]!=NoRights && board[castlingRank[4]][initialRights[4]] != BlackRook) board[CASTLING][4] = NoRights;
14921     if(initialRights[5]!=NoRights && board[castlingRank[5]][initialRights[5]] != BlackUnicorn
14922                                   && board[castlingRank[5]][initialRights[5]] != BlackKing) board[CASTLING][5] = NoRights;
14923     FENrulePlies = 0;
14924
14925     while(*p==' ') p++;
14926     if(nrCastlingRights) {
14927       if(*p=='K' || *p=='Q' || *p=='k' || *p=='q' || *p=='-') {
14928           /* castling indicator present, so default becomes no castlings */
14929           for(i=0; i<nrCastlingRights; i++ ) {
14930                  board[CASTLING][i] = NoRights;
14931           }
14932       }
14933       while(*p=='K' || *p=='Q' || *p=='k' || *p=='q' || *p=='-' ||
14934              (gameInfo.variant == VariantFischeRandom || gameInfo.variant == VariantCapaRandom) &&
14935              ( *p >= 'a' && *p < 'a' + gameInfo.boardWidth) ||
14936              ( *p >= 'A' && *p < 'A' + gameInfo.boardWidth)   ) {
14937         char c = *p++; int whiteKingFile=NoRights, blackKingFile=NoRights;
14938
14939         for(i=BOARD_LEFT; i<BOARD_RGHT; i++) {
14940             if(board[BOARD_HEIGHT-1][i] == BlackKing) blackKingFile = i;
14941             if(board[0             ][i] == WhiteKing) whiteKingFile = i;
14942         }
14943         if(gameInfo.variant == VariantTwoKings || gameInfo.variant == VariantKnightmate)
14944             whiteKingFile = blackKingFile = BOARD_WIDTH >> 1; // for these variant scanning fails
14945         if(whiteKingFile == NoRights || board[0][whiteKingFile] != WhiteUnicorn
14946                                      && board[0][whiteKingFile] != WhiteKing) whiteKingFile = NoRights;
14947         if(blackKingFile == NoRights || board[BOARD_HEIGHT-1][blackKingFile] != BlackUnicorn
14948                                      && board[BOARD_HEIGHT-1][blackKingFile] != BlackKing) blackKingFile = NoRights;
14949         switch(c) {
14950           case'K':
14951               for(i=BOARD_RGHT-1; board[0][i]!=WhiteRook && i>whiteKingFile; i--);
14952               board[CASTLING][0] = i != whiteKingFile ? i : NoRights;
14953               board[CASTLING][2] = whiteKingFile;
14954               break;
14955           case'Q':
14956               for(i=BOARD_LEFT;  i<BOARD_RGHT && board[0][i]!=WhiteRook && i<whiteKingFile; i++);
14957               board[CASTLING][1] = i != whiteKingFile ? i : NoRights;
14958               board[CASTLING][2] = whiteKingFile;
14959               break;
14960           case'k':
14961               for(i=BOARD_RGHT-1; board[BOARD_HEIGHT-1][i]!=BlackRook && i>blackKingFile; i--);
14962               board[CASTLING][3] = i != blackKingFile ? i : NoRights;
14963               board[CASTLING][5] = blackKingFile;
14964               break;
14965           case'q':
14966               for(i=BOARD_LEFT; i<BOARD_RGHT && board[BOARD_HEIGHT-1][i]!=BlackRook && i<blackKingFile; i++);
14967               board[CASTLING][4] = i != blackKingFile ? i : NoRights;
14968               board[CASTLING][5] = blackKingFile;
14969           case '-':
14970               break;
14971           default: /* FRC castlings */
14972               if(c >= 'a') { /* black rights */
14973                   for(i=BOARD_LEFT; i<BOARD_RGHT; i++)
14974                     if(board[BOARD_HEIGHT-1][i] == BlackKing) break;
14975                   if(i == BOARD_RGHT) break;
14976                   board[CASTLING][5] = i;
14977                   c -= AAA;
14978                   if(board[BOARD_HEIGHT-1][c] <  BlackPawn ||
14979                      board[BOARD_HEIGHT-1][c] >= BlackKing   ) break;
14980                   if(c > i)
14981                       board[CASTLING][3] = c;
14982                   else
14983                       board[CASTLING][4] = c;
14984               } else { /* white rights */
14985                   for(i=BOARD_LEFT; i<BOARD_RGHT; i++)
14986                     if(board[0][i] == WhiteKing) break;
14987                   if(i == BOARD_RGHT) break;
14988                   board[CASTLING][2] = i;
14989                   c -= AAA - 'a' + 'A';
14990                   if(board[0][c] >= WhiteKing) break;
14991                   if(c > i)
14992                       board[CASTLING][0] = c;
14993                   else
14994                       board[CASTLING][1] = c;
14995               }
14996         }
14997       }
14998       for(i=0; i<nrCastlingRights; i++)
14999         if(board[CASTLING][i] != NoRights) initialRights[i] = board[CASTLING][i];
15000     if (appData.debugMode) {
15001         fprintf(debugFP, "FEN castling rights:");
15002         for(i=0; i<nrCastlingRights; i++)
15003         fprintf(debugFP, " %d", board[CASTLING][i]);
15004         fprintf(debugFP, "\n");
15005     }
15006
15007       while(*p==' ') p++;
15008     }
15009
15010     /* read e.p. field in games that know e.p. capture */
15011     if(gameInfo.variant != VariantShogi    && gameInfo.variant != VariantXiangqi &&
15012        gameInfo.variant != VariantShatranj && gameInfo.variant != VariantCourier && gameInfo.variant != VariantMakruk ) { 
15013       if(*p=='-') {
15014         p++; board[EP_STATUS] = EP_NONE;
15015       } else {
15016          char c = *p++ - AAA;
15017
15018          if(c < BOARD_LEFT || c >= BOARD_RGHT) return TRUE;
15019          if(*p >= '0' && *p <='9') *p++;
15020          board[EP_STATUS] = c;
15021       }
15022     }
15023
15024
15025     if(sscanf(p, "%d", &i) == 1) {
15026         FENrulePlies = i; /* 50-move ply counter */
15027         /* (The move number is still ignored)    */
15028     }
15029
15030     return TRUE;
15031 }
15032       
15033 void
15034 EditPositionPasteFEN(char *fen)
15035 {
15036   if (fen != NULL) {
15037     Board initial_position;
15038
15039     if (!ParseFEN(initial_position, &blackPlaysFirst, fen)) {
15040       DisplayError(_("Bad FEN position in clipboard"), 0);
15041       return ;
15042     } else {
15043       int savedBlackPlaysFirst = blackPlaysFirst;
15044       EditPositionEvent();
15045       blackPlaysFirst = savedBlackPlaysFirst;
15046       CopyBoard(boards[0], initial_position);
15047       initialRulePlies = FENrulePlies; /* [HGM] copy FEN attributes as well */
15048       EditPositionDone(FALSE); // [HGM] fake: do not fake rights if we had FEN
15049       DisplayBothClocks();
15050       DrawPosition(FALSE, boards[currentMove]);
15051     }
15052   }
15053 }
15054
15055 static char cseq[12] = "\\   ";
15056
15057 Boolean set_cont_sequence(char *new_seq)
15058 {
15059     int len;
15060     Boolean ret;
15061
15062     // handle bad attempts to set the sequence
15063         if (!new_seq)
15064                 return 0; // acceptable error - no debug
15065
15066     len = strlen(new_seq);
15067     ret = (len > 0) && (len < sizeof(cseq));
15068     if (ret)
15069         strcpy(cseq, new_seq);
15070     else if (appData.debugMode)
15071         fprintf(debugFP, "Invalid continuation sequence \"%s\"  (maximum length is: %u)\n", new_seq, (unsigned) sizeof(cseq)-1);
15072     return ret;
15073 }
15074
15075 /*
15076     reformat a source message so words don't cross the width boundary.  internal
15077     newlines are not removed.  returns the wrapped size (no null character unless
15078     included in source message).  If dest is NULL, only calculate the size required
15079     for the dest buffer.  lp argument indicats line position upon entry, and it's
15080     passed back upon exit.
15081 */
15082 int wrap(char *dest, char *src, int count, int width, int *lp)
15083 {
15084     int len, i, ansi, cseq_len, line, old_line, old_i, old_len, clen;
15085
15086     cseq_len = strlen(cseq);
15087     old_line = line = *lp;
15088     ansi = len = clen = 0;
15089
15090     for (i=0; i < count; i++)
15091     {
15092         if (src[i] == '\033')
15093             ansi = 1;
15094
15095         // if we hit the width, back up
15096         if (!ansi && (line >= width) && src[i] != '\n' && src[i] != ' ')
15097         {
15098             // store i & len in case the word is too long
15099             old_i = i, old_len = len;
15100
15101             // find the end of the last word
15102             while (i && src[i] != ' ' && src[i] != '\n')
15103             {
15104                 i--;
15105                 len--;
15106             }
15107
15108             // word too long?  restore i & len before splitting it
15109             if ((old_i-i+clen) >= width)
15110             {
15111                 i = old_i;
15112                 len = old_len;
15113             }
15114
15115             // extra space?
15116             if (i && src[i-1] == ' ')
15117                 len--;
15118
15119             if (src[i] != ' ' && src[i] != '\n')
15120             {
15121                 i--;
15122                 if (len)
15123                     len--;
15124             }
15125
15126             // now append the newline and continuation sequence
15127             if (dest)
15128                 dest[len] = '\n';
15129             len++;
15130             if (dest)
15131                 strncpy(dest+len, cseq, cseq_len);
15132             len += cseq_len;
15133             line = cseq_len;
15134             clen = cseq_len;
15135             continue;
15136         }
15137
15138         if (dest)
15139             dest[len] = src[i];
15140         len++;
15141         if (!ansi)
15142             line++;
15143         if (src[i] == '\n')
15144             line = 0;
15145         if (src[i] == 'm')
15146             ansi = 0;
15147     }
15148     if (dest && appData.debugMode)
15149     {
15150         fprintf(debugFP, "wrap(count:%d,width:%d,line:%d,len:%d,*lp:%d,src: ",
15151             count, width, line, len, *lp);
15152         show_bytes(debugFP, src, count);
15153         fprintf(debugFP, "\ndest: ");
15154         show_bytes(debugFP, dest, len);
15155         fprintf(debugFP, "\n");
15156     }
15157     *lp = dest ? line : old_line;
15158
15159     return len;
15160 }
15161
15162 // [HGM] vari: routines for shelving variations
15163
15164 void 
15165 PushTail(int firstMove, int lastMove)
15166 {
15167         int i, j, nrMoves = lastMove - firstMove;
15168
15169         if(appData.icsActive) { // only in local mode
15170                 forwardMostMove = currentMove; // mimic old ICS behavior
15171                 return;
15172         }
15173         if(storedGames >= MAX_VARIATIONS-1) return;
15174
15175         // push current tail of game on stack
15176         savedResult[storedGames] = gameInfo.result;
15177         savedDetails[storedGames] = gameInfo.resultDetails;
15178         gameInfo.resultDetails = NULL;
15179         savedFirst[storedGames] = firstMove;
15180         savedLast [storedGames] = lastMove;
15181         savedFramePtr[storedGames] = framePtr;
15182         framePtr -= nrMoves; // reserve space for the boards
15183         for(i=nrMoves; i>=1; i--) { // copy boards to stack, working downwards, in case of overlap
15184             CopyBoard(boards[framePtr+i], boards[firstMove+i]);
15185             for(j=0; j<MOVE_LEN; j++)
15186                 moveList[framePtr+i][j] = moveList[firstMove+i-1][j];
15187             for(j=0; j<2*MOVE_LEN; j++)
15188                 parseList[framePtr+i][j] = parseList[firstMove+i-1][j];
15189             timeRemaining[0][framePtr+i] = timeRemaining[0][firstMove+i];
15190             timeRemaining[1][framePtr+i] = timeRemaining[1][firstMove+i];
15191             pvInfoList[framePtr+i] = pvInfoList[firstMove+i-1];
15192             pvInfoList[firstMove+i-1].depth = 0;
15193             commentList[framePtr+i] = commentList[firstMove+i];
15194             commentList[firstMove+i] = NULL;
15195         }
15196
15197         storedGames++;
15198         forwardMostMove = firstMove; // truncate game so we can start variation
15199         if(storedGames == 1) GreyRevert(FALSE);
15200 }
15201
15202 Boolean
15203 PopTail(Boolean annotate)
15204 {
15205         int i, j, nrMoves;
15206         char buf[8000], moveBuf[20];
15207
15208         if(appData.icsActive) return FALSE; // only in local mode
15209         if(!storedGames) return FALSE; // sanity
15210         CommentPopDown(); // make sure no stale variation comments to the destroyed line can remain open
15211
15212         storedGames--;
15213         ToNrEvent(savedFirst[storedGames]); // sets currentMove
15214         nrMoves = savedLast[storedGames] - currentMove;
15215         if(annotate) {
15216                 int cnt = 10;
15217                 if(!WhiteOnMove(currentMove)) sprintf(buf, "(%d...", currentMove+2>>1);
15218                 else strcpy(buf, "(");
15219                 for(i=currentMove; i<forwardMostMove; i++) {
15220                         if(WhiteOnMove(i))
15221                              sprintf(moveBuf, " %d. %s", i+2>>1, SavePart(parseList[i]));
15222                         else sprintf(moveBuf, " %s", SavePart(parseList[i]));
15223                         strcat(buf, moveBuf);
15224                         if(commentList[i]) { strcat(buf, " "); strcat(buf, commentList[i]); }
15225                         if(!--cnt) { strcat(buf, "\n"); cnt = 10; }
15226                 }
15227                 strcat(buf, ")");
15228         }
15229         for(i=1; i<=nrMoves; i++) { // copy last variation back
15230             CopyBoard(boards[currentMove+i], boards[framePtr+i]);
15231             for(j=0; j<MOVE_LEN; j++)
15232                 moveList[currentMove+i-1][j] = moveList[framePtr+i][j];
15233             for(j=0; j<2*MOVE_LEN; j++)
15234                 parseList[currentMove+i-1][j] = parseList[framePtr+i][j];
15235             timeRemaining[0][currentMove+i] = timeRemaining[0][framePtr+i];
15236             timeRemaining[1][currentMove+i] = timeRemaining[1][framePtr+i];
15237             pvInfoList[currentMove+i-1] = pvInfoList[framePtr+i];
15238             if(commentList[currentMove+i]) free(commentList[currentMove+i]);
15239             commentList[currentMove+i] = commentList[framePtr+i];
15240             commentList[framePtr+i] = NULL;
15241         }
15242         if(annotate) AppendComment(currentMove+1, buf, FALSE);
15243         framePtr = savedFramePtr[storedGames];
15244         gameInfo.result = savedResult[storedGames];
15245         if(gameInfo.resultDetails != NULL) {
15246             free(gameInfo.resultDetails);
15247       }
15248         gameInfo.resultDetails = savedDetails[storedGames];
15249         forwardMostMove = currentMove + nrMoves;
15250         if(storedGames == 0) GreyRevert(TRUE);
15251         return TRUE;
15252 }
15253
15254 void 
15255 CleanupTail()
15256 {       // remove all shelved variations
15257         int i;
15258         for(i=0; i<storedGames; i++) {
15259             if(savedDetails[i])
15260                 free(savedDetails[i]);
15261             savedDetails[i] = NULL;
15262         }
15263         for(i=framePtr; i<MAX_MOVES; i++) {
15264                 if(commentList[i]) free(commentList[i]);
15265                 commentList[i] = NULL;
15266         }
15267         framePtr = MAX_MOVES-1;
15268         storedGames = 0;
15269 }
15270
15271 void
15272 LoadVariation(int index, char *text)
15273 {       // [HGM] vari: shelve previous line and load new variation, parsed from text around text[index]
15274         char *p = text, *start = NULL, *end = NULL, wait = NULLCHAR;
15275         int level = 0, move;
15276
15277         if(gameMode != EditGame && gameMode != AnalyzeMode) return;
15278         // first find outermost bracketing variation
15279         while(*p) { // hope I got this right... Non-nesting {} and [] can screen each other and nesting ()
15280             if(!wait) { // while inside [] pr {}, ignore everyting except matching closing ]}
15281                 if(*p == '{') wait = '}'; else
15282                 if(*p == '[') wait = ']'; else
15283                 if(*p == '(' && level++ == 0 && p-text < index) start = p+1;
15284                 if(*p == ')' && level > 0 && --level == 0 && p-text > index && end == NULL) end = p-1;
15285             }
15286             if(*p == wait) wait = NULLCHAR; // closing ]} found
15287             p++;
15288         }
15289         if(!start || !end) return; // no variation found, or syntax error in PGN: ignore click
15290         if(appData.debugMode) fprintf(debugFP, "at move %d load variation '%s'\n", currentMove, start);
15291         end[1] = NULLCHAR; // clip off comment beyond variation
15292         ToNrEvent(currentMove-1);
15293         PushTail(currentMove, forwardMostMove); // shelve main variation. This truncates game
15294         // kludge: use ParsePV() to append variation to game
15295         move = currentMove;
15296         ParsePV(start, TRUE);
15297         forwardMostMove = endPV; endPV = -1; currentMove = move; // cleanup what ParsePV did
15298         ClearPremoveHighlights();
15299         CommentPopDown();
15300         ToNrEvent(currentMove+1);
15301 }
15302