rename src to polyp
[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 "clitext.h"
43
44 struct command {
45     const char *name;
46     int (*proc) (struct pa_core *c, struct pa_tokenizer*t, struct pa_strbuf *buf, int *fail, int *verbose);
47     const char *help;
48     unsigned args;
49 };
50
51 static int pa_cli_command_exit(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
52 static int pa_cli_command_help(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
53 static int pa_cli_command_modules(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
54 static int pa_cli_command_clients(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
55 static int pa_cli_command_sinks(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
56 static int pa_cli_command_sources(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
57 static int pa_cli_command_sink_inputs(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
58 static int pa_cli_command_source_outputs(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
59 static int pa_cli_command_stat(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
60 static int pa_cli_command_info(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
61 static int pa_cli_command_load(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
62 static int pa_cli_command_unload(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
63 static int pa_cli_command_sink_volume(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
64 static int pa_cli_command_sink_input_volume(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
65 static int pa_cli_command_sink_default(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
66 static int pa_cli_command_source_default(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
67 static int pa_cli_command_kill_client(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
68 static int pa_cli_command_kill_sink_input(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
69 static int pa_cli_command_kill_source_output(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose);
70
71 static const struct command commands[] = {
72     { "exit",                    pa_cli_command_exit,               "Terminate the daemon",         1 },
73     { "help",                    pa_cli_command_help,               "Show this help",               1 },
74     { "modules",                 pa_cli_command_modules,            "List loaded modules",          1 },
75     { "sinks",                   pa_cli_command_sinks,              "List loaded sinks",            1 },
76     { "sources",                 pa_cli_command_sources,            "List loaded sources",          1 },
77     { "clients",                 pa_cli_command_clients,            "List loaded clients",          1 },
78     { "sink_inputs",             pa_cli_command_sink_inputs,        "List sink inputs",             1 },
79     { "source_outputs",          pa_cli_command_source_outputs,     "List source outputs",          1 },
80     { "stat",                    pa_cli_command_stat,               "Show memory block statistics", 1 },
81     { "info",                    pa_cli_command_info,               "Show comprehensive status",    1 },
82     { "ls",                      pa_cli_command_info,               NULL,                           1 },
83     { "list",                    pa_cli_command_info,               NULL,                           1 },
84     { "load",                    pa_cli_command_load,               "Load a module (args: name, arguments)",                     3},
85     { "unload",                  pa_cli_command_unload,             "Unload a module (args: index)",                             2},
86     { "sink_volume",             pa_cli_command_sink_volume,        "Set the volume of a sink (args: index|name, volume)",             3},
87     { "sink_input_volume",       pa_cli_command_sink_input_volume,  "Set the volume of a sink input (args: index|name, volume)", 3},
88     { "sink_default",            pa_cli_command_sink_default,       "Set the default sink (args: index|name)", 2},
89     { "source_default",          pa_cli_command_source_default,     "Set the default source (args: index|name)", 2},
90     { "kill_client",             pa_cli_command_kill_client,        "Kill a client (args: index)", 2},
91     { "kill_sink_input",         pa_cli_command_kill_sink_input,    "Kill a sink input (args: index)", 2},
92     { "kill_source_output",      pa_cli_command_kill_source_output, "Kill a source output (args: index)", 2},
93     { NULL, NULL, NULL, 0 }
94 };
95
96 static const char whitespace[] = " \t\n\r";
97 static const char linebreak[] = "\n\r";
98
99 static uint32_t parse_index(const char *n) {
100     long index;
101     char *x;
102     index = strtol(n, &x, 0);
103     if (!x || *x != 0 || index < 0)
104         return (uint32_t) PA_IDXSET_INVALID;
105
106     return (uint32_t) index;
107 }
108
109 static int pa_cli_command_exit(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
110     assert(c && c->mainloop && t);
111     c->mainloop->quit(c->mainloop, 0);
112     return 0;
113 }
114
115 static int pa_cli_command_help(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
116     const struct command*command;
117     assert(c && t && buf);
118
119     pa_strbuf_puts(buf, "Available commands:\n");
120     
121     for (command = commands; command->name; command++)
122         if (command->help)
123             pa_strbuf_printf(buf, "    %-20s %s\n", command->name, command->help);
124     return 0;
125 }
126
127 static int pa_cli_command_modules(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
128     char *s;
129     assert(c && t);
130     s = pa_module_list_to_string(c);
131     assert(s);
132     pa_strbuf_puts(buf, s);
133     free(s);
134     return 0;
135 }
136
137 static int pa_cli_command_clients(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
138     char *s;
139     assert(c && t);
140     s = pa_client_list_to_string(c);
141     assert(s);
142     pa_strbuf_puts(buf, s);
143     free(s);
144     return 0;
145 }
146
147 static int pa_cli_command_sinks(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
148     char *s;
149     assert(c && t);
150     s = pa_sink_list_to_string(c);
151     assert(s);
152     pa_strbuf_puts(buf, s);
153     free(s);
154     return 0;
155 }
156
157 static int pa_cli_command_sources(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
158     char *s;
159     assert(c && t);
160     s = pa_source_list_to_string(c);
161     assert(s);
162     pa_strbuf_puts(buf, s);
163     free(s);
164     return 0;
165 }
166
167 static int pa_cli_command_sink_inputs(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
168     char *s;
169     assert(c && t);
170     s = pa_sink_input_list_to_string(c);
171     assert(s);
172     pa_strbuf_puts(buf, s);
173     free(s);
174     return 0;
175 }
176
177 static int pa_cli_command_source_outputs(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
178     char *s;
179     assert(c && t);
180     s = pa_source_output_list_to_string(c);
181     assert(s);
182     pa_strbuf_puts(buf, s);
183     free(s);
184     return 0;
185 }
186
187 static int pa_cli_command_stat(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
188     assert(c && t);
189     pa_strbuf_printf(buf, "Memory blocks allocated: %u, total size: %u bytes.\n", pa_memblock_get_count(), pa_memblock_get_total());
190     return 0;
191 }
192
193 static int pa_cli_command_info(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
194     assert(c && t);
195     pa_cli_command_stat(c, t, buf, fail, verbose);
196     pa_cli_command_modules(c, t, buf, fail, verbose);
197     pa_cli_command_sinks(c, t, buf, fail, verbose);
198     pa_cli_command_sources(c, t, buf, fail, verbose);
199     pa_cli_command_clients(c, t, buf, fail, verbose);
200     pa_cli_command_sink_inputs(c, t, buf, fail, verbose);
201     pa_cli_command_source_outputs(c, t, buf, fail, verbose);
202     return 0;
203 }
204
205 static int pa_cli_command_load(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
206     struct pa_module *m;
207     const char *name;
208     char txt[256];
209     assert(c && t);
210
211     if (!(name = pa_tokenizer_get(t, 1))) {
212         pa_strbuf_puts(buf, "You need to specify the module name and optionally arguments.\n");
213         return -1;
214     }
215     
216     if (!(m = pa_module_load(c, name,  pa_tokenizer_get(t, 2)))) {
217         pa_strbuf_puts(buf, "Module load failed.\n");
218         return -1;
219     }
220
221     if (*verbose) {
222         snprintf(txt, sizeof(txt), "Module successfully loaded, index: %u.\n", m->index);
223         pa_strbuf_puts(buf, txt);
224     }
225     return 0;
226 }
227
228 static int pa_cli_command_unload(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
229     struct pa_module *m;
230     uint32_t index;
231     const char *i;
232     char *e;
233     assert(c && t);
234
235     if (!(i = pa_tokenizer_get(t, 1))) {
236         pa_strbuf_puts(buf, "You need to specify the module index.\n");
237         return -1;
238     }
239
240     index = (uint32_t) strtoul(i, &e, 10);
241     if (*e || !(m = pa_idxset_get_by_index(c->modules, index))) {
242         pa_strbuf_puts(buf, "Invalid module index.\n");
243         return -1;
244     }
245
246     pa_module_unload_request(c, m);
247     return 0;
248 }
249
250 static int pa_cli_command_sink_volume(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
251     const char *n, *v;
252     char *x = NULL;
253     struct pa_sink *sink;
254     long volume;
255
256     if (!(n = pa_tokenizer_get(t, 1))) {
257         pa_strbuf_puts(buf, "You need to specify a sink either by its name or its index.\n");
258         return -1;
259     }
260
261     if (!(v = pa_tokenizer_get(t, 2))) {
262         pa_strbuf_puts(buf, "You need to specify a volume >= 0. (0 is muted, 0x100 is normal volume)\n");
263         return -1;
264     }
265
266     volume = strtol(v, &x, 0);
267     if (!x || *x != 0 || volume < 0) {
268         pa_strbuf_puts(buf, "Failed to parse volume.\n");
269         return -1;
270     }
271
272     if (!(sink = pa_namereg_get(c, n, PA_NAMEREG_SINK))) {
273         pa_strbuf_puts(buf, "No sink found by this name or index.\n");
274         return -1;
275     }
276
277     sink->volume = (uint32_t) volume;
278     return 0;
279 }
280
281 static int pa_cli_command_sink_input_volume(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
282     const char *n, *v;
283     struct pa_sink_input *si;
284     long volume;
285     uint32_t index;
286     char *x;
287
288     if (!(n = pa_tokenizer_get(t, 1))) {
289         pa_strbuf_puts(buf, "You need to specify a sink input by its index.\n");
290         return -1;
291     }
292
293     if ((index = parse_index(n)) == PA_IDXSET_INVALID) {
294         pa_strbuf_puts(buf, "Failed to parse index.\n");
295         return -1;
296     }
297
298     if (!(v = pa_tokenizer_get(t, 2))) {
299         pa_strbuf_puts(buf, "You need to specify a volume >= 0. (0 is muted, 0x100 is normal volume)\n");
300         return -1;
301     }
302
303     x = NULL;
304     volume = strtol(v, &x, 0);
305     if (!x || *x != 0 || volume < 0) {
306         pa_strbuf_puts(buf, "Failed to parse volume.\n");
307         return -1;
308     }
309
310     if (!(si = pa_idxset_get_by_index(c->sink_inputs, (uint32_t) index))) {
311         pa_strbuf_puts(buf, "No sink input found with this index.\n");
312         return -1;
313     }
314
315     si->volume = (uint32_t) volume;
316     return 0;
317 }
318
319 static int pa_cli_command_sink_default(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
320     const char *n;
321     struct pa_sink *sink;
322     assert(c && t);
323
324     if (!(n = pa_tokenizer_get(t, 1))) {
325         pa_strbuf_puts(buf, "You need to specify a sink either by its name or its index.\n");
326         return -1;
327     }
328
329     if (!(sink = pa_namereg_get(c, n, PA_NAMEREG_SINK))) {
330         pa_strbuf_puts(buf, "No sink found by this name or index.\n");
331         return -1;
332     }
333
334     c->default_sink_index = sink->index;
335     return 0;
336 }
337
338 static int pa_cli_command_source_default(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
339     const char *n;
340     struct pa_source *source;
341     assert(c && t);
342
343     if (!(n = pa_tokenizer_get(t, 1))) {
344         pa_strbuf_puts(buf, "You need to specify a source either by its name or its index.\n");
345         return -1;
346     }
347
348     if (!(source = pa_namereg_get(c, n, PA_NAMEREG_SOURCE))) {
349         pa_strbuf_puts(buf, "No source found by this name or index.\n");
350         return -1;
351     }
352
353     c->default_source_index = source->index;
354     return 0;
355 }
356
357 static int pa_cli_command_kill_client(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
358     const char *n;
359     struct pa_client *client;
360     uint32_t index;
361     assert(c && t);
362
363     if (!(n = pa_tokenizer_get(t, 1))) {
364         pa_strbuf_puts(buf, "You need to specify a client by its index.\n");
365         return -1;
366     }
367
368     if ((index = parse_index(n)) == PA_IDXSET_INVALID) {
369         pa_strbuf_puts(buf, "Failed to parse index.\n");
370         return -1;
371     }
372
373     if (!(client = pa_idxset_get_by_index(c->clients, index))) {
374         pa_strbuf_puts(buf, "No client found by this index.\n");
375         return -1;
376     }
377
378     pa_client_kill(client);
379     return 0;
380 }
381
382 static int pa_cli_command_kill_sink_input(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
383     const char *n;
384     struct pa_sink_input *sink_input;
385     uint32_t index;
386     assert(c && t);
387
388     if (!(n = pa_tokenizer_get(t, 1))) {
389         pa_strbuf_puts(buf, "You need to specify a sink input by its index.\n");
390         return -1;
391     }
392
393     if ((index = parse_index(n)) == PA_IDXSET_INVALID) {
394         pa_strbuf_puts(buf, "Failed to parse index.\n");
395         return -1;
396     }
397
398     if (!(sink_input = pa_idxset_get_by_index(c->sink_inputs, index))) {
399         pa_strbuf_puts(buf, "No sink input found by this index.\n");
400         return -1;
401     }
402
403     pa_sink_input_kill(sink_input);
404     return 0;
405 }
406
407 static int pa_cli_command_kill_source_output(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
408     const char *n;
409     struct pa_source_output *source_output;
410     uint32_t index;
411     assert(c && t);
412
413     if (!(n = pa_tokenizer_get(t, 1))) {
414         pa_strbuf_puts(buf, "You need to specify a source output by its index.\n");
415         return -1;
416     }
417
418     if ((index = parse_index(n)) == PA_IDXSET_INVALID) {
419         pa_strbuf_puts(buf, "Failed to parse index.\n");
420         return -1;
421     }
422
423     if (!(source_output = pa_idxset_get_by_index(c->source_outputs, index))) {
424         pa_strbuf_puts(buf, "No source output found by this index.\n");
425         return -1;
426     }
427
428     pa_source_output_kill(source_output);
429     return 0;
430 }
431
432 int pa_cli_command_execute_line(struct pa_core *c, const char *s, struct pa_strbuf *buf, int *fail, int *verbose) {
433     const char *cs;
434     
435     cs = s+strspn(s, whitespace);
436
437     if (*cs == '#' || !*cs)
438         return 0;
439     else if (*cs == '.') {
440         static const char fail_meta[] = ".fail";
441         static const char nofail_meta[] = ".nofail";
442         static const char verbose_meta[] = ".verbose";
443         static const char noverbose_meta[] = ".noverbose";
444
445         if (!strcmp(cs, verbose_meta))
446             *verbose = 1;
447         else if (!strcmp(cs, noverbose_meta))
448             *verbose = 0;
449         else if (!strcmp(cs, fail_meta))
450             *fail = 1;
451         else if (!strcmp(cs, nofail_meta))
452             *fail = 0;
453         else {
454             size_t l;
455             static const char include_meta[] = ".include";
456             l = strcspn(cs, whitespace);
457
458             if (l == sizeof(include_meta)-1 && !strncmp(cs, include_meta, l)) {
459                 const char *filename = cs+l+strspn(cs+l, whitespace);
460
461                 if (pa_cli_command_execute_file(c, filename, buf, fail, verbose) < 0)
462                     if (*fail) return -1;
463             } else {
464                 pa_strbuf_printf(buf, "Invalid meta command: %s\n", cs);
465                 if (*fail) return -1;
466             }
467         }
468     } else {
469         const struct command*command;
470         int unknown = 1;
471         size_t l;
472         
473         l = strcspn(cs, whitespace);
474
475         for (command = commands; command->name; command++) 
476             if (strlen(command->name) == l && !strncmp(cs, command->name, l)) {
477                 int ret;
478                 struct pa_tokenizer *t = pa_tokenizer_new(cs, command->args);
479                 assert(t);
480                 ret = command->proc(c, t, buf, fail, verbose);
481                 pa_tokenizer_free(t);
482                 unknown = 0;
483
484                 if (ret < 0 && *fail)
485                     return -1;
486                 
487                 break;
488             }
489
490         if (unknown) {
491             pa_strbuf_printf(buf, "Unknown command: %s\n", cs);
492             if (*fail)
493                 return -1;
494         }
495     }
496
497     return 0;
498 }
499
500 int pa_cli_command_execute_file(struct pa_core *c, const char *fn, struct pa_strbuf *buf, int *fail, int *verbose) {
501     char line[256];
502     FILE *f = NULL;
503     int ret = -1;
504     assert(c && fn && buf);
505
506     if (!(f = fopen(fn, "r"))) {
507         pa_strbuf_printf(buf, "open('%s') failed: %s\n", fn, strerror(errno));
508         if (!*fail)
509             ret = 0;
510         goto fail;
511     }
512
513     if (*verbose)
514         pa_strbuf_printf(buf, "Executing file: '%s'\n", fn);
515
516     while (fgets(line, sizeof(line), f)) {
517         char *e = line + strcspn(line, linebreak);
518         *e = 0;
519
520         if (pa_cli_command_execute_line(c, line, buf, fail, verbose) < 0 && *fail)
521             goto fail;
522     }
523
524     if (*verbose)
525         pa_strbuf_printf(buf, "Executed file: '%s'\n", fn);
526
527     ret = 0;
528
529 fail:
530     if (f)
531         fclose(f);
532
533     return ret;
534 }
535
536 int pa_cli_command_execute(struct pa_core *c, const char *s, struct pa_strbuf *buf, int *fail, int *verbose) {
537     const char *p;
538     assert(c && s && buf && fail && verbose);
539
540     p = s;
541     while (*p) {
542         size_t l = strcspn(p, linebreak);
543         char *line = strndup(p, l);
544         assert(line);
545         
546         if (pa_cli_command_execute_line(c, line, buf, fail, verbose) < 0&& *fail) {
547             free(line);
548             return -1;
549         }
550         free(line);
551
552         p += l;
553         p += strspn(p, linebreak);
554     }
555
556     return 0;
557 }