summaryrefslogtreecommitdiff
path: root/include/libHX
diff options
context:
space:
mode:
authorJörg Frings-Fürst <debian@jff-webhosting.net>2015-02-04 14:09:54 +0100
committerJörg Frings-Fürst <debian@jff-webhosting.net>2015-02-04 14:09:54 +0100
commitbd82d030011cd8b9655e5ded6b6df9343b42a6bd (patch)
treede82d886dfea0cb7dbb6e80436218a25cb211bc3 /include/libHX
Imported Upstream version 3.22upstream/3.22
Diffstat (limited to 'include/libHX')
-rw-r--r--include/libHX/ctype_helper.h86
-rw-r--r--include/libHX/defs.h230
-rw-r--r--include/libHX/deque.h86
-rw-r--r--include/libHX/init.h15
-rw-r--r--include/libHX/io.h46
-rw-r--r--include/libHX/libxml_helper.h113
-rw-r--r--include/libHX/list.h172
-rw-r--r--include/libHX/map.h140
-rw-r--r--include/libHX/misc.h113
-rw-r--r--include/libHX/option.h260
-rw-r--r--include/libHX/proc.h49
-rw-r--r--include/libHX/string.h116
-rw-r--r--include/libHX/wx_helper.hpp20
13 files changed, 1446 insertions, 0 deletions
diff --git a/include/libHX/ctype_helper.h b/include/libHX/ctype_helper.h
new file mode 100644
index 0000000..2ba0a7a
--- /dev/null
+++ b/include/libHX/ctype_helper.h
@@ -0,0 +1,86 @@
+#ifndef _LIBHX_CTYPE_H
+#define _LIBHX_CTYPE_H 1
+
+#ifdef __cplusplus
+# include <cctype>
+#else
+# include <ctype.h>
+# include <stdbool.h>
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/*
+ * ctype.h workarounds. The is*() functions takes an int, but people
+ * commonly pass in a char. Because char can technically be signed, the
+ * value would be sign-extended during promotion to the int type which the
+ * ctype family functions take.
+ *
+ * The HX_ ctype related functions therefore explicitly take an unsigned char,
+ * and thus knowingly make it impossible to pass in stdio's "EOF" (-1).
+ * [When was the last time you did a isalpha(EOF) anyway?]
+ *
+ * Because, again, this all works due to implicit type conversion, these
+ * wrappers look rather plain. Oh, also note we are returning the much
+ * more modern "bool".
+ *
+ * And not all ctype functions are provided - no need so far, and I do not
+ * want to clutter it before needing it.
+ */
+static __inline__ bool HX_isalnum(unsigned char c)
+{
+ return isalnum(c);
+}
+
+static __inline__ bool HX_isalpha(unsigned char c)
+{
+ return isalpha(c);
+}
+
+static __inline__ bool HX_isdigit(unsigned char c)
+{
+ return isdigit(c);
+}
+
+static __inline__ bool HX_islower(unsigned char c)
+{
+ return islower(c);
+}
+
+static __inline__ bool HX_isprint(unsigned char c)
+{
+ return isprint(c);
+}
+
+static __inline__ bool HX_isspace(unsigned char c)
+{
+ return isspace(c);
+}
+
+static __inline__ bool HX_isupper(unsigned char c)
+{
+ return isupper(c);
+}
+
+static __inline__ bool HX_isxdigit(unsigned char c)
+{
+ return isxdigit(c);
+}
+
+static __inline__ unsigned char HX_tolower(unsigned char c)
+{
+ return tolower(c);
+}
+
+static __inline__ unsigned char HX_toupper(unsigned char c)
+{
+ return toupper(c);
+}
+
+#ifdef __cplusplus
+} /* extern "C" */
+#endif
+
+#endif /* _LIBHX_CTYPE_H */
diff --git a/include/libHX/defs.h b/include/libHX/defs.h
new file mode 100644
index 0000000..bb03f40
--- /dev/null
+++ b/include/libHX/defs.h
@@ -0,0 +1,230 @@
+#ifndef _LIBHX_DEFS_H
+#define _LIBHX_DEFS_H 1
+
+#ifdef __cplusplus
+# define FIELD_SIZEOF(type, member) \
+ sizeof(static_cast<type *>(NULL)->member)
+# define HXsizeof_member(type, member) FIELD_SIZEOF(type, member)
+# define HXtypeof_member(type, member) \
+ __typeof__(static_cast<type *>(NULL)->member)
+# if defined(__GNUC__) && __GNUC__ >= 4 && !defined(offsetof)
+ /*
+ * This is here so most programs can skip inclusion
+ * of stddef.h just to get offsetof.
+ */
+# define offsetof(type, member) __builtin_offsetof(type, member)
+# endif
+# ifndef offsetof
+# define offsetof(type, member) \
+ reinterpret_cast<long>(&(static_cast<type *>(NULL)->member))
+# endif
+# ifndef containerof
+# define containerof(var, type, member) reinterpret_cast<type *>( \
+ reinterpret_cast<char *>(var) - offsetof(type, member))
+# endif
+
+template<typename new_type>
+static __inline__ new_type signed_cast(const char *expr)
+{
+ return reinterpret_cast<new_type>(expr);
+}
+
+template<typename new_type>
+static __inline__ new_type signed_cast(const signed char *expr)
+{
+ return reinterpret_cast<new_type>(expr);
+}
+
+template<typename new_type>
+static __inline__ new_type signed_cast(const unsigned char *expr)
+{
+ return reinterpret_cast<new_type>(expr);
+}
+
+template<typename new_type>
+static __inline__ new_type signed_cast(char *expr)
+{
+ return reinterpret_cast<new_type>(expr);
+}
+
+template<typename new_type>
+static __inline__ new_type signed_cast(signed char *expr)
+{
+ return reinterpret_cast<new_type>(expr);
+}
+
+template<typename new_type>
+static __inline__ new_type signed_cast(unsigned char *expr)
+{
+ return reinterpret_cast<new_type>(expr);
+}
+#else
+# define HXsizeof_member(type, member) sizeof(((type *)NULL)->member)
+# define HXtypeof_member(type, member) __typeof__(((type *)NULL)->member)
+ /* N.B. signed_cast<> does not exist in C++. */
+# define __signed_cast_compatible(a, b) \
+ __builtin_choose_expr( \
+ __builtin_types_compatible_p(b, const char *) || \
+ __builtin_types_compatible_p(b, const signed char *) || \
+ __builtin_types_compatible_p(b, const unsigned char *), \
+ /* if src has a const qualifier */ \
+ __builtin_types_compatible_p(a, const char *) || \
+ __builtin_types_compatible_p(a, const signed char *) || \
+ __builtin_types_compatible_p(a, const unsigned char *), \
+ /* and if it has none... */ \
+ __builtin_types_compatible_p(a, const char *) || \
+ __builtin_types_compatible_p(a, const signed char *) || \
+ __builtin_types_compatible_p(a, const unsigned char *) || \
+ __builtin_types_compatible_p(a, char *) || \
+ __builtin_types_compatible_p(a, signed char *) || \
+ __builtin_types_compatible_p(a, unsigned char *) \
+ )
+
+# if defined(__GNUC__) && !defined(__clang__) && !defined(signed_cast)
+# define signed_cast(type, expr) ({ \
+ BUILD_BUG_ON(!__signed_cast_compatible(__typeof__(type), __typeof__(expr))); \
+ (type)(expr); \
+ })
+# endif
+# if defined(__GNUC__) && !defined(__clang__) && !defined(static_cast)
+# define static_cast(type, expr) \
+ ((struct { type x; }){(expr)}.x)
+# endif
+# if defined(__GNUC__) && !defined(__clang__) && !defined(const_cast1)
+# define __const_cast_strip1(expr) \
+ __typeof__(*(union { int z; __typeof__(expr) x; }){0}.x)
+# define __const_cast_strip2(expr) \
+ __typeof__(**(union { int z; __typeof__(expr) x; }){0}.x)
+# define __const_cast_strip3(expr) \
+ __typeof__(***(union { int z; __typeof__(expr) x; }){0}.x)
+# define const_cast1(new_type, expr) ({ \
+ BUILD_BUG_ON(!__builtin_types_compatible_p(__const_cast_strip1(expr), __const_cast_strip1(new_type))); \
+ (new_type)(expr); \
+ })
+# define const_cast2(new_type, expr) ({ \
+ BUILD_BUG_ON(!__builtin_types_compatible_p(__const_cast_strip2(expr), __const_cast_strip2(new_type))); \
+ (new_type)(expr); \
+ })
+# define const_cast3(new_type, expr) ({ \
+ BUILD_BUG_ON(!__builtin_types_compatible_p(__const_cast_strip3(expr), __const_cast_strip3(new_type))); \
+ (new_type)(expr); \
+ })
+# endif
+# ifndef signed_cast
+# define signed_cast(type, expr) ((type)(expr))
+# endif
+# ifndef static_cast
+# define static_cast(type, expr) ((type)(expr))
+# endif
+# ifndef const_cast
+# define const_cast(type, expr) ((type)(expr))
+# endif
+# ifndef const_cast1
+# define const_cast1(type, expr) ((type)(expr))
+# define const_cast2(type, expr) ((type)(expr))
+# define const_cast3(type, expr) ((type)(expr))
+# endif
+# ifndef reinterpret_cast
+# define reinterpret_cast(type, expr) ((type)(expr))
+# endif
+# if defined(__GNUC__) && __GNUC__ >= 4 && !defined(offsetof)
+# define offsetof(type, member) __builtin_offsetof(type, member)
+# endif
+# ifndef offsetof
+# define offsetof(type, member) \
+ reinterpret_cast(long, &(static_cast(type *, NULL)->member))
+# endif
+# ifndef containerof
+# define containerof(var, type, member) reinterpret_cast(type *, \
+ reinterpret_cast(char *, var) - offsetof(type, member))
+# endif
+#endif
+
+#if defined(__GNUC__) && !defined(__cplusplus)
+ /*
+ * If typeof @a stays the same through a demotion to pointer,
+ * @a cannot be an array.
+ */
+# define __array_size_check(a) BUILD_BUG_ON_EXPR(\
+ __builtin_types_compatible_p(__typeof__(a), \
+ __typeof__(DEMOTE_TO_PTR(a))))
+#else
+# define __array_size_check(a) 0
+#endif
+#ifndef ARRAY_SIZE
+# define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)) + __array_size_check(x))
+#endif
+#ifndef BUILD_BUG_ON_EXPR
+# define BUILD_BUG_ON_EXPR(condition) (sizeof(char[1 - 2 * !!(condition)]) - 1)
+#endif
+#ifndef BUILD_BUG_ON
+# define BUILD_BUG_ON(condition) ((void)BUILD_BUG_ON_EXPR(condition))
+#endif
+#ifndef DEMOTE_TO_PTR
+ /*
+ * An alternative approach is also (p+0), but that does not ensure that
+ * @p is a pointer. Since functions "support" infinite dereferencing,
+ * "&*" also works on them.
+ */
+# define DEMOTE_TO_PTR(p) (&*(p))
+#endif
+#ifndef O_BINARY
+# define O_BINARY 0
+#endif
+#ifndef S_IRGRP
+ /* Can happen in mingw */
+# define S_IRGRP (S_IRUSR >> 3)
+#endif
+#ifndef S_IWGRP
+# define S_IWGRP (S_IWUSR >> 3)
+#endif
+#ifndef S_IXGRP
+# define S_IXGRP (S_IXUSR >> 3)
+#endif
+#ifndef S_IROTH
+# define S_IROTH (S_IRUSR >> 6)
+#endif
+#ifndef S_IWOTH
+# define S_IWOTH (S_IWUSR >> 6)
+#endif
+#ifndef S_IXOTH
+# define S_IXOTH (S_IXUSR >> 6)
+#endif
+#ifndef S_IRUGO
+# define S_IRUGO (S_IRUSR | S_IRGRP | S_IROTH)
+#endif
+#ifndef S_IWUGO
+# define S_IWUGO (S_IWUSR | S_IWGRP | S_IWOTH)
+#endif
+#ifndef S_IXUGO
+# define S_IXUGO (S_IXUSR | S_IXGRP | S_IXOTH)
+#endif
+#ifndef S_IRWXUGO
+# define S_IRWXUGO (S_IRUGO | S_IWUGO | S_IXUGO)
+#endif
+
+#define HXSIZEOF_Z16 sizeof("-65536")
+/* 2^32 and -2^31 have differing length */
+#define HXSIZEOF_Z32 sizeof("-4294967296")
+/* 2^64 and -2^63 have same length */
+#define HXSIZEOF_Z64 sizeof("18446744073709551616")
+
+#define __HX_STRINGIFY_EXPAND(s) #s
+#define HX_STRINGIFY(s) __HX_STRINGIFY_EXPAND(s)
+
+#ifndef container_of
+# define container_of(v, s, m) containerof((v), s, m)
+#endif
+
+#ifdef _WIN32
+ /*
+ * Sufficiently old versions of the VC runtime do not even support %ll.
+ */
+# define HX_LONGLONG_FMT "I64"
+# define HX_SIZET_FMT "I"
+#else
+# define HX_LONGLONG_FMT "ll"
+# define HX_SIZET_FMT "z"
+#endif
+
+#endif /* _LIBHX_DEFS_H */
diff --git a/include/libHX/deque.h b/include/libHX/deque.h
new file mode 100644
index 0000000..f43def9
--- /dev/null
+++ b/include/libHX/deque.h
@@ -0,0 +1,86 @@
+#ifndef _LIBHX_DEQUE_H
+#define _LIBHX_DEQUE_H 1
+
+#ifdef __cplusplus
+# include <cstdlib>
+#else
+# include <stdlib.h>
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+struct HXdeque_node {
+ struct HXdeque_node *next;
+ union {
+ void *ptr;
+ char *sptr;
+ };
+ struct HXdeque *parent;
+ struct HXdeque_node *prev;
+};
+
+struct HXdeque {
+ struct HXdeque_node *first;
+ void *ptr;
+ struct HXdeque_node *last;
+ unsigned int items;
+};
+
+extern struct HXdeque *HXdeque_init(void);
+extern struct HXdeque_node *HXdeque_push(struct HXdeque *, const void *);
+extern struct HXdeque_node *HXdeque_unshift(struct HXdeque *, const void *);
+extern void *HXdeque_pop(struct HXdeque *);
+extern void *HXdeque_shift(struct HXdeque *);
+extern void HXdeque_move(struct HXdeque_node *, struct HXdeque_node *);
+extern struct HXdeque_node *HXdeque_find(struct HXdeque *, const void *);
+extern void *HXdeque_get(struct HXdeque *, const void *);
+extern void *HXdeque_del(struct HXdeque_node *);
+extern void HXdeque_free(struct HXdeque *);
+extern void HXdeque_genocide2(struct HXdeque *, void (*)(void *));
+extern void **HXdeque_to_vec(const struct HXdeque *, unsigned int *);
+
+static __inline__ void HXdeque_genocide(struct HXdeque *dq)
+{
+ HXdeque_genocide2(dq, free);
+}
+
+#ifdef __cplusplus
+} /* extern "C" */
+
+extern "C++" {
+
+template<typename type> static __inline__ type HXdeque_pop(struct HXdeque *dq)
+{
+ return reinterpret_cast<type>(HXdeque_pop(dq));
+}
+
+template<typename type> static __inline__ type
+HXdeque_shift(struct HXdeque *dq)
+{
+ return reinterpret_cast<type>(HXdeque_shift(dq));
+}
+
+template<typename type> static __inline__ type
+HXdeque_get(struct HXdeque *dq, const void *ptr)
+{
+ return reinterpret_cast<type>(HXdeque_get(dq, ptr));
+}
+
+template<typename type> static __inline__ type
+HXdeque_del(struct HXdeque_node *nd)
+{
+ return reinterpret_cast<type>(HXdeque_del(nd));
+}
+
+template<typename type> static __inline__ type *
+HXdeque_to_vec(struct HXdeque *dq, unsigned int *n)
+{
+ return reinterpret_cast<type *>(HXdeque_to_vec(dq, n));
+}
+
+} /* extern "C++" */
+#endif
+
+#endif /* _LIBHX_DEQUE_H */
diff --git a/include/libHX/init.h b/include/libHX/init.h
new file mode 100644
index 0000000..c584209
--- /dev/null
+++ b/include/libHX/init.h
@@ -0,0 +1,15 @@
+#ifndef _LIBHX_INIT_H
+#define _LIBHX_INIT_H 1
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+extern int HX_init(void);
+extern void HX_exit(void);
+
+#ifdef __cplusplus
+} /* extern "C" */
+#endif
+
+#endif /* _LIBHX_INIT_H */
diff --git a/include/libHX/io.h b/include/libHX/io.h
new file mode 100644
index 0000000..c86af72
--- /dev/null
+++ b/include/libHX/io.h
@@ -0,0 +1,46 @@
+#ifndef _LIBHX_IO_H
+#define _LIBHX_IO_H 1
+
+#include <sys/types.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifndef __libhx_internal_hxmc_t_defined
+#define __libhx_internal_hxmc_t_defined 1
+typedef char hxmc_t;
+#endif
+
+enum {
+ HXF_UID = 1 << 0,
+ HXF_GID = 1 << 1,
+ HXF_KEEP = 1 << 2,
+ HX_REALPATH_NOFLAGS = 0,
+ HX_REALPATH_ABSOLUTE = 1 << 0,
+ HX_REALPATH_SELF = 1 << 1,
+ HX_REALPATH_PARENT = 1 << 2,
+ /* HX_REALPATH_SYMLINK = 1 << 3, removed in v3.13, thus blocked */
+ HX_REALPATH_DEFAULT = HX_REALPATH_SELF | HX_REALPATH_PARENT,
+};
+
+struct HXdir;
+
+extern struct HXdir *HXdir_open(const char *);
+extern const char *HXdir_read(struct HXdir *);
+extern void HXdir_close(struct HXdir *);
+extern int HX_copy_dir(const char *, const char *, unsigned int, ...);
+extern int HX_copy_file(const char *, const char *, unsigned int, ...);
+extern int HX_mkdir(const char *, unsigned int);
+extern int HX_readlink(hxmc_t **, const char *);
+extern int HX_realpath(hxmc_t **, const char *, unsigned int);
+extern int HX_rrmdir(const char *);
+
+extern ssize_t HXio_fullread(int, void *, size_t);
+extern ssize_t HXio_fullwrite(int, const void *, size_t);
+
+#ifdef __cplusplus
+} /* extern "C" */
+#endif
+
+#endif /* _LIBHX_IO_H */
diff --git a/include/libHX/libxml_helper.h b/include/libHX/libxml_helper.h
new file mode 100644
index 0000000..599ede1
--- /dev/null
+++ b/include/libHX/libxml_helper.h
@@ -0,0 +1,113 @@
+#ifndef _LIBHX_LIBXML_HELPER_H
+#define _LIBHX_LIBXML_HELPER_H 1
+
+#ifdef __cplusplus
+# include <cstring>
+#else
+# include <string.h>
+#endif
+#include <libHX/defs.h>
+#include <libxml/parser.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+static __inline__ int xml_strcmp(const xmlChar *a, const char *b)
+{
+#ifdef __cplusplus
+ return strcmp(signed_cast<const char *>(a), b);
+#else
+ return strcmp(signed_cast(const char *, a), b);
+#endif
+}
+
+static __inline__ int xml_strcasecmp(const xmlChar *a, const char *b)
+{
+#ifdef __cplusplus
+ return strcasecmp(signed_cast<const char *>(a), b);
+#else
+ return strcasecmp(signed_cast(const char *, a), b);
+#endif
+}
+
+static __inline__ char *xml_getprop(xmlNode *node, const char *attr)
+{
+#ifdef __cplusplus
+ return signed_cast<char *>(xmlGetProp(node,
+ signed_cast<const xmlChar *>(attr)));
+#else
+ return signed_cast(char *, xmlGetProp(node,
+ signed_cast(const xmlChar *, attr)));
+#endif
+}
+
+/**
+ * xmlGetNsProp takes, as 3rd argument, a full namespace string.
+ * That is unwieldy.
+ */
+static __inline__ char *xml_getnsprop(xmlNode *node, const char *nsprefix,
+ const char *key)
+{
+ const struct _xmlAttr *attr = NULL;
+ for (attr = node->properties; attr != NULL; attr = attr->next)
+ if (attr->ns != NULL && attr->ns->prefix != NULL &&
+ xml_strcmp(attr->ns->prefix, nsprefix) == 0)
+ break;
+ if (attr == NULL)
+ return NULL;
+#ifdef __cplusplus
+ return signed_cast<char *>(xmlGetNsProp(node,
+ signed_cast<const xmlChar *>(key), attr->ns->href));
+#else
+ return signed_cast(char *, xmlGetNsProp(node,
+ signed_cast(const xmlChar *, key), attr->ns->href));
+#endif
+}
+
+static __inline__ xmlAttr *
+xml_newprop(xmlNode *node, const char *name, const char *value)
+{
+#ifdef __cplusplus
+ return xmlNewProp(node, signed_cast<const xmlChar *>(name),
+ signed_cast<const xmlChar *>(value));
+#else
+ return xmlNewProp(node, signed_cast(const xmlChar *, name),
+ signed_cast(const xmlChar *, value));
+#endif
+}
+
+/**
+ * @ptr: parent node
+ * @name: name of new node
+ * @value: string, or %NULL
+ */
+static __inline__ xmlNode *
+xml_newnode(xmlNode *ptr, const char *name, const char *value)
+{
+#ifdef __cplusplus
+ return xmlNewTextChild(ptr, NULL, signed_cast<const xmlChar *>(name),
+ signed_cast<const xmlChar *>(value));
+#else
+ return xmlNewTextChild(ptr, NULL, signed_cast(const xmlChar *, name),
+ signed_cast(const xmlChar *, value));
+#endif
+}
+
+static __inline__ xmlAttr *
+xml_setprop(xmlNode *node, const char *name, const char *value)
+{
+#ifdef __cplusplus
+ return xmlSetProp(node, signed_cast<const xmlChar *>(name),
+ signed_cast<const xmlChar *>(value));
+#else
+ return xmlSetProp(node, signed_cast(const xmlChar *, name),
+ signed_cast(const xmlChar *, value));
+#endif
+}
+
+#ifdef __cplusplus
+} /* extern "C" */
+#endif
+
+#endif /* _LIBHX_LIBXML_HELPER_H */
diff --git a/include/libHX/list.h b/include/libHX/list.h
new file mode 100644
index 0000000..a18b17e
--- /dev/null
+++ b/include/libHX/list.h
@@ -0,0 +1,172 @@
+#ifndef _LIBHX_LIST_H
+#define _LIBHX_LIST_H 1
+
+#ifdef __cplusplus
+# include <cstddef>
+#else
+# include <stdbool.h>
+# include <stddef.h>
+#endif
+#include <libHX/defs.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#define HXlist_entry(ptr, type, member) containerof((ptr), type, member)
+
+struct HXlist_head {
+ struct HXlist_head *next, *prev;
+};
+
+#define HXLIST_HEAD_INIT(name) {&(name), &(name)}
+#define HXLIST_HEAD(name) \
+ struct HXlist_head name = HXLIST_HEAD_INIT(name)
+
+static __inline__ void HXlist_init(struct HXlist_head *list)
+{
+ list->next = list->prev = list;
+}
+
+static __inline__ void __HXlist_add(struct HXlist_head *nu,
+ struct HXlist_head *prev, struct HXlist_head *next)
+{
+ nu->next = next;
+ nu->prev = prev;
+ next->prev = nu;
+ prev->next = nu;
+}
+
+static __inline__ void
+HXlist_add(struct HXlist_head *head, struct HXlist_head *entry)
+{
+ __HXlist_add(entry, head, head->next);
+}
+
+static __inline__ void
+HXlist_add_tail(struct HXlist_head *head, struct HXlist_head *entry)
+{
+ __HXlist_add(entry, head->prev, head);
+}
+
+static __inline__ void HXlist_del(struct HXlist_head *entry)
+{
+ entry->prev->next = entry->next;
+ entry->next->prev = entry->prev;
+ entry->next = NULL;
+ entry->prev = NULL;
+}
+
+static __inline__ bool HXlist_empty(const struct HXlist_head *head)
+{
+ return head->next == head;
+}
+
+#define HXlist_for_each(pos, head) \
+ for ((pos) = (head)->next; (pos) != (void *)(head); \
+ (pos) = (pos)->next)
+
+#define HXlist_for_each_rev(pos, head) \
+ for ((pos) = (head)->prev; (pos) != (void *)(head); \
+ (pos) = (pos)->prev)
+
+#define HXlist_for_each_safe(pos, n, head) \
+ for ((pos) = (head)->next, (n) = (pos)->next; (pos) != (void *)(head); \
+ (pos) = (n), (n) = (pos)->next)
+
+#define HXlist_for_each_rev_safe(pos, n, head) \
+ for ((pos) = (head)->prev, (n) = (pos)->prev; (pos) != (void *)(head); \
+ (pos) = (n), (n) = (pos)->prev)
+
+#define HXlist_for_each_entry(pos, head, member) \
+ for ((pos) = HXlist_entry((head)->next, __typeof__(*(pos)), member); \
+ &(pos)->member != (void *)(head); \
+ (pos) = HXlist_entry((pos)->member.next, __typeof__(*(pos)), member))
+
+#define HXlist_for_each_entry_rev(pos, head, member) \
+ for ((pos) = HXlist_entry((head)->prev, __typeof__(*(pos)), member); \
+ &(pos)->member != (void *)(head); \
+ (pos) = HXlist_entry((pos)->member.prev, __typeof__(*(pos)), member))
+
+#define HXlist_for_each_entry_safe(pos, n, head, member) \
+ for ((pos) = HXlist_entry((head)->next, __typeof__(*(pos)), member), \
+ (n) = HXlist_entry((pos)->member.next, __typeof__(*(pos)), member); \
+ &(pos)->member != (void *)(head); \
+ (pos) = (n), (n) = HXlist_entry((n)->member.next, __typeof__(*(n)), \
+ member))
+
+struct HXclist_head {
+ union {
+ struct HXlist_head list;
+ struct {
+ struct HXlist_head *next, *prev;
+ };
+ };
+ unsigned int items;
+};
+
+#define HXCLIST_HEAD_INIT(name) {{{&(name).list, &(name).list}}, 0}
+#define HXCLIST_HEAD(name) \
+ struct HXclist_head name = HXCLIST_HEAD_INIT(name)
+
+static __inline__ void HXclist_init(struct HXclist_head *head)
+{
+ head->list.next = head->list.prev = &head->list;
+ head->items = 0;
+}
+
+static __inline__ void
+HXclist_del(struct HXclist_head *head, struct HXlist_head *node)
+{
+ --head->items;
+ HXlist_del(node);
+}
+
+static __inline__ void
+HXclist_unshift(struct HXclist_head *head, struct HXlist_head *nu)
+{
+ ++head->items;
+ __HXlist_add(nu, &head->list, head->list.next);
+}
+
+static __inline__ void
+HXclist_push(struct HXclist_head *head, struct HXlist_head *nu)
+{
+ ++head->items;
+ __HXlist_add(nu, head->list.prev, &head->list);
+}
+
+static __inline__ struct HXlist_head *__HXclist_pop(struct HXclist_head *head)
+{
+ struct HXlist_head *p;
+ if ((const void *)head == head->list.next)
+ return NULL;
+ p = head->list.prev;
+ HXlist_del(p);
+ --head->items;
+ return p;
+}
+
+#define HXclist_pop(head, type, member) \
+ HXlist_entry(__HXclist_pop(head), type, member)
+
+static __inline__ struct HXlist_head *
+__HXclist_shift(struct HXclist_head *head)
+{
+ struct HXlist_head *p;
+ if ((const void *)head == head->list.next)
+ return NULL;
+ p = head->list.next;
+ HXlist_del(p);
+ --head->items;
+ return p;
+}
+
+#define HXclist_shift(head, type, member) \
+ HXlist_entry(__HXclist_shift(head), type, member)
+
+#ifdef __cplusplus
+} /* extern "C" */
+#endif
+
+#endif /* _LIBHX_LIST_H */
diff --git a/include/libHX/map.h b/include/libHX/map.h
new file mode 100644
index 0000000..ecbf703
--- /dev/null
+++ b/include/libHX/map.h
@@ -0,0 +1,140 @@
+#ifndef _LIBHX_MAP_H
+#define _LIBHX_MAP_H 1
+
+#ifdef __cplusplus
+# include <cstddef>
+#else
+# include <stdbool.h>
+# include <stddef.h>
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * Abstract:
+ * %HXMAPT_DEFAULT: whatever is fast and maps k-v, without preference
+ * %HXMAPT_ORDERED: anything ordered, no further preference
+ *
+ * Specific:
+ * %HXMAPT_HASH: map based on hash
+ * %HXMAPT_RBTREE: map based on red-black binary tree
+ */
+enum HXmap_type {
+ HXMAPT_HASH = 1,
+ HXMAPT_RBTREE,
+
+ /* aliases - assignments may change */
+ HXMAPT_DEFAULT = HXMAPT_HASH,
+ HXMAPT_ORDERED = HXMAPT_RBTREE,
+};
+
+/**
+ * Flags changable at runtime:
+ * %HXMAP_NOREPLACE: Calling HXmap_add() for an already existing key will
+ * throw an error (no-overwrite semantics)
+ *
+ * Initialization-time flags only:
+ * %HXMAP_NONE: mnemonic for no flags
+ * %HXMAP_SINGULAR: Instead of an associative map, provide a set
+ * %HXMAP_SKEY: Key will be a C-style string (sets ops->k_*)
+ * %HXMAP_CKEY: Make a copy of the key on HXmap_add
+ * %HXMAP_SDATA: Data will be a C-style string (presets ops->d_*)
+ * %HXMAP_CDATA: Make a copy of the data on HXmap_add
+ */
+enum {
+ HXMAP_NONE = 0,
+ HXMAP_NOREPLACE = 1 << 0,
+ HXMAP_SINGULAR = 1 << 1,
+ HXMAP_SKEY = 1 << 2,
+ HXMAP_CKEY = 1 << 3,
+ HXMAP_SDATA = 1 << 4,
+ HXMAP_CDATA = 1 << 5,
+
+ HXMAP_SCKEY = HXMAP_SKEY | HXMAP_CKEY,
+ HXMAP_SCDATA = HXMAP_SDATA | HXMAP_CDATA,
+};
+
+/**
+ * Flags for the traverser
+ * %HXMAP_NOFLAGS: Mnemonic for no flags
+ * %HXMAP_DTRAV: Support deletion of elements while traversing
+ */
+enum {
+ HXMAP_NOFLAGS = 0,
+ HXMAP_DTRAV = 1 << 0,
+};
+
+struct HXmap_trav;
+
+/**
+ * @items: number of items in the map
+ * @flags: flags for this map
+ */
+struct HXmap {
+ unsigned int items, flags;
+};
+
+struct HXmap_ops {
+ /* k_compare: the size argument is needed for memcmp. */
+ int (*k_compare)(const void *, const void *, size_t);
+ void *(*k_clone)(const void *, size_t);
+ void (*k_free)(void *);
+ void *(*d_clone)(const void *, size_t);
+ void (*d_free)(void *);
+ unsigned long (*k_hash)(const void *, size_t);
+};
+
+struct HXmap_node {
+ union {
+ void *key;
+ const char *const skey;
+ };
+ union {
+ void *data;
+ char *sdata;
+ };
+};
+
+extern struct HXmap *HXmap_init(enum HXmap_type, unsigned int);
+extern struct HXmap *HXmap_init5(enum HXmap_type, unsigned int,
+ const struct HXmap_ops *, size_t, size_t);
+
+extern int HXmap_add(struct HXmap *, const void *, const void *);
+extern const struct HXmap_node *HXmap_find(const struct HXmap *, const void *);
+extern void *HXmap_get(const struct HXmap *, const void *);
+extern void *HXmap_del(struct HXmap *, const void *);
+extern struct HXmap_node *HXmap_keysvalues(const struct HXmap *);
+extern struct HXmap_trav *HXmap_travinit(const struct HXmap *, unsigned int);
+extern const struct HXmap_node *HXmap_traverse(struct HXmap_trav *);
+extern void HXmap_travfree(struct HXmap_trav *);
+extern void HXmap_qfe(const struct HXmap *,
+ bool (*)(const struct HXmap_node *, void *), void *);
+extern void HXmap_free(struct HXmap *);
+
+extern unsigned long HXhash_jlookup3(const void *, size_t);
+extern unsigned long HXhash_jlookup3s(const void *, size_t);
+extern unsigned long HXhash_djb2(const void *, size_t);
+
+#ifdef __cplusplus
+} /* extern "C" */
+
+extern "C++" {
+
+template<typename type> static __inline__ type
+HXmap_get(const struct HXmap *map, const void *key)
+{
+ return reinterpret_cast<type>(HXmap_get(map, key));
+}
+
+template<typename type> static __inline__ type
+HXmap_del(struct HXmap *map, const void *key)
+{
+ return reinterpret_cast<type>(HXmap_del(map, key));
+}
+
+}
+#endif
+
+#endif /* _LIBHX_MAP_H */
diff --git a/include/libHX/misc.h b/include/libHX/misc.h
new file mode 100644
index 0000000..3f68917
--- /dev/null
+++ b/include/libHX/misc.h
@@ -0,0 +1,113 @@
+#ifndef _LIBHX_MISC_H
+#define _LIBHX_MISC_H 1
+
+#ifndef __cplusplus
+# include <limits.h>
+# include <stdarg.h>
+# include <stdbool.h>
+# include <stdio.h>
+#else
+# include <climits>
+# include <cstdarg>
+# include <cstdio>
+#endif
+#include <libHX/defs.h>
+#include <libHX/io.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/*
+ * BITMAP.H
+ */
+#define __HXbitmap_bpq(type) \
+ (sizeof(type) * CHAR_BIT)
+#define HXbitmap_size(type, bits) \
+ (((bits) + __HXbitmap_bpq(type) - 1) / __HXbitmap_bpq(type))
+#define __HXbitmap_quant(map, bit) \
+ ((map)[(bit) / __HXbitmap_bpq(*(map))])
+#define HXbitmap_set(map, bit) \
+ ((void)(__HXbitmap_quant((map), (bit)) |= (1ULL << ((bit) % __HXbitmap_bpq(*(map))))))
+#define HXbitmap_clear(map, bit) \
+ ((void)(__HXbitmap_quant((map), (bit)) &= ~(1ULL << ((bit) % __HXbitmap_bpq(*(map))))))
+#define HXbitmap_test(map, bit) \
+ ((bool)(__HXbitmap_quant((map), (bit)) & (1ULL << ((bit) % __HXbitmap_bpq(*(map))))))
+
+#define HX_TIMESPEC_FMT "%ld.%09ld"
+#define HX_TIMEVAL_FMT "%ld.%06ld"
+#ifdef __cplusplus
+# define HX_TIMESPEC_EXP(p) static_cast<long>((p)->tv_sec), (p)->tv_nsec
+# define HX_TIMEVAL_EXP(p) static_cast<long>((p)->tv_sec), (p)->tv_usec
+#else
+# define HX_TIMESPEC_EXP(p) static_cast(long, (p)->tv_sec), (p)->tv_nsec
+# define HX_TIMEVAL_EXP(p) static_cast(long, (p)->tv_sec), (p)->tv_usec
+#endif
+
+struct stat;
+struct timespec;
+struct timeval;
+
+/*
+ * DL.C
+ */
+extern void *HX_dlopen(const char *);
+extern void *HX_dlsym(void *, const char *);
+extern void HX_dlclose(void *);
+extern const char *HX_dlerror(void);
+
+/*
+ * MISC.C
+ */
+extern int HX_ffs(unsigned long);
+extern int HX_fls(unsigned long);
+extern void HX_hexdump(FILE *, const void *, unsigned int);
+extern bool HX_timespec_isneg(const struct timespec *);
+extern struct timespec *HX_timespec_neg(struct timespec *,
+ const struct timespec *);
+extern struct timespec *HX_timespec_add(struct timespec *,
+ const struct timespec *, const struct timespec *);
+extern struct timespec *HX_timespec_sub(struct timespec *,
+ const struct timespec *, const struct timespec *);
+extern struct timespec *HX_timespec_mul(struct timespec *,
+ const struct timespec *, int);
+extern struct timespec *HX_timespec_mulf(struct timespec *,
+ const struct timespec *, double);
+extern struct timeval *HX_timeval_sub(struct timeval *,
+ const struct timeval *, const struct timeval *);
+extern long HX_time_compare(const struct stat *, const struct stat *, char);
+extern void HX_zvecfree(char **);
+
+/*
+ * RAND.C
+ */
+extern int HX_rand(void);
+extern unsigned int HX_irand(unsigned int, unsigned int);
+extern double HX_drand(double, double);
+
+/*
+ * INLINE FUNCTIONS
+ */
+static __inline__ unsigned int HX_zveclen(const char *const *args)
+{
+ unsigned int argk = 0;
+ while (*args++ != NULL)
+ ++argk;
+ return argk;
+}
+
+#ifdef __cplusplus
+} /* extern "C" */
+
+extern "C++" {
+
+template<typename type> static __inline__ type
+HX_dlsym(void *handle, const char *symbol)
+{
+ return reinterpret_cast<type>(HX_dlsym(handle, symbol));
+}
+
+} /* extern "C++" */
+#endif
+
+#endif /* _LIBHX_MISC_H */
diff --git a/include/libHX/option.h b/include/libHX/option.h
new file mode 100644
index 0000000..3b8187f
--- /dev/null
+++ b/include/libHX/option.h
@@ -0,0 +1,260 @@
+#ifndef _LIBHX_OPTION_H
+#define _LIBHX_OPTION_H 1
+
+#ifdef __cplusplus
+# include <cstddef>
+# include <cstdio>
+#else
+# include <stddef.h>
+# include <stdio.h>
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifndef __libhx_internal_hxmc_t_defined
+#define __libhx_internal_hxmc_t_defined 1
+typedef char hxmc_t;
+#endif
+
+struct HXoption;
+
+/*
+ * FORMAT.C
+ */
+extern struct HXformat_map *HXformat_init(void);
+extern void HXformat_free(struct HXformat_map *);
+extern int HXformat_add(struct HXformat_map *, const char *, const void *,
+ unsigned int);
+extern int HXformat_aprintf(const struct HXformat_map *,
+ hxmc_t **, const char *);
+extern int HXformat_sprintf(const struct HXformat_map *,
+ char *, size_t, const char *);
+extern int HXformat_fprintf(const struct HXformat_map *,
+ FILE *, const char *);
+
+/*
+ * OPT.C
+ */
+
+/**
+ * Available types for struct HXoption.type.
+ * %HXTYPE_NONE: [-o] (int *) No argument; counts presence.
+ * %HXTYPE_VAL: [-o] (int *) Set to value in .val.
+ * %HXTYPE_SVAL: [-o] (const char *) Set to value in .uptr.
+ * %HXTYPE_BOOL: [fo] (int *) Parse argument as boolean
+ * ("yes", "no", "true", "false", 0 or non-zero)
+ * %HXTYPE_BYTE: [fo] (unsigned char *) Take first char of argument
+ * %HXTYPE_UCHAR: [fo] (unsigned char *) An integer.
+ * %HXTYPE_CHAR: [fo] (char *) An integer.
+ * %HXTYPE_USHORT: [fo] (unsigned short *) An integer.
+ * %HXTYPE_SHORT: [fo] (short *) An integer.
+ * %HXTYPE_UINT: [fo] (unsigned int *) An integer.
+ * %HXTYPE_INT: [fo] (int *) An integer.
+ * %HXTYPE_ULONG: [fo] (unsigned long *) An integer.
+ * %HXTYPE_LONG: [fo] (long *) An integer.
+ * %HXTYPE_ULLONG: [fo] (unsigned long long *) An integer.
+ * %HXTYPE_LLONG: [fo] (long long *) An integer.
+ * %HXTYPE_FLOAT: [fo] (float *) Read a floating point number
+ * %HXTYPE_DOUBLE: [fo] (double *) Read a floating point number
+ * %HXTYPE_STRING: [fo] (char **) Any string.
+ * %HXTYPE_STRP: [f-] (const char *const *) A string.
+ * %HXTYPE_STRDQ: [-o] (struct HXdeque *) A string.
+ * %HXTYPE_UINT8: [-o] (uint8_t *) An integer.
+ * %HXTYPE_UINT16: [-o] (uint8_t *) An integer.
+ * %HXTYPE_UINT32: [-o] (uint8_t *) An integer.
+ * %HXTYPE_UINT64: [-o] (uint8_t *) An integer.
+ * %HXTYPE_INT8: [-o] (uint8_t *) An integer.
+ * %HXTYPE_INT16: [-o] (uint8_t *) An integer.
+ * %HXTYPE_INT32: [-o] (uint8_t *) An integer.
+ * %HXTYPE_INT64: [-o] (uint8_t *) An integer.
+ * %HXTYPE_MCSTR: [-o] (hxmc_t *) A string.
+ * %HXTYPE_XSNTMARK: [-o] Internal sentinal marker (used in HXOPT_TABLEEND)
+ * %HXTYPE_XHELP: [-o] Internal helper marker (used in HXOPT_AUTOHELP)
+ * %HXTYPE_SIZE_T: [-o] (size_t *) An integer.
+ *
+ * Type expected of struct HXoption.ptr is given in ().
+ * HX_getopt (o) and HXformat_* (f) support different sets, marked with [].
+ */
+enum HX_option_type {
+ HXTYPE_NONE = 0,
+ HXTYPE_VAL,
+ HXTYPE_SVAL,
+ HXTYPE_BOOL,
+ HXTYPE_BYTE,
+ HXTYPE_UCHAR, /* 5 */
+ HXTYPE_CHAR,
+ HXTYPE_USHORT,
+ HXTYPE_SHORT,
+ HXTYPE_UINT,
+ HXTYPE_INT, /* 10 */
+ HXTYPE_ULONG,
+ HXTYPE_LONG,
+ HXTYPE_ULLONG,
+ HXTYPE_LLONG,
+ HXTYPE_FLOAT, /* 15 */
+ HXTYPE_DOUBLE,
+ HXTYPE_STRING,
+ HXTYPE_STRP, /* (const char **) */
+ HXTYPE_STRDQ,
+ HXTYPE_UINT8, /* 20 */
+ HXTYPE_UINT16,
+ HXTYPE_UINT32,
+ HXTYPE_UINT64,
+ HXTYPE_INT8,
+ HXTYPE_INT16, /* 25 */
+ HXTYPE_INT32,
+ HXTYPE_INT64,
+ HXTYPE_MCSTR,
+ HXTYPE_XSNTMARK,
+ HXTYPE_XHELP, /* 30 */
+ HXTYPE_SIZE_T,
+};
+
+/**
+ * Extra flags to be OR'ed into struct HXoption.type.
+ * %HXOPT_OPTIONAL: argument to option is optional
+ * (it's bad taste to use this)
+ * %HXOPT_INC: increase variable pointed to by .ptr.
+ * (only applies to %HXTYPE_NONE)
+ * %HXOPT_DEC: increase variable pointed to by .ptr.
+ * (only applies to %HXTYPE_NONE)
+ * %HXOPT_NOT: negate input (*ptr), this is done before OR/AND
+ * %HXOPT_OR: OR *ptr by argument
+ * %HXOPT_AND: AND *ptr by argument
+ * %HXOPT_XOR: XOR *ptr by argument
+ */
+enum {
+ HXOPT_OPTIONAL = 1 << 6,
+ HXOPT_INC = 1 << 7,
+ HXOPT_DEC = 1 << 8,
+ HXOPT_NOT = 1 << 9,
+ HXOPT_OR = 1 << 10,
+ HXOPT_AND = 1 << 11,
+ HXOPT_XOR = 1 << 12,
+};
+
+/**
+ * Flags (4th arg) to HX_getopt.
+ * %HXOPT_PTHRU: pass-through unknown options to new argv
+ * %HXOPT_DESTROY_OLD: destroy old argv after parsing is successful
+ * %HXOPT_QUIET: do not output any warnings to stderr
+ * %HXOPT_HELPONERR: print out help when a parsing error occurs
+ * %HXOPT_USAGEONERR: print out short usage when a parsing error occurs
+ * %HXOPT_RQ_ORDER: require option order/POSIX mode:
+ * first non-option terminates option processing
+ */
+enum {
+ HXOPT_PTHRU = 1 << 0,
+ HXOPT_DESTROY_OLD = 1 << 1,
+ HXOPT_QUIET = 1 << 2,
+ HXOPT_HELPONERR = 1 << 3,
+ HXOPT_USAGEONERR = 1 << 4,
+ HXOPT_RQ_ORDER = 1 << 5,
+};
+
+/**
+ * (Positive-ranged) return values for HX_getopt.
+ * %HXOPT_ERR_SUCCESS: success
+ * %HXOPT_ERR_UNKN: unknown option was encountered
+ * %HXOPT_ERR_VOID: long option takes no value
+ * %HXOPT_ERR_MIS: option requires a value argument
+ */
+enum {
+ HXOPT_ERR_SUCCESS = 0,
+ HXOPT_ERR_UNKN,
+ HXOPT_ERR_VOID,
+ HXOPT_ERR_MIS,
+};
+
+/**
+ * Extra flags to be OR'ed into HXformat_add()'s 4th arg.
+ * %HXFORMAT_IMMED: do not dereference the 4th arg to get at the value
+ */
+enum {
+ HXFORMAT_IMMED = 1 << 13,
+};
+
+/**
+ * Flags for HX_shconfig_pv()
+ * %SHCONF_ONE: only read one configuration file
+ */
+enum {
+ SHCONF_ONE = 1 << 0,
+};
+
+/**
+ * Flags in struct HXoptcb.flags
+ * %HXOPTCB_BY_LONG: cb was called by invocation of @current->ln
+ * %HXOPTCB_BY_SHORT: cb was called by invocation of @current->sh
+ */
+enum {
+ HXOPTCB_BY_LONG = 1 << 0,
+ HXOPTCB_BY_SHORT = 1 << 1,
+};
+
+struct HXoptcb {
+ const struct HXoption *table, *current;
+ const char *data;
+ union {
+ double data_dbl;
+ long data_long;
+ };
+ unsigned int flags;
+};
+
+/**
+ * @ln: long option string (without "--"), or %NULL
+ * @sh: short option character, or '\0'
+ * @type: type of variable pointed to by .ptr
+ * @ptr: pointer to variable to set/update
+ * @uptr: freeform user-supplied pointer;
+ * in case of %HXTYPE_SVAL, this is the specific value to set
+ * @cb: callback function to invoke, or %NULL
+ * @val: specific value to set if type == HXTYPE_VAL
+ * @help: help string to display
+ * @htyp: type string to show in option's help
+ */
+struct HXoption {
+ const char *ln;
+ char sh;
+ unsigned int type;
+ void *ptr, *uptr;
+ void (*cb)(const struct HXoptcb *);
+ int val;
+ const char *help, *htyp;
+};
+
+extern int HX_getopt(const struct HXoption *, int *, const char ***,
+ unsigned int);
+extern void HX_getopt_help(const struct HXoptcb *, FILE *);
+extern void HX_getopt_help_cb(const struct HXoptcb *);
+extern void HX_getopt_usage(const struct HXoptcb *, FILE *);
+extern void HX_getopt_usage_cb(const struct HXoptcb *);
+extern int HX_shconfig(const char *, const struct HXoption *);
+extern struct HXmap *HX_shconfig_map(const char *);
+extern int HX_shconfig_pv(const char **, const char *,
+ const struct HXoption *, unsigned int);
+extern void HX_shconfig_free(const struct HXoption *);
+
+#ifdef __cplusplus
+} /* extern "C" */
+#endif
+
+#ifndef __cplusplus
+# define HXOPT_AUTOHELP \
+ {.ln = "help", .sh = '?', .type = HXTYPE_XHELP, \
+ .cb = HX_getopt_help_cb, .help = "Show this help message"}, \
+ {.ln = "usage", .type = HXTYPE_NONE, \
+ .cb = HX_getopt_usage_cb, \
+ .help = "Display brief usage message"}
+# define HXOPT_TABLEEND {.type = HXTYPE_XSNTMARK}
+#else
+# define HXOPT_AUTOHELP \
+ {NULL, '?', HXTYPE_XHELP, NULL, NULL, HX_getopt_help_cb, \
+ 0, "Show this help message"}
+# define HXOPT_TABLEEND {NULL, 0, HXTYPE_XSNTMARK}
+#endif
+
+#endif /* _LIBHX_OPTION_H */
diff --git a/include/libHX/proc.h b/include/libHX/proc.h
new file mode 100644
index 0000000..cb682ed
--- /dev/null
+++ b/include/libHX/proc.h
@@ -0,0 +1,49 @@
+#ifndef _LIBHX_PROC_H
+#define _LIBHX_PROC_H
+
+#ifndef __cplusplus
+# include <stdbool.h>
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+enum {
+ HXPROC_VERBOSE = 1 << 0,
+ HXPROC_EXECV = 1 << 1,
+ HXPROC_A0 = 1 << 2,
+ HXPROC_STDIN = 1 << 3,
+ HXPROC_STDOUT = 1 << 4,
+ HXPROC_STDERR = 1 << 5,
+ HXPROC_NULL_STDIN = 1 << 6,
+ HXPROC_NULL_STDOUT = 1 << 7,
+ HXPROC_NULL_STDERR = 1 << 8,
+};
+
+struct HXproc_ops {
+ void (*p_prefork)(void *);
+ void (*p_postfork)(void *);
+ void (*p_complete)(void *);
+};
+
+struct HXproc {
+ const struct HXproc_ops *p_ops;
+ void *p_data;
+ unsigned int p_flags;
+
+ int p_stdin, p_stdout, p_stderr;
+ int p_pid;
+ char p_status;
+ bool p_exited, p_terminated;
+};
+
+extern int HXproc_run_async(const char *const *, struct HXproc *);
+extern int HXproc_run_sync(const char *const *, unsigned int);
+extern int HXproc_wait(struct HXproc *);
+
+#ifdef __cplusplus
+} /* extern "C" */
+#endif
+
+#endif /* _LIBHX_PROC_H */
diff --git a/include/libHX/string.h b/include/libHX/string.h
new file mode 100644
index 0000000..f7146b5
--- /dev/null
+++ b/include/libHX/string.h
@@ -0,0 +1,116 @@
+#ifndef _LIBHX_STRING_H
+#define _LIBHX_STRING_H 1
+
+#ifdef __cplusplus
+# include <cstddef>
+# include <cstdio>
+# include <cstdlib>
+# include <cstring>
+#else
+# include <stddef.h>
+# include <stdio.h>
+# include <stdlib.h>
+# include <string.h>
+#endif
+#include <sys/types.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+enum {
+ HXQUOTE_SQUOTE = 1,
+ HXQUOTE_DQUOTE,
+ HXQUOTE_HTML,
+ HXQUOTE_LDAPFLT,
+ HXQUOTE_LDAPRDN,
+ HXQUOTE_BASE64,
+ HXQUOTE_URIENC,
+ HXQUOTE_SQLSQUOTE,
+ HXQUOTE_SQLBQUOTE,
+ _HXQUOTE_MAX,
+};
+
+#ifndef __libhx_internal_hxmc_t_defined
+#define __libhx_internal_hxmc_t_defined 1
+typedef char hxmc_t;
+#endif
+
+/*
+ * MC.C
+ */
+extern hxmc_t *HXmc_strinit(const char *);
+extern hxmc_t *HXmc_meminit(const void *, size_t);
+extern hxmc_t *HXmc_strcpy(hxmc_t **, const char *);
+extern hxmc_t *HXmc_memcpy(hxmc_t **, const void *, size_t);
+extern size_t HXmc_length(const hxmc_t *);
+extern hxmc_t *HXmc_setlen(hxmc_t **, size_t);
+extern hxmc_t *HXmc_trunc(hxmc_t **, size_t);
+extern hxmc_t *HXmc_strcat(hxmc_t **, const char *);
+extern hxmc_t *HXmc_memcat(hxmc_t **, const void *, size_t);
+extern hxmc_t *HXmc_strpcat(hxmc_t **, const char *);
+extern hxmc_t *HXmc_mempcat(hxmc_t **, const void *, size_t);
+extern hxmc_t *HXmc_strins(hxmc_t **, size_t, const char *);
+extern hxmc_t *HXmc_memins(hxmc_t **, size_t, const void *, size_t);
+extern hxmc_t *HXmc_memdel(hxmc_t *, size_t, size_t);
+extern void HXmc_free(hxmc_t *);
+extern void HXmc_zvecfree(hxmc_t **);
+
+/*
+ * STRING.C
+ */
+extern char *HX_basename(const char *);
+extern char *HX_basename_exact(const char *);
+extern char *HX_chomp(char *);
+extern char *HX_dirname(const char *);
+extern hxmc_t *HX_getl(hxmc_t **, FILE *);
+extern void *HX_memmem(const void *, size_t, const void *, size_t);
+extern char **HX_split(const char *, const char *, int *, int);
+extern char **HX_split4(char *, const char *, int *, int);
+extern int HX_split5(char *, const char *, int, char **);
+extern char *HX_strbchr(const char *, const char *, char);
+extern char *HX_strchr2(const char *, const char *);
+extern char *HX_strclone(char **, const char *);
+extern char *HX_strdup(const char *);
+extern char *HX_strlcat(char *, const char *, size_t);
+extern char *HX_strlcpy(char *, const char *, size_t);
+extern char *HX_strlncat(char *, const char *, size_t, size_t);
+extern char *HX_strlower(char *);
+extern size_t HX_strltrim(char *);
+extern char *HX_stpltrim(const char *);
+extern char *HX_strmid(const char *, long, long);
+extern char *HX_strndup(const char *, size_t);
+extern size_t HX_strnlen(const char *, size_t);
+extern char *HX_strquote(const char *, unsigned int, char **);
+extern size_t HX_strrcspn(const char *, const char *);
+extern char *HX_strrev(char *);
+extern size_t HX_strrtrim(char *);
+extern char *HX_strsep(char **, const char *);
+extern char *HX_strsep2(char **, const char *);
+extern char *HX_strupper(char *);
+
+static __inline__ void *HX_memdup(const void *buf, size_t len)
+{
+ void *ret;
+ if ((ret = malloc(len)) == NULL)
+ return NULL;
+ return memcpy(ret, buf, len);
+}
+
+#ifdef __cplusplus
+} /* extern "C" */
+#endif
+
+#ifdef __cplusplus
+extern "C++" {
+
+template<typename type> static __inline__ type
+HX_memdup(const void *data, size_t n)
+{
+ return reinterpret_cast<type>(HX_memdup(data, n));
+}
+
+} /* extern "C++" */
+#endif
+
+#endif /* _LIBHX_STRING_H */
diff --git a/include/libHX/wx_helper.hpp b/include/libHX/wx_helper.hpp
new file mode 100644
index 0000000..07d7802
--- /dev/null
+++ b/include/libHX/wx_helper.hpp
@@ -0,0 +1,20 @@
+#ifndef _LIBHX_WXHELPER_HPP
+#define _LIBHX_WXHELPER_HPP 1
+
+/* Convert from UTF-8 to wxString; only valid within the scope of the result */
+#define wxfu8(s) wxString((s), wxConvUTF8)
+
+/* Convert from UTF-8 to wxString for varargs/wxPrintf */
+#define wxfv8(s) (wxfu8(s).c_str())
+
+/* Convert from wxString to UTF-8; limited validity */
+#define wxtu8(s) static_cast<const char *>((s).ToUTF8())
+
+/* Common dialog flags */
+#define wxCDF (wxDEFAULT_FRAME_STYLE | wxFRAME_NO_TASKBAR)
+#define wxACV wxALIGN_CENTER_VERTICAL
+#define wxDPOS wxDefaultPosition
+#define wxDSIZE wxDefaultSize
+#define wxDSPAN wxDefaultSpan
+
+#endif /* _LIBHX_WXHELPER_HPP */