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: 881


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 # 10 # Version 1.1 is out a day after 1.0 because I got a clue and had a look at 11 # am-utils. Among the scripts is amd2sun, which does something like what 12 # I'm doing with this script. amd2sun missed one translation optimization 13 # and creates a monolithic automount map file, so I felt this script still 14 # had some value. I hacked in much of the guts of amd2sun, which means 15 # this script now processes mount options more or less correctly. 16 # 17 # Howard Owen - hbo@egbok.com 18 # 19 use strict; 20 21 amd2au("-"); # translate STDIN. Maps are written in CWD 22 23 sub amd2au { 24 25 no strict 'refs'; # so we can have symbolic filehandles 26 my $amdmap=shift; # name of file containing amd map 27 my $mapregexp="[a-zA-Z\-]"; # for consistency 28 29 my (@maps,%maps,@lines,@names,%mapentries); 30 my ($map,$key,$subkey,$rest,$rfs,$rhost, 31 $line,$line2,$opts,$defopts,$mapopts); 32 33 local (*AM,*ATMP); 34 35 open ATMP, "$amdmap" || die "Error opening $amdmap $!"; 36 while(<ATMP>){ # look for map names 37 chomp; 38 push @lines,$_; # and save contents 39 if (/^($mapregexp+)\s+/){ 40 push @maps,$1; # save map names 41 } 42 } 43 close ATMP; 44 45 foreach (@maps){ # Optimistically create subsidiary maps 46 open uc $_, ">auto_$_" || die "Couldn't open auto_$_ $!"; 47 $maps{$_}=uc $_; # symbolic filehandle. need "no strict 'refs'" 48 print { $maps{$_} } "# $_ map for automounter\n"; 49 print { $maps{$_} } "# Automatically generated by amd2au\n"; 50 print { $maps{$_} } "#\n"; 51 } 52 # 53 # Process amd map selecting subkeys 54 # Much of this code stolen from amd2sun in am-utils 55 # 56 for ($line=shift @lines;$#lines>=0;$line=shift @lines) { 57 next if ($line=~/^\#/ || $line =~/^$/); 58 $map=$key=$subkey=$rfs=$rhost=""; 59 60 while ($line =~ /\\$/) { # flatten out continued lines 61 chop $line; # munch continuation character 62 $line2 = shift @lines; # get the next line 63 $line2 =~ s/^\s*//; # strip out leading whitespace 64 $line .= $line2; 65 } 66 # a key line - something in column 0 67 next unless $line =~ /^([^\s]+)\s+(.*)$/; 68 69 $key = $1; $rest=$2; 70 # /defaults line 71 if ($key =~ /^\/defaults/) { #grab defopts 72 ($defopts = $rest) =~ s/^.*[\s;]opts:=([^;\s]+)[;\s]*.*$/\1/; 73 next; 74 } 75 76 77 if ($key=~/^($mapregexp+)$/){ # a base map entry 78 if ($rest =~ /opts:=([^;\s]+)[;\s]/) { # check for per-map opts 79 $mapopts=$1; # save 'em if present 80 } else { 81 undef $mapopts; 82 } 83 next; # sun2amd passed these on. We are creating seperate maps however. 84 } 85 if ($key=~/^($mapregexp+)\/(.*?)$/){ # a full key - had better match 86 $map=$1; 87 $subkey=$2; 88 $mapentries{$map}=1; # flag non-empty map 89 } else { 90 next; # Punt! 91 } 92 93 # we must have a subkey now. 94 95 $opts=$defopts; 96 $opts=$mapopts if ($mapopts); # let these override defopts 97 98 if ($rest =~ /opts:=([^;\s]+)[;\s]/) { # and finally per-key opts 99 $opts = $1; 100 } 101 $opts =~ s/,ping=[-\d]+//g; # this was in amd2sun, so it's here too. 102 103 # extract rhost and rfs 104 ($rhost = $rest) =~ s/^.*[\s;]rhost:=([^;\s]+)[;\s]*.*$/\1/; 105 ($rfs = $rest) =~ s/^.*[\s;]rfs:=([^;\s]+)[;\s]*.*$/\1/; 106 # I woulda missed this one... 107 if ($rest =~ /sublink:=([^;\s]+)[;\s]/ ) { 108 $rfs .= "/$1"; 109 } 110 # .. but sun2amd missed this one! 111 $rfs=~s/\$\{key\}/$key/;# translate magic '${key}' 112 113 next if (!($subkey && $rhost && $rfs)); # punt if missing data 114 print { $maps{$map} } "$subkey -$opts $rhost:$rfs\n";# store the map entry 115 } 116 117 # Tidy up the subsidiary maps 118 foreach (@maps){ 119 close $maps{$_}; # close all the map files; 120 if (exists $mapentries{$_}){# save names with data 121 push @names,$_; 122 } else { 123 unlink "auto_$_"; # remove empty map files 124 } 125 126 } 127 # 128 # now build auto_master 129 # 130 my $am = "auto_master"; 131 open AM, ">$am" || die "Error opening $am $!"; 132 print AM "# Master map for automounter\n"; 133 print AM "#\n"; 134 print AM "+auto_master\n"; 135 print AM "/net\t-hosts\t-nosuid,nobrowse\n"; 136 foreach $map (@names){ 137 print AM "/$map\tauto_$map\t-nobrowse\n"; 138 } 139 print AM "/xfn\t-xfn\n"; 140 close AM; 141 } 142

Noch kein Kommentar vorhanden

Dieses Snippet kommentieren

Name *  

E-Mail (wird nicht angezeigt) *    

Website  

Kommentar *  

Sicherheitscode Sicherheitscode *    

RSS