#!/usr/bin/perl -w # # extendFilename # # Given the start of a filename, pick a reasonable file to edit, or # return nil. # # 2007-04-10: Written by Steven J. DeRose, sderose@acm.org. # 2007-12-17 sjd: strict. # # To do: # use strict; use Getopt::Long; my $version = "2010-09-12"; my @extensions = qw / xml sgml sgm gml htm html xsl xslt bxml pxml nxml svg dtd text txt log css pl sh c cc cpp el js java /; # Option values my $quiet = 0; my $verbose = 0; # Process options Getopt::Long::Configure ("ignore_case"); my $result = GetOptions( "h|help|?" => sub { system "perldoc extendFilename"; exit; }, "q|quiet!" => \$quiet, "v|verbose+" => \$verbose, "version" => sub { die "Version of $version, by Steven J. DeRose.\n"; } ); ($result) || die "Bad options.\n"; ############################################################################### # if ($ARGV[0] eq "") { warn "No filename portion specified.\n"; exit 1; } my $file = $ARGV[0]; if (-e $file) { print $file; exit 0; } my @matches = `ls $file* 2>/dev/null`; if ($verbose) { warn "Matching files:\n " . join(" ", @matches); warn "----\n"; } if (scalar(@matches) <= 0) { warn "No files starting with '$file' found.\n"; exit 2; } foreach my $x (@extensions) { ($verbose) && warn "Checking for any with extension '$x'.\n"; foreach my $f (@matches) { chomp $f; if ($f =~ m/\.$x$/) { ($verbose) && warn "Matched '$f'.\n"; print $f; exit 0; } } } warn "No matching files found with useful extensions.\n"; exit 3; ############################################################################### # sub showUsage() { print " =head1 Usage extendFilename [options] [path] Try to extend the path provided (by assuming '*' on the end) to find a matching file. Finds the first one in 'ls', that has any of certain extensions (common ones for text files). =head1 Options =over =item * B<-q> Suppress most messages. =item * B<-v> Add more detailed messages. =item * B<-version> Display version info and exit. =back =head1 Known bugs and limitations Should check extensions in priority order, rather than check for any of them in files' alphabetical order. Perhaps also allow 'ls' sort options. =head1 Related commands 'editfancy' uses this to find a file to edit if needed. =head1 Ownership This work by Steven J. DeRose is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License. For further information on this license, see http://creativecommons.org/licenses/by-sa/3.0/. The author's present email is sderose at acm.org. For the most recent version, see http://www.derose.net/steve/utilities/. =cut "; }