diff --git a/lib/io.bas b/lib/io.bas index 1e83497..f39e8f6 100644 --- a/lib/io.bas +++ b/lib/io.bas @@ -3,29 +3,29 @@ 'listDir$ - Returns tab delimited file list of directory specified ' spec$ - Directory spec. Pass "" to get current directory FUNCTION listDir$ (spec$) - rndbuf = Rnd * 999999 - buff_file$ = "/tmp/buff_qbsh_" + LTrim$(Str$(rndbuf)) - If _FileExists(buff_file$) Then - Kill buff_file$ - End If - Shell "SHELL='" + SELFPATH$ + "'; dir -a " + spec$ + " 2>&1 >" + buff_file$ - Open buff_file$ For Binary As #1 - x$ = Space$(LOF(1)) - Get #1, , x$ - Close #1 - Kill buff_file$ - listDir$ = x$ + rndbuf = Rnd * 999999 + buff_file$ = "/tmp/buff_qbsh_" + LTrim$(Str$(rndbuf)) + If _FileExists(buff_file$) Then + Kill buff_file$ + End If + Shell "SHELL='" + SELFPATH$ + "'; $(which unbuffer) dir --color -a " + spec$ + " 2>&1 >" + buff_file$ + Open buff_file$ For Binary As #1 + x$ = Space$(LOF(1)) + Get #1, , x$ + Close #1 + Kill buff_file$ + listDir$ = x$ END FUNCTION 'listXDir$ - Give more detailed listing output than listDir$ ' spec$ - Directory spec. Pass "" to get current directory FUNCTION listXDir$ (spec$) - spec$ = "-l " + spec$ - listXDir$ = listDir$(spec$) + spec$ = "-l " + spec$ + listXDir$ = listDir$(spec$) END FUNCTION 'resolvePath$ - Expands ~ prefix to $HOME. Useful before doing filesystem operations -' RPATH$ - File-system path string +' RPATH$ - File-system path string FUNCTION resolvePath$(RPATH$) If RPATH$ = "~" Then RPATH$ = Environ$("HOME") + "/" @@ -35,3 +35,20 @@ FUNCTION resolvePath$(RPATH$) End If resolvePath = RPATH$ END FUNCTION + +'Subprocess$ - Run another process and return stdout +' cmd$ - Subprocess command to run on host +FUNCTION Subprocess$(cmd$) + rndbuf = Rnd * 999999 + buff_file$ = "/tmp/buff_qbsh_" + LTrim$(Str$(rndbuf)) + If _FileExists(buff_file$) Then + Kill buff_file$ + End If + Shell "SHELL='" + SELFPATH$ + "'; " + cmd$ + " | tee /dev/stderr > " + buff_file$ + Open buff_file$ For Binary As #1 + x$ = Space$(LOF(1)) + Get #1, , x$ + Close #1 + Kill buff_file$ + Subprocess$ = x$ +END FUNCTION diff --git a/qbsh.bas b/qbsh.bas index 5389c4e..5b9e5f4 100644 --- a/qbsh.bas +++ b/qbsh.bas @@ -145,18 +145,7 @@ Return 'Offload unhandled call to legacy system shell. qbsh is the only shell of the future. CMDOUT: -rndbuf = Rnd * 999999 -buff_file$ = "/tmp/buff_qbsh_" + LTrim$(Str$(rndbuf)) -If _FileExists(buff_file$) Then - Kill buff_file$ -End If -Shell "SHELL='" + SELFPATH$ + "'; " + cmd$ + " 2>&1 >" + buff_file$ -Open buff_file$ For Binary As #1 -x$ = Space$(LOF(1)) -Get #1, , x$ -Close #1 -Print x$ -Kill buff_file$ +stdOut$ = Subprocess$(cmd$) Return 'Delete a file path