ref: d0c6ade53dd763a92e31333fd789158304e8e842
parent: 3749e92cdb88a157f99c0709a264bd508603be9b
author: Sigrid <[email protected]>
date: Tue Dec 15 09:25:59 EST 2020
pc: treat EOF gracefully, allowing easier use within sam command language
--- a/sys/src/cmd/pc.y
+++ b/sys/src/cmd/pc.y
@@ -7,7 +7,7 @@
#include <thread.h>
#include <libsec.h>
-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 @@
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 @@
for(; kw->name[0] == c; kw++)
if(kw->name[0] == b)
return kw->tok;
- Bungetc(in);
+ if(c >= 0) Bungetc(in);
}
return c;
}