From 706ec0fcd483e5f758c9770731fe16bf74920157 Mon Sep 17 00:00:00 2001 From: Jarcode Date: Fri, 15 Mar 2019 00:32:33 -0700 Subject: [PATCH] properly handle default pipe names --- glava.c | 84 ++++++++++++++++++++++++++++++------------------------ glsl_ext.c | 2 +- render.c | 6 ++-- render.h | 2 ++ 4 files changed, 54 insertions(+), 40 deletions(-) diff --git a/glava.c b/glava.c index 1c77e82..47666ad 100644 --- a/glava.c +++ b/glava.c @@ -165,33 +165,29 @@ static const char* help_str = "Opens a window with an OpenGL context to draw an audio visualizer.\n" "\n" "Available arguments:\n" - "-h, --help show this help and exit\n" - "-v, --verbose enables printing of detailed information about execution\n" - "-d, --desktop enables running glava as a desktop window by detecting the\n" - " desktop environment and setting the appropriate properties\n" - " automatically. Can override properties in \"rc.glsl\".\n" - "-r, --request=REQUEST evaluates the specified request after loading \"rc.glsl\".\n" - "-m, --force-mod=NAME forces the specified module to load instead, ignoring any\n" - " `#request mod` instances in the entry point.\n" - "-e, --entry=FILE specifies the name of the file to look for when loading shaders,\n" - " by default this is \"rc.glsl\".\n" - "-C, --copy-config creates copies and symbolic links in the user configuration\n" - " directory for glava, copying any files in the root directory\n" - " of the installed shader directory, and linking any modules.\n" - "-b, --backend specifies a window creation backend to use. By default, the most\n" - " appropriate backend will be used for the underlying windowing\n" - " system.\n" - "-a, --audio=BACKEND specifies an audio input backend to use.\n" - "-p, --pipe[=FORMAT] binds a value to be read from stdin. The input my be read using\n" + "-h, --help show this help and exit\n" + "-v, --verbose enables printing of detailed information about execution\n" + "-d, --desktop enables running glava as a desktop window by detecting the\n" + " desktop environment and setting the appropriate properties\n" + " automatically. Can override properties in \"rc.glsl\".\n" + "-r, --request=REQUEST evaluates the specified request after loading \"rc.glsl\".\n" + "-m, --force-mod=NAME forces the specified module to load instead, ignoring any\n" + " `#request mod` instances in the entry point.\n" + "-e, --entry=FILE specifies the name of the file to look for when loading shaders,\n" + " by default this is \"rc.glsl\".\n" + "-C, --copy-config creates copies and symbolic links in the user configuration\n" + " directory for glava, copying any files in the root directory\n" + " of the installed shader directory, and linking any modules.\n" + "-b, --backend specifies a window creation backend to use. By default, the most\n" + " appropriate backend will be used for the underlying windowing\n" + " system.\n" + "-a, --audio=BACKEND specifies an audio input backend to use.\n" + "-p, --pipe=BIND[:TYPE] binds a value to be read from stdin. The input my be read using\n" " `@name` or `@name:default` syntax within shader sources.\n" " A stream of inputs (each overriding the previous) must be\n" " assigned with the `name = value` syntax and separated by\n" " newline (\'\\n\') characters.\n" - "-i, --stdin[=OLDFORMAT] specifies a format for input to be read from stdin. The input\n" - " may be read from the STDIN macro from within shader sources.\n" - " A stream of inputs (each overriding the previous) must be\n" - " separated by newline (\'\\n\') characters.\n" - "-V, --version print application version and exit\n" + "-V, --version print application version and exit\n" "\n" "The REQUEST argument is evaluated identically to the \'#request\' preprocessor directive\n" "in GLSL files.\n" @@ -205,8 +201,10 @@ static const char* help_str = "The BACKEND argument may be any of the following strings (for this particular build):\n" "%s" "\n" - "The OLDFORMAT argument must be a valid GLSL type. If `--stdin` is used without an argument,\n" - "the default type is `vec4` (type used for RGBA colors)\n" + "The BIND argument must a valid GLSL identifier." + "\n" + "The TYPE argument must be a valid GLSL type. If `--pipe` is used without a \n" + "type argument, the default type is `vec4` (type used for RGBA colors).\n" "\n" GLAVA_VERSION_STRING "\n"; @@ -296,25 +294,37 @@ int main(int argc, char** argv) { if (stdin_type != STDIN_TYPE_NONE) goto conflict_error; char* parsed_name = NULL; const char* parsed_type = NULL; - size_t in_sz = strlen(optarg); - int sep = -1; - for (size_t t = 0; t < in_sz; ++t) { - switch (optarg[t]) { - case ' ': optarg[t] = '\0'; goto after; - case ':': sep = (int) t; break; + if (optarg) { + size_t in_sz = strlen(optarg); + int sep = -1; + for (size_t t = 0; t < in_sz; ++t) { + switch (optarg[t]) { + case ' ': optarg[t] = '\0'; goto after; + case ':': sep = (int) t; break; + } } - } after: - if (sep >= 0) { - parsed_type = optarg + sep + 1; - optarg[sep] = '\0'; + if (sep >= 0) { + parsed_type = optarg + sep + 1; + optarg[sep] = '\0'; + } + parsed_name = optarg; + } else parsed_name = PIPE_DEFAULT; + if (*parsed_name == '\0') { + fprintf(stderr, "Error: invalid pipe binding name: \"%s\"\n" + "Zero length names are not permitted.\n", parsed_name); + exit(EXIT_FAILURE); } - parsed_name = optarg; for (char* c = parsed_name; *c != '\0'; ++c) { switch (*c) { + case '0' ... '9': + if (c == parsed_name) { + fprintf(stderr, "Error: invalid pipe binding name: \"%s\" ('%c')\n" + "Valid names may not start with a number.\n", parsed_name, *c); + exit(EXIT_FAILURE); + } case 'a' ... 'z': case 'A' ... 'Z': - case '0' ... '9': case '_': continue; default: fprintf(stderr, "Error: invalid pipe binding name: \"%s\" ('%c')\n" diff --git a/glsl_ext.c b/glsl_ext.c index 7960cfd..7fa193d 100644 --- a/glsl_ext.c +++ b/glsl_ext.c @@ -318,7 +318,7 @@ void ext_process(struct glsl_ext* ext, const char* f) { bool quoted = false, arg_start, b_sep = false, b_spc = false; int b_br = 0; char cbuf[9]; - char bbuf[128]; + char bbuf[256]; char** args = malloc(sizeof(char*)); size_t args_sz = 0; diff --git a/render.c b/render.c index c66a662..b3e9fa6 100644 --- a/render.c +++ b/render.c @@ -1644,6 +1644,8 @@ bool rd_update(struct renderer* r, float* lb, float* rb, size_t bsz, bool modifi if (c != EOF && c != '\n') stdin_buf[stdin_idx++] = c; else { + if (stdin_idx == 0) + goto reset; stdin_buf[stdin_idx] = '\0'; stdin_select = gl->stdin_type; @@ -1669,9 +1671,9 @@ bool rd_update(struct renderer* r, float* lb, float* rb, size_t bsz, bool modifi break; } } - if (!valid && !v) { + if ((stdin_name && stdin_name[0] == '\0') || (!valid && !v)) { /* no assignment, just a default value */ - stdin_name = ""; + stdin_name = PIPE_DEFAULT; stdin_name_len = 0; valid = true; } diff --git a/render.h b/render.h index 7472193..43274f9 100644 --- a/render.h +++ b/render.h @@ -15,6 +15,8 @@ extern const struct { #define STDIN_TYPE_VEC3 5 #define STDIN_TYPE_VEC4 6 +#define PIPE_DEFAULT "_" + struct gl_data; typedef struct renderer {