This file is indexed.

/usr/share/lintian/checks/scripts is in lintian 2.5.10.4.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
# scripts -- lintian check script -*- perl -*-
#
# This is probably the right file to add a check for the use of
# set -e in bash and sh scripts.
#
# Copyright (C) 1998 Richard Braakman
# Copyright (C) 2002 Josip Rodin
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, you can find it on the World Wide
# Web at http://www.gnu.org/copyleft/gpl.html, or write to the Free
# Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
# MA 02110-1301, USA.

package Lintian::scripts;
use strict;
use warnings;

use Lintian::Check qw($known_shells_regex);
use Lintian::Data;
use Lintian::Relation;
use Lintian::Tags qw(tag);
use Lintian::Util qw(fail);

# This is a map of all known interpreters.  The key is the interpreter
# name (the binary invoked on the #! line).  The value is an anonymous
# array of two elements.  The first argument is the path on a Debian
# system where that interpreter would be installed.  The second
# argument is the dependency that provides that interpreter.
#
# $INTERPRETERS maps names of (unversioned) interpreters to the path
# they are installed and what package to depend on to use them.
#
my $INTERPRETERS = Lintian::Data->new ('scripts/interpreters', qr/\s*=\>\s*/o,
                                       \&_parse_interpreters);

# The more complex case of interpreters that may have a version number.
#
# This is a hash from the base interpreter name to a list.  The base
# interpreter name may appear by itself or followed by some combination of
# dashes, digits, and periods.
#
# The list contains the following values:
#  [<path>, <dependency-relation>, <regex>, <dependency-templatel>, <version-list>]
#
# Their meaning is documented in Lintian's scripts/versioned-interpreters
# file, though they are ordered differently and there are a few differences
# as described below:
#
# * <regex> has been passed through qr/^<value>$/
# * If <dependency-relation> was left out, it has been substituted by the
#   interpreter.
# * The magic values of <dependency-relation> are represented as:
#   @NO_DEFAULT_DEPS@  -> '' (i.e. an empty string)
#   @SKIP_UNVERSIONED@ ->  undef (i.e the undefined value)
# * <version-list> has been split into a list of versions.
#   (e.g. "1.6 1.8" will be ["1.6", "1.8"])
#
# A full example is:
#
#    data:
#        lua => /usr/bin, lua([\d.]+), 'lua$1', 40 50 5.1
#
#    $VERSIONED_INTERPRETERS->value ('lua') is
#       [ '/usr/bin', 'lua', qr/^lua([\d.]+)$/, 'lua$1', ["40", "50", "5.1"] ]
#
my $VERSIONED_INTERPRETERS = Lintian::Data->new ('scripts/versioned-interpreters', qr/\s*=\>\s*/o,
                                                 \&_parse_versioned_interpreters);

# Any of the following packages can satisfy an update-inetd dependency.
my $update_inetd
    = join (' | ', qw(update-inetd inet-superserver openbsd-inetd
                      inetutils-inetd rlinetd xinetd));

# Appearance of one of these regexes in a maintainer script means that there
# must be a dependency (or pre-dependency) on the given package.  The tag
# reported is maintainer-script-needs-depends-on-%s, so be sure to update
# scripts.desc when adding a new rule.
my @depends_needed = (
        [ adduser       => '\badduser\s'           ],
        [ gconf2        => '\bgconf-schemas\s'     ],
        [ $update_inetd => '\bupdate-inetd\s'      ],
        [ ucf           => '\bucf\s'               ],
        [ 'xml-core'    => '\bupdate-xmlcatalog\s' ],
);

# When detecting commands inside shell scripts, use this regex to match the
# beginning of the command rather than checking whether the command is at the
# beginning of a line.
my $LEADIN = qr'(?:(?:^|[`&;(|{])\s*|(?:if|then|do|while)\s+)';

