- rule.config_line = lineno;
- strlcpy(rule.config_file, filename, sizeof(rule.config_file));
- retval = add_config_dev(&rule);
- if (retval) {
- dbg("add_config_dev returned with error %d", retval);
+ err("unknown key '%s' in %s:%u", key, filename, lineno);
+ }
+
+ /* skip line if not any valid key was found */
+ if (!valid)
+ goto invalid;
+
+ /* grow buffer and add rule */
+ rule_size = sizeof(struct udev_rule) + rule->bufsize;
+ padding = (sizeof(size_t) - rule_size % sizeof(size_t)) % sizeof(size_t);
+ dbg("add %zi padding bytes", padding);
+ rule_size += padding;
+ rule->bufsize += padding;
+
+ rules->buf = realloc(rules->buf, rules->bufsize + rule_size);
+ if (!rules->buf) {
+ err("realloc failed");
+ goto exit;
+ }
+ dbg("adding rule to offset %zi", rules->bufsize);
+ memcpy(rules->buf + rules->bufsize, rule, rule_size);
+ rules->bufsize += rule_size;
+exit:
+ free(rule);
+ return 0;
+
+invalid:
+ free(rule);
+ err("invalid rule '%s:%u'", filename, lineno);
+ return -1;
+}
+
+static int parse_file(struct udev_rules *rules, const char *filename)
+{
+ char line[LINE_SIZE];
+ char *bufline;
+ unsigned int lineno;
+ char *buf;
+ size_t bufsize;
+ size_t cur;
+ size_t count;
+ int retval = 0;
+
+ if (file_map(filename, &buf, &bufsize) != 0) {
+ err("can't open '%s' as rules file: %s", filename, strerror(errno));
+ return -1;
+ }
+ dbg("reading '%s' as rules file", filename);
+
+ /* loop through the whole file */
+ cur = 0;
+ lineno = 0;
+ while (cur < bufsize) {
+ unsigned int i, j;
+
+ count = buf_get_line(buf, bufsize, cur);
+ bufline = &buf[cur];
+ cur += count+1;
+ lineno++;
+
+ /* eat the whitespace */
+ while ((count > 0) && isspace(bufline[0])) {
+ bufline++;
+ count--;
+ }
+ if (count == 0)
+ continue;
+
+ /* see if this is a comment */
+ if (bufline[0] == COMMENT_CHARACTER)
+ continue;
+
+ if (count >= sizeof(line)) {
+ err("line too long, rule skipped '%s:%u'", filename, lineno);