From 7bf87192828e33fbdad2dd2a82474acaffc1bdf1 Mon Sep 17 00:00:00 2001 From: Kalle Wallin Date: Thu, 1 Jul 2004 21:42:53 +0000 Subject: [PATCH] Removed popt code/dependency git-svn-id: https://svn.musicpd.org/ncmpc/trunk@1761 09075e82-0dd4-0310-85a5-a0d7c8717e4f --- NEWS | 2 +- TODO | 7 +- configure.ac | 44 ++++++-- src/options.c | 303 ++++++++++++++++++++++++++++++++------------------ 4 files changed, 235 insertions(+), 121 deletions(-) diff --git a/NEWS b/NEWS index 43ebcfb..959339d 100644 --- a/NEWS +++ b/NEWS @@ -1,5 +1,5 @@ ncmpc-0.11.0 - + + removed popt dependency + the default port is now 6600 (mpd-0.11.0) + nls support (glib-2.4 required for nls) + ncmpc now uses the terminal in raw mode (enabling key bindings to Ctrl-C...) diff --git a/TODO b/TODO index 0d833b8..3a8db60 100644 --- a/TODO +++ b/TODO @@ -5,9 +5,14 @@ Features: * id3 browser Other: -* Remove popt dependency * Add libgpm support? +Glib +* replace snprintf with g_snprintf or g_strdup_printf +* replace strcat with g_strlcat or g_strconcat() +* replace strncpy with g_strlcpy +* replace strstr with g_strrstr +* ... diff --git a/configure.ac b/configure.ac index c708b7f..4aadbeb 100644 --- a/configure.ac +++ b/configure.ac @@ -4,7 +4,7 @@ dnl AC_INIT AC_CONFIG_SRCDIR([src/main.c]) -AM_INIT_AUTOMAKE(ncmpc, 0.11.0-svn) +AM_INIT_AUTOMAKE(ncmpc, 0.11.0-svn-20040630) AM_CONFIG_HEADER([config.h]) dnl Check for programs @@ -16,6 +16,9 @@ dnl ======================================================= dnl initialize variables dnl ======================================================= +dnl i18n +ALL_LINGUAS="sv" + set -- $CFLAGS dnl @@ -49,15 +52,10 @@ dnl Check for libraries dnl dnl ncurses -AC_CHECK_LIB([ncurses], [initscr],, +AC_CHECK_LIB([ncurses], + [initscr], + [LIBS="$LIBS -lncurses"], [AC_MSG_ERROR(ncurses library is required)]) -LIBS="$LIBS -lncurses" - -dnl popt -AC_CHECK_LIB([popt], - [poptGetArg], - [LIBS="$LIBS -lpopt"], - AC_MSG_ERROR([Missing popt command line parsing library])) dnl Check for glib-2.4 @@ -74,8 +72,6 @@ fi dnl i18n -ALL_LINGUAS="sv" - AM_NLS if test "x$glib24" = "xyes"; then nls=$USE_NLS @@ -101,7 +97,7 @@ dnl Debugging AC_MSG_CHECKING([whether to build with debug support]) AC_ARG_ENABLE([debug], AC_HELP_STRING([--enable-debug], - [Enable debugging @<:@default=no@:>@)]), + [Enable debugging @<:@default=no@:>@]), [enable_debug="$enableval"], [enable_debug=no]) AC_MSG_RESULT([$enable_debug]) @@ -109,6 +105,30 @@ if test "x$enable_debug" = "xyes" ; then CFLAGS="$CFLAGS -Wall -g -DDEBUG" fi +dnl Optional screen - help screen +#AC_MSG_CHECKING([whether to include the help screen]) +#AC_ARG_ENABLE([help-screen], +# AC_HELP_STRING([--enable-help-screen], +# [Enable the help screen @<:@default=yes@:>@]), +# [help_screen="$enableval"], +# [help_screen=yes]) +#AC_MSG_RESULT([$help_screen]) +#if test "x$help_screen" = "xyes" ; then +# AC_DEFINE(ENABLE_HELP_SCREEN, 1, [Enable the help screen]) +#fi + +dnl Optional screen - browse screen +#AC_MSG_CHECKING([whether to include the browse screen]) +#AC_ARG_ENABLE([browse-screen], +# AC_HELP_STRING([--enable-browse-screen], +# [Enable the browse screen @<:@default=yes@:>@]), +# [browse_screen="$enableval"], +# [browse_screen=yes]) +#AC_MSG_RESULT([$browse_screen]) +#if test "x$browse_screen" = "xyes" ; then +# AC_DEFINE(ENABLE_BROWSE_SCREEN, 1, [Enable the browse screen]) +#fi + dnl Optional screen - key editor AC_MSG_CHECKING([whether to include the key editor screen]) AC_ARG_ENABLE([key-screen], diff --git a/src/options.c b/src/options.c index 0aa8122..3751aae 100644 --- a/src/options.c +++ b/src/options.c @@ -20,133 +20,228 @@ #include #include -#include #include -#include #include -#include #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( iargument==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 ) { @@ -185,9 +280,3 @@ options_init( void ) return &options; } - -options_t * -options_get(void) -{ - return &options; -} -- 2.30.2