diff options
Diffstat (limited to 'src/options.c')
-rw-r--r-- | src/options.c | 408 |
1 files changed, 163 insertions, 245 deletions
diff --git a/src/options.c b/src/options.c index e8de727..e730073 100644 --- a/src/options.c +++ b/src/options.c @@ -29,274 +29,178 @@ #include "options.h" #include "log.h" -#include <stdlib.h> #include <stdio.h> #include <string.h> -#include <ctype.h> +#include <glib.h> +#include <gst/gst.h> +#include <locale.h> -#define PARSE_BOOL_PARAM(SHORT, LONG, VALUE) \ - else if(!strcmp(str,SHORT) || !strcmp(str,LONG)) \ - VALUE = 1; - -#define PARSE_INVERSE_BOOL_PARAM(SHORT, LONG, VALUE) \ - else if(!strcmp(str,SHORT) || !strcmp(str,LONG)) \ - VALUE = 0; - -#define PARSE_INT_PARAM(SHORT, LONG, VALUE, BASE) \ - else if(!strcmp(str,SHORT) || !strcmp(str,LONG)) \ - { \ - if(argc < 1) \ - return i; \ - VALUE = strtol(argv[i+1], (char **)NULL, BASE); \ - argc--; \ - i++; \ - } - -#define PARSE_STRING_PARAM(SHORT, LONG, VALUE) \ - else if(!strcmp(str,SHORT) || !strcmp(str,LONG)) \ - { \ - if(argc < 1 || argv[i+1][0] == '-') \ - return i; \ - if(VALUE) free(VALUE); \ - VALUE = strdup(argv[i+1]); \ - if(!VALUE) \ - return -2; \ - argc--; \ - i++; \ - } - -#define PARSE_STRING_PARAM_SEC(SHORT, LONG, VALUE) \ - else if(!strcmp(str,SHORT) || !strcmp(str,LONG)) \ - { \ - if(argc < 1 || argv[i+1][0] == '-') \ - return i; \ - if(VALUE) free(VALUE); \ - VALUE = strdup(argv[i+1]); \ - if(!VALUE) \ - return -2; \ - size_t j; \ - for(j=0; j < strlen(argv[i+1]); ++j) \ - argv[i+1][j] = '#'; \ - argc--; \ - i++; \ - } - -#define PARSE_HEXSTRING_PARAM_SEC(SHORT, LONG, VALUE) \ - else if(!strcmp(str,SHORT) || !strcmp(str,LONG)) \ - { \ - if(argc < 1 || argv[i+1][0] == '-') \ - return i; \ - int ret; \ - ret = options_parse_hex_string(argv[i+1], &VALUE); \ - if(ret > 0) \ - return i+1; \ - else if(ret < 0) \ - return ret; \ - size_t j; \ - for(j=0; j < strlen(argv[i+1]); ++j) \ - argv[i+1][j] = '#'; \ - argc--; \ - i++; \ - } - -#define PARSE_STRING_LIST(SHORT, LONG, LIST) \ - else if(!strcmp(str,SHORT) || !strcmp(str,LONG)) \ - { \ - if(argc < 1 || argv[i+1][0] == '-') \ - return i; \ - int ret = string_list_add(&LIST, argv[i+1]); \ - if(ret == -2) \ - return ret; \ - else if(ret) \ - return i+1; \ - argc--; \ - i++; \ - } - -#define PARSE_RESOLV_TYPE(SHORT, LONG, VALUE) \ - else if(!strcmp(str,SHORT) || !strcmp(str,LONG)) \ - { \ - if(argc < 1 || argv[i+1][0] == '-') \ - return i; \ - if(!strcmp(argv[i+1], "4") || \ - !strcmp(argv[i+1], "ipv4")) \ - VALUE = IPV4_ONLY; \ - else if(!strcmp(argv[i+1], "6") || \ - !strcmp(argv[i+1], "ipv6")) \ - VALUE = IPV6_ONLY; \ - else \ - return i+1; \ - argc--; \ - i++; \ - } - -int options_parse_hex_string(const char* hex, buffer_t* buffer) +static void options_defaults(options_t* opt) { - if(!hex || !buffer) - return -1; + if(!opt) + return; - uint32_t hex_len = strlen(hex); - if(hex_len%2) - return 1; + opt->progname_ = g_strdup("sydra-rtp"); + opt->daemonize_ = TRUE; + opt->username_ = NULL; + opt->groupname_ = NULL; + opt->chroot_dir_ = NULL; + opt->pid_file_ = NULL; + opt->log_targets_ = NULL; + opt->debug_ = FALSE; - if(buffer->buf_) - free(buffer->buf_); + opt->adapter_ = 0; + opt->frontend_ = 0; + opt->frequency_ = 514000000; + opt->polarity_ = g_strdup("H"); + opt->pids_ = g_strdup("5010:5011"); + opt->host_ = NULL; + opt->port_ = g_strdup("80"); +} - buffer->length_ = hex_len/2; - buffer->buf_ = malloc(buffer->length_); - if(!buffer->buf_) { - buffer->length_ = 0; - return -2; - } +static GQuark options_error_quark() +{ + static GQuark quark = 0; + if (!quark) + quark = g_quark_from_static_string("sydra_options_error"); - const char* ptr = hex; - int i; - for(i=0;i<buffer->length_;++i) { - uint32_t tmp; - sscanf(ptr, "%2X", &tmp); - buffer->buf_[i] = (uint8_t)tmp; - ptr += 2; - } + return quark; +} - return 0; +static gboolean options_parse_remaining(const gchar *option_name, const gchar *value, gpointer data, GError **error) +{ + g_set_error(error, options_error_quark(), G_OPTION_ERROR_FAILED, "unkown option '%s'", value); + return FALSE; } +static int options_parse_post(options_t* opt); int options_parse(options_t* opt, int argc, char* argv[]) { if(!opt) return -1; - options_default(opt); + setlocale (LC_ALL, ""); + options_defaults(opt); - if(opt->progname_) - free(opt->progname_); - opt->progname_ = strdup(argv[0]); + g_free(opt->progname_); + opt->progname_ = g_strdup(argv[0]); if(!opt->progname_) - return -2; + return -127; + + gboolean show_version = FALSE; + GOptionEntry main_entries[] = { + { "version", 'v', 0, G_OPTION_ARG_NONE, &show_version, + "print version info and exit", NULL }, + { "nodaemonize", 'D', G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &opt->daemonize_, + "don't run in background", NULL }, + { "username", 'u', 0, G_OPTION_ARG_STRING, &opt->username_, + "change to this user", "USERNAME" }, + { "group", 'g', 0, G_OPTION_ARG_STRING, &opt->groupname_, + "change to this group", "GROUP" }, + { "chroot", 'C', 0, G_OPTION_ARG_STRING, &opt->chroot_dir_, + "chroot to this directory", "PATH" }, + { "write-pid", 'P', 0, G_OPTION_ARG_STRING, &opt->pid_file_, + "write pid to this file", "PATH" }, + { "log", 'L', 0, G_OPTION_ARG_STRING_ARRAY, &opt->log_targets_, + "add a log target, can be invoked several times", "<TARGET>:<LEVEL>[,<PARAM1>[,<PARAM2>..]]" }, + { "debug", 'U', 0, G_OPTION_ARG_NONE, &opt->debug_, + "don't daemonize and log to stdout with maximum log level", NULL }, + { "adapter", 'a', 0, G_OPTION_ARG_INT, &opt->adapter_, + "the DVB adapter to use", "NUM" }, + { "frontend", 'F', 0, G_OPTION_ARG_INT, &opt->frontend_, + "the frontend of the DVB adapter to use", "NUM" }, + { "frequency", 'f', 0, G_OPTION_ARG_INT, &opt->frequency_, + "the frequency to tune to", "Hz" }, + { "polarity", 'o', 0, G_OPTION_ARG_STRING, &opt->polarity_, + "the polarity of the signal", "(H|V)" }, + { "pids", 'i', 0, G_OPTION_ARG_STRING, &opt->pids_, + "the pids of the stream", "PID:PID" }, + { "host", 'H', 0, G_OPTION_ARG_STRING, &opt->host_, + "the local interface to bind to", "ADDR" }, + { "port", 'p', 0, G_OPTION_ARG_STRING, &opt->port_, + "the port to listen to", "NUM" }, + { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_CALLBACK, options_parse_remaining, NULL, NULL}, + { NULL } + }; + GOptionContext *ctx = g_option_context_new("- spreadspace streaming hydra "); + GOptionGroup* main_group = g_option_group_new ("main", "Application Options", + "Show Application Options", opt, NULL); + if(main_group) + g_option_group_add_entries(main_group, main_entries); + + GOptionGroup* gst_group = gst_init_get_option_group(); + if(!main_group || !gst_group) { + printf("ERROR: Failed to initialize: memory error\n"); + return -127; + } + g_option_context_set_main_group(ctx, main_group); + g_option_context_add_group(ctx, gst_group); - argc--; + GError *err = NULL; + if(!g_option_context_parse(ctx, &argc, &argv, &err)) { + printf("ERROR: Failed to initialize: %s\n", err->message); + g_error_free(err); + return 1; + } - int i; - for(i=1; argc > 0; ++i) - { - char* str = argv[i]; - argc--; + if(show_version) + return -1; - if(!strcmp(str,"-h") || !strcmp(str,"--help")) - return -1; - else if(!strcmp(str,"-v") || !strcmp(str,"--version")) - return -3; - PARSE_INVERSE_BOOL_PARAM("-D","--nodaemonize", opt->daemonize_) - PARSE_STRING_PARAM("-u","--username", opt->username_) - PARSE_STRING_PARAM("-g","--groupname", opt->groupname_) - PARSE_STRING_PARAM("-C","--chroot", opt->chroot_dir_) - PARSE_STRING_PARAM("-P","--write-pid", opt->pid_file_) - PARSE_STRING_LIST("-L","--log", opt->log_targets_) - PARSE_BOOL_PARAM("-U", "--debug", opt->debug_) - PARSE_INT_PARAM("-a","--adapter", opt->adapter_, 10) - PARSE_INT_PARAM("-F","--frontend", opt->frontend_, 10) - PARSE_INT_PARAM("-f","--frequency", opt->frequency_, 10) - PARSE_STRING_PARAM("-o","--polarity", opt->polarity_) - PARSE_STRING_PARAM("-i","--pids", opt->pids_) - PARSE_STRING_PARAM("-H","--host", opt->host_) - PARSE_STRING_PARAM("-p","--port", opt->port_) + return options_parse_post(opt); +} - else - return i; +static int options_parse_post(options_t* opt) +{ + if(opt->adapter_ < 0) { + printf("ERROR: adapter must be a >= 0!\n"); + return -2; + } + if(opt->frontend_ < 0) { + printf("ERROR: frontend must be a >= 0!\n"); + return -2; + } + if(opt->frequency_ < 0) { + printf("ERROR: frequency must be a >= 0!\n"); + return -2; } - - if(opt->frequency_ < 0 || opt->adapter_ < 0 || opt->frontend_ < 0) - return -4; if(opt->debug_) { - string_list_add(&opt->log_targets_, "stdout:5"); opt->daemonize_ = 0; + g_strfreev(opt->log_targets_); + opt->log_targets_ = g_new(gchar*, 2); + if(!opt->log_targets_) return -127; + opt->log_targets_[0] = g_strdup("stdout:5"); + if(!(opt->log_targets_[0])) return -127; + opt->log_targets_[1] = NULL; } - if(!opt->log_targets_.first_) { - string_list_add(&opt->log_targets_, "syslog:3,gstdvbbackend,daemon"); + if(!opt->log_targets_ || !g_strv_length(opt->log_targets_)) { + opt->log_targets_ = g_new(gchar*, 2); + if(!opt->log_targets_) return -127; + opt->log_targets_[0] = g_strdup("syslog:3,sydra-launch,daemon"); + if(!(opt->log_targets_[0])) return -127; + opt->log_targets_[1] = NULL; } return 0; } -void options_parse_post(options_t* opt) -{ - if(!opt) - return; - -// nothing yet -} - -void options_default(options_t* opt) -{ - if(!opt) - return; - - opt->progname_ = strdup("gstdvbbackend"); - opt->daemonize_ = 1; - opt->username_ = NULL; - opt->groupname_ = NULL; - opt->chroot_dir_ = NULL; - opt->pid_file_ = NULL; - string_list_init(&opt->log_targets_); - opt->debug_ = 0; - opt->adapter_ = 0; - opt->frontend_ = 0; - opt->frequency_ = 514000000; - opt->polarity_ = strdup("H"); - opt->pids_ = strdup("5010:5011"); - opt->host_ = NULL; - opt->port_ = strdup("80"); -} - void options_clear(options_t* opt) { if(!opt) return; - if(opt->progname_) - free(opt->progname_); - if(opt->username_) - free(opt->username_); - if(opt->groupname_) - free(opt->groupname_); - if(opt->chroot_dir_) - free(opt->chroot_dir_); - if(opt->pid_file_) - free(opt->pid_file_); - string_list_clear(&opt->log_targets_); - if(opt->polarity_) - free(opt->polarity_); - if(opt->pids_) - free(opt->pids_); - if(opt->host_) - free(opt->host_); - if(opt->port_) - free(opt->port_); -} - -void options_print_usage() -{ - printf("USAGE:\n"); - printf("gstdvbbackend [-h|--help] prints this...\n"); - printf(" [-v|--version] print version info and exit\n"); - printf(" [-D|--nodaemonize] don't run in background\n"); - printf(" [-u|--username] <username> change to this user\n"); - printf(" [-g|--groupname] <groupname> change to this group\n"); - printf(" [-C|--chroot] <path> chroot to this directory\n"); - printf(" [-P|--write-pid] <path> write pid to this file\n"); - printf(" [-L|--log] <target>:<level>[,<param1>[,<param2>..]]\n"); - printf(" add a log target, can be invoked several times\n"); - printf(" [-U|--debug] don't daemonize and log to stdout with maximum log level\n"); - printf(" [-a|--adapter] <num> the dvb adapter to use (default 0)\n"); - printf(" [-F|--frontend] <num> the dvb adapter to use (default 0)\n"); - printf(" [-f|--frequency <hertz> the frequency to tune to (default 514000000)\n"); - printf(" [-o|--polarity (H,V) polarity of the signal (default H)\n"); - printf(" [-i|--pids <pid:pid> the pids of the stream (default 5010:5011)\n"); - printf(" [-H|--host <addr> the local interface to bind to (default any interface)\n"); - printf(" [-p|--port <port> the port to listen to (default 80)\n"); + g_free(opt->progname_); + g_free(opt->username_); + g_free(opt->groupname_); + g_free(opt->chroot_dir_); + g_free(opt->pid_file_); + g_strfreev(opt->log_targets_); + g_free(opt->polarity_); + g_free(opt->pids_); + g_free(opt->host_); + g_free(opt->port_); } void options_print_version() @@ -309,6 +213,16 @@ void options_print_version() #else printf("%s\n", VERSION_STRING_1); #endif + const gchar *nano_str; + guint major, minor, micro, nano; + gst_version(&major, &minor, µ, &nano); + if (nano == 1) + nano_str = " (CVS)"; + else if (nano == 2) + nano_str = " (Prerelease)"; + else + nano_str = ""; + printf(" linked against GStreamer %d.%d.%d%s\n", major, minor, micro, nano_str); } void options_print(options_t* opt) @@ -316,20 +230,24 @@ void options_print(options_t* opt) if(!opt) return; - printf("progname: '%s'\n", opt->progname_); - printf("daemonize: %d\n", opt->daemonize_); - printf("username: '%s'\n", opt->username_); - printf("groupname: '%s'\n", opt->groupname_); - printf("chroot_dir: '%s'\n", opt->chroot_dir_); - printf("pid_file: '%s'\n", opt->pid_file_); - printf("log_targets: \n"); - string_list_print(&opt->log_targets_, " '", "'\n"); - printf("debug: %s\n", !opt->debug_ ? "false" : "true"); - printf("adapter: %d\n", opt->adapter_); - printf("frontend: %d\n", opt->frontend_); - printf("frequency: %d\n", opt->frequency_); - printf("polarity: '%s'\n", opt->polarity_); - printf("pids: '%s'\n", opt->pids_); - printf("host: '%s'\n", opt->host_); - printf("port: '%s'\n", opt->port_); + printf(" progname: '%s'\n", opt->progname_); + printf(" daemonize: %s\n", opt->daemonize_ ? "true" : "false"); + printf(" username: '%s'\n", opt->username_); + printf(" groupname: '%s'\n", opt->groupname_); + printf(" chroot_dir: '%s'\n", opt->chroot_dir_); + printf(" pid_file: '%s'\n", opt->pid_file_); + printf(" log_targets: \n"); + gchar* lt = opt->log_targets_ ? g_strjoinv ("'\n '", opt->log_targets_) : NULL; + if(lt) { + printf(" '%s'\n", lt); + g_free(lt); + } + printf(" debug: %s\n", opt->debug_ ? "true" : "false"); + printf(" adapter: %d\n", opt->adapter_); + printf(" frontend: %d\n", opt->frontend_); + printf(" frequency: %d\n", opt->frequency_); + printf(" polarity: '%s'\n", opt->polarity_); + printf(" pids: '%s'\n", opt->pids_); + printf(" host: '%s'\n", opt->host_); + printf(" port: '%s'\n", opt->port_); } |