Pcal: postscript/c calendar printer

Joseph P. Larson joe at dayton.UUCP
Fri Jan 12 09:32:22 AEST 1990


(This is an enhanced and repaired version of a program by the same name
posted by Ken Keirnan last summer or so.)

This is "pcal", a program that lets you print a monthly calendar complete with
text taken from a calendar file.  See the two ReadMe files and the man page
for more information.  -Joe

#! /bin/sh
# This is a shell archive, meaning:
# 1. Remove everything above the #! /bin/sh line.
# 2. Save the resulting text in a file.
# 3. Execute the file with /bin/sh (not csh) to create:
#	Makefile
#	ReadMe
#	ReadMe.orig
#	pcal.c
#	pcal.man
# This archive created: Thu Jan 11 16:22:36 1990
export PATH; PATH=/bin:/usr/bin:$PATH
if test -f 'Makefile'
then
	echo shar: "will not over-write existing file 'Makefile'"
else
cat << \SHAR_EOF > 'Makefile'
#
# Normally the day numbers for Saturday and Sunday are printed in gray
# instead of black.  If you want only Sundays in gray, uncomment the
# following line.
#
# COPTS = -DSATBLK
MANDIR=/usr1/jad/man

pcal:	pcal.c
	$(CC) $(CFLAGS) $(LDFLAGS) $(COPTS) -o pcal pcal.c

man:	pcal.man
	nroff -man pcal.man > pcal.1
	pack pcal.1
	mv pcal.1.z $(MANDIR)
SHAR_EOF
fi
if test -f 'ReadMe'
then
	echo shar: "will not over-write existing file 'ReadMe'"
else
cat << \SHAR_EOF > 'ReadMe'
This is pcal as posted some time ago.  However, the "for" loops didn't always
use their arguments.  I found this when I tried to allow a full-year to be
dumped at once.  I'd get through 3 months and *wham*: stack overflow.
Surprise!

Anyways, I put in a couple of pop's, modified the command line a little
and hopefully, the man page enough, and here it is.  The accompanying file
called "ReadMe.orig" came with the original distribution as README and
states this program is copyrighted but with permission to modify and
redistribute.

Note: if I had the time, I'd pull the PostScript code back out of the
program and include a PS prolog file; as is, this is ugly and a little
more difficult to modify.  Ah well.  It may be easier sometimes to include
log prolog inside the C program, but it's an awful lot easier to modify
the prolog when it's a separate file.  Whatever.

-Joe
SHAR_EOF
fi
if test -f 'ReadMe.orig'
then
	echo shar: "will not over-write existing file 'ReadMe.orig'"
else
cat << \SHAR_EOF > 'ReadMe.orig'
"Pcal" is a program to print PostScript calendars for any month and year.
By default, it looks for a file in the home directory named "calendar"
for entries with leading dates matching dates on the calendar, and prints
any following text under the appropriate day.

The program may be a little System V flavored (getopt, time routines)
but should be easily portable to other vintages of UNIX.

Pcal is the combined effort of several people, most notably Patrick Wood
of Pipeline Associates, Inc. for the original PostScript code and Bill
Vogel of AT&T for the calendar file mechanism.  My part was simple
translation to a "C" program, the addition of a couple options and a more
generalized date searching routine (oh yes, and a manual page :-).

The original calendar PostScript was Copyright (c) 1987 by Patrick Wood
and Pipeline Associates, Inc. with permission to modify and redistribute.
Please retain this README file with the package.


Ken Keirnan
Pacific Bell
San Ramon, CA.
SHAR_EOF
fi
if test -f 'pcal.c'
then
	echo shar: "will not over-write existing file 'pcal.c'"
else
cat << \SHAR_EOF > 'pcal.c'
#include <stdio.h>
#include <ctype.h>
#include <time.h>
#include <string.h>

#define PRT	(void)printf
#define FPR	(void)fprintf

char *words[100];	/* maximum number of words on a line */
char lbuf[512];		/* maximum line size */

char *months[] = {	/* used to match alpha months */
	"jan", "feb", "mar", "apr", "may", "jun",
	"jul", "aug", "sep", "oct", "nov", "dec",
	(char *)0,
};

/*
 * pheader - provides the PostScript routines
 */
