changeset: 6841:42fee7585fae
user:      Damien Riegel <[email protected]>
date:      Sun Nov 13 18:45:35 2016 -0800
link:      http://dev.mutt.org/hg/mutt/rev/42fee7585fae

search muttrc file according to XDG Base Specification (closes #3207)

First of all, the MUTT_VERSION symbol is now concatenated (when
possible) at compile time.

Then, the logic to find the config file has been changed a bit to remove
unnecessary calls to access(), so now each possible locations for the
config file is only tested once, and it stops as soon as a valid one has
been found. So instead of:

  access("/home/dkc/.muttrc-1.7.1", F_OK) = -1 ENOENT (No such file or 
directory)
  access("/home/dkc/.muttrc", F_OK)       = 0
  access("/home/dkc/.muttrc", F_OK)       = 0
  access("/home/dkc/.muttrc", F_OK)       = 0
  [... Tests for Muttrc ... ]
  access("/home/dkc/.muttrc", F_OK)       = 0

We now have:

  access("/home/dkc/.muttrc-1.7+13 (f658e517960e)", F_OK) = -1 ENOENT (No such 
file or directory)
  access("/home/dkc/.muttrc", F_OK)       = 0

It also cleans up the case where -F is passed on the command line but
points to a non-existent file by moving the error path closer to the
actual fail condition.

Finally, it adds partial support for the XDG Base Directory
Specification. mutt will now try to locate its config at:

  $XDG_CONFIG_HOME/mutt/muttrc-MUTT_VERSION
  $XDG_CONFIG_HOME/mutt/muttrc.

If XDG_CONFIG_HOME is not set, it will use '~/.config' as a default.

diffs (237 lines):

diff -r d18482f6641e -r 42fee7585fae commands.c
--- a/commands.c        Tue Nov 08 12:42:56 2016 -0800
+++ b/commands.c        Sun Nov 13 18:45:35 2016 -0800
@@ -879,7 +879,7 @@
 
 void mutt_version (void)
 {
-  mutt_message ("Mutt %s (%s)", MUTT_VERSION, ReleaseDate);
+  mutt_message ("Mutt " MUTT_VERSION " (%s)", ReleaseDate);
 }
 
 void mutt_edit_content_type (HEADER *h, BODY *b, FILE *fp)
diff -r d18482f6641e -r 42fee7585fae compose.c
--- a/compose.c Tue Nov 08 12:42:56 2016 -0800
+++ b/compose.c Sun Nov 13 18:45:35 2016 -0800
@@ -448,8 +448,7 @@
       break;
 
     case 'v':
-      snprintf (fmt, sizeof (fmt), "Mutt %%s");
-      snprintf (buf, buflen, fmt, MUTT_VERSION);
+      strfcpy(buf, "Mutt " MUTT_VERSION, buflen);
       break;
 
     case 0:
diff -r d18482f6641e -r 42fee7585fae dotlock.c
--- a/dotlock.c Tue Nov 08 12:42:56 2016 -0800
+++ b/dotlock.c Sun Nov 13 18:45:35 2016 -0800
@@ -339,7 +339,7 @@
 static void 
 usage (const char *av0)
 {
-  fprintf (stderr, "dotlock [Mutt %s (%s)]\n", MUTT_VERSION, ReleaseDate);
+  fprintf (stderr, "dotlock [Mutt " MUTT_VERSION " (%s)]\n", ReleaseDate);
   fprintf (stderr, "usage: %s [-t|-f|-u|-d] [-p] [-r <retries>] file\n",
          av0);
 
diff -r d18482f6641e -r 42fee7585fae init.c
--- a/init.c    Tue Nov 08 12:42:56 2016 -0800
+++ b/init.c    Sun Nov 13 18:45:35 2016 -0800
@@ -2851,8 +2851,8 @@
   if ((debugfile = safe_fopen(buf, "w")) != NULL)
   {
     setbuf (debugfile, NULL); /* don't buffer the debugging output! */
-    dprint(1,(debugfile,"Mutt/%s (%s) debugging at level %d\n",
-             MUTT_VERSION, ReleaseDate, debuglevel));
+    dprint(1,(debugfile,"Mutt/" MUTT_VERSION " (%s) debugging at level %d\n",
+             ReleaseDate, debuglevel));
   }
 }
 #endif
@@ -2899,13 +2899,53 @@
   srandom(seed);
 }
 
