argument "-" for stdin file

This commit is contained in:
dzaima 2021-09-21 16:44:23 +03:00
parent 59a9a5daf4
commit 6f99b58099
6 changed files with 42 additions and 11 deletions

View File

@ -1,6 +1,9 @@
B m_str8(usz sz, char* s);
B m_str8l(char* s);
B m_str32(u32* s);
B m_str8l(char* s);
B fromUTF8l(char* x);
B fromUTF8a(I8Arr* x);
C8Arr* cpyC8Arr (B x); // consumes
C16Arr* cpyC16Arr(B x); // consumes

View File

@ -118,8 +118,6 @@ static bool elNum(u8 x) {
// string stuff
B m_str8l(char* s);
B fromUTF8l(char* x);
i32 num_fmt(char buf[30], f64 x);
#define NUM_FMT_BUF(N,X) char N[30]; num_fmt(N, X);
B append_fmt(B s, char* p, ...);

View File

@ -53,11 +53,13 @@ int main(int argc, char* argv[]) {
#endif
bool startREPL = argc==1;
bool silentREPL = false;
bool execStdin = false;
if (!startREPL) {
i32 i = 1;
while (i!=argc) {
char* carg = argv[i];
if (carg[0]!='-') break;
if (carg[1]==0) { execStdin=true; i++; break; }
i++;
if (carg[1]=='-') {
if (!strcmp(carg, "--help")) {
@ -124,16 +126,17 @@ int main(int argc, char* argv[]) {
break;
}
#endif
case 'r': { startREPL = true; break; }
case 's': { startREPL = true; silentREPL = true; break; }
case 'r': { startREPL=true; break; }
case 's': { startREPL=true; silentREPL=true; break; }
}
}
}
}
execFile:
if (i!=argc) {
if (i!=argc || execStdin) {
repl_init();
B src = fromUTF8l(argv[i++]);
B src;
if (!execStdin) src = fromUTF8l(argv[i++]);
B args;
if (i==argc) {
args = emptySVec();
@ -144,7 +147,14 @@ int main(int argc, char* argv[]) {
}
args = ap.b;
}
dec(bqn_execFile(src, args));
B execRes;
if (execStdin) {
execRes = gsc_exec_inline(fromUTF8a(stdin_allBytes()), m_str8l("(-)"), args);
} else {
execRes = bqn_execFile(src, args);
}
dec(execRes);
#ifdef HEAP_VERIFY
heapVerify();
#endif

View File

@ -37,10 +37,7 @@ I8Arr* file_bytes(B path) { // consumes
return src;
}
B file_chars(B path) { // consumes
I8Arr* c = file_bytes(path);
B r = fromUTF8((char*)c->a, c->ia);
ptr_dec(c);
return r;
return fromUTF8a(file_bytes(path));
}
B file_lines(B path) { // consumes
I8Arr* tf = file_bytes(path);
@ -68,6 +65,21 @@ B file_lines(B path) { // consumes
return harr_fv(r);
}
I8Arr* stdin_allBytes() {
B r = emptyIVec();
u64 SZ = 8192;
TALLOC(i8, t, SZ);
while(true) {
u64 read = fread(t, 1, SZ, stdin);
if (read==0) break;
i8* ap; B a = m_i8arrv(&ap, read);
memcpy(ap, t, read);
r = vec_join(r, a);
}
TFREE(t);
return toI8Arr(r);
}
B path_resolve(B base, B rel) { // consumes rel; assumes base is a char vector or bi_N

View File

@ -10,6 +10,8 @@ I8Arr* file_bytes(B path); // consumes
B file_chars(B path); // consumes
B file_lines(B path); // consumes
I8Arr* stdin_allBytes();
void file_wChars(B path, B x); // consumes path
void file_wBytes(B path, B x); // consumes path

View File

@ -49,6 +49,12 @@ B fromUTF8l(char* s) {
return fromUTF8(s, strlen(s));
}
B fromUTF8a(I8Arr* a) { // consumes a
B r = fromUTF8((char*)a->a, a->ia);
ptr_dec(a);
return r;
}
void printUTF8(u32 c) {
if (c<128) printf("%c", c);
else if (c<=0x07FF) printf("%c%c" , 0xC0| c>>6 , 0x80|(c &0x3F) );