]> code.delx.au - pulseaudio/blob - polyp/cli-command.c
sample cache work
[pulseaudio] / polyp / cli-command.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
10
11 polypaudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <string.h>
28 #include <assert.h>
29 #include <stdlib.h>
30 #include <errno.h>
31
32 #include "cli-command.h"
33 #include "module.h"
34 #include "sink.h"
35 #include "source.h"
36 #include "client.h"
37 #include "sink-input.h"
38 #include "source-output.h"
39 #include "tokenizer.h"
40 #include "strbuf.h"
41 #include "namereg.h"
42 #include "clitext.h"
43 #include "scache.h"
44 #include "sample-util.h"
45 #include "sound-file.h"
46 #include "play-memchunk.h"
47
48 struct command {
49 const char *name;
50 int (*proc) (struct pa_core *c, struct pa_tokenizer*t, struct pa_strbuf *buf, int *fail, int *verbose);
51 const char *help;
52 unsigned args;
53 };
54
55 static int pa_cli_command_exit(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
56 static int pa_cli_command_help(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
57 static int pa_cli_command_modules(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
58 static int pa_cli_command_clients(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
59 static int pa_cli_command_sinks(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
60 static int pa_cli_command_sources(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
61 static int pa_cli_command_sink_inputs(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
62 static int pa_cli_command_source_outputs(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
63 static int pa_cli_command_stat(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
64 static int pa_cli_command_info(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
65 static int pa_cli_command_load(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
66 static int pa_cli_command_unload(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
67 static int pa_cli_command_sink_volume(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
68 static int pa_cli_command_sink_input_volume(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
69 static int pa_cli_command_sink_default(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
70 static int pa_cli_command_source_default(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
71 static int pa_cli_command_kill_client(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
72 static int pa_cli_command_kill_sink_input(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
73 static int pa_cli_command_kill_source_output(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
74 static int pa_cli_command_scache_play(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
75 static int pa_cli_command_scache_remove(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
76 static int pa_cli_command_scache_list(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
77 static int pa_cli_command_scache_load(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
78 static int pa_cli_command_play_file(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
79
80 static const struct command commands[] = {
81 { "exit", pa_cli_command_exit, "Terminate the daemon", 1 },
82 { "help", pa_cli_command_help, "Show this help", 1 },
83 { "modules", pa_cli_command_modules, "List loaded modules", 1 },
84 { "sinks", pa_cli_command_sinks, "List loaded sinks", 1 },
85 { "sources", pa_cli_command_sources, "List loaded sources", 1 },
86 { "clients", pa_cli_command_clients, "List loaded clients", 1 },
87 { "sink_inputs", pa_cli_command_sink_inputs, "List sink inputs", 1 },
88 { "source_outputs", pa_cli_command_source_outputs, "List source outputs", 1 },
89 { "stat", pa_cli_command_stat, "Show memory block statistics", 1 },
90 { "info", pa_cli_command_info, "Show comprehensive status", 1 },
91 { "ls", pa_cli_command_info, NULL, 1 },
92 { "list", pa_cli_command_info, NULL, 1 },
93 { "load", pa_cli_command_load, "Load a module (args: name, arguments)", 3},
94 { "unload", pa_cli_command_unload, "Unload a module (args: index)", 2},
95 { "sink_volume", pa_cli_command_sink_volume, "Set the volume of a sink (args: index|name, volume)", 3},
96 { "sink_input_volume", pa_cli_command_sink_input_volume, "Set the volume of a sink input (args: index|name, volume)", 3},
97 { "sink_default", pa_cli_command_sink_default, "Set the default sink (args: index|name)", 2},
98 { "source_default", pa_cli_command_source_default, "Set the default source (args: index|name)", 2},
99 { "kill_client", pa_cli_command_kill_client, "Kill a client (args: index)", 2},
100 { "kill_sink_input", pa_cli_command_kill_sink_input, "Kill a sink input (args: index)", 2},
101 { "kill_source_output", pa_cli_command_kill_source_output, "Kill a source output (args: index)", 2},
102 { "scache_list", pa_cli_command_scache_list, "List all entries in the sample cache", 2},
103 { "scache_play", pa_cli_command_scache_play, "Play a sample from the sample cache (args: name, sink|index)", 3},
104 { "scache_remove", pa_cli_command_scache_remove, "Remove a sample from the sample cache (args: name)", 2},
105 { "scache_load", pa_cli_command_scache_load, "Load a sound file into the sample cache (args: filename,name)", 3},
106 { "play_file", pa_cli_command_play_file, "Play a sound file (args: filename, sink|index)", 3},
107 { NULL, NULL, NULL, 0 }
108 };
109
110 static const char whitespace[] = " \t\n\r";
111 static const char linebreak[] = "\n\r";
112
113 static uint32_t parse_index(const char *n) {
114 long index;
115 char *x;
116 index = strtol(n, &x, 0);
117 if (!x || *x != 0 || index < 0)
118 return (uint32_t) PA_IDXSET_INVALID;
119
120 return (uint32_t) index;
121 }
122
123 static int pa_cli_command_exit(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
124 assert(c && c->mainloop && t);
125 c->mainloop->quit(c->mainloop, 0);
126 return 0;
127 }
128
129 static int pa_cli_command_help(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
130 const struct command*command;
131 assert(c && t && buf);
132
133 pa_strbuf_puts(buf, "Available commands:\n");
134
135 for (command = commands; command->name; command++)
136 if (command->help)
137 pa_strbuf_printf(buf, " %-20s %s\n", command->name, command->help);
138 return 0;
139 }
140
141 static int pa_cli_command_modules(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
142 char *s;
143 assert(c && t);
144 s = pa_module_list_to_string(c);
145 assert(s);
146 pa_strbuf_puts(buf, s);
147 free(s);
148 return 0;
149 }
150
151 static int pa_cli_command_clients(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
152 char *s;
153 assert(c && t);
154 s = pa_client_list_to_string(c);
155 assert(s);
156 pa_strbuf_puts(buf, s);
157 free(s);
158 return 0;
159 }
160
161 static int pa_cli_command_sinks(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
162 char *s;
163 assert(c && t);
164 s = pa_sink_list_to_string(c);
165 assert(s);
166 pa_strbuf_puts(buf, s);
167 free(s);
168 return 0;
169 }
170
171 static int pa_cli_command_sources(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
172 char *s;
173 assert(c && t);
174 s = pa_source_list_to_string(c);
175 assert(s);
176 pa_strbuf_puts(buf, s);
177 free(s);
178 return 0;
179 }
180
181 static int pa_cli_command_sink_inputs(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
182 char *s;
183 assert(c && t);
184 s = pa_sink_input_list_to_string(c);
185 assert(s);
186 pa_strbuf_puts(buf, s);
187 free(s);
188 return 0;
189 }
190
191 static int pa_cli_command_source_outputs(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
192 char *s;
193 assert(c && t);
194 s = pa_source_output_list_to_string(c);
195 assert(s);
196 pa_strbuf_puts(buf, s);
197 free(s);
198 return 0;
199 }
200
201 static int pa_cli_command_stat(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
202 assert(c && t);
203 pa_strbuf_printf(buf, "Memory blocks allocated: %u, total size: %u bytes.\n", pa_memblock_get_count(), pa_memblock_get_total());
204 return 0;
205 }
206
207 static int pa_cli_command_info(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
208 assert(c && t);
209 pa_cli_command_stat(c, t, buf, fail, verbose);
210 pa_cli_command_modules(c, t, buf, fail, verbose);
211 pa_cli_command_sinks(c, t, buf, fail, verbose);
212 pa_cli_command_sources(c, t, buf, fail, verbose);
213 pa_cli_command_clients(c, t, buf, fail, verbose);
214 pa_cli_command_sink_inputs(c, t, buf, fail, verbose);
215 pa_cli_command_source_outputs(c, t, buf, fail, verbose);
216 pa_cli_command_scache_list(c, t, buf, fail, verbose);
217 return 0;
218 }
219
220 static int pa_cli_command_load(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
221 struct pa_module *m;
222 const char *name;
223 char txt[256];
224 assert(c && t);
225
226 if (!(name = pa_tokenizer_get(t, 1))) {
227 pa_strbuf_puts(buf, "You need to specify the module name and optionally arguments.\n");
228 return -1;
229 }
230
231 if (!(m = pa_module_load(c, name, pa_tokenizer_get(t, 2)))) {
232 pa_strbuf_puts(buf, "Module load failed.\n");
233 return -1;
234 }
235
236 if (*verbose) {
237 snprintf(txt, sizeof(txt), "Module successfully loaded, index: %u.\n", m->index);
238 pa_strbuf_puts(buf, txt);
239 }
240 return 0;
241 }
242
243 static int pa_cli_command_unload(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
244 struct pa_module *m;
245 uint32_t index;
246 const char *i;
247 char *e;
248 assert(c && t);
249
250 if (!(i = pa_tokenizer_get(t, 1))) {
251 pa_strbuf_puts(buf, "You need to specify the module index.\n");
252 return -1;
253 }
254
255 index = (uint32_t) strtoul(i, &e, 10);
256 if (*e || !(m = pa_idxset_get_by_index(c->modules, index))) {
257 pa_strbuf_puts(buf, "Invalid module index.\n");
258 return -1;
259 }
260
261 pa_module_unload_request(c, m);
262 return 0;
263 }
264
265 static int pa_cli_command_sink_volume(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
266 const char *n, *v;
267 char *x = NULL;
268 struct pa_sink *sink;
269 long volume;
270
271 if (!(n = pa_tokenizer_get(t, 1))) {
272 pa_strbuf_puts(buf, "You need to specify a sink either by its name or its index.\n");
273 return -1;
274 }
275
276 if (!(v = pa_tokenizer_get(t, 2))) {
277 pa_strbuf_puts(buf, "You need to specify a volume >= 0. (0 is muted, 0x100 is normal volume)\n");
278 return -1;
279 }
280
281 volume = strtol(v, &x, 0);
282 if (!x || *x != 0 || volume < 0) {
283 pa_strbuf_puts(buf, "Failed to parse volume.\n");
284 return -1;
285 }
286
287 if (!(sink = pa_namereg_get(c, n, PA_NAMEREG_SINK))) {
288 pa_strbuf_puts(buf, "No sink found by this name or index.\n");
289 return -1;
290 }
291
292 sink->volume = (uint32_t) volume;
293 return 0;
294 }
295
296 static int pa_cli_command_sink_input_volume(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
297 const char *n, *v;
298 struct pa_sink_input *si;
299 long volume;
300 uint32_t index;
301 char *x;
302
303 if (!(n = pa_tokenizer_get(t, 1))) {
304 pa_strbuf_puts(buf, "You need to specify a sink input by its index.\n");
305 return -1;
306 }
307
308 if ((index = parse_index(n)) == PA_IDXSET_INVALID) {
309 pa_strbuf_puts(buf, "Failed to parse index.\n");
310 return -1;
311 }
312
313 if (!(v = pa_tokenizer_get(t, 2))) {
314 pa_strbuf_puts(buf, "You need to specify a volume >= 0. (0 is muted, 0x100 is normal volume)\n");
315 return -1;
316 }
317
318 x = NULL;
319 volume = strtol(v, &x, 0);
320 if (!x || *x != 0 || volume < 0) {
321 pa_strbuf_puts(buf, "Failed to parse volume.\n");
322 return -1;
323 }
324
325 if (!(si = pa_idxset_get_by_index(c->sink_inputs, (uint32_t) index))) {
326 pa_strbuf_puts(buf, "No sink input found with this index.\n");
327 return -1;
328 }
329
330 si->volume = (uint32_t) volume;
331 return 0;
332 }
333
334 static int pa_cli_command_sink_default(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
335 const char *n;
336 struct pa_sink *sink;
337 assert(c && t);
338
339 if (!(n = pa_tokenizer_get(t, 1))) {
340 pa_strbuf_puts(buf, "You need to specify a sink either by its name or its index.\n");
341 return -1;
342 }
343
344 if (!(sink = pa_namereg_get(c, n, PA_NAMEREG_SINK))) {
345 pa_strbuf_puts(buf, "No sink found by this name or index.\n");
346 return -1;
347 }
348
349 c->default_sink_index = sink->index;
350 return 0;
351 }
352
353 static int pa_cli_command_source_default(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
354 const char *n;
355 struct pa_source *source;
356 assert(c && t);
357
358 if (!(n = pa_tokenizer_get(t, 1))) {
359 pa_strbuf_puts(buf, "You need to specify a source either by its name or its index.\n");
360 return -1;
361 }
362
363 if (!(source = pa_namereg_get(c, n, PA_NAMEREG_SOURCE))) {
364 pa_strbuf_puts(buf, "No source found by this name or index.\n");
365 return -1;
366 }
367
368 c->default_source_index = source->index;
369 return 0;
370 }
371
372 static int pa_cli_command_kill_client(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
373 const char *n;
374 struct pa_client *client;
375 uint32_t index;
376 assert(c && t);
377
378 if (!(n = pa_tokenizer_get(t, 1))) {
379 pa_strbuf_puts(buf, "You need to specify a client by its index.\n");
380 return -1;
381 }
382
383 if ((index = parse_index(n)) == PA_IDXSET_INVALID) {
384 pa_strbuf_puts(buf, "Failed to parse index.\n");
385 return -1;
386 }
387
388 if (!(client = pa_idxset_get_by_index(c->clients, index))) {
389 pa_strbuf_puts(buf, "No client found by this index.\n");
390 return -1;
391 }
392
393 pa_client_kill(client);
394 return 0;
395 }
396
397 static int pa_cli_command_kill_sink_input(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
398 const char *n;
399 struct pa_sink_input *sink_input;
400 uint32_t index;
401 assert(c && t);
402
403 if (!(n = pa_tokenizer_get(t, 1))) {
404 pa_strbuf_puts(buf, "You need to specify a sink input by its index.\n");
405 return -1;
406 }
407
408 if ((index = parse_index(n)) == PA_IDXSET_INVALID) {
409 pa_strbuf_puts(buf, "Failed to parse index.\n");
410 return -1;
411 }
412
413 if (!(sink_input = pa_idxset_get_by_index(c->sink_inputs, index))) {
414 pa_strbuf_puts(buf, "No sink input found by this index.\n");
415 return -1;
416 }
417
418 pa_sink_input_kill(sink_input);
419 return 0;
420 }
421
422 static int pa_cli_command_kill_source_output(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
423 const char *n;
424 struct pa_source_output *source_output;
425 uint32_t index;
426 assert(c && t);
427
428 if (!(n = pa_tokenizer_get(t, 1))) {
429 pa_strbuf_puts(buf, "You need to specify a source output by its index.\n");
430 return -1;
431 }
432
433 if ((index = parse_index(n)) == PA_IDXSET_INVALID) {
434 pa_strbuf_puts(buf, "Failed to parse index.\n");
435 return -1;
436 }
437
438 if (!(source_output = pa_idxset_get_by_index(c->source_outputs, index))) {
439 pa_strbuf_puts(buf, "No source output found by this index.\n");
440 return -1;
441 }
442
443 pa_source_output_kill(source_output);
444 return 0;
445 }
446
447 static int pa_cli_command_scache_list(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
448 char *s;
449 assert(c && t);
450 s = pa_scache_list_to_string(c);
451 assert(s);
452 pa_strbuf_puts(buf, s);
453 free(s);
454 return 0;
455 }
456
457 static int pa_cli_command_scache_play(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
458 const char *n, *sink_name;
459 struct pa_sink *sink;
460 assert(c && t && buf && fail && verbose);
461
462 if (!(n = pa_tokenizer_get(t, 1)) || !(sink_name = pa_tokenizer_get(t, 2))) {
463 pa_strbuf_puts(buf, "You need to specify a sample name and a sink name.\n");
464 return -1;
465 }
466
467 if (!(sink = pa_namereg_get(c, sink_name, PA_NAMEREG_SINK))) {
468 pa_strbuf_puts(buf, "No sink by that name.\n");
469 return -1;
470 }
471
472 if (pa_scache_play_item(c, n, sink, PA_VOLUME_NORM) < 0) {
473 pa_strbuf_puts(buf, "Failed to play sample.\n");
474 return -1;
475 }
476
477 return 0;
478 }
479
480 static int pa_cli_command_scache_remove(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
481 const char *n;
482 assert(c && t && buf && fail && verbose);
483
484 if (!(n = pa_tokenizer_get(t, 1))) {
485 pa_strbuf_puts(buf, "You need to specify a sample name.\n");
486 return -1;
487 }
488
489 if (pa_scache_remove_item(c, n) < 0) {
490 pa_strbuf_puts(buf, "Failed to remove sample.\n");
491 return -1;
492 }
493
494 return 0;
495 }
496
497 static int pa_cli_command_scache_load(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
498 const char *fname, *n;
499 struct pa_memchunk chunk;
500 struct pa_sample_spec ss;
501 assert(c && t && buf && fail && verbose);
502
503 if (!(fname = pa_tokenizer_get(t, 1)) || !(n = pa_tokenizer_get(t, 2))) {
504 pa_strbuf_puts(buf, "You need to specify a file name and a sample name.\n");
505 return -1;
506 }
507
508 if (pa_sound_file_load(fname, &ss, &chunk) < 0) {
509 pa_strbuf_puts(buf, "Failed to load sound file.\n");
510 return -1;
511 }
512
513 pa_scache_add_item(c, n, &ss, &chunk, NULL);
514 pa_memblock_unref(chunk.memblock);
515 return 0;
516 }
517
518 static int pa_cli_command_play_file(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
519 const char *fname, *sink_name;
520 struct pa_memchunk chunk;
521 struct pa_sample_spec ss;
522 struct pa_sink *sink;
523 int ret;
524 assert(c && t && buf && fail && verbose);
525
526 if (!(fname = pa_tokenizer_get(t, 1)) || !(sink_name = pa_tokenizer_get(t, 2))) {
527 pa_strbuf_puts(buf, "You need to specify a file name and a sink name.\n");
528 return -1;
529 }
530
531 if (!(sink = pa_namereg_get(c, sink_name, PA_NAMEREG_SINK))) {
532 pa_strbuf_puts(buf, "No sink by that name.\n");
533 return -1;
534 }
535
536 if (pa_sound_file_load(fname, &ss, &chunk) < 0) {
537 pa_strbuf_puts(buf, "Failed to load sound file.\n");
538 return -1;
539 }
540
541 ret = pa_play_memchunk(sink, fname, &ss, &chunk, PA_VOLUME_NORM);
542 pa_memblock_unref(chunk.memblock);
543 return ret;
544 }
545
546 int pa_cli_command_execute_line(struct pa_core *c, const char *s, struct pa_strbuf *buf, int *fail, int *verbose) {
547 const char *cs;
548
549 cs = s+strspn(s, whitespace);
550
551 if (*cs == '#' || !*cs)
552 return 0;
553 else if (*cs == '.') {
554 static const char fail_meta[] = ".fail";
555 static const char nofail_meta[] = ".nofail";
556 static const char verbose_meta[] = ".verbose";
557 static const char noverbose_meta[] = ".noverbose";
558
559 if (!strcmp(cs, verbose_meta))
560 *verbose = 1;
561 else if (!strcmp(cs, noverbose_meta))
562 *verbose = 0;
563 else if (!strcmp(cs, fail_meta))
564 *fail = 1;
565 else if (!strcmp(cs, nofail_meta))
566 *fail = 0;
567 else {
568 size_t l;
569 static const char include_meta[] = ".include";
570 l = strcspn(cs, whitespace);
571
572 if (l == sizeof(include_meta)-1 && !strncmp(cs, include_meta, l)) {
573 const char *filename = cs+l+strspn(cs+l, whitespace);
574
575 if (pa_cli_command_execute_file(c, filename, buf, fail, verbose) < 0)
576 if (*fail) return -1;
577 } else {
578 pa_strbuf_printf(buf, "Invalid meta command: %s\n", cs);
579 if (*fail) return -1;
580 }
581 }
582 } else {
583 const struct command*command;
584 int unknown = 1;
585 size_t l;
586
587 l = strcspn(cs, whitespace);
588
589 for (command = commands; command->name; command++)
590 if (strlen(command->name) == l && !strncmp(cs, command->name, l)) {
591 int ret;
592 struct pa_tokenizer *t = pa_tokenizer_new(cs, command->args);
593 assert(t);
594 ret = command->proc(c, t, buf, fail, verbose);
595 pa_tokenizer_free(t);
596 unknown = 0;
597
598 if (ret < 0 && *fail)
599 return -1;
600
601 break;
602 }
603
604 if (unknown) {
605 pa_strbuf_printf(buf, "Unknown command: %s\n", cs);
606 if (*fail)
607 return -1;
608 }
609 }
610
611 return 0;
612 }
613
614 int pa_cli_command_execute_file(struct pa_core *c, const char *fn, struct pa_strbuf *buf, int *fail, int *verbose) {
615 char line[256];
616 FILE *f = NULL;
617 int ret = -1;
618 assert(c && fn && buf);
619
620 if (!(f = fopen(fn, "r"))) {
621 pa_strbuf_printf(buf, "open('%s') failed: %s\n", fn, strerror(errno));
622 if (!*fail)
623 ret = 0;
624 goto fail;
625 }
626
627 if (*verbose)
628 pa_strbuf_printf(buf, "Executing file: '%s'\n", fn);
629
630 while (fgets(line, sizeof(line), f)) {
631 char *e = line + strcspn(line, linebreak);
632 *e = 0;
633
634 if (pa_cli_command_execute_line(c, line, buf, fail, verbose) < 0 && *fail)
635 goto fail;
636 }
637
638 if (*verbose)
639 pa_strbuf_printf(buf, "Executed file: '%s'\n", fn);
640
641 ret = 0;
642
643 fail:
644 if (f)
645 fclose(f);
646
647 return ret;
648 }
649
650 int pa_cli_command_execute(struct pa_core *c, const char *s, struct pa_strbuf *buf, int *fail, int *verbose) {
651 const char *p;
652 assert(c && s && buf && fail && verbose);
653
654 p = s;
655 while (*p) {
656 size_t l = strcspn(p, linebreak);
657 char *line = strndup(p, l);
658 assert(line);
659
660 if (pa_cli_command_execute_line(c, line, buf, fail, verbose) < 0&& *fail) {
661 free(line);
662 return -1;
663 }
664 free(line);
665
666 p += l;
667 p += strspn(p, linebreak);
668 }
669
670 return 0;
671 }