[xiph-commits] r13569 - in experimental/moritz/iceadmin: . examples include src

moritz at svn.xiph.org moritz at svn.xiph.org
Sat Aug 18 20:32:58 PDT 2007


Author: moritz
Date: 2007-08-18 20:32:58 -0700 (Sat, 18 Aug 2007)
New Revision: 13569

Added:
   experimental/moritz/iceadmin/examples/
   experimental/moritz/iceadmin/examples/Makefile.am
   experimental/moritz/iceadmin/examples/iceadmin.conf
   experimental/moritz/iceadmin/src/cfg.c
   experimental/moritz/iceadmin/src/cfg.h
Removed:
   experimental/moritz/iceadmin/include/config.h.in
Modified:
   experimental/moritz/iceadmin/Makefile.am
   experimental/moritz/iceadmin/configure.in
   experimental/moritz/iceadmin/include/
   experimental/moritz/iceadmin/src/Makefile.am
   experimental/moritz/iceadmin/src/iceadmin.c
Log:
Avoid losing work. Far from complete, but iceadmin (the utility) has
configfile support now.


Modified: experimental/moritz/iceadmin/Makefile.am
===================================================================
--- experimental/moritz/iceadmin/Makefile.am	2007-08-18 15:02:41 UTC (rev 13568)
+++ experimental/moritz/iceadmin/Makefile.am	2007-08-19 03:32:58 UTC (rev 13569)
@@ -1,6 +1,6 @@
 AUTOMAKE_OPTIONS = 1.9 foreign
 
-SUBDIRS =	compat doc include lib src
+SUBDIRS =	compat doc examples include lib src
 
 dist_doc_DATA = COPYING NEWS README
 

Modified: experimental/moritz/iceadmin/configure.in
===================================================================
--- experimental/moritz/iceadmin/configure.in	2007-08-18 15:02:41 UTC (rev 13568)
+++ experimental/moritz/iceadmin/configure.in	2007-08-19 03:32:58 UTC (rev 13569)
@@ -29,6 +29,16 @@
 
 dnl SETUP
 
+EXAMPLES_DIR="\$(datadir)/examples/${PACKAGE_TARNAME}"
+AC_ARG_ENABLE(examplesdir,
+	AS_HELP_STRING([--enable-examplesdir=DIR],
+		[example configuration files installation directory (default: DATADIR/examples/iceadmin)]),
+[case "$enableval" in
+	yes) ;;
+	no) AC_MSG_ERROR([Must have an example configuration files install dir.]) ;;
+	*) EXAMPLES_DIR="$enableval" ;;
+esac], [])
+
 AC_CANONICAL_HOST
 
 if test -z "$GCC"; then
@@ -166,6 +176,7 @@
 AC_SUBST(ICEADMIN_REQUIRES)
 
 AC_SUBST(LIBTOOL_DEPS)
+AC_SUBST(EXAMPLES_DIR)
 AC_SUBST(XALLOC_CPPFLAGS)
 AC_SUBST(COMPAT_INCLUDES)
 AC_SUBST(LIBICEADMIN_SO_MAJOR)
@@ -178,6 +189,7 @@
 		compat/Makefile \
 		compat/sys/Makefile \
 		doc/Makefile \
+		examples/Makefile \
 		include/Makefile \
 		lib/Makefile \
 		src/Makefile \


Property changes on: experimental/moritz/iceadmin/examples
___________________________________________________________________
Name: svn:ignore
   + Makefile.in


Added: experimental/moritz/iceadmin/examples/Makefile.am
===================================================================
--- experimental/moritz/iceadmin/examples/Makefile.am	                        (rev 0)
+++ experimental/moritz/iceadmin/examples/Makefile.am	2007-08-19 03:32:58 UTC (rev 13569)
@@ -0,0 +1,6 @@
+AUTOMAKE_OPTIONS = 1.9 foreign
+
+examplesdir =	@EXAMPLES_DIR@
+dist_examples_DATA = iceadmin.conf
+
+CLEANFILES =	core *.core *~ .*~

