+static int get_key(char **line, char **key, char **value)
+{
+ char *linepos;
+ char *temp;
+
+ linepos = *line;
+ if (!linepos)
+ return -1;
+
+ if (strchr(linepos, '\\')) {
+ dbg("escaped characters are not supported, skip");
+ return -1;
+ }
+
+ /* skip whitespace */
+ while (isspace(linepos[0]))
+ linepos++;
+
+ /* get the key */
+ *key = linepos;
+ while (1) {
+ linepos++;
+ if (linepos[0] == '\0')
+ return -1;
+ if (isspace(linepos[0]))
+ break;
+ if (linepos[0] == '=')
+ break;
+ }
+
+ /* terminate key */
+ linepos[0] = '\0';
+ linepos++;
+
+ /* skip whitespace */
+ while (isspace(linepos[0]))
+ linepos++;
+
+ /* get the value*/
+ if (linepos[0] == '"') {
+ linepos++;
+ temp = strchr(linepos, '"');
+ if (!temp) {
+ dbg("missing closing quote");
+ return -1;
+ }
+ dbg("value is quoted");
+ temp[0] = '\0';
+ } else if (linepos[0] == '\'') {
+ linepos++;
+ temp = strchr(linepos, '\'');
+ if (!temp) {
+ dbg("missing closing quote");
+ return -1;
+ }
+ dbg("value is quoted");
+ temp[0] = '\0';
+ } else if (linepos[0] == '\0') {
+ dbg("value is empty");
+ } else {
+ temp = linepos;
+ while (temp[0] && !isspace(temp[0]))
+ temp++;
+ temp[0] = '\0';
+ }
+ *value = linepos;
+
+ return 0;
+}
+
+static int import_keys_into_env(struct udevice *udev, const char *buf, size_t bufsize)
+{
+ char line[LINE_SIZE];
+ const char *bufline;
+ char *linepos;
+ char *variable;
+ char *value;
+ size_t cur;
+ size_t count;
+ int lineno;
+
+ /* loop through the whole buffer */
+ lineno = 0;
+ cur = 0;
+ while (cur < bufsize) {
+ count = buf_get_line(buf, bufsize, cur);
+ bufline = &buf[cur];
+ cur += count+1;
+ lineno++;
+
+ if (count >= sizeof(line)) {
+ err("line too long, conf line skipped %s, line %d", udev_config_filename, lineno);
+ continue;
+ }
+
+ /* 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;
+
+ strlcpy(line, bufline, count+1);
+
+ linepos = line;
+ if (get_key(&linepos, &variable, &value) == 0) {
+ dbg("import '%s=%s'", variable, value);
+ name_list_key_add(&udev->env_list, variable, value);
+ setenv(variable, value, 1);
+ }
+ }
+
+ return 0;
+}
+
+static int import_file_into_env(struct udevice *udev, const char *filename)
+{
+ char *buf;
+ size_t bufsize;
+
+ if (file_map(filename, &buf, &bufsize) != 0) {
+ err("can't open '%s'", filename);
+ return -1;
+ }
+ import_keys_into_env(udev, buf, bufsize);
+ file_unmap(buf, bufsize);
+
+ return 0;
+}
+
+static int import_program_into_env(struct udevice *udev, const char *program)
+{
+ char result[1024];
+ size_t reslen;
+
+ if (execute_program(program, udev->subsystem, result, sizeof(result), &reslen) != 0)
+ return -1;
+ return import_keys_into_env(udev, result, reslen);
+}
+
+/* finds the lowest positive N such that <name>N isn't present in the udevdb
+ * if <name> doesn't exist, 0 is returned, N otherwise