Skip to content

Commit

Permalink
Merge pull request #15 from vwbusguy/feature/improve-subshell-piping
Browse files Browse the repository at this point in the history
Feature/improve subshell piping
  • Loading branch information
vwbusguy committed Mar 10, 2024
2 parents b029027 + 850f554 commit 7fcfd0c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 27 deletions.
47 changes: 32 additions & 15 deletions lib/io.bas
Original file line number Diff line number Diff line change
Expand Up @@ -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") + "/"
Expand All @@ -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
13 changes: 1 addition & 12 deletions qbsh.bas
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7fcfd0c

Please sign in to comment.