union value {
const char *string;
long integer;
+ bool boolean;
};
enum ptype {
PTYPE_STRING,
PTYPE_INTEGER,
+ PTYPE_BOOLEAN,
};
struct ptype_class {
#define ARG_STRING(A) (A.v.string)
#define ARG_INTEGER(A) (A.v.integer)
+#define ARG_BOOLEAN(A) (A.v.boolean)
struct arg {
union value v;
void (*free)(union value value);
/* Do nothing */
}
+static char *boolean_sprint(const union value *value)
+{
+ return xstrdup(value->boolean? "true": "false");
+}
+
+static union value boolean_read(const char *arg, const union value *defv)
+{
+ union value r;
+
+ if (!arg)
+ return *defv;
+
+ if (strcasecmp(arg, "true") == 0
+ || strcmp(arg, "1") == 0
+ || strcasecmp(arg, "yes") == 0
+ || strcasecmp(arg, "y") == 0)
+ r.boolean = true;
+ else
+ r.boolean = false;
+ return r;
+}
+
+static void boolean_free(union value value _U_)
+{
+ /* Do nothing */
+}
struct ptype_class ptype_classes [] = {
[PTYPE_STRING] = {
.read = integer_read,
.free = integer_free,
},
+ [PTYPE_BOOLEAN] = {
+ .name = "boolean",
+ .sprint = boolean_sprint,
+ .read = boolean_read,
+ .free = boolean_free,
+ },
};
static struct arg decode_arg(const char *pname,