This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
metaconfig unit change for #13850.
[metaconfig.git] / U / compline / nblock_io.U
CommitLineData
959f3c4c
JH
1?RCS: $Id: nblock_io.U,v 3.0.1.2 1997/02/28 16:17:14 ram Exp $
2?RCS:
3?RCS: Copyright (c) 1991-1993, Raphael Manfredi
4?RCS:
5?RCS: You may redistribute only under the terms of the Artistic Licence,
6?RCS: as specified in the README file that comes with the distribution.
7?RCS: You may reuse parts of this distribution only within the terms of
8?RCS: that same Artistic Licence; a copy of which may be found at the root
9?RCS: of the source tree for dist 3.0.
10?RCS:
11?RCS: $Log: nblock_io.U,v $
12?RCS: Revision 3.0.1.2 1997/02/28 16:17:14 ram
13?RCS: patch61: simplify here document for shells that can't handle them well
14?RCS: patch61: force use of "startsh" at the head of the generated script
15?RCS: patch61: added new files to the ?F: metalint hint
16?RCS:
17?RCS: Revision 3.0.1.1 1995/07/25 14:13:22 ram
18?RCS: patch56: created
19?RCS:
20?X:
21?X: Simplify here document for shells that can't handle them well.
22?X: (Problem reported on FreeBSD; it's unclear if this helps.) --AD
23?X:
0f00356b 24?MAKE:o_nonblock eagain rd_nodata d_eofnblk: cat rm Compile run \
37a56d0d 25 d_open3 h_sysfile h_fcntl signal_t hint Oldconfig Setvar \
8411e286 26 startsh i_unistd i_string i_fcntl
959f3c4c
JH
27?MAKE: -pick add $@ %<
28?S:o_nonblock:
29?S: This variable bears the symbol value to be used during open() or fcntl()
30?S: to turn on non-blocking I/O for a file descriptor. If you wish to switch
31?S: between blocking and non-blocking, you may try ioctl(FIOSNBIO) instead,
32?S: but that is only supported by some devices.
33?S:.
34?S:eagain:
35?S: This variable bears the symbolic errno code set by read() when no
36?S: data is present on the file and non-blocking I/O was enabled (otherwise,
37?S: read() blocks naturally).
38?S:.
39?S:rd_nodata:
40?S: This variable holds the return code from read() when no data is
41?S: present. It should be -1, but some systems return 0 when O_NDELAY is
42?S: used, which is a shame because you cannot make the difference between
43?S: no data and an EOF.. Sigh!
44?S:.
45?S:d_eofnblk:
46?S: This variable conditionally defines EOF_NONBLOCK if EOF can be seen
47?S: when reading from a non-blocking I/O source.
48?S:.
49?C:VAL_O_NONBLOCK:
50?C: This symbol is to be used during open() or fcntl(F_SETFL) to turn on
51?C: non-blocking I/O for the file descriptor. Note that there is no way
52?C: back, i.e. you cannot turn it blocking again this way. If you wish to
53?C: alternatively switch between blocking and non-blocking, use the
54?C: ioctl(FIOSNBIO) call instead, but that is not supported by all devices.
55?C:.
56?C:VAL_EAGAIN:
57?C: This symbol holds the errno error code set by read() when no data was
58?C: present on the non-blocking file descriptor.
59?C:.
60?C:RD_NODATA:
61?C: This symbol holds the return code from read() when no data is present
62?C: on the non-blocking file descriptor. Be careful! If EOF_NONBLOCK is
63?C: not defined, then you can't distinguish between no data and EOF by
64?C: issuing a read(). You'll have to find another way to tell for sure!
65?C:.
66?C:EOF_NONBLOCK:
67?C: This symbol, if defined, indicates to the C program that a read() on
68?C: a non-blocking file descriptor will return 0 on EOF, and not the value
69?C: held in RD_NODATA (-1 usually, in that case!).
70?C:.
71?H:#define VAL_O_NONBLOCK $o_nonblock
72?H:#define VAL_EAGAIN $eagain
73?H:#define RD_NODATA $rd_nodata
74?H:#$d_eofnblk EOF_NONBLOCK
75?H:.
0f00356b 76?F:!try.out !try.ret !try.err
959f3c4c
JH
77?T:status
78?LINT:use d_open3
79: check for non-blocking I/O stuff
80case "$h_sysfile" in
cede5cd9
JH
81true) echo "#include <sys/file.h>" > head.c;;
82*)
83 case "$h_fcntl" in
84 true) echo "#include <fcntl.h>" > head.c;;
85 *) echo "#include <sys/fcntl.h>" > head.c;;
86 esac
87 ;;
959f3c4c
JH
88esac
89echo " "
90echo "Figuring out the flag used by open() for non-blocking I/O..." >&4
91case "$o_nonblock" in
92'')
93 $cat head.c > try.c
e8970d08 94 $cat >>try.c <<EOCP
37a56d0d 95#include <stdio.h>
d0abe53c 96#include <stdlib.h>
8411e286
JH
97#$i_fcntl I_FCNTL
98#ifdef I_FCNTL
99#include <fcntl.h>
100#endif
959f3c4c
JH
101int main() {
102#ifdef O_NONBLOCK
103 printf("O_NONBLOCK\n");
104 exit(0);
105#endif
106#ifdef O_NDELAY
107 printf("O_NDELAY\n");
108 exit(0);
109#endif
110?X: Stevens "Advanced Programming in the UNIX Environment" page 364 mentions
8411e286 111?X: the FNDELAY symbol, used in 4.3BSD (source: Paul Marquess).
959f3c4c
JH
112#ifdef FNDELAY
113 printf("FNDELAY\n");
114 exit(0);
115#endif
116 exit(0);
117}
118EOCP
119 set try
120 if eval $compile_ok; then
0f00356b 121 o_nonblock=`$run ./try`
959f3c4c
JH
122 case "$o_nonblock" in
123 '') echo "I can't figure it out, assuming O_NONBLOCK will do.";;
124 *) echo "Seems like we can use $o_nonblock.";;
125 esac
126 else
127 echo "(I can't compile the test program; pray O_NONBLOCK is right!)"
128 fi
129 ;;
130*) echo "Using $hint value $o_nonblock.";;
131esac
132$rm -f try try.* .out core
133
134echo " "
135echo "Let's see what value errno gets from read() on a $o_nonblock file..." >&4
136case "$eagain" in
137'')
138 $cat head.c > try.c
139 $cat >>try.c <<EOCP
140#include <errno.h>
141#include <sys/types.h>
142#include <signal.h>
37a56d0d 143#include <stdio.h>
d0abe53c 144#include <stdlib.h>
959f3c4c
JH
145#define MY_O_NONBLOCK $o_nonblock
146#ifndef errno /* XXX need better Configure test */
147extern int errno;
148#endif
b8fb7a32
JH
149#$i_unistd I_UNISTD
150#ifdef I_UNISTD
37a56d0d
JH
151#include <unistd.h>
152#endif
08b5bf24
JH
153#$i_string I_STRING
154#ifdef I_STRING
37a56d0d
JH
155#include <string.h>
156#else
157#include <strings.h>
158#endif
959f3c4c
JH
159$signal_t blech(x) int x; { exit(3); }
160EOCP
161 $cat >> try.c <<'EOCP'
162int main()
163{
164 int pd[2];
165 int pu[2];
166 char buf[1];
167 char string[100];
168
169 pipe(pd); /* Down: child -> parent */
170 pipe(pu); /* Up: parent -> child */
171 if (0 != fork()) {
172 int ret;
173 close(pd[1]); /* Parent reads from pd[0] */
174 close(pu[0]); /* Parent writes (blocking) to pu[1] */
cede5cd9 175#ifdef F_SETFL
959f3c4c
JH
176 if (-1 == fcntl(pd[0], F_SETFL, MY_O_NONBLOCK))
177 exit(1);
cede5cd9
JH
178#else
179 exit(4);
180#endif
959f3c4c
JH
181 signal(SIGALRM, blech);
182 alarm(5);
183 if ((ret = read(pd[0], buf, 1)) > 0) /* Nothing to read! */
184 exit(2);
185 sprintf(string, "%d\n", ret);
186 write(2, string, strlen(string));
187 alarm(0);
188#ifdef EAGAIN
189 if (errno == EAGAIN) {
190 printf("EAGAIN\n");
191 goto ok;
192 }
193#endif
194#ifdef EWOULDBLOCK
195 if (errno == EWOULDBLOCK)
196 printf("EWOULDBLOCK\n");
197#endif
198 ok:
199 write(pu[1], buf, 1); /* Unblocks child, tell it to close our pipe */
200 sleep(2); /* Give it time to close our pipe */
201 alarm(5);
202 ret = read(pd[0], buf, 1); /* Should read EOF */
203 alarm(0);
204 sprintf(string, "%d\n", ret);
205 write(3, string, strlen(string));
206 exit(0);
207 }
208
209 close(pd[0]); /* We write to pd[1] */
210 close(pu[1]); /* We read from pu[0] */
211 read(pu[0], buf, 1); /* Wait for parent to signal us we may continue */
212 close(pd[1]); /* Pipe pd is now fully closed! */
213 exit(0); /* Bye bye, thank you for playing! */
214}
215EOCP
216 set try
217 if eval $compile_ok; then
218?X: Use script to avoid the possible 'alarm call' message
219 echo "$startsh" >mtry
0f00356b 220 echo "$run ./try >try.out 2>try.ret 3>try.err || exit 4" >>mtry
959f3c4c
JH
221 chmod +x mtry
222 ./mtry >/dev/null 2>&1
223 case $? in
224 0) eagain=`$cat try.out`;;
225 1) echo "Could not perform non-blocking setting!";;
226 2) echo "I did a successful read() for something that was not there!";;
227 3) echo "Hmm... non-blocking I/O does not seem to be working!";;
cede5cd9 228 4) echo "Could not find F_SETFL!";;
959f3c4c
JH
229 *) echo "Something terribly wrong happened during testing.";;
230 esac
231 rd_nodata=`$cat try.ret`
232 echo "A read() system call with no data present returns $rd_nodata."
233 case "$rd_nodata" in
234 0|-1) ;;
235 *)
236 echo "(That's peculiar, fixing that to be -1.)"
237 rd_nodata=-1
238 ;;
239 esac
240 case "$eagain" in
241 '')
242 echo "Forcing errno EAGAIN on read() with no data available."
243 eagain=EAGAIN
244 ;;
245 *)
246 echo "Your read() sets errno to $eagain when no data is available."
247 ;;
248 esac
249 status=`$cat try.err`
250 case "$status" in
251 0) echo "And it correctly returns 0 to signal EOF.";;
252 -1) echo "But it also returns -1 to signal EOF, so be careful!";;
253 *) echo "However, your read() returns '$status' on EOF??";;
254 esac
255 val="$define"
256 if test "$status" = "$rd_nodata"; then
257 echo "WARNING: you can't distinguish between EOF and no data!"
258 val="$undef"
259 fi
260 else
261 echo "I can't compile the test program--assuming errno EAGAIN will do."
262 eagain=EAGAIN
263 fi
264 set d_eofnblk
265 eval $setvar
266 ;;
267*)
268 echo "Using $hint value $eagain."
269 echo "Your read() returns $rd_nodata when no data is present."
270 case "$d_eofnblk" in
271 "$define") echo "And you can see EOF because read() returns 0.";;
272 "$undef") echo "But you can't see EOF status from read() returned value.";;
273 *)
274?X: Should not happen, but if it does, assume the worst!
275 echo "(Assuming you can't see EOF status from read anyway.)"
276 d_eofnblk=$undef
277 ;;
278 esac
279 ;;
280esac
281$rm -f try try.* .out core head.c mtry
282