my @bashism_single_quote_regexs = (
    $LEADIN . qr'echo\s+(?:-[^e\s]+\s+)?\'[^\']*(\\[abcEfnrtv0])+.*?[\']',
        # unsafe echo with backslashes
    $LEADIN . qr'source\s+[\"\']?(?:\.\/|\/|\$|[\w~.-])\S*',
        # should be '.', not 'source'
);
my @bashism_string_regexs = (
    qr'\$\[\w+\]',               # arith not allowed
    qr'\$\{\w+\:\d+(?::\d+)?\}',   # ${foo:3[:1]}
    qr'\$\{\w+(/.+?){1,2}\}',    # ${parm/?/pat[/str]}
    qr'\$\{\#?\w+\[[0-9\*\@]+\]\}',# bash arrays, ${name[0|*|@]}
    qr'\$\{!\w+[\@*]\}',                 # ${!prefix[*|@]}
    qr'\$\{!\w+\}',              # ${!name}
    qr'(\$\(|\`)\s*\<\s*\S+\s*(\)|\`)', # $(\< foo) should be $(cat foo)
    qr'\$\{?RANDOM\}?\b',                # $RANDOM
    qr'\$\{?(OS|MACH)TYPE\}?\b',   # $(OS|MACH)TYPE
    qr'\$\{?HOST(TYPE|NAME)\}?\b', # $HOST(TYPE|NAME)
    qr'\$\{?DIRSTACK\}?\b',        # $DIRSTACK
    qr'\$\{?EUID\}?\b',            # $EUID should be "id -u"
    qr'\$\{?UID\}?\b',           # $UID should be "id -ru"
    qr'\$\{?SECONDS\}?\b',       # $SECONDS
    qr'\$\{?BASH_[A-Z]+\}?\b',     # $BASH_SOMETHING
    qr'\$\{?SHELLOPTS\}?\b',       # $SHELLOPTS
    qr'\$\{?PIPESTATUS\}?\b',      # $PIPESTATUS
    qr'\$\{?SHLVL\}?\b',                 # $SHLVL
    qr'<<<',                       # <<< here string
    $LEADIN . qr'echo\s+(?:-[^e\s]+\s+)?\"[^\"]*(\\[abcEfnrtv0])+.*?[\"]',
        # unsafe echo with backslashes
);
my @bashism_regexs = (
    qr'(?:^|\s+)function \w+(\s|\(|\Z)',  # function is useless
    qr'(test|-o|-a)\s*[^\s]+\s+==\s', # should be 'b = a'
    qr'\[\s+[^\]]+\s+==\s',        # should be 'b = a'
    qr'\s(\|\&)',                        # pipelining is not POSIX
    qr'[^\\\$]\{(?:[^\s\\\}]*?,)+[^\\\}\s]*\}', # brace expansion
    qr'(?:^|\s+)\w+\[\d+\]=',      # bash arrays, H[0]
    $LEADIN . qr'read\s+(?:-[a-qs-zA-Z\d-]+)',
        # read with option other than -r
    $LEADIN . qr'read\s*(?:-\w+\s*)*(?:\".*?\"|[\'].*?[\'])?\s*(?:;|$)',
        # read without variable
    qr'\&>',                     # cshism
    qr'(<\&|>\&)\s*((-|\d+)[^\s;|)`&\\\\]|[^-\d\s]+)', # should be >word 2>&1
    qr'\[\[(?!:)',               # alternative test command
    $LEADIN . qr'select\s+\w+',    # 'select' is not POSIX
    $LEADIN . qr'echo\s+(-n\s+)?-n?en?',  # echo -e
    $LEADIN . qr'exec\s+-[acl]',   # exec -c/-l/-a name
    qr'(?:^|\s+)let\s',          # let ...
    qr'(?<![\$\(])\(\(.*\)\)',     # '((' should be '$(('
    qr'\$\[[^][]+\]',            # '$[' should be '$(('
    qr'(\[|test)\s+-a',          # test with unary -a (should be -e)
    qr'/dev/(tcp|udp)',          # /dev/(tcp|udp)
    $LEADIN . qr'\w+\+=',                # should be "VAR="${VAR}foo"
    $LEADIN . qr'suspend\s',
    $LEADIN . qr'caller\s',
    $LEADIN . qr'complete\s',
    $LEADIN . qr'compgen\s',
    $LEADIN . qr'declare\s',
    $LEADIN . qr'typeset\s',
    $LEADIN . qr'disown\s',
    $LEADIN . qr'builtin\s',
    $LEADIN . qr'set\s+-[BHT]+',   # set -[BHT]
    $LEADIN . qr'alias\s+-p',      # alias -p
    $LEADIN . qr'unalias\s+-a',    # unalias -a
    $LEADIN . qr'local\s+-[a-zA-Z]+', # local -opt
    qr'(?:^|\s+)\s*\(?\w*[^\(\w\s]+\S*?\s*\(\)\s*([\{|\(]|\Z)',
        # function names should only contain [a-z0-9_]
    $LEADIN . qr'(push|pop)d(\s|\Z)',   # (push|pod)d
    $LEADIN . qr'export\s+-[^p]',  # export only takes -p as an option
    $LEADIN . qr'ulimit(\s|\Z)',
    $LEADIN . qr'shopt(\s|\Z)',
    $LEADIN . qr'type\s',
    $LEADIN . qr'time\s',
    $LEADIN . qr'dirs(\s|\Z)',
    qr'(?:^|\s+)[<>]\(.*?\)',      # <() process substituion
    qr'(?:^|\s+)readonly\s+-[af]', # readonly -[af]
    $LEADIN . qr'(sh|\$\{?SHELL\}?) -[rD]', # sh -[rD]
    $LEADIN . qr'(sh|\$\{?SHELL\}?) --\w+', # sh --long-option
    $LEADIN . qr'(sh|\$\{?SHELL\}?) [-+]O', # sh [-+]O
);

# a local function to help use separate tags for example scripts
sub script_tag {
    my( $tag, $filename, @rest ) = @_;

    $tag = "example-$tag"
        if $filename and $filename =~ m,usr/share/doc/[^/]+/examples/,;

    tag( $tag, $filename, @rest );
}

