Skip to content

Commit

Permalink
add error messages for cmdline parsing, add -P option for pid file - …
Browse files Browse the repository at this point in the history
…based on patch from Gordon Harris
  • Loading branch information
triode committed Dec 30, 2014
1 parent 367bffc commit 8c3eff0
Showing 1 changed file with 46 additions and 7 deletions.
53 changes: 46 additions & 7 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ static void usage(const char *argv0) {
" -N <filename>\t\tStore player name in filename to allow server defined name changes to be shared between servers (not supported with -n)\n"
#if ALSA
" -p <priority>\t\tSet real time priority of output thread (1-99)\n"
#endif
#if LINUX || FREEBSD
" -P <filename>\t\tStore the process id (PID) in filename\n"
#endif
" -r <rates>[:<delay>]\tSample rates supported, allows output to be off when squeezelite is started; rates = <maxrate>|<minrate>-<maxrate>|<rate1>,<rate2>,<rate3>; delay = optional delay switching rates in ms\n"
#if RESAMPLE
Expand All @@ -89,6 +92,7 @@ static void usage(const char *argv0) {
" -z \t\t\tDaemonize\n"
#endif
" -t \t\t\tLicense terms\n"
" -? \t\t\tDisplay this help text\n"
"\n"
"Build options:"
#if LINUX
Expand Down Expand Up @@ -185,6 +189,8 @@ int main(int argc, char **argv) {
char *output_params = NULL;
#if LINUX || FREEBSD
bool daemonize = false;
char *pidfile = NULL;
FILE *pidfp = NULL;
#endif
#if ALSA
unsigned rt_priority = OUTPUT_RT_PRIORITY;
Expand Down Expand Up @@ -218,7 +224,7 @@ int main(int argc, char **argv) {

while (optind < argc && strlen(argv[optind]) >= 2 && argv[optind][0] == '-') {
char *opt = argv[optind] + 1;
if (strstr("oabcdefmMnNprs", opt) && optind < argc - 1) {
if (strstr("oabcdefmMnNpPrs?", opt) && optind < argc - 1) {
optarg = argv[optind + 1];
optind += 2;
} else if (strstr("ltz"
Expand All @@ -235,8 +241,9 @@ int main(int argc, char **argv) {
optarg = NULL;
optind += 1;
} else {
fprintf(stderr, "\nOption error: -%s\n\n", opt);
usage(argv[0]);
exit(0);
exit(1);
}

switch (opt[0]) {
Expand Down Expand Up @@ -274,8 +281,9 @@ int main(int argc, char **argv) {
if (!strcmp(l, "all") || !strcmp(l, "decode")) log_decode = new;
if (!strcmp(l, "all") || !strcmp(l, "output")) log_output = new;
} else {
fprintf(stderr, "\nDebug settings error: -d %s\n\n", optarg);
usage(argv[0]);
exit(0);
exit(1);
}
}
break;
Expand Down Expand Up @@ -358,10 +366,16 @@ int main(int argc, char **argv) {
case 'p':
rt_priority = atoi(optarg);
if (rt_priority > 99 || rt_priority < 1) {
fprintf(stderr, "\nError: invalid priority: %s\n\n", optarg);
usage(argv[0]);
exit(0);
exit(1);
}
break;
#endif
#if LINUX || FREEBSD
case 'P':
pidfile = optarg;
break;
#endif
case 'l':
list_devices();
Expand Down Expand Up @@ -398,15 +412,20 @@ int main(int argc, char **argv) {
case 't':
license();
exit(0);
case '?':
usage(argv[0]);
exit(0);
default:
fprintf(stderr, "Arg error: %s\n", argv[optind]);
break;
}
}

// warn if command line includes something which isn't parsed
if (optind < argc) {
fprintf(stderr, "\nError: command line argument error\n\n");
usage(argv[0]);
exit(0);
exit(1);
}

signal(SIGINT, sighandler);
Expand Down Expand Up @@ -443,11 +462,24 @@ int main(int argc, char **argv) {
}

#if LINUX || FREEBSD
if (pidfile) {
if (!(pidfp = fopen(pidfile, "w")) ) {
fprintf(stderr, "Error opening pidfile %s: %s\n", pidfile, strerror(errno));
exit(1);
}
pidfile = realpath(pidfile, NULL); // daemonize will change cwd
}

if (daemonize) {
if (daemon(0, logfile ? 1 : 0)) {
fprintf(stderr, "error daemonizing: %s\n", strerror(errno));
}
}

if (pidfp) {
fprintf(pidfp, "%d\n", getpid());
fclose(pidfp);
}
#endif

#if WIN
Expand Down Expand Up @@ -486,8 +518,8 @@ int main(int argc, char **argv) {
#endif

if (name && namefile) {
printf("-n and -N option should not be used at same time\n");
exit(0);
fprintf(stderr, "-n and -N option should not be used at same time\n");
exit(1);
}

slimproto(log_slimproto, server, mac, name, namefile, modelname);
Expand All @@ -510,5 +542,12 @@ int main(int argc, char **argv) {
winsock_close();
#endif

#if LINUX || FREEBSD
if (pidfile) {
unlink(pidfile);
free(pidfile);
}
#endif

exit(0);
}

0 comments on commit 8c3eff0

Please sign in to comment.