remove global memblock statistic variables in favor of memblock_stat objects
[profile/ivi/pulseaudio.git] / 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 "cli-text.h"
43 #include "scache.h"
44 #include "sample-util.h"
45 #include "sound-file.h"
46 #include "play-memchunk.h"
47 #include "autoload.h"
48 #include "xmalloc.h"
49
50 struct command {
51     const char *name;
52     int (*proc) (struct pa_core *c, struct pa_tokenizer*t, struct pa_strbuf *buf, int *fail, int *verbose);
53     const char *help;
54     unsigned args;
55 };
56
57 static int pa_cli_command_exit(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
58 static int pa_cli_command_help(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
59 static int pa_cli_command_modules(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
60 static int pa_cli_command_clients(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
61 static int pa_cli_command_sinks(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
62 static int pa_cli_command_sources(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
63 static int pa_cli_command_sink_inputs(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
64 static int pa_cli_command_source_outputs(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
65 static int pa_cli_command_stat(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
66 static int pa_cli_command_info(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
67 static int pa_cli_command_load(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
68 static int pa_cli_command_unload(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
69 static int pa_cli_command_sink_volume(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
70 static int pa_cli_command_sink_input_volume(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
71 static int pa_cli_command_sink_default(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
72 static int pa_cli_command_source_default(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
73 static int pa_cli_command_kill_client(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
74 static int pa_cli_command_kill_sink_input(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
75 static int pa_cli_command_kill_source_output(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
76 static int pa_cli_command_scache_play(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
77 static int pa_cli_command_scache_remove(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
78 static int pa_cli_command_scache_list(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
79 static int pa_cli_command_scache_load(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
80 static int pa_cli_command_play_file(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
81 static int pa_cli_command_autoload_list(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
82 static int pa_cli_command_autoload_add(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
83 static int pa_cli_command_autoload_remove(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
84
85 static const struct command commands[] = {
86     { "exit",                    pa_cli_command_exit,               "Terminate the daemon",         1 },
87     { "help",                    pa_cli_command_help,               "Show this help",               1 },
88     { "modules",                 pa_cli_command_modules,            "List loaded modules",          1 },
89     { "sinks",                   pa_cli_command_sinks,              "List loaded sinks",            1 },
90     { "sources",                 pa_cli_command_sources,            "List loaded sources",          1 },
91     { "clients",                 pa_cli_command_clients,            "List loaded clients",          1 },
92     { "sink_inputs",             pa_cli_command_sink_inputs,        "List sink inputs",             1 },
93     { "source_outputs",          pa_cli_command_source_outputs,     "List source outputs",          1 },
94     { "stat",                    pa_cli_command_stat,               "Show memory block statistics", 1 },
95     { "info",                    pa_cli_command_info,               "Show comprehensive status",    1 },
96     { "ls",                      pa_cli_command_info,               NULL,                           1 },
97     { "list",                    pa_cli_command_info,               NULL,                           1 },
98     { "load",                    pa_cli_command_load,               "Load a module (args: name, arguments)",                     3},
99     { "unload",                  pa_cli_command_unload,             "Unload a module (args: index)",                             2},
100     { "sink_volume",             pa_cli_command_sink_volume,        "Set the volume of a sink (args: index|name, volume)",             3},
101     { "sink_input_volume",       pa_cli_command_sink_input_volume,  "Set the volume of a sink input (args: index|name, volume)", 3},
102     { "sink_default",            pa_cli_command_sink_default,       "Set the default sink (args: index|name)", 2},
103     { "source_default",          pa_cli_command_source_default,     "Set the default source (args: index|name)", 2},
104     { "kill_client",             pa_cli_command_kill_client,        "Kill a client (args: index)", 2},
105     { "kill_sink_input",         pa_cli_command_kill_sink_input,    "Kill a sink input (args: index)", 2},
106     { "kill_source_output",      pa_cli_command_kill_source_output, "Kill a source output (args: index)", 2},
107     { "scache_list",             pa_cli_command_scache_list,        "List all entries in the sample cache", 1},
108     { "scache_play",             pa_cli_command_scache_play,        "Play a sample from the sample cache (args: name, sink|index)", 3},
109     { "scache_remove",           pa_cli_command_scache_remove,      "Remove a sample from the sample cache (args: name)", 2},
110     { "scache_load",             pa_cli_command_scache_load,        "Load a sound file into the sample cache (args: filename,name)", 3},
111     { "play_file",               pa_cli_command_play_file,          "Play a sound file (args: filename, sink|index)", 3},
112     { "autoload_list",           pa_cli_command_autoload_list,      "List autoload entries", 1},
113     { "autoload_sink_add",       pa_cli_command_autoload_add,       "Add autoload entry for a sink (args: sink, name, arguments)", 4},
114     { "autoload_source_add",     pa_cli_command_autoload_add,       "Add autoload entry for a source (args: source, name, arguments)", 4},
115     { "autoload_sink_remove",    pa_cli_command_autoload_remove,    "Remove autoload entry for a sink (args: sink)", 2},
116     { "autoload_source_remove",  pa_cli_command_autoload_remove,    "Remove autoload entry for a source (args: source)", 2},
117     { NULL, NULL, NULL, 0 }
118 };
119
120 static const char whitespace[] = " \t\n\r";
121 static const char linebreak[] = "\n\r";
122
123 static uint32_t parse_index(const char *n) {
124     long index;
125     char *x;
126     index = strtol(n, &x, 0);
127     if (!x || *x != 0 || index < 0)
128         return (uint32_t) PA_IDXSET_INVALID;
129
130     return (uint32_t) index;
131 }
132
133 static int pa_cli_command_exit(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
134     assert(c && c->mainloop && t);
135     c->mainloop->quit(c->mainloop, 0);
136     return 0;
137 }
138
139 static int pa_cli_command_help(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
140     const struct command*command;
141     assert(c && t && buf);
142
143     pa_strbuf_puts(buf, "Available commands:\n");
144     
145     for (command = commands; command->name; command++)
146         if (command->help)
147             pa_strbuf_printf(buf, "    %-20s %s\n", command->name, command->help);
148     return 0;
149 }
150
151 static int pa_cli_command_modules(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_module_list_to_string(c);
155     assert(s);
156     pa_strbuf_puts(buf, s);
157     pa_xfree(s);
158     return 0;
159 }
160
161 static int pa_cli_command_clients(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_client_list_to_string(c);
165     assert(s);
166     pa_strbuf_puts(buf, s);
167     pa_xfree(s);
168     return 0;
169 }
170
171 static int pa_cli_command_sinks(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_sink_list_to_string(c);
175     assert(s);
176     pa_strbuf_puts(buf, s);
177     pa_xfree(s);
178     return 0;
179 }
180
181 static int pa_cli_command_sources(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_source_list_to_string(c);
185     assert(s);
186     pa_strbuf_puts(buf, s);
187     pa_xfree(s);
188     return 0;
189 }
190
191 static int pa_cli_command_sink_inputs(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_sink_input_list_to_string(c);
195     assert(s);
196     pa_strbuf_puts(buf, s);
197     pa_xfree(s);
198     return 0;
199 }
200
201 static int pa_cli_command_source_outputs(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
202     char *s;
203     assert(c && t);
204     s = pa_source_output_list_to_string(c);
205     assert(s);
206     pa_strbuf_puts(buf, s);
207     pa_xfree(s);
208     return 0;
209 }
210
211 static int pa_cli_command_stat(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
212     assert(c && t);
213     pa_strbuf_printf(buf, "Memory blocks currently allocated: %u, size: %u bytes.\nMemory blocks allocated during the whole lifetime: %u, size: %u bytes.\n", c->memblock_stat->total, c->memblock_stat->total_size, c->memblock_stat->allocated, c->memblock_stat->allocated_size);
214     return 0;
215 }
216
217 static int pa_cli_command_info(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
218     assert(c && t);
219     pa_cli_command_stat(c, t, buf, fail, verbose);
220     pa_cli_command_modules(c, t, buf, fail, verbose);
221     pa_cli_command_sinks(c, t, buf, fail, verbose);
222     pa_cli_command_sources(c, t, buf, fail, verbose);
223     pa_cli_command_clients(c, t, buf, fail, verbose);
224     pa_cli_command_sink_inputs(c, t, buf, fail, verbose);
225     pa_cli_command_source_outputs(c, t, buf, fail, verbose);
226     pa_cli_command_scache_list(c, t, buf, fail, verbose);
227     pa_cli_command_autoload_list(c, t, buf, fail, verbose);
228     return 0;
229 }
230
231 static int pa_cli_command_load(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
232     struct pa_module *m;
233     const char *name;
234     char txt[256];
235     assert(c && t);
236
237     if (!(name = pa_tokenizer_get(t, 1))) {
238         pa_strbuf_puts(buf, "You need to specify the module name and optionally arguments.\n");
239         return -1;
240     }
241     
242     if (!(m = pa_module_load(c, name,  pa_tokenizer_get(t, 2)))) {
243         pa_strbuf_puts(buf, "Module load failed.\n");
244         return -1;
245     }
246
247     if (*verbose) {
248         snprintf(txt, sizeof(txt), "Module successfully loaded, index: %u.\n", m->index);
249         pa_strbuf_puts(buf, txt);
250     }
251     return 0;
252 }
253
254 static int pa_cli_command_unload(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
255     struct pa_module *m;
256     uint32_t index;
257     const char *i;
258     char *e;
259     assert(c && t);
260
261     if (!(i = pa_tokenizer_get(t, 1))) {
262         pa_strbuf_puts(buf, "You need to specify the module index.\n");
263         return -1;
264     }
265
266     index = (uint32_t) strtoul(i, &e, 10);
267     if (*e || !(m = pa_idxset_get_by_index(c->modules, index))) {
268         pa_strbuf_puts(buf, "Invalid module index.\n");
269         return -1;
270     }
271
272     pa_module_unload_request(c, m);
273     return 0;
274 }
275
276 static int pa_cli_command_sink_volume(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
277     const char *n, *v;
278     char *x = NULL;
279     struct pa_sink *sink;
280     long volume;
281
282     if (!(n = pa_tokenizer_get(t, 1))) {
283         pa_strbuf_puts(buf, "You need to specify a sink either by its name or its index.\n");
284         return -1;
285     }
286
287     if (!(v = pa_tokenizer_get(t, 2))) {
288         pa_strbuf_puts(buf, "You need to specify a volume >= 0. (0 is muted, 0x100 is normal volume)\n");
289         return -1;
290     }
291
292     volume = strtol(v, &x, 0);
293     if (!x || *x != 0 || volume < 0) {
294         pa_strbuf_puts(buf, "Failed to parse volume.\n");
295         return -1;
296     }
297
298     if (!(sink = pa_namereg_get(c, n, PA_NAMEREG_SINK, 1))) {
299         pa_strbuf_puts(buf, "No sink found by this name or index.\n");
300         return -1;
301     }
302
303     pa_sink_set_volume(sink, (uint32_t) volume);
304     return 0;
305 }
306
307 static int pa_cli_command_sink_input_volume(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
308     const char *n, *v;
309     struct pa_sink_input *si;
310     long volume;
311     uint32_t index;
312     char *x;
313
314     if (!(n = pa_tokenizer_get(t, 1))) {
315         pa_strbuf_puts(buf, "You need to specify a sink input by its index.\n");
316         return -1;
317     }
318
319     if ((index = parse_index(n)) == PA_IDXSET_INVALID) {
320         pa_strbuf_puts(buf, "Failed to parse index.\n");
321         return -1;
322     }
323
324     if (!(v = pa_tokenizer_get(t, 2))) {
325         pa_strbuf_puts(buf, "You need to specify a volume >= 0. (0 is muted, 0x100 is normal volume)\n");
326         return -1;
327     }
328
329     x = NULL;
330     volume = strtol(v, &x, 0);
331     if (!x || *x != 0 || volume < 0) {
332         pa_strbuf_puts(buf, "Failed to parse volume.\n");
333         return -1;
334     }
335
336     if (!(si = pa_idxset_get_by_index(c->sink_inputs, (uint32_t) index))) {
337         pa_strbuf_puts(buf, "No sink input found with this index.\n");
338         return -1;
339     }
340
341     pa_sink_input_set_volume(si, (uint32_t) volume);
342     return 0;
343 }
344
345 static int pa_cli_command_sink_default(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
346     const char *n;
347     assert(c && t);
348
349     if (!(n = pa_tokenizer_get(t, 1))) {
350         pa_strbuf_puts(buf, "You need to specify a sink either by its name or its index.\n");
351         return -1;
352     }
353
354     pa_namereg_set_default(c, n, PA_NAMEREG_SINK);
355     return 0;
356 }
357
358 static int pa_cli_command_source_default(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
359     const char *n;
360     assert(c && t);
361
362     if (!(n = pa_tokenizer_get(t, 1))) {
363         pa_strbuf_puts(buf, "You need to specify a source either by its name or its index.\n");
364         return -1;
365     }
366
367     pa_namereg_set_default(c, n, PA_NAMEREG_SOURCE);
368     return 0;
369 }
370
371 static int pa_cli_command_kill_client(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
372     const char *n;
373     struct pa_client *client;
374     uint32_t index;
375     assert(c && t);
376
377     if (!(n = pa_tokenizer_get(t, 1))) {
378         pa_strbuf_puts(buf, "You need to specify a client by its index.\n");
379         return -1;
380     }
381
382     if ((index = parse_index(n)) == PA_IDXSET_INVALID) {
383         pa_strbuf_puts(buf, "Failed to parse index.\n");
384         return -1;
385     }
386
387     if (!(client = pa_idxset_get_by_index(c->clients, index))) {
388         pa_strbuf_puts(buf, "No client found by this index.\n");
389         return -1;
390     }
391
392     pa_client_kill(client);
393     return 0;
394 }
395
396 static int pa_cli_command_kill_sink_input(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
397     const char *n;
398     struct pa_sink_input *sink_input;
399     uint32_t index;
400     assert(c && t);
401
402     if (!(n = pa_tokenizer_get(t, 1))) {
403         pa_strbuf_puts(buf, "You need to specify a sink input by its index.\n");
404         return -1;
405     }
406
407     if ((index = parse_index(n)) == PA_IDXSET_INVALID) {
408         pa_strbuf_puts(buf, "Failed to parse index.\n");
409         return -1;
410     }
411
412     if (!(sink_input = pa_idxset_get_by_index(c->sink_inputs, index))) {
413         pa_strbuf_puts(buf, "No sink input found by this index.\n");
414         return -1;
415     }
416
417     pa_sink_input_kill(sink_input);
418     return 0;
419 }
420
421 static int pa_cli_command_kill_source_output(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
422     const char *n;
423     struct pa_source_output *source_output;
424     uint32_t index;
425     assert(c && t);
426
427     if (!(n = pa_tokenizer_get(t, 1))) {
428         pa_strbuf_puts(buf, "You need to specify a source output by its index.\n");
429         return -1;
430     }
431
432     if ((index = parse_index(n)) == PA_IDXSET_INVALID) {
433         pa_strbuf_puts(buf, "Failed to parse index.\n");
434         return -1;
435     }
436
437     if (!(source_output = pa_idxset_get_by_index(c->source_outputs, index))) {
438         pa_strbuf_puts(buf, "No source output found by this index.\n");
439         return -1;
440     }
441
442     pa_source_output_kill(source_output);
443     return 0;
444 }
445
446 static int pa_cli_command_scache_list(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
447     char *s;
448     assert(c && t);
449     s = pa_scache_list_to_string(c);
450     assert(s);
451     pa_strbuf_puts(buf, s);
452     pa_xfree(s);
453     return 0;
454 }
455
456 static int pa_cli_command_scache_play(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
457     const char *n, *sink_name;
458     struct pa_sink *sink;
459     assert(c && t && buf && fail && verbose);
460
461     if (!(n = pa_tokenizer_get(t, 1)) || !(sink_name = pa_tokenizer_get(t, 2))) {
462         pa_strbuf_puts(buf, "You need to specify a sample name and a sink name.\n");
463         return -1;
464     }
465
466     if (!(sink = pa_namereg_get(c, sink_name, PA_NAMEREG_SINK, 1))) {
467         pa_strbuf_puts(buf, "No sink by that name.\n");
468         return -1;
469     }
470
471     if (pa_scache_play_item(c, n, sink, PA_VOLUME_NORM) < 0) {
472         pa_strbuf_puts(buf, "Failed to play sample.\n");
473         return -1;
474     }
475
476     return 0;
477 }
478
479 static int pa_cli_command_scache_remove(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
480     const char *n;
481     assert(c && t && buf && fail && verbose);
482
483     if (!(n = pa_tokenizer_get(t, 1))) {
484         pa_strbuf_puts(buf, "You need to specify a sample name.\n");
485         return -1;
486     }
487
488     if (pa_scache_remove_item(c, n) < 0) {
489         pa_strbuf_puts(buf, "Failed to remove sample.\n");
490         return -1;
491     }
492
493     return 0;
494 }
495
496 static int pa_cli_command_scache_load(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
497     const char *fname, *n;
498     struct pa_memchunk chunk;
499     struct pa_sample_spec ss;
500     assert(c && t && buf && fail && verbose);
501
502     if (!(fname = pa_tokenizer_get(t, 1)) || !(n = pa_tokenizer_get(t, 2))) {
503         pa_strbuf_puts(buf, "You need to specify a file name and a sample name.\n");
504         return -1;
505     }
506
507     if (pa_sound_file_load(fname, &ss, &chunk, c->memblock_stat) < 0) {
508         pa_strbuf_puts(buf, "Failed to load sound file.\n");
509         return -1;
510     }
511
512     pa_scache_add_item(c, n, &ss, &chunk, NULL);
513     pa_memblock_unref(chunk.memblock);
514     return 0;
515 }
516
517 static int pa_cli_command_play_file(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
518     const char *fname, *sink_name;
519     struct pa_memchunk chunk;
520     struct pa_sample_spec ss;
521     struct pa_sink *sink;
522     int ret;
523     assert(c && t && buf && fail && verbose);
524
525     if (!(fname = pa_tokenizer_get(t, 1)) || !(sink_name = pa_tokenizer_get(t, 2))) {
526         pa_strbuf_puts(buf, "You need to specify a file name and a sink name.\n");
527         return -1;
528     }
529
530     if (!(sink = pa_namereg_get(c, sink_name, PA_NAMEREG_SINK, 1))) {
531         pa_strbuf_puts(buf, "No sink by that name.\n");
532         return -1;
533     }
534
535     if (pa_sound_file_load(fname, &ss, &chunk, c->memblock_stat) < 0) {
536         pa_strbuf_puts(buf, "Failed to load sound file.\n");
537         return -1;
538     }
539
540     ret = pa_play_memchunk(sink, fname, &ss, &chunk, PA_VOLUME_NORM);
541     pa_memblock_unref(chunk.memblock);
542     return ret;
543 }
544
545 static int pa_cli_command_autoload_add(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
546     const char *devname, *module;
547     assert(c && t && buf && fail && verbose);
548
549     if (!(devname = pa_tokenizer_get(t, 1)) || !(module = pa_tokenizer_get(t, 2))) {
550         pa_strbuf_puts(buf, "You need to specify a device name, a module name and optionally module arguments\n");
551         return -1;
552     }
553     
554     pa_autoload_add(c, devname, strstr(pa_tokenizer_get(t, 0), "sink") ? PA_NAMEREG_SINK : PA_NAMEREG_SOURCE, module, pa_tokenizer_get(t, 3));
555     return 0;
556 }
557
558 static int pa_cli_command_autoload_remove(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
559     const char *devname;
560     assert(c && t && buf && fail && verbose);
561
562     if (!(devname = pa_tokenizer_get(t, 1))) {
563         pa_strbuf_puts(buf, "You need to specify a device name\n");
564         return -1;
565     }
566
567     if (pa_autoload_remove(c, devname, strstr(pa_tokenizer_get(t, 0), "sink") ? PA_NAMEREG_SINK : PA_NAMEREG_SOURCE) < 0) {
568         pa_strbuf_puts(buf, "Failed to remove autload entry\n");
569         return -1;
570     }
571
572     return 0;        
573 }
574
575 static int pa_cli_command_autoload_list(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
576     char *s;
577     assert(c && t);
578     s = pa_autoload_list_to_string(c);
579     assert(s);
580     pa_strbuf_puts(buf, s);
581     pa_xfree(s);
582     return 0;
583 }
584
585 int pa_cli_command_execute_line(struct pa_core *c, const char *s, struct pa_strbuf *buf, int *fail, int *verbose) {
586     const char *cs;
587     
588     cs = s+strspn(s, whitespace);
589
590     if (*cs == '#' || !*cs)
591         return 0;
592     else if (*cs == '.') {
593         static const char fail_meta[] = ".fail";
594         static const char nofail_meta[] = ".nofail";
595         static const char verbose_meta[] = ".verbose";
596         static const char noverbose_meta[] = ".noverbose";
597
598         if (!strcmp(cs, verbose_meta))
599             *verbose = 1;
600         else if (!strcmp(cs, noverbose_meta))
601             *verbose = 0;
602         else if (!strcmp(cs, fail_meta))
603             *fail = 1;
604         else if (!strcmp(cs, nofail_meta))
605             *fail = 0;
606         else {
607             size_t l;
608             static const char include_meta[] = ".include";
609             l = strcspn(cs, whitespace);
610
611             if (l == sizeof(include_meta)-1 && !strncmp(cs, include_meta, l)) {
612                 const char *filename = cs+l+strspn(cs+l, whitespace);
613
614                 if (pa_cli_command_execute_file(c, filename, buf, fail, verbose) < 0)
615                     if (*fail) return -1;
616             } else {
617                 pa_strbuf_printf(buf, "Invalid meta command: %s\n", cs);
618                 if (*fail) return -1;
619             }
620         }
621     } else {
622         const struct command*command;
623         int unknown = 1;
624         size_t l;
625         
626         l = strcspn(cs, whitespace);
627
628         for (command = commands; command->name; command++) 
629             if (strlen(command->name) == l && !strncmp(cs, command->name, l)) {
630                 int ret;
631                 struct pa_tokenizer *t = pa_tokenizer_new(cs, command->args);
632                 assert(t);
633                 ret = command->proc(c, t, buf, fail, verbose);
634                 pa_tokenizer_free(t);
635                 unknown = 0;
636
637                 if (ret < 0 && *fail)
638                     return -1;
639                 
640                 break;
641             }
642
643         if (unknown) {
644             pa_strbuf_printf(buf, "Unknown command: %s\n", cs);
645             if (*fail)
646                 return -1;
647         }
648     }
649
650     return 0;
651 }
652
653 int pa_cli_command_execute_file(struct pa_core *c, const char *fn, struct pa_strbuf *buf, int *fail, int *verbose) {
654     char line[256];
655     FILE *f = NULL;
656     int ret = -1;
657     assert(c && fn && buf);
658
659     if (!(f = fopen(fn, "r"))) {
660         pa_strbuf_printf(buf, "open('%s') failed: %s\n", fn, strerror(errno));
661         if (!*fail)
662             ret = 0;
663         goto fail;
664     }
665
666     if (*verbose)
667         pa_strbuf_printf(buf, "Executing file: '%s'\n", fn);
668
669     while (fgets(line, sizeof(line), f)) {
670         char *e = line + strcspn(line, linebreak);
671         *e = 0;
672
673         if (pa_cli_command_execute_line(c, line, buf, fail, verbose) < 0 && *fail)
674             goto fail;
675     }
676
677     if (*verbose)
678         pa_strbuf_printf(buf, "Executed file: '%s'\n", fn);
679
680     ret = 0;
681
682 fail:
683     if (f)
684         fclose(f);
685
686     return ret;
687 }
688
689 int pa_cli_command_execute(struct pa_core *c, const char *s, struct pa_strbuf *buf, int *fail, int *verbose) {
690     const char *p;
691     assert(c && s && buf && fail && verbose);
692
693     p = s;
694     while (*p) {
695         size_t l = strcspn(p, linebreak);
696         char *line = pa_xstrndup(p, l);
697         
698         if (pa_cli_command_execute_line(c, line, buf, fail, verbose) < 0&& *fail) {
699             pa_xfree(line);
700             return -1;
701         }
702         pa_xfree(line);
703
704         p += l;
705         p += strspn(p, linebreak);
706     }
707
708     return 0;
709 }