X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Fmain.c;h=da5984ec25dcc2b955bbb37d8396aac4dbe60e7d;hb=b841c268ae2d7572bc2caf21263692737cfe5648;hp=78d7f79e62b1e25c9a00510bc3ac60ae676f0e1f;hpb=1edf05100332c85ee28abc0754fcb825648157c4;p=src%2Fapp-framework-binder.git diff --git a/src/main.c b/src/main.c index 78d7f79e..da5984ec 100644 --- a/src/main.c +++ b/src/main.c @@ -31,6 +31,8 @@ #include #include +#define AFB_VERSION "0.1" + static sigjmp_buf exitPoint; // context save for set/longjmp /*---------------------------------------------------------- @@ -40,7 +42,7 @@ static sigjmp_buf exitPoint; // context save for set/longjmp static void printVersion (void) { fprintf (stderr,"\n----------------------------------------- \n"); - fprintf (stderr,"| AFB [Application Framework Binder] version=%s |\n", AJQ_VERSION); + fprintf (stderr,"| AFB [Application Framework Binder] version=%s |\n", AFB_VERSION); fprintf (stderr,"----------------------------------------- \n"); fprintf (stderr,"| Copyright(C) 2015 Fulup Ar Foll /IoT.bzh [fulup -at- iot.bzh]\n"); fprintf (stderr,"| AFB comes with ABSOLUTELY NO WARRANTY.\n"); @@ -82,6 +84,14 @@ static sigjmp_buf exitPoint; // context save for set/longjmp #define SET_MODE 160 #define SET_READYFD 161 +// Command line structure hold cli --command + help text +typedef struct { + int val; // command number within application + int has_arg; // command number within application + char *name; // command as used in --xxxx cli + char *help; // help text +} AFB_options; + // Supported option static AFB_options cliOptions [] = { @@ -117,7 +127,7 @@ static AFB_options cliOptions [] = { {SET_MODE ,1,"mode" , "set the mode: either local, remote or global"}, {SET_READYFD ,1,"readyfd" , "set the #fd to signal when ready"}, - {0, 0, 0} + {0, 0, NULL, NULL} }; static AFB_aliasdir aliasdir[MAX_ALIAS]; @@ -277,7 +287,6 @@ int main(int argc, char *argv[]) { cliconfig.aliasdir = aliasdir; // GNU CLI getopts nterface. - struct option ggcOption; struct option *gnuOptions; // ------------------ Process Command Line ----------------------- @@ -285,12 +294,12 @@ int main(int argc, char *argv[]) { // if no argument print help and return if (argc < 2) { printHelp(programName); - return (-1); + return 1; } // build GNU getopt info from cliOptions nbcmd = sizeof (cliOptions) / sizeof (AFB_options); - gnuOptions = malloc (sizeof (ggcOption) * nbcmd); + 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; @@ -345,12 +354,16 @@ int main(int argc, char *argv[]) { if (optarg == 0) goto needValueForOption; if (aliascount < MAX_ALIAS) { aliasdir[aliascount].url = strsep(&optarg,":"); - aliasdir[aliascount].path = strsep(&optarg,":"); - aliasdir[aliascount].len = strlen(aliasdir[aliascount].url); - if (verbose) fprintf(stderr, "Alias url=%s path=%s\n", aliasdir[aliascount].url, aliasdir[aliascount].path); - aliascount++; + if (optarg == NULL) { + fprintf(stderr, "missing ':' in alias %s, ignored\n", aliasdir[aliascount].url); + } else { + aliasdir[aliascount].path = optarg; + aliasdir[aliascount].len = strlen(aliasdir[aliascount].url); + if (verbose) fprintf(stderr, "Alias url=%s path=%s\n", aliasdir[aliascount].url, aliasdir[aliascount].path); + aliascount++; + } } else { - fprintf(stderr, "Too many aliases [max:%s] %s ignored\n", optarg, MAX_ALIAS-1); + fprintf(stderr, "Too many aliases [max:%d] %s ignored\n", MAX_ALIAS, optarg); } break; @@ -397,7 +410,7 @@ int main(int argc, char *argv[]) { case SET_USERID: if (optarg == 0) goto needValueForOption; - if (!sscanf (optarg, "%s", &cliconfig.setuid)) goto notAnInteger; + cliconfig.setuid = optarg; break; case SET_FAKE_MOD: @@ -458,7 +471,7 @@ int main(int argc, char *argv[]) { // ------------------ sanity check ---------------------------------------- if ((session->background) && (session->foreground)) { fprintf (stderr, "%s ERR: cannot select foreground & background at the same time\n",configTime()); - exit (-1); + exit (1); } // ------------------ Some useful default values ------------------------- @@ -495,7 +508,7 @@ int main(int argc, char *argv[]) { // ------------------ clean exit on CTR-C signal ------------------------ if (signal (SIGINT, signalQuit) == SIG_ERR) { fprintf (stderr, "%s Quit Signal received.",configTime()); - return (-1); + return 1; } // save exitPoint context when returning from longjmp closeSession and exit @@ -611,61 +624,57 @@ normalExit: // ------------- Fatal ERROR display error and quit ------------- errorSetuid: fprintf (stderr,"\nERR:AFB-daemon Failed to change UID to username=[%s]\n\n", session->config->setuid); - exit (-1); + exit (1); //errorNoRoot: // fprintf (stderr,"\nERR:AFB-daemon Not allow to run as root [use --seteuid=username option]\n\n"); -// exit (-1); +// exit (1); errorPidFile: fprintf (stderr,"\nERR:AFB-daemon Failed to write pid file [%s]\n\n", session->config->pidfile); - exit (-1); + exit (1); errorFork: fprintf (stderr,"\nERR:AFB-daemon Failed to fork son process\n\n"); - exit (-1); + exit (1); needValueForOption: fprintf (stderr,"\nERR:AFB-daemon option [--%s] need a value i.e. --%s=xxx\n\n" ,gnuOptions[optionIndex].name, gnuOptions[optionIndex].name); - exit (-1); + exit (1); noValueForOption: fprintf (stderr,"\nERR:AFB-daemon option [--%s] don't take value\n\n" ,gnuOptions[optionIndex].name); - exit (-1); + exit (1); notAnInteger: fprintf (stderr,"\nERR:AFB-daemon option [--%s] requirer an interger i.e. --%s=9\n\n" ,gnuOptions[optionIndex].name, gnuOptions[optionIndex].name); - exit (-1); + exit (1); badMode: fprintf (stderr,"\nERR:AFB-daemon option [--%s] only accepts local, global or remote.\n\n" ,gnuOptions[optionIndex].name); - exit (-1); + exit (1); exitOnSignal: fprintf (stderr,"\n%s INF:AFB-daemon pid=%d received exit signal (Hopefully crtl-C or --kill-previous !!!)\n\n" ,configTime(), getpid()); - exit (-1); + exit (1); errConsole: fprintf (stderr,"\nERR:AFB-daemon cannot open /dev/console (use --foreground)\n\n"); - exit (-1); + exit (1); errSessiondir: fprintf (stderr,"\nERR:AFB-daemon cannot read/write session dir\n\n"); - exit (-1); - -errSoundCard: - fprintf (stderr,"\nERR:AFB-daemon fail to probe sound cards\n\n"); - exit (-1); + exit (1); exitInitLoop: // try to unlink pid file if any if (session->background && session->config->pidfile != NULL) unlink (session->config->pidfile); - exit (-1); + exit (1); -}; /* END AFB-daemon() */ +} /* END AFB-daemon() */