main: add option parsing

This commit is contained in:
James Laird
2013-04-01 21:44:14 +11:00
parent 9b33878c59
commit f39ff3a44e
+61 -5
View File
@@ -1,4 +1,5 @@
#include <signal.h>
#include <unistd.h>
#include <memory.h>
#include "common.h"
#include "rtsp.h"
@@ -22,20 +23,77 @@ static void sig_shutdown(int foo, siginfo_t *bar, void *baz) {
shutdown();
}
void usage(char *progname) {
printf("Usage: %s [options...] [-- [output options...]]\n\n", progname);
printf("Available options:\n"
" -h show this help\n"
" -p port set RTSP listening port\n"
" -n name set advertised name\n"
" -o output set audio output\n"
" -s fill set how full the buffer must be before audio output starts\n"
" This value is in frames; default 220\n"
"Run %s -o <output> -h to find the available options for a specific output\n"
"\n", progname);
if (config.output_name)
config.output = audio_get_output(config.output_name);
if (config.output) {
printf("Options for output %s:\n", config.output_name);
config.output->help();
} else {
audio_ls_outputs();
}
}
int parse_options(int argc, char **argv) {
char opt;
while ((opt = getopt(argc, argv, "+hp:n:o:s:")) > 0) {
switch (opt) {
default:
printf("Unknown argument -%c\n", optopt);
case 'h':
usage(argv[0]);
exit(1);
case 'v':
debuglev++;
break;
case 'p':
config.port = atoi(optarg);
break;
case 'n':
config.apname = optarg;
break;
case 'o':
config.output_name = optarg;
break;
case 's':
config.buffer_start_fill = atoi(optarg);
break;
}
}
return optind;
}
int main(int argc, char **argv) {
memset(&config, 0, sizeof(config));
// config.password = "hello";
memcpy(config.hw_addr, "\0\x11\x22\x33\x44\x55", 6);
// set defaults
config.buffer_start_fill = 220;
config.port = 9000;
config.apname = "hellothere";
char hostname[100];
gethostname(hostname, 100);
config.apname = malloc(20 + 100);
snprintf(config.apname, 20 + 100, "Shairport on %s", hostname);
int audio_arg = parse_options(argc, argv);
config.output = audio_get_output(config.output_name);
if (!config.output) {
audio_ls_outputs();
die("Invalid audio output specified!\n");
}
config.output->init(argc-audio_arg, argv+audio_arg);
// mask off all signals before creating threads.
// this way we control which thread gets which signals.
@@ -60,8 +118,6 @@ int main(int argc, char **argv) {
sigaction(SIGTERM, &sa, NULL);
sigaction(SIGCHLD, &sa, NULL);
config.output->init(0, 0);
rtsp_listen_loop();
// should not.