Codebase list squeezelite / 8c3eff0
add error messages for cmdline parsing, add -P option for pid file - based on patch from Gordon Harris Adrian Smith 9 years ago
1 changed file(s) with 47 addition(s) and 8 deletion(s). Raw diff Collapse all Expand all
6767 #if ALSA
6868 " -p <priority>\t\tSet real time priority of output thread (1-99)\n"
6969 #endif
70 #if LINUX || FREEBSD
71 " -P <filename>\t\tStore the process id (PID) in filename\n"
72 #endif
7073 " -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"
7174 #if RESAMPLE
7275 " -R -u [params]\tResample, params = <recipe>:<flags>:<attenuation>:<precision>:<passband_end>:<stopband_start>:<phase_response>,\n"
8891 " -z \t\t\tDaemonize\n"
8992 #endif
9093 " -t \t\t\tLicense terms\n"
94 " -? \t\t\tDisplay this help text\n"
9195 "\n"
9296 "Build options:"
9397 #if LINUX
184188 char *output_params = NULL;
185189 #if LINUX || FREEBSD
186190 bool daemonize = false;
191 char *pidfile = NULL;
192 FILE *pidfp = NULL;
187193 #endif
188194 #if ALSA
189195 unsigned rt_priority = OUTPUT_RT_PRIORITY;
217223
218224 while (optind < argc && strlen(argv[optind]) >= 2 && argv[optind][0] == '-') {
219225 char *opt = argv[optind] + 1;
220 if (strstr("oabcdefmMnNprs", opt) && optind < argc - 1) {
226 if (strstr("oabcdefmMnNpPrs?", opt) && optind < argc - 1) {
221227 optarg = argv[optind + 1];
222228 optind += 2;
223229 } else if (strstr("ltz"
234240 optarg = NULL;
235241 optind += 1;
236242 } else {
243 fprintf(stderr, "\nOption error: -%s\n\n", opt);
237244 usage(argv[0]);
238 exit(0);
245 exit(1);
239246 }
240247
241248 switch (opt[0]) {
273280 if (!strcmp(l, "all") || !strcmp(l, "decode")) log_decode = new;
274281 if (!strcmp(l, "all") || !strcmp(l, "output")) log_output = new;
275282 } else {
283 fprintf(stderr, "\nDebug settings error: -d %s\n\n", optarg);
276284 usage(argv[0]);
277 exit(0);
285 exit(1);
278286 }
279287 }
280288 break;
357365 case 'p':
358366 rt_priority = atoi(optarg);
359367 if (rt_priority > 99 || rt_priority < 1) {
368 fprintf(stderr, "\nError: invalid priority: %s\n\n", optarg);
360369 usage(argv[0]);
361 exit(0);
362 }
370 exit(1);
371 }
372 break;
373 #endif
374 #if LINUX || FREEBSD
375 case 'P':
376 pidfile = optarg;
363377 break;
364378 #endif
365379 case 'l':
397411 case 't':
398412 license();
399413 exit(0);
414 case '?':
415 usage(argv[0]);
416 exit(0);
400417 default:
418 fprintf(stderr, "Arg error: %s\n", argv[optind]);
401419 break;
402420 }
403421 }
404422
405423 // warn if command line includes something which isn't parsed
406424 if (optind < argc) {
425 fprintf(stderr, "\nError: command line argument error\n\n");
407426 usage(argv[0]);
408 exit(0);
427 exit(1);
409428 }
410429
411430 signal(SIGINT, sighandler);
442461 }
443462
444463 #if LINUX || FREEBSD
464 if (pidfile) {
465 if (!(pidfp = fopen(pidfile, "w")) ) {
466 fprintf(stderr, "Error opening pidfile %s: %s\n", pidfile, strerror(errno));
467 exit(1);
468 }
469 pidfile = realpath(pidfile, NULL); // daemonize will change cwd
470 }
471
445472 if (daemonize) {
446473 if (daemon(0, logfile ? 1 : 0)) {
447474 fprintf(stderr, "error daemonizing: %s\n", strerror(errno));
448475 }
449476 }
477
478 if (pidfp) {
479 fprintf(pidfp, "%d\n", getpid());
480 fclose(pidfp);
481 }
450482 #endif
451483
452484 #if WIN
485517 #endif
486518
487519 if (name && namefile) {
488 printf("-n and -N option should not be used at same time\n");
489 exit(0);
520 fprintf(stderr, "-n and -N option should not be used at same time\n");
521 exit(1);
490522 }
491523
492524 slimproto(log_slimproto, server, mac, name, namefile, modelname);
509541 winsock_close();
510542 #endif
511543
544 #if LINUX || FREEBSD
545 if (pidfile) {
546 unlink(pidfile);
547 free(pidfile);
548 }
549 #endif
550
512551 exit(0);
513552 }