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