char *pheader[] = {
  "%!",
  "/titlefont /Times-Bold def",
  "/dayfont /Helvetica-Bold def",
  "/month_names [ (January) (February) (March) (April) (May) (June) (July)",
  "\t\t(August) (September) (October) (November) (December) ] def",
  "/prtnum { 3 string cvs show} def",
  "/drawgrid {\t\t% draw calendar boxes",
  "\tdayfont findfont 10 scalefont setfont",
  "\t0 1 6 {",
  "\t\tdup dup 100 mul 40 moveto",
  "\t\t[ (Sunday) (Monday) (Tuesday) (Wednesday) (Thursday) (Friday) (Saturday) ] exch get",
  "\t\t100 center",
  "\t\t100 mul 35 moveto",
  "\t\t1.0 setlinewidth",
  "\t\t0 1 5 {",
  "\t\t\tgsave",
  "\t\t\t100 0 rlineto ",
  "\t\t\t0 -80 rlineto",
  "\t\t\t-100 0 rlineto",
  "\t\t\tclosepath stroke",
  "\t\t\tgrestore",
  "\t\t\t0 -80 rmoveto",
  "\t\t pop } for",
  "\t} for",
  "} def",
  "/drawnums {\t\t% place day numbers on calendar",
  "\tdayfont findfont 30 scalefont setfont",
  "\t/start startday def",
  "\t/days ndays def",
  "\tstart 100 mul 5 add 10 rmoveto",
  "\t1 1 days {",
  "\t\t/day exch def",
  "\t\tgsave",
#ifndef SATBLK
  "\t\tday start add 7 mod 0 eq",
  "\t\t{",
  "\t\t\tsubmonth 0 eq",
  "\t\t\t{",
  "\t\t\t\t.8 setgray",
  "\t\t\t} if",
  "\t\t} if",
#endif
  "\t\tday start add 7 mod 1 eq",
  "\t\t{",
  "\t\t\tsubmonth 0 eq",
  "\t\t\t{",
  "\t\t\t\t.8 setgray",
  "\t\t\t} if",
  "\t\t} if",
  "\t\tday prtnum",
  "\t\tgrestore",
  "\t\tday start add 7 mod 0 eq",
  "\t\t{",
  "\t\t\tcurrentpoint exch pop 80 sub 5 exch moveto",
  "\t\t}",
  "\t\t{",
  "\t\t\t100 0 rmoveto",
  "\t\t} ifelse",
  "\t} for",
  "} def",
  "/drawfill {\t\t% place fill squares on calendar",
  "\t/start startday def",
  "\t/days ndays def",
  "\t0 35 rmoveto",
  "\t1.0 setlinewidth",
  "\t0 1 start 1 sub {",
  "\t\tgsave",
  "\t\t.9 setgray",
  "\t\t100 0 rlineto ",
  "\t\t0 -80 rlineto",
  "\t\t-100 0 rlineto",
  "\t\tclosepath fill",
  "\t\tgrestore",
  "\t\t100 0 rmoveto",
  "\tpop } for",
  "\tsubmonth 1 eq",
  "\t{",
  "\t\t/lastday 42 def",
  "\t\t600 -365 moveto",
  "\t}",
  "\t{",
  "\t\t/lastday 40 def",
  "\t\t400 -365 moveto",
  "\t} ifelse",
  "\tlastday -1 ndays start 1 add add",
  "\t{",
  "\t\t/day exch def",
  "\t\tgsave",
  "\t\t.9 setgray",
  "\t\t100 0 rlineto ",
  "\t\t0 -80 rlineto",
  "\t\t-100 0 rlineto",
  "\t\tclosepath fill",
  "\t\tgrestore",
  "\t\tday 7 mod 1 eq",
  "\t\t{",
  "\t\t\t600 -365 80 add moveto",
  "\t\t}",
  "\t\t{",
  "\t\t\t-100 0 rmoveto",
  "\t\t} ifelse",
  "\t} for",
  "} def",
  "/isleap {\t\t% is this a leap year?",
  "\tyear 4 mod 0 eq\t\t% multiple of 4",
  "\tyear 100 mod 0 ne \t% not century",
  "\tyear 1000 mod 0 eq or and\t% unless it's a millenia",
  "} def",
  "/days_month [ 31 28 31 30 31 30 31 31 30 31 30 31 ] def",
  "/ndays {\t\t% number of days in this month",
  "\tdays_month month 1 sub get",
  "\tmonth 2 eq\t% Feb",
  "\tisleap and",
  "\t{",
  "\t\t1 add",
  "\t} if",
  "} def",
  "/startday {\t\t% starting day-of-week for this month",
  "\t/off year 2000 sub def\t% offset from start of epoch",
  "\toff",
  "\toff 4 idiv add\t\t% number of leap years",
  "\toff 100 idiv sub\t% number of centuries",
  "\toff 1000 idiv add\t% number of millenia",
  "\t6 add 7 mod 7 add \t% offset from Jan 1 2000",
  "\t/off exch def",
  "\t1 1 month 1 sub {",
  "\t\t/idx exch def",
  "\t\tdays_month idx 1 sub get",
  "\t\tidx 2 eq",
  "\t\tisleap and",
  "\t\t{",
  "\t\t\t1 add",
  "\t\t} if",
  "\t\t/off exch off add def",
  "\t} for",
  "\toff 7 mod\t\t% 0--Sunday, 1--monday, etc.",
  "} def",
  "/center {\t\t% center string in given width",
  "\t/width exch def",
  "\t/str exch def width str ",
  "\tstringwidth pop sub 2 div 0 rmoveto str show",
  "} def",
  "/calendar",
  "{",
  "\ttitlefont findfont 48 scalefont setfont",
  "\t0 60 moveto",
  "\t/month_name month_names month 1 sub get def",
  "\tmonth_name show",
  "\t/yearstring year 10 string cvs def",
  "\t700 yearstring stringwidth pop sub 60 moveto",
  "\tyearstring show",
  "\t0 0 moveto",
  "\tdrawnums",
  "\t0 0 moveto",
  "\tdrawfill",
  "\t0 0 moveto",
  "\tdrawgrid",
  "} def",
  "/daytext {",
  "\t/Helvetica-Narrow findfont 6 scalefont setfont",
  "\t/mytext\texch def /myday exch def",
  "\tstartday myday 1 sub add dup 7 mod 100 mul 5 add % gives column",
  "\texch 7 idiv -80 mul % gives row",
  "\tdup /ypos exch def moveto",
  "\t/LM currentpoint pop def /RM LM 95 add def",
  "        mytext { dup (.p) eq { crlf pop} {prstr ( ) show} ifelse } forall",
  "} def",
  "/crlf {",
  "    ypos 8 sub /ypos exch def LM ypos moveto",
  "} def",
  "/prstr {",
  "    dup stringwidth pop currentpoint pop",
  "    add RM gt {crlf} if show",
  "} def",
  "/printmonth {",
  "\t90 rotate",
  "\t50 -120 translate",
  "\t/submonth 0 def",
  "\tcalendar",
  "\tmonth 1 sub 0 eq",
  "\t{",
  "\t\t/lmonth 12 def",
  "\t\t/lyear year 1 sub def",
  "\t}",
  "\t{",
  "\t\t/lmonth month 1 sub def",
  "\t\t/lyear year def",
  "\t} ifelse",
  "\tmonth 1 add 13 eq",
  "\t{",
  "\t\t/nmonth 1 def",
  "\t\t/nyear year 1 add def",
  "\t} ",
  "\t{",
  "\t\t/nmonth month 1 add def",
  "\t\t/nyear year def",
  "\t} ifelse",
  "\t/savemonth month def",
  "\t/saveyear year def",
  "\t/submonth 1 def",
  "\t/year lyear def",
  "\t/month lmonth def",
  "\tgsave",
  "\t500 -365 translate",
  "\tgsave",
  "\t.138 .138 scale",
  "\t10 -120 translate",
  "\tcalendar",
  "\tgrestore",
  "\t/submonth 1 def",
  "\t/year nyear def",
  "\t/month nmonth def",
  "\t100 0 translate",
  "\tgsave",
  "\t.138 .138 scale",
  "\t10 -120 translate",
  "\tcalendar",
  "\tgrestore",
  "\t/month savemonth def",
  "\t/year saveyear def",
  "\t/submonth 0 def",
  "\tgrestore",
  "} def",
  (char *)0,
};

