Debian Codegraph @

lib/wp/private/parse-conf-section.c

raw
1 /* PipeWire */
2 /* SPDX-FileCopyrightText: Copyright © 2021 Wim Taymans */
3 /* SPDX-License-Identifier: MIT */
4
5 /*
6 This is a partial copy of functions from libpipewire's conf.c that is meant to
7 live here temporarily until pw_context_parse_conf_section() is fixed upstream.
8 See https://gitlab.freedesktop.org/pipewire/pipewire/-/merge_requests/1925
9 */
10
11 #include <string.h>
12
13 #include <spa/utils/result.h>
14 #include <spa/utils/string.h>
15 #include <spa/utils/json.h>
16 #include <spa/utils/cleanup.h>
17
18 #include <pipewire/impl.h>
19
20 struct data {
21 struct pw_context *context;
22 struct pw_properties *props;
23 int count;
24 };
25
26 /* context.spa-libs = {
27 * <factory-name regex> = <library-name>
28 * }
29 */
30 static int parse_spa_libs(void *user_data, const char *location,
31 const char *section, const char *str, size_t len)
32 {
33 struct data *d = user_data;
34 struct pw_context *context = d->context;
35 struct spa_json it[2];
36 char key[512], value[512];
37
38 spa_json_init(&it[0], str, len);
39 if (spa_json_enter_object(&it[0], &it[1]) < 0) {
40 pw_log_error("config file error: context.spa-libs is not an object");
41 return -EINVAL;
42 }
43
44 while (spa_json_get_string(&it[1], key, sizeof(key)) > 0) {
45 if (spa_json_get_string(&it[1], value, sizeof(value)) > 0) {
46 pw_context_add_spa_lib(context, key, value);
47 d->count++;
48 }
49 }
50 return 0;
51 }
52
53
54
55 static int load_module(struct pw_context *context, const char *key, const char *args, const char *flags)
56 {
57 if (pw_context_load_module(context, key, args, NULL) == NULL) {
58 if (errno == ENOENT && flags && strstr(flags, "ifexists") != NULL) {
59 pw_log_info("%p: skipping unavailable module %s",
60 context, key);
61 } else if (flags == NULL || strstr(flags, "nofail") == NULL) {
62 pw_log_error("%p: could not load mandatory module \"%s\": %m",
63 context, key);
64 return -errno;
65 } else {
66 pw_log_info("%p: could not load optional module \"%s\": %m",
67 context, key);
68 }
69 } else {
70 pw_log_info("%p: loaded module %s", context, key);
71 }
72 return 0;
73 }
74
75 /*
76 * context.modules = [
77 * { name = <module-name>
78 * ( args = { <key> = <value> ... } )
79 * ( flags = [ ( ifexists ) ( nofail ) ]
80 * ( condition = [ { key = value, .. } .. ] )
81 * }
82 * ]
83 */
84 static int parse_modules(void *user_data, const char *location,
85 const char *section, const char *str, size_t len)
86 {
87 struct data *d = user_data;
88 struct pw_context *context = d->context;
89 struct spa_json it[4];
90 char key[512];
91 int res = 0;
92
93 spa_autofree char *s = strndup(str, len);
94 spa_json_init(&it[0], s, len);
95 if (spa_json_enter_array(&it[0], &it[1]) < 0) {
96 pw_log_error("config file error: context.modules is not an array");
97 return -EINVAL;
98 }
99
100 while (spa_json_enter_object(&it[1], &it[2]) > 0) {
101 char *name = NULL, *args = NULL, *flags = NULL;
102 bool have_match = true;
103
104 while (spa_json_get_string(&it[2], key, sizeof(key)) > 0) {
105 const char *val;
106 int len;
107
108 if ((len = spa_json_next(&it[2], &val)) <= 0)
109 break;
110
111 if (spa_streq(key, "name")) {
112 name = (char*)val;
113 spa_json_parse_stringn(val, len, name, len+1);
114 } else if (spa_streq(key, "args")) {
115 if (spa_json_is_container(val, len))
116 len = spa_json_container_len(&it[2], val, len);
117
118 args = (char*)val;
119 spa_json_parse_stringn(val, len, args, len+1);
120 } else if (spa_streq(key, "flags")) {
121 if (spa_json_is_container(val, len))
122 len = spa_json_container_len(&it[2], val, len);
123 flags = (char*)val;
124 spa_json_parse_stringn(val, len, flags, len+1);
125 }
126 }
127 if (!have_match)
128 continue;
129
130 if (name != NULL)
131 res = load_module(context, name, args, flags);
132
133 if (res < 0)
134 break;
135
136 d->count++;
137 }
138
139 return res;
140 }
141
142 static int _pw_context_parse_conf_section(struct pw_context *context,
143 struct pw_properties *conf, const char *section)
144 {
145 struct data data = { .context = context };
146 int res;
147
148 if (spa_streq(section, "context.spa-libs"))
149 res = pw_conf_section_for_each(&conf->dict, section,
150 parse_spa_libs, &data);
151 else if (spa_streq(section, "context.modules"))
152 res = pw_conf_section_for_each(&conf->dict, section,
153 parse_modules, &data);
154 else
155 res = -EINVAL;
156
157 return res == 0 ? data.count : res;
158 }