2 * backend.c -- Common back end for X and Windows NT versions of
4 * Copyright 1991 by Digital Equipment Corporation, Maynard,
7 * Enhancements Copyright 1992-2001, 2002, 2003, 2004, 2005, 2006,
8 * 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
10 * Enhancements Copyright 2005 Alessandro Scotti
12 * The following terms apply to Digital Equipment Corporation's copyright
14 * ------------------------------------------------------------------------
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.
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
32 * ------------------------------------------------------------------------
34 * The following terms apply to the enhanced version of XBoard
35 * distributed by the Free Software Foundation:
36 * ------------------------------------------------------------------------
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.
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.
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/. *
51 *------------------------------------------------------------------------
52 ** See the file ChangeLog for a revision history. */
54 /* [AS] Also useful here for debugging */
58 #define DoSleep( n ) if( (n) != 0 ) Sleep( (n) );
60 int flock(int f, int code);
65 #define DoSleep( n ) if( (n) >= 0) sleep(n)
75 #include <sys/types.h>
84 #else /* not STDC_HEADERS */
87 # else /* not HAVE_STRING_H */
89 # endif /* not HAVE_STRING_H */
90 #endif /* not STDC_HEADERS */
93 # include <sys/fcntl.h>
94 #else /* not HAVE_SYS_FCNTL_H */
97 # endif /* HAVE_FCNTL_H */
98 #endif /* not HAVE_SYS_FCNTL_H */
100 #if TIME_WITH_SYS_TIME
101 # include <sys/time.h>
105 # include <sys/time.h>
111 #if defined(_amigados) && !defined(__GNUC__)
116 extern int gettimeofday(struct timeval *, struct timezone *);
124 #include "frontend.h"
131 #include "backendz.h"
135 # define _(s) gettext (s)
136 # define N_(s) gettext_noop (s)
137 # define T_(s) gettext(s)
150 /* A point in time */
152 long sec; /* Assuming this is >= 32 bits */
153 int ms; /* Assuming this is >= 16 bits */
156 int establish P((void));
157 void read_from_player P((InputSourceRef isr, VOIDSTAR closure,
158 char *buf, int count, int error));
159 void read_from_ics P((InputSourceRef isr, VOIDSTAR closure,
160 char *buf, int count, int error));
161 void ics_printf P((char *format, ...));
162 void SendToICS P((char *s));
163 void SendToICSDelayed P((char *s, long msdelay));
164 void SendMoveToICS P((ChessMove moveType, int fromX, int fromY, int toX, int toY, char promoChar));
165 void HandleMachineMove P((char *message, ChessProgramState *cps));
166 int AutoPlayOneMove P((void));
167 int LoadGameOneMove P((ChessMove readAhead));
168 int LoadGameFromFile P((char *filename, int n, char *title, int useList));
169 int LoadPositionFromFile P((char *filename, int n, char *title));
170 int SavePositionToFile P((char *filename));
171 void ApplyMove P((int fromX, int fromY, int toX, int toY, int promoChar,
173 void MakeMove P((int fromX, int fromY, int toX, int toY, int promoChar));
174 void ShowMove P((int fromX, int fromY, int toX, int toY));
175 int FinishMove P((ChessMove moveType, int fromX, int fromY, int toX, int toY,
176 /*char*/int promoChar));
177 void BackwardInner P((int target));
178 void ForwardInner P((int target));
179 int Adjudicate P((ChessProgramState *cps));
180 void GameEnds P((ChessMove result, char *resultDetails, int whosays));
181 void EditPositionDone P((Boolean fakeRights));
182 void PrintOpponents P((FILE *fp));
183 void PrintPosition P((FILE *fp, int move));
184 void StartChessProgram P((ChessProgramState *cps));
185 void SendToProgram P((char *message, ChessProgramState *cps));
186 void SendMoveToProgram P((int moveNum, ChessProgramState *cps));
187 void ReceiveFromProgram P((InputSourceRef isr, VOIDSTAR closure,
188 char *buf, int count, int error));
189 void SendTimeControl P((ChessProgramState *cps,
190 int mps, long tc, int inc, int sd, int st));
191 char *TimeControlTagValue P((void));
192 void Attention P((ChessProgramState *cps));
193 void FeedMovesToProgram P((ChessProgramState *cps, int upto));
194 void ResurrectChessProgram P((void));
195 void DisplayComment P((int moveNumber, char *text));
196 void DisplayMove P((int moveNumber));
198 void ParseGameHistory P((char *game));
199 void ParseBoard12 P((char *string));
200 void KeepAlive P((void));
201 void StartClocks P((void));
202 void SwitchClocks P((int nr));
203 void StopClocks P((void));
204 void ResetClocks P((void));
205 char *PGNDate P((void));
206 void SetGameInfo P((void));
207 Boolean ParseFEN P((Board board, int *blackPlaysFirst, char *fen));
208 int RegisterMove P((void));
209 void MakeRegisteredMove P((void));
210 void TruncateGame P((void));
211 int looking_at P((char *, int *, char *));
212 void CopyPlayerNameIntoFileName P((char **, char *));
213 char *SavePart P((char *));
214 int SaveGameOldStyle P((FILE *));
215 int SaveGamePGN P((FILE *));
216 void GetTimeMark P((TimeMark *));
217 long SubtractTimeMarks P((TimeMark *, TimeMark *));
218 int CheckFlags P((void));
219 long NextTickLength P((long));
220 void CheckTimeControl P((void));
221 void show_bytes P((FILE *, char *, int));
222 int string_to_rating P((char *str));
223 void ParseFeatures P((char* args, ChessProgramState *cps));
224 void InitBackEnd3 P((void));
225 void FeatureDone P((ChessProgramState* cps, int val));
226 void InitChessProgram P((ChessProgramState *cps, int setup));
227 void OutputKibitz(int window, char *text);
228 int PerpetualChase(int first, int last);
229 int EngineOutputIsUp();
230 void InitDrawingSizes(int x, int y);
233 extern void ConsoleCreate();
236 ChessProgramState *WhitePlayer();
237 void InsertIntoMemo P((int which, char *text)); // [HGM] kibitz: in engineo.c
238 int VerifyDisplayMode P(());
240 char *GetInfoFromComment( int, char * ); // [HGM] PV time: returns stripped comment
241 void InitEngineUCI( const char * iniDir, ChessProgramState * cps ); // [HGM] moved here from winboard.c
242 char *ProbeBook P((int moveNr, char *book)); // [HGM] book: returns a book move
243 char *SendMoveToBookUser P((int nr, ChessProgramState *cps, int initial)); // [HGM] book
244 void ics_update_width P((int new_width));
245 extern char installDir[MSG_SIZ];
246 VariantClass startVariant; /* [HGM] nicks: initial variant */
248 extern int tinyLayout, smallLayout;
249 ChessProgramStats programStats;
250 char lastPV[2][2*MSG_SIZ]; /* [HGM] pv: last PV in thinking output of each engine */
252 static int exiting = 0; /* [HGM] moved to top */
253 static int setboardSpoiledMachineBlack = 0 /*, errorExitFlag = 0*/;
254 int startedFromPositionFile = FALSE; Board filePosition; /* [HGM] loadPos */
255 Board partnerBoard; /* [HGM] bughouse: for peeking at partner game */
256 int partnerHighlight[2];
257 Boolean partnerBoardValid = 0;
258 char partnerStatus[MSG_SIZ];
260 Boolean originalFlip;
261 Boolean twoBoards = 0;
262 char endingGame = 0; /* [HGM] crash: flag to prevent recursion of GameEnds() */
263 int whiteNPS, blackNPS; /* [HGM] nps: for easily making clocks aware of NPS */
264 VariantClass currentlyInitializedVariant; /* [HGM] variantswitch */
265 int lastIndex = 0; /* [HGM] autoinc: last game/position used in match mode */
266 Boolean connectionAlive;/* [HGM] alive: ICS connection status from probing */
267 int opponentKibitzes;
268 int lastSavedGame; /* [HGM] save: ID of game */
269 char chatPartner[MAX_CHAT][MSG_SIZ]; /* [HGM] chat: list of chatting partners */
270 extern int chatCount;
272 char marker[BOARD_RANKS][BOARD_FILES]; /* [HGM] marks for target squares */
273 char lastMsg[MSG_SIZ];
274 ChessSquare pieceSweep = EmptySquare;
275 ChessSquare promoSweep = EmptySquare, defaultPromoChoice;
276 int promoDefaultAltered;
278 /* States for ics_getting_history */
280 #define H_REQUESTED 1
281 #define H_GOT_REQ_HEADER 2
282 #define H_GOT_UNREQ_HEADER 3
283 #define H_GETTING_MOVES 4
284 #define H_GOT_UNWANTED_HEADER 5
286 /* whosays values for GameEnds */
295 /* Maximum number of games in a cmail message */
296 #define CMAIL_MAX_GAMES 20
298 /* Different types of move when calling RegisterMove */
300 #define CMAIL_RESIGN 1
302 #define CMAIL_ACCEPT 3
304 /* Different types of result to remember for each game */
305 #define CMAIL_NOT_RESULT 0
306 #define CMAIL_OLD_RESULT 1
307 #define CMAIL_NEW_RESULT 2
309 /* Telnet protocol constants */
320 safeStrCpy( char *dst, const char *src, size_t count )
323 assert( dst != NULL );
324 assert( src != NULL );
327 for(i=0; i<count; i++) if((dst[i] = src[i]) == NULLCHAR) break;
328 if( i == count && dst[count-1] != NULLCHAR)
330 dst[ count-1 ] = '\0'; // make sure incomplete copy still null-terminated
331 if(appData.debugMode)
332 fprintf(debugFP, "safeStrCpy: copying %s into %s didn't work, not enough space %d\n",src,dst, (int)count);
338 /* Some compiler can't cast u64 to double
339 * This function do the job for us:
341 * We use the highest bit for cast, this only
342 * works if the highest bit is not
343 * in use (This should not happen)
345 * We used this for all compiler
348 u64ToDouble(u64 value)
351 u64 tmp = value & u64Const(0x7fffffffffffffff);
352 r = (double)(s64)tmp;
353 if (value & u64Const(0x8000000000000000))
354 r += 9.2233720368547758080e18; /* 2^63 */
358 /* Fake up flags for now, as we aren't keeping track of castling
359 availability yet. [HGM] Change of logic: the flag now only
360 indicates the type of castlings allowed by the rule of the game.
361 The actual rights themselves are maintained in the array
362 castlingRights, as part of the game history, and are not probed
368 int flags = F_ALL_CASTLE_OK;
369 if ((index % 2) == 0) flags |= F_WHITE_ON_MOVE;
370 switch (gameInfo.variant) {
372 flags &= ~F_ALL_CASTLE_OK;
373 case VariantGiveaway: // [HGM] moved this case label one down: seems Giveaway does have castling on ICC!
374 flags |= F_IGNORE_CHECK;
376 flags |= F_MANDATORY_CAPTURE; //[HGM] losers: sets flag so TestLegality rejects non-capts if capts exist
379 flags |= F_IGNORE_CHECK | F_ATOMIC_CAPTURE;
381 case VariantKriegspiel:
382 flags |= F_KRIEGSPIEL_CAPTURE;
384 case VariantCapaRandom:
385 case VariantFischeRandom:
386 flags |= F_FRC_TYPE_CASTLING; /* [HGM] enable this through flag */
387 case VariantNoCastle:
388 case VariantShatranj:
391 flags &= ~F_ALL_CASTLE_OK;
399 FILE *gameFileFP, *debugFP;
402 [AS] Note: sometimes, the sscanf() function is used to parse the input
403 into a fixed-size buffer. Because of this, we must be prepared to
404 receive strings as long as the size of the input buffer, which is currently
405 set to 4K for Windows and 8K for the rest.
406 So, we must either allocate sufficiently large buffers here, or
407 reduce the size of the input buffer in the input reading part.
410 char cmailMove[CMAIL_MAX_GAMES][MOVE_LEN], cmailMsg[MSG_SIZ];
411 char bookOutput[MSG_SIZ*10], thinkOutput[MSG_SIZ*10], lastHint[MSG_SIZ];
412 char thinkOutput1[MSG_SIZ*10];
414 ChessProgramState first, second;
416 /* premove variables */
419 int premoveFromX = 0;
420 int premoveFromY = 0;
421 int premovePromoChar = 0;
423 Boolean alarmSounded;
424 /* end premove variables */
426 char *ics_prefix = "$";
427 int ics_type = ICS_GENERIC;
429 int currentMove = 0, forwardMostMove = 0, backwardMostMove = 0;
430 int pauseExamForwardMostMove = 0;
431 int nCmailGames = 0, nCmailResults = 0, nCmailMovesRegistered = 0;
432 int cmailMoveRegistered[CMAIL_MAX_GAMES], cmailResult[CMAIL_MAX_GAMES];
433 int cmailMsgLoaded = FALSE, cmailMailedMove = FALSE;
434 int cmailOldMove = -1, firstMove = TRUE, flipView = FALSE;
435 int blackPlaysFirst = FALSE, startedFromSetupPosition = FALSE;
436 int searchTime = 0, pausing = FALSE, pauseExamInvalid = FALSE;
437 int whiteFlag = FALSE, blackFlag = FALSE;
438 int userOfferedDraw = FALSE;
439 int ics_user_moved = 0, ics_gamenum = -1, ics_getting_history = H_FALSE;
440 int matchMode = FALSE, hintRequested = FALSE, bookRequested = FALSE;
441 int cmailMoveType[CMAIL_MAX_GAMES];
442 long ics_clock_paused = 0;
443 ProcRef icsPR = NoProc, cmailPR = NoProc;
444 InputSourceRef telnetISR = NULL, fromUserISR = NULL, cmailISR = NULL;
445 GameMode gameMode = BeginningOfGame;
446 char moveList[MAX_MOVES][MOVE_LEN], parseList[MAX_MOVES][MOVE_LEN * 2];
447 char *commentList[MAX_MOVES], *cmailCommentList[CMAIL_MAX_GAMES];
448 ChessProgramStats_Move pvInfoList[MAX_MOVES]; /* [AS] Info about engine thinking */
449 int hiddenThinkOutputState = 0; /* [AS] */
450 int adjudicateLossThreshold = 0; /* [AS] Automatic adjudication */
451 int adjudicateLossPlies = 6;
452 char white_holding[64], black_holding[64];
453 TimeMark lastNodeCountTime;
454 long lastNodeCount=0;
455 int shiftKey; // [HGM] set by mouse handler
457 int have_sent_ICS_logon = 0;
459 int suddenDeath, whiteStartMove, blackStartMove; /* [HGM] for implementation of 'any per time' sessions, as in first part of byoyomi TC */
460 long whiteTimeRemaining, blackTimeRemaining, timeControl, timeIncrement, lastWhite, lastBlack;
461 long timeControl_2; /* [AS] Allow separate time controls */
462 char *fullTimeControlString = NULL, *nextSession, *whiteTC, *blackTC; /* [HGM] secondary TC: merge of MPS, TC and inc */
463 long timeRemaining[2][MAX_MOVES];
465 TimeMark programStartTime;
466 char ics_handle[MSG_SIZ];
467 int have_set_title = 0;
469 /* animateTraining preserves the state of appData.animate
470 * when Training mode is activated. This allows the
471 * response to be animated when appData.animate == TRUE and
472 * appData.animateDragging == TRUE.
474 Boolean animateTraining;
480 Board boards[MAX_MOVES];
481 /* [HGM] Following 7 needed for accurate legality tests: */
482 signed char castlingRank[BOARD_FILES]; // and corresponding ranks
483 signed char initialRights[BOARD_FILES];
484 int nrCastlingRights; // For TwoKings, or to implement castling-unknown status
485 int initialRulePlies, FENrulePlies;
486 FILE *serverMoves = NULL; // next two for broadcasting (/serverMoves option)
489 int mute; // mute all sounds
491 // [HGM] vari: next 12 to save and restore variations
492 #define MAX_VARIATIONS 10
493 int framePtr = MAX_MOVES-1; // points to free stack entry
495 int savedFirst[MAX_VARIATIONS];
496 int savedLast[MAX_VARIATIONS];
497 int savedFramePtr[MAX_VARIATIONS];
498 char *savedDetails[MAX_VARIATIONS];
499 ChessMove savedResult[MAX_VARIATIONS];
501 void PushTail P((int firstMove, int lastMove));
502 Boolean PopTail P((Boolean annotate));
503 void CleanupTail P((void));
505 ChessSquare FIDEArray[2][BOARD_FILES] = {
506 { WhiteRook, WhiteKnight, WhiteBishop, WhiteQueen,
507 WhiteKing, WhiteBishop, WhiteKnight, WhiteRook },
508 { BlackRook, BlackKnight, BlackBishop, BlackQueen,
509 BlackKing, BlackBishop, BlackKnight, BlackRook }
512 ChessSquare twoKingsArray[2][BOARD_FILES] = {
513 { WhiteRook, WhiteKnight, WhiteBishop, WhiteQueen,
514 WhiteKing, WhiteKing, WhiteKnight, WhiteRook },
515 { BlackRook, BlackKnight, BlackBishop, BlackQueen,
516 BlackKing, BlackKing, BlackKnight, BlackRook }
519 ChessSquare KnightmateArray[2][BOARD_FILES] = {
520 { WhiteRook, WhiteMan, WhiteBishop, WhiteQueen,
521 WhiteUnicorn, WhiteBishop, WhiteMan, WhiteRook },
522 { BlackRook, BlackMan, BlackBishop, BlackQueen,
523 BlackUnicorn, BlackBishop, BlackMan, BlackRook }
526 ChessSquare SpartanArray[2][BOARD_FILES] = {
527 { WhiteRook, WhiteKnight, WhiteBishop, WhiteQueen,
528 WhiteKing, WhiteBishop, WhiteKnight, WhiteRook },
529 { BlackAlfil, BlackMarshall, BlackKing, BlackDragon,
530 BlackDragon, BlackKing, BlackAngel, BlackAlfil }
533 ChessSquare fairyArray[2][BOARD_FILES] = { /* [HGM] Queen side differs from King side */
534 { WhiteRook, WhiteKnight, WhiteBishop, WhiteQueen,
535 WhiteKing, WhiteBishop, WhiteKnight, WhiteRook },
536 { BlackCardinal, BlackAlfil, BlackMarshall, BlackAngel,
537 BlackKing, BlackMarshall, BlackAlfil, BlackCardinal }
540 ChessSquare ShatranjArray[2][BOARD_FILES] = { /* [HGM] (movGen knows about Shatranj Q and P) */
541 { WhiteRook, WhiteKnight, WhiteAlfil, WhiteKing,
542 WhiteFerz, WhiteAlfil, WhiteKnight, WhiteRook },
543 { BlackRook, BlackKnight, BlackAlfil, BlackKing,
544 BlackFerz, BlackAlfil, BlackKnight, BlackRook }
547 ChessSquare makrukArray[2][BOARD_FILES] = { /* [HGM] (movGen knows about Shatranj Q and P) */
548 { WhiteRook, WhiteKnight, WhiteMan, WhiteKing,
549 WhiteFerz, WhiteMan, WhiteKnight, WhiteRook },
550 { BlackRook, BlackKnight, BlackMan, BlackFerz,
551 BlackKing, BlackMan, BlackKnight, BlackRook }
555 #if (BOARD_FILES>=10)
556 ChessSquare ShogiArray[2][BOARD_FILES] = {
557 { WhiteQueen, WhiteKnight, WhiteFerz, WhiteWazir,
558 WhiteKing, WhiteWazir, WhiteFerz, WhiteKnight, WhiteQueen },
559 { BlackQueen, BlackKnight, BlackFerz, BlackWazir,
560 BlackKing, BlackWazir, BlackFerz, BlackKnight, BlackQueen }
563 ChessSquare XiangqiArray[2][BOARD_FILES] = {
564 { WhiteRook, WhiteKnight, WhiteAlfil, WhiteFerz,
565 WhiteWazir, WhiteFerz, WhiteAlfil, WhiteKnight, WhiteRook },
566 { BlackRook, BlackKnight, BlackAlfil, BlackFerz,
567 BlackWazir, BlackFerz, BlackAlfil, BlackKnight, BlackRook }
570 ChessSquare CapablancaArray[2][BOARD_FILES] = {
571 { WhiteRook, WhiteKnight, WhiteAngel, WhiteBishop, WhiteQueen,
572 WhiteKing, WhiteBishop, WhiteMarshall, WhiteKnight, WhiteRook },
573 { BlackRook, BlackKnight, BlackAngel, BlackBishop, BlackQueen,
574 BlackKing, BlackBishop, BlackMarshall, BlackKnight, BlackRook }
577 ChessSquare GreatArray[2][BOARD_FILES] = {
578 { WhiteDragon, WhiteKnight, WhiteAlfil, WhiteGrasshopper, WhiteKing,
579 WhiteSilver, WhiteCardinal, WhiteAlfil, WhiteKnight, WhiteDragon },
580 { BlackDragon, BlackKnight, BlackAlfil, BlackGrasshopper, BlackKing,
581 BlackSilver, BlackCardinal, BlackAlfil, BlackKnight, BlackDragon },
584 ChessSquare JanusArray[2][BOARD_FILES] = {
585 { WhiteRook, WhiteAngel, WhiteKnight, WhiteBishop, WhiteKing,
586 WhiteQueen, WhiteBishop, WhiteKnight, WhiteAngel, WhiteRook },
587 { BlackRook, BlackAngel, BlackKnight, BlackBishop, BlackKing,
588 BlackQueen, BlackBishop, BlackKnight, BlackAngel, BlackRook }
592 ChessSquare GothicArray[2][BOARD_FILES] = {
593 { WhiteRook, WhiteKnight, WhiteBishop, WhiteQueen, WhiteMarshall,
594 WhiteKing, WhiteAngel, WhiteBishop, WhiteKnight, WhiteRook },
595 { BlackRook, BlackKnight, BlackBishop, BlackQueen, BlackMarshall,
596 BlackKing, BlackAngel, BlackBishop, BlackKnight, BlackRook }
599 #define GothicArray CapablancaArray
603 ChessSquare FalconArray[2][BOARD_FILES] = {
604 { WhiteRook, WhiteKnight, WhiteBishop, WhiteLance, WhiteQueen,
605 WhiteKing, WhiteLance, WhiteBishop, WhiteKnight, WhiteRook },
606 { BlackRook, BlackKnight, BlackBishop, BlackLance, BlackQueen,
607 BlackKing, BlackLance, BlackBishop, BlackKnight, BlackRook }
610 #define FalconArray CapablancaArray
613 #else // !(BOARD_FILES>=10)
614 #define XiangqiPosition FIDEArray
615 #define CapablancaArray FIDEArray
616 #define GothicArray FIDEArray
617 #define GreatArray FIDEArray
618 #endif // !(BOARD_FILES>=10)
620 #if (BOARD_FILES>=12)
621 ChessSquare CourierArray[2][BOARD_FILES] = {
622 { WhiteRook, WhiteKnight, WhiteAlfil, WhiteBishop, WhiteMan, WhiteKing,
623 WhiteFerz, WhiteWazir, WhiteBishop, WhiteAlfil, WhiteKnight, WhiteRook },
624 { BlackRook, BlackKnight, BlackAlfil, BlackBishop, BlackMan, BlackKing,
625 BlackFerz, BlackWazir, BlackBishop, BlackAlfil, BlackKnight, BlackRook }
627 #else // !(BOARD_FILES>=12)
628 #define CourierArray CapablancaArray
629 #endif // !(BOARD_FILES>=12)
632 Board initialPosition;
635 /* Convert str to a rating. Checks for special cases of "----",
637 "++++", etc. Also strips ()'s */
639 string_to_rating(str)
642 while(*str && !isdigit(*str)) ++str;
644 return 0; /* One of the special "no rating" cases */
652 /* Init programStats */
653 programStats.movelist[0] = 0;
654 programStats.depth = 0;
655 programStats.nr_moves = 0;
656 programStats.moves_left = 0;
657 programStats.nodes = 0;
658 programStats.time = -1; // [HGM] PGNtime: make invalid to recognize engine output
659 programStats.score = 0;
660 programStats.got_only_move = 0;
661 programStats.got_fail = 0;
662 programStats.line_is_book = 0;
667 { // [HGM] moved some code here from InitBackend1 that has to be done after both engines have contributed their settings
668 if (appData.firstPlaysBlack) {
669 first.twoMachinesColor = "black\n";
670 second.twoMachinesColor = "white\n";
672 first.twoMachinesColor = "white\n";
673 second.twoMachinesColor = "black\n";
676 first.other = &second;
677 second.other = &first;
680 if(appData.timeOddsMode) {
681 norm = appData.timeOdds[0];
682 if(norm > appData.timeOdds[1]) norm = appData.timeOdds[1];
684 first.timeOdds = appData.timeOdds[0]/norm;
685 second.timeOdds = appData.timeOdds[1]/norm;
688 if(programVersion) free(programVersion);
689 if (appData.noChessProgram) {
690 programVersion = (char*) malloc(5 + strlen(PACKAGE_STRING));
691 sprintf(programVersion, "%s", PACKAGE_STRING);
693 /* [HGM] tidy: use tidy name, in stead of full pathname (which was probably a bug due to / vs \ ) */
694 programVersion = (char*) malloc(8 + strlen(PACKAGE_STRING) + strlen(first.tidy));
695 sprintf(programVersion, "%s + %s", PACKAGE_STRING, first.tidy);
700 UnloadEngine(ChessProgramState *cps)
702 /* Kill off first chess program */
703 if (cps->isr != NULL)
704 RemoveInputSource(cps->isr);
707 if (cps->pr != NoProc) {
709 DoSleep( appData.delayBeforeQuit );
710 SendToProgram("quit\n", cps);
711 DoSleep( appData.delayAfterQuit );
712 DestroyChildProcess(cps->pr, cps->useSigterm);
718 ClearOptions(ChessProgramState *cps)
721 cps->nrOptions = cps->comboCnt = 0;
722 for(i=0; i<MAX_OPTIONS; i++) {
723 cps->option[i].min = cps->option[i].max = cps->option[i].value = 0;
724 cps->option[i].textValue = 0;
728 char *engineNames[] = {
733 InitEngine(ChessProgramState *cps, int n)
734 { // [HGM] all engine initialiation put in a function that does one engine
738 cps->which = engineNames[n];
739 cps->maybeThinking = FALSE;
743 cps->sendDrawOffers = 1;
745 cps->program = appData.chessProgram[n];
746 cps->host = appData.host[n];
747 cps->dir = appData.directory[n];
748 cps->initString = appData.engInitString[n];
749 cps->computerString = appData.computerString[n];
750 cps->useSigint = TRUE;
751 cps->useSigterm = TRUE;
752 cps->reuse = appData.reuse[n];
753 cps->nps = appData.NPS[n]; // [HGM] nps: copy nodes per second
754 cps->useSetboard = FALSE;
756 cps->usePing = FALSE;
759 cps->usePlayother = FALSE;
760 cps->useColors = TRUE;
761 cps->useUsermove = FALSE;
762 cps->sendICS = FALSE;
763 cps->sendName = appData.icsActive;
764 cps->sdKludge = FALSE;
765 cps->stKludge = FALSE;
766 TidyProgramName(cps->program, cps->host, cps->tidy);
768 safeStrCpy(cps->variants, appData.variant, MSG_SIZ);
769 cps->analysisSupport = 2; /* detect */
770 cps->analyzing = FALSE;
771 cps->initDone = FALSE;
773 /* New features added by Tord: */
774 cps->useFEN960 = FALSE;
775 cps->useOOCastle = TRUE;
776 /* End of new features added by Tord. */
777 cps->fenOverride = appData.fenOverride[n];
779 /* [HGM] time odds: set factor for each machine */
780 cps->timeOdds = appData.timeOdds[n];
782 /* [HGM] secondary TC: how to handle sessions that do not fit in 'level'*/
783 cps->accumulateTC = appData.accumulateTC[n];
784 cps->maxNrOfSessions = 1;
788 cps->supportsNPS = UNKNOWN;
791 cps->optionSettings = appData.engOptions[n];
793 cps->scoreIsAbsolute = appData.scoreIsAbsolute[n]; /* [AS] */
794 cps->isUCI = appData.isUCI[n]; /* [AS] */
795 cps->hasOwnBookUCI = appData.hasOwnBookUCI[n]; /* [AS] */
797 if (appData.protocolVersion[n] > PROTOVER
798 || appData.protocolVersion[n] < 1)
803 len = snprintf(buf, MSG_SIZ, _("protocol version %d not supported"),
804 appData.protocolVersion[n]);
805 if( (len > MSG_SIZ) && appData.debugMode )
806 fprintf(debugFP, "InitBackEnd1: buffer truncated.\n");
808 DisplayFatalError(buf, 0, 2);
812 cps->protocolVersion = appData.protocolVersion[n];
815 InitEngineUCI( installDir, cps ); // [HGM] moved here from winboard.c, to make available in xboard
818 ChessProgramState *savCps;
824 if(WaitForEngine(savCps, LoadEngine)) return;
825 CommonEngineInit(); // recalculate time odds
826 if(gameInfo.variant != StringToVariant(appData.variant)) {
827 // we changed variant when loading the engine; this forces us to reset
828 Reset(TRUE, savCps != &first);
829 EditGameEvent(); // for consistency with other path, as Reset changes mode
831 InitChessProgram(savCps, FALSE);
832 SendToProgram("force\n", savCps);
833 DisplayMessage("", "");
834 if (startedFromSetupPosition) SendBoard(savCps, backwardMostMove);
835 for (i = backwardMostMove; i < forwardMostMove; i++) SendMoveToProgram(i, savCps);
841 ReplaceEngine(ChessProgramState *cps, int n)
845 appData.noChessProgram = False;
846 appData.clockMode = True;
848 if(n) return; // only startup first engine immediately; second can wait
849 savCps = cps; // parameter to LoadEngine passed as globals, to allow scheduled calling :-(
856 int matched, min, sec;
858 ShowThinkingEvent(); // [HGM] thinking: make sure post/nopost state is set according to options
859 startVariant = StringToVariant(appData.variant); // [HGM] nicks: remember original variant
861 GetTimeMark(&programStartTime);
862 srandom((programStartTime.ms + 1000*programStartTime.sec)*0x1001001); // [HGM] book: makes sure random is unpredictabe to msec level
865 programStats.ok_to_send = 1;
866 programStats.seen_stat = 0;
869 * Initialize game list
875 * Internet chess server status
877 if (appData.icsActive) {
878 appData.matchMode = FALSE;
879 appData.matchGames = 0;
881 appData.noChessProgram = !appData.zippyPlay;
883 appData.zippyPlay = FALSE;
884 appData.zippyTalk = FALSE;
885 appData.noChessProgram = TRUE;
887 if (*appData.icsHelper != NULLCHAR) {
888 appData.useTelnet = TRUE;
889 appData.telnetProgram = appData.icsHelper;
892 appData.zippyTalk = appData.zippyPlay = FALSE;
895 /* [AS] Initialize pv info list [HGM] and game state */
899 for( i=0; i<=framePtr; i++ ) {
900 pvInfoList[i].depth = -1;
901 boards[i][EP_STATUS] = EP_NONE;
902 for( j=0; j<BOARD_FILES-2; j++ ) boards[i][CASTLING][j] = NoRights;
907 * Parse timeControl resource
909 if (!ParseTimeControl(appData.timeControl, appData.timeIncrement,
910 appData.movesPerSession)) {
912 snprintf(buf, sizeof(buf), _("bad timeControl option %s"), appData.timeControl);
913 DisplayFatalError(buf, 0, 2);
917 * Parse searchTime resource
919 if (*appData.searchTime != NULLCHAR) {
920 matched = sscanf(appData.searchTime, "%d:%d", &min, &sec);
922 searchTime = min * 60;
923 } else if (matched == 2) {
924 searchTime = min * 60 + sec;
927 snprintf(buf, sizeof(buf), _("bad searchTime option %s"), appData.searchTime);
928 DisplayFatalError(buf, 0, 2);
932 /* [AS] Adjudication threshold */
933 adjudicateLossThreshold = appData.adjudicateLossThreshold;
935 InitEngine(&first, 0);
936 InitEngine(&second, 1);
939 if (appData.icsActive) {
940 appData.clockMode = TRUE; /* changes dynamically in ICS mode */
941 } else if (appData.noChessProgram) { // [HGM] st: searchTime mode now also is clockMode
942 appData.clockMode = FALSE;
943 first.sendTime = second.sendTime = 0;
947 /* Override some settings from environment variables, for backward
948 compatibility. Unfortunately it's not feasible to have the env
949 vars just set defaults, at least in xboard. Ugh.
951 if (appData.icsActive && (appData.zippyPlay || appData.zippyTalk)) {
956 if (!appData.icsActive) {
960 /* Check for variants that are supported only in ICS mode,
961 or not at all. Some that are accepted here nevertheless
962 have bugs; see comments below.
964 VariantClass variant = StringToVariant(appData.variant);
966 case VariantBughouse: /* need four players and two boards */
967 case VariantKriegspiel: /* need to hide pieces and move details */
968 /* case VariantFischeRandom: (Fabien: moved below) */
969 len = snprintf(buf,MSG_SIZ, _("Variant %s supported only in ICS mode"), appData.variant);
970 if( (len > MSG_SIZ) && appData.debugMode )
971 fprintf(debugFP, "InitBackEnd1: buffer truncated.\n");
973 DisplayFatalError(buf, 0, 2);
977 case VariantLoadable:
987 len = snprintf(buf, MSG_SIZ, _("Unknown variant name %s"), appData.variant);
988 if( (len > MSG_SIZ) && appData.debugMode )
989 fprintf(debugFP, "InitBackEnd1: buffer truncated.\n");
991 DisplayFatalError(buf, 0, 2);
994 case VariantXiangqi: /* [HGM] repetition rules not implemented */
995 case VariantFairy: /* [HGM] TestLegality definitely off! */
996 case VariantGothic: /* [HGM] should work */
997 case VariantCapablanca: /* [HGM] should work */
998 case VariantCourier: /* [HGM] initial forced moves not implemented */
999 case VariantShogi: /* [HGM] could still mate with pawn drop */
1000 case VariantKnightmate: /* [HGM] should work */
1001 case VariantCylinder: /* [HGM] untested */
1002 case VariantFalcon: /* [HGM] untested */
1003 case VariantCrazyhouse: /* holdings not shown, ([HGM] fixed that!)
1004 offboard interposition not understood */
1005 case VariantNormal: /* definitely works! */
1006 case VariantWildCastle: /* pieces not automatically shuffled */
1007 case VariantNoCastle: /* pieces not automatically shuffled */
1008 case VariantFischeRandom: /* [HGM] works and shuffles pieces */
1009 case VariantLosers: /* should work except for win condition,
1010 and doesn't know captures are mandatory */
1011 case VariantSuicide: /* should work except for win condition,
1012 and doesn't know captures are mandatory */
1013 case VariantGiveaway: /* should work except for win condition,
1014 and doesn't know captures are mandatory */
1015 case VariantTwoKings: /* should work */
1016 case VariantAtomic: /* should work except for win condition */
1017 case Variant3Check: /* should work except for win condition */
1018 case VariantShatranj: /* should work except for all win conditions */
1019 case VariantMakruk: /* should work except for daw countdown */
1020 case VariantBerolina: /* might work if TestLegality is off */
1021 case VariantCapaRandom: /* should work */
1022 case VariantJanus: /* should work */
1023 case VariantSuper: /* experimental */
1024 case VariantGreat: /* experimental, requires legality testing to be off */
1025 case VariantSChess: /* S-Chess, should work */
1026 case VariantSpartan: /* should work */
1033 int NextIntegerFromString( char ** str, long * value )
1038 while( *s == ' ' || *s == '\t' ) {
1044 if( *s >= '0' && *s <= '9' ) {
1045 while( *s >= '0' && *s <= '9' ) {
1046 *value = *value * 10 + (*s - '0');
1058 int NextTimeControlFromString( char ** str, long * value )
1061 int result = NextIntegerFromString( str, &temp );
1064 *value = temp * 60; /* Minutes */
1065 if( **str == ':' ) {
1067 result = NextIntegerFromString( str, &temp );
1068 *value += temp; /* Seconds */
1075 int NextSessionFromString( char ** str, int *moves, long * tc, long *inc, int *incType)
1076 { /* [HGM] routine added to read '+moves/time' for secondary time control. */
1077 int result = -1, type = 0; long temp, temp2;
1079 if(**str != ':') return -1; // old params remain in force!
1081 if(**str == '*') type = *(*str)++, temp = 0; // sandclock TC
1082 if( NextIntegerFromString( str, &temp ) ) return -1;
1083 if(type) { *moves = 0; *tc = temp * 500; *inc = temp * 1000; *incType = '*'; return 0; }
1086 /* time only: incremental or sudden-death time control */
1087 if(**str == '+') { /* increment follows; read it */
1089 if(**str == '!') type = *(*str)++; // Bronstein TC
1090 if(result = NextIntegerFromString( str, &temp2)) return -1;
1091 *inc = temp2 * 1000;
1092 if(**str == '.') { // read fraction of increment
1093 char *start = ++(*str);
1094 if(result = NextIntegerFromString( str, &temp2)) return -1;
1096 while(start++ < *str) temp2 /= 10;
1100 *moves = 0; *tc = temp * 1000; *incType = type;
1104 (*str)++; /* classical time control */
1105 result = NextIntegerFromString( str, &temp2); // NOTE: already converted to seconds by ParseTimeControl()
1116 int GetTimeQuota(int movenr, int lastUsed, char *tcString)
1117 { /* [HGM] get time to add from the multi-session time-control string */
1118 int incType, moves=1; /* kludge to force reading of first session */
1119 long time, increment;
1122 if(!*s) return 0; // empty TC string means we ran out of the last sudden-death version
1123 if(appData.debugMode) fprintf(debugFP, "TC string = '%s'\n", tcString);
1125 if(moves) NextSessionFromString(&s, &moves, &time, &increment, &incType);
1126 nextSession = s; suddenDeath = moves == 0 && increment == 0;
1127 if(appData.debugMode) fprintf(debugFP, "mps=%d tc=%d inc=%d\n", moves, (int) time, (int) increment);
1128 if(movenr == -1) return time; /* last move before new session */
1129 if(incType == '*') increment = 0; else // for sandclock, time is added while not thinking
1130 if(incType == '!' && lastUsed < increment) increment = lastUsed;
1131 if(!moves) return increment; /* current session is incremental */
1132 if(movenr >= 0) movenr -= moves; /* we already finished this session */
1133 } while(movenr >= -1); /* try again for next session */
1135 return 0; // no new time quota on this move
1139 ParseTimeControl(tc, ti, mps)
1146 char buf[MSG_SIZ], buf2[MSG_SIZ], *mytc = tc;
1149 if(ti >= 0 && !strchr(tc, '+') && !strchr(tc, '/') ) mps = 0;
1150 if(!strchr(tc, '+') && !strchr(tc, '/') && sscanf(tc, "%d:%d", &min, &sec) >= 1)
1151 sprintf(mytc=buf2, "%d", 60*min+sec); // convert 'classical' min:sec tc string to seconds
1155 snprintf(buf, MSG_SIZ, ":%d/%s+%g", mps, mytc, ti);
1157 snprintf(buf, MSG_SIZ, ":%s+%g", mytc, ti);
1160 snprintf(buf, MSG_SIZ, ":%d/%s", mps, mytc);
1162 snprintf(buf, MSG_SIZ, ":%s", mytc);
1164 fullTimeControlString = StrSave(buf); // this should now be in PGN format
1166 if( NextTimeControlFromString( &tc, &tc1 ) != 0 ) {
1171 /* Parse second time control */
1174 if( NextTimeControlFromString( &tc, &tc2 ) != 0 ) {
1182 timeControl_2 = tc2 * 1000;
1192 timeControl = tc1 * 1000;
1195 timeIncrement = ti * 1000; /* convert to ms */
1196 movesPerSession = 0;
1199 movesPerSession = mps;
1207 if (appData.debugMode) {
1208 fprintf(debugFP, "%s\n", programVersion);
1211 set_cont_sequence(appData.wrapContSeq);
1212 if (appData.matchGames > 0) {
1213 appData.matchMode = TRUE;
1214 } else if (appData.matchMode) {
1215 appData.matchGames = 1;
1217 if(appData.matchMode && appData.sameColorGames > 0) /* [HGM] alternate: overrule matchGames */
1218 appData.matchGames = appData.sameColorGames;
1219 if(appData.rewindIndex > 1) { /* [HGM] autoinc: rewind implies auto-increment and overrules given index */
1220 if(appData.loadPositionIndex >= 0) appData.loadPositionIndex = -1;
1221 if(appData.loadGameIndex >= 0) appData.loadGameIndex = -1;
1224 if (appData.noChessProgram || first.protocolVersion == 1) {
1227 /* kludge: allow timeout for initial "feature" commands */
1229 DisplayMessage("", _("Starting chess program"));
1230 ScheduleDelayedEvent(InitBackEnd3, FEATURE_TIMEOUT);
1235 CalculateIndex(int index, int gameNr)
1236 { // [HGM] autoinc: absolute way to determine load index from game number (taking auto-inc and rewind into account)
1238 if(index > 0) return index; // fixed nmber
1239 if(index == 0) return 1;
1240 res = (index == -1 ? gameNr : (gameNr-1)/2 + 1); // autoinc
1241 if(appData.rewindIndex > 0) res = (res-1) % appData.rewindIndex + 1; // rewind
1246 LoadGameOrPosition(int gameNr)
1247 { // [HGM] taken out of MatchEvent and NextMatchGame (to combine it)
1248 if (*appData.loadGameFile != NULLCHAR) {
1249 if (!LoadGameFromFile(appData.loadGameFile,
1250 CalculateIndex(appData.loadGameIndex, gameNr),
1251 appData.loadGameFile, FALSE)) {
1252 DisplayFatalError(_("Bad game file"), 0, 1);
1255 } else if (*appData.loadPositionFile != NULLCHAR) {
1256 if (!LoadPositionFromFile(appData.loadPositionFile,
1257 CalculateIndex(appData.loadPositionIndex, gameNr),
1258 appData.loadPositionFile)) {
1259 DisplayFatalError(_("Bad position file"), 0, 1);
1267 MatchEvent(int mode)
1268 { // [HGM] moved out of InitBackend3, to make it callable when match starts through menu
1269 /* Set up machine vs. machine match */
1270 if (appData.noChessProgram) {
1271 DisplayFatalError(_("Can't have a match with no chess programs"),
1277 if(!LoadGameOrPositionFile(1)) return;
1278 first.matchWins = second.matchWins = 0; // [HGM] match: needed in later matches
\r
1283 InitBackEnd3 P((void))
1285 GameMode initialMode;
1289 InitChessProgram(&first, startedFromSetupPosition);
1291 if(!appData.noChessProgram) { /* [HGM] tidy: redo program version to use name from myname feature */
1292 free(programVersion);
1293 programVersion = (char*) malloc(8 + strlen(PACKAGE_STRING) + strlen(first.tidy));
1294 sprintf(programVersion, "%s + %s", PACKAGE_STRING, first.tidy);
1297 if (appData.icsActive) {
1299 /* [DM] Make a console window if needed [HGM] merged ifs */
1305 if (*appData.icsCommPort != NULLCHAR)
1306 len = snprintf(buf, MSG_SIZ, _("Could not open comm port %s"),
1307 appData.icsCommPort);
1309 len = snprintf(buf, MSG_SIZ, _("Could not connect to host %s, port %s"),
1310 appData.icsHost, appData.icsPort);
1312 if( (len > MSG_SIZ) && appData.debugMode )
1313 fprintf(debugFP, "InitBackEnd3: buffer truncated.\n");
1315 DisplayFatalError(buf, err, 1);
1320 AddInputSource(icsPR, FALSE, read_from_ics, &telnetISR);
1322 AddInputSource(NoProc, FALSE, read_from_player, &fromUserISR);
1323 if(appData.keepAlive) // [HGM] alive: schedule sending of dummy 'date' command
1324 ScheduleDelayedEvent(KeepAlive, appData.keepAlive*60*1000);
1325 } else if (appData.noChessProgram) {
1331 if (*appData.cmailGameName != NULLCHAR) {
1333 OpenLoopback(&cmailPR);
1335 AddInputSource(cmailPR, FALSE, CmailSigHandlerCallBack, &cmailISR);
1339 DisplayMessage("", "");
1340 if (StrCaseCmp(appData.initialMode, "") == 0) {
1341 initialMode = BeginningOfGame;
1342 if(!appData.icsActive && appData.noChessProgram) { // [HGM] could be fall-back
1343 gameMode = MachinePlaysBlack; // "Machine Black" might have been implicitly highlighted
1344 ModeHighlight(); // make sure XBoard knows it is highlighted, so it will un-highlight it
1345 gameMode = BeginningOfGame; // in case BeginningOfGame now means "Edit Position"
1348 } else if (StrCaseCmp(appData.initialMode, "TwoMachines") == 0) {
1349 initialMode = TwoMachinesPlay;
1350 } else if (StrCaseCmp(appData.initialMode, "AnalyzeFile") == 0) {
1351 initialMode = AnalyzeFile;
1352 } else if (StrCaseCmp(appData.initialMode, "Analysis") == 0) {
1353 initialMode = AnalyzeMode;
1354 } else if (StrCaseCmp(appData.initialMode, "MachineWhite") == 0) {
1355 initialMode = MachinePlaysWhite;
1356 } else if (StrCaseCmp(appData.initialMode, "MachineBlack") == 0) {
1357 initialMode = MachinePlaysBlack;
1358 } else if (StrCaseCmp(appData.initialMode, "EditGame") == 0) {
1359 initialMode = EditGame;
1360 } else if (StrCaseCmp(appData.initialMode, "EditPosition") == 0) {
1361 initialMode = EditPosition;
1362 } else if (StrCaseCmp(appData.initialMode, "Training") == 0) {
1363 initialMode = Training;
1365 len = snprintf(buf, MSG_SIZ, _("Unknown initialMode %s"), appData.initialMode);
1366 if( (len > MSG_SIZ) && appData.debugMode )
1367 fprintf(debugFP, "InitBackEnd3: buffer truncated.\n");
1369 DisplayFatalError(buf, 0, 2);
1373 if (appData.matchMode) {
1375 } else if (*appData.cmailGameName != NULLCHAR) {
1376 /* Set up cmail mode */
1377 ReloadCmailMsgEvent(TRUE);
1379 /* Set up other modes */
1380 if (initialMode == AnalyzeFile) {
1381 if (*appData.loadGameFile == NULLCHAR) {
1382 DisplayFatalError(_("AnalyzeFile mode requires a game file"), 0, 1);
1386 if (*appData.loadGameFile != NULLCHAR) {
1387 (void) LoadGameFromFile(appData.loadGameFile,
1388 appData.loadGameIndex,
1389 appData.loadGameFile, TRUE);
1390 } else if (*appData.loadPositionFile != NULLCHAR) {
1391 (void) LoadPositionFromFile(appData.loadPositionFile,
1392 appData.loadPositionIndex,
1393 appData.loadPositionFile);
1394 /* [HGM] try to make self-starting even after FEN load */
1395 /* to allow automatic setup of fairy variants with wtm */
1396 if(initialMode == BeginningOfGame && !blackPlaysFirst) {
1397 gameMode = BeginningOfGame;
1398 setboardSpoiledMachineBlack = 1;
1400 /* [HGM] loadPos: make that every new game uses the setup */
1401 /* from file as long as we do not switch variant */
1402 if(!blackPlaysFirst) {
1403 startedFromPositionFile = TRUE;
1404 CopyBoard(filePosition, boards[0]);
1407 if (initialMode == AnalyzeMode) {
1408 if (appData.noChessProgram) {
1409 DisplayFatalError(_("Analysis mode requires a chess engine"), 0, 2);
1412 if (appData.icsActive) {
1413 DisplayFatalError(_("Analysis mode does not work with ICS mode"),0,2);
1417 } else if (initialMode == AnalyzeFile) {
1418 appData.showThinking = TRUE; // [HGM] thinking: moved out of ShowThinkingEvent
1419 ShowThinkingEvent();
1421 AnalysisPeriodicEvent(1);
1422 } else if (initialMode == MachinePlaysWhite) {
1423 if (appData.noChessProgram) {
1424 DisplayFatalError(_("MachineWhite mode requires a chess engine"),
1428 if (appData.icsActive) {
1429 DisplayFatalError(_("MachineWhite mode does not work with ICS mode"),
1433 MachineWhiteEvent();
1434 } else if (initialMode == MachinePlaysBlack) {
1435 if (appData.noChessProgram) {
1436 DisplayFatalError(_("MachineBlack mode requires a chess engine"),
1440 if (appData.icsActive) {
1441 DisplayFatalError(_("MachineBlack mode does not work with ICS mode"),
1445 MachineBlackEvent();
1446 } else if (initialMode == TwoMachinesPlay) {
1447 if (appData.noChessProgram) {
1448 DisplayFatalError(_("TwoMachines mode requires a chess engine"),
1452 if (appData.icsActive) {
1453 DisplayFatalError(_("TwoMachines mode does not work with ICS mode"),
1458 } else if (initialMode == EditGame) {
1460 } else if (initialMode == EditPosition) {
1461 EditPositionEvent();
1462 } else if (initialMode == Training) {
1463 if (*appData.loadGameFile == NULLCHAR) {
1464 DisplayFatalError(_("Training mode requires a game file"), 0, 2);
1473 * Establish will establish a contact to a remote host.port.
1474 * Sets icsPR to a ProcRef for a process (or pseudo-process)
1475 * used to talk to the host.
1476 * Returns 0 if okay, error code if not.
1483 if (*appData.icsCommPort != NULLCHAR) {
1484 /* Talk to the host through a serial comm port */
1485 return OpenCommPort(appData.icsCommPort, &icsPR);
1487 } else if (*appData.gateway != NULLCHAR) {
1488 if (*appData.remoteShell == NULLCHAR) {
1489 /* Use the rcmd protocol to run telnet program on a gateway host */
1490 snprintf(buf, sizeof(buf), "%s %s %s",
1491 appData.telnetProgram, appData.icsHost, appData.icsPort);
1492 return OpenRcmd(appData.gateway, appData.remoteUser, buf, &icsPR);
1495 /* Use the rsh program to run telnet program on a gateway host */
1496 if (*appData.remoteUser == NULLCHAR) {
1497 snprintf(buf, sizeof(buf), "%s %s %s %s %s", appData.remoteShell,
1498 appData.gateway, appData.telnetProgram,
1499 appData.icsHost, appData.icsPort);
1501 snprintf(buf, sizeof(buf), "%s %s -l %s %s %s %s",
1502 appData.remoteShell, appData.gateway,
1503 appData.remoteUser, appData.telnetProgram,
1504 appData.icsHost, appData.icsPort);
1506 return StartChildProcess(buf, "", &icsPR);
1509 } else if (appData.useTelnet) {
1510 return OpenTelnet(appData.icsHost, appData.icsPort, &icsPR);
1513 /* TCP socket interface differs somewhat between
1514 Unix and NT; handle details in the front end.
1516 return OpenTCP(appData.icsHost, appData.icsPort, &icsPR);
1520 void EscapeExpand(char *p, char *q)
1521 { // [HGM] initstring: routine to shape up string arguments
1522 while(*p++ = *q++) if(p[-1] == '\\')
1524 case 'n': p[-1] = '\n'; break;
1525 case 'r': p[-1] = '\r'; break;
1526 case 't': p[-1] = '\t'; break;
1527 case '\\': p[-1] = '\\'; break;
1528 case 0: *p = 0; return;
1529 default: p[-1] = q[-1]; break;
1534 show_bytes(fp, buf, count)
1540 if (*buf < 040 || *(unsigned char *) buf > 0177) {
1541 fprintf(fp, "\\%03o", *buf & 0xff);
1550 /* Returns an errno value */
1552 OutputMaybeTelnet(pr, message, count, outError)
1558 char buf[8192], *p, *q, *buflim;
1559 int left, newcount, outcount;
1561 if (*appData.icsCommPort != NULLCHAR || appData.useTelnet ||
1562 *appData.gateway != NULLCHAR) {
1563 if (appData.debugMode) {
1564 fprintf(debugFP, ">ICS: ");
1565 show_bytes(debugFP, message, count);
1566 fprintf(debugFP, "\n");
1568 return OutputToProcess(pr, message, count, outError);
1571 buflim = &buf[sizeof(buf)-1]; /* allow 1 byte for expanding last char */
1578 if (appData.debugMode) {
1579 fprintf(debugFP, ">ICS: ");
1580 show_bytes(debugFP, buf, newcount);
1581 fprintf(debugFP, "\n");
1583 outcount = OutputToProcess(pr, buf, newcount, outError);
1584 if (outcount < newcount) return -1; /* to be sure */
1591 } else if (((unsigned char) *p) == TN_IAC) {
1592 *q++ = (char) TN_IAC;
1599 if (appData.debugMode) {
1600 fprintf(debugFP, ">ICS: ");
1601 show_bytes(debugFP, buf, newcount);
1602 fprintf(debugFP, "\n");
1604 outcount = OutputToProcess(pr, buf, newcount, outError);
1605 if (outcount < newcount) return -1; /* to be sure */
1610 read_from_player(isr, closure, message, count, error)
1617 int outError, outCount;
1618 static int gotEof = 0;
1620 /* Pass data read from player on to ICS */
1623 outCount = OutputMaybeTelnet(icsPR, message, count, &outError);
1624 if (outCount < count) {
1625 DisplayFatalError(_("Error writing to ICS"), outError, 1);
1627 } else if (count < 0) {
1628 RemoveInputSource(isr);
1629 DisplayFatalError(_("Error reading from keyboard"), error, 1);
1630 } else if (gotEof++ > 0) {
1631 RemoveInputSource(isr);
1632 DisplayFatalError(_("Got end of file from keyboard"), 0, 0);
1638 { // [HGM] alive: periodically send dummy (date) command to ICS to prevent time-out
1639 if(!connectionAlive) DisplayFatalError("No response from ICS", 0, 1);
1640 connectionAlive = FALSE; // only sticks if no response to 'date' command.
1641 SendToICS("date\n");
1642 if(appData.keepAlive) ScheduleDelayedEvent(KeepAlive, appData.keepAlive*60*1000);
1645 /* added routine for printf style output to ics */
1646 void ics_printf(char *format, ...)
1648 char buffer[MSG_SIZ];
1651 va_start(args, format);
1652 vsnprintf(buffer, sizeof(buffer), format, args);
1653 buffer[sizeof(buffer)-1] = '\0';
1662 int count, outCount, outError;
1664 if (icsPR == NULL) return;
1667 outCount = OutputMaybeTelnet(icsPR, s, count, &outError);
1668 if (outCount < count) {
1669 DisplayFatalError(_("Error writing to ICS"), outError, 1);
1673 /* This is used for sending logon scripts to the ICS. Sending
1674 without a delay causes problems when using timestamp on ICC
1675 (at least on my machine). */
1677 SendToICSDelayed(s,msdelay)
1681 int count, outCount, outError;
1683 if (icsPR == NULL) return;
1686 if (appData.debugMode) {
1687 fprintf(debugFP, ">ICS: ");
1688 show_bytes(debugFP, s, count);
1689 fprintf(debugFP, "\n");
1691 outCount = OutputToProcessDelayed(icsPR, s, count, &outError,
1693 if (outCount < count) {
1694 DisplayFatalError(_("Error writing to ICS"), outError, 1);
1699 /* Remove all highlighting escape sequences in s
1700 Also deletes any suffix starting with '('
1703 StripHighlightAndTitle(s)
1706 static char retbuf[MSG_SIZ];
1709 while (*s != NULLCHAR) {
1710 while (*s == '\033') {
1711 while (*s != NULLCHAR && !isalpha(*s)) s++;
1712 if (*s != NULLCHAR) s++;
1714 while (*s != NULLCHAR && *s != '\033') {
1715 if (*s == '(' || *s == '[') {
1726 /* Remove all highlighting escape sequences in s */
1731 static char retbuf[MSG_SIZ];
1734 while (*s != NULLCHAR) {
1735 while (*s == '\033') {
1736 while (*s != NULLCHAR && !isalpha(*s)) s++;
1737 if (*s != NULLCHAR) s++;
1739 while (*s != NULLCHAR && *s != '\033') {
1747 char *variantNames[] = VARIANT_NAMES;
1752 return variantNames[v];
1756 /* Identify a variant from the strings the chess servers use or the
1757 PGN Variant tag names we use. */
1764 VariantClass v = VariantNormal;
1765 int i, found = FALSE;
1771 /* [HGM] skip over optional board-size prefixes */
1772 if( sscanf(e, "%dx%d_", &i, &i) == 2 ||
1773 sscanf(e, "%dx%d+%d_", &i, &i, &i) == 3 ) {
1774 while( *e++ != '_');
1777 if(StrCaseStr(e, "misc/")) { // [HGM] on FICS, misc/shogi is not shogi
1781 for (i=0; i<sizeof(variantNames)/sizeof(char*); i++) {
1782 if (StrCaseStr(e, variantNames[i])) {
1783 v = (VariantClass) i;
1790 if ((StrCaseStr(e, "fischer") && StrCaseStr(e, "random"))
1791 || StrCaseStr(e, "wild/fr")
1792 || StrCaseStr(e, "frc") || StrCaseStr(e, "960")) {
1793 v = VariantFischeRandom;
1794 } else if ((i = 4, p = StrCaseStr(e, "wild")) ||
1795 (i = 1, p = StrCaseStr(e, "w"))) {
1797 while (*p && (isspace(*p) || *p == '(' || *p == '/')) p++;
1804 case 0: /* FICS only, actually */
1806 /* Castling legal even if K starts on d-file */
1807 v = VariantWildCastle;
1812 /* Castling illegal even if K & R happen to start in
1813 normal positions. */
1814 v = VariantNoCastle;
1827 /* Castling legal iff K & R start in normal positions */
1833 /* Special wilds for position setup; unclear what to do here */
1834 v = VariantLoadable;
1837 /* Bizarre ICC game */
1838 v = VariantTwoKings;
1841 v = VariantKriegspiel;
1847 v = VariantFischeRandom;
1850 v = VariantCrazyhouse;
1853 v = VariantBughouse;
1859 /* Not quite the same as FICS suicide! */
1860 v = VariantGiveaway;
1866 v = VariantShatranj;
1869 /* Temporary names for future ICC types. The name *will* change in
1870 the next xboard/WinBoard release after ICC defines it. */
1908 v = VariantCapablanca;
1911 v = VariantKnightmate;
1917 v = VariantCylinder;
1923 v = VariantCapaRandom;
1926 v = VariantBerolina;
1938 /* Found "wild" or "w" in the string but no number;
1939 must assume it's normal chess. */
1943 len = snprintf(buf, MSG_SIZ, _("Unknown wild type %d"), wnum);
1944 if( (len > MSG_SIZ) && appData.debugMode )
1945 fprintf(debugFP, "StringToVariant: buffer truncated.\n");
1947 DisplayError(buf, 0);
1953 if (appData.debugMode) {
1954 fprintf(debugFP, _("recognized '%s' (%d) as variant %s\n"),
1955 e, wnum, VariantName(v));
1960 static int leftover_start = 0, leftover_len = 0;
1961 char star_match[STAR_MATCH_N][MSG_SIZ];
1963 /* Test whether pattern is present at &buf[*index]; if so, return TRUE,
1964 advance *index beyond it, and set leftover_start to the new value of
1965 *index; else return FALSE. If pattern contains the character '*', it
1966 matches any sequence of characters not containing '\r', '\n', or the
1967 character following the '*' (if any), and the matched sequence(s) are
1968 copied into star_match.
1971 looking_at(buf, index, pattern)
1976 char *bufp = &buf[*index], *patternp = pattern;
1978 char *matchp = star_match[0];
1981 if (*patternp == NULLCHAR) {
1982 *index = leftover_start = bufp - buf;
1986 if (*bufp == NULLCHAR) return FALSE;
1987 if (*patternp == '*') {
1988 if (*bufp == *(patternp + 1)) {
1990 matchp = star_match[++star_count];
1994 } else if (*bufp == '\n' || *bufp == '\r') {
1996 if (*patternp == NULLCHAR)
2001 *matchp++ = *bufp++;
2005 if (*patternp != *bufp) return FALSE;
2012 SendToPlayer(data, length)
2016 int error, outCount;
2017 outCount = OutputToProcess(NoProc, data, length, &error);
2018 if (outCount < length) {
2019 DisplayFatalError(_("Error writing to display"), error, 1);
2024 PackHolding(packed, holding)
2036 switch (runlength) {
2047 sprintf(q, "%d", runlength);
2059 /* Telnet protocol requests from the front end */
2061 TelnetRequest(ddww, option)
2062 unsigned char ddww, option;
2064 unsigned char msg[3];
2065 int outCount, outError;
2067 if (*appData.icsCommPort != NULLCHAR || appData.useTelnet) return;
2069 if (appData.debugMode) {
2070 char buf1[8], buf2[8], *ddwwStr, *optionStr;
2086 snprintf(buf1,sizeof(buf1)/sizeof(buf1[0]), "%d", ddww);
2095 snprintf(buf2,sizeof(buf2)/sizeof(buf2[0]), "%d", option);
2098 fprintf(debugFP, ">%s %s ", ddwwStr, optionStr);
2103 outCount = OutputToProcess(icsPR, (char *)msg, 3, &outError);
2105 DisplayFatalError(_("Error writing to ICS"), outError, 1);
2112 if (!appData.icsActive) return;
2113 TelnetRequest(TN_DO, TN_ECHO);
2119 if (!appData.icsActive) return;
2120 TelnetRequest(TN_DONT, TN_ECHO);
2124 CopyHoldings(Board board, char *holdings, ChessSquare lowestPiece)
2126 /* put the holdings sent to us by the server on the board holdings area */
2127 int i, j, holdingsColumn, holdingsStartRow, direction, countsColumn;
2131 if(gameInfo.holdingsWidth < 2) return;
2132 if(gameInfo.variant != VariantBughouse && board[HOLDINGS_SET])
2133 return; // prevent overwriting by pre-board holdings
2135 if( (int)lowestPiece >= BlackPawn ) {
2138 holdingsStartRow = BOARD_HEIGHT-1;
2141 holdingsColumn = BOARD_WIDTH-1;
2142 countsColumn = BOARD_WIDTH-2;
2143 holdingsStartRow = 0;
2147 for(i=0; i<BOARD_HEIGHT; i++) { /* clear holdings */
2148 board[i][holdingsColumn] = EmptySquare;
2149 board[i][countsColumn] = (ChessSquare) 0;
2151 while( (p=*holdings++) != NULLCHAR ) {
2152 piece = CharToPiece( ToUpper(p) );
2153 if(piece == EmptySquare) continue;
2154 /*j = (int) piece - (int) WhitePawn;*/
2155 j = PieceToNumber(piece);
2156 if(j >= gameInfo.holdingsSize) continue; /* ignore pieces that do not fit */
2157 if(j < 0) continue; /* should not happen */
2158 piece = (ChessSquare) ( (int)piece + (int)lowestPiece );
2159 board[holdingsStartRow+j*direction][holdingsColumn] = piece;
2160 board[holdingsStartRow+j*direction][countsColumn]++;
2166 VariantSwitch(Board board, VariantClass newVariant)
2168 int newHoldingsWidth, newWidth = 8, newHeight = 8, i, j;
2169 static Board oldBoard;
2171 startedFromPositionFile = FALSE;
2172 if(gameInfo.variant == newVariant) return;
2174 /* [HGM] This routine is called each time an assignment is made to
2175 * gameInfo.variant during a game, to make sure the board sizes
2176 * are set to match the new variant. If that means adding or deleting
2177 * holdings, we shift the playing board accordingly
2178 * This kludge is needed because in ICS observe mode, we get boards
2179 * of an ongoing game without knowing the variant, and learn about the
2180 * latter only later. This can be because of the move list we requested,
2181 * in which case the game history is refilled from the beginning anyway,
2182 * but also when receiving holdings of a crazyhouse game. In the latter
2183 * case we want to add those holdings to the already received position.
2187 if (appData.debugMode) {
2188 fprintf(debugFP, "Switch board from %s to %s\n",
2189 VariantName(gameInfo.variant), VariantName(newVariant));
2190 setbuf(debugFP, NULL);
2192 shuffleOpenings = 0; /* [HGM] shuffle */
2193 gameInfo.holdingsSize = 5; /* [HGM] prepare holdings */
2197 newWidth = 9; newHeight = 9;
2198 gameInfo.holdingsSize = 7;
2199 case VariantBughouse:
2200 case VariantCrazyhouse:
2201 newHoldingsWidth = 2; break;
2205 newHoldingsWidth = 2;
2206 gameInfo.holdingsSize = 8;
2209 case VariantCapablanca:
2210 case VariantCapaRandom:
2213 newHoldingsWidth = gameInfo.holdingsSize = 0;
2216 if(newWidth != gameInfo.boardWidth ||
2217 newHeight != gameInfo.boardHeight ||
2218 newHoldingsWidth != gameInfo.holdingsWidth ) {
2220 /* shift position to new playing area, if needed */
2221 if(newHoldingsWidth > gameInfo.holdingsWidth) {
2222 for(i=0; i<BOARD_HEIGHT; i++)
2223 for(j=BOARD_RGHT-1; j>=BOARD_LEFT; j--)
2224 board[i][j+newHoldingsWidth-gameInfo.holdingsWidth] =
2226 for(i=0; i<newHeight; i++) {
2227 board[i][0] = board[i][newWidth+2*newHoldingsWidth-1] = EmptySquare;
2228 board[i][1] = board[i][newWidth+2*newHoldingsWidth-2] = (ChessSquare) 0;
2230 } else if(newHoldingsWidth < gameInfo.holdingsWidth) {
2231 for(i=0; i<BOARD_HEIGHT; i++)
2232 for(j=BOARD_LEFT; j<BOARD_RGHT; j++)
2233 board[i][j+newHoldingsWidth-gameInfo.holdingsWidth] =
2236 gameInfo.boardWidth = newWidth;
2237 gameInfo.boardHeight = newHeight;
2238 gameInfo.holdingsWidth = newHoldingsWidth;
2239 gameInfo.variant = newVariant;
2240 InitDrawingSizes(-2, 0);
2241 } else gameInfo.variant = newVariant;
2242 CopyBoard(oldBoard, board); // remember correctly formatted board
2243 InitPosition(FALSE); /* this sets up board[0], but also other stuff */
2244 DrawPosition(TRUE, currentMove ? boards[currentMove] : oldBoard);
2247 static int loggedOn = FALSE;
2249 /*-- Game start info cache: --*/
2251 char gs_kind[MSG_SIZ];
2252 static char player1Name[128] = "";
2253 static char player2Name[128] = "";
2254 static char cont_seq[] = "\n\\ ";
2255 static int player1Rating = -1;
2256 static int player2Rating = -1;
2257 /*----------------------------*/
2259 ColorClass curColor = ColorNormal;
2260 int suppressKibitz = 0;
2263 Boolean soughtPending = FALSE;
2264 Boolean seekGraphUp;
2265 #define MAX_SEEK_ADS 200
2267 char *seekAdList[MAX_SEEK_ADS];
2268 int ratingList[MAX_SEEK_ADS], xList[MAX_SEEK_ADS], yList[MAX_SEEK_ADS], seekNrList[MAX_SEEK_ADS], zList[MAX_SEEK_ADS];
2269 float tcList[MAX_SEEK_ADS];
2270 char colorList[MAX_SEEK_ADS];
2271 int nrOfSeekAds = 0;
2272 int minRating = 1010, maxRating = 2800;
2273 int hMargin = 10, vMargin = 20, h, w;
2274 extern int squareSize, lineGap;
2279 int x, y, color = 0, r = ratingList[i]; float tc = tcList[i];
2280 xList[i] = yList[i] = -100; // outside graph, so cannot be clicked
2281 if(r < minRating+100 && r >=0 ) r = minRating+100;
2282 if(r > maxRating) r = maxRating;
2283 if(tc < 1.) tc = 1.;
2284 if(tc > 95.) tc = 95.;
2285 x = (w-hMargin-squareSize/8-7)* log(tc)/log(95.) + hMargin;
2286 y = ((double)r - minRating)/(maxRating - minRating)
2287 * (h-vMargin-squareSize/8-1) + vMargin;
2288 if(ratingList[i] < 0) y = vMargin + squareSize/4;
2289 if(strstr(seekAdList[i], " u ")) color = 1;
2290 if(!strstr(seekAdList[i], "lightning") && // for now all wilds same color
2291 !strstr(seekAdList[i], "bullet") &&
2292 !strstr(seekAdList[i], "blitz") &&
2293 !strstr(seekAdList[i], "standard") ) color = 2;
2294 if(strstr(seekAdList[i], "(C) ")) color |= SQUARE; // plot computer seeks as squares
2295 DrawSeekDot(xList[i]=x+3*(color&~SQUARE), yList[i]=h-1-y, colorList[i]=color);
2299 AddAd(char *handle, char *rating, int base, int inc, char rated, char *type, int nr, Boolean plot)
2301 char buf[MSG_SIZ], *ext = "";
2302 VariantClass v = StringToVariant(type);
2303 if(strstr(type, "wild")) {
2304 ext = type + 4; // append wild number
2305 if(v == VariantFischeRandom) type = "chess960"; else
2306 if(v == VariantLoadable) type = "setup"; else
2307 type = VariantName(v);
2309 snprintf(buf, MSG_SIZ, "%s (%s) %d %d %c %s%s", handle, rating, base, inc, rated, type, ext);
2310 if(nrOfSeekAds < MAX_SEEK_ADS-1) {
2311 if(seekAdList[nrOfSeekAds]) free(seekAdList[nrOfSeekAds]);
2312 ratingList[nrOfSeekAds] = -1; // for if seeker has no rating
2313 sscanf(rating, "%d", &ratingList[nrOfSeekAds]);
2314 tcList[nrOfSeekAds] = base + (2./3.)*inc;
2315 seekNrList[nrOfSeekAds] = nr;
2316 zList[nrOfSeekAds] = 0;
2317 seekAdList[nrOfSeekAds++] = StrSave(buf);
2318 if(plot) PlotSeekAd(nrOfSeekAds-1);
2325 int x = xList[i], y = yList[i], d=squareSize/4, k;
2326 DrawSeekBackground(x-squareSize/8, y-squareSize/8, x+squareSize/8+1, y+squareSize/8+1);
2327 if(x < hMargin+d) DrawSeekAxis(hMargin, y-squareSize/8, hMargin, y+squareSize/8+1);
2328 // now replot every dot that overlapped
2329 for(k=0; k<nrOfSeekAds; k++) if(k != i) {
2330 int xx = xList[k], yy = yList[k];
2331 if(xx <= x+d && xx > x-d && yy <= y+d && yy > y-d)
2332 DrawSeekDot(xx, yy, colorList[k]);
2337 RemoveSeekAd(int nr)
2340 for(i=0; i<nrOfSeekAds; i++) if(seekNrList[i] == nr) {
2342 if(seekAdList[i]) free(seekAdList[i]);
2343 seekAdList[i] = seekAdList[--nrOfSeekAds];
2344 seekNrList[i] = seekNrList[nrOfSeekAds];
2345 ratingList[i] = ratingList[nrOfSeekAds];
2346 colorList[i] = colorList[nrOfSeekAds];
2347 tcList[i] = tcList[nrOfSeekAds];
2348 xList[i] = xList[nrOfSeekAds];
2349 yList[i] = yList[nrOfSeekAds];
2350 zList[i] = zList[nrOfSeekAds];
2351 seekAdList[nrOfSeekAds] = NULL;
2357 MatchSoughtLine(char *line)
2359 char handle[MSG_SIZ], rating[MSG_SIZ], type[MSG_SIZ];
2360 int nr, base, inc, u=0; char dummy;
2362 if(sscanf(line, "%d %s %s %d %d rated %s", &nr, rating, handle, &base, &inc, type) == 6 ||
2363 sscanf(line, "%d %s %s %s %d %d rated %c", &nr, rating, handle, type, &base, &inc, &dummy) == 7 ||
2365 (sscanf(line, "%d %s %s %d %d unrated %s", &nr, rating, handle, &base, &inc, type) == 6 ||
2366 sscanf(line, "%d %s %s %s %d %d unrated %c", &nr, rating, handle, type, &base, &inc, &dummy) == 7) ) {
2367 // match: compact and save the line
2368 AddAd(handle, rating, base, inc, u ? 'u' : 'r', type, nr, FALSE);
2378 if(!seekGraphUp) return FALSE;
2379 h = BOARD_HEIGHT * (squareSize + lineGap) + lineGap;
2380 w = BOARD_WIDTH * (squareSize + lineGap) + lineGap;
2382 DrawSeekBackground(0, 0, w, h);
2383 DrawSeekAxis(hMargin, h-1-vMargin, w-5, h-1-vMargin);
2384 DrawSeekAxis(hMargin, h-1-vMargin, hMargin, 5);
2385 for(i=0; i<4000; i+= 100) if(i>=minRating && i<maxRating) {
2386 int yy =((double)i - minRating)/(maxRating - minRating)*(h-vMargin-squareSize/8-1) + vMargin;
2388 DrawSeekAxis(hMargin+5*(i%500==0), yy, hMargin-5, yy); // rating ticks
2391 snprintf(buf, MSG_SIZ, "%d", i);
2392 DrawSeekText(buf, hMargin+squareSize/8+7, yy);
2395 DrawSeekText("unrated", hMargin+squareSize/8+7, h-1-vMargin-squareSize/4);
2396 for(i=1; i<100; i+=(i<10?1:5)) {
2397 int xx = (w-hMargin-squareSize/8-7)* log((double)i)/log(95.) + hMargin;
2398 DrawSeekAxis(xx, h-1-vMargin, xx, h-6-vMargin-3*(i%10==0)); // TC ticks
2399 if(i<=5 || (i>40 ? i%20 : i%10) == 0) {
2401 snprintf(buf, MSG_SIZ, "%d", i);
2402 DrawSeekText(buf, xx-2-3*(i>9), h-1-vMargin/2);
2405 for(i=0; i<nrOfSeekAds; i++) PlotSeekAd(i);
2409 int SeekGraphClick(ClickType click, int x, int y, int moving)
2411 static int lastDown = 0, displayed = 0, lastSecond;
2412 if(!seekGraphUp) { // initiate cration of seek graph by requesting seek-ad list
2413 if(click == Release || moving) return FALSE;
2415 soughtPending = TRUE;
2416 SendToICS(ics_prefix);
2417 SendToICS("sought\n"); // should this be "sought all"?
2418 } else { // issue challenge based on clicked ad
2419 int dist = 10000; int i, closest = 0, second = 0;
2420 for(i=0; i<nrOfSeekAds; i++) {
2421 int d = (x-xList[i])*(x-xList[i]) + (y-yList[i])*(y-yList[i]) + zList[i];
2422 if(d < dist) { dist = d; closest = i; }
2423 second += (d - zList[i] < 120); // count in-range ads
2424 if(click == Press && moving != 1 && zList[i]>0) zList[i] *= 0.8; // age priority
2428 second = (second > 1);
2429 if(displayed != closest || second != lastSecond) {
2430 DisplayMessage(second ? "!" : "", seekAdList[closest]);
2431 lastSecond = second; displayed = closest;
2433 if(click == Press) {
2434 if(moving == 2) zList[closest] = 100; // right-click; push to back on press
2437 } // on press 'hit', only show info
2438 if(moving == 2) return TRUE; // ignore right up-clicks on dot
2439 snprintf(buf, MSG_SIZ, "play %d\n", seekNrList[closest]);
2440 SendToICS(ics_prefix);
2442 return TRUE; // let incoming board of started game pop down the graph
2443 } else if(click == Release) { // release 'miss' is ignored
2444 zList[lastDown] = 100; // make future selection of the rejected ad more difficult
2445 if(moving == 2) { // right up-click
2446 nrOfSeekAds = 0; // refresh graph
2447 soughtPending = TRUE;
2448 SendToICS(ics_prefix);
2449 SendToICS("sought\n"); // should this be "sought all"?
2452 } else if(moving) { if(displayed >= 0) DisplayMessage("", ""); displayed = -1; return TRUE; }
2453 // press miss or release hit 'pop down' seek graph
2454 seekGraphUp = FALSE;
2455 DrawPosition(TRUE, NULL);
2461 read_from_ics(isr, closure, data, count, error)
2468 #define BUF_SIZE (16*1024) /* overflowed at 8K with "inchannel 1" on FICS? */
2469 #define STARTED_NONE 0
2470 #define STARTED_MOVES 1
2471 #define STARTED_BOARD 2
2472 #define STARTED_OBSERVE 3
2473 #define STARTED_HOLDINGS 4
2474 #define STARTED_CHATTER 5
2475 #define STARTED_COMMENT 6
2476 #define STARTED_MOVES_NOHIDE 7
2478 static int started = STARTED_NONE;
2479 static char parse[20000];
2480 static int parse_pos = 0;
2481 static char buf[BUF_SIZE + 1];
2482 static int firstTime = TRUE, intfSet = FALSE;
2483 static ColorClass prevColor = ColorNormal;
2484 static int savingComment = FALSE;
2485 static int cmatch = 0; // continuation sequence match
2492 int backup; /* [DM] For zippy color lines */
2494 char talker[MSG_SIZ]; // [HGM] chat
2497 connectionAlive = TRUE; // [HGM] alive: I think, therefore I am...
2499 if (appData.debugMode) {
2501 fprintf(debugFP, "<ICS: ");
2502 show_bytes(debugFP, data, count);
2503 fprintf(debugFP, "\n");
2507 if (appData.debugMode) { int f = forwardMostMove;
2508 fprintf(debugFP, "ics input %d, castling = %d %d %d %d %d %d\n", f,
2509 boards[f][CASTLING][0],boards[f][CASTLING][1],boards[f][CASTLING][2],
2510 boards[f][CASTLING][3],boards[f][CASTLING][4],boards[f][CASTLING][5]);
2513 /* If last read ended with a partial line that we couldn't parse,
2514 prepend it to the new read and try again. */
2515 if (leftover_len > 0) {
2516 for (i=0; i<leftover_len; i++)
2517 buf[i] = buf[leftover_start + i];
2520 /* copy new characters into the buffer */
2521 bp = buf + leftover_len;
2522 buf_len=leftover_len;
2523 for (i=0; i<count; i++)
2526 if (data[i] == '\r')
2529 // join lines split by ICS?
2530 if (!appData.noJoin)
2533 Joining just consists of finding matches against the
2534 continuation sequence, and discarding that sequence
2535 if found instead of copying it. So, until a match
2536 fails, there's nothing to do since it might be the
2537 complete sequence, and thus, something we don't want
2540 if (data[i] == cont_seq[cmatch])
2543 if (cmatch == strlen(cont_seq))
2545 cmatch = 0; // complete match. just reset the counter
2548 it's possible for the ICS to not include the space
2549 at the end of the last word, making our [correct]
2550 join operation fuse two separate words. the server
2551 does this when the space occurs at the width setting.
2553 if (!buf_len || buf[buf_len-1] != ' ')
2564 match failed, so we have to copy what matched before
2565 falling through and copying this character. In reality,
2566 this will only ever be just the newline character, but
2567 it doesn't hurt to be precise.
2569 strncpy(bp, cont_seq, cmatch);
2581 buf[buf_len] = NULLCHAR;
2582 // next_out = leftover_len; // [HGM] should we set this to 0, and not print it in advance?
2587 while (i < buf_len) {
2588 /* Deal with part of the TELNET option negotiation
2589 protocol. We refuse to do anything beyond the
2590 defaults, except that we allow the WILL ECHO option,
2591 which ICS uses to turn off password echoing when we are
2592 directly connected to it. We reject this option
2593 if localLineEditing mode is on (always on in xboard)
2594 and we are talking to port 23, which might be a real
2595 telnet server that will try to keep WILL ECHO on permanently.
2597 if (buf_len - i >= 3 && (unsigned char) buf[i] == TN_IAC) {
2598 static int remoteEchoOption = FALSE; /* telnet ECHO option */
2599 unsigned char option;
2601 switch ((unsigned char) buf[++i]) {
2603 if (appData.debugMode)
2604 fprintf(debugFP, "\n<WILL ");
2605 switch (option = (unsigned char) buf[++i]) {
2607 if (appData.debugMode)
2608 fprintf(debugFP, "ECHO ");
2609 /* Reply only if this is a change, according
2610 to the protocol rules. */
2611 if (remoteEchoOption) break;
2612 if (appData.localLineEditing &&
2613 atoi(appData.icsPort) == TN_PORT) {
2614 TelnetRequest(TN_DONT, TN_ECHO);
2617 TelnetRequest(TN_DO, TN_ECHO);
2618 remoteEchoOption = TRUE;
2622 if (appData.debugMode)
2623 fprintf(debugFP, "%d ", option);
2624 /* Whatever this is, we don't want it. */
2625 TelnetRequest(TN_DONT, option);
2630 if (appData.debugMode)
2631 fprintf(debugFP, "\n<WONT ");
2632 switch (option = (unsigned char) buf[++i]) {
2634 if (appData.debugMode)
2635 fprintf(debugFP, "ECHO ");
2636 /* Reply only if this is a change, according
2637 to the protocol rules. */
2638 if (!remoteEchoOption) break;
2640 TelnetRequest(TN_DONT, TN_ECHO);
2641 remoteEchoOption = FALSE;
2644 if (appData.debugMode)
2645 fprintf(debugFP, "%d ", (unsigned char) option);
2646 /* Whatever this is, it must already be turned
2647 off, because we never agree to turn on
2648 anything non-default, so according to the
2649 protocol rules, we don't reply. */
2654 if (appData.debugMode)
2655 fprintf(debugFP, "\n<DO ");
2656 switch (option = (unsigned char) buf[++i]) {
2658 /* Whatever this is, we refuse to do it. */
2659 if (appData.debugMode)
2660 fprintf(debugFP, "%d ", option);
2661 TelnetRequest(TN_WONT, option);
2666 if (appData.debugMode)
2667 fprintf(debugFP, "\n<DONT ");
2668 switch (option = (unsigned char) buf[++i]) {
2670 if (appData.debugMode)
2671 fprintf(debugFP, "%d ", option);
2672 /* Whatever this is, we are already not doing
2673 it, because we never agree to do anything
2674 non-default, so according to the protocol
2675 rules, we don't reply. */
2680 if (appData.debugMode)
2681 fprintf(debugFP, "\n<IAC ");
2682 /* Doubled IAC; pass it through */
2686 if (appData.debugMode)
2687 fprintf(debugFP, "\n<%d ", (unsigned char) buf[i]);
2688 /* Drop all other telnet commands on the floor */
2691 if (oldi > next_out)
2692 SendToPlayer(&buf[next_out], oldi - next_out);
2698 /* OK, this at least will *usually* work */
2699 if (!loggedOn && looking_at(buf, &i, "ics%")) {
2703 if (loggedOn && !intfSet) {
2704 if (ics_type == ICS_ICC) {
2705 snprintf(str, MSG_SIZ,
2706 "/set-quietly interface %s\n/set-quietly style 12\n",
2708 if(appData.seekGraph && appData.autoRefresh) // [HGM] seekgraph
2709 strcat(str, "/set-2 51 1\n/set seek 1\n");
2710 } else if (ics_type == ICS_CHESSNET) {
2711 snprintf(str, MSG_SIZ, "/style 12\n");
2713 safeStrCpy(str, "alias $ @\n$set interface ", sizeof(str)/sizeof(str[0]));
2714 strcat(str, programVersion);
2715 strcat(str, "\n$iset startpos 1\n$iset ms 1\n");
2716 if(appData.seekGraph && appData.autoRefresh) // [HGM] seekgraph
2717 strcat(str, "$iset seekremove 1\n$set seek 1\n");
2719 strcat(str, "$iset nohighlight 1\n");
2721 strcat(str, "$iset lock 1\n$style 12\n");
2724 NotifyFrontendLogin();
2728 if (started == STARTED_COMMENT) {
2729 /* Accumulate characters in comment */
2730 parse[parse_pos++] = buf[i];
2731 if (buf[i] == '\n') {
2732 parse[parse_pos] = NULLCHAR;
2733 if(chattingPartner>=0) {
2735 snprintf(mess, MSG_SIZ, "%s%s", talker, parse);
2736 OutputChatMessage(chattingPartner, mess);
2737 chattingPartner = -1;
2738 next_out = i+1; // [HGM] suppress printing in ICS window
2740 if(!suppressKibitz) // [HGM] kibitz
2741 AppendComment(forwardMostMove, StripHighlight(parse), TRUE);
2742 else { // [HGM kibitz: divert memorized engine kibitz to engine-output window
2743 int nrDigit = 0, nrAlph = 0, j;
2744 if(parse_pos > MSG_SIZ - 30) // defuse unreasonably long input
2745 { parse_pos = MSG_SIZ-30; parse[parse_pos - 1] = '\n'; }
2746 parse[parse_pos] = NULLCHAR;
2747 // try to be smart: if it does not look like search info, it should go to
2748 // ICS interaction window after all, not to engine-output window.
2749 for(j=0; j<parse_pos; j++) { // count letters and digits
2750 nrDigit += (parse[j] >= '0' && parse[j] <= '9');
2751 nrAlph += (parse[j] >= 'a' && parse[j] <= 'z');
2752 nrAlph += (parse[j] >= 'A' && parse[j] <= 'Z');
2754 if(nrAlph < 9*nrDigit) { // if more than 10% digit we assume search info
2755 int depth=0; float score;
2756 if(sscanf(parse, "!!! %f/%d", &score, &depth) == 2 && depth>0) {
2757 // [HGM] kibitz: save kibitzed opponent info for PGN and eval graph
2758 pvInfoList[forwardMostMove-1].depth = depth;
2759 pvInfoList[forwardMostMove-1].score = 100*score;
2761 OutputKibitz(suppressKibitz, parse);
2764 snprintf(tmp, MSG_SIZ, _("your opponent kibitzes: %s"), parse);
2765 SendToPlayer(tmp, strlen(tmp));
2767 next_out = i+1; // [HGM] suppress printing in ICS window
2769 started = STARTED_NONE;
2771 /* Don't match patterns against characters in comment */
2776 if (started == STARTED_CHATTER) {
2777 if (buf[i] != '\n') {
2778 /* Don't match patterns against characters in chatter */
2782 started = STARTED_NONE;
2783 if(suppressKibitz) next_out = i+1;
2786 /* Kludge to deal with rcmd protocol */
2787 if (firstTime && looking_at(buf, &i, "\001*")) {
2788 DisplayFatalError(&buf[1], 0, 1);
2794 if (!loggedOn && looking_at(buf, &i, "chessclub.com")) {
2797 if (appData.debugMode)
2798 fprintf(debugFP, "ics_type %d\n", ics_type);
2801 if (!loggedOn && looking_at(buf, &i, "freechess.org")) {
2802 ics_type = ICS_FICS;
2804 if (appData.debugMode)
2805 fprintf(debugFP, "ics_type %d\n", ics_type);
2808 if (!loggedOn && looking_at(buf, &i, "chess.net")) {
2809 ics_type = ICS_CHESSNET;
2811 if (appData.debugMode)
2812 fprintf(debugFP, "ics_type %d\n", ics_type);
2817 (looking_at(buf, &i, "\"*\" is *a registered name") ||
2818 looking_at(buf, &i, "Logging you in as \"*\"") ||
2819 looking_at(buf, &i, "will be \"*\""))) {
2820 safeStrCpy(ics_handle, star_match[0], sizeof(ics_handle)/sizeof(ics_handle[0]));
2824 if (loggedOn && !have_set_title && ics_handle[0] != NULLCHAR) {
2826 snprintf(buf, sizeof(buf), "%s@%s", ics_handle, appData.icsHost);
2827 DisplayIcsInteractionTitle(buf);
2828 have_set_title = TRUE;
2831 /* skip finger notes */
2832 if (started == STARTED_NONE &&
2833 ((buf[i] == ' ' && isdigit(buf[i+1])) ||
2834 (buf[i] == '1' && buf[i+1] == '0')) &&
2835 buf[i+2] == ':' && buf[i+3] == ' ') {
2836 started = STARTED_CHATTER;
2842 // [HGM] seekgraph: recognize sought lines and end-of-sought message
2843 if(appData.seekGraph) {
2844 if(soughtPending && MatchSoughtLine(buf+i)) {
2845 i = strstr(buf+i, "rated") - buf;
2846 if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
2847 next_out = leftover_start = i;
2848 started = STARTED_CHATTER;
2849 suppressKibitz = TRUE;
2852 if((gameMode == IcsIdle || gameMode == BeginningOfGame)
2853 && looking_at(buf, &i, "* ads displayed")) {
2854 soughtPending = FALSE;
2859 if(appData.autoRefresh) {
2860 if(looking_at(buf, &i, "* (*) seeking * * * * *\"play *\" to respond)\n")) {
2861 int s = (ics_type == ICS_ICC); // ICC format differs
2863 AddAd(star_match[0], star_match[1], atoi(star_match[2+s]), atoi(star_match[3+s]),
2864 star_match[4+s][0], star_match[5-3*s], atoi(star_match[7]), TRUE);
2865 looking_at(buf, &i, "*% "); // eat prompt
2866 if(oldi > 0 && buf[oldi-1] == '\n') oldi--; // suppress preceding LF, if any
2867 if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
2868 next_out = i; // suppress
2871 if(looking_at(buf, &i, "\nAds removed: *\n") || looking_at(buf, &i, "\031(51 * *\031)")) {
2872 char *p = star_match[0];
2874 if(seekGraphUp) RemoveSeekAd(atoi(p));
2875 while(*p && *p++ != ' '); // next
2877 looking_at(buf, &i, "*% "); // eat prompt
2878 if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
2885 /* skip formula vars */
2886 if (started == STARTED_NONE &&
2887 buf[i] == 'f' && isdigit(buf[i+1]) && buf[i+2] == ':') {
2888 started = STARTED_CHATTER;
2893 // [HGM] kibitz: try to recognize opponent engine-score kibitzes, to divert them to engine-output window
2894 if (appData.autoKibitz && started == STARTED_NONE &&
2895 !appData.icsEngineAnalyze && // [HGM] [DM] ICS analyze
2896 (gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack || gameMode == IcsObserving)) {
2897 if((looking_at(buf, &i, "* kibitzes: ") || looking_at(buf, &i, "* whispers: ")) &&
2898 (StrStr(star_match[0], gameInfo.white) == star_match[0] ||
2899 StrStr(star_match[0], gameInfo.black) == star_match[0] )) { // kibitz of self or opponent
2900 suppressKibitz = TRUE;
2901 if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
2903 if((StrStr(star_match[0], gameInfo.white) == star_match[0]
2904 && (gameMode == IcsPlayingWhite)) ||
2905 (StrStr(star_match[0], gameInfo.black) == star_match[0]
2906 && (gameMode == IcsPlayingBlack)) ) // opponent kibitz
2907 started = STARTED_CHATTER; // own kibitz we simply discard
2909 started = STARTED_COMMENT; // make sure it will be collected in parse[]
2910 parse_pos = 0; parse[0] = NULLCHAR;
2911 savingComment = TRUE;
2912 suppressKibitz = gameMode != IcsObserving ? 2 :
2913 (StrStr(star_match[0], gameInfo.white) == NULL) + 1;
2917 if((looking_at(buf, &i, "\nkibitzed to *\n") || looking_at(buf, &i, "kibitzed to *\n") ||
2918 looking_at(buf, &i, "\n(kibitzed to *\n") || looking_at(buf, &i, "(kibitzed to *\n"))
2919 && atoi(star_match[0])) {
2920 // suppress the acknowledgements of our own autoKibitz
2922 if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
2923 if(p = strchr(star_match[0], ' ')) p[1] = NULLCHAR; // clip off "players)" on FICS
2924 SendToPlayer(star_match[0], strlen(star_match[0]));
2925 if(looking_at(buf, &i, "*% ")) // eat prompt
2926 suppressKibitz = FALSE;
2930 } // [HGM] kibitz: end of patch
2932 // [HGM] chat: intercept tells by users for which we have an open chat window
2934 if(started == STARTED_NONE && (looking_at(buf, &i, "* tells you:") || looking_at(buf, &i, "* says:") ||
2935 looking_at(buf, &i, "* whispers:") ||
2936 looking_at(buf, &i, "* kibitzes:") ||
2937 looking_at(buf, &i, "* shouts:") ||
2938 looking_at(buf, &i, "* c-shouts:") ||
2939 looking_at(buf, &i, "--> * ") ||
2940 looking_at(buf, &i, "*(*):") && (sscanf(star_match[1], "%d", &channel),1) ||
2941 looking_at(buf, &i, "*(*)(*):") && (sscanf(star_match[2], "%d", &channel),1) ||
2942 looking_at(buf, &i, "*(*)(*)(*):") && (sscanf(star_match[3], "%d", &channel),1) ||
2943 looking_at(buf, &i, "*(*)(*)(*)(*):") && sscanf(star_match[4], "%d", &channel) == 1 )) {
2945 sscanf(star_match[0], "%[^(]", talker+1); // strip (C) or (U) off ICS handle
2946 chattingPartner = -1;
2948 if(channel >= 0) // channel broadcast; look if there is a chatbox for this channel
2949 for(p=0; p<MAX_CHAT; p++) {
2950 if(chatPartner[p][0] >= '0' && chatPartner[p][0] <= '9' && channel == atoi(chatPartner[p])) {
2951 talker[0] = '['; strcat(talker, "] ");
2952 Colorize(channel == 1 ? ColorChannel1 : ColorChannel, FALSE);
2953 chattingPartner = p; break;
2956 if(buf[i-3] == 'e') // kibitz; look if there is a KIBITZ chatbox
2957 for(p=0; p<MAX_CHAT; p++) {
2958 if(!strcmp("kibitzes", chatPartner[p])) {
2959 talker[0] = '['; strcat(talker, "] ");
2960 chattingPartner = p; break;
2963 if(buf[i-3] == 'r') // whisper; look if there is a WHISPER chatbox
2964 for(p=0; p<MAX_CHAT; p++) {
2965 if(!strcmp("whispers", chatPartner[p])) {
2966 talker[0] = '['; strcat(talker, "] ");
2967 chattingPartner = p; break;
2970 if(buf[i-3] == 't' || buf[oldi+2] == '>') {// shout, c-shout or it; look if there is a 'shouts' chatbox
2971 if(buf[i-8] == '-' && buf[i-3] == 't')
2972 for(p=0; p<MAX_CHAT; p++) { // c-shout; check if dedicatesd c-shout box exists
2973 if(!strcmp("c-shouts", chatPartner[p])) {
2974 talker[0] = '('; strcat(talker, ") "); Colorize(ColorSShout, FALSE);
2975 chattingPartner = p; break;
2978 if(chattingPartner < 0)
2979 for(p=0; p<MAX_CHAT; p++) {
2980 if(!strcmp("shouts", chatPartner[p])) {
2981 if(buf[oldi+2] == '>') { talker[0] = '<'; strcat(talker, "> "); Colorize(ColorShout, FALSE); }
2982 else if(buf[i-8] == '-') { talker[0] = '('; strcat(talker, ") "); Colorize(ColorSShout, FALSE); }
2983 else { talker[0] = '['; strcat(talker, "] "); Colorize(ColorShout, FALSE); }
2984 chattingPartner = p; break;
2988 if(chattingPartner<0) // if not, look if there is a chatbox for this indivdual
2989 for(p=0; p<MAX_CHAT; p++) if(!StrCaseCmp(talker+1, chatPartner[p])) {
2990 talker[0] = 0; Colorize(ColorTell, FALSE);
2991 chattingPartner = p; break;
2993 if(chattingPartner<0) i = oldi; else {
2994 Colorize(curColor, TRUE); // undo the bogus colorations we just made to trigger the souds
2995 if(oldi > 0 && buf[oldi-1] == '\n') oldi--;
2996 if (oldi > next_out) SendToPlayer(&buf[next_out], oldi - next_out);
2997 started = STARTED_COMMENT;
2998 parse_pos = 0; parse[0] = NULLCHAR;
2999 savingComment = 3 + chattingPartner; // counts as TRUE
3000 suppressKibitz = TRUE;
3003 } // [HGM] chat: end of patch
3006 if (appData.zippyTalk || appData.zippyPlay) {
3007 /* [DM] Backup address for color zippy lines */
3009 if (loggedOn == TRUE)
3010 if (ZippyControl(buf, &backup) || ZippyConverse(buf, &backup) ||
3011 (appData.zippyPlay && ZippyMatch(buf, &backup)));
3013 } // [DM] 'else { ' deleted
3015 /* Regular tells and says */
3016 (tkind = 1, looking_at(buf, &i, "* tells you: ")) ||
3017 looking_at(buf, &i, "* (your partner) tells you: ") ||
3018 looking_at(buf, &i, "* says: ") ||
3019 /* Don't color "message" or "messages" output */
3020 (tkind = 5, looking_at(buf, &i, "*. * (*:*): ")) ||
3021 looking_at(buf, &i, "*. * at *:*: ") ||
3022 looking_at(buf, &i, "--* (*:*): ") ||
3023 /* Message notifications (same color as tells) */
3024 looking_at(buf, &i, "* has left a message ") ||
3025 looking_at(buf, &i, "* just sent you a message:\n") ||
3026 /* Whispers and kibitzes */
3027 (tkind = 2, looking_at(buf, &i, "* whispers: ")) ||
3028 looking_at(buf, &i, "* kibitzes: ") ||
3030 (tkind = 3, looking_at(buf, &i, "*(*: "))) {
3032 if (tkind == 1 && strchr(star_match[0], ':')) {
3033 /* Avoid "tells you:" spoofs in channels */
3036 if (star_match[0][0] == NULLCHAR ||
3037 strchr(star_match[0], ' ') ||
3038 (tkind == 3 && strchr(star_match[1], ' '))) {
3039 /* Reject bogus matches */
3042 if (appData.colorize) {
3043 if (oldi > next_out) {
3044 SendToPlayer(&buf[next_out], oldi - next_out);
3049 Colorize(ColorTell, FALSE);
3050 curColor = ColorTell;
3053 Colorize(ColorKibitz, FALSE);
3054 curColor = ColorKibitz;
3057 p = strrchr(star_match[1], '(');
3064 Colorize(ColorChannel1, FALSE);
3065 curColor = ColorChannel1;
3067 Colorize(ColorChannel, FALSE);
3068 curColor = ColorChannel;
3072 curColor = ColorNormal;
3076 if (started == STARTED_NONE && appData.autoComment &&
3077 (gameMode == IcsObserving ||
3078 gameMode == IcsPlayingWhite ||
3079 gameMode == IcsPlayingBlack)) {
3080 parse_pos = i - oldi;
3081 memcpy(parse, &buf[oldi], parse_pos);
3082 parse[parse_pos] = NULLCHAR;
3083 started = STARTED_COMMENT;
3084 savingComment = TRUE;
3086 started = STARTED_CHATTER;
3087 savingComment = FALSE;
3094 if (looking_at(buf, &i, "* s-shouts: ") ||
3095 looking_at(buf, &i, "* c-shouts: ")) {
3096 if (appData.colorize) {
3097 if (oldi > next_out) {
3098 SendToPlayer(&buf[next_out], oldi - next_out);
3101 Colorize(ColorSShout, FALSE);
3102 curColor = ColorSShout;
3105 started = STARTED_CHATTER;
3109 if (looking_at(buf, &i, "--->")) {
3114 if (looking_at(buf, &i, "* shouts: ") ||
3115 looking_at(buf, &i, "--> ")) {
3116 if (appData.colorize) {
3117 if (oldi > next_out) {
3118 SendToPlayer(&buf[next_out], oldi - next_out);
3121 Colorize(ColorShout, FALSE);
3122 curColor = ColorShout;
3125 started = STARTED_CHATTER;
3129 if (looking_at( buf, &i, "Challenge:")) {
3130 if (appData.colorize) {
3131 if (oldi > next_out) {
3132 SendToPlayer(&buf[next_out], oldi - next_out);
3135 Colorize(ColorChallenge, FALSE);
3136 curColor = ColorChallenge;
3142 if (looking_at(buf, &i, "* offers you") ||
3143 looking_at(buf, &i, "* offers to be") ||
3144 looking_at(buf, &i, "* would like to") ||
3145 looking_at(buf, &i, "* requests to") ||
3146 looking_at(buf, &i, "Your opponent offers") ||
3147 looking_at(buf, &i, "Your opponent requests")) {
3149 if (appData.colorize) {
3150 if (oldi > next_out) {
3151 SendToPlayer(&buf[next_out], oldi - next_out);
3154 Colorize(ColorRequest, FALSE);
3155 curColor = ColorRequest;
3160 if (looking_at(buf, &i, "* (*) seeking")) {
3161 if (appData.colorize) {
3162 if (oldi > next_out) {
3163 SendToPlayer(&buf[next_out], oldi - next_out);
3166 Colorize(ColorSeek, FALSE);
3167 curColor = ColorSeek;
3172 if(i < backup) { i = backup; continue; } // [HGM] for if ZippyControl matches, but the colorie code doesn't
3174 if (looking_at(buf, &i, "\\ ")) {
3175 if (prevColor != ColorNormal) {
3176 if (oldi > next_out) {
3177 SendToPlayer(&buf[next_out], oldi - next_out);
3180 Colorize(prevColor, TRUE);
3181 curColor = prevColor;
3183 if (savingComment) {
3184 parse_pos = i - oldi;
3185 memcpy(parse, &buf[oldi], parse_pos);
3186 parse[parse_pos] = NULLCHAR;
3187 started = STARTED_COMMENT;
3188 if(savingComment >= 3) // [HGM] chat: continuation of line for chat box
3189 chattingPartner = savingComment - 3; // kludge to remember the box
3191 started = STARTED_CHATTER;
3196 if (looking_at(buf, &i, "Black Strength :") ||
3197 looking_at(buf, &i, "<<< style 10 board >>>") ||
3198 looking_at(buf, &i, "<10>") ||
3199 looking_at(buf, &i, "#@#")) {
3200 /* Wrong board style */
3202 SendToICS(ics_prefix);
3203 SendToICS("set style 12\n");
3204 SendToICS(ics_prefix);
3205 SendToICS("refresh\n");
3209 if (!have_sent_ICS_logon && looking_at(buf, &i, "login:")) {
3211 have_sent_ICS_logon = 1;
3215 if (ics_getting_history != H_GETTING_MOVES /*smpos kludge*/ &&
3216 (looking_at(buf, &i, "\n<12> ") ||
3217 looking_at(buf, &i, "<12> "))) {
3219 if (oldi > next_out) {
3220 SendToPlayer(&buf[next_out], oldi - next_out);
3223 started = STARTED_BOARD;
3228 if ((started == STARTED_NONE && looking_at(buf, &i, "\n<b1> ")) ||
3229 looking_at(buf, &i, "<b1> ")) {
3230 if (oldi > next_out) {
3231 SendToPlayer(&buf[next_out], oldi - next_out);
3234 started = STARTED_HOLDINGS;
3239 if (looking_at(buf, &i, "* *vs. * *--- *")) {
3241 /* Header for a move list -- first line */
3243 switch (ics_getting_history) {
3247 case BeginningOfGame:
3248 /* User typed "moves" or "oldmoves" while we
3249 were idle. Pretend we asked for these
3250 moves and soak them up so user can step
3251 through them and/or save them.
3254 gameMode = IcsObserving;
3257 ics_getting_history = H_GOT_UNREQ_HEADER;
3259 case EditGame: /*?*/
3260 case EditPosition: /*?*/
3261 /* Should above feature work in these modes too? */
3262 /* For now it doesn't */
3263 ics_getting_history = H_GOT_UNWANTED_HEADER;
3266 ics_getting_history = H_GOT_UNWANTED_HEADER;
3271 /* Is this the right one? */
3272 if (gameInfo.white && gameInfo.black &&
3273 strcmp(gameInfo.white, star_match[0]) == 0 &&
3274 strcmp(gameInfo.black, star_match[2]) == 0) {
3276 ics_getting_history = H_GOT_REQ_HEADER;
3279 case H_GOT_REQ_HEADER:
3280 case H_GOT_UNREQ_HEADER:
3281 case H_GOT_UNWANTED_HEADER:
3282 case H_GETTING_MOVES:
3283 /* Should not happen */
3284 DisplayError(_("Error gathering move list: two headers"), 0);
3285 ics_getting_history = H_FALSE;
3289 /* Save player ratings into gameInfo if needed */
3290 if ((ics_getting_history == H_GOT_REQ_HEADER ||
3291 ics_getting_history == H_GOT_UNREQ_HEADER) &&
3292 (gameInfo.whiteRating == -1 ||
3293 gameInfo.blackRating == -1)) {
3295 gameInfo.whiteRating = string_to_rating(star_match[1]);
3296 gameInfo.blackRating = string_to_rating(star_match[3]);
3297 if (appData.debugMode)
3298 fprintf(debugFP, _("Ratings from header: W %d, B %d\n"),
3299 gameInfo.whiteRating, gameInfo.blackRating);
3304 if (looking_at(buf, &i,
3305 "* * match, initial time: * minute*, increment: * second")) {
3306 /* Header for a move list -- second line */
3307 /* Initial board will follow if this is a wild game */
3308 if (gameInfo.event != NULL) free(gameInfo.event);
3309 snprintf(str, MSG_SIZ, "ICS %s %s match", star_match[0], star_match[1]);
3310 gameInfo.event = StrSave(str);
3311 /* [HGM] we switched variant. Translate boards if needed. */
3312 VariantSwitch(boards[currentMove], StringToVariant(gameInfo.event));
3316 if (looking_at(buf, &i, "Move ")) {
3317 /* Beginning of a move list */
3318 switch (ics_getting_history) {
3320 /* Normally should not happen */
3321 /* Maybe user hit reset while we were parsing */
3324 /* Happens if we are ignoring a move list that is not
3325 * the one we just requested. Common if the user
3326 * tries to observe two games without turning off
3329 case H_GETTING_MOVES:
3330 /* Should not happen */
3331 DisplayError(_("Error gathering move list: nested"), 0);
3332 ics_getting_history = H_FALSE;
3334 case H_GOT_REQ_HEADER:
3335 ics_getting_history = H_GETTING_MOVES;
3336 started = STARTED_MOVES;
3338 if (oldi > next_out) {
3339 SendToPlayer(&buf[next_out], oldi - next_out);
3342 case H_GOT_UNREQ_HEADER:
3343 ics_getting_history = H_GETTING_MOVES;
3344 started = STARTED_MOVES_NOHIDE;
3347 case H_GOT_UNWANTED_HEADER:
3348 ics_getting_history = H_FALSE;
3354 if (looking_at(buf, &i, "% ") ||
3355 ((started == STARTED_MOVES || started == STARTED_MOVES_NOHIDE)
3356 && looking_at(buf, &i, "}*"))) { char *bookHit = NULL; // [HGM] book
3357 if(ics_type == ICS_ICC && soughtPending) { // [HGM] seekgraph: on ICC sought-list has no termination line
3358 soughtPending = FALSE;
3362 if(suppressKibitz) next_out = i;
3363 savingComment = FALSE;
3367 case STARTED_MOVES_NOHIDE:
3368 memcpy(&parse[parse_pos], &buf[oldi], i - oldi);
3369 parse[parse_pos + i - oldi] = NULLCHAR;
3370 ParseGameHistory(parse);
3372 if (appData.zippyPlay && first.initDone) {
3373 FeedMovesToProgram(&first, forwardMostMove);
3374 if (gameMode == IcsPlayingWhite) {
3375 if (WhiteOnMove(forwardMostMove)) {
3376 if (first.sendTime) {
3377 if (first.useColors) {
3378 SendToProgram("black\n", &first);
3380 SendTimeRemaining(&first, TRUE);
3382 if (first.useColors) {
3383 SendToProgram("white\n", &first); // [HGM] book: made sending of "go\n" book dependent
3385 bookHit = SendMoveToBookUser(forwardMostMove-1, &first, TRUE); // [HGM] book: probe book for initial pos
3386 first.maybeThinking = TRUE;
3388 if (first.usePlayother) {
3389 if (first.sendTime) {
3390 SendTimeRemaining(&first, TRUE);
3392 SendToProgram("playother\n", &first);
3398 } else if (gameMode == IcsPlayingBlack) {
3399 if (!WhiteOnMove(forwardMostMove)) {
3400 if (first.sendTime) {
3401 if (first.useColors) {
3402 SendToProgram("white\n", &first);
3404 SendTimeRemaining(&first, FALSE);
3406 if (first.useColors) {
3407 SendToProgram("black\n", &first);
3409 bookHit = SendMoveToBookUser(forwardMostMove-1, &first, TRUE);
3410 first.maybeThinking = TRUE;
3412 if (first.usePlayother) {
3413 if (first.sendTime) {
3414 SendTimeRemaining(&first, FALSE);
3416 SendToProgram("playother\n", &first);
3425 if (gameMode == IcsObserving && ics_gamenum == -1) {
3426 /* Moves came from oldmoves or moves command
3427 while we weren't doing anything else.
3429 currentMove = forwardMostMove;
3430 ClearHighlights();/*!!could figure this out*/
3431 flipView = appData.flipView;
3432 DrawPosition(TRUE, boards[currentMove]);
3433 DisplayBothClocks();
3434 snprintf(str, MSG_SIZ, "%s vs. %s",
3435 gameInfo.white, gameInfo.black);
3439 /* Moves were history of an active game */
3440 if (gameInfo.resultDetails != NULL) {
3441 free(gameInfo.resultDetails);
3442 gameInfo.resultDetails = NULL;
3445 HistorySet(parseList, backwardMostMove,
3446 forwardMostMove, currentMove-1);
3447 DisplayMove(currentMove - 1);
3448 if (started == STARTED_MOVES) next_out = i;
3449 started = STARTED_NONE;
3450 ics_getting_history = H_FALSE;
3453 case STARTED_OBSERVE:
3454 started = STARTED_NONE;
3455 SendToICS(ics_prefix);
3456 SendToICS("refresh\n");
3462 if(bookHit) { // [HGM] book: simulate book reply
3463 static char bookMove[MSG_SIZ]; // a bit generous?
3465 programStats.nodes = programStats.depth = programStats.time =
3466 programStats.score = programStats.got_only_move = 0;
3467 sprintf(programStats.movelist, "%s (xbook)", bookHit);
3469 safeStrCpy(bookMove, "move ", sizeof(bookMove)/sizeof(bookMove[0]));
3470 strcat(bookMove, bookHit);
3471 HandleMachineMove(bookMove, &first);
3476 if ((started == STARTED_MOVES || started == STARTED_BOARD ||
3477 started == STARTED_HOLDINGS ||
3478 started == STARTED_MOVES_NOHIDE) && i >= leftover_len) {
3479 /* Accumulate characters in move list or board */
3480 parse[parse_pos++] = buf[i];
3483 /* Start of game messages. Mostly we detect start of game
3484 when the first board image arrives. On some versions
3485 of the ICS, though, we need to do a "refresh" after starting
3486 to observe in order to get the current board right away. */
3487 if (looking_at(buf, &i, "Adding game * to observation list")) {
3488 started = STARTED_OBSERVE;
3492 /* Handle auto-observe */
3493 if (appData.autoObserve &&
3494 (gameMode == IcsIdle || gameMode == BeginningOfGame) &&
3495 looking_at(buf, &i, "Game notification: * (*) vs. * (*)")) {
3497 /* Choose the player that was highlighted, if any. */
3498 if (star_match[0][0] == '\033' ||
3499 star_match[1][0] != '\033') {
3500 player = star_match[0];
3502 player = star_match[2];
3504 snprintf(str, MSG_SIZ, "%sobserve %s\n",
3505 ics_prefix, StripHighlightAndTitle(player));
3508 /* Save ratings from notify string */
3509 safeStrCpy(player1Name, star_match[0], sizeof(player1Name)/sizeof(player1Name[0]));
3510 player1Rating = string_to_rating(star_match[1]);
3511 safeStrCpy(player2Name, star_match[2], sizeof(player2Name)/sizeof(player2Name[0]));
3512 player2Rating = string_to_rating(star_match[3]);
3514 if (appData.debugMode)
3516 "Ratings from 'Game notification:' %s %d, %s %d\n",
3517 player1Name, player1Rating,
3518 player2Name, player2Rating);
3523 /* Deal with automatic examine mode after a game,
3524 and with IcsObserving -> IcsExamining transition */
3525 if (looking_at(buf, &i, "Entering examine mode for game *") ||
3526 looking_at(buf, &i, "has made you an examiner of game *")) {
3528 int gamenum = atoi(star_match[0]);
3529 if ((gameMode == IcsIdle || gameMode == IcsObserving) &&
3530 gamenum == ics_gamenum) {
3531 /* We were already playing or observing this game;
3532 no need to refetch history */
3533 gameMode = IcsExamining;
3535 pauseExamForwardMostMove = forwardMostMove;
3536 } else if (currentMove < forwardMostMove) {
3537 ForwardInner(forwardMostMove);
3540 /* I don't think this case really can happen */
3541 SendToICS(ics_prefix);
3542 SendToICS("refresh\n");
3547 /* Error messages */
3548 // if (ics_user_moved) {
3549 if (1) { // [HGM] old way ignored error after move type in; ics_user_moved is not set then!
3550 if (looking_at(buf, &i, "Illegal move") ||
3551 looking_at(buf, &i, "Not a legal move") ||
3552 looking_at(buf, &i, "Your king is in check") ||
3553 looking_at(buf, &i, "It isn't your turn") ||
3554 looking_at(buf, &i, "It is not your move")) {
3556 if (ics_user_moved && forwardMostMove > backwardMostMove) { // only backup if we already moved
3557 currentMove = forwardMostMove-1;
3558 DisplayMove(currentMove - 1); /* before DMError */
3559 DrawPosition(FALSE, boards[currentMove]);
3560 SwitchClocks(forwardMostMove-1); // [HGM] race
3561 DisplayBothClocks();
3563 DisplayMoveError(_("Illegal move (rejected by ICS)")); // [HGM] but always relay error msg
3569 if (looking_at(buf, &i, "still have time") ||
3570 looking_at(buf, &i, "not out of time") ||
3571 looking_at(buf, &i, "either player is out of time") ||
3572 looking_at(buf, &i, "has timeseal; checking")) {
3573 /* We must have called his flag a little too soon */
3574 whiteFlag = blackFlag = FALSE;
3578 if (looking_at(buf, &i, "added * seconds to") ||
3579 looking_at(buf, &i, "seconds were added to")) {
3580 /* Update the clocks */
3581 SendToICS(ics_prefix);
3582 SendToICS("refresh\n");
3586 if (!ics_clock_paused && looking_at(buf, &i, "clock paused")) {
3587 ics_clock_paused = TRUE;
3592 if (ics_clock_paused && looking_at(buf, &i, "clock resumed")) {
3593 ics_clock_paused = FALSE;
3598 /* Grab player ratings from the Creating: message.
3599 Note we have to check for the special case when
3600 the ICS inserts things like [white] or [black]. */
3601 if (looking_at(buf, &i, "Creating: * (*)* * (*)") ||
3602 looking_at(buf, &i, "Creating: * (*) [*] * (*)")) {
3604 0 player 1 name (not necessarily white)
3606 2 empty, white, or black (IGNORED)
3607 3 player 2 name (not necessarily black)
3610 The names/ratings are sorted out when the game
3611 actually starts (below).
3613 safeStrCpy(player1Name, StripHighlightAndTitle(star_match[0]), sizeof(player1Name)/sizeof(player1Name[0]));
3614 player1Rating = string_to_rating(star_match[1]);
3615 safeStrCpy(player2Name, StripHighlightAndTitle(star_match[3]), sizeof(player2Name)/sizeof(player2Name[0]));
3616 player2Rating = string_to_rating(star_match[4]);
3618 if (appData.debugMode)
3620 "Ratings from 'Creating:' %s %d, %s %d\n",
3621 player1Name, player1Rating,
3622 player2Name, player2Rating);
3627 /* Improved generic start/end-of-game messages */
3628 if ((tkind=0, looking_at(buf, &i, "{Game * (* vs. *) *}*")) ||
3629 (tkind=1, looking_at(buf, &i, "{Game * (*(*) vs. *(*)) *}*"))){
3630 /* If tkind == 0: */
3631 /* star_match[0] is the game number */
3632 /* [1] is the white player's name */
3633 /* [2] is the black player's name */
3634 /* For end-of-game: */
3635 /* [3] is the reason for the game end */
3636 /* [4] is a PGN end game-token, preceded by " " */
3637 /* For start-of-game: */
3638 /* [3] begins with "Creating" or "Continuing" */
3639 /* [4] is " *" or empty (don't care). */
3640 int gamenum = atoi(star_match[0]);
3641 char *whitename, *blackname, *why, *endtoken;
3642 ChessMove endtype = EndOfFile;
3645 whitename = star_match[1];
3646 blackname = star_match[2];
3647 why = star_match[3];
3648 endtoken = star_match[4];
3650 whitename = star_match[1];
3651 blackname = star_match[3];
3652 why = star_match[5];
3653 endtoken = star_match[6];
3656 /* Game start messages */
3657 if (strncmp(why, "Creating ", 9) == 0 ||
3658 strncmp(why, "Continuing ", 11) == 0) {
3659 gs_gamenum = gamenum;
3660 safeStrCpy(gs_kind, strchr(why, ' ') + 1,sizeof(gs_kind)/sizeof(gs_kind[0]));
3661 VariantSwitch(boards[currentMove], StringToVariant(gs_kind)); // [HGM] variantswitch: even before we get first board
3663 if (appData.zippyPlay) {
3664 ZippyGameStart(whitename, blackname);
3667 partnerBoardValid = FALSE; // [HGM] bughouse
3671 /* Game end messages */
3672 if (gameMode == IcsIdle || gameMode == BeginningOfGame ||
3673 ics_gamenum != gamenum) {
3676 while (endtoken[0] == ' ') endtoken++;
3677 switch (endtoken[0]) {
3680 endtype = GameUnfinished;
3683 endtype = BlackWins;
3686 if (endtoken[1] == '/')
3687 endtype = GameIsDrawn;
3689 endtype = WhiteWins;
3692 GameEnds(endtype, why, GE_ICS);
3694 if (appData.zippyPlay && first.initDone) {
3695 ZippyGameEnd(endtype, why);
3696 if (first.pr == NULL) {
3697 /* Start the next process early so that we'll
3698 be ready for the next challenge */
3699 StartChessProgram(&first);
3701 /* Send "new" early, in case this command takes
3702 a long time to finish, so that we'll be ready
3703 for the next challenge. */
3704 gameInfo.variant = VariantNormal; // [HGM] variantswitch: suppress sending of 'variant'
3708 if(appData.bgObserve && partnerBoardValid) DrawPosition(TRUE, partnerBoard);
3712 if (looking_at(buf, &i, "Removing game * from observation") ||
3713 looking_at(buf, &i, "no longer observing game *") ||
3714 looking_at(buf, &i, "Game * (*) has no examiners")) {
3715 if (gameMode == IcsObserving &&
3716 atoi(star_match[0]) == ics_gamenum)
3718 /* icsEngineAnalyze */
3719 if (appData.icsEngineAnalyze) {
3726 ics_user_moved = FALSE;
3731 if (looking_at(buf, &i, "no longer examining game *")) {
3732 if (gameMode == IcsExamining &&
3733 atoi(star_match[0]) == ics_gamenum)
3737 ics_user_moved = FALSE;
3742 /* Advance leftover_start past any newlines we find,
3743 so only partial lines can get reparsed */
3744 if (looking_at(buf, &i, "\n")) {
3745 prevColor = curColor;
3746 if (curColor != ColorNormal) {
3747 if (oldi > next_out) {
3748 SendToPlayer(&buf[next_out], oldi - next_out);
3751 Colorize(ColorNormal, FALSE);
3752 curColor = ColorNormal;
3754 if (started == STARTED_BOARD) {
3755 started = STARTED_NONE;
3756 parse[parse_pos] = NULLCHAR;
3757 ParseBoard12(parse);
3760 /* Send premove here */
3761 if (appData.premove) {
3763 if (currentMove == 0 &&
3764 gameMode == IcsPlayingWhite &&
3765 appData.premoveWhite) {
3766 snprintf(str, MSG_SIZ, "%s\n", appData.premoveWhiteText);
3767 if (appData.debugMode)
3768 fprintf(debugFP, "Sending premove:\n");
3770 } else if (currentMove == 1 &&
3771 gameMode == IcsPlayingBlack &&
3772 appData.premoveBlack) {
3773 snprintf(str, MSG_SIZ, "%s\n", appData.premoveBlackText);
3774 if (appData.debugMode)
3775 fprintf(debugFP, "Sending premove:\n");
3777 } else if (gotPremove) {
3779 ClearPremoveHighlights();
3780 if (appData.debugMode)
3781 fprintf(debugFP, "Sending premove:\n");
3782 UserMoveEvent(premoveFromX, premoveFromY,
3783 premoveToX, premoveToY,
3788 /* Usually suppress following prompt */
3789 if (!(forwardMostMove == 0 && gameMode == IcsExamining)) {
3790 while(looking_at(buf, &i, "\n")); // [HGM] skip empty lines
3791 if (looking_at(buf, &i, "*% ")) {
3792 savingComment = FALSE;
3797 } else if (started == STARTED_HOLDINGS) {
3799 char new_piece[MSG_SIZ];
3800 started = STARTED_NONE;
3801 parse[parse_pos] = NULLCHAR;
3802 if (appData.debugMode)
3803 fprintf(debugFP, "Parsing holdings: %s, currentMove = %d\n",
3804 parse, currentMove);
3805 if (sscanf(parse, " game %d", &gamenum) == 1) {
3806 if(gamenum == ics_gamenum) { // [HGM] bughouse: old code if part of foreground game
3807 if (gameInfo.variant == VariantNormal) {
3808 /* [HGM] We seem to switch variant during a game!
3809 * Presumably no holdings were displayed, so we have
3810 * to move the position two files to the right to
3811 * create room for them!
3813 VariantClass newVariant;
3814 switch(gameInfo.boardWidth) { // base guess on board width
3815 case 9: newVariant = VariantShogi; break;
3816 case 10: newVariant = VariantGreat; break;
3817 default: newVariant = VariantCrazyhouse; break;
3819 VariantSwitch(boards[currentMove], newVariant); /* temp guess */
3820 /* Get a move list just to see the header, which
3821 will tell us whether this is really bug or zh */
3822 if (ics_getting_history == H_FALSE) {
3823 ics_getting_history = H_REQUESTED;
3824 snprintf(str, MSG_SIZ, "%smoves %d\n", ics_prefix, gamenum);
3828 new_piece[0] = NULLCHAR;
3829 sscanf(parse, "game %d white [%s black [%s <- %s",
3830 &gamenum, white_holding, black_holding,
3832 white_holding[strlen(white_holding)-1] = NULLCHAR;