summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 95633d4)
raw | patch | inline | side by side (parent: 95633d4)
author | Kalle Wallin <kaw@linux.se> | |
Thu, 1 Jul 2004 21:42:53 +0000 (21:42 +0000) | ||
committer | Kalle Wallin <kaw@linux.se> | |
Thu, 1 Jul 2004 21:42:53 +0000 (21:42 +0000) |
NEWS | patch | blob | history | |
TODO | patch | blob | history | |
configure.ac | patch | blob | history | |
src/options.c | patch | blob | history |
index 43ebcfb6c7d658f921d7bf88c8baeadc0b243ea1..959339d716f2a6bc1aba3c43f94c545246b6bfa1 100644 (file)
--- a/NEWS
+++ b/NEWS
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...)
index 0d833b8b1dedcf63ca72a68da50b9f13812917e6..3a8db603ddeb86b8c63b4a81580a7ea1327cea90 100644 (file)
--- a/TODO
+++ b/TODO
* 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 c708b7febfa7bac7fd5c1606f9820a8d99cd149f..4aadbeb0b14d6e3e10b7aa1d52880d86e953a6a5 100644 (file)
--- a/configure.ac
+++ b/configure.ac
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
dnl initialize variables
dnl =======================================================
+dnl i18n
+ALL_LINGUAS="sv"
+
set -- $CFLAGS
dnl
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
dnl i18n
-ALL_LINGUAS="sv"
-
AM_NLS
if test "x$glib24" = "xyes"; then
nls=$USE_NLS
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])
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 0aa812204554b1230988fe78948b553834b323ff..3751aae424806f29e5c59a2316192aef1126a23b 100644 (file)
--- a/src/options.c
+++ b/src/options.c
#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 )
{
return &options;
}
-
-options_t *
-options_get(void)
-{
- return &options;
-}