Added: experimental/moritz/iceadmin/examples/iceadmin.conf
===================================================================
--- experimental/moritz/iceadmin/examples/iceadmin.conf	                        (rev 0)
+++ experimental/moritz/iceadmin/examples/iceadmin.conf	2007-08-19 03:32:58 UTC (rev 13569)
@@ -0,0 +1,26 @@
+# Example configuration for IceAdmin
+#
+# Here, defaults for administrating one or more servers can be set, so
+# that they do not have to be provided on the command line each time.
+# Every set of defaults is associated with a unique ID, the "server 
+# token", and can be accessed from the command line.
+#
+# Comments begin with a '#' character and may appear anywhere on an
+# empty line or after a valid configuration directive. Comments, empty
+# lines and whitespaces are ignored.
+#
+# The configuration file is in the simple INI format. The unique server
+# token is defined in square brackets '[]' and followed by certain
+# key/value pairs. A new token denotes a new server default
+# configuration.
+#
+# Available configuration keys are 'hostname', 'service', 'username',
+# 'password' and 'default_mountpoint'.
+
+#[local] 	# A local Icecast server called "local".
+#    hostname		= "localhost"	# - This can be a hostname or an IP.
+#    service		= "8000"	# - Port number or service from
+#					#   /etc/services.
+#    username		= "admin"	# - The serveradmin username.
+#    password		= "hackme"	# - The serveradmin password.
+#    default_mount	= "/stream.ogg" # - The default mountpoint to work on.


Property changes on: experimental/moritz/iceadmin/include
___________________________________________________________________
Name: svn:ignore
   - Makefile.in

   + Makefile.in
config.h


Deleted: experimental/moritz/iceadmin/include/config.h.in
===================================================================
--- experimental/moritz/iceadmin/include/config.h.in	2007-08-18 15:02:41 UTC (rev 13568)
+++ experimental/moritz/iceadmin/include/config.h.in	2007-08-19 03:32:58 UTC (rev 13569)
@@ -1,121 +0,0 @@
-/* include/config.h.in.  Generated from configure.in by autoheader.  */
-
-/* Define to 1 if you have the <dlfcn.h> header file. */
-#undef HAVE_DLFCN_H
-
-/* Define to 1 if you have the <inttypes.h> header file. */
-#undef HAVE_INTTYPES_H
-
-/* Define to 1 if you have the <memory.h> header file. */
-#undef HAVE_MEMORY_H
-
-/* Define to 1 if you have the <netdb.h> header file. */
-#undef HAVE_NETDB_H
-
-/* Define whether we have struct sockaddr.sa_len */
-#undef HAVE_SOCKADDR_SA_LEN
-
-/* Define to 1 if you have the <stdint.h> header file. */
-#undef HAVE_STDINT_H
-
-/* Define to 1 if you have the <stdlib.h> header file. */
-#undef HAVE_STDLIB_H
-
-/* Define to 1 if you have the <strings.h> header file. */
-#undef HAVE_STRINGS_H
-
-/* Define to 1 if you have the <string.h> header file. */
-#undef HAVE_STRING_H
-
-/* Define to 1 if you have the `strlcat' function. */
-#undef HAVE_STRLCAT
-
-/* Define to 1 if you have the <sys/socket.h> header file. */
-#undef HAVE_SYS_SOCKET_H
-
-/* Define to 1 if you have the <sys/stat.h> header file. */
-#undef HAVE_SYS_STAT_H
-
-/* Define to 1 if you have the <sys/tree.h> header file. */
-#undef HAVE_SYS_TREE_H
-
-/* Define to 1 if you have the <sys/types.h> header file. */
-#undef HAVE_SYS_TYPES_H
-
-/* Define to 1 if you have the <unistd.h> header file. */
-#undef HAVE_UNISTD_H
-
-/* Define whether RB_FOREACH is defined in <sys/tree.h> */
-#undef HAVE_WORKING_SYS_TREE_H
-
-/* Iceadmin library major version */
-#undef LIBICEADMIN_MAJOR
-
-/* Iceadmin library patch version */
-#undef LIBICEADMIN_MICRO
-
-/* Iceadmin library minor version */
-#undef LIBICEADMIN_MINOR
-
-/* Name of package */
-#undef PACKAGE
-
-/* Define to the address where bug reports for this package should be sent. */
-#undef PACKAGE_BUGREPORT
-
-/* Define to the full name of this package. */
-#undef PACKAGE_NAME
-
-/* Define to the full name and version of this package. */
-#undef PACKAGE_STRING
-
-/* Define to the one symbol short name of this package. */
-#undef PACKAGE_TARNAME
-
-/* Define to the version of this package. */
-#undef PACKAGE_VERSION
-
-/* Define to 1 if you have the ANSI C header files. */
-#undef STDC_HEADERS
-
-/* Version number of package */
-#undef VERSION
-
-/* Define to 1 if on AIX 3.
-   System headers sometimes define this.
-   We just want to avoid a redefinition error message.  */
-#ifndef _ALL_SOURCE
-# undef _ALL_SOURCE
-#endif
-
-/* Enable GNU extensions on systems that have them.  */
-#ifndef _GNU_SOURCE
-# undef _GNU_SOURCE
-#endif
-
-/* Define to 1 if on MINIX. */
-#undef _MINIX
-
-/* Define to 2 if the system does not provide POSIX.1 features except with
-   this defined. */
-#undef _POSIX_1_SOURCE
-
-/* Define to 1 if you need to in order for `stat' and other things to work. */
-#undef _POSIX_SOURCE
-
-/* Enable extensions on Solaris.  */
-#ifndef __EXTENSIONS__
-# undef __EXTENSIONS__
-#endif
-#ifndef _POSIX_PTHREAD_SEMANTICS
-# undef _POSIX_PTHREAD_SEMANTICS
-#endif
-#ifndef _TANDEM_SOURCE
-# undef _TANDEM_SOURCE
-#endif
-
-/* Define to empty if `const' does not conform to ANSI C. */
-#undef const
-
-/* Define to `unsigned int' if <sys/types.h> does not define. */
-#undef size_t

