home *** CD-ROM | disk | FTP | other *** search
/ Super Net 1 / SUPERNET_1.iso / PC / OTROS / EXTRAS / UUCODE / UUPC / TEST / UPC12ES2.ZIP / UUCICO / checktim.c next >
Encoding:
C/C++ Source or Header  |  1993-10-12  |  13.9 KB  |  340 lines

  1. /*--------------------------------------------------------------------*/
  2. /*    c h e c k t i m . c                                             */
  3. /*                                                                    */
  4. /*    Time of day validation routine for UUPC/extended                */
  5. /*--------------------------------------------------------------------*/
  6.  
  7. /*--------------------------------------------------------------------*/
  8. /*       Changes Copyright (c) 1989-1993 by Kendra Electronic         */
  9. /*       Wonderworks.                                                 */
  10. /*                                                                    */
  11. /*       All rights reserved except those explicitly granted by       */
  12. /*       the UUPC/extended license agreement.                         */
  13. /*--------------------------------------------------------------------*/
  14.  
  15. /*--------------------------------------------------------------------*/
  16. /*                          RCS Information                           */
  17. /*--------------------------------------------------------------------*/
  18.  
  19. /*
  20.  *    $Id: checktim.c 1.3 1993/10/12 01:32:46 ahd Exp $
  21.  *
  22.  *    Revision history:
  23.  *    $Log: checktim.c $
  24.  * Revision 1.3  1993/10/12  01:32:46  ahd
  25.  * Normalize comments to PL/I style
  26.  *
  27.  * Revision 1.2  1993/09/20  04:46:34  ahd
  28.  * OS/2 2.x support (BC++ 1.0 support)
  29.  * TCP/IP support from Dave Watt
  30.  * 't' protocol support
  31.  *
  32.  */
  33.  
  34. /*--------------------------------------------------------------------*/
  35. /*                        System include files                        */
  36. /*--------------------------------------------------------------------*/
  37.  
  38. #include <stdio.h>
  39. #include <string.h>
  40. #include <stdlib.h>
  41. #include <time.h>
  42.  
  43. /*--------------------------------------------------------------------*/
  44. /*                    UUPC/extended include files                     */
  45. /*--------------------------------------------------------------------*/
  46.  
  47. #include "lib.h"
  48. #include "checktim.h"
  49.  
  50. /*--------------------------------------------------------------------*/
  51. /*                          Global variables                          */
  52. /*--------------------------------------------------------------------*/
  53.  
  54. currentfile();
  55.  
  56. /*--------------------------------------------------------------------*/
  57. /*                          Local functions                           */
  58. /*--------------------------------------------------------------------*/
  59.  
  60. static char checkone( char *input, size_t hhmm, int weekday );
  61.  
  62. /*--------------------------------------------------------------------*/
  63. /*   The following day values are based on the fact the               */
  64. /*   localtime() call returns the day of the week as a value zero     */
  65. /*   (0) through six (6), which is converted into the bit number      */
  66. /*   and then AND'ed against the date mask.                           */
  67. /*--------------------------------------------------------------------*/
  68.  
  69. #define SUN 0x80
  70. #define MON 0x40
  71. #define TUE 0x20
  72. #define WED 0x10
  73. #define THU 0x08
  74. #define FRI 0x04
  75. #define SAT 0x02
  76. #define NEVER 0x00
  77. #define WEEKEND (SAT | SUN)
  78. #define WEEKDAY (MON | TUE | WED | THU | FRI)
  79. #define ANY (WEEKEND | WEEKDAY)
  80.  
  81. /*--------------------------------------------------------------------*/
  82. /*   Table of values for schedules.  Based on values given for        */
  83. /*   legal schedule keywords in "Managing uucp and Usenet" by         */
  84. /*   O'Reilly & Associates.  Multiple entries for a single keyword    */
  85. /*   are processed in logical OR fashion, just as multiple entries    */
  86. /*   for the same host in L.sys are treated in a logical OR           */
  87. /*   fashion.                                                         */
  88. /*                                                                    */
  89. /*   Timing windows are adjusted five minutes away from higher        */
  90. /*   telephone rates in an attempt to avoid massive charges           */
  91. /*   because of inaccurate PC clocks and the fact that a telephone    */
  92. /*   call generally requires more than one minute if the system is    */
  93. /*   trying to do useful work.                                        */
  94. /*                                                                    */
  95. /*   Does not support multiple keywords in one token                  */
  96. /*   (TuFr0800-0805), but allows multiple tokens                      */
  97. /*   (Tu0800-805,Fr0800-0805) on one line.                            */
  98. /*                                                                    */
  99. /*   The NonPeak keyword has been corrected to the current (May       */
  100. /*   1989) NonPeak hours for Telenet's PC-Pursuit.  However, keep     */
  101. /*   in mind the PC-Pursuit customer agreement specifies that you     */
  102. /*   can't use PC-Pursuit to network multiple PC's, so thou shalt     */
  103. /*   not use PC-Pursuit from a central mail server.  *sigh*           */
  104. /*                                                                    */
  105. /*   I also have Reach-Out America from ATT, for which night rates    */
  106. /*   begin at 10:00 pm.  It is duly added to the table.               */
  107. /*--------------------------------------------------------------------*/
  108.  
  109. static struct Table {
  110.    char *keyword;
  111.    int wdays;
  112.    unsigned int start;
  113.    unsigned int end;
  114.  
  115. } table[] = {
  116.    { "Any",     ANY,         0, 2400},
  117.    { "Wk",      WEEKDAY,     0, 2400},
  118.    { "Su",      SUN,         0, 2400},
  119.    { "Mo",      MON,         0, 2400},
  120.    { "Tu",      TUE,         0, 2400},
  121.    { "We",      WED,         0, 2400},
  122.    { "Th",      THU,         0, 2400},
  123.    { "Fr",      FRI,         0, 2400},
  124.    { "Sa",      SAT,         0, 2400},
  125.    { "Evening", WEEKDAY,  1705,  755},
  126.    { "Evening", WEEKEND,     0, 2400},
  127.    { "Night",   WEEKDAY,  2305,  755},
  128.    { "Night",   SAT,         0, 2400},
  129.    { "Night",   SUN,      2305, 1655},
  130.    { "NonPeak", WEEKDAY,  1805,  655}, /* Subject to change at TELENET's whim */
  131.    { "NonPeak", WEEKEND,     0, 2400},
  132.    { "ROA",     WEEKDAY,  2205,  755}, /* Reach Out America (sm) (AT&T) */
  133.    { "ROA",     SAT,         0, 2400}, /* Reach Out America (sm) (AT&T) */
  134.    { "ROA",     SUN,      2205, 1655}, /* Reach Out America (sm) (AT&T) */
  135.    { "Never",   NEVER,       0, 2400},
  136.    {  nil(char) }
  137. }; /* table */
  138.  
  139. /*--------------------------------------------------------------------*/
  140. /*    c h e c k t i m e                                               */
  141. /*                                                                    */
  142. /*    Validate a time of day field; returns lowest grade              */
  143. /*--------------------------------------------------------------------*/
  144.  
  145. char checktime(const char *xtime)
  146. {
  147.  
  148.    struct tm *tm_now;
  149.    time_t secs_now;
  150.    size_t hhmm;
  151.    char  buf[BUFSIZ];
  152.    char  *token;
  153.    char  *nexttoken;
  154.    char  bestgrade = '\0';    /* No grade found yet                  */
  155.    int   weekday;
  156.  
  157.    strcpy(buf,xtime);         /* Copy time to local buffer we can alter */
  158.    time(&secs_now);
  159.    tm_now = localtime(&secs_now);
  160.                                        /* Create structure with time   */
  161.    weekday = SUN >> tm_now->tm_wday;   /* Get day of week as single bit */
  162.    hhmm = tm_now->tm_hour*100 + tm_now->tm_min;
  163.    nexttoken = buf;           /* First pass, look at start of buffer   */
  164.  
  165.    while ((bestgrade < ALL_GRADES) &&
  166.           ((token = strtok(nexttoken,",")) != NULL))
  167.    {
  168.       char grade = checkone( token, hhmm, weekday);
  169.  
  170.       if ( bestgrade < grade )
  171.             bestgrade = grade;
  172.  
  173.       nexttoken = NULL;       /* Continue parsing same string          */
  174.  
  175.    } /* while (!(dial) && ((token = strtok(nexttoken,",")) != NULL) ) */
  176.  
  177. /*--------------------------------------------------------------------*/
  178. /*            Report our results and return to the caller             */
  179. /*--------------------------------------------------------------------*/
  180.  
  181.    return (bestgrade);
  182.  
  183. } /*checktime*/
  184.  
  185. /*--------------------------------------------------------------------*/
  186. /*    c h e c k o n e                                                 */
  187. /*                                                                    */
  188. /*    Process one time field for checktime                            */
  189. /*--------------------------------------------------------------------*/
  190.  
  191. static char checkone( char *input, size_t hhmm, int weekday )
  192. {
  193.    char  tdays[20];           /* String version of user tokens       */
  194.    char  tstart[20];
  195.    char  tend[20];
  196.    size_t istart;
  197.    size_t iend;
  198.    struct Table *tptr;
  199.    boolean dial = FALSE;      /* Assume we cannot dial               */
  200.    char  found = 0;           /* Did not yet find current keyword    */
  201.    char grade = ALL_GRADES;   /* Default grade if none specified     */
  202.  
  203.    char *slash = strchr( input, '/' );
  204.  
  205. /*--------------------------------------------------------------------*/
  206. /*     Parse a day/time combination from the L.SYS file         *     */
  207. /*--------------------------------------------------------------------*/
  208.  
  209.    strcpy(tstart,"0000");  /* Set default times to all day           */
  210.    strcpy(tend,"2400");
  211.  
  212. /*--------------------------------------------------------------------*/
  213. /*                      Determine the call grade                      */
  214. /*--------------------------------------------------------------------*/
  215.  
  216.    if ( slash != NULL )
  217.    {
  218.  
  219.       if (strlen( slash ) != 2)
  220.       {
  221.          printmsg(0,"Invalid call grade in field: %s", input );
  222.          panic();
  223.       }
  224.  
  225.       *slash++ = '\0';     /* Terminate original string              */
  226.       grade = *slash;      /* Save the grade the user wanted         */
  227.  
  228.    } /* if ( slash != NULL ) */
  229.  
  230. /*--------------------------------------------------------------------*/
  231. /*         Get the period and time limits the user specified          */
  232. /*--------------------------------------------------------------------*/
  233.  
  234.     sscanf(input,
  235.            "%[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz]"
  236.            "%[0123456789]-%[0123456789]", tdays, tstart, tend);
  237.  
  238. /*--------------------------------------------------------------------*/
  239. /*          Verify the lengths of the fields the user specified       */
  240. /*--------------------------------------------------------------------*/
  241.  
  242.    if (strlen(tstart) >= sizeof tstart)
  243.    {
  244.       printf( "%d character buffer overflow \"%s\"\n",
  245.                sizeof tstart , tstart );
  246.       panic();
  247.    }
  248.  
  249.    if (strlen(tend) >= sizeof tend)
  250.    {
  251.       printf( "%d character buffer overflow \"%s\"\n",
  252.                sizeof tend , tend );
  253.       panic();
  254.    }
  255.  
  256.    if (strlen(tdays) >= sizeof tdays)
  257.    {
  258.       printf( "%d character buffer overflow \"%s\"\n",
  259.                sizeof tdays, tdays );
  260.       panic();
  261.    }
  262.  
  263.    printmsg(8,"checkone: %s broken into \"%s\" from \"%s\" to \"%s\""
  264.               " with grade %c",
  265.             input,tdays,tstart,tend,grade );
  266.  
  267.    istart = atoi(tstart);  /* Convert start/end times to binary       */
  268.    iend  = atoi(tend);
  269.  
  270. /*--------------------------------------------------------------------*/
  271. /*    Handle case of midnight specified in such a way that the        */
  272. /*    time wraps through the daylight hours; we'll take the           */
  273. /*    conservative approach that the user really meant to start at    */
  274. /*    midnight.                                                       */
  275. /*--------------------------------------------------------------------*/
  276.  
  277.    if ((istart > iend) && (istart == 2400))
  278.       istart = 0000;
  279.  
  280. /*--------------------------------------------------------------------*/
  281. /*                  Search for the requested keyword                  */
  282. /*--------------------------------------------------------------------*/
  283.  
  284.    for (tptr = table, found = FALSE;
  285.          (tptr->keyword != nil(char)) && !dial; tptr++)
  286.    {
  287.  
  288. /*--------------------------------------------------------------------*/
  289. /*      We found the keyword, see if today qualifies for dialing      */
  290. /*--------------------------------------------------------------------*/
  291.  
  292.       if (equal(tptr->keyword,tdays))
  293.       {
  294.          found = TRUE;     /* Win or Lose, keyword is valid          */
  295.          if (weekday & tptr->wdays)    /* Can we dial out today?     */
  296.          {                             /* Yes --> Check the time     */
  297.  
  298. /*--------------------------------------------------------------------*/
  299. /*    This entry allows us to dial out today; now determine if the    */
  300. /*    time slot in the table allows dialing.                          */
  301. /*--------------------------------------------------------------------*/
  302.  
  303.             if (tptr->start > tptr->end)  /* span midnight?          */
  304.                dial = (tptr->start <= hhmm) || (tptr->end >= hhmm);
  305.             else
  306.                dial = (tptr->start <= hhmm) && (tptr->end >= hhmm);
  307.  
  308. /*--------------------------------------------------------------------*/
  309. /*    Now do a logical AND of that time with the time the user        */
  310. /*    specified in L.sys for this particular system.                  */
  311. /*--------------------------------------------------------------------*/
  312.  
  313.             if (istart > iend)            /* span midnight?          */
  314.                dial = ((istart <= hhmm) || (iend >= hhmm)) && dial;
  315.             else if (istart == iend)
  316.                dial = (istart == hhmm) && dial;
  317.             else
  318.                dial = (istart <= hhmm) && (iend >= hhmm) && dial;
  319.          } /* if */
  320.       } /* if */
  321.    } /* for */
  322.  
  323.    if (!found)
  324.       printmsg(0,"checkone: keyword \"%s\" not found",input);
  325.  
  326.    printmsg(3,"checkone: call window \"%s\" %s",
  327.          input,
  328.          dial ? "open" :"closed");
  329.  
  330. /*--------------------------------------------------------------------*/
  331. /*                Return success or failure to caller                 */
  332. /*--------------------------------------------------------------------*/
  333.  
  334.    if ( dial )
  335.       return grade;
  336.    else
  337.       return '\0';
  338.  
  339. } /* checkone */
  340.