From 58cd8d022f045bcf19d40253c20fc4439f60d5d8 Mon Sep 17 00:00:00 2001 From: "Rafael G. Martins" Date: Sat, 15 Oct 2016 02:53:24 +0200 Subject: blogc: add cli option to read list of source files from stdin this patch adds `-i` option to command line. it will instruct blogc to read stdin and parse it as a file where each source file is a line and empty lines and lines starting with `#` are ignored. this patch makes it possible to use blogc to build big blogs with lots of pages, that would hit the operating system max command line length when calling blogc to build pages. --- src/blogc/main.c | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/blogc/main.c b/src/blogc/main.c index 35d9f69..35bcd88 100644 --- a/src/blogc/main.c +++ b/src/blogc/main.c @@ -27,6 +27,7 @@ #include "loader.h" #include "renderer.h" #include "../common/error.h" +#include "../common/file.h" #include "../common/utf8.h" #include "../common/utils.h" @@ -40,7 +41,7 @@ blogc_print_help(void) { printf( "usage:\n" - " blogc [-h] [-v] [-d] [-l] [-D KEY=VALUE ...] [-p KEY] [-t TEMPLATE]\n" + " blogc [-h] [-v] [-d] [-i] [-l] [-D KEY=VALUE ...] [-p KEY] [-t TEMPLATE]\n" " [-o OUTPUT] [SOURCE ...] - A blog compiler.\n" "\n" "positional arguments:\n" @@ -50,6 +51,7 @@ blogc_print_help(void) " -h show this help message and exit\n" " -v show version and exit\n" " -d enable debug\n" + " -i read list of source files from standard input\n" " -l build listing page, from multiple source files\n" " -D KEY=VALUE set global configuration parameter\n" " -p KEY show the value of a global configuration parameter\n" @@ -63,7 +65,7 @@ static void blogc_print_usage(void) { printf( - "usage: blogc [-h] [-v] [-d] [-l] [-D KEY=VALUE ...] [-p KEY] [-t TEMPLATE]\n" + "usage: blogc [-h] [-v] [-d] [-i] [-l] [-D KEY=VALUE ...] [-p KEY] [-t TEMPLATE]\n" " [-o OUTPUT] [SOURCE ...]\n"); } @@ -103,6 +105,22 @@ blogc_mkdir_recursive(const char *filename) } +static bc_slist_t* +blogc_read_stdin_to_list(bc_slist_t *l) +{ + char buffer[4096]; + while (NULL != fgets(buffer, 4096, stdin)) { + char *tmp = bc_str_strip(buffer); + if (tmp[0] == '\0') + continue; + if (tmp[0] == '#') + continue; + l = bc_slist_append(l, bc_strdup(tmp)); + } + return l; +} + + int main(int argc, char **argv) { @@ -111,6 +129,7 @@ main(int argc, char **argv) int rv = 0; bool debug = false; + bool input_stdin = false; bool listing = false; char *template = NULL; char *output = NULL; @@ -135,6 +154,9 @@ main(int argc, char **argv) case 'd': debug = true; break; + case 'i': + input_stdin = true; + break; case 'l': listing = true; break; @@ -204,6 +226,9 @@ main(int argc, char **argv) sources = bc_slist_append(sources, bc_strdup(argv[i])); } + if (input_stdin) + sources = blogc_read_stdin_to_list(sources); + if (!listing && bc_slist_length(sources) == 0) { blogc_print_usage(); fprintf(stderr, "blogc: error: one source file is required\n"); -- cgit v1.2.3-18-g5258