vfmg (DEVEL): vfmg - use resursion for menu file parsing - killed ...
sparky
sparky at pld-linux.org
Sat Nov 5 16:41:25 CET 2005
Author: sparky Date: Sat Nov 5 15:41:25 2005 GMT
Module: vfmg Tag: DEVEL
---- Log message:
- use resursion for menu file parsing
- killed some global variables
---- Files affected:
vfmg:
vfmg (1.73.2.28 -> 1.73.2.29)
---- Diffs:
================================================================
Index: vfmg/vfmg
diff -u vfmg/vfmg:1.73.2.28 vfmg/vfmg:1.73.2.29
--- vfmg/vfmg:1.73.2.28 Sat Nov 5 00:45:29 2005
+++ vfmg/vfmg Sat Nov 5 16:41:20 2005
@@ -447,15 +447,9 @@
my @menu; # $menu[parent]{name}=[x=menu number,icon]
# $menu[parent]{name}=[-1=entry,icon,exec]
-my $level=0;
my $mno=0; # menu number
-my $trigger=0;
-my $tag;
my $name="";
-my $parent="";
-my $dirfile;
my $dir;
-my $include;
my $tmpapps;
my @menustack;
@@ -465,7 +459,7 @@
my $name;
my $firstrun=1;
while (1) {
- $tag=lc(gettag());
+ my $tag = lc(gettag());
last if($tag eq '/and');
if($tag eq 'category') {
$dir=getname();
@@ -511,7 +505,7 @@
my @iapps; # internal applications list
my $name;
while (1) {
- $tag=lc(gettag());
+ my $tag=lc(gettag());
last if($tag eq '/or');
if($tag eq 'category') {
$dir=getname();
@@ -544,7 +538,7 @@
my $name;
@iapps=@apps;
while (1) {
- $tag=lc(gettag());
+ my $tag=lc(gettag());
last if($tag eq '/not');
if($tag eq 'category') {
$dir=getname();
@@ -576,163 +570,177 @@
# parse menu file {{{
my @tmp=grep -d, map {"$_/desktop-directories/"} @xdg_data_dirs;
-$#menu++;
-while($trigger==0 || $level>0) {
- $tag=lc(gettag());
- if(($tag eq 'category')&& $include) {
- $dir=getname();
- foreach my $name(0..$#happs) {
- $menu[$mno]{$happs[$name]}=-1
- if exists $apps[$name]{$dir};
+sub include { # {{{
+ while ( (my $tag = lc(gettag())) ne "/include" ) {
+ if($tag eq 'category') {
+ $dir=getname();
+ foreach my $name(0..$#happs) {
+ $menu[$mno]{$happs[$name]}=-1
+ if exists $apps[$name]{$dir};
+ }
+ gettag(); # must be </category> else GIGO and we don't care
+ next;
}
- gettag(); # must be </category> else GIGO and we don't care
- next;
- }
- if(($tag=~/^(and|or|not)$/)&& $include) {
- if ($tag eq 'or') {
- $tmpapps=cor();
- } elsif ($tag eq 'and') {
- $tmpapps=cand();
- } else {
- $tmpapps=cnot();
- }
- foreach my $name(0..$#$tmpapps) {
- next unless defined $tmpapps->[$name];
- foreach my $dir (keys %{$apps[$name]}) {
- $menu[$mno]{$happs[$name]}=-1;
+ if($tag=~/^(and|or|not)$/) {
+ if ($tag eq 'or') {
+ $tmpapps=cor();
+ } elsif ($tag eq 'and') {
+ $tmpapps=cand();
+ } else {
+ $tmpapps=cnot();
}
+ foreach my $name(0..$#$tmpapps) {
+ next unless defined $tmpapps->[$name];
+ foreach my $dir (keys %{$apps[$name]}) {
+ $menu[$mno]{$happs[$name]}=-1;
+ }
+ }
+ next;
}
- next;
- }
- if($tag eq 'menu') {
- $level++;
- $#menu++;
- $menustack[$#menustack+1]=$mno;
- $mno=$#menu;
- $name="";
- $dirfile="";
- $include=0;
- $trigger=1;
- next;
- }
- if($tag eq '/menu') {
- $mno=$menustack[$#menustack];
- $#menustack--;
- $level--;
- $parent =~ s/(.*):.*?$/$1/;
- next;
- }
- if($tag eq 'include') {
- $include=1;
- next;
- }
- if($tag eq '/include') {
- $include=0;
- next;
- }
- if($tag eq 'name') {
- $name = getname();
- $parent .= ":".$name;
- $menu[$menustack[$#menustack]]{$parent}=$mno;
- if (length $dirfile) {
- $desktop{$parent}=$desktop{""};
- delete $desktop{""};
- } else {
- $desktop{$parent}=[$name,$name,""] unless defined $desktop{$parent};
+ if( $o_verbose &&
+ ( $tag=~/^\/(name|directory|and|or|not|category|mergefile)$/ )) {
+ warn "warning: XDG file corrupted!\n";
}
- gettag(); # must be </name> else GIGO and we don't care
- next;
+ warn "Omitted tag: $tag\n" if $o_tags;
}
- if($tag eq 'directory') {
- $dirfile=getname();
- my $lang=0;
- my $dname="";
- my $icon="";
- my $enc="iso-8859-1";
- my $ok=0;
- foreach my $tmp (@tmp) {
- open F_IN, "$tmp$dirfile" or next;
- my %tags = (
- Name => '',
- Icon => '',
- map {
- ## "foo = bar" or "Name[baz] = bar"
- /^\s*
- (Name(?:\[(?:$langs)\])?
- |Icon|Encoding)
- \s* = \s* (.+?)
- \s*$/ox
- } <F_IN>,
- );
- $dname = $tags{(sort findfirstlang grep /^Name/, keys %tags)[0]};
- $icon = $tags{Icon};
- $enc = $tags{Encoding};
- close F_IN;
- $ok=1;
- last;
- }
- if ($ok) {
- if($opt{icons_full}) {
- my $exists=0;
- foreach my $dir(@icondirs) {
- foreach my $ext ('', qw(.svg .xpm .png)) {
- if (-f $dir . $icon . $ext) {
- $exists=1;
- $icon = $dir . $icon . $ext;
- last;
+} # }}}
+
+sub menu($);
+sub menu($) { # {{{
+ my ($parent_name) = @_;
+ # just in case, will fail if more <menu>'s have no <name> tag
+ my $this_name = $parent_name . ":new";
+ my $name;
+ $#menu++;
+ $menustack[$#menustack+1]=$mno;
+ $mno=$#menu;
+ my $dirfile;
+
+ while ( (my $tag = lc(gettag())) ne "/menu" ) {
+ if($tag eq 'include') {
+ include();
+ next;
+ }
+ if($tag eq 'menu') {
+ menu($this_name);
+ next;
+ }
+ if($tag eq 'name') {
+ $name = getname();
+ $this_name = $parent_name .":". $name;
+ $menu[$menustack[$#menustack]]{$this_name}=$mno;
+ if ( defined $dirfile ) {
+ # $desktop created, but without name
+ my $old_name = $parent_name . ":new";
+ $desktop{$this_name}=$desktop{$old_name};
+ delete $desktop{$old_name . ":new"};
+ }
+ gettag(); # must be </name> else GIGO and we don't care
+ next;
+ }
+ if($tag eq 'directory') {
+ $dirfile=getname();
+ my $lang=0;
+ my $dname="";
+ my $icon="";
+ my $enc="iso-8859-1";
+ my $ok=0;
+ foreach my $tmp (@tmp) {
+ open F_IN, $tmp. $dirfile or next;
+ my %tags = (
+ Name => '',
+ Icon => '',
+ map {
+ ## "foo = bar" or "Name[baz] = bar"
+ /^\s*
+ (Name(?:\[(?:$langs)\])?
+ |Icon|Encoding)
+ \s* = \s* (.+?)
+ \s*$/ox
+ } <F_IN>,
+ );
+ $dname = $tags{(sort findfirstlang grep /^Name/, keys %tags)[0]};
+ $icon = $tags{Icon};
+ $enc = $tags{Encoding};
+ close F_IN;
+ $ok=1;
+ last;
+ }
+ if ($ok) {
+ if($opt{icons_full}) {
+ my $exists=0;
+ foreach my $dir(@icondirs) {
+ foreach my $ext ('', qw(.svg .xpm .png)) {
+ if (-f $dir . $icon . $ext) {
+ $exists=1;
+ $icon = $dir . $icon . $ext;
+ last;
+ }
}
+ last if $exists;
}
- last if $exists;
+ $icon="" unless $exists;
}
- $icon="" unless $exists;
- }
- my $utfname;
- if(length $dname) {
- $utfname=decode($enc,$dname);
- unless($utfname) {
- warn "$dirfile: wrong encoding!\n";
- $utfname=$dname;
+ my $utfname;
+ if(length $dname) {
+ $utfname=decode($enc,$dname);
+ unless($utfname) {
+ warn "$dirfile: wrong encoding!\n";
+ $utfname=$dname;
+ }
+ } else {
+ warn "$dirfile: missing Name tag!\n";
+ $utfname=$name;
}
+ $dirfile =~ s/\..*?$//;
+ $desktop{$this_name} = [$dirfile, $utfname, $icon];
} else {
- warn "$dirfile: missing Name tag!\n";
- $utfname=$name;
+ local $" = "\n- ";
+ warn "No readable $dirfile in:\n- @tmp\n";
+ warn "Last error: $!\n";
}
- $dirfile =~ s/\..*?$//;
- $desktop{$parent} = [$dirfile, $utfname, $icon];
- } else {
- local $" = "\n- ";
- warn "No readable $dirfile in:\n- @tmp\n";
- warn "Last error: $!\n";
+ gettag(); # must be </directory> else GIGO and we don't care
+ next;
}
- gettag(); # must be </directory> else GIGO and we don't care
- next;
- }
- if($tag eq 'mergefile') {
- my $name = getname();
- gettag(); # must be </mergefile> else GIGO and we don't care
- my $ok;
- foreach my $tmp (@xdg_config_dirs, '') {
- open F_IN, "$tmp/$name" or next;
- local $/ = undef;
- ($ok = <F_IN>) =~ y/ \t\r\n//d;
- close F_IN;
- $ok =~ s/<!--.*?-->//g;
- $file = $ok . $file;
- last;
- }
- unless (defined $ok) {
- local $" = "\n- ";
- warn "No readable $name in:\n- @xdg_config_dirs\n";
- warn "Last error: $!\n";
+ if($tag eq 'mergefile') {
+ my $name = getname();
+ gettag(); # must be </mergefile> else GIGO and we don't care
+ my $ok;
+ foreach my $tmp (@xdg_config_dirs, '') {
+ open F_IN, "$tmp/$name" or next;
+ local $/ = undef;
+ ($ok = <F_IN>) =~ y/ \t\r\n//d;
+ close F_IN;
+ $ok =~ s/<!--.*?-->//g;
+ $file = $ok . $file;
+ last;
+ }
+ unless (defined $ok) {
+ local $" = "\n- ";
+ warn "No readable $name in:\n- @xdg_config_dirs\n";
+ warn "Last error: $!\n";
+ }
+ next;
}
- next;
- }
- if( $o_verbose && $include &&
- ( $tag=~/^\/(name|directory|and|or|not|category|mergefile)$/ )) {
- warn "warning: XDG file corrupted!\n";
- }
+ if( $o_verbose &&
+ ( $tag=~/^\/(name|directory|and|or|not|category|mergefile)$/ )) {
+ warn "warning: XDG file corrupted!\n";
+ }
+ warn "Omitted tag: $tag\n" if $o_tags;
+ }
+
+ # if there was no <Directory>
+ $desktop{$this_name}=[$name,$name,""] unless defined $desktop{$this_name};
+
+ $mno=$menustack[$#menustack];
+ $#menustack--;
+} # }}}
+
+$#menu++;
+while( (my $tag = lc(gettag())) ne "menu" ) {
warn "Omitted tag: $tag\n" if $o_tags;
}
+menu("");
warn "Omitted ending: $file\n" if(($file ne "")&& $o_end);
# parse menu file }}}
================================================================
---- CVS-web:
http://cvs.pld-linux.org/vfmg/vfmg?r1=1.73.2.28&r2=1.73.2.29&f=u
More information about the pld-cvs-commit
mailing list