Skip to content

Commit 8f22def

Browse files
author
Kurtis Rader
committed
return to psub --file being the default
The recent change to switch `psub` to use `argparse` caused it to use a fifo by default because it inadvertently fixed a long standing bug in the fish script. This changes the behavior back to `psub --file` being the default behavior and introduces a `--fifo` flag. It also updates the documentation to make it clearer when and why `--fifo` mode should not be used. Fixes #4222
1 parent 3e226f0 commit 8f22def

File tree

7 files changed

+75
-51
lines changed

7 files changed

+75
-51
lines changed

doc_src/psub.txt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,27 @@
22

33
\subsection psub-synopsis Synopsis
44
\fish{synopsis}
5-
COMMAND1 ( COMMAND2 | psub [-f] [-s SUFFIX])
5+
COMMAND1 ( COMMAND2 | psub [-F | --fifo] [-f | --file] [-s SUFFIX])
66
\endfish
77

88
\subsection psub-description Description
99

10-
Posix shells feature a syntax that is a mix between command substitution and piping, called process substitution. It is used to send the output of a command into the calling command, much like command substitution, but with the difference that the output is not sent through commandline arguments but through a named pipe, with the filename of the named pipe sent as an argument to the calling program. `psub` combined with a regular command substitution provides the same functionality.
10+
Some shells (e.g., ksh, bash) feature a syntax that is a mix between command substitution and piping, called process substitution. It is used to send the output of a command into the calling command, much like command substitution, but with the difference that the output is not sent through commandline arguments but through a named pipe, with the filename of the named pipe sent as an argument to the calling program. `psub` combined with a regular command substitution provides the same functionality.
1111

12-
If the `-f` or `--file` switch is given to `psub`, `psub` will use a regular file instead of a named pipe to communicate with the calling process. This will cause `psub` to be significantly slower when large amounts of data are involved, but has the advantage that the reading process can seek in the stream.
12+
The following options are available:
1313

14-
If the `-s` or `---suffix` switch is given, `psub` will append SUFFIX to the filename.
14+
- `-f` or `--file` will cause psub to use a regular file instead of a named pipe to communicate with the calling process. This will cause `psub` to be significantly slower when large amounts of data are involved, but has the advantage that the reading process can seek in the stream. This is the default.
1515

16+
- `-F` or `--fifo` will cause psub to use a named pipe rather than a file. You should only use this if the command produces no more than 8 KiB of output. The limit on the amount of data a FIFO can buffer varies with the OS but is typically 8 KiB, 16 KiB or 64 KiB. If you use this option and the command on the left of the psub pipeline produces more output a deadlock is likely to occur.
17+
18+
- `-s` or `--suffix` will append SUFFIX to the filename.
1619

1720
\subsection psub-example Example
1821

1922
\fish
2023
diff (sort a.txt | psub) (sort b.txt | psub)
2124
# shows the difference between the sorted versions of files `a.txt` and `b.txt`.
2225

23-
source-highlight -f esc (cpp main.c | psub -s .c)
26+
source-highlight -f esc (cpp main.c | psub -f -s .c)
2427
# highlights `main.c` after preprocessing as a C source.
2528
\endfish

share/functions/psub.fish

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
function psub --description "Read from stdin into a file and output the filename. Remove the file when the command that called psub exits."
2-
set -l options 'h/help' 'f/file' 's/suffix='
2+
set -l options -x 'f,F' -x 'F,s' 'h/help' 'f/file' 'F/fifo' 's/suffix=' 'T-testing'
33
argparse -n psub --max-args=0 $options -- $argv
44
or return
55

@@ -21,10 +21,10 @@ function psub --description "Read from stdin into a file and output the filename
2121
set -q TMPDIR
2222
and set tmpdir $TMPDIR
2323

24-
if not set -q _flag_file
24+
if set -q _flag_fifo
2525
# Write output to pipe. This needs to be done in the background so
2626
# that the command substitution exits without needing to wait for
27-
# all the commands to exit
27+
# all the commands to exit.
2828
set dirname (mktemp -d $tmpdir/.psub.XXXXXXXXXX)
2929
or return
3030
set filename $dirname/psub.fifo"$_flag_suffix"
@@ -35,13 +35,18 @@ function psub --description "Read from stdin into a file and output the filename
3535
cat >$filename
3636
else
3737
set dirname (mktemp -d $tmpdir/.psub.XXXXXXXXXX)
38-
set filename $dirname/psub"$_flag_suffix"
38+
set filename "$dirname/psub$_flag_suffix"
3939
cat >$filename
4040
end
4141

4242
# Write filename to stdout
4343
echo $filename
4444