+static char* mutt_find_cfg (const char *home, const char *xdg_cfg_home)
+{
+  const char* names[] =
+  {
+    "muttrc-" MUTT_VERSION,
+    "muttrc",
+    NULL,
+  };
+
+  const char* locations[][2] =
+  {
+    { home, ".", },
+    { home, ".mutt/" },
+    { xdg_cfg_home, "mutt/", },
+    { NULL, NULL },
+  };
+
+  int i;
+
+  for (i = 0; locations[i][0] && locations[i][1]; i++)
+  {
+    int j;
+
+    if (!locations[i][0])
+      continue;
+
+    for (j = 0; names[j]; j++)
+    {
+      char buffer[STRING];
+
+      snprintf (buffer, sizeof (buffer),
+                "%s/%s%s", locations[i][0], locations[i][1], names[j]);
+      if (access (buffer, F_OK) == 0)
+        return safe_strdup(buffer);
+    }
+  }
+
+  return NULL;
+}
+
 void mutt_init (int skip_sys_rc, LIST *commands)
 {
   struct passwd *pw;
   struct utsname utsname;
   char *p, buffer[STRING];
   char *domain = NULL;
-  int i, default_rc = 0, need_pause = 0;
+  int i, need_pause = 0;
   BUFFER err;
 
   mutt_buffer_init (&err);
@@ -3121,24 +3161,18 @@
    * 
    * 
    */
-  
-  
-  
-  
+
   if (!Muttrc)
   {
-    snprintf (buffer, sizeof(buffer), "%s/.muttrc-%s", NONULL(Homedir), 
MUTT_VERSION);
-    if (access(buffer, F_OK) == -1)
-      snprintf (buffer, sizeof(buffer), "%s/.muttrc", NONULL(Homedir));
-    if (access(buffer, F_OK) == -1)
-      snprintf (buffer, sizeof (buffer), "%s/.mutt/muttrc-%s", 
NONULL(Homedir), MUTT_VERSION);
-    if (access(buffer, F_OK) == -1)
-      snprintf (buffer, sizeof (buffer), "%s/.mutt/muttrc", NONULL(Homedir));
-    if (access(buffer, F_OK) == -1) /* default to .muttrc for alias_file */
-      snprintf (buffer, sizeof(buffer), "%s/.muttrc", NONULL(Homedir));
-
-    default_rc = 1;
-    Muttrc = safe_strdup (buffer);
+    char *xdg_cfg_home = getenv ("XDG_CONFIG_HOME");
+
+    if (!xdg_cfg_home && Homedir)
+    {
+      snprintf (buffer, sizeof (buffer), "%s/.config", Homedir);
+      xdg_cfg_home = buffer;
+    }
+
+    Muttrc = mutt_find_cfg (Homedir, xdg_cfg_home);
   }
   else
   {
@@ -3146,19 +3180,29 @@
     FREE (&Muttrc);
     mutt_expand_path (buffer, sizeof (buffer));
     Muttrc = safe_strdup (buffer);
+    if (access (Muttrc, F_OK))
+    {
+      snprintf (buffer, sizeof (buffer), "%s: %s", Muttrc, strerror (errno));
+      mutt_endwin (buffer);
+      exit (1);
+    }
   }
-  FREE (&AliasFile);
-  AliasFile = safe_strdup (NONULL(Muttrc));
+
+  if (Muttrc)
+  {
+    FREE (&AliasFile);
+    AliasFile = safe_strdup (Muttrc);
+  }
 
   /* Process the global rc file if it exists and the user hasn't explicity
      requested not to via "-n".  */
   if (!skip_sys_rc)
   {
-    snprintf (buffer, sizeof(buffer), "%s/Muttrc-%s", SYSCONFDIR, 
MUTT_VERSION);
+    snprintf (buffer, sizeof(buffer), "%s/Muttrc-" MUTT_VERSION, SYSCONFDIR);
     if (access (buffer, F_OK) == -1)
       snprintf (buffer, sizeof(buffer), "%s/Muttrc", SYSCONFDIR);
     if (access (buffer, F_OK) == -1)
-      snprintf (buffer, sizeof (buffer), "%s/Muttrc-%s", PKGDATADIR, 
MUTT_VERSION);
+      snprintf (buffer, sizeof (buffer), "%s/Muttrc-" MUTT_VERSION, 
PKGDATADIR);
     if (access (buffer, F_OK) == -1)
       snprintf (buffer, sizeof (buffer), "%s/Muttrc", PKGDATADIR);
     if (access (buffer, F_OK) != -1)
@@ -3173,7 +3217,7 @@
   }
 
   /* Read the user's initialization file.  */
-  if (access (Muttrc, F_OK) != -1)
+  if (Muttrc)
   {
     if (!option (OPTNOCURSES))
       endwin ();
@@ -3184,13 +3228,6 @@
       need_pause = 1;
     }
   }
