+
+#include <yaz/odr.h>
+#include <yaz/proto.h>
+#include <yaz/log.h>
+#include <yaz/data1.h>
+
+static Z_Variant *read_variant(int argc, char **argv, NMEM nmem,
+ const char *file, int lineno)
+{
+ Z_Variant *r = (Z_Variant *)nmem_malloc(nmem, sizeof(*r));
+ oident var1;
+ int i;
+ int oid[OID_SIZE];
+
+ var1.proto = PROTO_Z3950;
+ var1.oclass = CLASS_VARSET;
+ var1.value = VAL_VAR1;
+ r->globalVariantSetId = odr_oiddup_nmem(nmem, oid_ent_to_oid(&var1, oid));
+
+ if (argc)
+ r->triples = (Z_Triple **)nmem_malloc(nmem, sizeof(Z_Triple*) * argc);
+ else
+ r->triples = 0;
+ r->num_triples = argc;
+ for (i = 0; i < argc; i++)
+ {
+ int zclass, type;
+ char value[512];
+ Z_Triple *t;
+
+ if (sscanf(argv[i], "(%d,%d,%511[^)])", &zclass, &type, value) < 3)
+ {
+ yaz_log(LOG_WARN, "%s:%d: Syntax error in variant component '%s'",
+ file, lineno, argv[i]);
+ return 0;
+ }
+ t = r->triples[i] = (Z_Triple *)nmem_malloc(nmem, sizeof(Z_Triple));
+ t->variantSetId = 0;
+ t->zclass = (int *)nmem_malloc(nmem, sizeof(int));
+ *t->zclass = zclass;
+ t->type = (int *)nmem_malloc(nmem, sizeof(int));
+ *t->type = type;
+ /*
+ * This is wrong.. we gotta look up the correct type for the
+ * variant, I guess... damn this stuff.
+ */
+ if (*value == '@')
+ {
+ t->which = Z_Triple_null;
+ t->value.null = odr_nullval();
+ }
+ else if (d1_isdigit(*value))
+ {
+ t->which = Z_Triple_integer;
+ t->value.integer = (int *)
+ nmem_malloc(nmem, sizeof(*t->value.integer));
+ *t->value.integer = atoi(value);
+ }
+ else
+ {
+ t->which = Z_Triple_internationalString;
+ t->value.internationalString = (char *)
+ nmem_malloc(nmem, strlen(value)+1);
+ strcpy(t->value.internationalString, value);
+ }
+ }
+ return r;
+}
+
+static Z_Occurrences *read_occurrences(char *occ, NMEM nmem,
+ const char *file, int lineno)
+{
+ Z_Occurrences *op = (Z_Occurrences *)nmem_malloc(nmem, sizeof(*op));
+ char *p;
+
+ if (!occ)
+ {
+ op->which = Z_Occurrences_values;
+ op->u.values = (Z_OccurValues *)
+ nmem_malloc(nmem, sizeof(Z_OccurValues));
+ op->u.values->start = (int *)nmem_malloc(nmem, sizeof(int));
+ *op->u.values->start = 1;
+ op->u.values->howMany = 0;
+ }
+ else if (!strcmp(occ, "all"))
+ {
+ op->which = Z_Occurrences_all;
+ op->u.all = odr_nullval();
+ }
+ else if (!strcmp(occ, "last"))
+ {
+ op->which = Z_Occurrences_last;
+ op->u.all = odr_nullval();
+ }
+ else
+ {
+ Z_OccurValues *ov = (Z_OccurValues *)nmem_malloc(nmem, sizeof(*ov));
+
+ if (!d1_isdigit(*occ))
+ {
+ yaz_log(LOG_WARN, "%s:%d: Bad occurrences-spec %s",
+ file, lineno, occ);
+ return 0;
+ }
+ op->which = Z_Occurrences_values;
+ op->u.values = ov;
+ ov->start = (int *)nmem_malloc(nmem, sizeof(*ov->start));
+ *ov->start = atoi(occ);
+ if ((p = strchr(occ, '+')))
+ {
+ ov->howMany = (int *)nmem_malloc(nmem, sizeof(*ov->howMany));
+ *ov->howMany = atoi(p + 1);
+ }
+ else
+ ov->howMany = 0;
+ }
+ return op;
+}
+
+
+static Z_ETagUnit *read_tagunit(char *buf, NMEM nmem,
+ const char *file, int lineno)
+{
+ Z_ETagUnit *u = (Z_ETagUnit *)nmem_malloc(nmem, sizeof(*u));
+ int terms;
+ int type;
+ char value[512], occ[512];
+
+ if (*buf == '*')
+ {
+ u->which = Z_ETagUnit_wildPath;
+ u->u.wildPath = odr_nullval();
+ }
+ else if (*buf == '?')
+ {
+ u->which = Z_ETagUnit_wildThing;
+ if (buf[1] == ':')
+ u->u.wildThing = read_occurrences(buf+2, nmem, file, lineno);
+ else
+ u->u.wildThing = read_occurrences(0, nmem, file, lineno);
+ }
+ else if ((terms = sscanf(buf, "(%d,%511[^)]):%511[a-zA-Z0-9+]",
+ &type, value, occ)) >= 2)
+ {
+ int numval;
+ Z_SpecificTag *t;
+ char *valp = value;
+ int force_string = 0;
+
+ if (*valp == '\'')
+ {
+ valp++;
+ force_string = 1;
+ }
+ u->which = Z_ETagUnit_specificTag;
+ u->u.specificTag = t = (Z_SpecificTag *)nmem_malloc(nmem, sizeof(*t));
+ t->tagType = (int *)nmem_malloc(nmem, sizeof(*t->tagType));
+ *t->tagType = type;
+ t->tagValue = (Z_StringOrNumeric *)
+ nmem_malloc(nmem, sizeof(*t->tagValue));
+ if (!force_string && (numval = atoi(valp)))
+ {
+ t->tagValue->which = Z_StringOrNumeric_numeric;
+ t->tagValue->u.numeric = (int *)nmem_malloc(nmem, sizeof(int));
+ *t->tagValue->u.numeric = numval;
+ }
+ else
+ {
+ t->tagValue->which = Z_StringOrNumeric_string;
+ t->tagValue->u.string = (char *)nmem_malloc(nmem, strlen(valp)+1);
+ strcpy(t->tagValue->u.string, valp);
+ }
+ if (terms > 2) /* an occurrences-spec exists */
+ t->occurrences = read_occurrences(occ, nmem, file, lineno);
+ else
+ t->occurrences = 0;
+ }
+ return u;
+}