mirror of
https://github.com/troglobit/finit.git
synced 2026-10-29 12:40:44 +00:00
conf: new function conf_parse_env()
Reduce code duplication. Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
3 files changed
+35
-60
No files matched your search
+28
-8
@@ -490,14 +490,16 @@ void conf_save_service(int type, char *cfg, char *file)
|
|||||||
fclose(fp);
|
fclose(fp);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/**
|
||||||
* Sets, and makes a note of, all KEY=VALUE lines in a given .conf line
|
* conf_parse_env - Parse a key=value line
|
||||||
* from finit.conf, or other .conf file. Note, PATH is always reset in
|
* @line: Line buffer without newline
|
||||||
* the conf_reset_env() function.
|
* @value: Whitespace trimmed value
|
||||||
|
*
|
||||||
|
* Returns:
|
||||||
|
* %NULL on error, otherwise a whitespace trimmed key.
|
||||||
*/
|
*/
|
||||||
static void parse_env(char *line)
|
char *conf_parse_env(char *line, char **value)
|
||||||
{
|
{
|
||||||
struct env_entry *node;
|
|
||||||
char *key, *val, *end;
|
char *key, *val, *end;
|
||||||
|
|
||||||
/* skip any leading whitespace */
|
/* skip any leading whitespace */
|
||||||
@@ -519,7 +521,7 @@ static void parse_env(char *line)
|
|||||||
|
|
||||||
val = strchr(key, '=');
|
val = strchr(key, '=');
|
||||||
if (!val)
|
if (!val)
|
||||||
return;
|
return NULL;
|
||||||
*val++ = 0;
|
*val++ = 0;
|
||||||
|
|
||||||
/* strip leading whitespace from value */
|
/* strip leading whitespace from value */
|
||||||
@@ -528,6 +530,7 @@ static void parse_env(char *line)
|
|||||||
|
|
||||||
/* unquote value, if quoted */
|
/* unquote value, if quoted */
|
||||||
unquote(&val, end);
|
unquote(&val, end);
|
||||||
|
*value = val;
|
||||||
|
|
||||||
/* find end of key */
|
/* find end of key */
|
||||||
end = key;
|
end = key;
|
||||||
@@ -554,9 +557,26 @@ static void parse_env(char *line)
|
|||||||
end++;
|
end++;
|
||||||
if (*end != 0) {
|
if (*end != 0) {
|
||||||
warnx("'%s=%s': not a valid identifier", key, val);
|
warnx("'%s=%s': not a valid identifier", key, val);
|
||||||
return; /* invalid key */
|
return NULL; /* invalid key */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Sets, and makes a note of, all KEY=VALUE lines in a given .conf line
|
||||||
|
* from finit.conf, or other .conf file. Note, PATH is always reset in
|
||||||
|
* the conf_reset_env() function.
|
||||||
|
*/
|
||||||
|
static void parse_env(char *line)
|
||||||
|
{
|
||||||
|
struct env_entry *node;
|
||||||
|
char *key, *val;
|
||||||
|
|
||||||
|
key = conf_parse_env(line, &val);
|
||||||
|
if (!key)
|
||||||
|
return;
|
||||||
|
|
||||||
dbg("Global env '%s'='%s'", key, val);
|
dbg("Global env '%s'='%s'", key, val);
|
||||||
setenv(key, val, 1);
|
setenv(key, val, 1);
|
||||||
|
|
||||||
|
|||||||
@@ -47,6 +47,7 @@ void conf_saverc (void);
|
|||||||
void conf_save_exec_order (svc_t *svc, char *cmdline, int result);
|
void conf_save_exec_order (svc_t *svc, char *cmdline, int result);
|
||||||
void conf_save_service (int type, char *cfg, char *file);
|
void conf_save_service (int type, char *cfg, char *file);
|
||||||
void conf_parse_cmdline (int argc, char *argv[]);
|
void conf_parse_cmdline (int argc, char *argv[]);
|
||||||
|
char *conf_parse_env (char *line, char **val);
|
||||||
int conf_parse_runlevels (char *runlevels);
|
int conf_parse_runlevels (char *runlevels);
|
||||||
void conf_parse_cond (svc_t *svc, char *cond);
|
void conf_parse_cond (svc_t *svc, char *cond);
|
||||||
|
|
||||||
|
|||||||
+6
-52
@@ -425,11 +425,13 @@ static void source_env(svc_t *svc)
|
|||||||
|
|
||||||
line = buf;
|
line = buf;
|
||||||
while (fgets(line, LINE_SIZE, fp)) {
|
while (fgets(line, LINE_SIZE, fp)) {
|
||||||
char *key = chomp(line);
|
|
||||||
wordexp_t we = { 0 };
|
wordexp_t we = { 0 };
|
||||||
char *value, *end;
|
char *key, *value;
|
||||||
size_t i;
|
size_t i;
|
||||||
|
|
||||||
|
/* Trim newline */
|
||||||
|
key = chomp(line);
|
||||||
|
|
||||||
/* skip any leading whitespace */
|
/* skip any leading whitespace */
|
||||||
while (isspace(*key))
|
while (isspace(*key))
|
||||||
key++;
|
key++;
|
||||||
@@ -438,57 +440,9 @@ static void source_env(svc_t *svc)
|
|||||||
if (*key == '#' || *key == ';')
|
if (*key == '#' || *key == ';')
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
/* find end of line */
|
key = conf_parse_env(key, &value);
|
||||||
end = key;
|
if (!key)
|
||||||
while (*end)
|
|
||||||
end++;
|
|
||||||
|
|
||||||
/* strip trailing whitespace */
|
|
||||||
if (end > key) {
|
|
||||||
end--;
|
|
||||||
while (isspace(*end))
|
|
||||||
*end-- = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
value = strchr(key, '=');
|
|
||||||
if (!value)
|
|
||||||
continue;
|
continue;
|
||||||
*value++ = 0;
|
|
||||||
|
|
||||||
/* strip leading whitespace from value */
|
|
||||||
while (isspace(*value))
|
|
||||||
value++;
|
|
||||||
|
|
||||||
/* unquote value, if quoted */
|
|
||||||
unquote(&value, end);
|
|
||||||
|
|
||||||
/* find end of key */
|
|
||||||
end = key;
|
|
||||||
while (*end)
|
|
||||||
end++;
|
|
||||||
|
|
||||||
/* strip trailing whitespace */
|
|
||||||
if (end > key) {
|
|
||||||
end--;
|
|
||||||
while (isspace(*end))
|
|
||||||
*end-- = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* strip any leading 'set ' */
|
|
||||||
end = key;
|
|
||||||
if (!strncmp(key, "set", 3))
|
|
||||||
end += 3;
|
|
||||||
|
|
||||||
/* check key, no spaces allowed */
|
|
||||||
while (*end && isspace(*end))
|
|
||||||
end++;
|
|
||||||
key = end;
|
|
||||||
while (*end && !isspace(*end))
|
|
||||||
end++;
|
|
||||||
if (*end != 0) {
|
|
||||||
warnx("'%s=%s': not a valid identifier", key, value);
|
|
||||||
continue; /* invalid key */
|
|
||||||
}
|
|
||||||
|
|
||||||
if (wordexp(value, &we, 0)) {
|
if (wordexp(value, &we, 0)) {
|
||||||
setenv(key, value, 1);
|
setenv(key, value, 1);
|
||||||
|
|||||||
Reference in new issue
Block a user