Implement XDG Base Directory Specification

$XDG_CONFIG_HOME/xephem can be used as an alternative to the
$HOME/.xephem directory. The old directory is checked first to
minimise disruption to existing installations.
This commit is contained in:
89391914+robxnano@users.noreply.github.com 2023-04-08 18:00:22 +01:00
parent 1ab180bba5
commit 6547f27b72
No known key found for this signature in database
GPG Key ID: 9FB6B03B782D1E42
4 changed files with 85 additions and 10 deletions

View File

@ -559,6 +559,11 @@ resources then exit<br>
<h3><a class="mozTocH3" name="mozTocId644691"></a> 1.3.1 <a
name="SharedPrivateDirs"></a>Shared and Private Directories</h3>
When XEphem is launched it looks for a file named <span
style="font-family: monospace;">xephemrc</span> in the user's <span
style="font-family: monospace;">$HOME/.config</span> directory (or
the location specified by the <span
style="font-family: monospace;">$XDG_CONFIG_HOME</span>
environment variable), or if that doesn't exist, <span
style="font-family: monospace;">.xephemrc</span> in the user's <span
style="font-family: monospace;">$HOME</span> directory. This file is
optional. If it exists, it should contain a line with the following
@ -576,10 +581,13 @@ A leading "." in the file name can be used to refer to the current
working directory of the running program. The example above causes the
Private directory to be <span style="font-family: monospace;">.xephem</span>
in the users <span style="font-family: monospace;">$HOME</span>
directory. If <span style="font-family: monospace;">.xephemrc</span>
does not exist or does not contain this line the default Private
directory is <span style="font-family: monospace;">~/.xephem</span>.
The private directory will be created if it does not already exist.<br>
directory. If <span style="font-family: monospace;">xephemrc</span>
does not exist or does not contain this line XEphem will first search for
the Private directory at <span style="font-family: monospace;">~/.xephem</span>,
followed by <span style="font-family: monospace;">~/.config/xephem</span>.
The private directory will be created in one of those locations if it doe
not already exist, depending on whether the
<span style="font-family: monospace;">~/.config</span> directory exists.<br>
<br>
XEphem then also searches for another directory for <i>Shared</i>
files. This directory contains files presumed to be shared

View File

@ -31,6 +31,9 @@ expand_home (char *path)
char *p;
int l;
if (!path)
return NULL;
/* get home, if we don't already know it */
if (!home) {
home = getenv ("HOME");

View File

@ -172,6 +172,9 @@ static char mydirdef[] = "~/.xephem";
static char mdovride[] = "~/.xephemrc";
static char mdres[] = "XEphem.PrivateDir";
#endif
static char xdgprivdir[] = "xephem";
static char xdgovride[] = "xephemrc";
static char dotconfig[] = "~/.config";
#define RESWID 45 /* columns for resource name, if possible */
#define PGWID 75 /* overall number of default columns */
@ -930,13 +933,49 @@ sr_getDirPM (Pixmap *pmopen, Pixmap *pmclose)
*pmclose = nomore_pm;
}
/* Returns a full path in the directory specified by $XDG_CONFIG_HOME, or
* $HOME/.config if not set. Passing NULL returns the root of the directory.
*/
static char *
getXdgConfigPath (const char *subpath)
{
static char *xdgconfig;
unsigned pathlen;
char *path;
if (!xdgconfig)
xdgconfig = getenv("XDG_CONFIG_HOME");
if (!xdgconfig)
xdgconfig = XtNewString(expand_home(dotconfig));
if (!subpath) {
path = XtNewString(xdgconfig);
} else {
pathlen = (unsigned)(strlen(xdgconfig) + strlen(subpath) + 2);
path = XtMalloc(pathlen);
snprintf(path, pathlen, "%s/%s", xdgconfig, subpath);
}
return path;
}
/* return full path of per-user working directory,
* allowing for possible override.
* Directories are searched in this order:
* 1. Directory specified by $HOME/.xephemrc
* 2. Directory specified by $XDG_CONFIG_HOME/xephemrc or $home/.config/xephemrc
* 3. $HOME/.xephem
* 4. $XDG_CONFIG_HOME/xephem or $HOME/.config/xephem
* If none of these directories exists, a new one is created:
* 1. If $XDG_CONFIG_HOME or $HOME/.config exists, create xephem directory there
* 2. if not, create .xephem directory in $HOME
*/
char *
getPrivateDir (void)
{
static char *mydir;
char *xdgdir;
char *ovride = mdovride;
if (!mydir) {
/* try mdovride else use default */
@ -944,6 +983,13 @@ getPrivateDir (void)
char *vhome, *vp = NULL;
char nam[MRNAM], val[MLL], buf[MLL];
#if !defined(__APPLE__) || !defined(__VMS)
if (!ofp) {
ovride = getXdgConfigPath(xdgovride);
ofp = fopenh (ovride, "r");
}
#endif
if (ofp) {
while (fgets (buf, sizeof(buf), ofp)) {
if (!crackNam (buf, nam) && !strcmp (nam, mdres) &&
@ -954,13 +1000,24 @@ getPrivateDir (void)
}
fclose(ofp);
if (!vp)
fprintf (stderr, "%s: %s not found. Using %s\n",
mdovride, mdres, mydirdef);
fprintf (stderr, "%s: %s not found. Trying default dirs\n",
ovride, mdres);
}
if (!vp)
vp = mydirdef;
vhome = expand_home(vp);
mydir = XtNewString (vhome); /* macro! */
#if !defined(__APPLE__) || !defined(__VMS)
xdgdir = getXdgConfigPath(NULL);
if (access (mydir, X_OK) < 0 && access (xdgdir, X_OK) == 0) {
XtFree (mydir);
mydir = getXdgConfigPath(xdgprivdir);
}
XtFree (xdgdir);
#endif
if (access (mydir, X_OK) < 0 && mkdir (mydir, 0744) < 0) {
/* don't try and fake it */
printf ("%s: %s\n", mydir, syserrstr());

View File

@ -126,15 +126,22 @@ references; version number and the Copyright statement.
.SH X Resources
When first started, XEphem looks for a file named
.B .xephemrc
in your $HOME directory. It should contain one line of the form:
in your $HOME directory, or if that doesn't exist,
.B xephemrc
in your $HOME/.config directory. It should contain one line of the form:
XEphem.PrivateDir: ~/.xephem
This defines the Private directory, where XEphem will store your personal
settings. The example line shown here, which is also the assumption if the
file is not present, means XEphem will create and use a directory named
settings. The example line shown here means XEphem will create and use a
directory named
.B .xephem
for this purpose in your home directory.
for this purpose in your home directory. If the file is not present, the
XEphem will look for the directory in
.B $HOME/.xephem
first, followed by
.B $HOME/.config/xephem
if that does not exist. If neither exists, a new directory will be created.
Within this directory a text file named
.B XEphem