[comp.lang.postscript] pcal - another version of the PostScript calendar generator

kjk@PacBell.COM (Ken Keirnan) (05/12/89)

OK, OK, stop the E-Mail.  Here is a "C" version of the PostScript
calendar generator (with thanks to Patrick Wood and Bill Vogel).
This version can load text into the day boxes from a calendar file.
See the README and manual page for more info.


Ken Keirnan

#-------- CUT HERE -------- CUT HERE -------- CUT HERE --------
#! /bin/sh
# This is a shell archive.  Remove anything before this line, then unpack
# it by saving it into a file and typing "sh file".  To overwrite existing
# files, type "sh file -c".  You can also feed this as standard input via
# unshar, or by typing "sh <file", e.g..  If this archive is complete, you
# will see the following message at the end:
#		"End of shell archive."
# Contents:  README Makefile pcal.c pcal.1
# Wrapped by kk1@pt06a on Thu May 11 11:25:49 1989
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test -f 'README' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'README'\"
else
echo shar: Extracting \"'README'\" \(984 characters\)
sed "s/^X//" >'README' <<'END_OF_FILE'
X"Pcal" is a program to print PostScript calendars for any month and year.
XBy default, it looks for a file in the home directory named "calendar"
Xfor entries with leading dates matching dates on the calendar, and prints
Xany following text under the appropriate day.
X
XThe program may be a little System V flavored (getopt, time routines)
Xbut should be easily portable to other vintages of UNIX.
X
XPcal is the combined effort of several people, most notably Patrick Wood
Xof Pipeline Associates, Inc. for the original PostScript code and Bill
XVogel of AT&T for the calendar file mechanism.  My part was simple
Xtranslation to a "C" program, the addition of a couple options and a more
Xgeneralized date searching routine (oh yes, and a manual page :-).
X
XThe original calendar PostScript was Copyright (c) 1987 by Patrick Wood
Xand Pipeline Associates, Inc. with permission to modify and redistribute.
XPlease retain this README file with the package.
X
X
XKen Keirnan
XPacific Bell
XSan Ramon, CA.
END_OF_FILE
if test 984 -ne `wc -c <'README'`; then
    echo shar: \"'README'\" unpacked with wrong size!
fi
# end of 'README'
fi
if test -f 'Makefile' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'Makefile'\"
else
echo shar: Extracting \"'Makefile'\" \(295 characters\)
sed "s/^X//" >'Makefile' <<'END_OF_FILE'
X#
X# Normally the day numbers for Saturday and Sunday are printed in gray
X# instead of black.  If you want only Sundays in gray, uncomment the
X# following line.
X#
X# COPTS = -DSATBLK
X
Xpcal:	pcal.c
X	$(CC) $(CFLAGS) $(LDFLAGS) $(COPTS) -o pcal pcal.c
X
Xpcal.man:	pcal.1
X	nroff -man pcal.1 > pcal.man
END_OF_FILE
if test 295 -ne `wc -c <'Makefile'`; then
    echo shar: \"'Makefile'\" unpacked with wrong size!
