b23619e45f3c875c4edc088ff4883af9e5547be0
[src/app-framework-binder.git] / src / afb-config.c
1 /*
2  * Copyright (C) 2015-2018 "IoT.bzh"
3  * Author José Bollo <jose.bollo@iot.bzh>
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #define _GNU_SOURCE
19
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <getopt.h>
24 #include <limits.h>
25 #include <unistd.h>
26
27 #include <json-c/json.h>
28
29 #include "verbose.h"
30 #include "afb-config.h"
31 #include "afb-hook.h"
32
33 #include <afb/afb-binding-v1.h>
34
35 #if !defined(BINDING_INSTALL_DIR)
36 #error "you should define BINDING_INSTALL_DIR"
37 #endif
38 #if !defined(AFB_VERSION)
39 #error "you should define AFB_VERSION"
40 #endif
41
42 // default
43 #define DEFLT_CNTX_TIMEOUT  32000000    // default Client Connection
44                                         // Timeout: few more than one year
45 #define DEFLT_API_TIMEOUT   20          // default Plugin API Timeout [0=NoLimit
46                                         // for Debug Only]
47 #define DEFLT_CACHE_TIMEOUT 100000      // default Static File Chache
48                                         // [Client Side Cache
49                                         // 100000~=1day]
50 #define CTX_NBCLIENTS       10          // allow a default of 10 authenticated
51                                         // clients
52
53
54 // Define command line option
55 #define SET_BACKGROUND     2
56 #define SET_FORGROUND      3
57
58 #define SET_ROOT_DIR       6
59 #define SET_ROOT_BASE      7
60 #define SET_ROOT_API       8
61 #define SET_ALIAS          9
62
63 #define SET_CACHE_TIMEOUT  10
64 #define SET_SESSION_DIR    11
65
66 #define SET_LDPATH         13
67 #define SET_APITIMEOUT     14
68 #define SET_CNTXTIMEOUT    15
69 #define SET_WEAK_LDPATH    16
70 #define NO_LDPATH          17
71
72 #define SET_MODE           18
73
74 #define DBUS_CLIENT        20
75 #define DBUS_SERVICE       21
76 #define SO_BINDING         22
77
78 #define SET_SESSIONMAX     23
79
80 #define WS_CLIENT          24
81 #define WS_SERVICE         25
82
83 #define SET_ROOT_HTTP      26
84
85 #define SET_NO_HTTPD       28
86
87 #define ADD_CALL           'c'
88 #define SET_TRACEDITF      'D'
89 #define SET_TRACEEVT       'E'
90 #define SET_EXEC           'e'
91 #define DISPLAY_HELP       'h'
92 #if defined(WITH_MONITORING_OPTION)
93 #define SET_MONITORING     'M'
94 #endif
95 #define SET_NAME           'n'
96 #define SET_TCP_PORT       'p'
97 #define SET_QUIET          'q'
98 #define SET_RNDTOKEN       'r'
99 #define SET_TRACESVC       'S'
100 #define SET_TRACESES       's'
101 #define SET_TRACEREQ       'T'
102 #define SET_AUTH_TOKEN     't'
103 #define SET_UPLOAD_DIR     'u'
104 #define DISPLAY_VERSION    'V'
105 #define SET_VERBOSE        'v'
106 #define SET_WORK_DIR       'w'
107
108 const char shortopts[] =
109         "c:D:E:ehn:p:qrS:s:T:t:u:Vvw:"
110 #if defined(WITH_MONITORING_OPTION)
111         "M"
112 #endif
113 ;
114
115 // Command line structure hold cli --command + help text
116 typedef struct {
117         int val;                // command number within application
118         int has_arg;            // command number within application
119         char *name;             // command as used in --xxxx cli
120         char *help;             // help text
121 } AFB_options;
122
123 // Supported option
124 static AFB_options cliOptions[] = {
125 /* *INDENT-OFF* */
126         {SET_VERBOSE,       0, "verbose",     "Verbose Mode, repeat to increase verbosity"},
127         {SET_QUIET,         0, "quiet",       "Quiet Mode, repeat to decrease verbosity"},
128
129         {SET_FORGROUND,     0, "foreground",  "Get all in foreground mode"},
130         {SET_BACKGROUND,    0, "daemon",      "Get all in background mode"},
131
132         {SET_NAME,          1, "name",        "Set the visible name"},
133
134         {SET_TCP_PORT,      1, "port",        "HTTP listening TCP port  [default 1234]"},
135         {SET_ROOT_HTTP,     1, "roothttp",    "HTTP Root Directory [default no root http (files not served but apis still available)]"},
136         {SET_ROOT_BASE,     1, "rootbase",    "Angular Base Root URL [default /opa]"},
137         {SET_ROOT_API,      1, "rootapi",     "HTML Root API URL [default /api]"},
138         {SET_ALIAS,         1, "alias",       "Multiple url map outside of rootdir [eg: --alias=/icons:/usr/share/icons]"},
139
140         {SET_APITIMEOUT,    1, "apitimeout",  "Binding API timeout in seconds [default 10]"},
141         {SET_CNTXTIMEOUT,   1, "cntxtimeout", "Client Session Context Timeout [default 900]"},
142         {SET_CACHE_TIMEOUT, 1, "cache-eol",   "Client cache end of live [default 3600]"},
143
144         {SET_WORK_DIR,      1, "workdir",     "Set the working directory [default: $PWD or current working directory]"},
145         {SET_UPLOAD_DIR,    1, "uploaddir",   "Directory for uploading files [default: workdir]"},
146         {SET_ROOT_DIR,      1, "rootdir",     "Root Directory of the application [default: workdir]"},
147         {SET_SESSION_DIR,   1, "sessiondir",  "OBSOLETE (was: Sessions file path)"},
148
149         {SET_LDPATH,        1, "ldpaths",     "Load bindings from dir1:dir2:... [default = " BINDING_INSTALL_DIR "]"},
150         {SO_BINDING,        1, "binding",     "Load the binding of path"},
151         {SET_WEAK_LDPATH,   1, "weak-ldpaths","Same as --ldpaths but ignore errors"},
152         {NO_LDPATH,         0, "no-ldpaths",  "Discard default ldpaths loading"},
153
154         {SET_AUTH_TOKEN,    1, "token",       "Initial Secret [default=random, use --token="" to allow any token]"},
155         {SET_RNDTOKEN,      0, "random-token","Enforce a random token"},
156
157         {DISPLAY_VERSION,   0, "version",     "Display version and copyright"},
158         {DISPLAY_HELP,      0, "help",        "Display this help"},
159
160         {SET_MODE,          1, "mode",        "Set the mode: either local, remote or global"},
161
162         {DBUS_CLIENT,       1, "dbus-client", "Bind to an afb service through dbus"},
163         {DBUS_SERVICE,      1, "dbus-server", "Provides an afb service through dbus"},
164         {WS_CLIENT,         1, "ws-client",   "Bind to an afb service through websocket"},
165         {WS_SERVICE,        1, "ws-server",   "Provides an afb service through websockets"},
166
167         {SET_SESSIONMAX,    1, "session-max", "Max count of session simultaneously [default 10]"},
168
169         {SET_TRACEREQ,      1, "tracereq",    "Log the requests: no, common, extra, all"},
170         {SET_TRACEDITF,     1, "traceditf",   "Log the requests: no, common, extra, all"},
171         {SET_TRACESVC,      1, "tracesvc",    "Log the requests: no, all"},
172         {SET_TRACEEVT,      1, "traceevt",    "Log the requests: no, common, extra, all"},
173         {SET_TRACESES,      1, "traceses",    "Log the sessions: no, all"},
174
175         {ADD_CALL,          1, "call",        "call at start format of val: API/VERB:json-args"},
176
177         {SET_NO_HTTPD,      0, "no-httpd",    "Forbids HTTP service"},
178         {SET_EXEC,          0, "exec",        "Execute the remaining arguments"},
179
180 #if defined(WITH_MONITORING_OPTION)
181         {SET_MONITORING,    0, "monitoring",  "enable HTTP monitoring at <ROOT>/monitoring/"},
182 #endif
183         {0, 0, NULL, NULL}
184 /* *INDENT-ON* */
185 };
186
187
188 struct enumdesc
189 {
190         const char *name;
191         int value;
192 };
193
194 static struct enumdesc tracereq_desc[] = {
195         { "no",     0 },
196         { "common", afb_hook_flags_req_common },
197         { "extra",  afb_hook_flags_req_extra },
198         { "all",    afb_hook_flags_req_all },
199         { NULL, 0 }
200 };
201
202 static struct enumdesc traceditf_desc[] = {
203         { "no",     0 },
204         { "common", afb_hook_flags_ditf_common },
205         { "extra",  afb_hook_flags_ditf_extra },
206         { "all",    afb_hook_flags_ditf_all },
207         { NULL, 0 }
208 };
209
210 static struct enumdesc tracesvc_desc[] = {
211         { "no",     0 },
212         { "all",    afb_hook_flags_svc_all },
213         { NULL, 0 }
214 };
215
216 static struct enumdesc traceevt_desc[] = {
217         { "no",     0 },
218         { "common", afb_hook_flags_evt_common },
219         { "extra",  afb_hook_flags_evt_extra },
220         { "all",    afb_hook_flags_evt_all },
221         { NULL, 0 }
222 };
223
224 static struct enumdesc traceses_desc[] = {
225         { "no",     0 },
226         { "common", afb_hook_flags_session_common },
227         { "all",    afb_hook_flags_session_all },
228         { NULL, 0 }
229 };
230
231 static struct enumdesc mode_desc[] = {
232         { "local",  AFB_MODE_LOCAL },
233         { "remote", AFB_MODE_REMOTE },
234         { "global", AFB_MODE_GLOBAL },
235         { NULL, 0 }
236 };
237
238 /*----------------------------------------------------------
239  | printversion
240  |   print version and copyright
241  +--------------------------------------------------------- */
242 static void printVersion(FILE * file)
243 {
244         static const char version[] =
245                 "\n"
246                 "  AFB [Application Framework Binder] version="AFB_VERSION"\n"
247                 "\n"
248                 "  Copyright (C) 2015, 2016, 2017 \"IoT.bzh\"\n"
249                 "  AFB comes with ABSOLUTELY NO WARRANTY.\n"
250                 "  Licence Apache 2\n"
251                 "\n";
252
253         fprintf(file, "%s", version);
254 }
255
256 /*----------------------------------------------------------
257  | printHelp
258  |   print information from long option array
259  +--------------------------------------------------------- */
260
261 static void printHelp(FILE * file, const char *name)
262 {
263         int ind;
264         char command[50];
265
266         fprintf(file, "%s:\nallowed options\n", name);
267         for (ind = 0; cliOptions[ind].name != NULL; ind++) {
268                 strcpy(command, cliOptions[ind].name);
269                 if (cliOptions[ind].has_arg)
270                         strcat(command, "=xxxx");
271                 fprintf(file, "  --%-15s %s\n", command, cliOptions[ind].help);
272         }
273         fprintf(file,
274                 "Example:\n  %s  --verbose --port=1234 --token='azerty' --ldpaths=build/bindings:/usr/lib64/agl/bindings\n",
275                 name);
276 }
277
278
279 /*----------------------------------------------------------
280  |   adds a string to the list
281  +--------------------------------------------------------- */
282 static void list_add(struct afb_config_list **head, char *value)
283 {
284         struct afb_config_list *item;
285
286         /*
287          * search tail
288          */
289         item = *head;
290         while (item != NULL) {
291                 head = &item->next;
292                 item = item->next;
293         }
294
295         /*
296          * alloc the item
297          */
298         item = malloc(sizeof *item);
299         if (item == NULL) {
300                 ERROR("out of memory");
301                 exit(1);
302         }
303
304         /*
305          * init the item
306          */
307         *head = item;
308         item->value = value;
309         item->next = NULL;
310 }
311
312 /*---------------------------------------------------------
313  |   helpers for argument scanning
314  +--------------------------------------------------------- */
315
316 static const char *name_of_option(int optc)
317 {
318         AFB_options *o = cliOptions;
319         while (o->name && o->val != optc)
320                 o++;
321         return o->name ? : "<unknown-option-name>";
322 }
323
324 static const char *current_argument(int optc)
325 {
326         if (optarg == 0) {
327                 ERROR("option [--%s] needs a value i.e. --%s=xxx",
328                       name_of_option(optc), name_of_option(optc));
329                 exit(1);
330         }
331         return optarg;
332 }
333
334 static char *argvalstr(int optc)
335 {
336         char *result = strdup(current_argument(optc));
337         if (result == NULL) {
338                 ERROR("can't alloc memory");
339                 exit(1);
340         }
341         return result;
342 }
343
344 static int argvalenum(int optc, struct enumdesc *desc)
345 {
346         int i;
347         size_t len;
348         char *list;
349         const char *name = current_argument(optc);
350
351         i = 0;
352         while(desc[i].name && strcmp(desc[i].name, name))
353                 i++;
354         if (!desc[i].name) {
355                 len = 0;
356                 i = 0;
357                 while(desc[i].name)
358                         len += strlen(desc[i++].name);
359                 list = malloc(len + i + i);
360                 if (!i || !list)
361                         ERROR("option [--%s] bad value (found %s)",
362                                 name_of_option(optc), name);
363                 else {
364                         i = 0;
365                         strcpy(list, desc[i].name ? : "");
366                         while(desc[++i].name)
367                                 strcat(strcat(list, ", "), desc[i].name);
368                         ERROR("option [--%s] bad value, only accepts values %s (found %s)",
369                                 name_of_option(optc), list, name);
370                 }
371                 free(list);
372                 exit(1);
373         }
374         return desc[i].value;
375 }
376
377 static int argvalint(int optc, int mini, int maxi, int base)
378 {
379         const char *beg, *end;
380         long int val;
381         beg = current_argument(optc);
382         val = strtol(beg, (char**)&end, base);
383         if (*end || end == beg) {
384                 ERROR("option [--%s] requires a valid integer (found %s)",
385                         name_of_option(optc), beg);
386                 exit(1);
387         }
388         if (val < (long int)mini || val > (long int)maxi) {
389                 ERROR("option [--%s] value out of bounds (not %d<=%ld<=%d)",
390                         name_of_option(optc), mini, val, maxi);
391                 exit(1);
392         }
393         return (int)val;
394 }
395
396 static int argvalintdec(int optc, int mini, int maxi)
397 {
398         return argvalint(optc, mini, maxi, 10);
399 }
400
401 static void noarg(int optc)
402 {
403         if (optarg != 0) {
404                 ERROR("option [--%s] need no value (found %s)", name_of_option(optc), optarg);
405                 exit(1);
406         }
407 }
408
409 static char **make_exec(char **argv)
410 {
411         char **result, *iter;
412         size_t length;
413         int i;
414
415         length = 0;
416         for (i = 0 ; argv[i] ; i++)
417                 length += strlen(argv[i]) + 1;
418
419         result = malloc(length + ((unsigned)(i + 1)) * sizeof *result);
420         if (result == NULL) {
421                 ERROR("can't alloc memory");
422                 exit(1);
423         }
424
425         iter = (char*)&result[i+1];
426         for (i = 0 ; argv[i] ; i++) {
427                 result[i] = iter;
428                 iter = stpcpy(iter, argv[i]) + 1;
429         }
430         result[i] = NULL;
431         return result;
432 }
433
434 /*---------------------------------------------------------
435  |   Parse option and launch action
436  +--------------------------------------------------------- */
437
438 static void parse_arguments(int argc, char **argv, struct afb_config *config)
439 {
440         char *programName = argv[0];
441         int optc, ind;
442         int nbcmd;
443         struct option *gnuOptions;
444
445         // ------------------ Process Command Line -----------------------
446
447         // build GNU getopt info from cliOptions
448         nbcmd = sizeof(cliOptions) / sizeof(AFB_options);
449         gnuOptions = malloc(sizeof(*gnuOptions) * (unsigned)nbcmd);
450         for (ind = 0; ind < nbcmd; ind++) {
451                 gnuOptions[ind].name = cliOptions[ind].name;
452                 gnuOptions[ind].has_arg = cliOptions[ind].has_arg;
453                 gnuOptions[ind].flag = 0;
454                 gnuOptions[ind].val = cliOptions[ind].val;
455         }
456
457         // get all options from command line
458         while ((optc = getopt_long(argc, argv, shortopts, gnuOptions, NULL)) != EOF) {
459                 switch (optc) {
460                 case SET_VERBOSE:
461                         verbosity++;
462                         break;
463
464                 case SET_QUIET:
465                         verbosity--;
466                         break;
467
468                 case SET_TCP_PORT:
469                         config->httpdPort = argvalintdec(optc, 1024, 32767);
470                         break;
471
472                 case SET_APITIMEOUT:
473                         config->apiTimeout = argvalintdec(optc, 0, INT_MAX);
474                         break;
475
476                 case SET_CNTXTIMEOUT:
477                         config->cntxTimeout = argvalintdec(optc, 0, INT_MAX);
478                         break;
479
480                 case SET_ROOT_DIR:
481                         config->rootdir = argvalstr(optc);
482                         INFO("Forcing Rootdir=%s", config->rootdir);
483                         break;
484
485                 case SET_ROOT_HTTP:
486                         config->roothttp = argvalstr(optc);
487                         INFO("Forcing Root HTTP=%s", config->roothttp);
488                         break;
489
490                 case SET_ROOT_BASE:
491                         config->rootbase = argvalstr(optc);
492                         INFO("Forcing Rootbase=%s", config->rootbase);
493                         break;
494
495                 case SET_ROOT_API:
496                         config->rootapi = argvalstr(optc);
497                         INFO("Forcing Rootapi=%s", config->rootapi);
498                         break;
499
500                 case SET_ALIAS:
501                         list_add(&config->aliases, argvalstr(optc));
502                         break;
503
504                 case SET_AUTH_TOKEN:
505                         config->token = argvalstr(optc);
506                         break;
507
508                 case SET_LDPATH:
509                         list_add(&config->ldpaths, argvalstr(optc));
510                         break;
511
512                 case SET_WEAK_LDPATH:
513                         list_add(&config->weak_ldpaths, argvalstr(optc));
514                         break;
515
516                 case NO_LDPATH:
517                         noarg(optc);
518                         config->no_ldpaths = 1;
519                         break;
520
521                 case ADD_CALL:
522                         list_add(&config->calls, argvalstr(optc));
523                         break;
524
525                 case SET_SESSION_DIR:
526                         /* config->sessiondir = argvalstr(optc); */
527                         WARNING("Obsolete option %s ignored", name_of_option(optc));
528                         break;
529
530                 case SET_UPLOAD_DIR:
531                         config->uploaddir = argvalstr(optc);
532                         break;
533
534                 case SET_WORK_DIR:
535                         config->workdir = argvalstr(optc);
536                         break;
537
538                 case SET_CACHE_TIMEOUT:
539                         config->cacheTimeout = argvalintdec(optc, 0, INT_MAX);
540                         break;
541
542                 case SET_SESSIONMAX:
543                         config->nbSessionMax = argvalintdec(optc, 1, INT_MAX);
544                         break;
545
546                 case SET_FORGROUND:
547                         noarg(optc);
548                         config->background = 0;
549                         break;
550
551                 case SET_BACKGROUND:
552                         noarg(optc);
553                         config->background = 1;
554                         break;
555
556                 case SET_NAME:
557                         config->name = argvalstr(optc);
558                         break;
559
560                 case SET_MODE:
561                         config->mode = argvalenum(optc, mode_desc);
562                         break;
563
564                 case DBUS_CLIENT:
565                         list_add(&config->dbus_clients, argvalstr(optc));
566                         break;
567
568                 case DBUS_SERVICE:
569                         list_add(&config->dbus_servers, argvalstr(optc));
570                         break;
571
572                 case WS_CLIENT:
573                         list_add(&config->ws_clients, argvalstr(optc));
574                         break;
575
576                 case WS_SERVICE:
577                         list_add(&config->ws_servers, argvalstr(optc));
578                         break;
579
580                 case SO_BINDING:
581                         list_add(&config->so_bindings, argvalstr(optc));
582                         break;
583
584                 case SET_TRACEREQ:
585                         config->tracereq = argvalenum(optc, tracereq_desc);
586                         break;
587
588                 case SET_TRACEDITF:
589                         config->traceditf = argvalenum(optc, traceditf_desc);
590                         break;
591
592                 case SET_TRACESVC:
593                         config->tracesvc = argvalenum(optc, tracesvc_desc);
594                         break;
595
596                 case SET_TRACEEVT:
597                         config->traceevt = argvalenum(optc, traceevt_desc);
598                         break;
599
600                 case SET_TRACESES:
601                         config->traceses = argvalenum(optc, traceses_desc);
602                         break;
603
604                 case SET_NO_HTTPD:
605                         noarg(optc);
606                         config->noHttpd = 1;
607                         break;
608
609                 case SET_EXEC:
610                         config->exec = make_exec(&argv[optind]);
611                         optind = argc; /* stop option scanning */
612                         break;
613
614                 case SET_RNDTOKEN:
615                         config->random_token = 1;
616                         break;
617
618 #if defined(WITH_MONITORING_OPTION)
619                 case SET_MONITORING:
620                         config->monitoring = 1;
621                         break;
622 #endif
623
624                 case DISPLAY_VERSION:
625                         noarg(optc);
626                         printVersion(stdout);
627                         exit(0);
628
629                 case DISPLAY_HELP:
630                         printHelp(stdout, programName);
631                         exit(0);
632
633                 default:
634                         exit(1);
635                 }
636         }
637         free(gnuOptions);
638 }
639
640 static void fulfill_config(struct afb_config *config)
641 {
642         // default HTTP port
643         if (config->httpdPort == 0)
644                 config->httpdPort = 1234;
645
646         // default binding API timeout
647         if (config->apiTimeout == 0)
648                 config->apiTimeout = DEFLT_API_TIMEOUT;
649
650         // default AUTH_TOKEN
651         if (config->random_token)
652                 config->token = NULL;
653
654         // cache timeout default one hour
655         if (config->cacheTimeout == 0)
656                 config->cacheTimeout = DEFLT_CACHE_TIMEOUT;
657
658         // cache timeout default one hour
659         if (config->cntxTimeout == 0)
660                 config->cntxTimeout = DEFLT_CNTX_TIMEOUT;
661
662         // max count of sessions
663         if (config->nbSessionMax == 0)
664                 config->nbSessionMax = CTX_NBCLIENTS;
665
666         /* set directories */
667         if (config->workdir == NULL)
668                 config->workdir = ".";
669
670         if (config->rootdir == NULL)
671                 config->rootdir = ".";
672
673         if (config->uploaddir == NULL)
674                 config->uploaddir = ".";
675
676         // if no Angular/HTML5 rootbase let's try '/' as default
677         if (config->rootbase == NULL)
678                 config->rootbase = "/opa";
679
680         if (config->rootapi == NULL)
681                 config->rootapi = "/api";
682
683         if (config->ldpaths == NULL && config->weak_ldpaths == NULL && !config->no_ldpaths)
684                 list_add(&config->ldpaths, BINDING_INSTALL_DIR);
685
686 #if defined(WITH_MONITORING_OPTION)
687         if (config->monitoring)
688                 list_add(&config->aliases, strdup("/monitoring:"BINDING_INSTALL_DIR"/monitoring"));
689 #endif
690
691         // if no config dir create a default path from uploaddir
692         if (config->console == NULL) {
693                 config->console = malloc(512);
694                 strncpy(config->console, config->uploaddir, 512);
695                 strncat(config->console, "/AFB-console.out", 512);
696         }
697 }
698
699 void afb_config_dump(struct afb_config *config)
700 {
701         struct afb_config_list *l;
702         struct enumdesc *e;
703         char **v;
704
705 #define NN(x)   (x)?:""
706 #define P(...)  fprintf(stderr, __VA_ARGS__)
707 #define PF(x)   P("-- %15s: ", #x)
708 #define PE      P("\n")
709 #define S(x)    PF(x);P("%s",NN(config->x));PE;
710 #define D(x)    PF(x);P("%d",config->x);PE;
711 #define B(x)    PF(x);P("%s",config->x?"yes":"no");PE;
712 #define L(x)    PF(x);l=config->x;if(l){P("%s\n",NN(l->value));for(l=l->next;l;l=l->next)P("-- %15s  %s\n","",NN(l->value));}else PE;
713 #define E(x,d)  for(e=d;e->name&&e->value!=config->x;e++);if(e->name){PF(x);P("%s",e->name);PE;}else{D(x);}
714 #define V(x)    P("-- %15s:", #x);for(v=config->x;v&&*v;v++)P(" %s",*v); PE;
715
716         P("---BEGIN-OF-CONFIG---\n");
717         S(console)
718         S(rootdir)
719         S(roothttp)
720         S(rootbase)
721         S(rootapi)
722         S(workdir)
723         S(uploaddir)
724         S(token)
725         S(name)
726
727         L(aliases)
728         L(dbus_clients)
729         L(dbus_servers)
730         L(ws_clients)
731         L(ws_servers)
732         L(so_bindings)
733         L(ldpaths)
734         L(weak_ldpaths)
735         L(calls)
736
737         V(exec)
738
739         D(httpdPort)
740         D(cacheTimeout)
741         D(apiTimeout)
742         D(cntxTimeout)
743         D(nbSessionMax)
744
745         E(mode,mode_desc)
746         E(tracereq,tracereq_desc)
747         E(traceditf,traceditf_desc)
748         E(tracesvc,tracesvc_desc)
749         E(traceevt,traceevt_desc)
750         E(traceses,traceses_desc)
751
752         B(no_ldpaths)
753         B(noHttpd)
754         B(background)
755 #if defined(WITH_MONITORING_OPTION)
756         B(monitoring)
757 #endif
758         B(random_token)
759         P("---END-OF-CONFIG---\n");
760
761 #undef V
762 #undef E
763 #undef L
764 #undef B
765 #undef D
766 #undef S
767 #undef PE
768 #undef PF
769 #undef P
770 #undef NN
771 }
772
773 static void on_environment_list(struct afb_config_list **to, const char *name)
774 {
775         char *value = getenv(name);
776
777         if (value)
778                 list_add(to, value);
779 }
780
781 static void on_environment_enum(int *to, const char *name, struct enumdesc *desc)
782 {
783         char *value = getenv(name);
784
785         if (value) {
786                 while (desc->name) {
787                         if (strcmp(desc->name, value))
788                                 desc++;
789                         else {
790                                 *to = desc->value;
791                                 return;
792                         }
793                 }
794                 WARNING("Unknown value %s for environment variable %s, ignored", value, name);
795         }
796 }
797
798 static void parse_environment(struct afb_config *config)
799 {
800         on_environment_enum(&config->tracereq, "AFB_TRACEREQ", tracereq_desc);
801         on_environment_enum(&config->traceditf, "AFB_TRACEDITF", traceditf_desc);
802         on_environment_enum(&config->tracesvc, "AFB_TRACESVC", tracesvc_desc);
803         on_environment_enum(&config->traceevt, "AFB_TRACEEVT", traceevt_desc);
804         on_environment_enum(&config->traceses, "AFB_TRACESES", traceses_desc);
805         on_environment_list(&config->ldpaths, "AFB_LDPATHS");
806 }
807
808 struct afb_config *afb_config_parse_arguments(int argc, char **argv)
809 {
810         struct afb_config *result;
811
812         result = calloc(1, sizeof *result);
813
814         parse_environment(result);
815         parse_arguments(argc, argv, result);
816         fulfill_config(result);
817         if (verbosity >= 3)
818                 afb_config_dump(result);
819         return result;
820 }
821
822 struct json_object *afb_config_json(struct afb_config *config)
823 {
824         struct json_object *r, *a;
825         struct afb_config_list *l;
826         struct enumdesc *e;
827         char **v;
828
829 #define XA(t,o)         json_object_array_add(t,o);
830 #define XO(t,x,o)       json_object_object_add(t,x,o);
831 #define YS(s)           ((s)?json_object_new_string(s):NULL)
832
833 #define AO(o)           XA(a,o)
834 #define AS(s)           AO(YS(s))
835 #define RO(x,o)         XO(r,x,o)
836 #define RS(x,s)         RO(x,YS(s))
837 #define RA(x)           RO(x,(a=json_object_new_array()))
838 #define RI(x,i)         RO(x,json_object_new_int(i))
839 #define RB(x,b)         RO(x,json_object_new_boolean(b))
840
841 #define S(x)            RS(#x,config->x)
842 #define V(x)            RA(#x);for(v=config->x;v&&*v;v++)AS(*v);
843 #define L(x)            RA(#x);for(l=config->x;l;l=l->next)AS(l->value);
844 #define D(x)            RI(#x,config->x)
845 #define B(x)            RB(#x,config->x)
846 #define E(x,d)          for(e=d;e->name&&e->value!=config->x;e++);if(e->name){RS(#x,e->name);}else{D(x);}
847
848         r = json_object_new_object();
849         S(console)
850         S(rootdir)
851         S(roothttp)
852         S(rootbase)
853         S(rootapi)
854         S(workdir)
855         S(uploaddir)
856         S(token)
857         S(name)
858
859         L(aliases)
860         L(dbus_clients)
861         L(dbus_servers)
862         L(ws_clients)
863         L(ws_servers)
864         L(so_bindings)
865         L(ldpaths)
866         L(weak_ldpaths)
867         L(calls)
868
869         V(exec)
870
871         D(httpdPort)
872         D(cacheTimeout)
873         D(apiTimeout)
874         D(cntxTimeout)
875         D(nbSessionMax)
876
877         E(mode,mode_desc)
878         E(tracereq,tracereq_desc)
879         E(traceditf,traceditf_desc)
880         E(tracesvc,tracesvc_desc)
881         E(traceevt,traceevt_desc)
882         E(traceses,traceses_desc)
883
884         B(no_ldpaths)
885         B(noHttpd)
886         B(background)
887 #if defined(WITH_MONITORING_OPTION)
888         B(monitoring)
889 #endif
890         B(random_token)
891
892 #undef E
893 #undef B
894 #undef D
895 #undef L
896 #undef V
897 #undef S
898
899 #undef RB
900 #undef RI
901 #undef RA
902 #undef RS
903 #undef RS
904 #undef AS
905 #undef AO
906
907 #undef YS
908 #undef XO
909 #undef XA
910
911         return r;
912 }
913