4 This file is part of polypaudio.
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.
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.
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
32 #include "cli-command.h"
37 #include "sink-input.h"
38 #include "source-output.h"
39 #include "tokenizer.h"
46 int (*proc) (struct pa_core *c, struct pa_tokenizer*t, struct pa_strbuf *buf, int *fail, int *verbose);
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);
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 }
96 static const char whitespace[] = " \t\n\r";
97 static const char linebreak[] = "\n\r";
99 static uint32_t parse_index(const char *n) {
102 index = strtol(n, &x, 0);
103 if (!x || *x != 0 || index < 0)
104 return (uint32_t) PA_IDXSET_INVALID;
106 return (uint32_t) index;
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);
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);
119 pa_strbuf_puts(buf, "Available commands:\n");
121 for (command = commands; command->name; command++)
123 pa_strbuf_printf(buf, " %-20s %s\n", command->name, command->help);
127 static int pa_cli_command_modules(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
130 s = pa_module_list_to_string(c);
132 pa_strbuf_puts(buf, s);
137 static int pa_cli_command_clients(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
140 s = pa_client_list_to_string(c);
142 pa_strbuf_puts(buf, s);
147 static int pa_cli_command_sinks(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
150 s = pa_sink_list_to_string(c);
152 pa_strbuf_puts(buf, s);
157 static int pa_cli_command_sources(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
160 s = pa_source_list_to_string(c);
162 pa_strbuf_puts(buf, s);
167 static int pa_cli_command_sink_inputs(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
170 s = pa_sink_input_list_to_string(c);
172 pa_strbuf_puts(buf, s);
177 static int pa_cli_command_source_outputs(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
180 s = pa_source_output_list_to_string(c);
182 pa_strbuf_puts(buf, s);
187 static int pa_cli_command_stat(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
189 pa_strbuf_printf(buf, "Memory blocks allocated: %u, total size: %u bytes.\n", pa_memblock_get_count(), pa_memblock_get_total());
193 static int pa_cli_command_info(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
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);
205 static int pa_cli_command_load(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
211 if (!(name = pa_tokenizer_get(t, 1))) {
212 pa_strbuf_puts(buf, "You need to specify the module name and optionally arguments.\n");
216 if (!(m = pa_module_load(c, name, pa_tokenizer_get(t, 2)))) {
217 pa_strbuf_puts(buf, "Module load failed.\n");
222 snprintf(txt, sizeof(txt), "Module successfully loaded, index: %u.\n", m->index);
223 pa_strbuf_puts(buf, txt);
228 static int pa_cli_command_unload(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
235 if (!(i = pa_tokenizer_get(t, 1))) {
236 pa_strbuf_puts(buf, "You need to specify the module index.\n");
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");
246 pa_module_unload_request(c, m);
250 static int pa_cli_command_sink_volume(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
253 struct pa_sink *sink;
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");
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");
266 volume = strtol(v, &x, 0);
267 if (!x || *x != 0 || volume < 0) {
268 pa_strbuf_puts(buf, "Failed to parse volume.\n");
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");
277 sink->volume = (uint32_t) volume;
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) {
283 struct pa_sink_input *si;
288 if (!(n = pa_tokenizer_get(t, 1))) {
289 pa_strbuf_puts(buf, "You need to specify a sink input by its index.\n");
293 if ((index = parse_index(n)) == PA_IDXSET_INVALID) {
294 pa_strbuf_puts(buf, "Failed to parse index.\n");
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");
304 volume = strtol(v, &x, 0);
305 if (!x || *x != 0 || volume < 0) {
306 pa_strbuf_puts(buf, "Failed to parse volume.\n");
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");
315 si->volume = (uint32_t) volume;
319 static int pa_cli_command_sink_default(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
321 struct pa_sink *sink;
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");
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");
334 c->default_sink_index = sink->index;
338 static int pa_cli_command_source_default(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
340 struct pa_source *source;
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");
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");
353 c->default_source_index = source->index;
357 static int pa_cli_command_kill_client(struct pa_core *c, struct pa_tokenizer *t, struct pa_strbuf *buf, int *fail, int *verbose) {
359 struct pa_client *client;
363 if (!(n = pa_tokenizer_get(t, 1))) {
364 pa_strbuf_puts(buf, "You need to specify a client by its index.\n");
368 if ((index = parse_index(n)) == PA_IDXSET_INVALID) {
369 pa_strbuf_puts(buf, "Failed to parse index.\n");
373 if (!(client = pa_idxset_get_by_index(c->clients, index))) {
374 pa_strbuf_puts(buf, "No client found by this index.\n");
378 pa_client_kill(client);
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) {
384 struct pa_sink_input *sink_input;
388 if (!(n = pa_tokenizer_get(t, 1))) {
389 pa_strbuf_puts(buf, "You need to specify a sink input by its index.\n");
393 if ((index = parse_index(n)) == PA_IDXSET_INVALID) {
394 pa_strbuf_puts(buf, "Failed to parse index.\n");
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");
403 pa_sink_input_kill(sink_input);
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) {
409 struct pa_source_output *source_output;
413 if (!(n = pa_tokenizer_get(t, 1))) {
414 pa_strbuf_puts(buf, "You need to specify a source output by its index.\n");
418 if ((index = parse_index(n)) == PA_IDXSET_INVALID) {
419 pa_strbuf_puts(buf, "Failed to parse index.\n");
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");
428 pa_source_output_kill(source_output);
432 int pa_cli_command_execute_line(struct pa_core *c, const char *s, struct pa_strbuf *buf, int *fail, int *verbose) {
435 cs = s+strspn(s, whitespace);
437 if (*cs == '#' || !*cs)
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";
445 if (!strcmp(cs, verbose_meta))
447 else if (!strcmp(cs, noverbose_meta))
449 else if (!strcmp(cs, fail_meta))
451 else if (!strcmp(cs, nofail_meta))
455 static const char include_meta[] = ".include";
456 l = strcspn(cs, whitespace);
458 if (l == sizeof(include_meta)-1 && !strncmp(cs, include_meta, l)) {
459 const char *filename = cs+l+strspn(cs+l, whitespace);
461 if (pa_cli_command_execute_file(c, filename, buf, fail, verbose) < 0)
462 if (*fail) return -1;
464 pa_strbuf_printf(buf, "Invalid meta command: %s\n", cs);
465 if (*fail) return -1;
469 const struct command*command;
473 l = strcspn(cs, whitespace);
475 for (command = commands; command->name; command++)
476 if (strlen(command->name) == l && !strncmp(cs, command->name, l)) {
478 struct pa_tokenizer *t = pa_tokenizer_new(cs, command->args);
480 ret = command->proc(c, t, buf, fail, verbose);
481 pa_tokenizer_free(t);
484 if (ret < 0 && *fail)
491 pa_strbuf_printf(buf, "Unknown command: %s\n", cs);
500 int pa_cli_command_execute_file(struct pa_core *c, const char *fn, struct pa_strbuf *buf, int *fail, int *verbose) {
504 assert(c && fn && buf);
506 if (!(f = fopen(fn, "r"))) {
507 pa_strbuf_printf(buf, "open('%s') failed: %s\n", fn, strerror(errno));
514 pa_strbuf_printf(buf, "Executing file: '%s'\n", fn);
516 while (fgets(line, sizeof(line), f)) {
517 char *e = line + strcspn(line, linebreak);
520 if (pa_cli_command_execute_line(c, line, buf, fail, verbose) < 0 && *fail)
525 pa_strbuf_printf(buf, "Executed file: '%s'\n", fn);
536 int pa_cli_command_execute(struct pa_core *c, const char *s, struct pa_strbuf *buf, int *fail, int *verbose) {
538 assert(c && s && buf && fail && verbose);
542 size_t l = strcspn(p, linebreak);
543 char *line = strndup(p, l);
546 if (pa_cli_command_execute_line(c, line, buf, fail, verbose) < 0&& *fail) {
553 p += strspn(p, linebreak);