Implement option complex for installing engines
authorH.G. Muller <h.g.muller@hccnet.nl>
Fri, 25 Oct 2013 12:39:23 +0000 (14:39 +0200)
committerH.G. Muller <h.g.muller@hccnet.nl>
Sat, 4 Jan 2014 11:49:01 +0000 (12:49 +0100)
Options are added to allow XBoard to be called to install engines in the
user settings files. With -addMasterOption a line can be added to the
master settings file, and this will be automatically be preceded by a
-date option to record the date it was added. This is intended for adding
-installEngine options to the master file, in order to 'broadcast' the
availability of the engine to the users.
  We now store the date in the user settings file, through a new -saveDate
option (as integer). This allows XBoard to relate the date of the saved
settings to -date stamps in the master settings file, and thus decide
if the latter (and install options that follow it) are seen for the first
time, or not. Depending on that it can decide if the install option should
be ignored.
  The volatile Boolean optio -autoClose causes XBoard to save its settings
and exit before bringing up its GUI (but after processing the command line).
This is added to facilitate calling XBoard from engine-install scripts,
with an -addMasterOption argument.

args.h
gtk/xboard.c
xaw/xboard.c

diff --git a/args.h b/args.h
index 2d2fc1d..3a1347a 100644 (file)
--- a/args.h
+++ b/args.h
@@ -60,7 +60,7 @@
 typedef enum {
   ArgString, ArgInt, ArgFloat, ArgBoolean, ArgTrue, ArgFalse, ArgNone,
   ArgColor, ArgAttribs, ArgFilename, ArgBoardSize, ArgFont, ArgCommSettings,
-  ArgSettingsFilename, ArgBackupSettingsFile, ArgTwo, ArgInstall,
+  ArgSettingsFilename, ArgBackupSettingsFile, ArgTwo, ArgInstall, ArgMaster,
   ArgX, ArgY, ArgZ // [HGM] placement: for window-placement options stored relative to main window
 } ArgType;
 
@@ -102,7 +102,10 @@ typedef struct {
 IcsTextMenuEntry icsTextMenuEntry[ICS_TEXT_MENU_SIZE];
 
 int junk;
+int saveDate;
+int dateStamp;
 Boolean singleList;
+Boolean autoClose;
 char *homeDir;
 char *firstEngineLine;
 char *secondEngineLine;
@@ -149,6 +152,9 @@ ArgDescriptor argDescriptors[] = {
   { "loadGameFile", ArgFilename, (void *) &appData.loadGameFile, FALSE, INVALID },
   { "", ArgNone, NULL, FALSE, INVALID },
   /* keyword arguments */
+  { "saveDate", ArgInt, (void *) &saveDate, TRUE, 0 },
+  { "date", ArgInt, (void *) &dateStamp, FALSE, 0 },
+  { "autoClose", ArgTrue, (void *) &autoClose, FALSE, FALSE },
   JAWS_ARGS
   { "whitePieceColor", ArgColor, (void *) 0, TRUE, (ArgIniType) WHITE_PIECE_COLOR },
   { "wpc", ArgColor, (void *) 0, FALSE, INVALID },
@@ -507,6 +513,7 @@ ArgDescriptor argDescriptors[] = {
   { "secondChessProgramNames", ArgString, (void *) &secondChessProgramNames,
     !XBOARD, (ArgIniType) SCP_NAMES },
   { "themeNames", ArgString, (void *) &appData.themeNames, !XBOARD, (ArgIniType) "native -upf false -ub false -ubt false -pid \"\"\n" },
+  { "addMasterOption", ArgMaster, NULL, FALSE, INVALID },
   { "installEngine", ArgInstall, (void *) &firstChessProgramNames, FALSE, (ArgIniType) "" },
   { "initialMode", ArgString, (void *) &appData.initialMode, FALSE, (ArgIniType) "" },
   { "mode", ArgString, (void *) &appData.initialMode, FALSE, INVALID },
@@ -825,6 +832,30 @@ ExitArgError(char *msg, char *badArg, Boolean quit)
   exit(2);
 }
 
+void
+AppendToSettingsFile (char *line)
+{
+  char buf[MSG_SIZ];
+  FILE *f;
+  int c;
+  if(f = fopen(SETTINGS_FILE, "r")) {
+    do {
+      int i = 0;
+      while((buf[i] = c = fgetc(f)) != '\n' && c != EOF) if(i < MSG_SIZ-1) i++;
+      buf[i] = NULLCHAR;
+      if(!strcmp(line, buf)) return; // line occurs
+    } while(c != EOF);
+    // line did not occur; add it
+    fclose(f);
+    if(f = fopen(SETTINGS_FILE, "a")) {
+      TimeMark now;
+      GetTimeMark(&now);
+      fprintf(f, "-date %ld\n%s\n", now.sec, line);
+      fclose(f);
+    }
+  }
+}
+
 int
 ValidateInt(char *s)
 {
@@ -1177,9 +1208,13 @@ ParseArgs(GetFunc get, void *cl)
       ParseCommPortSettings(argValue);
       break;
 
+    case ArgMaster:
+      AppendToSettingsFile(argValue);
+      break;
+
     case ArgInstall:
       q = *(char **) ad->argLoc;
-      if((strcmp(version, VERSION) || autoClose) && !strstr(q, argValue) ) {
+      if((saveDate == 0 || saveDate - dateStamp < 0) && !strstr(q, argValue) ) {
         int l = strlen(q) + strlen(argValue);
         *(char **) ad->argLoc = malloc(l+2);
         snprintf(*(char **) ad->argLoc, l+2, "%s%s\n", q, argValue);
@@ -1422,6 +1457,11 @@ InitAppData(char *lpCmdLine)
     appData.savePositionFile = strdup(buf);
   }
 
+  if(autoClose) { // was called for updating settingsfile only
+    if(saveSettingsOnExit) SaveSettings(settingsFileName);
+    exit(0);
+  }
+
   /* Finish initialization for fonts and sounds */
   CreateFonts();
 
@@ -1452,8 +1492,11 @@ SaveSettings(char* name)
   ArgDescriptor *ad;
   char dir[MSG_SIZ], buf[MSG_SIZ];
   int mps = appData.movesPerSession;
+  TimeMark now;
+
+  if (!MainWindowUp() && !autoClose) return;
 
-  if (!MainWindowUp()) return;
+  GetTimeMark(&now); saveDate = now.sec;
 
   GetCurrentDirectory(MSG_SIZ, dir);
   if(MySearchPath(installDir, name, buf)) {
@@ -1571,6 +1614,7 @@ SaveSettings(char* name)
     case ArgNone:
     case ArgBackupSettingsFile:
     case ArgSettingsFilename: ;
+    case ArgMaster: ;
     case ArgInstall: ;
     }
   }
index 33aa548..7711047 100644 (file)
@@ -663,6 +663,7 @@ PrintArg (ArgType t)
     case ArgTwo:
     case ArgNone:
     case ArgCommSettings:
+    default:
       break;
   }
   return p;
index b88c445..1b13b7c 100644 (file)
@@ -819,6 +819,7 @@ PrintArg (ArgType t)
     case ArgTwo:
     case ArgNone:
     case ArgCommSettings:
+    case ArgMaster:
     case ArgInstall:
       break;
   }