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