2 * backend.c -- Common back end for X and Windows NT versions of
5 * Copyright 1991 by Digital Equipment Corporation, Maynard, Massachusetts.
6 * Enhancements Copyright 1992-2001 Free Software Foundation, Inc.
8 * The following terms apply to Digital Equipment Corporation's copyright
10 * ------------------------------------------------------------------------
13 * Permission to use, copy, modify, and distribute this software and its
14 * documentation for any purpose and without fee is hereby granted,
15 * provided that the above copyright notice appear in all copies and that
16 * both that copyright notice and this permission notice appear in
17 * supporting documentation, and that the name of Digital not be
18 * used in advertising or publicity pertaining to distribution of the
19 * software without specific, written prior permission.
21 * DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
22 * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
23 * DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
24 * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
25 * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
26 * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
28 * ------------------------------------------------------------------------
30 * The following terms apply to the enhanced version of XBoard distributed
31 * by the Free Software Foundation:
32 * ------------------------------------------------------------------------
33 * This program is free software; you can redistribute it and/or modify
34 * it under the terms of the GNU General Public License as published by
35 * the Free Software Foundation; either version 2 of the License, or
36 * (at your option) any later version.
38 * This program is distributed in the hope that it will be useful,
39 * but WITHOUT ANY WARRANTY; without even the implied warranty of
40 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
41 * GNU General Public License for more details.
43 * You should have received a copy of the GNU General Public License
44 * along with this program; if not, write to the Free Software
45 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
46 * ------------------------------------------------------------------------
48 * See the file ChangeLog for a revision history. */
55 #include <sys/types.h>
62 #else /* not STDC_HEADERS */
65 # else /* not HAVE_STRING_H */
67 # endif /* not HAVE_STRING_H */
68 #endif /* not STDC_HEADERS */
71 # include <sys/fcntl.h>
72 #else /* not HAVE_SYS_FCNTL_H */
75 # endif /* HAVE_FCNTL_H */
76 #endif /* not HAVE_SYS_FCNTL_H */
78 #if TIME_WITH_SYS_TIME
79 # include <sys/time.h>
83 # include <sys/time.h>
89 #if defined(_amigados) && !defined(__GNUC__)
94 extern int gettimeofday(struct timeval *, struct timezone *);
102 #include "frontend.h"
109 #include "backendz.h"
113 # define _(s) gettext (s)
114 # define N_(s) gettext_noop (s)
121 /* A point in time */
123 long sec; /* Assuming this is >= 32 bits */
124 int ms; /* Assuming this is >= 16 bits */
127 /* Search stats from chessprogram */
129 char movelist[2*MSG_SIZ]; /* Last PV we were sent */
130 int depth; /* Current search depth */
131 int nr_moves; /* Total nr of root moves */
132 int moves_left; /* Moves remaining to be searched */
133 char move_name[MOVE_LEN]; /* Current move being searched, if provided */
134 u64 nodes; /* # of nodes searched */
135 int time; /* Search time (centiseconds) */
136 int score; /* Score (centipawns) */
137 int got_only_move; /* If last msg was "(only move)" */
138 int got_fail; /* 0 - nothing, 1 - got "--", 2 - got "++" */
139 int ok_to_send; /* handshaking between send & recv */
140 int line_is_book; /* 1 if movelist is book moves */
141 int seen_stat; /* 1 if we've seen the stat01: line */
144 int establish P((void));
145 void read_from_player P((InputSourceRef isr, VOIDSTAR closure,
146 char *buf, int count, int error));
147 void read_from_ics P((InputSourceRef isr, VOIDSTAR closure,
148 char *buf, int count, int error));
149 void SendToICS P((char *s));
150 void SendToICSDelayed P((char *s, long msdelay));
151 void SendMoveToICS P((ChessMove moveType, int fromX, int fromY,
153 void InitPosition P((int redraw));
154 void HandleMachineMove P((char *message, ChessProgramState *cps));
155 int AutoPlayOneMove P((void));
156 int LoadGameOneMove P((ChessMove readAhead));
157 int LoadGameFromFile P((char *filename, int n, char *title, int useList));
158 int LoadPositionFromFile P((char *filename, int n, char *title));
159 int SavePositionToFile P((char *filename));
160 void ApplyMove P((int fromX, int fromY, int toX, int toY, int promoChar,
162 void MakeMove P((int fromX, int fromY, int toX, int toY, int promoChar));
163 void ShowMove P((int fromX, int fromY, int toX, int toY));
164 void FinishMove P((ChessMove moveType, int fromX, int fromY, int toX, int toY,
165 /*char*/int promoChar));
166 void BackwardInner P((int target));
167 void ForwardInner P((int target));
168 void GameEnds P((ChessMove result, char *resultDetails, int whosays));
169 void EditPositionDone P((void));
170 void PrintOpponents P((FILE *fp));
171 void PrintPosition P((FILE *fp, int move));
172 void StartChessProgram P((ChessProgramState *cps));
173 void SendToProgram P((char *message, ChessProgramState *cps));
174 void SendMoveToProgram P((int moveNum, ChessProgramState *cps));
175 void ReceiveFromProgram P((InputSourceRef isr, VOIDSTAR closure,
176 char *buf, int count, int error));
177 void SendTimeControl P((ChessProgramState *cps,
178 int mps, long tc, int inc, int sd, int st));
179 char *TimeControlTagValue P((void));
180 void Attention P((ChessProgramState *cps));
181 void FeedMovesToProgram P((ChessProgramState *cps, int upto));
182 void ResurrectChessProgram P((void));
183 void DisplayComment P((int moveNumber, char *text));
184 void DisplayMove P((int moveNumber));
185 void DisplayAnalysis P((void));
187 void ParseGameHistory P((char *game));
188 void ParseBoard12 P((char *string));
189 void StartClocks P((void));
190 void SwitchClocks P((void));
191 void StopClocks P((void));
192 void ResetClocks P((void));
193 char *PGNDate P((void));
194 void SetGameInfo P((void));
195 Boolean ParseFEN P((Board board, int *blackPlaysFirst, char *fen));
196 int RegisterMove P((void));
197 void MakeRegisteredMove P((void));
198 void TruncateGame P((void));
199 int looking_at P((char *, int *, char *));
200 void CopyPlayerNameIntoFileName P((char **, char *));
201 char *SavePart P((char *));
202 int SaveGameOldStyle P((FILE *));
203 int SaveGamePGN P((FILE *));
204 void GetTimeMark P((TimeMark *));
205 long SubtractTimeMarks P((TimeMark *, TimeMark *));
206 int CheckFlags P((void));
207 long NextTickLength P((long));
208 void CheckTimeControl P((void));
209 void show_bytes P((FILE *, char *, int));
210 int string_to_rating P((char *str));
211 void ParseFeatures P((char* args, ChessProgramState *cps));
212 void InitBackEnd3 P((void));
213 void FeatureDone P((ChessProgramState* cps, int val));
214 void InitChessProgram P((ChessProgramState *cps));
217 extern void ConsoleCreate();
\r
219 extern int tinyLayout, smallLayout;
220 static ChessProgramStats programStats;
222 /* States for ics_getting_history */
224 #define H_REQUESTED 1
225 #define H_GOT_REQ_HEADER 2
226 #define H_GOT_UNREQ_HEADER 3
227 #define H_GETTING_MOVES 4
228 #define H_GOT_UNWANTED_HEADER 5
230 /* whosays values for GameEnds */
237 /* Maximum number of games in a cmail message */
238 #define CMAIL_MAX_GAMES 20
240 /* Different types of move when calling RegisterMove */
242 #define CMAIL_RESIGN 1
244 #define CMAIL_ACCEPT 3
246 /* Different types of result to remember for each game */
247 #define CMAIL_NOT_RESULT 0
248 #define CMAIL_OLD_RESULT 1
249 #define CMAIL_NEW_RESULT 2
251 /* Telnet protocol constants */
261 /* Fake up flags for now, as we aren't keeping track of castling
266 int flags = F_ALL_CASTLE_OK;
267 if ((index % 2) == 0) flags |= F_WHITE_ON_MOVE;
268 switch (gameInfo.variant) {
270 case VariantGiveaway:
271 flags |= F_IGNORE_CHECK;
272 flags &= ~F_ALL_CASTLE_OK;
275 flags |= F_IGNORE_CHECK | F_ATOMIC_CAPTURE;
277 case VariantKriegspiel:
278 flags |= F_KRIEGSPIEL_CAPTURE;
280 case VariantNoCastle:
281 flags &= ~F_ALL_CASTLE_OK;
289 FILE *gameFileFP, *debugFP;
291 char cmailMove[CMAIL_MAX_GAMES][MOVE_LEN], cmailMsg[MSG_SIZ];
292 char bookOutput[MSG_SIZ*10], thinkOutput[MSG_SIZ*10], lastHint[MSG_SIZ];
293 char thinkOutput1[MSG_SIZ*10];
295 ChessProgramState first, second;
297 /* premove variables */
300 int premoveFromX = 0;
301 int premoveFromY = 0;
302 int premovePromoChar = 0;
304 Boolean alarmSounded;
305 /* end premove variables */
307 char *ics_prefix = "$";
308 ics_type = ICS_GENERIC;
\r
310 int currentMove = 0, forwardMostMove = 0, backwardMostMove = 0;
311 int pauseExamForwardMostMove = 0;
312 int nCmailGames = 0, nCmailResults = 0, nCmailMovesRegistered = 0;
313 int cmailMoveRegistered[CMAIL_MAX_GAMES], cmailResult[CMAIL_MAX_GAMES];
314 int cmailMsgLoaded = FALSE, cmailMailedMove = FALSE;
315 int cmailOldMove = -1, firstMove = TRUE, flipView = FALSE;
316 int blackPlaysFirst = FALSE, startedFromSetupPosition = FALSE;
317 int searchTime = 0, pausing = FALSE, pauseExamInvalid = FALSE;
318 int whiteFlag = FALSE, blackFlag = FALSE;
319 int userOfferedDraw = FALSE;
320 int ics_user_moved = 0, ics_gamenum = -1, ics_getting_history = H_FALSE;
321 int matchMode = FALSE, hintRequested = FALSE, bookRequested = FALSE;
322 int cmailMoveType[CMAIL_MAX_GAMES];
323 long ics_clock_paused = 0;
324 ProcRef icsPR = NoProc, cmailPR = NoProc;
325 InputSourceRef telnetISR = NULL, fromUserISR = NULL, cmailISR = NULL;
326 GameMode gameMode = BeginningOfGame;
327 char moveList[MAX_MOVES][MOVE_LEN], parseList[MAX_MOVES][MOVE_LEN * 2];
328 char *commentList[MAX_MOVES], *cmailCommentList[CMAIL_MAX_GAMES];
329 char white_holding[64], black_holding[64];
330 TimeMark lastNodeCountTime;
331 long lastNodeCount=0;
332 int have_sent_ICS_logon = 0;
334 long whiteTimeRemaining, blackTimeRemaining, timeControl, timeIncrement;
335 long timeRemaining[2][MAX_MOVES];
337 TimeMark programStartTime;
338 char ics_handle[MSG_SIZ];
339 int have_set_title = 0;
341 /* animateTraining preserves the state of appData.animate
342 * when Training mode is activated. This allows the
343 * response to be animated when appData.animate == TRUE and
344 * appData.animateDragging == TRUE.
346 Boolean animateTraining;
352 Board boards[MAX_MOVES];
353 Board initialPosition = {
354 { WhiteRook, WhiteKnight, WhiteBishop, WhiteQueen,
355 WhiteKing, WhiteBishop, WhiteKnight, WhiteRook },
356 { WhitePawn, WhitePawn, WhitePawn, WhitePawn,
357 WhitePawn, WhitePawn, WhitePawn, WhitePawn },
358 { EmptySquare, EmptySquare, EmptySquare, EmptySquare,
359 EmptySquare, EmptySquare, EmptySquare, EmptySquare },
360 { EmptySquare, EmptySquare, EmptySquare, EmptySquare,
361 EmptySquare, EmptySquare, EmptySquare, EmptySquare },
362 { EmptySquare, EmptySquare, EmptySquare, EmptySquare,
363 EmptySquare, EmptySquare, EmptySquare, EmptySquare },
364 { EmptySquare, EmptySquare, EmptySquare, EmptySquare,
365 EmptySquare, EmptySquare, EmptySquare, EmptySquare },
366 { BlackPawn, BlackPawn, BlackPawn, BlackPawn,
367 BlackPawn, BlackPawn, BlackPawn, BlackPawn },
368 { BlackRook, BlackKnight, BlackBishop, BlackQueen,
369 BlackKing, BlackBishop, BlackKnight, BlackRook }
371 Board twoKingsPosition = {
372 { WhiteRook, WhiteKnight, WhiteBishop, WhiteQueen,
373 WhiteKing, WhiteKing, WhiteKnight, WhiteRook },
374 { WhitePawn, WhitePawn, WhitePawn, WhitePawn,
375 WhitePawn, WhitePawn, WhitePawn, WhitePawn },
376 { EmptySquare, EmptySquare, EmptySquare, EmptySquare,
377 EmptySquare, EmptySquare, EmptySquare, EmptySquare },
378 { EmptySquare, EmptySquare, EmptySquare, EmptySquare,
379 EmptySquare, EmptySquare, EmptySquare, EmptySquare },
380 { EmptySquare, EmptySquare, EmptySquare, EmptySquare,
381 EmptySquare, EmptySquare, EmptySquare, EmptySquare },
382 { EmptySquare, EmptySquare, EmptySquare, EmptySquare,
383 EmptySquare, EmptySquare, EmptySquare, EmptySquare },
384 { BlackPawn, BlackPawn, BlackPawn, BlackPawn,
385 BlackPawn, BlackPawn, BlackPawn, BlackPawn },
386 { BlackRook, BlackKnight, BlackBishop, BlackQueen,
387 BlackKing, BlackKing, BlackKnight, BlackRook }
391 /* Convert str to a rating. Checks for special cases of "----",
392 "++++", etc. Also strips ()'s */
394 string_to_rating(str)
397 while(*str && !isdigit(*str)) ++str;
399 return 0; /* One of the special "no rating" cases */
407 /* Init programStats */
408 programStats.movelist[0] = 0;
409 programStats.depth = 0;
410 programStats.nr_moves = 0;
411 programStats.moves_left = 0;
412 programStats.nodes = 0;
413 programStats.time = 100;
414 programStats.score = 0;
415 programStats.got_only_move = 0;
416 programStats.got_fail = 0;
417 programStats.line_is_book = 0;
423 int matched, min, sec;
425 GetTimeMark(&programStartTime);
428 programStats.ok_to_send = 1;
429 programStats.seen_stat = 0;
432 * Initialize game list
438 * Internet chess server status
440 if (appData.icsActive) {
441 appData.matchMode = FALSE;
442 appData.matchGames = 0;
444 appData.noChessProgram = !appData.zippyPlay;
446 appData.zippyPlay = FALSE;
447 appData.zippyTalk = FALSE;
448 appData.noChessProgram = TRUE;
450 if (*appData.icsHelper != NULLCHAR) {
451 appData.useTelnet = TRUE;
452 appData.telnetProgram = appData.icsHelper;
455 appData.zippyTalk = appData.zippyPlay = FALSE;
459 * Parse timeControl resource
461 if (!ParseTimeControl(appData.timeControl, appData.timeIncrement,
462 appData.movesPerSession)) {
464 sprintf(buf, _("bad timeControl option %s"), appData.timeControl);
465 DisplayFatalError(buf, 0, 2);
469 * Parse searchTime resource
471 if (*appData.searchTime != NULLCHAR) {
472 matched = sscanf(appData.searchTime, "%d:%d", &min, &sec);
474 searchTime = min * 60;
475 } else if (matched == 2) {
476 searchTime = min * 60 + sec;
479 sprintf(buf, _("bad searchTime option %s"), appData.searchTime);
480 DisplayFatalError(buf, 0, 2);
484 first.which = "first";
485 second.which = "second";
486 first.maybeThinking = second.maybeThinking = FALSE;
487 first.pr = second.pr = NoProc;
488 first.isr = second.isr = NULL;
489 first.sendTime = second.sendTime = 2;
490 first.sendDrawOffers = 1;
491 if (appData.firstPlaysBlack) {
492 first.twoMachinesColor = "black\n";
493 second.twoMachinesColor = "white\n";
495 first.twoMachinesColor = "white\n";
496 second.twoMachinesColor = "black\n";
498 first.program = appData.firstChessProgram;
499 second.program = appData.secondChessProgram;
500 first.host = appData.firstHost;
501 second.host = appData.secondHost;
502 first.dir = appData.firstDirectory;
503 second.dir = appData.secondDirectory;
504 first.other = &second;
505 second.other = &first;
506 first.initString = appData.initString;
507 second.initString = appData.secondInitString;
508 first.computerString = appData.firstComputerString;
509 second.computerString = appData.secondComputerString;
510 first.useSigint = second.useSigint = TRUE;
511 first.useSigterm = second.useSigterm = TRUE;
512 first.reuse = appData.reuseFirst;
513 second.reuse = appData.reuseSecond;
514 first.useSetboard = second.useSetboard = FALSE;
515 first.useSAN = second.useSAN = FALSE;
516 first.usePing = second.usePing = FALSE;
517 first.lastPing = second.lastPing = 0;
518 first.lastPong = second.lastPong = 0;
519 first.usePlayother = second.usePlayother = FALSE;
520 first.useColors = second.useColors = TRUE;
521 first.useUsermove = second.useUsermove = FALSE;
522 first.sendICS = second.sendICS = FALSE;
523 first.sendName = second.sendName = appData.icsActive;
524 first.sdKludge = second.sdKludge = FALSE;
525 first.stKludge = second.stKludge = FALSE;
526 TidyProgramName(first.program, first.host, first.tidy);
527 TidyProgramName(second.program, second.host, second.tidy);
528 first.matchWins = second.matchWins = 0;
529 strcpy(first.variants, appData.variant);
530 strcpy(second.variants, appData.variant);
531 first.analysisSupport = second.analysisSupport = 2; /* detect */
532 first.analyzing = second.analyzing = FALSE;
533 first.initDone = second.initDone = FALSE;
535 if (appData.firstProtocolVersion > PROTOVER ||
536 appData.firstProtocolVersion < 1) {
538 sprintf(buf, _("protocol version %d not supported"),
539 appData.firstProtocolVersion);
540 DisplayFatalError(buf, 0, 2);
542 first.protocolVersion = appData.firstProtocolVersion;
545 if (appData.secondProtocolVersion > PROTOVER ||
546 appData.secondProtocolVersion < 1) {
548 sprintf(buf, _("protocol version %d not supported"),
549 appData.secondProtocolVersion);
550 DisplayFatalError(buf, 0, 2);
552 second.protocolVersion = appData.secondProtocolVersion;
555 if (appData.icsActive) {
556 appData.clockMode = TRUE; /* changes dynamically in ICS mode */
557 } else if (*appData.searchTime != NULLCHAR || appData.noChessProgram) {
558 appData.clockMode = FALSE;
559 first.sendTime = second.sendTime = 0;
563 /* Override some settings from environment variables, for backward
564 compatibility. Unfortunately it's not feasible to have the env
565 vars just set defaults, at least in xboard. Ugh.
567 if (appData.icsActive && (appData.zippyPlay || appData.zippyTalk)) {
572 if (appData.noChessProgram) {
573 programVersion = (char*) malloc(5 + strlen(PRODUCT) + strlen(VERSION)
574 + strlen(PATCHLEVEL));
575 sprintf(programVersion, "%s %s.%s", PRODUCT, VERSION, PATCHLEVEL);
579 while (*q != ' ' && *q != NULLCHAR) q++;
581 while (p > first.program && *(p-1) != '/') p--;
582 programVersion = (char*) malloc(8 + strlen(PRODUCT) + strlen(VERSION)
583 + strlen(PATCHLEVEL) + (q - p));
584 sprintf(programVersion, "%s %s.%s + ", PRODUCT, VERSION, PATCHLEVEL);
585 strncat(programVersion, p, q - p);
588 if (!appData.icsActive) {
590 /* Check for variants that are supported only in ICS mode,
591 or not at all. Some that are accepted here nevertheless
592 have bugs; see comments below.
594 VariantClass variant = StringToVariant(appData.variant);
596 case VariantBughouse: /* need four players and two boards */
597 case VariantKriegspiel: /* need to hide pieces and move details */
598 case VariantFischeRandom: /* castling doesn't work, shuffle not done */
599 sprintf(buf, _("Variant %s supported only in ICS mode"), appData.variant);
600 DisplayFatalError(buf, 0, 2);
604 case VariantLoadable:
614 sprintf(buf, _("Unknown variant name %s"), appData.variant);
615 DisplayFatalError(buf, 0, 2);
618 case VariantNormal: /* definitely works! */
619 case VariantWildCastle: /* pieces not automatically shuffled */
620 case VariantNoCastle: /* pieces not automatically shuffled */
621 case VariantCrazyhouse: /* holdings not shown,
622 offboard interposition not understood */
623 case VariantLosers: /* should work except for win condition,
624 and doesn't know captures are mandatory */
625 case VariantSuicide: /* should work except for win condition,
626 and doesn't know captures are mandatory */
627 case VariantGiveaway: /* should work except for win condition,
628 and doesn't know captures are mandatory */
629 case VariantTwoKings: /* should work */
630 case VariantAtomic: /* should work except for win condition */
631 case Variant3Check: /* should work except for win condition */
632 case VariantShatranj: /* might work if TestLegality is off */
639 ParseTimeControl(tc, ti, mps)
644 int matched, min, sec;
646 matched = sscanf(tc, "%d:%d", &min, &sec);
648 timeControl = min * 60 * 1000;
649 } else if (matched == 2) {
650 timeControl = (min * 60 + sec) * 1000;
656 timeIncrement = ti * 1000; /* convert to ms */
660 movesPerSession = mps;
668 if (appData.debugMode) {
669 fprintf(debugFP, "%s\n", programVersion);
672 if (appData.matchGames > 0) {
673 appData.matchMode = TRUE;
674 } else if (appData.matchMode) {
675 appData.matchGames = 1;
678 if (appData.noChessProgram || first.protocolVersion == 1) {
681 /* kludge: allow timeout for initial "feature" commands */
683 DisplayMessage("", "Starting chess program");
684 ScheduleDelayedEvent(InitBackEnd3, FEATURE_TIMEOUT);
689 InitBackEnd3 P((void))
691 GameMode initialMode;
695 InitChessProgram(&first);
\r
698 /* Make a console window if needed */
\r
699 if (appData.icsActive) ConsoleCreate();
\r
702 if (appData.icsActive) {
705 if (*appData.icsCommPort != NULLCHAR) {
706 sprintf(buf, _("Could not open comm port %s"),
707 appData.icsCommPort);
709 sprintf(buf, _("Could not connect to host %s, port %s"),
710 appData.icsHost, appData.icsPort);
712 DisplayFatalError(buf, err, 1);
717 AddInputSource(icsPR, FALSE, read_from_ics, &telnetISR);
719 AddInputSource(NoProc, FALSE, read_from_player, &fromUserISR);
720 } else if (appData.noChessProgram) {
726 if (*appData.cmailGameName != NULLCHAR) {
728 OpenLoopback(&cmailPR);
730 AddInputSource(cmailPR, FALSE, CmailSigHandlerCallBack, &cmailISR);
734 DisplayMessage("", "");
735 if (StrCaseCmp(appData.initialMode, "") == 0) {
736 initialMode = BeginningOfGame;
737 } else if (StrCaseCmp(appData.initialMode, "TwoMachines") == 0) {
738 initialMode = TwoMachinesPlay;
739 } else if (StrCaseCmp(appData.initialMode, "AnalyzeFile") == 0) {
740 initialMode = AnalyzeFile;
741 } else if (StrCaseCmp(appData.initialMode, "Analysis") == 0) {
742 initialMode = AnalyzeMode;
743 } else if (StrCaseCmp(appData.initialMode, "MachineWhite") == 0) {
744 initialMode = MachinePlaysWhite;
745 } else if (StrCaseCmp(appData.initialMode, "MachineBlack") == 0) {
746 initialMode = MachinePlaysBlack;
747 } else if (StrCaseCmp(appData.initialMode, "EditGame") == 0) {
748 initialMode = EditGame;
749 } else if (StrCaseCmp(appData.initialMode, "EditPosition") == 0) {
750 initialMode = EditPosition;
751 } else if (StrCaseCmp(appData.initialMode, "Training") == 0) {
752 initialMode = Training;
754 sprintf(buf, _("Unknown initialMode %s"), appData.initialMode);
755 DisplayFatalError(buf, 0, 2);
759 if (appData.matchMode) {
760 /* Set up machine vs. machine match */
761 if (appData.noChessProgram) {
762 DisplayFatalError(_("Can't have a match with no chess programs"),
768 if (*appData.loadGameFile != NULLCHAR) {
769 if (!LoadGameFromFile(appData.loadGameFile,
770 appData.loadGameIndex,
771 appData.loadGameFile, FALSE)) {
772 DisplayFatalError(_("Bad game file"), 0, 1);
775 } else if (*appData.loadPositionFile != NULLCHAR) {
776 if (!LoadPositionFromFile(appData.loadPositionFile,
777 appData.loadPositionIndex,
778 appData.loadPositionFile)) {
779 DisplayFatalError(_("Bad position file"), 0, 1);
784 } else if (*appData.cmailGameName != NULLCHAR) {
785 /* Set up cmail mode */
786 ReloadCmailMsgEvent(TRUE);
788 /* Set up other modes */
789 if (initialMode == AnalyzeFile) {
790 if (*appData.loadGameFile == NULLCHAR) {
791 DisplayFatalError(_("AnalyzeFile mode requires a game file"), 0, 1);
795 if (*appData.loadGameFile != NULLCHAR) {
796 (void) LoadGameFromFile(appData.loadGameFile,
797 appData.loadGameIndex,
798 appData.loadGameFile, TRUE);
799 } else if (*appData.loadPositionFile != NULLCHAR) {
800 (void) LoadPositionFromFile(appData.loadPositionFile,
801 appData.loadPositionIndex,
802 appData.loadPositionFile);
804 if (initialMode == AnalyzeMode) {
805 if (appData.noChessProgram) {
806 DisplayFatalError(_("Analysis mode requires a chess engine"), 0, 2);
809 if (appData.icsActive) {
810 DisplayFatalError(_("Analysis mode does not work with ICS mode"),0,2);
814 } else if (initialMode == AnalyzeFile) {
815 ShowThinkingEvent(TRUE);
817 AnalysisPeriodicEvent(1);
818 } else if (initialMode == MachinePlaysWhite) {
819 if (appData.noChessProgram) {
820 DisplayFatalError(_("MachineWhite mode requires a chess engine"),
824 if (appData.icsActive) {
825 DisplayFatalError(_("MachineWhite mode does not work with ICS mode"),
830 } else if (initialMode == MachinePlaysBlack) {
831 if (appData.noChessProgram) {
832 DisplayFatalError(_("MachineBlack mode requires a chess engine"),
836 if (appData.icsActive) {
837 DisplayFatalError(_("MachineBlack mode does not work with ICS mode"),
842 } else if (initialMode == TwoMachinesPlay) {
843 if (appData.noChessProgram) {
844 DisplayFatalError(_("TwoMachines mode requires a chess engine"),
848 if (appData.icsActive) {
849 DisplayFatalError(_("TwoMachines mode does not work with ICS mode"),
854 } else if (initialMode == EditGame) {
856 } else if (initialMode == EditPosition) {
858 } else if (initialMode == Training) {
859 if (*appData.loadGameFile == NULLCHAR) {
860 DisplayFatalError(_("Training mode requires a game file"), 0, 2);
869 * Establish will establish a contact to a remote host.port.
870 * Sets icsPR to a ProcRef for a process (or pseudo-process)
871 * used to talk to the host.
872 * Returns 0 if okay, error code if not.
879 if (*appData.icsCommPort != NULLCHAR) {
880 /* Talk to the host through a serial comm port */
881 return OpenCommPort(appData.icsCommPort, &icsPR);
883 } else if (*appData.gateway != NULLCHAR) {
884 if (*appData.remoteShell == NULLCHAR) {
885 /* Use the rcmd protocol to run telnet program on a gateway host */
886 sprintf(buf, "%s %s %s",
887 appData.telnetProgram, appData.icsHost, appData.icsPort);
888 return OpenRcmd(appData.gateway, appData.remoteUser, buf, &icsPR);
891 /* Use the rsh program to run telnet program on a gateway host */
892 if (*appData.remoteUser == NULLCHAR) {
893 sprintf(buf, "%s %s %s %s %s", appData.remoteShell,
894 appData.gateway, appData.telnetProgram,
895 appData.icsHost, appData.icsPort);
897 sprintf(buf, "%s %s -l %s %s %s %s",
898 appData.remoteShell, appData.gateway,
899 appData.remoteUser, appData.telnetProgram,
900 appData.icsHost, appData.icsPort);
902 return StartChildProcess(buf, "", &icsPR);
905 } else if (appData.useTelnet) {
906 return OpenTelnet(appData.icsHost, appData.icsPort, &icsPR);
909 /* TCP socket interface differs somewhat between
910 Unix and NT; handle details in the front end.
912 return OpenTCP(appData.icsHost, appData.icsPort, &icsPR);
917 show_bytes(fp, buf, count)
923 if (*buf < 040 || *(unsigned char *) buf > 0177) {
924 fprintf(fp, "\\%03o", *buf & 0xff);
933 /* Returns an errno value */
935 OutputMaybeTelnet(pr, message, count, outError)
941 char buf[8192], *p, *q, *buflim;
942 int left, newcount, outcount;
944 if (*appData.icsCommPort != NULLCHAR || appData.useTelnet ||
945 *appData.gateway != NULLCHAR) {
946 if (appData.debugMode) {
947 fprintf(debugFP, ">ICS: ");
948 show_bytes(debugFP, message, count);
949 fprintf(debugFP, "\n");
951 return OutputToProcess(pr, message, count, outError);
954 buflim = &buf[sizeof(buf)-1]; /* allow 1 byte for expanding last char */
961 if (appData.debugMode) {
962 fprintf(debugFP, ">ICS: ");
963 show_bytes(debugFP, buf, newcount);
964 fprintf(debugFP, "\n");
966 outcount = OutputToProcess(pr, buf, newcount, outError);
967 if (outcount < newcount) return -1; /* to be sure */
974 } else if (((unsigned char) *p) == TN_IAC) {
975 *q++ = (char) TN_IAC;
982 if (appData.debugMode) {
983 fprintf(debugFP, ">ICS: ");
984 show_bytes(debugFP, buf, newcount);
985 fprintf(debugFP, "\n");
987 outcount = OutputToProcess(pr, buf, newcount, outError);
988 if (outcount < newcount) return -1; /* to be sure */
993 read_from_player(isr, closure, message, count, error)
1000 int outError, outCount;
1001 static int gotEof = 0;
1003 /* Pass data read from player on to ICS */
1006 outCount = OutputMaybeTelnet(icsPR, message, count, &outError);
1007 if (outCount < count) {
1008 DisplayFatalError(_("Error writing to ICS"), outError, 1);
1010 } else if (count < 0) {
1011 RemoveInputSource(isr);
1012 DisplayFatalError(_("Error reading from keyboard"), error, 1);
1013 } else if (gotEof++ > 0) {
1014 RemoveInputSource(isr);
1015 DisplayFatalError(_("Got end of file from keyboard"), 0, 0);
1023 int count, outCount, outError;
1025 if (icsPR == NULL) return;
1028 outCount = OutputMaybeTelnet(icsPR, s, count, &outError);
1029 if (outCount < count) {
1030 DisplayFatalError(_("Error writing to ICS"), outError, 1);
1034 /* This is used for sending logon scripts to the ICS. Sending
1035 without a delay causes problems when using timestamp on ICC
1036 (at least on my machine). */
1038 SendToICSDelayed(s,msdelay)
1042 int count, outCount, outError;
1044 if (icsPR == NULL) return;
1047 if (appData.debugMode) {
1048 fprintf(debugFP, ">ICS: ");
1049 show_bytes(debugFP, s, count);
1050 fprintf(debugFP, "\n");
1052 outCount = OutputToProcessDelayed(icsPR, s, count, &outError,
1054 if (outCount < count) {
1055 DisplayFatalError(_("Error writing to ICS"), outError, 1);
1060 /* Remove all highlighting escape sequences in s
1061 Also deletes any suffix starting with '('
1064 StripHighlightAndTitle(s)
1067 static char retbuf[MSG_SIZ];
1070 while (*s != NULLCHAR) {
1071 while (*s == '\033') {
1072 while (*s != NULLCHAR && !isalpha(*s)) s++;
1073 if (*s != NULLCHAR) s++;
1075 while (*s != NULLCHAR && *s != '\033') {
1076 if (*s == '(' || *s == '[') {
1087 /* Remove all highlighting escape sequences in s */
1092 static char retbuf[MSG_SIZ];
1095 while (*s != NULLCHAR) {
1096 while (*s == '\033') {
1097 while (*s != NULLCHAR && !isalpha(*s)) s++;
1098 if (*s != NULLCHAR) s++;
1100 while (*s != NULLCHAR && *s != '\033') {
1108 char *variantNames[] = VARIANT_NAMES;
1113 return variantNames[v];
1117 /* Identify a variant from the strings the chess servers use or the
1118 PGN Variant tag names we use. */
1125 VariantClass v = VariantNormal;
1126 int i, found = FALSE;
1131 for (i=0; i<sizeof(variantNames)/sizeof(char*); i++) {
1132 if (StrCaseStr(e, variantNames[i])) {
1133 v = (VariantClass) i;
1140 if ((StrCaseStr(e, "fischer") && StrCaseStr(e, "random"))
1141 || StrCaseStr(e, "wild/fr")) {
1142 v = VariantFischeRandom;
1143 } else if ((i = 4, p = StrCaseStr(e, "wild")) ||
1144 (i = 1, p = StrCaseStr(e, "w"))) {
1146 while (*p && (isspace(*p) || *p == '(' || *p == '/')) p++;
1153 case 0: /* FICS only, actually */
1155 /* Castling legal even if K starts on d-file */
1156 v = VariantWildCastle;
1161 /* Castling illegal even if K & R happen to start in
1162 normal positions. */
1163 v = VariantNoCastle;
1176 /* Castling legal iff K & R start in normal positions */
1182 /* Special wilds for position setup; unclear what to do here */
1183 v = VariantLoadable;
1186 /* Bizarre ICC game */
1187 v = VariantTwoKings;
1190 v = VariantKriegspiel;
1196 v = VariantFischeRandom;
1199 v = VariantCrazyhouse;
1202 v = VariantBughouse;
1208 /* Not quite the same as FICS suicide! */
1209 v = VariantGiveaway;
1215 v = VariantShatranj;
1218 /* Temporary names for future ICC types. The name *will* change in
1219 the next xboard/WinBoard release after ICC defines it. */
1246 /* Found "wild" or "w" in the string but no number;
1247 must assume it's normal chess. */
1251 sprintf(buf, "Unknown wild type %d", wnum);
1252 DisplayError(buf, 0);
1258 if (appData.debugMode) {
1259 fprintf(debugFP, _("recognized '%s' (%d) as variant %s\n"),
1260 e, wnum, VariantName(v));
1265 static int leftover_start = 0, leftover_len = 0;
1266 char star_match[STAR_MATCH_N][MSG_SIZ];
1268 /* Test whether pattern is present at &buf[*index]; if so, return TRUE,
1269 advance *index beyond it, and set leftover_start to the new value of
1270 *index; else return FALSE. If pattern contains the character '*', it
1271 matches any sequence of characters not containing '\r', '\n', or the
1272 character following the '*' (if any), and the matched sequence(s) are
1273 copied into star_match.
1276 looking_at(buf, index, pattern)
1281 char *bufp = &buf[*index], *patternp = pattern;
1283 char *matchp = star_match[0];
1286 if (*patternp == NULLCHAR) {
1287 *index = leftover_start = bufp - buf;
1291 if (*bufp == NULLCHAR) return FALSE;
1292 if (*patternp == '*') {
1293 if (*bufp == *(patternp + 1)) {
1295 matchp = star_match[++star_count];
1299 } else if (*bufp == '\n' || *bufp == '\r') {
1301 if (*patternp == NULLCHAR)
1306 *matchp++ = *bufp++;
1310 if (*patternp != *bufp) return FALSE;
1317 SendToPlayer(data, length)
1321 int error, outCount;
1322 outCount = OutputToProcess(NoProc, data, length, &error);
1323 if (outCount < length) {
1324 DisplayFatalError(_("Error writing to display"), error, 1);
1329 PackHolding(packed, holding)
1341 switch (runlength) {
1352 sprintf(q, "%d", runlength);
1364 /* Telnet protocol requests from the front end */
1366 TelnetRequest(ddww, option)
1367 unsigned char ddww, option;
1369 unsigned char msg[3];
1370 int outCount, outError;
1372 if (*appData.icsCommPort != NULLCHAR || appData.useTelnet) return;
1374 if (appData.debugMode) {
1375 char buf1[8], buf2[8], *ddwwStr, *optionStr;
1391 sprintf(buf1, "%d", ddww);
1400 sprintf(buf2, "%d", option);
1403 fprintf(debugFP, ">%s %s ", ddwwStr, optionStr);
1408 outCount = OutputToProcess(icsPR, (char *)msg, 3, &outError);
1410 DisplayFatalError(_("Error writing to ICS"), outError, 1);
1417 if (!appData.icsActive) return;
1418 TelnetRequest(TN_DO, TN_ECHO);
1424 if (!appData.icsActive) return;
1425 TelnetRequest(TN_DONT, TN_ECHO);
1428 static int loggedOn = FALSE;
1430 /*-- Game start info cache: --*/
1432 char gs_kind[MSG_SIZ];
1433 static char player1Name[128] = "";
1434 static char player2Name[128] = "";
1435 static int player1Rating = -1;
1436 static int player2Rating = -1;
1437 /*----------------------------*/
1440 read_from_ics(isr, closure, data, count, error)
1447 #define BUF_SIZE 8192
1448 #define STARTED_NONE 0
1449 #define STARTED_MOVES 1
1450 #define STARTED_BOARD 2
1451 #define STARTED_OBSERVE 3
1452 #define STARTED_HOLDINGS 4
1453 #define STARTED_CHATTER 5
1454 #define STARTED_COMMENT 6
1455 #define STARTED_MOVES_NOHIDE 7
1457 static int started = STARTED_NONE;
1458 static char parse[20000];
1459 static int parse_pos = 0;
1460 static char buf[BUF_SIZE + 1];
1461 static int firstTime = TRUE, intfSet = FALSE;
1462 static ColorClass curColor = ColorNormal;
1463 static ColorClass prevColor = ColorNormal;
1464 static int savingComment = FALSE;
1471 /* For zippy color lines of winboard
\r
1472 * cleanup for gcc compiler */
\r
1478 if (appData.debugMode) {
1480 fprintf(debugFP, "<ICS: ");
1481 show_bytes(debugFP, data, count);
1482 fprintf(debugFP, "\n");
1488 /* If last read ended with a partial line that we couldn't parse,
1489 prepend it to the new read and try again. */
1490 if (leftover_len > 0) {
1491 for (i=0; i<leftover_len; i++)
1492 buf[i] = buf[leftover_start + i];
1495 /* Copy in new characters, removing nulls and \r's */
1496 buf_len = leftover_len;
1497 for (i = 0; i < count; i++) {
1498 if (data[i] != NULLCHAR && data[i] != '\r')
1499 buf[buf_len++] = data[i];
1502 buf[buf_len] = NULLCHAR;
1503 next_out = leftover_len;
1507 while (i < buf_len) {
1508 /* Deal with part of the TELNET option negotiation
1509 protocol. We refuse to do anything beyond the
1510 defaults, except that we allow the WILL ECHO option,
1511 which ICS uses to turn off password echoing when we are
1512 directly connected to it. We reject this option
1513 if localLineEditing mode is on (always on in xboard)
1514 and we are talking to port 23, which might be a real
1515 telnet server that will try to keep WILL ECHO on permanently.
1517 if (buf_len - i >= 3 && (unsigned char) buf[i] == TN_IAC) {
1518 static int remoteEchoOption = FALSE; /* telnet ECHO option */
1519 unsigned char option;
1521 switch ((unsigned char) buf[++i]) {
1523 if (appData.debugMode)
1524 fprintf(debugFP, "\n<WILL ");
1525 switch (option = (unsigned char) buf[++i]) {
1527 if (appData.debugMode)
1528 fprintf(debugFP, "ECHO ");
1529 /* Reply only if this is a change, according
1530 to the protocol rules. */
1531 if (remoteEchoOption) break;
1532 if (appData.localLineEditing &&
1533 atoi(appData.icsPort) == TN_PORT) {
1534 TelnetRequest(TN_DONT, TN_ECHO);
1537 TelnetRequest(TN_DO, TN_ECHO);
1538 remoteEchoOption = TRUE;
1542 if (appData.debugMode)
1543 fprintf(debugFP, "%d ", option);
1544 /* Whatever this is, we don't want it. */
1545 TelnetRequest(TN_DONT, option);
1550 if (appData.debugMode)
1551 fprintf(debugFP, "\n<WONT ");
1552 switch (option = (unsigned char) buf[++i]) {
1554 if (appData.debugMode)
1555 fprintf(debugFP, "ECHO ");
1556 /* Reply only if this is a change, according
1557 to the protocol rules. */
1558 if (!remoteEchoOption) break;
1560 TelnetRequest(TN_DONT, TN_ECHO);
1561 remoteEchoOption = FALSE;
1564 if (appData.debugMode)
1565 fprintf(debugFP, "%d ", (unsigned char) option);
1566 /* Whatever this is, it must already be turned
1567 off, because we never agree to turn on
1568 anything non-default, so according to the
1569 protocol rules, we don't reply. */
1574 if (appData.debugMode)
1575 fprintf(debugFP, "\n<DO ");
1576 switch (option = (unsigned char) buf[++i]) {
1578 /* Whatever this is, we refuse to do it. */
1579 if (appData.debugMode)
1580 fprintf(debugFP, "%d ", option);
1581 TelnetRequest(TN_WONT, option);
1586 if (appData.debugMode)
1587 fprintf(debugFP, "\n<DONT ");
1588 switch (option = (unsigned char) buf[++i]) {
1590 if (appData.debugMode)
1591 fprintf(debugFP, "%d ", option);
1592 /* Whatever this is, we are already not doing
1593 it, because we never agree to do anything
1594 non-default, so according to the protocol
1595 rules, we don't reply. */
1600 if (appData.debugMode)
1601 fprintf(debugFP, "\n<IAC ");
1602 /* Doubled IAC; pass it through */
1606 if (appData.debugMode)
1607 fprintf(debugFP, "\n<%d ", (unsigned char) buf[i]);
1608 /* Drop all other telnet commands on the floor */
1611 if (oldi > next_out)
1612 SendToPlayer(&buf[next_out], oldi - next_out);
1618 /* OK, this at least will *usually* work */
1619 if (!loggedOn && looking_at(buf, &i, "ics%")) {
1623 if (loggedOn && !intfSet) {
1624 if (ics_type == ICS_ICC) {
1626 "/set-quietly interface %s\n/set-quietly style 12\n",
1629 } else if (ics_type == ICS_CHESSNET) {
1630 sprintf(str, "/style 12\n");
1632 strcpy(str, "alias $ @\n$set interface ");
1633 strcat(str, programVersion);
1634 strcat(str, "\n$iset startpos 1\n$iset ms 1\n");
1636 strcat(str, "$iset nohighlight 1\n");
1638 strcat(str, "$iset lock 1\n$style 12\n");
1644 if (started == STARTED_COMMENT) {
1645 /* Accumulate characters in comment */
1646 parse[parse_pos++] = buf[i];
1647 if (buf[i] == '\n') {
1648 parse[parse_pos] = NULLCHAR;
1649 AppendComment(forwardMostMove, StripHighlight(parse));
1650 started = STARTED_NONE;
1652 /* Don't match patterns against characters in chatter */
1657 if (started == STARTED_CHATTER) {
1658 if (buf[i] != '\n') {
1659 /* Don't match patterns against characters in chatter */
1663 started = STARTED_NONE;
1666 /* Kludge to deal with rcmd protocol */
1667 if (firstTime && looking_at(buf, &i, "\001*")) {
1668 DisplayFatalError(&buf[1], 0, 1);
1674 if (!loggedOn && looking_at(buf, &i, "chessclub.com")) {
1677 if (appData.debugMode)
1678 fprintf(debugFP, "ics_type %d\n", ics_type);
1681 if (!loggedOn && looking_at(buf, &i, "freechess.org")) {
1682 ics_type = ICS_FICS;
1684 if (appData.debugMode)
1685 fprintf(debugFP, "ics_type %d\n", ics_type);
1688 if (!loggedOn && looking_at(buf, &i, "chess.net")) {
1689 ics_type = ICS_CHESSNET;
1691 if (appData.debugMode)
1692 fprintf(debugFP, "ics_type %d\n", ics_type);
1697 (looking_at(buf, &i, "\"*\" is *a registered name") ||
1698 looking_at(buf, &i, "Logging you in as \"*\"") ||
1699 looking_at(buf, &i, "will be \"*\""))) {
1700 strcpy(ics_handle, star_match[0]);
1704 if (loggedOn && !have_set_title && ics_handle[0] != NULLCHAR) {
1706 sprintf(buf, "%s@%s", ics_handle, appData.icsHost);
1707 DisplayIcsInteractionTitle(buf);
1708 have_set_title = TRUE;
1711 /* skip finger notes */
1712 if (started == STARTED_NONE &&
1713 ((buf[i] == ' ' && isdigit(buf[i+1])) ||
1714 (buf[i] == '1' && buf[i+1] == '0')) &&
1715 buf[i+2] == ':' && buf[i+3] == ' ') {
1716 started = STARTED_CHATTER;
1721 /* skip formula vars */
1722 if (started == STARTED_NONE &&
1723 buf[i] == 'f' && isdigit(buf[i+1]) && buf[i+2] == ':') {
1724 started = STARTED_CHATTER;
1730 if (appData.zippyTalk || appData.zippyPlay) {
1733 /* Backup address for color zippy lines */
\r
1735 if (loggedOn == TRUE)
\r
1736 if (ZippyControl(buf, &backup) || ZippyConverse(buf, &backup) ||
\r
1737 (appData.zippyPlay && ZippyMatch(buf, &backup)));
\r
1739 if (ZippyControl(buf, &i) ||
1740 ZippyConverse(buf, &i) ||
1741 (appData.zippyPlay && ZippyMatch(buf, &i))) {
1748 if (/* Don't color "message" or "messages" output */
1749 (tkind = 5, looking_at(buf, &i, "*. * (*:*): ")) ||
1750 looking_at(buf, &i, "*. * at *:*: ") ||
1751 looking_at(buf, &i, "--* (*:*): ") ||
1752 /* Regular tells and says */
1753 (tkind = 1, looking_at(buf, &i, "* tells you: ")) ||
1754 looking_at(buf, &i, "* (your partner) tells you: ") ||
1755 looking_at(buf, &i, "* says: ") ||
1756 /* Message notifications (same color as tells) */
1757 looking_at(buf, &i, "* has left a message ") ||
1758 looking_at(buf, &i, "* just sent you a message:\n") ||
1759 /* Whispers and kibitzes */
1760 (tkind = 2, looking_at(buf, &i, "* whispers: ")) ||
1761 looking_at(buf, &i, "* kibitzes: ") ||
1763 (tkind = 3, looking_at(buf, &i, "*(*: "))) {
1765 if (tkind == 1 && strchr(star_match[0], ':')) {
1766 /* Avoid "tells you:" spoofs in channels */
1769 if (star_match[0][0] == NULLCHAR ||
1770 strchr(star_match[0], ' ') ||
1771 (tkind == 3 && strchr(star_match[1], ' '))) {
1772 /* Reject bogus matches */
1775 if (appData.colorize) {
1776 if (oldi > next_out) {
1777 SendToPlayer(&buf[next_out], oldi - next_out);
1782 Colorize(ColorTell, FALSE);
1783 curColor = ColorTell;
1786 Colorize(ColorKibitz, FALSE);
1787 curColor = ColorKibitz;
1790 p = strrchr(star_match[1], '(');
1797 Colorize(ColorChannel1, FALSE);
1798 curColor = ColorChannel1;
1800 Colorize(ColorChannel, FALSE);
1801 curColor = ColorChannel;
1805 curColor = ColorNormal;
1809 if (started == STARTED_NONE && appData.autoComment &&
1810 (gameMode == IcsObserving ||
1811 gameMode == IcsPlayingWhite ||
1812 gameMode == IcsPlayingBlack)) {
1813 parse_pos = i - oldi;
1814 memcpy(parse, &buf[oldi], parse_pos);
1815 parse[parse_pos] = NULLCHAR;
1816 started = STARTED_COMMENT;
1817 savingComment = TRUE;
1819 started = STARTED_CHATTER;
1820 savingComment = FALSE;
1827 if (looking_at(buf, &i, "* s-shouts: ") ||
1828 looking_at(buf, &i, "* c-shouts: ")) {
1829 if (appData.colorize) {
1830 if (oldi > next_out) {
1831 SendToPlayer(&buf[next_out], oldi - next_out);
1834 Colorize(ColorSShout, FALSE);
1835 curColor = ColorSShout;
1838 started = STARTED_CHATTER;
1842 if (looking_at(buf, &i, "--->")) {
1847 if (looking_at(buf, &i, "* shouts: ") ||
1848 looking_at(buf, &i, "--> ")) {
1849 if (appData.colorize) {
1850 if (oldi > next_out) {
1851 SendToPlayer(&buf[next_out], oldi - next_out);
1854 Colorize(ColorShout, FALSE);
1855 curColor = ColorShout;
1858 started = STARTED_CHATTER;
1862 if (looking_at( buf, &i, "Challenge:")) {
1863 if (appData.colorize) {
1864 if (oldi > next_out) {
1865 SendToPlayer(&buf[next_out], oldi - next_out);
1868 Colorize(ColorChallenge, FALSE);
1869 curColor = ColorChallenge;
1875 if (looking_at(buf, &i, "* offers you") ||
1876 looking_at(buf, &i, "* offers to be") ||
1877 looking_at(buf, &i, "* would like to") ||
1878 looking_at(buf, &i, "* requests to") ||
1879 looking_at(buf, &i, "Your opponent offers") ||
1880 looking_at(buf, &i, "Your opponent requests")) {
1882 if (appData.colorize) {
1883 if (oldi > next_out) {
1884 SendToPlayer(&buf[next_out], oldi - next_out);
1887 Colorize(ColorRequest, FALSE);
1888 curColor = ColorRequest;
1893 if (looking_at(buf, &i, "* (*) seeking")) {
1894 if (appData.colorize) {
1895 if (oldi > next_out) {
1896 SendToPlayer(&buf[next_out], oldi - next_out);
1899 Colorize(ColorSeek, FALSE);
1900 curColor = ColorSeek;
1905 if (looking_at(buf, &i, "\\ ")) {
1906 if (prevColor != ColorNormal) {
1907 if (oldi > next_out) {
1908 SendToPlayer(&buf[next_out], oldi - next_out);
1911 Colorize(prevColor, TRUE);
1912 curColor = prevColor;
1914 if (savingComment) {
1915 parse_pos = i - oldi;
1916 memcpy(parse, &buf[oldi], parse_pos);
1917 parse[parse_pos] = NULLCHAR;
1918 started = STARTED_COMMENT;
1920 started = STARTED_CHATTER;
1925 if (looking_at(buf, &i, "Black Strength :") ||
1926 looking_at(buf, &i, "<<< style 10 board >>>") ||
1927 looking_at(buf, &i, "<10>") ||
1928 looking_at(buf, &i, "#@#")) {
1929 /* Wrong board style */
1931 SendToICS(ics_prefix);
1932 SendToICS("set style 12\n");
1933 SendToICS(ics_prefix);
1934 SendToICS("refresh\n");
1938 if (!have_sent_ICS_logon && looking_at(buf, &i, "login:")) {
1940 have_sent_ICS_logon = 1;
1944 if (ics_getting_history != H_GETTING_MOVES /*smpos kludge*/ &&
1945 (looking_at(buf, &i, "\n<12> ") ||
1946 looking_at(buf, &i, "<12> "))) {
1948 if (oldi > next_out) {
1949 SendToPlayer(&buf[next_out], oldi - next_out);
1952 started = STARTED_BOARD;
1957 if ((started == STARTED_NONE && looking_at(buf, &i, "\n<b1> ")) ||
1958 looking_at(buf, &i, "<b1> ")) {
1959 if (oldi > next_out) {
1960 SendToPlayer(&buf[next_out], oldi - next_out);
1963 started = STARTED_HOLDINGS;
1968 if (looking_at(buf, &i, "* *vs. * *--- *")) {
1970 /* Header for a move list -- first line */
1972 switch (ics_getting_history) {
1976 case BeginningOfGame:
1977 /* User typed "moves" or "oldmoves" while we
1978 were idle. Pretend we asked for these
1979 moves and soak them up so user can step
1980 through them and/or save them.
1983 gameMode = IcsObserving;
1986 ics_getting_history = H_GOT_UNREQ_HEADER;
1988 case EditGame: /*?*/
1989 case EditPosition: /*?*/
1990 /* Should above feature work in these modes too? */
1991 /* For now it doesn't */
1992 ics_getting_history = H_GOT_UNWANTED_HEADER;
1995 ics_getting_history = H_GOT_UNWANTED_HEADER;
2000 /* Is this the right one? */
2001 if (gameInfo.white && gameInfo.black &&
2002 strcmp(gameInfo.white, star_match[0]) == 0 &&
2003 strcmp(gameInfo.black, star_match[2]) == 0) {
2005 ics_getting_history = H_GOT_REQ_HEADER;
2008 case H_GOT_REQ_HEADER:
2009 case H_GOT_UNREQ_HEADER:
2010 case H_GOT_UNWANTED_HEADER:
2011 case H_GETTING_MOVES:
2012 /* Should not happen */
2013 DisplayError(_("Error gathering move list: two headers"), 0);
2014 ics_getting_history = H_FALSE;
2018 /* Save player ratings into gameInfo if needed */
2019 if ((ics_getting_history == H_GOT_REQ_HEADER ||
2020 ics_getting_history == H_GOT_UNREQ_HEADER) &&
2021 (gameInfo.whiteRating == -1 ||
2022 gameInfo.blackRating == -1)) {
2024 gameInfo.whiteRating = string_to_rating(star_match[1]);
2025 gameInfo.blackRating = string_to_rating(star_match[3]);
2026 if (appData.debugMode)
2027 fprintf(debugFP, _("Ratings from header: W %d, B %d\n"),
2028 gameInfo.whiteRating, gameInfo.blackRating);
2033 if (looking_at(buf, &i,
2034 "* * match, initial time: * minute*, increment: * second")) {
2035 /* Header for a move list -- second line */
2036 /* Initial board will follow if this is a wild game */
2038 if (gameInfo.event != NULL) free(gameInfo.event);
2039 sprintf(str, "ICS %s %s match", star_match[0], star_match[1]);
2040 gameInfo.event = StrSave(str);
2041 gameInfo.variant = StringToVariant(gameInfo.event);
2045 if (looking_at(buf, &i, "Move ")) {
2046 /* Beginning of a move list */
2047 switch (ics_getting_history) {
2049 /* Normally should not happen */
2050 /* Maybe user hit reset while we were parsing */
2053 /* Happens if we are ignoring a move list that is not
2054 * the one we just requested. Common if the user
2055 * tries to observe two games without turning off
2058 case H_GETTING_MOVES:
2059 /* Should not happen */
2060 DisplayError(_("Error gathering move list: nested"), 0);
2061 ics_getting_history = H_FALSE;
2063 case H_GOT_REQ_HEADER:
2064 ics_getting_history = H_GETTING_MOVES;
2065 started = STARTED_MOVES;
2067 if (oldi > next_out) {
2068 SendToPlayer(&buf[next_out], oldi - next_out);
2071 case H_GOT_UNREQ_HEADER:
2072 ics_getting_history = H_GETTING_MOVES;
2073 started = STARTED_MOVES_NOHIDE;
2076 case H_GOT_UNWANTED_HEADER:
2077 ics_getting_history = H_FALSE;
2083 if (looking_at(buf, &i, "% ") ||
2084 ((started == STARTED_MOVES || started == STARTED_MOVES_NOHIDE)
2085 && looking_at(buf, &i, "}*"))) {
2086 savingComment = FALSE;
2089 case STARTED_MOVES_NOHIDE:
2090 memcpy(&parse[parse_pos], &buf[oldi], i - oldi);
2091 parse[parse_pos + i - oldi] = NULLCHAR;
2092 ParseGameHistory(parse);
2094 if (appData.zippyPlay && first.initDone) {
2095 FeedMovesToProgram(&first, forwardMostMove);
2096 if (gameMode == IcsPlayingWhite) {
2097 if (WhiteOnMove(forwardMostMove)) {
2098 if (first.sendTime) {
2099 if (first.useColors) {
2100 SendToProgram("black\n", &first);
2102 SendTimeRemaining(&first, TRUE);
2104 if (first.useColors) {
2105 SendToProgram("white\ngo\n", &first);
2107 SendToProgram("go\n", &first);
2109 first.maybeThinking = TRUE;
2111 if (first.usePlayother) {
2112 if (first.sendTime) {
2113 SendTimeRemaining(&first, TRUE);
2115 SendToProgram("playother\n", &first);
2121 } else if (gameMode == IcsPlayingBlack) {
2122 if (!WhiteOnMove(forwardMostMove)) {
2123 if (first.sendTime) {
2124 if (first.useColors) {
2125 SendToProgram("white\n", &first);
2127 SendTimeRemaining(&first, FALSE);
2129 if (first.useColors) {
2130 SendToProgram("black\ngo\n", &first);
2132 SendToProgram("go\n", &first);
2134 first.maybeThinking = TRUE;
2136 if (first.usePlayother) {
2137 if (first.sendTime) {
2138 SendTimeRemaining(&first, FALSE);
2140 SendToProgram("playother\n", &first);
2149 if (gameMode == IcsObserving && ics_gamenum == -1) {
2150 /* Moves came from oldmoves or moves command
2151 while we weren't doing anything else.
2153 currentMove = forwardMostMove;
2154 ClearHighlights();/*!!could figure this out*/
2155 flipView = appData.flipView;
2156 DrawPosition(FALSE, boards[currentMove]);
2157 DisplayBothClocks();
2158 sprintf(str, "%s vs. %s",
2159 gameInfo.white, gameInfo.black);
2163 /* Moves were history of an active game */
2164 if (gameInfo.resultDetails != NULL) {
2165 free(gameInfo.resultDetails);
2166 gameInfo.resultDetails = NULL;
2169 HistorySet(parseList, backwardMostMove,
2170 forwardMostMove, currentMove-1);
2171 DisplayMove(currentMove - 1);
2172 if (started == STARTED_MOVES) next_out = i;
2173 started = STARTED_NONE;
2174 ics_getting_history = H_FALSE;
2177 case STARTED_OBSERVE:
2178 started = STARTED_NONE;
2179 SendToICS(ics_prefix);
2180 SendToICS("refresh\n");
2189 if ((started == STARTED_MOVES || started == STARTED_BOARD ||
2190 started == STARTED_HOLDINGS ||
2191 started == STARTED_MOVES_NOHIDE) && i >= leftover_len) {
2192 /* Accumulate characters in move list or board */
2193 parse[parse_pos++] = buf[i];
2196 /* Start of game messages. Mostly we detect start of game
2197 when the first board image arrives. On some versions
2198 of the ICS, though, we need to do a "refresh" after starting
2199 to observe in order to get the current board right away. */
2200 if (looking_at(buf, &i, "Adding game * to observation list")) {
2201 started = STARTED_OBSERVE;
2205 /* Handle auto-observe */
2206 if (appData.autoObserve &&
2207 (gameMode == IcsIdle || gameMode == BeginningOfGame) &&
2208 looking_at(buf, &i, "Game notification: * (*) vs. * (*)")) {
2210 /* Choose the player that was highlighted, if any. */
2211 if (star_match[0][0] == '\033' ||
2212 star_match[1][0] != '\033') {
2213 player = star_match[0];
2215 player = star_match[2];
2217 sprintf(str, "%sobserve %s\n",
2218 ics_prefix, StripHighlightAndTitle(player));
2221 /* Save ratings from notify string */
2222 strcpy(player1Name, star_match[0]);
2223 player1Rating = string_to_rating(star_match[1]);
2224 strcpy(player2Name, star_match[2]);
2225 player2Rating = string_to_rating(star_match[3]);
2227 if (appData.debugMode)
2229 "Ratings from 'Game notification:' %s %d, %s %d\n",
2230 player1Name, player1Rating,
2231 player2Name, player2Rating);
2236 /* Deal with automatic examine mode after a game,
2237 and with IcsObserving -> IcsExamining transition */
2238 if (looking_at(buf, &i, "Entering examine mode for game *") ||
2239 looking_at(buf, &i, "has made you an examiner of game *")) {
2241 int gamenum = atoi(star_match[0]);
2242 if ((gameMode == IcsIdle || gameMode == IcsObserving) &&
2243 gamenum == ics_gamenum) {
2244 /* We were already playing or observing this game;
2245 no need to refetch history */
2246 gameMode = IcsExamining;
2248 pauseExamForwardMostMove = forwardMostMove;
2249 } else if (currentMove < forwardMostMove) {
2250 ForwardInner(forwardMostMove);
2253 /* I don't think this case really can happen */
2254 SendToICS(ics_prefix);
2255 SendToICS("refresh\n");
2260 /* Error messages */
2261 if (ics_user_moved) {
2262 if (looking_at(buf, &i, "Illegal move") ||
2263 looking_at(buf, &i, "Not a legal move") ||
2264 looking_at(buf, &i, "Your king is in check") ||
2265 looking_at(buf, &i, "It isn't your turn") ||
2266 looking_at(buf, &i, "It is not your move")) {
2269 if (forwardMostMove > backwardMostMove) {
2270 currentMove = --forwardMostMove;
2271 DisplayMove(currentMove - 1); /* before DMError */
2272 DisplayMoveError("Illegal move (rejected by ICS)");
2273 DrawPosition(FALSE, boards[currentMove]);
2275 DisplayBothClocks();
2281 if (looking_at(buf, &i, "still have time") ||
2282 looking_at(buf, &i, "not out of time") ||
2283 looking_at(buf, &i, "either player is out of time") ||
2284 looking_at(buf, &i, "has timeseal; checking")) {
2285 /* We must have called his flag a little too soon */
2286 whiteFlag = blackFlag = FALSE;
2290 if (looking_at(buf, &i, "added * seconds to") ||
2291 looking_at(buf, &i, "seconds were added to")) {
2292 /* Update the clocks */
2293 SendToICS(ics_prefix);
2294 SendToICS("refresh\n");
2298 if (!ics_clock_paused && looking_at(buf, &i, "clock paused")) {
2299 ics_clock_paused = TRUE;
2304 if (ics_clock_paused && looking_at(buf, &i, "clock resumed")) {
2305 ics_clock_paused = FALSE;
2310 /* Grab player ratings from the Creating: message.
2311 Note we have to check for the special case when
2312 the ICS inserts things like [white] or [black]. */
2313 if (looking_at(buf, &i, "Creating: * (*)* * (*)") ||
2314 looking_at(buf, &i, "Creating: * (*) [*] * (*)")) {
2316 0 player 1 name (not necessarily white)
2318 2 empty, white, or black (IGNORED)
2319 3 player 2 name (not necessarily black)
2322 The names/ratings are sorted out when the game
2323 actually starts (below).
2325 strcpy(player1Name, StripHighlightAndTitle(star_match[0]));
2326 player1Rating = string_to_rating(star_match[1]);
2327 strcpy(player2Name, StripHighlightAndTitle(star_match[3]));
2328 player2Rating = string_to_rating(star_match[4]);
2330 if (appData.debugMode)
2332 "Ratings from 'Creating:' %s %d, %s %d\n",
2333 player1Name, player1Rating,
2334 player2Name, player2Rating);
2339 /* Improved generic start/end-of-game messages */
2340 if ((tkind=0, looking_at(buf, &i, "{Game * (* vs. *) *}*")) ||
2341 (tkind=1, looking_at(buf, &i, "{Game * (*(*) vs. *(*)) *}*"))){
2342 /* If tkind == 0: */
2343 /* star_match[0] is the game number */
2344 /* [1] is the white player's name */
2345 /* [2] is the black player's name */
2346 /* For end-of-game: */
2347 /* [3] is the reason for the game end */
2348 /* [4] is a PGN end game-token, preceded by " " */
2349 /* For start-of-game: */
2350 /* [3] begins with "Creating" or "Continuing" */
2351 /* [4] is " *" or empty (don't care). */
2352 int gamenum = atoi(star_match[0]);
2353 char *whitename, *blackname, *why, *endtoken;
2354 ChessMove endtype = (ChessMove) 0;
2357 whitename = star_match[1];
2358 blackname = star_match[2];
2359 why = star_match[3];
2360 endtoken = star_match[4];
2362 whitename = star_match[1];
2363 blackname = star_match[3];
2364 why = star_match[5];
2365 endtoken = star_match[6];
2368 /* Game start messages */
2369 if (strncmp(why, "Creating ", 9) == 0 ||
2370 strncmp(why, "Continuing ", 11) == 0) {
2371 gs_gamenum = gamenum;
2372 strcpy(gs_kind, strchr(why, ' ') + 1);
2374 if (appData.zippyPlay) {
2375 ZippyGameStart(whitename, blackname);
2381 /* Game end messages */
2382 if (gameMode == IcsIdle || gameMode == BeginningOfGame ||
2383 ics_gamenum != gamenum) {
2386 while (endtoken[0] == ' ') endtoken++;
2387 switch (endtoken[0]) {
2390 endtype = GameUnfinished;
2393 endtype = BlackWins;
2396 if (endtoken[1] == '/')
2397 endtype = GameIsDrawn;
2399 endtype = WhiteWins;
2402 GameEnds(endtype, why, GE_ICS);
2404 if (appData.zippyPlay && first.initDone) {
2405 ZippyGameEnd(endtype, why);
2406 if (first.pr == NULL) {
2407 /* Start the next process early so that we'll
2408 be ready for the next challenge */
2409 StartChessProgram(&first);
2411 /* Send "new" early, in case this command takes
2412 a long time to finish, so that we'll be ready
2413 for the next challenge. */
2420 if (looking_at(buf, &i, "Removing game * from observation") ||
2421 looking_at(buf, &i, "no longer observing game *") ||
2422 looking_at(buf, &i, "Game * (*) has no examiners")) {
2423 if (gameMode == IcsObserving &&
2424 atoi(star_match[0]) == ics_gamenum)
2429 ics_user_moved = FALSE;
2434 if (looking_at(buf, &i, "no longer examining game *")) {
2435 if (gameMode == IcsExamining &&
2436 atoi(star_match[0]) == ics_gamenum)
2440 ics_user_moved = FALSE;
2445 /* Advance leftover_start past any newlines we find,
2446 so only partial lines can get reparsed */
2447 if (looking_at(buf, &i, "\n")) {
2448 prevColor = curColor;
2449 if (curColor != ColorNormal) {
2450 if (oldi > next_out) {
2451 SendToPlayer(&buf[next_out], oldi - next_out);
2454 Colorize(ColorNormal, FALSE);
2455 curColor = ColorNormal;
2457 if (started == STARTED_BOARD) {
2458 started = STARTED_NONE;
2459 parse[parse_pos] = NULLCHAR;
2460 ParseBoard12(parse);
2463 /* Send premove here */
2464 if (appData.premove) {
2466 if (currentMove == 0 &&
2467 gameMode == IcsPlayingWhite &&
2468 appData.premoveWhite) {
2469 sprintf(str, "%s%s\n", ics_prefix,
2470 appData.premoveWhiteText);
2471 if (appData.debugMode)
2472 fprintf(debugFP, "Sending premove:\n");
2474 } else if (currentMove == 1 &&
2475 gameMode == IcsPlayingBlack &&
2476 appData.premoveBlack) {
2477 sprintf(str, "%s%s\n", ics_prefix,
2478 appData.premoveBlackText);
2479 if (appData.debugMode)
2480 fprintf(debugFP, "Sending premove:\n");
2482 } else if (gotPremove) {
2484 ClearPremoveHighlights();
2485 if (appData.debugMode)
2486 fprintf(debugFP, "Sending premove:\n");
2487 UserMoveEvent(premoveFromX, premoveFromY,
2488 premoveToX, premoveToY,
2493 /* Usually suppress following prompt */
2494 if (!(forwardMostMove == 0 && gameMode == IcsExamining)) {
2495 if (looking_at(buf, &i, "*% ")) {
2496 savingComment = FALSE;
2500 } else if (started == STARTED_HOLDINGS) {
2502 char new_piece[MSG_SIZ];
2503 started = STARTED_NONE;
2504 parse[parse_pos] = NULLCHAR;
2505 if (appData.debugMode)
2506 fprintf(debugFP, "Parsing holdings: %s\n", parse);
2507 if (sscanf(parse, " game %d", &gamenum) == 1 &&
2508 gamenum == ics_gamenum) {
2509 if (gameInfo.variant == VariantNormal) {
2510 gameInfo.variant = VariantCrazyhouse; /*temp guess*/
2511 /* Get a move list just to see the header, which
2512 will tell us whether this is really bug or zh */
2513 if (ics_getting_history == H_FALSE) {
2514 ics_getting_history = H_REQUESTED;
2515 sprintf(str, "%smoves %d\n", ics_prefix, gamenum);
2519 new_piece[0] = NULLCHAR;
2520 sscanf(parse, "game %d white [%s black [%s <- %s",
2521 &gamenum, white_holding, black_holding,
2523 white_holding[strlen(white_holding)-1] = NULLCHAR;
2524 black_holding[strlen(black_holding)-1] = NULLCHAR;
2526 if (appData.zippyPlay && first.initDone) {
2527 ZippyHoldings(white_holding, black_holding,
2531 if (tinyLayout || smallLayout) {
2532 char wh[16], bh[16];
2533 PackHolding(wh, white_holding);
2534 PackHolding(bh, black_holding);
2535 sprintf(str, "[%s-%s] %s-%s", wh, bh,
2536 gameInfo.white, gameInfo.black);
2538 sprintf(str, "%s [%s] vs. %s [%s]",
2539 gameInfo.white, white_holding,
2540 gameInfo.black, black_holding);
2542 DrawPosition(FALSE, NULL);
2545 /* Suppress following prompt */
2546 if (looking_at(buf, &i, "*% ")) {
2547 savingComment = FALSE;
2554 i++; /* skip unparsed character and loop back */
2557 if (started != STARTED_MOVES && started != STARTED_BOARD &&
2558 started != STARTED_HOLDINGS && i > next_out) {
2559 SendToPlayer(&buf[next_out], i - next_out);
2563 leftover_len = buf_len - leftover_start;
2564 /* if buffer ends with something we couldn't parse,
2565 reparse it after appending the next read */
2567 } else if (count == 0) {
2568 RemoveInputSource(isr);
2569 DisplayFatalError(_("Connection closed by ICS"), 0, 0);
2571 DisplayFatalError(_("Error reading from ICS"), error, 1);
2576 /* Board style 12 looks like this:
2578 <12> r-b---k- pp----pp ---bP--- ---p---- q------- ------P- P--Q--BP -----R-K W -1 0 0 0 0 0 0 paf MaxII 0 2 12 21 25 234 174 24 Q/d7-a4 (0:06) Qxa4 0 0
2580 * The "<12> " is stripped before it gets to this routine. The two
2581 * trailing 0's (flip state and clock ticking) are later addition, and
2582 * some chess servers may not have them, or may have only the first.
2583 * Additional trailing fields may be added in the future.
2586 #define PATTERN "%72c%c%d%d%d%d%d%d%d%s%s%d%d%d%d%d%d%d%d%s%s%s%d%d"
2588 #define RELATION_OBSERVING_PLAYED 0
2589 #define RELATION_OBSERVING_STATIC -2 /* examined, oldmoves, or smoves */
2590 #define RELATION_PLAYING_MYMOVE 1
2591 #define RELATION_PLAYING_NOTMYMOVE -1
2592 #define RELATION_EXAMINING 2
2593 #define RELATION_ISOLATED_BOARD -3
2594 #define RELATION_STARTING_POSITION -4 /* FICS only */
2597 ParseBoard12(string)
2600 GameMode newGameMode;
2601 int gamenum, newGame, newMove, relation, basetime, increment, ics_flip = 0;
2602 int j, k, n, moveNum, white_stren, black_stren, white_time, black_time;
2603 int double_push, castle_ws, castle_wl, castle_bs, castle_bl, irrev_count;
2604 char to_play, board_chars[72];
2605 char move_str[500], str[500], elapsed_time[500];
2606 char black[32], white[32];
2608 int prevMove = currentMove;
2611 int fromX, fromY, toX, toY;
2614 fromX = fromY = toX = toY = -1;
2618 if (appData.debugMode)
2619 fprintf(debugFP, _("Parsing board: %s\n"), string);
2621 move_str[0] = NULLCHAR;
2622 elapsed_time[0] = NULLCHAR;
2623 n = sscanf(string, PATTERN, board_chars, &to_play, &double_push,
2624 &castle_ws, &castle_wl, &castle_bs, &castle_bl, &irrev_count,
2625 &gamenum, white, black, &relation, &basetime, &increment,
2626 &white_stren, &black_stren, &white_time, &black_time,
2627 &moveNum, str, elapsed_time, move_str, &ics_flip,
2631 sprintf(str, _("Failed to parse board string:\n\"%s\""), string);
2632 DisplayError(str, 0);
2636 /* Convert the move number to internal form */
2637 moveNum = (moveNum - 1) * 2;
2638 if (to_play == 'B') moveNum++;
2639 if (moveNum >= MAX_MOVES) {
2640 DisplayFatalError(_("Game too long; increase MAX_MOVES and recompile"),
2646 case RELATION_OBSERVING_PLAYED:
2647 case RELATION_OBSERVING_STATIC:
2648 if (gamenum == -1) {
2649 /* Old ICC buglet */
2650 relation = RELATION_OBSERVING_STATIC;
2652 newGameMode = IcsObserving;
2654 case RELATION_PLAYING_MYMOVE:
2655 case RELATION_PLAYING_NOTMYMOVE:
2657 ((relation == RELATION_PLAYING_MYMOVE) == (to_play == 'W')) ?
2658 IcsPlayingWhite : IcsPlayingBlack;
2660 case RELATION_EXAMINING:
2661 newGameMode = IcsExamining;
2663 case RELATION_ISOLATED_BOARD:
2665 /* Just display this board. If user was doing something else,
2666 we will forget about it until the next board comes. */
2667 newGameMode = IcsIdle;
2669 case RELATION_STARTING_POSITION:
2670 newGameMode = gameMode;
2674 /* Modify behavior for initial board display on move listing
2677 switch (ics_getting_history) {
2681 case H_GOT_REQ_HEADER:
2682 case H_GOT_UNREQ_HEADER:
2683 /* This is the initial position of the current game */
2684 gamenum = ics_gamenum;
2685 moveNum = 0; /* old ICS bug workaround */
2686 if (to_play == 'B') {
2687 startedFromSetupPosition = TRUE;
2688 blackPlaysFirst = TRUE;
2690 if (forwardMostMove == 0) forwardMostMove = 1;
2691 if (backwardMostMove == 0) backwardMostMove = 1;
2692 if (currentMove == 0) currentMove = 1;
2694 newGameMode = gameMode;
2695 relation = RELATION_STARTING_POSITION; /* ICC needs this */
2697 case H_GOT_UNWANTED_HEADER:
2698 /* This is an initial board that we don't want */
2700 case H_GETTING_MOVES:
2701 /* Should not happen */
2702 DisplayError(_("Error gathering move list: extra board"), 0);
2703 ics_getting_history = H_FALSE;
2707 /* Take action if this is the first board of a new game, or of a
2708 different game than is currently being displayed. */
2709 if (gamenum != ics_gamenum || newGameMode != gameMode ||
2710 relation == RELATION_ISOLATED_BOARD) {
2712 /* Forget the old game and get the history (if any) of the new one */
2713 if (gameMode != BeginningOfGame) {
2717 if (appData.autoRaiseBoard) BoardToTop();
2719 if (gamenum == -1) {
2720 newGameMode = IcsIdle;
2721 } else if (moveNum > 0 && newGameMode != IcsIdle &&
2722 appData.getMoveList) {
2723 /* Need to get game history */
2724 ics_getting_history = H_REQUESTED;
2725 sprintf(str, "%smoves %d\n", ics_prefix, gamenum);
2729 /* Initially flip the board to have black on the bottom if playing
2730 black or if the ICS flip flag is set, but let the user change
2731 it with the Flip View button. */
2732 flipView = appData.autoFlipView ?
2733 (newGameMode == IcsPlayingBlack) || ics_flip :
2736 /* Done with values from previous mode; copy in new ones */
2737 gameMode = newGameMode;
2739 ics_gamenum = gamenum;
2740 if (gamenum == gs_gamenum) {
2741 int klen = strlen(gs_kind);
2742 if (gs_kind[klen - 1] == '.') gs_kind[klen - 1] = NULLCHAR;
2743 sprintf(str, "ICS %s", gs_kind);
2744 gameInfo.event = StrSave(str);
2746 gameInfo.event = StrSave("ICS game");
2748 gameInfo.site = StrSave(appData.icsHost);
2749 gameInfo.date = PGNDate();
2750 gameInfo.round = StrSave("-");
2751 gameInfo.white = StrSave(white);
2752 gameInfo.black = StrSave(black);
2753 timeControl = basetime * 60 * 1000;
2754 timeIncrement = increment * 1000;
2755 movesPerSession = 0;
2756 gameInfo.timeControl = TimeControlTagValue();
2757 gameInfo.variant = StringToVariant(gameInfo.event);
2759 /* Do we have the ratings? */
2760 if (strcmp(player1Name, white) == 0 &&
2761 strcmp(player2Name, black) == 0) {
2762 if (appData.debugMode)
2763 fprintf(debugFP, "Remembered ratings: W %d, B %d\n",
2764 player1Rating, player2Rating);
2765 gameInfo.whiteRating = player1Rating;
2766 gameInfo.blackRating = player2Rating;
2767 } else if (strcmp(player2Name, white) == 0 &&
2768 strcmp(player1Name, black) == 0) {
2769 if (appData.debugMode)
2770 fprintf(debugFP, "Remembered ratings: W %d, B %d\n",
2771 player2Rating, player1Rating);
2772 gameInfo.whiteRating = player2Rating;
2773 gameInfo.blackRating = player1Rating;
2775 player1Name[0] = player2Name[0] = NULLCHAR;
2777 /* Silence shouts if requested */
2778 if (appData.quietPlay &&
2779 (gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack)) {
2780 SendToICS(ics_prefix);
2781 SendToICS("set shout 0\n");
2785 /* Deal with midgame name changes */
2787 if (!gameInfo.white || strcmp(gameInfo.white, white) != 0) {
2788 if (gameInfo.white) free(gameInfo.white);
2789 gameInfo.white = StrSave(white);
2791 if (!gameInfo.black || strcmp(gameInfo.black, black) != 0) {
2792 if (gameInfo.black) free(gameInfo.black);
2793 gameInfo.black = StrSave(black);
2797 /* Throw away game result if anything actually changes in examine mode */
2798 if (gameMode == IcsExamining && !newGame) {
2799 gameInfo.result = GameUnfinished;
2800 if (gameInfo.resultDetails != NULL) {
2801 free(gameInfo.resultDetails);
2802 gameInfo.resultDetails = NULL;
2806 /* In pausing && IcsExamining mode, we ignore boards coming
2807 in if they are in a different variation than we are. */
2808 if (pauseExamInvalid) return;
2809 if (pausing && gameMode == IcsExamining) {
2810 if (moveNum <= pauseExamForwardMostMove) {
2811 pauseExamInvalid = TRUE;
2812 forwardMostMove = pauseExamForwardMostMove;
2817 /* Parse the board */
2818 for (k = 0; k < 8; k++)
2819 for (j = 0; j < 8; j++)
2820 board[k][j] = CharToPiece(board_chars[(7-k)*9 + j]);
2821 CopyBoard(boards[moveNum], board);
2823 startedFromSetupPosition =
2824 !CompareBoards(board, initialPosition);
2827 if (ics_getting_history == H_GOT_REQ_HEADER ||
2828 ics_getting_history == H_GOT_UNREQ_HEADER) {
2829 /* This was an initial position from a move list, not
2830 the current position */
2834 /* Update currentMove and known move number limits */
2835 newMove = newGame || moveNum > forwardMostMove;
2837 forwardMostMove = backwardMostMove = currentMove = moveNum;
2838 if (gameMode == IcsExamining && moveNum == 0) {
2839 /* Workaround for ICS limitation: we are not told the wild
2840 type when starting to examine a game. But if we ask for
2841 the move list, the move list header will tell us */
2842 ics_getting_history = H_REQUESTED;
2843 sprintf(str, "%smoves %d\n", ics_prefix, gamenum);
2846 } else if (moveNum == forwardMostMove + 1 || moveNum == forwardMostMove
2847 || (moveNum < forwardMostMove && moveNum >= backwardMostMove)) {
2848 forwardMostMove = moveNum;
2849 if (!pausing || currentMove > forwardMostMove)
2850 currentMove = forwardMostMove;
2852 /* New part of history that is not contiguous with old part */
2853 if (pausing && gameMode == IcsExamining) {
2854 pauseExamInvalid = TRUE;
2855 forwardMostMove = pauseExamForwardMostMove;
2858 forwardMostMove = backwardMostMove = currentMove = moveNum;
2859 if (gameMode == IcsExamining && moveNum > 0 && appData.getMoveList) {
2860 ics_getting_history = H_REQUESTED;
2861 sprintf(str, "%smoves %d\n", ics_prefix, gamenum);
2866 /* Update the clocks */
2867 if (strchr(elapsed_time, '.')) {
2869 timeRemaining[0][moveNum] = whiteTimeRemaining = white_time;
2870 timeRemaining[1][moveNum] = blackTimeRemaining = black_time;
2872 /* Time is in seconds */
2873 timeRemaining[0][moveNum] = whiteTimeRemaining = white_time * 1000;
2874 timeRemaining[1][moveNum] = blackTimeRemaining = black_time * 1000;
2879 if (appData.zippyPlay && newGame &&
2880 gameMode != IcsObserving && gameMode != IcsIdle &&
2881 gameMode != IcsExamining)
2882 ZippyFirstBoard(moveNum, basetime, increment);
2885 /* Put the move on the move list, first converting
2886 to canonical algebraic form. */
2888 if (moveNum <= backwardMostMove) {
2889 /* We don't know what the board looked like before
2891 strcpy(parseList[moveNum - 1], move_str);
2892 strcat(parseList[moveNum - 1], " ");
2893 strcat(parseList[moveNum - 1], elapsed_time);
2894 moveList[moveNum - 1][0] = NULLCHAR;
2895 } else if (ParseOneMove(move_str, moveNum - 1, &moveType,
2896 &fromX, &fromY, &toX, &toY, &promoChar)) {
2897 (void) CoordsToAlgebraic(boards[moveNum - 1],
2898 PosFlags(moveNum - 1), EP_UNKNOWN,
2899 fromY, fromX, toY, toX, promoChar,
2900 parseList[moveNum-1]);
2901 switch (MateTest(boards[moveNum], PosFlags(moveNum), EP_UNKNOWN)){
2907 strcat(parseList[moveNum - 1], "+");
2910 strcat(parseList[moveNum - 1], "#");
2913 strcat(parseList[moveNum - 1], " ");
2914 strcat(parseList[moveNum - 1], elapsed_time);
2915 /* currentMoveString is set as a side-effect of ParseOneMove */
2916 strcpy(moveList[moveNum - 1], currentMoveString);
2917 strcat(moveList[moveNum - 1], "\n");
2918 } else if (strcmp(move_str, "none") == 0) {
2919 /* Again, we don't know what the board looked like;
2920 this is really the start of the game. */
2921 parseList[moveNum - 1][0] = NULLCHAR;
2922 moveList[moveNum - 1][0] = NULLCHAR;
2923 backwardMostMove = moveNum;
2924 startedFromSetupPosition = TRUE;
2925 fromX = fromY = toX = toY = -1;
2927 /* Move from ICS was illegal!? Punt. */
2929 if (appData.testLegality && appData.debugMode) {
2930 sprintf(str, "Illegal move \"%s\" from ICS", move_str);
2931 DisplayError(str, 0);
2934 strcpy(parseList[moveNum - 1], move_str);
2935 strcat(parseList[moveNum - 1], " ");
2936 strcat(parseList[moveNum - 1], elapsed_time);
2937 moveList[moveNum - 1][0] = NULLCHAR;
2938 fromX = fromY = toX = toY = -1;
2942 /* Send move to chess program (BEFORE animating it). */
2943 if (appData.zippyPlay && !newGame && newMove &&
2944 (!appData.getMoveList || backwardMostMove == 0) && first.initDone) {
2946 if ((gameMode == IcsPlayingWhite && WhiteOnMove(moveNum)) ||
2947 (gameMode == IcsPlayingBlack && !WhiteOnMove(moveNum))) {
2948 if (moveList[moveNum - 1][0] == NULLCHAR) {
2949 sprintf(str, _("Couldn't parse move \"%s\" from ICS"),
2951 DisplayError(str, 0);
2953 if (first.sendTime) {
2954 SendTimeRemaining(&first, gameMode == IcsPlayingWhite);
2956 SendMoveToProgram(moveNum - 1, &first);
2959 if (first.useColors) {
2960 SendToProgram(gameMode == IcsPlayingWhite ?
2962 "black\ngo\n", &first);
2964 SendToProgram("go\n", &first);
2966 first.maybeThinking = TRUE;
2969 } else if (gameMode == IcsObserving || gameMode == IcsExamining) {
2970 if (moveList[moveNum - 1][0] == NULLCHAR) {
2971 sprintf(str, _("Couldn't parse move \"%s\" from ICS"), move_str);
2972 DisplayError(str, 0);
2974 SendMoveToProgram(moveNum - 1, &first);
2981 if (moveNum > 0 && !gotPremove) {
2982 /* If move comes from a remote source, animate it. If it
2983 isn't remote, it will have already been animated. */
2984 if (!pausing && !ics_user_moved && prevMove == moveNum - 1) {
2985 AnimateMove(boards[moveNum - 1], fromX, fromY, toX, toY);
2987 if (!pausing && appData.highlightLastMove) {
2988 SetHighlights(fromX, fromY, toX, toY);
2992 /* Start the clocks */
2993 whiteFlag = blackFlag = FALSE;
2994 appData.clockMode = !(basetime == 0 && increment == 0);
2996 ics_clock_paused = TRUE;
2998 } else if (ticking == 1) {
2999 ics_clock_paused = FALSE;
3001 if (gameMode == IcsIdle ||
3002 relation == RELATION_OBSERVING_STATIC ||
3003 relation == RELATION_EXAMINING ||
3005 DisplayBothClocks();
3009 /* Display opponents and material strengths */
3010 if (gameInfo.variant != VariantBughouse &&
3011 gameInfo.variant != VariantCrazyhouse) {
3012 if (tinyLayout || smallLayout) {
3013 sprintf(str, "%s(%d) %s(%d) {%d %d}",
3014 gameInfo.white, white_stren, gameInfo.black, black_stren,
3015 basetime, increment);
3017 sprintf(str, "%s (%d) vs. %s (%d) {%d %d}",
3018 gameInfo.white, white_stren, gameInfo.black, black_stren,
3019 basetime, increment);
3025 /* Display the board */
3028 if (appData.premove)
3030 ((gameMode == IcsPlayingWhite) && (WhiteOnMove(currentMove))) ||
3031 ((gameMode == IcsPlayingBlack) && (!WhiteOnMove(currentMove))))
3032 ClearPremoveHighlights();
3034 DrawPosition(FALSE, boards[currentMove]);
3035 DisplayMove(moveNum - 1);
3036 if (appData.ringBellAfterMoves && !ics_user_moved)
3040 HistorySet(parseList, backwardMostMove, forwardMostMove, currentMove-1);
3047 if (appData.icsActive && gameMode != IcsIdle && ics_gamenum > 0) {
3048 ics_getting_history = H_REQUESTED;
3049 sprintf(buf, "%smoves %d\n", ics_prefix, ics_gamenum);
3055 AnalysisPeriodicEvent(force)
3058 if (((programStats.ok_to_send == 0 || programStats.line_is_book)
3059 && !force) || !appData.periodicUpdates)
3062 /* Send . command to Crafty to collect stats */
3063 SendToProgram(".\n", &first);
3065 /* Don't send another until we get a response (this makes
3066 us stop sending to old Crafty's which don't understand
3067 the "." command (sending illegal cmds resets node count & time,
3068 which looks bad)) */
3069 programStats.ok_to_send = 0;
3073 SendMoveToProgram(moveNum, cps)
3075 ChessProgramState *cps;
3078 if (cps->useUsermove) {
3079 SendToProgram("usermove ", cps);
3083 if ((space = strchr(parseList[moveNum], ' ')) != NULL) {
3084 int len = space - parseList[moveNum];
3085 memcpy(buf, parseList[moveNum], len);
3087 buf[len] = NULLCHAR;
3089 sprintf(buf, "%s\n", parseList[moveNum]);
3091 SendToProgram(buf, cps);
3093 SendToProgram(moveList[moveNum], cps);
3098 SendMoveToICS(moveType, fromX, fromY, toX, toY)
3100 int fromX, fromY, toX, toY;
3102 char user_move[MSG_SIZ];
3106 sprintf(user_move, "say Internal error; bad moveType %d (%d,%d-%d,%d)",
3107 (int)moveType, fromX, fromY, toX, toY);
3108 DisplayError(user_move + strlen("say "), 0);
3110 case WhiteKingSideCastle:
3111 case BlackKingSideCastle:
3112 case WhiteQueenSideCastleWild:
3113 case BlackQueenSideCastleWild:
3114 sprintf(user_move, "o-o\n");
3116 case WhiteQueenSideCastle:
3117 case BlackQueenSideCastle:
3118 case WhiteKingSideCastleWild:
3119 case BlackKingSideCastleWild:
3120 sprintf(user_move, "o-o-o\n");
3122 case WhitePromotionQueen:
3123 case BlackPromotionQueen:
3124 case WhitePromotionRook:
3125 case BlackPromotionRook:
3126 case WhitePromotionBishop:
3127 case BlackPromotionBishop:
3128 case WhitePromotionKnight:
3129 case BlackPromotionKnight:
3130 case WhitePromotionKing:
3131 case BlackPromotionKing:
3132 sprintf(user_move, "%c%c%c%c=%c\n",
3133 'a' + fromX, '1' + fromY, 'a' + toX, '1' + toY,
3134 PieceToChar(PromoPiece(moveType)));
3138 sprintf(user_move, "%c@%c%c\n",
3139 ToUpper(PieceToChar((ChessSquare) fromX)),
3140 'a' + toX, '1' + toY);
3143 case WhiteCapturesEnPassant:
3144 case BlackCapturesEnPassant:
3145 case IllegalMove: /* could be a variant we don't quite understand */
3146 sprintf(user_move, "%c%c%c%c\n",
3147 'a' + fromX, '1' + fromY, 'a' + toX, '1' + toY);
3150 SendToICS(user_move);
3154 CoordsToComputerAlgebraic(rf, ff, rt, ft, promoChar, move)
3159 if (rf == DROP_RANK) {
3160 sprintf(move, "%c@%c%c\n",
3161 ToUpper(PieceToChar((ChessSquare) ff)), 'a' + ft, '1' + rt);
3163 if (promoChar == 'x' || promoChar == NULLCHAR) {
3164 sprintf(move, "%c%c%c%c\n",
3165 'a' + ff, '1' + rf, 'a' + ft, '1' + rt);
3167 sprintf(move, "%c%c%c%c%c\n",
3168 'a' + ff, '1' + rf, 'a' + ft, '1' + rt, promoChar);
3174 ProcessICSInitScript(f)
3179 while (fgets(buf, MSG_SIZ, f)) {
3180 SendToICSDelayed(buf,(long)appData.msLoginDelay);
3187 /* Parser for moves from gnuchess, ICS, or user typein box */
3189 ParseOneMove(move, moveNum, moveType, fromX, fromY, toX, toY, promoChar)
3192 ChessMove *moveType;
3193 int *fromX, *fromY, *toX, *toY;
3196 *moveType = yylexstr(moveNum, move);
3197 switch (*moveType) {
3198 case WhitePromotionQueen:
3199 case BlackPromotionQueen:
3200 case WhitePromotionRook:
3201 case BlackPromotionRook:
3202 case WhitePromotionBishop:
3203 case BlackPromotionBishop:
3204 case WhitePromotionKnight:
3205 case BlackPromotionKnight:
3206 case WhitePromotionKing:
3207 case BlackPromotionKing:
3209 case WhiteCapturesEnPassant:
3210 case BlackCapturesEnPassant:
3211 case WhiteKingSideCastle:
3212 case WhiteQueenSideCastle:
3213 case BlackKingSideCastle:
3214 case BlackQueenSideCastle:
3215 case WhiteKingSideCastleWild:
3216 case WhiteQueenSideCastleWild:
3217 case BlackKingSideCastleWild:
3218 case BlackQueenSideCastleWild:
3219 case IllegalMove: /* bug or odd chess variant */
3220 *fromX = currentMoveString[0] - 'a';
3221 *fromY = currentMoveString[1] - '1';
3222 *toX = currentMoveString[2] - 'a';
3223 *toY = currentMoveString[3] - '1';
3224 *promoChar = currentMoveString[4];
3225 if (*fromX < 0 || *fromX > 7 || *fromY < 0 || *fromY > 7 ||
3226 *toX < 0 || *toX > 7 || *toY < 0 || *toY > 7) {
3227 *fromX = *fromY = *toX = *toY = 0;
3230 if (appData.testLegality) {
3231 return (*moveType != IllegalMove);
3233 return !(fromX == fromY && toX == toY);
3238 *fromX = *moveType == WhiteDrop ?
3239 (int) CharToPiece(ToUpper(currentMoveString[0])) :
3240 (int) CharToPiece(ToLower(currentMoveString[0]));
3242 *toX = currentMoveString[2] - 'a';
3243 *toY = currentMoveString[3] - '1';
3244 *promoChar = NULLCHAR;
3248 case ImpossibleMove:
3249 case (ChessMove) 0: /* end of file */
3259 *fromX = *fromY = *toX = *toY = 0;
3260 *promoChar = NULLCHAR;
3267 InitPosition(redraw)
3270 currentMove = forwardMostMove = backwardMostMove = 0;
3271 switch (gameInfo.variant) {
3273 CopyBoard(boards[0], initialPosition);
3275 case VariantTwoKings:
3276 CopyBoard(boards[0], twoKingsPosition);
3277 startedFromSetupPosition = TRUE;
3279 case VariantWildCastle:
3280 CopyBoard(boards[0], initialPosition);
3281 /* !!?shuffle with kings guaranteed to be on d or e file */
3283 case VariantNoCastle:
3284 CopyBoard(boards[0], initialPosition);
3285 /* !!?unconstrained back-rank shuffle */
3287 case VariantFischeRandom:
3288 CopyBoard(boards[0], initialPosition);
3289 /* !!shuffle according to FR rules */
3293 DrawPosition(FALSE, boards[currentMove]);
3297 SendBoard(cps, moveNum)
3298 ChessProgramState *cps;
3301 char message[MSG_SIZ];
3303 if (cps->useSetboard) {
3304 char* fen = PositionToFEN(moveNum);
3305 sprintf(message, "setboard %s\n", fen);
3306 SendToProgram(message, cps);
3312 /* Kludge to set black to move, avoiding the troublesome and now
3313 * deprecated "black" command.
3315 if (!WhiteOnMove(moveNum)) SendToProgram("a2a3\n", cps);
3317 SendToProgram("edit\n", cps);
3318 SendToProgram("#\n", cps);
3319 for (i = BOARD_SIZE - 1; i >= 0; i--) {
3320 bp = &boards[moveNum][i][0];
3321 for (j = 0; j < BOARD_SIZE; j++, bp++) {
3322 if ((int) *bp < (int) BlackPawn) {
3323 sprintf(message, "%c%c%c\n", PieceToChar(*bp),
3325 SendToProgram(message, cps);
3330 SendToProgram("c\n", cps);
3331 for (i = BOARD_SIZE - 1; i >= 0; i--) {
3332 bp = &boards[moveNum][i][0];
3333 for (j = 0; j < BOARD_SIZE; j++, bp++) {
3334 if (((int) *bp != (int) EmptySquare)
3335 && ((int) *bp >= (int) BlackPawn)) {
3336 sprintf(message, "%c%c%c\n", ToUpper(PieceToChar(*bp)),
3338 SendToProgram(message, cps);
3343 SendToProgram(".\n", cps);
3348 IsPromotion(fromX, fromY, toX, toY)
3349 int fromX, fromY, toX, toY;
3351 return gameMode != EditPosition &&
3352 fromX >=0 && fromY >= 0 && toX >= 0 && toY >= 0 &&
3353 ((boards[currentMove][fromY][fromX] == WhitePawn && toY == 7) ||
3354 (boards[currentMove][fromY][fromX] == BlackPawn && toY == 0));
3359 PieceForSquare (x, y)
3363 if (x < 0 || x >= BOARD_SIZE || y < 0 || y >= BOARD_SIZE)
3366 return boards[currentMove][y][x];
3370 OKToStartUserMove(x, y)
3373 ChessSquare from_piece;
3376 if (matchMode) return FALSE;
3377 if (gameMode == EditPosition) return TRUE;
3379 if (x >= 0 && y >= 0)
3380 from_piece = boards[currentMove][y][x];
3382 from_piece = EmptySquare;
3384 if (from_piece == EmptySquare) return FALSE;
3386 white_piece = (int)from_piece >= (int)WhitePawn &&
3387 (int)from_piece <= (int)WhiteKing;
3390 case PlayFromGameFile:
3392 case TwoMachinesPlay:
3400 case MachinePlaysWhite:
3401 case IcsPlayingBlack:
3402 if (appData.zippyPlay) return FALSE;
3404 DisplayMoveError(_("You are playing Black"));
3409 case MachinePlaysBlack:
3410 case IcsPlayingWhite:
3411 if (appData.zippyPlay) return FALSE;
3413 DisplayMoveError(_("You are playing White"));
3419 if (!white_piece && WhiteOnMove(currentMove)) {
3420 DisplayMoveError(_("It is White's turn"));
3423 if (white_piece && !WhiteOnMove(currentMove)) {
3424 DisplayMoveError(_("It is Black's turn"));
3427 if (cmailMsgLoaded && (currentMove < cmailOldMove)) {
3428 /* Editing correspondence game history */
3429 /* Could disallow this or prompt for confirmation */
3432 if (currentMove < forwardMostMove) {
3433 /* Discarding moves */
3434 /* Could prompt for confirmation here,
3435 but I don't think that's such a good idea */
3436 forwardMostMove = currentMove;
3440 case BeginningOfGame:
3441 if (appData.icsActive) return FALSE;
3442 if (!appData.noChessProgram) {
3444 DisplayMoveError(_("You are playing White"));
3451 if (!white_piece && WhiteOnMove(currentMove)) {
3452 DisplayMoveError(_("It is White's turn"));
3455 if (white_piece && !WhiteOnMove(currentMove)) {
3456 DisplayMoveError(_("It is Black's turn"));
3465 if (currentMove != forwardMostMove && gameMode != AnalyzeMode
3466 && gameMode != AnalyzeFile && gameMode != Training) {
3467 DisplayMoveError(_("Displayed position is not current"));
3473 FILE *lastLoadGameFP = NULL, *lastLoadPositionFP = NULL;
3474 int lastLoadGameNumber = 0, lastLoadPositionNumber = 0;
3475 int lastLoadGameUseList = FALSE;
3476 char lastLoadGameTitle[MSG_SIZ], lastLoadPositionTitle[MSG_SIZ];
3477 ChessMove lastLoadGameStart = (ChessMove) 0;
3481 UserMoveEvent(fromX, fromY, toX, toY, promoChar)
3482 int fromX, fromY, toX, toY;
3487 if (fromX < 0 || fromY < 0) return;
3488 if ((fromX == toX) && (fromY == toY)) {
3492 /* Check if the user is playing in turn. This is complicated because we
3493 let the user "pick up" a piece before it is his turn. So the piece he
3494 tried to pick up may have been captured by the time he puts it down!
3495 Therefore we use the color the user is supposed to be playing in this
3496 test, not the color of the piece that is currently on the starting
3497 square---except in EditGame mode, where the user is playing both
3498 sides; fortunately there the capture race can't happen. (It can
3499 now happen in IcsExamining mode, but that's just too bad. The user
3500 will get a somewhat confusing message in that case.)
3504 case PlayFromGameFile:
3506 case TwoMachinesPlay:
3510 /* We switched into a game mode where moves are not accepted,
3511 perhaps while the mouse button was down. */
3514 case MachinePlaysWhite:
3515 /* User is moving for Black */
3516 if (WhiteOnMove(currentMove)) {
3517 DisplayMoveError(_("It is White's turn"));
3522 case MachinePlaysBlack:
3523 /* User is moving for White */
3524 if (!WhiteOnMove(currentMove)) {
3525 DisplayMoveError(_("It is Black's turn"));
3532 case BeginningOfGame:
3535 if ((int) boards[currentMove][fromY][fromX] >= (int) BlackPawn &&
3536 (int) boards[currentMove][fromY][fromX] <= (int) BlackKing) {
3537 /* User is moving for Black */
3538 if (WhiteOnMove(currentMove)) {
3539 DisplayMoveError(_("It is White's turn"));
3543 /* User is moving for White */
3544 if (!WhiteOnMove(currentMove)) {
3545 DisplayMoveError(_("It is Black's turn"));
3551 case IcsPlayingBlack:
3552 /* User is moving for Black */
3553 if (WhiteOnMove(currentMove)) {
3554 if (!appData.premove) {
3555 DisplayMoveError(_("It is White's turn"));
3556 } else if (toX >= 0 && toY >= 0) {
3559 premoveFromX = fromX;
3560 premoveFromY = fromY;
3561 premovePromoChar = promoChar;
3563 if (appData.debugMode)
3564 fprintf(debugFP, "Got premove: fromX %d,"
3565 "fromY %d, toX %d, toY %d\n",
3566 fromX, fromY, toX, toY);
3572 case IcsPlayingWhite:
3573 /* User is moving for White */
3574 if (!WhiteOnMove(currentMove)) {
3575 if (!appData.premove) {
3576 DisplayMoveError(_("It is Black's turn"));
3577 } else if (toX >= 0 && toY >= 0) {
3580 premoveFromX = fromX;
3581 premoveFromY = fromY;
3582 premovePromoChar = promoChar;
3584 if (appData.debugMode)
3585 fprintf(debugFP, "Got premove: fromX %d,"
3586 "fromY %d, toX %d, toY %d\n",
3587 fromX, fromY, toX, toY);
3597 if (toX == -2 || toY == -2) {
3598 boards[0][fromY][fromX] = EmptySquare;
3599 DrawPosition(FALSE, boards[currentMove]);
3600 } else if (toX >= 0 && toY >= 0) {
3601 boards[0][toY][toX] = boards[0][fromY][fromX];
3602 boards[0][fromY][fromX] = EmptySquare;
3603 DrawPosition(FALSE, boards[currentMove]);
3608 if (toX < 0 || toY < 0) return;
3609 userOfferedDraw = FALSE;
3611 if (appData.testLegality) {
3612 moveType = LegalityTest(boards[currentMove], PosFlags(currentMove),
3613 EP_UNKNOWN, fromY, fromX, toY, toX, promoChar);
3614 if (moveType == IllegalMove || moveType == ImpossibleMove) {
3615 DisplayMoveError(_("Illegal move"));
3619 moveType = PromoCharToMoveType(WhiteOnMove(currentMove), promoChar);
3622 if (gameMode == Training) {
3623 /* compare the move played on the board to the next move in the
3624 * game. If they match, display the move and the opponent's response.
3625 * If they don't match, display an error message.
3629 CopyBoard(testBoard, boards[currentMove]);
3630 ApplyMove(fromX, fromY, toX, toY, promoChar, testBoard);
3632 if (CompareBoards(testBoard, boards[currentMove+1])) {
3633 ForwardInner(currentMove+1);
3635 /* Autoplay the opponent's response.
3636 * if appData.animate was TRUE when Training mode was entered,
3637 * the response will be animated.
3639 saveAnimate = appData.animate;
3640 appData.animate = animateTraining;
3641 ForwardInner(currentMove+1);
3642 appData.animate = saveAnimate;
3644 /* check for the end of the game */
3645 if (currentMove >= forwardMostMove) {
3646 gameMode = PlayFromGameFile;
3648 SetTrainingModeOff();
3649 DisplayInformation(_("End of game"));
3652 DisplayError(_("Incorrect move"), 0);
3657 FinishMove(moveType, fromX, fromY, toX, toY, promoChar);
3660 /* Common tail of UserMoveEvent and DropMenuEvent */
3662 FinishMove(moveType, fromX, fromY, toX, toY, promoChar)
3664 int fromX, fromY, toX, toY;
3665 /*char*/int promoChar;
3667 /* Ok, now we know that the move is good, so we can kill
3668 the previous line in Analysis Mode */
3669 if (gameMode == AnalyzeMode && currentMove < forwardMostMove) {
3670 forwardMostMove = currentMove;
3673 /* If we need the chess program but it's dead, restart it */
3674 ResurrectChessProgram();
3676 /* A user move restarts a paused game*/
3680 thinkOutput[0] = NULLCHAR;
3682 MakeMove(fromX, fromY, toX, toY, promoChar); /*updates forwardMostMove*/
3684 if (gameMode == BeginningOfGame) {
3685 if (appData.noChessProgram) {
3686 gameMode = EditGame;
3690 gameMode = MachinePlaysBlack;
3692 sprintf(buf, "%s vs. %s", gameInfo.white, gameInfo.black);
3694 if (first.sendName) {
3695 sprintf(buf, "name %s\n", gameInfo.white);
3696 SendToProgram(buf, &first);
3702 /* Relay move to ICS or chess engine */
3703 if (appData.icsActive) {
3704 if (gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack ||
3705 gameMode == IcsExamining) {
3706 SendMoveToICS(moveType, fromX, fromY, toX, toY);
3710 if (first.sendTime && (gameMode == BeginningOfGame ||
3711 gameMode == MachinePlaysWhite ||
3712 gameMode == MachinePlaysBlack)) {
3713 SendTimeRemaining(&first, gameMode != MachinePlaysBlack);
3715 SendMoveToProgram(forwardMostMove-1, &first);
3716 if (gameMode != EditGame && gameMode != PlayFromGameFile) {
3717 first.maybeThinking = TRUE;
3719 if (currentMove == cmailOldMove + 1) {
3720 cmailMoveType[lastLoadGameNumber - 1] = CMAIL_MOVE;
3724 ShowMove(fromX, fromY, toX, toY); /*updates currentMove*/
3728 switch (MateTest(boards[currentMove], PosFlags(currentMove),
3734 if (WhiteOnMove(currentMove)) {
3735 GameEnds(BlackWins, "Black mates", GE_PLAYER);
3737 GameEnds(WhiteWins, "White mates", GE_PLAYER);
3741 GameEnds(GameIsDrawn, "Stalemate", GE_PLAYER);
3746 case MachinePlaysBlack:
3747 case MachinePlaysWhite:
3748 /* disable certain menu options while machine is thinking */
3749 SetMachineThinkingEnables();
3758 HandleMachineMove(message, cps)
3760 ChessProgramState *cps;
3762 char machineMove[MSG_SIZ], buf1[MSG_SIZ*10], buf2[MSG_SIZ];
3763 char realname[MSG_SIZ];
3764 int fromX, fromY, toX, toY;
3771 * Kludge to ignore BEL characters
3773 while (*message == '\007') message++;
3776 * Look for book output
3778 if (cps == &first && bookRequested) {
3779 if (message[0] == '\t' || message[0] == ' ') {
3780 /* Part of the book output is here; append it */
3781 strcat(bookOutput, message);
3782 strcat(bookOutput, " \n");
3784 } else if (bookOutput[0] != NULLCHAR) {
3785 /* All of book output has arrived; display it */
3786 char *p = bookOutput;
3787 while (*p != NULLCHAR) {
3788 if (*p == '\t') *p = ' ';
3791 DisplayInformation(bookOutput);
3792 bookRequested = FALSE;
3793 /* Fall through to parse the current output */
3798 * Look for machine move.
3800 if ((sscanf(message, "%s %s %s", buf1, buf2, machineMove) == 3 &&
3801 strcmp(buf2, "...") == 0) ||
3802 (sscanf(message, "%s %s", buf1, machineMove) == 2 &&
3803 strcmp(buf1, "move") == 0)) {
3805 /* This method is only useful on engines that support ping */
3806 if (cps->lastPing != cps->lastPong) {
3807 if (gameMode == BeginningOfGame) {
3808 /* Extra move from before last new; ignore */
3809 if (appData.debugMode) {
3810 fprintf(debugFP, "Ignoring extra move from %s\n", cps->which);
3813 if (appData.debugMode) {
3814 fprintf(debugFP, "Undoing extra move from %s, gameMode %d\n",
3815 cps->which, gameMode);
3817 SendToProgram("undo\n", cps);
3823 case BeginningOfGame:
3824 /* Extra move from before last reset; ignore */
3825 if (appData.debugMode) {
3826 fprintf(debugFP, "Ignoring extra move from %s\n", cps->which);
3833 /* Extra move after we tried to stop. The mode test is
3834 not a reliable way of detecting this problem, but it's
3835 the best we can do on engines that don't support ping.
3837 if (appData.debugMode) {
3838 fprintf(debugFP, "Undoing extra move from %s, gameMode %d\n",
3839 cps->which, gameMode);
3841 SendToProgram("undo\n", cps);
3844 case MachinePlaysWhite:
3845 case IcsPlayingWhite:
3846 machineWhite = TRUE;
3849 case MachinePlaysBlack:
3850 case IcsPlayingBlack:
3851 machineWhite = FALSE;
3854 case TwoMachinesPlay:
3855 machineWhite = (cps->twoMachinesColor[0] == 'w');
3858 if (WhiteOnMove(forwardMostMove) != machineWhite) {
3859 if (appData.debugMode) {
3861 "Ignoring move out of turn by %s, gameMode %d"
3862 ", forwardMost %d\n",
3863 cps->which, gameMode, forwardMostMove);
3868 if (!ParseOneMove(machineMove, forwardMostMove, &moveType,
3869 &fromX, &fromY, &toX, &toY, &promoChar)) {
3870 /* Machine move could not be parsed; ignore it. */
3871 sprintf(buf1, _("Illegal move \"%s\" from %s machine"),
3872 machineMove, cps->which);
3873 DisplayError(buf1, 0);
3874 if (gameMode == TwoMachinesPlay) {
3875 GameEnds(machineWhite ? BlackWins : WhiteWins,
3876 "Forfeit due to illegal move", GE_XBOARD);
3881 hintRequested = FALSE;
3882 lastHint[0] = NULLCHAR;
3883 bookRequested = FALSE;
3884 /* Program may be pondering now */
3885 cps->maybeThinking = TRUE;
3886 if (cps->sendTime == 2) cps->sendTime = 1;
3887 if (cps->offeredDraw) cps->offeredDraw--;
3890 if ((gameMode == IcsPlayingWhite || gameMode == IcsPlayingBlack) &&
3892 SendMoveToICS(moveType, fromX, fromY, toX, toY);
3896 /* currentMoveString is set as a side-effect of ParseOneMove */
3897 strcpy(machineMove, currentMoveString);
3898 strcat(machineMove, "\n");
3899 strcpy(moveList[forwardMostMove], machineMove);
3901 MakeMove(fromX, fromY, toX, toY, promoChar);/*updates forwardMostMove*/
3903 if (gameMode == TwoMachinesPlay) {
3904 if (cps->other->sendTime) {
3905 SendTimeRemaining(cps->other,
3906 cps->other->twoMachinesColor[0] == 'w');
3908 SendMoveToProgram(forwardMostMove-1, cps->other);