Debian Codegraph @

IO/IO.xs

raw
1 /*
2 Copyright (c) 1995-2004 Nick Ing-Simmons. All rights reserved.
3 This program is free software; you can redistribute it and/or
4 modify it under the same terms as Perl itself.
5 */
6 #define PERL_NO_GET_CONTEXT
7 #include <EXTERN.h>
8 #include <perl.h>
9 #include <XSUB.h>
10 #include <patchlevel.h>
11 #include <fcntl.h>
12
13 #include "tkGlue.def"
14
15 #include "pTk/tkPort.h"
16 #include "pTk/tkInt.h"
17 #include "pTk/tkEvent.h"
18 #include "pTk/tkEvent_f.h"
19 #include "pTk/tkEvent.m"
20 #include "tkGlue.h"
21 #include "tkGlue.m"
22
23 DECLARE_EVENT;
24
25 #define InputStream PerlIO *
26 #define OutputStream PerlIO *
27
28
29 typedef struct
30 {
31 PerlIO *f;
32 SV *buf;
33 int len;
34 int offset;
35 int count;
36 int error;
37 int eof;
38 } nIO_read;
39
40 static void read_handler _((ClientData clientData, int mask));
41 static void
42 read_handler(clientData, mask)
43 ClientData clientData;
44 int mask;
45 {
46 dTHX; /* FIXME */
47 if (mask & TCL_READABLE)
48 {
49 nIO_read *info = (nIO_read *) clientData;
50 SV *buf = info->buf;
51 int count;
52 SvGROW(buf,(Size_t) (info->offset+info->len+1));
53 count = read(PerlIO_fileno(info->f),SvPVX(buf)+info->offset,(size_t) info->len);
54 if (count == 0)
55 {
56 info->eof = 1;
57 }
58 else if (count == -1)
59 {
60 perror("read_handler");
61 if (errno == EAGAIN)
62 {
63 PerlIO_printf(PerlIO_stderr(),"%d would block\n",PerlIO_fileno(info->f));
64 }
65 else
66 info->error = errno;
67 }
68 else
69 {STRLEN len;
70 SvCUR_set(buf,SvCUR(buf)+count);
71 info->len -= count;
72 info->count += count;
73 info->offset += count;
74 }
75 SvPVX(buf)[SvCUR(buf)] = '\0';
76 }
77 }
78
79
80 #if defined(__WIN32__) && !defined(__CYGWIN__)
81 static int
82 make_nonblock (pTHX_ PerlIO *f,int *mode,int *newmode)
83 {
84 croak("Cannot make nonblocking on Win32 yet");
85 return -1;
86 }
87
88 static int
89 restore_mode (pTHX_ PerlIO *f,int mode)
90 {
91 croak("Cannot make nonblocking on Win32 yet");
92 return -1;
93 }
94 #else
95 static int
96 make_nonblock (pTHX_ PerlIO *f,int *mode,int *newmode)
97 {
98 int RETVAL = fcntl(PerlIO_fileno(f), F_GETFL, 0);
99 if (RETVAL >= 0)
100 {
101 *newmode = *mode = RETVAL;
102 #ifdef O_NONBLOCK
103 /* POSIX style */
104 #ifdef O_NDELAY
105 /* Ooops has O_NDELAY too - make sure we don't
106 * get SysV behaviour by mistake
107 */
108 if ((*mode & O_NDELAY) || !(*mode & O_NONBLOCK))
109 {
110 *newmode = (*mode & ~O_NDELAY) | O_NONBLOCK;
111 RETVAL = fcntl(PerlIO_fileno(f),F_SETFL,*newmode);
112 }
113 #else
114 /* Standard POSIX */
115 if (!(*mode & O_NONBLOCK))
116 {
117 *newmode = *mode | O_NONBLOCK;
118 RETVAL = fcntl(PerlIO_fileno(f),F_SETFL,*newmode);
119 }
120 #endif
121 #else
122 /* Not POSIX - better have O_NDELAY or we can't cope.
123 * for BSD-ish machines this is an acceptable alternative
124 * for SysV we can't tell "would block" from EOF but that is
125 * the way SysV is...
126 */
127 if (!(*mode & O_NDELAY))
128 {
129 *newmode = *mode | O_NDELAY;
130 RETVAL = fcntl(PerlIO_fileno(f),F_SETFL,*newmode);
131 }
132 #endif
133 }
134 return RETVAL;
135 }
136
137 static int
138 restore_mode (pTHX_ PerlIO *f,int mode)
139 {
140 return fcntl(PerlIO_fileno(f), F_SETFL, mode);
141 }
142
143 #endif
144
145 static int has_nl _((SV *sv));
146
147 static int has_nl(sv)
148 SV *sv;
149 {
150 STRLEN n = SvCUR(sv);
151 char *p = SvPVX(sv);
152 while (n-- > 0)
153 {
154 if (*p++ == '\n')
155 return 1;
156 }
157 return 0;
158 }
159
160 MODULE = Tk::IO PACKAGE = Tk::IO
161
162 PROTOTYPES: ENABLE
163
164 int
165 make_nonblock(f,mode,newmode)
166 InputStream f
167 int &mode = NO_INIT
168 int &newmode = NO_INIT
169 CODE:
170 {
171 make_nonblock(aTHX_ f,&mode,&newmode);
172 }
173 OUTPUT:
174 mode
175 newmode
176
177 int
178 restore_mode(f,mode)
179 InputStream f
180 int mode
181 CODE:
182 {
183 restore_mode(aTHX_ f,mode);
184 }
185
186 SV *
187 read(f,buf,len,offset = 0)
188 InputStream f
189 SV * buf
190 int len
191 int offset
192 CODE:
193 {
194 int mode;
195 int newmode;
196 int count = make_nonblock(aTHX_ f,&mode,&newmode);
197 /* Copy stuff out of PerlIO * */
198 ST(0) = &PL_sv_undef;
199 if (count == 0)
200 {
201 int fd = PerlIO_fileno(f);
202 nIO_read info;
203 info.f = f;
204 info.buf = buf;
205 info.len = len;
206 info.offset = offset;
207 info.count = 0;
208 info.error = 0;
209 info.eof = 0;
210 (void)SvUPGRADE(buf, SVt_PV);
211 SvPOK_only(buf); /* validate pointer */
212 Tcl_CreateFileHandler(fd, TCL_READABLE, read_handler, (ClientData) &info);
213 do
214 {
215 Tcl_DoOneEvent(0);
216 } while (!info.eof && !info.error && info.count == 0);
217 Tcl_DeleteFileHandler(fd);
218 if (mode != newmode)
219 {
220 count = restore_mode(aTHX_ f,mode);
221 if (count != 0)
222 croak("Cannot make blocking");
223 }
224 if (!info.eof && !info.error)
225 {
226 ST(0) = sv_2mortal(newSViv(info.count));
227 }
228 }
229 else
230 croak("Cannot make non-blocking");
231 }
232
233 SV *
234 readline(f)
235 InputStream f
236 CODE:
237 {
238 int mode;
239 int newmode;
240 int count = make_nonblock(aTHX_ f,&mode,&newmode);
241 /* Copy stuff out of PerlIO * */
242 ST(0) = &PL_sv_undef;
243 if (count == 0)
244 {
245 SV *buf = newSVpv("",0);
246 int fd = PerlIO_fileno(f);
247 nIO_read info;
248 info.f = f;
249 info.buf = buf;
250 info.len = 1;
251 info.offset = 0;
252 info.count = 0;
253 info.error = 0;
254 info.eof = 0;
255 Tcl_CreateFileHandler(fd, TCL_READABLE, read_handler, (ClientData) &info);
256 while (!info.eof && !info.error && !has_nl(buf))
257 {
258 info.len = 1;
259 info.count = 0;
260 while (!info.eof && !info.error && !info.count)
261 Tcl_DoOneEvent(0);
262 }
263 Tcl_DeleteFileHandler(fd);
264 if (mode != newmode)
265 {
266 count = restore_mode(aTHX_ f,mode);
267 if (count != 0)
268 croak("Cannot make blocking");
269 }
270 if (!info.eof && !info.error)
271 {
272 sv_setiv(buf,1);
273 SvPOK_on(buf);
274 ST(0) = sv_2mortal(buf);
275 }
276 else if (info.error)
277 {
278 warn("error=%d",info.error);
279 }
280 }
281 else
282 {
283 croak("Cannot make non-blocking");
284 }
285 }
286
287 BOOT:
288 {
289 IMPORT_EVENT;
290 }