1 /* $Header: /src/pub/tcsh/tc.sched.c,v 3.17 2000/07/04 19:46:23 christos Exp $ */
3 * tc.sched.c: Scheduled command execution
5 * Karl Kleinpaste: Computer Consoles Inc. 1984
8 * Copyright (c) 1980, 1991 The Regents of the University of California.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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
41 RCSID("$Id: tc.sched.c,v 3.17 2000/07/04 19:46:23 christos Exp $")
46 extern int just_signaled;
49 struct sched_event *t_next;
53 static struct sched_event *sched_ptr = NULL;
60 return (sched_ptr->t_when);
61 return ((time_t) - 1);
70 register struct sched_event *tp, *tp1, *tp2;
72 int count, hours, minutes, dif_hour, dif_min;
74 bool relative; /* time specified as +hh:mm */
78 /* This is a major kludge because of a gcc linker */
79 /* Problem. It may or may not be needed for you */
80 #if defined(_MINIX) && !defined(_MINIX_VMD)
82 extern char *sprintf();
83 sprintf(kludge, CGETS(24, 1, "kludge"));
84 #endif /* _MINIX && !_MINIX_VMD */
90 if ((fmt = varval(STRsched)) == STRNULL)
91 fmt = str2short("%h\t%T\t%R\n");
92 /* print list of scheduled events */
93 for (count = 1, tp = sched_ptr; tp; count++, tp = tp->t_next) {
94 Char buf[BUFSIZE], sbuf[BUFSIZE];
95 blkexpand(tp->t_lex, buf);
96 tprintf(FMT_SCHED, sbuf, fmt, sizeof(sbuf),
97 short2str(buf), tp->t_when, (ptr_t) &count);
105 /* remove item from list */
107 stderror(ERR_NOSCHED);
109 stderror(ERR_SCHEDUSAGE);
110 count = atoi(short2str(++cp));
112 stderror(ERR_SCHEDUSAGE);
124 stderror(ERR_SCHEDEV);
126 sched_ptr = tp->t_next;
128 tp1->t_next = tp->t_next;
134 /* else, add an item to the list */
136 stderror(ERR_SCHEDCOM);
138 if (!Isdigit(*cp)) { /* not abs. time */
140 stderror(ERR_SCHEDUSAGE);
144 hours = atoi(short2str(cp));
145 while (*cp && *cp != ':' && *cp != 'a' && *cp != 'p')
147 if (*cp && *cp == ':')
148 minutes = atoi(short2str(++cp));
149 if ((hours < 0) || (minutes < 0) ||
150 (hours > 23) || (minutes > 59))
151 stderror(ERR_SCHEDTIME);
152 while (*cp && *cp != 'p' && *cp != 'a')
155 stderror(ERR_SCHEDREL);
158 (void) time(&cur_time);
159 ltp = localtime(&cur_time);
165 if ((dif_hour = hours - ltp->tm_hour) < 0)
167 if ((dif_min = minutes - ltp->tm_min) < 0) {
169 if ((--dif_hour) < 0)
173 tp = (struct sched_event *) xcalloc(1, sizeof *tp);
175 tp->t_when = cur_time - ltp->tm_sec + dif_hour * 3600 + dif_min * 60;
177 tp->t_when = cur_time - ltp->tm_sec + dif_hour * 3600L + dif_min * 60L;
179 /* use of tm_sec: get to beginning of minute. */
180 if (!sched_ptr || tp->t_when < sched_ptr->t_when) {
181 tp->t_next = sched_ptr;
185 tp1 = sched_ptr->t_next;
187 while (tp1 && tp->t_when >= tp1->t_when) {
194 tp->t_lex = saveblk(v);
198 * Execute scheduled events
206 register struct sched_event *tp, *tp1;
207 struct wordent cmd, *nextword, *lastword;
210 extern Char GettingInput;
214 omask = sigblock(sigmask(SIGINT)) & ~sigmask(SIGINT);
216 (void) sighold(SIGINT);
221 (void) time(&cur_time);
224 /* bugfix by: Justin Bur at Universite de Montreal */
226 * this test wouldn't be necessary if this routine were not called before
227 * each prompt (in sh.c). But it is, to catch missed alarms. Someone
228 * ought to fix it all up. -jbb
230 if (!(tp && tp->t_when < cur_time)) {
232 (void) sigsetmask(omask);
234 (void) sigrelse(SIGINT);
242 while (tp && tp->t_when < cur_time) {
244 xfree((ptr_t) seterr);
250 for (cp = *v; cp; cp = *++v) {
251 nextword = (struct wordent *) xcalloc(1, sizeof cmd);
252 nextword->word = Strsave(cp);
253 lastword->next = nextword;
254 nextword->prev = lastword;
257 lastword->next = &cmd;
260 sched_ptr = tp = tp1->t_next; /* looping termination cond: */
261 blkfree(tp1->t_lex); /* straighten out in case of */
262 xfree((ptr_t) tp1); /* command blow-up. */
264 /* expand aliases like process() does. */
266 /* build a syntax tree for the command. */
267 t = syntax(cmd.next, &cmd, 0);
270 /* execute the parse tree. */
271 execute(t, -1, NULL, NULL);
272 /* done. free the lex list and parse tree. */
273 freelex(&cmd), freesyn(t);
275 if (GettingInput && !just_signaled) { /* PWP */
277 ClearLines(); /* do a real refresh since something may */
278 ClearDisp(); /* have printed to the screen */
284 (void) sigsetmask(omask);
286 (void) sigrelse(SIGINT);