home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 2 / crawlyvol2.bin / program / c / bts314b4 / evtparse.c < prev    next >
C/C++ Source or Header  |  1994-01-08  |  16KB  |  539 lines

  1. /*--------------------------------------------------------------------------*/
  2. /*                                                                          */
  3. /*                                                                          */
  4. /*      ------------         Bit-Bucket Software, Co.                       */
  5. /*      \ 10001101 /         Writers and Distributors of                    */
  6. /*       \ 011110 /          Freely Available<tm> Software.                 */
  7. /*        \ 1011 /                                                          */
  8. /*         ------                                                           */
  9. /*                                                                          */
  10. /*  (C) Copyright 1987-90, Bit Bucket Software Co., a Delaware Corporation. */
  11. /*                                                                          */
  12. /*                                                                          */
  13. /*                 This module was written by Bob Hartman                   */
  14. /*                                                                          */
  15. /*                                                                          */
  16. /*                     BinkleyTerm Scheduler Routines                       */
  17. /*                                                                          */
  18. /*                                                                          */
  19. /*    For complete  details  of the licensing restrictions, please refer    */
  20. /*    to the License  agreement,  which  is published in its entirety in    */
  21. /*    the MAKEFILE and BT.C, and also contained in the file LICENSE.240.    */
  22. /*                                                                          */
  23. /*    USE  OF THIS FILE IS SUBJECT TO THE  RESTRICTIONS CONTAINED IN THE    */
  24. /*    BINKLEYTERM  LICENSING  AGREEMENT.  IF YOU DO NOT FIND THE TEXT OF    */
  25. /*    THIS  AGREEMENT IN ANY OF THE  AFOREMENTIONED FILES,  OR IF YOU DO    */
  26. /*    NOT HAVE THESE FILES,  YOU  SHOULD  IMMEDIATELY CONTACT BIT BUCKET    */
  27. /*    SOFTWARE CO.  AT ONE OF THE  ADDRESSES  LISTED BELOW.  IN NO EVENT    */
  28. /*    SHOULD YOU  PROCEED TO USE THIS FILE  WITHOUT HAVING  ACCEPTED THE    */
  29. /*    TERMS  OF  THE  BINKLEYTERM  LICENSING  AGREEMENT,  OR  SUCH OTHER    */
  30. /*    AGREEMENT AS YOU ARE ABLE TO REACH WITH BIT BUCKET SOFTWARE, CO.      */
  31. /*                                                                          */
  32. /*                                                                          */
  33. /* You can contact Bit Bucket Software Co. at any one of the following      */
  34. /* addresses:                                                               */
  35. /*                                                                          */
  36. /* Bit Bucket Software Co.        FidoNet  1:104/501, 1:132/491, 1:141/491  */
  37. /* P.O. Box 460398                AlterNet 7:491/0                          */
  38. /* Aurora, CO 80046               BBS-Net  86:2030/1                        */
  39. /*                                Internet f491.n132.z1.fidonet.org         */
  40. /*                                                                          */
  41. /* Please feel free to contact us at any time to share your comments about  */
  42. /* our software and/or licensing policies.                                  */
  43. /*                                                                          */
  44. /*--------------------------------------------------------------------------*/
  45.  
  46. #include <stdio.h>
  47. #include <ctype.h>
  48. #ifdef __TOS__
  49. #include <ext.h>
  50. #else
  51. #include <sys/types.h>
  52. #include <sys/stat.h>
  53. #endif
  54. #include <string.h>
  55. #include <stdlib.h>
  56. #ifndef LATTICE
  57. #include <process.h>
  58. #endif
  59. #include <time.h>
  60.  
  61. #ifdef __TURBOC__
  62. #include "tc_utime.h"
  63. #ifndef __TOS__
  64. #include <alloc.h>
  65. #include <mem.h>
  66. #endif
  67. #else
  68. #ifndef LATTICE
  69. #include <sys/utime.h>
  70. #include <malloc.h>
  71. #include <memory.h>
  72. #endif
  73. #endif
  74.  
  75. #include "bink.h"
  76. #include "msgs.h"
  77. #include "sched.h"
  78.  
  79. static char *start_time (BTEVENT *, char *);
  80. static char *end_time (BTEVENT *, char *);
  81.  
  82.  
  83. static char *start_time (e, p)
  84. BTEVENT *e;
  85. char *p;
  86. {
  87.    int i, j, k, l, n;
  88.  
  89.    if ((n = sscanf (p, "%d:%d,%d,%d", &i, &j, &k, &l)) < 2)
  90.       {
  91.       return NULL;
  92.       }
  93.  
  94.    e->minute = i * 60 + j;
  95.    if ((e->minute < 0) || (e->minute > (24 * 60)))
  96.       {
  97.       return (NULL);
  98.       }
  99.  
  100.    if (n >= 3)
  101.       e->month = (char)k;
  102.    if (n >= 4)
  103.       e->day = (char)l;
  104.  
  105.    p = skip_to_blank (p);
  106.  
  107.    return (p);
  108. }
  109.  
  110. static char *end_time (e, p)
  111. BTEVENT *e;
  112. char *p;
  113. {
  114.    int i, j, k;
  115.  
  116.    if (sscanf (p, "%d:%d", &i, &j) != 2)
  117.       {
  118.       return NULL;
  119.       }
  120.  
  121.    k = i * 60 + j;
  122.    if ((k > (24 * 60)) || (k < 0))
  123.       {
  124.       return (NULL);
  125.       }
  126.  
  127.    if (k < e->minute)
  128.       {
  129.       (void) printf (msgtxt[M_NO_END_MIDNIGHT]);
  130.       return (NULL);
  131.       }
  132.  
  133.    e->length = k - e->minute;
  134.  
  135.    p = skip_to_blank (p);
  136.  
  137.    return (p);
  138. }
  139.  
  140. int parse_event( char *e_line )
  141. {
  142.    int i, j, j1, j2;
  143.    char *p, *p1, call_slots[100];
  144.    BTEVENT *e;
  145.  
  146.    /* If we already have a schedule, then forget it */
  147.    if (got_sched)
  148.       return (0);
  149.  
  150.    /* Skip blanks to get to the days field */
  151.    p = skip_blanks (e_line);
  152.  
  153.    /* Parse the days field */
  154.    e = (BTEVENT *) calloc (sizeof (BTEVENT), 1);
  155.    memset (e->call_slot, 0, 33);
  156.    e->days = 0;
  157.    e->wait_time = 120;
  158.    while ((*p) && (!isspace (*p)))
  159.       {
  160.       switch (toupper (*p))
  161.          {
  162.          case 'S':                              /* Sunday or Saturday */
  163.             if (!strnicmp (p, "sun", 3))
  164.                {
  165.                e->days |= DAY_SUNDAY;
  166.                }
  167.             else if (!strnicmp (p, "sat", 3))
  168.                {
  169.                e->days |= DAY_SATURDAY;
  170.                }
  171.             else /* Error condition */ 
  172.                {
  173.                goto err;
  174.                }
  175.             p += 3;
  176.             break;
  177.  
  178.          case 'M':                              /* Monday */
  179.             if (!strnicmp (p, "mon", 3))
  180.                {
  181.                e->days |= DAY_MONDAY;
  182.                }
  183.             else /* Error condition */ 
  184.                {
  185.                goto err;
  186.                }
  187.             p += 3;
  188.             break;
  189.  
  190.          case 'T':                              /* Tuesday or Thursday */
  191.             if (!strnicmp (p, "tue", 3))
  192.                {
  193.                e->days |= DAY_TUESDAY;
  194.                }
  195.             else if (!strnicmp (p, "thu", 3))
  196.                {
  197.                e->days |= DAY_THURSDAY;
  198.                }
  199.             else /* Error condition */ 
  200.                {
  201.                goto err;
  202.                }
  203.             p += 3;
  204.             break;
  205.  
  206.          case 'W':                              /* Wednesday, Week or
  207.                                                   * Weekend */
  208.             if (!strnicmp (p, "wed", 3))
  209.                {
  210.                e->days |= DAY_WEDNESDAY;
  211.                p += 3;
  212.                }
  213.             else if (!strnicmp (p, "week", 4))
  214.                {
  215.                e->days |= DAY_WEEK;
  216.                p += 4;
  217.                }
  218.             else if (!strnicmp (p, "wkend", 5))
  219.                {
  220.                e->days |= DAY_WKEND;
  221.                p += 5;
  222.                }
  223.             else /* Error condition */ 
  224.                {
  225.                goto err;
  226.                }
  227.             break;
  228.  
  229.          case 'F':                              /* Friday */
  230.             if (!strnicmp (p, "fri", 3))
  231.                {
  232.                e->days |= DAY_FRIDAY;
  233.                }
  234.             else /* Error condition */ 
  235.                {
  236.                goto err;
  237.                }
  238.             p += 3;
  239.             break;
  240.  
  241.          case 'A':                              /* All */
  242.             if (!strnicmp (p, "all", 3))
  243.                {
  244.                e->days |= (DAY_WEEK | DAY_WKEND);
  245.                }
  246.             else /* Error condition */ 
  247.                {
  248.                goto err;
  249.                }
  250.             p += 3;
  251.             break;
  252.  
  253.          default:                               /* Error condition */
  254.             goto err;
  255.          }
  256.  
  257.       if (*p == '|')
  258.          ++p;
  259.       }
  260.  
  261.    /* Did we get something valid? */
  262.    if (e->days == 0)
  263.       {
  264.       goto err;
  265.       }
  266.  
  267.    /* Skip blanks to get to the start-time field */
  268.    p = skip_blanks (p);
  269.  
  270.    /* Parse the start-time field */
  271.    if ((p = start_time (e, p)) == NULL)
  272.       {
  273.       (void) printf (msgtxt[M_INVALID_START], e_line);
  274.       free (e);
  275.       return (1);
  276.       }
  277.  
  278.    /* Give each event a default of 60 minutes */
  279.    e->length = 60;
  280.  
  281.    /* Give each event a local cost of 0 */
  282.    e->node_cost = 0;
  283.  
  284.    /* Give each event a default of T=3,10000 */
  285.    e->with_connect = 3;
  286.    e->no_connect = 10000;
  287.  
  288.    /* While there are still things on the line */
  289.    while (*p)
  290.       {
  291.       /* Skip blanks to get to the next field */
  292.       p = skip_blanks (p);
  293.  
  294.       /* switch to find what thing is being parsed */
  295.       switch (tolower (*p))
  296.          {
  297.          case '\0':                             /* No more stuff */
  298.             break;
  299.  
  300.          case '0':                              /* Digits must be an ending
  301.                                                   * time */
  302.          case '1':
  303.          case '2':
  304.          case '3':
  305.          case '4':
  306.          case '5':
  307.          case '6':
  308.          case '7':
  309.          case '8':
  310.          case '9':
  311.             /* Parse ending time */
  312.             if ((p = end_time (e, p)) == NULL)
  313.                {
  314.                (void) printf (msgtxt[M_INVALID_END], e_line);
  315.                free (e);
  316.                return (1);
  317.                }
  318.             break;
  319.  
  320.          case ';':                              /* Comment */
  321.          case '%':
  322.             *p = '\0';
  323.             break;
  324.  
  325.          case '"':                              /* Extra chars to append to
  326.                                                   * packer strings */
  327.             ++p;
  328.             p1 = e->cmd;
  329.             *p1++ = ' ';
  330.             while (*p != '"')
  331.                *p1++ = *p++;
  332.             *p1 = '\0';
  333.             ++p;
  334.             break;
  335.  
  336.          case 'a':                              /* Average wait */
  337.             ++p;
  338.             if (*p == '=')
  339.                {
  340.                ++p;
  341.                if (isdigit (*p))
  342.                   {
  343.                   i = atoi (p);
  344.                   if ((i > 1800) || (i < 0))
  345.                      {
  346.                      (void) printf (msgtxt[M_INVALID_AVGWAIT], e_line);
  347.                      free (e);
  348.                      return (1);
  349.                      }
  350.                   e->wait_time = i;
  351.                   p = skip_to_blank (p);
  352.                   break;
  353.                   }
  354.                }
  355.             (void) printf (msgtxt[M_INVALID_AVGWAIT], e_line);
  356.             free (e);
  357.             return (1);
  358.  
  359.          case 'b':                              /* BBS type event */
  360.             p = skip_to_blank (p);
  361.             e->behavior |= MAT_BBS;
  362.             break;
  363.  
  364.          case 'c':                              /* #CM event */
  365.             p = skip_to_blank (p);
  366.             e->behavior |= MAT_CM;
  367.             break;
  368.  
  369.          case 'd':                              /* Dynamic event */
  370.             p = skip_to_blank (p);
  371.             e->behavior |= MAT_DYNAM;
  372.             break;
  373.  
  374.          case 'e':                              /* An errorlevel exit */
  375.             ++p;
  376.             if (isdigit (*p))
  377.                {
  378.                i = *p - '0';
  379.                ++p;
  380.                if (*p == '=')
  381.                   {
  382.                   if ((i <= 3) && (i > 0))
  383.                      {
  384.                      ++p;
  385.                      if (isdigit (*p))
  386.                         {
  387.                         j = atoi (p);
  388.                         e->errlevel[i - 1] = j;
  389.                         p = skip_to_blank (p);
  390.                         break;
  391.                         }
  392.                      }
  393.                   else if ((i > 3) && (i <= 9))
  394.                      {
  395.                      ++p;
  396.                      if (isdigit (*p))
  397.                         {
  398.                         j = atoi (p);
  399.                         e->errlevel[i - 1] = j;
  400.                         while (*p && (*p != ','))
  401.                            ++p;
  402.                         ++p;
  403.                         (void) strncpy (&(e->err_extent[i - 4][0]), p, 3);
  404.                         p = skip_to_blank (p);
  405.                         break;
  406.                         }
  407.                      }
  408.                   }
  409.                }
  410.             (void) printf (msgtxt[M_BAD_ERRORLEVEL], e_line);
  411.             free (e);
  412.             return (1);
  413.  
  414.          case 'f':                              /* Forced event */
  415.             p = skip_to_blank (p);
  416.             e->behavior |= MAT_FORCED;
  417.             break;
  418.  
  419.          case 'k':                              /* no #CM event */
  420.             p = skip_to_blank (p);
  421.             e->behavior |= MAT_NOCM;
  422.             break;
  423.  
  424.          case 'l':                              /* Local only mail */
  425.             ++p;
  426.             e->node_cost = 0;
  427.             if (*p == '=')
  428.                {
  429.                ++p;
  430.                if (isdigit (*p))
  431.                   {
  432.                   e->node_cost = atoi (p);
  433.                   }
  434.                }
  435.             else if (*p == '>')
  436.                {
  437.                ++p;
  438.                if (isdigit (*p))
  439.                   {
  440.                   e->node_cost = -atoi (p) - 1;
  441.                   }
  442.                }
  443.             else if (*p == '<')
  444.                {
  445.                ++p;
  446.                if (isdigit (*p))
  447.                   {
  448.                   e->node_cost = atoi (p) - 1;
  449.                   }
  450.                }
  451.             p = skip_to_blank (p);
  452.             e->behavior |= MAT_LOCAL;
  453.             break;
  454.  
  455.          case 'm':                              /* Mailable 24 hours */
  456.             p = skip_to_blank (p);
  457.             e->behavior |= MAT_NOMAIL24;
  458.             break;
  459.  
  460.          case 'n':                              /* No requests */
  461.             p = skip_to_blank (p);
  462.             e->behavior |= MAT_NOREQ;
  463.             break;
  464.  
  465.          case 'r':                              /* Receive only */
  466.             p = skip_to_blank (p);
  467.             e->behavior |= MAT_NOOUT;
  468.             break;
  469.  
  470.          case 's':                              /* Send only */
  471.             p = skip_to_blank (p);
  472.             e->behavior |= MAT_OUTONLY;
  473.             break;
  474.  
  475.          case 't':                              /* Tries */
  476.             ++p;
  477.             if (sscanf (p, "=%d,%d", &j1, &j2) != 2)
  478.                {
  479.                (void) printf (msgtxt[M_BAD_TRIES], e_line);
  480.                return (1);
  481.                }
  482.             else
  483.                {
  484.                if ((j1 > 8) || (j1 < 1))
  485.                   {
  486.                   (void) printf (msgtxt[M_BAD_TRIES], e_line);
  487.                   return (1);
  488.                   }
  489.                e->with_connect = j1;
  490.                e->no_connect = j2;
  491.                }
  492.             p = skip_to_blank (p);
  493.             break;
  494.  
  495.          case 'x':                              /* No outbound requests here */
  496.             p = skip_to_blank (p);
  497.             e->behavior |= MAT_NOOUTREQ;
  498.             break;
  499.  
  500. #ifdef NEW
  501.          case '$':
  502.             p = skip_to_blank(p);
  503.             e->behavior |= MAT_KILLBAD;
  504.             break;
  505.             
  506.          case 'p':
  507.             ++p;
  508.             if (sscanf (p, "=%s", call_slots) != 1)
  509.             {
  510.                 (void) printf (msgtxt[M_BAD_POLLSLOT], e_line);
  511.                 return (1);
  512.             }
  513.             else
  514.             {
  515.                  strncpy (e->call_slot, call_slots, 32);
  516.             }
  517.             p = skip_to_blank (p);
  518.             break;
  519. #endif
  520.  
  521.          default:                               /* Error condition */
  522.             (void) printf (msgtxt[M_INDECIPHERABLE], e_line);
  523.             free (e);
  524.             return (1);
  525.          }
  526.       }
  527.  
  528.    /* Save it in the array  of pointers */
  529.    e_ptrs[num_events++] = e;
  530.  
  531.    /* Return that everything is cool */
  532.    return (0);
  533.  
  534. err:
  535.    (void) printf (msgtxt[M_BAD_DAY], e_line);
  536.    free (e);
  537.    return (1);
  538. }
  539.