kopia lustrzana https://github.com/Hamlib/Hamlib
Add config path to settings including XDG_CONFIG_HOME usage
https://github.com/Hamlib/Hamlib/issues/985pull/1077/head
rodzic
7944ea511d
commit
021dbcd26e
|
@ -844,6 +844,7 @@ int HAMLIB_API rig_open(RIG *rig)
|
||||||
strcpy(rs->dcdport.pathname, rs->dcdport_deprecated.pathname);
|
strcpy(rs->dcdport.pathname, rs->dcdport_deprecated.pathname);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
rig_settings_load_all(NULL); // load default .hamlib_settings
|
||||||
// Read in our settings
|
// Read in our settings
|
||||||
char *cwd = malloc(4096);
|
char *cwd = malloc(4096);
|
||||||
|
|
||||||
|
|
|
@ -973,8 +973,38 @@ int HAMLIB_API rig_setting2idx(setting_t s)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
#define SETTINGS_FILE ".hamlib_settings"
|
#define SETTINGS_FILE "hamlib_settings"
|
||||||
char *settings_file = SETTINGS_FILE;
|
char settings_file[4096];
|
||||||
|
|
||||||
|
HAMLIB_EXPORT(int) rig_settings_get_path(char *path, int pathlen)
|
||||||
|
{
|
||||||
|
char cwd[4096];
|
||||||
|
|
||||||
|
if (getcwd(cwd, 4096) == NULL)
|
||||||
|
{
|
||||||
|
rig_debug(RIG_DEBUG_ERR, "%s: getcwd: %s\n", __func__, strerror(errno));
|
||||||
|
return -RIG_EINTERNAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
char *xdgpath = getenv("XDG_CONFIG_HOME");
|
||||||
|
char *home = getenv("HOME");
|
||||||
|
snprintf(path, pathlen, "%s/.config", home);
|
||||||
|
if (xdgpath)
|
||||||
|
{
|
||||||
|
snprintf(path, pathlen-1, "%s/%s/%s", xdgpath, cwd, SETTINGS_FILE);
|
||||||
|
}
|
||||||
|
else if (home && access(path, F_OK) != -1)
|
||||||
|
{
|
||||||
|
snprintf(path, pathlen-1, "%s/.config/%s", home, SETTINGS_FILE);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// we add a leading period to hide the file
|
||||||
|
snprintf(path, pathlen-1, "%s/.%s", home, SETTINGS_FILE);
|
||||||
|
}
|
||||||
|
rig_debug(RIG_DEBUG_TRACE, "%s: path=%s\n", __func__, path);
|
||||||
|
return RIG_OK;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \brief Save setting parameter
|
* \brief Save setting parameter
|
||||||
|
@ -987,17 +1017,25 @@ char *settings_file = SETTINGS_FILE;
|
||||||
HAMLIB_EXPORT(int) rig_settings_save(char *setting, void *value,
|
HAMLIB_EXPORT(int) rig_settings_save(char *setting, void *value,
|
||||||
settings_value_t valuetype)
|
settings_value_t valuetype)
|
||||||
{
|
{
|
||||||
FILE *fp = fopen(settings_file, "r");
|
FILE *fp;
|
||||||
FILE *fptmp;
|
FILE *fptmp;
|
||||||
|
char path[4096];
|
||||||
char buf[4096];
|
char buf[4096];
|
||||||
char *cvalue = (char *)value;
|
char *cvalue = (char *)value;
|
||||||
int *ivalue = (int *)value;
|
int *ivalue = (int *)value;
|
||||||
|
int n=0;
|
||||||
long *lvalue = (long *) value;
|
long *lvalue = (long *) value;
|
||||||
float *fvalue = (float *) value;
|
float *fvalue = (float *) value;
|
||||||
double *dvalue = (double *) value;
|
double *dvalue = (double *) value;
|
||||||
char *vformat;
|
char *vformat;
|
||||||
char template[64];
|
char template[64];
|
||||||
|
|
||||||
|
rig_settings_get_path(path,sizeof(path));
|
||||||
|
fp = fopen(path, "r");
|
||||||
|
if (fp == NULL) {
|
||||||
|
rig_debug(RIG_DEBUG_WARN, "%s: %s not found\n", __func__, path);
|
||||||
|
return -RIG_EIO;
|
||||||
|
}
|
||||||
strcpy(template, "hamlib_settings_XXXXXX");
|
strcpy(template, "hamlib_settings_XXXXXX");
|
||||||
|
|
||||||
switch (valuetype)
|
switch (valuetype)
|
||||||
|
@ -1057,6 +1095,10 @@ HAMLIB_EXPORT(int) rig_settings_save(char *setting, void *value,
|
||||||
{
|
{
|
||||||
char *tmp = strdup(buf);
|
char *tmp = strdup(buf);
|
||||||
char *s = strtok(tmp, "=");
|
char *s = strtok(tmp, "=");
|
||||||
|
if (buf[0] == '#') {
|
||||||
|
fprintf(fptmp, "%s\n", buf);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
if (s == NULL)
|
if (s == NULL)
|
||||||
{
|
{
|
||||||
|
@ -1066,6 +1108,7 @@ HAMLIB_EXPORT(int) rig_settings_save(char *setting, void *value,
|
||||||
fclose(fptmp);
|
fclose(fptmp);
|
||||||
return -RIG_EINTERNAL;
|
return -RIG_EINTERNAL;
|
||||||
}
|
}
|
||||||
|
++n;
|
||||||
|
|
||||||
char *v = strtok(NULL, "\r\n");
|
char *v = strtok(NULL, "\r\n");
|
||||||
|
|
||||||
|
@ -1077,7 +1120,7 @@ HAMLIB_EXPORT(int) rig_settings_save(char *setting, void *value,
|
||||||
fclose(fptmp);
|
fclose(fptmp);
|
||||||
return -RIG_EINTERNAL;
|
return -RIG_EINTERNAL;
|
||||||
}
|
}
|
||||||
|
rig_debug(RIG_DEBUG_TRACE, "%s: parsing setting %s=%s\n", __func__, s, v);
|
||||||
fprintf(fptmp, vformat, s, value);
|
fprintf(fptmp, vformat, s, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1085,6 +1128,7 @@ HAMLIB_EXPORT(int) rig_settings_save(char *setting, void *value,
|
||||||
fclose(fptmp);
|
fclose(fptmp);
|
||||||
remove(settings_file);
|
remove(settings_file);
|
||||||
rename(template, settings_file);
|
rename(template, settings_file);
|
||||||
|
rig_debug(RIG_DEBUG_TRACE, "%s: %d settings read\n", __func__, n);
|
||||||
return -RIG_ENIMPL;
|
return -RIG_ENIMPL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1096,15 +1140,23 @@ HAMLIB_EXPORT(int) rig_settings_load(char *setting, void *value,
|
||||||
|
|
||||||
HAMLIB_EXPORT(int) rig_settings_load_all(char *settings_file)
|
HAMLIB_EXPORT(int) rig_settings_load_all(char *settings_file)
|
||||||
{
|
{
|
||||||
FILE *fp = fopen(settings_file, "r");
|
FILE *fp;
|
||||||
char buf[4096];
|
char buf[4096];
|
||||||
|
char settingstmp[4096];
|
||||||
|
|
||||||
|
if (settings_file == NULL) {
|
||||||
|
rig_settings_get_path(settingstmp, sizeof(settingstmp));
|
||||||
|
settings_file = settingstmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
fp = fopen(settings_file, "r");
|
||||||
if (fp == NULL)
|
if (fp == NULL)
|
||||||
{
|
{
|
||||||
rig_debug(RIG_DEBUG_ERR, "%s: settings_file error(%s): %s\n", __func__,
|
rig_debug(RIG_DEBUG_ERR, "%s: settings_file error(%s): %s\n", __func__,
|
||||||
settings_file, strerror(errno));
|
settings_file, strerror(errno));
|
||||||
return -RIG_EINVAL;
|
return -RIG_EINVAL;
|
||||||
}
|
}
|
||||||
|
rig_debug(RIG_DEBUG_TRACE, "%s: opened %s\n", __func__, settings_file);
|
||||||
|
|
||||||
while (fgets(buf, sizeof(buf), fp))
|
while (fgets(buf, sizeof(buf), fp))
|
||||||
{
|
{
|
||||||
|
|
Ładowanie…
Reference in New Issue