#if defined(__GNUC__) && !defined(_GNU_SOURCE)
#define _GNU_SOURCE
#endif
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include "config.h"
#include "buf.h"
#include "ktable.h"
#include "capture.h"
static void print_capture(CapState *cs) {
size_t len;
Capture *c = cs->cap;
printf(" kind = %s\n", CAPTURE_NAME(capkind(c)));
printf(" pos (1-based) = %lu\n", c->s ? (c->s - cs->s + 1) : 0);
const char *name = ktable_element_name(cs->kt, capidx(c), &len);
if (name) {
printf(" idx = %u\n", capidx(c));
printf(" ktable[idx] = %.*s\n", (int) len, name);
}
}
static void print_constant_capture(CapState *cs) {
size_t len;
const char *name = ktable_element_name(cs->kt, capidx(cs->cap), &len);
printf(" constant match: %.*s\n", (int) len, name);
}
int debug_Close(CapState *cs, Buffer *buf, int count, const char *start) {
UNUSED(buf); UNUSED(count); UNUSED(start);
if (isopencap(cs->cap)) return MATCH_CLOSE_ERROR;
if (capkind(cs->cap) == Ccloseconst)
print_constant_capture(cs);
printf("CLOSE:\n");
print_capture(cs);
return MATCH_OK;
}
int debug_Open(CapState *cs, Buffer *buf, int count) {
UNUSED(buf); UNUSED(count);
if (!acceptable_capture(capkind(cs->cap))) return MATCH_OPEN_ERROR;
printf("OPEN:\n");
print_capture(cs);
return MATCH_OK;
}
static void encode_pos(size_t pos, int negate, Buffer *buf) {
int intpos = (int) pos;
if (negate) intpos = - intpos;
buf_addint(buf, intpos);
}
static void encode_string(const char *string, size_t len,
int shortflag, int negflag, Buffer *buf) {
assert(string);
if (shortflag) buf_addshort(buf, (short) (negflag ? -len : len));
else buf_addint(buf, (int) (negflag ? -len : len));
buf_addlstring(buf, string, len);
}
static void encode_ktable_element(CapState *cs, byte negflag, Buffer *buf) {
size_t len;
const char *name = ktable_element_name(cs->kt, capidx(cs->cap), &len);
assert( name );
encode_string(name, len, 1, negflag, buf);
}
int byte_Close(CapState *cs, Buffer *buf, int count, const char *start) {
size_t e;
UNUSED(count); UNUSED(start);
if (isopencap(cs->cap)) return MATCH_CLOSE_ERROR;
if (capkind(cs->cap) == Ccloseconst) encode_ktable_element(cs, 0, buf);
e = cs->cap->s - cs->s + 1;
encode_pos(e, 0, buf);
return MATCH_OK;
}
int byte_Open(CapState *cs, Buffer *buf, int count) {
size_t s;
UNUSED(count);
if (!acceptable_capture(capkind(cs->cap))) {
printf("byte_Open: capkind is %d (%s)\n", capkind(cs->cap), CAPTURE_NAME(capkind(cs->cap)));
return MATCH_OPEN_ERROR;
}
s = cs->cap->s - cs->s + 1;
assert(capidx(cs->cap) >= 0);
encode_pos(s, 1, buf);
encode_ktable_element(cs, (capkind(cs->cap) == Crosieconst), buf);
return MATCH_OK;
}
Encoder debug_encoder = { debug_Open, debug_Close };
Encoder byte_encoder = { byte_Open, byte_Close };