summaryrefslogtreecommitdiffstats
path: root/lib/Lintian/Check/MenuFormat.pm
blob: c9d40a85d18ecb8d4a69442eff574d93ec6fe76e (plain)
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
# menu format -- lintian check script -*- perl -*-

# Copyright (C) 1998 by Joey Hess
# Copyright (C) 2017-2018 Chris Lamb <lamby@debian.org>
#
# 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 https://www.gnu.org/copyleft/gpl.html, or write to the Free
# Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
# MA 02110-1301, USA.

# This script also checks desktop entries, since they share quite a bit of
# code.  At some point, it would make sense to try to refactor this so that
# shared code is in libraries.
#
# Further things that the desktop file validation should be checking:
#
#  - Encoding of the file should be UTF-8.
#  - Additional Categories should be associated with Main Categories.
#  - List entries (MimeType, Categories) should end with a semicolon.
#  - Check for GNOME/GTK/X11/etc. dependencies and require the relevant
#    Additional Category to be present.
#  - Check all the escape characters supported by Exec.
#  - Review desktop-file-validate to see what else we're missing.

package Lintian::Check::MenuFormat;

use v5.20;
use warnings;
use utf8;

use Const::Fast;
use File::Basename;
use List::SomeUtils qw(any first_value);
use Unicode::UTF8 qw(encode_utf8);

use Moo;
use namespace::clean;

with 'Lintian::Check';

const my $EMPTY => q{};
const my $SPACE => q{ };
const my $SLASH => q{/};

const my $MAXIMUM_SIZE_STANDARD_ICON => 32;
const my $MAXIMUM_SIZE_32X32_ICON => 32;
const my $MAXIMUM_SIZE_16X16_ICON => 16;

# This is a list of all tags that should be in every menu item.
my @req_tags = qw(needs section title command);

# This is a list of all known tags.
my @known_tags = qw(
  needs
  section
  title
  sort
  command
  longtitle
  icon
  icon16x16
  icon32x32
  description
  hotkey
  hints
);

# These 'needs' tags are always valid, no matter the context, and no other
# values are valid outside the Window Managers context (don't include wm here,
# in other words).  It's case insensitive, use lower case here.
my @needs_tag_vals = qw(x11 text vc);

has MENU_SECTIONS => (
    is => 'rw',
    lazy => 1,
    default => sub {
        my ($self) = @_;

        my %menu_sections;

        my $data = $self->data->load('menu-format/menu-sections');

        for my $key ($data->all) {

            my ($root, $under) = split(m{/}, $key, 2);

            $under //= $EMPTY;

            # $under is empty if this is just a root section
            $menu_sections{$root}{$under} = 1;
        }

        return \%menu_sections;
    }
);

# Authoritative source of desktop keys:
# https://specifications.freedesktop.org/desktop-entry-spec/latest/
#
# This is a list of all keys that should be in every desktop entry.
my @req_desktop_keys = qw(Type Name);

# This is a list of all known keys.
has KNOWN_DESKTOP_KEYS => (
    is => 'rw',
    lazy => 1,
    default => sub {
        my ($self) = @_;

        return $self->data->load('menu-format/known-desktop-keys');
    }
);

has DEPRECATED_DESKTOP_KEYS => (
    is => 'rw',
    lazy => 1,
    default => sub {
        my ($self) = @_;

        return $self->data->load('menu-format/deprecated-desktop-keys');
    }
);

# KDE uses some additional keys that should start with X-KDE but don't for
# historical reasons.
has KDE_DESKTOP_KEYS => (
    is => 'rw',
    lazy => 1,
    default => sub {
        my ($self) = @_;

        return $self->data->load('menu-format/kde-desktop-keys');
    }
);

# Known types of desktop entries.
# https://specifications.freedesktop.org/desktop-entry-spec/latest/ar01s06.html
my %known_desktop_types = map { $_ => 1 } qw(
  Application
  Link
  Directory
);

