annotate lib-src/=timer.c @ 4774:8e36034f65e2

(doprnt): Use a fixed buffer to store the format specification, but only if we are sure it will fit. Otherwise, use alloca () to get enouhg space. Don't allow negative size specifications to core dump; instead, treat them as positive ("%-20d" == "%20d").
author Brian Fox <bfox@gnu.org>
date Wed, 22 Sep 1993 18:12:09 +0000
parents 1fc792473491
children 1d84e80b47a4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
1 /* timer.c --- daemon to provide a tagged interval timer service
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
2
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
3 This little daemon runs forever waiting for signals. SIGIO (or
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
4 SIGUSR1) causes it to read an event spec from stdin; that is, a
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
5 date followed by colon followed by an event label. SIGALRM causes
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
6 it to check its queue for events attached to the current second; if
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
7 one is found, its label is written to stdout. SIGTERM causes it to
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
8 terminate, printing a list of pending events.
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
9
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
10 This program is intended to be used with the lisp package called
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
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
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
13 Aug 7 1992. */
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
14
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
15 #include <stdio.h>
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
16 #include <signal.h>
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
17 #include <fcntl.h> /* FASYNC */
958
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
18 #include <sys/types.h> /* time_t */
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
19
4696
1fc792473491 Include <config.h> instead of "config.h".
Roland McGrath <roland@gnu.org>
parents: 4392
diff changeset
20 #include <../src/config.h>
958
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
21 #ifdef USG
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
22 #undef SIGIO
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
23 #define SIGIO SIGUSR1
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
24 #endif
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
25
3392
0cab056764c8 [LINUX]: #undef signal.
Richard M. Stallman <rms@gnu.org>
parents: 2820
diff changeset
26 #ifdef LINUX
0cab056764c8 [LINUX]: #undef signal.
Richard M. Stallman <rms@gnu.org>
parents: 2820
diff changeset
27 /* Perhaps this is correct unconditionally. */
0cab056764c8 [LINUX]: #undef signal.
Richard M. Stallman <rms@gnu.org>
parents: 2820
diff changeset
28 #undef signal
0cab056764c8 [LINUX]: #undef signal.
Richard M. Stallman <rms@gnu.org>
parents: 2820
diff changeset
29 #endif
0cab056764c8 [LINUX]: #undef signal.
Richard M. Stallman <rms@gnu.org>
parents: 2820
diff changeset
30
0cab056764c8 [LINUX]: #undef signal.
Richard M. Stallman <rms@gnu.org>
parents: 2820
diff changeset
31
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
32 extern int errno;
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
33 extern char *sys_errlist[], *malloc ();
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
34 extern time_t time ();
958
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
35
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
36 /*
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
37 * The field separator for input. This character shouldn't be legal in a date,
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
38 * and should be printable so event strings are readable by people. Was
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
39 * originally ';', then got changed to bogus `\001'.
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
40 */
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
41 #define FS '@'
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
42
958
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
43 struct event
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
44 {
958
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
45 char *token;
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
46 time_t reply_at;
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
47 };
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
48 int events_size; /* How many slots have we allocated? */
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
49 int num_events; /* How many are actually scheduled? */
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
50 struct event *events; /* events[0 .. num_events-1] are the
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
51 valid events. */
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
52
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
53 char *pname; /* programme name for error messages */
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
54
3591
507f64624555 Apply typo patches from Paul Eggert.
Jim Blandy <jimb@redhat.com>
parents: 3392
diff changeset
55 /* Accepts 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
56 First field is string for get_date, saying when to wake-up.
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
57 Second field is a token to identify the request. */
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
58 void
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
59 schedule (str)
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
60 char *str;
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
61 {
2813
89b1121e2d43 * timer.c: Fix mispellings of get_date function's name.
Jim Blandy <jimb@redhat.com>
parents: 2592
diff changeset
62 extern time_t get_date ();
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
63 extern char *strcpy ();
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
64 time_t now;
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
65 register char *p;
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
66 static struct event *ep;
958
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
67
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
68 /* check entry format */
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
69 for (p = str; *p && *p != FS; p++)
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
70 continue;
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
71 if (!*p)
958
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
72 {
1751
fac61b478a41 Also, write a newline after the token.
Michael I. Bushnell <mib@gnu.org>
parents: 1750
diff changeset
73 fprintf (stderr, "%s: bad input format: %s\n", pname, str);
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
74 return;
958
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
75 }
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
76 *p++ = 0;
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
77
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
78 /* allocate an event slot */
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
79 ep = events + num_events;
958
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
80
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
81 /* If the event array is full, stretch it. After stretching, we know
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
82 that ep will be pointing to an available event spot. */
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
83 if (ep == events + events_size)
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
84 {
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
85 int old_size = events_size;
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
86
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
87 events_size *= 2;
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
88 events = ((struct event *)
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
89 realloc (events, events_size * sizeof (struct event)));
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
90 if (! events)
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
91 {
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
92 fprintf (stderr, "%s: virtual memory exhausted.\n", pname);
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
93
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
94 /* Should timer exit now? Well, we've still got other
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
95 events in the queue, and more memory might become
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
96 available in the future, so we'll just toss this event.
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
97 This will screw up whoever scheduled the event, but
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
98 maybe someone else will survive. */
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
99 return;
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
100 }
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
101
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
102 while (old_size < events_size)
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
103 events[old_size++].token = NULL;
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
104 }
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
105
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
106 /* Don't allow users to schedule events in past time. */
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
107 ep->reply_at = get_date (str, NULL);
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
108 if (ep->reply_at - time (&now) < 0)
958
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
109 {
1751
fac61b478a41 Also, write a newline after the token.
Michael I. Bushnell <mib@gnu.org>
parents: 1750
diff changeset
110 fprintf (stderr, "%s: bad time spec: %s%c%s\n", pname, str, FS, p);
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
111 return;
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
112 }
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
113
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
114 /* save the event description */
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
115 ep->token = (char *) malloc ((unsigned) strlen (p) + 1);
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
116 if (! ep->token)
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
117 {
1751
fac61b478a41 Also, write a newline after the token.
Michael I. Bushnell <mib@gnu.org>
parents: 1750
diff changeset
118 fprintf (stderr, "%s: malloc %s: %s%c%s\n",
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
119 pname, sys_errlist[errno], str, FS, p);
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
120 return;
958
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
121 }
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
122
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
123 strcpy (ep->token, p);
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
124 num_events++;
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
125 }
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
126
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
127 void
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
128 notify ()
958
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
129 {
1995
f149ad4ad9d4 * timer.c (notify): Initialize waitfor properly.
Jim Blandy <jimb@redhat.com>
parents: 1751
diff changeset
130 time_t now, tdiff, waitfor = -1;
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
131 register struct event *ep;
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
132
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
133 /* 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
134 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
135 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
136 #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
137 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
138 #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
139
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
140 now = time ((time_t *) NULL);
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
141
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
142 for (ep = events; ep < events + num_events; ep++)
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
143 /* Are any events ready to fire? */
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
144 if (ep->reply_at <= now)
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
145 {
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
146 fputs (ep->token, stdout);
1750
2a92e870a448 Also, write a newline after the token.
Michael I. Bushnell <mib@gnu.org>
parents: 1749
diff changeset
147 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
148 fflush (stdout);
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
149 free (ep->token);
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
150
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
151 /* We now have a hole in the event array; fill it with the last
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
152 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
153 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
154 ep->reply_at = events[num_events - 1].reply_at;
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
155 num_events--;
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
156
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
157 /* We ought to scan this event again. */
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
158 ep--;
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
159 }
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
160 else
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
161 {
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
162 /* next timeout should be the soonest of any remaining */
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
163 if ((tdiff = ep->reply_at - now) < waitfor || waitfor < 0)
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
164 waitfor = (long)tdiff;
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
165 }
958
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
166
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
167 /* If there are no more events, we needn't bother setting an alarm. */
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
168 if (num_events > 0)
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
169 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
170
2820
37954061f933 * timer.c (notify): Don't call sighold or sigrelse; they're USG
Jim Blandy <jimb@redhat.com>
parents: 2813
diff changeset
171 #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
172 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
173 #endif
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
174 }
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
175
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
176 void
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
177 getevent ()
958
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
178 {
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
179 int i;
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
180 char *buf;
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
181 int buf_size;
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
182
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
183 /* In principle the itimer should be disabled on entry to this
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
184 function, but it really doesn't make any important difference
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
185 if it isn't. */
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
186
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
187 buf_size = 80;
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
188 buf = (char *) malloc (buf_size);
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
189
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
190 /* Read a line from standard input, expanding buf if it is too short
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
191 to hold the line. */
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
192 for (i = 0; ; i++)
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
193 {
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
194 int c;
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
195
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
196 if (i >= buf_size)
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
197 {
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
198 buf_size *= 2;
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
199 buf = (char *) realloc (buf, buf_size);
958
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
200
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
201 /* If we're out of memory, toss this event. */
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
202 do
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
203 {
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
204 c = getchar ();
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
205 }
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
206 while (c != '\n' && c != EOF);
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
207
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
208 return;
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
209 }
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
210
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
211 c = getchar ();
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
212
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
213 if (c == EOF)
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
214 exit (0);
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
215
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
216 if (c == '\n')
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
217 {
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
218 buf[i] = '\0';
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
219 break;
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
220 }
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
221
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
222 buf[i] = c;
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
223 }
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
224
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
225 /* Register the event. */
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
226 schedule (buf);
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
227 free (buf);
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
228
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
229 /* Who knows what this interrupted, or if it said "now"? */
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
230 notify ();
958
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
231 }
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
232
2102
b11495a4ecdf * timer.c (main): Set the ownership of the stdin file descriptor
Jim Blandy <jimb@redhat.com>
parents: 1995
diff changeset
233 SIGTYPE
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
234 sigcatch (sig)
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
235 int sig;
958
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
236 /* dispatch on incoming signal, then restore it */
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
237 {
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
238 struct event *ep;
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
239
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
240 switch (sig)
958
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
241 {
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
242 case SIGALRM:
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
243 notify ();
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
244 break;
958
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
245 case SIGIO:
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
246 getevent ();
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
247 break;
958
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
248 case SIGTERM:
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
249 fprintf (stderr, "Events still queued:\n");
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
250 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
251 fprintf (stderr, "%d = %ld @ %s\n",
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
252 ep - events, ep->reply_at, ep->token);
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
253 exit (0);
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
254 break;
958
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
255 }
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
256
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
257 /* required on older UNIXes; harmless on newer ones */
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
258 signal (sig, sigcatch);
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
259 }
958
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
260
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
261 /*ARGSUSED*/
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
262 int
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
263 main (argc, argv)
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
264 int argc;
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
265 char **argv;
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
266 {
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
267 for (pname = argv[0] + strlen (argv[0]);
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
268 *pname != '/' && pname != argv[0];
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
269 pname--);
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
270 if (*pname == '/')
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
271 pname++;
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
272
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
273 events_size = 16;
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
274 events = ((struct event *) malloc (events_size * sizeof (*events)));
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
275 num_events = 0;
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
276
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
277 signal (SIGIO, sigcatch);
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
278 signal (SIGALRM, sigcatch);
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
279 signal (SIGTERM, sigcatch);
958
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
280
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
281 #ifndef USG
2102
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_SETOWN, getpid ()) == -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 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
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 }
b11495a4ecdf * timer.c (main): Set the ownership of the stdin file descriptor
Jim Blandy <jimb@redhat.com>
parents: 1995
diff changeset
288 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
289 {
b11495a4ecdf * timer.c (main): Set the ownership of the stdin file descriptor
Jim Blandy <jimb@redhat.com>
parents: 1995
diff changeset
290 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
291 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
292 exit (1);
b11495a4ecdf * timer.c (main): Set the ownership of the stdin file descriptor
Jim Blandy <jimb@redhat.com>
parents: 1995
diff changeset
293 }
958
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
294 #endif /* USG */
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
295
4392
b64b1b80f371 (main): Generate a SIGIO as soon as we've initialized.
Richard M. Stallman <rms@gnu.org>
parents: 3591
diff changeset
296 /* In case Emacs sent some input before we set up
b64b1b80f371 (main): Generate a SIGIO as soon as we've initialized.
Richard M. Stallman <rms@gnu.org>
parents: 3591
diff changeset
297 the handling of SIGIO, read it now. */
b64b1b80f371 (main): Generate a SIGIO as soon as we've initialized.
Richard M. Stallman <rms@gnu.org>
parents: 3591
diff changeset
298 kill (0, SIGIO);
b64b1b80f371 (main): Generate a SIGIO as soon as we've initialized.
Richard M. Stallman <rms@gnu.org>
parents: 3591
diff changeset
299
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
300 for (;;)
4392
b64b1b80f371 (main): Generate a SIGIO as soon as we've initialized.
Richard M. Stallman <rms@gnu.org>
parents: 3591
diff changeset
301 pause ();
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
302 }
958
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
303
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
304 /* timer.c ends here */