home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume17 / remind / part03 / init.c < prev    next >
C/C++ Source or Header  |  1991-02-19  |  6KB  |  238 lines

  1. #include <stdio.h>
  2. #ifndef UNIX
  3. #include <stdlib.h>
  4. #endif
  5. #include <string.h>
  6. #include "defines.h"
  7. #include "globals.h"
  8. #include "protos.h"
  9.  
  10. #define PATCHLEVEL 0
  11.  
  12. static char DPMsg[] = "Debug and Purge options conflict - Purge chosen.\n";
  13. static char DPCMsg[] = "Calendar overrides Debug and Purge options.\n";
  14. static char NMsg[] = "Next overrides Calendar, Debug and Purge options.\n";
  15.  
  16. /***************************************************************/
  17. /*                                                             */
  18. /*  void initialize(int argc, char *argv[])                    */
  19. /*                                                             */
  20. /*  Reads command line options, sets appropriate flags         */
  21. /*  and FileName.  Also exits with error if invoked            */
  22. /*  incorrectly.                                               */
  23. /*                                                             */
  24. /***************************************************************/
  25. #ifdef __STDC__
  26. void initialize(int argc, char *argv[])
  27. #else
  28. void initialize(argc, argv)
  29.      int argc;
  30.      char *argv[];
  31. #endif
  32. {
  33.    int i;
  34.    char *s;
  35.    int d, m, y, t;
  36.    Token tok;
  37.  
  38.    Debug    = 0;
  39.    Purge    = 0;
  40.    Verbose  = 0;
  41.    IgOnce   = 0;
  42.    IgRun    = 0;
  43.    Calendar = 0;
  44.    Next     = 0;
  45.    PrintAts = 1;
  46.    QueueAts = 1;
  47.    CalWidth = 10;
  48.    SimpleCalendar = 0;
  49.  
  50.    if(argc == 1) {
  51.      fprintf(stderr, "\nREMIND 2.3 Patch Level %d (C) 1990, 1991 by David Skoll.\n\n", PATCHLEVEL);
  52. #ifdef UNIX
  53.      fprintf(stderr, "Usage: remind [-n | -d | -p | -c# [-w# | -s]] [-voraq] filename [date]\n\n");
  54. #else
  55.      fprintf(stderr, "Usage: remind [-n | -d | -p | -c# [-w# | -s]] [-vor] filename [date]\n\n");
  56. #endif
  57.      fprintf(stderr, "-n   Output next occurrence of reminders in simple format\n");
  58.      fprintf(stderr, "-d   Debug reminder file\n-p   Purge reminder file\n");
  59.      fprintf(stderr, "-c#  Produce calendar for # months\n");
  60.      fprintf(stderr, "-w#  Make calendar # columns wide\n");
  61.      fprintf(stderr, "-s   Produce simple calendar listing (used with -c)\n");
  62.      fprintf(stderr, "-v   Verbose messages\n-o   Ignore ONCE directives\n");
  63.      fprintf(stderr, "-r   Ignore RUN directives\n");
  64. #ifdef UNIX
  65.      fprintf(stderr, "-a   Do not trigger current AT reminders in foreground\n");
  66.      fprintf(stderr, "-q   Do not queue current AT reminders\n\n");
  67. #endif
  68.      exit(1);
  69.    }
  70.  
  71.    i = 1;
  72.    s = argv[i];
  73.  
  74.    /* Process options */
  75.    while (*s == '-') {
  76.       while (*++s) {
  77.          switch(upper(*s)) {
  78.  
  79.         case 'N': Next = 1;
  80.                  if (Calendar || Debug || Purge) {
  81.                 Calendar = Debug = Purge = 0;
  82.             fprintf(stderr, NMsg);
  83.                  }
  84.              break;
  85.  
  86.             case 'P':  Purge = 1;
  87.              if (Next) {
  88.                 Calendar = Debug = Purge = 0;
  89.             fprintf(stderr, NMsg);
  90.              }
  91.              if (Calendar) {
  92.                 Debug = Purge = 0;
  93.             fprintf(stderr, DPCMsg);
  94.                  }
  95.                      if (Debug) {
  96.                         Debug = 0;
  97.                         fprintf(stderr, DPMsg);
  98.                      }
  99.              break;
  100.  
  101.             case 'D': Debug = 1;
  102.              if (Next) {
  103.                 Calendar = Debug = Purge = 0;
  104.             fprintf(stderr, NMsg);
  105.              }
  106.              if (Calendar) {
  107.                 Debug = Purge = 0;
  108.             fprintf(stderr, DPCMsg);
  109.                  }
  110.                   if (Purge) {
  111.                  Debug = 0;
  112.              fprintf(stderr, DPMsg);
  113.               }
  114.               break;
  115.  
  116.             case 'C': Calendar = 1;
  117.              if (Next) {
  118.                 Calendar = Debug = Purge = 0;
  119.             fprintf(stderr, NMsg);
  120.              }
  121.              if (Debug || Purge) {
  122.                 Debug = Purge = 0;
  123.             fprintf(stderr, DPCMsg);
  124.                  }
  125.                      t = atoi(s + 1);
  126.              if (t > 0 && t <= 12) Calendar = t;
  127.                      /* Skip remaining chars on this option */
  128.                  while (*++s) ;
  129.                  s--;
  130.              break;
  131.  
  132.         case 'W': CalWidth = (atoi(s+1)-9)/7;
  133.                  if (CalWidth < 10) CalWidth = 10;
  134.              if (CalWidth > 40) CalWidth = 40;
  135.                  while (*++s) ;
  136.                  s--;
  137.              break;
  138.  
  139.             case 'S': SimpleCalendar = 1; break;
  140.  
  141.         case 'V': Verbose = 1; break;
  142.  
  143.         case 'O': IgOnce = 1; break;
  144.  
  145.         case 'R': IgRun = 1; break;
  146. #ifdef UNIX
  147.         case 'A': PrintAts = 0; break;
  148.  
  149.             case 'Q': QueueAts = 0; break;          
  150. #endif        
  151.         default: fprintf(stderr, "Unknown option '%c' ignored.\n", *s);
  152.      }                  
  153.       }
  154.       i++;
  155.       if (i >= argc) {
  156.     fprintf(stderr, "Missing filename - type 'remind' for usage information.\n");
  157.      exit(1);
  158.       }
  159.       s = argv[i];
  160.    }     
  161.    
  162.    /* Set FileName */
  163.    strcpy(FileName, argv[i++]);
  164.    
  165.    /* Get date, if supplied */
  166.    if (i < argc) {
  167.       *WorkBuf = 0;
  168.       while (i < argc) {
  169.          strcat(WorkBuf, argv[i++]);
  170.      strcat(WorkBuf, " ");
  171.       }
  172.       /* Parse the date */
  173.       d = m = y = -1;
  174.       tok.type = Unknown_t;
  175.       s = WorkBuf;
  176.       while (tok.type != Eol_t) {
  177.          tok = ParseToken(&s);
  178.      switch(tok.type) {
  179.  
  180.         case Eol_t: break;
  181.  
  182.         case Year_t: if (y == -1) 
  183.                         y = tok.val;
  184.              else {
  185.                 fprintf(stderr, "Year specified twice!\n");
  186.                 exit(1);
  187.              }
  188.              break;
  189.  
  190.         case Month_t: if (m == -1) 
  191.                         m = tok.val;
  192.              else {
  193.                 fprintf(stderr, "Month specified twice!\n");
  194.                 exit(1);
  195.              }
  196.              break;
  197.              
  198.         case Day_t: if (d == -1) 
  199.                         d = tok.val;
  200.              else {
  201.                 fprintf(stderr, "Day specified twice!\n");
  202.                 exit(1);
  203.              }
  204.              break;
  205.        
  206.             default: fprintf(stderr, "Illegal token %s on command line.\n", tok.str);
  207.                  exit(1);
  208.    
  209.          }
  210.       } 
  211.       
  212.       if (d == -1 || m == -1 || y == -1) {
  213.          fprintf(stderr, "Date on command line must be fully specified.\n");
  214.      exit(1);
  215.       }
  216.       if (CheckDate(d, m, y)) {
  217.          fprintf(stderr, "Illegal date on command line.\n");
  218.      exit(1);
  219.       }
  220.  
  221.       CurDay = d;
  222.       CurMon = m;
  223.       CurYear = y;
  224.       JulianToday = Julian(d, m, y);
  225.    }
  226.    OpenFile(FileName);
  227.    if (Debug) {
  228.       FromJulian(LastRun, &d, &m, &y);
  229. #ifndef UNIX
  230.       fprintf(stderr, "\nFile %s last modified on %s, %d %s, %d\n", FileName,
  231. #else
  232.       fprintf(stderr, "\nFile %s last accessed on %s, %d %s, %d\n", FileName,
  233. #endif
  234.                        DayName[LastRun % 7], d, MonthName[m], y);
  235.    }
  236.    return;
  237. }
  238.