Modified: experimental/moritz/iceadmin/src/Makefile.am
===================================================================
--- experimental/moritz/iceadmin/src/Makefile.am	2007-08-18 15:02:41 UTC (rev 13568)
+++ experimental/moritz/iceadmin/src/Makefile.am	2007-08-19 03:32:58 UTC (rev 13569)
@@ -2,12 +2,14 @@
 
 bin_PROGRAMS =	iceadmin
 
-iceadmin_SOURCES = iceadmin.c $(top_srcdir)/lib/xalloc.c
+iceadmin_SOURCES = cfg.c iceadmin.c $(top_srcdir)/lib/xalloc.c
 iceadmin_LDADD = $(top_builddir)/lib/libiceadmin.la @XML2_LIBS@
 
 INCLUDES =	@COMPAT_INCLUDES@ -I$(top_srcdir)/include -I$(top_srcdir)/lib
 AM_CFLAGS =	@XML2_CFLAGS@
-# AM_CPPFLAGS =	@XIPH_CPPFLAGS@ -DUSE_XALLOC=1
-AM_CPPFLAGS =	@XIPH_CPPFLAGS@
+AM_CPPFLAGS =	@XIPH_CPPFLAGS@ -DUSE_XALLOC=1 -DXALLOC_DEBUG=1
+# AM_CPPFLAGS =	@XIPH_CPPFLAGS@
 
+EXTRA_DIST =	cfg.h
+
 CLEANFILES =	core *.core *~ .*~

