- char* programName = argv [0];
- int optionIndex = 0;
- int optc, ind;
- int nbcmd;
- struct option *gnuOptions;
-
- // ------------------ 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);
- for (ind=0; ind < nbcmd;ind++) {
- gnuOptions [ind].name = cliOptions[ind].name;
- gnuOptions [ind].has_arg = cliOptions[ind].has_arg;
- gnuOptions [ind].flag = 0;
- gnuOptions [ind].val = cliOptions[ind].val;
- }
-
- // get all options from command line
- while ((optc = getopt_long (argc, argv, "vsp?", gnuOptions, &optionIndex))
- != EOF)
- {
- switch (optc)
- {
- case SET_VERBOSE:
- verbosity++;
- break;
-
- case SET_TCP_PORT:
- if (optarg == 0) goto needValueForOption;
- if (!sscanf (optarg, "%d", &config->httpdPort)) goto notAnInteger;
- break;
-
- case SET_APITIMEOUT:
- if (optarg == 0) goto needValueForOption;
- if (!sscanf (optarg, "%d", &config->apiTimeout)) goto notAnInteger;
- break;
-
- case SET_CNTXTIMEOUT:
- if (optarg == 0) goto needValueForOption;
- if (!sscanf (optarg, "%d", &config->cntxTimeout)) goto notAnInteger;
- break;
-
- case SET_ROOT_DIR:
- if (optarg == 0) goto needValueForOption;
- config->rootdir = optarg;
- INFO("Forcing Rootdir=%s",config->rootdir);
- break;
-
- case SET_ROOT_BASE:
- if (optarg == 0) goto needValueForOption;
- config->rootbase = optarg;
- INFO("Forcing Rootbase=%s",config->rootbase);
- break;
-
- case SET_ROOT_API:
- if (optarg == 0) goto needValueForOption;
- config->rootapi = optarg;
- INFO("Forcing Rootapi=%s",config->rootapi);
- break;
-
- case SET_ALIAS:
- if (optarg == 0) goto needValueForOption;
- if ((unsigned)config->aliascount < sizeof (config->aliasdir) / sizeof (config->aliasdir[0])) {
- config->aliasdir[config->aliascount].url = strsep(&optarg,":");
- if (optarg == NULL) {
- ERROR("missing ':' in alias %s, ignored", config->aliasdir[config->aliascount].url);
- } else {
- config->aliasdir[config->aliascount].path = optarg;
- INFO("Alias url=%s path=%s", config->aliasdir[config->aliascount].url, config->aliasdir[config->aliascount].path);
- config->aliascount++;
- }
- } else {
- ERROR("Too many aliases [max:%d] %s ignored", MAX_ALIAS, optarg);
- }
- break;
-
- case SET_AUTH_TOKEN:
- if (optarg == 0) goto needValueForOption;
- config->token = optarg;
- break;
-
- case SET_LDPATH:
- if (optarg == 0) goto needValueForOption;
- config->ldpaths = optarg;
- break;
-
- case SET_SESSION_DIR:
- if (optarg == 0) goto needValueForOption;
- config->sessiondir = optarg;
- break;
-
- case SET_CACHE_TIMEOUT:
- if (optarg == 0) goto needValueForOption;
- if (!sscanf (optarg, "%d", &config->cacheTimeout)) goto notAnInteger;
- break;
-
- case SET_SESSIONMAX:
- if (optarg == 0) goto needValueForOption;
- if (!sscanf (optarg, "%d", &config->nbSessionMax)) goto notAnInteger;
- break;
-
- case SET_FORGROUND:
- if (optarg != 0) goto noValueForOption;
- config->background = 0;
- break;
-
- case SET_BACKGROUND:
- if (optarg != 0) goto noValueForOption;
- config->background = 1;
- break;
-
- case SET_MODE:
- if (optarg == 0) goto needValueForOption;
- if (!strcmp(optarg, "local")) config->mode = AFB_MODE_LOCAL;
- else if (!strcmp(optarg, "remote")) config->mode = AFB_MODE_REMOTE;
- else if (!strcmp(optarg, "global")) config->mode = AFB_MODE_GLOBAL;
- else goto badMode;
- break;
-
- case SET_READYFD:
- if (optarg == 0) goto needValueForOption;
- if (!sscanf (optarg, "%u", &config->readyfd)) goto notAnInteger;
- break;
-
- case DBUS_CLIENT:
- case DBUS_SERVICE:
- case SO_PLUGIN:
- if (optarg == 0) goto needValueForOption;
- add_item(config, optc, optarg);
- break;
-
- case DISPLAY_VERSION:
- if (optarg != 0) goto noValueForOption;
- printVersion(stdout);
- break;
-
- case DISPLAY_HELP:
- default:
- printHelp(stdout, programName);
- exit(0);
- }
- }
- free(gnuOptions);
-
- config_set_default (config);
- return;
-
-
-needValueForOption:
- ERROR("AFB-daemon option [--%s] need a value i.e. --%s=xxx"
- ,gnuOptions[optionIndex].name, gnuOptions[optionIndex].name);
- exit (1);
-
-notAnInteger:
- ERROR("AFB-daemon option [--%s] requirer an interger i.e. --%s=9"
- ,gnuOptions[optionIndex].name, gnuOptions[optionIndex].name);
- exit (1);
-
-noValueForOption:
- ERROR("AFB-daemon option [--%s] don't take value"
- ,gnuOptions[optionIndex].name);
- exit (1);
-
-badMode:
- ERROR("AFB-daemon option [--%s] only accepts local, global or remote."
- ,gnuOptions[optionIndex].name);
- exit (1);