tisp

tiny lisp
git clone git://edryd.org/tisp
Log | Files | Refs | LICENSE

commit ef3108dfd09b0f3d123ffb9157849ef021313354
parent 7a20c9c7545bb24e71ece3e9530b92a013045091
Author: Ed van Bruggen <edvb@uw.edu>
Date:   Wed, 28 Mar 2018 19:04:24 -0700

Read multiple lines of file

Diffstat:
tisp.c | 39++++++++++++++++++---------------------
1 file changed, 18 insertions(+), 21 deletions(-)

diff --git a/tisp.c b/tisp.c @@ -391,27 +391,20 @@ hints(const char *buf, int *color, int *bold) } static Val -tisp_read(char *cmd) +tisp_read(Str cmd) { - char *line; struct Str str; - Val ast; - - if (cmd) { - line = estrdup(cmd); - } else { - if (SHOW_HINTS) - linenoiseSetHintsCallback(hints); - if (!(line = linenoise("> "))) - return NULL; - linenoiseHistoryAdd(line); - } - str.d = line; - ast = read_val(&str); - free(line); + if (cmd->d) + return read_val(cmd); - return ast; + if (SHOW_HINTS) + linenoiseSetHintsCallback(hints); + if (!(str.d = linenoise("> "))) + return NULL; + linenoiseHistoryAdd(str.d); + + return read_val(&str); } static int @@ -686,7 +679,8 @@ main(int argc, char *argv[]) } ARGEND; size_t nread; - char *str = NULL, buf[BUF_SIZE]; + char buf[BUF_SIZE]; + struct Str str = { NULL }; FILE *fp; Val v; Hash env = init_env(); @@ -699,16 +693,19 @@ main(int argc, char *argv[]) if (!(fp = fopen(*argv, "r"))) die(1, "%s: %s:", argv[0], *argv); while ((nread = fread(buf, 1, sizeof(buf), fp)) > 0) ; - str = buf; + str.d = estrdup(buf); } - while ((v = tisp_read(str))) { + while ((v = tisp_read(&str))) { if (!(v = tisp_eval(env, v))) continue; tisp_print(v); putchar('\n'); - if (str) break; + + if (!str.d) continue; + skip_spaces(&str); + if (!*str.d) break; } return 0;