Added: experimental/moritz/iceadmin/src/cfg.c
===================================================================
--- experimental/moritz/iceadmin/src/cfg.c	                        (rev 0)
+++ experimental/moritz/iceadmin/src/cfg.c	2007-08-19 03:32:58 UTC (rev 13569)
@@ -0,0 +1,528 @@
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <sys/tree.h>
+#include <errno.h>
+#include <limits.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#ifdef HAVE_UNISTD_H
+# include <unistd.h>
+#endif
+
+#include "cfg.h"
+#include "xalloc.h"
+
+#ifndef PATH_MAX
+# define PATH_MAX	256
+#endif /* !PATH_MAX */
+
+extern char		*__progname;
+
+struct server_pref {
+	RB_ENTRY(server_pref)	 entry;
+	char			*token;
+	unsigned int		 cfg_line;
+	char			*hostname;
+	char			*service;
+	char			*username;
+	char			*password;
+	char			*default_mount;
+};
+RB_HEAD(server_pref_tree, server_pref) server_pref_tree_head = RB_INITIALIZER(&server_pref_tree_head);
+int	                _cfg_serverpref_cmp(void *, void *);
+RB_PROTOTYPE(server_pref_tree, server_pref, entry, _cfg_serverpref_cmp)
+RB_GENERATE(server_pref_tree, server_pref, entry, _cfg_serverpref_cmp)
+
+void			_cfg_print_usage(void);
+void			_cfg_print_usagehelp(void);
+struct server_pref *	_cfg_serverpref_create(const char *, const char *,
+					       const char *, const char *,
+					       const char *, const char *);
+void			_cfg_serverpref_free(struct server_pref **);
+int			_cfg_parse_cfgfile(FILE *, const char *);
+char *			_cfg_parse_token(const char *);
+char *			_cfg_parse_keyvalue(const char *, const char *,
+					    const char *, unsigned int);
+
+static const char *optstring = "c:h";
+
+void
+_cfg_print_usage(void)
+{
+}
+
+void
+_cfg_print_usagehelp(void)
+{
+}
+
+int
+cfg_parse_cmdline(int argc, char *argv[])
+{
+	extern char	*optarg;
+	extern int	 optind;
+	int		 ch;
+	FILE		*cfgfile;
+	char		*home;
+	char		 cfgfile_name[PATH_MAX];
+
+	cfgfile = NULL;
+	cfgfile_name[0] = '\0';
+
+	while ((ch = getopt(argc, argv, optstring)) != -1) {
+		switch (ch) {
+		case 'h':
+			_cfg_print_usage();
+			_cfg_print_usagehelp();
+			return (0);
+		case 'c':
+			if (cfgfile != NULL) {
+				fprintf(stderr, "%s: Error: Multiple -c arguments given\n",
+					__progname);
+				fclose(cfgfile);
+				return (2);
+			}
+			snprintf(cfgfile_name, sizeof(cfgfile_name), "%s", optarg);
+			if ((cfgfile = fopen(cfgfile_name, "r")) == NULL) {
+				fprintf(stderr, "%s: %s: %s\n", cfgfile_name,
+					strerror(errno), __progname);
+				return (2);
+			}
+			break;
+		default:
+			_cfg_print_usage();
+			return (2);
+		}
+	}
+	argc -= optind;
+	argv += optind;
+
+	if (cfgfile == NULL && (home = getenv("HOME")) != NULL) {
+		snprintf(cfgfile_name, sizeof(cfgfile_name), "%s/%s", home, DEFAULT_CONFIG_FILE);
+		cfgfile = fopen(cfgfile_name, "r");
+	}
+
+	if (_cfg_parse_cfgfile(cfgfile, cfgfile_name) != 0) {
+		fclose(cfgfile);
+		return (1);
+	}
+
+	if (cfgfile != NULL)
+		fclose(cfgfile);
+
+	return (CONFIG_OKAY);
+}
+
+void
+cfg_initialize(void)
+{
+}
+
+void
+cfg_shutdown(void)
+{
+	struct server_pref	*cur, *next;
+
+	for (cur = RB_MIN(server_pref_tree, &server_pref_tree_head);
+	     cur != NULL; cur = next) {
+		next = RB_NEXT(server_pref_tree, &server_pref_tree_head, cur);
+		RB_REMOVE(server_pref_tree, &server_pref_tree_head, cur);
+		_cfg_serverpref_free(&cur);
+	}
+}
+
+void
+cfg_print_help(void)
+{
+	_cfg_print_usage();
+	_cfg_print_usagehelp();
+}
+
+int
+_cfg_serverpref_cmp(void *arg_a, void *arg_b)
+{
+	struct server_pref	*a = (struct server_pref *)arg_a;
+	struct server_pref	*b = (struct server_pref *)arg_b;
+
+	return (strcmp(a->token, b->token));
+}
+
+struct server_pref *
+_cfg_serverpref_create(const char *token, const char *hostname,
+		       const char *service, const char *username,
+		       const char *password, const char *default_mount)
+{
+	struct server_pref      *srvp;
+
+	srvp = xcalloc(1, sizeof(struct server_pref));
+	srvp->token = xstrdup(token);
+	if (hostname != NULL)
+		srvp->hostname = xstrdup(hostname);
+	if (service != NULL)
+		srvp->service = xstrdup(service);
+	if (username != NULL)
+		srvp->username = xstrdup(username);
+	if (password != NULL)
+		srvp->password = xstrdup(password);
+	if (default_mount != NULL)
+		srvp->default_mount = xstrdup(default_mount);
+
+	return (srvp);
+}
+
+void
+_cfg_serverpref_free(struct server_pref **srvp_p)
+{
+	struct server_pref      *srvp = *srvp_p;
+
+	if (srvp->token != NULL)
+		xfree(srvp->token);
+	if (srvp->hostname != NULL)
+		xfree(srvp->hostname);
+	if (srvp->service != NULL)
+		xfree(srvp->service);
+	if (srvp->username != NULL)
+		xfree(srvp->username);
+	if (srvp->password != NULL)
+		xfree(srvp->password);
+	if (srvp->default_mount != NULL)
+		xfree(srvp->default_mount);
+	memset(srvp, 0, sizeof(struct server_pref));
+	xfree(*srvp_p);
+	*srvp_p = NULL;
+}
+
+int
+_cfg_parse_cfgfile(FILE *filep, const char *filename)
+{
+	char		 buf[BUFSIZ];
+	char		*bp;
+	unsigned int	 line;
+	char		*token;
+	char		*hostname, *service, *username, *password,
+				*default_mount;
+
+	if (filep == NULL)
+		return (0);
+
+	line = 0;
+	token = NULL;
+	hostname = service = username = password = default_mount = NULL;
+	while (fgets(buf, sizeof(buf), filep) != NULL) {
+		int	cont;
+
+		line++;
+
+		if (strlen(buf) == sizeof(buf) - 1) {
+			fprintf(stderr, "%s: %s[%u]: Line too long\n",
+				__progname, filename, line);
+			return (1);
+		}
+
+		/* Trim trailing newlines and carriage returns: */
+		buf[strcspn(buf, "\n")] = '\0';
+		buf[strcspn(buf, "\r")] = '\0';
+		if (buf[0] == '\0')
+			continue;
+
+		/* Skip whitespaces at the beginning of a line: */
+		for (bp = buf; bp != '\0'; bp++) {
+			if (*bp != ' ' && *bp != '\t')
+				break;
+		}
+
+		/* Skip comments and lines with whitespaces only: */
+		if (*bp == '\0' || *bp == '#')
+			continue;
+
+		if (*bp == '[') {
+			if (token != NULL) {
+				/* Add whatever we've got to the tree: */
+				struct server_pref *srvp, *exists;
+
+				srvp = _cfg_serverpref_create(token, hostname,
+							      service, username,
+							      password,
+							      default_mount);
+				srvp->cfg_line = line;
+				exists = RB_INSERT(server_pref_tree, &server_pref_tree_head, srvp);
+				xfree(token);
+				token = NULL;
+				if (hostname != NULL) {
+					xfree(hostname);
+					hostname = NULL;
+				}
+				if (service != NULL) {
+					xfree(service);
+					service = NULL;
+				}
+				if (username != NULL) {
+					xfree(username);
+					username = NULL;
+				}
+				if (password != NULL) {
+					xfree(password);
+					password = NULL;
+				}
+				if (default_mount != NULL) {
+					xfree(default_mount);
+					default_mount = NULL;
+				}
+				if (exists != NULL) {
+					fprintf(stderr, "%s: %s[%u]: Duplicate server token '%s' (already defined in line %u)\n",
+						__progname, filename, line,
+						exists->token,
+						exists->cfg_line);
+					return (1);
+				}
+			}
+			if ((token = _cfg_parse_token(bp)) == NULL) {
+				fprintf(stderr, "%s: %s[%u]: Invalid server token\n",
+					__progname, filename, line);
+				return (1);
+			}
+			continue;
+		}
+
+		cont = 1;
+		if (token == NULL) {
+			fprintf(stderr, "%s: %s[%u]: Missing server token\n",
+				__progname, filename, line);
+			cont = 0;
+		} else if (strncasecmp(bp, CFG_KEY_HOSTNAME,
+				       strlen(CFG_KEY_HOSTNAME)) == 0) {
+			if (hostname != NULL) {
+				fprintf(stderr, "%s: %s[%u]: Duplicate '%s' key in server configuration '%s'\n",
+					__progname, filename, line,
+					CFG_KEY_HOSTNAME, token);
+				cont = 0;
+			} else if ((hostname = _cfg_parse_keyvalue(bp, CFG_KEY_HOSTNAME, filename, line))
+				   == NULL) {
+				cont = 0;
+			}
+		} else if (strncasecmp(bp, CFG_KEY_SERVICE,
+				       strlen(CFG_KEY_SERVICE)) == 0) {
+			if (service != NULL) {
+				fprintf(stderr, "%s: %s[%u]: Duplicate '%s' key in server configuration '%s'\n",
+					__progname, filename, line,
+					CFG_KEY_SERVICE, token);
+				cont = 0;
+			} else if ((service = _cfg_parse_keyvalue(bp, CFG_KEY_SERVICE, filename, line))
+				   == NULL) {
+				cont = 0;
+			}
+		} else if (strncasecmp(bp, CFG_KEY_USERNAME,
+				       strlen(CFG_KEY_USERNAME)) == 0) {
+			if (username != NULL) {
+				fprintf(stderr, "%s: %s[%u]: Duplicate '%s' key in server configuration '%s'\n",
+					__progname, filename, line,
+					CFG_KEY_USERNAME, token);
+				cont = 0;
+			} else if ((username = _cfg_parse_keyvalue(bp, CFG_KEY_USERNAME, filename, line))
+				   == NULL) {
+				cont = 0;
+			}
+		} else if (strncasecmp(bp, CFG_KEY_PASSWORD,
+				       strlen(CFG_KEY_PASSWORD)) == 0) {
+			if (password != NULL) {
+				fprintf(stderr, "%s: %s[%u]: Duplicate '%s' key in server configuration '%s'\n",
+					__progname, filename, line,
+					CFG_KEY_PASSWORD, token);
+				cont = 0;
+			} else if ((password = _cfg_parse_keyvalue(bp, CFG_KEY_PASSWORD, filename, line))
+				   == NULL) {
+				cont = 0;
+			}
+		} else if (strncasecmp(bp, CFG_KEY_DEFAULT_MOUNT,
+				       strlen(CFG_KEY_DEFAULT_MOUNT)) == 0) {
+			if (default_mount != NULL) {
+				fprintf(stderr, "%s: %s[%u]: Duplicate '%s' key in server configuration '%s'\n",
+					__progname, filename, line,
+					CFG_KEY_DEFAULT_MOUNT, token);
+				cont = 0;
+			} else if ((default_mount = _cfg_parse_keyvalue(bp, CFG_KEY_DEFAULT_MOUNT, filename, line))
+				   == NULL) {
+				cont = 0;
+			}
+		} else {
+			fprintf(stderr, "%s: %s[%u]: Unknown configuration key/value pair '%s'\n",
+				__progname, filename, line, bp);
+			cont = 0;
+		}
+
+		if (!cont) {
+			if (token != NULL) {
+				xfree(token);
+				token = NULL;
+			}
+			if (hostname != NULL) {
+				xfree(hostname);
+				hostname = NULL;
+			}
+			if (service != NULL) {
+				xfree(service);
+				service = NULL;
+			}
+			if (username != NULL) {
+				xfree(username);
+				username = NULL;
+			}
+			if (password != NULL) {
+				xfree(password);
+				password = NULL;
+			}
+			if (default_mount != NULL) {
+				xfree(default_mount);
+				default_mount = NULL;
+			}
+			return (1);
+		}
+	}
+	if (ferror(filep)) {
+		fprintf(stderr, "%s: %s: %s\n", __progname, filename, strerror(errno));
+		return (1);
+	}
+	if (token != NULL) {
+		/* Add the final server preference to the tree: */
+		struct server_pref *srvp;
+
+		srvp = _cfg_serverpref_create(token, hostname,
+					      service, username,
+					      password,
+					      default_mount);
+		RB_INSERT(server_pref_tree, &server_pref_tree_head, srvp);
+		xfree(token);
+		token = NULL;
+		if (hostname != NULL) {
+			xfree(hostname);
+			hostname = NULL;
+		}
+		if (service != NULL) {
+			xfree(service);
+			service = NULL;
+		}
+		if (username != NULL) {
+			xfree(username);
+			username = NULL;
+		}
+		if (password != NULL) {
+			xfree(password);
+			password = NULL;
+		}
+		if (default_mount != NULL) {
+			xfree(default_mount);
+			default_mount = NULL;
+		}
+	}
+
+	return (0);
+}
+
+char *
+_cfg_parse_token(const char *buf)
+{
+	char		 tbuf[BUFSIZ];
+	const char	*bp;
+	unsigned int	 i, okay;
+	char		*ret;
+	size_t		 retsize;
+
+	ret = NULL;
+
+	okay = 0;
+	bp = buf + 1;
+	for (i = 0; i < (sizeof(tbuf) - 1) && *bp != '\0'; i++, bp++) {
+		if (*bp == ']') {
+			okay = 1;
+			break;
+		}
+		tbuf[i] = *bp;
+	}
+	tbuf[i] = '\0';
+
+	if (!okay)
+		return (ret);
+
+	retsize = strlen(tbuf) + 1;
+	ret = xcalloc(retsize, sizeof(char));
+	snprintf(ret, retsize, "%s", tbuf);
+
+	return (ret);
+}
+
+char *
+_cfg_parse_keyvalue(const char *buf, const char *key, const char *filename,
+		    unsigned int line)
+{
+	char		 tbuf[BUFSIZ];
+	const char	*bp;
+	unsigned int	 i, okay;
+	char		*ret;
+	size_t		 retsize;
+
+	ret = NULL;
+
+	bp = buf;
+	/* Skip the key, we already have it. */
+	bp += strlen(key);
+	/* Skip any whitespaces after the key. */
+	while (*bp != '\0' && (*bp == ' ' || *bp == '\t'))
+		bp++;
+	/* An equal sign is now expected. */
+	if (*bp++ != '=') {
+		fprintf(stderr, "%s: %s[%u]: Malformed ``key = \"value\"'': Bad key or missing '='\n",
+			__progname, filename, line);
+		return (ret);
+	}
+	/* Skip any whitespaces after the equal sign. */
+	while (*bp != '\0' && (*bp == ' ' || *bp == '\t'))
+		bp++;
+	/* Double-quotes around the key value are required. */
+	if (*bp++ != '"') {
+		fprintf(stderr, "%s: %s[%u]: Malformed ``key = \"value\"'': Missing opening quotes\n",
+			__progname, filename, line);
+		return (ret);
+	}
+
+	/* Read value until the second quote. '\\' is the escape character. */
+	okay = 0;
+	for (i = 0; i < (sizeof(tbuf) - 1) && *bp != '\0'; i++, bp++) {
+		if (*bp == '"') {
+			okay = 1;
+			break;
+		}
+		if (*bp == '\\')
+			bp++;
+		tbuf[i] = *bp;
+	}
+	tbuf[i] = '\0';
+	if (!okay) {
+		if (i >= sizeof(tbuf) - 1)
+			fprintf(stderr, "%s: %s[%u]: Malformed ``key = \"value\"'': Value too long\n",
+				__progname, filename, line);
+		else
+			fprintf(stderr, "%s: %s[%u]: Malformed ``key = \"value\"'': Missing closing quotes\n",
+				__progname, filename, line);
+		return (ret);
+	}
+
+	/* Skip closing quote. */
+	bp++;
+	/* Whitespaces and comments behind the value are ok, the rest not. */
+	while (*bp != '\0' && (*bp == ' ' || *bp == '\t'))
+		bp++;
+	if (*bp != '\0' && *bp != '#')  {
+		fprintf(stderr, "%s: %s[%u]: Malformed ``key = \"value\"'': Garbage after \"%s\"\n",
+			__progname, filename, line, tbuf);
+		return (ret);
+	}
+
+	retsize = strlen(tbuf) + 1;
+	ret = xcalloc(retsize, sizeof(char));
+	snprintf(ret, retsize, "%s", tbuf);
+
+	return (ret);
+}

