diff options
author | Kalle Wallin <kaw@linux.se> | 2004-07-01 21:42:53 +0000 |
---|---|---|
committer | Kalle Wallin <kaw@linux.se> | 2004-07-01 21:42:53 +0000 |
commit | 7bf87192828e33fbdad2dd2a82474acaffc1bdf1 (patch) | |
tree | ed1da46b13a4271dab2d0c3ddd61a737f7da311d /src/options.c | |
parent | 95633d4b608f6ae3d74c6ee5c269ed2559a9966c (diff) | |
download | mpd-7bf87192828e33fbdad2dd2a82474acaffc1bdf1.tar.gz mpd-7bf87192828e33fbdad2dd2a82474acaffc1bdf1.tar.xz mpd-7bf87192828e33fbdad2dd2a82474acaffc1bdf1.zip |
Removed popt code/dependency
git-svn-id: https://svn.musicpd.org/ncmpc/trunk@1761 09075e82-0dd4-0310-85a5-a0d7c8717e4f
Diffstat (limited to '')
-rw-r--r-- | src/options.c | 303 |
1 files changed, 196 insertions, 107 deletions
diff --git a/src/options.c b/src/options.c index 0aa812204..3751aae42 100644 --- a/src/options.c +++ b/src/options.c @@ -20,134 +20,229 @@ #include <stdio.h> #include <stdlib.h> -#include <unistd.h> #include <string.h> -#include <ncurses.h> #include <glib.h> -#include <popt.h> #include "config.h" #include "ncmpc.h" -#include "options.h" -#include "command.h" #include "support.h" +#include "options.h" -options_t options; +#define MAX_LONGOPT_LENGTH 32 + +typedef struct +{ + int shortopt; + char *longopt; + char *argument; + char *descrition; +} arg_opt_t; + + +typedef void (*option_callback_fn_t) (int c, char *arg); -static char *mpd_host = NULL; -static char *mpd_password = NULL; -static char *config_file = NULL; -static char *key_file = NULL; -static struct poptOption optionsTable[] = { +options_t options; + +static arg_opt_t option_table[] = { + { '?', "help", NULL, "Show this help message" }, + { 'V', "version", NULL, "Display version information" }, + { 'c', "colors", NULL, "Enable colors" }, + { 'C', "no-colors", NULL, "Disable colors" }, + { 'e', "exit", NULL, "Exit on connection errors" }, + { 'p', "port", "PORT", "Connect to server on port [" DEFAULT_PORT_STR "]" }, + { 'h', "host", "HOST", "Connect to server on host [" DEFAULT_HOST "]" }, + { 'P', "password","PASSWORD", "Connect with password" }, + { 'f', "config", "FILE", "Read configuration from file" }, + { 'k', "key-file","FILE", "Read configuration from file" }, #ifdef DEBUG - { "debug", 'D', 0, 0, 'D', "Enable debug output." }, + { 'D', "debug", NULL, "Enable debug output on stderr" }, #endif - { "version", 'V', 0, 0, 'V', "Display version information." }, - { "colors", 'c', 0, 0, 'c', "Enable colors." }, - { "no-colors", 'C', 0, 0, 'C', "Disable colors." }, - { "exit", 'e', 0, 0, 'e', "Exit on connection errors." }, - { "port", 'p', POPT_ARG_INT, &options.port, 0, - "Connect to server on port [" DEFAULT_PORT_STR "].", "PORT" }, - { "host", 'h', POPT_ARG_STRING, &mpd_host, 0, - "Connect to server [" DEFAULT_HOST "].", "HOSTNAME" }, - { "password", 'P', POPT_ARG_STRING, &mpd_password, 0, - "Connect with password.", "PASSWORD" }, - { "config", 'f', POPT_ARG_STRING, &config_file, 0, - "Read config from FILE." , "FILE" }, - { "key-file", 'k', POPT_ARG_STRING, &key_file, 0, - "Read key bindings from FILE." , "FILE" }, - - POPT_AUTOHELP - { NULL, 0, 0, NULL, 0 } + { 0, NULL, NULL, NULL }, }; -static void -usage(poptContext optCon, int exitcode, char *error, char *addl) +static arg_opt_t * +lookup_option(int s, char *l) { - poptPrintUsage(optCon, stderr, 0); - if (error) - fprintf(stderr, "%s: %s0", error, addl); - exit(exitcode); -} + int i; -options_t * -options_parse( int argc, const char **argv) -{ - int c; - poptContext optCon; /* context for parsing command-line options */ - - mpd_host = NULL; - mpd_password = NULL; - config_file = NULL; - key_file = NULL; - optCon = poptGetContext(NULL, argc, argv, optionsTable, 0); - while ((c = poptGetNextOpt(optCon)) >= 0) - { - switch (c) - { -#ifdef DEBUG - case 'D': - options.debug = 1; - break; -#endif - case 'c': - options.enable_colors = 1; - break; - case 'C': - options.enable_colors = 0; - break; - case 'V': - printf("Version " VERSION "\n"); - exit(EXIT_SUCCESS); - case 'e': - options.reconnect = 0; - break; - default: - fprintf(stderr, "%s: %s\n", - poptBadOption(optCon, POPT_BADOPTION_NOALIAS), - poptStrerror(c)); - poptFreeContext(optCon); - exit(EXIT_FAILURE); - break; - } - } - if (c < -1) + i=0; + while( option_table[i].descrition ) { - /* an error occurred during option processing */ - fprintf(stderr, "%s: %s\n", - poptBadOption(optCon, POPT_BADOPTION_NOALIAS), - poptStrerror(c)); - poptFreeContext(optCon); - exit(EXIT_FAILURE); + if( l && strcmp(l, option_table[i].longopt) == 0 ) + return &option_table[i];; + if( s && s==option_table[i].shortopt ) + return &option_table[i];; + i++; } + return NULL; +} - if( mpd_host ) - { - g_free(options.host); - options.host = mpd_host; - } - if( mpd_password ) - { - g_free(options.password); - options.password = mpd_password; - } - if( config_file ) +static void +display_help(void) +{ + int i = 0; + + printf("Usage: %s [OPTION]...\n", PACKAGE); + while( option_table[i].descrition ) { - g_free(options.config_file); - options.config_file = config_file; + char tmp[MAX_LONGOPT_LENGTH]; + + if( option_table[i].argument ) + g_snprintf(tmp, MAX_LONGOPT_LENGTH, "%s=%s", + option_table[i].longopt, + option_table[i].argument); + else + g_strlcpy(tmp, option_table[i].longopt, 64); + + printf(" -%c, --%-20s %s\n", + option_table[i].shortopt, + tmp, + option_table[i].descrition); + i++; } - if( key_file ) +} + +static void +handle_option(int c, char *arg) +{ + switch(c) { - g_free(options.key_file); - options.key_file = key_file; + case '?': /* --help */ + display_help(); + exit(EXIT_SUCCESS); + case 'V': /* --version */ + printf("Version %s\n", VERSION); + exit(EXIT_SUCCESS); + case 'c': /* --colors */ + options.enable_colors = TRUE; + break; + case 'C': /* --no-colors */ + options.enable_colors = FALSE; + break; + case 'e': /* --exit */ + options.reconnect = FALSE; + break; + case 'p': /* --port */ + options.port = atoi(arg); + break; + case 'h': /* --host */ + if( options.host ) + g_free(options.host); + options.host = g_strdup(arg); + break; + case 'P': /* --password */ + if( options.password ) + g_free(options.password); + options.password = locale_to_utf8(arg); + break; + case 'f': /* --config */ + if( options.config_file ) + g_free(options.config_file); + options.config_file = g_strdup(arg); + break; + case 'k': /* --key-file */ + if( options.key_file ) + g_free(options.key_file); + options.key_file = g_strdup(arg); + break; + case 'D': /* --debug */ + options.debug = TRUE; + break; + default: + fprintf(stderr,"Unknown Option %c = %s\n", c, arg); + break; } +} - poptFreeContext(optCon); +options_t * +options_get(void) +{ return &options; } options_t * +options_parse(int argc, const char *argv[]) +{ + int i; + arg_opt_t *opt = NULL; + option_callback_fn_t option_cb = handle_option; + + i=1; + while( i<argc ) + { + char *arg = (char *) argv[i]; + size_t len=strlen(arg); + + /* check for a long option */ + if( g_str_has_prefix(arg, "--") ) + { + char *value; + + /* retreive a option argument */ + if( (value=g_strrstr(arg+2, "=")) ) + { + *value = '\0'; + value++; + } + /* check if the option exists */ + if( (opt=lookup_option(0, arg+2)) == NULL ) + { + fprintf(stderr, PACKAGE ": invalid option %s\n", arg); + exit(EXIT_FAILURE); + } + /* abort if we got an argument to the option and dont want one */ + if( value && opt->argument==NULL ) + { + fprintf(stderr, PACKAGE ": invalid option argument %s=%s\n", + arg, value); + exit(EXIT_FAILURE); + } + /* execute option callback */ + if( value || opt->argument==NULL ) + { + option_cb (opt->shortopt, value); + opt = NULL; + } + } + /* check for a short option */ + else if( len==2 && g_str_has_prefix(arg, "-") ) + { + /* check if the option exists */ + if( (opt=lookup_option(arg[1], NULL))==NULL ) + { + fprintf(stderr, PACKAGE ": invalid option %s\n",arg); + exit(EXIT_FAILURE); + } + /* if no option argument is needed execute callback */ + if( opt->argument==NULL ) + { + option_cb (opt->shortopt, NULL); + opt = NULL; + } + } + else + { + /* is this a option argument? */ + if( opt && opt->argument) + { + option_cb (opt->shortopt, arg); + } + else + { + fprintf(stderr, PACKAGE ": bad argument: %s\n", arg); + exit(EXIT_FAILURE); + } + opt = NULL; + } + + i++; + } + return &options; +} + + +options_t * options_init( void ) { const char *value; @@ -185,9 +280,3 @@ options_init( void ) return &options; } - -options_t * -options_get(void) -{ - return &options; -} |