xref: /openbsd-src/gnu/usr.bin/perl/t/op/exec.t (revision 56d68f1e19ff848c889ecfa71d3a06340ff64892)
1#!./perl
2
3BEGIN {
4    chdir 't' if -d 't';
5    require './test.pl';
6    set_up_inc('../lib');
7}
8
9my $vms_exit_mode = 0;
10
11if ($^O eq 'VMS') {
12    if (eval 'require VMS::Feature') {
13        $vms_exit_mode = !(VMS::Feature::current("posix_exit"));
14    } else {
15        my $env_unix_rpt = $ENV{'DECC$FILENAME_UNIX_REPORT'} || '';
16        my $env_posix_ex = $ENV{'PERL_VMS_POSIX_EXIT'} || '';
17        my $unix_rpt = $env_unix_rpt =~ /^[ET1]/i;
18        my $posix_ex = $env_posix_ex =~ /^[ET1]/i;
19        if (($unix_rpt || $posix_ex) ) {
20            $vms_exit_mode = 0;
21        } else {
22            $vms_exit_mode = 1;
23        }
24    }
25}
26
27
28# suppress VMS whinging about bad execs.
29use vmsish qw(hushed);
30
31$| = 1;				# flush stdout
32
33$ENV{LC_ALL}   = 'C';		# Force English error messages.
34$ENV{LANGUAGE} = 'C';		# Ditto in GNU.
35
36my $Is_VMS   = $^O eq 'VMS';
37my $Is_Win32 = $^O eq 'MSWin32';
38
39plan(tests => 41);
40
41my $Perl = which_perl();
42
43my $exit;
44SKIP: {
45    skip("bug/feature of pdksh", 2) if $^O eq 'os2';
46
47    my $tnum = curr_test();
48    $exit = system qq{$Perl -le "print q{ok $tnum - interp system(EXPR)"}};
49    next_test();
50    is( $exit, 0, '  exited 0' );
51}
52
53my $tnum = curr_test();
54$exit = system qq{$Perl -le "print q{ok $tnum - split & direct system(EXPR)"}};
55next_test();
56is( $exit, 0, '  exited 0' );
57
58# On VMS and Win32 you need the quotes around the program or it won't work.
59# On Unix its the opposite.
60my $quote = $Is_VMS || $Is_Win32 ? '"' : '';
61$tnum = curr_test();
62$exit = system $Perl, '-le',
63               "${quote}print q{ok $tnum - system(PROG, LIST)}${quote}";
64next_test();
65is( $exit, 0, '  exited 0' );
66
67
68# Some basic piped commands.  Some OS's have trouble with "helpfully"
69# putting newlines on the end of piped output.  So we split this into
70# newline insensitive and newline sensitive tests.
71my $echo_out = `$Perl -e "print 'ok'" | $Perl -le "print <STDIN>"`;
72$echo_out =~ s/\n\n/\n/g;
73is( $echo_out, "ok\n", 'piped echo emulation');
74
75{
76    # here we check if extra newlines are going to be slapped on
77    # piped output.
78    local $TODO = 'VMS sticks newlines on everything' if $Is_VMS;
79
80    is( scalar `$Perl -e "print 'ok'"`,
81        "ok", 'no extra newlines on ``' );
82
83    is( scalar `$Perl -e "print 'ok'" | $Perl -e "print <STDIN>"`,
84        "ok", 'no extra newlines on pipes');
85
86    is( scalar `$Perl -le "print 'ok'" | $Perl -le "print <STDIN>"`,
87        "ok\n\n", 'doubled up newlines');
88
89    is( scalar `$Perl -e "print 'ok'" | $Perl -le "print <STDIN>"`,
90        "ok\n", 'extra newlines on inside pipes');
91
92    is( scalar `$Perl -le "print 'ok'" | $Perl -e "print <STDIN>"`,
93        "ok\n", 'extra newlines on outgoing pipes');
94
95    {
96	local($/) = \2;
97	$out = runperl(prog => 'print q{1234}');
98	is($out, "1234", 'ignore $/ when capturing output in scalar context');
99    }
100}
101
102
103is( system(qq{$Perl -e "exit 0"}), 0,     'Explicit exit of 0' );
104
105my $exit_one = $vms_exit_mode ? 4 << 8 : 1 << 8;
106is( system(qq{$Perl "-I../lib" -e "use vmsish qw(hushed); exit 1"}), $exit_one,
107    'Explicit exit of 1' );
108
109$rc = system { "lskdfj" } "lskdfj";
110unless( ok($rc == 255 << 8 or $rc == -1 or $rc == 256 or $rc == 512) ) {
111    print "# \$rc == $rc\n";
112}
113
114unless ( ok( $! == 2  or  $! =~ /\bno\b.*\bfile/i or
115             $! == 13 or  $! =~ /permission denied/i or
116             $! == 20 or  $! =~ /not a directory/i or   # If PATH component is
117                                                        # a non-directory
118             $! == 22 or  $! =~ /invalid argument/i  ) ) {
119    diag sprintf "\$! eq %d, '%s'\n", $!, $!;
120}
121
122
123is( `$Perl -le "print 'ok'"`,   "ok\n",     'basic ``' );
124is( <<`END`,                    "ok\n",     '<<`HEREDOC`' );
125$Perl -le "print 'ok'"
126END
127
128is( <<~`END`,                   "ok\n",     '<<~`HEREDOC`' );
129  $Perl -le "print 'ok'"
130  END
131
132{
133    sub rpecho { qq($Perl -le "print '$_[0]'") }
134    is scalar(readpipe(rpecho("b"))), "b\n",
135	"readpipe with one argument in scalar context";
136    is join(",", "a", readpipe(rpecho("b")), "c"), "a,b\n,c",
137	"readpipe with one argument in list context";
138    local $_ = rpecho("f");
139    is scalar(readpipe), "f\n",
140	"readpipe default argument in scalar context";
141    is join(",", "a", readpipe, "c"), "a,f\n,c",
142	"readpipe default argument in list context";
143    sub rpechocxt {
144	rpecho(wantarray ? "list" : defined(wantarray) ? "scalar" : "void");
145    }
146    is scalar(readpipe(rpechocxt())), "scalar\n",
147	"readpipe argument context in scalar context";
148    is join(",", "a", readpipe(rpechocxt()), "b"), "a,scalar\n,b",
149	"readpipe argument context in list context";
150    foreach my $args ("(\$::p,\$::q)", "((\$::p,\$::q))") {
151	foreach my $lvalue ("my \$r", "my \@r") {
152	    eval("$lvalue = readpipe$args if 0");
153	    like $@, qr/\AToo many arguments for /;
154	}
155    }
156}
157
158package o {
159    use subs "readpipe";
160    sub readpipe { pop }
161    ::is `${\"hello"}`, 'hello',
162         'overridden `` interpolates [perl #115330]';
163    ::is <<`119827`, "ls\n",
164l${\"s"}
165119827
166        '<<`` respects overrides and interpolates [perl #119827]';
167}
168
169TODO: {
170    my $tnum = curr_test();
171    if( $^O =~ /Win32/ ) {
172        print "not ok $tnum - exec failure doesn't terminate process " .
173              "# TODO Win32 exec failure waits for user input\n";
174        next_test();
175        last TODO;
176    }
177
178    ok( !exec("lskdjfalksdjfdjfkls"),
179        "exec failure doesn't terminate process");
180}
181
182{
183    local $! = 0;
184    ok !exec(), 'empty exec LIST fails';
185    ok $! == 2 || $! =~ qr/\bno\b.*\bfile\b/i, 'errno = ENOENT'
186        or diag sprintf "\$! eq %d, '%s'\n", $!, $!;
187
188}
189{
190    local $! = 0;
191    my $err = $!;
192    ok !(exec {""} ()), 'empty exec PROGRAM LIST fails';
193    ok $! == 2 || $! =~ qr/\bno\b.*\bfile\b/i, 'errno = ENOENT'
194        or diag sprintf "\$! eq %d, '%s'\n", $!, $!;
195}
196
197package CountRead {
198    sub TIESCALAR { bless({ n => 0 }, $_[0]) }
199    sub FETCH { ++$_[0]->{n} }
200}
201my $cr;
202tie $cr, "CountRead";
203my $exit_statement = "exit(\$ARGV[0] eq '1' ? 0 : 1)";
204$exit_statement = qq/"$exit_statement"/ if $^O eq 'VMS';
205is system($^X, "-e", $exit_statement, $cr), 0,
206    "system args have magic processed exactly once";
207is tied($cr)->{n}, 1, "system args have magic processed before fork";
208
209$exit_statement = "exit(\$ARGV[0] eq \$ARGV[1] ? 0 : 1)";
210$exit_statement = qq/"$exit_statement"/ if $^O eq 'VMS';
211is system($^X, "-e", $exit_statement, "$$", $$), 0,
212    "system args have magic processed before fork";
213
214my $test = curr_test();
215exec $Perl, '-le', qq{${quote}print 'ok $test - exec PROG, LIST'${quote}};
216fail("This should never be reached if the exec() worked");
217