FILE *cfp = NULL;
int year;
int cyear;

void exit();
char *getenv();

main(argc, argv)
int argc;
char **argv;
{
	extern char *optarg;
	extern int optind;
	register struct tm *lt;
	register char **ap;
	register char *cp;
	char *cfile = NULL;
	char cbuf[80];
	long t, time();
	int errflg = 0;
	int nocal = 0;
	int m;
	int month;
	char doyear = 0;

	while ((m = getopt(argc, argv, "ef:m:y:")) != EOF)

		switch (m) {

		case 'e':
			nocal++;
			cfile = NULL;
			break;

		case 'f':
			cfile = optarg;
			nocal = 0;
			break;

		case 'm':
			month = atoi(month);
			if (!month) doyear = 1;
			break;

		case 'y':
			year = atoi(optarg);
			if (year < 1900) year = year % 100 + 1900;
			break;

		case '?':
			errflg = 1;
			break;
		}
	if (errflg) {
		FPR(stderr,
			"Usage: pcal [ -e | -f <cal> ] [ -m month] [ -y <year> ]\n");
		exit(1);
	}
	t = time((long *)0);
	lt = localtime(&t);

	if (!month && !doyear)
		m = lt->tm_mon + 1;
	if (!year)
		year = lt->tm_year + 1900;

	/*
	 * In case we don't encounter any year data in the
	 * calendar file, assume the current year.
	 */
	cyear = year;

	/*
	 * Open a supplied calendar file (if any)
	 */
	if (cfile != NULL) {
		if ((cfp = fopen(cfile, "r")) == NULL) {
			FPR(stderr, "pcal: can't open file: %s\n", cfile);
			exit(1);
		}
	}
	/*
	 * Else see if a calendar file exists in the home directory
	 */
	else if (nocal == 0 && (cp = getenv("HOME")) != NULL) {
		(void)strcpy(cbuf, cp);
		(void)strcat(cbuf, "/calendar");
		cfp = fopen(cbuf, "r");
	}
	/*
	 * Write out PostScript prolog
	 */
	for (ap = pheader; *ap; ap++)
		PRT("%s\n", *ap);

	if (month)
		pmonth(month);
	else
		for (month = 1; month <= 12; month++)
			pmonth(month);

	return(0);
}

