xref: /freebsd/contrib/tcsh/tc.sched.c (revision 2be1a816b9ff69588e55be0a84cbe2a31efc0f2f)
1 /* $Header: /p/tcsh/cvsroot/tcsh/tc.sched.c,v 3.25 2006/03/02 18:46:45 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("$tcsh: tc.sched.c,v 3.25 2006/03/02 18:46:45 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(void)
55 {
56     if (sched_ptr)
57 	return (sched_ptr->t_when);
58     return ((time_t) - 1);
59 }
60 
61 /*ARGSUSED*/
62 void
63 dosched(Char **v, struct command *c)
64 {
65     struct sched_event *tp, **pp;
66     time_t  cur_time;
67     int     count, hours, minutes, dif_hour, dif_min;
68     Char   *cp;
69     int    relative;		/* time specified as +hh:mm */
70     struct tm *ltp;
71 
72     USE(c);
73 /* This is a major kludge because of a gcc linker  */
74 /* Problem.  It may or may not be needed for you   */
75 #if defined(_MINIX) && !defined(_MINIX_VMD)
76     char kludge[10];
77     extern char *sprintf();
78     sprintf(kludge, CGETS(24, 1, "kludge"));
79 #endif /* _MINIX && !_MINIX_VMD */
80 
81     v++;
82     cp = *v++;
83     if (cp == NULL) {
84 	const Char *fmt;
85 	if ((fmt = varval(STRsched)) == STRNULL)
86 	    fmt = str2short("%h\t%T\t%R\n");
87 	/* print list of scheduled events */
88 	for (count = 1, tp = sched_ptr; tp; count++, tp = tp->t_next) {
89 	    Char *buf, *str;
90 
91 	    buf = blkexpand(tp->t_lex);
92 	    cleanup_push(buf, xfree);
93 	    str = tprintf(FMT_SCHED, fmt, short2str(buf), tp->t_when, &count);
94 	    cleanup_until(buf);
95 	    cleanup_push(str, xfree);
96 	    for (cp = str; *cp;)
97 		xputwchar(*cp++);
98 	    cleanup_until(str);
99 	}
100 	return;
101     }
102 
103     if (*cp == '-') {
104 	/* remove item from list */
105 	if (!sched_ptr)
106 	    stderror(ERR_NOSCHED);
107 	if (*v)
108 	    stderror(ERR_SCHEDUSAGE);
109 	count = atoi(short2str(++cp));
110 	if (count <= 0)
111 	    stderror(ERR_SCHEDUSAGE);
112 	pp = &sched_ptr;
113 	tp = sched_ptr;
114 	while (--count) {
115 	    if (tp->t_next == 0)
116 		break;
117 	    else {
118 		pp = &tp->t_next;
119 		tp = tp->t_next;
120 	    }
121 	}
122 	if (count)
123 	    stderror(ERR_SCHEDEV);
124 	*pp = tp->t_next;
125 	blkfree(tp->t_lex);
126 	xfree(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 = xcalloc(1, sizeof *tp);
170 #ifdef _SX
171     tp->t_when = cur_time - ltp->tm_sec + dif_hour * 3600 + dif_min * 60;
172 #else	/* _SX */
173     tp->t_when = cur_time - ltp->tm_sec + dif_hour * 3600L + dif_min * 60L;
174 #endif /* _SX */
175     /* use of tm_sec: get to beginning of minute. */
176     for (pp = &sched_ptr; *pp != NULL && tp->t_when >= (*pp)->t_when;
177 	 pp = &(*pp)->t_next)
178 	;
179     tp->t_next = *pp;
180     *pp = tp;
181     tp->t_lex = saveblk(v);
182 }
183 
184 /*
185  * Execute scheduled events
186  */
187 void
188 sched_run(void)
189 {
190     time_t   cur_time;
191     struct sched_event *tp;
192     struct wordent cmd, *nextword, *lastword;
193     struct command *t;
194     Char  **v, *cp;
195 
196     pintr_disabled++;
197     cleanup_push(&pintr_disabled, disabled_cleanup);
198 
199     (void) time(&cur_time);
200 
201     /* bugfix by: Justin Bur at Universite de Montreal */
202     /*
203      * this test wouldn't be necessary if this routine were not called before
204      * each prompt (in sh.c).  But it is, to catch missed alarms.  Someone
205      * ought to fix it all up.  -jbb
206      */
207     if (!(sched_ptr && sched_ptr->t_when < cur_time)) {
208 	cleanup_until(&pintr_disabled);
209 	return;
210     }
211 
212     if (GettingInput)
213 	(void) Cookedmode();
214 
215     while ((tp = sched_ptr) != NULL && tp->t_when < cur_time) {
216 	if (seterr) {
217 	    xfree(seterr);
218 	    seterr = NULL;
219 	}
220 	cmd.word = STRNULL;
221 	lastword = &cmd;
222 	v = tp->t_lex;
223 	for (cp = *v; cp; cp = *++v) {
224 	    nextword = xcalloc(1, sizeof cmd);
225 	    nextword->word = Strsave(cp);
226 	    lastword->next = nextword;
227 	    nextword->prev = lastword;
228 	    lastword = nextword;
229 	}
230 	lastword->next = &cmd;
231 	cmd.prev = lastword;
232 	sched_ptr = tp->t_next;	/* looping termination cond: */
233 	blkfree(tp->t_lex);	/* straighten out in case of */
234 	xfree(tp);		/* command blow-up. */
235 
236 	cleanup_push(&cmd, lex_cleanup);
237 	/* expand aliases like process() does. */
238 	alias(&cmd);
239 	/* build a syntax tree for the command. */
240 	t = syntax(cmd.next, &cmd, 0);
241 	cleanup_push(t, syntax_cleanup);
242 	if (seterr)
243 	    stderror(ERR_OLD);
244 	/* execute the parse tree. */
245 	execute(t, -1, NULL, NULL, TRUE);
246 	/* done. free the lex list and parse tree. */
247 	cleanup_until(&cmd);
248     }
249     if (GettingInput && !just_signaled) {	/* PWP */
250 	(void) Rawmode();
251 	ClearLines();		/* do a real refresh since something may */
252 	ClearDisp();		/* have printed to the screen */
253 	Refresh();
254     }
255     just_signaled = 0;
256 
257     cleanup_until(&pintr_disabled);
258 }
259