Added: experimental/moritz/iceadmin/src/cfg.h
===================================================================
--- experimental/moritz/iceadmin/src/cfg.h	                        (rev 0)
+++ experimental/moritz/iceadmin/src/cfg.h	2007-08-19 03:32:58 UTC (rev 13569)
@@ -0,0 +1,31 @@
+#ifndef __CFG_H__
+#define __CFG_H__
+
+#define DEFAULT_CONFIG_FILE	".iceadminrc"
+
+#define CFG_KEY_HOSTNAME	"hostname"
+#define CFG_KEY_SERVICE 	"service"
+#define CFG_KEY_USERNAME	"username"
+#define CFG_KEY_PASSWORD	"password"
+#define CFG_KEY_DEFAULT_MOUNT	"default_mount"
+
+#define CONFIG_OKAY		0x7A69
+
+#ifndef PATH_SEPARATOR
+# ifdef WIN32
+#  define PATH_SEPARATOR	'\\'
+# else
+#  define PATH_SEPARATOR	'/'
+# endif /* WIN32 */
+#endif /* !PATH_SEPARATOR */
+#ifndef BUFSIZ
+# define BUFSIZ 		1024
+#endif /* !BUFSIZ */
+
+void	cfg_initialize(void);
+void	cfg_shutdown(void);
+
+int	cfg_parse_cmdline(int, char *[]);
+void	cfg_print_help(void);
+
+#endif /* __CFG_H__ */

