From d0f91b1cf79f4274a3e8cddef2c85040149457e4 Mon Sep 17 00:00:00 2001 From: Colin Guthrie Date: Sat, 4 Sep 2010 17:07:23 +0100 Subject: cli: Allow .include on directories as well as files. When .including a directory, all files with the extension '.pa' in that folder will be parsed in alphabetical order. --- src/pulsecore/cli-command.c | 94 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 81 insertions(+), 13 deletions(-) (limited to 'src/pulsecore/cli-command.c') diff --git a/src/pulsecore/cli-command.c b/src/pulsecore/cli-command.c index a5497d9d..d23331d9 100644 --- a/src/pulsecore/cli-command.c +++ b/src/pulsecore/cli-command.c @@ -30,6 +30,8 @@ #include #include #include +#include +#include #include #include @@ -53,6 +55,7 @@ #include #include #include +#include #include "cli-command.h" @@ -329,7 +332,7 @@ static int pa_cli_command_stat(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_b char ss[PA_SAMPLE_SPEC_SNPRINT_MAX]; char cm[PA_CHANNEL_MAP_SNPRINT_MAX]; char bytes[PA_BYTES_SNPRINT_MAX]; - const pa_mempool_stat *stat; + const pa_mempool_stat *mstat; unsigned k; pa_sink *def_sink; pa_source *def_source; @@ -348,23 +351,23 @@ static int pa_cli_command_stat(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_b pa_assert(buf); pa_assert(fail); - stat = pa_mempool_get_stat(c->mempool); + mstat = pa_mempool_get_stat(c->mempool); pa_strbuf_printf(buf, "Memory blocks currently allocated: %u, size: %s.\n", - (unsigned) pa_atomic_load(&stat->n_allocated), - pa_bytes_snprint(bytes, sizeof(bytes), (unsigned) pa_atomic_load(&stat->allocated_size))); + (unsigned) pa_atomic_load(&mstat->n_allocated), + pa_bytes_snprint(bytes, sizeof(bytes), (unsigned) pa_atomic_load(&mstat->allocated_size))); pa_strbuf_printf(buf, "Memory blocks allocated during the whole lifetime: %u, size: %s.\n", - (unsigned) pa_atomic_load(&stat->n_accumulated), - pa_bytes_snprint(bytes, sizeof(bytes), (unsigned) pa_atomic_load(&stat->accumulated_size))); + (unsigned) pa_atomic_load(&mstat->n_accumulated), + pa_bytes_snprint(bytes, sizeof(bytes), (unsigned) pa_atomic_load(&mstat->accumulated_size))); pa_strbuf_printf(buf, "Memory blocks imported from other processes: %u, size: %s.\n", - (unsigned) pa_atomic_load(&stat->n_imported), - pa_bytes_snprint(bytes, sizeof(bytes), (unsigned) pa_atomic_load(&stat->imported_size))); + (unsigned) pa_atomic_load(&mstat->n_imported), + pa_bytes_snprint(bytes, sizeof(bytes), (unsigned) pa_atomic_load(&mstat->imported_size))); pa_strbuf_printf(buf, "Memory blocks exported to other processes: %u, size: %s.\n", - (unsigned) pa_atomic_load(&stat->n_exported), - pa_bytes_snprint(bytes, sizeof(bytes), (unsigned) pa_atomic_load(&stat->exported_size))); + (unsigned) pa_atomic_load(&mstat->n_exported), + pa_bytes_snprint(bytes, sizeof(bytes), (unsigned) pa_atomic_load(&mstat->exported_size))); pa_strbuf_printf(buf, "Total sample cache size: %s.\n", pa_bytes_snprint(bytes, sizeof(bytes), (unsigned) pa_scache_total_size(c))); @@ -386,8 +389,8 @@ static int pa_cli_command_stat(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_b pa_strbuf_printf(buf, "Memory blocks of type %s: %u allocated/%u accumulated.\n", type_table[k], - (unsigned) pa_atomic_load(&stat->n_allocated_by_type[k]), - (unsigned) pa_atomic_load(&stat->n_accumulated_by_type[k])); + (unsigned) pa_atomic_load(&mstat->n_allocated_by_type[k]), + (unsigned) pa_atomic_load(&mstat->n_accumulated_by_type[k])); return 0; } @@ -1677,10 +1680,74 @@ int pa_cli_command_execute_line_stateful(pa_core *c, const char *s, pa_strbuf *b l = strcspn(cs, whitespace); if (l == sizeof(META_INCLUDE)-1 && !strncmp(cs, META_INCLUDE, l)) { + struct stat st; const char *filename = cs+l+strspn(cs+l, whitespace); - if (pa_cli_command_execute_file(c, filename, buf, fail) < 0) + + if (stat(filename, &st) < 0) { + pa_log_warn("stat('%s'): %s", filename, pa_cstrerror(errno)); if (*fail) return -1; + } else { + if (S_ISDIR(st.st_mode)) { + DIR *d; + + if (!(d = opendir(filename))) { + pa_log_warn("Failed to read '%s': %s", filename, pa_cstrerror(errno)); + if (*fail) + return -1; + } else { + unsigned i, count; + char **sorted_files; + struct dirent *de; + pa_bool_t failed = FALSE; + pa_dynarray *files = pa_dynarray_new(); + + while ((de = readdir(d))) { + char *extn; + size_t flen = strlen(de->d_name); + + if (flen < 4) + continue; + + extn = &de->d_name[flen-3]; + if (strncmp(extn, ".pa", 3) == 0) + pa_dynarray_append(files, pa_sprintf_malloc("%s" PA_PATH_SEP "%s", filename, de->d_name)); + } + + closedir(d); + + count = pa_dynarray_size(files); + sorted_files = pa_xnew(char*, count); + for (i = 0; i < count; ++i) + sorted_files[i] = pa_dynarray_get(files, i); + pa_dynarray_free(files, NULL, NULL); + + for (i = 0; i < count; ++i) { + for (unsigned j = 0; j < count; ++j) { + if (strcmp(sorted_files[i], sorted_files[j]) < 0) { + char *tmp = sorted_files[i]; + sorted_files[i] = sorted_files[j]; + sorted_files[j] = tmp; + } + } + } + + for (i = 0; i < count; ++i) { + if (!failed) { + if (pa_cli_command_execute_file(c, sorted_files[i], buf, fail) < 0 && *fail) + failed = TRUE; + } + + pa_xfree(sorted_files[i]); + } + pa_xfree(sorted_files); + if (failed) + return -1; + } + } else if (pa_cli_command_execute_file(c, filename, buf, fail) < 0 && *fail) { + return -1; + } + } } else if (l == sizeof(META_IFEXISTS)-1 && !strncmp(cs, META_IFEXISTS, l)) { if (!ifstate) { pa_strbuf_printf(buf, "Meta command %s is not valid in this context\n", cs); @@ -1811,6 +1878,7 @@ int pa_cli_command_execute_file(pa_core *c, const char *fn, pa_strbuf *buf, pa_b goto fail; } + pa_log_debug("Parsing script '%s'", fn); ret = pa_cli_command_execute_file_stream(c, f, buf, fail); fail: -- cgit