static int x11_read_data(Context *c) { _cleanup_fclose_ FILE *f; char line[LINE_MAX]; bool in_section = false; int r; context_free_x11(c); f = fopen("/etc/X11/xorg.conf.d/00-keyboard.conf", "re"); if (!f) return errno == ENOENT ? 0 : -errno; while (fgets(line, sizeof(line), f)) { char *l; char_array_0(line); l = strstrip(line); if (l[0] == 0 || l[0] == '#') continue; if (in_section && first_word(l, "Option")) { _cleanup_strv_free_ char **a = NULL; r = strv_split_quoted(&a, l, false); if (r < 0) return r; if (strv_length(a) == 3) { if (streq(a[1], "XkbLayout")) { free_and_replace(&c->x11_layout, a[2]); a[2] = NULL; } else if (streq(a[1], "XkbModel")) { free_and_replace(&c->x11_model, a[2]); a[2] = NULL; } else if (streq(a[1], "XkbVariant")) { free_and_replace(&c->x11_variant, a[2]); a[2] = NULL; } else if (streq(a[1], "XkbOptions")) { free_and_replace(&c->x11_options, a[2]); a[2] = NULL; } } } else if (!in_section && first_word(l, "Section")) { _cleanup_strv_free_ char **a = NULL; r = strv_split_quoted(&a, l, false); if (r < 0) return -ENOMEM; if (strv_length(a) == 2 && streq(a[1], "InputClass")) in_section = true; } else if (in_section && first_word(l, "EndSection")) in_section = false; } return 0; }
static void test_strv_quote_unquote(const char* const *split, const char *quoted) { _cleanup_free_ char *p; _cleanup_strv_free_ char **s; char **t; p = strv_join_quoted((char **)split); printf("-%s- --- -%s-\n", p, quoted); /* fprintf deals with NULL, puts does not */ assert_se(p); assert_se(streq(p, quoted)); s = strv_split_quoted(quoted); assert_se(s); STRV_FOREACH(t, s) { assert_se(*t); assert_se(streq(*t, *split)); split++; }
static int spawn_getter(const char *getter, const char *url) { int r; _cleanup_strv_free_ char **words = NULL; assert(getter); r = strv_split_quoted(&words, getter, false); if (r < 0) return log_error_errno(r, "Failed to split getter option: %m"); r = strv_extend(&words, url); if (r < 0) return log_error_errno(r, "Failed to create command line: %m"); r = spawn_child(words[0], words); if (r < 0) log_error_errno(errno, "Failed to spawn getter %s: %m", getter); return r; }
static int read_next_mapping(FILE *f, unsigned *n, char ***a) { assert(f); assert(n); assert(a); for (;;) { char line[LINE_MAX]; char *l, **b; int r; errno = 0; if (!fgets(line, sizeof(line), f)) { if (ferror(f)) return errno ? -errno : -EIO; return 0; } (*n) ++; l = strstrip(line); if (l[0] == 0 || l[0] == '#') continue; r = strv_split_quoted(&b, l, false); if (r < 0) return r; if (strv_length(b) < 5) { log_error("Invalid line "SYSTEMD_KBD_MODEL_MAP":%u, ignoring.", *n); strv_free(b); continue; } *a = b; return 1; } }
static int read_data_x11(void) { FILE *f; char line[LINE_MAX]; bool in_section = false; free_data_x11(); f = fopen("/etc/X11/xorg.conf.d/00-keyboard.conf", "re"); if (!f) { if (errno == ENOENT) { #ifdef TARGET_FEDORA f = fopen("/etc/X11/xorg.conf.d/00-system-setup-keyboard.conf", "re"); if (!f) { if (errno == ENOENT) return 0; else return -errno; } #else return 0; #endif } else return -errno; } while (fgets(line, sizeof(line), f)) { char *l; char_array_0(line); l = strstrip(line); if (l[0] == 0 || l[0] == '#') continue; if (in_section && first_word(l, "Option")) { char **a; a = strv_split_quoted(l); if (!a) { fclose(f); return -ENOMEM; } if (strv_length(a) == 3) { if (streq(a[1], "XkbLayout")) { free(state.x11_layout); state.x11_layout = a[2]; a[2] = NULL; } else if (streq(a[1], "XkbModel")) { free(state.x11_model); state.x11_model = a[2]; a[2] = NULL; } else if (streq(a[1], "XkbVariant")) { free(state.x11_variant); state.x11_variant = a[2]; a[2] = NULL; } else if (streq(a[1], "XkbOptions")) { free(state.x11_options); state.x11_options = a[2]; a[2] = NULL; } } strv_free(a); } else if (!in_section && first_word(l, "Section")) { char **a; a = strv_split_quoted(l); if (!a) { fclose(f); return -ENOMEM; } if (strv_length(a) == 2 && streq(a[1], "InputClass")) in_section = true; strv_free(a); } else if (in_section && first_word(l, "EndSection")) in_section = false; } fclose(f); return 0; }