Mercurial > emacs
annotate lib-src/=timer.c @ 16581:1d74abbf5ac3
use simpler fn.
author | Simon Marshall <simon@gnu.org> |
---|---|
date | Sat, 16 Nov 1996 13:37:51 +0000 |
parents | dd3b83e4ceb0 |
children |
rev | line source |
---|---|
998 | 1 /* timer.c --- daemon to provide a tagged interval timer service |
2 | |
5634
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
3 This little daemon runs forever waiting for commands to schedule events. |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
4 SIGALRM causes |
998 | 5 it to check its queue for events attached to the current second; if |
6 one is found, its label is written to stdout. SIGTERM causes it to | |
7 terminate, printing a list of pending events. | |
8 | |
9 This program is intended to be used with the lisp package called | |
5634
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
10 timer.el. The first such program was written anonymously in 1990. |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
11 This version was documented and rewritten for portability by |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
12 esr@snark.thyrsus.com, Aug 7 1992. */ |
998 | 13 |
45 | 14 #include <stdio.h> |
15 #include <signal.h> | |
5634
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
16 #include <errno.h> |
958 | 17 #include <sys/types.h> /* time_t */ |
18 | |
4696
1fc792473491
Include <config.h> instead of "config.h".
Roland McGrath <roland@gnu.org>
parents:
4392
diff
changeset
|
19 #include <../src/config.h> |
5634
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
20 #undef read |
45 | 21 |
3392 | 22 #ifdef LINUX |
23 /* Perhaps this is correct unconditionally. */ | |
24 #undef signal | |
25 #endif | |
5634
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
26 #ifdef _CX_UX |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
27 /* I agree with the comment above, this probably should be unconditional (it |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
28 * is already unconditional in a couple of other files in this directory), |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
29 * but in the spirit of minimizing the effects of my port, I am making it |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
30 * conditional on _CX_UX. |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
31 */ |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
32 #undef signal |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
33 #endif |
3392 | 34 |
35 | |
45 | 36 extern int errno; |
8857 | 37 extern char *strerror (); |
998 | 38 extern time_t time (); |
958 | 39 |
40 /* | |
5634
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
41 * The field separator for input. This character shouldn't occur in dates, |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
42 * and should be printable so event strings are readable by people. |
958 | 43 */ |
44 #define FS '@' | |
45 | 45 |
958 | 46 struct event |
998 | 47 { |
958 | 48 char *token; |
49 time_t reply_at; | |
998 | 50 }; |
51 int events_size; /* How many slots have we allocated? */ | |
52 int num_events; /* How many are actually scheduled? */ | |
53 struct event *events; /* events[0 .. num_events-1] are the | |
54 valid events. */ | |
45 | 55 |
5634
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
56 char *pname; /* program name for error messages */ |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
57 |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
58 /* This buffer is used for reading commands. |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
59 We make it longer when necessary, but we never free it. */ |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
60 char *buf; |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
61 /* This is the allocated size of buf. */ |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
62 int buf_size; |
45 | 63 |
5634
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
64 /* Non-zero means don't handle an alarm now; |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
65 instead, just set alarm_deferred if an alarm happens. |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
66 We set this around parts of the program that call malloc and free. */ |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
67 int defer_alarms; |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
68 |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
69 /* Non-zero if an alarm came in during the reading of a command. */ |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
70 int alarm_deferred; |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
71 |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
72 /* Schedule one event, and arrange an alarm for it. |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
73 STR is a string of two fields separated by FS. |
2813
89b1121e2d43
* timer.c: Fix mispellings of get_date function's name.
Jim Blandy <jimb@redhat.com>
parents:
2592
diff
changeset
|
74 First field is string for get_date, saying when to wake-up. |
998 | 75 Second field is a token to identify the request. */ |
5634
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
76 |
998 | 77 void |
78 schedule (str) | |
79 char *str; | |
45 | 80 { |
2813
89b1121e2d43
* timer.c: Fix mispellings of get_date function's name.
Jim Blandy <jimb@redhat.com>
parents:
2592
diff
changeset
|
81 extern time_t get_date (); |
998 | 82 extern char *strcpy (); |
83 time_t now; | |
84 register char *p; | |
85 static struct event *ep; | |
5634
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
86 |
998 | 87 /* check entry format */ |
88 for (p = str; *p && *p != FS; p++) | |
89 continue; | |
90 if (!*p) | |
958 | 91 { |
1751
fac61b478a41
Also, write a newline after the token.
Michael I. Bushnell <mib@gnu.org>
parents:
1750
diff
changeset
|
92 fprintf (stderr, "%s: bad input format: %s\n", pname, str); |
998 | 93 return; |
958 | 94 } |
998 | 95 *p++ = 0; |
45 | 96 |
998 | 97 /* allocate an event slot */ |
98 ep = events + num_events; | |
958 | 99 |
998 | 100 /* If the event array is full, stretch it. After stretching, we know |
101 that ep will be pointing to an available event spot. */ | |
102 if (ep == events + events_size) | |
103 { | |
104 int old_size = events_size; | |
105 | |
106 events_size *= 2; | |
107 events = ((struct event *) | |
108 realloc (events, events_size * sizeof (struct event))); | |
109 if (! events) | |
110 { | |
111 fprintf (stderr, "%s: virtual memory exhausted.\n", pname); | |
5634
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
112 /* Since there is so much virtual memory, and running out |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
113 almost surely means something is very very wrong, |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
114 it is best to exit rather than continue. */ |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
115 exit (1); |
998 | 116 } |
117 | |
118 while (old_size < events_size) | |
119 events[old_size++].token = NULL; | |
120 } | |
121 | |
122 /* Don't allow users to schedule events in past time. */ | |
123 ep->reply_at = get_date (str, NULL); | |
124 if (ep->reply_at - time (&now) < 0) | |
958 | 125 { |
1751
fac61b478a41
Also, write a newline after the token.
Michael I. Bushnell <mib@gnu.org>
parents:
1750
diff
changeset
|
126 fprintf (stderr, "%s: bad time spec: %s%c%s\n", pname, str, FS, p); |
998 | 127 return; |
128 } | |
45 | 129 |
998 | 130 /* save the event description */ |
131 ep->token = (char *) malloc ((unsigned) strlen (p) + 1); | |
132 if (! ep->token) | |
133 { | |
1751
fac61b478a41
Also, write a newline after the token.
Michael I. Bushnell <mib@gnu.org>
parents:
1750
diff
changeset
|
134 fprintf (stderr, "%s: malloc %s: %s%c%s\n", |
5525
1d84e80b47a4
Don't declare sys_errlist; declare strerror instead.
Roland McGrath <roland@gnu.org>
parents:
4696
diff
changeset
|
135 pname, strerror (errno), str, FS, p); |
998 | 136 return; |
958 | 137 } |
998 | 138 |
139 strcpy (ep->token, p); | |
140 num_events++; | |
45 | 141 } |
5634
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
142 |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
143 /* Print the notification for the alarmed event just arrived if any, |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
144 and schedule an alarm for the next event if any. */ |
45 | 145 |
146 void | |
998 | 147 notify () |
958 | 148 { |
1995
f149ad4ad9d4
* timer.c (notify): Initialize waitfor properly.
Jim Blandy <jimb@redhat.com>
parents:
1751
diff
changeset
|
149 time_t now, tdiff, waitfor = -1; |
998 | 150 register struct event *ep; |
151 | |
5634
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
152 /* Inhibit interference with alarms while changing global vars. */ |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
153 defer_alarms = 1; |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
154 alarm_deferred = 0; |
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
|
155 |
998 | 156 now = time ((time_t *) NULL); |
45 | 157 |
998 | 158 for (ep = events; ep < events + num_events; ep++) |
159 /* Are any events ready to fire? */ | |
160 if (ep->reply_at <= now) | |
161 { | |
162 fputs (ep->token, stdout); | |
1750
2a92e870a448
Also, write a newline after the token.
Michael I. Bushnell <mib@gnu.org>
parents:
1749
diff
changeset
|
163 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
|
164 fflush (stdout); |
998 | 165 free (ep->token); |
45 | 166 |
998 | 167 /* We now have a hole in the event array; fill it with the last |
168 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
|
169 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
|
170 ep->reply_at = events[num_events - 1].reply_at; |
998 | 171 num_events--; |
45 | 172 |
998 | 173 /* We ought to scan this event again. */ |
174 ep--; | |
175 } | |
176 else | |
177 { | |
178 /* next timeout should be the soonest of any remaining */ | |
179 if ((tdiff = ep->reply_at - now) < waitfor || waitfor < 0) | |
180 waitfor = (long)tdiff; | |
181 } | |
958 | 182 |
998 | 183 /* If there are no more events, we needn't bother setting an alarm. */ |
184 if (num_events > 0) | |
185 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
|
186 |
5634
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
187 /* Now check if there was another alarm |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
188 while we were handling an explicit request. */ |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
189 defer_alarms = 0; |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
190 if (alarm_deferred) |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
191 notify (); |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
192 alarm_deferred = 0; |
45 | 193 } |
5634
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
194 |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
195 /* Read one command from command from standard input |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
196 and schedule the event for it. */ |
45 | 197 |
198 void | |
998 | 199 getevent () |
958 | 200 { |
998 | 201 int i; |
202 | |
203 /* In principle the itimer should be disabled on entry to this | |
204 function, but it really doesn't make any important difference | |
205 if it isn't. */ | |
206 | |
5634
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
207 if (buf == 0) |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
208 { |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
209 buf_size = 80; |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
210 buf = (char *) malloc (buf_size); |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
211 } |
45 | 212 |
998 | 213 /* Read a line from standard input, expanding buf if it is too short |
214 to hold the line. */ | |
215 for (i = 0; ; i++) | |
216 { | |
5634
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
217 char c; |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
218 int nread; |
998 | 219 |
220 if (i >= buf_size) | |
221 { | |
222 buf_size *= 2; | |
5634
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
223 alarm_deferred = 0; |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
224 defer_alarms = 1; |
998 | 225 buf = (char *) realloc (buf, buf_size); |
5634
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
226 defer_alarms = 0; |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
227 if (alarm_deferred) |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
228 notify (); |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
229 alarm_deferred = 0; |
998 | 230 } |
231 | |
5634
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
232 /* Read one character into c. */ |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
233 while (1) |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
234 { |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
235 nread = read (fileno (stdin), &c, 1); |
998 | 236 |
5634
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
237 /* Retry after transient error. */ |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
238 if (nread < 0 |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
239 && (1 |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
240 #ifdef EINTR |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
241 || errno == EINTR |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
242 #endif |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
243 #ifdef EAGAIN |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
244 || errno == EAGAIN |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
245 #endif |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
246 )) |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
247 continue; |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
248 |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
249 /* Report serious errors. */ |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
250 if (nread < 0) |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
251 { |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
252 perror ("read"); |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
253 exit (1); |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
254 } |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
255 |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
256 /* On eof, exit. */ |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
257 if (nread == 0) |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
258 exit (0); |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
259 |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
260 break; |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
261 } |
45 | 262 |
998 | 263 if (c == '\n') |
264 { | |
265 buf[i] = '\0'; | |
266 break; | |
267 } | |
45 | 268 |
998 | 269 buf[i] = c; |
270 } | |
271 | |
272 /* Register the event. */ | |
5634
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
273 alarm_deferred = 0; |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
274 defer_alarms = 1; |
998 | 275 schedule (buf); |
5634
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
276 defer_alarms = 0; |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
277 notify (); |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
278 alarm_deferred = 0; |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
279 } |
998 | 280 |
5634
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
281 /* Handle incoming signal SIG. */ |
45 | 282 |
2102
b11495a4ecdf
* timer.c (main): Set the ownership of the stdin file descriptor
Jim Blandy <jimb@redhat.com>
parents:
1995
diff
changeset
|
283 SIGTYPE |
998 | 284 sigcatch (sig) |
285 int sig; | |
958 | 286 { |
998 | 287 struct event *ep; |
45 | 288 |
5634
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
289 /* required on older UNIXes; harmless on newer ones */ |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
290 signal (sig, sigcatch); |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
291 |
998 | 292 switch (sig) |
958 | 293 { |
294 case SIGALRM: | |
5634
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
295 if (defer_alarms) |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
296 alarm_deferred = 1; |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
297 else |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
298 notify (); |
998 | 299 break; |
958 | 300 case SIGTERM: |
998 | 301 fprintf (stderr, "Events still queued:\n"); |
302 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
|
303 fprintf (stderr, "%d = %ld @ %s\n", |
998 | 304 ep - events, ep->reply_at, ep->token); |
305 exit (0); | |
306 break; | |
958 | 307 } |
45 | 308 } |
958 | 309 |
45 | 310 /*ARGSUSED*/ |
311 int | |
998 | 312 main (argc, argv) |
45 | 313 int argc; |
314 char **argv; | |
315 { | |
998 | 316 for (pname = argv[0] + strlen (argv[0]); |
317 *pname != '/' && pname != argv[0]; | |
45 | 318 pname--); |
998 | 319 if (*pname == '/') |
320 pname++; | |
45 | 321 |
998 | 322 events_size = 16; |
323 events = ((struct event *) malloc (events_size * sizeof (*events))); | |
324 num_events = 0; | |
325 | |
326 signal (SIGALRM, sigcatch); | |
327 signal (SIGTERM, sigcatch); | |
958 | 328 |
5634
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
329 /* Loop reading commands from standard input |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
330 and scheduling alarms accordingly. |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
331 The alarms are handled asynchronously, while we wait for commands. */ |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
332 while (1) |
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
333 getevent (); |
45 | 334 } |
5527
51451a050975
[! HAVE_STRERROR] (strerror): Define the function.
Roland McGrath <roland@gnu.org>
parents:
5525
diff
changeset
|
335 |
51451a050975
[! HAVE_STRERROR] (strerror): Define the function.
Roland McGrath <roland@gnu.org>
parents:
5525
diff
changeset
|
336 #ifndef HAVE_STRERROR |
51451a050975
[! HAVE_STRERROR] (strerror): Define the function.
Roland McGrath <roland@gnu.org>
parents:
5525
diff
changeset
|
337 char * |
51451a050975
[! HAVE_STRERROR] (strerror): Define the function.
Roland McGrath <roland@gnu.org>
parents:
5525
diff
changeset
|
338 strerror (errnum) |
51451a050975
[! HAVE_STRERROR] (strerror): Define the function.
Roland McGrath <roland@gnu.org>
parents:
5525
diff
changeset
|
339 int errnum; |
51451a050975
[! HAVE_STRERROR] (strerror): Define the function.
Roland McGrath <roland@gnu.org>
parents:
5525
diff
changeset
|
340 { |
51451a050975
[! HAVE_STRERROR] (strerror): Define the function.
Roland McGrath <roland@gnu.org>
parents:
5525
diff
changeset
|
341 extern char *sys_errlist[]; |
51451a050975
[! HAVE_STRERROR] (strerror): Define the function.
Roland McGrath <roland@gnu.org>
parents:
5525
diff
changeset
|
342 extern int sys_nerr; |
51451a050975
[! HAVE_STRERROR] (strerror): Define the function.
Roland McGrath <roland@gnu.org>
parents:
5525
diff
changeset
|
343 |
51451a050975
[! HAVE_STRERROR] (strerror): Define the function.
Roland McGrath <roland@gnu.org>
parents:
5525
diff
changeset
|
344 if (errnum >= 0 && errnum < sys_nerr) |
51451a050975
[! HAVE_STRERROR] (strerror): Define the function.
Roland McGrath <roland@gnu.org>
parents:
5525
diff
changeset
|
345 return sys_errlist[errnum]; |
51451a050975
[! HAVE_STRERROR] (strerror): Define the function.
Roland McGrath <roland@gnu.org>
parents:
5525
diff
changeset
|
346 return (char *) "Unknown error"; |
51451a050975
[! HAVE_STRERROR] (strerror): Define the function.
Roland McGrath <roland@gnu.org>
parents:
5525
diff
changeset
|
347 } |
51451a050975
[! HAVE_STRERROR] (strerror): Define the function.
Roland McGrath <roland@gnu.org>
parents:
5525
diff
changeset
|
348 |
51451a050975
[! HAVE_STRERROR] (strerror): Define the function.
Roland McGrath <roland@gnu.org>
parents:
5525
diff
changeset
|
349 #endif /* ! HAVE_STRERROR */ |
958 | 350 |
8241
a16dfe068972
(xmalloc): New function.
Richard M. Stallman <rms@gnu.org>
parents:
5634
diff
changeset
|
351 long * |
a16dfe068972
(xmalloc): New function.
Richard M. Stallman <rms@gnu.org>
parents:
5634
diff
changeset
|
352 xmalloc (size) |
a16dfe068972
(xmalloc): New function.
Richard M. Stallman <rms@gnu.org>
parents:
5634
diff
changeset
|
353 int size; |
a16dfe068972
(xmalloc): New function.
Richard M. Stallman <rms@gnu.org>
parents:
5634
diff
changeset
|
354 { |
a16dfe068972
(xmalloc): New function.
Richard M. Stallman <rms@gnu.org>
parents:
5634
diff
changeset
|
355 register long *val; |
a16dfe068972
(xmalloc): New function.
Richard M. Stallman <rms@gnu.org>
parents:
5634
diff
changeset
|
356 |
a16dfe068972
(xmalloc): New function.
Richard M. Stallman <rms@gnu.org>
parents:
5634
diff
changeset
|
357 val = (long *) malloc (size); |
a16dfe068972
(xmalloc): New function.
Richard M. Stallman <rms@gnu.org>
parents:
5634
diff
changeset
|
358 |
a16dfe068972
(xmalloc): New function.
Richard M. Stallman <rms@gnu.org>
parents:
5634
diff
changeset
|
359 if (!val && size) |
a16dfe068972
(xmalloc): New function.
Richard M. Stallman <rms@gnu.org>
parents:
5634
diff
changeset
|
360 { |
a16dfe068972
(xmalloc): New function.
Richard M. Stallman <rms@gnu.org>
parents:
5634
diff
changeset
|
361 fprintf (stderr, "timer: virtual memory exceeded\n"); |
a16dfe068972
(xmalloc): New function.
Richard M. Stallman <rms@gnu.org>
parents:
5634
diff
changeset
|
362 exit (1); |
a16dfe068972
(xmalloc): New function.
Richard M. Stallman <rms@gnu.org>
parents:
5634
diff
changeset
|
363 } |
a16dfe068972
(xmalloc): New function.
Richard M. Stallman <rms@gnu.org>
parents:
5634
diff
changeset
|
364 |
a16dfe068972
(xmalloc): New function.
Richard M. Stallman <rms@gnu.org>
parents:
5634
diff
changeset
|
365 return val; |
a16dfe068972
(xmalloc): New function.
Richard M. Stallman <rms@gnu.org>
parents:
5634
diff
changeset
|
366 } |
a16dfe068972
(xmalloc): New function.
Richard M. Stallman <rms@gnu.org>
parents:
5634
diff
changeset
|
367 |
958 | 368 /* timer.c ends here */ |