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

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