// Define command line option
-#define SET_VERBOSE 1
+#define SET_VERBOSE 'v'
#define SET_BACKGROUND 2
#define SET_FORGROUND 3
+#define SET_QUIET 'q'
#define SET_TCP_PORT 5
#define SET_ROOT_DIR 6
#define SET_APITIMEOUT 14
#define SET_CNTXTIMEOUT 15
-#define DISPLAY_VERSION 16
-#define DISPLAY_HELP 17
+#define DISPLAY_VERSION 'V'
+#define DISPLAY_HELP 'h'
#define SET_MODE 18
#define SET_READYFD 19
#define SET_TRACEREQ 27
+#define SET_NO_HTTPD 28
+
+#define SET_EXEC 'e'
+
+#define SHORTOPTS "vqhVe"
+
// Command line structure hold cli --command + help text
typedef struct {
int val; // command number within application
static AFB_options cliOptions[] = {
/* *INDENT-OFF* */
{SET_VERBOSE, 0, "verbose", "Verbose Mode, repeat to increase verbosity"},
+ {SET_QUIET, 0, "quiet", "Quiet Mode, repeat to decrease verbosity"},
{SET_FORGROUND, 0, "foreground", "Get all in foreground mode"},
{SET_BACKGROUND, 0, "daemon", "Get all in background mode"},
{SET_SESSION_DIR, 1, "sessiondir", "Sessions file path [default rootdir/sessions]"},
- {SET_LDPATH, 1, "ldpaths", "Load bindingss from dir1:dir2:... [default = " BINDING_INSTALL_DIR "]"},
+ {SET_LDPATH, 1, "ldpaths", "Load bindings from dir1:dir2:... [default = " BINDING_INSTALL_DIR "]"},
{SET_AUTH_TOKEN, 1, "token", "Initial Secret [default=no-session, --token= for session without authentication]"},
{DISPLAY_VERSION, 0, "version", "Display version and copyright"},
{SET_TRACEREQ, 1, "tracereq", "Log the requests: no, common, extra, all"},
+ {SET_NO_HTTPD, 0, "no-httpd", "Forbids HTTP service"},
+ {SET_EXEC, 0, "exec", "Execute the remaining arguments"},
+
{0, 0, NULL, NULL}
/* *INDENT-ON* */
};
" 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");
- exit(0);
}
/*----------------------------------------------------------
fprintf(file, " --%-15s %s\n", command, cliOptions[ind].help);
}
fprintf(file,
- "Example:\n %s\\\n --verbose --port=1234 --token='azerty' --ldpaths=build/bindings:/usr/lib64/agl/bindings\n",
+ "Example:\n %s --verbose --port=1234 --token='azerty' --ldpaths=build/bindings:/usr/lib64/agl/bindings\n",
name);
}
-// load config from disk and merge with CLI option
-static void config_set_default(struct afb_config *config)
-{
- // default HTTP port
- if (config->httpdPort == 0)
- config->httpdPort = 1234;
-
- // default binding API timeout
- if (config->apiTimeout == 0)
- config->apiTimeout = DEFLT_API_TIMEOUT;
-
- // default AUTH_TOKEN
- if (config->token == NULL)
- config->token = DEFLT_AUTH_TOKEN;
-
- // cache timeout default one hour
- if (config->cacheTimeout == 0)
- config->cacheTimeout = DEFLT_CACHE_TIMEOUT;
-
- // cache timeout default one hour
- if (config->cntxTimeout == 0)
- config->cntxTimeout = DEFLT_CNTX_TIMEOUT;
-
- // max count of sessions
- if (config->nbSessionMax == 0)
- config->nbSessionMax = CTX_NBCLIENTS;
-
- if (config->rootdir == NULL) {
- config->rootdir = getenv("AFBDIR");
- if (config->rootdir == NULL) {
- config->rootdir = malloc(512);
- strncpy(config->rootdir, getenv("HOME"), 512);
- strncat(config->rootdir, "/.AFB", 512);
- }
- }
- // if no Angular/HTML5 rootbase let's try '/' as default
- if (config->rootbase == NULL)
- config->rootbase = "/opa";
-
- if (config->rootapi == NULL)
- config->rootapi = "/api";
-
- if (config->ldpaths == NULL)
- config->ldpaths = BINDING_INSTALL_DIR;
-
- // if no session dir create a default path from rootdir
- if (config->sessiondir == NULL) {
- config->sessiondir = malloc(512);
- strncpy(config->sessiondir, config->rootdir, 512);
- strncat(config->sessiondir, "/sessions", 512);
- }
- // if no config dir create a default path from sessiondir
- if (config->console == NULL) {
- config->console = malloc(512);
- strncpy(config->console, config->sessiondir, 512);
- strncat(config->console, "/AFB-console.out", 512);
- }
-}
-
-/*---------------------------------------------------------
- | main
- | Parse option and launch action
+/*----------------------------------------------------------
+ | adds a string to the list
+--------------------------------------------------------- */
-
static void list_add(struct afb_config_list **head, char *value)
{
struct afb_config_list *item;
item->next = NULL;
}
+/*---------------------------------------------------------
+ | helpers for argument scanning
+ +--------------------------------------------------------- */
+
static char *argvalstr(int index)
{
if (optarg == 0) {
}
}
+/*---------------------------------------------------------
+ | Parse option and launch action
+ +--------------------------------------------------------- */
+
static void parse_arguments(int argc, char **argv, struct afb_config *config)
{
char *programName = argv[0];
// ------------------ Process Command Line -----------------------
- // if no argument print help and return
- if (argc < 2) {
- printHelp(stderr, programName);
- exit(1);
- }
// build GNU getopt info from cliOptions
nbcmd = sizeof(cliOptions) / sizeof(AFB_options);
gnuOptions = malloc(sizeof(*gnuOptions) * (unsigned)nbcmd);
}
// get all options from command line
- while ((optc =
- getopt_long(argc, argv, "vsp?", gnuOptions, &optionIndex))
- != EOF) {
+ while ((optc = getopt_long(argc, argv, SHORTOPTS, gnuOptions, &optionIndex)) != EOF) {
switch (optc) {
case SET_VERBOSE:
verbosity++;
break;
+ case SET_QUIET:
+ verbosity--;
+ break;
+
case SET_TCP_PORT:
config->httpdPort = argvalintdec(optionIndex, 1024, 32767);
break;
break;
case SET_LDPATH:
- config->ldpaths = argvalstr(optionIndex);
+ list_add(&config->ldpaths, argvalstr(optionIndex));
break;
case SET_SESSION_DIR:
config->tracereq = argvalenum(optionIndex, tracereq_desc);
break;
+ case SET_NO_HTTPD:
+ noarg(optionIndex);
+ config->noHttpd = 1;
+ break;
+
+ case SET_EXEC:
+ config->exec = &argv[optind];
+ optind = argc;
+ break;
+
case DISPLAY_VERSION:
noarg(optionIndex);
printVersion(stdout);
- break;
+ exit(0);
case DISPLAY_HELP:
- default:
printHelp(stdout, programName);
exit(0);
+
+ default:
+ exit(1);
}
}
free(gnuOptions);
+}
+
+// load config from disk and merge with CLI option
+static void config_set_default(struct afb_config *config)
+{
+ // default HTTP port
+ if (config->httpdPort == 0)
+ config->httpdPort = 1234;
+
+ // default binding API timeout
+ if (config->apiTimeout == 0)
+ config->apiTimeout = DEFLT_API_TIMEOUT;
+
+ // default AUTH_TOKEN
+ if (config->token == NULL)
+ config->token = DEFLT_AUTH_TOKEN;
+
+ // cache timeout default one hour
+ if (config->cacheTimeout == 0)
+ config->cacheTimeout = DEFLT_CACHE_TIMEOUT;
+
+ // cache timeout default one hour
+ if (config->cntxTimeout == 0)
+ config->cntxTimeout = DEFLT_CNTX_TIMEOUT;
- config_set_default(config);
+ // max count of sessions
+ if (config->nbSessionMax == 0)
+ config->nbSessionMax = CTX_NBCLIENTS;
+
+ if (config->rootdir == NULL) {
+ config->rootdir = getenv("AFBDIR");
+ if (config->rootdir == NULL) {
+ config->rootdir = malloc(512);
+ strncpy(config->rootdir, getenv("HOME"), 512);
+ strncat(config->rootdir, "/.AFB", 512);
+ }
+ }
+ // if no Angular/HTML5 rootbase let's try '/' as default
+ if (config->rootbase == NULL)
+ config->rootbase = "/opa";
+
+ if (config->rootapi == NULL)
+ config->rootapi = "/api";
+
+ if (config->ldpaths == NULL)
+ list_add(&config->ldpaths, BINDING_INSTALL_DIR);
+
+ // if no session dir create a default path from rootdir
+ if (config->sessiondir == NULL) {
+ config->sessiondir = malloc(512);
+ strncpy(config->sessiondir, config->rootdir, 512);
+ strncat(config->sessiondir, "/sessions", 512);
+ }
+ // if no config dir create a default path from sessiondir
+ if (config->console == NULL) {
+ config->console = malloc(512);
+ strncpy(config->console, config->sessiondir, 512);
+ strncat(config->console, "/AFB-console.out", 512);
+ }
+}
+
+void afb_config_dump(struct afb_config *config)
+{
+ struct afb_config_list *l;
+ struct enumdesc *e;
+ char **v;
+
+#define NN(x) (x)?:""
+#define P(...) fprintf(stderr, __VA_ARGS__)
+#define PF(x) P("-- %15s: ", #x)
+#define PE P("\n")
+#define S(x) PF(x);P("%s",NN(config->x));PE;
+#define D(x) PF(x);P("%d",config->x);PE;
+#define H(x) PF(x);P("%x",config->x);PE;
+#define B(x) PF(x);P("%s",config->x?"yes":"no");PE;
+#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;
+#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);}
+#define V(x) P("-- %15s:", #x);for(v=config->x;v&&*v;v++)P(" %s",*v); PE;
+
+ P("---BEGIN-OF-CONFIG---\n");
+ S(console)
+ S(rootdir)
+ S(roothttp)
+ S(rootbase)
+ S(rootapi)
+ S(sessiondir)
+ S(token)
+
+ L(aliases)
+ L(dbus_clients)
+ L(dbus_servers)
+ L(ws_clients)
+ L(ws_servers)
+ L(so_bindings)
+ L(ldpaths)
+
+ V(exec)
+
+ D(httpdPort)
+ B(background)
+ D(readyfd)
+ D(cacheTimeout)
+ D(apiTimeout)
+ D(cntxTimeout)
+ D(nbSessionMax)
+ E(mode,mode_desc)
+ E(tracereq,tracereq_desc)
+ B(noHttpd)
+ P("---END-OF-CONFIG---\n");
+
+#undef V
+#undef E
+#undef L
+#undef B
+#undef H
+#undef D
+#undef S
+#undef PE
+#undef PF
+#undef P
+#undef NN
}
struct afb_config *afb_config_parse_arguments(int argc, char **argv)
result = calloc(1, sizeof *result);
parse_arguments(argc, argv, result);
+ config_set_default(result);
+ if (verbosity >= 3)
+ afb_config_dump(result);
return result;
}
+