home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1916 / pcal.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-28  |  45.6 KB  |  1,943 lines

  1. static char  VERISON_STRING[]    = "@(#)pcal v2.5 - print Postscript calendars";
  2. /*
  3.  * pcal.c - generate PostScript file to print calendar for any month and year
  4.  *
  5.  * The original PostScript code to generate the calendars was written by
  6.  * Patrick Wood (Copyright (c) 1987 by Patrick Wood of Pipeline Associates,
  7.  * Inc.), and authorized for modification and redistribution.  The calendar
  8.  * file inclusion code was originally written in "bs(1)" by Bill Vogel of
  9.  * AT&T.  Patrick's original PostScript was modified and enhanced several
  10.  * times by others whose names have regrettably been lost.  This C version
  11.  * was originally created by Ken Keirnan of Pacific Bell; additional
  12.  * enhancements by Joseph P. Larson, Ed Hand, and Andrew Rogers (who also
  13.  * did the VMS port), Mark Kantrowitz, Joe Brownlee.  The moon routines were
  14.  * originally written by Mark Hanson, and were improved and incorporated into
  15.  * this version by Jamie Zawinski.
  16.  *
  17.  * Revision history:
  18.  *
  19.  *    2.5    JAB    10/04/90    added -F option
  20.  *
  21.  *    2.4    ---    10/01/90    * no modifications *
  22.  *
  23.  *    2.3    jwz    09/18/90    added moon routines
  24.  *
  25.  *    2.2    AWR    09/17/90    revise logic of parse(); new usage
  26.  *                    message
  27.  *
  28.  *        JAB/AWR    09/14/90    support "note" lines in date file
  29.  *
  30.  *    2.1    MK/AWR    08/27/90    support -L, -C, -R, -n options;
  31.  *                    print holiday text next to date
  32.  *
  33.  *        AWR    08/24/90    incorporate cpp-like functionality;
  34.  *                    add -D and -U options; save date file
  35.  *                    information in internal data structure;
  36.  *                    look for PCAL_OPTS and PCAL_DIR; look
  37.  *                    for ~/.calendar and ~/calendar
  38.  *
  39.  *    2.0    AWR    08/08/90    included revision history; replaced -r
  40.  *                    flag with -l and -p; replaced -s and -S
  41.  *                    flags with -b and -g; recognize flags
  42.  *                    set in date file; translate ( and ) in
  43.  *                    text to octal escape sequence; usage()
  44.  *                    message condensed to fit 24x80 screen
  45.  *
  46.  *    Parameters:
  47.  *
  48.  *        pcal [opts]        generate calendar for current month/year
  49.  *
  50.  *        pcal [opts] yy        generate calendar for entire year yy
  51.  *
  52.  *        pcal [opts] mm yy    generate calendar for month mm
  53.  *                    (Jan = 1), year yy (19yy if yy < 100)
  54.  *
  55.  *        pcal [opts] mm yy n    as above, for n consecutive months
  56.  *
  57.  *    Output:
  58.  *
  59.  *        PostScript file to print calendars for all selected months.
  60.  *
  61.  *    Options:
  62.  *
  63.  *        -b <DAY>    print specified weekday in black
  64.  *        -g <DAY>    print specified weekday in gray
  65.  *                (default: print Saturdays and Sundays in gray)
  66.  *        
  67.  *        -d <FONT>    specify alternate font for day names
  68.  *                (default: Times-Bold)
  69.  *
  70.  *        -n <FONT>    specify alternate font for notes in boxes
  71.  *                (default: Helvetica-Narrow)
  72.  *
  73.  *        -t <FONT>    specify alternate font for titles
  74.  *                (default: Times-Bold)
  75.  *
  76.  *        -D <SYM>    define preprocessor symbol
  77.  *        -U <SYM>    un-define preprocessor symbol
  78.  *
  79.  *        -e        generate empty calendar (ignore date file)
  80.  *
  81.  *        -f <FILE>    specify alternate date file (default:
  82.  *                ~/.calendar on Un*x, SYS$LOGIN:CALENDAR.DAT
  83.  *                on VMS; if environment variable [logical
  84.  *                name on VMS] PCAL_DIR exists, looks there
  85.  *                instead)
  86.  *
  87.  *        -o <FILE>    specify alternate output file (default:
  88.  *                stdout on Un*x, CALENDAR.PS on VMS)
  89.  *
  90.  *        -L <STRING>    specify left foot string   (default: "")
  91.  *        -C <STRING>    specify center foot string (default: "")
  92.  *        -R <STRING>    specify right foot string  (default: "")
  93.  *
  94.  *        -l        generate landscape-mode calendars
  95.  *        -p        generate portrait-mode calendars
  96.  *                (default: landscape-mode)
  97.  *
  98.  *        -m        draw a small moon icon on the days of the
  99.  *                full, new, and half moons.
  100.  *        -M        draw a small moon icon every day.  
  101.  *                (default: no moons).
  102.  *
  103.  *        -F <DAY>    select any day to be displayed as the first
  104.  *                day of the weel
  105.  *
  106.  *    There are many ways to specify these options in addition to using the
  107.  *    command line; this facilitates customization to the user's needs.
  108.  *
  109.  *    If the environment variable (global symbol on VMS) PCAL_OPTS is
  110.  *    present, its value will be parsed as if it were a command line.
  111.  *    Any options specified will override the program defaults.
  112.  *
  113.  *    All but the -e, -f, -D, and -U options may be specified in the date
  114.  *    file by the inclusion of one or more lines of the form "opt <options>".
  115.  *    Any such options override any previous values set either as program
  116.  *    defaults, via PCAL_OPTS, or in previous "opt" lines.
  117.  *
  118.  *    Options explicitly specified on the command line in turn override all
  119.  *    of the above.
  120.  *
  121.  *    Any flag which normally takes an argument may also be specified without
  122.  *    an argument; this resets the corresponding option to its default.  -D
  123.  *    alone un-defines all symbols; -U alone has no effect.
  124.  *
  125.  *    Parameters and flags may be mixed on the command line.  In some cases
  126.  *    (e.g., when a parameter follows a flag without its optional argument)
  127.  *    this may lead to ambiguity; the dummy flag '-' (or '--') may be used
  128.  *    to separate them, i.e. "pcal -t - 9 90".
  129.  *
  130.  *    Simple cpp-like functionality is provided.  The date file may include
  131.  *    the following commands, which work like their cpp counterparts:
  132.  *
  133.  *        define <sym>
  134.  *        undef <sym>
  135.  *
  136.  *        if{n}def <sym>
  137.  *           ...
  138.  *        { else
  139.  *           ... }
  140.  *        endif
  141.  *
  142.  *        include <file>
  143.  *
  144.  *    Note that these do not start with '#', which is reserved as a comment
  145.  *    character.
  146.  *
  147.  *    "define" alone deletes all the current definitions; "ifdef" alone is
  148.  *    always false; "ifndef" alone is always true.  All defined symbols are
  149.  *    treated in a case-insensitive manner.
  150.  *
  151.  *    The file name in the "include" directive may optionally be surrounded
  152.  *    by "" or <>.
  153.  *
  154.  *    Additional notes may be propagated to an empty calendar box by the
  155.  *    inclusion of one or more lines of the form "note <month> <text>",
  156.  *    where <month> may be numeric or alphabetic.
  157.  *
  158.  */
  159.  
  160.  
  161. #include <stdio.h>
  162. #include <ctype.h>
  163. #include <time.h>
  164. #include <string.h>
  165.  
  166. #ifdef VMS        /* VMS oddities isolated here */
  167.  
  168. #include <ssdef.h>    /* required for trnlog() */
  169. #include <descrip.h>
  170.  
  171. #define HOME_DIR    "SYS$LOGIN"
  172. #define DATEFILE    "calendar.dat"
  173. #define OUTFILE        "calendar.ps"
  174. #define START_PATH    '['
  175. #define END_PATH    ']'
  176.  
  177. #define EXIT_SUCCESS 1
  178. #define EXIT_FAILURE 3
  179.  
  180. #else            /* non-VMS - assume Un*x of some sort */
  181.  
  182. #define HOME_DIR    "HOME"
  183. #define DATEFILE    ".calendar"
  184. #define ALT_DATEFILE    "calendar"    /* for backward compatibility */
  185. #define OUTFILE        ""
  186. #define START_PATH    '/'
  187. #define END_PATH    '/'
  188.  
  189. #define EXIT_SUCCESS 0
  190. #define EXIT_FAILURE 1
  191.  
  192. #endif
  193.  
  194. #define PCAL_OPTS    "PCAL_OPTS"    /* environment variables */
  195. #define PCAL_DIR    "PCAL_DIR"
  196.  
  197. #define IS_LEAP(y)    ((y) % 4 == 0 && ((y) % 100 != 0 || (y) % 400 == 0))
  198. #define INIT_COLORS    memcpy(color, default_color, sizeof(color))
  199. #define LASTCHAR(p)    ((p) && *(p) ? (p) + strlen(p) - 1 : NULL)
  200.  
  201. #ifdef __STDC__
  202. #define TOLOWER(c)    tolower(c)
  203. #else
  204. #define TOLOWER(c)    (isupper(c) ? tolower(c) : (c))
  205. #endif
  206.  
  207. #define PRT        (void)printf
  208. #define FPR        (void)fprintf
  209.  
  210. #define FALSE    0
  211. #define TRUE    1
  212.  
  213. #define ALL_FLAGS    "bCDdefgLlMmnopRtUF"    /* all command-line flags */
  214. #define DATEFILE_FLAGS    "DefU"            /* parsed before opening datefile */
  215. #define OTHER_FLAGS    "bCdgLlMmnopRtF"    /* parsed inside datefile */
  216.  
  217. #define DAYFONT        "Times-Bold"        /* default font names */
  218. #define TITLEFONT    "Times-Bold"
  219. #define NOTESFONT    "Helvetica-Narrow"
  220.  
  221. #define LFOOT         ""                        /* default foot strings */
  222. #define CFOOT         ""
  223. #define RFOOT         ""
  224.  
  225. #define LANDSCAPE  90        /* degrees to rotate for landscape/portrait */
  226. #define PORTRAIT    0
  227. #define ROTATE       LANDSCAPE    /* default */
  228.  
  229. #define BLACK        0    /* colors for dates */
  230. #define GRAY        1
  231.  
  232. #define NO_DATEFILE     0    /* date file (if any) to use */
  233. #define USER_DATEFILE    1
  234. #define SYS_DATEFILE    2
  235.  
  236. /* preprocessor token codes - must be contiguous range of integers starting
  237.  * at 0 and ending with code for non-tokens (cf. pp_info[], pp_token())
  238.  */
  239. #define PP_DEFINE    0
  240. #define PP_ELSE        1
  241. #define PP_ENDIF    2
  242. #define PP_IFDEF    3
  243. #define PP_IFNDEF    4
  244. #define PP_INCLUDE    5
  245. #define PP_UNDEF    6
  246. #define PP_OTHER    7    /* not pp token */
  247.  
  248. #define MAX_NESTING    10    /* maximum nesting level for file inclusion */
  249.  
  250. #define MAX_PP_SYMS    100    /* number of definable preprocessor symbols */
  251. #define PP_SYM_UNDEF     -1    /* flag for undefined symbol */
  252.  
  253. #define MIN_YR        1900    /* significant years (calendar limits) */
  254. #define MAX_YR        9999
  255.  
  256. #define JAN         1    /* significant months */
  257. #define FEB         2
  258. #define DEC        12
  259.  
  260. #define DAY_TEXT    0    /* types of text in data structure */
  261. #define HOLIDAY_TEXT    1
  262. #define NOTE_TEXT    2
  263.  
  264. #define NOTE_DAY    32    /* dummy day for notes text */
  265.  
  266. #define DF_YEAR        0    /* returns from date_type() */
  267. #define DF_OPT        1
  268. #define DF_NOTE        2
  269. #define DF_MONTH    3
  270. #define DF_DATE        4
  271. #define DF_OTHER    5
  272.  
  273. #define PARSE_OK    0    /* returns from parse(), enter_day_info() */
  274. #define PARSE_INVDATE    1
  275. #define PARSE_INVLINE    2
  276.  
  277. #define STRSIZ    200        /* size of misc. strings */
  278.  
  279. #define MAXARGS 3        /* numeric command-line args */
  280.  
  281. #define WHITESPACE " \t"    /* token delimiters in date file */
  282.  
  283. /*
  284.  * Global typedef declarations for data structure
  285.  */
  286.  
  287. typedef struct d_i {
  288.     int is_holiday;
  289.     char *text;
  290.     struct d_i *next;
  291.     } day_info;
  292.  
  293. typedef struct m_i {
  294.     unsigned long holidays;
  295.     day_info *day[32];    /* including NOTE_DAY */
  296.     } month_info;
  297.  
  298. typedef struct y_i {
  299.     int year;
  300.     month_info *month[12];
  301.     struct y_i *next;
  302.     } year_info;
  303.  
  304.  
  305. /*
  306.  * Global variables:
  307.  */
  308.  
  309. int do_define(), do_ifdef(), do_ifndef(), do_include(), do_undef();
  310. char *trnlog(), *mk_path(), *mk_filespec();
  311.  
  312. extern char *getenv();
  313.  
  314. year_info *head = NULL;        /* head of internal data structure */
  315. int nesting_level = 0;        /* level of include file nesting */
  316. int init_month;            /* initial month, year, number of months */
  317. int init_year;
  318. int nmonths;
  319. int curr_year;            /* current default year for date file entries */
  320. char *words[100];        /* maximum number of words per date file line */
  321. char lbuf[512];            /* maximum date file line size */
  322. char *pp_sym[MAX_PP_SYMS];    /* preprocessor defined symbols */
  323. char progname[STRSIZ];        /* program name (for error messages) */
  324. char color[7];            /* colors of weekdays - cf. default_color[] */
  325. int first_day_of_week = 0;    /* first day of week; 0 = Sun, 1 = Mon, etc. */
  326.  
  327. /*
  328.  * Default values for command-line options:
  329.  */
  330.  
  331. char default_color[7] = {        /* -b, -g */
  332.         GRAY, BLACK, BLACK, BLACK, BLACK, BLACK, GRAY   /* cf. COLOR_MSG */
  333.     };
  334.  
  335. int datefile_type = SYS_DATEFILE;    /* -e, -f */
  336. char datefile[STRSIZ] = "";
  337. char default_dir[STRSIZ] = "";
  338.  
  339. int rotate = ROTATE;            /* -l, -p */
  340.  
  341. char *draw_moons = "false";        /* -m, -M */
  342.  
  343. char dayfont[STRSIZ] = DAYFONT;        /* -d, -t, -n */
  344. char titlefont[STRSIZ] = TITLEFONT;
  345. char notesfont[STRSIZ] = NOTESFONT;
  346.  
  347. char lfoot[STRSIZ] = LFOOT;             /* -L, -C, -R */
  348. char cfoot[STRSIZ] = CFOOT;
  349. char rfoot[STRSIZ] = RFOOT;
  350.  
  351. char outfile[STRSIZ] = OUTFILE;        /* -o */
  352.  
  353. /*
  354.  * Language-dependent strings (month and day names, option file keywords,
  355.  * preprocessor tokens):
  356.  */
  357.  
  358. static char *months[12] = {
  359.     "January", "February", "March", "April", "May", "June",
  360.     "July", "August", "September", "October", "November", "December"
  361.     };
  362.  
  363. static char *days[7] = {
  364.         "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
  365.         "Saturday"
  366.     };
  367.  
  368. /* preprocessor tokens - must be in same order as PP_XXXXX (cf. pp_token()) */
  369. static struct pp {
  370.     char    *token;        /* name */
  371.     int    (*pfcn)();    /* dispatch routine */
  372.     } pp_info[] = {
  373.         { "define",  do_define  },        /* PP_DEFINE    */
  374.         { "else",    NULL       },        /* PP_ELSE    */
  375.         { "endif",   NULL       },        /* PP_ENDIF    */
  376.         { "ifdef",   do_ifdef   },        /* PP_IFDEF    */
  377.         { "ifndef",  do_ifndef  },        /* PP_IFNDEF    */
  378.         { "include", do_include },        /* PP_INCLUDE    */
  379.         { "undef",   do_undef   },        /* PP_UNDEF    */
  380.         { NULL,      NULL       }        /* PP_OTHER    */
  381.     };
  382.  
  383. #define MIN_DAY_LEN   2        /* minimum size of abbreviations  */
  384. #define MIN_MONTH_LEN 3
  385. #define MIN_PPTOK_LEN 3
  386.  
  387. #define ALL    "all"        /* command-line or date file keywords */
  388. #define NOTE    "note"
  389. #define OPT    "opt"
  390. #define YEAR    "year"
  391.  
  392. #define COLOR_MSG    "Sat/Sun in gray, others in black"    /* cf. usage() */
  393.  
  394. /*
  395.  * PostScript boilerplate
  396.  */
  397.  
  398. #include "pcalinit.h"
  399.  
  400.  
  401. /*
  402.  * Main program - parse and validate command-line arguments, open files,
  403.  * generate PostScript boilerplate and code to generate calendars.
  404.  *
  405.  * Program structure:
  406.  *
  407.  * main() looks for the environment variable (global symbol on VMS) PCAL_OPTS
  408.  * and calles get_args() to parse it.  It then calls get_args() again to parse
  409.  * the command line for the date file name, -D and -U options to be in effect
  410.  * prior to reading the date file, and any numeric arguments (month, year, 
  411.  * number of months).  It then calls read_datefile() to read and parse the
  412.  * date file; any "opt" lines present will override the defaults for the
  413.  * command-line flags.  It then calls get_args() again to process the other
  414.  * command-line flags, which in turn override any specified earlier.
  415.  *
  416.  * main() then generates the common PostScript code and then calls pmonth() to
  417.  * print the calendars.
  418.  *
  419.  * read_datefile() calls getline() to read the date file, do_xxxxx() to process
  420.  * the preprocessor tokens, and parse() to parse each date line.
  421.  *
  422.  * getline() reads one or more lines from the date file, stripping comments
  423.  * (# through end-of-line) and ignoring blank lines.
  424.  *
  425.  * parse() parses a line from the date file and processes it.  If "opt", it
  426.  * calls loadwords() to split the line into tokens and get_args() to
  427.  * process them.  If the line contains "note" or a date, it calls
  428.  * enter_day_info() to enter the day and related text into the data structure.
  429.  *
  430.  * pmonth() calls find_holidays() to generate the list of holidays to be
  431.  * printed in gray; it then calls find_daytext() to generate the text to
  432.  * be printed inside the calendar boxes.
  433.  *
  434.  */
  435. main(argc, argv)
  436.     int argc;
  437.     char **argv;
  438. {
  439.     FILE *dfp = NULL;        /* date file pointer */
  440.     char *p, **ap;
  441.     int i, month, year, ngray;
  442.  
  443. #define DO_HEADER(phdr)    for (ap = phdr; *ap; ap++) PRT("%s\n", *ap)
  444.  
  445.     INIT_COLORS;        /* set up default colors */
  446.  
  447.     /* isolate root program name (for use in error messages) */
  448.  
  449.     strcpy(progname, **argv ? *argv : "pcal");
  450.  
  451.     if ((p = strrchr(progname, END_PATH)) != NULL)
  452.         strcpy(progname, ++p);
  453.     if ((p = strchr(progname, '.')) != NULL)
  454.         *p = '\0';
  455.  
  456.     /*
  457.      * Get the arguments from a) the environment variable, b) "opt" lines
  458.      * in the date file, and c) the command line, in that order
  459.      */
  460.  
  461.     /* look for environment variable for options */
  462.  
  463.     if ((p = getenv(PCAL_OPTS)) != NULL) {
  464.         strcpy(lbuf, "x ");    /* prepend a dummy token */
  465.         strcat(lbuf, p);
  466.         loadwords();
  467.         if (! get_args(words, ALL_FLAGS, FALSE)) {
  468.             usage();
  469.             exit(EXIT_FAILURE);
  470.         }
  471.     }
  472.  
  473.     /* parse command-line arguments once to find name of date file, etc. */
  474.  
  475.     if (!get_args(argv, DATEFILE_FLAGS, TRUE)) {
  476.         usage();
  477.         exit(EXIT_FAILURE);
  478.     }
  479.  
  480.     /* Attempt to open the date file as specified by the [-e | -f] flags */
  481.  
  482.     switch (datefile_type) {
  483.     case NO_DATEFILE:
  484.         dfp = NULL;
  485.         break;
  486.  
  487.     case USER_DATEFILE:    
  488.         /* Attempt to open user-specified calendar file */
  489.         if ((dfp = fopen(datefile, "r")) == NULL) {
  490.             FPR(stderr, "%s: can't open file %s\n", progname, 
  491.                 datefile);
  492.             exit(EXIT_FAILURE);
  493.         }
  494.         mk_path(default_dir, datefile);    /* extract path */
  495.         break;
  496.  
  497.     case SYS_DATEFILE:
  498.         /* Attempt to open system-specified calendar file */
  499.         if ((p = trnlog(PCAL_DIR)) || (p = trnlog(HOME_DIR)))
  500.             strcpy(default_dir, p);
  501.  
  502.         mk_filespec(datefile, default_dir, DATEFILE);
  503.         dfp = fopen(datefile, "r");    /* no error if nonexistent */
  504. #ifdef ALT_DATEFILE
  505.         if (!dfp) {        /* try again with alternate file */
  506.             mk_filespec(datefile, default_dir, ALT_DATEFILE);
  507.             dfp = fopen(datefile, "r");
  508.         }
  509. #endif
  510.         break;
  511.     }
  512.  
  513.     /* read the date file (if any) and build internal data structure */
  514.  
  515.     if (dfp) {
  516.         curr_year = init_year;
  517.         read_datefile(dfp, datefile);
  518.         fclose(dfp);
  519.     }
  520.  
  521.     /* reparse command line - flags there supersede those in date file */
  522.  
  523.     get_args(argv, OTHER_FLAGS, FALSE);
  524.  
  525.     /* reshuffle days depending on first day of week selected by user */
  526.  
  527.     set_first_day(first_day_of_week);
  528.  
  529.     /* done with the arguments and flags - try to open the output file */
  530.  
  531.     if (*outfile && freopen(outfile, "w", stdout) == (FILE *) NULL) {
  532.         FPR(stderr, "%s: can't open file %s\n", progname, outfile);
  533.         exit(EXIT_FAILURE);
  534.     }
  535.  
  536.     /*
  537.      * Write out PostScript prolog
  538.      */
  539.  
  540.     /* font names */
  541.  
  542.      PRT("%%!\n");
  543.     PRT("/titlefont /%s def\n/dayfont /%s def\n/notesfont /%s def\n", titlefont, dayfont, notesfont);
  544.  
  545.     /* set offset from Sunday to selected first day of week */
  546.  
  547.     PRT("/dayoffset %d def\n", first_day_of_week);
  548.  
  549.     /* foot strings */
  550.  
  551.     def_footstring(lfoot, 'L');
  552.     def_footstring(cfoot, 'C');
  553.     def_footstring(rfoot, 'R');
  554.  
  555.     /* month names */
  556.  
  557.     PRT("/month_names [");
  558.     for (i = 0; i < 12; i++)
  559.         PRT("%s(%s) ", i % 6 == 0 ? "\n\t" : "", months[i]);
  560.     PRT("] def\n");
  561.  
  562.     /* day names */
  563.  
  564.     PRT("/day_names [");
  565.     for (i = 0; i < 7; i++)
  566.         PRT("%s(%s) ", i % 6 == 0 ? "\n\t" : "", days[i]);
  567.     PRT("] def\n");
  568.  
  569.     /* colors (black/gray) to print weekdays and holidays */
  570.  
  571.     PRT("/day_gray [");
  572.     for (ngray = i = 0; i < 7; ngray += color[i++] == GRAY)
  573.         PRT(" %s", color[i] == GRAY ? "true" : "false");
  574.     PRT(" ] def\n");
  575.     PRT("/holiday_gray %s def\n", ngray <= 3 ? "true" : "false");
  576.  
  577.     /* PostScript boilerplate (part 1 of 1) */
  578.     DO_HEADER(header);
  579.  
  580.     /* landscape or portrait mode */
  581.  
  582.     PRT("\n/landscape-p %s def\n", (rotate == LANDSCAPE) ? "true":"false");
  583.     PRT("/draw-moons %s def\n", draw_moons);
  584.  
  585.     /*
  586.      * Write out PostScript code to print calendars
  587.      */
  588.  
  589.     month = init_month;
  590.     year = init_year;
  591.  
  592.     while (nmonths--) {
  593.         pmonth(month, year);
  594.         if (++month > DEC) {
  595.             month = JAN;
  596.             year++;
  597.         }
  598.     }
  599.  
  600.     cleanup();
  601.  
  602. #ifdef VMS
  603.     FPR(stderr, "Output is in file %s\n", outfile);
  604. #endif
  605.     exit(EXIT_SUCCESS);
  606. }
  607.  
  608. /*
  609.  * get_args - walk the argument list, parsing all arguments but processing only
  610.  * those specified in "flags".  If "do_numargs" is TRUE, processes numeric
  611.  * arguments (month, year, number of months) as well.
  612.  */
  613. int get_args(argv, flags, do_numargs)
  614.     char **argv;        /* argument list */
  615.     char *flags;        /* which flags to process */
  616.     int do_numargs;        /* process numeric arguments? */
  617. {
  618.     char *p, *opt;
  619.     int i, do_flag;
  620.     long tmp;            /* for getting current month/year */
  621.     struct tm *p_tm;
  622.     int badopt = FALSE;        /* flag set if bad param   */
  623.     int nargs = 0;            /* count of non-flag args  */
  624.     int numargs[MAXARGS];        /* non-flag (numeric) args */
  625.  
  626. /* Look for the argument following flag - may be separated by spaces or
  627.  * not (bumps argv in former case).  If no non-flag argument appears, set
  628.  * "arg" to NULL (-b, -C, -d, -g, -L, -n, -o, -R, and -t without an argument
  629.  * reset the corresponding option to its default value).
  630.  */
  631. #define GETARG(arg) arg = *(*argv + 2) ? *argv + 2 : \
  632.             (*(argv+1) && **(argv+1) != '-' ? *++argv : NULL)
  633.  
  634.     /* Walk argument list, ignoring first element (program name) */
  635.  
  636.      while (*++argv) {
  637.  
  638.         /* Assume that any non-flag argument is a numeric argument */
  639.         if (**argv != '-') {
  640.                 if (do_numargs && nargs < MAXARGS)
  641.                 numargs[nargs++] = atoi(*argv);
  642.             continue;
  643.         }
  644.  
  645.         /* Is this flag among those to be processed beyond parsing? */
  646.  
  647.         do_flag = strchr(flags, *(opt = *argv + 1)) != NULL;
  648.  
  649.         switch (*opt) {
  650.  
  651.         case '\0':        /* take - or -- as dummy flags */
  652.         case '-' :
  653.             break;
  654.  
  655.         case 'b':        /* print day in black or gray */
  656.         case 'g':
  657.             GETARG(p);
  658.             if (do_flag)
  659.                 if (p)
  660.                     set_color(p, *opt == 'b' ? BLACK : GRAY);
  661.                 else
  662.                     INIT_COLORS;    /* reset to defaults */
  663.             break;
  664.  
  665.         case 'C':        /* specify alternate center foot */
  666.             GETARG(p);
  667.             if (do_flag)
  668.                 strcpy(cfoot, p ? p : CFOOT);
  669.             break;
  670.  
  671.          case 'd':        /* specify alternate day font */
  672.              GETARG(p);
  673.             if (do_flag)
  674.                 strcpy(dayfont, p ? p : DAYFONT);
  675.              break;
  676.  
  677.         case 'D':        /* define preprocessor symbol */
  678.             GETARG(p);
  679.             if (do_flag)
  680.                 do_define(p);
  681.             break;
  682.  
  683.         case 'e':        /* generate empty calendar */
  684.             if (do_flag) {
  685.                 datefile_type = NO_DATEFILE;
  686.                 datefile[0] = '\0';
  687.             }
  688.             break;
  689.  
  690.         case 'f':        /* specify alternate date file */
  691.             GETARG(p);
  692.             if (p && do_flag) {
  693.                 datefile_type = USER_DATEFILE;
  694.                 strcpy(datefile, p);
  695.             }
  696.             break;
  697.  
  698.         case 'L':        /* specify alternate left foot */
  699.             GETARG(p);
  700.             if (do_flag)
  701.                 strcpy(lfoot, p ? p : LFOOT);
  702.             break;
  703.  
  704.          case 'l':        /* generate landscape calendar */
  705.             if (do_flag)
  706.                  rotate = LANDSCAPE;
  707.              break;
  708.  
  709.         case 'n':        /* specify alternate notes font */
  710.             GETARG(p);
  711.             if (do_flag)
  712.                 strcpy(notesfont, p ? p : NOTESFONT);
  713.             break;
  714.  
  715.         case 'o':        /* specify alternate output file */
  716.             GETARG(p);
  717.             if (do_flag)
  718.                 strcpy(outfile, p ? p : OUTFILE);
  719.             break;
  720.  
  721.          case 'p':        /* generate portrait calendar */
  722.             if (do_flag)
  723.                  rotate = PORTRAIT;
  724.              break;
  725.  
  726.          case 'R':        /* specify alternate right foot */
  727.             GETARG(p);
  728.             if (do_flag)
  729.                 strcpy(rfoot, p ? p : RFOOT);
  730.             break;
  731.  
  732.  
  733.          case 't':        /* specify alternate title font */
  734.              GETARG(p);
  735.             if (do_flag)
  736.                 strcpy(titlefont, p ? p : TITLEFONT);
  737.              break;
  738.  
  739.         case 'U':        /* undef preprocessor symbol */
  740.             GETARG(p);
  741.             if (do_flag)
  742.                 do_undef(p);
  743.             break;
  744.  
  745.         case 'm':        /* draw four moons */
  746.             if (do_flag)
  747.                 draw_moons = "4";
  748.             break;
  749.  
  750.         case 'M':        /* draw a moon for each day */
  751.             if (do_flag)
  752.                 draw_moons = "true";
  753.             break;
  754.  
  755.         case 'F':        /* select starting day of week */
  756.             GETARG(p);
  757.             if (do_flag)  {
  758.                 if (p)  {
  759.                     for (i = 0; i < 7; i++)
  760.                         if (ci_strncmp(p, days[i],
  761.                                 MIN_DAY_LEN) == 0)
  762.                             first_day_of_week = i;
  763.                 }  else
  764.                     first_day_of_week = 0;    /* default */
  765.             }
  766.             break;
  767.  
  768.         default:        /* unrecognized flag */
  769.             FPR(stderr, "%s: illegal option -%s\n", progname, opt);
  770.             badopt = TRUE;
  771.             break;
  772.         }
  773.         }
  774.  
  775.     if (!do_numargs)
  776.         return !badopt;        /* return TRUE if OK, FALSE if error */
  777.  
  778.     /* Validate non-flag (numeric) parameters */
  779.  
  780.     switch (nargs) {
  781.     case 0:        /* no arguments - print current month/year */
  782.         time(&tmp);
  783.         p_tm = localtime(&tmp);
  784.         init_month = p_tm->tm_mon + 1;
  785.         init_year = p_tm->tm_year;
  786.         nmonths = 1;
  787.         break;            
  788.     case 1:        /* one argument - print entire year */
  789.         init_month = JAN;
  790.         init_year = numargs[0];
  791.         nmonths = 12;
  792.         break;
  793.     default:    /* two or three arguments - print one or more months */
  794.         init_month = numargs[0];
  795.         init_year = numargs[1];
  796.         nmonths = nargs > 2 ? numargs[2] : 1;
  797.         break;
  798.     }
  799.  
  800.     if (nmonths < 1)        /* ensure at least one month */
  801.         nmonths = 1;
  802.  
  803.     /* check range of month and year */
  804.  
  805.     if (init_month < JAN || init_month > DEC) {
  806.         FPR(stderr, "%s: month %d not in range 1 .. 12\n", progname,
  807.             init_month);
  808.         badopt = TRUE;
  809.     }
  810.     
  811.     if (init_year > 0 && init_year < 100)    /* treat nn as 19nn */
  812.         init_year += 1900;
  813.     
  814.     if (init_year < MIN_YR || init_year > MAX_YR) {
  815.         FPR(stderr, "%s year %d not in range %d .. %d\n", progname,
  816.             init_year, MIN_YR, MAX_YR);
  817.         badopt = TRUE;
  818.     }
  819.  
  820.     return !badopt;        /* return TRUE if OK, FALSE if error */
  821. }
  822.  
  823.  
  824.  
  825. /*
  826.  *    usage - print message explaining correct usage of the command-line
  827.  *    arguments and flags
  828.  */
  829. usage()
  830. {
  831.     FPR(stderr, "\nUsage:\t%s [-b|-g DAY]* [-d|-n|-t FONT] [-e | -f FILE] [-o FILE] [-l | -p]\n", progname);
  832.     FPR(stderr, "\t[-m|-M] [-D|-U SYM] [-L|-C|-R STRING] [ [ [mm] yy ] | [mm yy n] ]\n");
  833.     FPR(stderr, "\n");
  834.     FPR(stderr, "\t-b DAY\t\tprint weekday DAY in black\n");
  835.     FPR(stderr, "\t-g DAY\t\tprint weekday DAY in gray\n");
  836.     FPR(stderr, "\t\t\t(default: %s)\n", COLOR_MSG);
  837.     FPR(stderr, "\n");
  838.     FPR(stderr, "\t-d FONT\t\tspecify alternate day name font (default: %s)\n",
  839.         DAYFONT);
  840.     FPR(stderr, "\t-n FONT\t\tspecify alternate notes font (default: %s)\n",
  841.         NOTESFONT);
  842.     FPR(stderr, "\t-t FONT\t\tspecify alternate title font (default: %s)\n",
  843.         TITLEFONT);
  844.     FPR(stderr, "\n");
  845.     FPR(stderr, "\t-e\t\tgenerate empty calendar (ignore date file)\n");
  846.     FPR(stderr, "\t-f FILE\t\tspecify alternate date file (default: %s)\n",
  847.         DATEFILE);
  848.     FPR(stderr, "\t-o FILE\t\tspecify alternate output file (default: %s)\n",
  849.         OUTFILE[0] ? OUTFILE : "stdout");
  850.     FPR(stderr, "\n");
  851.     FPR(stderr, "\t-l\t\tgenerate landscape-style calendars");
  852. #if (ROTATE == LANDSCAPE)
  853.     FPR(stderr, " (default)");
  854. #endif
  855.     FPR(stderr, "\n\t-p\t\tgenerate portrait-style calendars");
  856. #if (ROTATE == PORTRAIT)
  857.     FPR(stderr, " (default)");
  858. #endif
  859.     FPR(stderr, "\n\n");
  860.     FPR(stderr, "\t-m\t\t\draw a \"moon\" icon on days of full, new, and half moons\n");
  861. #ifdef VMS
  862.     FPR(stderr, "\t-\"M\"\t\t\draw a \"moon\" icon every day (default: no moons)\n");
  863.     FPR(stderr, "\n");
  864.     FPR(stderr, "\t-\"D\" SYM\tdefine preprocessor symbol\n");
  865.     FPR(stderr, "\t-\"U\" SYM\tundefine preprocessor symbol\n");
  866.     FPR(stderr, "\n");
  867.     FPR(stderr, "\t-\"L\" STRING\tspecify left foot string (default: \"%s\")\n",
  868.         LFOOT);
  869.     FPR(stderr, "\t-\"C\" STRING\tspecify center foot string (default: \"%s\")\n",
  870.         CFOOT);
  871.     FPR(stderr, "\t-\"R\" STRING\tspecify right foot string (default: \"%s\")\n",
  872.         RFOOT);
  873. #else
  874.     FPR(stderr, "\t-M\t\t\draw a \"moon\" icon every day (default: no moons)\n");
  875.     FPR(stderr, "\n");
  876.     FPR(stderr, "\t-D SYM\t\tdefine preprocessor symbol\n");
  877.     FPR(stderr, "\t-U SYM\t\tundefine preprocessor symbol\n");
  878.     FPR(stderr, "\n");
  879.     FPR(stderr, "\t-L STRING\tspecify left foot string (default: \"%s\")\n",
  880.         LFOOT);
  881.     FPR(stderr, "\t-C STRING\tspecify center foot string (default: \"%s\")\n",
  882.         CFOOT);
  883.     FPR(stderr, "\t-R STRING\tspecify right foot string (default: \"%s\")\n",
  884.         RFOOT);
  885. #endif
  886.     FPR(stderr, "\n");
  887.     FPR(stderr, "\tyy\t\tgenerate calendar for year yy (19yy if yy < 100)\n");
  888.     FPR(stderr, "\tmm yy\t\tgenerate calendar for month mm (Jan = 1), year yy\n");
  889.     FPR(stderr, "\tmm yy n\t\tgenerate calendars for n months, starting at mm/yy\n");
  890.     FPR(stderr, "\t(default)\tgenerate calendar for current month/year\n");
  891. }
  892.  
  893.  
  894. /*
  895.  * General-purpose utility routines
  896.  */
  897.  
  898.  
  899. /*
  900.  * alloc - interface to calloc(); terminates if unsuccessful
  901.  */
  902. char *alloc(size)
  903.     int size;
  904. {
  905.     char *p;
  906.     extern char *calloc();
  907.  
  908.     if (size == 0)        /* not all calloc()s like null requests */
  909.         size = 1;
  910.  
  911.     if ((p = calloc(1, size)) == NULL) {
  912.         FPR(stderr, "%s: out of memory\n", progname);
  913.         exit(EXIT_FAILURE);
  914.     }
  915.  
  916.     return p;
  917. }
  918.  
  919.  
  920. /*
  921.  * ci_str{n}cmp - case-insensitive flavors of strcmp(), strncmp()
  922.  */
  923. int ci_strcmp(s1, s2)
  924. register char *s1, *s2;
  925. {
  926.     register char c1, c2;
  927.  
  928.     for ( ; (c1 = TOLOWER(*s1)) == (c2 = TOLOWER(*s2)); s1++, s2++)
  929.         if (c1 == '\0')
  930.             return 0;
  931.  
  932.     return c1 - c2;
  933. }
  934.  
  935.  
  936. int ci_strncmp(s1, s2, n)
  937. register char *s1, *s2;
  938. int n;
  939. {
  940.     register char c1, c2;
  941.  
  942.     for ( ; --n >= 0 && (c1 = TOLOWER(*s1)) == (c2 = TOLOWER(*s2)); s1++, s2++)
  943.         if (c1 == '\0')
  944.             return 0;
  945.  
  946.     return n < 0 ? 0 : c1 - c2;
  947. }
  948.  
  949.  
  950. /*
  951.  * Preprocessor token and symbol table routines
  952.  */
  953.  
  954.  
  955. /*
  956.  * find_sym - look up symbol; return symbol table index if found, PP_SYM_UNDEF
  957.  * if not found
  958.  */
  959. int find_sym(sym)
  960.     char *sym;
  961. {
  962.     int i;
  963.  
  964.     if (!sym)
  965.         return PP_SYM_UNDEF;
  966.  
  967.     for (i = 0; i < MAX_PP_SYMS; i++)
  968.         if (pp_sym[i] && ci_strcmp(pp_sym[i], sym) == 0)
  969.             return i;
  970.  
  971.     return PP_SYM_UNDEF;
  972. }
  973.  
  974.  
  975. /*
  976.  * do_ifdef - return TRUE if 'sym' is currently defined; FALSE if not
  977.  */
  978. int do_ifdef(sym)
  979.     char *sym;
  980. {
  981.     return find_sym(sym) != PP_SYM_UNDEF;
  982. }
  983.  
  984.  
  985. /*
  986.  * do_ifndef - return FALSE if 'sym' is currently defined; TRUE if not
  987.  */
  988. int do_ifndef(sym)
  989.     char *sym;
  990. {
  991.     return find_sym(sym) == PP_SYM_UNDEF;
  992. }
  993.  
  994.  
  995. /*
  996.  * do_define - enter 'sym' into symbol table; if 'sym' NULL, clear symbol table
  997.  */
  998. do_define(sym)
  999.     char *sym;
  1000. {
  1001.     int i;
  1002.  
  1003.     if (! sym) {        /* null argument - clear all definitions */
  1004.         clear_syms();
  1005.         return;
  1006.     }
  1007.  
  1008.     if (do_ifdef(sym))    /* already defined? */
  1009.         return;
  1010.  
  1011.     for (i = 0; i < MAX_PP_SYMS; i++)    /* find room for it */
  1012.         if (! pp_sym[i]) {
  1013.             strcpy(pp_sym[i] = alloc(strlen(sym)+1), sym);
  1014.             return;
  1015.         }
  1016.  
  1017.     FPR(stderr, "%s: no room to define %s\n", progname, sym);
  1018. }
  1019.  
  1020.  
  1021. /*
  1022.  * do_undef - undefine 'sym' and free its space; no error if not defined
  1023.  */
  1024. do_undef(sym)
  1025.     char *sym;
  1026. {
  1027.     int i;
  1028.  
  1029.     if (! sym) 
  1030.         return;
  1031.  
  1032.     if ((i = find_sym(sym)) != PP_SYM_UNDEF) {
  1033.         free(pp_sym[i]);
  1034.         pp_sym[i] = NULL;
  1035.     }
  1036. }
  1037.  
  1038.  
  1039. /*
  1040.  * do_include - include specified file (optionally in "" or <>)
  1041.  */
  1042. do_include(path, name)
  1043.     char *path;        /* path to file */
  1044.     char *name;        /* file name */
  1045. {
  1046.     FILE *fp;
  1047.     char *p, incfile[STRSIZ], tmpnam[STRSIZ];
  1048.  
  1049.     if (! name)        /* whoops, no date file */
  1050.         return;
  1051.  
  1052.     /* copy name, stripping "" or <> */
  1053.     strcpy(tmpnam, name + (*name == '"' || *name == '<'));
  1054.     if ((p = LASTCHAR(tmpnam)) && *p == '"' || *p == '>')
  1055.         *p = '\0';
  1056.  
  1057.     if ((fp = fopen(mk_filespec(incfile, path, tmpnam), "r")) == NULL) {
  1058.         FPR(stderr, "%s: can't open file %s\n",    progname, incfile);
  1059.         exit(EXIT_FAILURE);
  1060.     }
  1061.  
  1062.     read_datefile(fp, incfile);
  1063.     fclose(fp);
  1064. }
  1065.  
  1066.  
  1067. /*
  1068.  * pp_token - look up 'token' in list of preprocessor tokens; return its
  1069.  * index if found, PP_OTHER if not (N.B.: this relies on the ordering of
  1070.  * PP_XXXXX and pp_info[]; see comments at their definitions).
  1071.  */
  1072. int pp_token(token)
  1073.     char *token;
  1074. {
  1075.     struct pp *p;
  1076.  
  1077.     for (p = pp_info;
  1078.              p->token && ci_strncmp(token, p->token, MIN_PPTOK_LEN);
  1079.          p++)
  1080.         ;
  1081.  
  1082.     return p - pp_info;
  1083. }
  1084.  
  1085. /*
  1086.  * Internal data structure routines
  1087.  */
  1088.  
  1089.  
  1090. /*
  1091.  * find_year - find record in year list; optionally create if not present 
  1092.  */
  1093. year_info *find_year(year, insert)    /* find record in year list */
  1094.     int year;
  1095.     int insert;            /* insert if missing */
  1096. {
  1097.     year_info *pyear, *plast, *p;
  1098.  
  1099.     for (plast = NULL, pyear = head;        /* search linked list */
  1100.          pyear && pyear->year < year;
  1101.          plast = pyear, pyear = pyear->next)
  1102.         ;
  1103.  
  1104.     if (pyear && pyear->year == year)        /* found - return it */
  1105.         return pyear;
  1106.  
  1107.     if (insert) {        /* not found - insert it if requested */
  1108.         p = (year_info *) alloc(sizeof(year_info));    /* create new record */
  1109.         p->year = year;
  1110.  
  1111.         p->next = pyear;                /* link it in */
  1112.         return *(plast ? &plast->next : &head) = p;
  1113.     }
  1114.     else
  1115.         return NULL;
  1116. }
  1117.  
  1118.  
  1119. /*
  1120.  * enter_day_info - enter text for specified day; avoid entering duplicates.
  1121.  * returns PARSE_INVDATE if date invalid, PARSE_OK if OK
  1122.  */
  1123. int enter_day_info(m, d, y, text_type, pword)    /* fill in information for given day */
  1124.     int m, d, y;
  1125.     int text_type;
  1126.     char **pword;
  1127. {
  1128.     static year_info *pyear;
  1129.     static int prev_year = 0;
  1130.     month_info *pmonth;
  1131.     day_info *pday, *plast;
  1132.     int is_holiday = text_type == HOLIDAY_TEXT;
  1133.  
  1134.     if (! is_valid(m, d == NOTE_DAY && text_type == NOTE_TEXT ? 1 : d, y))
  1135.         return PARSE_INVDATE;
  1136.  
  1137.     if (y != prev_year)        /* avoid unnecessary year lookup */
  1138.         pyear = find_year(y, 1);
  1139.  
  1140.     --m, --d;            /* adjust for use as subscripts */
  1141.  
  1142.     if ((pmonth = pyear->month[m]) == NULL)    /* find/create month record */
  1143.         pyear->month[m] = pmonth = (month_info *) alloc(sizeof(month_info));
  1144.  
  1145.     if (is_holiday)
  1146.         pmonth->holidays |= (1 << d);
  1147.  
  1148.     /* insert text for day at end of list (preserving the order of entry
  1149.      * for multiple lines on same day); eliminate those differing only
  1150.      * in spacing and capitalization from existing entries
  1151.          */
  1152.  
  1153.     get_text(pword);    /* consolidate text into lbuf */
  1154.  
  1155.     if (*lbuf) {
  1156.         for (plast = NULL, pday = pmonth->day[d];
  1157.              pday;
  1158.              plast = pday, pday = pday->next)
  1159.             if (ci_strcmp(pday->text, lbuf) == 0) {
  1160.                 pday->is_holiday |= is_holiday;
  1161.                 return PARSE_OK;
  1162.             }
  1163.  
  1164.         /* unique - add to end of list */
  1165.  
  1166.         pday = (day_info *) alloc(sizeof(day_info));
  1167.         pday->is_holiday = is_holiday;
  1168.         strcpy(pday->text = (char *) alloc(strlen(lbuf)+1), lbuf);
  1169.         pday->next = NULL;
  1170.         *(plast ? &plast->next : &pmonth->day[d]) = pday;
  1171.     }
  1172.  
  1173.     return PARSE_OK;
  1174. }
  1175.  
  1176.  
  1177. /*
  1178.  * Housekeeping routines to free allocated data
  1179.  */
  1180.  
  1181.  
  1182. /*
  1183.  * clear_syms - clear and deallocate the symbol table
  1184.  */
  1185. clear_syms()
  1186. {
  1187.     int i;
  1188.  
  1189.     for (i = 0; i < MAX_PP_SYMS; i++)
  1190.         if (pp_sym[i]) {
  1191.             free(pp_sym[i]);
  1192.             pp_sym[i] = NULL;
  1193.         }
  1194. }
  1195.  
  1196.  
  1197. /*
  1198.  * cleanup - free all allocated data
  1199.  */
  1200. cleanup()
  1201. {
  1202.     int i, j;
  1203.     year_info *py, *pny;
  1204.     month_info *pm;
  1205.     day_info *pd, *pnd;
  1206.  
  1207.     for (py = head; py; py = pny) {        /* main data structure */
  1208.         pny = py->next;
  1209.         for (i = 0; i < 12; i++) {
  1210.             if ((pm = py->month[i]) == NULL)
  1211.                 continue;
  1212.             for (j = 0; j < NOTE_DAY; j++)
  1213.                 for (pd = pm->day[j]; pd; pd = pnd) {
  1214.                     pnd = pd->next;
  1215.                     free(pd->text);
  1216.                     free(pd);
  1217.                 }
  1218.             free(pm);
  1219.         }
  1220.     free(py);
  1221.     }
  1222.  
  1223.     clear_syms();                /* symbol table */
  1224.  
  1225. }
  1226.  
  1227.  
  1228. /*
  1229.  * Utility routines for date and text parsing and entry
  1230.  */
  1231.  
  1232. /*
  1233.  * get_month - convert numeric or alpha string to month; return 1-12 if valid,
  1234.  * 0 if not valid
  1235.  */
  1236. int get_month(cp)
  1237.     char *cp;
  1238. {
  1239.     int mm;
  1240.  
  1241.     if (! cp)
  1242.         return 0;
  1243.  
  1244.     if (isdigit(*cp))
  1245.         mm = atoi(cp);
  1246.     else
  1247.         for (mm = JAN;
  1248.              mm <= DEC && ci_strncmp(cp, months[mm-1], MIN_MONTH_LEN);
  1249.              mm++)
  1250.             ;
  1251.  
  1252.     return mm >= JAN && mm <= DEC ? mm : 0;
  1253. }
  1254.  
  1255.  
  1256. /*
  1257.  * date_type - examine token and return date type code; if DF_MONTH, fill
  1258.  * in number of month
  1259.  */
  1260. int date_type(cp, pm)
  1261.     char *cp;    /* pointer to start of token */
  1262.     int *pm;    /* month value (returned)    */
  1263. {
  1264.     int mm;
  1265.  
  1266.     if (isdigit(*cp))
  1267.         return DF_DATE;
  1268.  
  1269.     if (ci_strncmp(cp, YEAR, strlen(YEAR)) == 0)
  1270.         return DF_YEAR;
  1271.  
  1272.     if (ci_strncmp(cp, OPT, strlen(OPT)) == 0)
  1273.         return DF_OPT;
  1274.  
  1275.     if (ci_strncmp(cp, NOTE, strlen(NOTE)) == 0)
  1276.         return DF_NOTE;
  1277.  
  1278.     return (mm = get_month(cp)) != 0 ? (*pm = mm, DF_MONTH) : DF_OTHER;
  1279. }
  1280.  
  1281.  
  1282. /*
  1283.  * is_valid - return TRUE if m/d/y is a valid date
  1284.  */
  1285. int is_valid(m, d, y)
  1286.     register int m, d, y;
  1287. {
  1288.     static char len[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  1289.  
  1290.     return m >= JAN && m <= DEC && 
  1291.         d >= 1 && d <= (len[m] + (m == FEB && IS_LEAP(y)));
  1292. }
  1293.  
  1294.  
  1295. /*
  1296.  * loadwords - tokenize line buffer into word array, return word count.
  1297.  * differs from old loadwords() in that it handles quoted (" or ') strings
  1298.  */
  1299.  
  1300. int loadwords()
  1301. {
  1302.     register char *pstr, *ptok;
  1303.     char *delim, **ap;
  1304.     register int i;
  1305.  
  1306.     pstr = lbuf;
  1307.  
  1308.     for (i = 0, ap = words; TRUE; i++, ap++) {
  1309.         delim =    *pstr == '"'  ? "\"" :
  1310.             *pstr == '\'' ? "'"  :
  1311.             WHITESPACE;
  1312.  
  1313.         ptok = pstr += strspn(pstr, delim); /* look for next token */
  1314.  
  1315.         if (! *pstr) {        /* end of lbuf? */
  1316.             *ap = NULL;    /* add null ptr at end */
  1317.             return i;    /* return count of non-null ptrs */
  1318.             }
  1319.  
  1320.         if (*ptok == '"' || *ptok == '\'')    /* bump past quote */
  1321.             ptok++;
  1322.  
  1323.         *ap = ptok;                /* save token ptr */
  1324.  
  1325.         pstr += strcspn(pstr, delim);         /* skip past token */
  1326.  
  1327.         if (*pstr)                /* terminate token */
  1328.             *pstr++ = '\0';
  1329.         }
  1330.  
  1331. }
  1332.  
  1333.  
  1334. /*
  1335.  * get_text - retrieve remaining text in lbuf and transform in place, removing
  1336.  * leading/trailing whitespace and condensing runs of whitespace to one blank
  1337.  */
  1338. get_text(pword)
  1339.     char **pword;        /* pointer to first desired word in "words" */
  1340. {
  1341. char *pbuf, *p;
  1342.  
  1343. /* copy words back to lbuf in place, separating by one blank */
  1344.  
  1345. for (pbuf = lbuf; p = *pword; *pbuf++ = *++pword ? ' ' : '\0')
  1346.     while (*p)
  1347.         *pbuf++ = *p++;
  1348.  
  1349. if (pbuf == lbuf)
  1350.     *lbuf = '\0';
  1351. }
  1352.  
  1353.  
  1354. /*
  1355.  * print_text - print tokens in text (assumed separated by single blank)
  1356.  * in PostScript format; convert '(' or ')' to octal escape
  1357.  */
  1358. print_text(p)
  1359.     char *p;
  1360. {
  1361.     char c;
  1362.  
  1363.     PRT("(");
  1364.     for ( ; c = *p ; p++) {
  1365.         if (c == ' ')
  1366.             PRT(")\n(");
  1367.         else
  1368.             PRT(c == '(' || c == ')' ? "\\%03o" : "%c", c);
  1369.     }
  1370.     PRT(")\n");
  1371. }
  1372.  
  1373.  
  1374. /*
  1375.  * def_footstring - print definition for foot string, again converting '('
  1376.  * or ')' to octal escape
  1377.  */
  1378. def_footstring(p, c)
  1379.     char *p;            /* definition */
  1380.     char c;                /* L, C, or R */
  1381. {
  1382.  
  1383.     PRT("/%cfootstring (", c);
  1384.  
  1385.     while (c = *p++)
  1386.         PRT(c == '(' || c == ')' ? "\\%03o" : "%c", c);
  1387.  
  1388.     PRT(") def\n");
  1389. }
  1390.  
  1391.  
  1392. /*
  1393.  * set_color - set one or all weekdays to print in black or gray
  1394.  */
  1395. set_color(day, col)
  1396.     char *day;        /* weekday name (or "all") */
  1397.     int  col;        /* select black or gray */
  1398. {
  1399.     int i, do_all;
  1400.  
  1401.     do_all = ci_strncmp(day, ALL, strlen(ALL)) == 0;    /* set all days? */
  1402.  
  1403.     for (i = 0; i < 7; i++)
  1404.         if (do_all || ci_strncmp(day, days[i], MIN_DAY_LEN) == 0)
  1405.             color[i] = col;
  1406. }
  1407.  
  1408. /*
  1409.  * set_first_day - change the first day of the week from Sunday to any day
  1410.  */
  1411. set_first_day(day)
  1412.     int day;        /* day of week number; 0 = Sun, 1 = Mon, etc. */
  1413. {
  1414.     char temp_color[7], *temp_days[7];
  1415.     int i, from_index;
  1416.  
  1417.     if (day == 0)        /* if Sunday, no need to change things */
  1418.         return;
  1419.  
  1420.     /*
  1421.      * first we save off the data items in the "color" and "days"
  1422.      * arrays which must be reshuffled.
  1423.      */
  1424.  
  1425.     for (i = 0; i < 7; i++)  {
  1426.         temp_color[i] = color[i];
  1427.         temp_days[i] = days[i];
  1428.     }
  1429.  
  1430.     /*
  1431.      * now we reshuffle the copies back into the originals after
  1432.          * offsetting by the apprpriate number of days
  1433.      */
  1434.  
  1435.     for (i = 0; i < 7; i++)  {
  1436.         from_index = (i + day) % 7;
  1437.         color[i] = temp_color[from_index];
  1438.         days[i] = temp_days[from_index];
  1439.     }
  1440. }
  1441.  
  1442. /*
  1443.  * parse - enter date file data (in lbuf[]) into data structure
  1444.  *
  1445.  * Looks for an entry of one of the following forms:
  1446.  *
  1447.  *    year <year>
  1448.  *    <month_name> <day>{*} {<text>}
  1449.  *    <month><sep><day>{<sep><year>}{*} {<text>}
  1450.  *    opt <options>
  1451.  *    note <month_name> <text>
  1452.  *    note <month> <text>
  1453.  *
  1454.  * where
  1455.  *    <month_name> := first 3+ characters of name of month (in English)
  1456.  *    <sep> := one or more non-numeric, non-space, non-'*' characters
  1457.  *    <options> := any command-line option except -e, -f, -D, -U
  1458.  *
  1459.  * Enters information for year, month, and day into data structure for
  1460.  * subsequent retrieval.  Lines of form "opt <options>" override any
  1461.  * defaults.
  1462.  *
  1463.  * N.B.: "inc" and other cpp-like lines are handled in read_datefile().
  1464.  *
  1465.  */
  1466. int parse()
  1467. {
  1468.     register char *cp;
  1469.     char **pword, *p;
  1470.     int mm, dd, yy;
  1471.     int text_type;
  1472.  
  1473. /* macro to skip numeric field */
  1474.  
  1475. #define SKIP_FIELD(p) \
  1476.     if (1) {while (isdigit(*p)) p++; while (*p && !isdigit(*p)) p++;} else
  1477.  
  1478.     /*
  1479.      * Get first field - can be either "year", "opt", "note", a month
  1480.      * name, or a (complete) numeric date spec
  1481.          */
  1482.  
  1483.     cp = *(pword = words);
  1484.  
  1485.     switch (date_type(cp, &mm)) {
  1486.  
  1487.     case DF_YEAR:
  1488.         if ((cp = *++pword) != NULL && (yy = atoi(cp)) > 0) {
  1489.             if (yy < 100)
  1490.                 yy += 1900;
  1491.             curr_year = yy;
  1492.             return PARSE_OK;
  1493.         }
  1494.         return PARSE_INVDATE;    /* year missing or invalid */
  1495.         break;
  1496.  
  1497.     case DF_OPT:
  1498.          if (!get_args(words, OTHER_FLAGS, FALSE)) {
  1499.             usage();
  1500.             exit(EXIT_FAILURE);
  1501.         }
  1502.         return PARSE_OK;
  1503.         break;
  1504.  
  1505.     case DF_NOTE:
  1506.         mm = get_month(*++pword);
  1507.         return enter_day_info(mm, NOTE_DAY, curr_year, NOTE_TEXT,
  1508.             ++pword);
  1509.         break;
  1510.  
  1511.     case DF_MONTH:
  1512.         if ((cp = *++pword) == NULL || (dd = atoi(cp)) == 0)
  1513.             return PARSE_INVDATE;
  1514.         text_type = cp[strlen(cp) - 1] == '*' ? HOLIDAY_TEXT : DAY_TEXT;
  1515.  
  1516.         return enter_day_info(mm, dd, curr_year, text_type, ++pword);
  1517.         break;
  1518.  
  1519.     case DF_DATE:
  1520.         text_type = cp[strlen(cp) - 1] == '*' ? HOLIDAY_TEXT : DAY_TEXT;
  1521.  
  1522.         /* extract month and day fields */
  1523.  
  1524.         mm = atoi(cp);
  1525.         SKIP_FIELD(cp);
  1526.  
  1527.         dd = atoi(cp);
  1528.         SKIP_FIELD(cp);
  1529.  
  1530.         /* Numeric dates may (or may not) have a year */
  1531.  
  1532.         if ((yy = atoi(cp)) > 0) {
  1533.             if (yy < 100)
  1534.                 yy += 1900;
  1535.             curr_year = yy; /* if present, reset current year */
  1536.         }
  1537.  
  1538.         return enter_day_info(mm, dd, curr_year, text_type, ++pword);
  1539.         break;
  1540.  
  1541.     case DF_OTHER:
  1542.         return PARSE_INVLINE;        /* line not recognized */
  1543.         break;
  1544.     }
  1545. }
  1546.  
  1547.  
  1548. /*
  1549.  * getline - read next non-null line of input file into lbuf; return 0 on EOF
  1550.  */
  1551. int getline(dfp, pline)
  1552.     FILE *dfp;
  1553.     int *pline;
  1554. {
  1555.     register char *cp;
  1556.     register int c;
  1557.     int in_comment;        /* comments: from '#' to end-of-line */
  1558.  
  1559.     cp = lbuf;
  1560.     do {
  1561.         in_comment = FALSE;
  1562.         while ((c = getc(dfp)) != '\n' && c != EOF) {
  1563.             if (c == '#')
  1564.                 in_comment = TRUE;
  1565.  
  1566.             /* ignore comments and leading white space */
  1567.             if (in_comment ||
  1568.                 (cp == lbuf && (c == ' ' || c == '\t')))
  1569.                 continue;
  1570.             *cp++ = c;
  1571.         }
  1572.         if (c == EOF)
  1573.             return FALSE;
  1574.  
  1575.         (*pline)++;    /* bump line number */
  1576.  
  1577.     } while (cp == lbuf);    /* ignore empty lines */
  1578.  
  1579.     *cp = '\0';
  1580.     return TRUE;
  1581. }
  1582.  
  1583.  
  1584. /*
  1585.  * read_datefile - read and parse date file, handling preprocessor lines
  1586.  */
  1587. read_datefile(fp, filename)
  1588.     FILE *fp;        /* file pointer (assumed open) */
  1589.     char *filename;        /* file name (for error messages) */
  1590. {
  1591.     static int file_level = 0;
  1592.     int if_level = 0;
  1593.     int restart_level = 0;
  1594.     int line = 0;
  1595.     int processing = TRUE;
  1596.  
  1597.     int pptype, extra, ntokens, save_year;
  1598.     int (*pfcn)();
  1599.     char *ptok;
  1600.     char **pword;
  1601.     char msg[STRSIZ], incpath[STRSIZ];
  1602.  
  1603. #define ERR(errmsg)    FPR(stderr, "%s: %s in file %s, line %d\n", \
  1604.             progname, errmsg, filename, line);
  1605.  
  1606.     if (fp == NULL)        /* whoops, no date file */
  1607.         return;
  1608.  
  1609.     if (++file_level > MAX_NESTING) {
  1610.         ERR("maximum file nesting level exceeded");
  1611.         exit(EXIT_FAILURE);
  1612.     }
  1613.  
  1614.     save_year = curr_year;        /* save default year */
  1615.  
  1616.     /* read lines until EOF */
  1617.  
  1618.     while (getline(fp, &line)) {
  1619.  
  1620.         ntokens = loadwords();        /* split line into tokens */
  1621.         pword = words;            /* point to the first */
  1622.  
  1623.         /* get token type and pointers to function and name */
  1624.  
  1625.         pptype = pp_token(*pword++);
  1626.         pfcn = pp_info[pptype].pfcn;
  1627.         ptok = pp_info[pptype].token;
  1628.  
  1629.         switch (pptype) {
  1630.  
  1631.         case PP_DEFINE:
  1632.         case PP_UNDEF:
  1633.             if (processing)
  1634.                 (*pfcn)(*pword);
  1635.             extra = ntokens > 2;
  1636.             break;
  1637.  
  1638.         case PP_ELSE:
  1639.             if (if_level < 1) {
  1640.                 ERR("unmatched \"else\"");
  1641.                 break;
  1642.             }
  1643.  
  1644.             if (processing) {
  1645.                 processing = FALSE;    /* disable processing */
  1646.                 restart_level = if_level;
  1647.             } else if (if_level == restart_level) {
  1648.                 processing = TRUE;    /* re-enable processing */
  1649.                 restart_level = 0;
  1650.             }
  1651.             extra = ntokens > 1;
  1652.             break;
  1653.  
  1654.         case PP_ENDIF:
  1655.             if (if_level < 1) {
  1656.                 ERR("unmatched \"end\"");
  1657.                 break;
  1658.             }
  1659.  
  1660.             if (! processing && if_level == restart_level) {
  1661.                 processing = TRUE;    /* re-enable processing */
  1662.                 restart_level = 0;
  1663.             }
  1664.             if_level--;
  1665.             extra = ntokens > 1;
  1666.             break;
  1667.  
  1668.         case PP_IFDEF:
  1669.         case PP_IFNDEF:
  1670.             if_level++;
  1671.             if (processing) {
  1672.                 if (! (*pfcn)(*pword)) {
  1673.                     processing = FALSE;
  1674.                     restart_level = if_level;
  1675.                 }
  1676.             }
  1677.             extra = ntokens > 2;
  1678.             break;
  1679.  
  1680.         case PP_INCLUDE:
  1681.             if (processing)
  1682.                 do_include(mk_path(incpath, filename), *pword);
  1683.             extra = ntokens > 2;
  1684.             break;
  1685.  
  1686.         case PP_OTHER:    /* none of the above - parse as date */
  1687.             if (processing) {
  1688.                 switch (parse()) {
  1689.  
  1690.                 case PARSE_INVDATE:
  1691.                     ERR("invalid date");
  1692.                     break;
  1693.  
  1694.                 case PARSE_INVLINE:
  1695.                     ERR("unrecognized line");
  1696.                     break;
  1697.  
  1698.                 }
  1699.             }
  1700.             extra = FALSE;
  1701.             break;
  1702.  
  1703.         } /* end switch */
  1704.  
  1705.         if (extra) {        /* extraneous data? */
  1706.             sprintf(msg, "extraneous data on \"%s\" line", ptok);
  1707.             ERR(msg);
  1708.         }
  1709.  
  1710.     } /* end while */
  1711.  
  1712.     if (if_level > 0)
  1713.         FPR(stderr, "%s: unterminated if{n}def..{else..}endif in %s\n",
  1714.             progname, filename);
  1715.  
  1716.     file_level--;
  1717.     curr_year = save_year;        /* restore default year */
  1718. }
  1719.  
  1720. /*
  1721.  * Routines to extract and print data
  1722.  */
  1723.  
  1724.  
  1725. /*
  1726.  * Browse through the data structure looking for day, holiday, or notes text
  1727.  * in the specified month/year
  1728.  */
  1729. find_daytext(month, year, is_holiday)
  1730.     int month, year;
  1731.     int is_holiday;
  1732. {
  1733.     register int day;
  1734.     year_info *py;
  1735.     month_info *pm;
  1736.     register day_info *pd;
  1737.     int first;
  1738.     char *fcn = is_holiday ? "holidaytext" : "daytext";
  1739.  
  1740.     /* if no text for this year and month, return */
  1741.  
  1742.     if ((py = find_year(year, FALSE)) == NULL ||
  1743.         (pm = py->month[month-1]) == NULL)
  1744.         return;
  1745.  
  1746.     /* walk array of day text pointers and linked lists of text */
  1747.  
  1748.     for (day = 1; day <= NOTE_DAY; day++) {
  1749.         for (pd = pm->day[day-1], first = TRUE;
  1750.              pd;
  1751.              pd = pd->next) {
  1752.             if (pd->is_holiday != is_holiday)
  1753.                 continue;
  1754.             if (first) {
  1755.                 if (day != NOTE_DAY)    /* set up call */
  1756.                     PRT("%d ", day);
  1757.                 printf("[ \n");
  1758.             } else
  1759.                 PRT("(.p)\n");        /* separate text */
  1760.             print_text(pd->text);
  1761.             first = FALSE;
  1762.         }
  1763.         if (! first)        /* wrap up call (if one made) */
  1764.             PRT("] %s\n", day == NOTE_DAY ? "notetext" : fcn);
  1765.     }
  1766. }
  1767.  
  1768.  
  1769. /*
  1770.  * Browse through the date file looking for holidays in specified month/year
  1771.  */
  1772. find_holidays(month, year)
  1773.     int month, year;
  1774. {
  1775.     register int day;
  1776.     register unsigned long holidays;
  1777.     year_info *py;
  1778.     month_info *pm;
  1779.  
  1780.     pm = (py = find_year(year, FALSE)) ? py->month[month-1] : NULL;
  1781.  
  1782.      PRT("/note_block %s def\n", pm && pm->day[NOTE_DAY-1] ? "true" :
  1783.         "false");        /* are there notes? */
  1784.  
  1785.     PRT("/holidays [");    /* start definition of list */
  1786.  
  1787.     for (holidays = pm ? pm->holidays : 0, day = 1;
  1788.          holidays;
  1789.          holidays >>= 1, day++)
  1790.         if (holidays & 01)
  1791.             PRT(" %d", day);
  1792.  
  1793.     PRT(" 99 ] def\n");    /* terminate with dummy entry */
  1794.  
  1795. }
  1796.  
  1797.  
  1798. /*
  1799.  * pmonth - generate calendar for specified month/year
  1800.  */
  1801. pmonth(month, year)
  1802.     int month, year;
  1803. {
  1804.  
  1805.     PRT("/year %d def\n", year);        /* set up year and month */
  1806.     PRT("/month %d def\n", month);
  1807.     find_holidays(month, year);        /* make list of holidays */
  1808.     PRT("printmonth\n");
  1809.     find_daytext(month, year, TRUE);    /* holiday text */
  1810.     find_daytext(month, year, FALSE);    /* day and note text */
  1811.     PRT("showpage\n");
  1812. }
  1813.  
  1814.  
  1815. /*
  1816.  * Routines dealing with translation of file specifications (VMS, Un*x)
  1817.  */
  1818.  
  1819. #ifdef VMS
  1820. /*
  1821.  * mk_path - extract the path component from VMS file spec
  1822.  */
  1823. char *mk_path(path, filespec)
  1824.     char *path;        /* output path */
  1825.     char *filespec;        /* input filespec */
  1826. {
  1827.     char *p;
  1828.  
  1829.     strcpy(path, filespec);
  1830.     if (!(p = strchr(path, ']')) && !(p = strchr(path, ':')))
  1831.         p = path - 1;    /* return null string if no path */
  1832.     *++p = '\0';
  1833.  
  1834.     return path;
  1835. }
  1836.  
  1837.  
  1838. /*
  1839.  * mk_filespec - merge VMS path and file names, where latter can be relative
  1840.  */
  1841.  
  1842. char *mk_filespec(filespec, path, name)
  1843.     char *filespec;        /* output filespec */
  1844.     char *path;        /* input path */
  1845.     char *name;        /* input file name */
  1846. {
  1847.     char *p;
  1848.  
  1849.     *filespec = '\0';
  1850.  
  1851.     /* copy name intact if absolute; else merge path and relative name */
  1852.     if (!strchr(name, ':')) {
  1853.         strcpy(filespec, path);
  1854.         if ((p = LASTCHAR(filespec)) && *p == END_PATH &&
  1855.             name[0] == START_PATH && strchr(".-", name[1]))
  1856.             *p = *++name == '-' ? '.' : '\0';
  1857.     }
  1858.  
  1859.     return strcat(filespec, name);
  1860. }
  1861.  
  1862.  
  1863. /*
  1864.  * trnlog - return translation of VMS logical name (null if missing)
  1865.  */
  1866. char *trnlog(logname)    /* look up logical name */
  1867.     char *logname;
  1868. {
  1869. static char trnbuf[STRSIZ];
  1870.  
  1871. $DESCRIPTOR(src, logname);
  1872. $DESCRIPTOR(dst, trnbuf);
  1873. short len;
  1874. int ret;
  1875.  
  1876. src.dsc$w_length  = strlen(logname);
  1877. ret = LIB$SYS_TRNLOG(&src, &len, &dst);
  1878. return ret == SS$_NORMAL ? (trnbuf[len] = '\0', trnbuf) : NULL;
  1879. }
  1880.  
  1881. #else
  1882.  
  1883. /*
  1884.  * mk_path - extract the path component from a Un*x file spec
  1885.  */
  1886. char *mk_path(path, filespec)
  1887.     char *path;        /* output path */
  1888.     char *filespec;        /* input filespec */
  1889. {
  1890.     char *p;
  1891.  
  1892.     strcpy(path, filespec);
  1893.     if (! (p = strrchr(path, END_PATH)) )
  1894.         p = path - 1;    /* return null string if no path */
  1895.  
  1896.     *++p = '\0';
  1897.     return path;
  1898. }
  1899.  
  1900.  
  1901. /*
  1902.  * mk_filespec - merge Un*x path and file names, where latter can be relative
  1903.  */
  1904.  
  1905. char *mk_filespec(filespec, path, name)
  1906.     char *filespec;        /* output filespec */
  1907.     char *path;        /* input path */
  1908.     char *name;        /* input file name */
  1909. {
  1910.     char *p;
  1911.  
  1912.     *filespec = '\0';
  1913.  
  1914.     /* copy name intact if absolute; else merge path and relative name */
  1915.  
  1916.     /* if path starts with "~/", translate it for user */
  1917.     if (strncmp(name, "~/", 2) == 0 && (p = trnlog(HOME_DIR)) != NULL) {
  1918.         strcpy(filespec, p);
  1919.         if ((p = LASTCHAR(filespec)) && *p != END_PATH)
  1920.             *++p = END_PATH, *++p = '\0';
  1921.         name += 2;        /* skip "~/" */
  1922.     }
  1923.     else if (*name != START_PATH) {        /* relative path */
  1924.         strcpy(filespec, path);
  1925.         if ((p = LASTCHAR(filespec)) && *p != END_PATH)
  1926.             *++p = END_PATH, *++p = '\0';
  1927.     }
  1928.  
  1929.     return strcat(filespec, name);
  1930. }
  1931.  
  1932.  
  1933. /*
  1934.  * trnlog - return translation of Un*x environment variable
  1935.  */
  1936. char *trnlog(logname)    /* look up logical name */
  1937.     char *logname;
  1938. {
  1939. return getenv(logname);
  1940. }
  1941.  
  1942. #endif
  1943.