#!/usr/local/bin/perl # # calrh -- a calendar utility # # Copyright (C) 1998, Robert Harlander. All rights reserved. # # Comments, bug reports, and suggestions to # rh@particle.physik.uni-karlsruhe.de # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2, or (at your option) # any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with GNU Emacs; see the file COPYING. If not, write to # the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. # # ---------------------------------------------------------------------- $help = argproc("h(elp)?",0); $edit = argproc("e(dit)?",0); $termin = argproc("t(ermin)?",0); $weekly = argproc("w(eekly)?",0); $quiet = argproc("q(uiet)?",0); $all = argproc("a(ll)?",0); $only = argproc("o(nly)?",0); ($messkey,$message) = argproc("m(essage)?",1); # # $Id: calrh,v 1.5 2004/01/27 14:11:06 rharland Exp $ # $Log: calrh,v $ # Revision 1.5 2004/01/27 14:11:06 rharland # bug fix. # # Revision 1.4 2004/01/26 18:08:41 rharland # bug fixes # # Revision 1.3 2004/01/26 17:15:17 rharland # Id and Log introduced. # # # ---------------------------------------------------------------------- # # Last changes: # # - Jan 26, 2004 (rh, v2.23): breakstring introduced, and ">>>>" removed # - Jan 26, 2004 (rh, v2.22): dayname appears in front of message # - Oct 10, 2003 (rh, v2.21): -o and -m option added # - Oct 10, 2001 (rh): bug fix ('calrh 0' now looks at only TODAY) # - May 5, 1998 (rh): -a and -w option added # - Nov. 10, 1997 (rh): -e and -t option added # # ---------------------------------------------------------------------- if($help) { print( " \n", " \n", " calrh --- Version 2.23 by rh\n", " \n", " \n", " USAGE: calrh [-options] [n]\n", " \n", " DESCRIPTION:\n", " Works approximately like the command 'calendar' in UNIX\n", " so type 'man calendar' for more information.\n", " Note the options below\!\n", " For comments send email to robert.harlander\@cern.ch.\n", " \n", " Possible formats for date:\n", " December 7\n", " December/7\n", " 12/7\n", " 12 7\n", " \n", " You may also wildcard the month:\n", " * 7\n", " \n", " Possible formats for weekly events:\n", " Fr Seminar\n", " Freitag Seminar\n", " Friday Seminar\n", " NOTE: Options \-w or \-a must be given to read weekly ", "events!\n", " \n", " Where there is a blank, you may also use several blanks or tabs.\n", " \n", " ARGUMENTS:\n", " n the number of days to look ahead\n", " Default is 1. If n < 0, it is set to 0.\n\n", " OPTIONS:\n", " -h ............. show these help pages\n", " -e ............. edit the calendar file\n", " -t ............. insert new item to calendar file\n", " -w ............. show ONLY weekly events\n", " -a ............. show ALL events within \'n\' days\n", " -o ............. show ONLY the events \'n\' days ahead\n", " -m ... print \'message\' before event", " \n", " NOTE: \n", " - the format 'Dec. 7' is not implemented\n", # " - the date need not be the first entry in the line\n", # " (this might be changed somewhen, so avoid to use it)\n", " - the date must be the first entry in the line\n", " - each option must be preceded by a '-'", " \n"); exit; } # ---------------------------------------------------------------------- $home = $ENV{HOME}; if ($#ARGV != -1) {$offset = $ARGV[0]} else {$offset = 1} $offsetp = $offset; if (!$messkey) {$message=""}; ;# if ($offset < 0) {$offset = 0} if (-e "$home/calendar") {push(@calfiles,"$home/calendar")} if (-e "$home/.calendar") {push(@calfiles,"$home/.calendar")} unless (@calfiles) {print "You don't have a calendar file.\n"} @monthnames = ("January","February","March","April","May","June","July", "August","September","October","November","December"); if ($edit) {system("$ENV{'EDITOR'} $calfiles[0]"); exit} if ($termin) {print("Monat: "); chop($tmonth = ); print("Tag: "); chop($tday = ); print("Subject: "); chop($tsubject = ); if((grep(/^$tmonth$/,@monthnames) || grep(/^$tmonth$/,(1..12)) || $tmonth =~ /^\*$/) && grep(/^$tday$/,(1..31))) { termin($calfiles[0],$tmonth,$tday,$tsubject)} else {print "Wrong format.\n"; exit} } chop($dayname = `date +%A`); chop($year = `date +%Y`); chop($monthnum = `date +%m`); chop($day = `date +%d`); $monthnum =~ s/0(\d)/$1/; $day =~ s/0(\d)/$1/; $month = $monthnames[$monthnum-1]; if (dayname($dayname,$offsetp,"english") eq "Saturday") {$offsetp += 2} elsif (dayname($dayname,$offsetp,"english") eq "Sunday") {$offsetp += 1} foreach $calfile (@calfiles) { unless (open(FILE,$calfile)) {print "You don't have a calendar file.\n"; exit}; if ($only) {@range = ($offset)} else { if ($offsetp < 0) {@range = reverse($offsetp..-1)} else {@range = (0..$offsetp)} } for $i (@range) { ($daynew,$monthnumnew) = makedate($day+$i,$monthnum,$year); $month = $monthnames[$monthnumnew-1]; $daygerman = dayname($dayname,$i,"german"); $dayenglish = dayname($dayname,$i,"english"); $daygermanshort = substr($daygerman,0,2); $dayenglishshort = substr($dayenglish,0,2); while () { if (!$weekly || $all) { if ( (/^[\t\/\ ]*$month[\t\/\ ]+$daynew[\s\:]/) || (/^[\t\/\ ]*$monthnumnew[\t\/\ ]+$daynew[\s\:]/) || (/^[\t\/\ ]+\*[\t\/\ ]+$daynew[\s\:]/) ) { chop($_); $outstring = ""; $outday = $daygerman; $outstring .= $message." ".$outday.", ".$_; $outstring = breakstring(80,$outstring); unless ($quiet) {print("\a")} print($outstring,"\n"); } } if ($weekly || $all) { if ((/^[\t\/\ ]*$daygerman[\s\:]/) || (/^[\t\/\ ]*$daygermanshort[\s\:]/) || (/^[\t\/\ ]*$dayenglish[\s\:]/) || (/^[\t\/\ ]*$dayenglishshort[\s\:]/)) { chop($_); print("$message $_"); print(" ($daynew. $month)\n"); } } } seek(FILE,1,0); } close(FILE) } sub breakstring { # # breakstring($maxlength,$string) # # Returns $string with lines shorther than maxlength. # my($string,$newstring,@strings,$maxlength); $maxlength = shift; $string = shift; $string =~ s/ *$//; @strings = split(/ /,$string); $newstring = shift(@strings); $settled = 0; $breaks=0; foreach $s (@strings) { if (length($s) >= $maxlength) { $newstring .= "\n"." $s"; $breaks += length($newstring) % 80; $settled = length("$newstring")-$breaks; } elsif (length("$newstring"." $s")-$settled > $maxlength) { $settled = length("$newstring")-$breaks; $newstring .= "\n "."$s"; $breaks++; } else { $newstring .= " $s"; } } return($newstring); } sub termin { my($file,$tmonth,$tday,$tsubject); ($file,$tmonth,$tday,$tsubject) = @_; open(FILE,">>$file"); print {FILE} ("$tmonth $tday $tsubject\n"); close(FILE) } ;# ---------------------------------------------------------------------- sub makedate { my($day,$month,$year); $day = shift; $month = shift; $year = shift; while ($day < 0) { if (($year % 400 == 0) || (($year % 4 == 0) && ($year % 100 != 0))) { $day += 366; } else {$day += 365} $year -= 1; } DLOOP: while ($day > 28) { if (($day > 28) && ($month == 2)) { if (($year % 400 == 0) || (($year % 4 == 0) && ($year % 100 != 0))) { if ($day > 29) { $day -= 29; $month += 1; } else {last DLOOP} } else { $day -= 28; $month += 1; } } elsif (($day > 30) && ("$month" =~ /^0?(4|6|9|11)$/)) { $day -= 30; $month += 1; } elsif ($day > 31) { $day -= 31; $month += 1; } else {last DLOOP} while ($month > 12) { $month -= 12; $year += 1; } } return($day,$month,$year); } sub dayname { # # dayname($dayname,$offset) # # Returns the name of the day $offset days from $dayname # Examples: dayname("Monday",2) -> "Wednesday" # dayname("Tuesday",8) -> "Wednesday" # dayname("Tuesday",8,"german") -> "Mittwoch" # my(@daynames,$dayname,%daynums); unless ($language = $_[2]) {$language = "german"}; %daynums = (Monday => 1, Tuesday => 2, Wednesday => 3, Thursday => 4, Friday => 5, Saturday => 6, Sunday => 7); %daynames = (english => ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"], german => ["Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag", "Sonntag"], french => ["Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi", "Dimanche"]); $dayname = ${$daynames{$language}}[($daynums{"$_[0]"} + $_[1] - 1) % 7]; return($dayname) } sub argproc { # # by rh # # get the options from the argument list # (more flexible than PERL-builtin options management) # # Usage: argproc($optkey,$optnum) # # $optkey is the name of the option (may be a pattern) # $optnum is the number of arguments for the option # # Example: argproc("d(efault)?",1) together with the program call # 'program -d test1 test2' returns the list (-d,test1). # @ARGV is (test2) afterwards. # my($optkey,$optnum,$val,@optlist); $optkey = $_[0]; $optnum = $_[1]; foreach $val (0..$#ARGV) { if ($ARGV[$val] =~ m/^-$optkey$/) { @optlist = splice(@ARGV,$val,$optnum+1); last; } } return(@optlist); } sub min { ;#; min(ARRAY) returns minimum of ARRAY my($OUT,@IN); $OUT = $_[0]; @IN = @_; for (@IN) { if ($_ < $OUT) { $OUT = $_} } return($OUT); }