# Authoritative source of desktop categories:
# https://specifications.freedesktop.org/menu-spec/latest/apa.html

# This is a list of all Main Categories for .desktop files.  Application is
# added as an exception; it's not listed in the standard, but it's widely used
# and used as an example in the GNOME documentation.  GNUstep is added as an
# exception since it's used by GNUstep packages.
my %main_categories = map { $_ => 1 } qw(
  AudioVideo
  Audio
  Video
  Development
  Education
  Game
  Graphics
  Network
  Office
  Science
  Settings
  System
  Utility
  Application
  GNUstep
);

# This is a list of all Additional Categories for .desktop files.  Ideally we
# should be checking to be sure the associated Main Categories are present,
# but we don't have support for that yet.
has ADD_CATEGORIES => (
    is => 'rw',
    lazy => 1,
    default => sub {
        my ($self) = @_;

        return $self->data->load('menu-format/add-categories');
    }
);

# This is a list of Reserved Categories for .desktop files.  To use one of
# these, the desktop entry must also have an OnlyShowIn key limiting the
# environment to one that supports this category.
my %reserved_categories = map { $_ => 1 } qw(
  Screensaver
  TrayIcon
  Applet
  Shell
);

# Path in which to search for binaries referenced in menu entries.  These must
# not have leading slashes.
my @path = qw(usr/local/bin/ usr/bin/ bin/ usr/games/);

my %known_tags_hash = map { $_ => 1 } @known_tags;
my %needs_tag_vals_hash = map { $_ => 1 } @needs_tag_vals;

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

sub installable {
    my ($self) = @_;

    my $index = $self->processable->installed;

    my (@menufiles, %desktop_cmds);
    for my $dirname (qw(usr/share/menu/ usr/lib/menu/)) {
        if (my $dir = $index->resolve_path($dirname)) {
            push(@menufiles, $dir->children);
        }
    }

    # Find the desktop files in the package for verification.
    my @desktop_files;
    for my $subdir (qw(applications xsessions)) {
        if (my $dir = $index->lookup("usr/share/$subdir/")) {
            for my $item ($dir->children) {
                next
                  unless $item->is_file;

                next
                  if $item->is_dir;

                next
                  unless $item->basename =~ /\.desktop$/;

                $self->pointed_hint('executable-desktop-file', $item->pointer,
                    $item->octal_permissions)
                  if $item->is_executable;

                push(@desktop_files, $item)
                  unless $item->name =~ / template /msx;
            }
        }
    }

    # Verify all the desktop files.
    for my $desktop_file (@desktop_files) {
        $self->verify_desktop_file($desktop_file, \%desktop_cmds);
    }

    # Now all the menu files.
    for my $menufile (@menufiles) {
        # Do not try to parse executables
        next if $menufile->is_executable or not $menufile->is_open_ok;

        # README is a special case
        next if $menufile->basename eq 'README' && !$menufile->is_dir;
        my $menufile_line =$EMPTY;

        open(my $fd, '<', $menufile->unpacked_path)
          or die encode_utf8('Cannot open ' . $menufile->unpacked_path);

        # line below is commented out in favour of the while loop
        # do { $_=<IN>; } while defined && (m/^\s* \#/ || m/^\s*$/);
        while (my $line = <$fd>) {
            if ($line =~ /^\s*\#/ || $line =~ /^\s*$/) {
                next;

            } else {
                $menufile_line = $line;
                last;
            }
        }

        # Check first line of file to see if it matches the new menu
        # file format.
        if ($menufile_line =~ m/^!C\s*menu-2/) {
            # we can't parse that yet
            close($fd);
            next;
        }

        # Parse entire file as a new format menu file.
        my $line=$EMPTY;
        my $lc=0;
        do {
            $lc++;

            # Ignore lines that are comments.
            if ($menufile_line =~ m/^\s*\#/) {
                next;
            }
            $line .= $menufile_line;
            # Note that I allow whitespace after the continuation character.
            # This is caught by verify_line().
            if (!($menufile_line =~ m/\\\s*?$/)) {
                $self->verify_line($menufile, $line,$lc,\%desktop_cmds);
                $line=$EMPTY;
            }
        } while ($menufile_line = <$fd>);
        $self->verify_line($menufile, $line,$lc,\%desktop_cmds);

        close($fd);
    }

    return;
}

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

