aboutsummaryrefslogtreecommitdiff
blob: 3f4e6e18218c23436ff2ec621ec855c16e3aec3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
 * misc.c
 *
 * Miscellaneous macro's and functions.
 *
 * Copyright 1999-2008 Gentoo Foundation
 * Copyright 2004-2007 Martin Schlemmer <azarah@nosferatu.za.org>
 * Licensed under the GPL-2
 */

#include <errno.h>
#include <string.h>
#include <stdlib.h>

#include "rcscripts/rcutil.h"

/* This handles simple 'entry="bar"' type variables.  If it is more complex
 * ('entry="$(pwd)"' or such), it will obviously not work, but current behaviour
 * should be fine for the type of variables we want. */
char *
rc_get_cnf_entry (const char *pathname, const char *entry, const char *sep)
{
  rc_dynbuf_t *dynbuf = NULL;
  char *buf = NULL;
  char *str_ptr;
  char *value = NULL;
  char *token;

  rc_errno_save ();

  if ((!check_arg_str (pathname)) || (!check_arg_str (entry)))
    return NULL;

  /* If it is not a file or symlink pointing to a file, bail */
  if (!rc_is_file (pathname, TRUE))
    {
      rc_errno_set (ENOENT);
      DBG_MSG ("'%s' is not a file or do not exist!\n", pathname);
      return NULL;
    }

  if (0 == rc_get_size (pathname, TRUE))
    {
      /* XXX: Should we set errno here ? */
      DBG_MSG ("'%s' have a size of 0!\n", pathname);
      return NULL;
    }

  dynbuf = rc_dynbuf_new_mmap_file (pathname);
  if (NULL == dynbuf)
    {
      DBG_MSG ("Could not open config file for reading!\n");
      return NULL;
    }

  /* Make sure we do not get false positives below */
  rc_errno_clear ();
  while (NULL != (buf = rc_dynbuf_read_line (dynbuf)))
    {
      str_ptr = buf;

      /* Strip leading spaces/tabs */
      while ((str_ptr[0] == ' ') || (str_ptr[0] == '\t'))
	str_ptr++;

      /* Get entry and value */
      token = strsep (&str_ptr, "=");
      /* Bogus entry or value */
      if (NULL == token)
	goto _continue;

      /* Make sure we have a string that is larger than 'entry', and
       * the first part equals 'entry' */
      if ((strlen (token) > 0) && (0 == strcmp (entry, token)))
	{
	  do
	    {
	      /* Bash variables are usually quoted */
	      token = strsep (&str_ptr, "\"\'");
	      /* If quoted, the first match will be "" */
	    }
	  while ((NULL != token) && (0 == strlen (token)));

	  /* We have a 'entry='.  We respect bash rules, so NULL
	   * value for now (if not already) */
	  if (NULL == token)
	    {
	      /* We might have 'entry=' and later 'entry="bar"',
	       * so just continue for now ... we will handle
	       * it below when 'value == NULL' */
	      if ((!check_str(sep)) && (NULL != value))
		{
		  free (value);
		  value = NULL;
		}
	      goto _continue;
	    }

	  if ((!check_str(sep)) ||
	      ((check_str(sep)) && (NULL == value)))
	    {
	      /* If we have already allocated 'value', free it */
	      if (NULL != value)
		free (value);

	      value = xstrndup (token, strlen (token));
	      if (NULL == value)
		{
		  rc_dynbuf_free (dynbuf);
		  free (buf);

		  return NULL;
		}
	    }
	  else
	    {
	      value = xrealloc (value, strlen(value) + strlen(token) +
				strlen(sep) + 1);
	      if (NULL == value)
		{
		  rc_dynbuf_free (dynbuf);
		  free (buf);

		  return NULL;
		}
	      snprintf(value + strlen(value), strlen(token) + strlen(sep) + 1,
		       "%s%s", sep, token);
	    }

	  /* We do not break, as there might be more than one entry
	   * defined, and as bash uses the last, so should we */
	  /* break; */
	}

_continue:
      free (buf);
    }

  /* rc_dynbuf_read_line() returned NULL with errno set */
  if ((NULL == buf) && (rc_errno_is_set ()))
    {
      DBG_MSG ("Failed to read line from dynamic buffer!\n");
      rc_dynbuf_free (dynbuf);
      if (NULL != value)
	free (value);

      return NULL;
    }


  if (NULL == value)
    /* NULL without errno set means everything went OK, but we did not get a
     * entry */
    DBG_MSG ("Failed to get value for config entry '%s'!\n", entry);

  rc_dynbuf_free (dynbuf);

  rc_errno_restore ();

  return value;
}

char **
rc_get_list_file (char **list, char *filename)
{
  rc_dynbuf_t *dynbuf = NULL;
  char *buf = NULL;
  char *tmp_p = NULL;
  char *token = NULL;

  rc_errno_save ();

  if (!check_arg_str (filename))
    return NULL;

  dynbuf = rc_dynbuf_new_mmap_file (filename);
  if (NULL == dynbuf)
    return NULL;

  /* Make sure we do not get false positives below */
  rc_errno_clear ();
  while (NULL != (buf = rc_dynbuf_read_line (dynbuf)))
    {
      tmp_p = buf;

      /* Strip leading spaces/tabs */
      while ((tmp_p[0] == ' ') || (tmp_p[0] == '\t'))
	tmp_p++;

      /* Get entry - we do not want comments, and only the first word
       * on a line is valid */
      token = strsep (&tmp_p, "# \t");
      if (check_str (token))
	{
	  tmp_p = xstrndup (token, strlen (token));
	  if (NULL == tmp_p)
	    {
	      if (NULL != list)
		str_list_free (list);
	      rc_dynbuf_free (dynbuf);
	      free (buf);

	      return NULL;
	    }

	  str_list_add_item (list, tmp_p, error);
	}

      free (buf);
    }

  /* rc_dynbuf_read_line() returned NULL with errno set */
  if ((NULL == buf) && (rc_errno_is_set ()))
    {
      DBG_MSG ("Failed to read line from dynamic buffer!\n");
error:
      if (NULL != list)
	str_list_free (list);
      rc_dynbuf_free (dynbuf);

      return NULL;
    }

  rc_dynbuf_free (dynbuf);

  rc_errno_restore ();

  return list;
}