Skip to content

Commit dfaa461

Browse files
committed
support input through process substitution, #925
1 parent b4cdd3d commit dfaa461

File tree

2 files changed

+30
-14
lines changed

2 files changed

+30
-14
lines changed

Unix/cloc

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
# <http://www.gnu.org/licenses/gpl.txt>.
3030
#
3131
# 1}}}
32-
my $VERSION = "2.06"; # odd number == beta; even number == stable
32+
my $VERSION = "2.07"; # odd number == beta; even number == stable
3333
my $URL = "github.com/AlDanial/cloc"; # 'https://' pushes header too wide
3434
require 5.10.0;
3535
# use modules {{{1
@@ -393,7 +393,7 @@ my $getopt_success = GetOptions( # {{{1
393393
"ksep|thousands-delimiter=s" => \$opt_thousands_delimiter ,
394394
);
395395
$opt_txt = 0;
396-
my $opt_fmt_tempfile = "";
396+
my $tmp_file_to_delete = "";
397397
# 1}}}
398398
$config_file = $opt_config_file if defined $opt_config_file;
399399
load_from_config_file($config_file, # {{{2
@@ -6098,6 +6098,12 @@ sub make_file_list { # {{{1
60986098
push @file_list, "$file_or_dir";
60996099
} elsif (is_dir($file_or_dir)) {
61006100
push @dir_list, $file_or_dir;
6101+
} elsif (-p $file_or_dir) {
6102+
# a pipe via process substitution, eg cloc <(cat cloc)
6103+
my @lines = <>;
6104+
$tmp_file_to_delete = "temp_process_substitution_$$";
6105+
write_file($tmp_file_to_delete, {}, @lines);
6106+
push @dir_list, $tmp_file_to_delete;
61016107
} else {
61026108
push @{$raa_errors}, [$rh_Err->{'Neither file nor directory'} , $F];
61036109
$rh_ignored->{$F} = 'not file, not directory';
@@ -7350,7 +7356,7 @@ sub write_file { # {{{1
73507356
# filename starts with "SCALAR" followed by a memory address in hex.
73517357
# This temporary file will need to be cleaned up before the program ends.
73527358
if ($ON_WINDOWS and (ref($file) eq "SCALAR" or $file =~ /^SCALAR/)) {
7353-
$opt_fmt_tempfile = "$file"; # string version of the file name
7359+
$tmp_file_to_delete = "$file"; # string version of the file name
73547360
}
73557361
if ($opt_file_encoding) {
73567362
$OUT = open_file(">:encoding($opt_file_encoding)", $file, 0);
@@ -15818,10 +15824,12 @@ sub really_is_php { # {{{1
1581815824
return $is_php;
1581915825
} # 1}}}
1582015826
END { # {{{1
15821-
# Remove temp file on Windows if running with --fmt
15822-
if ($ON_WINDOWS and $opt_fmt and $opt_fmt_tempfile) {
15823-
print "-> END unlink $opt_fmt_tempfile\n" if $opt_v > 2;
15824-
unlink($opt_fmt_tempfile)
15827+
# Temp files might have been created
15828+
# - on Windows if running with --fmt
15829+
# - on POSIX if using process substitution, eg, cloc <(cat cloc)
15830+
if ($tmp_file_to_delete) {
15831+
print "-> END unlink $tmp_file_to_delete\n" if $opt_v > 2;
15832+
unlink($tmp_file_to_delete)
1582515833
}
1582615834
} # 1}}}
1582715835
__END__

cloc

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
# <http://www.gnu.org/licenses/gpl.txt>.
3030
#
3131
# 1}}}
32-
my $VERSION = "2.06"; # odd number == beta; even number == stable
32+
my $VERSION = "2.07"; # odd number == beta; even number == stable
3333
my $URL = "github.com/AlDanial/cloc"; # 'https://' pushes header too wide
3434
require 5.10.0;
3535
# use modules {{{1
@@ -396,7 +396,7 @@ my $getopt_success = GetOptions( # {{{1
396396
"ksep|thousands-delimiter=s" => \$opt_thousands_delimiter ,
397397
);
398398
$opt_txt = 0;
399-
my $opt_fmt_tempfile = "";
399+
my $tmp_file_to_delete = "";
400400
# 1}}}
401401
$config_file = $opt_config_file if defined $opt_config_file;
402402
load_from_config_file($config_file, # {{{2
@@ -6090,6 +6090,12 @@ sub make_file_list { # {{{1
60906090
push @file_list, "$file_or_dir";
60916091
} elsif (is_dir($file_or_dir)) {
60926092
push @dir_list, $file_or_dir;
6093+
} elsif (-p $file_or_dir) {
6094+
# a pipe via process substitution, eg cloc <(cat cloc)
6095+
my @lines = <>;
6096+
$tmp_file_to_delete = "temp_process_substitution_$$";
6097+
write_file($tmp_file_to_delete, {}, @lines);
6098+
push @dir_list, $tmp_file_to_delete;
60936099
} else {
60946100
push @{$raa_errors}, [$rh_Err->{'Neither file nor directory'} , $F];
60956101
$rh_ignored->{$F} = 'not file, not directory';
@@ -7342,7 +7348,7 @@ sub write_file { # {{{1
73427348
# filename starts with "SCALAR" followed by a memory address in hex.
73437349
# This temporary file will need to be cleaned up before the program ends.
73447350
if ($ON_WINDOWS and (ref($file) eq "SCALAR" or $file =~ /^SCALAR/)) {
7345-
$opt_fmt_tempfile = "$file"; # string version of the file name
7351+
$tmp_file_to_delete = "$file"; # string version of the file name
73467352
}
73477353
if ($opt_file_encoding) {
73487354
$OUT = open_file(">:encoding($opt_file_encoding)", $file, 0);
@@ -19182,10 +19188,12 @@ EOAlgDiff
1918219188
print "<- Install_Algorithm_Diff\n" if $opt_v > 2;
1918319189
} # 1}}}
1918419190
END { # {{{1
19185-
# Remove temp file on Windows if running with --fmt
19186-
if ($ON_WINDOWS and $opt_fmt and $opt_fmt_tempfile) {
19187-
print "-> END unlink $opt_fmt_tempfile\n" if $opt_v > 2;
19188-
unlink($opt_fmt_tempfile)
19191+
# Temp files might have been created
19192+
# - on Windows if running with --fmt
19193+
# - on POSIX if using process substitution, eg, cloc <(cat cloc)
19194+
if ($tmp_file_to_delete) {
19195+
print "-> END unlink $tmp_file_to_delete\n" if $opt_v > 2;
19196+
unlink($tmp_file_to_delete)
1918919197
}
1919019198
} # 1}}}
1919119199
__END__

0 commit comments

Comments
 (0)