FreeBSD: Add missing OSS option --excl to man page.
[jack2.git] / tools / property.c
blob4d5e2a22ff8d8088d6f8623801778a91ee54c88b
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <string.h>
5 #include <getopt.h>
7 #include <jack/jack.h>
8 #include <jack/metadata.h>
9 #include <jack/uuid.h>
10 #include <jack/session.h>
12 static int subject_is_client = 0;
13 static int subject_is_port = 0;
14 static jack_uuid_t uuid = JACK_UUID_EMPTY_INITIALIZER;
15 static char* subject = NULL;
17 static void
18 show_usage (void)
20 fprintf (stderr, "\nUsage: jack_property [options] UUID [ key [ value [ type ] ] ]\n");
21 fprintf (stderr, "Set/Display JACK properties (metadata).\n\n");
22 fprintf (stderr, "Set options:\n");
23 fprintf (stderr, " -s, --set Set property \"key\" to \"value\" for \"UUID\" with optional MIME type \"type\"\n");
24 fprintf (stderr, " -d, --delete Remove/delete property \"key\" for \"UUID\"\n");
25 fprintf (stderr, " -d, --delete UUID Remove/delete all properties for \"UUID\"\n");
26 fprintf (stderr, " -D, --delete-all Remove/delete all properties\n");
27 fprintf (stderr, " --client Interpret UUID as a client name, not a UUID\n");
28 fprintf (stderr, " --port \tInterpret UUID as a port name, not a UUID\n");
29 fprintf (stderr, "\nDisplay options:\n");
30 fprintf (stderr, " -l Show all properties\n");
31 fprintf (stderr, " -l, --list UUID \tShow value for all properties of UUID\n");
32 fprintf (stderr, " -l, --list UUID key Show value for key of UUID\n");
33 fprintf (stderr, "\nFor more information see https://jackaudio.org/\n");
36 static int
37 get_subject (jack_client_t* client, char* argv[], int* optind)
39 if (subject_is_client) {
40 char* cstr = argv[(*optind)++];
41 char* ustr;
43 if ((ustr = jack_get_uuid_for_client_name (client, cstr)) == NULL) {
44 fprintf (stderr, "cannot get UUID for client named %s\n", cstr);
45 return -1;
48 if (jack_uuid_parse (ustr, &uuid)) {
49 fprintf (stderr, "cannot parse client UUID as UUID '%s' '%s'\n", cstr, ustr);
50 return -1;
53 subject = cstr;
55 } else if (subject_is_port) {
57 char* pstr = argv[(*optind)++];
58 jack_port_t* port;
60 if ((port = jack_port_by_name (client, pstr)) == NULL) {
61 fprintf (stderr, "cannot find port name %s\n", pstr);
62 return -1;
65 uuid = jack_port_uuid (port);
66 subject = pstr;
68 } else {
69 char* str = argv[(*optind)++];
71 if (jack_uuid_parse (str, &uuid)) {
72 fprintf (stderr, "cannot parse subject as UUID\n");
73 return -1;
76 subject = str;
79 return 0;
82 int main (int argc, char* argv[])
84 jack_client_t* client = NULL;
85 jack_options_t options = JackNoStartServer;
86 char* key = NULL;
87 char* value = NULL;
88 char* type = NULL;
89 int set = 1;
90 int delete = 0;
91 int delete_all = 0;
92 int c;
93 int option_index;
94 extern int optind;
95 struct option long_options[] = {
96 { "set", 0, 0, 's' },
97 { "delete", 0, 0, 'd' },
98 { "delete-all", 0, 0, 'D' },
99 { "list", 0, 0, 'l' },
100 { "client", 0, 0, 'c' },
101 { "port", 0, 0, 'p' },
102 { 0, 0, 0, 0 }
105 if (argc < 2) {
106 show_usage ();
107 exit (1);
110 while ((c = getopt_long (argc, argv, "sdDlaApc", long_options, &option_index)) >= 0) {
111 switch (c) {
112 case 's':
113 if (argc < 5) {
114 show_usage ();
115 exit (1);
117 set = 1;
118 break;
119 case 'd':
120 if (argc < 3) {
121 show_usage ();
122 return 1;
124 set = 0;
125 delete = 1;
126 break;
128 case 'D':
129 delete = 0;
130 set = 0;
131 delete_all = 1;
132 break;
134 case 'l':
135 set = 0;
136 delete = 0;
137 delete_all = 0;
138 break;
140 case 'p':
141 subject_is_port = 1;
142 break;
144 case 'c':
145 subject_is_client = 1;
146 break;
148 case '?':
149 default:
150 show_usage ();
151 exit (1);
155 if ((client = jack_client_open ("jack-property", options, NULL)) == 0) {
156 fprintf (stderr, "Cannot connect to JACK server\n");
157 exit (1);
160 if (delete_all) {
162 if (jack_remove_all_properties (client) == 0) {
163 printf ("JACK metadata successfully delete\n");
164 exit (0);
166 exit (1);
169 if (delete) {
171 int args_left = argc - optind;
173 if (args_left < 1) {
174 show_usage ();
175 exit (1);
178 /* argc == 3: delete all properties for a subject
179 argc == 4: delete value of key for subject
182 if (args_left >= 2) {
184 if (get_subject (client, argv, &optind)) {
185 return 1;
188 key = argv[optind++];
190 if (jack_remove_property (client, uuid, key)) {
191 fprintf (stderr, "\"%s\" property not removed for %s\n", key, subject);
192 exit (1);
195 } else {
197 if (get_subject (client, argv, &optind)) {
198 return 1;
201 if (jack_remove_properties (client, uuid) < 0) {
202 fprintf (stderr, "cannot remove properties for UUID %s\n", subject);
203 exit (1);
207 } else if (set) {
209 int args_left = argc - optind;
211 if (get_subject (client, argv, &optind)) {
212 return -1;
215 key = argv[optind++];
216 value = argv[optind++];
218 if (args_left >= 3) {
219 type = argv[optind++];
220 } else {
221 type = "";
224 if (jack_set_property (client, uuid, key, value, type)) {
225 fprintf (stderr, "cannot set value for key %s of %s\n", key, subject);
226 exit (1);
229 } else {
231 /* list properties */
233 int args_left = argc - optind;
235 if (args_left >= 2) {
237 /* list properties for a UUID/key pair */
239 if (get_subject (client, argv, &optind)) {
240 return -1;
243 key = argv[optind++];
245 if (jack_get_property (uuid, key, &value, &type) == 0) {
246 printf ("%s\n", value);
247 free (value);
248 if (type) {
249 free (type);
251 } else {
252 fprintf (stderr, "Value not found for %s of %s\n", key, subject);
253 exit (1);
256 } else if (args_left == 1) {
258 /* list all properties for a given UUID */
260 jack_description_t description;
261 int cnt, n;
263 if (get_subject (client, argv, &optind)) {
264 return -1;
267 if ((cnt = jack_get_properties (uuid, &description)) < 0) {
268 fprintf (stderr, "could not retrieve properties for %s\n", subject);
269 exit (1);
272 for (n = 0; n < cnt; ++n) {
273 if (description.properties[n].type) {
274 printf ("key: %s value: %s type: %s\n",
275 description.properties[n].key,
276 description.properties[n].data,
277 description.properties[n].type);
278 } else {
279 printf ("key: %s value: %s\n",
280 description.properties[n].key,
281 description.properties[n].data);
285 jack_free_description (&description, 0);
287 } else {
289 /* list all properties */
291 jack_description_t* description;
292 int cnt, n;
293 size_t p;
294 char buf[JACK_UUID_STRING_SIZE];
296 if ((cnt = jack_get_all_properties (&description)) < 0) {
297 fprintf (stderr, "could not retrieve all properties\n");
298 exit (1);
301 for (n = 0; n < cnt; ++n) {
302 jack_uuid_unparse (description[n].subject, buf);
303 printf ("%s\n", buf);
304 for (p = 0; p < description[n].property_cnt; ++p) {
305 if (description[n].properties[p].type) {
306 printf ("key: %s value: %s type: %s\n",
307 description[n].properties[p].key,
308 description[n].properties[p].data,
309 description[n].properties[p].type);
310 } else {
311 printf ("key: %s value: %s\n",
312 description[n].properties[p].key,
313 description[n].properties[p].data);
316 jack_free_description (&description[n], 0);
319 free (description);
324 (void) jack_client_close (client);
325 return 0;