Commit | Line | Data |
---|---|---|
840ae05d | 1 | /* Copyright (C) 1995,1996,1997,1998,1999 Free Software Foundation, Inc. |
0f2d19dd JB |
2 | * |
3 | * This program is free software; you can redistribute it and/or modify | |
4 | * it under the terms of the GNU General Public License as published by | |
5 | * the Free Software Foundation; either version 2, or (at your option) | |
6 | * any later version. | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, | |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 | * GNU General Public License for more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public License | |
14 | * along with this software; see the file COPYING. If not, write to | |
82892bed JB |
15 | * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, |
16 | * Boston, MA 02111-1307 USA | |
0f2d19dd JB |
17 | * |
18 | * As a special exception, the Free Software Foundation gives permission | |
19 | * for additional uses of the text contained in its release of GUILE. | |
20 | * | |
21 | * The exception is that, if you link the GUILE library with other files | |
22 | * to produce an executable, this does not by itself cause the | |
23 | * resulting executable to be covered by the GNU General Public License. | |
24 | * Your use of that executable is in no way restricted on account of | |
25 | * linking the GUILE library code into it. | |
26 | * | |
27 | * This exception does not however invalidate any other reasons why | |
28 | * the executable file might be covered by the GNU General Public License. | |
29 | * | |
30 | * This exception applies only to the code released by the | |
31 | * Free Software Foundation under the name GUILE. If you copy | |
32 | * code from other Free Software Foundation releases into a copy of | |
33 | * GUILE, as the General Public License permits, the exception does | |
34 | * not apply to the code that you add in this way. To avoid misleading | |
35 | * anyone as to the status of such modified files, you must delete | |
36 | * this exception notice from them. | |
37 | * | |
38 | * If you write modifications of your own for GUILE, it is your choice | |
39 | * whether to permit this exception to apply to your modifications. | |
82892bed | 40 | * If you do not wish that, delete this exception notice. */ |
1bbd0b84 GB |
41 | |
42 | /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999, | |
43 | gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */ | |
44 | ||
0f2d19dd JB |
45 | \f |
46 | ||
47 | #include <stdio.h> | |
cb63cf9e | 48 | #include <fcntl.h> |
0f2d19dd | 49 | #include "_scm.h" |
20e6290e | 50 | |
b6791b2e | 51 | #include "validate.h" |
20e6290e | 52 | #include "fports.h" |
95b88819 GH |
53 | |
54 | #ifdef HAVE_STRING_H | |
55 | #include <string.h> | |
56 | #endif | |
0f2d19dd JB |
57 | #ifdef HAVE_UNISTD_H |
58 | #include <unistd.h> | |
59 | #else | |
0f2d19dd JB |
60 | scm_sizet fwrite (); |
61 | #endif | |
cb63cf9e JB |
62 | #ifdef HAVE_ST_BLKSIZE |
63 | #include <sys/stat.h> | |
64 | #endif | |
0f2d19dd | 65 | |
cb63cf9e | 66 | #include <errno.h> |
e145dd02 | 67 | |
cb63cf9e JB |
68 | #include "iselect.h" |
69 | ||
70 | /* create FPORT buffer with specified sizes (or -1 to use default size or | |
71 | 0 for no buffer. */ | |
72 | static void | |
73 | scm_fport_buffer_add (SCM port, int read_size, int write_size) | |
e145dd02 | 74 | { |
cb63cf9e | 75 | struct scm_fport *fp = SCM_FSTREAM (port); |
840ae05d | 76 | scm_port *pt = SCM_PTAB_ENTRY (port); |
cb63cf9e | 77 | char *s_scm_fport_buffer_add = "scm_fport_buffer_add"; |
e145dd02 | 78 | |
cb63cf9e JB |
79 | if (read_size == -1 || write_size == -1) |
80 | { | |
81 | int default_size; | |
82 | #ifdef HAVE_ST_BLKSIZE | |
83 | struct stat st; | |
84 | ||
85 | if (fstat (fp->fdes, &st) == -1) | |
86 | scm_syserror (s_scm_fport_buffer_add); | |
87 | default_size = st.st_blksize; | |
88 | #else | |
89 | default_size = 1024; | |
90 | #endif | |
91 | if (read_size == -1) | |
92 | read_size = default_size; | |
93 | if (write_size == -1) | |
94 | write_size = default_size; | |
95 | } | |
0f2d19dd | 96 | |
cb63cf9e JB |
97 | if (SCM_INPORTP (port) && read_size > 0) |
98 | { | |
840ae05d JB |
99 | pt->read_buf = malloc (read_size); |
100 | if (pt->read_buf == NULL) | |
101 | scm_memory_error (s_scm_fport_buffer_add); | |
cb63cf9e JB |
102 | pt->read_pos = pt->read_end = pt->read_buf; |
103 | pt->read_buf_size = read_size; | |
104 | } | |
105 | else | |
106 | { | |
840ae05d | 107 | pt->read_pos = pt->read_buf = pt->read_end = &pt->shortbuf; |
cb63cf9e JB |
108 | pt->read_buf_size = 1; |
109 | } | |
1717856b | 110 | |
cb63cf9e JB |
111 | if (SCM_OUTPORTP (port) && write_size > 0) |
112 | { | |
840ae05d JB |
113 | pt->write_buf = malloc (write_size); |
114 | if (pt->write_buf == NULL) | |
115 | scm_memory_error (s_scm_fport_buffer_add); | |
cb63cf9e JB |
116 | pt->write_pos = pt->write_buf; |
117 | pt->write_buf_size = write_size; | |
118 | } | |
119 | else | |
120 | { | |
121 | pt->write_buf = pt->write_pos = &pt->shortbuf; | |
122 | pt->write_buf_size = 1; | |
123 | } | |
124 | ||
125 | pt->write_end = pt->write_buf + pt->write_buf_size; | |
126 | if (read_size > 0 || write_size > 0) | |
f1267706 | 127 | SCM_SETCAR (port, SCM_UNPACK_CAR (port) & ~SCM_BUF0); |
cb63cf9e | 128 | else |
f1267706 | 129 | SCM_SETCAR (port, (SCM_UNPACK_CAR (port) | SCM_BUF0)); |
7a6f1ffa GH |
130 | } |
131 | ||
a1ec6916 | 132 | SCM_DEFINE (scm_setvbuf, "setvbuf", 2, 1, 0, |
1bbd0b84 | 133 | (SCM port, SCM mode, SCM size), |
fc0d72d4 MD |
134 | "Set the buffering mode for @var{port}. @var{mode} can be:\n" |
135 | "@table @code\n" | |
136 | "@item _IONBF\n" | |
137 | "non-buffered\n" | |
138 | "@item _IOLBF\n" | |
139 | "line buffered\n" | |
140 | "@item _IOFBF\n" | |
141 | "block buffered, using a newly allocated buffer of @var{size} bytes.\n" | |
142 | "If @var{size} is omitted, a default size will be used.\n" | |
143 | "@end table\n\n\n" | |
144 | "@deffn primitive fcntl fd/port command [value]\n" | |
145 | "Apply @var{command} to the specified file descriptor or the underlying\n" | |
146 | "file descriptor of the specified port. @var{value} is an optional\n" | |
147 | "integer argument.\n\n" | |
148 | "Values for @var{command} are:\n\n" | |
149 | "@table @code\n" | |
150 | "@item F_DUPFD\n" | |
151 | "Duplicate a file descriptor\n" | |
152 | "@item F_GETFD\n" | |
153 | "Get flags associated with the file descriptor.\n" | |
154 | "@item F_SETFD\n" | |
155 | "Set flags associated with the file descriptor to @var{value}.\n" | |
156 | "@item F_GETFL\n" | |
157 | "Get flags associated with the open file.\n" | |
158 | "@item F_SETFL\n" | |
159 | "Set flags associated with the open file to @var{value}\n" | |
160 | "@item F_GETOWN\n" | |
161 | "Get the process ID of a socket's owner, for @code{SIGIO} signals.\n" | |
162 | "@item F_SETOWN\n" | |
163 | "Set the process that owns a socket to @var{value}, for @code{SIGIO} signals.\n" | |
164 | "@item FD_CLOEXEC\n" | |
165 | "The value used to indicate the \"close on exec\" flag with @code{F_GETFL} or\n" | |
166 | "@code{F_SETFL}.\n" | |
167 | "@end table\n" | |
168 | "") | |
1bbd0b84 | 169 | #define FUNC_NAME s_scm_setvbuf |
7a6f1ffa | 170 | { |
7a6f1ffa | 171 | int cmode, csize; |
840ae05d | 172 | scm_port *pt; |
7a6f1ffa | 173 | |
78446828 MV |
174 | port = SCM_COERCE_OUTPORT (port); |
175 | ||
3b3b36dd GB |
176 | SCM_VALIDATE_OPFPORT (1,port); |
177 | SCM_VALIDATE_INUM_COPY (2,mode,cmode); | |
d3639214 | 178 | if (cmode != _IONBF && cmode != _IOFBF && cmode != _IOLBF) |
1bbd0b84 | 179 | scm_out_of_range (FUNC_NAME, mode); |
d3639214 GH |
180 | |
181 | if (cmode == _IOLBF) | |
182 | { | |
f1267706 | 183 | SCM_SETCAR (port, SCM_UNPACK_CAR (port) | SCM_BUFLINE); |
d3639214 GH |
184 | cmode = _IOFBF; |
185 | } | |
186 | else | |
187 | { | |
f1267706 | 188 | SCM_SETCAR (port, SCM_UNPACK_CAR (port) ^ SCM_BUFLINE); |
d3639214 GH |
189 | } |
190 | ||
7a6f1ffa | 191 | if (SCM_UNBNDP (size)) |
cb63cf9e JB |
192 | { |
193 | if (cmode == _IOFBF) | |
194 | csize = -1; | |
195 | else | |
196 | csize = 0; | |
197 | } | |
7a6f1ffa GH |
198 | else |
199 | { | |
3b3b36dd | 200 | SCM_VALIDATE_INUM_COPY (3,size,csize); |
cb63cf9e | 201 | if (csize < 0 || (cmode == _IONBF && csize > 0)) |
1bbd0b84 | 202 | scm_out_of_range (FUNC_NAME, size); |
7a6f1ffa | 203 | } |
d3639214 | 204 | |
cb63cf9e | 205 | pt = SCM_PTAB_ENTRY (port); |
7a6f1ffa | 206 | |
cb63cf9e JB |
207 | /* silently discards buffered chars. */ |
208 | if (pt->read_buf != &pt->shortbuf) | |
209 | scm_must_free (pt->read_buf); | |
210 | if (pt->write_buf != &pt->shortbuf) | |
211 | scm_must_free (pt->write_buf); | |
7a6f1ffa | 212 | |
cb63cf9e JB |
213 | scm_fport_buffer_add (port, csize, csize); |
214 | return SCM_UNSPECIFIED; | |
0f2d19dd | 215 | } |
1bbd0b84 | 216 | #undef FUNC_NAME |
0f2d19dd | 217 | |
eadd48de GH |
218 | /* Move ports with the specified file descriptor to new descriptors, |
219 | * reseting the revealed count to 0. | |
0f2d19dd | 220 | */ |
1717856b | 221 | |
eadd48de | 222 | void |
6e8d25a6 | 223 | scm_evict_ports (int fd) |
0f2d19dd | 224 | { |
eadd48de | 225 | int i; |
0f2d19dd | 226 | |
eadd48de GH |
227 | for (i = 0; i < scm_port_table_size; i++) |
228 | { | |
cb63cf9e JB |
229 | SCM port = scm_port_table[i]->port; |
230 | ||
231 | if (SCM_FPORTP (port)) | |
eadd48de | 232 | { |
cb63cf9e JB |
233 | struct scm_fport *fp = SCM_FSTREAM (port); |
234 | ||
235 | if (fp->fdes == fd) | |
236 | { | |
237 | fp->fdes = dup (fd); | |
238 | if (fp->fdes == -1) | |
239 | scm_syserror ("scm_evict_ports"); | |
240 | scm_set_port_revealed_x (port, SCM_MAKINUM (0)); | |
241 | } | |
eadd48de GH |
242 | } |
243 | } | |
244 | } | |
0f2d19dd JB |
245 | |
246 | /* scm_open_file | |
247 | * Return a new port open on a given file. | |
248 | * | |
249 | * The mode string must match the pattern: [rwa+]** which | |
250 | * is interpreted in the usual unix way. | |
251 | * | |
252 | * Return the new port. | |
253 | */ | |
3b3b36dd | 254 | SCM_DEFINE (scm_open_file, "open-file", 2, 0, 0, |
1bbd0b84 | 255 | (SCM filename, SCM modes), |
fc0d72d4 MD |
256 | "Open the file whose name is @var{string}, and return a port\n" |
257 | "representing that file. The attributes of the port are\n" | |
258 | "determined by the @var{mode} string. The way in \n" | |
259 | "which this is interpreted is similar to C stdio:\n\n" | |
260 | "The first character must be one of the following:\n\n" | |
261 | "@table @samp\n" | |
262 | "@item r\n" | |
263 | "Open an existing file for input.\n" | |
264 | "@item w\n" | |
265 | "Open a file for output, creating it if it doesn't already exist\n" | |
266 | "or removing its contents if it does.\n" | |
267 | "@item a\n" | |
268 | "Open a file for output, creating it if it doesn't already exist.\n" | |
269 | "All writes to the port will go to the end of the file.\n" | |
270 | "The \"append mode\" can be turned off while the port is in use\n" | |
271 | "@pxref{Ports and File Descriptors, fcntl}\n" | |
272 | "@end table\n\n" | |
273 | "The following additional characters can be appended:\n\n" | |
274 | "@table @samp\n" | |
275 | "@item +\n" | |
276 | "Open the port for both input and output. E.g., @code{r+}: open\n" | |
277 | "an existing file for both input and output.\n" | |
278 | "@item 0\n" | |
279 | "Create an \"unbuffered\" port. In this case input and output operations\n" | |
280 | "are passed directly to the underlying port implementation without\n" | |
281 | "additional buffering. This is likely to slow down I/O operations.\n" | |
282 | "The buffering mode can be changed while a port is in use\n" | |
283 | "@pxref{Ports and File Descriptors, setvbuf}\n" | |
284 | "@item l\n" | |
285 | "Add line-buffering to the port. The port output buffer will be\n" | |
286 | "automatically flushed whenever a newline character is written.\n" | |
287 | "@end table\n\n" | |
288 | "In theory we could create read/write ports which were buffered in one\n" | |
289 | "direction only. However this isn't included in the current interfaces.\n\n" | |
290 | "If a file cannot be opened with the access requested,\n" | |
291 | "@code{open-file} throws an exception.") | |
1bbd0b84 | 292 | #define FUNC_NAME s_scm_open_file |
0f2d19dd | 293 | { |
19639113 | 294 | SCM port; |
cb63cf9e JB |
295 | int fdes; |
296 | int flags = 0; | |
19639113 GH |
297 | char *file; |
298 | char *mode; | |
cb63cf9e | 299 | char *ptr; |
19639113 | 300 | |
3b3b36dd GB |
301 | SCM_VALIDATE_ROSTRING (1,filename); |
302 | SCM_VALIDATE_ROSTRING (2,modes); | |
19639113 GH |
303 | if (SCM_SUBSTRP (filename)) |
304 | filename = scm_makfromstr (SCM_ROCHARS (filename), SCM_ROLENGTH (filename), 0); | |
305 | if (SCM_SUBSTRP (modes)) | |
306 | modes = scm_makfromstr (SCM_ROCHARS (modes), SCM_ROLENGTH (modes), 0); | |
307 | ||
308 | file = SCM_ROCHARS (filename); | |
309 | mode = SCM_ROCHARS (modes); | |
310 | ||
cb63cf9e | 311 | switch (*mode) |
0f2d19dd | 312 | { |
cb63cf9e JB |
313 | case 'r': |
314 | flags |= O_RDONLY; | |
315 | break; | |
316 | case 'w': | |
317 | flags |= O_WRONLY | O_CREAT | O_TRUNC; | |
318 | break; | |
319 | case 'a': | |
320 | flags |= O_WRONLY | O_CREAT | O_APPEND; | |
321 | break; | |
322 | default: | |
1bbd0b84 | 323 | scm_out_of_range (FUNC_NAME, modes); |
0f2d19dd | 324 | } |
cb63cf9e JB |
325 | ptr = mode + 1; |
326 | while (*ptr != '\0') | |
e145dd02 | 327 | { |
cb63cf9e JB |
328 | switch (*ptr) |
329 | { | |
330 | case '+': | |
331 | flags = (flags & ~(O_RDONLY | O_WRONLY)) | O_RDWR; | |
332 | break; | |
333 | case '0': /* unbuffered: handled later. */ | |
334 | case 'b': /* 'binary' mode: ignored. */ | |
d3639214 | 335 | case 'l': /* line buffered: handled during output. */ |
cb63cf9e JB |
336 | break; |
337 | default: | |
1bbd0b84 | 338 | scm_out_of_range (FUNC_NAME, modes); |
cb63cf9e JB |
339 | } |
340 | ptr++; | |
e145dd02 | 341 | } |
cb63cf9e JB |
342 | SCM_SYSCALL (fdes = open (file, flags, 0666)); |
343 | if (fdes == -1) | |
e145dd02 | 344 | { |
cb63cf9e JB |
345 | int en = errno; |
346 | ||
5d2d2ffc | 347 | SCM_SYSERROR_MSG ("~A: ~S", |
cb63cf9e | 348 | scm_cons (scm_makfrom0str (strerror (en)), |
5d2d2ffc | 349 | scm_cons (filename, SCM_EOL)), en); |
0f2d19dd | 350 | } |
cb63cf9e | 351 | port = scm_fdes_to_port (fdes, mode, filename); |
0f2d19dd JB |
352 | return port; |
353 | } | |
1bbd0b84 | 354 | #undef FUNC_NAME |
0f2d19dd | 355 | |
e145dd02 | 356 | \f |
cb63cf9e | 357 | /* Building Guile ports from a file descriptor. */ |
e145dd02 | 358 | |
cb63cf9e | 359 | /* Build a Scheme port from an open file descriptor `fdes'. |
a089567e JB |
360 | MODE indicates whether FILE is open for reading or writing; it uses |
361 | the same notation as open-file's second argument. | |
e145dd02 | 362 | Use NAME as the port's filename. */ |
cb63cf9e | 363 | |
a089567e | 364 | SCM |
cb63cf9e | 365 | scm_fdes_to_port (int fdes, char *mode, SCM name) |
a089567e JB |
366 | { |
367 | long mode_bits = scm_mode_bits (mode); | |
368 | SCM port; | |
840ae05d | 369 | scm_port *pt; |
a089567e JB |
370 | |
371 | SCM_NEWCELL (port); | |
372 | SCM_DEFER_INTS; | |
cb63cf9e JB |
373 | pt = scm_add_to_port_table (port); |
374 | SCM_SETPTAB_ENTRY (port, pt); | |
375 | SCM_SETCAR (port, (scm_tc16_fport | mode_bits)); | |
376 | ||
a089567e | 377 | { |
cb63cf9e | 378 | struct scm_fport *fp |
840ae05d JB |
379 | = (struct scm_fport *) malloc (sizeof (struct scm_fport)); |
380 | if (fp == NULL) | |
381 | scm_memory_error ("scm_fdes_to_port"); | |
cb63cf9e | 382 | fp->fdes = fdes; |
0de97b83 | 383 | pt->rw_random = SCM_FDES_RANDOM_P (fdes); |
cb63cf9e JB |
384 | SCM_SETSTREAM (port, fp); |
385 | if (mode_bits & SCM_BUF0) | |
386 | scm_fport_buffer_add (port, 0, 0); | |
387 | else | |
388 | scm_fport_buffer_add (port, -1, -1); | |
a089567e | 389 | } |
cb63cf9e | 390 | SCM_PTAB_ENTRY (port)->file_name = name; |
a089567e | 391 | SCM_ALLOW_INTS; |
e145dd02 JB |
392 | return port; |
393 | } | |
394 | ||
395 | ||
affc96b5 | 396 | /* Return a lower bound on the number of bytes available for input. */ |
cb63cf9e | 397 | static int |
affc96b5 | 398 | fport_input_waiting (SCM port) |
e145dd02 | 399 | { |
cb63cf9e JB |
400 | int fdes = SCM_FSTREAM (port)->fdes; |
401 | ||
402 | #ifdef HAVE_SELECT | |
403 | struct timeval timeout; | |
404 | SELECT_TYPE read_set; | |
405 | SELECT_TYPE write_set; | |
406 | SELECT_TYPE except_set; | |
407 | ||
408 | FD_ZERO (&read_set); | |
409 | FD_ZERO (&write_set); | |
410 | FD_ZERO (&except_set); | |
411 | ||
412 | FD_SET (fdes, &read_set); | |
413 | ||
414 | timeout.tv_sec = 0; | |
415 | timeout.tv_usec = 0; | |
416 | ||
417 | if (select (SELECT_SET_SIZE, | |
418 | &read_set, &write_set, &except_set, &timeout) | |
419 | < 0) | |
affc96b5 GH |
420 | scm_syserror ("fport_input_waiting"); |
421 | return FD_ISSET (fdes, &read_set) ? 1 : 0; | |
cb63cf9e JB |
422 | #elif defined (FIONREAD) |
423 | int remir; | |
424 | ioctl(fdes, FIONREAD, &remir); | |
425 | return remir; | |
426 | #else | |
affc96b5 | 427 | scm_misc_error ("fport_input_waiting", |
cb63cf9e JB |
428 | "Not fully implemented on this platform", |
429 | SCM_EOL); | |
430 | #endif | |
a089567e JB |
431 | } |
432 | ||
cb63cf9e | 433 | \f |
0f2d19dd | 434 | static int |
1bbd0b84 | 435 | prinfport (SCM exp,SCM port,scm_print_state *pstate) |
0f2d19dd | 436 | { |
b3ec3c64 MD |
437 | scm_puts ("#<", port); |
438 | scm_print_port_mode (exp, port); | |
439 | if (SCM_OPFPORTP (exp)) | |
0f2d19dd | 440 | { |
b3ec3c64 MD |
441 | int fdes; |
442 | SCM name = SCM_PTAB_ENTRY (exp)->file_name; | |
0c95b57d | 443 | scm_puts (SCM_ROSTRINGP (name) |
b3ec3c64 MD |
444 | ? SCM_ROCHARS (name) |
445 | : SCM_PTOBNAME (SCM_PTOBNUM (exp)), | |
446 | port); | |
447 | scm_putc (' ', port); | |
448 | fdes = (SCM_FSTREAM (exp))->fdes; | |
449 | ||
450 | if (isatty (fdes)) | |
451 | scm_puts (ttyname (fdes), port); | |
452 | else | |
453 | scm_intprint (fdes, 10, port); | |
0f2d19dd JB |
454 | } |
455 | else | |
456 | { | |
b3ec3c64 MD |
457 | scm_puts (SCM_PTOBNAME (SCM_PTOBNUM (exp)), port); |
458 | scm_putc (' ', port); | |
f1267706 | 459 | scm_intprint (SCM_UNPACK (SCM_CDR (exp)), 16, port); |
0f2d19dd | 460 | } |
b3ec3c64 MD |
461 | scm_putc ('>', port); |
462 | return 1; | |
0f2d19dd JB |
463 | } |
464 | ||
cb63cf9e JB |
465 | #ifdef GUILE_ISELECT |
466 | /* thread-local block for input on fport's fdes. */ | |
467 | static void | |
468 | fport_wait_for_input (SCM port) | |
3cb988bd | 469 | { |
cb63cf9e | 470 | int fdes = SCM_FSTREAM (port)->fdes; |
3cb988bd | 471 | |
affc96b5 | 472 | if (!fport_input_waiting (port)) |
8122b543 | 473 | { |
cb63cf9e JB |
474 | int n; |
475 | SELECT_TYPE readfds; | |
476 | int flags = fcntl (fdes, F_GETFL); | |
477 | ||
478 | if (flags == -1) | |
479 | scm_syserror ("scm_fdes_wait_for_input"); | |
480 | if (!(flags & O_NONBLOCK)) | |
481 | do | |
482 | { | |
483 | FD_ZERO (&readfds); | |
484 | FD_SET (fdes, &readfds); | |
485 | n = scm_internal_select (fdes + 1, &readfds, NULL, NULL, NULL); | |
486 | } | |
487 | while (n == -1 && errno == EINTR); | |
8122b543 | 488 | } |
3cb988bd | 489 | } |
0f2d19dd JB |
490 | #endif |
491 | ||
affc96b5 | 492 | static void fport_flush (SCM port); |
0f2d19dd | 493 | |
cb63cf9e JB |
494 | /* fill a port's read-buffer with a single read. |
495 | returns the first char and moves the read_pos pointer past it. | |
496 | or returns EOF if end of file. */ | |
0f2d19dd | 497 | static int |
affc96b5 | 498 | fport_fill_input (SCM port) |
0f2d19dd | 499 | { |
cb63cf9e | 500 | int count; |
840ae05d | 501 | scm_port *pt = SCM_PTAB_ENTRY (port); |
cb63cf9e JB |
502 | struct scm_fport *fp = SCM_FSTREAM (port); |
503 | ||
cb63cf9e JB |
504 | #ifdef GUILE_ISELECT |
505 | fport_wait_for_input (port); | |
506 | #endif | |
507 | SCM_SYSCALL (count = read (fp->fdes, pt->read_buf, pt->read_buf_size)); | |
508 | if (count == -1) | |
affc96b5 | 509 | scm_syserror ("fport_fill_input"); |
cb63cf9e JB |
510 | if (count == 0) |
511 | return EOF; | |
512 | else | |
513 | { | |
5c070ca7 | 514 | pt->read_pos = pt->read_buf; |
cb63cf9e | 515 | pt->read_end = pt->read_buf + count; |
5c070ca7 | 516 | return *pt->read_buf; |
cb63cf9e | 517 | } |
0f2d19dd JB |
518 | } |
519 | ||
cb63cf9e | 520 | static off_t |
affc96b5 | 521 | fport_seek (SCM port, off_t offset, int whence) |
0f2d19dd | 522 | { |
7dcb364d | 523 | scm_port *pt = SCM_PTAB_ENTRY (port); |
cb63cf9e | 524 | struct scm_fport *fp = SCM_FSTREAM (port); |
7dcb364d GH |
525 | off_t rv; |
526 | off_t result; | |
527 | ||
528 | if (pt->rw_active == SCM_PORT_WRITE) | |
529 | { | |
530 | if (offset != 0 || whence != SEEK_CUR) | |
531 | { | |
532 | fport_flush (port); | |
533 | result = rv = lseek (fp->fdes, offset, whence); | |
534 | } | |
535 | else | |
536 | { | |
537 | /* read current position without disturbing the buffer. */ | |
538 | rv = lseek (fp->fdes, offset, whence); | |
539 | result = rv + (pt->write_pos - pt->write_buf); | |
540 | } | |
541 | } | |
542 | else if (pt->rw_active == SCM_PORT_READ) | |
543 | { | |
544 | if (offset != 0 || whence != SEEK_CUR) | |
545 | { | |
546 | /* could expand to avoid a second seek. */ | |
547 | scm_end_input (port); | |
548 | result = rv = lseek (fp->fdes, offset, whence); | |
549 | } | |
550 | else | |
551 | { | |
552 | /* read current position without disturbing the buffer | |
553 | (particularly the unread-char buffer). */ | |
554 | rv = lseek (fp->fdes, offset, whence); | |
555 | result = rv - (pt->read_end - pt->read_pos); | |
556 | ||
557 | if (pt->read_buf == pt->putback_buf) | |
558 | result -= pt->saved_read_end - pt->saved_read_pos; | |
559 | } | |
560 | } | |
561 | else /* SCM_PORT_NEITHER */ | |
562 | { | |
563 | result = rv = lseek (fp->fdes, offset, whence); | |
564 | } | |
cb8dfa3f | 565 | |
7dcb364d | 566 | if (rv == -1) |
affc96b5 | 567 | scm_syserror ("fport_seek"); |
7dcb364d | 568 | |
cb8dfa3f | 569 | return result; |
0f2d19dd JB |
570 | } |
571 | ||
840ae05d | 572 | static void |
affc96b5 | 573 | fport_truncate (SCM port, off_t length) |
840ae05d JB |
574 | { |
575 | struct scm_fport *fp = SCM_FSTREAM (port); | |
576 | ||
577 | if (ftruncate (fp->fdes, length) == -1) | |
578 | scm_syserror ("ftruncate"); | |
579 | } | |
580 | ||
31703ab8 | 581 | static void |
8aa011a1 | 582 | fport_write (SCM port, const void *data, size_t size) |
31703ab8 GH |
583 | { |
584 | scm_port *pt = SCM_PTAB_ENTRY (port); | |
585 | ||
586 | if (pt->write_buf == &pt->shortbuf) | |
587 | { | |
588 | /* "unbuffered" port. */ | |
589 | int fdes = SCM_FSTREAM (port)->fdes; | |
590 | ||
591 | if (write (fdes, data, size) == -1) | |
592 | scm_syserror ("fport_write"); | |
593 | } | |
594 | else | |
595 | { | |
596 | const char *input = (char *) data; | |
d3639214 GH |
597 | size_t remaining = size; |
598 | ||
599 | while (remaining > 0) | |
31703ab8 GH |
600 | { |
601 | int space = pt->write_end - pt->write_pos; | |
d3639214 | 602 | int write_len = (remaining > space) ? space : remaining; |
31703ab8 | 603 | |
162d88ca | 604 | memcpy (pt->write_pos, input, write_len); |
31703ab8 | 605 | pt->write_pos += write_len; |
d3639214 | 606 | remaining -= write_len; |
31703ab8 GH |
607 | input += write_len; |
608 | if (write_len == space) | |
affc96b5 | 609 | fport_flush (port); |
31703ab8 GH |
610 | } |
611 | ||
612 | /* handle line buffering. */ | |
f1267706 | 613 | if ((SCM_UNPACK_CAR (port) & SCM_BUFLINE) && memchr (data, '\n', size)) |
affc96b5 | 614 | fport_flush (port); |
31703ab8 GH |
615 | } |
616 | } | |
617 | ||
618 | /* becomes 1 when process is exiting: normal exception handling won't | |
619 | work by this time. */ | |
cb63cf9e | 620 | extern int terminating; |
0f2d19dd | 621 | |
cb63cf9e | 622 | static void |
affc96b5 | 623 | fport_flush (SCM port) |
0f2d19dd | 624 | { |
840ae05d | 625 | scm_port *pt = SCM_PTAB_ENTRY (port); |
cb63cf9e JB |
626 | struct scm_fport *fp = SCM_FSTREAM (port); |
627 | char *ptr = pt->write_buf; | |
628 | int init_size = pt->write_pos - pt->write_buf; | |
629 | int remaining = init_size; | |
0f2d19dd | 630 | |
cb63cf9e JB |
631 | while (remaining > 0) |
632 | { | |
633 | int count; | |
634 | ||
635 | SCM_SYSCALL (count = write (fp->fdes, ptr, remaining)); | |
636 | if (count < 0) | |
637 | { | |
638 | /* error. assume nothing was written this call, but | |
639 | fix up the buffer for any previous successful writes. */ | |
640 | int done = init_size - remaining; | |
641 | ||
642 | if (done > 0) | |
643 | { | |
644 | int i; | |
645 | ||
646 | for (i = 0; i < remaining; i++) | |
647 | { | |
648 | *(pt->write_buf + i) = *(pt->write_buf + done + i); | |
649 | } | |
650 | pt->write_pos = pt->write_buf + remaining; | |
651 | } | |
652 | if (!terminating) | |
affc96b5 | 653 | scm_syserror ("fport_flush"); |
cb63cf9e JB |
654 | else |
655 | { | |
656 | const char *msg = "Error: could not flush file-descriptor "; | |
657 | char buf[11]; | |
658 | ||
659 | write (2, msg, strlen (msg)); | |
660 | sprintf (buf, "%d\n", fp->fdes); | |
661 | write (2, buf, strlen (buf)); | |
662 | ||
663 | count = remaining; | |
664 | } | |
665 | } | |
666 | ptr += count; | |
667 | remaining -= count; | |
668 | } | |
669 | pt->write_pos = pt->write_buf; | |
61e452ba | 670 | pt->rw_active = SCM_PORT_NEITHER; |
840ae05d JB |
671 | } |
672 | ||
283a1a0e | 673 | /* clear the read buffer and adjust the file position for unread bytes. */ |
840ae05d | 674 | static void |
affc96b5 | 675 | fport_end_input (SCM port, int offset) |
840ae05d JB |
676 | { |
677 | struct scm_fport *fp = SCM_FSTREAM (port); | |
678 | scm_port *pt = SCM_PTAB_ENTRY (port); | |
283a1a0e GH |
679 | |
680 | offset += pt->read_end - pt->read_pos; | |
840ae05d | 681 | |
840ae05d JB |
682 | if (offset > 0) |
683 | { | |
684 | pt->read_pos = pt->read_end; | |
685 | /* will throw error if unread-char used at beginning of file | |
686 | then attempting to write. seems correct. */ | |
687 | if (lseek (fp->fdes, -offset, SEEK_CUR) == -1) | |
affc96b5 | 688 | scm_syserror ("fport_end_input"); |
840ae05d | 689 | } |
61e452ba | 690 | pt->rw_active = SCM_PORT_NEITHER; |
8f29fbd0 JB |
691 | } |
692 | ||
6a2c4c81 | 693 | static int |
affc96b5 | 694 | fport_close (SCM port) |
6a2c4c81 | 695 | { |
cb63cf9e | 696 | struct scm_fport *fp = SCM_FSTREAM (port); |
840ae05d | 697 | scm_port *pt = SCM_PTAB_ENTRY (port); |
cb63cf9e | 698 | int rv; |
840ae05d | 699 | |
affc96b5 | 700 | fport_flush (port); |
cb63cf9e JB |
701 | SCM_SYSCALL (rv = close (fp->fdes)); |
702 | if (rv == -1 && errno != EBADF) | |
affc96b5 | 703 | scm_syserror ("fport_close"); |
6c951427 GH |
704 | if (pt->read_buf == pt->putback_buf) |
705 | pt->read_buf = pt->saved_read_buf; | |
cb63cf9e | 706 | if (pt->read_buf != &pt->shortbuf) |
840ae05d | 707 | free (pt->read_buf); |
cb63cf9e | 708 | if (pt->write_buf != &pt->shortbuf) |
840ae05d JB |
709 | free (pt->write_buf); |
710 | free ((char *) fp); | |
cb63cf9e | 711 | return rv; |
6a2c4c81 JB |
712 | } |
713 | ||
b3ec3c64 | 714 | static scm_sizet |
affc96b5 | 715 | fport_free (SCM port) |
b3ec3c64 | 716 | { |
affc96b5 | 717 | fport_close (port); |
b3ec3c64 MD |
718 | return 0; |
719 | } | |
720 | ||
721 | void scm_make_fptob (void); /* Called from ports.c */ | |
722 | ||
723 | void | |
724 | scm_make_fptob () | |
725 | { | |
affc96b5 GH |
726 | long tc = scm_make_port_type ("file", fport_fill_input, fport_write); |
727 | scm_set_port_free (tc, fport_free); | |
6c747373 | 728 | scm_set_port_print (tc, prinfport); |
affc96b5 GH |
729 | scm_set_port_flush (tc, fport_flush); |
730 | scm_set_port_end_input (tc, fport_end_input); | |
731 | scm_set_port_close (tc, fport_close); | |
732 | scm_set_port_seek (tc, fport_seek); | |
733 | scm_set_port_truncate (tc, fport_truncate); | |
734 | scm_set_port_input_waiting (tc, fport_input_waiting); | |
b3ec3c64 | 735 | } |
0f2d19dd | 736 | |
0f2d19dd JB |
737 | void |
738 | scm_init_fports () | |
0f2d19dd JB |
739 | { |
740 | #include "fports.x" | |
7a6f1ffa GH |
741 | scm_sysintern ("_IOFBF", SCM_MAKINUM (_IOFBF)); |
742 | scm_sysintern ("_IOLBF", SCM_MAKINUM (_IOLBF)); | |
743 | scm_sysintern ("_IONBF", SCM_MAKINUM (_IONBF)); | |
0f2d19dd | 744 | } |