home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 2 BBS / 02-BBS.zip / BTMTSRC3.ZIP / SCHED.C < prev    next >
C/C++ Source or Header  |  1991-08-13  |  17KB  |  534 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. #include <sys/types.h>
  49. #include <sys/stat.h>
  50. #include <string.h>
  51. #include <stdlib.h>
  52. #include <process.h>
  53. #include <time.h>
  54.  
  55. #ifdef __TURBOC__
  56. #include "tc_utime.h"
  57. #include <alloc.h>
  58. #include <mem.h>
  59. #else
  60. #include <sys/utime.h>
  61. #include <malloc.h>
  62. #include <memory.h>
  63. #endif
  64.  
  65. #include "com.h"
  66. #include "xfer.h"
  67. #include "zmodem.h"
  68. #include "keybd.h"
  69. #include "sbuf.h"
  70. #include "sched.h"
  71. #include "externs.h"
  72. #include "prototyp.h"
  73.  
  74.  
  75.  
  76. /**************************************************************************/
  77. /*** This MUST be exactly 16 total bytes including the terminating null ***/
  78. /*** or the routines read_sched() and write_sched() must be changed!!!! ***/
  79. /**************************************************************************/
  80. static char *BinkSched = "BinkSchedule 04";      /* Version of scheduler   */
  81.  
  82.  
  83. void find_event ()
  84. {
  85.    int cur_day;
  86.    int cur_hour;
  87.    int cur_minute;
  88.    int cur_mday;
  89.    int cur_mon;
  90.    int cur_year;
  91.    int junk;
  92.    int our_time;
  93.    int i;
  94.    char cmds[150];
  95.  
  96.    /* Get the current day of the week */
  97.    dosdate (&cur_mon, &cur_mday, &cur_year, &cur_day);
  98.  
  99.    cur_day = 1 << cur_day;
  100.  
  101.    /* Get the current time in minutes */
  102.    dostime (&cur_hour, &cur_minute, &junk, &junk);
  103.    our_time = (cur_hour % 24) * 60 + (cur_minute % 60);
  104.  
  105.    cur_event = -1;
  106.  
  107.    if (cur_mday != hist.which_day)
  108.       {
  109.       write_stats ();
  110.       (void) memset (&hist, 0, sizeof (HISTORY));
  111.       hist.which_day = cur_mday;
  112.       if (un_attended && fullscreen)
  113.          {
  114.          do_today ();
  115.          sb_show ();
  116.          }
  117.       }
  118.  
  119.    /* Go through the events from top to bottom */
  120.    for (i = 0; i < num_events; i++)
  121.       {
  122.       if (our_time >= e_ptrs[i]->minute)
  123.          {
  124.          if ((cur_day & e_ptrs[i]->days) &&
  125.      ((!e_ptrs[i]->day) || (e_ptrs[i]->day == (char)cur_mday)) &&
  126.      ((!e_ptrs[i]->month) || (e_ptrs[i]->month == (char)cur_mon)))
  127.             {
  128.             if (((our_time - e_ptrs[i]->minute) < e_ptrs[i]->length) ||
  129.             ((our_time == e_ptrs[i]->minute) && (e_ptrs[i]->length == 0)) ||
  130.                 ((e_ptrs[i]->behavior & MAT_FORCED) && (e_ptrs[i]->last_ran != cur_mday)))
  131.                {
  132.                /* Are we not supposed to force old events */
  133.                if (((our_time - e_ptrs[i]->minute) > e_ptrs[i]->length) && (noforce))
  134.                   {
  135.                   e_ptrs[i]->last_ran = cur_mday;
  136.                   continue;
  137.                   }
  138.  
  139.                if (e_ptrs[i]->last_ran != cur_mday)
  140.                   {
  141.                   cur_event = i;
  142.                   do_ready (msgtxt[M_READY_WAITING]);
  143.                   status_line (msgtxt[M_STARTING_EVENT], i + 1);
  144.  
  145.                   if (!blank_on_key)
  146.                      screen_blank = 0;
  147.  
  148.                   more_mail = 1;
  149.  
  150.                   /* Mark that this one is running */
  151.                   e_ptrs[i]->last_ran = cur_mday;
  152.  
  153.                   /*
  154.                    * Mark that we have not yet skipped it. After all, it just
  155.                    * started! 
  156.                    */
  157.                   e_ptrs[i]->behavior &= ~MAT_SKIP;
  158.  
  159.                   /* Write out the schedule */
  160.                   write_sched ();
  161.  
  162.                   /* If we are supposed to exit, then do it */
  163.                   if (e_ptrs[i]->errlevel[0])
  164.                      {
  165.                      status_line (msgtxt[M_EVENT_EXIT], e_ptrs[i]->errlevel[0]);
  166.  
  167.                      screen_blank = 0;
  168.                      errl_exit (e_ptrs[i]->errlevel[0]);
  169.                      }
  170.                   else if (packer != NULL)
  171.                      {
  172.                      if (!blank_on_key)
  173.                         screen_blank = 0;
  174.                      status_line (msgtxt[M_CLEAN_PACK]);
  175.                      mdm_init (modem_busy);
  176.                      exit_DTR ();
  177.                      screen_clear ();
  178.                      vfossil_cursor (1);
  179.                      if (cleanup != NULL)
  180.                         {
  181.                         (void) strcpy (cmds, cleanup);
  182.                         if (i >= 0)
  183.                            (void) strcat (cmds, e_ptrs[i]->cmd);
  184.                         b_spawn (cmds);
  185.                         }
  186.                      (void) strcpy (cmds, packer);
  187.                      if (i >= 0)
  188.                         (void) strcat (cmds, e_ptrs[i]->cmd);
  189.                      b_spawn (cmds);
  190.                     come_back();     /* CML */
  191.                      if (fullscreen)
  192.                         {
  193.                         screen_clear ();
  194.                         sb_dirty ();
  195.                         opening_banner ();
  196.                         mailer_banner ();
  197.                         }
  198.                      DTR_ON ();
  199.                      mdm_init (modem_init);
  200.                      status_line (msgtxt[M_AFTER_CLEAN_PACK]);
  201.                      }
  202.  
  203.                   cur_event = i;
  204.                   max_connects = e_ptrs[i]->with_connect;
  205.                   max_noconnects = e_ptrs[i]->no_connect;
  206.                   set_up_outbound ();
  207.                   }
  208.                else
  209.                   {
  210.                   /* Don't do events that have been exited already */
  211.                   if (e_ptrs[i]->behavior & MAT_SKIP)
  212.                      continue;
  213.                   }
  214.  
  215.                cur_event = i;
  216.  
  217.                if (e_ptrs[i]->behavior & MAT_NOREQ)
  218.                   {
  219.                   matrix_mask &= ~TAKE_REQ;
  220.                   no_requests = 1;
  221.                   }
  222.                else
  223.                   {
  224.                   matrix_mask |= TAKE_REQ;
  225.                   no_requests = 0;
  226.                   }
  227.  
  228.                if (e_ptrs[i]->behavior & MAT_NOOUTREQ)
  229.                   {
  230.                   requests_ok = 0;
  231.                   }
  232.                else
  233.                   {
  234.                   requests_ok = 1;
  235.                   }
  236.  
  237.                max_connects = e_ptrs[i]->with_connect;
  238.                max_noconnects = e_ptrs[i]->no_connect;
  239.  
  240.                break;
  241.                }
  242.             }
  243.          }
  244.       }
  245. }
  246.  
  247. void read_sched ()
  248. {
  249.    char temp1[80], temp2[80];
  250.    EVENT *sptr;
  251.    struct stat buffer1, buffer2;
  252.    FILE *f;
  253.    int i;
  254.  
  255.    (void) strcpy (temp1, BINKpath);
  256.    (void) strcpy (temp2, BINKpath);
  257.    (void) strcat (temp1, "Binkley.Scd");
  258.    (void) strcat (temp2, "Binkley.Evt");
  259.  
  260.    if (stat (temp1, &buffer1))
  261.       {
  262.       return;
  263.       }
  264.  
  265.    if (stat (temp2, &buffer2))
  266.       {
  267.       (void) strcpy (temp2, BINKpath);
  268.       (void) strcat (temp2, config_name);
  269.       if (stat (temp2, &buffer2))
  270.          {
  271.          return;
  272.          }
  273.       }
  274.  
  275.    if ((buffer1.st_atime < buffer2.st_atime) ||
  276.        (buffer1.st_size < sizeof (EVENT)))
  277.       {
  278.       return;
  279.       }
  280.  
  281.    if ((sptr = (EVENT *) malloc ((unsigned int) (buffer1.st_size - 16 - sizeof (HISTORY)))) == NULL)
  282.       {
  283.       return;
  284.       }
  285.  
  286.    if ((f = fopen (temp1, "rb")) == NULL)
  287.       {
  288.       return;
  289.       }
  290.  
  291.    temp1[0] = '\0';
  292.    (void) fread (temp1, 16, 1, f);
  293.    if (strcmp (temp1, BinkSched) != 0)
  294.       {
  295.       (void) fclose (f);
  296.       return;
  297.       }
  298.  
  299.    (void) fread (&hist, (int) sizeof (HISTORY), 1, f);
  300.    (void) fread (sptr, (unsigned int) (buffer1.st_size - 16 - sizeof (HISTORY)), 1, f);
  301.    got_sched = 1;
  302.  
  303.    num_events = (int) ((buffer1.st_size - 16 - sizeof (HISTORY)) / sizeof (EVENT));
  304.    for (i = 0; i < num_events; i++)
  305.       {
  306.       e_ptrs[i] = sptr++;
  307.       }
  308.  
  309.    (void) fclose (f);
  310.    return;
  311. }
  312.  
  313. void write_sched ()
  314. {
  315.    char temp1[80], temp2[80];
  316.    FILE *f;
  317.    int i;
  318.    struct stat buffer1;
  319.    struct utimbuf times;
  320.    long t;
  321.  
  322.    /* Get the current time */
  323.    t = time (NULL);
  324.  
  325.    (void) strcpy (temp1, BINKpath);
  326.    (void) strcpy (temp2, BINKpath);
  327.    (void) strcat (temp1, "Binkley.Scd");
  328.    (void) strcat (temp2, "Binkley.Evt");
  329.  
  330.    /* Get the current stat for .Evt file */
  331.    if (!stat (temp2, &buffer1))
  332.       {
  333.  
  334.       /*
  335.        * If it is newer than current time, we have a problem and we must
  336.        * reset the file date - yucky, but it will probably work 
  337.        */
  338.       if (t < buffer1.st_atime)
  339.          {
  340.          times.actime = buffer1.st_atime;
  341.          times.modtime = buffer1.st_atime;
  342.          status_line (msgtxt[M_DATE_PROBLEM]);
  343.          }
  344.       else
  345.          {
  346.          times.actime = t;
  347.          times.modtime = t;
  348.          }
  349.       }
  350.  
  351.    if ((f = fopen (temp1, "wb")) == NULL)
  352.       {
  353.       return;
  354.       }
  355.  
  356.    (void) fwrite (BinkSched, 16, 1, f);
  357.    (void) fwrite (&hist, (int) sizeof (HISTORY), 1, f);
  358.  
  359.    for (i = 0; i < num_events; i++)
  360.       {
  361.       /* If it is skipped, but not dynamic, reset it */
  362.       if ((e_ptrs[i]->behavior & MAT_SKIP) &&
  363.           (!(e_ptrs[i]->behavior & MAT_DYNAM)))
  364.          {
  365.          e_ptrs[i]->behavior &= ~MAT_SKIP;
  366.          }
  367.  
  368.       /* Write this one out */
  369.       (void) fwrite (e_ptrs[i], sizeof (EVENT), 1, f);
  370.       }
  371.  
  372.    (void) fclose (f);
  373.  
  374.    (void) utime (temp1, ×);
  375.  
  376.    return;
  377. }
  378.  
  379. void write_stats ()
  380. {
  381.    char temp1[80];
  382.    FILE *f;
  383.  
  384.    (void) strcpy (temp1, BINKpath);
  385.    (void) strcat (temp1, "Binkley.Day");
  386.  
  387.    if ((f = fopen (temp1, "wb")) == NULL)
  388.       {
  389.       return;
  390.       }
  391.  
  392.    (void) fwrite (&hist, (int) sizeof (HISTORY), 1, f);
  393.  
  394.    (void) fclose (f);
  395.  
  396.    return;
  397. }
  398.  
  399. int time_to_next (skip_bbs)
  400. int skip_bbs;
  401. {
  402.    int cur_day;
  403.    int cur_hour;
  404.    int cur_minute;
  405.    int cur_mday;
  406.    int cur_mon;
  407.    int cur_year;
  408.    int junk;
  409.    int our_time;
  410.    int i;
  411.    int time_to;
  412.    int guess;
  413.    int nmin;
  414.  
  415.    /* Get the current time in minutes */
  416.    dostime (&cur_hour, &cur_minute, &junk, &junk);
  417.    our_time = cur_hour * 60 + cur_minute;
  418.  
  419.    /* Get the current day of the week */
  420.    dosdate (&cur_mon, &cur_mday, &cur_year, &cur_day);
  421.  
  422.    next_event = -1;
  423.    cur_day = 1 << cur_day;
  424.  
  425.    /* A ridiculous number */
  426.    time_to = 3000;
  427.  
  428.    /* Go through the events from top to bottom */
  429.    for (i = 0; i < num_events; i++)
  430.       {
  431.       /* If it is the current event, skip it */
  432.       if (cur_event == i)
  433.          continue;
  434.  
  435.       /* If it is a BBS event, skip it */
  436.       if (skip_bbs && e_ptrs[i]->behavior & MAT_BBS)
  437.          continue;
  438.  
  439.       /* If it was already run today, skip it */
  440.       if (e_ptrs[i]->last_ran == cur_mday)
  441.          continue;
  442.  
  443.       /* If it doesn't happen today, skip it */
  444.       if (!(e_ptrs[i]->days & cur_day))
  445.          continue;
  446.  
  447.       /* If not this day of the month, skip it */
  448.       if ((e_ptrs[i]->day) && (e_ptrs[i]->day != (char)cur_mday))
  449.          continue;
  450.       
  451.       /* If not this month of the year, skip it */
  452.       if ((e_ptrs[i]->month) && (e_ptrs[i]->month != (char)cur_mon))
  453.          continue;
  454.  
  455.       /* If it is earlier than now, skip it unless it is forced */
  456.       if (e_ptrs[i]->minute <= our_time)
  457.          {
  458.          if (!(e_ptrs[i]->behavior & MAT_FORCED))
  459.             {
  460.             continue;
  461.             }
  462.  
  463.          /* Hmm, found a forced event that has not executed yet */
  464.          /* Give the guy 2 minutes and call it quits */
  465.          guess = 2;
  466.          }
  467.       else
  468.          {
  469.          /* Calculate how far it is from now */
  470.          guess = e_ptrs[i]->minute - our_time;
  471.          }
  472.  
  473.       /* If less than closest so far, keep it */
  474.       if (time_to > guess)
  475.          {
  476.          time_to = guess;
  477.          next_event = i;
  478.          }
  479.       }
  480.  
  481.    /* If we still have nothing, then do it again, starting at midnight */
  482.    if (time_to >= 1441)
  483.       {
  484.       /* Calculate here to midnight */
  485.       nmin = 1440 - our_time;
  486.  
  487.       /* Go to midnight */
  488.       our_time = 0;
  489.  
  490.       /* Go to the next possible day */
  491.       cur_day = (int) (((unsigned) cur_day) << 1);
  492.       if (cur_day > DAY_SATURDAY)
  493.          cur_day = DAY_SUNDAY;
  494.  
  495.       /* Go through the events from top to bottom */
  496.       for (i = 0; i < num_events; i++)
  497.          {
  498.          /* If it is a BBS event, skip it */
  499.          if (skip_bbs && e_ptrs[i]->behavior & MAT_BBS)
  500.             continue;
  501.  
  502.          /* If it doesn't happen today, skip it */
  503.          if (!(e_ptrs[i]->days & cur_day))
  504.             continue;
  505.  
  506.          /* If not this day of the month, skip it */
  507.          if ((e_ptrs[i]->day) && (e_ptrs[i]->day != (char)cur_mday))
  508.             continue;
  509.       
  510.          /* If not this month of the year, skip it */
  511.          if ((e_ptrs[i]->month) && (e_ptrs[i]->month != (char)cur_mon))
  512.             continue;
  513.  
  514.          /* Calculate how far it is from now */
  515.          guess = e_ptrs[i]->minute + nmin;
  516.  
  517.          /* If less than closest so far, keep it */
  518.          if (time_to > guess)
  519.             {
  520.             time_to = guess;
  521.             next_event = i;
  522.             }
  523.          }
  524.       }
  525.  
  526.    if (time_to > 1440)
  527.       time_to = 1440;
  528.  
  529.    if (skip_bbs && (time_to < 1))
  530.       time_to = 1;
  531.  
  532.    return (time_to);
  533. }
  534.