Modified: experimental/moritz/iceadmin/src/iceadmin.c
===================================================================
--- experimental/moritz/iceadmin/src/iceadmin.c	2007-08-18 15:02:41 UTC (rev 13568)
+++ experimental/moritz/iceadmin/src/iceadmin.c	2007-08-19 03:32:58 UTC (rev 13569)
@@ -3,31 +3,63 @@
 #endif
 
 #include <stdio.h>
+#include <string.h>
 
 #include <iceadmin.h>
-#include <xalloc.h>
 
+#include "cfg.h"
+#include "xalloc.h"
+
+#ifdef HAVE___PROGNAME
+extern char		*__progname;
+#else
+char			*__progname;
+#endif /* HAVE___PROGNAME */
+
 void	init_self(void);
 int	shutdown_self(int);
+char *  get_progname(const char *);
 
 void
 init_self(void)
 {
 	xalloc_initialize_debug(2, stderr);
 	iceadmin_initialize();
+	cfg_initialize();
 }
 
 int
 shutdown_self(int ret)
 {
+	cfg_shutdown();
 	iceadmin_shutdown();
 	xalloc_shutdown();
 
 	return (ret);
 }
 
+char *
+get_progname(const char *argv0)
+{
+#ifdef HAVE___PROGNAME
+	return (strdup(__progname));
+#else
+	char	*p;
+
+	if (argv0 == NULL)
+		return ((char *)"iceadmin");
+	p = strrchr(argv0, PATH_SEPARATOR);
+	if (p == NULL)
+		p = (char *)argv0;
+	else
+		p++;
+
+	return (strdup(p));
+#endif /* HAVE___PROGNAME */
+}
+
 int
