From d0c6ade53dd763a92e31333fd789158304e8e842 Mon Sep 17 00:00:00 2001 From: Sigrid Date: Tue, 15 Dec 2020 14:25:59 +0100 Subject: [PATCH] pc: treat EOF gracefully, allowing easier use within sam command language --- sys/src/cmd/pc.y | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/sys/src/cmd/pc.y b/sys/src/cmd/pc.y index 6d828f8a2..1abb328df 100644 --- a/sys/src/cmd/pc.y +++ b/sys/src/cmd/pc.y @@ -7,7 +7,7 @@ #include #include -int inbase = 10, outbase, divmode, sep, heads, fail, prompt; +int inbase = 10, outbase, divmode, sep, heads, fail, prompt, eof; enum { MAXARGS = 16 }; typedef struct Num Num; @@ -591,18 +591,23 @@ yylex(void) int c, b; char buf[512], *p; Keyword *kw; - - if(prompt && !prompted) {print("; "); prompted = 1;} + + if(prompt && !prompted && !eof) {print("; "); prompted = 1;} do c = Bgetc(in); while(c != '\n' && isspace(c)); - if(c == '\n') prompted = 0; + if(c < 0 && !eof){ + eof = 1; + c = '\n'; + } + if(c == '\n') + prompted = 0; if(isdigit(c)){ for(p = buf, *p++ = c; c = Bgetc(in), isalnum(c) || c == '_'; ) if(p < buf + sizeof(buf) - 1 && c != '_') *p++ = c; *p = 0; - Bungetc(in); + if(c >= 0) Bungetc(in); b = inbase; p = buf; if(*p == '0'){ @@ -636,7 +641,7 @@ yylex(void) for(; kw->name[0] == c; kw++) if(kw->name[0] == b) return kw->tok; - Bungetc(in); + if(c >= 0) Bungetc(in); } return c; }