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