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 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_rules(void)
149 struct config_device dev;
151 fd = fopen(udev_rules_filename, "r");
153 dbg("reading '%s' as rules file", udev_rules_filename);
155 dbg("can't open '%s' as a rules file", udev_rules_filename);
159 /* loop through the whole file */
163 temp = fgets(line, sizeof(line), fd);
168 dbg_parse("read '%s'", temp);
170 /* eat the whitespace at the beginning of the line */
171 while (isspace(*temp))
178 /* see if this is a comment */
179 if (*temp == COMMENT_CHARACTER)
182 memset(&dev, 0x00, sizeof(struct config_device));
185 temp2 = strsep(&temp, ",");
186 if (strcasecmp(temp2, TYPE_LABEL) == 0) {
191 retval = get_value("BUS", &temp, &temp3);
194 strfieldcpy(dev.bus, temp3);
197 temp2 = strsep(&temp, ",");
198 retval = get_pair(&temp, &temp2, &temp3);
201 strfieldcpy(dev.sysfs_file, temp2);
202 strfieldcpy(dev.sysfs_value, temp3);
204 /* NAME="new_name" */
205 temp2 = strsep(&temp, ",");
206 retval = get_value("NAME", &temp, &temp3);
209 strfieldcpy(dev.name, temp3);
211 dbg_parse("LABEL name='%s', bus='%s', "
212 "sysfs_file='%s', sysfs_value='%s'",
213 dev.name, dev.bus, dev.sysfs_file,
217 if (strcasecmp(temp2, TYPE_NUMBER) == 0) {
222 retval = get_value("BUS", &temp, &temp3);
225 strfieldcpy(dev.bus, temp3);
228 temp2 = strsep(&temp, ",");
229 retval = get_value("ID", &temp, &temp3);
232 strfieldcpy(dev.id, temp3);
234 /* NAME="new_name" */
235 temp2 = strsep(&temp, ",");
236 retval = get_value("NAME", &temp, &temp3);
239 strfieldcpy(dev.name, temp3);
241 dbg_parse("NUMBER name='%s', bus='%s', id='%s'",
242 dev.name, dev.bus, dev.id);
245 if (strcasecmp(temp2, TYPE_TOPOLOGY) == 0) {
250 retval = get_value("BUS", &temp, &temp3);
253 strfieldcpy(dev.bus, temp3);
256 temp2 = strsep(&temp, ",");
257 retval = get_value("PLACE", &temp, &temp3);
260 strfieldcpy(dev.place, temp3);
262 /* NAME="new_name" */
263 temp2 = strsep(&temp, ",");
264 retval = get_value("NAME", &temp, &temp3);
267 strfieldcpy(dev.name, temp3);
269 dbg_parse("TOPOLOGY name='%s', bus='%s', place='%s'",
270 dev.name, dev.bus, dev.place);
273 if (strcasecmp(temp2, TYPE_REPLACE) == 0) {
277 /* KERNEL="kernel_name" */
278 retval = get_value("KERNEL", &temp, &temp3);
281 strfieldcpy(dev.kernel_name, temp3);
283 /* NAME="new_name" */
284 temp2 = strsep(&temp, ",");
285 retval = get_value("NAME", &temp, &temp3);
288 strfieldcpy(dev.name, temp3);
289 dbg_parse("REPLACE name='%s', kernel_name='%s'",
290 dev.name, dev.kernel_name);
292 if (strcasecmp(temp2, TYPE_CALLOUT) == 0) {
297 retval = get_value("BUS", &temp, &temp3);
300 strfieldcpy(dev.bus, temp3);
302 /* PROGRAM="executable" */
303 temp2 = strsep(&temp, ",");
304 retval = get_value("PROGRAM", &temp, &temp3);
307 strfieldcpy(dev.exec_program, temp3);
310 temp2 = strsep(&temp, ",");
311 retval = get_value("ID", &temp, &temp3);
314 strfieldcpy(dev.id, temp3);
316 /* NAME="new_name" */
317 temp2 = strsep(&temp, ",");
318 retval = get_value("NAME", &temp, &temp3);
321 strfieldcpy(dev.name, temp3);
322 dbg_parse("CALLOUT name='%s', program='%s'",
323 dev.name, dev.exec_program);
326 retval = add_config_dev(&dev);
328 dbg("add_config_dev returned with error %d", retval);
332 dbg_parse("%s:%d:%Zd: error parsing '%s'", udev_rules_filename,
333 lineno, temp - line, temp);
340 int namedev_init_permissions(void)
347 struct config_device dev;
349 fd = fopen(udev_permission_filename, "r");
351 dbg("reading '%s' as permissions file", udev_permission_filename);
353 dbg("can't open '%s' as permissions file", udev_permission_filename);
357 /* loop through the whole file */
359 temp = fgets(line, sizeof(line), fd);
363 dbg_parse("read '%s'", temp);
365 /* eat the whitespace at the beginning of the line */
366 while (isspace(*temp))
373 /* see if this is a comment */
374 if (*temp == COMMENT_CHARACTER)
377 memset(&dev, 0x00, sizeof(dev));
380 temp2 = strsep(&temp, ":");
382 dbg("cannot parse line '%s'", line);
385 strncpy(dev.name, temp2, sizeof(dev.name));
387 temp2 = strsep(&temp, ":");
389 dbg("cannot parse line '%s'", line);
392 strncpy(dev.owner, temp2, sizeof(dev.owner));
394 temp2 = strsep(&temp, ":");
396 dbg("cannot parse line '%s'", line);
399 strncpy(dev.group, temp2, sizeof(dev.owner));
402 dbg("cannot parse line: %s", line);
405 dev.mode = strtol(temp, NULL, 8);
407 dbg_parse("name='%s', owner='%s', group='%s', mode=%#o",
408 dev.name, dev.owner, dev.group,
410 retval = add_config_dev(&dev);
412 dbg("add_config_dev returned with error %d", retval);