/* * Remember input @inp for a while. */ void save_input(char inp) { int eol; while (ring_putc(&recent_input, inp) < 0) { eol = ring_search(&recent_input, "\n", 0); assert(eol >= 0); ring_discard(&recent_input, eol + 1); } }
/* * Forget remembered input up to SEEN. * SEEN should be obtained from save_input() or seen_input(). */ void forget_input(size_t seen) { size_t forgotten = saved_bytes - ring_len(&recent_input); assert(seen); if (seen > forgotten) { assert(ring_peek(&recent_input, seen - forgotten - 1) == '\n'); ring_discard(&recent_input, seen - forgotten); } }
/* * Remember line of input INP for a while. * It must end with a newline. * Return value is suitable for forget_input(): it makes it forget all * input up to and including this line. */ size_t save_input(char *inp) { size_t len = strlen(inp); int eol; assert(len && inp[len - 1] == '\n'); while (ring_putm(&recent_input, inp, len) < 0) { eol = ring_search(&recent_input, "\n"); assert(eol >= 0); ring_discard(&recent_input, eol + 1); } saved_bytes += len; return saved_bytes; }