fi
# end of 'Makefile'
fi
if test -f 'pcal.c' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'pcal.c'\"
else
echo shar: Extracting \"'pcal.c'\" \(10818 characters\)
sed "s/^X//" >'pcal.c' <<'END_OF_FILE'
X#include <stdio.h>
X#include <ctype.h>
X#include <time.h>
X#include <string.h>
X
X#define PRT	(void)printf
X#define FPR	(void)fprintf
X
Xchar *words[100];	/* maximum number of words on a line */
Xchar lbuf[512];		/* maximum line size */
X
Xchar *months[] = {	/* used to match alpha months */
X	"jan", "feb", "mar", "apr", "may", "jun",
X	"jul", "aug", "sep", "oct", "nov", "dec",
X	(char *)0,
X};
X
X/*
X * pheader - provides the PostScript routines
X */
Xchar *pheader[] = {
X  "%!",
X  "/titlefont /Times-Bold def",
X  "/dayfont /Helvetica-Bold def",
X  "/month_names [ (January) (February) (March) (April) (May) (June) (July)",
X  "\t\t(August) (September) (October) (November) (December) ] def",
X  "/prtnum { 3 string cvs show} def",
X  "/drawgrid {\t\t% draw calendar boxes",
X  "\tdayfont findfont 10 scalefont setfont",
X  "\t0 1 6 {",
X  "\t\tdup dup 100 mul 40 moveto",
X  "\t\t[ (Sunday) (Monday) (Tuesday) (Wednesday) (Thursday) (Friday) (Saturday) ] exch get",
X  "\t\t100 center",
X  "\t\t100 mul 35 moveto",
X  "\t\t1.0 setlinewidth",
X  "\t\t0 1 5 {",
X  "\t\t\tgsave",
X  "\t\t\t100 0 rlineto ",
X  "\t\t\t0 -80 rlineto",
X  "\t\t\t-100 0 rlineto",
X  "\t\t\tclosepath stroke",
X  "\t\t\tgrestore",
X  "\t\t\t0 -80 rmoveto",
X  "\t\t} for",
X  "\t} for",
X  "} def",
X  "/drawnums {\t\t% place day numbers on calendar",
X  "\tdayfont findfont 30 scalefont setfont",
X  "\t/start startday def",
X  "\t/days ndays def",
X  "\tstart 100 mul 5 add 10 rmoveto",
X  "\t1 1 days {",
X  "\t\t/day exch def",
X  "\t\tgsave",
X#ifndef SATBLK
X  "\t\tday start add 7 mod 0 eq",
X  "\t\t{",
X  "\t\t\tsubmonth 0 eq",
X  "\t\t\t{",
X  "\t\t\t\t.8 setgray",
X  "\t\t\t} if",
X  "\t\t} if",
X#endif
X  "\t\tday start add 7 mod 1 eq",
X  "\t\t{",
X  "\t\t\tsubmonth 0 eq",
X  "\t\t\t{",
X  "\t\t\t\t.8 setgray",
X  "\t\t\t} if",
X  "\t\t} if",
X  "\t\tday prtnum",
X  "\t\tgrestore",
X  "\t\tday start add 7 mod 0 eq",
X  "\t\t{",
X  "\t\t\tcurrentpoint exch pop 80 sub 5 exch moveto",
X  "\t\t}",
X  "\t\t{",
X  "\t\t\t100 0 rmoveto",
X  "\t\t} ifelse",
X  "\t} for",
X  "} def",
X  "/drawfill {\t\t% place fill squares on calendar",
X  "\t/start startday def",
X  "\t/days ndays def",
X  "\t0 35 rmoveto",
X  "\t1.0 setlinewidth",
X  "\t0 1 start 1 sub {",
X  "\t\tgsave",
X  "\t\t.9 setgray",
X  "\t\t100 0 rlineto ",
X  "\t\t0 -80 rlineto",
X  "\t\t-100 0 rlineto",
X  "\t\tclosepath fill",
X  "\t\tgrestore",
X  "\t\t100 0 rmoveto",
X  "\t} for",
X  "\tsubmonth 1 eq",
X  "\t{",
X  "\t\t/lastday 42 def",
X  "\t\t600 -365 moveto",
X  "\t}",
X  "\t{",
X  "\t\t/lastday 40 def",
X  "\t\t400 -365 moveto",
X  "\t} ifelse",
X  "\tlastday -1 ndays start 1 add add",
X  "\t{",
X  "\t\t/day exch def",
X  "\t\tgsave",
X  "\t\t.9 setgray",
X  "\t\t100 0 rlineto ",
X  "\t\t0 -80 rlineto",
X  "\t\t-100 0 rlineto",
X  "\t\tclosepath fill",
X  "\t\tgrestore",
X  "\t\tday 7 mod 1 eq",
X  "\t\t{",
X  "\t\t\t600 -365 80 add moveto",
X  "\t\t}",
X  "\t\t{",
X  "\t\t\t-100 0 rmoveto",
X  "\t\t} ifelse",
X  "\t} for",
X  "} def",
X  "/isleap {\t\t% is this a leap year?",
X  "\tyear 4 mod 0 eq\t\t% multiple of 4",
X  "\tyear 100 mod 0 ne \t% not century",
X  "\tyear 1000 mod 0 eq or and\t% unless it's a millenia",
X  "} def",
X  "/days_month [ 31 28 31 30 31 30 31 31 30 31 30 31 ] def",
X  "/ndays {\t\t% number of days in this month",
X  "\tdays_month month 1 sub get",
X  "\tmonth 2 eq\t% Feb",
X  "\tisleap and",
X  "\t{",
X  "\t\t1 add",
X  "\t} if",
X  "} def",
X  "/startday {\t\t% starting day-of-week for this month",
X  "\t/off year 2000 sub def\t% offset from start of epoch",
X  "\toff",
X  "\toff 4 idiv add\t\t% number of leap years",
X  "\toff 100 idiv sub\t% number of centuries",
X  "\toff 1000 idiv add\t% number of millenia",
X  "\t6 add 7 mod 7 add \t% offset from Jan 1 2000",
X  "\t/off exch def",
X  "\t1 1 month 1 sub {",
X  "\t\t/idx exch def",
X  "\t\tdays_month idx 1 sub get",
X  "\t\tidx 2 eq",
X  "\t\tisleap and",
X  "\t\t{",
X  "\t\t\t1 add",
X  "\t\t} if",
X  "\t\t/off exch off add def",
X  "\t} for",
X  "\toff 7 mod\t\t% 0--Sunday, 1--monday, etc.",
X  "} def",
X  "/center {\t\t% center string in given width",
X  "\t/width exch def",
X  "\t/str exch def width str ",
X  "\tstringwidth pop sub 2 div 0 rmoveto str show",
X  "} def",
X  "/calendar",
X  "{",
X  "\ttitlefont findfont 48 scalefont setfont",
X  "\t0 60 moveto",
X  "\t/month_name month_names month 1 sub get def",
X  "\tmonth_name show",
X  "\t/yearstring year 10 string cvs def",
X  "\t700 yearstring stringwidth pop sub 60 moveto",
X  "\tyearstring show",
X  "\t0 0 moveto",
X  "\tdrawnums",
X  "\t0 0 moveto",
X  "\tdrawfill",
X  "\t0 0 moveto",
X  "\tdrawgrid",
X  "} def",
X  "/daytext {",
X  "\t/Helvetica-Narrow findfont 6 scalefont setfont",
X  "\t/mytext\texch def /myday exch def",
X  "\tstartday myday 1 sub add dup 7 mod 100 mul 5 add % gives column",
X  "\texch 7 idiv -80 mul % gives row",
X  "\tdup /ypos exch def moveto",
X  "\t/LM currentpoint pop def /RM LM 95 add def",
X  "        mytext { dup (.p) eq { crlf pop} {prstr ( ) show} ifelse } forall",
X  "} def",
X  "/crlf {",
X  "    ypos 8 sub /ypos exch def LM ypos moveto",
X  "} def",
X  "/prstr {",
X  "    dup stringwidth pop currentpoint pop",
X  "    add RM gt {crlf} if show",
X  "} def",
X  "/printmonth {",
X  "\t90 rotate",
X  "\t50 -120 translate",
X  "\t/submonth 0 def",
X  "\tcalendar",
X  "\tmonth 1 sub 0 eq",
X  "\t{",
X  "\t\t/lmonth 12 def",
X  "\t\t/lyear year 1 sub def",
X  "\t}",
X  "\t{",
X  "\t\t/lmonth month 1 sub def",
X  "\t\t/lyear year def",
X  "\t} ifelse",
X  "\tmonth 1 add 13 eq",
X  "\t{",
X  "\t\t/nmonth 1 def",
X  "\t\t/nyear year 1 add def",
X  "\t} ",
X  "\t{",
X  "\t\t/nmonth month 1 add def",
X  "\t\t/nyear year def",
X  "\t} ifelse",
X  "\t/savemonth month def",
X  "\t/saveyear year def",
X  "\t/submonth 1 def",
X  "\t/year lyear def",
X  "\t/month lmonth def",
X  "\tgsave",
X  "\t500 -365 translate",
X  "\tgsave",
X  "\t.138 .138 scale",
X  "\t10 -120 translate",
X  "\tcalendar",
X  "\tgrestore",
X  "\t/submonth 1 def",
X  "\t/year nyear def",
X  "\t/month nmonth def",
X  "\t100 0 translate",
X  "\tgsave",
X  "\t.138 .138 scale",
X  "\t10 -120 translate",
X  "\tcalendar",
X  "\tgrestore",
X  "\t/month savemonth def",
X  "\t/year saveyear def",
X  "\t/submonth 0 def",
X  "\tgrestore",
X  "} def",
X  (char *)0,
X};
X
XFILE *cfp = NULL;
Xint year;
Xint cyear;
X
Xvoid exit();
Xchar *getenv();
X
Xmain(argc, argv)
Xint argc;
Xchar **argv;
X{
X	extern char *optarg;
X	extern int optind;
X	register struct tm *lt;
X	register char **ap;
X	register char *cp;
X	char *cfile = NULL;
X	char cbuf[80];
X	long t, time();
X	int errflg = 0;
X	int nocal = 0;
X	int m;
X
X	while ((m = getopt(argc, argv, "ef:")) != EOF)
X
X		switch (m) {
X
X		case 'e':
X			nocal++;
X			cfile = NULL;
X			break;
X
X		case 'f':
X			cfile = optarg;
X			nocal = 0;
X			break;
X
X		case '?':
X			errflg = 1;
X			break;
X		}
X	if (errflg) {
X		FPR(stderr, "Usage: pcal [ -e | -f <cal> ] [ month [ year ]]\n");
X		exit(1);
X	}
X	t = time((long *)0);
X	lt = localtime(&t);
X
X	/*
X	 * First argument should be a month
X	 */
X	if ((argc - optind) > 0) {
X		if ((m = atoi(argv[optind])) < 1 || m > 12) {
X			FPR(stderr, "Month must be in range 1 - 12\n");
X			exit(1);
X		}
X	} else	/* otherwise, use current month */
X		m = lt->tm_mon + 1;
X
X	/*
X	 * More than one arg, second must be a year
X	 */
X	if ((argc - optind) > 1) {
X		if ((year = atoi(argv[optind+1])) < 100)
X			year += 1900;
X	} else
X		year = lt->tm_year + 1900;
X
X	/*
X	 * In case we don't encounter any year data in the
X	 * calendar file, assume the current year.
X	 */
X	cyear = year;
X
X	/*
X	 * Open a supplied calendar file (if any)
X	 */
X	if (cfile != NULL) {
X		if ((cfp = fopen(cfile, "r")) == NULL) {
X			FPR(stderr, "pcal: can't open file: %s\n", cfile);
X			exit(1);
X		}
X	}
X	/*
X	 * Else see if a calendar file exists in the home directory
X	 */
X	else if (nocal == 0 && (cp = getenv("HOME")) != NULL) {
X		(void)strcpy(cbuf, cp);
X		(void)strcat(cbuf, "/calendar");
X		cfp = fopen(cbuf, "r");
X	}
X	/*
X	 * Write out PostScript prolog
X	 */
X	for (ap = pheader; *ap; ap++)
X		PRT("%s\n", *ap);
X
X	pmonth(m);
X
X	return(0);
X}
X
X/*
X * pmonth - do calendar for month "m"
X */
Xpmonth(m)
Xint m;
X{
X	register char **s;
X	register oldday = -1;
X	register day;
X
X	/*
X	 * Do the calendar
X	 */
X	PRT("/year %d def\n", year);
X	PRT("/month %d def\n", m);
X	PRT("printmonth\n");
X
X	/*
X	 * Browse through the calendar file looking for day info
X	 */
X	if ((day = getday(m)) == 0) {	/* no info */
X		PRT("showpage\n");
X		return;
X	}
X	do {
X		if (day != oldday) {
X			if (oldday == -1)
X				PRT("%d [ \n", day);
X			else
X				PRT(" ] daytext %d [  \n", day);
X			oldday = day;
X		} else
X			PRT("(.p)\n");
X		for (s = words; *s; s++)
X			PRT("(%s)\n", *s);
X
X	} while (day = getday(m));
X	PRT("] daytext\n");
X	PRT("showpage\n");
X}
X
X/*
X * getday - find next day entry for desired month in the calendar file
X */
Xgetday(m)
Xregister m;
X{
X	static mon = 0;
X	static eof = 0;
X	register char *cp;
X	register c;
X
X	if (cfp == NULL)	/* whoops, no calendar file */
X		return(0);
X
X	if (m != mon) {		/* new month, rewind */
X		rewind(cfp);
X		eof = 0;
X		mon = m;
X	}
X	if (eof)
X		return(0);
Xnextline:
X	cp = lbuf;
X	do {
X		while ((c = getc(cfp)) != '\n' && c != EOF) {
X			/* ignore leading white space */
X			if (cp == lbuf && (c == ' ' || c == '\t'))
X				continue;
X			*cp++ = c;
X		}
X		if (c == EOF) {
X			eof = 1;
X			return(0);
X		}
X	} while (cp == lbuf);	/* ignore empty lines */
X	*cp = 0;
X
X	/* examine the line, see if its one we want */
X	if ((c = parse(m)) == 0)
X		goto nextline;
X
X	return(c);
X}
X
X/*
X * parse - check calendar entry for desired month, break line into fields
X */
Xparse(m)
Xregister m;
X{
X	register char *cp;
X	register i;
X
X	cp = strtok(lbuf, " \t");	/* get first field */
X	while (*cp) {
X		*cp = tolower(*cp);
X		cp++;
X	}
X	cp = lbuf;
X
X	/*
X	 * Check for "year" line
X	 */
X	if (strcmp(cp, "year") == 0) {
X		cp = strtok((char *)0, " \t");
X		if ((i = atoi(cp)) > 0) {
X			if (i < 100)
X				i += 1900;
X			cyear = i;
X		}
X		return(0);
X	}
X	/*
X	 * If field begins with alpha, try to decode month name
X	 */
X	if (isalpha(*cp)) {
X		if (cyear != year)
X			return(0);
X
X		for (i = 0; months[i]; i++)
X			if (strncmp(cp, months[i], 3) == 0) {
X				if (++i != m)
X					return(0);
X
X				/* month found, get day */
X
X				if ((cp = strtok((char *)0, " \t")) == NULL)
X					return(0);
X				if ((i = atoi(cp)) < 1 || i > 31)
X					return(0);
X				if (loadwords())
X					return(i);
X				return(0);
X			}
X		return(0);
X	}
X	/*
X	 * Not alpha month, try numeric
X	 */
X	if ((i = atoi(cp)) != m)
X		return(0);
X	while (isdigit(*cp))
X		cp++;
X	while (*cp && !isdigit(*cp))
X		cp++;
X
X	/* now get day */
X
X	if ((i = atoi(cp)) < 1 || i > 31)
X		return(0);
X
X	 /* Numeric dates may have a year */
X
X	while (isdigit(*cp))
X		cp++;
X	while (*cp && !isdigit(*cp))
X		cp++;
X	if ((m = atoi(cp)) > 0) {
X		if (m < 100)
X			m += 1900;
X		cyear = m;
X	}
X
X	if (cyear != year)
X		return(0);
X
X	if (loadwords())
X		return(i);
X	return(0);
X}
X
X/*
X * loadwords - tokenize line buffer into word array
X */
Xloadwords()
X{
X	register char **ap = words;
X	register i;
X
X	for (i = 0; *ap = strtok((char *)0, " \t"); ap++, i++);
X	return(i);
X}
END_OF_FILE
if test 10818 -ne `wc -c <'pcal.c'`; then
    echo shar: \"'pcal.c'\" unpacked with wrong size!
