rw.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /* Copyright (C) 2001, 2006 Free Software Foundation, Inc.
  2. *
  3. * This library is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU Lesser General Public
  5. * License as published by the Free Software Foundation; either
  6. * version 2.1 of the License, or (at your option) any later version.
  7. *
  8. * This library 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 GNU
  11. * Lesser General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Lesser General Public
  14. * License along with this library; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  16. */
  17. /* This is the C part of the (ice-9 rw) module. */
  18. #ifdef HAVE_CONFIG_H
  19. # include <config.h>
  20. #endif
  21. #include <errno.h>
  22. #include <string.h>
  23. #include "libguile/_scm.h"
  24. #include "libguile/fports.h"
  25. #include "libguile/ports.h"
  26. #include "libguile/root.h"
  27. #include "libguile/rw.h"
  28. #include "libguile/strings.h"
  29. #include "libguile/validate.h"
  30. #include "libguile/modules.h"
  31. #include "libguile/strports.h"
  32. #ifdef HAVE_UNISTD_H
  33. #include <unistd.h>
  34. #endif
  35. #ifdef HAVE_IO_H
  36. #include <io.h>
  37. #endif
  38. #if defined (EAGAIN)
  39. #define SCM_MAYBE_EAGAIN || errno == EAGAIN
  40. #else
  41. #define SCM_MAYBE_EAGAIN
  42. #endif
  43. #if defined (EWOULDBLOCK)
  44. #define SCM_MAYBE_EWOULDBLOCK || errno == EWOULDBLOCK
  45. #else
  46. #define SCM_MAYBE_EWOULDBLOCK
  47. #endif
  48. /* MAYBE there is EAGAIN way of defining this macro but now I EWOULDBLOCK. */
  49. #define SCM_EBLOCK(errno) \
  50. (0 SCM_MAYBE_EAGAIN SCM_MAYBE_EWOULDBLOCK)
  51. SCM_DEFINE (scm_read_string_x_partial, "read-string!/partial", 1, 3, 0,
  52. (SCM str, SCM port_or_fdes, SCM start, SCM end),
  53. "Read characters from a port or file descriptor into a\n"
  54. "string @var{str}. A port must have an underlying file\n"
  55. "descriptor --- a so-called fport. This procedure is\n"
  56. "scsh-compatible and can efficiently read large strings.\n"
  57. "It will:\n\n"
  58. "@itemize\n"
  59. "@item\n"
  60. "attempt to fill the entire string, unless the @var{start}\n"
  61. "and/or @var{end} arguments are supplied. i.e., @var{start}\n"
  62. "defaults to 0 and @var{end} defaults to\n"
  63. "@code{(string-length str)}\n"
  64. "@item\n"
  65. "use the current input port if @var{port_or_fdes} is not\n"
  66. "supplied.\n"
  67. "@item\n"
  68. "return fewer than the requested number of characters in some\n"
  69. "cases, e.g., on end of file, if interrupted by a signal, or if\n"
  70. "not all the characters are immediately available.\n"
  71. "@item\n"
  72. "wait indefinitely for some input if no characters are\n"
  73. "currently available,\n"
  74. "unless the port is in non-blocking mode.\n"
  75. "@item\n"
  76. "read characters from the port's input buffers if available,\n"
  77. "instead from the underlying file descriptor.\n"
  78. "@item\n"
  79. "return @code{#f} if end-of-file is encountered before reading\n"
  80. "any characters, otherwise return the number of characters\n"
  81. "read.\n"
  82. "@item\n"
  83. "return 0 if the port is in non-blocking mode and no characters\n"
  84. "are immediately available.\n"
  85. "@item\n"
  86. "return 0 if the request is for 0 bytes, with no\n"
  87. "end-of-file check.\n"
  88. "@end itemize")
  89. #define FUNC_NAME s_scm_read_string_x_partial
  90. {
  91. char *dest;
  92. size_t offset;
  93. long read_len;
  94. long chars_read = 0;
  95. int fdes;
  96. {
  97. size_t last;
  98. SCM_VALIDATE_STRING (1, str);
  99. scm_i_get_substring_spec (scm_i_string_length (str),
  100. start, &offset, end, &last);
  101. read_len = last - offset;
  102. }
  103. if (scm_is_integer (port_or_fdes))
  104. fdes = scm_to_int (port_or_fdes);
  105. else
  106. {
  107. SCM port = (SCM_UNBNDP (port_or_fdes)?
  108. scm_current_input_port () : port_or_fdes);
  109. SCM_VALIDATE_OPFPORT (2, port);
  110. SCM_VALIDATE_INPUT_PORT (2, port);
  111. /* if there's anything in the port buffers, use it, but then
  112. don't touch the file descriptor. otherwise the
  113. "return immediately if something is available" rule may
  114. be violated. */
  115. dest = scm_i_string_writable_chars (str) + offset;
  116. chars_read = scm_take_from_input_buffers (port, dest, read_len);
  117. scm_i_string_stop_writing ();
  118. fdes = SCM_FPORT_FDES (port);
  119. }
  120. if (chars_read == 0 && read_len > 0) /* don't confuse read_len == 0 with
  121. EOF. */
  122. {
  123. dest = scm_i_string_writable_chars (str) + offset;
  124. SCM_SYSCALL (chars_read = read (fdes, dest, read_len));
  125. scm_i_string_stop_writing ();
  126. if (chars_read == -1)
  127. {
  128. if (SCM_EBLOCK (errno))
  129. chars_read = 0;
  130. else
  131. SCM_SYSERROR;
  132. }
  133. else if (chars_read == 0)
  134. {
  135. scm_remember_upto_here_1 (str);
  136. return SCM_BOOL_F;
  137. }
  138. }
  139. scm_remember_upto_here_1 (str);
  140. return scm_from_long (chars_read);
  141. }
  142. #undef FUNC_NAME
  143. SCM_DEFINE (scm_write_string_partial, "write-string/partial", 1, 3, 0,
  144. (SCM str, SCM port_or_fdes, SCM start, SCM end),
  145. "Write characters from a string @var{str} to a port or file\n"
  146. "descriptor. A port must have an underlying file descriptor\n"
  147. "--- a so-called fport. This procedure is\n"
  148. "scsh-compatible and can efficiently write large strings.\n"
  149. "It will:\n\n"
  150. "@itemize\n"
  151. "@item\n"
  152. "attempt to write the entire string, unless the @var{start}\n"
  153. "and/or @var{end} arguments are supplied. i.e., @var{start}\n"
  154. "defaults to 0 and @var{end} defaults to\n"
  155. "@code{(string-length str)}\n"
  156. "@item\n"
  157. "use the current output port if @var{port_of_fdes} is not\n"
  158. "supplied.\n"
  159. "@item\n"
  160. "in the case of a buffered port, store the characters in the\n"
  161. "port's output buffer, if all will fit. If they will not fit\n"
  162. "then any existing buffered characters will be flushed\n"
  163. "before attempting\n"
  164. "to write the new characters directly to the underlying file\n"
  165. "descriptor. If the port is in non-blocking mode and\n"
  166. "buffered characters can not be flushed immediately, then an\n"
  167. "@code{EAGAIN} system-error exception will be raised (Note:\n"
  168. "scsh does not support the use of non-blocking buffered ports.)\n"
  169. "@item\n"
  170. "write fewer than the requested number of\n"
  171. "characters in some cases, e.g., if interrupted by a signal or\n"
  172. "if not all of the output can be accepted immediately.\n"
  173. "@item\n"
  174. "wait indefinitely for at least one character\n"
  175. "from @var{str} to be accepted by the port, unless the port is\n"
  176. "in non-blocking mode.\n"
  177. "@item\n"
  178. "return the number of characters accepted by the port.\n"
  179. "@item\n"
  180. "return 0 if the port is in non-blocking mode and can not accept\n"
  181. "at least one character from @var{str} immediately\n"
  182. "@item\n"
  183. "return 0 immediately if the request size is 0 bytes.\n"
  184. "@end itemize")
  185. #define FUNC_NAME s_scm_write_string_partial
  186. {
  187. const char *src;
  188. long write_len;
  189. int fdes;
  190. {
  191. size_t offset;
  192. size_t last;
  193. SCM_VALIDATE_STRING (1, str);
  194. src = scm_i_string_chars (str);
  195. scm_i_get_substring_spec (scm_i_string_length (str),
  196. start, &offset, end, &last);
  197. src += offset;
  198. write_len = last - offset;
  199. }
  200. if (write_len == 0)
  201. return SCM_INUM0;
  202. if (scm_is_integer (port_or_fdes))
  203. fdes = scm_to_int (port_or_fdes);
  204. else
  205. {
  206. SCM port = (SCM_UNBNDP (port_or_fdes)?
  207. scm_current_output_port () : port_or_fdes);
  208. scm_t_port *pt;
  209. off_t space;
  210. SCM_VALIDATE_OPFPORT (2, port);
  211. SCM_VALIDATE_OUTPUT_PORT (2, port);
  212. pt = SCM_PTAB_ENTRY (port);
  213. /* filling the last character in the buffer would require a flush. */
  214. space = pt->write_end - pt->write_pos - 1;
  215. if (space >= write_len)
  216. {
  217. memcpy (pt->write_pos, src, write_len);
  218. pt->write_pos += write_len;
  219. return scm_from_long (write_len);
  220. }
  221. if (pt->write_pos > pt->write_buf)
  222. scm_flush (port);
  223. fdes = SCM_FPORT_FDES (port);
  224. }
  225. {
  226. long rv;
  227. SCM_SYSCALL (rv = write (fdes, src, write_len));
  228. if (rv == -1)
  229. {
  230. if (SCM_EBLOCK (errno))
  231. rv = 0;
  232. else
  233. SCM_SYSERROR;
  234. }
  235. scm_remember_upto_here_1 (str);
  236. return scm_from_long (rv);
  237. }
  238. }
  239. #undef FUNC_NAME
  240. SCM
  241. scm_init_rw_builtins ()
  242. {
  243. #include "libguile/rw.x"
  244. return SCM_UNSPECIFIED;
  245. }
  246. void
  247. scm_init_rw ()
  248. {
  249. scm_c_define_gsubr ("%init-rw-builtins", 0, 0, 0, scm_init_rw_builtins);
  250. }
  251. /*
  252. Local Variables:
  253. c-file-style: "gnu"
  254. End:
  255. */