| 1 |
/*
|
| 2 |
* misc.c
|
| 3 |
*
|
| 4 |
* Miscellaneous macro's and functions.
|
| 5 |
*
|
| 6 |
* Copyright 1999-2008 Gentoo Foundation
|
| 7 |
* Copyright 2004-2007 Martin Schlemmer <azarah@nosferatu.za.org>
|
| 8 |
* Licensed under the GPL-2
|
| 9 |
*/
|
| 10 |
|
| 11 |
#include "headers.h"
|
| 12 |
#include "rcscripts/rcutil.h"
|
| 13 |
|
| 14 |
/* This handles simple 'entry="bar"' type variables. If it is more complex
|
| 15 |
* ('entry="$(pwd)"' or such), it will obviously not work, but current behaviour
|
| 16 |
* should be fine for the type of variables we want. */
|
| 17 |
char *
|
| 18 |
rc_get_cnf_entry (const char *pathname, const char *entry, const char *sep)
|
| 19 |
{
|
| 20 |
rc_dynbuf_t *dynbuf = NULL;
|
| 21 |
char *buf = NULL;
|
| 22 |
char *str_ptr;
|
| 23 |
char *value = NULL;
|
| 24 |
char *token;
|
| 25 |
|
| 26 |
rc_errno_save ();
|
| 27 |
|
| 28 |
if ((!check_arg_str (pathname)) || (!check_arg_str (entry)))
|
| 29 |
return NULL;
|
| 30 |
|
| 31 |
/* If it is not a file or symlink pointing to a file, bail */
|
| 32 |
if (!rc_is_file (pathname, TRUE))
|
| 33 |
{
|
| 34 |
rc_errno_set (ENOENT);
|
| 35 |
DBG_MSG ("'%s' is not a file or do not exist!\n", pathname);
|
| 36 |
return NULL;
|
| 37 |
}
|
| 38 |
|
| 39 |
if (0 == rc_get_size (pathname, TRUE))
|
| 40 |
{
|
| 41 |
/* XXX: Should we set errno here ? */
|
| 42 |
DBG_MSG ("'%s' have a size of 0!\n", pathname);
|
| 43 |
return NULL;
|
| 44 |
}
|
| 45 |
|
| 46 |
dynbuf = rc_dynbuf_new_mmap_file (pathname);
|
| 47 |
if (NULL == dynbuf)
|
| 48 |
{
|
| 49 |
DBG_MSG ("Could not open config file for reading!\n");
|
| 50 |
return NULL;
|
| 51 |
}
|
| 52 |
|
| 53 |
/* Make sure we do not get false positives below */
|
| 54 |
rc_errno_clear ();
|
| 55 |
while (NULL != (buf = rc_dynbuf_read_line (dynbuf)))
|
| 56 |
{
|
| 57 |
str_ptr = buf;
|
| 58 |
|
| 59 |
/* Strip leading spaces/tabs */
|
| 60 |
while ((str_ptr[0] == ' ') || (str_ptr[0] == '\t'))
|
| 61 |
str_ptr++;
|
| 62 |
|
| 63 |
/* Get entry and value */
|
| 64 |
token = strsep (&str_ptr, "=");
|
| 65 |
/* Bogus entry or value */
|
| 66 |
if (NULL == token)
|
| 67 |
goto _continue;
|
| 68 |
|
| 69 |
/* Make sure we have a string that is larger than 'entry', and
|
| 70 |
* the first part equals 'entry' */
|
| 71 |
if ((strlen (token) > 0) && (0 == strcmp (entry, token)))
|
| 72 |
{
|
| 73 |
do
|
| 74 |
{
|
| 75 |
/* Bash variables are usually quoted */
|
| 76 |
token = strsep (&str_ptr, "\"\'");
|
| 77 |
/* If quoted, the first match will be "" */
|
| 78 |
}
|
| 79 |
while ((NULL != token) && (0 == strlen (token)));
|
| 80 |
|
| 81 |
/* We have a 'entry='. We respect bash rules, so NULL
|
| 82 |
* value for now (if not already) */
|
| 83 |
if (NULL == token)
|
| 84 |
{
|
| 85 |
/* We might have 'entry=' and later 'entry="bar"',
|
| 86 |
* so just continue for now ... we will handle
|
| 87 |
* it below when 'value == NULL' */
|
| 88 |
if ((!check_str(sep)) && (NULL != value))
|
| 89 |
{
|
| 90 |
free (value);
|
| 91 |
value = NULL;
|
| 92 |
}
|
| 93 |
goto _continue;
|
| 94 |
}
|
| 95 |
|
| 96 |
if ((!check_str(sep)) ||
|
| 97 |
((check_str(sep)) && (NULL == value)))
|
| 98 |
{
|
| 99 |
/* If we have already allocated 'value', free it */
|
| 100 |
if (NULL != value)
|
| 101 |
free (value);
|
| 102 |
|
| 103 |
value = xstrndup (token, strlen (token));
|
| 104 |
if (NULL == value)
|
| 105 |
{
|
| 106 |
rc_dynbuf_free (dynbuf);
|
| 107 |
free (buf);
|
| 108 |
|
| 109 |
return NULL;
|
| 110 |
}
|
| 111 |
}
|
| 112 |
else
|
| 113 |
{
|
| 114 |
value = xrealloc (value, strlen(value) + strlen(token) +
|
| 115 |
strlen(sep) + 1);
|
| 116 |
if (NULL == value)
|
| 117 |
{
|
| 118 |
rc_dynbuf_free (dynbuf);
|
| 119 |
free (buf);
|
| 120 |
|
| 121 |
return NULL;
|
| 122 |
}
|
| 123 |
snprintf(value + strlen(value), strlen(token) + strlen(sep) + 1,
|
| 124 |
"%s%s", sep, token);
|
| 125 |
}
|
| 126 |
|
| 127 |
/* We do not break, as there might be more than one entry
|
| 128 |
* defined, and as bash uses the last, so should we */
|
| 129 |
/* break; */
|
| 130 |
}
|
| 131 |
|
| 132 |
_continue:
|
| 133 |
free (buf);
|
| 134 |
}
|
| 135 |
|
| 136 |
/* rc_dynbuf_read_line() returned NULL with errno set */
|
| 137 |
if ((NULL == buf) && (rc_errno_is_set ()))
|
| 138 |
{
|
| 139 |
DBG_MSG ("Failed to read line from dynamic buffer!\n");
|
| 140 |
rc_dynbuf_free (dynbuf);
|
| 141 |
if (NULL != value)
|
| 142 |
free (value);
|
| 143 |
|
| 144 |
return NULL;
|
| 145 |
}
|
| 146 |
|
| 147 |
|
| 148 |
if (NULL == value)
|
| 149 |
/* NULL without errno set means everything went OK, but we did not get a
|
| 150 |
* entry */
|
| 151 |
DBG_MSG ("Failed to get value for config entry '%s'!\n", entry);
|
| 152 |
|
| 153 |
rc_dynbuf_free (dynbuf);
|
| 154 |
|
| 155 |
rc_errno_restore ();
|
| 156 |
|
| 157 |
return value;
|
| 158 |
}
|
| 159 |
|
| 160 |
char **
|
| 161 |
rc_get_list_file (char **list, char *filename)
|
| 162 |
{
|
| 163 |
rc_dynbuf_t *dynbuf = NULL;
|
| 164 |
char *buf = NULL;
|
| 165 |
char *tmp_p = NULL;
|
| 166 |
char *token = NULL;
|
| 167 |
|
| 168 |
rc_errno_save ();
|
| 169 |
|
| 170 |
if (!check_arg_str (filename))
|
| 171 |
return NULL;
|
| 172 |
|
| 173 |
dynbuf = rc_dynbuf_new_mmap_file (filename);
|
| 174 |
if (NULL == dynbuf)
|
| 175 |
return NULL;
|
| 176 |
|
| 177 |
/* Make sure we do not get false positives below */
|
| 178 |
rc_errno_clear ();
|
| 179 |
while (NULL != (buf = rc_dynbuf_read_line (dynbuf)))
|
| 180 |
{
|
| 181 |
tmp_p = buf;
|
| 182 |
|
| 183 |
/* Strip leading spaces/tabs */
|
| 184 |
while ((tmp_p[0] == ' ') || (tmp_p[0] == '\t'))
|
| 185 |
tmp_p++;
|
| 186 |
|
| 187 |
/* Get entry - we do not want comments, and only the first word
|
| 188 |
* on a line is valid */
|
| 189 |
token = strsep (&tmp_p, "# \t");
|
| 190 |
if (check_str (token))
|
| 191 |
{
|
| 192 |
tmp_p = xstrndup (token, strlen (token));
|
| 193 |
if (NULL == tmp_p)
|
| 194 |
{
|
| 195 |
if (NULL != list)
|
| 196 |
str_list_free (list);
|
| 197 |
rc_dynbuf_free (dynbuf);
|
| 198 |
free (buf);
|
| 199 |
|
| 200 |
return NULL;
|
| 201 |
}
|
| 202 |
|
| 203 |
str_list_add_item (list, tmp_p, error);
|
| 204 |
}
|
| 205 |
|
| 206 |
free (buf);
|
| 207 |
}
|
| 208 |
|
| 209 |
/* rc_dynbuf_read_line() returned NULL with errno set */
|
| 210 |
if ((NULL == buf) && (rc_errno_is_set ()))
|
| 211 |
{
|
| 212 |
DBG_MSG ("Failed to read line from dynamic buffer!\n");
|
| 213 |
error:
|
| 214 |
if (NULL != list)
|
| 215 |
str_list_free (list);
|
| 216 |
rc_dynbuf_free (dynbuf);
|
| 217 |
|
| 218 |
return NULL;
|
| 219 |
}
|
| 220 |
|
| 221 |
rc_dynbuf_free (dynbuf);
|
| 222 |
|
| 223 |
rc_errno_restore ();
|
| 224 |
|
| 225 |
return list;
|
| 226 |
}
|