fi
# end of 'pcal.c'
fi
if test -f 'pcal.1' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'pcal.1'\"
else
echo shar: Extracting \"'pcal.1'\" \(2157 characters\)
sed "s/^X//" >'pcal.1' <<'END_OF_FILE'
X.TH PCAL 1
X.SH NAME
Xpcal \- generate PostScript calendars
X.SH SYNOPSIS
X.B pcal
X[
X.BR \-e |
X.BR \-f <cal>
X] [
X.B <month>
X[
X.B <year>
X]]
X.SH DESCRIPTION
X.I Pcal
Xgenerates PostScript to produce landscape calendars for any month and year.
XThe arguments
X.B <month>
Xand
X.BR <year> ,
Xif provided, should be numeric.  The month should be in the range 1 - 12,
Xand year should be specified as 1 or 2 digits or as the full 4 digit year.
XThe defaults for month and year are the current month and year.
X.P
XIf a file named
X.I calendar
Xresides in the callers home directory, it will be searched for lines with
Xleading dates matching the requested month and year (current by default).
XAny text following the date will be printed on the calendar under the
Xappropriate day of the month.  Dates in the
X.I calendar
Xfile may consist of a numeric or alpha month (at least the first 3 characters
Xfor month names) followed by a numeric day and optionally followed by a
Xyear.  Any non-numeric character may separate numeric dates.  Lines in the
X.I calendar
Xfile consisting of "year xxxx" (where xxxx is a numeric year) can be used
Xto set the year for following entries.  This assumes that the following
Xentries do not contain a year.  Any date entries containing year information
Xwill set the remembered year to that year.
X.P
X.I Pcal has two options:
X.P
X.TP
X.B \-e
XPrint an empty calendar.  Do not print entries from a calendar file.
X.TP
X.BR \-f <cal>
XDirects
X.I pcal
Xto use the file name <cal> as the input file in place of the default
Xcalendar file in the callers home directory.
X.SH SEE ALSO
Xcal(1)
X.SH CAVEATS
XThe original PostScript code to generate the calendars was written by
XPatrick Wood (Copywrite (c) 1987 by Patrick Wood of Pipeline Associates,
XInc.), and authorized for modification and redistribution.  The calendar
Xfile inclusion code was originally written in "bs(1)" by Bill Vogel of
XAT&T.  Patricks original PostScript was modified and enhanced several
Xtimes by others whos names have regrettably been lost.  I am responsible
Xfor assembling this "C" version, but the lion's share of the work was
Xdone by others.
X.sp
XKen Keirnan
X.br
XPacific Bell
X.br
XSan Ramon, CA.
END_OF_FILE
if test 2157 -ne `wc -c <'pcal.1'`; then
    echo shar: \"'pcal.1'\" unpacked with wrong size!
fi
# end of 'pcal.1'
fi
echo shar: End of shell archive.
exit 0
-- 

Ken Keirnan - Pacific Bell - {att,bellcore,sun,ames,pyramid}!pacbell!pbhyf!kjk
  San Ramon, California	                    kjk@pbhyf.PacBell.COM

nelson@sun.soe.clarkson.edu (Russ Nelson) (05/13/89)

I had to change the first part of parse() to get it to work under
SunOS 4.0 as shown below.  The stupid tolower changes everything to
lowercase, whether it is an uppercase letter or not!  Many thanks to Ken
for putting this together -- it's exactly what I was going to do (eventually).

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

--
--russ (nelson@clutx [.bitnet | .clarkson.edu])
I'm a right-to-lifer -- everyone has a right to earn a living sufficient to
feed himself and his family.

billr@tekred.CNA.TEK.COM (Bill Randle) (05/18/89)

The pcal capability has been folded into the latest version of calentool
(actually as part of Patch#2) to generate pretty PostScript output of a
months worth of appointments.  [Calentool, recently sent to comp.sources.sun,
is a SunView based desktop calendar.]

	-Bill Randle
	Tektronix, Inc.
	billr@saab.CNA.TEK.COM