#!/usr/bin/perl -w # # file: /home/frink/code/scripts/todo.pl # purpose: manage my TODO file. # # chad c d clark < email: frink-todo.pl *at* superfrink *dot* com > # created: 2005-03-26 # updated: 2006-01-24 added -e , -h , -l , -p # $Id$ use strict; use Getopt::Long; sub list_file($); sub usage_mesage(); # -- set the global settings -- my %G; $G{editor} = $ENV{EDITOR} || "vi"; # editor to use $G{list_only} = 0; # dump file contents $G{list_pager} = $ENV{PAGER} || "more"; # output pager program $G{todo_file} = "/home/chad/TODO"; # TODO file $G{usage_only} = 0; # display usage message # -- get the command line args -- GetOptions ("e|editor=s" => \$G{editor}, "h|help" => \$G{usage_only}, "l|list" => \$G{list_only}, "p|pager=s" => \$G{list_pager}) or die("Problem with command line options. $!\n"); # -- now take the requested action -- # if a usage message was requested print one and exit if($G{usage_only}) { usage_mesage(); exit(0); } # if a contents list was requested then list the file and exit if($G{list_only}) { list_file($G{todo_file}); exit(0); } # -- if we get here we need to open the file for editing -- # make sure the file exists. unless(-e $G{todo_file}) { `touch $G{todo_file}`; } # open the file for editing. my $cmd = "$G{editor} $G{todo_file}"; print $cmd, "\n"; exec $cmd; # -- we should never get here -- die("$0 : exec($cmd) failed. $!\n"); # -- subroutines ---------------------------------------------------- sub usage_mesage() { # try to get a shorter version of the current program name. # (some people might not have File::Basename.) my $me = $0; if ($0 =~ m/^(.*\/)(.*)$/) { $me = $2; } print qq{ usage: $0 [options] where [options] are: -e , --editor specify an alternate editor like "vi" -h , --help this usage message -l , --list list the file contents instead of editing -p , --pager specify an alternate pager program like "more" or "cat" examples: $me $me -l $me -e vi $me -l -p head $me -l -p cat $me -l -p more $me -l -p "mail -S \\\"TODO reminders\\\" me\@domain.com" }; } sub list_file($) { # purpose: dump the file using an optional pager program # globals used: # $G{list_pager} - pager program, eg "less" or "cat" my $fname = shift; unless ($fname) { die ("No file specified.\n"); } unless (-e $fname) { die ("File does not exist '$fname'.\n"); } unless (-r $fname) { die ("File is not readable '$fname'.\n"); } # open the input file open FH , "<$fname" or die("Unable to open file '$fname'. $!\n"); # if there is a pager program then use it if($G{list_pager}) { open PAGER , "| $G{list_pager}" or die("Unable to open file '$G{list_pager}'. $!\n"); while() { print PAGER; } close PAGER; # else there is no pager program so just print the file contents } else { while() { print; } } # close the input file close FH; }