rw.c 8.5 KB

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