add error messages for cmdline parsing, add -P option for pid file - based on patch from Gordon Harris
Adrian Smith
9 years ago
67 | 67 | #if ALSA |
68 | 68 | " -p <priority>\t\tSet real time priority of output thread (1-99)\n" |
69 | 69 | #endif |
70 | #if LINUX || FREEBSD | |
71 | " -P <filename>\t\tStore the process id (PID) in filename\n" | |
72 | #endif | |
70 | 73 | " -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" |
71 | 74 | #if RESAMPLE |
72 | 75 | " -R -u [params]\tResample, params = <recipe>:<flags>:<attenuation>:<precision>:<passband_end>:<stopband_start>:<phase_response>,\n" |
88 | 91 | " -z \t\t\tDaemonize\n" |
89 | 92 | #endif |
90 | 93 | " -t \t\t\tLicense terms\n" |
94 | " -? \t\t\tDisplay this help text\n" | |
91 | 95 | "\n" |
92 | 96 | "Build options:" |
93 | 97 | #if LINUX |
184 | 188 | char *output_params = NULL; |
185 | 189 | #if LINUX || FREEBSD |
186 | 190 | bool daemonize = false; |
191 | char *pidfile = NULL; | |
192 | FILE *pidfp = NULL; | |
187 | 193 | #endif |
188 | 194 | #if ALSA |
189 | 195 | unsigned rt_priority = OUTPUT_RT_PRIORITY; |
217 | 223 | |
218 | 224 | while (optind < argc && strlen(argv[optind]) >= 2 && argv[optind][0] == '-') { |
219 | 225 | char *opt = argv[optind] + 1; |
220 | if (strstr("oabcdefmMnNprs", opt) && optind < argc - 1) { | |
226 | if (strstr("oabcdefmMnNpPrs?", opt) && optind < argc - 1) { | |
221 | 227 | optarg = argv[optind + 1]; |
222 | 228 | optind += 2; |
223 | 229 | } else if (strstr("ltz" |
234 | 240 | optarg = NULL; |
235 | 241 | optind += 1; |
236 | 242 | } else { |
243 | fprintf(stderr, "\nOption error: -%s\n\n", opt); | |
237 | 244 | usage(argv[0]); |
238 | exit(0); | |
245 | exit(1); | |
239 | 246 | } |
240 | 247 | |
241 | 248 | switch (opt[0]) { |
273 | 280 | if (!strcmp(l, "all") || !strcmp(l, "decode")) log_decode = new; |
274 | 281 | if (!strcmp(l, "all") || !strcmp(l, "output")) log_output = new; |
275 | 282 | } else { |
283 | fprintf(stderr, "\nDebug settings error: -d %s\n\n", optarg); | |
276 | 284 | usage(argv[0]); |
277 | exit(0); | |
285 | exit(1); | |
278 | 286 | } |
279 | 287 | } |
280 | 288 | break; |
357 | 365 | case 'p': |
358 | 366 | rt_priority = atoi(optarg); |
359 | 367 | if (rt_priority > 99 || rt_priority < 1) { |
368 | fprintf(stderr, "\nError: invalid priority: %s\n\n", optarg); | |
360 | 369 | 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; | |
363 | 377 | break; |
364 | 378 | #endif |
365 | 379 | case 'l': |
397 | 411 | case 't': |
398 | 412 | license(); |
399 | 413 | exit(0); |
414 | case '?': | |
415 | usage(argv[0]); | |
416 | exit(0); | |
400 | 417 | default: |
418 | fprintf(stderr, "Arg error: %s\n", argv[optind]); | |
401 | 419 | break; |
402 | 420 | } |
403 | 421 | } |
404 | 422 | |
405 | 423 | // warn if command line includes something which isn't parsed |
406 | 424 | if (optind < argc) { |
425 | fprintf(stderr, "\nError: command line argument error\n\n"); | |
407 | 426 | usage(argv[0]); |
408 | exit(0); | |
427 | exit(1); | |
409 | 428 | } |
410 | 429 | |
411 | 430 | signal(SIGINT, sighandler); |
442 | 461 | } |
443 | 462 | |
444 | 463 | #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 | ||
445 | 472 | if (daemonize) { |
446 | 473 | if (daemon(0, logfile ? 1 : 0)) { |
447 | 474 | fprintf(stderr, "error daemonizing: %s\n", strerror(errno)); |
448 | 475 | } |
449 | 476 | } |
477 | ||
478 | if (pidfp) { | |
479 | fprintf(pidfp, "%d\n", getpid()); | |
480 | fclose(pidfp); | |
481 | } | |
450 | 482 | #endif |
451 | 483 | |
452 | 484 | #if WIN |
485 | 517 | #endif |
486 | 518 | |
487 | 519 | 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); | |
490 | 522 | } |
491 | 523 | |
492 | 524 | slimproto(log_slimproto, server, mac, name, namefile, modelname); |
509 | 541 | winsock_close(); |
510 | 542 | #endif |
511 | 543 | |
544 | #if LINUX || FREEBSD | |
545 | if (pidfile) { | |
546 | unlink(pidfile); | |
547 | free(pidfile); | |
548 | } | |
549 | #endif | |
550 | ||
512 | 551 | exit(0); |
513 | 552 | } |