45+
# This flag isn't documented. It's strictly for our unit tests.
46+
if set -q _flag_testing
47+
return
48+
end
49+
4550
# Find unique function name
4651
while true
4752
set funcname __fish_psub_(random)
@@ -53,7 +58,7 @@ function psub --description "Read from stdin into a file and output the filename
5358
# Make sure we erase file when caller exits
5459
function $funcname --on-job-exit caller --inherit-variable filename --inherit-variable dirname --inherit-variable funcname
5560
command rm $filename
56-
if count $dirname >/dev/null
61+
if test -n "$dirname"
5762
command rmdir $dirname
5863
end
5964
functions -e $funcname

tests/psub.err

Whitespace-only changes.

tests/psub.in

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Test psub behavior
2+
set -l filename (echo foo | psub --testing)
3+
test -f $filename
4+
or echo 'psub is not a regular file' >&2
5+
rm $filename
6+
7+
set -l filename (echo foo | psub --testing --file)
8+
test -f $filename
9+
or echo 'psub is not a regular file' >&2
10+
rm $filename
11+
12+
set -l filename (echo foo | psub --testing --fifo)
13+
test -p $filename
14+
or echo 'psub is not a fifo' >&2
15+
rm $filename
16+
17+
cat (echo foo | psub)
18+
cat (echo bar | psub --fifo)
19+
cat (echo baz | psub)
20+
21+
set -l filename (echo foo | psub)
22+
if test -e $filename
23+
echo 'psub file was not deleted'
24+
else
25+
echo 'psub file was deleted'
26+
end
27+
28+
# The --file flag is the default behavior.
29+
if count (echo foo | psub -s .cc | grep -o '\.cc$') >/dev/null
30+
echo 'psub filename ends with .cc'
31+
else
32+
echo 'psub filename does not end with .cc'
33+
end
34+
35+
# Make sure we get the same result if we explicitly ask for a temp file.
36+
if count (echo foo | psub -f -s .cc | grep -o '\.cc$') >/dev/null
37+
echo 'psub filename ends with .cc'
38+
else
39+
echo 'psub filename does not end with .cc'
40+
end
41+
42+
set -l filename (echo foo | psub -s .fish)
43+
if test -e (dirname $filename)
44+
echo 'psub directory was not deleted'
45+
else
46+
echo 'psub directory was deleted'
47+
end
48+
49+
diff -q (__fish_print_help psub | psub) (psub -hs banana | psub)

tests/psub.out

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
foo
2+
bar
3+
baz
4+
psub file was deleted
5+
psub filename ends with .cc
6+
psub filename ends with .cc
7+
psub directory was deleted

tests/test9.in

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ cat ../test/temp/file_truncation_test.txt
77
echo -n > ../test/temp/file_truncation_test.txt
88
cat ../test/temp/file_truncation_test.txt
99

10-
# Test events.
10+
# Test events.
1111

1212

1313
# This pattern caused a crash; github issue #449
@@ -85,39 +85,6 @@ eval 'status -n
8585
status -n
8686
status -n'
8787

88-
# Test psub
89-
cat (echo foo | psub)
90-
cat (echo bar | psub)
91-
cat (echo baz | psub)
92-
93-
set -l filename (echo foo | psub)
94-
if test -e $filename
95-
echo 'psub file was not deleted'
96-
else
97-
echo 'psub file was deleted'
98-
end
99-
100-
if count (echo foo | psub -s .cc | grep -o '\.cc$') >/dev/null
101-
echo 'psub filename ends with .cc'
102-
else
103-
echo 'psub filename does not end with .cc'
104-
end
105-
106-
if count (echo foo | psub -f -s .cc | grep -o '\.cc$') >/dev/null
107-
echo 'psub filename ends with .cc'
108-
else
109-
echo 'psub filename does not end with .cc'
110-
end
111-
112-
set -l filename (echo foo | psub -s .fish)
113-
if test -e (dirname $filename)
114-
echo 'psub directory was not deleted'
115-
else
116-
echo 'psub directory was deleted'
117-
end
118-
119-
diff -q (__fish_print_help psub | psub) (psub -hs banana | psub)
120-
12188
# Test support for unbalanced blocks
12289
function try_unbalanced_block
12390
../test/root/bin/fish -c "echo $argv | source " 2>&1 | grep "Missing end" 1>&2

tests/test9.out

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,6 @@ Testing for loop
1313
1
1414
2
1515
3
16-
foo
17-
bar
18-
baz
19-
psub file was deleted
20-
psub filename ends with .cc
21-
psub filename ends with .cc
22-
psub directory was deleted
2316
bom_test
2417
not#a#comment
2518
is

0 commit comments

Comments
 (0)