home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Unix / Shells / tcsh / Source / tc.sched.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-21  |  7.2 KB  |  278 lines

  1. /* $Header: /u/christos/src/tcsh-6.03/RCS/tc.sched.c,v 3.8 1992/03/27 01:59:46 christos Exp $ */
  2. /*
  3.  * tc.sched.c: Scheduled command execution
  4.  *
  5.  * Karl Kleinpaste: Computer Consoles Inc. 1984
  6.  */
  7. /*-
  8.  * Copyright (c) 1980, 1991 The Regents of the University of California.
  9.  * All rights reserved.
  10.  *
  11.  * Redistribution and use in source and binary forms, with or without
  12.  * modification, are permitted provided that the following conditions
  13.  * are met:
  14.  * 1. Redistributions of source code must retain the above copyright
  15.  *    notice, this list of conditions and the following disclaimer.
  16.  * 2. Redistributions in binary form must reproduce the above copyright
  17.  *    notice, this list of conditions and the following disclaimer in the
  18.  *    documentation and/or other materials provided with the distribution.
  19.  * 3. All advertising materials mentioning features or use of this software
  20.  *    must display the following acknowledgement:
  21.  *    This product includes software developed by the University of
  22.  *    California, Berkeley and its contributors.
  23.  * 4. Neither the name of the University nor the names of its contributors
  24.  *    may be used to endorse or promote products derived from this software
  25.  *    without specific prior written permission.
  26.  *
  27.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  28.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  29.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  30.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  31.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  32.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  33.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  34.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  35.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  36.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  37.  * SUCH DAMAGE.
  38.  */
  39. #include "sh.h"
  40.  
  41. RCSID("$Id: tc.sched.c,v 3.8 1992/03/27 01:59:46 christos Exp $")
  42.  
  43. #include "ed.h"
  44. #include "tc.h"
  45.  
  46. extern int just_signaled;
  47.  
  48. struct sched_event {
  49.     struct sched_event *t_next;
  50.     time_t t_when;
  51.     Char  **t_lex;
  52. };
  53. static struct sched_event *sched_ptr = NULL;
  54.  
  55.  
  56. time_t
  57. sched_next()
  58. {
  59.     if (sched_ptr)
  60.     return (sched_ptr->t_when);
  61.     return ((time_t) - 1);
  62. }
  63.  
  64. /*ARGSUSED*/
  65. void
  66. dosched(v, c)
  67.     register Char **v;
  68.     struct command *c;
  69. {
  70.     register struct sched_event *tp, *tp1, *tp2;
  71.     time_t  cur_time;
  72.     int     count, hours, minutes, dif_hour, dif_min;
  73.     Char   *cp;
  74.     bool    relative;        /* time specified as +hh:mm */
  75.     struct tm *ltp;
  76.     char   *timeline;
  77.  
  78. /* This is a major kludge because of a gcc linker  */
  79. /* Problem.  It may or may not be needed for you   */
  80. #ifdef _MINIX
  81.     char kludge[10];
  82.     extern char *sprintf();
  83.     sprintf(kludge,"kludge");
  84. #endif /* _MINIX */
  85.  
  86.     v++;
  87.     cp = *v++;
  88.     if (cp == NULL) {
  89.     /* print list of scheduled events */
  90.     for (count = 1, tp = sched_ptr; tp; count++, tp = tp->t_next) {
  91.         timeline = (char *) ctime(&tp->t_when);
  92.         timeline[16] = '\0';
  93.         xprintf("%6d\t%s\t", count, timeline);
  94.         blkpr(tp->t_lex);
  95.         xputchar('\n');
  96.     }
  97.     return;
  98.     }
  99.  
  100.     if (*cp == '-') {
  101.     /* remove item from list */
  102.     if (!sched_ptr)
  103.         stderror(ERR_NOSCHED);
  104.     if (*v)
  105.         stderror(ERR_SCHEDUSAGE);
  106.     count = atoi(short2str(++cp));
  107.     if (count <= 0)
  108.         stderror(ERR_SCHEDUSAGE);
  109.     tp = sched_ptr;
  110.     tp1 = 0;
  111.     while (--count) {
  112.         if (tp->t_next == 0)
  113.         break;
  114.         else {
  115.         tp1 = tp;
  116.         tp = tp->t_next;
  117.         }
  118.     }
  119.     if (count)
  120.         stderror(ERR_SCHEDEV);
  121.     if (tp1 == 0)
  122.         sched_ptr = tp->t_next;
  123.     else
  124.         tp1->t_next = tp->t_next;
  125.     blkfree(tp->t_lex);
  126.     xfree((ptr_t) tp);
  127.     return;
  128.     }
  129.  
  130.     /* else, add an item to the list */
  131.     if (!*v)
  132.     stderror(ERR_SCHEDCOM);
  133.     relative = 0;
  134.     if (!Isdigit(*cp)) {    /* not abs. time */
  135.     if (*cp != '+')
  136.         stderror(ERR_SCHEDUSAGE);
  137.     cp++, relative++;
  138.     }
  139.     minutes = 0;
  140.     hours = atoi(short2str(cp));
  141.     while (*cp && *cp != ':' && *cp != 'a' && *cp != 'p')
  142.     cp++;
  143.     if (*cp && *cp == ':')
  144.     minutes = atoi(short2str(++cp));
  145.     if ((hours < 0) || (minutes < 0) ||
  146.     (hours > 23) || (minutes > 59))
  147.     stderror(ERR_SCHEDTIME);
  148.     while (*cp && *cp != 'p' && *cp != 'a')
  149.     cp++;
  150.     if (*cp && relative)
  151.     stderror(ERR_SCHEDREL);
  152.     if (*cp == 'p')
  153.     hours += 12;
  154.     (void) time(&cur_time);
  155.     ltp = localtime(&cur_time);
  156.     if (relative) {
  157.     dif_hour = hours;
  158.     dif_min = minutes;
  159.     }
  160.     else {
  161.     if ((dif_hour = hours - ltp->tm_hour) < 0)
  162.         dif_hour += 24;
  163.     if ((dif_min = minutes - ltp->tm_min) < 0) {
  164.         dif_min += 60;
  165.         if ((--dif_hour) < 0)
  166.         dif_hour = 23;
  167.     }
  168.     }
  169.     tp = (struct sched_event *) xcalloc(1, sizeof *tp);
  170.     tp->t_when = cur_time - ltp->tm_sec + dif_hour * 3600L + dif_min * 60L;
  171.     /* use of tm_sec: get to beginning of minute. */
  172.     if (!sched_ptr || tp->t_when < sched_ptr->t_when) {
  173.     tp->t_next = sched_ptr;
  174.     sched_ptr = tp;
  175.     }
  176.     else {
  177.     tp1 = sched_ptr->t_next;
  178.     tp2 = sched_ptr;
  179.     while (tp1 && tp->t_when >= tp1->t_when) {
  180.         tp2 = tp1;
  181.         tp1 = tp1->t_next;
  182.     }
  183.     tp->t_next = tp1;
  184.     tp2->t_next = tp;
  185.     }
  186.     tp->t_lex = saveblk(v);
  187. }
  188.  
  189. /*
  190.  * Execute scheduled events
  191.  */
  192. void
  193. sched_run()
  194. {
  195.     time_t   cur_time;
  196.     register struct sched_event *tp, *tp1;
  197.     struct wordent cmd, *nextword, *lastword;
  198.     struct command *t;
  199.     Char  **v, *cp;
  200.     extern Char GettingInput;
  201.  
  202. #ifdef BSDSIGS
  203.     sigmask_t omask;
  204.  
  205.     omask = sigblock(sigmask(SIGINT)) & ~sigmask(SIGINT);
  206. #else
  207.     (void) sighold(SIGINT);
  208. #endif
  209.  
  210.     (void) time(&cur_time);
  211.     tp = sched_ptr;
  212.  
  213.     /* bugfix by: Justin Bur at Universite de Montreal */
  214.     /*
  215.      * this test wouldn't be necessary if this routine were not called before
  216.      * each prompt (in sh.c).  But it is, to catch missed alarms.  Someone
  217.      * ought to fix it all up.  -jbb
  218.      */
  219.     if (!(tp && tp->t_when < cur_time)) {
  220. #ifdef BSDSIGS
  221.     (void) sigsetmask(omask);
  222. #else
  223.     (void) sigrelse(SIGINT);
  224. #endif
  225.     return;
  226.     }
  227.  
  228.     if (GettingInput)
  229.     (void) Cookedmode();
  230.  
  231.     while (tp && tp->t_when < cur_time) {
  232.     if (seterr) {
  233.         xfree((ptr_t) seterr);
  234.         seterr = NULL;
  235.     }
  236.     cmd.word = STRNULL;
  237.     lastword = &cmd;
  238.     v = tp->t_lex;
  239.     for (cp = *v; cp; cp = *++v) {
  240.         nextword = (struct wordent *) xcalloc(1, sizeof cmd);
  241.         nextword->word = Strsave(cp);
  242.         lastword->next = nextword;
  243.         nextword->prev = lastword;
  244.         lastword = nextword;
  245.     }
  246.     lastword->next = &cmd;
  247.     cmd.prev = lastword;
  248.     tp1 = tp;
  249.     sched_ptr = tp = tp1->t_next;    /* looping termination cond: */
  250.     blkfree(tp1->t_lex);    /* straighten out in case of */
  251.     xfree((ptr_t) tp1);    /* command blow-up. */
  252.  
  253.     /* expand aliases like process() does. */
  254.     alias(&cmd);
  255.     /* build a syntax tree for the command. */
  256.     t = syntax(cmd.next, &cmd, 0);
  257.     if (seterr)
  258.         stderror(ERR_OLD);
  259.     /* execute the parse tree. */
  260.     execute(t, -1, NULL, NULL);
  261.     /* done. free the lex list and parse tree. */
  262.     freelex(&cmd), freesyn(t);
  263.     }
  264.     if (GettingInput && !just_signaled) {    /* PWP */
  265.     (void) Rawmode();
  266.     ClearLines();        /* do a real refresh since something may */
  267.     ClearDisp();        /* have printed to the screen */
  268.     Refresh();
  269.     }
  270.     just_signaled = 0;
  271.  
  272. #ifdef BSDSIGS
  273.     (void) sigsetmask(omask);
  274. #else
  275.     (void) sigrelse(SIGINT);
  276. #endif
  277. }
  278.