afb-config: Check environment variables
[src/app-framework-binder.git] / src / afb-config.c
index 0bfd9d4..f4aed05 100644 (file)
@@ -16,7 +16,6 @@
  */
 
 #define _GNU_SOURCE
-#define AFB_BINDING_PRAGMA_NO_VERBOSE_MACRO
 
 #include <stdlib.h>
 #include <stdio.h>
@@ -34,8 +33,9 @@
 #if !defined(BINDING_INSTALL_DIR)
 #error "you should define BINDING_INSTALL_DIR"
 #endif
-
-#define AFB_VERSION    "0.6"
+#if !defined(AFB_VERSION)
+#error "you should define AFB_VERSION"
+#endif
 
 // default
 #define DEFLT_CNTX_TIMEOUT  3600       // default Client Connection
@@ -230,14 +230,16 @@ static struct enumdesc mode_desc[] = {
  +--------------------------------------------------------- */
 static void printVersion(FILE * file)
 {
-       fprintf(file, "\n----------------------------------------- \n");
-       fprintf(file, "  AFB [Application Framework Binder] version=%s |\n",
-               AFB_VERSION);
-       fprintf(file, " \n");
-       fprintf(file,
-               "  Copyright (C) 2015, 2016, 2017 \"IoT.bzh\" [fulup -at- iot.bzh]\n");
-       fprintf(file, "  AFB comes with ABSOLUTELY NO WARRANTY.\n");
-       fprintf(file, "  Licence Apache 2\n\n");
+       static const char version[] =
+               "\n"
+               "  AFB [Application Framework Binder] version="AFB_VERSION"\n"
+               "\n"
+               "  Copyright (C) 2015, 2016, 2017 \"IoT.bzh\"\n"
+               "  AFB comes with ABSOLUTELY NO WARRANTY.\n"
+               "  Licence Apache 2\n"
+               "\n";
+
+       fprintf(file, "%s", version);
 }
 
 /*----------------------------------------------------------
@@ -752,12 +754,47 @@ void afb_config_dump(struct afb_config *config)
 #undef NN
 }
 
+static void on_environment_list(struct afb_config_list **to, const char *name)
+{
+       char *value = getenv(name);
+
+       if (value)
+               list_add(to, value);
+}
+
+static void on_environment_enum(int *to, const char *name, struct enumdesc *desc)
+{
+       char *value = getenv(name);
+
+       if (value) {
+               while (desc->name) {
+                       if (strcmp(desc->name, value))
+                               desc++;
+                       else {
+                               *to = desc->value;
+                               return;
+                       }
+               }
+               WARNING("Unknown value %s for environment variable %s, ignored", value, name);
+       }
+}
+
+static void parse_environment(struct afb_config *config)
+{
+       on_environment_enum(&config->tracereq, "AFB_TRACEREQ", tracereq_desc);
+       on_environment_enum(&config->traceditf, "AFB_TRACEDITF", traceditf_desc);
+       on_environment_enum(&config->tracesvc, "AFB_TRACESVC", tracesvc_desc);
+       on_environment_enum(&config->traceevt, "AFB_TRACEEVT", traceevt_desc);
+       on_environment_list(&config->ldpaths, "AFB_LDPATHS");
+}
+
 struct afb_config *afb_config_parse_arguments(int argc, char **argv)
 {
        struct afb_config *result;
 
        result = calloc(1, sizeof *result);
 
+       parse_environment(result);
        parse_arguments(argc, argv, result);
        fulfill_config(result);
        if (verbosity >= 3)