mirror of
https://github.com/troglobit/finit.git
synced 2026-10-05 07:13:44 +07:00
conf: new function conf_parse_env()
Reduce code duplication. Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
+28
-8
@@ -490,14 +490,16 @@ void conf_save_service(int type, char *cfg, char *file)
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
/*
|
||||
* 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.
|
||||
/**
|
||||
* conf_parse_env - Parse a key=value line
|
||||
* @line: Line buffer without newline
|
||||
* @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;
|
||||
|
||||
/* skip any leading whitespace */
|
||||
@@ -519,7 +521,7 @@ static void parse_env(char *line)
|
||||
|
||||
val = strchr(key, '=');
|
||||
if (!val)
|
||||
return;
|
||||
return NULL;
|
||||
*val++ = 0;
|
||||
|
||||
/* strip leading whitespace from value */
|
||||
@@ -528,6 +530,7 @@ static void parse_env(char *line)
|
||||
|
||||
/* unquote value, if quoted */
|
||||
unquote(&val, end);
|
||||
*value = val;
|
||||
|
||||
/* find end of key */
|
||||
end = key;
|
||||
@@ -554,9 +557,26 @@ static void parse_env(char *line)
|
||||
end++;
|
||||
if (*end != 0) {
|
||||
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);
|
||||
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_service (int type, char *cfg, char *file);
|
||||
void conf_parse_cmdline (int argc, char *argv[]);
|
||||
char *conf_parse_env (char *line, char **val);
|
||||
int conf_parse_runlevels (char *runlevels);
|
||||
void conf_parse_cond (svc_t *svc, char *cond);
|
||||
|
||||
|
||||
+6
-52
@@ -425,11 +425,13 @@ static void source_env(svc_t *svc)
|
||||
|
||||
line = buf;
|
||||
while (fgets(line, LINE_SIZE, fp)) {
|
||||
char *key = chomp(line);
|
||||
wordexp_t we = { 0 };
|
||||
char *value, *end;
|
||||
char *key, *value;
|
||||
size_t i;
|
||||
|
||||
/* Trim newline */
|
||||
key = chomp(line);
|
||||
|
||||
/* skip any leading whitespace */
|
||||
while (isspace(*key))
|
||||
key++;
|
||||
@@ -438,57 +440,9 @@ static void source_env(svc_t *svc)
|
||||
if (*key == '#' || *key == ';')
|
||||
continue;
|
||||
|
||||
/* find end of line */
|
||||
end = key;
|
||||
while (*end)
|
||||
end++;
|
||||
|
||||
/* strip trailing whitespace */
|
||||
if (end > key) {
|
||||
end--;
|
||||
while (isspace(*end))
|
||||
*end-- = 0;
|
||||
}
|
||||
|
||||
value = strchr(key, '=');
|
||||
if (!value)
|
||||
key = conf_parse_env(key, &value);
|
||||
if (!key)
|
||||
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)) {
|
||||
setenv(key, value, 1);
|
||||
|
||||
Reference in New Issue
Block a user