2007-04-05 16:48:42 +05:30
|
|
|
/*
|
|
|
|
rc-misc.c
|
|
|
|
rc misc functions
|
|
|
|
Copyright 2007 Gentoo Foundation
|
|
|
|
*/
|
|
|
|
|
2007-04-13 19:38:16 +05:30
|
|
|
#include "librc.h"
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-09-26 12:35:33 +05:30
|
|
|
bool rc_env_bool (const char *var)
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
char *v;
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
if (! var)
|
2007-09-25 23:00:07 +05:30
|
|
|
return (false);
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-09-26 12:35:33 +05:30
|
|
|
if (! (v = getenv (var))) {
|
|
|
|
errno = ENOENT;
|
|
|
|
return (false);
|
|
|
|
}
|
|
|
|
|
2007-09-26 13:22:48 +05:30
|
|
|
if (strcasecmp (v, "true") == 0 ||
|
|
|
|
strcasecmp (v, "y") == 0 ||
|
|
|
|
strcasecmp (v, "yes") == 0 ||
|
|
|
|
strcasecmp (v, "1") == 0)
|
2007-09-26 12:35:33 +05:30
|
|
|
return (true);
|
|
|
|
|
2007-09-26 13:22:48 +05:30
|
|
|
if (strcasecmp (v, "false") != 0 &&
|
|
|
|
strcasecmp (v, "n") != 0 &&
|
|
|
|
strcasecmp (v, "no") != 0 &&
|
|
|
|
strcasecmp (v, "0") != 0)
|
2007-09-26 12:35:33 +05:30
|
|
|
errno = EINVAL;
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-09-26 12:35:33 +05:30
|
|
|
return (false);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
2007-09-26 12:35:33 +05:30
|
|
|
librc_hidden_def(rc_env_bool)
|
2007-04-05 16:48:42 +05:30
|
|
|
|
|
|
|
char *rc_strcatpaths (const char *path1, const char *paths, ...)
|
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
va_list ap;
|
|
|
|
int length;
|
|
|
|
int i;
|
|
|
|
char *p;
|
|
|
|
char *path;
|
|
|
|
char *pathp;
|
|
|
|
|
|
|
|
if (! path1 || ! paths)
|
|
|
|
return (NULL);
|
|
|
|
|
2007-08-09 20:03:20 +05:30
|
|
|
length = strlen (path1) + strlen (paths) + 1;
|
|
|
|
if (*paths != '/')
|
|
|
|
length ++;
|
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
va_start (ap, paths);
|
2007-08-09 20:03:20 +05:30
|
|
|
while ((p = va_arg (ap, char *)) != NULL) {
|
|
|
|
if (*p != '/')
|
|
|
|
length ++;
|
|
|
|
length += strlen (p);
|
|
|
|
}
|
2007-04-11 18:14:47 +05:30
|
|
|
va_end (ap);
|
|
|
|
|
2007-10-08 16:41:21 +05:30
|
|
|
pathp = path = xmalloc (length * sizeof (char *));
|
2007-04-11 18:14:47 +05:30
|
|
|
memset (path, 0, length);
|
2007-08-09 20:03:20 +05:30
|
|
|
i = strlen (path1);
|
|
|
|
memcpy (path, path1, i);
|
|
|
|
pathp += i;
|
|
|
|
if (*paths != '/')
|
|
|
|
*pathp ++ = '/';
|
|
|
|
i = strlen (paths);
|
|
|
|
memcpy (pathp, paths, i);
|
|
|
|
pathp += i;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
va_start (ap, paths);
|
|
|
|
while ((p = va_arg (ap, char *)) != NULL) {
|
2007-08-09 20:03:20 +05:30
|
|
|
if (*p != '/')
|
|
|
|
*pathp ++= '/';
|
2007-04-11 18:14:47 +05:30
|
|
|
i = strlen (p);
|
|
|
|
memcpy (pathp, p, i);
|
|
|
|
pathp += i;
|
|
|
|
}
|
|
|
|
va_end (ap);
|
|
|
|
|
|
|
|
*pathp++ = 0;
|
|
|
|
|
|
|
|
return (path);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
2007-04-13 19:38:16 +05:30
|
|
|
librc_hidden_def(rc_strcatpaths)
|
2007-04-05 16:48:42 +05:30
|
|
|
|
|
|
|
|
2007-10-03 20:13:05 +05:30
|
|
|
char **rc_config_load (const char *file)
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2007-09-18 17:34:51 +05:30
|
|
|
char **list = NULL;
|
2007-04-11 18:14:47 +05:30
|
|
|
FILE *fp;
|
|
|
|
char buffer[RC_LINEBUFFER];
|
|
|
|
char *p;
|
|
|
|
char *token;
|
|
|
|
char *line;
|
|
|
|
char *linep;
|
|
|
|
char *linetok;
|
|
|
|
int i = 0;
|
|
|
|
bool replaced;
|
|
|
|
char *entry;
|
|
|
|
char *newline;
|
|
|
|
|
2007-09-29 22:12:08 +05:30
|
|
|
if (! (fp = fopen (file, "r")))
|
2007-09-18 22:27:39 +05:30
|
|
|
return (NULL);
|
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
while (fgets (buffer, RC_LINEBUFFER, fp)) {
|
|
|
|
p = buffer;
|
|
|
|
|
|
|
|
/* Strip leading spaces/tabs */
|
|
|
|
while ((*p == ' ') || (*p == '\t'))
|
|
|
|
p++;
|
|
|
|
|
|
|
|
if (! p || strlen (p) < 3 || p[0] == '#')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Get entry */
|
|
|
|
token = strsep (&p, "=");
|
2007-07-20 03:14:55 +05:30
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
if (! token)
|
|
|
|
continue;
|
|
|
|
|
2007-10-08 16:46:22 +05:30
|
|
|
entry = xstrdup (token);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2007-07-09 15:56:02 +05:30
|
|
|
/* Preserve shell coloring */
|
|
|
|
if (*p == '$')
|
|
|
|
token = p;
|
|
|
|
else
|
|
|
|
do {
|
|
|
|
/* Bash variables are usually quoted */
|
|
|
|
token = strsep (&p, "\"\'");
|
|
|
|
} while ((token) && (strlen (token) == 0));
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
/* Drop a newline if that's all we have */
|
|
|
|
i = strlen (token) - 1;
|
|
|
|
if (token[i] == 10)
|
|
|
|
token[i] = 0;
|
|
|
|
|
|
|
|
i = strlen (entry) + strlen (token) + 2;
|
2007-10-08 16:41:21 +05:30
|
|
|
newline = xmalloc (i);
|
2007-04-11 18:14:47 +05:30
|
|
|
snprintf (newline, i, "%s=%s", entry, token);
|
|
|
|
|
|
|
|
replaced = false;
|
|
|
|
/* In shells the last item takes precedence, so we need to remove
|
|
|
|
any prior values we may already have */
|
|
|
|
STRLIST_FOREACH (list, line, i) {
|
2007-10-08 16:46:22 +05:30
|
|
|
char *tmp = xstrdup (line);
|
2007-04-11 18:14:47 +05:30
|
|
|
linep = tmp;
|
|
|
|
linetok = strsep (&linep, "=");
|
|
|
|
if (strcmp (linetok, entry) == 0) {
|
|
|
|
/* We have a match now - to save time we directly replace it */
|
|
|
|
free (list[i - 1]);
|
2007-04-13 19:38:16 +05:30
|
|
|
list[i - 1] = newline;
|
2007-04-11 18:14:47 +05:30
|
|
|
replaced = true;
|
|
|
|
free (tmp);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
free (tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! replaced) {
|
2007-09-18 17:06:55 +05:30
|
|
|
rc_strlist_addsort (&list, newline);
|
2007-04-11 18:14:47 +05:30
|
|
|
free (newline);
|
|
|
|
}
|
|
|
|
free (entry);
|
|
|
|
}
|
|
|
|
fclose (fp);
|
|
|
|
|
|
|
|
return (list);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
2007-10-03 20:13:05 +05:30
|
|
|
librc_hidden_def(rc_config_load)
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-10-03 20:13:05 +05:30
|
|
|
char *rc_config_value (char **list, const char *entry)
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
char *line;
|
|
|
|
int i;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
STRLIST_FOREACH (list, line, i) {
|
|
|
|
p = strchr (line, '=');
|
|
|
|
if (p && strncmp (entry, line, p - line) == 0)
|
|
|
|
return (p += 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (NULL);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
2007-10-03 20:13:05 +05:30
|
|
|
librc_hidden_def(rc_config_value)
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-10-03 20:13:05 +05:30
|
|
|
char **rc_config_list (const char *file)
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
FILE *fp;
|
|
|
|
char buffer[RC_LINEBUFFER];
|
|
|
|
char *p;
|
|
|
|
char *token;
|
2007-09-18 17:34:51 +05:30
|
|
|
char **list = NULL;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2007-09-29 22:12:08 +05:30
|
|
|
if (! (fp = fopen (file, "r")))
|
2007-09-18 17:34:51 +05:30
|
|
|
return (NULL);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
while (fgets (buffer, RC_LINEBUFFER, fp)) {
|
|
|
|
p = buffer;
|
|
|
|
|
|
|
|
/* Strip leading spaces/tabs */
|
|
|
|
while ((*p == ' ') || (*p == '\t'))
|
|
|
|
p++;
|
|
|
|
|
|
|
|
/* Get entry - we do not want comments */
|
|
|
|
token = strsep (&p, "#");
|
|
|
|
if (token && (strlen (token) > 1)) {
|
|
|
|
/* Stip the newline if present */
|
|
|
|
if (token[strlen (token) - 1] == '\n')
|
|
|
|
token[strlen (token) - 1] = 0;
|
|
|
|
|
2007-09-18 17:06:55 +05:30
|
|
|
rc_strlist_add (&list, token);
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose (fp);
|
|
|
|
|
|
|
|
return (list);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
2007-10-03 20:13:05 +05:30
|
|
|
librc_hidden_def(rc_config_list)
|