7 #include "cli-command.h"
12 #include "sink-input.h"
13 #include "source-output.h"
14 #include "tokenizer.h"
21 int (*proc
) (struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
);
26 static int pa_cli_command_exit(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
);
27 static int pa_cli_command_help(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
);
28 static int pa_cli_command_modules(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
);
29 static int pa_cli_command_clients(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
);
30 static int pa_cli_command_sinks(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
);
31 static int pa_cli_command_sources(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
);
32 static int pa_cli_command_sink_inputs(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
);
33 static int pa_cli_command_source_outputs(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
);
34 static int pa_cli_command_stat(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
);
35 static int pa_cli_command_info(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
);
36 static int pa_cli_command_load(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
);
37 static int pa_cli_command_unload(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
);
38 static int pa_cli_command_sink_volume(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
);
39 static int pa_cli_command_sink_input_volume(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
);
40 static int pa_cli_command_sink_default(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
);
41 static int pa_cli_command_source_default(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
);
42 static int pa_cli_command_kill_client(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
);
43 static int pa_cli_command_kill_sink_input(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
);
44 static int pa_cli_command_kill_source_output(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
);
46 static const struct command commands
[] = {
47 { "exit", pa_cli_command_exit
, "Terminate the daemon", 1 },
48 { "help", pa_cli_command_help
, "Show this help", 1 },
49 { "modules", pa_cli_command_modules
, "List loaded modules", 1 },
50 { "sinks", pa_cli_command_sinks
, "List loaded sinks", 1 },
51 { "sources", pa_cli_command_sources
, "List loaded sources", 1 },
52 { "clients", pa_cli_command_clients
, "List loaded clients", 1 },
53 { "sink_inputs", pa_cli_command_sink_inputs
, "List sink inputs", 1 },
54 { "source_outputs", pa_cli_command_source_outputs
, "List source outputs", 1 },
55 { "stat", pa_cli_command_stat
, "Show memory block statistics", 1 },
56 { "info", pa_cli_command_info
, "Show comprehensive status", 1 },
57 { "ls", pa_cli_command_info
, NULL
, 1 },
58 { "list", pa_cli_command_info
, NULL
, 1 },
59 { "load", pa_cli_command_load
, "Load a module (args: name, arguments)", 3},
60 { "unload", pa_cli_command_unload
, "Unload a module (args: index)", 2},
61 { "sink_volume", pa_cli_command_sink_volume
, "Set the volume of a sink (args: index|name, volume)", 3},
62 { "sink_input_volume", pa_cli_command_sink_input_volume
, "Set the volume of a sink input (args: index|name, volume)", 3},
63 { "sink_default", pa_cli_command_sink_default
, "Set the default sink (args: index|name)", 2},
64 { "source_default", pa_cli_command_source_default
, "Set the default source (args: index|name)", 2},
65 { "kill_client", pa_cli_command_kill_client
, "Kill a client (args: index)", 2},
66 { "kill_sink_input", pa_cli_command_kill_sink_input
, "Kill a sink input (args: index)", 2},
67 { "kill_source_output", pa_cli_command_kill_source_output
, "Kill a source output (args: index)", 2},
68 { NULL
, NULL
, NULL
, 0 }
71 static const char whitespace
[] = " \t\n\r";
72 static const char linebreak
[] = "\n\r";
74 static uint32_t parse_index(const char *n
) {
77 index
= strtol(n
, &x
, 0);
78 if (!x
|| *x
!= 0 || index
< 0)
79 return (uint32_t) PA_IDXSET_INVALID
;
81 return (uint32_t) index
;
84 static int pa_cli_command_exit(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
) {
85 assert(c
&& c
->mainloop
&& t
);
86 c
->mainloop
->quit(c
->mainloop
, 0);
90 static int pa_cli_command_help(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
) {
91 const struct command
*command
;
92 assert(c
&& t
&& buf
);
94 pa_strbuf_puts(buf
, "Available commands:\n");
96 for (command
= commands
; command
->name
; command
++)
98 pa_strbuf_printf(buf
, " %-20s %s\n", command
->name
, command
->help
);
102 static int pa_cli_command_modules(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
) {
105 s
= pa_module_list_to_string(c
);
107 pa_strbuf_puts(buf
, s
);
112 static int pa_cli_command_clients(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
) {
115 s
= pa_client_list_to_string(c
);
117 pa_strbuf_puts(buf
, s
);
122 static int pa_cli_command_sinks(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
) {
125 s
= pa_sink_list_to_string(c
);
127 pa_strbuf_puts(buf
, s
);
132 static int pa_cli_command_sources(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
) {
135 s
= pa_source_list_to_string(c
);
137 pa_strbuf_puts(buf
, s
);
142 static int pa_cli_command_sink_inputs(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
) {
145 s
= pa_sink_input_list_to_string(c
);
147 pa_strbuf_puts(buf
, s
);
152 static int pa_cli_command_source_outputs(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
) {
155 s
= pa_source_output_list_to_string(c
);
157 pa_strbuf_puts(buf
, s
);
162 static int pa_cli_command_stat(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
) {
164 pa_strbuf_printf(buf
, "Memory blocks allocated: %u, total size: %u bytes.\n", pa_memblock_get_count(), pa_memblock_get_total());
168 static int pa_cli_command_info(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
) {
170 pa_cli_command_stat(c
, t
, buf
, fail
, verbose
);
171 pa_cli_command_modules(c
, t
, buf
, fail
, verbose
);
172 pa_cli_command_sinks(c
, t
, buf
, fail
, verbose
);
173 pa_cli_command_sources(c
, t
, buf
, fail
, verbose
);
174 pa_cli_command_clients(c
, t
, buf
, fail
, verbose
);
175 pa_cli_command_sink_inputs(c
, t
, buf
, fail
, verbose
);
176 pa_cli_command_source_outputs(c
, t
, buf
, fail
, verbose
);
180 static int pa_cli_command_load(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
) {
186 if (!(name
= pa_tokenizer_get(t
, 1))) {
187 pa_strbuf_puts(buf
, "You need to specify the module name and optionally arguments.\n");
191 if (!(m
= pa_module_load(c
, name
, pa_tokenizer_get(t
, 2)))) {
192 pa_strbuf_puts(buf
, "Module load failed.\n");
197 snprintf(txt
, sizeof(txt
), "Module successfully loaded, index: %u.\n", m
->index
);
198 pa_strbuf_puts(buf
, txt
);
203 static int pa_cli_command_unload(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
) {
210 if (!(i
= pa_tokenizer_get(t
, 1))) {
211 pa_strbuf_puts(buf
, "You need to specify the module index.\n");
215 index
= (uint32_t) strtoul(i
, &e
, 10);
216 if (*e
|| !(m
= pa_idxset_get_by_index(c
->modules
, index
))) {
217 pa_strbuf_puts(buf
, "Invalid module index.\n");
221 pa_module_unload_request(c
, m
);
225 static int pa_cli_command_sink_volume(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
) {
228 struct pa_sink
*sink
;
231 if (!(n
= pa_tokenizer_get(t
, 1))) {
232 pa_strbuf_puts(buf
, "You need to specify a sink either by its name or its index.\n");
236 if (!(v
= pa_tokenizer_get(t
, 2))) {
237 pa_strbuf_puts(buf
, "You need to specify a volume >= 0. (0 is muted, 0x100 is normal volume)\n");
241 volume
= strtol(v
, &x
, 0);
242 if (!x
|| *x
!= 0 || volume
< 0) {
243 pa_strbuf_puts(buf
, "Failed to parse volume.\n");
247 if (!(sink
= pa_namereg_get(c
, n
, PA_NAMEREG_SINK
))) {
248 pa_strbuf_puts(buf
, "No sink found by this name or index.\n");
252 sink
->volume
= (uint32_t) volume
;
256 static int pa_cli_command_sink_input_volume(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
) {
258 struct pa_sink_input
*si
;
263 if (!(n
= pa_tokenizer_get(t
, 1))) {
264 pa_strbuf_puts(buf
, "You need to specify a sink input by its index.\n");
268 if ((index
= parse_index(n
)) == PA_IDXSET_INVALID
) {
269 pa_strbuf_puts(buf
, "Failed to parse index.\n");
273 if (!(v
= pa_tokenizer_get(t
, 2))) {
274 pa_strbuf_puts(buf
, "You need to specify a volume >= 0. (0 is muted, 0x100 is normal volume)\n");
279 volume
= strtol(v
, &x
, 0);
280 if (!x
|| *x
!= 0 || volume
< 0) {
281 pa_strbuf_puts(buf
, "Failed to parse volume.\n");
285 if (!(si
= pa_idxset_get_by_index(c
->sink_inputs
, (uint32_t) index
))) {
286 pa_strbuf_puts(buf
, "No sink input found with this index.\n");
290 si
->volume
= (uint32_t) volume
;
294 static int pa_cli_command_sink_default(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
) {
296 struct pa_sink
*sink
;
299 if (!(n
= pa_tokenizer_get(t
, 1))) {
300 pa_strbuf_puts(buf
, "You need to specify a sink either by its name or its index.\n");
304 if (!(sink
= pa_namereg_get(c
, n
, PA_NAMEREG_SINK
))) {
305 pa_strbuf_puts(buf
, "No sink found by this name or index.\n");
309 c
->default_sink_index
= sink
->index
;
313 static int pa_cli_command_source_default(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
) {
315 struct pa_source
*source
;
318 if (!(n
= pa_tokenizer_get(t
, 1))) {
319 pa_strbuf_puts(buf
, "You need to specify a source either by its name or its index.\n");
323 if (!(source
= pa_namereg_get(c
, n
, PA_NAMEREG_SOURCE
))) {
324 pa_strbuf_puts(buf
, "No source found by this name or index.\n");
328 c
->default_source_index
= source
->index
;
332 static int pa_cli_command_kill_client(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
) {
334 struct pa_client
*client
;
338 if (!(n
= pa_tokenizer_get(t
, 1))) {
339 pa_strbuf_puts(buf
, "You need to specify a client by its index.\n");
343 if ((index
= parse_index(n
)) == PA_IDXSET_INVALID
) {
344 pa_strbuf_puts(buf
, "Failed to parse index.\n");
348 if (!(client
= pa_idxset_get_by_index(c
->clients
, index
))) {
349 pa_strbuf_puts(buf
, "No client found by this index.\n");
353 pa_client_kill(client
);
357 static int pa_cli_command_kill_sink_input(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
) {
359 struct pa_sink_input
*sink_input
;
363 if (!(n
= pa_tokenizer_get(t
, 1))) {
364 pa_strbuf_puts(buf
, "You need to specify a sink input 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 (!(sink_input
= pa_idxset_get_by_index(c
->sink_inputs
, index
))) {
374 pa_strbuf_puts(buf
, "No sink input found by this index.\n");
378 pa_sink_input_kill(sink_input
);
382 static int pa_cli_command_kill_source_output(struct pa_core
*c
, struct pa_tokenizer
*t
, struct pa_strbuf
*buf
, int *fail
, int *verbose
) {
384 struct pa_source_output
*source_output
;
388 if (!(n
= pa_tokenizer_get(t
, 1))) {
389 pa_strbuf_puts(buf
, "You need to specify a source output 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 (!(source_output
= pa_idxset_get_by_index(c
->source_outputs
, index
))) {
399 pa_strbuf_puts(buf
, "No source output found by this index.\n");
403 pa_source_output_kill(source_output
);
407 int pa_cli_command_execute_line(struct pa_core
*c
, const char *s
, struct pa_strbuf
*buf
, int *fail
, int *verbose
) {
410 cs
= s
+strspn(s
, whitespace
);
412 if (*cs
== '#' || !*cs
)
414 else if (*cs
== '.') {
415 static const char fail_meta
[] = ".fail";
416 static const char nofail_meta
[] = ".nofail";
417 static const char verbose_meta
[] = ".verbose";
418 static const char noverbose_meta
[] = ".noverbose";
420 if (!strcmp(cs
, verbose_meta
))
422 else if (!strcmp(cs
, noverbose_meta
))
424 else if (!strcmp(cs
, fail_meta
))
426 else if (!strcmp(cs
, nofail_meta
))
430 static const char include_meta
[] = ".include";
431 l
= strcspn(cs
, whitespace
);
433 if (l
== sizeof(include_meta
)-1 && !strncmp(cs
, include_meta
, l
)) {
434 const char *filename
= cs
+l
+strspn(cs
+l
, whitespace
);
436 if (pa_cli_command_execute_file(c
, filename
, buf
, fail
, verbose
) < 0)
437 if (*fail
) return -1;
439 pa_strbuf_printf(buf
, "Invalid meta command: %s\n", cs
);
440 if (*fail
) return -1;
444 const struct command
*command
;
448 l
= strcspn(cs
, whitespace
);
450 for (command
= commands
; command
->name
; command
++)
451 if (strlen(command
->name
) == l
&& !strncmp(cs
, command
->name
, l
)) {
453 struct pa_tokenizer
*t
= pa_tokenizer_new(cs
, command
->args
);
455 ret
= command
->proc(c
, t
, buf
, fail
, verbose
);
456 pa_tokenizer_free(t
);
459 if (ret
< 0 && *fail
)
466 pa_strbuf_printf(buf
, "Unknown command: %s\n", cs
);
475 int pa_cli_command_execute_file(struct pa_core
*c
, const char *fn
, struct pa_strbuf
*buf
, int *fail
, int *verbose
) {
479 assert(c
&& fn
&& buf
);
481 if (!(f
= fopen(fn
, "r"))) {
482 pa_strbuf_printf(buf
, "open('%s') failed: %s\n", fn
, strerror(errno
));
489 pa_strbuf_printf(buf
, "Executing file: '%s'\n", fn
);
491 while (fgets(line
, sizeof(line
), f
)) {
492 char *e
= line
+ strcspn(line
, linebreak
);
495 if (pa_cli_command_execute_line(c
, line
, buf
, fail
, verbose
) < 0 && *fail
)
500 pa_strbuf_printf(buf
, "Executed file: '%s'\n", fn
);
511 int pa_cli_command_execute(struct pa_core
*c
, const char *s
, struct pa_strbuf
*buf
, int *fail
, int *verbose
) {
513 assert(c
&& s
&& buf
&& fail
&& verbose
);
517 size_t l
= strcspn(p
, linebreak
);
518 char *line
= strndup(p
, l
);
521 if (pa_cli_command_execute_line(c
, line
, buf
, fail
, verbose
) < 0&& *fail
) {
528 p
+= strspn(p
, linebreak
);