]> granicus.if.org Git - mutt/commitdiff
search muttrc file according to XDG Base Specification (closes #3207)
authorDamien Riegel <damien.riegel@gmail.com>
Mon, 14 Nov 2016 02:45:35 +0000 (18:45 -0800)
committerDamien Riegel <damien.riegel@gmail.com>
Mon, 14 Nov 2016 02:45:35 +0000 (18:45 -0800)
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.

commands.c
compose.c
dotlock.c
init.c
muttbug.sh.in
muttlib.c
status.c

index 2202a673328ec3ba027525f5f0e569da0133da8a..4243623142263f31d7069451aa0c9c4e898eae4e 100644 (file)
@@ -879,7 +879,7 @@ int mutt_save_message (HEADER *h, int delete,
 
 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)
index aa59506e1bc824fb3df0bbfd31a2862e3969f752..ac7cd89b246af527d185d3f80b3e00d777fcefd9 100644 (file)
--- a/compose.c
+++ b/compose.c
@@ -448,8 +448,7 @@ compose_format_str (char *buf, size_t buflen, size_t col, int cols, char op, con
       break;
 
     case 'v':
-      snprintf (fmt, sizeof (fmt), "Mutt %%s");
-      snprintf (buf, buflen, fmt, MUTT_VERSION);
+      strfcpy(buf, "Mutt " MUTT_VERSION, buflen);
       break;
 
     case 0:
index 5bf03480a60833ba2f2c5b98ac35281fbba2459c..3828fdaa2937207bf40f16bf41381b84b497dd19 100644 (file)
--- a/dotlock.c
+++ b/dotlock.c
@@ -339,7 +339,7 @@ END_PRIVILEGED (void)
 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 --git a/init.c b/init.c
index 0586638dea29d60f0f117cb014a2fd3445610270..6484ddce6d4ca68a670de0fa9acb854cfd24d3f0 100644 (file)
--- a/init.c
+++ b/init.c
@@ -2851,8 +2851,8 @@ static void start_debug (void)
   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 @@ static void mutt_srandom (void)
   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 @@ void mutt_init (int skip_sys_rc, LIST *commands)
    * 
    * 
    */
-  
-  
-  
-  
+
   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 @@ void mutt_init (int skip_sys_rc, LIST *commands)
     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);
+    }
+  }
+
+  if (Muttrc)
+  {
+    FREE (&AliasFile);
+    AliasFile = safe_strdup (Muttrc);
   }
-  FREE (&AliasFile);
-  AliasFile = safe_strdup (NONULL(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 @@ void mutt_init (int skip_sys_rc, LIST *commands)
   }
 
   /* Read the user's initialization file.  */
-  if (access (Muttrc, F_OK) != -1)
+  if (Muttrc)
   {
     if (!option (OPTNOCURSES))
       endwin ();
@@ -3184,13 +3228,6 @@ void mutt_init (int skip_sys_rc, LIST *commands)
       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;
index b429faeda7214ca07cbc70ad558b67e7d3183883..c6cc94e6e9aba6572c00b5817174243c14af0701 100644 (file)
@@ -268,7 +268,7 @@ if test "$CORE" && test -f "$CORE" ; then
 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
index a60ba0f778d8d3fd3a80c594c98a9035dc8ecc92..baf48aa9e01b5fc0807d15c33534d4d9cb2189a3 100644 (file)
--- a/muttlib.c
+++ b/muttlib.c
@@ -1850,8 +1850,8 @@ void mutt_set_mtime (const char* from, const char* to)
 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;
 }
 
index a8921a93d9190bbe63285d8f8d0c9694f8984c8f..6ac5b56bdab1dee6e5461f6b3c27578e16b011e9 100644 (file)
--- a/status.c
+++ b/status.c
@@ -266,8 +266,7 @@ status_format_str (char *buf, size_t buflen, size_t col, int cols, char op, cons
       break;
 
     case 'v':
-      snprintf (fmt, sizeof (fmt), "Mutt %%s");
-      snprintf (buf, buflen, fmt, MUTT_VERSION);
+      strfcpy (buf, "Mutt " MUTT_VERSION, buflen);
       break;
 
     case 'V':