6 * Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation version 2 of the License.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 675 Mass Ave, Cambridge, MA 02139, USA.
24 /* define this to enable parsing debugging */
25 /* #define DEBUG_PARSER */
39 static int get_pair(char **orig_string, char **left, char **right)
42 char *string = *orig_string;
47 /* eat any whitespace */
48 while (isspace(*string))
51 /* split based on '=' */
52 temp = strsep(&string, "=");
57 /* take the right side and strip off the '"' */
58 while (isspace(*string))
65 temp = strsep(&string, "\"");
66 if (!string || *temp == '\0')
69 *orig_string = string;
74 static int get_value(const char *left, char **orig_string, char **ret_string)
79 retval = get_pair(orig_string, &left_string, ret_string);
82 if (strcasecmp(left_string, left) != 0)
87 void dump_config_dev(struct config_device *dev)
91 dbg_parse("KERNEL name='%s' ,"
92 "owner='%s', group='%s', mode=%#o",
93 dev->name, dev->owner, dev->group, dev->mode);
96 dbg_parse("LABEL name='%s', bus='%s', sysfs_file='%s', sysfs_value='%s', "
97 "owner='%s', group='%s', mode=%#o",
98 dev->name, dev->bus, dev->sysfs_file, dev->sysfs_value,
99 dev->owner, dev->group, dev->mode);
102 dbg_parse("NUMBER name='%s', bus='%s', id='%s', "
103 "owner='%s', group='%s', mode=%#o",
104 dev->name, dev->bus, dev->id,
105 dev->owner, dev->group, dev->mode);
108 dbg_parse("TOPOLOGY name='%s', bus='%s', place='%s', "
109 "owner='%s', group='%s', mode=%#o",
110 dev->name, dev->bus, dev->place,
111 dev->owner, dev->group, dev->mode);
114 dbg_parse("REPLACE name=%s, kernel_name=%s, "
115 "owner='%s', group='%s', mode=%#o",
116 dev->name, dev->kernel_name,
117 dev->owner, dev->group, dev->mode);
120 dbg_parse("CALLOUT name='%s', bus='%s', program='%s', id='%s', "
121 "owner='%s', group='%s', mode=%#o",
122 dev->name, dev->bus, dev->exec_program, dev->id,
123 dev->owner, dev->group, dev->mode);
126 dbg_parse("unknown type of method");
130 void dump_config_dev_list(void)
132 struct list_head *tmp;
134 list_for_each(tmp, &config_device_list) {
135 struct config_device *dev = list_entry(tmp, struct config_device, node);
136 dump_config_dev(dev);
140 int namedev_init_config(void)
149 struct config_device dev;
151 dbg("opening '%s' to read as config", udev_config_filename);
152 fd = fopen(udev_config_filename, "r");
154 dbg("can't open '%s'", udev_config_filename);
158 /* loop through the whole file */
162 temp = fgets(line, sizeof(line), fd);
167 dbg_parse("read '%s'", temp);
169 /* eat the whitespace at the beginning of the line */
170 while (isspace(*temp))
177 /* see if this is a comment */
178 if (*temp == COMMENT_CHARACTER)
181 memset(&dev, 0x00, sizeof(struct config_device));
184 temp2 = strsep(&temp, ",");
185 if (strcasecmp(temp2, TYPE_LABEL) == 0) {
190 retval = get_value("BUS", &temp, &temp3);
193 strfieldcpy(dev.bus, temp3);
196 temp2 = strsep(&temp, ",");
197 retval = get_pair(&temp, &temp2, &temp3);
200 strfieldcpy(dev.sysfs_file, temp2);
201 strfieldcpy(dev.sysfs_value, temp3);
203 /* NAME="new_name" */
204 temp2 = strsep(&temp, ",");
205 retval = get_value("NAME", &temp, &temp3);
208 strfieldcpy(dev.name, temp3);
210 dbg_parse("LABEL name='%s', bus='%s', "
211 "sysfs_file='%s', sysfs_value='%s'",
212 dev.name, dev.bus, dev.sysfs_file,
216 if (strcasecmp(temp2, TYPE_NUMBER) == 0) {
221 retval = get_value("BUS", &temp, &temp3);
224 strfieldcpy(dev.bus, temp3);
227 temp2 = strsep(&temp, ",");
228 retval = get_value("ID", &temp, &temp3);
231 strfieldcpy(dev.id, temp3);
233 /* NAME="new_name" */
234 temp2 = strsep(&temp, ",");
235 retval = get_value("NAME", &temp, &temp3);
238 strfieldcpy(dev.name, temp3);
240 dbg_parse("NUMBER name='%s', bus='%s', id='%s'",
241 dev.name, dev.bus, dev.id);
244 if (strcasecmp(temp2, TYPE_TOPOLOGY) == 0) {
249 retval = get_value("BUS", &temp, &temp3);
252 strfieldcpy(dev.bus, temp3);
255 temp2 = strsep(&temp, ",");
256 retval = get_value("PLACE", &temp, &temp3);
259 strfieldcpy(dev.place, temp3);
261 /* NAME="new_name" */
262 temp2 = strsep(&temp, ",");
263 retval = get_value("NAME", &temp, &temp3);
266 strfieldcpy(dev.name, temp3);
268 dbg_parse("TOPOLOGY name='%s', bus='%s', place='%s'",
269 dev.name, dev.bus, dev.place);
272 if (strcasecmp(temp2, TYPE_REPLACE) == 0) {
276 /* KERNEL="kernel_name" */
277 retval = get_value("KERNEL", &temp, &temp3);
280 strfieldcpy(dev.kernel_name, temp3);
282 /* NAME="new_name" */
283 temp2 = strsep(&temp, ",");
284 retval = get_value("NAME", &temp, &temp3);
287 strfieldcpy(dev.name, temp3);
288 dbg_parse("REPLACE name='%s', kernel_name='%s'",
289 dev.name, dev.kernel_name);
291 if (strcasecmp(temp2, TYPE_CALLOUT) == 0) {
296 retval = get_value("BUS", &temp, &temp3);
299 strfieldcpy(dev.bus, temp3);
301 /* PROGRAM="executable" */
302 temp2 = strsep(&temp, ",");
303 retval = get_value("PROGRAM", &temp, &temp3);
306 strfieldcpy(dev.exec_program, temp3);
309 temp2 = strsep(&temp, ",");
310 retval = get_value("ID", &temp, &temp3);
313 strfieldcpy(dev.id, temp3);
315 /* NAME="new_name" */
316 temp2 = strsep(&temp, ",");
317 retval = get_value("NAME", &temp, &temp3);
320 strfieldcpy(dev.name, temp3);
321 dbg_parse("CALLOUT name='%s', program='%s'",
322 dev.name, dev.exec_program);
325 retval = add_config_dev(&dev);
327 dbg("add_config_dev returned with error %d", retval);
331 dbg_parse("%s:%d:%Zd: error parsing '%s'", udev_config_filename,
332 lineno, temp - line, temp);
339 int namedev_init_permissions(void)
346 struct config_device dev;
348 dbg("opening '%s' to read as permissions config", udev_config_permission_filename);
349 fd = fopen(udev_config_permission_filename, "r");
351 dbg("can't open '%s'", udev_config_permission_filename);
355 /* loop through the whole file */
357 temp = fgets(line, sizeof(line), fd);
361 dbg_parse("read '%s'", temp);
363 /* eat the whitespace at the beginning of the line */
364 while (isspace(*temp))
371 /* see if this is a comment */
372 if (*temp == COMMENT_CHARACTER)
375 memset(&dev, 0x00, sizeof(dev));
378 temp2 = strsep(&temp, ":");
380 dbg("cannot parse line '%s'", line);
383 strncpy(dev.name, temp2, sizeof(dev.name));
385 temp2 = strsep(&temp, ":");
387 dbg("cannot parse line '%s'", line);
390 strncpy(dev.owner, temp2, sizeof(dev.owner));
392 temp2 = strsep(&temp, ":");
394 dbg("cannot parse line '%s'", line);
397 strncpy(dev.group, temp2, sizeof(dev.owner));
400 dbg("cannot parse line: %s", line);
403 dev.mode = strtol(temp, NULL, 8);
405 dbg_parse("name='%s', owner='%s', group='%s', mode=%#o",
406 dev.name, dev.owner, dev.group,
408 retval = add_config_dev(&dev);
410 dbg("add_config_dev returned with error %d", retval);