From de4914991a4955f133e6bcd0b68896a373ecdbb3 Mon Sep 17 00:00:00 2001 From: dzaima Date: Fri, 25 Feb 2022 18:20:25 +0200 Subject: [PATCH] =?UTF-8?q?=E2=80=A2term.OutRaw,=20=E2=80=A2term.ErrRaw?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/builtins.h | 2 +- src/builtins/sysfn.c | 77 +++++++++++++++++++++++--------------------- src/jit/nvm_x86_64.c | 6 ++-- src/load.c | 2 +- src/main.c | 4 +-- src/utils/file.c | 30 +++++++++-------- src/utils/file.h | 15 +++++---- 7 files changed, 73 insertions(+), 63 deletions(-) diff --git a/src/builtins.h b/src/builtins.h index f3061005..9ea64354 100644 --- a/src/builtins.h +++ b/src/builtins.h @@ -12,7 +12,7 @@ /* sysfn.c*/M(repr,"•Repr") M(fmt,"•Fmt") A(asrt,"!") A(casrt,"!") M(out,"•Out") M(show,"•Show") A(bqn,"•BQN") A(sh,"•SH") M(fromUtf8,"•FromUTF8") M(currentError,"•CurrentError") \ /* sysfn.c*/D(cmp,"•Cmp") A(hash,"•Hash") M(unixTime,"•UnixTime") M(monoTime,"•MonoTime") M(delay,"•Delay") M(makeRand,"•MakeRand") M(reBQN,"•ReBQN") M(exit,"•Exit") M(getLine,"•GetLine") \ /* sysfn.c*/M(fName,"•file.Name") \ -/* sysfn.c*/M(tRaw,"•term.Raw") M(tFlush,"•term.Flush") M(tCharB,"•term.CharB") M(tCharN,"•term.CharN") \ +/* sysfn.c*/M(tRaw,"•term.Raw") M(tFlush,"•term.Flush") M(tCharB,"•term.CharB") M(tCharN,"•term.CharN") M(tOutRaw,"•term.OutRaw") M(tErrRaw,"•term.ErrRaw") \ /* inverse.c*/M(setInvReg, "(SetInvReg)") M(setInvSwap, "(SetInvSwap)") M(nativeInvReg, "(NativeInvReg)") M(nativeInvSwap, "(NativeInvSwap)") \ /*internal.c*/M(itype,"•internal.Type") M(elType,"•internal.ElType") M(refc,"•internal.Refc") M(isPure,"•internal.IsPure") A(info,"•internal.Info") \ /*internal.c*/M(squeeze,"•internal.Squeeze") M(deepSqueeze,"•internal.DeepSqueeze") \ diff --git a/src/builtins/sysfn.c b/src/builtins/sysfn.c index 38fa5df6..cf436c32 100644 --- a/src/builtins/sysfn.c +++ b/src/builtins/sysfn.c @@ -575,18 +575,18 @@ B fileAt_c2(B d, B w, B x) { } static NFnDesc* fCharsDesc; B fchars_c1(B d, B x) { - return file_chars(path_rel(nfn_objU(d), x)); + return path_chars(path_rel(nfn_objU(d), x)); } B fchars_c2(B d, B w, B x) { if (!isArr(x)) thrM("•FChars: Non-array 𝕩"); B p = path_rel(nfn_objU(d), w); - file_wChars(inc(p), x); + path_wChars(inc(p), x); dec(x); return p; } static NFnDesc* fBytesDesc; B fbytes_c1(B d, B x) { - I8Arr* tf = file_bytes(path_rel(nfn_objU(d), x)); + I8Arr* tf = path_bytes(path_rel(nfn_objU(d), x)); usz ia = tf->ia; u8* p = (u8*)tf->a; u8* rp; B r = m_c8arrv(&rp, ia); for (i64 i = 0; i < ia; i++) rp[i] = p[i]; @@ -596,13 +596,13 @@ B fbytes_c1(B d, B x) { B fbytes_c2(B d, B w, B x) { if (!isArr(x)) thrM("•FBytes: Non-array 𝕩"); B p = path_rel(nfn_objU(d), w); - file_wBytes(inc(p), x); + path_wBytes(inc(p), x); dec(x); return p; } static NFnDesc* fLinesDesc; B flines_c1(B d, B x) { - return file_lines(path_rel(nfn_objU(d), x)); + return path_lines(path_rel(nfn_objU(d), x)); } B flines_c2(B d, B w, B x) { if (!isArr(x)) thrM("•FLines: Non-array 𝕩"); @@ -618,7 +618,7 @@ B flines_c2(B d, B w, B x) { } dec(x); B p = path_rel(nfn_objU(d), w); - file_wChars(inc(p), s); + path_wChars(inc(p), s); dec(s); return p; } @@ -672,11 +672,11 @@ static void sys_gcFn() { static NFnDesc* fTypeDesc; static NFnDesc* fListDesc; B list_c1(B d, B x) { - return file_list(path_rel(nfn_objU(d), x)); + return path_list(path_rel(nfn_objU(d), x)); } B ftype_c1(B d, B x) { - return m_c32(file_type(path_rel(nfn_objU(d), x))); + return m_c32(path_type(path_rel(nfn_objU(d), x))); } B fName_c1(B t, B x) { @@ -867,11 +867,6 @@ B sh_c1(B t, B x) { return sh_c2(t, bi_N, x); } #if __has_include() #include #include -B tFlush_c1(B t, B x) { - fflush(stdout); - fflush(stderr); - return x; -} B tRaw_c1(B t, B x) { struct termios term; tcgetattr(STDIN_FILENO, &term); @@ -880,11 +875,6 @@ B tRaw_c1(B t, B x) { tcsetattr(STDIN_FILENO, TCSAFLUSH, &term); return x; } -B tCharB_c1(B t, B x) { - dec(x); - int n = fgetc(stdin); - return n>=0? m_c32(n) : m_f64(0); -} B tCharN_c1(B t, B x) { dec(x); fcntl(0, F_SETFL, O_NONBLOCK); @@ -893,13 +883,41 @@ B tCharN_c1(B t, B x) { return n>=0? m_c32(n) : m_f64(0); } #else -NORETURN B noTerm() { thrM("•term not available"); } -B tFlush_c1(B t, B x) { noTerm(); } -B tRaw_c1(B t, B x) { noTerm(); } -B tCharB_c1(B t, B x) { noTerm(); } -B tCharN_c1(B t, B x) { noTerm(); } +B tRaw_c1(B t, B x) { thrM("•term.Raw not available"); } +B tCharN_c1(B t, B x) { thrM("•term.CharN not available"); } #endif +B tCharB_c1(B t, B x) { + dec(x); + int n = fgetc(stdin); + return n>=0? m_c32(n) : m_f64(0); +} +B tFlush_c1(B t, B x) { + fflush(stdout); + fflush(stderr); + return x; +} +B tOutRaw_c1(B t, B x) { + file_wBytes(stdout, bi_N, x); + return x; +} +B tErrRaw_c1(B t, B x) { + file_wBytes(stdout, bi_N, x); + return x; +} + +static B termNS; +B getTermNS() { + if (termNS.u == 0) { + #define F(X) inc(bi_##X), + Body* d = m_nnsDesc("flush", "raw", "charb", "charn", "outraw", "errraw"); + termNS = m_nns(d,F(tFlush)F(tRaw)F(tCharB)F(tCharN)F(tOutRaw)F(tErrRaw)); + #undef F + gc_add(termNS); + } + return inc(termNS); +} + typedef struct CastType { usz s; bool c; } CastType; @@ -1016,19 +1034,6 @@ B getBitNS() { return inc(bitNS); } - -static B termNS; -B getTermNS() { - if (termNS.u == 0) { - #define F(X) inc(bi_##X), - Body* d = m_nnsDesc("flush", "raw", "charb", "charn"); - termNS = m_nns(d,F(tFlush)F(tRaw)F(tCharB)F(tCharN)); - #undef F - gc_add(termNS); - } - return inc(termNS); -} - B getInternalNS(void); B getMathNS(void); B getPrimitives(void); diff --git a/src/jit/nvm_x86_64.c b/src/jit/nvm_x86_64.c index 9319a0bc..5ca075f6 100644 --- a/src/jit/nvm_x86_64.c +++ b/src/jit/nvm_x86_64.c @@ -480,15 +480,15 @@ void freeOpt(OptRes o) { static void write_asm(u8* p, u64 sz) { i32* rp; B r = m_i32arrv(&rp, sz); for (u64 i = 0; i < sz; i++) rp[i] = p[i]; - file_wBytes(m_str8l("asm_bin"), r); dec(r); + path_wBytes(m_str8l("asm_bin"), r); dec(r); char off[20]; snprintf(off, 20, "%p", p); B o = m_str8l(off); - file_wChars(m_str8l("asm_off"), o); dec(o); + path_wChars(m_str8l("asm_off"), o); dec(o); B s = emptyCVec(); #define F(X) AFMT("s/%p$/%p # i_" #X "/;", i_##X, i_##X); F(POPS)F(INC)F(FN1C)F(FN1O)F(FN2C)F(FN2O)F(FN1Oi)F(FN2Oi)F(ARR_0)F(ARR_2)F(ARR_p)F(DFND_0)F(DFND_1)F(DFND_2)F(MD1C)F(MD2C)F(MD2R)F(TR2D)F(TR3D)F(TR3O)F(NOVAR)F(EXTO)F(EXTU)F(SETN)F(SETU)F(SETM)F(SETC)F(SETH1)F(SETH2)F(PRED1)F(PRED2)F(SETNi)F(SETUi)F(SETMi)F(SETCi)F(SETNv)F(SETUv)F(SETMv)F(SETCv)F(FLDO)F(VFYM)F(ALIM)F(CHKV)F(FAIL)F(RETD) #undef F - file_wChars(m_str8l("asm_sed"), s); dec(s); + path_wChars(m_str8l("asm_sed"), s); dec(s); } #endif diff --git a/src/load.c b/src/load.c index 237f815b..e8d69389 100644 --- a/src/load.c +++ b/src/load.c @@ -479,7 +479,7 @@ void load_init() { // very last init function } B bqn_execFile(B path, B args) { // consumes both - return bqn_exec(file_chars(inc(path)), path, args); + return bqn_exec(path_chars(inc(path)), path, args); } void bqn_exit(i32 code) { diff --git a/src/main.c b/src/main.c index e5d97293..044618cc 100644 --- a/src/main.c +++ b/src/main.c @@ -107,7 +107,7 @@ int main(int argc, char* argv[]) { #ifdef PERF_TEST case 'R': { repl_init(); REQARG(R); B path = fromUTF8l(argv[i++]); - B lines = file_lines(path); + B lines = path_lines(path); usz ia = a(lines)->ia; SGet(lines) for (u64 i = 0; i < ia; i++) { @@ -176,7 +176,7 @@ int main(int argc, char* argv[]) { char* cmdE; if (isCmd(cmdS, &cmdE, "ex ")) { B path = fromUTF8l(cmdE); - code = file_chars(path); + code = path_chars(path); output = 0; } else if (isCmd(cmdS, &cmdE, "t ") || isCmd(cmdS, &cmdE, "time ")) { code = fromUTF8l(cmdE); diff --git a/src/utils/file.c b/src/utils/file.c index 1d96a570..1eba4562 100644 --- a/src/utils/file.c +++ b/src/utils/file.c @@ -49,7 +49,7 @@ I8Arr* stream_bytes(FILE* f) { return toI8Arr(r); } -I8Arr* file_bytes(B path) { // consumes +I8Arr* path_bytes(B path) { // consumes FILE* f = file_open(path, "read", "r"); int seekRes = fseek(f, 0, SEEK_END); I8Arr* src; @@ -65,11 +65,11 @@ I8Arr* file_bytes(B path) { // consumes fclose(f); return src; } -B file_chars(B path) { // consumes - return fromUTF8a(file_bytes(path)); +B path_chars(B path) { // consumes + return fromUTF8a(path_bytes(path)); } -B file_lines(B path) { // consumes; TODO rewrite this, it's horrible - I8Arr* tf = file_bytes(path); +B path_lines(B path) { // consumes; TODO rewrite this, it's horrible + I8Arr* tf = path_bytes(path); usz ia = tf->ia; u8* p = (u8*)tf->a; usz lineCount = 0; for (usz i = 0; i < ia; i++) { @@ -172,7 +172,7 @@ B path_abs(B path) { } -void file_wChars(B path, B x) { // consumes path +void path_wChars(B path, B x) { // consumes path FILE* f = file_open(path, "write to", "w"); u64 len = utf8lenB(x); @@ -184,9 +184,7 @@ void file_wChars(B path, B x) { // consumes path dec(path); fclose(f); } -void file_wBytes(B path, B x) { // consumes path - FILE* f = file_open(path, "write to", "w"); - +void file_wBytes(FILE* f, B name, B x) { u64 len = a(x)->ia; TALLOC(char, val, len); SGetU(x) @@ -195,13 +193,19 @@ void file_wBytes(B path, B x) { // consumes path val[i] = isNum(c)? o2iu(c) : o2c(c); } - if (fwrite(val, 1, len, f) != len) thrF("Error writing to file \"%R\"", path); + if (fwrite(val, 1, len, f) != len) { + if (q_N(name)) thrM("Error writing to file"); + else thrF("Error writing to file \"%R\"", name); + } TFREE(val); - dec(path); +} +void path_wBytes(B path, B x) { // consumes path + FILE* f = file_open(path, "write to", "w"); + file_wBytes(f, path, x); fclose(f); } -B file_list(B path) { +B path_list(B path) { DIR* d = dir_open(path); struct dirent *c; B res = emptySVec(); @@ -217,7 +221,7 @@ B file_list(B path) { #include #include -char file_type(B path) { +char path_type(B path) { char* p = toCStr(path); struct stat path_stat; stat(p, &path_stat); diff --git a/src/utils/file.h b/src/utils/file.h index c81b9391..8e145b97 100644 --- a/src/utils/file.h +++ b/src/utils/file.h @@ -7,14 +7,15 @@ B path_name(B path); // consumes; returns filename from a path B path_abs(B path); // consumes; returns absolute version of the path; propagates bi_N FILE* file_open(B path, char* desc, char* mode); // doesn't consume path -I8Arr* file_bytes(B path); // consumes -B file_chars(B path); // consumes -B file_lines(B path); // consumes +I8Arr* path_bytes(B path); // consumes +B path_chars(B path); // consumes +B path_lines(B path); // consumes I8Arr* stream_bytes(FILE* f); -void file_wChars(B path, B x); // consumes path -void file_wBytes(B path, B x); // consumes path +void path_wChars(B path, B x); // consumes path +void path_wBytes(B path, B x); // consumes path +void file_wBytes(FILE* file, B name, B x); // consumes x -B file_list(B path); // consumes -char file_type(B path); // consumes +B path_list(B path); // consumes +char path_type(B path); // consumes