From 4bd2410ee750cb9fb2b45909d7d26ec27f67e54c Mon Sep 17 00:00:00 2001 From: "roberto@oneiric64" Date: Fri, 10 Feb 2012 09:06:09 +0100 Subject: [PATCH] added support for logic in config directives with a first 'for' implementation --- utils.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ uwsgi.c | 18 ++++++++++++++++++ uwsgi.h | 11 +++++++++++ 3 files changed, 86 insertions(+) diff --git a/utils.c b/utils.c index 0901e87c..4aa3d31a 100644 --- a/utils.c +++ b/utils.c @@ -1747,8 +1747,65 @@ struct uwsgi_option *uwsgi_opt_get(char *name) { return NULL; } +char *uwsgi_substitue(char *src, char *what, char *with) { + + size_t len = strlen(src)+1; + size_t wlen = strlen(with)+1; + char *dst = uwsgi_calloc(len); + char *ptr = src; + + char *p = strstr(ptr, what); + while(p) { + strncat(dst, ptr, (p-ptr)); + ptr = p + (wlen-2); + len += wlen; + dst = realloc(dst, len); + strcat(dst, with); + p = strstr(ptr, what); + } + + return dst; +} + +int uwsgi_logic_opt_for(char *key, char *value) { + + char *p = strtok(uwsgi.logic_opt_data, " "); + while(p) { + uwsgi.logic_opt_running = 1; + add_exported_option(key, uwsgi_substitue(value, "%(_)", p), 0); + uwsgi.logic_opt_running = 0; + p = strtok(NULL, " "); + } + + return 0; +} + void add_exported_option(char *key, char *value, int configured) { + if (uwsgi.logic_opt_running) goto add; + + if (!strcmp(key, "end") || !strcmp(key, "endfor") || !strcmp(key, "endif")) { + if (uwsgi.logic_opt_data) { + free(uwsgi.logic_opt_data); + } + uwsgi.logic_opt = NULL; + uwsgi.logic_opt_arg = NULL; + uwsgi.logic_opt_cycles = 0; + uwsgi.logic_opt_data = NULL; + } + + if (uwsgi.logic_opt) { + if (uwsgi.logic_opt_data) { + free(uwsgi.logic_opt_data); + } + uwsgi.logic_opt_data = uwsgi_str(uwsgi.logic_opt_arg); + uwsgi.logic_opt_cycles++; + uwsgi.logic_opt(key, value); + return; + } + +add: + if (!uwsgi.exported_opts) { uwsgi.exported_opts = uwsgi_malloc(sizeof(struct uwsgi_opt *)); } diff --git a/uwsgi.c b/uwsgi.c index fe4a2269..44e34855 100644 --- a/uwsgi.c +++ b/uwsgi.c @@ -57,6 +57,10 @@ static struct uwsgi_option uwsgi_base_options[] = { {"xml", required_argument, 'x', "load config from xml file", uwsgi_opt_load_xml, NULL, UWSGI_OPT_IMMEDIATE}, #endif {"set", required_argument, 'S', "set a custom placeholder", uwsgi_opt_set_placeholder, NULL, UWSGI_OPT_IMMEDIATE}, + + {"for", required_argument, 0, "(opt logic) for cycle", uwsgi_opt_logic, (void *) uwsgi_logic_opt_for, UWSGI_OPT_IMMEDIATE}, + {"endfor", optional_argument, 0, "(opt logic) end for cycle", uwsgi_opt_noop, NULL, UWSGI_OPT_IMMEDIATE}, + {"inherit", required_argument, 0, "use the specified file as config template", uwsgi_opt_load, NULL,0}, {"daemonize", required_argument, 'd', "daemonize uWSGI", uwsgi_opt_set_str, &uwsgi.daemonize, 0}, {"stop", required_argument, 0, "stop an instance", uwsgi_opt_pidfile_signal, (void *) SIGINT, UWSGI_OPT_IMMEDIATE}, @@ -3544,6 +3548,20 @@ void uwsgi_opt_load(char *opt, char *filename, void *none) { #endif } +void uwsgi_opt_logic(char *opt, char *arg, void *func) { + uwsgi.logic_opt = (int (*)(char *, char *)) func; + uwsgi.logic_opt_cycles = 0; + if (arg) { + uwsgi.logic_opt_arg = uwsgi_str(arg); + } + else { + uwsgi.logic_opt_arg = NULL; + } +} + +void uwsgi_opt_noop(char *opt, char *foo, void *bar) { +} + #ifdef UWSGI_INI void uwsgi_opt_load_ini(char *opt, char *filename, void *none) { config_magic_table_fill(filename, uwsgi.magic_table); diff --git a/uwsgi.h b/uwsgi.h index 45b90e8f..9370da89 100644 --- a/uwsgi.h +++ b/uwsgi.h @@ -1043,6 +1043,11 @@ struct uwsgi_server { // store options int dirty_config; int option_index; + int (*logic_opt)(char *, char *); + char *logic_opt_arg; + char *logic_opt_data; + int logic_opt_running; + int logic_opt_cycles; struct uwsgi_option *options; struct option *long_options; char *short_options; @@ -2556,6 +2561,12 @@ void uwsgi_opt_chmod_socket(char *, char *, void *); void uwsgi_opt_max_vars(char *, char *, void *); void uwsgi_opt_deprecated(char *, char *, void *); +void uwsgi_opt_noop(char *, char *, void *); + +void uwsgi_opt_logic(char *, char *, void *); +int uwsgi_logic_opt_for(char *, char *); + + #ifdef UWSGI_CAP void uwsgi_opt_set_cap(char *, char *, void *); #endif