5556bcb346137819b6cac416c0da1b4accae81be
[profile/ivi/pulseaudio.git] / src / polypcore / 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 Lesser 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 Lesser 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 <polyp/xmalloc.h>
33
34 #include <polypcore/module.h>
35 #include <polypcore/sink.h>
36 #include <polypcore/source.h>
37 #include <polypcore/client.h>
38 #include <polypcore/sink-input.h>
39 #include <polypcore/source-output.h>
40 #include <polypcore/tokenizer.h>
41 #include <polypcore/strbuf.h>
42 #include <polypcore/namereg.h>
43 #include <polypcore/cli-text.h>
44 #include <polypcore/core-scache.h>
45 #include <polypcore/sample-util.h>
46 #include <polypcore/sound-file.h>
47 #include <polypcore/play-memchunk.h>
48 #include <polypcore/autoload.h>
49 #include <polypcore/sound-file-stream.h>
50 #include <polypcore/props.h>
51 #include <polypcore/util.h>
52
53 #include "cli-command.h"
54
55 struct command {
56     const char *name;
57     int (*proc) (pa_core *c, pa_tokenizer*t, pa_strbuf *buf, int *fail);
58     const char *help;
59     unsigned args;
60 };
61
62 #define INCLUDE_META ".include"
63 #define FAIL_META ".fail"
64 #define NOFAIL_META ".nofail"
65
66 /* Prototypes for all available commands */
67 static int pa_cli_command_exit(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
68 static int pa_cli_command_help(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
69 static int pa_cli_command_modules(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
70 static int pa_cli_command_clients(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
71 static int pa_cli_command_sinks(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
72 static int pa_cli_command_sources(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
73 static int pa_cli_command_sink_inputs(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
74 static int pa_cli_command_source_outputs(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
75 static int pa_cli_command_stat(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
76 static int pa_cli_command_info(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
77 static int pa_cli_command_load(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
78 static int pa_cli_command_unload(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
79 static int pa_cli_command_sink_volume(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
80 static int pa_cli_command_sink_input_volume(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
81 static int pa_cli_command_source_volume(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
82 static int pa_cli_command_sink_mute(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
83 static int pa_cli_command_source_mute(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
84 static int pa_cli_command_sink_default(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
85 static int pa_cli_command_source_default(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
86 static int pa_cli_command_kill_client(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
87 static int pa_cli_command_kill_sink_input(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
88 static int pa_cli_command_kill_source_output(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
89 static int pa_cli_command_scache_play(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
90 static int pa_cli_command_scache_remove(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
91 static int pa_cli_command_scache_list(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
92 static int pa_cli_command_scache_load(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
93 static int pa_cli_command_scache_load_dir(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
94 static int pa_cli_command_play_file(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
95 static int pa_cli_command_autoload_list(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
96 static int pa_cli_command_autoload_add(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
97 static int pa_cli_command_autoload_remove(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
98 static int pa_cli_command_dump(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
99 static int pa_cli_command_list_props(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
100
101
102 /* A method table for all available commands */
103
104 static const struct command commands[] = {
105     { "exit",                    pa_cli_command_exit,               "Terminate the daemon",         1 },
106     { "help",                    pa_cli_command_help,               "Show this help",               1 },
107     { "list-modules",            pa_cli_command_modules,            "List loaded modules",          1 },
108     { "list-sinks",              pa_cli_command_sinks,              "List loaded sinks",            1 },
109     { "list-sources",            pa_cli_command_sources,            "List loaded sources",          1 },
110     { "list-clients",            pa_cli_command_clients,            "List loaded clients",          1 },
111     { "list-sink-inputs",        pa_cli_command_sink_inputs,        "List sink inputs",             1 },
112     { "list-source-outputs",     pa_cli_command_source_outputs,     "List source outputs",          1 },
113     { "stat",                    pa_cli_command_stat,               "Show memory block statistics", 1 },
114     { "info",                    pa_cli_command_info,               "Show comprehensive status",    1 },
115     { "ls",                      pa_cli_command_info,               NULL,                           1 },
116     { "list",                    pa_cli_command_info,               NULL,                           1 },
117     { "load-module",             pa_cli_command_load,               "Load a module (args: name, arguments)",                     3},
118     { "unload-module",           pa_cli_command_unload,             "Unload a module (args: index)",                             2},
119     { "set-sink-volume",         pa_cli_command_sink_volume,        "Set the volume of a sink (args: index|name, volume)",             3},
120     { "set-sink-input-volume",   pa_cli_command_sink_input_volume,  "Set the volume of a sink input (args: index|name, volume)", 3},
121     { "set-source-volume",       pa_cli_command_source_volume,      "Set the volume of a source (args: index|name, volume)", 3},
122     { "set-sink-mute",           pa_cli_command_sink_mute,          "Set the mute switch of a sink (args: index|name, mute)", 3},
123     { "set-source-mute",         pa_cli_command_source_mute,        "Set the mute switch of a source (args: index|name, mute)", 3},
124     { "set-default-sink",        pa_cli_command_sink_default,       "Set the default sink (args: index|name)", 2},
125     { "set-default-source",      pa_cli_command_source_default,     "Set the default source (args: index|name)", 2},
126     { "kill-client",             pa_cli_command_kill_client,        "Kill a client (args: index)", 2},
127     { "kill-sink-input",         pa_cli_command_kill_sink_input,    "Kill a sink input (args: index)", 2},
128     { "kill-source-output",      pa_cli_command_kill_source_output, "Kill a source output (args: index)", 2},
129     { "list-samples",            pa_cli_command_scache_list,        "List all entries in the sample cache", 1},
130     { "play-sample",             pa_cli_command_scache_play,        "Play a sample from the sample cache (args: name, sink|index)", 3},
131     { "remove-sample",           pa_cli_command_scache_remove,      "Remove a sample from the sample cache (args: name)", 2},
132     { "load-sample",             pa_cli_command_scache_load,        "Load a sound file into the sample cache (args: name, filename)", 3},
133     { "load-sample-lazy",        pa_cli_command_scache_load,        "Lazily load a sound file into the sample cache (args: name, filename)", 3},
134     { "load-sample-dir-lazy",    pa_cli_command_scache_load_dir,    "Lazily load all files in a directory into the sample cache (args: pathname)", 2},
135     { "play-file",               pa_cli_command_play_file,          "Play a sound file (args: filename, sink|index)", 3},
136     { "list-autoload",           pa_cli_command_autoload_list,      "List autoload entries", 1},
137     { "add-autoload-sink",       pa_cli_command_autoload_add,       "Add autoload entry for a sink (args: sink, module name, arguments)", 4},
138     { "add-autoload-source",     pa_cli_command_autoload_add,       "Add autoload entry for a source (args: source, module name, arguments)", 4},
139     { "remove-autoload-sink",    pa_cli_command_autoload_remove,    "Remove autoload entry for a sink (args: name)", 2},
140     { "remove-autoload-source",  pa_cli_command_autoload_remove,    "Remove autoload entry for a source (args: name)", 2},
141     { "dump",                    pa_cli_command_dump,               "Dump daemon configuration", 1},
142     { "list-props",              pa_cli_command_list_props,         NULL, 1},
143     { NULL, NULL, NULL, 0 }
144 };
145
146 static const char whitespace[] = " \t\n\r";
147 static const char linebreak[] = "\n\r";
148
149 static uint32_t parse_index(const char *n) {
150     uint32_t idx;
151
152     if (pa_atou(n, &idx) < 0)
153         return (uint32_t) PA_IDXSET_INVALID;
154
155     return idx;
156 }
157
158 static int pa_cli_command_exit(pa_core *c, pa_tokenizer *t, PA_GCC_UNUSED pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
159     assert(c && c->mainloop && t);
160     c->mainloop->quit(c->mainloop, 0);
161     return 0;
162 }
163
164 static int pa_cli_command_help(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
165     const struct command*command;
166     assert(c && t && buf);
167
168     pa_strbuf_puts(buf, "Available commands:\n");
169     
170     for (command = commands; command->name; command++)
171         if (command->help)
172             pa_strbuf_printf(buf, "    %-25s %s\n", command->name, command->help);
173     return 0;
174 }
175
176 static int pa_cli_command_modules(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
177     char *s;
178     assert(c && t);
179     s = pa_module_list_to_string(c);
180     assert(s);
181     pa_strbuf_puts(buf, s);
182     pa_xfree(s);
183     return 0;
184 }
185
186 static int pa_cli_command_clients(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
187     char *s;
188     assert(c && t);
189     s = pa_client_list_to_string(c);
190     assert(s);
191     pa_strbuf_puts(buf, s);
192     pa_xfree(s);
193     return 0;
194 }
195
196 static int pa_cli_command_sinks(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
197     char *s;
198     assert(c && t);
199     s = pa_sink_list_to_string(c);
200     assert(s);
201     pa_strbuf_puts(buf, s);
202     pa_xfree(s);
203     return 0;
204 }
205
206 static int pa_cli_command_sources(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
207     char *s;
208     assert(c && t);
209     s = pa_source_list_to_string(c);
210     assert(s);
211     pa_strbuf_puts(buf, s);
212     pa_xfree(s);
213     return 0;
214 }
215
216 static int pa_cli_command_sink_inputs(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
217     char *s;
218     assert(c && t);
219     s = pa_sink_input_list_to_string(c);
220     assert(s);
221     pa_strbuf_puts(buf, s);
222     pa_xfree(s);
223     return 0;
224 }
225
226 static int pa_cli_command_source_outputs(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
227     char *s;
228     assert(c && t);
229     s = pa_source_output_list_to_string(c);
230     assert(s);
231     pa_strbuf_puts(buf, s);
232     pa_xfree(s);
233     return 0;
234 }
235
236 static int pa_cli_command_stat(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
237     char s[256];
238     assert(c && t);
239
240     pa_bytes_snprint(s, sizeof(s), c->memblock_stat->total_size);
241     pa_strbuf_printf(buf, "Memory blocks currently allocated: %u, size: %s.\n",
242                      c->memblock_stat->total,
243                      s);
244
245     pa_bytes_snprint(s, sizeof(s), c->memblock_stat->allocated_size);
246     pa_strbuf_printf(buf, "Memory blocks allocated during the whole lifetime: %u, size: %s.\n",
247                      c->memblock_stat->allocated,
248                      s);
249
250     pa_bytes_snprint(s, sizeof(s), pa_scache_total_size(c));
251     pa_strbuf_printf(buf, "Total sample cache size: %s.\n", s);
252
253     pa_sample_spec_snprint(s, sizeof(s), &c->default_sample_spec);
254     pa_strbuf_printf(buf, "Default sample spec: %s\n", s);
255
256     pa_strbuf_printf(buf, "Default sink name: %s\n"
257                      "Default source name: %s\n",
258                      pa_namereg_get_default_sink_name(c),
259                      pa_namereg_get_default_source_name(c));
260
261     return 0;
262 }
263
264 static int pa_cli_command_info(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail) {
265     assert(c && t);
266     pa_cli_command_stat(c, t, buf, fail);
267     pa_cli_command_modules(c, t, buf, fail);
268     pa_cli_command_sinks(c, t, buf, fail);
269     pa_cli_command_sources(c, t, buf, fail);
270     pa_cli_command_clients(c, t, buf, fail);
271     pa_cli_command_sink_inputs(c, t, buf, fail);
272     pa_cli_command_source_outputs(c, t, buf, fail);
273     pa_cli_command_scache_list(c, t, buf, fail);
274     pa_cli_command_autoload_list(c, t, buf, fail);
275     return 0;
276 }
277
278 static int pa_cli_command_load(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
279     pa_module *m;
280     const char *name;
281     assert(c && t);
282
283     if (!(name = pa_tokenizer_get(t, 1))) {
284         pa_strbuf_puts(buf, "You need to specify the module name and optionally arguments.\n");
285         return -1;
286     }
287     
288     if (!(m = pa_module_load(c, name,  pa_tokenizer_get(t, 2)))) {
289         pa_strbuf_puts(buf, "Module load failed.\n");
290         return -1;
291     }
292
293     return 0;
294 }
295
296 static int pa_cli_command_unload(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
297     pa_module *m;
298     uint32_t idx;
299     const char *i;
300     char *e;
301     assert(c && t);
302
303     if (!(i = pa_tokenizer_get(t, 1))) {
304         pa_strbuf_puts(buf, "You need to specify the module index.\n");
305         return -1;
306     }
307
308     idx = (uint32_t) strtoul(i, &e, 10);
309     if (*e || !(m = pa_idxset_get_by_index(c->modules, idx))) {
310         pa_strbuf_puts(buf, "Invalid module index.\n");
311         return -1;
312     }
313
314     pa_module_unload_request(m);
315     return 0;
316 }
317
318 static int pa_cli_command_sink_volume(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
319     const char *n, *v;
320     pa_sink *sink;
321     uint32_t volume;
322     pa_cvolume cvolume;
323
324     if (!(n = pa_tokenizer_get(t, 1))) {
325         pa_strbuf_puts(buf, "You need to specify a sink either by its name or its index.\n");
326         return -1;
327     }
328
329     if (!(v = pa_tokenizer_get(t, 2))) {
330         pa_strbuf_puts(buf, "You need to specify a volume >= 0. (0 is muted, 0x100 is normal volume)\n");
331         return -1;
332     }
333
334     if (pa_atou(v, &volume) < 0) {
335         pa_strbuf_puts(buf, "Failed to parse volume.\n");
336         return -1;
337     }
338
339     if (!(sink = pa_namereg_get(c, n, PA_NAMEREG_SINK, 1))) {
340         pa_strbuf_puts(buf, "No sink found by this name or index.\n");
341         return -1;
342     }
343
344     pa_cvolume_set(&cvolume, sink->sample_spec.channels, volume);
345     pa_sink_set_volume(sink, PA_MIXER_HARDWARE, &cvolume);
346     return 0;
347 }
348
349 static int pa_cli_command_sink_input_volume(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
350     const char *n, *v;
351     pa_sink_input *si;
352     pa_volume_t volume;
353     pa_cvolume cvolume;
354     uint32_t idx;
355
356     if (!(n = pa_tokenizer_get(t, 1))) {
357         pa_strbuf_puts(buf, "You need to specify a sink input by its index.\n");
358         return -1;
359     }
360
361     if ((idx = parse_index(n)) == PA_IDXSET_INVALID) {
362         pa_strbuf_puts(buf, "Failed to parse index.\n");
363         return -1;
364     }
365
366     if (!(v = pa_tokenizer_get(t, 2))) {
367         pa_strbuf_puts(buf, "You need to specify a volume >= 0. (0 is muted, 0x100 is normal volume)\n");
368         return -1;
369     }
370
371     if (pa_atou(v, &volume) < 0) {
372         pa_strbuf_puts(buf, "Failed to parse volume.\n");
373         return -1;
374     }
375
376     if (!(si = pa_idxset_get_by_index(c->sink_inputs, (uint32_t) idx))) {
377         pa_strbuf_puts(buf, "No sink input found with this index.\n");
378         return -1;
379     }
380
381     pa_cvolume_set(&cvolume, si->sample_spec.channels, volume);
382     pa_sink_input_set_volume(si, &cvolume);
383     return 0;
384 }
385
386 static int pa_cli_command_source_volume(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
387     const char *n, *v;
388     pa_source *source;
389     uint32_t volume;
390     pa_cvolume cvolume;
391
392     if (!(n = pa_tokenizer_get(t, 1))) {
393         pa_strbuf_puts(buf, "You need to specify a source either by its name or its index.\n");
394         return -1;
395     }
396
397     if (!(v = pa_tokenizer_get(t, 2))) {
398         pa_strbuf_puts(buf, "You need to specify a volume >= 0. (0 is muted, 0x100 is normal volume)\n");
399         return -1;
400     }
401
402     if (pa_atou(v, &volume) < 0) {
403         pa_strbuf_puts(buf, "Failed to parse volume.\n");
404         return -1;
405     }
406
407     if (!(source = pa_namereg_get(c, n, PA_NAMEREG_SOURCE, 1))) {
408         pa_strbuf_puts(buf, "No source found by this name or index.\n");
409         return -1;
410     }
411
412     pa_cvolume_set(&cvolume, source->sample_spec.channels, volume);
413     pa_source_set_volume(source, PA_MIXER_HARDWARE, &cvolume);
414     return 0;
415 }
416
417 static int pa_cli_command_sink_mute(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
418     const char *n, *m;
419     pa_sink *sink;
420     int mute;
421
422     if (!(n = pa_tokenizer_get(t, 1))) {
423         pa_strbuf_puts(buf, "You need to specify a sink either by its name or its index.\n");
424         return -1;
425     }
426
427     if (!(m = pa_tokenizer_get(t, 2))) {
428         pa_strbuf_puts(buf, "You need to specify a mute switch setting (0/1).\n");
429         return -1;
430     }
431
432     if (pa_atoi(m, &mute) < 0) {
433         pa_strbuf_puts(buf, "Failed to parse mute switch.\n");
434         return -1;
435     }
436
437     if (!(sink = pa_namereg_get(c, n, PA_NAMEREG_SINK, 1))) {
438         pa_strbuf_puts(buf, "No sink found by this name or index.\n");
439         return -1;
440     }
441
442     pa_sink_set_mute(sink, PA_MIXER_HARDWARE, mute);
443     return 0;
444 }
445
446 static int pa_cli_command_source_mute(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
447     const char *n, *m;
448     pa_source *source;
449     int mute;
450
451     if (!(n = pa_tokenizer_get(t, 1))) {
452         pa_strbuf_puts(buf, "You need to specify a source either by its name or its index.\n");
453         return -1;
454     }
455
456     if (!(m = pa_tokenizer_get(t, 2))) {
457         pa_strbuf_puts(buf, "You need to specify a mute switch setting (0/1).\n");
458         return -1;
459     }
460
461     if (pa_atoi(m, &mute) < 0) {
462         pa_strbuf_puts(buf, "Failed to parse mute switch.\n");
463         return -1;
464     }
465
466     if (!(source = pa_namereg_get(c, n, PA_NAMEREG_SOURCE, 1))) {
467         pa_strbuf_puts(buf, "No sink found by this name or index.\n");
468         return -1;
469     }
470
471     pa_source_set_mute(source, PA_MIXER_HARDWARE, mute);
472     return 0;
473 }
474
475 static int pa_cli_command_sink_default(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
476     const char *n;
477     assert(c && t);
478
479     if (!(n = pa_tokenizer_get(t, 1))) {
480         pa_strbuf_puts(buf, "You need to specify a sink either by its name or its index.\n");
481         return -1;
482     }
483
484     pa_namereg_set_default(c, n, PA_NAMEREG_SINK);
485     return 0;
486 }
487
488 static int pa_cli_command_source_default(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
489     const char *n;
490     assert(c && t);
491
492     if (!(n = pa_tokenizer_get(t, 1))) {
493         pa_strbuf_puts(buf, "You need to specify a source either by its name or its index.\n");
494         return -1;
495     }
496
497     pa_namereg_set_default(c, n, PA_NAMEREG_SOURCE);
498     return 0;
499 }
500
501 static int pa_cli_command_kill_client(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
502     const char *n;
503     pa_client *client;
504     uint32_t idx;
505     assert(c && t);
506
507     if (!(n = pa_tokenizer_get(t, 1))) {
508         pa_strbuf_puts(buf, "You need to specify a client by its index.\n");
509         return -1;
510     }
511
512     if ((idx = parse_index(n)) == PA_IDXSET_INVALID) {
513         pa_strbuf_puts(buf, "Failed to parse index.\n");
514         return -1;
515     }
516
517     if (!(client = pa_idxset_get_by_index(c->clients, idx))) {
518         pa_strbuf_puts(buf, "No client found by this index.\n");
519         return -1;
520     }
521
522     pa_client_kill(client);
523     return 0;
524 }
525
526 static int pa_cli_command_kill_sink_input(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
527     const char *n;
528     pa_sink_input *sink_input;
529     uint32_t idx;
530     assert(c && t);
531
532     if (!(n = pa_tokenizer_get(t, 1))) {
533         pa_strbuf_puts(buf, "You need to specify a sink input by its index.\n");
534         return -1;
535     }
536
537     if ((idx = parse_index(n)) == PA_IDXSET_INVALID) {
538         pa_strbuf_puts(buf, "Failed to parse index.\n");
539         return -1;
540     }
541
542     if (!(sink_input = pa_idxset_get_by_index(c->sink_inputs, idx))) {
543         pa_strbuf_puts(buf, "No sink input found by this index.\n");
544         return -1;
545     }
546
547     pa_sink_input_kill(sink_input);
548     return 0;
549 }
550
551 static int pa_cli_command_kill_source_output(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
552     const char *n;
553     pa_source_output *source_output;
554     uint32_t idx;
555     assert(c && t);
556
557     if (!(n = pa_tokenizer_get(t, 1))) {
558         pa_strbuf_puts(buf, "You need to specify a source output by its index.\n");
559         return -1;
560     }
561
562     if ((idx = parse_index(n)) == PA_IDXSET_INVALID) {
563         pa_strbuf_puts(buf, "Failed to parse index.\n");
564         return -1;
565     }
566
567     if (!(source_output = pa_idxset_get_by_index(c->source_outputs, idx))) {
568         pa_strbuf_puts(buf, "No source output found by this index.\n");
569         return -1;
570     }
571
572     pa_source_output_kill(source_output);
573     return 0;
574 }
575
576 static int pa_cli_command_scache_list(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
577     char *s;
578     assert(c && t);
579     s = pa_scache_list_to_string(c);
580     assert(s);
581     pa_strbuf_puts(buf, s);
582     pa_xfree(s);
583     return 0;
584 }
585
586 static int pa_cli_command_scache_play(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail) {
587     const char *n, *sink_name;
588     pa_sink *sink;
589     assert(c && t && buf && fail);
590
591     if (!(n = pa_tokenizer_get(t, 1)) || !(sink_name = pa_tokenizer_get(t, 2))) {
592         pa_strbuf_puts(buf, "You need to specify a sample name and a sink name.\n");
593         return -1;
594     }
595
596     if (!(sink = pa_namereg_get(c, sink_name, PA_NAMEREG_SINK, 1))) {
597         pa_strbuf_puts(buf, "No sink by that name.\n");
598         return -1;
599     }
600
601     if (pa_scache_play_item(c, n, sink, PA_VOLUME_NORM) < 0) {
602         pa_strbuf_puts(buf, "Failed to play sample.\n");
603         return -1;
604     }
605
606     return 0;
607 }
608
609 static int pa_cli_command_scache_remove(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail) {
610     const char *n;
611     assert(c && t && buf && fail);
612
613     if (!(n = pa_tokenizer_get(t, 1))) {
614         pa_strbuf_puts(buf, "You need to specify a sample name.\n");
615         return -1;
616     }
617
618     if (pa_scache_remove_item(c, n) < 0) {
619         pa_strbuf_puts(buf, "Failed to remove sample.\n");
620         return -1;
621     }
622
623     return 0;
624 }
625
626 static int pa_cli_command_scache_load(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail) {
627     const char *fname, *n;
628     int r;
629     assert(c && t && buf && fail);
630
631     if (!(fname = pa_tokenizer_get(t, 2)) || !(n = pa_tokenizer_get(t, 1))) {
632         pa_strbuf_puts(buf, "You need to specify a file name and a sample name.\n");
633         return -1;
634     }
635
636     if (strstr(pa_tokenizer_get(t, 0), "lazy"))
637         r = pa_scache_add_file_lazy(c, n, fname, NULL);
638     else
639         r = pa_scache_add_file(c, n, fname, NULL);
640
641     if (r < 0)
642         pa_strbuf_puts(buf, "Failed to load sound file.\n");
643
644     return 0;
645 }
646
647 static int pa_cli_command_scache_load_dir(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail) {
648     const char *pname;
649     assert(c && t && buf && fail);
650
651     if (!(pname = pa_tokenizer_get(t, 1))) {
652         pa_strbuf_puts(buf, "You need to specify a path name.\n");
653         return -1;
654     }
655
656     if (pa_scache_add_directory_lazy(c, pname) < 0) {
657         pa_strbuf_puts(buf, "Failed to load directory.\n");
658         return -1;
659     }
660
661     return 0;
662 }
663
664 static int pa_cli_command_play_file(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail) {
665     const char *fname, *sink_name;
666     pa_sink *sink;
667     assert(c && t && buf && fail);
668
669     if (!(fname = pa_tokenizer_get(t, 1)) || !(sink_name = pa_tokenizer_get(t, 2))) {
670         pa_strbuf_puts(buf, "You need to specify a file name and a sink name.\n");
671         return -1;
672     }
673
674     if (!(sink = pa_namereg_get(c, sink_name, PA_NAMEREG_SINK, 1))) {
675         pa_strbuf_puts(buf, "No sink by that name.\n");
676         return -1;
677     }
678
679
680     return pa_play_file(sink, fname, NULL);
681 }
682
683 static int pa_cli_command_autoload_add(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail) {
684     const char *a, *b;
685     assert(c && t && buf && fail);
686
687     if (!(a = pa_tokenizer_get(t, 1)) || !(b = pa_tokenizer_get(t, 2))) {
688         pa_strbuf_puts(buf, "You need to specify a device name, a filename or a module name and optionally module arguments\n");
689         return -1;
690     }
691
692     pa_autoload_add(c, a, strstr(pa_tokenizer_get(t, 0), "sink") ? PA_NAMEREG_SINK : PA_NAMEREG_SOURCE, b, pa_tokenizer_get(t, 3), NULL);
693     
694     return 0;
695 }
696
697 static int pa_cli_command_autoload_remove(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail) {
698     const char *name;
699     assert(c && t && buf && fail);
700     
701     if (!(name = pa_tokenizer_get(t, 1))) {
702         pa_strbuf_puts(buf, "You need to specify a device name\n");
703         return -1;
704     }
705
706     if (pa_autoload_remove_by_name(c, name, strstr(pa_tokenizer_get(t, 0), "sink") ? PA_NAMEREG_SINK : PA_NAMEREG_SOURCE) < 0) {
707         pa_strbuf_puts(buf, "Failed to remove autload entry\n");
708         return -1;
709     }
710
711     return 0;        
712 }
713
714 static int pa_cli_command_autoload_list(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
715     char *s;
716     assert(c && t);
717     s = pa_autoload_list_to_string(c);
718     assert(s);
719     pa_strbuf_puts(buf, s);
720     pa_xfree(s);
721     return 0;
722 }
723
724 static int pa_cli_command_list_props(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
725     assert(c && t);
726     pa_property_dump(c, buf);
727     return 0;
728 }
729
730 static int pa_cli_command_dump(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
731     pa_module *m;
732     pa_sink *sink;
733     pa_source *source;
734     int nl;
735     const char *p;
736     uint32_t idx;
737     char txt[256];
738     time_t now;
739     void *i;
740     pa_autoload_entry *a;
741     
742     assert(c && t);
743
744     time(&now);
745
746 #ifdef HAVE_CTIME_R
747     pa_strbuf_printf(buf, "### Configuration dump generated at %s\n", ctime_r(&now, txt));
748 #else
749     pa_strbuf_printf(buf, "### Configuration dump generated at %s\n", ctime(&now));
750 #endif
751
752     
753     for (m = pa_idxset_first(c->modules, &idx); m; m = pa_idxset_next(c->modules, &idx)) {
754         if (m->auto_unload)
755             continue;
756
757         pa_strbuf_printf(buf, "load-module %s", m->name);
758
759         if (m->argument)
760             pa_strbuf_printf(buf, " %s", m->argument);
761
762         pa_strbuf_puts(buf, "\n");
763     }
764
765     nl = 0;
766
767     for (sink = pa_idxset_first(c->sinks, &idx); sink; sink = pa_idxset_next(c->sinks, &idx)) {
768         if (sink->owner && sink->owner->auto_unload)
769             continue;
770
771         if (!nl) {
772             pa_strbuf_puts(buf, "\n");
773             nl = 1;
774         }
775
776         pa_strbuf_printf(buf, "set-sink-volume %s 0x%03x\n", sink->name, pa_cvolume_avg(pa_sink_get_volume(sink, PA_MIXER_HARDWARE)));
777         pa_strbuf_printf(buf, "set-sink-mute %s %d\n", sink->name, pa_sink_get_mute(sink, PA_MIXER_HARDWARE));
778     }
779
780     for (source = pa_idxset_first(c->sources, &idx); source; source = pa_idxset_next(c->sources, &idx)) {
781         if (source->owner && source->owner->auto_unload)
782             continue;
783
784         if (!nl) {
785             pa_strbuf_puts(buf, "\n");
786             nl = 1;
787         }
788
789         pa_strbuf_printf(buf, "set-source-volume %s 0x%03x\n", source->name, pa_cvolume_avg(pa_source_get_volume(source, PA_MIXER_HARDWARE)));
790         pa_strbuf_printf(buf, "set-source-mute %s %d\n", source->name, pa_source_get_mute(source, PA_MIXER_HARDWARE));
791     }
792
793
794     if (c->autoload_hashmap) {
795         nl = 0;
796         
797         i = NULL;
798         while ((a = pa_hashmap_iterate(c->autoload_hashmap, &i, NULL))) {
799
800             if (!nl) {
801                 pa_strbuf_puts(buf, "\n");
802                 nl = 1;
803             }
804             
805             pa_strbuf_printf(buf, "add-autoload-%s %s %s", a->type == PA_NAMEREG_SINK ? "sink" : "source", a->name, a->module);
806             
807             if (a->argument)
808                 pa_strbuf_printf(buf, " %s", a->argument);
809             
810             pa_strbuf_puts(buf, "\n");
811         }
812     }
813
814     nl = 0;
815     
816     if ((p = pa_namereg_get_default_sink_name(c))) {
817         if (!nl) {
818             pa_strbuf_puts(buf, "\n");
819             nl = 1;
820         }
821         pa_strbuf_printf(buf, "set-default-sink %s\n", p);
822     }
823
824     if ((p = pa_namereg_get_default_source_name(c))) {
825         if (!nl) {
826             pa_strbuf_puts(buf, "\n");
827             nl = 1;
828         }
829         pa_strbuf_printf(buf, "set-default-source %s\n", p);
830     }
831
832     pa_strbuf_puts(buf, "\n### EOF\n");
833
834     return 0;
835 }
836
837
838 int pa_cli_command_execute_line(pa_core *c, const char *s, pa_strbuf *buf, int *fail) {
839     const char *cs;
840     
841     cs = s+strspn(s, whitespace);
842
843     if (*cs == '#' || !*cs)
844         return 0;
845     else if (*cs == '.') {
846         if (!strcmp(cs, FAIL_META))
847             *fail = 1;
848         else if (!strcmp(cs, NOFAIL_META))
849             *fail = 0;
850         else {
851             size_t l;
852             l = strcspn(cs, whitespace);
853
854             if (l == sizeof(INCLUDE_META)-1 && !strncmp(cs, INCLUDE_META, l)) {
855                 const char *filename = cs+l+strspn(cs+l, whitespace);
856
857                 if (pa_cli_command_execute_file(c, filename, buf, fail) < 0)
858                     if (*fail) return -1;
859             } else {
860                 pa_strbuf_printf(buf, "Invalid meta command: %s\n", cs);
861                 if (*fail) return -1;
862             }
863         }
864     } else {
865         const struct command*command;
866         int unknown = 1;
867         size_t l;
868         
869         l = strcspn(cs, whitespace);
870
871         for (command = commands; command->name; command++) 
872             if (strlen(command->name) == l && !strncmp(cs, command->name, l)) {
873                 int ret;
874                 pa_tokenizer *t = pa_tokenizer_new(cs, command->args);
875                 assert(t);
876                 ret = command->proc(c, t, buf, fail);
877                 pa_tokenizer_free(t);
878                 unknown = 0;
879
880                 if (ret < 0 && *fail)
881                     return -1;
882                 
883                 break;
884             }
885
886         if (unknown) {
887             pa_strbuf_printf(buf, "Unknown command: %s\n", cs);
888             if (*fail)
889                 return -1;
890         }
891     }
892
893     return 0;
894 }
895
896 int pa_cli_command_execute_file(pa_core *c, const char *fn, pa_strbuf *buf, int *fail) {
897     char line[256];
898     FILE *f = NULL;
899     int ret = -1;
900     assert(c && fn && buf);
901
902     if (!(f = fopen(fn, "r"))) {
903         pa_strbuf_printf(buf, "open('%s') failed: %s\n", fn, strerror(errno));
904         if (!*fail)
905             ret = 0;
906         goto fail;
907     }
908
909     while (fgets(line, sizeof(line), f)) {
910         char *e = line + strcspn(line, linebreak);
911         *e = 0;
912
913         if (pa_cli_command_execute_line(c, line, buf, fail) < 0 && *fail)
914             goto fail;
915     }
916
917     ret = 0;
918
919 fail:
920     if (f)
921         fclose(f);
922
923     return ret;
924 }
925
926 int pa_cli_command_execute(pa_core *c, const char *s, pa_strbuf *buf, int *fail) {
927     const char *p;
928     assert(c && s && buf && fail);
929
930     p = s;
931     while (*p) {
932         size_t l = strcspn(p, linebreak);
933         char *line = pa_xstrndup(p, l);
934         
935         if (pa_cli_command_execute_line(c, line, buf, fail) < 0&& *fail) {
936             pa_xfree(line);
937             return -1;
938         }
939         pa_xfree(line);
940
941         p += l;
942         p += strspn(p, linebreak);
943     }
944
945     return 0;
946 }