annotate lib-src/=timer.c @ 2895:1ed91566882a

* fileio.c (Ffind_file_name_handler): Check that FILENAME is a string.
author Jim Blandy <jimb@redhat.com>
date Wed, 19 May 1993 06:01:21 +0000
parents 37954061f933
children 0cab056764c8
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
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.
2813
89b1121e2d43 * timer.c: Fix mispellings of get_date function's name.
Jim Blandy <jimb@redhat.com>
parents: 2592
diff changeset
50 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
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 {
2813
89b1121e2d43 * timer.c: Fix mispellings of get_date function's name.
Jim Blandy <jimb@redhat.com>
parents: 2592
diff changeset
56 extern time_t get_date ();
998
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 {
1995
f149ad4ad9d4 * timer.c (notify): Initialize waitfor properly.
Jim Blandy <jimb@redhat.com>
parents: 1751
diff changeset
124 time_t now, tdiff, waitfor = -1;
998
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
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
127 /* 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
128 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
129 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
130 #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
131 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
132 #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
133
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
134 now = time ((time_t *) NULL);
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 for (ep = events; ep < events + num_events; ep++)
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
137 /* Are any events ready to fire? */
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
138 if (ep->reply_at <= now)
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
139 {
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
140 fputs (ep->token, stdout);
1750
2a92e870a448 Also, write a newline after the token.
Michael I. Bushnell <mib@gnu.org>
parents: 1749
diff changeset
141 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
142 fflush (stdout);
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
143 free (ep->token);
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
144
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
145 /* 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
146 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
147 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
148 ep->reply_at = events[num_events - 1].reply_at;
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
149 num_events--;
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 ought to scan this event again. */
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
152 ep--;
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
153 }
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
154 else
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
155 {
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
156 /* next timeout should be the soonest of any remaining */
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
157 if ((tdiff = ep->reply_at - now) < waitfor || waitfor < 0)
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
158 waitfor = (long)tdiff;
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
159 }
958
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
160
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
161 /* 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
162 if (num_events > 0)
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
163 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
164
2820
37954061f933 * timer.c (notify): Don't call sighold or sigrelse; they're USG
Jim Blandy <jimb@redhat.com>
parents: 2813
diff changeset
165 #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
166 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
167 #endif
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
168 }
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
169
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
170 void
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
171 getevent ()
958
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
172 {
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
173 int i;
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
174 char *buf;
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
175 int buf_size;
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 /* In principle the itimer should be disabled on entry to this
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
178 function, but it really doesn't make any important difference
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
179 if it isn't. */
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 = 80;
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
182 buf = (char *) malloc (buf_size);
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
183
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
184 /* 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
185 to hold the line. */
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
186 for (i = 0; ; i++)
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
187 {
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
188 int c;
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
189
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
190 if (i >= buf_size)
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 buf_size *= 2;
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
193 buf = (char *) realloc (buf, buf_size);
958
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
194
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
195 /* If we're out of memory, toss this event. */
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
196 do
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 c = getchar ();
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
199 }
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
200 while (c != '\n' && c != EOF);
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
201
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
202 return;
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
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
205 c = getchar ();
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 if (c == EOF)
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
208 exit (0);
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
209
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
210 if (c == '\n')
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 buf[i] = '\0';
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
213 break;
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
214 }
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 buf[i] = c;
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
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
219 /* Register the event. */
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
220 schedule (buf);
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
221 free (buf);
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
222
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
223 /* Who knows what this interrupted, or if it said "now"? */
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
224 notify ();
958
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
225 }
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
226
2102
b11495a4ecdf * timer.c (main): Set the ownership of the stdin file descriptor
Jim Blandy <jimb@redhat.com>
parents: 1995
diff changeset
227 SIGTYPE
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
228 sigcatch (sig)
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
229 int sig;
958
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
230 /* dispatch on incoming signal, then restore it */
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
231 {
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
232 struct event *ep;
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
233
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
234 switch (sig)
958
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
235 {
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
236 case SIGALRM:
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
237 notify ();
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
238 break;
958
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
239 case SIGIO:
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
240 getevent ();
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
241 break;
958
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
242 case SIGTERM:
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
243 fprintf (stderr, "Events still queued:\n");
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
244 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
245 fprintf (stderr, "%d = %ld @ %s\n",
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
246 ep - events, ep->reply_at, ep->token);
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
247 exit (0);
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
248 break;
958
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
249 }
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
250
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
251 /* required on older UNIXes; harmless on newer ones */
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
252 signal (sig, sigcatch);
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
253 }
958
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
254
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
255 /*ARGSUSED*/
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
256 int
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
257 main (argc, argv)
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
258 int argc;
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
259 char **argv;
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
260 {
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
261 for (pname = argv[0] + strlen (argv[0]);
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
262 *pname != '/' && pname != argv[0];
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
263 pname--);
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
264 if (*pname == '/')
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
265 pname++;
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
266
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
267 events_size = 16;
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
268 events = ((struct event *) malloc (events_size * sizeof (*events)));
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
269 num_events = 0;
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
270
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
271 signal (SIGIO, sigcatch);
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
272 signal (SIGALRM, sigcatch);
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 958
diff changeset
273 signal (SIGTERM, sigcatch);
958
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
274
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
275 #ifndef USG
2102
b11495a4ecdf * timer.c (main): Set the ownership of the stdin file descriptor
Jim Blandy <jimb@redhat.com>
parents: 1995
diff changeset
276 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
277 {
b11495a4ecdf * timer.c (main): Set the ownership of the stdin file descriptor
Jim Blandy <jimb@redhat.com>
parents: 1995
diff changeset
278 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
279 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
280 exit (1);
b11495a4ecdf * timer.c (main): Set the ownership of the stdin file descriptor
Jim Blandy <jimb@redhat.com>
parents: 1995
diff changeset
281 }
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_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
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 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
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 }
958
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
288 #endif /* USG */
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
289
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
290 for (;;)
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
291 pause ();
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
292 }
958
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
293
cc82116a8f1c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 621
diff changeset
294 /* timer.c ends here */