Skip to content

Commit

Permalink
Don't display snek prompt for input() on embedded targets
Browse files Browse the repository at this point in the history
Make the prompt that snek-io displays conditional based on whether
it is being called from 'input'. There might be a cleaner way to manage
this, but that probably means doing the prompt from snek-lex, which gets
messy.

Signed-off-by: Keith Packard <keithp@keithp.com>
  • Loading branch information
keith-packard committed Sep 18, 2019
1 parent c526fe7 commit 9d4db5e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
5 changes: 4 additions & 1 deletion snek-input.c
Expand Up @@ -15,6 +15,8 @@
#include "snek.h"
#include <math.h>

bool snek_in_input;

snek_poly_t
snek_builtin_input(uint8_t nposition, uint8_t nnamed, snek_poly_t *args)
{
Expand All @@ -29,7 +31,7 @@ snek_builtin_input(uint8_t nposition, uint8_t nnamed, snek_poly_t *args)
putc(' ', stdout);
}
(void) nnamed;
fflush(stdout);
snek_in_input = true;
while ((c = getchar()) != '\n' && c != EOF) {
if (snek_is_null(s)) {
s = snek_string_make(c);
Expand All @@ -38,6 +40,7 @@ snek_builtin_input(uint8_t nposition, uint8_t nnamed, snek_poly_t *args)
s = snek_string_cat(snek_poly_to_string(s), in);
}
}
snek_in_input = false;
return s;
}

Expand Down
36 changes: 30 additions & 6 deletions snek-io.c
Expand Up @@ -48,23 +48,41 @@ snek_io_addc(char c)
SNEK_IO_PUTC(c);
}

#ifdef SNEK_BUILTIN_input
extern bool snek_in_input;
static char unget;
#endif

int
snek_io_getc(FILE *stream)
{
(void) stream;
if (used == avail) {
#ifdef SNEK_BUILTIN_input
if (!snek_in_input)
#endif
{
restart_cooked:
if (snek_parse_middle)
SNEK_IO_PUTC('+');
else
SNEK_IO_PUTC('>');
SNEK_IO_PUTC(' ');
if (snek_parse_middle)
SNEK_IO_PUTC('+');
else
SNEK_IO_PUTC('>');
SNEK_IO_PUTC(' ');
}
restart_raw:
used = avail = 0;
for (;;) {
if (!SNEK_IO_WAITING(stream))
fflush(stdout);
uint8_t c = SNEK_IO_GETC(stream);
uint8_t c;

#ifdef SNEK_BUILTIN_input
if (unget) {
c = unget;
unget = 0;
} else
#endif
c = SNEK_IO_GETC(stream);

switch (c)
{
Expand All @@ -79,6 +97,12 @@ snek_io_getc(FILE *stream)
raw_mode = false;
continue;
case 'c' & 0x1f:
#ifdef SNEK_BUILTIN_input
if (snek_in_input) {
unget = c;
return EOF;
}
#endif
snek_abort = false;
if (raw_mode)
goto restart_raw;
Expand Down

0 comments on commit 9d4db5e

Please sign in to comment.