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