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 SET_AUTH_TOKEN 't'
75 #define SET_UPLOAD_DIR 'u'
76 #define DISPLAY_VERSION 'V'
77 #define SET_VERBOSE 'v'
78 #define SET_WORK_DIR 'w'
80 const char shortopts[] =
84 // Command line structure hold cli --command + help text
86 int val; // command number within application
87 int has_arg; // command number within application
88 char *name; // command as used in --xxxx cli
89 char *help; // help text
93 static AFB_options cliOptions[] = {
95 {SET_VERBOSE, 0, "verbose", "Verbose Mode, repeat to increase verbosity"},
96 {SET_QUIET, 0, "quiet", "Quiet Mode, repeat to decrease verbosity"},
98 {SET_NAME, 1, "name", "Set the visible name"},
100 {SET_TCP_PORT, 1, "port", "HTTP listening TCP port [default " STRINGIFY(AFS_SUPERVISOR_PORT) "]"},
101 {SET_ROOT_HTTP, 1, "roothttp", "HTTP Root Directory [default no root http (files not served but apis still available)]"},
102 {SET_ROOT_BASE, 1, "rootbase", "Angular Base Root URL [default /opa]"},
103 {SET_ROOT_API, 1, "rootapi", "HTML Root API URL [default /api]"},
105 {SET_APITIMEOUT, 1, "apitimeout", "Binding API timeout in seconds [default 10]"},
106 {SET_CNTXTIMEOUT, 1, "cntxtimeout", "Client Session Context Timeout [default 900]"},
107 {SET_CACHE_TIMEOUT, 1, "cache-eol", "Client cache end of live [default 3600]"},
109 {SET_WORK_DIR, 1, "workdir", "Set the working directory [default: $PWD or current working directory]"},
110 {SET_UPLOAD_DIR, 1, "uploaddir", "Directory for uploading files [default: workdir]"},
111 {SET_ROOT_DIR, 1, "rootdir", "Root Directory of the application [default: workdir]"},
112 {SET_SESSION_DIR, 1, "sessiondir", "OBSOLETE (was: Sessions file path)"},
114 {SET_AUTH_TOKEN, 1, "token", "Initial Secret [default=" AFS_SUPERVISOR_TOKEN ", use --token="" to allow any token]"},
116 {DISPLAY_VERSION, 0, "version", "Display version and copyright"},
117 {DISPLAY_HELP, 0, "help", "Display this help"},
119 {SET_SESSIONMAX, 1, "session-max", "Max count of session simultaneously [default 10]"},
132 /*----------------------------------------------------------
134 | print version and copyright
135 +--------------------------------------------------------- */
136 static void printVersion(FILE * file)
138 static const char version[] =
140 " afs-supervisor [Application Framework Supervisor] version="AFB_VERSION"\n"
142 " Copyright (C) 2015-2018 \"IoT.bzh\"\n"
143 " afs-supervisor comes with ABSOLUTELY NO WARRANTY.\n"
144 " Licence Apache 2\n"
147 fprintf(file, "%s", version);
150 /*----------------------------------------------------------
152 | print information from long option array
153 +--------------------------------------------------------- */
155 static void printHelp(FILE * file, const char *name)
160 fprintf(file, "%s:\nallowed options\n", name);
161 for (ind = 0; cliOptions[ind].name != NULL; ind++) {
162 strcpy(command, cliOptions[ind].name);
163 if (cliOptions[ind].has_arg)
164 strcat(command, "=xxxx");
165 fprintf(file, " --%-15s %s\n", command, cliOptions[ind].help);
168 "Example:\n %s --verbose --port=1234 --token='azerty'\n",
173 /*---------------------------------------------------------
174 | helpers for argument scanning
175 +--------------------------------------------------------- */
177 static const char *name_of_option(int optc)
179 AFB_options *o = cliOptions;
180 while (o->name && o->val != optc)
182 return o->name ? : "<unknown-option-name>";
185 static const char *current_argument(int optc)
188 ERROR("option [--%s] needs a value i.e. --%s=xxx",
189 name_of_option(optc), name_of_option(optc));
195 static char *argvalstr(int optc)
197 char *result = strdup(current_argument(optc));
198 if (result == NULL) {
199 ERROR("can't alloc memory");
205 static int argvalint(int optc, int mini, int maxi, int base)
207 const char *beg, *end;
209 beg = current_argument(optc);
210 val = strtol(beg, (char**)&end, base);
211 if (*end || end == beg) {
212 ERROR("option [--%s] requires a valid integer (found %s)",
213 name_of_option(optc), beg);
216 if (val < (long int)mini || val > (long int)maxi) {
217 ERROR("option [--%s] value out of bounds (not %d<=%ld<=%d)",
218 name_of_option(optc), mini, val, maxi);
224 static int argvalintdec(int optc, int mini, int maxi)
226 return argvalint(optc, mini, maxi, 10);
229 static void noarg(int optc)
232 ERROR("option [--%s] need no value (found %s)", name_of_option(optc), optarg);
237 /*---------------------------------------------------------
238 | Parse option and launch action
239 +--------------------------------------------------------- */
241 static void parse_arguments(int argc, char **argv, struct afs_config *config)
243 char *programName = argv[0];
246 struct option *gnuOptions;
248 // ------------------ Process Command Line -----------------------
250 // build GNU getopt info from cliOptions
251 nbcmd = sizeof(cliOptions) / sizeof(AFB_options);
252 gnuOptions = malloc(sizeof(*gnuOptions) * (unsigned)nbcmd);
253 for (ind = 0; ind < nbcmd; ind++) {
254 gnuOptions[ind].name = cliOptions[ind].name;
255 gnuOptions[ind].has_arg = cliOptions[ind].has_arg;
256 gnuOptions[ind].flag = 0;
257 gnuOptions[ind].val = cliOptions[ind].val;
260 // get all options from command line
261 while ((optc = getopt_long(argc, argv, shortopts, gnuOptions, NULL)) != EOF) {
272 config->httpdPort = argvalintdec(optc, 1024, 32767);
276 config->apiTimeout = argvalintdec(optc, 0, INT_MAX);
279 case SET_CNTXTIMEOUT:
280 config->cntxTimeout = argvalintdec(optc, 0, INT_MAX);
284 config->rootdir = argvalstr(optc);
285 INFO("Forcing Rootdir=%s", config->rootdir);
289 config->roothttp = argvalstr(optc);
290 INFO("Forcing Root HTTP=%s", config->roothttp);
294 config->rootbase = argvalstr(optc);
295 INFO("Forcing Rootbase=%s", config->rootbase);
299 config->rootapi = argvalstr(optc);
300 INFO("Forcing Rootapi=%s", config->rootapi);
304 config->token = argvalstr(optc);
308 config->uploaddir = argvalstr(optc);
312 config->workdir = argvalstr(optc);
315 case SET_CACHE_TIMEOUT:
316 config->cacheTimeout = argvalintdec(optc, 0, INT_MAX);
320 config->nbSessionMax = argvalintdec(optc, 1, INT_MAX);
324 config->name = argvalstr(optc);
327 case DISPLAY_VERSION:
329 printVersion(stdout);
333 printHelp(stdout, programName);
343 static void fulfill_config(struct afs_config *config)
346 if (config->httpdPort == 0)
347 config->httpdPort = AFS_SUPERVISOR_PORT;
349 // default binding API timeout
350 if (config->apiTimeout == 0)
351 config->apiTimeout = DEFLT_API_TIMEOUT;
353 // cache timeout default one hour
354 if (config->cacheTimeout == 0)
355 config->cacheTimeout = DEFLT_CACHE_TIMEOUT;
357 // cache timeout default one hour
358 if (config->cntxTimeout == 0)
359 config->cntxTimeout = DEFLT_CNTX_TIMEOUT;
361 // max count of sessions
362 if (config->nbSessionMax == 0)
363 config->nbSessionMax = CTX_NBCLIENTS;
365 /* set directories */
366 if (config->workdir == NULL)
367 config->workdir = ".";
369 if (config->rootdir == NULL)
370 config->rootdir = ".";
372 if (config->uploaddir == NULL)
373 config->uploaddir = ".";
375 // if no Angular/HTML5 rootbase let's try '/' as default
376 if (config->rootbase == NULL)
377 config->rootbase = "/opa";
379 if (config->rootapi == NULL)
380 config->rootapi = "/api";
382 if (config->token == NULL)
383 config->token = AFS_SUPERVISOR_TOKEN;
386 void afs_config_dump(struct afs_config *config)
388 #define NN(x) (x)?:""
389 #define P(...) fprintf(stderr, __VA_ARGS__)
390 #define PF(x) P("-- %15s: ", #x)
392 #define S(x) PF(x);P("%s",NN(config->x));PE;
393 #define D(x) PF(x);P("%d",config->x);PE;
395 P("---BEGIN-OF-CONFIG---\n");
410 P("---END-OF-CONFIG---\n");
424 static void parse_environment(struct afs_config *config)
428 struct afs_config *afs_config_parse_arguments(int argc, char **argv)
430 struct afs_config *result;
432 result = calloc(1, sizeof *result);
434 parse_environment(result);
435 parse_arguments(argc, argv, result);
436 fulfill_config(result);
438 afs_config_dump(result);