2 * Copyright (C) 2015-2018 "IoT.bzh"
3 * Author José Bollo <jose.bollo@iot.bzh>
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
28 #include "afs-config.h"
30 #if !defined(AFB_VERSION)
31 # error "you should define AFB_VERSION"
34 #if !defined(AFS_SUPERVISOR_TOKEN)
35 # define AFS_SUPERVISOR_TOKEN ""
37 #if !defined(AFS_SUPERVISOR_PORT)
38 # define AFS_SUPERVISOR_PORT 1619
40 #define _STRINGIFY_(x) #x
41 #define STRINGIFY(x) _STRINGIFY_(x)
44 #define DEFLT_CNTX_TIMEOUT 32000000 // default Client Connection
45 // Timeout: few more than one year
46 #define DEFLT_API_TIMEOUT 20 // default Plugin API Timeout [0=NoLimit
48 #define DEFLT_CACHE_TIMEOUT 100000 // default Static File Chache
51 #define CTX_NBCLIENTS 10 // allow a default of 10 authenticated
55 // Define command line option
56 #define SET_ROOT_DIR 6
57 #define SET_ROOT_BASE 7
58 #define SET_ROOT_API 8
60 #define SET_CACHE_TIMEOUT 10
61 #define SET_SESSION_DIR 11
63 #define SET_APITIMEOUT 14
64 #define SET_CNTXTIMEOUT 15
66 #define SET_SESSIONMAX 23
68 #define SET_ROOT_HTTP 26
70 #define DISPLAY_HELP 'h'
72 #define SET_TCP_PORT 'p'
74 #define WS_SERVICE 's'
75 #define SET_AUTH_TOKEN 't'
76 #define SET_UPLOAD_DIR 'u'
77 #define DISPLAY_VERSION 'V'
78 #define SET_VERBOSE 'v'
79 #define SET_WORK_DIR 'w'
81 const char shortopts[] =
85 // Command line structure hold cli --command + help text
87 int val; // command number within application
88 int has_arg; // command number within application
89 char *name; // command as used in --xxxx cli
90 char *help; // help text
94 static AFB_options cliOptions[] = {
96 {SET_VERBOSE, 0, "verbose", "Verbose Mode, repeat to increase verbosity"},
97 {SET_QUIET, 0, "quiet", "Quiet Mode, repeat to decrease verbosity"},
99 {SET_NAME, 1, "name", "Set the visible name"},
101 {SET_TCP_PORT, 1, "port", "HTTP listening TCP port [default " STRINGIFY(AFS_SUPERVISOR_PORT) "]"},
102 {SET_ROOT_HTTP, 1, "roothttp", "HTTP Root Directory [default no root http (files not served but apis still available)]"},
103 {SET_ROOT_BASE, 1, "rootbase", "Angular Base Root URL [default /opa]"},
104 {SET_ROOT_API, 1, "rootapi", "HTML Root API URL [default /api]"},
106 {SET_APITIMEOUT, 1, "apitimeout", "Binding API timeout in seconds [default 10]"},
107 {SET_CNTXTIMEOUT, 1, "cntxtimeout", "Client Session Context Timeout [default 900]"},
108 {SET_CACHE_TIMEOUT, 1, "cache-eol", "Client cache end of live [default 3600]"},
110 {SET_WORK_DIR, 1, "workdir", "Set the working directory [default: $PWD or current working directory]"},
111 {SET_UPLOAD_DIR, 1, "uploaddir", "Directory for uploading files [default: workdir]"},
112 {SET_ROOT_DIR, 1, "rootdir", "Root Directory of the application [default: workdir]"},
113 {SET_SESSION_DIR, 1, "sessiondir", "OBSOLETE (was: Sessions file path)"},
115 {SET_AUTH_TOKEN, 1, "token", "Initial Secret [default=" AFS_SUPERVISOR_TOKEN ", use --token="" to allow any token]"},
117 {WS_SERVICE, 1, "ws-server", "Provide supervisor as websocket"},
118 {DISPLAY_VERSION, 0, "version", "Display version and copyright"},
119 {DISPLAY_HELP, 0, "help", "Display this help"},
121 {SET_SESSIONMAX, 1, "session-max", "Max count of session simultaneously [default 10]"},
134 /*----------------------------------------------------------
136 | print version and copyright
137 +--------------------------------------------------------- */
138 static void printVersion(FILE * file)
140 static const char version[] =
142 " afs-supervisor [Application Framework Supervisor] version="AFB_VERSION"\n"
144 " Copyright (C) 2015-2018 \"IoT.bzh\"\n"
145 " afs-supervisor comes with ABSOLUTELY NO WARRANTY.\n"
146 " Licence Apache 2\n"
149 fprintf(file, "%s", version);
152 /*----------------------------------------------------------
154 | print information from long option array
155 +--------------------------------------------------------- */
157 static void printHelp(FILE * file, const char *name)
162 fprintf(file, "%s:\nallowed options\n", name);
163 for (ind = 0; cliOptions[ind].name != NULL; ind++) {
164 strcpy(command, cliOptions[ind].name);
165 if (cliOptions[ind].has_arg)
166 strcat(command, "=xxxx");
167 fprintf(file, " --%-15s %s\n", command, cliOptions[ind].help);
170 "Example:\n %s --verbose --port=1234 --token='azerty'\n",
175 /*---------------------------------------------------------
176 | helpers for argument scanning
177 +--------------------------------------------------------- */
179 static const char *name_of_option(int optc)
181 AFB_options *o = cliOptions;
182 while (o->name && o->val != optc)
184 return o->name ? : "<unknown-option-name>";
187 static const char *current_argument(int optc)
190 ERROR("option [--%s] needs a value i.e. --%s=xxx",
191 name_of_option(optc), name_of_option(optc));
197 static char *argvalstr(int optc)
199 char *result = strdup(current_argument(optc));
200 if (result == NULL) {
201 ERROR("can't alloc memory");
207 static int argvalint(int optc, int mini, int maxi, int base)
209 const char *beg, *end;
211 beg = current_argument(optc);
212 val = strtol(beg, (char**)&end, base);
213 if (*end || end == beg) {
214 ERROR("option [--%s] requires a valid integer (found %s)",
215 name_of_option(optc), beg);
218 if (val < (long int)mini || val > (long int)maxi) {
219 ERROR("option [--%s] value out of bounds (not %d<=%ld<=%d)",
220 name_of_option(optc), mini, val, maxi);
226 static int argvalintdec(int optc, int mini, int maxi)
228 return argvalint(optc, mini, maxi, 10);
231 static void noarg(int optc)
234 ERROR("option [--%s] need no value (found %s)", name_of_option(optc), optarg);
239 /*---------------------------------------------------------
240 | Parse option and launch action
241 +--------------------------------------------------------- */
243 static void parse_arguments(int argc, char **argv, struct afs_config *config)
245 char *programName = argv[0];
248 struct option *gnuOptions;
250 // ------------------ Process Command Line -----------------------
252 // build GNU getopt info from cliOptions
253 nbcmd = sizeof(cliOptions) / sizeof(AFB_options);
254 gnuOptions = malloc(sizeof(*gnuOptions) * (unsigned)nbcmd);
255 for (ind = 0; ind < nbcmd; ind++) {
256 gnuOptions[ind].name = cliOptions[ind].name;
257 gnuOptions[ind].has_arg = cliOptions[ind].has_arg;
258 gnuOptions[ind].flag = 0;
259 gnuOptions[ind].val = cliOptions[ind].val;
262 // get all options from command line
263 while ((optc = getopt_long(argc, argv, shortopts, gnuOptions, NULL)) != EOF) {
274 config->httpdPort = argvalintdec(optc, 1024, 32767);
278 config->apiTimeout = argvalintdec(optc, 0, INT_MAX);
281 case SET_CNTXTIMEOUT:
282 config->cntxTimeout = argvalintdec(optc, 0, INT_MAX);
286 config->rootdir = argvalstr(optc);
287 INFO("Forcing Rootdir=%s", config->rootdir);
291 config->roothttp = argvalstr(optc);
292 INFO("Forcing Root HTTP=%s", config->roothttp);
296 config->rootbase = argvalstr(optc);
297 INFO("Forcing Rootbase=%s", config->rootbase);
301 config->rootapi = argvalstr(optc);
302 INFO("Forcing Rootapi=%s", config->rootapi);
306 config->token = argvalstr(optc);
310 config->uploaddir = argvalstr(optc);
314 config->workdir = argvalstr(optc);
317 case SET_CACHE_TIMEOUT:
318 config->cacheTimeout = argvalintdec(optc, 0, INT_MAX);
322 config->nbSessionMax = argvalintdec(optc, 1, INT_MAX);
326 config->name = argvalstr(optc);
330 config->ws_server = argvalstr(optc);
333 case DISPLAY_VERSION:
335 printVersion(stdout);
339 printHelp(stdout, programName);
349 static void fulfill_config(struct afs_config *config)
352 if (config->httpdPort == 0)
353 config->httpdPort = AFS_SUPERVISOR_PORT;
355 // default binding API timeout
356 if (config->apiTimeout == 0)
357 config->apiTimeout = DEFLT_API_TIMEOUT;
359 // cache timeout default one hour
360 if (config->cacheTimeout == 0)
361 config->cacheTimeout = DEFLT_CACHE_TIMEOUT;
363 // cache timeout default one hour
364 if (config->cntxTimeout == 0)
365 config->cntxTimeout = DEFLT_CNTX_TIMEOUT;
367 // max count of sessions
368 if (config->nbSessionMax == 0)
369 config->nbSessionMax = CTX_NBCLIENTS;
371 /* set directories */
372 if (config->workdir == NULL)
373 config->workdir = ".";
375 if (config->rootdir == NULL)
376 config->rootdir = ".";
378 if (config->uploaddir == NULL)
379 config->uploaddir = ".";
381 // if no Angular/HTML5 rootbase let's try '/' as default
382 if (config->rootbase == NULL)
383 config->rootbase = "/opa";
385 if (config->rootapi == NULL)
386 config->rootapi = "/api";
388 if (config->token == NULL)
389 config->token = AFS_SUPERVISOR_TOKEN;
392 void afs_config_dump(struct afs_config *config)
394 #define NN(x) (x)?:""
395 #define P(...) fprintf(stderr, __VA_ARGS__)
396 #define PF(x) P("-- %15s: ", #x)
398 #define S(x) PF(x);P("%s",NN(config->x));PE;
399 #define D(x) PF(x);P("%d",config->x);PE;
401 P("---BEGIN-OF-CONFIG---\n");
417 P("---END-OF-CONFIG---\n");
431 static void parse_environment(struct afs_config *config)
435 struct afs_config *afs_config_parse_arguments(int argc, char **argv)
437 struct afs_config *result;
439 result = calloc(1, sizeof *result);
441 parse_environment(result);
442 parse_arguments(argc, argv, result);
443 fulfill_config(result);
444 if (verbose_wants(Log_Level_Info))
445 afs_config_dump(result);