sub run {

my %executable = ();
my %ELF = ();
my %scripts = ();

# no dependency for install-menu, because the menu package specifically
# says not to depend on it.

my $pkg = shift;
my $type = shift;
my $info = shift;

foreach my $file ($info->sorted_index) {
    next if $file eq '';
    $ELF{$file} = 1 if $info->file_info->{$file} =~ /^[^,]*\bELF\b/o;

    my $index_info = $info->index->{$file};
    my $operm = $index_info->operm;
    next unless $index_info->is_file and ($operm & 0111);
    $executable{$file} = 1;
}

my $all_parsed = Lintian::Relation->and ($info->relation ('all'),
                                         $info->relation ('provides'),
                                         $pkg);
my $str_deps = $info->relation('strong');


for my $filename (sort keys %{$info->scripts}) {
    my $interpreter = $info->scripts->{$filename}->{interpreter};
    my $calls_env = $info->scripts->{$filename}->{calls_env};
    my $path;
    $scripts{$filename} = 1;

    my $in_docs = $filename =~ m,usr/share/doc/,;
    my $in_examples = $filename =~ m,usr/share/doc/[^/]+/examples/,;

    # no checks necessary at all for scripts in /usr/share/doc/
    # unless they are examples
    next if $in_docs and !$in_examples;

    my ($base) = $interpreter =~ m,([^/]*)$,;

    # allow exception for .in files that have stuff like #!@PERL@
    next if ($filename =~ m,\.in$, and $interpreter =~ m,^(\@|<\<)[A-Z_]+(\@|>\>)$,);

    my $is_absolute = ($interpreter =~ m,^/, or defined $calls_env);

    # Skip files that have the #! line, but are not executable and do not have
    # an absolute path and are not in a bin/ directory (/usr/bin, /bin etc)
    # They are probably not scripts after all.
    next if ($filename !~ m,(bin/|etc/init\.d/), and !$executable{$filename}
             and !$is_absolute and !$in_examples);

    # Example directories sometimes contain Perl libraries, and some people
    # use initial lines like #!perl or #!python to provide editor hints, so
    # skip those too if they're not executable.  Be conservative here, since
    # it's not uncommon for people to both not set examples executable and not
    # fix the path and we want to warn about that.
    next if ($filename =~ /\.pm\z/ and !$executable{$filename}
             and !$is_absolute and $in_examples);

    if ($interpreter eq '') {
        script_tag('script-without-interpreter', $filename);
        next;
    }

    # Either they use an absolute path or they use '/usr/bin/env interp'.
    script_tag('interpreter-not-absolute', $filename, "#!$interpreter")
        unless $is_absolute;
    tag 'script-not-executable', $filename
        unless ($executable{$filename}
                or $filename =~ m,^usr/(lib|share)/.*\.pm,
                or $filename =~ m,^usr/(lib|share)/.*\.py,
                or $filename =~ m,^usr/(lib|share)/ruby/.*\.rb,
                or $filename =~ m,^usr/share/debconf/confmodule(?:\.sh)?$,
                or $filename =~ m,\.in$,
                or $filename =~ m,\.erb$,
                or $filename =~ m,\.ex$,
                or $filename eq 'etc/init.d/skeleton'
                or $filename =~ m,^etc/menu-methods,
                or $filename =~ m,^etc/X11/Xsession\.d,)
                or $in_docs;

    # Warn about csh scripts.
    tag 'csh-considered-harmful', $filename
        if (($base eq 'csh' or $base eq 'tcsh')
            and $executable{$filename}
            and $filename !~ m,^etc/csh/login\.d/,)
            and !$in_docs;

    $path = $info->unpacked($filename);
    # Syntax-check most shell scripts, but don't syntax-check scripts that end
    # in .dpatch.  bash -n doesn't stop checking at exit 0 and goes on to blow
    # up on the patch itself.
    if ($base =~ /^$known_shells_regex$/) {
        if (-x $interpreter
            and ! script_is_evil_and_wrong($path)
            and $filename !~ m,\.dpatch$,
            and $filename !~ m,\.erb$,
            # exclude some shells. zsh -n is broken, see #485885
            and $base !~ m/^(z|t?c)sh$/) {

            if (check_script_syntax($interpreter, $path)) {
                script_tag('shell-script-fails-syntax-check', $filename);
            }
        }
    }

    # Try to find the expected path of the script to check.  First check
    # $INTERPRETERS and %versioned_interpreters.  If not found there, see if
    # it ends in a version number and the base is found in
    # %versioned_interpreters.
    my $data = $INTERPRETERS->value ($base);
    my $versioned = 0;
    if (not defined $data) {
        $data = $VERSIONED_INTERPRETERS->value ($base);
        undef $data if ($data and not defined ($data->[1]));
        if (not defined ($data) and $base =~ /^(.*[^\d.-])-?[\d.]+$/) {
            $data = $VERSIONED_INTERPRETERS->value ($1);
            undef $data unless ($data and $base =~ /$data->[2]/);
        }
        $versioned = 1 if $data;
    }
    if ($data) {
        my $expected = $data->[0] . '/' . $base;
        unless ($interpreter eq $expected or defined $calls_env) {
            script_tag('wrong-path-for-interpreter', $filename,
                "(#!$interpreter != $expected)");
        }
    } elsif ($interpreter =~ m,/usr/local/,) {
        script_tag('interpreter-in-usr-local', $filename, "#!$interpreter");
    } elsif ($executable{'.' . $interpreter}) {
        # Package installs the interpreter itself, so it's probably ok.  Don't
        # emit any tag for this.
    } elsif ($interpreter eq '/bin/env') {
        script_tag('script-uses-bin-env', $filename);
    } else {
        my $pinter = 0;
        if ($interpreter =~ m,^/,) {
            # Check if the package ships the interpreter (and it is
            # executable).
            my $interfile = substr $interpreter, 1;
            my $index_info = $info->index->{$interfile};
            $pinter = 1 if $index_info && ($index_info->operm & 0111);
        }
        script_tag('unusual-interpreter', $filename, "#!$interpreter")
            unless $pinter;
    }

    # Check for obsolete perl libraries
    if ($base eq 'perl' &&
             !$str_deps->implies ('libperl4-corelibs-perl | perl (<< 5.12.3-7)')) {
        open(FH, '<', $path) or fail("could not open script $path");
        while (<FH>) {
            if (/(?:do|require)\s+(?:'|")(abbrev|assert|bigfloat|bigint|bigrat|cacheout|complete|ctime|dotsh|exceptions|fastcwd|find|finddepth|flush|getcwd|getopt|getopts|hostname|importenv|look|newgetopt|open2|open3|pwd|shellwords|stat|syslog|tainted|termcap|timelocal|validate)\.pl(?:'|")/) {
                tag 'script-uses-perl4-libs-without-dep', "$filename:$. ${1}.pl";
            }
        }
        close(FH);
    }

    # If we found the interpreter and the script is executable, check
    # dependencies.  This should be the last thing we do in the loop so that
    # we can use next for an early exit and reduce the nesting.
    next unless ($data && $executable{$filename} and !$in_docs);
    if (!$versioned) {
        my $depends = $data->[1];
        if (not defined $depends) {
            $depends = $base;
        }
        if ($depends && !$all_parsed->implies($depends)) {
            if ($base =~ /^(python|ruby|(m|g)awk)$/) {
                tag("$base-script-but-no-$base-dep", $filename);
            } elsif ($base eq 'csh' && $filename =~ m,^etc/csh/login\.d/,) {
                # Initialization files for csh.
            } elsif ($base eq 'fish' && $filename =~ m,^etc/fish\.d/,) {
                # Initialization files for fish.
            } elsif ($base eq 'ocamlrun' &&
                     $all_parsed->matches (qr/^ocaml(?:-base)?(?:-nox)?-\d\.[\d.]+/)) {
                # ABI-versioned virtual packages for ocaml
            } else {
                tag 'missing-dep-for-interpreter', "$base => $depends",
                    "($filename)";
            }
        }
    } elsif ($VERSIONED_INTERPRETERS->known ($base)) {
        my @versions = @{ $data->[4] };
        my @depends = map {
            my $d = $data->[3];
            $d =~ s/\$1/$_/g;
            $d;
        } @versions;
        unshift (@depends, $data->[1]) if length $data->[1];
        my $depends = join (' | ',  @depends);
        unless ($all_parsed->implies($depends)) {
            if ($base eq 'php') {
                tag 'php-script-but-no-phpX-cli-dep', $filename;
            } elsif ($base =~ /^(wish|tclsh)/) {
                tag "$1-script-but-no-$1-dep", $filename;
            } else {
                tag 'missing-dep-for-interpreter', "$base => $depends",
                    "($filename)";
            }
        }
    } else {
        my ($version) = ($base =~ /$data->[2]/);
        my $depends = $data->[3];
        $depends =~ s/\$1/$version/g;
        unless ($all_parsed->implies($depends)) {
            if ($base =~ /^php/) {
                tag 'php-script-but-no-phpX-cli-dep', $filename;
            } elsif ($base =~ /^(python|ruby)/) {
                tag "$1-script-but-no-$1-dep", $filename;
            } else {
                tag 'missing-dep-for-interpreter', "$base => $depends",
                    "($filename)";
            }
        }
    }
}

foreach (keys %executable) {
    tag 'executable-not-elf-or-script', $_
        unless ( $ELF{$_}
                 or $scripts{$_}
                 or $_ =~ m,^usr(?:/X11R6)?/man/,
                 or $_ =~ m/\.exe$/ # mono convention
                 );
}

open(SCRIPTS, '<', 'control-scripts')
    or fail("cannot open lintian control-scripts file: $!");

# Handle control scripts.  This is an edited version of the code for
# normal scripts above, because there were just enough differences to
# make a shared function awkward.

my %added_diversions;
my %removed_diversions;
my $expand_diversions = 0;
while (<SCRIPTS>) {
    chop;

    m/^(\S*) (.*)$/ or fail("bad line in control-scripts file: $_");
    my $interpreter = $1;
    my $file = $2;
    my $filename = $info->control($file);

    $interpreter =~ m|([^/]*)$|;
    my $base = $1;

    if ($interpreter eq '') {
        tag 'script-without-interpreter', "control/$file";
        next;
    }

    tag 'interpreter-not-absolute', "control/$file", "#!$interpreter"
        unless ($interpreter =~ m|^/|);

    if ($interpreter =~ m|/usr/local/|) {
        tag 'control-interpreter-in-usr-local', "control/$file", "#!$interpreter";
    } elsif ($base eq 'sh' or $base eq 'bash' or $base eq 'perl') {
        my $expected = ($INTERPRETERS->value ($base))->[0] . '/' . $base;
        tag 'wrong-path-for-interpreter', "#!$interpreter != $expected",
            "(control/$file)"
            unless ($interpreter eq $expected);
    } elsif ($file eq 'config') {
        tag 'forbidden-config-interpreter', "#!$interpreter";
    } elsif ($file eq 'postrm') {
        tag 'forbidden-postrm-interpreter', "#!$interpreter";
    } elsif ($INTERPRETERS->known ($base)) {
        my $data = $INTERPRETERS->value ($base);
        my $expected = $data->[0] . '/' . $base;
        unless ($interpreter eq $expected) {
            tag 'wrong-path-for-interpreter', "#!$interpreter != $expected",
                "(control/$file)"
        }
        tag 'unusual-control-interpreter', "control/$file", "#!$interpreter";

        # Interpreters used by preinst scripts must be in Pre-Depends.
        # Interpreters used by postinst or prerm scripts must be in Depends.
        unless (not $data->[1]) {
            my $depends = Lintian::Relation->new($data->[1]);
            if ($file eq 'preinst') {
                unless ($info->relation('pre-depends')->implies($depends)) {
                    tag 'preinst-interpreter-without-predepends',
                        "#!$interpreter"
                }
            } else {
                unless ($info->relation('strong')->implies($depends)) {
                    tag 'control-interpreter-without-depends', "control/$file",
                        "#!$interpreter"
                }
            }
        }
    } else {
        tag 'unknown-control-interpreter', "control/$file", "#!$interpreter";
        next; # no use doing further checks if it's not a known interpreter
    }

    # perhaps we should warn about *csh even if they're somehow screwed,
    # but that's not really important...
    tag 'csh-considered-harmful', "control/$file"
        if ($base eq 'csh' or $base eq 'tcsh');

    my $shellscript = $base =~ /^$known_shells_regex$/ ? 1 : 0;

    # Only syntax-check scripts we can check with bash.
    my $checkbashisms;
    if ($shellscript) {
        $checkbashisms = $base eq 'sh' ? 1 : 0;
        if ($base eq 'sh' or $base eq 'bash') {
            if (check_script_syntax('/bin/bash', $filename)) {
                tag 'maintainer-shell-script-fails-syntax-check', $file;
            }
        }
    }

    # now scan the file contents themselves
    open (C, '<', $filename)
        or fail("cannot open maintainer script $filename for reading: $!");

    my %warned;
    my ($saw_init, $saw_invoke, $saw_debconf, $saw_bange, $saw_sete, $has_code);
    my $cat_string = '';

    my $previous_line = '';
    while (<C>) {
        if ($. == 1 && $shellscript && m,/$base\s*.*\s-\w*e\w*\b,) {
            $saw_bange = 1;
        }

        next if m,^\s*$,;  # skip empty lines
        next if m,^\s*\#,; # skip comment lines
        $_ = remove_comments($_);

        # Concatenate lines containing continuation character (\) at the end
        if ($shellscript && /\\$/) {
            s/\\//;
            chomp;
            $previous_line .= $_;
            next;
        }

        chomp;
        $_ = $previous_line . $_;
        $previous_line = '';

        # Don't consider the standard dh-make boilerplate to be code.  This
        # means ignoring the framework of a case statement, the labels, the
        # echo complaining about unknown arguments, and an exit.
        unless ($has_code
                || m/^\s*set\s+-\w+\s*$/
                || m/^\s*case\s+\"?\$1\"?\s+in\s*$/
                || m/^\s*(?:[a-z|-]+|\*)\)\s*$/
                || m/^\s*[:;]+\s*$/
                || m/^\s*echo\s+\"[^\"]+\"(?:\s*>&2)?\s*$/
                || m/^\s*esac\s*$/
                || m/^\s*exit\s+\d+\s*$/) {
            $has_code = 1;
        }

        if ($shellscript && m,${LEADIN}set\s*(\s+-(-.*|[^e]+))*\s-\w*e,) {
            $saw_sete = 1;
        }

        if (m,[^\w]((/var)?/tmp|\$TMPDIR)/[^)\]}\s], and not m/\bmks?temp\b/ and not m/\btempfile\b/ and not m/\bmkdir\b/ and not m/\$RANDOM/) {
            tag 'possibly-insecure-handling-of-tmp-files-in-maintainer-script', "$file:$."
                unless $warned{tmp};
            $warned{tmp} = 1;
        }
        if (m/^\s*killall(?:\s|\z)/) {
            tag 'killall-is-dangerous', "$file:$." unless $warned{killall};
            $warned{killall} = 1;
        }
        if (m/^\s*mknod(?:\s|\z)/ and not m/\sp\s/) {
            tag 'mknod-in-maintainer-script', "$file:$.";
        }

        # Collect information about init script invocations to catch running
        # init scripts directly rather than through invoke-rc.d.  Since the
        # script is allowed to run the init script directly if invoke-rc.d
        # doesn't exist, only tag direct invocations where invoke-rc.d is
        # never used in the same script.  Lots of false negatives, but
        # hopefully not many false positives.
        if (m%^\s*/etc/init\.d/(\S+)\s+[\"\']?(\S+)[\"\']?%) {
            $saw_init = $.;
        }
        if (m%^\s*invoke-rc\.d\s+%) {
            $saw_invoke = $.;
        }

        if ($shellscript) {
            if ($cat_string ne '' and m/^\Q$cat_string\E$/) {
                $cat_string = '';
            }
            my $within_another_shell = 0;
            if (m,(?:^|\s+)(?:(?:/usr)?/bin/)?($known_shells_regex)\s+-c\s*.+,
                and $1 ne 'sh') {
                $within_another_shell = 1;
            }
            # if cat_string is set, we are in a HERE document and need not
            # check for things
            if ($cat_string eq '' and $checkbashisms and !$within_another_shell) {
                my $found = 0;
                my $match = '';

                # since this test is ugly, I have to do it by itself
                # detect source (.) trying to pass args to the command it runs
                # The first expression weeds out '. "foo bar"'
                if (not $found and
                    not m/^\s*\.\s+(\"[^\"]+\"|\'[^\']+\')\s*(\&|\||\d?>|<|;|\Z)/
                    and m/^\s*(\.\s+[^\s;\`:]+\s+([^\s;]+))/) {

                    my $extra;
                    ($match, $extra) = ($1, $2);
                    if ($extra =~ /^(\&|\||\d?>|<)/) {
                        # everything is ok
                        ;
                    } else {
                        $found = 1;
                    }
                }

                my $line = $_;

                unless ($found) {
                    for my $re (@bashism_single_quote_regexs) {
                        if ($line =~ m/($re)/) {
                            $found = 1;
                            ($match) = m/($re)/;
                            last;
                        }
                    }
                }

                # Ignore anything inside single quotes; it could be an
                # argument to grep or the like.

                # $cat_line contains the version of the line we'll check
                # for heredoc delimiters later. Initially, remove any
                # spaces between << and the delimiter to make the following
                # updates to $cat_line easier.
                my $cat_line = $line;
                $cat_line =~ s/(<\<-?)\s+/$1/g;

                # Remove single quoted strings, with the exception that we
                # don't remove the string
                # if the quote is immediately preceeded by a < or a -, so we
                # can match "foo <<-?'xyz'" as a heredoc later
                # The check is a little more greedy than we'd like, but the
                # heredoc test itself will weed out any false positives
                $cat_line =~ s/(^|[^<\\\"-](?:\\\\)*)\'(?:\\.|[^\\\'])+\'/$1''/g;

                unless ($found) {
                    # Remove "quoted quotes". They're likely to be inside
                    # another pair of quotes; we're not interested in
                    # them for their own sake and removing them makes finding
                    # the limits of the outer pair far easier.
                    $line =~ s/(^|[^\\\'\"])\"\'\"/$1/g;
                    $line =~ s/(^|[^\\\'\"])\'\"\'/$1/g;

                    $line =~ s/(^|[^\\\"](?:\\\\)*)\'(?:\\.|[^\\\'])+\'/$1''/g;
                    for my $re (@bashism_string_regexs) {
                        if ($line =~ m/($re)/) {
                            $found = 1;
                            ($match) = m/($re)/;
                            last;
                        }
                    }
                }

                # We've checked for all the things we still want to notice in
                # double-quoted strings, so now remove those strings as well.
                $cat_line =~ s/(^|[^<\\\'-](?:\\\\)*)\"(?:\\.|[^\\\"])+\"/$1""/g;
                unless ($found) {
                    $line =~ s/(^|[^\\\'](?:\\\\)*)\"(?:\\.|[^\\\"])+\"/$1""/g;
                    for my $re (@bashism_regexs) {
                        if ($line =~ m/($re)/) {
                            $found = 1;
                            ($match) = m/($re)/;
                            last;
                        }
                    }
                }

                if ($found) {
                    tag 'possible-bashism-in-maintainer-script', "$file:$. \'$match\'";
                }

                # Only look for the beginning of a heredoc here, after we've
                # stripped out quoted material, to avoid false positives.
                if ($cat_line =~ m/(?:^|[^<])\<\<\-?\s*(?:[\\]?(\w+)|[\'\"](.*?)[\'\"])/) {
                    $cat_string = $1;
                    $cat_string = $2 if not defined $cat_string;
                }
            }
            if (!$cat_string) {
                if (/^\s*start-stop-daemon\s+/ && !/\s--stop\b/) {
                    tag 'start-stop-daemon-in-maintainer-script', "$file:$.";
                }
                # Don't use chown foo.bar
                if (/(chown(\s+--?[A-Za-z-]+)*\s+[-_A-Za-z0-9]+\.[-_A-Za-z0-9]+)\s+/) {
                    tag 'deprecated-chown-usage', "$file:$. \'$1\'";
                }
                if (/invoke-rc.d.*\|\| exit 0/) {
                    tag 'maintainer-script-hides-init-failure', "$file:$.";
                }
                if (m,/usr/share/debconf/confmodule,) {
                    $saw_debconf = 1;
                }
                if (m/^\s*read(?:\s|\z)/ && !$saw_debconf) {
                    tag 'read-in-maintainer-script', "$file:$.";
                }
                if (m,^\s*rm\s+(?:[^>]*\s)?/dev/([^/ ]+),) {
                    my $f = $1//'';
                    if ($f ne 'shm' && $f !~ m,^\.[^.]+,) {
                        # Ignore /dev/shm (initscripts) and dot-files (which are probably
                        # not a dev).
                        tag 'maintainer-script-removes-device-files', "$file:$.";
                    }
                }
                if (m,>\s*(/etc/(?:services|protocols|rpc))(\s|\Z),) {
                    tag 'maintainer-script-modifies-netbase-managed-file', "$file:$. $1";
                }
                if (m,^\s*(?:cp|mv)\s.*(/etc/(?:services|protocols|rpc))\s*$,) {
                    tag 'maintainer-script-modifies-netbase-managed-file', "$file:$. $1";
                }
                if (m,>\s*/etc/inetd\.conf(\s|\Z),) {
                    tag 'maintainer-script-modifies-inetd-conf', "$file:$."
                        unless $info->relation('provides')->implies('inet-superserver');
                }
                if (m,^\s*(?:cp|mv)\s+(?:.*\s)?/etc/inetd\.conf\s*$,) {
                    tag 'maintainer-script-modifies-inetd-conf', "$file:$."
                        unless $info->relation('provides')->implies('inet-superserver');
                }
                if (m,>\s*/etc/ld\.so\.conf(\s|\Z),) {
                    tag 'maintainer-script-modifies-ld-so-conf', "$file:$."
                        unless $pkg =~ /^libc/;
                }
                if (m,^\s*(?:cp|mv)\s+(?:.*\s)?/etc/ld\.so\.conf\s*$,) {
                    tag 'maintainer-script-modifies-ld-so-conf', "$file:$."
                        unless $pkg =~ /^libc/;
                }

                # Ancient dpkg feature tests.
                if (m/${LEADIN}dpkg\s+--assert-working-epoch\b/) {
                    tag 'ancient-dpkg-epoch-check', "$file:$.";
                }
                if (m/${LEADIN}dpkg\s+--assert-multi-conrep\b/) {
                    tag 'ancient-dpkg-multi-conrep-check', "$file:$.";
                }

                # Commands that should not be used in maintainer scripts.
                if (m,${LEADIN}(?:/usr/bin/)?fc-cache(\s|\Z),) {
                    tag 'fc-cache-used-in-maintainer-script', "$file:$.";
                }

                # Check for running commands with a leading path.
                #
                # Unfortunately, our $LEADIN string doesn't work well for this
                # in the presence of commands that contain backquoted
                # expressions because it can't tell the difference between the
                # initial backtick and the closing backtick.  We therefore
                # first extract all backquoted expressions and check them
                # separately, and then remove them from a copy of a string and
                # then check it for bashisms.
                while (m,\`([^\`]+)\`,g) {
                    my $cmd = $1;
                    if ($cmd =~ m,$LEADIN(/(usr/)?s?bin/[\w.+-]+)(\s|;|\z),) {
                        tag 'command-with-path-in-maintainer-script',
                            "$file:$. $1";
                    }
                }
                my $cmd = $_;
                $cmd =~ s/\`[^\`]+\`//g;
                if ($cmd =~ m,$LEADIN(/(?:usr/)?s?bin/[\w.+-]+)(?:\s|;|$),) {
                    tag 'command-with-path-in-maintainer-script', "$file:$. $1";
                }
            }
        }
        if (m,\bsuidregister\b,) {
            tag 'suidregister-used-in-maintainer-script', $file;
        }
        if ($file eq 'preinst') {
            if (m/^\s*dpkg-maintscript-helper(?:\s|\z)/ &&
                    !$info->relation('pre-depends')->implies('dpkg (>= 1.15.7.2~)')) {
                tag 'preinst-uses-dpkg-maintscript-helper-without-predepends', "$file:$.";
            }
        }
        if ($file eq 'postrm') {
            if (m,update\-alternatives \-\-remove,) {
                tag 'update-alternatives-remove-called-in-postrm';
            }
        } else {
            for my $rule (@depends_needed) {
                my ($package, $regex) = @$rule;
                if ($pkg ne $package and /$regex/ and ! $warned{$package}) {
                    if (m,-x\s+\S*$regex, or m,(which|type)\s+$regex, or m,command\s+.*?$regex,) {
                        $warned{$package} = 1;
                    } elsif (!/\|\|\s*true\b/) {
                        unless ($info->relation('strong')->implies($package)) {
                            my $shortpackage = $package;
                            $shortpackage =~ s/[ \(].*//;
                            tag "maintainer-script-needs-depends-on-$shortpackage", $file;
                            $warned{$package} = 1;
                        }
                    }
                }
            }
        }
        if (m,\bgconftool(-2)?(\s|\Z),) {
            tag 'gconftool-used-in-maintainer-script', "$file:$.";
        }
        if (m,\binstall-sgmlcatalog\b, && !(m,--remove, && ($file eq 'prerm' || $file eq 'postinst'))) {
            tag 'install-sgmlcatalog-deprecated', "$file:$.";
        }
        if (m,\binstall-info\b,) {
            tag 'install-info-used-in-maintainer-script', "$file:$.";
        }
        if (m,/var/lib/dpkg/status\b, && $pkg ne 'base-files' && $pkg ne 'dpkg') {
            tag 'maintainer-script-uses-dpkg-status-directly', $file;
        }
        if (m,$LEADIN(?:/usr/sbin/)?dpkg-divert\s, && ! /--(?:help|list|truename|version)/) {
            if (/--local/ or !/--package/) {
                tag 'package-uses-local-diversion', "$file:$.";
            }
            my $mode = /--remove/ ? 'remove' : 'add';
            my ($divert) = /dpkg-divert\s*(.*)$/;
            $divert =~ s/\s*(?:\${?[\w:=-]+}?)*\s*--(?:add|quiet|remove|rename|test|local|(:?admindir|divert|package)\s+\S+)\s*//g;
            # Remove unpaired opening or closing parenthesis
            1 while($divert =~ m/\G.*?\(.+?\)/gc);
            $divert =~ s/\G(.*?)[()]/$1/;
            pos($divert) = undef;
            # Remove unpaired opening or closing braces
            1 while($divert =~ m/\G.*?{.+?}/gc);
            $divert =~ s/\G(.*?)[{}]/$1/;
            pos($divert) = undef;

            # position after the last pair of quotation marks, if any
            1 while($divert =~ m/\G.*?(\"|\').+?\1/gc);
            # Strip anything matching and after '&&', '||', ';', or '>'
            # this is safe only after we are positioned after the last pair
            # of quotation marks
            $divert =~ s/\G.+?\K(?: && | \|\| | ; | \d*> ).*$//x;
            pos($divert) = undef;
            # Remove quotation marks, they affect:
            # * our var to regex trick
            # * stripping the initial slash if the path was quoted
            $divert =~ s/[\"\']//g;
            # remove the leading / because it's not in the index hash
            $divert =~ s,^/,,;

            # remove any remaining leading or trailing whitespace.
            $divert =~ s/^\s+//;
            $divert =~ s/\s+$//;

            $divert = quotemeta($divert);

            # For now just replace variables, they will later be normalised
            $expand_diversions = 1 if $divert =~ s/\\\$\w+/.+/g;
            $expand_diversions = 1 if $divert =~ s/\\\$\\{\w+.*?\\}/.+/g;
            # handle $() the same way:
            $expand_diversions = 1 if $divert =~ s/\\\$\\\(.+?\\\)/.+/g;

            if ($mode eq 'add') {
                $added_diversions{$divert} = {'script' => $file, 'line' => $.};
            } elsif ($mode eq 'remove') {
                push @{$removed_diversions{$divert}}, {'script' => $file, 'line' => $.};
            } else {
                fail "Internal error: \$mode has unknown value: $mode";
            }
        }
    }

    if ($saw_init && ! $saw_invoke) {
        tag 'maintainer-script-calls-init-script-directly', "$file:$saw_init";
    }
    unless ($has_code) {
        tag 'maintainer-script-empty', $file;
    }
    if ($shellscript && !$saw_sete) {
        if ($saw_bange) {
            tag 'maintainer-script-without-set-e', $file;
        } else {
            tag 'maintainer-script-ignores-errors', $file;
        }
    }

    close C;

}
close(SCRIPTS);

# If any of the maintainer scripts used a variable in the file or
# diversion name normalise them all
if ($expand_diversions) {
    for my $divert (keys %removed_diversions, keys %added_diversions) {

        # if a wider regex was found, the entries might no longer be there
        unless (exists($removed_diversions{$divert})
            or exists($added_diversions{$divert})) {
            next;
        }

        my $widerrx = $divert;
        my $wider = $widerrx;
        $wider =~ s/\\//g;

        # find the widest regex:
        my @matches = grep {
            my $lrx = $_;
            my $l = $lrx;
            $l =~ s/\\//g;

            if ($wider =~ m/^$lrx$/) {
                $widerrx = $lrx;
                $wider = $l;
                1;
            } elsif ($l =~ m/^$widerrx$/) {
                1;
            } else {
                0;
            }
        } (keys %removed_diversions, keys %added_diversions);

        # replace all the occurences with the widest regex:
        for my $k (@matches) {
            next if ($k eq $widerrx);

            if (exists($removed_diversions{$k})) {
                $removed_diversions{$widerrx} = $removed_diversions{$k};
                delete $removed_diversions{$k};
            }
            if (exists($added_diversions{$k})) {
                $added_diversions{$widerrx} = $added_diversions{$k};
                delete $added_diversions{$k};
            }
        }
    }
}

for my $divert (keys %removed_diversions) {
    if (exists $added_diversions{$divert}) {
        # just mark the entry, because a --remove might
        # happen in two branches in the script, i.e. we
        # see it twice, which is not a bug
        $added_diversions{$divert}{'removed'} = 1;
    } else {
        for my $item (@{$removed_diversions{$divert}}) {
            my $script = $item->{'script'};
            my $line = $item->{'line'};

            next unless ($script eq 'postrm');

            # Allow preinst and postinst to remove diversions the
            # package doesn't add to clean up after previous
            # versions of the package.

            $divert = unquote($divert, $expand_diversions);

            tag 'remove-of-unknown-diversion', $divert, "$script:$line";
        }
    }
}

for my $divert (keys %added_diversions) {
    my $script = $added_diversions{$divert}{'script'};
    my $line = $added_diversions{$divert}{'line'};

    my $divertrx = $divert;
    $divert = unquote($divert, $expand_diversions);

    if (not exists $added_diversions{$divertrx}{'removed'}) {
        tag 'orphaned-diversion', $divert, $script;
    }

    # Handle man page diversions somewhat specially.  We may divert away a man
    # page in one section without replacing that same file, since we're
    # installing a man page in a different section.  An example is diverting a
    # man page in section 1 and replacing it with one in section 1p (such as
    # libmodule-corelist-perl at the time of this writing).
    #
    # Deal with this by turning all man page diversions into wildcard
    # expressions instead that match everything in the same numeric section so
    # that they'll match the files shipped in the package.
    if ($divertrx =~ m,^(usr\\/share\\/man\\/\S+\\/.*\\\.\d)\w*(\\\.gz\z),) {
        $divertrx = "$1.*$2";
        $expand_diversions = 1;
    }

    if ($expand_diversions) {
        tag 'diversion-for-unknown-file', $divert, "$script:$line"
            unless (grep { $_ =~ m/$divertrx/ } $info->sorted_index);
    } else {
        tag 'diversion-for-unknown-file', $divert, "$script:$line"
            unless (exists $info->index->{$divert});
    }
}

}

# -----------------------------------

# Returns non-zero if the given file is not actually a shell script,
# just looks like one.
sub script_is_evil_and_wrong {
    my ($filename) = @_;
    my $ret = 0;
    open (IN, '<', $filename) or fail("cannot open $filename: $!");
    my $i = 0;
    my $var = '0';
    my $backgrounded = 0;
    local $_;
    while (<IN>) {
        chomp;
        next if m/^#/o;
        next if m/^$/o;
        last if (++$i > 55);
        if (m~
            # the exec should either be "eval"ed or a new statement
            (^\s*|\beval\s*[\'\"]|(;|&&|\b(then|else))\s*)

            # eat anything between the exec and $0
            exec\s*.+\s*

            # optionally quoted executable name (via $0)
            .?\$$var.?\s*

            # optional "end of options" indicator
            (--\s*)?

            # Match expressions of the form '${1+$@}', '${1:+"$@"',
            # '"${1+$@', "$@", etc where the quotes (before the dollar
            # sign(s)) are optional and the second (or only if the $1
            # clause is omitted) parameter may be $@ or $*.
            #
            # Finally the whole subexpression may be omitted for scripts
            # which do not pass on their parameters (i.e. after re-execing
            # they take their parameters (and potentially data) from stdin
            .?(\${1:?\+.?)?(\$(\@|\*))?~x) {
            $ret = 1;
            last;
        } elsif (/^\s*(\w+)=\$0;/) {
            $var = $1;
        } elsif (m~
            # Match scripts which use "foo $0 $@ &\nexec true\n"
            # Program name
            \S+\s+

            # As above
            .?\$$var.?\s*
            (--\s*)?
            .?(\${1:?\+.?)?(\$(\@|\*))?.?\s*\&~x) {

            $backgrounded = 1;
        } elsif ($backgrounded and m~
            # the exec should either be "eval"ed or a new statement
            (^\s*|\beval\s*[\'\"]|(;|&&|\b(then|else))\s*)
            exec\s+true(\s|\Z)~x) {

            $ret = 1;
            last;
        }
    }
    close IN;
    return $ret;
}

# Given an interpretor and a file, run the interpretor on that file with the
# -n option to check syntax, discarding output and returning the exit status.
sub check_script_syntax {
    my ($interpreter, $script) = @_;
    my $pid = fork;
    if (!defined $pid) {
        fail("cannot fork: $!");
    } elsif ($pid == 0) {
        open STDOUT, '>', '/dev/null' or fail("cannot reopen stdout: $!");
        open STDERR, '>&STDOUT' or fail("cannot reopen stderr: $!");
        exec $interpreter, '-n', $script
            or fail("cannot exec $interpreter: $!");
    } else {
        waitpid $pid, 0;
    }
    return $?;
}

sub remove_comments {
    local $_;

    my $line = shift || '';
    $_ = $line;

    # Remove quoted strings so we can more easily ignore comments
    # inside them
    s/(^|[^\\](?:\\\\)*)\'(?:\\.|[^\\\'])+\'/$1''/g;
    s/(^|[^\\](?:\\\\)*)\"(?:\\.|[^\\\"])+\"/$1""/g;

    # If the remaining string contains what looks like a comment,
    # eat it. In either case, swap the unmodified script line
    # back in for processing (if required) and return it.
    if (m/(?:^|[^[\\])[\s\&;\(\)](\#.*$)/) {
        $_ = $line;
        s/\Q$1\E//;  # eat comments
    } else {
        $_ = $line;
    }

    return $_;
}

sub unquote($$) {
    my ($string, $replace_regex) = @_;

    $string =~ s,\\,,g;
    if ($replace_regex) {
        $string =~ s,\.\+,*,g;
    }

    return $string;
}

sub _parse_interpreters {
    my ($interpreter, $value) = @_;
    my ($path, $dep) = split m/\s*,\s*/, $value, 2;
    $dep = $interpreter if not $dep;
    if ($dep eq '@NODEPS@') {
        $dep = '';
    } elsif ($dep =~ m/@/) {
        fail "Unknown magic value $dep for versioned interpreter $interpreter";
    }
    return [$path, $dep];
}

sub _parse_versioned_interpreters {
    my ($interpreter, $value) = @_;
    my ($path, $regex, $deptmp, $vers, $deprel) = split m/\s*,\s*/, $value, 5;
    my @versions = split m/\s++/, $vers;
    $deprel = $interpreter if not $deprel;
    if ($deprel eq '@NO_DEFAULT_DEPS@') {
        $deprel = '';
    } elsif ($deprel eq '@SKIP_UNVERSIONED@') {
        $deprel = undef;
    } elsif ($deprel =~ m/@/) {
        fail "Unknown magic value $deprel for versioned interpreter $interpreter";
    }
    return [$path, $deprel, qr/^$regex$/, $deptmp, \@versions];
}


1;

# Local Variables:
# indent-tabs-mode: nil
# cperl-indent-level: 4
# End:
# vim: syntax=perl sw=4 sts=4 sr et