#!/usr/local/bin/perl -w # # phone # # Look up a name in the phone book # # The awk version of this program allowed for searching on everything # except the ResidenceName and the InResidence fields. This only searches # based upon the name, which is how I use this in practice. # # The home for this program is at: # http://mordred.ao.com/phone.list/ # # Dave Regan # 9 May 2000 # regan@mordred.ao.com # use strict; ### ### Configuration ### use vars qw(@Attr $DataFile @Titles $Version); $Version = 'phone v0.10 regan@ao.com'; $DataFile = $ENV{'HOME'} . '/Notes/phone.dat'; # Text data file. # Build an appropriate set of attributes and titles @Attr = ("E-mail", "Address1", "Address2", "WorksAt", "Phone", "WorkPhone", "CellPhone", "Pager", "Modem", "Fax", "Reference", "URL", "Comment1", "Comment2", "Birthday", "Anniversary"); @Titles = ("E-mail:", "Address", "", "Works at:", "Phone:", "Work Phone:", "Cellular Phone", "Pager:", "Modem:", "Fax:", "Reference:", "URL:", "Comment:", "", "Birthday:", "Anniversary:"); ### ### Main program ### my(@allkeys, $fname, $fullname, $idx, @matches, $name, %phone, %tokens); # Get the database open $fname = MakeDbCurrent(); dbmopen(%phone, $fname, 0666) || die "Cannot create the DBM file $fname: $!"; # Process each of the requests in turn @allkeys = keys(%phone); foreach $name (@ARGV) { @matches = grep(/$name/i, @allkeys); foreach $fullname (@matches) { %tokens = split(/[\n=]/, $phone{$fullname}); undef $tokens{'Birthday'} if (defined($tokens{'Birthday'}) && $tokens{'Birthday'} eq "None"); printf ("%-16.16s %s\n", "Name:", $fullname); for ($idx = 0; $idx < scalar(@Attr); $idx++) { printf("%-16.16s %s\n", $Titles[$idx], $tokens{$Attr[$idx]}) if (defined($tokens{$Attr[$idx]})); } print "\n"; } } ### ### MakeDbCurrent ### ### See if the database is current. If so, we are done. ### Otherwise, rebuild the DBM database. ### sub MakeDbCurrent { my($basename, $fname, $name, %phone, $val); # Ensure that the database file is up to date. ($basename = $DataFile) =~ s/\.dat$//; $fname = $basename; # Some dbm files have different file types. return $fname if (-e $fname && -M $DataFile >= -M $fname); # Get rid of old files unlink("$basename.dir", "$basename.pag", "$basename.db"); open(TEXT, "<$DataFile") || die "Cannot find the datafile $DataFile: $!"; dbmopen(%phone, $fname, 0666) || die "Cannot create the DBM file $fname: $!"; undef %phone; $val = ""; while () { chomp; # Go through the text file looking at records, and building # up a DBM record. if (/^%%$/) { print "Duplicate name $name\n" if (defined($phone{$name}) && $phone{$name} ne ""); $phone{$name} = $val; $val = ""; } elsif (/^Name=/) { $name = $_; $name =~ s/^Name=//; $val = ""; } elsif (/=/) { $val .= "$_\n"; } } dbmclose(%phone); return $fname; }