/*
 * pmonth - do calendar for month "m"
 */
pmonth(m)
int m;
{
	register char **s;
	register oldday = -1;
	register day;

	/*
	 * Do the calendar
	 */
	PRT("/year %d def\n", year);
	PRT("/month %d def\n", m);
	PRT("printmonth\n");

	/*
	 * Browse through the calendar file looking for day info
	 */
	if ((day = getday(m)) == 0) {	/* no info */
		PRT("showpage\n");
		return;
	}
	do {
		if (day != oldday) {
			if (oldday == -1)
				PRT("%d [ \n", day);
			else
				PRT(" ] daytext %d [  \n", day);
			oldday = day;
		} else
			PRT("(.p)\n");
		for (s = words; *s; s++)
			PRT("(%s)\n", *s);

	} while (day = getday(m));
	PRT("] daytext\n");
	PRT("showpage\n");
}

/*
 * getday - find next day entry for desired month in the calendar file
 */
getday(m)
register m;
{
	static mon = 0;
	static eof = 0;
	register char *cp;
	register c;

	if (cfp == NULL)	/* whoops, no calendar file */
		return(0);

	if (m != mon) {		/* new month, rewind */
		rewind(cfp);
		eof = 0;
		mon = m;
	}
	if (eof)
		return(0);
nextline:
	cp = lbuf;
	do {
		while ((c = getc(cfp)) != '\n' && c != EOF) {
			/* ignore leading white space */
			if (cp == lbuf && (c == ' ' || c == '\t'))
				continue;
			*cp++ = c;
		}
		if (c == EOF) {
			eof = 1;
			return(0);
		}
	} while (cp == lbuf);	/* ignore empty lines */
	*cp = 0;

	/* examine the line, see if its one we want */
	if ((c = parse(m)) == 0)
		goto nextline;

	return(c);
}

/*
 * parse - check calendar entry for desired month, break line into fields
 */
