Mercurial > emacs
annotate lib-src/=timer.c @ 3166:419d3bf1cb2b
(x_get_local_selection): If no conversion function
exists for the requested type, just return nil.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Thu, 27 May 1993 04:01:13 +0000 |
parents | 37954061f933 |
children | 0cab056764c8 |
rev | line source |
---|---|
998 | 1 /* timer.c --- daemon to provide a tagged interval timer service |
2 | |
3 This little daemon runs forever waiting for signals. SIGIO (or | |
4 SIGUSR1) causes it to read an event spec from stdin; that is, a | |
5 date followed by colon followed by an event label. SIGALRM causes | |
6 it to check its queue for events attached to the current second; if | |
7 one is found, its label is written to stdout. SIGTERM causes it to | |
8 terminate, printing a list of pending events. | |
9 | |
10 This program is intended to be used with the lisp package called | |
11 timer.el. It was written anonymously in 1990. This version was | |
2592
2e57e16282f0
(notify): Bug fix. Treat the body of this function as a critical region.
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2102
diff
changeset
|
12 documented and rewritten for portability by esr@snark.thyrsus.com, |
998 | 13 Aug 7 1992. */ |
14 | |
45 | 15 #include <stdio.h> |
16 #include <signal.h> | |
17 #include <fcntl.h> /* FASYNC */ | |
958 | 18 #include <sys/types.h> /* time_t */ |
19 | |
20 #include "../src/config.h" | |
21 #ifdef USG | |
22 #undef SIGIO | |
23 #define SIGIO SIGUSR1 | |
45 | 24 #endif |
25 | |
26 extern int errno; | |
998 | 27 extern char *sys_errlist[], *malloc (); |
28 extern time_t time (); | |
958 | 29 |
30 /* | |
31 * The field separator for input. This character shouldn't be legal in a date, | |
32 * and should be printable so event strings are readable by people. Was | |
33 * originally ';', then got changed to bogus `\001'. | |
34 */ | |
35 #define FS '@' | |
45 | 36 |
958 | 37 struct event |
998 | 38 { |
958 | 39 char *token; |
40 time_t reply_at; | |
998 | 41 }; |
42 int events_size; /* How many slots have we allocated? */ | |
43 int num_events; /* How many are actually scheduled? */ | |
44 struct event *events; /* events[0 .. num_events-1] are the | |
45 valid events. */ | |
45 | 46 |
47 char *pname; /* programme name for error messages */ | |
48 | |
958 | 49 /* Accepts a string of two fields seperated by FS. |
2813
89b1121e2d43
* timer.c: Fix mispellings of get_date function's name.
Jim Blandy <jimb@redhat.com>
parents:
2592
diff
changeset
|
50 First field is string for get_date, saying when to wake-up. |
998 | 51 Second field is a token to identify the request. */ |
52 void | |
53 schedule (str) | |
54 char *str; | |
45 | 55 { |
2813
89b1121e2d43
* timer.c: Fix mispellings of get_date function's name.
Jim Blandy <jimb@redhat.com>
parents:
2592
diff
changeset
|
56 extern time_t get_date (); |
998 | 57 extern char *strcpy (); |
58 time_t now; | |
59 register char *p; | |
60 static struct event *ep; | |
958 | 61 |
998 | 62 /* check entry format */ |
63 for (p = str; *p && *p != FS; p++) | |
64 continue; | |
65 if (!*p) | |
958 | 66 { |
1751
fac61b478a41
Also, write a newline after the token.
Michael I. Bushnell <mib@gnu.org>
parents:
1750
diff
changeset
|
67 fprintf (stderr, "%s: bad input format: %s\n", pname, str); |
998 | 68 return; |
958 | 69 } |
998 | 70 *p++ = 0; |
45 | 71 |
998 | 72 /* allocate an event slot */ |
73 ep = events + num_events; | |
958 | 74 |
998 | 75 /* If the event array is full, stretch it. After stretching, we know |
76 that ep will be pointing to an available event spot. */ | |
77 if (ep == events + events_size) | |
78 { | |
79 int old_size = events_size; | |
80 | |
81 events_size *= 2; | |
82 events = ((struct event *) | |
83 realloc (events, events_size * sizeof (struct event))); | |
84 if (! events) | |
85 { | |
86 fprintf (stderr, "%s: virtual memory exhausted.\n", pname); | |
45 | 87 |
998 | 88 /* Should timer exit now? Well, we've still got other |
89 events in the queue, and more memory might become | |
90 available in the future, so we'll just toss this event. | |
91 This will screw up whoever scheduled the event, but | |
92 maybe someone else will survive. */ | |
93 return; | |
94 } | |
95 | |
96 while (old_size < events_size) | |
97 events[old_size++].token = NULL; | |
98 } | |
99 | |
100 /* Don't allow users to schedule events in past time. */ | |
101 ep->reply_at = get_date (str, NULL); | |
102 if (ep->reply_at - time (&now) < 0) | |
958 | 103 { |
1751
fac61b478a41
Also, write a newline after the token.
Michael I. Bushnell <mib@gnu.org>
parents:
1750
diff
changeset
|
104 fprintf (stderr, "%s: bad time spec: %s%c%s\n", pname, str, FS, p); |
998 | 105 return; |
106 } | |
45 | 107 |
998 | 108 /* save the event description */ |
109 ep->token = (char *) malloc ((unsigned) strlen (p) + 1); | |
110 if (! ep->token) | |
111 { | |
1751
fac61b478a41
Also, write a newline after the token.
Michael I. Bushnell <mib@gnu.org>
parents:
1750
diff
changeset
|
112 fprintf (stderr, "%s: malloc %s: %s%c%s\n", |
998 | 113 pname, sys_errlist[errno], str, FS, p); |
114 return; | |
958 | 115 } |
998 | 116 |
117 strcpy (ep->token, p); | |
118 num_events++; | |
45 | 119 } |
120 | |
121 void | |
998 | 122 notify () |
958 | 123 { |
1995
f149ad4ad9d4
* timer.c (notify): Initialize waitfor properly.
Jim Blandy <jimb@redhat.com>
parents:
1751
diff
changeset
|
124 time_t now, tdiff, waitfor = -1; |
998 | 125 register struct event *ep; |
126 | |
2592
2e57e16282f0
(notify): Bug fix. Treat the body of this function as a critical region.
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2102
diff
changeset
|
127 /* If an alarm timer runs out while this function is executing, |
2e57e16282f0
(notify): Bug fix. Treat the body of this function as a critical region.
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2102
diff
changeset
|
128 it could get called recursively. This would be bad, because |
2e57e16282f0
(notify): Bug fix. Treat the body of this function as a critical region.
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2102
diff
changeset
|
129 it's not re-entrant. So we must try to suspend the signal. */ |
2820
37954061f933
* timer.c (notify): Don't call sighold or sigrelse; they're USG
Jim Blandy <jimb@redhat.com>
parents:
2813
diff
changeset
|
130 #if 0 /* This function isn't right for BSD. Fix it later. */ |
2592
2e57e16282f0
(notify): Bug fix. Treat the body of this function as a critical region.
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2102
diff
changeset
|
131 sighold(SIGIO); |
2e57e16282f0
(notify): Bug fix. Treat the body of this function as a critical region.
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2102
diff
changeset
|
132 #endif |
2e57e16282f0
(notify): Bug fix. Treat the body of this function as a critical region.
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2102
diff
changeset
|
133 |
998 | 134 now = time ((time_t *) NULL); |
45 | 135 |
998 | 136 for (ep = events; ep < events + num_events; ep++) |
137 /* Are any events ready to fire? */ | |
138 if (ep->reply_at <= now) | |
139 { | |
140 fputs (ep->token, stdout); | |
1750
2a92e870a448
Also, write a newline after the token.
Michael I. Bushnell <mib@gnu.org>
parents:
1749
diff
changeset
|
141 putc ('\n', stdout); |
1746
7c4fc10fde41
timer.c (notify): Flush stdout after writing message to avoid lossage
Michael I. Bushnell <mib@gnu.org>
parents:
998
diff
changeset
|
142 fflush (stdout); |
998 | 143 free (ep->token); |
45 | 144 |
998 | 145 /* We now have a hole in the event array; fill it with the last |
146 event. */ | |
2592
2e57e16282f0
(notify): Bug fix. Treat the body of this function as a critical region.
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2102
diff
changeset
|
147 ep->token = events[num_events - 1].token; |
2e57e16282f0
(notify): Bug fix. Treat the body of this function as a critical region.
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2102
diff
changeset
|
148 ep->reply_at = events[num_events - 1].reply_at; |
998 | 149 num_events--; |
45 | 150 |
998 | 151 /* We ought to scan this event again. */ |
152 ep--; | |
153 } | |
154 else | |
155 { | |
156 /* next timeout should be the soonest of any remaining */ | |
157 if ((tdiff = ep->reply_at - now) < waitfor || waitfor < 0) | |
158 waitfor = (long)tdiff; | |
159 } | |
958 | 160 |
998 | 161 /* If there are no more events, we needn't bother setting an alarm. */ |
162 if (num_events > 0) | |
163 alarm (waitfor); | |
2592
2e57e16282f0
(notify): Bug fix. Treat the body of this function as a critical region.
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2102
diff
changeset
|
164 |
2820
37954061f933
* timer.c (notify): Don't call sighold or sigrelse; they're USG
Jim Blandy <jimb@redhat.com>
parents:
2813
diff
changeset
|
165 #if 0 /* This function isn't right for BSD. */ |
2592
2e57e16282f0
(notify): Bug fix. Treat the body of this function as a critical region.
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2102
diff
changeset
|
166 sigrelse(SIGIO); |
2e57e16282f0
(notify): Bug fix. Treat the body of this function as a critical region.
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2102
diff
changeset
|
167 #endif |
45 | 168 } |
169 | |
170 void | |
998 | 171 getevent () |
958 | 172 { |
998 | 173 int i; |
174 char *buf; | |
175 int buf_size; | |
176 | |
177 /* In principle the itimer should be disabled on entry to this | |
178 function, but it really doesn't make any important difference | |
179 if it isn't. */ | |
180 | |
181 buf_size = 80; | |
182 buf = (char *) malloc (buf_size); | |
45 | 183 |
998 | 184 /* Read a line from standard input, expanding buf if it is too short |
185 to hold the line. */ | |
186 for (i = 0; ; i++) | |
187 { | |
188 int c; | |
189 | |
190 if (i >= buf_size) | |
191 { | |
192 buf_size *= 2; | |
193 buf = (char *) realloc (buf, buf_size); | |
958 | 194 |
998 | 195 /* If we're out of memory, toss this event. */ |
196 do | |
197 { | |
198 c = getchar (); | |
199 } | |
200 while (c != '\n' && c != EOF); | |
201 | |
202 return; | |
203 } | |
204 | |
205 c = getchar (); | |
206 | |
207 if (c == EOF) | |
208 exit (0); | |
45 | 209 |
998 | 210 if (c == '\n') |
211 { | |
212 buf[i] = '\0'; | |
213 break; | |
214 } | |
45 | 215 |
998 | 216 buf[i] = c; |
217 } | |
218 | |
219 /* Register the event. */ | |
220 schedule (buf); | |
221 free (buf); | |
222 | |
223 /* Who knows what this interrupted, or if it said "now"? */ | |
224 notify (); | |
958 | 225 } |
45 | 226 |
2102
b11495a4ecdf
* timer.c (main): Set the ownership of the stdin file descriptor
Jim Blandy <jimb@redhat.com>
parents:
1995
diff
changeset
|
227 SIGTYPE |
998 | 228 sigcatch (sig) |
229 int sig; | |
958 | 230 /* dispatch on incoming signal, then restore it */ |
231 { | |
998 | 232 struct event *ep; |
45 | 233 |
998 | 234 switch (sig) |
958 | 235 { |
236 case SIGALRM: | |
998 | 237 notify (); |
238 break; | |
958 | 239 case SIGIO: |
998 | 240 getevent (); |
241 break; | |
958 | 242 case SIGTERM: |
998 | 243 fprintf (stderr, "Events still queued:\n"); |
244 for (ep = events; ep < events + num_events; ep++) | |
1751
fac61b478a41
Also, write a newline after the token.
Michael I. Bushnell <mib@gnu.org>
parents:
1750
diff
changeset
|
245 fprintf (stderr, "%d = %ld @ %s\n", |
998 | 246 ep - events, ep->reply_at, ep->token); |
247 exit (0); | |
248 break; | |
958 | 249 } |
45 | 250 |
998 | 251 /* required on older UNIXes; harmless on newer ones */ |
252 signal (sig, sigcatch); | |
45 | 253 } |
958 | 254 |
45 | 255 /*ARGSUSED*/ |
256 int | |
998 | 257 main (argc, argv) |
45 | 258 int argc; |
259 char **argv; | |
260 { | |
998 | 261 for (pname = argv[0] + strlen (argv[0]); |
262 *pname != '/' && pname != argv[0]; | |
45 | 263 pname--); |
998 | 264 if (*pname == '/') |
265 pname++; | |
45 | 266 |
998 | 267 events_size = 16; |
268 events = ((struct event *) malloc (events_size * sizeof (*events))); | |
269 num_events = 0; | |
270 | |
271 signal (SIGIO, sigcatch); | |
272 signal (SIGALRM, sigcatch); | |
273 signal (SIGTERM, sigcatch); | |
958 | 274 |
275 #ifndef USG | |
2102
b11495a4ecdf
* timer.c (main): Set the ownership of the stdin file descriptor
Jim Blandy <jimb@redhat.com>
parents:
1995
diff
changeset
|
276 if (fcntl (0, F_SETOWN, getpid ()) == -1) |
b11495a4ecdf
* timer.c (main): Set the ownership of the stdin file descriptor
Jim Blandy <jimb@redhat.com>
parents:
1995
diff
changeset
|
277 { |
b11495a4ecdf
* timer.c (main): Set the ownership of the stdin file descriptor
Jim Blandy <jimb@redhat.com>
parents:
1995
diff
changeset
|
278 fprintf (stderr, "%s: can't set ownership of stdin\n", pname); |
b11495a4ecdf
* timer.c (main): Set the ownership of the stdin file descriptor
Jim Blandy <jimb@redhat.com>
parents:
1995
diff
changeset
|
279 fprintf (stderr, "%s\n", sys_errlist[errno]); |
b11495a4ecdf
* timer.c (main): Set the ownership of the stdin file descriptor
Jim Blandy <jimb@redhat.com>
parents:
1995
diff
changeset
|
280 exit (1); |
b11495a4ecdf
* timer.c (main): Set the ownership of the stdin file descriptor
Jim Blandy <jimb@redhat.com>
parents:
1995
diff
changeset
|
281 } |
b11495a4ecdf
* timer.c (main): Set the ownership of the stdin file descriptor
Jim Blandy <jimb@redhat.com>
parents:
1995
diff
changeset
|
282 if (fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) | FASYNC) == -1) |
b11495a4ecdf
* timer.c (main): Set the ownership of the stdin file descriptor
Jim Blandy <jimb@redhat.com>
parents:
1995
diff
changeset
|
283 { |
b11495a4ecdf
* timer.c (main): Set the ownership of the stdin file descriptor
Jim Blandy <jimb@redhat.com>
parents:
1995
diff
changeset
|
284 fprintf (stderr, "%s: can't request asynchronous I/O on stdin\n", pname); |
b11495a4ecdf
* timer.c (main): Set the ownership of the stdin file descriptor
Jim Blandy <jimb@redhat.com>
parents:
1995
diff
changeset
|
285 fprintf (stderr, "%s\n", sys_errlist[errno]); |
b11495a4ecdf
* timer.c (main): Set the ownership of the stdin file descriptor
Jim Blandy <jimb@redhat.com>
parents:
1995
diff
changeset
|
286 exit (1); |
b11495a4ecdf
* timer.c (main): Set the ownership of the stdin file descriptor
Jim Blandy <jimb@redhat.com>
parents:
1995
diff
changeset
|
287 } |
958 | 288 #endif /* USG */ |
45 | 289 |
2592
2e57e16282f0
(notify): Bug fix. Treat the body of this function as a critical region.
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2102
diff
changeset
|
290 for (;;) |
2e57e16282f0
(notify): Bug fix. Treat the body of this function as a critical region.
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2102
diff
changeset
|
291 pause (); |
45 | 292 } |
958 | 293 |
294 /* timer.c ends here */ |