-main(void)
+main(int argc, char *argv[])
 {
 	iceadmin_t	 iceadmin;
 	char		 errbuf[256];
@@ -35,81 +67,12 @@
 	size_t		 bufsize;
 	int		 ret;
 
+	__progname = get_progname(argv[0]);
+
 	init_self();
 
-	if (iceadmin_new(&iceadmin, "/stream.ogg", "example.domain", "8000", "admin", "hackme") != ICEADMIN_SUCCESS) {
-		iceadmin_get_error(iceadmin, errbuf, sizeof(errbuf));
-		fprintf(stderr, "%s", errbuf);
-		iceadmin_delete(iceadmin);
-		return (shutdown_self(1));
-	}
+	if ((ret = cfg_parse_cmdline(argc, argv)) != CONFIG_OKAY)
+		return (shutdown_self(ret));
 
-	if ((ret = iceadmin_cmd_get_stats(iceadmin, &buf, &bufsize)) != ICEADMIN_SUCCESS) {
-		iceadmin_get_error(iceadmin, errbuf, sizeof(errbuf));
-		fprintf(stderr, "Get status: %s", errbuf);
-		if (ret != ICEADMIN_FAILURE) {
-			iceadmin_delete(iceadmin);
-			return (shutdown_self(1));
-		}
-	}
-
-	printf("%s\n", buf);
-	iceadmin_free(buf);
-
-	if ((ret = iceadmin_cmd_list_mounts(iceadmin, &buf, &bufsize)) != ICEADMIN_SUCCESS) {
-		iceadmin_get_error(iceadmin, errbuf, sizeof(errbuf));
-		fprintf(stderr, "List mounts: %s", errbuf);
-		if (ret != ICEADMIN_FAILURE) {
-			iceadmin_delete(iceadmin);
-			return (shutdown_self(1));
-		}
-	}
-
-	printf("%s\n", buf);
-	iceadmin_free(buf);
-
-	if ((ret = iceadmin_cmd_list_clients(iceadmin, &buf, &bufsize)) != ICEADMIN_SUCCESS) {
-		iceadmin_get_error(iceadmin, errbuf, sizeof(errbuf));
-		fprintf(stderr, "List clients: %s", errbuf);
-		if (ret != ICEADMIN_FAILURE) {
-			iceadmin_delete(iceadmin);
-			return (shutdown_self(1));
-		}
-	}
-
-	printf("%s\n", buf);
-	iceadmin_free(buf);
-
-	/*
-	if ((ret = iceadmin_cmd_kill_source(iceadmin)) != ICEADMIN_SUCCESS) {
-		iceadmin_get_error(iceadmin, errbuf, sizeof(errbuf));
-		fprintf(stderr, "Kill source: %s", errbuf);
-		if (ret != ICEADMIN_FAILURE) {
-			iceadmin_delete(iceadmin);
-			return (shutdown_self(1));
-		}
-	}
-	*/
-
-	if ((ret = iceadmin_cmd_kill_client(iceadmin, 354)) != ICEADMIN_SUCCESS) {
-		iceadmin_get_error(iceadmin, errbuf, sizeof(errbuf));
-		fprintf(stderr, "Kill client: %s", errbuf);
-		if (ret != ICEADMIN_FAILURE) {
-			iceadmin_delete(iceadmin);
-			return (shutdown_self(1));
-		}
-	}
-
-	if ((ret = iceadmin_cmd_update_metadata(iceadmin, "Yay! Test!?!?!")) != ICEADMIN_SUCCESS) {
-		iceadmin_get_error(iceadmin, errbuf, sizeof(errbuf));
-		fprintf(stderr, "Update metadata: %s", errbuf);
-		if (ret != ICEADMIN_FAILURE) {
-			iceadmin_delete(iceadmin);
-			return (shutdown_self(1));
-		}
-	}
-
-	iceadmin_delete(iceadmin);
-
 	return (shutdown_self(0));
 }



More information about the commits mailing list