rw.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /* Copyright 2001,2006,2009,2011,2014,2018
  2. Free Software Foundation, Inc.
  3. This file is part of Guile.
  4. Guile is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Lesser General Public License as published
  6. by the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. Guile is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  11. License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with Guile. If not, see
  14. <https://www.gnu.org/licenses/>. */
  15. /* This is the C part of the (ice-9 rw) module. */
  16. #ifdef HAVE_CONFIG_H
  17. # include <config.h>
  18. #endif
  19. #include <errno.h>
  20. #include <string.h>
  21. #include <unistd.h>
  22. #ifdef HAVE_IO_H
  23. #include <io.h>
  24. #endif
  25. #include "async.h"
  26. #include "fports.h"
  27. #include "gsubr.h"
  28. #include "modules.h"
  29. #include "numbers.h"
  30. #include "ports-internal.h"
  31. #include "ports.h"
  32. #include "strings.h"
  33. #include "strports.h"
  34. #include "syscalls.h"
  35. #include "rw.h"
  36. #if defined (EAGAIN)
  37. #define SCM_MAYBE_EAGAIN || errno == EAGAIN
  38. #else
  39. #define SCM_MAYBE_EAGAIN
  40. #endif
  41. #if defined (EWOULDBLOCK)
  42. #define SCM_MAYBE_EWOULDBLOCK || errno == EWOULDBLOCK
  43. #else
  44. #define SCM_MAYBE_EWOULDBLOCK
  45. #endif
  46. /* MAYBE there is EAGAIN way of defining this macro but now I EWOULDBLOCK. */
  47. #define SCM_EBLOCK(errno) \
  48. (0 SCM_MAYBE_EAGAIN SCM_MAYBE_EWOULDBLOCK)
  49. SCM_DEFINE (scm_read_string_x_partial, "read-string!/partial", 1, 3, 0,
  50. (SCM str, SCM port_or_fdes, SCM start, SCM end),
  51. "Read characters from a port or file descriptor into a\n"
  52. "string @var{str}. A port must have an underlying file\n"
  53. "descriptor --- a so-called fport. This procedure is\n"
  54. "scsh-compatible and can efficiently read large strings.\n"
  55. "It will:\n\n"
  56. "@itemize\n"
  57. "@item\n"
  58. "attempt to fill the entire string, unless the @var{start}\n"
  59. "and/or @var{end} arguments are supplied. i.e., @var{start}\n"
  60. "defaults to 0 and @var{end} defaults to\n"
  61. "@code{(string-length str)}\n"
  62. "@item\n"
  63. "use the current input port if @var{port_or_fdes} is not\n"
  64. "supplied.\n"
  65. "@item\n"
  66. "return fewer than the requested number of characters in some\n"
  67. "cases, e.g., on end of file, if interrupted by a signal, or if\n"
  68. "not all the characters are immediately available.\n"
  69. "@item\n"
  70. "wait indefinitely for some input if no characters are\n"
  71. "currently available,\n"
  72. "unless the port is in non-blocking mode.\n"
  73. "@item\n"
  74. "read characters from the port's input buffers if available,\n"
  75. "instead from the underlying file descriptor.\n"
  76. "@item\n"
  77. "return @code{#f} if end-of-file is encountered before reading\n"
  78. "any characters, otherwise return the number of characters\n"
  79. "read.\n"
  80. "@item\n"
  81. "return 0 if the port is in non-blocking mode and no characters\n"
  82. "are immediately available.\n"
  83. "@item\n"
  84. "return 0 if the request is for 0 bytes, with no\n"
  85. "end-of-file check.\n"
  86. "@end itemize")
  87. #define FUNC_NAME s_scm_read_string_x_partial
  88. {
  89. char *dest;
  90. size_t offset;
  91. long read_len;
  92. long chars_read = 0;
  93. int fdes;
  94. {
  95. size_t last;
  96. SCM_VALIDATE_STRING (1, str);
  97. scm_i_get_substring_spec (scm_i_string_length (str),
  98. start, &offset, end, &last);
  99. read_len = last - offset;
  100. }
  101. if (scm_is_integer (port_or_fdes))
  102. fdes = scm_to_int (port_or_fdes);
  103. else
  104. {
  105. SCM port = (SCM_UNBNDP (port_or_fdes)?
  106. scm_current_input_port () : port_or_fdes);
  107. SCM_VALIDATE_OPFPORT (2, port);
  108. SCM_VALIDATE_INPUT_PORT (2, port);
  109. /* if there's anything in the port buffers, use it, but then
  110. don't touch the file descriptor. otherwise the
  111. "return immediately if something is available" rule may
  112. be violated. */
  113. str = scm_i_string_start_writing (str);
  114. dest = scm_i_string_writable_chars (str) + offset;
  115. chars_read = scm_take_from_input_buffers (port, dest, read_len);
  116. scm_i_string_stop_writing ();
  117. fdes = SCM_FPORT_FDES (port);
  118. }
  119. if (chars_read == 0 && read_len > 0) /* don't confuse read_len == 0 with
  120. EOF. */
  121. {
  122. str = scm_i_string_start_writing (str);
  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. scm_t_off 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 write_buf;
  209. size_t end;
  210. SCM_VALIDATE_OPFPORT (2, port);
  211. SCM_VALIDATE_OUTPUT_PORT (2, port);
  212. write_buf = SCM_PORT (port)->write_buf;
  213. /* Filling the last character in the buffer would require a
  214. flush. */
  215. if (write_len < scm_port_buffer_can_put (write_buf, &end))
  216. {
  217. scm_c_write (port, src, write_len);
  218. return scm_from_long (write_len);
  219. }
  220. scm_flush (port);
  221. fdes = SCM_FPORT_FDES (port);
  222. }
  223. {
  224. long rv;
  225. SCM_SYSCALL (rv = write (fdes, src, write_len));
  226. if (rv == -1)
  227. {
  228. if (SCM_EBLOCK (errno))
  229. rv = 0;
  230. else
  231. SCM_SYSERROR;
  232. }
  233. scm_remember_upto_here_1 (str);
  234. return scm_from_long (rv);
  235. }
  236. }
  237. #undef FUNC_NAME
  238. SCM
  239. scm_init_rw_builtins ()
  240. {
  241. #include "rw.x"
  242. return SCM_UNSPECIFIED;
  243. }
  244. void
  245. scm_init_rw ()
  246. {
  247. scm_c_define_gsubr ("%init-rw-builtins", 0, 0, 0, scm_init_rw_builtins);
  248. }