parse(m)
register m;
{
	register char *cp;
	register i;

	cp = strtok(lbuf, " \t");	/* get first field */
	while (*cp) {
		if (isupper(*cp))
			*cp = tolower(*cp);
		cp++;
	}
	cp = lbuf;

	/*
	 * Check for "year" line
	 */
	if (strcmp(cp, "year") == 0) {
		cp = strtok((char *)0, " \t");
		if ((i = atoi(cp)) > 0) {
			if (i < 100)
				i += 1900;
			cyear = i;
		}
		return(0);
	}
	/*
	 * If field begins with alpha, try to decode month name
	 */
	if (isalpha(*cp)) {
		if (cyear != year)
			return(0);

		for (i = 0; months[i]; i++)
			if (strncmp(cp, months[i], 3) == 0) {
				if (++i != m)
					return(0);

				/* month found, get day */

				if ((cp = strtok((char *)0, " \t")) == NULL)
					return(0);
				if ((i = atoi(cp)) < 1 || i > 31)
					return(0);
				if (loadwords())
					return(i);
				return(0);
			}
		return(0);
	}
	/*
	 * Not alpha month, try numeric
	 */
	if ((i = atoi(cp)) != m)
		return(0);
	while (isdigit(*cp))
		cp++;
	while (*cp && !isdigit(*cp))
		cp++;

	/* now get day */

	if ((i = atoi(cp)) < 1 || i > 31)
		return(0);

	 /* Numeric dates may have a year */

	while (isdigit(*cp))
		cp++;
	while (*cp && !isdigit(*cp))
		cp++;
	if ((m = atoi(cp)) > 0) {
		if (m < 100)
			m += 1900;
		cyear = m;
	}

	if (cyear != year)
		return(0);

	if (loadwords())
		return(i);
	return(0);
}

/*
 * loadwords - tokenize line buffer into word array
 */
loadwords()
{
	register char **ap = words, *c;
	register i;

	for (i = 0; *ap = strtok((char *)0, " \t") ; ap++, i++);
/*	{
		if ((*ap = strtok((char *)0, " \t")) == NULL)
			break;
		while ((c = strpbrk(*ap, "()")) != NULL)
		{
			if (c != *ap) ap++;
			if (*c == '(')
				*ap = "\\(";
			else
				*ap = "\\)";
			*c = 0;
			c++;
			if (*c)
				*(++ap) = c;
		}
	} */
	return(i);
}
SHAR_EOF
fi
if test -f 'pcal.man'
then
	echo shar: "will not over-write existing file 'pcal.man'"
else
cat << \SHAR_EOF > 'pcal.man'
.TH PCAL 1
.SH NAME
pcal \- generate PostScript calendars
.SH SYNOPSIS
.B pcal
[
.BR \-e |
.BR \-f <cal>
] [
.B \-m <month>
[
.B \-y <year>
]]
.SH DESCRIPTION
.I Pcal
generates PostScript to produce landscape calendars for any month and year.
The arguments
.B <month>
and
.BR <year> ,
if provided, should be numeric.  The month should be in the range 1 - 12,
and year should be specified as 1 or 2 digits or as the full 4 digit year.
The defaults for month and year are the current month and year.
If month is provided, but zero, the entire year is printed.
.P
If a file named
.I calendar
resides in the callers home directory, it will be searched for lines with
leading dates matching the requested month and year (current by default).
Any text following the date will be printed on the calendar under the
appropriate day of the month.  Dates in the
.I calendar
file may consist of a numeric or alpha month (at least the first 3 characters
for month names) followed by a numeric day and optionally followed by a
year.  Any non-numeric character may separate numeric dates.  Lines in the
.I calendar
file consisting of "year xxxx" (where xxxx is a numeric year) can be used
to set the year for following entries.  This assumes that the following
entries do not contain a year.  Any date entries containing year information
will set the remembered year to that year.
.P
.I Pcal has two options:
.P
.TP
.B \-e
Print an empty calendar.  Do not print entries from a calendar file.
.TP
.BR \-f <cal>
Directs
.I pcal
to use the file name <cal> as the input file in place of the default
calendar file in the callers home directory.
.SH SEE ALSO
cal(1)
.SH CAVEATS
The original PostScript code to generate the calendars was written by
Patrick Wood (Copywrite (c) 1987 by Patrick Wood of Pipeline Associates,
Inc.), and authorized for modification and redistribution.  The calendar
file inclusion code was originally written in "bs(1)" by Bill Vogel of
AT&T.  Patricks original PostScript was modified and enhanced several
times by others whos names have regrettably been lost.  I am responsible
for assembling this "C" version, but the lion's share of the work was
done by others.
.sp
Ken Keirnan
.br
Pacific Bell
.br
San Ramon, CA.
SHAR_EOF
fi
exit 0
#	End of shell archive
-- 
UUCP: rutgers!dayton!joe   (Picts 1-16 are   DHDSC - Joe Larson/MIS 1060
ATT : (612) 375-3537       now ready.)       700 on the Mall, Mpls, Mn. 55402



More information about the Alt.sources mailing list