This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Fix goofups noticed by Mark Bixby and Jeff Okamoto.
[metaconfig.git] / U / compline / nblock_io.U
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:
24 ?MAKE:o_nonblock eagain rd_nodata d_eofnblk: cat rm Compile \
25         d_open3 h_sysfile h_fcntl signal_t hint Oldconfig Setvar \
26         startsh i_unistd i_string
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:.
76 ?F:!try !try.out !try.ret !try.err
77 ?T:status
78 ?LINT:use d_open3
79 : check for non-blocking I/O stuff
80 case "$h_sysfile" in
81 true) 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         ;;
88 esac
89 echo " "
90 echo "Figuring out the flag used by open() for non-blocking I/O..." >&4
91 case "$o_nonblock" in
92 '')
93         $cat head.c > try.c
94         $cat >>try.c <<'EOCP'
95 #include <stdio.h>
96 int main() {
97 #ifdef O_NONBLOCK
98         printf("O_NONBLOCK\n");
99         exit(0);
100 #endif
101 #ifdef O_NDELAY
102         printf("O_NDELAY\n");
103         exit(0);
104 #endif
105 ?X: Stevens "Advanced Programming in the UNIX Environment" page 364 mentions
106 ?X: the FNDELAY symbol, used in 4.33BSD (source: Paul Marquess).
107 #ifdef FNDELAY
108         printf("FNDELAY\n");
109         exit(0);
110 #endif
111         exit(0);
112 }
113 EOCP
114         set try
115         if eval $compile_ok; then
116                 o_nonblock=`./try`
117                 case "$o_nonblock" in
118                 '') echo "I can't figure it out, assuming O_NONBLOCK will do.";;
119                 *) echo "Seems like we can use $o_nonblock.";;
120                 esac
121         else
122                 echo "(I can't compile the test program; pray O_NONBLOCK is right!)"
123         fi
124         ;;
125 *) echo "Using $hint value $o_nonblock.";;
126 esac
127 $rm -f try try.* .out core
128
129 echo " "
130 echo "Let's see what value errno gets from read() on a $o_nonblock file..." >&4
131 case "$eagain" in
132 '')
133         $cat head.c > try.c
134         $cat >>try.c <<EOCP
135 #include <errno.h>
136 #include <sys/types.h>
137 #include <signal.h>
138 #include <stdio.h> 
139 #define MY_O_NONBLOCK $o_nonblock
140 #ifndef errno  /* XXX need better Configure test */
141 extern int errno;
142 #endif
143 #$i_unistd I_UNISTD
144 #ifdef I_UNISTD
145 #include <unistd.h>
146 #endif
147 #$i_string I_STRING
148 #ifdef I_STRING
149 #include <string.h>
150 #else
151 #include <strings.h>
152 #endif
153 $signal_t blech(x) int x; { exit(3); }
154 EOCP
155         $cat >> try.c <<'EOCP'
156 int main()
157 {
158         int pd[2];
159         int pu[2];
160         char buf[1];
161         char string[100];
162
163         pipe(pd);       /* Down: child -> parent */
164         pipe(pu);       /* Up: parent -> child */
165         if (0 != fork()) {
166                 int ret;
167                 close(pd[1]);   /* Parent reads from pd[0] */
168                 close(pu[0]);   /* Parent writes (blocking) to pu[1] */
169                 if (-1 == fcntl(pd[0], F_SETFL, MY_O_NONBLOCK))
170                         exit(1);
171                 signal(SIGALRM, blech);
172                 alarm(5);
173                 if ((ret = read(pd[0], buf, 1)) > 0)    /* Nothing to read! */
174                         exit(2);
175                 sprintf(string, "%d\n", ret);
176                 write(2, string, strlen(string));
177                 alarm(0);
178 #ifdef EAGAIN
179                 if (errno == EAGAIN) {
180                         printf("EAGAIN\n");
181                         goto ok;
182                 }
183 #endif
184 #ifdef EWOULDBLOCK
185                 if (errno == EWOULDBLOCK)
186                         printf("EWOULDBLOCK\n");
187 #endif
188         ok:
189                 write(pu[1], buf, 1);   /* Unblocks child, tell it to close our pipe */
190                 sleep(2);                               /* Give it time to close our pipe */
191                 alarm(5);
192                 ret = read(pd[0], buf, 1);      /* Should read EOF */
193                 alarm(0);
194                 sprintf(string, "%d\n", ret);
195                 write(3, string, strlen(string));
196                 exit(0);
197         }
198
199         close(pd[0]);                   /* We write to pd[1] */
200         close(pu[1]);                   /* We read from pu[0] */
201         read(pu[0], buf, 1);    /* Wait for parent to signal us we may continue */
202         close(pd[1]);                   /* Pipe pd is now fully closed! */
203         exit(0);                                /* Bye bye, thank you for playing! */
204 }
205 EOCP
206         set try
207         if eval $compile_ok; then
208 ?X: Use script to avoid the possible 'alarm call' message
209                 echo "$startsh" >mtry
210                 echo "./try >try.out 2>try.ret 3>try.err || exit 4" >>mtry
211                 chmod +x mtry
212                 ./mtry >/dev/null 2>&1
213                 case $? in
214                 0) eagain=`$cat try.out`;;
215                 1) echo "Could not perform non-blocking setting!";;
216                 2) echo "I did a successful read() for something that was not there!";;
217                 3) echo "Hmm... non-blocking I/O does not seem to be working!";;
218                 *) echo "Something terribly wrong happened during testing.";;
219                 esac
220                 rd_nodata=`$cat try.ret`
221                 echo "A read() system call with no data present returns $rd_nodata."
222                 case "$rd_nodata" in
223                 0|-1) ;;
224                 *)
225                         echo "(That's peculiar, fixing that to be -1.)"
226                         rd_nodata=-1
227                         ;;
228                 esac
229                 case "$eagain" in
230                 '')
231                         echo "Forcing errno EAGAIN on read() with no data available."
232                         eagain=EAGAIN
233                         ;;
234                 *)
235                         echo "Your read() sets errno to $eagain when no data is available."
236                         ;;
237                 esac
238                 status=`$cat try.err`
239                 case "$status" in
240                 0) echo "And it correctly returns 0 to signal EOF.";;
241                 -1) echo "But it also returns -1 to signal EOF, so be careful!";;
242                 *) echo "However, your read() returns '$status' on EOF??";;
243                 esac
244                 val="$define"
245                 if test "$status" = "$rd_nodata"; then
246                         echo "WARNING: you can't distinguish between EOF and no data!"
247                         val="$undef"
248                 fi
249         else
250                 echo "I can't compile the test program--assuming errno EAGAIN will do."
251                 eagain=EAGAIN
252         fi
253         set d_eofnblk
254         eval $setvar
255         ;;
256 *)
257         echo "Using $hint value $eagain."
258         echo "Your read() returns $rd_nodata when no data is present."
259         case "$d_eofnblk" in
260         "$define") echo "And you can see EOF because read() returns 0.";;
261         "$undef") echo "But you can't see EOF status from read() returned value.";;
262         *)
263 ?X: Should not happen, but if it does, assume the worst!
264                 echo "(Assuming you can't see EOF status from read anyway.)"
265                 d_eofnblk=$undef
266                 ;;
267         esac
268         ;;
269 esac
270 $rm -f try try.* .out core head.c mtry
271