From: Damien Riegel Date: Mon, 14 Nov 2016 02:45:35 +0000 (-0800) Subject: search muttrc file according to XDG Base Specification (closes #3207) X-Git-Tag: mutt-1-8-rel~101 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=b17cd67312913bc98269840652bc5c54eba9fc20;p=mutt 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. --- diff --git a/commands.c b/commands.c index 2202a673..42436231 100644 --- a/commands.c +++ b/commands.c @@ -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) diff --git a/compose.c b/compose.c index aa59506e..ac7cd89b 100644 --- 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: diff --git a/dotlock.c b/dotlock.c index 5bf03480..3828fdaa 100644 --- 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 ] file\n", av0); diff --git a/init.c b/init.c index 0586638d..6484ddce 100644 --- 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; diff --git a/muttbug.sh.in b/muttbug.sh.in index b429faed..c6cc94e6 100644 --- a/muttbug.sh.in +++ b/muttbug.sh.in @@ -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 diff --git a/muttlib.c b/muttlib.c index a60ba0f7..baf48aa9 100644 --- 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; } diff --git a/status.c b/status.c index a8921a93..6ac5b56b 100644 --- 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':