Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion cal3 (csh script), revisited

Newsgroups: alt.sources
Path: sparky!uunet!europa.asd.contel.com!news.ans.net!yktnews!admin!curt
From: c...@watson.ibm.com (Curt McDowell)
Subject: Re: cal3 (csh script), revisited
Sender: n...@watson.ibm.com (NNTP News Poster)
Message-ID: <1992May20.024200.25784@watson.ibm.com>
Date: Wed, 20 May 1992 02:42:00 GMT
Lines: 114
Disclaimer: This posting represents the poster's views, not necessarily those of IBM
References: <1992May19.001438.26821@watson.ibm.com> <1992May19.051325.3392@news.eng.convex.com> <1992May19.170405.342@porthos.cc.bellcore.com>
Nntp-Posting-Host: gorby.watson.ibm.com
Organization: IBM T.J. Watson Research Center

>>Though csh is clearly brain-dead, I switched to it long ago because sh used
>>to have some serious deficiencies, like no built-in echo.
>Later, in the csh script:
>>	/bin/sh -c "echo 'cal3: Bad parameter.' 1>&2"
>Doncha just love it?
>Kenneth Herron                                       kher...@ms.uky.edu

So, csh is clearly brain-dead.  And this is just about all /bin/sh is worth!

>########
># cal3 - 3-month calendar printer
>########
>...

Actually, Joe's sh script works very well, esp. if # is not the first char
in the file, and it's much faster than the csh script.

>The following program is smaller (and thus easier to read and understand),
>faster, uses no tempfiles, and gives you today in vt100-style inverse
>video.  Adding termcap support or command line parsing would be trivial.
>--tom

This stuff is so simple that there's hardly any readability problem to
begin with... and Joe's is the smallest and probably the least readable.
And you guys keep throwing out the error checking.

Hmmm... It seems that perl comes with IBM AIX 3.2.  There may be some real
hope for perl!  That being the case, I started fiddling with your script and,
seeing as this is alt.sources and not alt.sources.d, I feel obligated to post
the result :-).  You shouldn't gripe about how big it's gotten... everything
has a purpose... it always does the Right Thing.

Shall we put this one to rest?
                                             O O
Curt McDowell                                 >
IBM T. J. Watson Research Center             \_/

----------------------------------------------------------------------------

#!/usr/bin/perl

#
# cal3 - print out last month, this month, and next month from
#	 cal program, with today in reverse video (vt100 hardcoded)
#
# If you have tput, change the following variable to the correct path.
# Tput is available from comp.sources.unix archives (ftp.uu.net,
# j.cc.purdue.edu, etc.) as v07i072.

$tput  = "/usr/5bin/tput";	# Set for local environment
$width = 24;

if ( -x $tput ) {
    open(smso, $tput." smso; echo |"); chop($SO = <smso>);
    open(rmso, $tput." rmso; echo |"); chop($SE = <rmso>);
} else {
    $SO = "\033[7m";		# Default to vt100
    $SE = "\033[m";
}

($sec,$min,$hour,$mday,$curmon,$curyear,$wday,$yday,$isdst) = localtime(time);
$curmon++;

$mon = $curmon; $year = $curyear;

if ( $#ARGV >= 0 ) { $mon  = $ARGV[0]; }
if ( $#ARGV >= 1 ) { $year = $ARGV[1]; }

# Allow omission of century up to the year 2069.

if ( $curyear < 70  ) { $curyear += 2000; }
if ( $curyear < 100 ) { $curyear += 1900; }
if ( $year    < 70  ) { $year    += 2000; }
if ( $year    < 100 ) { $year    += 1900; }

if ( $mon < 1 || $mon > 12 || $year < 1 || $year > 9999 ) {
    printf(<STDERR>, "cal3: Illegal parameter.\n");
    exit(1);
}

# Select next year and prev year "wrapping" at 12 months.

$nmon = $mon + 1; $nyear = $year; if ( $nmon == 13 ) { $nmon =  1; $nyear++; }
$pmon = $mon - 1; $pyear = $year; if ( $pmon ==  0 ) { $pmon = 12; $pyear--; }

open (prev, "cal $pmon $pyear |");
open (cur,  "cal $mon $year |"  );
open (foll, "cal $nmon $nyear |");

# Determine which month, if any, will contain a highlighted day.

$hiprev = ( $pmon == $curmon && $pyear == $curyear );
$hicur  = ( $mon  == $curmon && $year  == $curyear );
$hifoll = ( $nmon == $curmon && $nyear == $curyear );

until (eof(prev) && eof(cur) && eof(foll)) {
    chop($prev = <prev>); $lenprev = length($prev);
    chop($cur  = <cur> ); $lencur  = length($cur);
    chop($foll = <foll>); $lenfoll = length($foll);
    if ( $hiprev ) { $prev =~ s/\b$mday\b/$SO$mday$SE/; };
    if ( $hicur  ) { $cur  =~ s/\b$mday\b/$SO$mday$SE/; };
    if ( $hifoll ) { $foll =~ s/\b$mday\b/$SO$mday$SE/; };
    printf (sprintf ("%%-%ds%%-%ds%%-%ds\n",
	$width + length($prev) - $lenprev,
	$width + length($cur ) - $lencur,
	$width + length($foll) - $lenfoll), $prev, $cur, $foll);
}