Diff options
authorBryce Kwon <bryce@brycekwon.com>
committerBryce Kwon <bryce@brycekwon.com>
commit
parent
tree
download
Pick a filter's backend with skip_prefix
A two entry table and a prefix measured by hand did what two prefix tests do. A drive letter and an unknown prefix are read as before.
-rw-r--r--source/filter.c46
1 file changed, 14 insertions, 32 deletions
diff --git a/source/filter.c b/source/filter.c
index f2dfe12..76cebe9 100644
--- a/source/filter.c
+++ b/source/filter.c
@@ -477,35 +477,14 @@ int cgit_filter_holds_stdout(void)
return running_exec != NULL;
}
-static const struct {
- const char *prefix;
- struct cgit_filter *(*create)(const char *cmd, int argument_count);
-} filter_specs[] = {
- { "exec", new_exec_filter },
-#ifndef NO_LUA
- { "lua", new_lua_filter },
-#endif
-};
-
struct cgit_filter *cgit_new_filter(const char *cmd, filter_type filtertype)
{
- const char *colon;
- size_t i;
- size_t len;
+ const char *colon, *arg;
int argument_count;
if (!cmd || !cmd[0])
return NULL;
- colon = strchr(cmd, ':');
- // Measured only once there is a colon to measure against, because
- // subtracting from a null pointer is not something C defines.
- len = colon ? (size_t)(colon - cmd) : 0;
- // A single letter before the colon is a Windows drive letter, not a
- // filter prefix.
- if (len == 1)
- colon = NULL;
-
switch (filtertype) {
case AUTH:
argument_count = 12;
@@ -526,17 +505,20 @@ struct cgit_filter *cgit_new_filter(const char *cmd, filter_type filtertype)
break;
}
- if (!colon)
- return new_exec_filter(cmd, argument_count);
-
- for (i = 0; i < ARRAY_SIZE(filter_specs); i++) {
- if (len == strlen(filter_specs[i].prefix) && !strncmp(filter_specs[i].prefix, cmd, len))
- return filter_specs[i].create(colon + 1, argument_count);
- }
-
+ if (skip_prefix(cmd, "exec:", &arg))
+ return new_exec_filter(arg, argument_count);
+ if (skip_prefix(cmd, "lua:", &arg)) {
#ifdef NO_LUA
- if (len == 3 && !strncmp("lua", cmd, len))
die("Built without Lua, cannot run filter %s", cmd);
+#else
+ return new_lua_filter(arg, argument_count);
#endif
- die("Invalid filter type: %.*s", (int)len, cmd);
+ }
+
+ // A single letter before the colon is a Windows drive letter, not a
+ // filter prefix.
+ colon = strchr(cmd, ':');
+ if (!colon || colon - cmd == 1)
+ return new_exec_filter(cmd, argument_count);
+ die("Invalid filter type: %.*s", (int)(colon - cmd), cmd);
}