From: Andrew Karpow Date: Mon, 11 Nov 2013 14:53:54 +0000 (+0100) Subject: better args handling X-Git-Url: http://gitweb.hugovil.com/?a=commitdiff_plain;h=81859d486bf9a0bcab13848b41ea8c0513717466;p=ympd.git better args handling --- diff --git a/src/http_server.c b/src/http_server.c index efe6d97..7ccb118 100644 --- a/src/http_server.c +++ b/src/http_server.c @@ -3,7 +3,6 @@ #include #include "http_server.h" -#define LOCAL_RESOURCE_PATH DATADIR"/htdocs" char *resource_path = LOCAL_RESOURCE_PATH; struct serveable { diff --git a/src/http_server.h b/src/http_server.h index bf60dde..f360106 100644 --- a/src/http_server.h +++ b/src/http_server.h @@ -9,3 +9,4 @@ int callback_http(struct libwebsocket_context *context, enum libwebsocket_callback_reasons reason, void *user, void *in, size_t len); +#define LOCAL_RESOURCE_PATH DATADIR"/htdocs" diff --git a/src/mpd_client.h b/src/mpd_client.h index 019f17b..3c29ce0 100644 --- a/src/mpd_client.h +++ b/src/mpd_client.h @@ -47,3 +47,6 @@ int mpd_put_state(char *buffer); int mpd_put_current_song(char *buffer); int mpd_put_playlist(char *buffer); int mpd_put_browse(char *buffer, char *path); + +int mpd_port; +const char *mpd_host; diff --git a/src/ympd.c b/src/ympd.c index 943fb55..4ed9369 100644 --- a/src/ympd.c +++ b/src/ympd.c @@ -3,6 +3,7 @@ #include #include #include +#include #include #include "http_server.h" @@ -41,23 +42,48 @@ void bye() int main(int argc, char **argv) { int n, gid = -1, uid = -1; + int option_index = 0; + unsigned int oldus = 0; struct libwebsocket_context *context; + struct lws_context_creation_info info; const char *cert_filepath = NULL; const char *private_key_filepath = NULL; const char *iface = NULL; - struct lws_context_creation_info info; - unsigned int oldus = 0; atexit(bye); memset(&info, 0, sizeof info); - info.port = 80; - - while((n = getopt(argc, argv, "i:p:r:c:k:g:u:h")) != -1) { + info.port = 8080; + mpd_host = "127.0.0.1"; + mpd_port = 6600; + lws_set_log_level(LLL_ERR | LLL_WARN, NULL); + + static struct option long_options[] = { + {"host", required_argument, 0, 'h'}, + {"port", required_argument, 0, 'p'}, + {"interface", required_argument, 0, 'i'}, + {"webport", required_argument, 0, 'w'}, + {"resourcepath", required_argument, 0, 'r'}, + {"ssl_cert", required_argument, 0, 'c'}, + {"ssl_key", required_argument, 0, 'k'}, + {"gid", required_argument, 0, 'g'}, + {"uid", required_argument, 0, 'u'}, + {"verbose", optional_argument, 0, 'v'}, + {"help", no_argument, 0, 0 }, + {0, 0, 0, 0 } + }; + + while((n = getopt_long(argc, argv, "h:p:i:r:c:k:g:uv::W", + long_options, &option_index)) != -1) { switch (n) { + case 'h': + mpd_host = optarg; + break; + case 'p': + mpd_port = atoi(optarg); case 'i': iface = optarg; break; - case 'p': + case 'w': info.port = atoi(optarg); break; case 'r': @@ -75,17 +101,26 @@ int main(int argc, char **argv) case 'u': uid = atoi(optarg); break; - case '?': - case 'h': - lwsl_err("Usage: %s [OPTION]...\n" - "\t[-p ]\n" - "\t[-i ]\n" - "\t[-r ]\n" - "\t[-c ]\n" - "\t[-k ]\n" - "\t[-g ]\n" - "\t[-u ]\n" - "\t[-h]\n" + case 'v': + if(optarg) + lws_set_log_level(strtol(optarg, NULL, 10), NULL); + else + lws_set_log_level(LLL_ERR | LLL_WARN | + LLL_NOTICE | LLL_INFO, NULL); + break; + default: + fprintf(stderr, "Usage: %s [OPTION]...\n\n" + "\t-h, --host \t\tconnect to mpd at host [localhost]\n" + "\t-p, --port \t\tconnect to mpd at port [6600]\n" + "\t-i, --interface \tlisten interface for webserver [all]\n" + "\t-w, --webport \t\tlisten port for webserver [8080]\n" + "\t-r, --resourcepath \tresourcepath for webserver [" LOCAL_RESOURCE_PATH "]\n" + "\t-c, --ssl_cert \tssl certificate ssl_private_key_filepath\n" + "\t-k, --ssl_key \tssl private key filepath\n" + "\t-u, --uid \t\t\tuser id after socket bind\n" + "\t-g, --gid \t\t\tgroup id after socket bind\n" + "\t-v, --verbose[]\t\tverbosity level\n" + "\t--help\t\t\t\tthis help\n" , argv[0]); return EXIT_FAILURE; }