Mercurial > emacs
annotate lib-src/=timer.c @ 1749:dbb15c2c5823
timer.c (notify): Flush stdout after writing message to avoid lossage
on terminals.
| author | Michael I. Bushnell <mib@gnu.org> |
|---|---|
| date | Fri, 08 Jan 1993 20:36:14 +0000 |
| parents | 7c4fc10fde41 |
| children | 2a92e870a448 |
| 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 | |
| 12 documented and rewritten for portability by esr@snark,thyrsus.com, | |
| 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. |
| 998 | 50 First field is string for getdate, saying when to wake-up. |
| 51 Second field is a token to identify the request. */ | |
| 52 void | |
| 53 schedule (str) | |
| 54 char *str; | |
| 45 | 55 { |
| 998 | 56 extern time_t getdate (); |
| 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 { |
| 998 | 67 fprintf (stderr, "%s: bad input format: %s", pname, str); |
| 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 { |
| 998 | 104 fprintf (stderr, "%s: bad time spec: %s%c%s", pname, str, FS, p); |
| 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 { | |
| 112 fprintf (stderr, "%s: malloc %s: %s%c%s", | |
| 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 { |
| 998 | 124 time_t now, tdiff, waitfor; |
| 125 register struct event *ep; | |
| 126 | |
| 127 now = time ((time_t *) NULL); | |
| 45 | 128 |
| 998 | 129 for (ep = events; ep < events + num_events; ep++) |
| 130 /* Are any events ready to fire? */ | |
| 131 if (ep->reply_at <= now) | |
| 132 { | |
| 133 fputs (ep->token, stdout); | |
|
1749
dbb15c2c5823
timer.c (notify): Flush stdout after writing message to avoid lossage
Michael I. Bushnell <mib@gnu.org>
parents:
1746
diff
changeset
|
134 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
|
135 fflush (stdout); |
| 998 | 136 free (ep->token); |
| 45 | 137 |
| 998 | 138 /* We now have a hole in the event array; fill it with the last |
| 139 event. */ | |
| 140 ep->token = events[num_events].token; | |
| 141 ep->reply_at = events[num_events].reply_at; | |
| 142 num_events--; | |
| 45 | 143 |
| 998 | 144 /* We ought to scan this event again. */ |
| 145 ep--; | |
| 146 } | |
| 147 else | |
| 148 { | |
| 149 /* next timeout should be the soonest of any remaining */ | |
| 150 if ((tdiff = ep->reply_at - now) < waitfor || waitfor < 0) | |
| 151 waitfor = (long)tdiff; | |
| 152 } | |
| 958 | 153 |
| 998 | 154 /* If there are no more events, we needn't bother setting an alarm. */ |
| 155 if (num_events > 0) | |
| 156 alarm (waitfor); | |
| 45 | 157 } |
| 158 | |
| 159 void | |
| 998 | 160 getevent () |
| 958 | 161 { |
| 998 | 162 int i; |
| 163 char *buf; | |
| 164 int buf_size; | |
| 165 | |
| 166 /* In principle the itimer should be disabled on entry to this | |
| 167 function, but it really doesn't make any important difference | |
| 168 if it isn't. */ | |
| 169 | |
| 170 buf_size = 80; | |
| 171 buf = (char *) malloc (buf_size); | |
| 45 | 172 |
| 998 | 173 /* Read a line from standard input, expanding buf if it is too short |
| 174 to hold the line. */ | |
| 175 for (i = 0; ; i++) | |
| 176 { | |
| 177 int c; | |
| 178 | |
| 179 if (i >= buf_size) | |
| 180 { | |
| 181 buf_size *= 2; | |
| 182 buf = (char *) realloc (buf, buf_size); | |
| 958 | 183 |
| 998 | 184 /* If we're out of memory, toss this event. */ |
| 185 do | |
| 186 { | |
| 187 c = getchar (); | |
| 188 } | |
| 189 while (c != '\n' && c != EOF); | |
| 190 | |
| 191 return; | |
| 192 } | |
| 193 | |
| 194 c = getchar (); | |
| 195 | |
| 196 if (c == EOF) | |
| 197 exit (0); | |
| 45 | 198 |
| 998 | 199 if (c == '\n') |
| 200 { | |
| 201 buf[i] = '\0'; | |
| 202 break; | |
| 203 } | |
| 45 | 204 |
| 998 | 205 buf[i] = c; |
| 206 } | |
| 207 | |
| 208 /* Register the event. */ | |
| 209 schedule (buf); | |
| 210 free (buf); | |
| 211 | |
| 212 /* Who knows what this interrupted, or if it said "now"? */ | |
| 213 notify (); | |
| 958 | 214 } |
| 45 | 215 |
| 958 | 216 void |
| 998 | 217 sigcatch (sig) |
| 218 int sig; | |
| 958 | 219 /* dispatch on incoming signal, then restore it */ |
| 220 { | |
| 998 | 221 struct event *ep; |
| 45 | 222 |
| 998 | 223 switch (sig) |
| 958 | 224 { |
| 225 case SIGALRM: | |
| 998 | 226 notify (); |
| 227 break; | |
| 958 | 228 case SIGIO: |
| 998 | 229 getevent (); |
| 230 break; | |
| 958 | 231 case SIGTERM: |
| 998 | 232 fprintf (stderr, "Events still queued:\n"); |
| 233 for (ep = events; ep < events + num_events; ep++) | |
| 234 fprintf (stderr, "%d = %ld @ %s", | |
| 235 ep - events, ep->reply_at, ep->token); | |
| 236 exit (0); | |
| 237 break; | |
| 958 | 238 } |
| 45 | 239 |
| 998 | 240 /* required on older UNIXes; harmless on newer ones */ |
| 241 signal (sig, sigcatch); | |
| 45 | 242 } |
| 958 | 243 |
| 45 | 244 /*ARGSUSED*/ |
| 245 int | |
| 998 | 246 main (argc, argv) |
| 45 | 247 int argc; |
| 248 char **argv; | |
| 249 { | |
| 998 | 250 for (pname = argv[0] + strlen (argv[0]); |
| 251 *pname != '/' && pname != argv[0]; | |
| 45 | 252 pname--); |
| 998 | 253 if (*pname == '/') |
| 254 pname++; | |
| 45 | 255 |
| 998 | 256 events_size = 16; |
| 257 events = ((struct event *) malloc (events_size * sizeof (*events))); | |
| 258 num_events = 0; | |
| 259 | |
| 260 signal (SIGIO, sigcatch); | |
| 261 signal (SIGALRM, sigcatch); | |
| 262 signal (SIGTERM, sigcatch); | |
| 958 | 263 |
| 264 #ifndef USG | |
| 998 | 265 fcntl (0, F_SETFL, FASYNC); |
| 958 | 266 #endif /* USG */ |
| 45 | 267 |
| 998 | 268 while (1) pause (); |
| 45 | 269 } |
| 958 | 270 |
| 271 /* timer.c ends here */ |
