aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorroot1990-01-20 04:04:04 +0000
committerroot1990-01-20 04:04:04 +0000
commit5cc564a68dea381edcb1c2faccfa923d787ba6e6 (patch)
treeaed4815377711b5670ee4a23898a7b7ae49b2d07
parente6211d55ff7c6306943aa49b850093e483e5aa80 (diff)
downloademacs-5cc564a68dea381edcb1c2faccfa923d787ba6e6.tar.gz
emacs-5cc564a68dea381edcb1c2faccfa923d787ba6e6.zip
Initial revision
-rw-r--r--lib-src/timer.c155
-rw-r--r--lisp/timer.el92
2 files changed, 247 insertions, 0 deletions
diff --git a/lib-src/timer.c b/lib-src/timer.c
new file mode 100644
index 00000000000..3af3210f7d0
--- /dev/null
+++ b/lib-src/timer.c
@@ -0,0 +1,155 @@
1#include <stdio.h>
2#include <signal.h>
3#include <fcntl.h> /* FASYNC */
4#ifdef USG /* FASYNC for SysV */
5#include <sys/file.h>
6#endif
7#include <sys/time.h> /* itimer */
8#include <sys/types.h> /* time_t */
9
10extern int errno;
11extern char *sys_errlist[], *malloc();
12extern time_t time();
13
14#define MAXEVENTS 256
15#define FS 1 /* field seperator for input */
16
17struct event {
18 char *token;
19 time_t reply_at;
20} *events[MAXEVENTS];
21
22int slot; /* The next open place in the events array */
23int mevent = 0; /* 1+ the highest event number */
24char *pname; /* programme name for error messages */
25
26/* Accepts a string of two fields seperated by a ';'
27 * First field is string for getdate, saying when to wake-up.
28 * Second field is a token to identify the request.
29 */
30struct event *
31schedule(str)
32 char *str;
33
34{
35 extern time_t getdate();
36 extern char *strcpy();
37 time_t now;
38 register char *p;
39 static struct event e;
40
41 for(p = str; *p && *p != FS; p++);
42 if (!*p) {
43 (void)fprintf(stderr, "%s: bad input format: %s", pname, str);
44 return((struct event *)NULL);
45 }
46 *p++ = 0;
47
48 if ((e.reply_at = getdate(str, NULL)) - time(&now) < 0) {
49 (void)fprintf(stderr, "%s: bad time spec: %s%c%s", pname, str, FS, p);
50 return((struct event *)NULL);
51 }
52
53 if ((e.token = malloc((unsigned)strlen(p) + 1)) == NULL) {
54 (void)fprintf(stderr, "%s: malloc %s: %s%c%s",
55 pname, sys_errlist[errno], str, FS, p);
56 return((struct event *)NULL);
57 }
58 (void)strcpy(e.token,p);
59
60 return(&e);
61}
62
63void
64notify()
65
66{
67 time_t now, tdiff;
68 register int i, newmax = 0;
69 /* I prefer using the interval timer rather than alarm(); the latter
70 could be substituted if portability requires it. */
71 struct itimerval itimer;
72
73 now = time((time_t *)NULL);
74 slot = mevent;
75 itimer.it_interval.tv_sec = itimer.it_interval.tv_usec = 0;
76 itimer.it_value.tv_usec = 0;
77 itimer.it_value.tv_sec = -1;
78
79 for(i=0; i < mevent; i++) {
80 while (events[i] && events[i]->reply_at <= now) {
81 (void)fputs(events[i]->token, stdout);
82 free(events[i]->token);
83 free((char *)events[i]);
84 events[i] = 0;
85 }
86
87 if (events[i]) {
88 newmax = i+1;
89 if ((tdiff = events[i]->reply_at - now) < (time_t)itimer.it_value.tv_sec
90 || itimer.it_value.tv_sec < 0)
91 /* next timeout */
92 itimer.it_value.tv_sec = (long)tdiff;
93 } else {
94 /* Keep slot as the lowest unused events element */
95 if (i < slot) slot = i;
96 }
97 }
98 /* if the array is full to mevent, slot should be the next available spot */
99 if (slot > (mevent = newmax)) slot = mevent;
100 /* If there's no more events, SIGIO should be next wake-up */
101 if (mevent) (void)setitimer(ITIMER_REAL, &itimer, (struct itimerval *)NULL);
102}
103
104void
105getevent()
106
107{
108 extern char *memcpy(), *fgets();
109 struct event *ep;
110 char buf[256];
111
112 /* in principle the itimer should be disabled on entry to this function,
113 but it really doesn't make any important difference if it isn't */
114
115 if (fgets(buf, sizeof(buf), stdin) == NULL) exit(0);
116
117 if (slot == MAXEVENTS)
118 (void)fprintf(stderr, "%s: too many events: %s", pname, buf);
119
120 else {
121 if ((events[slot] = (struct event *)malloc((sizeof(struct event))))
122 == NULL)
123 (void)fprintf(stderr,"%s: malloc %s: %s", pname, sys_errlist[errno],buf);
124
125 else {
126 if ((ep = schedule(buf)) == NULL)
127 free((char *)events[slot]), events[slot] = 0;
128
129 else {
130 (void)memcpy((char *)events[slot],(char *)ep,sizeof(struct event));
131 if (slot == mevent) mevent++;
132 } /* schedule */
133 } /* malloc */
134 } /* limit events */
135 /* timing, timing. Who knows what this interrupted, or if it said "now"? */
136 notify();
137}
138
139/*ARGSUSED*/
140int
141main(argc, argv)
142 int argc;
143 char **argv;
144
145{
146 for (pname = argv[0] + strlen(argv[0]); *pname != '/' && pname != argv[0];
147 pname--);
148 if (*pname == '/') pname++;
149
150 (void)signal(SIGIO, getevent);
151 (void)signal(SIGALRM, notify);
152 (void)fcntl(0, F_SETFL, FASYNC);
153
154 while (1) pause();
155}
diff --git a/lisp/timer.el b/lisp/timer.el
new file mode 100644
index 00000000000..7f71f784de9
--- /dev/null
+++ b/lisp/timer.el
@@ -0,0 +1,92 @@
1;; Run a function with args at some time in future
2;; Copyright (C) 1990 Free Software Foundation, Inc.
3
4;; This file is part of GNU Emacs.
5
6;; GNU Emacs is free software; you can redistribute it and/or modify
7;; it under the terms of the GNU General Public License as published by
8;; the Free Software Foundation; either version 1, or (at your option)
9;; any later version.
10
11;; GNU Emacs is distributed in the hope that it will be useful,
12;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14;; GNU General Public License for more details.
15
16;; You should have received a copy of the GNU General Public License
17;; along with GNU Emacs; see the file COPYING. If not, write to
18;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
19
20(defvar timer-process nil)
21(defvar timer-alist ())
22(defvar timer-out "")
23(defvar timer-dont-exit nil
24 ;; this is useful for functions which will be doing their own erratic
25 ;; rescheduling or people who otherwise expect to use the process frequently
26 "If non-nil, don't exit the timer process when no more events are pending.")
27
28(defun run-at-time (time repeat function &rest args)
29 "Run a function at a time, and optionally on a regular interval.
30Arguments are TIME, REPEAT, FUNCTION &rest ARGS.
31TIME, a string, can be specified absolutely or relative to now.
32REPEAT, an integer number of seconds, is the interval on which to repeat
33the call to the function."
34 (interactive "sRun at time: \nNRepeat interval: \naFunction: ")
35 (cond ((or (not timer-process)
36 (memq (process-status timer-process) '(exit signal nil)))
37 (if timer-process (delete-process timer-process))
38 (setq timer-process (start-process "timer" nil "timer")
39 timer-alist nil)
40 (set-process-filter timer-process 'timer-process-filter)
41 (set-process-sentinel timer-process 'timer-process-sentinel)
42 (process-kill-without-query timer-process))
43 ((eq (process-status timer-process) 'stop)
44 (continue-process timer-process)))
45 ;; There should be a living, breathing timer process now
46 (let ((token (concat (current-time-string) "-" (length timer-alist))))
47 (send-string timer-process (concat time "\001" token "\n"))
48 (setq timer-alist (cons (list token repeat function args) timer-alist))))
49
50(defun timer-process-filter (proc str)
51 (setq timer-out (concat timer-out str))
52 (let (do token error)
53 (while (string-match "\n" timer-out)
54 (setq token (substring timer-out 0 (match-beginning 0))
55 do (assoc token timer-alist)
56 timer-out (substring timer-out (match-end 0)))
57 (cond
58 (do (apply (nth 2 do) (nth 3 do)) ; do it
59 (if (natnump (nth 1 do)) ; reschedule it
60 (send-string proc (concat (nth 1 do) " sec\001" (car do) "\n"))
61 (setq timer-alist (delq do timer-alist))))
62 ((string-match "timer: \\([^:]+\\): \\([^\001]*\\)\001\\(.*\\)$" token)
63 (setq error (substring token (match-beginning 1) (match-end 1))
64 do (substring token (match-beginning 2) (match-end 2))
65 token (assoc (substring token (match-beginning 3) (match-end 3))
66 timer-alist)
67 timer-alist (delq token timer-alist))
68 (ding 'no-terminate) ; using error function in process filters is rude
69 (message "%s for %s; couldn't set at \"%s\"" error (nth 2 token) do))))
70 (or timer-alist timer-dont-exit (process-send-eof proc))))
71
72(defun timer-process-sentinel (proc str)
73 (let ((stat (process-status proc)))
74 (if (eq stat 'stop) (continue-process proc)
75 ;; if it exited normally, presumably it was intentional.
76 ;; if there were no pending events, who cares that it exited?
77 (if (or (not timer-alist) (eq stat 'exit)) ()
78 (ding 'no-terminate)
79 (message "Timer exited abnormally. All events cancelled."))
80 (setq timer-process nil timer-alist nil timer-scratch ""))))
81
82(defun cancel-timer (function)
83 "Cancel all events scheduled by ``run-at-time'' which would run FUNCTION."
84 (interactive "aCancel function: ")
85 (let ((alist timer-alist))
86 (while alist
87 (if (eq (nth 2 (car alist)) function)
88 (setq timer-alist (delq (car alist) timer-alist)))
89 (setq alist (cdr alist))))
90 (or timer-alist timer-dont-exit (process-send-eof timer-process)))
91
92(provide 'timer)