# Pass this a line of a menu file, it sanitizes it and
# verifies that it is correct.
sub verify_line {
    my ($self, $menufile, $line, $position,$desktop_cmds) = @_;

    my $pointer = $menufile->pointer($position);
    my %vals;

    chomp $line;

    # Replace all line continuation characters with whitespace.
    # (do not remove them completely, because update-menus doesn't)
    $line =~ s/\\\n/ /mg;

    # This is in here to fix a common mistake: whitespace after a '\'
    # character.
    if ($line =~ s/\\\s+\n/ /mg) {
        $self->pointed_hint('whitespace-after-continuation-character',
            $pointer);
    }

    # Ignore lines that are all whitespace or empty.
    return if $line =~ m/^\s*$/;

    # Ignore lines that are comments.
    return if $line =~ m/^\s*\#/;

    # Start by testing the package check.
    if (not $line =~ m/^\?package\((.*?)\):/) {
        $self->pointed_hint('bad-test-in-menu-item', $pointer);
        return;
    }
    my $pkg_test = $1;
    my %tested_packages = map { $_ => 1 } split(/\s*,\s*/, $pkg_test);
    my $tested_packages = scalar keys %tested_packages;
    unless (exists $tested_packages{$self->processable->name}) {
        $self->pointed_hint('pkg-not-in-package-test', $pointer, $pkg_test);
    }
    $line =~ s/^\?package\(.*?\)://;

    # Now collect all the tag=value pairs. I've heavily commented
    # the killer regexp that's responsible.
    #
    # The basic idea here is we start at the beginning of the line.
    # Each loop pulls off one tag=value pair and advances to the next
    # when we have no more matches, there should be no text left on
    # the line - if there is, it's a parse error.
    while (
        $line =~ m{
           \s*?                 # allow whitespace between pairs
           (                    # capture what follows in $1, it's our tag
            [^\"\s=]            # a non-quote, non-whitespace, character
            *                   # match as many as we can
           )
           =
           (                    # capture what follows in $2, it's our value
            (?:
             \"                 # this is a quoted string
             (?:
              \\.               # any quoted character
              |                 # or
              [^\"]             # a non-quote character
             )
             *                  # repeat as many times as possible
             \"                 # end of the quoted value string
            )
            |                   # the other possibility is a non-quoted string
            (?:
             [^\"\s]            # a non-quote, non-whitespace character
             *                  # match as many times as we can
            )
           )
           }gcx
    ) {
        my $tag = $1;
        my $value = $2;

        if (exists $vals{$tag}) {
            $self->pointed_hint('duplicate-tag-in-menu', $pointer, $1);
        }

        # If the value was quoted, remove those quotes.
        if ($value =~ m/^\"(.*)\"$/) {
            $value = $1;
        } else {
            $self->pointed_hint('unquoted-string-in-menu-item',$pointer, $1);
        }

        # If the value has escaped characters, remove the
        # escapes.
        $value =~ s/\\(.)/$1/g;

        $vals{$tag} = $value;
    }

    # This is not really a no-op. Note the use of the /c
    # switch - this makes perl keep track of the current
    # search position. Notice, we did it above in the loop,
    # too. (I have a /g here just so the /c takes affect.)
    # We use this below when we look at how far along in the
    # string we matched. So the point of this line is to allow
    # trailing whitespace on the end of a line.
    $line =~ m/\s*/gc;

    # If that loop didn't match up to end of line, we have a
    # problem..
    if (pos($line) < length($line)) {
        $self->pointed_hint('unparsable-menu-item', $pointer);
        # Give up now, before things just blow up in our face.
        return;
    }

    # Now validate the data in the menu file.

    # Test for important tags.
    for my $tag (@req_tags) {
        unless (exists($vals{$tag}) && defined($vals{$tag})) {
            $self->pointed_hint('menu-item-missing-required-tag',
                $pointer, $tag);
            # Just give up right away, if such an essential tag is missing,
            # chance is high the rest doesn't make sense either. And now all
            # following checks can assume those tags to be there
            return;
        }
    }

    # Make sure all tags are known.
    for my $tag (keys %vals) {
        if (!$known_tags_hash{$tag}) {
            $self->pointed_hint('menu-item-contains-unknown-tag',
                $pointer, $tag);
        }
    }

    # Sanitize the section tag
    my $section = $vals{'section'};
    $section =~ tr:/:/:s;       # eliminate duplicate slashes. # Hallo emacs ;;
    $section =~ s{/$}{}         # remove trailing slash
      unless $section eq $SLASH; # - except if $section is '/'

    # Be sure the command is provided by the package.
    my ($okay, $command)
      = $self->verify_cmd($pointer, $vals{'command'});

    $self->pointed_hint('menu-command-not-in-package', $pointer, $command)
      if !$okay
      && length $command
      && $tested_packages < 2
      && $section !~ m{^(?:WindowManagers/Modules|FVWM Modules|Window Maker)};

    if (length $command) {
        $command =~ s{^(?:usr/)?s?bin/}{};
        $command =~ s{^usr/games/}{};

        $self->pointed_hint('command-in-menu-file-and-desktop-file',
            $pointer, $command)
          if $desktop_cmds->{$command};
    }

    $self->verify_icon('icon', $vals{'icon'},$MAXIMUM_SIZE_STANDARD_ICON,
        $pointer);
    $self->verify_icon('icon32x32', $vals{'icon32x32'},
        $MAXIMUM_SIZE_32X32_ICON, $pointer);
    $self->verify_icon('icon16x16', $vals{'icon16x16'},
        $MAXIMUM_SIZE_16X16_ICON, $pointer);

    # needs is case insensitive
    my $needs = lc($vals{'needs'});

    if ($section =~ m{^(WindowManagers/Modules|FVWM Modules|Window Maker)}) {
        # WM/Modules: needs must not be the regular ones nor wm
        $self->pointed_hint('non-wm-module-in-wm-modules-menu-section',
            $pointer, $needs)
          if $needs_tag_vals_hash{$needs} || $needs eq 'wm';

    } elsif ($section =~ m{^Window ?Managers}) {
        # Other WM sections: needs must be wm
        $self->pointed_hint('non-wm-in-windowmanager-menu-section',
            $pointer, $needs)
          unless $needs eq 'wm';

    } else {
        # Any other section: just only the general ones
        if ($needs eq 'dwww') {
            $self->pointed_hint('menu-item-needs-dwww', $pointer);

        } elsif (!$needs_tag_vals_hash{$needs}) {
            $self->pointed_hint('menu-item-needs-tag-has-unknown-value',
                $pointer, $needs);
        }
    }

    # Check the section tag
    # Check for historical changes in the section tree.
    if ($section =~ m{^Apps/Games}) {
        $self->pointed_hint('menu-item-uses-apps-games-section', $pointer);
        $section =~ s{^Apps/}{};
    }

    if ($section =~ m{^Apps/}) {
        $self->pointed_hint('menu-item-uses-apps-section', $pointer);
        $section =~ s{^Apps/}{Applications/};
    }

    if ($section =~ m{^WindowManagers}) {
        $self->pointed_hint('menu-item-uses-windowmanagers-section', $pointer);
        $section =~ s{^WindowManagers}{Window Managers};
    }

    # Check for Evil new root sections.
    my ($rootsec, $sect) = split(m{/}, $section, 2);

    my $root_data = $self->MENU_SECTIONS->{$rootsec};

    if (!defined $root_data) {

        my $pkg = $self->processable->name;
        $self->pointed_hint('menu-item-creates-new-root-section',
            $pointer, $rootsec)
          unless $rootsec =~ /$pkg/i;

    } else {

        $self->pointed_hint('menu-item-creates-new-section',
            $pointer, $vals{section})
          if (length $sect && !exists $root_data->{$sect})
          || (!length $sect && !exists $root_data->{$EMPTY});
    }

    return;
}

sub verify_icon {
    my ($self, $tag, $name, $size, $pointer)= @_;

    return
      unless length $name;

    if ($name eq 'none') {

        $self->pointed_hint('menu-item-uses-icon-none', $pointer, $tag);
        return;
    }

    $self->pointed_hint('menu-icon-uses-relative-path', $pointer, $tag, $name)
      unless $name =~ s{^/+}{};

    if ($name !~ /\.xpm$/i) {

        $self->pointed_hint('menu-icon-not-in-xpm-format',
            $pointer, $tag, $name);
        return;
    }

    my @packages = (
        $self->processable,
        @{ $self->group->direct_dependencies($self->processable) }
    );

    my @candidates;
    for my $processable (@packages) {

        push(@candidates, $processable->installed->resolve_path($name));
        push(@candidates,
            $processable->installed->resolve_path("usr/share/pixmaps/$name"));
    }

    my $iconfile = first_value { defined } @candidates;

    if (!defined $iconfile || !$iconfile->is_open_ok) {

        $self->pointed_hint('menu-icon-missing', $pointer, $tag, $name);
        return;
    }

    open(my $fd, '<', $iconfile->unpacked_path)
      or die encode_utf8('Cannot open ' . $iconfile->unpacked_path);

    my $parse = 'XPM header';

    my $line;
    do { defined($line = <$fd>) or goto PARSE_ERROR; }
      until ($line =~ /\/\*\s*XPM\s*\*\//);

    $parse = 'size line';

    do { defined($line = <$fd>) or goto PARSE_ERROR; }
      until ($line =~ /"\s*([0-9]+)\s*([0-9]+)\s*(?:[0-9]+)\s*(?:[0-9]+)\s*"/);
    my $width = $1 + 0;
    my $height = $2 + 0;

    if ($width > $size || $height > $size) {
        $self->pointed_hint('menu-icon-too-big', $pointer, $tag,
            "$name: ${width}x${height} > ${size}x${size}");
    }

    close($fd);

    return;

  PARSE_ERROR:
    close($fd);
    $self->pointed_hint('menu-icon-cannot-be-parsed', $pointer, $tag,
        "$name: looking for $parse");

    return;
}

# Syntax-checks a .desktop file.
sub verify_desktop_file {
    my ($self, $item, $desktop_cmds) = @_;

    my ($saw_first, $warned_cr, %vals, @pending);
    open(my $fd, '<', $item->unpacked_path)
      or die encode_utf8('Cannot open ' . $item->unpacked_path);

    my $position = 1;
    while (my $line = <$fd>) {

        chomp $line;

        my $pointer = $item->pointer($position);

        next
          if $line =~ /^\s*\#/ || $line =~ /^\s*$/;

        if ($line =~ s/\r//) {
            $self->pointed_hint('desktop-entry-file-has-crs', $pointer)
              unless $warned_cr;
            $warned_cr = 1;
        }

        # Err on the side of caution for now.  If the first non-comment line
        # is not the required [Desktop Entry] group, ignore this file.  Also
        # ignore any keys in other groups.
        last
          if $saw_first && $line =~ /^\[(.*)\]\s*$/;

        unless ($saw_first) {
            return
              unless $line =~ /^\[(KDE )?Desktop Entry\]\s*$/;
            $saw_first = 1;
            $self->pointed_hint('desktop-contains-deprecated-key', $pointer)
              if $line =~ /^\[KDE Desktop Entry\]\s*$/;
        }

        # Tag = Value.  For most errors, just add the error to pending rather
        # than warning on it immediately since we want to not warn on tag
        # errors if we didn't know the file type.
        #
        # TODO: We do not check for properly formatted localised values for
        # keys but might be worth checking if they are properly formatted (not
        # their value)
        if ($line =~ /^(.*?)\s*=\s*(.*)$/) {
            my ($tag, $value) = ($1, $2);
            my $basetag = $tag;
            $basetag =~ s/\[([^\]]+)\]$//;
            if (exists $vals{$tag}) {
                $self->pointed_hint('duplicate-key-in-desktop', $pointer,$tag);
            } elsif ($self->DEPRECATED_DESKTOP_KEYS->recognizes($basetag)) {
                if ($basetag eq 'Encoding') {
                    push(@pending,
                        ['desktop-entry-contains-encoding-key',$pointer, $tag]
                    );
                } else {
                    push(
                        @pending,
                        [
                            'desktop-entry-contains-deprecated-key',
                            $pointer, $tag
                        ]
                    );
                }
            } elsif (not $self->KNOWN_DESKTOP_KEYS->recognizes($basetag)
                and not $self->KDE_DESKTOP_KEYS->recognizes($basetag)
                and not $basetag =~ /^X-/) {
                push(@pending,
                    ['desktop-entry-contains-unknown-key', $pointer, $tag]);
            }
            $vals{$tag} = $value;
        }

    } continue {
        ++$position;
    }

    close($fd);

    # Now validate the data in the desktop file, but only if it's a known type.
    # Warn if it's not.
    my $type = $vals{'Type'};
    return
      unless defined $type;

    unless ($known_desktop_types{$type}) {
        $self->pointed_hint('desktop-entry-unknown-type', $item->pointer,
            $type);
        return;
    }

    $self->pointed_hint(@{$_}) for @pending;

    # Test for important keys.
    for my $tag (@req_desktop_keys) {
        unless (defined $vals{$tag}) {
            $self->pointed_hint('desktop-entry-missing-required-key',
                $item->pointer, $tag);
        }
    }

    # test if missing Keywords (only if NoDisplay is not set)
    if (!defined $vals{NoDisplay}) {

        $self->pointed_hint('desktop-entry-lacks-icon-entry', $item->pointer)
          unless defined $vals{Icon};

        $self->pointed_hint('desktop-entry-lacks-keywords-entry',
            $item->pointer)
          if !defined $vals{Keywords} && $vals{'Type'} eq 'Application';
    }

    # Only test whether the binary is in the package if the desktop file is
    # directly under /usr/share/applications.  Too many applications use
    # desktop files for other purposes with custom paths.
    #
    # TODO:  Should check quoting and the check special field
    # codes in Exec for desktop files.
    if (   $item->name =~ m{^usr/share/applications/}
        && $vals{'Exec'}
        && $vals{'Exec'} =~ /\S/) {

        my ($okay, $command)
          = $self->verify_cmd($item->pointer, $vals{'Exec'});

        $self->pointed_hint('desktop-command-not-in-package',
            $item->pointer, $command)
          unless $okay
          || $command eq 'kcmshell';

        $command =~ s{^(?:usr/)?s?bin/}{};
        $desktop_cmds->{$command} = 1
          unless $command =~ m/^(?:su-to-root|sux?|(?:gk|kde)su)$/;
    }

    # Check the Category tag.
    my $in_reserved;
    if (defined $vals{'Categories'}) {

        my $saw_main;

        my @categories = split(/;/, $vals{'Categories'});
        for my $category (@categories) {

            next
              if $category =~ /^X-/;

            if ($reserved_categories{$category}) {
                $self->pointed_hint('desktop-entry-uses-reserved-category',
                    $item->pointer,$category)
                  unless $vals{'OnlyShowIn'};

                $saw_main = 1;
                $in_reserved = 1;

            } elsif (!$self->ADD_CATEGORIES->recognizes($category)
                && !$main_categories{$category}) {
                $self->pointed_hint('desktop-entry-invalid-category',
                    $item->pointer, $category);

            } elsif ($main_categories{$category}) {
                $saw_main = 1;
            }
        }

        $self->pointed_hint('desktop-entry-lacks-main-category',$item->pointer)
          unless $saw_main;
    }

    # Check the OnlyShowIn tag.  If this is not an application in a reserved
    # category, warn about any desktop entry that specifies OnlyShowIn for
    # more than one environment.  In that case, the application probably
    # should be using NotShowIn instead.
    if (defined $vals{OnlyShowIn} and not $in_reserved) {
        my @envs = split(/;/, $vals{OnlyShowIn});
        if (@envs > 1) {
            $self->pointed_hint('desktop-entry-limited-to-environments',
                $item->pointer);
        }
    }

    # Check that the Exec tag specifies how to pass a filename if MimeType
    # tags are present.
    if ($item->name =~ m{^usr/share/applications/}
        && defined $vals{'MimeType'}) {

        $self->pointed_hint('desktop-mime-but-no-exec-code', $item->pointer)
          unless defined $vals{'Exec'}
          && $vals{'Exec'} =~ /(?:^|[^%])%[fFuU]/;
    }

    return;
}

# Verify whether a command is shipped as part of the package.  Takes the full
# path to the file being checked (for error reporting) and the binary.
# Returns a list whose first member is true if the command is present and
# false otherwise, and whose second member is the command (minus any leading
# su-to-root wrapper).  Shared between the desktop and menu code.
sub verify_cmd {
    my ($self, $pointer, $exec) = @_;

    my $index = $self->processable->installed;

    # This routine handles su wrappers.  The option parsing here is ugly and
    # dead-simple, but it's hopefully good enough for what will show up in
    # desktop files.  su-to-root and sux require -c options, kdesu optionally
    # allows one, and gksu has the command at the end of its arguments.
    my @components = split($SPACE, $exec);
    my $cmd;

    $self->pointed_hint('su-to-root-with-usr-sbin', $pointer)
      if $components[0] && $components[0] eq '/usr/sbin/su-to-root';

    if (   $components[0]
        && $components[0] =~ m{^(?:/usr/s?bin/)?(su-to-root|gksu|kdesu|sux)$}){

        my $wrapper = $1;
        shift @components;

        while (@components) {
            unless ($components[0]) {
                shift @components;
                next;
            }

            if ($components[0] eq '-c') {
                $cmd = $components[1];
                last;

            } elsif (
                $components[0] =~ /^-[Dfmupi]|^--(user|description|message)/) {
                shift @components;
                shift @components;

            } elsif ($components[0] =~ /^-/) {
                shift @components;

            } else {
                last;
            }
        }

        if (!$cmd && $wrapper =~ /^(gk|kde)su$/) {
            if (@components) {
                $cmd = $components[0];
            } else {
                $cmd = $wrapper;
                undef $wrapper;
            }
        }

        $self->pointed_hint('su-wrapper-without--c', $pointer, $wrapper)
          unless $cmd;

        $self->pointed_hint('su-wrapper-not-su-to-root', $pointer, $wrapper)
          if $wrapper
          && $wrapper !~ /su-to-root/
          && $wrapper ne $self->processable->name;

    } else {
        $cmd = $components[0];
    }

    my $cmd_file = $cmd;
    if ($cmd_file) {
        $cmd_file =~ s{^/}{};
    }

    my $okay = $cmd
      && ( $cmd =~ /^[\'\"]/
        || $index->lookup($cmd_file)
        || $cmd =~ m{^(/bin/)?sh}
        || $cmd =~ m{^(/usr/bin/)?sensible-(pager|editor|browser)}
        || any { $index->lookup($_ . $cmd) } @path);

    return ($okay, $cmd_file);
}

1;

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