1#!/usr/bin/perl
2# exercise the 'invalid option' handling code in each program
3
4# Copyright (C) 2008-2023 Free Software Foundation, Inc.
5
6# This program is free software: you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation, either version 3 of the License, or
9# (at your option) any later version.
10
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15
16# You should have received a copy of the GNU General Public License
17# along with this program.  If not, see <https://www.gnu.org/licenses/>.
18
19require 5.003;
20use strict;
21
22# Turn off localization of executable's output.
23@ENV{qw(LANGUAGE LANG LC_ALL)} = ('C') x 3;
24
25my %exit_status =
26  (
27    ls => 2,
28    dir => 2,
29    vdir => 2,
30    test => 2,
31    chroot => 125,
32    echo => 0,
33    env => 125,
34    expr => 0,
35    nice => 125,
36    nohup => 125,
37    runcon => 125,
38    sort => 2,
39    stdbuf => 125,
40    test => 0,
41    timeout => 125,
42    true => 0,
43    tty => 2,
44    printf => 0,
45    printenv => 2,
46  );
47
48my %expected_out =
49  (
50    echo => "-/\n",
51    expr => "-/\n",
52    printf => "-/",
53  );
54
55my %expected_err =
56  (
57    false => '',
58    stty => '',
59  );
60
61my $fail = 0;
62my @built_programs = split ' ', $ENV{built_programs};
63foreach my $prog (@built_programs)
64  {
65    $prog eq '['
66      and next;
67
68    my $try = "Try '$prog --help' for more information.\n";
69    my $x = $exit_status{$prog};
70    defined $x
71      or $x = 1;
72
73    my $out = $expected_out{$prog};
74    defined $out
75      or $out = '';
76
77    my $err = $expected_err{$prog};
78    defined $err
79      or $err = $x == 0 ? '' : "$prog: invalid option -- /\n$try";
80
81    # Accommodate different syntax in glibc's getopt
82    # diagnostics by filtering out single quotes.
83    # Also accommodate BSD getopt.
84    my $err_subst = "s,'/',/,; s,unknown,invalid,";
85
86    # Depending on how this script is run, stty emits different
87    # diagnostics.  Don't bother checking them.
88    $prog eq 'stty'
89      and $err_subst = 's/(.|\n)*//ms';
90
91    my @Tests = (["$prog-invalid-opt", '-/', {OUT=>$out},
92                  {ERR_SUBST => $err_subst},
93                  {EXIT=>$x}, {ERR=>$err}]);
94
95    my $save_temps = $ENV{DEBUG};
96    my $verbose = $ENV{VERBOSE};
97
98    my $f = run_tests ($prog, \$prog, \@Tests, $save_temps, $verbose);
99    $f
100      and $fail = 1;
101  }
102
103exit $fail;
104