annotate lib-src/=timer.c @ 1498:098464e977d6

(preserved_fns): New var. (Fcall_interactively): Preserve fns listed in preserved_fns when they appear in an interactive spec which is a call to `list'. (syms_of_callint): Set preserved_fns and staticpro it.
author Richard M. Stallman <rms@gnu.org>
date Fri, 30 Oct 1992 06:01:13 +0000
parents 61c6983219ff
children 7c4fc10fde41
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 {
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
67 fprintf (stderr, "%s: bad input format: %s", pname, str);
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 {
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
104 fprintf (stderr, "%s: bad time spec: %s%c%s", pname, str, FS, p);
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 {
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
112 fprintf (stderr, "%s: malloc %s: %s%c%s",
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);
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
134 free (ep->token);
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
135
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
136 /* 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
137 event. */
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
138 ep->token = events[num_events].token;
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
139 ep->reply_at = events[num_events].reply_at;
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
140 num_events--;
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 /* We ought to scan this event again. */
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
143 ep--;
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
144 }
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
145 else
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 /* next timeout should be the soonest of any remaining */
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
148 if ((tdiff = ep->reply_at - now) < waitfor || waitfor < 0)
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
149 waitfor = (long)tdiff;
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
150 }
958
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
151
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
152 /* 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
153 if (num_events > 0)
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
154 alarm (waitfor);
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
155 }
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
156
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
157 void
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
158 getevent ()
958
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
159 {
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
160 int i;
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
161 char *buf;
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
162 int buf_size;
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
163
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
164 /* In principle the itimer should be disabled on entry to this
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
165 function, but it really doesn't make any important difference
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
166 if it isn't. */
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
167
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
168 buf_size = 80;
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
169 buf = (char *) malloc (buf_size);
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
170
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
171 /* 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
172 to hold the line. */
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
173 for (i = 0; ; i++)
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
174 {
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
175 int c;
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 if (i >= buf_size)
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 buf_size *= 2;
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
180 buf = (char *) realloc (buf, buf_size);
958
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
181
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
182 /* If we're out of memory, toss this event. */
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
183 do
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
184 {
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
185 c = getchar ();
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 while (c != '\n' && c != EOF);
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 return;
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
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
192 c = getchar ();
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 if (c == EOF)
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
195 exit (0);
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
196
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
197 if (c == '\n')
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
198 {
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
199 buf[i] = '\0';
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
200 break;
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
201 }
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
202
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
203 buf[i] = c;
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
204 }
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 /* Register the event. */
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
207 schedule (buf);
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
208 free (buf);
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 /* Who knows what this interrupted, or if it said "now"? */
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
211 notify ();
958
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
212 }
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
213
958
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
214 void
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
215 sigcatch (sig)
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
216 int sig;
958
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
217 /* dispatch on incoming signal, then restore it */
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
218 {
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
219 struct event *ep;
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
220
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
221 switch (sig)
958
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
222 {
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
223 case SIGALRM:
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
224 notify ();
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
225 break;
958
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
226 case SIGIO:
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
227 getevent ();
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
228 break;
958
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
229 case SIGTERM:
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
230 fprintf (stderr, "Events still queued:\n");
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
231 for (ep = events; ep < events + num_events; ep++)
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
232 fprintf (stderr, "%d = %ld @ %s",
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
233 ep - events, ep->reply_at, ep->token);
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
234 exit (0);
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
235 break;
958
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
236 }
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
237
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
238 /* required on older UNIXes; harmless on newer ones */
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
239 signal (sig, sigcatch);
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
240 }
958
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
241
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
242 /*ARGSUSED*/
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
243 int
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
244 main (argc, argv)
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
245 int argc;
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
246 char **argv;
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
247 {
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
248 for (pname = argv[0] + strlen (argv[0]);
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
249 *pname != '/' && pname != argv[0];
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
250 pname--);
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
251 if (*pname == '/')
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
252 pname++;
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
253
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
254 events_size = 16;
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
255 events = ((struct event *) malloc (events_size * sizeof (*events)));
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
256 num_events = 0;
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
257
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
258 signal (SIGIO, sigcatch);
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
259 signal (SIGALRM, sigcatch);
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
260 signal (SIGTERM, sigcatch);
958
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
261
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
262 #ifndef USG
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
263 fcntl (0, F_SETFL, FASYNC);
958
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
264 #endif /* USG */
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
265
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
266 while (1) pause ();
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
267 }
958
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
268
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
269 /* timer.c ends here */