Du bist hier: Snippet-Verzeichnis » Perl (198)
Sprache:

amd2au

Sprache: English
Programmiersprache: Perl
Veröffentlicht von: hbo [nicht registriert]
Letzte Änderung: 15.05.2006
Aufrufe: 967


Beschreibung

Perl script converts an amd.map file to a set of automounter mapsSee comments for limitations and caveats

Code

1 #!/usr/bin/perl 2 # amd2au - Convert an amd.map file to a set of automounter maps. 3 # 4 # This script is free software. You may redistribute it under the same 5 # terms as perl itself. 6 # 7 # This script works with the amd maps I have access to. If it doesn't 8 # work in your environment, help save the world and submit a patch. 9 # I don't currently handle translation of options, but it's on my list. 10 # 11 # Howard Owen - hbo@egbok.com 12 # 13 amd2au("-"); # translate STDIN. Maps are written in CWD 14 15 sub amd2au { 16 17 my $amdmap=shift; # name of file containing amd map 18 my $mapregexp="[a-zA-Z\-]"; # for consistency 19 20 my (@maps,%maps,@lines,%names,%mapentries); 21 my ($map,$subkey,$fs,$rhost,$end,$t,$line); 22 23 local (*AM,*ATMP); 24 25 open ATMP, "$amdmap" || die "Error opening $amdmap $!"; 26 while(<ATMP>){ # look for map names 27 chomp; 28 push @lines,$_; # and save contents 29 if (/^($mapregexp+)\s+/){ 30 push @maps,$1; # save map names 31 } 32 } 33 close ATMP; 34 35 foreach (@maps){ # Optimistically create subsidiary maps 36 open uc $_, ">auto_$_" || die "Couldn't open auto_$_ $!"; 37 $maps{$_}=uc $_; # symbolic filehandle. need "no strict 'refs'" 38 print { $maps{$_} } "# $_ map for automounter\n"; 39 print { $maps{$_} } "# Automatically generated by amd2au\n"; 40 print { $maps{$_} } "#\n"; 41 } 42 # 43 # Process amd map selecting subkeys 44 # 45 for ($line=shift @lines;$#lines>=0;$line=shift @lines) { 46 $map=$subkey=$fs=$rhost=$end=""; 47 if ($line=~/^($mapregexp+)\/(.*?)\s+/){ # a map entry - non-greedy req'd 48 $map=$1; # map name 49 $subkey=$2;# key under map, eg 'home/elmer' 50 51 $end=substr($line,-1,1);# possibly a continuation character 52 if ($line=~/.*fs:=([^;\s\\]*)[;\s\\]*/){ # filesystem 53 $fs=$1; 54 $fs=~s/\$\{key\}/$map\/$subkey/;# translate magic '${key}' 55 if ($line=~/.*rhost:=([^\s\;]+).*/){ # fs server 56 $rhost=$1; 57 } 58 # if we are missing stuff and we have a continuation mark 59 while (($rhost eq "" || $fs eq "") && $end eq "\\"){ 60 61 $line= shift @lines; # get the next line 62 $end=substr($line,-1,1);# save this line's ending 63 if ($fs eq "" && $line=~/.*fs:=([^\s\\;]*)[\;\s\\]*/){ #filesystem 64 $fs=$1; 65 $fs=~s/\$\{key\}/$map\/$subkey/; # translate magic '${key}' 66 } 67 if ($rhost eq "" && $line=~/.*rhost:=([^\s\;]+).*/){ # fs server 68 $rhost=$1; 69 } 70 } 71 } 72 next if ($rhost eq "" || $fs eq ""); # punt if missing stuff after all 73 print { $maps{$map} } "$subkey\t$rhost:$fs\n";# store the map entry 74 $mapentries{$map}=1; # flag non-empty map 75 } # ignore all other lines 76 } 77 # Tidy up the subsidiary maps 78 foreach (@maps){ 79 close $maps{$_}; # close all the map files; 80 if (exists $mapentries{$_}){# save names with data 81 push @names,$_; 82 } else { 83 unlink "auto_$_"; # remove empty map files 84 } 85 86 } 87 # 88 # now build auto_master 89 # 90 my $am = "auto_master"; 91 open AM, ">$am" || die "Error opening $am $!"; 92 print AM "# Master map for automounter\n"; 93 print AM "#\n"; 94 print AM "+auto_master\n"; 95 print AM "/net\t-hosts\t-nosuid,nobrowse\n"; 96 foreach $map (@names){ 97 print AM "/$map\tauto_$map\t-nobrowse\n"; 98 } 99 print AM "/xfn\t-xfn\n"; 100 close AM; 101 } 102

Noch kein Kommentar vorhanden

Dieses Snippet kommentieren

Name *  

E-Mail (wird nicht angezeigt) *    

Website  

Kommentar *  

Sicherheitscode Sicherheitscode *    

RSS