Mercurial > emacs
annotate lib-src/=timer.c @ 8241:a16dfe068972
(xmalloc): New function.
| author | Richard M. Stallman <rms@gnu.org> |
|---|---|
| date | Wed, 13 Jul 1994 19:10:27 +0000 |
| parents | e77e9d7386be |
| children | 76dea5c39776 |
| 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; |
|
5525
1d84e80b47a4
Don't declare sys_errlist; declare strerror instead.
Roland McGrath <roland@gnu.org>
parents:
4696
diff
changeset
|
37 extern char *strerror (), *malloc (); |
| 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; |
|
5634
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
202 int n_events; |
| 998 | 203 |
| 204 /* In principle the itimer should be disabled on entry to this | |
| 205 function, but it really doesn't make any important difference | |
| 206 if it isn't. */ | |
| 207 | |
|
5634
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
208 if (buf == 0) |
|
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
209 { |
|
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
210 buf_size = 80; |
|
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
211 buf = (char *) malloc (buf_size); |
|
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
212 } |
| 45 | 213 |
| 998 | 214 /* Read a line from standard input, expanding buf if it is too short |
| 215 to hold the line. */ | |
| 216 for (i = 0; ; i++) | |
| 217 { | |
|
5634
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
218 char c; |
|
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
219 int nread; |
| 998 | 220 |
| 221 if (i >= buf_size) | |
| 222 { | |
| 223 buf_size *= 2; | |
|
5634
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
224 alarm_deferred = 0; |
|
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
225 defer_alarms = 1; |
| 998 | 226 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
|
227 defer_alarms = 0; |
|
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
228 if (alarm_deferred) |
|
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
229 notify (); |
|
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
230 alarm_deferred = 0; |
| 998 | 231 } |
| 232 | |
|
5634
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
233 /* Read one character into c. */ |
|
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
234 while (1) |
|
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
235 { |
|
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
236 nread = read (fileno (stdin), &c, 1); |
| 998 | 237 |
|
5634
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
238 /* Retry after transient error. */ |
|
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
239 if (nread < 0 |
|
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
240 && (1 |
|
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
241 #ifdef EINTR |
|
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
242 || errno == EINTR |
|
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
243 #endif |
|
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
244 #ifdef EAGAIN |
|
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
245 || errno == EAGAIN |
|
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
246 #endif |
|
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
247 )) |
|
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
248 continue; |
|
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
249 |
|
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
250 /* Report serious errors. */ |
|
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
251 if (nread < 0) |
|
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
252 { |
|
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
253 perror ("read"); |
|
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
254 exit (1); |
|
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 |
|
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
257 /* On eof, exit. */ |
|
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
258 if (nread == 0) |
|
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
259 exit (0); |
|
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
260 |
|
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
261 break; |
|
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
262 } |
| 45 | 263 |
| 998 | 264 if (c == '\n') |
| 265 { | |
| 266 buf[i] = '\0'; | |
| 267 break; | |
| 268 } | |
| 45 | 269 |
| 998 | 270 buf[i] = c; |
| 271 } | |
| 272 | |
| 273 /* Register the event. */ | |
|
5634
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
274 alarm_deferred = 0; |
|
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
275 defer_alarms = 1; |
| 998 | 276 schedule (buf); |
|
5634
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
277 defer_alarms = 0; |
|
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
278 notify (); |
|
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
279 alarm_deferred = 0; |
|
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
280 } |
| 998 | 281 |
|
5634
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
282 /* Handle incoming signal SIG. */ |
| 45 | 283 |
|
2102
b11495a4ecdf
* timer.c (main): Set the ownership of the stdin file descriptor
Jim Blandy <jimb@redhat.com>
parents:
1995
diff
changeset
|
284 SIGTYPE |
| 998 | 285 sigcatch (sig) |
| 286 int sig; | |
| 958 | 287 { |
| 998 | 288 struct event *ep; |
| 45 | 289 |
|
5634
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
290 /* 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
|
291 signal (sig, sigcatch); |
|
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
292 |
| 998 | 293 switch (sig) |
| 958 | 294 { |
| 295 case SIGALRM: | |
|
5634
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
296 if (defer_alarms) |
|
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
297 alarm_deferred = 1; |
|
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
298 else |
|
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
299 notify (); |
| 998 | 300 break; |
| 958 | 301 case SIGTERM: |
| 998 | 302 fprintf (stderr, "Events still queued:\n"); |
| 303 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
|
304 fprintf (stderr, "%d = %ld @ %s\n", |
| 998 | 305 ep - events, ep->reply_at, ep->token); |
| 306 exit (0); | |
| 307 break; | |
| 958 | 308 } |
| 45 | 309 } |
| 958 | 310 |
| 45 | 311 /*ARGSUSED*/ |
| 312 int | |
| 998 | 313 main (argc, argv) |
| 45 | 314 int argc; |
| 315 char **argv; | |
| 316 { | |
| 998 | 317 for (pname = argv[0] + strlen (argv[0]); |
| 318 *pname != '/' && pname != argv[0]; | |
| 45 | 319 pname--); |
| 998 | 320 if (*pname == '/') |
| 321 pname++; | |
| 45 | 322 |
| 998 | 323 events_size = 16; |
| 324 events = ((struct event *) malloc (events_size * sizeof (*events))); | |
| 325 num_events = 0; | |
| 326 | |
| 327 signal (SIGALRM, sigcatch); | |
| 328 signal (SIGTERM, sigcatch); | |
| 958 | 329 |
|
5634
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
330 /* 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
|
331 and scheduling alarms accordingly. |
|
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
332 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
|
333 while (1) |
|
e77e9d7386be
Include errno.h; don't include fasync.h.
Richard M. Stallman <rms@gnu.org>
parents:
5571
diff
changeset
|
334 getevent (); |
| 45 | 335 } |
|
5527
51451a050975
[! HAVE_STRERROR] (strerror): Define the function.
Roland McGrath <roland@gnu.org>
parents:
5525
diff
changeset
|
336 |
|
51451a050975
[! HAVE_STRERROR] (strerror): Define the function.
Roland McGrath <roland@gnu.org>
parents:
5525
diff
changeset
|
337 #ifndef HAVE_STRERROR |
|
51451a050975
[! HAVE_STRERROR] (strerror): Define the function.
Roland McGrath <roland@gnu.org>
parents:
5525
diff
changeset
|
338 char * |
|
51451a050975
[! HAVE_STRERROR] (strerror): Define the function.
Roland McGrath <roland@gnu.org>
parents:
5525
diff
changeset
|
339 strerror (errnum) |
|
51451a050975
[! HAVE_STRERROR] (strerror): Define the function.
Roland McGrath <roland@gnu.org>
parents:
5525
diff
changeset
|
340 int errnum; |
|
51451a050975
[! HAVE_STRERROR] (strerror): Define the function.
Roland McGrath <roland@gnu.org>
parents:
5525
diff
changeset
|
341 { |
|
51451a050975
[! HAVE_STRERROR] (strerror): Define the function.
Roland McGrath <roland@gnu.org>
parents:
5525
diff
changeset
|
342 extern char *sys_errlist[]; |
|
51451a050975
[! HAVE_STRERROR] (strerror): Define the function.
Roland McGrath <roland@gnu.org>
parents:
5525
diff
changeset
|
343 extern int sys_nerr; |
|
51451a050975
[! HAVE_STRERROR] (strerror): Define the function.
Roland McGrath <roland@gnu.org>
parents:
5525
diff
changeset
|
344 |
|
51451a050975
[! HAVE_STRERROR] (strerror): Define the function.
Roland McGrath <roland@gnu.org>
parents:
5525
diff
changeset
|
345 if (errnum >= 0 && errnum < sys_nerr) |
|
51451a050975
[! HAVE_STRERROR] (strerror): Define the function.
Roland McGrath <roland@gnu.org>
parents:
5525
diff
changeset
|
346 return sys_errlist[errnum]; |
|
51451a050975
[! HAVE_STRERROR] (strerror): Define the function.
Roland McGrath <roland@gnu.org>
parents:
5525
diff
changeset
|
347 return (char *) "Unknown error"; |
|
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 |
|
51451a050975
[! HAVE_STRERROR] (strerror): Define the function.
Roland McGrath <roland@gnu.org>
parents:
5525
diff
changeset
|
350 #endif /* ! HAVE_STRERROR */ |
| 958 | 351 |
|
8241
a16dfe068972
(xmalloc): New function.
Richard M. Stallman <rms@gnu.org>
parents:
5634
diff
changeset
|
352 long * |
|
a16dfe068972
(xmalloc): New function.
Richard M. Stallman <rms@gnu.org>
parents:
5634
diff
changeset
|
353 xmalloc (size) |
|
a16dfe068972
(xmalloc): New function.
Richard M. Stallman <rms@gnu.org>
parents:
5634
diff
changeset
|
354 int size; |
|
a16dfe068972
(xmalloc): New function.
Richard M. Stallman <rms@gnu.org>
parents:
5634
diff
changeset
|
355 { |
|
a16dfe068972
(xmalloc): New function.
Richard M. Stallman <rms@gnu.org>
parents:
5634
diff
changeset
|
356 register long *val; |
|
a16dfe068972
(xmalloc): New function.
Richard M. Stallman <rms@gnu.org>
parents:
5634
diff
changeset
|
357 |
|
a16dfe068972
(xmalloc): New function.
Richard M. Stallman <rms@gnu.org>
parents:
5634
diff
changeset
|
358 val = (long *) malloc (size); |
|
a16dfe068972
(xmalloc): New function.
Richard M. Stallman <rms@gnu.org>
parents:
5634
diff
changeset
|
359 |
|
a16dfe068972
(xmalloc): New function.
Richard M. Stallman <rms@gnu.org>
parents:
5634
diff
changeset
|
360 if (!val && size) |
|
a16dfe068972
(xmalloc): New function.
Richard M. Stallman <rms@gnu.org>
parents:
5634
diff
changeset
|
361 { |
|
a16dfe068972
(xmalloc): New function.
Richard M. Stallman <rms@gnu.org>
parents:
5634
diff
changeset
|
362 fprintf (stderr, "timer: virtual memory exceeded\n"); |
|
a16dfe068972
(xmalloc): New function.
Richard M. Stallman <rms@gnu.org>
parents:
5634
diff
changeset
|
363 exit (1); |
|
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 |
|
a16dfe068972
(xmalloc): New function.
Richard M. Stallman <rms@gnu.org>
parents:
5634
diff
changeset
|
366 return val; |
|
a16dfe068972
(xmalloc): New function.
Richard M. Stallman <rms@gnu.org>
parents:
5634
diff
changeset
|
367 } |
|
a16dfe068972
(xmalloc): New function.
Richard M. Stallman <rms@gnu.org>
parents:
5634
diff
changeset
|
368 |
| 958 | 369 /* timer.c ends here */ |