-  else if (!default_rc)
-  {
-    /* file specified by -F does not exist */
-    snprintf (buffer, sizeof (buffer), "%s: %s", Muttrc, strerror (errno));
-    mutt_endwin (buffer);
-    exit (1);
-  }
 
   if (mutt_execute_commands (commands) != 0)
     need_pause = 1;
diff -r d18482f6641e -r 42fee7585fae muttbug.sh.in
--- a/muttbug.sh.in     Tue Nov 08 12:42:56 2016 -0800
+++ b/muttbug.sh.in     Sun Nov 13 18:45:35 2016 -0800
@@ -268,7 +268,7 @@
 fi
 
 if test "$personal" = "yes" ; then
-       CANDIDATES=".muttrc-${MUTTVERSION} .muttrc .mutt/muttrc-${MUTTVERSION} 
.mutt/muttrc"
+       CANDIDATES=".muttrc-${MUTTVERSION} .muttrc .mutt/muttrc-${MUTTVERSION} 
.mutt/muttrc .config/mutt/muttrc-${MUTTVERSION} .config/mutt/muttrc"
        MATCHED="none"
        for f in $CANDIDATES; do
                if test -f "${HOME}/$f" ; then
diff -r d18482f6641e -r 42fee7585fae muttlib.c
--- a/muttlib.c Tue Nov 08 12:42:56 2016 -0800
+++ b/muttlib.c Sun Nov 13 18:45:35 2016 -0800
@@ -1850,8 +1850,8 @@
 const char *mutt_make_version (void)
 {
   static char vstring[STRING];
-  snprintf (vstring, sizeof (vstring), "Mutt %s (%s)",
-           MUTT_VERSION, ReleaseDate);
+  snprintf (vstring, sizeof (vstring), "Mutt " MUTT_VERSION " (%s)",
+           ReleaseDate);
   return vstring;
 }
 
diff -r d18482f6641e -r 42fee7585fae status.c
--- a/status.c  Tue Nov 08 12:42:56 2016 -0800
+++ b/status.c  Sun Nov 13 18:45:35 2016 -0800
@@ -266,8 +266,7 @@
       break;
 
     case 'v':
-      snprintf (fmt, sizeof (fmt), "Mutt %%s");
-      snprintf (buf, buflen, fmt, MUTT_VERSION);
+      strfcpy (buf, "Mutt " MUTT_VERSION, buflen);
       break;
 
     case 'V':

Reply via email to