summaryrefslogtreecommitdiffstats
path: root/discover/parser-conf.c
blob: a21efc4aced845e10fec38469e9abcc72bc85ed9 (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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
 *  Copyright (C) 2009 Sony Computer Entertainment Inc.
 *  Copyright 2009 Sony Corp.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; version 2 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#define _GNU_SOURCE

#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>

#include "log/log.h"
#include "talloc/talloc.h"
#include "parser-conf.h"
#include "parser-utils.h"
#include "paths.h"

/**
 * conf_strip_str - Remove quotes and/or whitespace around a string.
 *
 * Returns the next byte to process, or NULL if the string is empty.
 */

char *conf_strip_str(char *s)
{
	char *e;

	if (!s)
		return NULL;

	while (*s == '"' || *s == '\'' || isspace(*s))
		s++;

	e = s + strlen(s) - 1;

	while (*e == '"' || *e == '\'' || isspace(*e))
		*(e--) = 0;

	return strlen(s) ? s : NULL;
}

/**
 * conf_replace_char - replace one char with another.
 */

char *conf_replace_char(char *s, char from, char to)
{
	if (!s)
		return NULL;

	for ( ; *s; s++)
		if (*s == from)
			*s = to;

	return s;
}

/**
 * conf_get_pair - Get the next 'name/value' parameter pair.
 * @str: The string to process.
 * @name_out: Returns a pointer to the name.
 * @value_out: Returns a pointer to the value.
 * @tdelimiter: The pair separator.
 * @terminator: The pair terminator.
 *
 * Parses a name=value pair returning pointers in @name_out and @value_out.
 * The pair can be terminated by @terminator or a zero.
 * If no '=' character is found @value_out is set and @name_out is
 * set to NULL.
 * If the value is empty *value_out is set to NULL.
 * The string is modified in place.
 *
 * Returns the next byte to process, or NULL if we've hit the end of the
 * string.
 */

char *conf_get_pair(struct conf_context __attribute__((unused)) *conf, char *str,
	char **name_out, char **value_out, char delimiter, char terminator)
{
	char *sep, *end;

	*name_out = *value_out = NULL;

	/* terminate the value */
	end = strchr(str, terminator);

	if (end)
		*end = 0;

	conf_replace_char(str, '\t', ' ');

	str = conf_strip_str(str);

	if (!str)
		goto exit;

	sep = strchr(str, delimiter);

	if (!sep) {
		*name_out = NULL;
		*value_out = conf_strip_str(str);
	} else {
		*sep = 0;
		*name_out = conf_strip_str(str);
		*value_out = conf_strip_str(sep + 1);
	}

exit:
	pb_log("%s: @%s@%s@\n", __func__, *name_out, *value_out);

	return end ? end + 1 : NULL;
}

/**
 * conf_param_in_list - Search a list of strings for an entry.
 * @list: A NULL treminated array of pointers to strings.
 * @param: A string to search for.
 *
 * Retuns 1 if @param is found in @list, 0 if @param is not found.
 */

int conf_param_in_list(const char *const *list, const char *param)
{
	const char *const *str;

	for (str = list; *str; str++)
		if (streq(*str, param))
			return 1;
	return 0;
}

/**
 * conf_init_global_options - Zero the global option table.
 */

void conf_init_global_options(struct conf_context *conf)
{
	int i;

	for (i = 0; conf->global_options[i].name; i++)
		conf->global_options[i].value = NULL;
}

/**
 * conf_set_global_option - Set a value in the global option table.
 *
 * Check if an option (name=value) is a global option. If so, store it in
 * the global options table, and return 1. Otherwise, return 0.
 */

int conf_set_global_option(struct conf_context *conf, const char *name,
	const char *value)
{
	int i;

	for (i = 0; conf->global_options[i].name; i++) {
		if (streq(name, conf->global_options[i].name)) {
			conf->global_options[i].value
				= talloc_strdup(conf, value);
			pb_log("%s: @%s@%s@\n", __func__, name, value);
			return 1;
		}
	}
	return 0;
}

/**
 * conf_get_global_option - Get a value from the global option table.
 * @conf: The parser struct conf_context.
 * @name: The name of the (name:value) to retrieve.
 *
 * Returns the value if @name is found in the table, or NULL if @name
 * is not found.
 */

const char *conf_get_global_option(struct conf_context *conf,
	const char *name)
{
	int i;

	for (i = 0; conf->global_options[i].name ;i++)
		if (streq(name, conf->global_options[i].name)) {
			pb_log("%s: @%s@%s@\n", __func__, name,
				conf->global_options[i].value);
			return conf->global_options[i].value;
		}

	assert(0 && "unknown global name");
	return NULL;
}

/**
 * conf_parse_buf - The main parser loop.
 *
 * Called from conf_parse() with data read from a conf file.
 */

static void conf_parse_buf(struct conf_context *conf)
{
	char *pos, *name, *value;

	for (pos = conf->buf; pos;) {
		pos = conf_get_pair_equal(conf, pos, &name, &value, '\n');

		if (!value)
			continue;

		if (name && *name == '#')
			continue;

		if (*value == '#')
			continue;

		conf->process_pair(conf, name, value);
	}

	if (conf->finish)
		conf->finish(conf);
}

/**
 * conf_parse - The common parser entry.
 *
 * Called from the parser specific setup routines.  Searches for .conf
 * files, reads data into buffers, and calls conf_parse_buf().
 */

int conf_parse(struct conf_context *conf)
{
	int fd, rc;
	unsigned int i;
	struct stat stat;
	ssize_t len;

	rc = 0;
	fd = -1;
	len = 0;

	/* The parser is only run on the first file found. */
	/* FIXME: Could try others on error, etc. */

	for (i = 0; conf->conf_files[i]; i++) {
		char *filepath = resolve_path(conf->dc,
			conf->conf_files[i], conf->dc->device_path);

		pb_log("%s: try: %s\n", __func__, filepath);

		fd = open(filepath, O_RDONLY);

		talloc_free(filepath);

		if (fd < 0) {
			pb_log("%s: open failed: %s\n", __func__,
				strerror(errno));
			continue;
		}

		if (fstat(fd, &stat)) {
			pb_log("%s: fstat failed: %s\n", __func__,
				strerror(errno));
			continue;
		}

		conf->buf = talloc_array(conf, char, stat.st_size + 1);

		len = read(fd, conf->buf, stat.st_size);

		if (len < 0) {
			pb_log("%s: read failed: %s\n", __func__,
				strerror(errno));
			continue;
		}
		conf->buf[len] = 0;

		break;
	}

	if (fd >= 0)
		close(fd);

	if (len <= 0)
		goto out;

	if (!conf->dc->device->icon_file)
		conf->dc->device->icon_file = talloc_strdup(conf->dc,
			generic_icon_file(guess_device_type(conf->dc)));

	conf_parse_buf(conf);

	rc = 1;

out:
	pb_log("%s: %s\n", __func__, (rc ? "ok" : "failed"));
	return rc;
}

OpenPOWER on IntegriCloud