annotate lib-src/=timer.c @ 1947:ba1e55a4ba76

(read_avail_input): Args to `kill' were backwards.
author Richard M. Stallman <rms@gnu.org>
date Tue, 23 Feb 1993 18:49:33 +0000
parents fac61b478a41
children f149ad4ad9d4
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
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
12 documented and rewritten for portability by esr@snark,thyrsus.com,
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
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
20 #include "../src/config.h"
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
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
26 extern int errno;
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
27 extern char *sys_errlist[], *malloc ();
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
28 extern time_t time ();
958
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
29
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
30 /*
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
31 * 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
32 * 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
33 * originally ';', then got changed to bogus `\001'.
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
34 */
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
35 #define FS '@'
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
36
958
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
37 struct event
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
38 {
958
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
39 char *token;
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
40 time_t reply_at;
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
41 };
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
42 int events_size; /* How many slots have we allocated? */
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
43 int num_events; /* How many are actually scheduled? */
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
44 struct event *events; /* events[0 .. num_events-1] are the
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
45 valid events. */
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
46
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
47 char *pname; /* programme name for error messages */
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
48
958
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
49 /* Accepts a string of two fields seperated by FS.
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
50 First field is string for getdate, saying when to wake-up.
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
51 Second field is a token to identify the request. */
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
52 void
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
53 schedule (str)
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
54 char *str;
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
55 {
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
56 extern time_t getdate ();
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
57 extern char *strcpy ();
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
58 time_t now;
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
59 register char *p;
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
60 static struct event *ep;
958
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
61
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
62 /* check entry format */
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
63 for (p = str; *p && *p != FS; p++)
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
64 continue;
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
65 if (!*p)
958
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
66 {
1751
fac61b478a41 Also, write a newline after the token.
Michael I. Bushnell <mib@gnu.org>
parents: 1750
diff changeset
67 fprintf (stderr, "%s: bad input format: %s\n", pname, str);
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
68 return;
958
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
69 }
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
70 *p++ = 0;
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
71
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
72 /* allocate an event slot */
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
73 ep = events + num_events;
958
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
74
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
75 /* 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
76 that ep will be pointing to an available event spot. */
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
77 if (ep == events + events_size)
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
78 {
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
79 int old_size = events_size;
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
80
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
81 events_size *= 2;
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
82 events = ((struct event *)
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
83 realloc (events, events_size * sizeof (struct event)));
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
84 if (! events)
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
85 {
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
86 fprintf (stderr, "%s: virtual memory exhausted.\n", pname);
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
87
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
88 /* Should timer exit now? Well, we've still got other
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
89 events in the queue, and more memory might become
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
90 available in the future, so we'll just toss this event.
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
91 This will screw up whoever scheduled the event, but
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
92 maybe someone else will survive. */
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
93 return;
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
94 }
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
95
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
96 while (old_size < events_size)
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
97 events[old_size++].token = NULL;
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
98 }
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
99
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
100 /* Don't allow users to schedule events in past time. */
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
101 ep->reply_at = get_date (str, NULL);
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
102 if (ep->reply_at - time (&now) < 0)
958
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
103 {
1751
fac61b478a41 Also, write a newline after the token.
Michael I. Bushnell <mib@gnu.org>
parents: 1750
diff changeset
104 fprintf (stderr, "%s: bad time spec: %s%c%s\n", pname, str, FS, p);
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
105 return;
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
106 }
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
107
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
108 /* save the event description */
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
109 ep->token = (char *) malloc ((unsigned) strlen (p) + 1);
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
110 if (! ep->token)
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
111 {
1751
fac61b478a41 Also, write a newline after the token.
Michael I. Bushnell <mib@gnu.org>
parents: 1750
diff changeset
112 fprintf (stderr, "%s: malloc %s: %s%c%s\n",
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
113 pname, sys_errlist[errno], str, FS, p);
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
114 return;
958
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
115 }
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
116
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
117 strcpy (ep->token, p);
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
118 num_events++;
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
119 }
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
120
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
121 void
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
122 notify ()
958
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
123 {
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
124 time_t now, tdiff, waitfor;
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
125 register struct event *ep;
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
126
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
127 now = time ((time_t *) NULL);
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
128
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
129 for (ep = events; ep < events + num_events; ep++)
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
130 /* Are any events ready to fire? */
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
131 if (ep->reply_at <= now)
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
132 {
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
133 fputs (ep->token, stdout);
1750
2a92e870a448 Also, write a newline after the token.
Michael I. Bushnell <mib@gnu.org>
parents: 1749
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
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
136 free (ep->token);
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
137
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
138 /* 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
139 event. */
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
140 ep->token = events[num_events].token;
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
141 ep->reply_at = events[num_events].reply_at;
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
142 num_events--;
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
143
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
144 /* We ought to scan this event again. */
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
145 ep--;
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
146 }
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
147 else
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
148 {
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
149 /* next timeout should be the soonest of any remaining */
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
150 if ((tdiff = ep->reply_at - now) < waitfor || waitfor < 0)
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
151 waitfor = (long)tdiff;
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
152 }
958
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
153
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
154 /* 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
155 if (num_events > 0)
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
156 alarm (waitfor);
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
157 }
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
158
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
159 void
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
160 getevent ()
958
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
161 {
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
162 int i;
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
163 char *buf;
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
164 int buf_size;
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
165
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
166 /* In principle the itimer should be disabled on entry to this
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
167 function, but it really doesn't make any important difference
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
168 if it isn't. */
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
169
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
170 buf_size = 80;
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
171 buf = (char *) malloc (buf_size);
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
172
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
173 /* 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
174 to hold the line. */
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
175 for (i = 0; ; i++)
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
176 {
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
177 int c;
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
178
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
179 if (i >= buf_size)
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
180 {
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
181 buf_size *= 2;
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
182 buf = (char *) realloc (buf, buf_size);
958
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
183
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
184 /* If we're out of memory, toss this event. */
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
185 do
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 c = getchar ();
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
188 }
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
189 while (c != '\n' && c != EOF);
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
190
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
191 return;
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
192 }
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 c = getchar ();
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 (c == EOF)
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
197 exit (0);
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
198
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
199 if (c == '\n')
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
200 {
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
201 buf[i] = '\0';
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
202 break;
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
203 }
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
204
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
205 buf[i] = c;
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
206 }
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 /* Register the event. */
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
209 schedule (buf);
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
210 free (buf);
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
211
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
212 /* Who knows what this interrupted, or if it said "now"? */
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
213 notify ();
958
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
214 }
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
215
958
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
216 void
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
217 sigcatch (sig)
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
218 int sig;
958
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
219 /* dispatch on incoming signal, then restore it */
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
220 {
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
221 struct event *ep;
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
222
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
223 switch (sig)
958
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
224 {
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
225 case SIGALRM:
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
226 notify ();
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
227 break;
958
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
228 case SIGIO:
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
229 getevent ();
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
230 break;
958
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
231 case SIGTERM:
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
232 fprintf (stderr, "Events still queued:\n");
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
233 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
234 fprintf (stderr, "%d = %ld @ %s\n",
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
235 ep - events, ep->reply_at, ep->token);
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
236 exit (0);
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
237 break;
958
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
238 }
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 /* required on older UNIXes; harmless on newer ones */
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
241 signal (sig, sigcatch);
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
242 }
958
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
243
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
244 /*ARGSUSED*/
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
245 int
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
246 main (argc, argv)
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
247 int argc;
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
248 char **argv;
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
249 {
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
250 for (pname = argv[0] + strlen (argv[0]);
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
251 *pname != '/' && pname != argv[0];
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
252 pname--);
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
253 if (*pname == '/')
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
254 pname++;
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
255
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
256 events_size = 16;
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
257 events = ((struct event *) malloc (events_size * sizeof (*events)));
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
258 num_events = 0;
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
259
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
260 signal (SIGIO, sigcatch);
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
261 signal (SIGALRM, sigcatch);
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
262 signal (SIGTERM, sigcatch);
958
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
263
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
264 #ifndef USG
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
265 fcntl (0, F_SETFL, FASYNC);
958
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
266 #endif /* USG */
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
267
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
268 while (1) pause ();
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
269 }
958
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
270
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
271 /* timer.c ends here */