static int invalidcmp(Dt_t* dict, void* a, void* b, Dtdisc_t* disc) { Invalid_t* ap = (Invalid_t*)a; Invalid_t* bp = (Invalid_t*)b; size_t az; size_t bz; int r; if (!(r = strcmp(ap->variable->name, bp->variable->name))) { if (cxisstring(ap->variable->type) || cxisbuffer(ap->variable->type)) { az = ap->value.buffer.size; bz = bp->value.buffer.size; if (!(r = memcmp(ap->value.buffer.data, bp->value.buffer.data, az < bz ? az : bz))) { if (az < bz) r = -1; if (az > bz) r = 1; } } else if (ap->value.number < bp->value.number) r = -1; else if (ap->value.number > bp->value.number) r = 1; } return r; }
static int ordercmp(Dt_t* dict, void* a, void* b, Dtdisc_t* disc) { State_t* state = (State_t*)disc; File_t* ap = (File_t*)a; File_t* bp = (File_t*)b; Key_t* kp; size_t az; size_t bz; int k; int r; for (k = 0, kp = state->keys; k < state->nkeys; k++, kp = kp->next) if (cxisstring(kp->variable->type) || cxisbuffer(kp->variable->type)) { az = ap->data[k].value.buffer.size; bz = bp->data[k].value.buffer.size; if (r = memcmp(ap->data[k].value.buffer.data, bp->data[k].value.buffer.data, az < bz ? az : bz)) return (r < 0) ? -kp->sense : kp->sense; if (az < bz) return -kp->sense; if (az > bz) return kp->sense; } else if (ap->data[k].value.number < bp->data[k].value.number) return -kp->sense; else if (ap->data[k].value.number > bp->data[k].value.number) return kp->sense; if (ap < bp) return -1; if (ap > bp) return 1; return 0; }
int dssprintf(Dss_t* dss, Cx_t* cx, Sfio_t* sp, const char* format, Dssrecord_t* record) { register char* s; register char* t; register char* d; register char* v; register int n; register int q; register Arg_t* ap; int l; int x; char* f; char* o; char* w; Format_t* fp; Fmt_t fmt; if (!cx) cx = dss->cx; for (fp = dss->print; fp && fp->oformat != (char*)format; fp = fp->next); if (!fp) { if (f = s = (char*)format) { char* details['z' - 'a' + 1]; memset(details, 0, sizeof(details)); d = 0; l = 0; n = 0; q = 0; w = 0; for (;;) { switch (*s++) { case 0: if (q) { if (dss->disc->errorf) (*dss->disc->errorf)(NiL, dss->disc, 2, "%s: format character omitted", f); return -1; } break; case '%': if (*s != '%') { q = 1; n++; f = s - 1; } continue; case '(': if (q == 1) { q++; for (;;) { switch (*s++) { case 0: s--; break; case '(': q++; continue; case ')': if (--q == 1) break; continue; case ':': if (*s == ':') s++; else if (!d) d = s; continue; default: continue; } break; } if (d) { l += s - d + 1; d = 0; } } continue; case 'c': case 'd': case 'e': case 'f': case 'g': case 'o': case 's': case 'u': case 'x': if (q == 1) q = 0; continue; default: continue; } break; } if (!(fp = vmnewof(dss->vm, 0, Format_t, 1, (n - 1) * sizeof(Arg_t) + strlen(format) + 2 * n + l + 2))) { if (dss->disc->errorf) (*dss->disc->errorf)(NiL, dss->disc, ERROR_SYSTEM|2, "out of space"); return -1; } fp->oformat = (char*)format; fp->next = dss->print; dss->print = fp; ap = &fp->arg[0]; s = t = fp->nformat = (char*)(&fp->arg[n]); strcpy(t, format); f = t + strlen(format) + 2 * n + 1; q = 0; d = 0; l = 0; for (;;) { switch (*t++ = *s++) { case 0: *(t - 1) = '\n'; *t = 0; break; case '%': if (*s == '%') *t++ = *s++; else q = 1; continue; case '(': if (q == 1) { q++; t--; x = 0; v = s; for (;;) { switch (*s++) { case 0: if (dss->disc->errorf) (*dss->disc->errorf)(NiL, dss->disc, 2, "%s: %(...) imbalance", fp->oformat); return -1; case '(': if (!d) x = 1; q++; continue; case ')': if (--q == 1) break; continue; case ':': if (*s == ':') s++; else if (!d && q == 2) d = s; continue; case ',': if (!d) x = 1; continue; default: if (!d && cx->table->opcode[*(unsigned char*)(s - 1)]) x = 1; continue; } break; } if (d) *(d - 1) = 0; *(s - 1) = 0; if (*v) { if (x) { void* pop; if (!(pop = cxpush(cx, NiL, NiL, v, (d ? d : s) - v, 0))) return -1; ap->expr = cxcomp(cx); cxpop(cx, pop); if (!ap->expr) return -1; } else if (cx->referencef) { Cxoperand_t a; Cxoperand_t b; Cxoperand_t r; a.type = cx->state->type_string; a.value.string.size = s - v - 1; a.value.string.data = v; b.type = a.type; if ((*cx->referencef)(cx, NiL, &r, &b, &a, NiL, cx->disc)) return -1; ap->variable = r.value.variable; } else if (!(ap->variable = cxvariable(cx, v, NiL, cx->disc))) return -1; } else if (d) { w = d; d = 0; } } continue; case 'c': if (q == 1) { ap->type = DSS_FORMAT_char; ap->cast = cx->state->type_number; goto set; } continue; case 'd': case 'o': case 'u': case 'x': if (q == 1) { if (l > 1 || ap->variable && (ap->variable->format.width == 8 || ap->variable->type->format.width == 8)) { n = *(t - 1); *(t - 1) = 'l'; *t++ = 'l'; *t++ = n; ap->type = DSS_FORMAT_long; } else ap->type = DSS_FORMAT_int; ap->cast = cx->state->type_number; goto set; } continue; case 'e': case 'f': case 'g': if (q == 1) { ap->type = DSS_FORMAT_float; ap->cast = cx->state->type_number; goto set; } continue; case 'h': if (q == 1) t--; continue; case 'l': if (q == 1) { t--; l++; } continue; case 's': if (q == 1) { ap->type = DSS_FORMAT_string; ap->cast = cx->state->type_string; set: if (w) { details[*(s-1) - 'a'] = w; w = 0; fp->nformat = t = s; continue; } if (!ap->variable && !ap->expr) { if (dss->disc->errorf) { *t = 0; (*dss->disc->errorf)(NiL, dss->disc, 2, "%s: (variable) omitted in format", fp->nformat); } return -1; } l = 0; q = 0; if (d || (d = details[*(s-1) - 'a']) || (d = cx->state->type_string->format.details)) { ap->fmt = FMT_ALWAYS|FMT_ESCAPED; while (*d) { o = 0; v = d; while (*d) if (*d++ == ':') { *(o = d - 1) = 0; break; } if (strneq(v, "edit=", 5)) { if (o) *o = ':'; if (ap->edit = cxedit(cx, v + 5, dss->disc)) { d = v + 5 + ap->edit->re.re_npat; if (d == o) d++; } } else if (strneq(v, "endquote=", 8)) { ap->qe = v += 8; while (*f++ = *v++); } else if (streq(v, "expand")) { ap->fmt |= FMT_EXP_CHAR|FMT_EXP_LINE|FMT_EXP_WIDE; continue; } else if (strneq(v, "expand=", 7)) { v += 7; while (*v) { if (*v == '|' || *v == ',') { v++; continue; } if (strneq(v, "all", 3)) { ap->fmt |= FMT_EXP_CHAR|FMT_EXP_LINE|FMT_EXP_WIDE; break; } else if (strneq(v, "char", 4)) { v += 4; ap->fmt |= FMT_EXP_CHAR; } else if (strneq(v, "line", 4)) { v += 4; ap->fmt |= FMT_EXP_LINE; } else if (strneq(v, "nocr", 4)) { v += 4; ap->fmt |= FMT_EXP_NOCR; } else if (strneq(v, "nonl", 4)) { v += 4; ap->fmt |= FMT_EXP_NONL; } else if (strneq(v, "wide", 4)) { v += 4; ap->fmt |= FMT_EXP_WIDE; } else while (*v && *v != '|' && *v != ',') v++; } continue; } else if (streq(v, "escape")) ap->fmt &= ~FMT_ESCAPED; else if (strneq(v, "opt", 3)) ap->fmt &= ~FMT_ALWAYS; else if (streq(v, "quote") || strneq(v, "quote=", 6)) { if (v[5]) { ap->qb = v += 6; while (*f++ = *v++); } else ap->qb = "\""; if (!ap->qe) ap->qe = ap->qb; } else if (streq(v, "shell") || strneq(v, "shell=", 6)) { ap->fmt |= FMT_SHELL; if (v[5]) { ap->qb = v += 6; while (*f++ = *v++); } else ap->qb = "$'"; if (!ap->qe) ap->qe = "'"; } else if (streq(v, "wide")) ap->fmt |= FMT_WIDE; else { if (*d) *(d - 1) = ':'; d = v; break; } ap->flags |= DSS_FORMAT_quote; } ap->details = f; while (*f++ = *d++); d = 0; } if (ap->variable && !ap->edit && cxisstring(ap->variable->type) && ap->variable->format.map && ap->variable->format.map->part && ap->variable->format.map->part->edit) ap->edit = ap->variable->format.map->part->edit; ap++; } continue; case 'L': if (q == 1) { t--; l += 2; } continue; default: continue; } break; } } else { Cxvariable_t* vp; n = q = 0; if (dss->meth->cx->fields) { for (vp = (Cxvariable_t*)dtfirst(dss->meth->cx->fields); vp; vp = (Cxvariable_t*)dtnext(dss->meth->cx->fields, vp), n++) if (!(vp->header.flags & CX_DEPRECATED) && q < (l = strlen(vp->name))) q = l; } else if (dss->meth->data) { for (vp = (Cxvariable_t*)dss->meth->data; vp->name; vp++, n++) if (!(vp->header.flags & CX_DEPRECATED) && q < (l = strlen(vp->name))) q = l; } q += 2; if (!(fp = vmnewof(dss->vm, 0, Format_t, 1, n * sizeof(Arg_t) + n * (q + 3) + 2))) { if (dss->disc->errorf) (*dss->disc->errorf)(NiL, dss->disc, ERROR_SYSTEM|2, "out of space"); return -1; } fp->oformat = 0; fp->next = dss->print; dss->print = fp; ap = &fp->arg[0]; s = fp->nformat = (char*)(&fp->arg[n]); *s++ = '\n'; vp = dss->meth->cx->fields ? (Cxvariable_t*)dtfirst(dss->meth->cx->fields) : dss->meth->data ? (Cxvariable_t*)dss->meth->data : 0; if (vp) for (;;) { if (dss->meth->cx->fields) { if (!vp) break; } else if (!vp->name) break; if (!(vp->header.flags & CX_DEPRECATED)) { s += sfsprintf(s, q + 4, "%-*s%%s\n", q, vp->name); ap->variable = vp; ap->type = DSS_FORMAT_string; ap->cast = cx->state->type_string; if (cxisstring(vp->type) && vp->format.map && vp->format.map->part && vp->format.map->part->edit) ap->edit = vp->format.map->part->edit; ap++; } if (dss->meth->cx->fields) vp = (Cxvariable_t*)dtnext(dss->meth->cx->fields, vp); else vp++; } *s = 0; } if (!sp) return 0; } memset(&fmt, 0, sizeof(fmt)); fmt.fmt.version = SFIO_VERSION; fmt.fmt.form = fp->nformat; fmt.fmt.extf = getfmt; fmt.cx = cx; fmt.data = record; fmt.ap = &fp->arg[0]; n = sfprintf(sp, "%!", &fmt); return !sp ? 0 : (fmt.errors || n <= 0 && sp && sferror(sp)) ? -1 : n; }
static int validate_sel(Cx_t* cx, Cxexpr_t* expr, void* data, Cxdisc_t* disc) { register State_t* state = (State_t*)expr->data; register Field_t* field; register Cxconstraint_t*constraint; Cxoperand_t o; Cxinstruction_t x; Invalid_t key; Invalid_t* ip; size_t n; for (field = state->field; field; field = field->next) { x.data.variable = field->variable; if ((*state->getf)(cx, &x, &o, NiL, NiL, data, disc)) return -1; if (field->variable->format.map) { if (cxisstring(field->variable->type)) { if (cxstr2num(cx, &field->variable->format, o.value.string.data, o.value.string.size, NiL)) { if (state->verbose && disc->errorf) (*disc->errorf)(NiL, disc, 1, "%s%s: %-.*s: unknown map name", cxlocation(cx, data), field->variable->name, o.value.string.size, o.value.string.data); goto invalid; } } else if (cxisnumber(field->variable->type)) { if (cxnum2str(cx, &field->variable->format, (Cxinteger_t)o.value.number, NiL)) { if (state->verbose && disc->errorf) (*disc->errorf)(NiL, disc, 1, "%s%s: %I*d: unknown map value", cxlocation(cx, data), field->variable->name, sizeof(Cxinteger_t), (Cxinteger_t)o.value.number); goto invalid; } } } if (constraint = field->variable->format.constraint) { if (constraint->constraintf) ; if (cxisnumber(field->variable->type)) { if (constraint->min && o.value.number < constraint->min->number) { if (state->verbose && disc->errorf) (*disc->errorf)(NiL, disc, 1, "%s%s: %1.15Lg violates min constraint %1.15Lg", cxlocation(cx, data), field->variable->name, o.value.number, constraint->min->number); goto invalid; } if (constraint->max && o.value.number > constraint->max->number) { if (state->verbose && disc->errorf) (*disc->errorf)(NiL, disc, 1, "%s%s: %1.15Lg violates max constraint %1.15Lg", cxlocation(cx, data), field->variable->name, o.value.number, constraint->max->number); goto invalid; } } if (constraint->expression) ; if (constraint->pattern) ; } continue; invalid: if (state->invalid) { key.variable = field->variable; key.value = o.value; if (!(ip = (Invalid_t*)dtsearch(state->invalid, &key))) { n = cxsize(field->variable->type, &o.value); if (!(ip = vmnewof(state->vm, 0, Invalid_t, 1, n))) { if (disc->errorf) (*disc->errorf)(NiL, disc, ERROR_SYSTEM|2, "out of space"); return -1; } *ip = key; ip->value = o.value; if (n) { ip->value.buffer.data = (void*)(ip + 1); memcpy(ip->value.buffer.data, o.value.buffer.data, n); } dtinsert(state->invalid, ip); ip->count = 0; } ip->count++; } if (state->setf && constraint && constraint->def) { o.type = field->variable->type; o.value = *constraint->def; if ((*state->setf)(cx, &x, &o, &o, NiL, data, disc)) return -1; field->repaired++; } else if (state->discard) { field->discarded++; return 0; } else field->invalid++; } return 1; }
static int validate_beg(Cx_t* cx, Cxexpr_t* expr, void* data, Cxdisc_t* disc) { char** argv = (char**)data; int errors = error_info.errors; char* s; State_t* state; Cxvariable_t* variable; register Field_t* field; Field_t* lastfield; Cxconstraint_t* constraint; int all; int list; Vmalloc_t* vm; if (!(vm = vmopen(Vmdcheap, Vmlast, 0)) || !(state = vmnewof(vm, 0, State_t, 1, 0))) { if (vm) vmclose(vm); if (disc->errorf) (*disc->errorf)(NiL, disc, ERROR_SYSTEM|2, "out of space"); return -1; } state->vm = vm; list = 0; sfprintf(cx->buf, "%s%s", strchr(dss_lib_validate.description, '['), validate_usage); s = sfstruse(cx->buf); for (;;) { switch (optget(argv, s)) { case 'd': state->discard = 1; continue; case 'l': list = 1; continue; case 'r': if (!(state->setf = cxcallout(cx, CX_SET, cx->state->type_void, cx->state->type_void, cx->disc))) { if (cx->disc->errorf) (*cx->disc->errorf)(NiL, cx->disc, 3, "reair requires CX_SET callout"); return -1; } continue; case 's': state->summary = 1; continue; case 'v': state->summary = state->verbose = 1; continue; case '?': if (disc->errorf) (*disc->errorf)(NiL, disc, ERROR_USAGE|4, "%s", opt_info.arg); else return -1; continue; case ':': if (disc->errorf) (*disc->errorf)(NiL, disc, 2, "%s", opt_info.arg); else return -1; continue; } break; } if (error_info.errors > errors) goto bad; argv += opt_info.index; if (all = !*argv) variable = 0; do { if (all) { if (!(variable = (Cxvariable_t*)(variable ? dtnext(cx->fields, variable) : dtfirst(cx->fields)))) break; } else if (!(variable = cxvariable(cx, *argv, NiL, disc))) goto bad; if (variable->format.constraint || variable->format.map) { if (!(field = vmnewof(vm, 0, Field_t, 1, 0))) { if (disc->errorf) (*disc->errorf)(NiL, disc, ERROR_SYSTEM|2, "out of space"); goto bad; } field->variable = variable; if (state->field) lastfield = lastfield->next = field; else lastfield = state->field = field; } } while (all || *++argv); if (!state->field && disc->errorf) (*disc->errorf)(NiL, disc, 1, "no field has constraints or maps"); if (list) { for (field = state->field; field; field = field->next) { sfprintf(expr->op, "%16s", field->variable->name); if (field->variable->format.map) sfprintf(expr->op, " map"); if (constraint = field->variable->format.constraint) { if (constraint->name) sfprintf(expr->op, " name=%s", constraint->name); if (constraint->constraintf) sfprintf(expr->op, " external"); if (cxisnumber(field->variable->type)) { if (constraint->def) number(expr->op, "default", constraint->def->number, &field->variable->format); if (constraint->min) number(expr->op, "min", constraint->min->number, &field->variable->format); if (constraint->max) number(expr->op, "max", constraint->max->number, &field->variable->format); } else if (cxisstring(field->variable->type) && constraint->def) sfprintf(expr->op, " default=\"%-.*s\"", constraint->def->string.size, constraint->def->string.data); if (constraint->expression) sfprintf(expr->op, " expression=\"%s\"", constraint->expression); if (constraint->pattern) sfprintf(expr->op, " pattern=\"%s\"", constraint->pattern); } sfprintf(expr->op, "\n"); } goto bad; } if (!(state->getf = cxcallout(cx, CX_GET, cx->state->type_void, cx->state->type_void, cx->disc))) { if (cx->disc->errorf) (*cx->disc->errorf)(NiL, cx->disc, 3, "validation requires CX_GET callout"); goto bad; } if (!state->verbose) { state->invaliddisc.comparf = invalidcmp; if (!(state->invalid = dtnew(vm, &state->invaliddisc, Dtoset))) { if (cx->disc->errorf) (*cx->disc->errorf)(NiL, cx->disc, 3, "validation requires CX_GET callout"); goto bad; } } expr->data = state; return 0; bad: vmclose(vm); return -1; }