6989069d365369ea9487a8e4f9eae5768db95df7
[profile/ivi/pulseaudio-panda.git] / src / pulsecore / cli-command.c
1 /* $Id$ */
2
3 /***
4   This file is part of PulseAudio.
5
6   Copyright 2004-2006 Lennart Poettering
7   Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
8
9   PulseAudio is free software; you can redistribute it and/or modify
10   it under the terms of the GNU Lesser General Public License as published
11   by the Free Software Foundation; either version 2 of the License,
12   or (at your option) any later version.
13
14   PulseAudio is distributed in the hope that it will be useful, but
15   WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   General Public License for more details.
18
19   You should have received a copy of the GNU Lesser General Public License
20   along with PulseAudio; if not, write to the Free Software
21   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22   USA.
23 ***/
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <stdio.h>
30 #include <string.h>
31 #include <assert.h>
32 #include <stdlib.h>
33 #include <errno.h>
34 #include <unistd.h>
35
36 #include <pulse/xmalloc.h>
37
38 #include <pulsecore/module.h>
39 #include <pulsecore/sink.h>
40 #include <pulsecore/source.h>
41 #include <pulsecore/client.h>
42 #include <pulsecore/sink-input.h>
43 #include <pulsecore/source-output.h>
44 #include <pulsecore/tokenizer.h>
45 #include <pulsecore/strbuf.h>
46 #include <pulsecore/namereg.h>
47 #include <pulsecore/cli-text.h>
48 #include <pulsecore/core-scache.h>
49 #include <pulsecore/sample-util.h>
50 #include <pulsecore/sound-file.h>
51 #include <pulsecore/play-memchunk.h>
52 #include <pulsecore/autoload.h>
53 #include <pulsecore/sound-file-stream.h>
54 #include <pulsecore/props.h>
55 #include <pulsecore/core-util.h>
56 #include <pulsecore/core-error.h>
57
58 #include "cli-command.h"
59
60 struct command {
61     const char *name;
62     int (*proc) (pa_core *c, pa_tokenizer*t, pa_strbuf *buf, int *fail);
63     const char *help;
64     unsigned args;
65 };
66
67 #define META_INCLUDE ".include"
68 #define META_FAIL ".fail"
69 #define META_NOFAIL ".nofail"
70 #define META_IFEXISTS ".ifexists"
71 #define META_ELSE ".else"
72 #define META_ENDIF ".endif"
73
74 enum {
75     IFSTATE_NONE = -1,
76     IFSTATE_FALSE = 0,
77     IFSTATE_TRUE = 1,
78 };
79
80 /* Prototypes for all available commands */
81 static int pa_cli_command_exit(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
82 static int pa_cli_command_help(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
83 static int pa_cli_command_modules(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
84 static int pa_cli_command_clients(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
85 static int pa_cli_command_sinks(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
86 static int pa_cli_command_sources(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
87 static int pa_cli_command_sink_inputs(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
88 static int pa_cli_command_source_outputs(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
89 static int pa_cli_command_stat(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
90 static int pa_cli_command_info(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
91 static int pa_cli_command_load(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
92 static int pa_cli_command_unload(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
93 static int pa_cli_command_sink_volume(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
94 static int pa_cli_command_sink_input_volume(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
95 static int pa_cli_command_source_volume(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
96 static int pa_cli_command_sink_mute(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
97 static int pa_cli_command_source_mute(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
98 static int pa_cli_command_sink_default(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
99 static int pa_cli_command_source_default(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
100 static int pa_cli_command_kill_client(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
101 static int pa_cli_command_kill_sink_input(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
102 static int pa_cli_command_kill_source_output(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
103 static int pa_cli_command_scache_play(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
104 static int pa_cli_command_scache_remove(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
105 static int pa_cli_command_scache_list(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
106 static int pa_cli_command_scache_load(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
107 static int pa_cli_command_scache_load_dir(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
108 static int pa_cli_command_play_file(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
109 static int pa_cli_command_autoload_list(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
110 static int pa_cli_command_autoload_add(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
111 static int pa_cli_command_autoload_remove(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
112 static int pa_cli_command_dump(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
113 static int pa_cli_command_list_props(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
114 static int pa_cli_command_move_sink_input(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
115 static int pa_cli_command_move_source_output(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
116 static int pa_cli_command_vacuum(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail);
117
118 /* A method table for all available commands */
119
120 static const struct command commands[] = {
121     { "exit",                    pa_cli_command_exit,               "Terminate the daemon",         1 },
122     { "help",                    pa_cli_command_help,               "Show this help",               1 },
123     { "list-modules",            pa_cli_command_modules,            "List loaded modules",          1 },
124     { "list-sinks",              pa_cli_command_sinks,              "List loaded sinks",            1 },
125     { "list-sources",            pa_cli_command_sources,            "List loaded sources",          1 },
126     { "list-clients",            pa_cli_command_clients,            "List loaded clients",          1 },
127     { "list-sink-inputs",        pa_cli_command_sink_inputs,        "List sink inputs",             1 },
128     { "list-source-outputs",     pa_cli_command_source_outputs,     "List source outputs",          1 },
129     { "stat",                    pa_cli_command_stat,               "Show memory block statistics", 1 },
130     { "info",                    pa_cli_command_info,               "Show comprehensive status",    1 },
131     { "ls",                      pa_cli_command_info,               NULL,                           1 },
132     { "list",                    pa_cli_command_info,               NULL,                           1 },
133     { "load-module",             pa_cli_command_load,               "Load a module (args: name, arguments)",                     3},
134     { "unload-module",           pa_cli_command_unload,             "Unload a module (args: index)",                             2},
135     { "set-sink-volume",         pa_cli_command_sink_volume,        "Set the volume of a sink (args: index|name, volume)",             3},
136     { "set-sink-input-volume",   pa_cli_command_sink_input_volume,  "Set the volume of a sink input (args: index|name, volume)", 3},
137     { "set-source-volume",       pa_cli_command_source_volume,      "Set the volume of a source (args: index|name, volume)", 3},
138     { "set-sink-mute",           pa_cli_command_sink_mute,          "Set the mute switch of a sink (args: index|name, mute)", 3},
139     { "set-source-mute",         pa_cli_command_source_mute,        "Set the mute switch of a source (args: index|name, mute)", 3},
140     { "set-default-sink",        pa_cli_command_sink_default,       "Set the default sink (args: index|name)", 2},
141     { "set-default-source",      pa_cli_command_source_default,     "Set the default source (args: index|name)", 2},
142     { "kill-client",             pa_cli_command_kill_client,        "Kill a client (args: index)", 2},
143     { "kill-sink-input",         pa_cli_command_kill_sink_input,    "Kill a sink input (args: index)", 2},
144     { "kill-source-output",      pa_cli_command_kill_source_output, "Kill a source output (args: index)", 2},
145     { "list-samples",            pa_cli_command_scache_list,        "List all entries in the sample cache", 1},
146     { "play-sample",             pa_cli_command_scache_play,        "Play a sample from the sample cache (args: name, sink|index)", 3},
147     { "remove-sample",           pa_cli_command_scache_remove,      "Remove a sample from the sample cache (args: name)", 2},
148     { "load-sample",             pa_cli_command_scache_load,        "Load a sound file into the sample cache (args: name, filename)", 3},
149     { "load-sample-lazy",        pa_cli_command_scache_load,        "Lazily load a sound file into the sample cache (args: name, filename)", 3},
150     { "load-sample-dir-lazy",    pa_cli_command_scache_load_dir,    "Lazily load all files in a directory into the sample cache (args: pathname)", 2},
151     { "play-file",               pa_cli_command_play_file,          "Play a sound file (args: filename, sink|index)", 3},
152     { "list-autoload",           pa_cli_command_autoload_list,      "List autoload entries", 1},
153     { "add-autoload-sink",       pa_cli_command_autoload_add,       "Add autoload entry for a sink (args: sink, module name, arguments)", 4},
154     { "add-autoload-source",     pa_cli_command_autoload_add,       "Add autoload entry for a source (args: source, module name, arguments)", 4},
155     { "remove-autoload-sink",    pa_cli_command_autoload_remove,    "Remove autoload entry for a sink (args: name)", 2},
156     { "remove-autoload-source",  pa_cli_command_autoload_remove,    "Remove autoload entry for a source (args: name)", 2},
157     { "dump",                    pa_cli_command_dump,               "Dump daemon configuration", 1},
158     { "list-props",              pa_cli_command_list_props,         NULL, 1},
159     { "move-sink-input",         pa_cli_command_move_sink_input,    "Move sink input to another sink (args: index, sink)", 3},
160     { "move-source-output",      pa_cli_command_move_source_output, "Move source output to another source (args: index, source)", 3},
161     { "vacuum",                  pa_cli_command_vacuum,             NULL, 1},
162     { NULL, NULL, NULL, 0 }
163 };
164
165 static const char whitespace[] = " \t\n\r";
166 static const char linebreak[] = "\n\r";
167
168 static uint32_t parse_index(const char *n) {
169     uint32_t idx;
170
171     if (pa_atou(n, &idx) < 0)
172         return (uint32_t) PA_IDXSET_INVALID;
173
174     return idx;
175 }
176
177 static int pa_cli_command_exit(pa_core *c, pa_tokenizer *t, PA_GCC_UNUSED pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
178     assert(c && c->mainloop && t);
179     c->mainloop->quit(c->mainloop, 0);
180     return 0;
181 }
182
183 static int pa_cli_command_help(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
184     const struct command*command;
185     assert(c && t && buf);
186
187     pa_strbuf_puts(buf, "Available commands:\n");
188
189     for (command = commands; command->name; command++)
190         if (command->help)
191             pa_strbuf_printf(buf, "    %-25s %s\n", command->name, command->help);
192     return 0;
193 }
194
195 static int pa_cli_command_modules(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
196     char *s;
197     assert(c && t);
198     s = pa_module_list_to_string(c);
199     assert(s);
200     pa_strbuf_puts(buf, s);
201     pa_xfree(s);
202     return 0;
203 }
204
205 static int pa_cli_command_clients(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
206     char *s;
207     assert(c && t);
208     s = pa_client_list_to_string(c);
209     assert(s);
210     pa_strbuf_puts(buf, s);
211     pa_xfree(s);
212     return 0;
213 }
214
215 static int pa_cli_command_sinks(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
216     char *s;
217     assert(c && t);
218     s = pa_sink_list_to_string(c);
219     assert(s);
220     pa_strbuf_puts(buf, s);
221     pa_xfree(s);
222     return 0;
223 }
224
225 static int pa_cli_command_sources(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
226     char *s;
227     assert(c && t);
228     s = pa_source_list_to_string(c);
229     assert(s);
230     pa_strbuf_puts(buf, s);
231     pa_xfree(s);
232     return 0;
233 }
234
235 static int pa_cli_command_sink_inputs(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
236     char *s;
237     assert(c && t);
238     s = pa_sink_input_list_to_string(c);
239     assert(s);
240     pa_strbuf_puts(buf, s);
241     pa_xfree(s);
242     return 0;
243 }
244
245 static int pa_cli_command_source_outputs(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
246     char *s;
247     assert(c && t);
248     s = pa_source_output_list_to_string(c);
249     assert(s);
250     pa_strbuf_puts(buf, s);
251     pa_xfree(s);
252     return 0;
253 }
254
255 static int pa_cli_command_stat(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
256     char s[256];
257     const pa_mempool_stat *stat;
258     unsigned k;
259     const char *def_sink, *def_source;
260
261     static const char* const type_table[PA_MEMBLOCK_TYPE_MAX] = {
262         [PA_MEMBLOCK_POOL] = "POOL",
263         [PA_MEMBLOCK_POOL_EXTERNAL] = "POOL_EXTERNAL",
264         [PA_MEMBLOCK_APPENDED] = "APPENDED",
265         [PA_MEMBLOCK_USER] = "USER",
266         [PA_MEMBLOCK_FIXED] = "FIXED",
267         [PA_MEMBLOCK_IMPORTED] = "IMPORTED",
268     };
269
270     assert(c);
271     assert(t);
272
273     stat = pa_mempool_get_stat(c->mempool);
274
275     pa_strbuf_printf(buf, "Memory blocks currently allocated: %u, size: %s.\n",
276                      (unsigned) pa_atomic_load(&stat->n_allocated),
277                      pa_bytes_snprint(s, sizeof(s), (size_t) pa_atomic_load(&stat->allocated_size)));
278
279     pa_strbuf_printf(buf, "Memory blocks allocated during the whole lifetime: %u, size: %s.\n",
280                      (unsigned) pa_atomic_load(&stat->n_accumulated),
281                      pa_bytes_snprint(s, sizeof(s), (size_t) pa_atomic_load(&stat->accumulated_size)));
282
283     pa_strbuf_printf(buf, "Memory blocks imported from other processes: %u, size: %s.\n",
284                      (unsigned) pa_atomic_load(&stat->n_imported),
285                      pa_bytes_snprint(s, sizeof(s), (size_t) pa_atomic_load(&stat->imported_size)));
286
287     pa_strbuf_printf(buf, "Memory blocks exported to other processes: %u, size: %s.\n",
288                      (unsigned) pa_atomic_load(&stat->n_exported),
289                      pa_bytes_snprint(s, sizeof(s), (size_t) pa_atomic_load(&stat->exported_size)));
290
291     pa_strbuf_printf(buf, "Total sample cache size: %s.\n",
292                      pa_bytes_snprint(s, sizeof(s), pa_scache_total_size(c)));
293
294     pa_strbuf_printf(buf, "Default sample spec: %s\n",
295                      pa_sample_spec_snprint(s, sizeof(s), &c->default_sample_spec));
296
297     def_sink = pa_namereg_get_default_sink_name(c);
298     def_source = pa_namereg_get_default_source_name(c);
299     pa_strbuf_printf(buf, "Default sink name: %s\n"
300                      "Default source name: %s\n",
301                      def_sink ? def_sink : "none",
302                      def_source ? def_source : "none");
303
304     for (k = 0; k < PA_MEMBLOCK_TYPE_MAX; k++)
305         pa_strbuf_printf(buf,
306                          "Memory blocks of type %s: %u allocated/%u accumulated.\n",
307                          type_table[k],
308                          (unsigned) pa_atomic_load(&stat->n_allocated_by_type[k]),
309                          (unsigned) pa_atomic_load(&stat->n_accumulated_by_type[k]));
310
311     return 0;
312 }
313
314 static int pa_cli_command_info(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail) {
315     assert(c && t);
316     pa_cli_command_stat(c, t, buf, fail);
317     pa_cli_command_modules(c, t, buf, fail);
318     pa_cli_command_sinks(c, t, buf, fail);
319     pa_cli_command_sources(c, t, buf, fail);
320     pa_cli_command_clients(c, t, buf, fail);
321     pa_cli_command_sink_inputs(c, t, buf, fail);
322     pa_cli_command_source_outputs(c, t, buf, fail);
323     pa_cli_command_scache_list(c, t, buf, fail);
324     pa_cli_command_autoload_list(c, t, buf, fail);
325     return 0;
326 }
327
328 static int pa_cli_command_load(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
329     pa_module *m;
330     const char *name;
331     assert(c && t);
332
333     if (!(name = pa_tokenizer_get(t, 1))) {
334         pa_strbuf_puts(buf, "You need to specify the module name and optionally arguments.\n");
335         return -1;
336     }
337
338     if (!(m = pa_module_load(c, name,  pa_tokenizer_get(t, 2)))) {
339         pa_strbuf_puts(buf, "Module load failed.\n");
340         return -1;
341     }
342
343     return 0;
344 }
345
346 static int pa_cli_command_unload(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
347     pa_module *m;
348     uint32_t idx;
349     const char *i;
350     char *e;
351     assert(c && t);
352
353     if (!(i = pa_tokenizer_get(t, 1))) {
354         pa_strbuf_puts(buf, "You need to specify the module index.\n");
355         return -1;
356     }
357
358     idx = (uint32_t) strtoul(i, &e, 10);
359     if (*e || !(m = pa_idxset_get_by_index(c->modules, idx))) {
360         pa_strbuf_puts(buf, "Invalid module index.\n");
361         return -1;
362     }
363
364     pa_module_unload_request(m);
365     return 0;
366 }
367
368 static int pa_cli_command_sink_volume(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
369     const char *n, *v;
370     pa_sink *sink;
371     uint32_t volume;
372     pa_cvolume cvolume;
373
374     if (!(n = pa_tokenizer_get(t, 1))) {
375         pa_strbuf_puts(buf, "You need to specify a sink either by its name or its index.\n");
376         return -1;
377     }
378
379     if (!(v = pa_tokenizer_get(t, 2))) {
380         pa_strbuf_puts(buf, "You need to specify a volume >= 0. (0 is muted, 0x100 is normal volume)\n");
381         return -1;
382     }
383
384     if (pa_atou(v, &volume) < 0) {
385         pa_strbuf_puts(buf, "Failed to parse volume.\n");
386         return -1;
387     }
388
389     if (!(sink = pa_namereg_get(c, n, PA_NAMEREG_SINK, 1))) {
390         pa_strbuf_puts(buf, "No sink found by this name or index.\n");
391         return -1;
392     }
393
394     pa_cvolume_set(&cvolume, sink->sample_spec.channels, volume);
395     pa_sink_set_volume(sink, PA_MIXER_HARDWARE, &cvolume);
396     return 0;
397 }
398
399 static int pa_cli_command_sink_input_volume(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
400     const char *n, *v;
401     pa_sink_input *si;
402     pa_volume_t volume;
403     pa_cvolume cvolume;
404     uint32_t idx;
405
406     if (!(n = pa_tokenizer_get(t, 1))) {
407         pa_strbuf_puts(buf, "You need to specify a sink input by its index.\n");
408         return -1;
409     }
410
411     if ((idx = parse_index(n)) == PA_IDXSET_INVALID) {
412         pa_strbuf_puts(buf, "Failed to parse index.\n");
413         return -1;
414     }
415
416     if (!(v = pa_tokenizer_get(t, 2))) {
417         pa_strbuf_puts(buf, "You need to specify a volume >= 0. (0 is muted, 0x100 is normal volume)\n");
418         return -1;
419     }
420
421     if (pa_atou(v, &volume) < 0) {
422         pa_strbuf_puts(buf, "Failed to parse volume.\n");
423         return -1;
424     }
425
426     if (!(si = pa_idxset_get_by_index(c->sink_inputs, (uint32_t) idx))) {
427         pa_strbuf_puts(buf, "No sink input found with this index.\n");
428         return -1;
429     }
430
431     pa_cvolume_set(&cvolume, si->sample_spec.channels, volume);
432     pa_sink_input_set_volume(si, &cvolume);
433     return 0;
434 }
435
436 static int pa_cli_command_source_volume(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
437     const char *n, *v;
438     pa_source *source;
439     uint32_t volume;
440     pa_cvolume cvolume;
441
442     if (!(n = pa_tokenizer_get(t, 1))) {
443         pa_strbuf_puts(buf, "You need to specify a source either by its name or its index.\n");
444         return -1;
445     }
446
447     if (!(v = pa_tokenizer_get(t, 2))) {
448         pa_strbuf_puts(buf, "You need to specify a volume >= 0. (0 is muted, 0x100 is normal volume)\n");
449         return -1;
450     }
451
452     if (pa_atou(v, &volume) < 0) {
453         pa_strbuf_puts(buf, "Failed to parse volume.\n");
454         return -1;
455     }
456
457     if (!(source = pa_namereg_get(c, n, PA_NAMEREG_SOURCE, 1))) {
458         pa_strbuf_puts(buf, "No source found by this name or index.\n");
459         return -1;
460     }
461
462     pa_cvolume_set(&cvolume, source->sample_spec.channels, volume);
463     pa_source_set_volume(source, PA_MIXER_HARDWARE, &cvolume);
464     return 0;
465 }
466
467 static int pa_cli_command_sink_mute(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
468     const char *n, *m;
469     pa_sink *sink;
470     int mute;
471
472     if (!(n = pa_tokenizer_get(t, 1))) {
473         pa_strbuf_puts(buf, "You need to specify a sink either by its name or its index.\n");
474         return -1;
475     }
476
477     if (!(m = pa_tokenizer_get(t, 2))) {
478         pa_strbuf_puts(buf, "You need to specify a mute switch setting (0/1).\n");
479         return -1;
480     }
481
482     if (pa_atoi(m, &mute) < 0) {
483         pa_strbuf_puts(buf, "Failed to parse mute switch.\n");
484         return -1;
485     }
486
487     if (!(sink = pa_namereg_get(c, n, PA_NAMEREG_SINK, 1))) {
488         pa_strbuf_puts(buf, "No sink found by this name or index.\n");
489         return -1;
490     }
491
492     pa_sink_set_mute(sink, PA_MIXER_HARDWARE, mute);
493     return 0;
494 }
495
496 static int pa_cli_command_source_mute(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
497     const char *n, *m;
498     pa_source *source;
499     int mute;
500
501     if (!(n = pa_tokenizer_get(t, 1))) {
502         pa_strbuf_puts(buf, "You need to specify a source either by its name or its index.\n");
503         return -1;
504     }
505
506     if (!(m = pa_tokenizer_get(t, 2))) {
507         pa_strbuf_puts(buf, "You need to specify a mute switch setting (0/1).\n");
508         return -1;
509     }
510
511     if (pa_atoi(m, &mute) < 0) {
512         pa_strbuf_puts(buf, "Failed to parse mute switch.\n");
513         return -1;
514     }
515
516     if (!(source = pa_namereg_get(c, n, PA_NAMEREG_SOURCE, 1))) {
517         pa_strbuf_puts(buf, "No sink found by this name or index.\n");
518         return -1;
519     }
520
521     pa_source_set_mute(source, PA_MIXER_HARDWARE, mute);
522     return 0;
523 }
524
525 static int pa_cli_command_sink_default(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
526     const char *n;
527     assert(c && t);
528
529     if (!(n = pa_tokenizer_get(t, 1))) {
530         pa_strbuf_puts(buf, "You need to specify a sink either by its name or its index.\n");
531         return -1;
532     }
533
534     pa_namereg_set_default(c, n, PA_NAMEREG_SINK);
535     return 0;
536 }
537
538 static int pa_cli_command_source_default(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
539     const char *n;
540     assert(c && t);
541
542     if (!(n = pa_tokenizer_get(t, 1))) {
543         pa_strbuf_puts(buf, "You need to specify a source either by its name or its index.\n");
544         return -1;
545     }
546
547     pa_namereg_set_default(c, n, PA_NAMEREG_SOURCE);
548     return 0;
549 }
550
551 static int pa_cli_command_kill_client(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
552     const char *n;
553     pa_client *client;
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 client 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 (!(client = pa_idxset_get_by_index(c->clients, idx))) {
568         pa_strbuf_puts(buf, "No client found by this index.\n");
569         return -1;
570     }
571
572     pa_client_kill(client);
573     return 0;
574 }
575
576 static int pa_cli_command_kill_sink_input(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
577     const char *n;
578     pa_sink_input *sink_input;
579     uint32_t idx;
580     assert(c && t);
581
582     if (!(n = pa_tokenizer_get(t, 1))) {
583         pa_strbuf_puts(buf, "You need to specify a sink input by its index.\n");
584         return -1;
585     }
586
587     if ((idx = parse_index(n)) == PA_IDXSET_INVALID) {
588         pa_strbuf_puts(buf, "Failed to parse index.\n");
589         return -1;
590     }
591
592     if (!(sink_input = pa_idxset_get_by_index(c->sink_inputs, idx))) {
593         pa_strbuf_puts(buf, "No sink input found by this index.\n");
594         return -1;
595     }
596
597     pa_sink_input_kill(sink_input);
598     return 0;
599 }
600
601 static int pa_cli_command_kill_source_output(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
602     const char *n;
603     pa_source_output *source_output;
604     uint32_t idx;
605     assert(c && t);
606
607     if (!(n = pa_tokenizer_get(t, 1))) {
608         pa_strbuf_puts(buf, "You need to specify a source output by its index.\n");
609         return -1;
610     }
611
612     if ((idx = parse_index(n)) == PA_IDXSET_INVALID) {
613         pa_strbuf_puts(buf, "Failed to parse index.\n");
614         return -1;
615     }
616
617     if (!(source_output = pa_idxset_get_by_index(c->source_outputs, idx))) {
618         pa_strbuf_puts(buf, "No source output found by this index.\n");
619         return -1;
620     }
621
622     pa_source_output_kill(source_output);
623     return 0;
624 }
625
626 static int pa_cli_command_scache_list(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
627     char *s;
628     assert(c && t);
629     s = pa_scache_list_to_string(c);
630     assert(s);
631     pa_strbuf_puts(buf, s);
632     pa_xfree(s);
633     return 0;
634 }
635
636 static int pa_cli_command_scache_play(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail) {
637     const char *n, *sink_name;
638     pa_sink *sink;
639     assert(c && t && buf && fail);
640
641     if (!(n = pa_tokenizer_get(t, 1)) || !(sink_name = pa_tokenizer_get(t, 2))) {
642         pa_strbuf_puts(buf, "You need to specify a sample name and a sink name.\n");
643         return -1;
644     }
645
646     if (!(sink = pa_namereg_get(c, sink_name, PA_NAMEREG_SINK, 1))) {
647         pa_strbuf_puts(buf, "No sink by that name.\n");
648         return -1;
649     }
650
651     if (pa_scache_play_item(c, n, sink, PA_VOLUME_NORM) < 0) {
652         pa_strbuf_puts(buf, "Failed to play sample.\n");
653         return -1;
654     }
655
656     return 0;
657 }
658
659 static int pa_cli_command_scache_remove(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail) {
660     const char *n;
661     assert(c && t && buf && fail);
662
663     if (!(n = pa_tokenizer_get(t, 1))) {
664         pa_strbuf_puts(buf, "You need to specify a sample name.\n");
665         return -1;
666     }
667
668     if (pa_scache_remove_item(c, n) < 0) {
669         pa_strbuf_puts(buf, "Failed to remove sample.\n");
670         return -1;
671     }
672
673     return 0;
674 }
675
676 static int pa_cli_command_scache_load(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail) {
677     const char *fname, *n;
678     int r;
679     assert(c && t && buf && fail);
680
681     if (!(fname = pa_tokenizer_get(t, 2)) || !(n = pa_tokenizer_get(t, 1))) {
682         pa_strbuf_puts(buf, "You need to specify a file name and a sample name.\n");
683         return -1;
684     }
685
686     if (strstr(pa_tokenizer_get(t, 0), "lazy"))
687         r = pa_scache_add_file_lazy(c, n, fname, NULL);
688     else
689         r = pa_scache_add_file(c, n, fname, NULL);
690
691     if (r < 0)
692         pa_strbuf_puts(buf, "Failed to load sound file.\n");
693
694     return 0;
695 }
696
697 static int pa_cli_command_scache_load_dir(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail) {
698     const char *pname;
699     assert(c && t && buf && fail);
700
701     if (!(pname = pa_tokenizer_get(t, 1))) {
702         pa_strbuf_puts(buf, "You need to specify a path name.\n");
703         return -1;
704     }
705
706     if (pa_scache_add_directory_lazy(c, pname) < 0) {
707         pa_strbuf_puts(buf, "Failed to load directory.\n");
708         return -1;
709     }
710
711     return 0;
712 }
713
714 static int pa_cli_command_play_file(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail) {
715     const char *fname, *sink_name;
716     pa_sink *sink;
717     assert(c && t && buf && fail);
718
719     if (!(fname = pa_tokenizer_get(t, 1)) || !(sink_name = pa_tokenizer_get(t, 2))) {
720         pa_strbuf_puts(buf, "You need to specify a file name and a sink name.\n");
721         return -1;
722     }
723
724     if (!(sink = pa_namereg_get(c, sink_name, PA_NAMEREG_SINK, 1))) {
725         pa_strbuf_puts(buf, "No sink by that name.\n");
726         return -1;
727     }
728
729
730     return pa_play_file(sink, fname, NULL);
731 }
732
733 static int pa_cli_command_autoload_add(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail) {
734     const char *a, *b;
735     assert(c && t && buf && fail);
736
737     if (!(a = pa_tokenizer_get(t, 1)) || !(b = pa_tokenizer_get(t, 2))) {
738         pa_strbuf_puts(buf, "You need to specify a device name, a filename or a module name and optionally module arguments\n");
739         return -1;
740     }
741
742     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);
743
744     return 0;
745 }
746
747 static int pa_cli_command_autoload_remove(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail) {
748     const char *name;
749     assert(c && t && buf && fail);
750
751     if (!(name = pa_tokenizer_get(t, 1))) {
752         pa_strbuf_puts(buf, "You need to specify a device name\n");
753         return -1;
754     }
755
756     if (pa_autoload_remove_by_name(c, name, strstr(pa_tokenizer_get(t, 0), "sink") ? PA_NAMEREG_SINK : PA_NAMEREG_SOURCE) < 0) {
757         pa_strbuf_puts(buf, "Failed to remove autload entry\n");
758         return -1;
759     }
760
761     return 0;
762 }
763
764 static int pa_cli_command_autoload_list(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
765     char *s;
766     assert(c && t);
767     s = pa_autoload_list_to_string(c);
768     assert(s);
769     pa_strbuf_puts(buf, s);
770     pa_xfree(s);
771     return 0;
772 }
773
774 static int pa_cli_command_list_props(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
775     assert(c && t);
776     pa_property_dump(c, buf);
777     return 0;
778 }
779
780 static int pa_cli_command_vacuum(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail) {
781     assert(c);
782     assert(t);
783
784     pa_mempool_vacuum(c->mempool);
785
786     return 0;
787 }
788
789 static int pa_cli_command_move_sink_input(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail) {
790     const char *n, *k;
791     pa_sink_input *si;
792     pa_sink *sink;
793     uint32_t idx;
794
795     if (!(n = pa_tokenizer_get(t, 1))) {
796         pa_strbuf_puts(buf, "You need to specify a sink input by its index.\n");
797         return -1;
798     }
799
800     if ((idx = parse_index(n)) == PA_IDXSET_INVALID) {
801         pa_strbuf_puts(buf, "Failed to parse index.\n");
802         return -1;
803     }
804
805     if (!(k = pa_tokenizer_get(t, 2))) {
806         pa_strbuf_puts(buf, "You need to specify a sink.\n");
807         return -1;
808     }
809
810     if (!(si = pa_idxset_get_by_index(c->sink_inputs, (uint32_t) idx))) {
811         pa_strbuf_puts(buf, "No sink input found with this index.\n");
812         return -1;
813     }
814
815     if (!(sink = pa_namereg_get(c, k, PA_NAMEREG_SINK, 1))) {
816         pa_strbuf_puts(buf, "No sink found by this name or index.\n");
817         return -1;
818     }
819
820     if (pa_sink_input_move_to(si, sink, 0) < 0) {
821         pa_strbuf_puts(buf, "Moved failed.\n");
822         return -1;
823     }
824     return 0;
825 }
826
827 static int pa_cli_command_move_source_output(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, int *fail) {
828     const char *n, *k;
829     pa_source_output *so;
830     pa_source *source;
831     uint32_t idx;
832
833     if (!(n = pa_tokenizer_get(t, 1))) {
834         pa_strbuf_puts(buf, "You need to specify a source output by its index.\n");
835         return -1;
836     }
837
838     if ((idx = parse_index(n)) == PA_IDXSET_INVALID) {
839         pa_strbuf_puts(buf, "Failed to parse index.\n");
840         return -1;
841     }
842
843     if (!(k = pa_tokenizer_get(t, 2))) {
844         pa_strbuf_puts(buf, "You need to specify a source.\n");
845         return -1;
846     }
847
848     if (!(so = pa_idxset_get_by_index(c->source_outputs, (uint32_t) idx))) {
849         pa_strbuf_puts(buf, "No source output found with this index.\n");
850         return -1;
851     }
852
853     if (!(source = pa_namereg_get(c, k, PA_NAMEREG_SOURCE, 1))) {
854         pa_strbuf_puts(buf, "No source found by this name or index.\n");
855         return -1;
856     }
857
858     if (pa_source_output_move_to(so, source) < 0) {
859         pa_strbuf_puts(buf, "Moved failed.\n");
860         return -1;
861     }
862     return 0;
863 }
864
865 static int pa_cli_command_dump(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, PA_GCC_UNUSED int *fail) {
866     pa_module *m;
867     pa_sink *sink;
868     pa_source *source;
869     int nl;
870     const char *p;
871     uint32_t idx;
872     char txt[256];
873     time_t now;
874     void *i;
875     pa_autoload_entry *a;
876
877     assert(c && t);
878
879     time(&now);
880
881 #ifdef HAVE_CTIME_R
882     pa_strbuf_printf(buf, "### Configuration dump generated at %s\n", ctime_r(&now, txt));
883 #else
884     pa_strbuf_printf(buf, "### Configuration dump generated at %s\n", ctime(&now));
885 #endif
886
887
888     for (m = pa_idxset_first(c->modules, &idx); m; m = pa_idxset_next(c->modules, &idx)) {
889         if (m->auto_unload)
890             continue;
891
892         pa_strbuf_printf(buf, "load-module %s", m->name);
893
894         if (m->argument)
895             pa_strbuf_printf(buf, " %s", m->argument);
896
897         pa_strbuf_puts(buf, "\n");
898     }
899
900     nl = 0;
901
902     for (sink = pa_idxset_first(c->sinks, &idx); sink; sink = pa_idxset_next(c->sinks, &idx)) {
903         if (sink->owner && sink->owner->auto_unload)
904             continue;
905
906         if (!nl) {
907             pa_strbuf_puts(buf, "\n");
908             nl = 1;
909         }
910
911         pa_strbuf_printf(buf, "set-sink-volume %s 0x%03x\n", sink->name, pa_cvolume_avg(pa_sink_get_volume(sink, PA_MIXER_HARDWARE)));
912         pa_strbuf_printf(buf, "set-sink-mute %s %d\n", sink->name, pa_sink_get_mute(sink, PA_MIXER_HARDWARE));
913     }
914
915     for (source = pa_idxset_first(c->sources, &idx); source; source = pa_idxset_next(c->sources, &idx)) {
916         if (source->owner && source->owner->auto_unload)
917             continue;
918
919         if (!nl) {
920             pa_strbuf_puts(buf, "\n");
921             nl = 1;
922         }
923
924         pa_strbuf_printf(buf, "set-source-volume %s 0x%03x\n", source->name, pa_cvolume_avg(pa_source_get_volume(source, PA_MIXER_HARDWARE)));
925         pa_strbuf_printf(buf, "set-source-mute %s %d\n", source->name, pa_source_get_mute(source, PA_MIXER_HARDWARE));
926     }
927
928
929     if (c->autoload_hashmap) {
930         nl = 0;
931
932         i = NULL;
933         while ((a = pa_hashmap_iterate(c->autoload_hashmap, &i, NULL))) {
934
935             if (!nl) {
936                 pa_strbuf_puts(buf, "\n");
937                 nl = 1;
938             }
939
940             pa_strbuf_printf(buf, "add-autoload-%s %s %s", a->type == PA_NAMEREG_SINK ? "sink" : "source", a->name, a->module);
941
942             if (a->argument)
943                 pa_strbuf_printf(buf, " %s", a->argument);
944
945             pa_strbuf_puts(buf, "\n");
946         }
947     }
948
949     nl = 0;
950
951     if ((p = pa_namereg_get_default_sink_name(c))) {
952         if (!nl) {
953             pa_strbuf_puts(buf, "\n");
954             nl = 1;
955         }
956         pa_strbuf_printf(buf, "set-default-sink %s\n", p);
957     }
958
959     if ((p = pa_namereg_get_default_source_name(c))) {
960         if (!nl) {
961             pa_strbuf_puts(buf, "\n");
962             nl = 1;
963         }
964         pa_strbuf_printf(buf, "set-default-source %s\n", p);
965     }
966
967     pa_strbuf_puts(buf, "\n### EOF\n");
968
969     return 0;
970 }
971
972 int pa_cli_command_execute_line_stateful(pa_core *c, const char *s, pa_strbuf *buf, int *fail, int *ifstate) {
973     const char *cs;
974
975     cs = s+strspn(s, whitespace);
976
977     if (*cs == '#' || !*cs)
978         return 0;
979     else if (*cs == '.') {
980         if (!strcmp(cs, META_ELSE)) {
981             if (!ifstate || *ifstate == IFSTATE_NONE) {
982                 pa_strbuf_printf(buf, "Meta command %s is not valid in this context\n", cs);
983                 return -1;
984             } else if (*ifstate == IFSTATE_TRUE)
985                 *ifstate = IFSTATE_FALSE;
986             else
987                 *ifstate = IFSTATE_TRUE;
988             return 0;
989         } else if (!strcmp(cs, META_ENDIF)) {
990             if (!ifstate || *ifstate == IFSTATE_NONE) {
991                 pa_strbuf_printf(buf, "Meta command %s is not valid in this context\n", cs);
992                 return -1;
993             } else
994                 *ifstate = IFSTATE_NONE;
995             return 0;
996         }
997         if (ifstate && *ifstate == IFSTATE_FALSE)
998             return 0;
999         if (!strcmp(cs, META_FAIL))
1000             *fail = 1;
1001         else if (!strcmp(cs, META_NOFAIL))
1002             *fail = 0;
1003         else {
1004             size_t l;
1005             l = strcspn(cs, whitespace);
1006
1007             if (l == sizeof(META_INCLUDE)-1 && !strncmp(cs, META_INCLUDE, l)) {
1008                 const char *filename = cs+l+strspn(cs+l, whitespace);
1009
1010                 if (pa_cli_command_execute_file(c, filename, buf, fail) < 0)
1011                     if (*fail) return -1;
1012             } else if (l == sizeof(META_IFEXISTS)-1 && !strncmp(cs, META_IFEXISTS, l)) {
1013                 if (!ifstate) {
1014                     pa_strbuf_printf(buf, "Meta command %s is not valid in this context\n", cs);
1015                     return -1;
1016                 } else if (*ifstate != IFSTATE_NONE) {
1017                     pa_strbuf_printf(buf, "Nested %s commands not supported\n", cs);
1018                     return -1;
1019                 } else {
1020                     const char *filename = cs+l+strspn(cs+l, whitespace);
1021
1022                     *ifstate = access(filename, F_OK) == 0 ? IFSTATE_TRUE : IFSTATE_FALSE;
1023                 }
1024             } else {
1025                 pa_strbuf_printf(buf, "Invalid meta command: %s\n", cs);
1026                 if (*fail) return -1;
1027             }
1028         }
1029     } else {
1030         const struct command*command;
1031         int unknown = 1;
1032         size_t l;
1033
1034         if (ifstate && *ifstate == IFSTATE_FALSE)
1035              return 0;
1036
1037
1038         l = strcspn(cs, whitespace);
1039
1040         for (command = commands; command->name; command++)
1041             if (strlen(command->name) == l && !strncmp(cs, command->name, l)) {
1042                 int ret;
1043                 pa_tokenizer *t = pa_tokenizer_new(cs, command->args);
1044                 assert(t);
1045                 ret = command->proc(c, t, buf, fail);
1046                 pa_tokenizer_free(t);
1047                 unknown = 0;
1048
1049                 if (ret < 0 && *fail)
1050                     return -1;
1051
1052                 break;
1053             }
1054
1055         if (unknown) {
1056             pa_strbuf_printf(buf, "Unknown command: %s\n", cs);
1057             if (*fail)
1058                 return -1;
1059         }
1060     }
1061
1062     return 0;
1063 }
1064
1065 int pa_cli_command_execute_line(pa_core *c, const char *s, pa_strbuf *buf, int *fail) {
1066     return pa_cli_command_execute_line_stateful(c, s, buf, fail, NULL);
1067 }
1068
1069 int pa_cli_command_execute_file(pa_core *c, const char *fn, pa_strbuf *buf, int *fail) {
1070     char line[256];
1071     FILE *f = NULL;
1072     int ifstate = IFSTATE_NONE;
1073     int ret = -1;
1074
1075     assert(c);
1076     assert(fn);
1077     assert(buf);
1078
1079     if (!(f = fopen(fn, "r"))) {
1080         pa_strbuf_printf(buf, "open('%s') failed: %s\n", fn, pa_cstrerror(errno));
1081         if (!*fail)
1082             ret = 0;
1083         goto fail;
1084     }
1085
1086     while (fgets(line, sizeof(line), f)) {
1087         char *e = line + strcspn(line, linebreak);
1088         *e = 0;
1089
1090         if (pa_cli_command_execute_line_stateful(c, line, buf, fail, &ifstate) < 0 && *fail)
1091             goto fail;
1092     }
1093
1094     ret = 0;
1095
1096 fail:
1097     if (f)
1098         fclose(f);
1099
1100     return ret;
1101 }
1102
1103 int pa_cli_command_execute(pa_core *c, const char *s, pa_strbuf *buf, int *fail) {
1104     const char *p;
1105     int ifstate = IFSTATE_NONE;
1106
1107     assert(c);
1108     assert(s);
1109     assert(buf);
1110
1111     p = s;
1112     while (*p) {
1113         size_t l = strcspn(p, linebreak);
1114         char *line = pa_xstrndup(p, l);
1115
1116         if (pa_cli_command_execute_line_stateful(c, line, buf, fail, &ifstate) < 0 && *fail) {
1117             pa_xfree(line);
1118             return -1;
1119         }
1120         pa_xfree(line);
1121
1122         p += l;
1123         p += strspn(p, linebreak);
1124     }
1125
1126     return 0;
1127 }