"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "src/frontend/cmd.cc" between
gnuchess-6.2.8.tar.gz and gnuchess-6.2.9.tar.gz

About: GNU Chess is a chess-playing program.

cmd.cc  (gnuchess-6.2.8):cmd.cc  (gnuchess-6.2.9)
/* cmd.cc /* cmd.cc
GNU Chess frontend GNU Chess frontend
Copyright (C) 2001-2020 Free Software Foundation, Inc. Copyright (C) 2001-2021 Free Software Foundation, Inc.
GNU Chess is based on the two research programs GNU Chess is based on the two research programs
Cobalt by Chua Kong-Sian and Gazebo by Stuart Cracraft. Cobalt by Chua Kong-Sian and Gazebo by Stuart Cracraft.
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
skipping to change at line 62 skipping to change at line 62
*/ */
#define TOKENS 3 #define TOKENS 3
static char *token[TOKENS]; static char *token[TOKENS];
char *endptr; char *endptr;
static int hardFlag=0; static int hardFlag=0;
static int postFlag=0; static int postFlag=0;
static const char setboard_cmd[] = "setboard ";
static void split_input(void) static void split_input(void)
{ {
/* r points to the last non-space character */ /* r points to the last non-space character */
char *s, *r; char *s, *r;
int k; int k;
for (k = 0, s = r = inputstr; k < TOKENS; ++k) { for (k = 0, s = r = inputstr; k < TOKENS; ++k) {
/* Skip leading whitespace */ /* Skip leading whitespace */
while (isspace(*s)) s++; while (isspace(*s)) s++;
token[k] = s; token[k] = s;
skipping to change at line 95 skipping to change at line 97
*/ */
static int tokeneq(const char *s, const char *t) static int tokeneq(const char *s, const char *t)
{ {
while (*s && *t && !isspace(*s) && !isspace(*t)) { while (*s && *t && !isspace(*s) && !isspace(*t)) {
if (*s++ != *t++) return 0; if (*s++ != *t++) return 0;
} }
return (!*s || isspace(*s)) && (!*t || isspace(*t)); return (!*s || isspace(*s)) && (!*t || isspace(*t));
} }
/* /*
* Reads a PGN file and returns the equivalent EPD content * Remove a trailing \n and return NULL if last character is not \n.
*
* The conversion relies on a temporary file in EPD format,
* which is removed afterwards.
*/ */
static char *load_pgn_as_epd( const char *pgn_filename, char *epdline, int showh eading ) static char *trim_newline(char *line)
{ {
FILE *epdfile=NULL; if (line == NULL) {
char tmp_epd[]=".tmp.epd"; return NULL;
}
const size_t line_len = strlen(line);
const size_t last_char_index = strlen(line) - 1;
if (line_len <= 0 || line[last_char_index] != '\n') {
return NULL;
}
line[last_char_index] = '\0';
return line;
}
PGNReadFromFile (pgn_filename, showheading); /*
SaveEPD( tmp_epd ); * Takes an EPD filename as input and returns the contents as a
epdfile = fopen( tmp_epd, "r" ); * 'setboard <epd-position>' command.
char *s = fgets( epdline, MAXSTR, epdfile ); */
fclose( epdfile ); static char *build_setboard_cmd_from_epd_file(char *data, const char *epd_filena
remove( tmp_epd ); me, unsigned int data_len)
{
char *result = NULL;
char *epdline = (char *)calloc(data_len, sizeof(char));
if (epdline == NULL) {
return NULL;
}
FILE *epdfile = fopen(epd_filename, "r");
if (epdfile == NULL) {
return NULL;
}
if (fgets(epdline, data_len, epdfile) && trim_newline(epdline) && strlen(setbo
ard_cmd) + strlen(epdline) < data_len) {
strcpy(data, setboard_cmd);
strcat(data, epdline);
result = data;
}
fclose(epdfile);
free(epdline);
return s; return result;
}
/*
* Takes a PGN filename as input and returns the contents as a
* 'setboard <epd-position>' command.
*/
static char *build_setboard_cmd_from_pgn_file(char *data, const char *pgn_filena
me, unsigned int data_len)
{
char *result = NULL;
char *epdline = (char *)calloc(data_len, sizeof(char));
if (epdline == NULL) {
return NULL;
}
PGNReadFromFile (pgn_filename, 0);
EPD2str(epdline);
if (strlen(setboard_cmd) + strlen(epdline) < data_len) {
strcpy(data, setboard_cmd);
strcat(data, epdline);
result = data;
}
free(epdline);
return result;
}
/*
* Loads a PGN file. Returns 1 on success, 0 on error.
*/
static int pgnload(const char *pgn_filename)
{
int success;
char data[MAXSTR]="";
if (build_setboard_cmd_from_pgn_file(data, pgn_filename, sizeof(data))) {
SetDataToEngine( data );
SetAutoGo( true );
success = 1;
} else {
printf( _("Error loading PGN file '%s'.\n"), pgn_filename );
success = 0;
}
return success;
} }
void cmd_accepted(void) void cmd_accepted(void)
{ {
SetDataToEngine( token[0] ); SetDataToEngine( token[0] );
} }
void cmd_activate(void) void cmd_activate(void)
{ {
printf( _("Command 'activate' is currently not supported.\n") ); printf( _("Command 'activate' is currently not supported.\n") );
skipping to change at line 319 skipping to change at line 387
printf(_("score - list by GNU best result first\n")); printf(_("score - list by GNU best result first\n"));
printf(_("reverse - list by GNU worst result first\n")); printf(_("reverse - list by GNU worst result first\n"));
} else { } else {
if (token[1][0] == '\0') DBListPlayer("rscore"); if (token[1][0] == '\0') DBListPlayer("rscore");
else DBListPlayer(token[1]); else DBListPlayer(token[1]);
} }
} }
void cmd_load(void) void cmd_load(void)
{ {
char *epd_filename = token[1];
char data[MAXSTR]=""; char data[MAXSTR]="";
LoadEPD (token[1]); LoadEPD (epd_filename);
pgnloaded = 0; pgnloaded = 0;
check_board(); check_board();
if (!ValidateBoard()) { if (!ValidateBoard()) {
SET (flags, ENDED); SET (flags, ENDED);
printf (_("Board is wrong!\n")); printf (_("Board is wrong!\n"));
} else { } else {
/* Read EPD file and send contents to engine */ /* Read EPD file and send contents to engine */
FILE *epdfile = fopen( token[1], "r" ); if (build_setboard_cmd_from_epd_file(data, epd_filename, strlen(data))) {
char epdline[MAXSTR]=""; SetDataToEngine( data );
if ( epdfile == NULL ) { SetAutoGo( true );
printf(_("Error reading file '%s'.\n"), token[1] );
} else { } else {
if ( fgets( epdline, MAXSTR, epdfile ) == NULL ) { printf( _("Error loading EPD file '%s'.\n"), epd_filename );
printf(_("Error reading file '%s'.\n"), token[1] );
} else {
const char setboardCmd[] = "setboard ";
unsigned int setboardLen = strlen(setboardCmd);
strcpy( data, setboardCmd );
int i=0;
while ( epdline[i] != '\n' ) {
if (i + setboardLen < MAXSTR - 1) {
data[i+setboardLen] = epdline[i];
++i;
} else {
printf(_("Error reading contents of file '%s'.\n"), token[1] );
break;
}
}
data[i+setboardLen] = '\0';
SetDataToEngine( data );
SetAutoGo( true );
}
} }
} }
} }
void cmd_manual(void) void cmd_manual(void)
{ {
SET (flags, MANUAL); SET (flags, MANUAL);
ExpectAnswerFromEngine( false ); ExpectAnswerFromEngine( false );
SetDataToEngine( "force" ); SetDataToEngine( "force" );
} }
skipping to change at line 470 skipping to change at line 519
/* /*
* Load a file containing a game in PGN format. * Load a file containing a game in PGN format.
* *
* The file contents will be passed on to the adapter * The file contents will be passed on to the adapter
* in EPD notation (the adapter expectes FEN actually, * in EPD notation (the adapter expectes FEN actually,
* but EPD and FEN are similar (possible issue here?), * but EPD and FEN are similar (possible issue here?),
* hence a PGN -> EPD conversion in done first. * hence a PGN -> EPD conversion in done first.
*/ */
void cmd_pgnload(void) void cmd_pgnload(void)
{ {
char data[MAXSTR]=""; pgnload(token[1]);
char epdline[MAXSTR]="";
char *s = load_pgn_as_epd( token[1], epdline, 0 );
if ( s == NULL ) {
printf( _("Incorrect epd file.\n") );
return;
}
strcpy( data, "setboard " );
int i=0;
while ( epdline[i] != '\n' ) {
data[i+9] = epdline[i];
++i;
}
data[i+9] = '\0';
SetDataToEngine( data );
SetAutoGo( true );
pgnloaded = 0;
} }
/* See comment above in cmd_pgnload about PGN -> EPD conversion. */ /* See comment above in cmd_pgnload about PGN -> EPD conversion. */
void cmd_pgnreplay(void) void cmd_pgnreplay(void)
{ {
char data[MAXSTR]=""; if (!pgnload(token[1])) {
char epdline[MAXSTR]="";
char *s = load_pgn_as_epd( token[1], epdline, 1 );
if ( s == NULL ) {
printf( _("Incorrect epd file.\n") );
return; return;
} }
strcpy( data, "setboard " );
int i=0;
while ( epdline[i] != '\n' ) {
data[i+9] = epdline[i];
++i;
}
data[i+9] = '\0';
SetDataToEngine( data );
SetAutoGo( true );
pgnloaded = 1; pgnloaded = 1;
pgncnt = GameCnt; pgncnt = GameCnt;
while (GameCnt >= 0) { while (GameCnt >= 0) {
if (GameCnt >= 0) { if (GameCnt >= 0) {
CLEAR (flags, ENDED); CLEAR (flags, ENDED);
CLEAR (flags, TIMEOUT); CLEAR (flags, TIMEOUT);
ChangeColor( true ); ChangeColor( true );
SetAutoGo( true ); SetAutoGo( true );
UnmakeMove (board.side, &Game[GameCnt].move); UnmakeMove (board.side, &Game[GameCnt].move);
skipping to change at line 842 skipping to change at line 856
fputs( _("\ fputs( _("\
Options xboard and post are accepted without leading dashes\n\ Options xboard and post are accepted without leading dashes\n\
for backward compatibility.\n\ for backward compatibility.\n\
\n"), stdout ); \n"), stdout );
fputs( _("\ fputs( _("\
Moves are accepted either in standard algebraic notation (SAN) or\n\ Moves are accepted either in standard algebraic notation (SAN) or\n\
in coordinate algebraic notation.\n\ in coordinate algebraic notation.\n\
\n"), stdout ); \n"), stdout );
fputs( _("\ fputs( _("\
The file 'gnuchess.ini' allows setting config options if --uci is not\n\ The file 'gnuchess.ini' allows setting config options if --uci is not\n\
used. See 'info gnuchess' for details. The file is looked in three\n\ used. See 'info gnuchess' for details. The file is looked for in three\n\
locations according to this precedence: current directory, the\n\ locations according to this precedence: current directory, the\n\
directory pointed to by environment variable GNUCHESS_PKGDATADIR,\n\ directory pointed to by environment variable GNUCHESS_PKGDATADIR,\n\
or the package data directory stated at configure time.\n\ or the package data directory stated at configure time.\n\
\n"), stdout ); \n"), stdout );
fputs( _("\ fputs( _("\
Report bugs to <bug-gnu-chess@gnu.org>.\n\ Report bugs to <bug-gnu-chess@gnu.org>.\n\
\n"), stdout ); \n"), stdout );
} }
/* Play variant, we instruct interface in protover we play normal */ /* Play variant, we instruct interface in protover we play normal */
 End of changes. 15 change blocks. 
78 lines changed or deleted 95 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)