Permalink
Browse files

Bucket engine builds.

  • Loading branch information...
1 parent eeec429 commit a035409401061660dc12499fff8605bdf968b762 @dustin dustin committed Nov 19, 2009
Showing with 358 additions and 1 deletion.
  1. 0 AUTHORS
  2. +19 −0 COPYING
  3. 0 ChangeLog
  4. +1 −1 Makefile.am
  5. 0 NEWS
  6. 0 README
  7. +74 −0 autogen.sh
  8. +202 −0 config_parser.c
  9. +62 −0 config_parser.h
View
No changes.
View
@@ -0,0 +1,19 @@
+Copyright (c) 2006-2009 Dustin Sallings <dustin@spy.net>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the "Software"), to deal in
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
+of the Software, and to permit persons to whom the Software is furnished to do
+so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
View
No changes.
View
@@ -1,3 +1,3 @@
lib_LTLIBRARIES = bucket_engine.la
-bucket_engine_la_SOURCES= bucket_engine.c
+bucket_engine_la_SOURCES= bucket_engine.c config_parser.c config_parser.h
bucket_engine_la_LDFLAGS= -module -dynamic
View
No changes.
View
No changes.
View
@@ -0,0 +1,74 @@
+#!/bin/sh
+
+LIBTOOLIZE_FLAGS=" --automake --copy --force"
+AUTOMAKE_FLAGS="--add-missing --copy --force"
+ACLOCAL_FLAGS=""
+
+die() { echo "$@"; exit 1; }
+
+ARGV0=$0
+ARGS="$@"
+
+run() {
+ echo "$ARGV0: running \`$@' $ARGS"
+ $@ $ARGS
+}
+
+# Try to locate a program by using which, and verify that the file is an
+# executable
+locate_binary() {
+ for f in $@
+ do
+ file=`which $f 2>/dev/null | grep -v '^no '`
+ if test -n "$file" -a -x "$file"; then
+ echo $file
+ return 0
+ fi
+ done
+
+ echo ""
+ return 1
+}
+
+# Try to detect the supported binaries if the user didn't
+# override that by pushing the environment variable
+if test x$LIBTOOLIZE = x; then
+ LIBTOOLIZE=`locate_binary glibtoolize libtoolize-1.5 libtoolize`
+ if test x$LIBTOOLIZE = x; then
+ die "Did not find a supported libtoolize"
+ fi
+fi
+
+if test x$ACLOCAL = x; then
+ ACLOCAL=`locate_binary aclocal-1.11 aclocal-1.10 aclocal-1.9 aclocal19 aclocal`
+ if test x$ACLOCAL = x; then
+ die "Did not find a supported aclocal"
+ fi
+fi
+
+if test x$AUTOMAKE = x; then
+ AUTOMAKE=`locate_binary automake-1.11 automake-1.10 automake-1.9 automake19 automake`
+ if test x$AUTOMAKE = x; then
+ die "Did not find a supported automake"
+ fi
+fi
+
+if test x$AUTOCONF = x; then
+ AUTOCONF=`locate_binary autoconf-2.59 autoconf259 autoconf`
+ if test x$AUTOCONF = x; then
+ die "Did not find a supported autoconf"
+ fi
+fi
+
+if test x$AUTOHEADER = x; then
+ AUTOHEADER=`locate_binary autoheader-2.59 autoheader259 autoheader`
+ if test x$AUTOHEADER = x; then
+ die "Did not find a supported autoheader"
+ fi
+fi
+
+run $LIBTOOLIZE $LIBTOOLIZE_FLAGS || die "Can't execute libtoolize"
+run $ACLOCAL $ACLOCAL_FLAGS || die "Can't execute aclocal"
+run $AUTOHEADER || die "Can't execute autoheader"
+run $AUTOMAKE $AUTOMAKE_FLAGS || die "Can't execute automake"
+run $AUTOCONF || die "Can't execute autoconf"
View
@@ -0,0 +1,202 @@
+#include <assert.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <strings.h>
+#include <string.h>
+#include <ctype.h>
+#include <stdbool.h>
+
+#include "config_parser.h"
+#include "util.h"
+
+static int read_config_file(const char *fname, struct config_item items[],
+ FILE *error);
+
+/**
+ * Copy a string and trim of leading and trailing white space characters.
+ * Allow the user to escape out the stop character by putting a backslash before
+ * the character.
+ * @param dest where to store the result
+ * @param size size of the result buffer
+ * @param src where to copy data from
+ * @param end the last character parsed is returned here
+ * @param stop the character to stop copying.
+ * @return 0 if success, -1 otherwise
+ */
+static int trim_copy(char *dest, size_t size, const char *src,
+ const char **end, char stop) {
+ while (isspace(*src)) {
+ ++src;
+ }
+ char *space = NULL;
+ size_t n = 0;
+ bool escape = false;
+ int ret = 0;
+
+ do {
+ if ((*dest = *src) == '\\') {
+ escape = true;
+ } else {
+ if (!escape) {
+ if (space == NULL && isspace(*src)) {
+ space = dest;
+ }
+ }
+ escape = false;
+ ++dest;
+ }
+ ++n;
+ ++src;
+
+ } while (!(n == size || ((*src == stop) && !escape) || *src == '\0'));
+ *end = src;
+
+ if (space) {
+ *space = '\0';
+ } else {
+ if (n == size) {
+ --dest;
+ ret = -1;
+ }
+ *dest = '\0';
+ }
+
+ return ret;
+}
+
+
+int parse_config(const char *str, struct config_item *items, FILE *error) {
+ int ret = 0;
+ const char *ptr = str;
+
+ while (*ptr != '\0') {
+ while (isspace(*ptr)) {
+ ++ptr;
+ }
+ if (*ptr == '\0') {
+ /* end of parameters */
+ return 0;
+ }
+
+ const char *end;
+ char key[80];
+ if (trim_copy(key, sizeof(key), ptr, &end, '=') == -1) {
+ fprintf(error, "ERROR: Invalid key, starting at: <%s>\n", ptr);
+ return -1;
+ }
+
+ ptr = end + 1;
+ char value[80];
+ if (trim_copy(value, sizeof(value), ptr, &end, ';') == -1) {
+ fprintf(error, "ERROR: Invalid value, starting at: <%s>\n", ptr);
+ return -1;
+ }
+ if (*end == ';') {
+ ptr = end + 1;
+ } else {
+ ptr = end;
+ }
+
+ int ii = 0;
+ while (items[ii].key != NULL) {
+ if (strcmp(key, items[ii].key) == 0) {
+ if (items[ii].found) {
+ fprintf(error, "WARNING: Found duplicate entry for \"%s\"\n",
+ items[ii].key);
+ }
+
+ switch (items[ii].datatype) {
+ case DT_SIZE:
+ {
+ uint64_t val;
+ if (safe_strtoull(value, &val)) {
+ *items[ii].value.dt_size = (size_t)val;
+ items[ii].found = true;
+ } else {
+ ret = -1;
+ }
+ }
+ break;
+ case DT_FLOAT:
+ {
+ float val;
+ if (safe_strtof(value, &val)) {
+ *items[ii].value.dt_float = val;
+ items[ii].found = true;
+ } else {
+ ret = -1;
+ }
+ }
+ break;
+ case DT_STRING:
+ *items[ii].value.dt_string = strdup(value);
+ items[ii].found = true;
+ break;
+ case DT_BOOL:
+ if (strcasecmp(value, "true") == 0 || strcasecmp(value, "on") == 0) {
+ *items[ii].value.dt_bool = true;
+ items[ii].found = true;
+ } else if (strcasecmp(value, "false") == 0 || strcasecmp(value, "off") == 0) {
+ *items[ii].value.dt_bool = false;
+ items[ii].found = true;
+ } else {
+ ret = -1;
+ }
+ break;
+ case DT_CONFIGFILE:
+ {
+ int r = read_config_file(value, items, error);
+ if (r != 0) {
+ ret = r;
+ }
+ }
+ break;
+ default:
+ /* You need to fix your code!!! */
+ abort();
+ }
+ if (ret == -1) {
+ fprintf(error, "Invalid entry, Key: <%s> Value: <%s>\n",
+ key, value);
+ return ret;
+ }
+ break;
+ }
+ ++ii;
+ }
+
+ if (items[ii].key == NULL) {
+ fprintf(error, "Unsupported key: <%s>\n", key);
+ ret = 1;
+ }
+ }
+ return ret;
+}
+
+static int read_config_file(const char *fname, struct config_item items[],
+ FILE *error) {
+ FILE *fp = fopen(fname, "r");
+ if (fp == NULL) {
+ (void)fprintf(error, "Failed to open file: %s\n", fname);
+ return -1;
+ }
+
+ int ret = 0;
+ char line[1024];
+ while (fgets(line, sizeof(line), fp) != NULL && ret != -1L) {
+ if (line[0] == '#') {
+ /* Ignore comment line */
+ continue;
+ }
+
+ int r = parse_config(line, items, error);
+ if (r != 0) {
+ ret = r;
+ }
+ }
+
+ (void)fclose(fp);
+
+ return ret;
+}
View
@@ -0,0 +1,62 @@
+#ifndef CONFIG_PARSER_H
+#define CONFIG_PARSER_H
+
+#include <stdint.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * The supported datatypes the config file parser can handle
+ */
+enum config_datatype {
+ DT_SIZE,
+ DT_FLOAT,
+ DT_BOOL,
+ DT_STRING,
+ DT_CONFIGFILE
+};
+
+/**
+ * I don't like casting, so let's create a union to keep all the values in
+ */
+union config_value {
+ size_t *dt_size;
+ float *dt_float;
+ bool *dt_bool;
+ char **dt_string;
+};
+
+/**
+ * An entry for a single item in the config file.
+ */
+struct config_item {
+ /** The name of the key */
+ const char* key;
+ /** The datatype for the value */
+ enum config_datatype datatype;
+ /** Where to store the value from the config file */
+ union config_value value;
+ /** If the item was found in the config file or not */
+ bool found;
+};
+
+/**
+ * Parse the configuration argument and populate the values into the
+ * config items.
+ *
+ * @param str the encoded configuration string
+ * @param items the config items to look for
+ * @param error stream to write error messages to
+ * @return 0 if config successfully parsed
+ * 1 if config successfully parsed, but unknown tokens found
+ * -1 if illegal values was found in the config
+ */
+int parse_config(const char *str, struct config_item items[], FILE *error);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif

0 comments on commit a035409

Please sign in to comment.