ioext.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /* Copyright 1995-2001,2003,2006,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. #ifdef HAVE_CONFIG_H
  16. # include <config.h>
  17. #endif
  18. #include <errno.h>
  19. #include <fcntl.h>
  20. #include <stdio.h>
  21. #include <unistd.h>
  22. #ifdef HAVE_IO_H
  23. #include <io.h>
  24. #endif
  25. #include "async.h"
  26. #include "dynwind.h"
  27. #include "extensions.h"
  28. #include "fdes-finalizers.h"
  29. #include "feature.h"
  30. #include "fports.h"
  31. #include "gsubr.h"
  32. #include "hashtab.h"
  33. #include "numbers.h"
  34. #include "pairs.h"
  35. #include "ports-internal.h"
  36. #include "ports.h"
  37. #include "strings.h"
  38. #include "syscalls.h"
  39. #include "weak-set.h"
  40. #include "ioext.h"
  41. SCM_DEFINE (scm_ftell, "ftell", 1, 0, 0,
  42. (SCM fd_port),
  43. "Return an integer representing the current position of\n"
  44. "@var{fd_port}, measured from the beginning. Equivalent to:\n"
  45. "\n"
  46. "@lisp\n"
  47. "(seek port 0 SEEK_CUR)\n"
  48. "@end lisp")
  49. #define FUNC_NAME s_scm_ftell
  50. {
  51. return scm_seek (fd_port, SCM_INUM0, scm_from_int (SEEK_CUR));
  52. }
  53. #undef FUNC_NAME
  54. SCM_DEFINE (scm_redirect_port, "redirect-port", 2, 0, 0,
  55. (SCM old, SCM new),
  56. "This procedure takes two ports and duplicates the underlying file\n"
  57. "descriptor from @var{old} into @var{new}. The\n"
  58. "current file descriptor in @var{new} will be closed.\n"
  59. "After the redirection the two ports will share a file position\n"
  60. "and file status flags.\n\n"
  61. "The return value is unspecified.\n\n"
  62. "Unexpected behaviour can result if both ports are subsequently used\n"
  63. "and the original and/or duplicate ports are buffered.\n\n"
  64. "This procedure does not have any side effects on other ports or\n"
  65. "revealed counts.")
  66. #define FUNC_NAME s_scm_redirect_port
  67. {
  68. int ans, oldfd, newfd;
  69. scm_t_fport *fp;
  70. old = SCM_COERCE_OUTPORT (old);
  71. new = SCM_COERCE_OUTPORT (new);
  72. SCM_VALIDATE_OPFPORT (1, old);
  73. SCM_VALIDATE_OPFPORT (2, new);
  74. oldfd = SCM_FPORT_FDES (old);
  75. fp = SCM_FSTREAM (new);
  76. newfd = fp->fdes;
  77. if (oldfd != newfd)
  78. {
  79. /* Ensure there is nothing in either port's input or output
  80. buffers. */
  81. if (SCM_OUTPUT_PORT_P (old))
  82. scm_flush (old);
  83. if (SCM_INPUT_PORT_P (old) && SCM_PORT (old)->rw_random)
  84. scm_end_input (old);
  85. if (SCM_OUTPUT_PORT_P (new))
  86. scm_flush (new);
  87. if (SCM_INPUT_PORT_P (new) && SCM_PORT (new)->rw_random)
  88. scm_end_input (new);
  89. ans = dup2 (oldfd, newfd);
  90. if (ans == -1)
  91. SCM_SYSERROR;
  92. SCM_PORT (new)->rw_random = SCM_PORT (old)->rw_random;
  93. }
  94. return SCM_UNSPECIFIED;
  95. }
  96. #undef FUNC_NAME
  97. SCM_DEFINE (scm_dup_to_fdes, "dup->fdes", 1, 1, 0,
  98. (SCM fd_or_port, SCM fd),
  99. "Return a new integer file descriptor referring to the open file\n"
  100. "designated by @var{fd_or_port}, which must be either an open\n"
  101. "file port or a file descriptor.")
  102. #define FUNC_NAME s_scm_dup_to_fdes
  103. {
  104. int oldfd, newfd, rv;
  105. fd_or_port = SCM_COERCE_OUTPORT (fd_or_port);
  106. if (scm_is_integer (fd_or_port))
  107. oldfd = scm_to_int (fd_or_port);
  108. else
  109. {
  110. SCM_VALIDATE_OPFPORT (1, fd_or_port);
  111. oldfd = SCM_FPORT_FDES (fd_or_port);
  112. }
  113. if (SCM_UNBNDP (fd))
  114. {
  115. newfd = dup (oldfd);
  116. if (newfd == -1)
  117. SCM_SYSERROR;
  118. fd = scm_from_int (newfd);
  119. }
  120. else
  121. {
  122. newfd = scm_to_int (fd);
  123. if (oldfd != newfd)
  124. {
  125. scm_evict_ports (newfd); /* see scsh manual. */
  126. rv = dup2 (oldfd, newfd);
  127. if (rv == -1)
  128. SCM_SYSERROR;
  129. }
  130. }
  131. return fd;
  132. }
  133. #undef FUNC_NAME
  134. SCM_DEFINE (scm_dup2, "dup2", 2, 0, 0,
  135. (SCM oldfd, SCM newfd),
  136. "A simple wrapper for the @code{dup2} system call.\n"
  137. "Copies the file descriptor @var{oldfd} to descriptor\n"
  138. "number @var{newfd}, replacing the previous meaning\n"
  139. "of @var{newfd}. Both @var{oldfd} and @var{newfd} must\n"
  140. "be integers.\n"
  141. "Unlike for dup->fdes or primitive-move->fdes, no attempt\n"
  142. "is made to move away ports which are using @var{newfd}.\n"
  143. "The return value is unspecified.")
  144. #define FUNC_NAME s_scm_dup2
  145. {
  146. int c_oldfd;
  147. int c_newfd;
  148. int rv;
  149. c_oldfd = scm_to_int (oldfd);
  150. c_newfd = scm_to_int (newfd);
  151. rv = dup2 (c_oldfd, c_newfd);
  152. if (rv == -1)
  153. SCM_SYSERROR;
  154. return SCM_UNSPECIFIED;
  155. }
  156. #undef FUNC_NAME
  157. SCM_DEFINE (scm_fileno, "fileno", 1, 0, 0,
  158. (SCM port),
  159. "Return the integer file descriptor underlying @var{port}. Does\n"
  160. "not change its revealed count.")
  161. #define FUNC_NAME s_scm_fileno
  162. {
  163. port = SCM_COERCE_OUTPORT (port);
  164. SCM_VALIDATE_OPFPORT (1, port);
  165. return scm_from_int (SCM_FPORT_FDES (port));
  166. }
  167. #undef FUNC_NAME
  168. /* GJB:FIXME:: why does this not throw
  169. an error if the arg is not a port?
  170. This proc as is would be better names isattyport?
  171. if it is not going to assume that the arg is a port
  172. [cmm] I don't see any problem with the above. why should a type
  173. predicate assume _anything_ about its argument?
  174. */
  175. SCM_DEFINE (scm_isatty_p, "isatty?", 1, 0, 0,
  176. (SCM port),
  177. "Return @code{#t} if @var{port} is using a serial non--file\n"
  178. "device, otherwise @code{#f}.")
  179. #define FUNC_NAME s_scm_isatty_p
  180. {
  181. int rv;
  182. port = SCM_COERCE_OUTPORT (port);
  183. if (!SCM_OPFPORTP (port))
  184. return SCM_BOOL_F;
  185. rv = isatty (SCM_FPORT_FDES (port));
  186. return scm_from_bool(rv);
  187. }
  188. #undef FUNC_NAME
  189. SCM_DEFINE (scm_fdopen, "fdopen", 2, 0, 0,
  190. (SCM fdes, SCM modes),
  191. "Return a new port based on the file descriptor @var{fdes}.\n"
  192. "Modes are given by the string @var{modes}. The revealed count\n"
  193. "of the port is initialized to zero. The modes string is the\n"
  194. "same as that accepted by @ref{File Ports, open-file}.")
  195. #define FUNC_NAME s_scm_fdopen
  196. {
  197. return scm_i_fdes_to_port (scm_to_int (fdes),
  198. scm_i_mode_bits (modes), SCM_BOOL_F,
  199. SCM_FPORT_OPTION_VERIFY);
  200. }
  201. #undef FUNC_NAME
  202. /* Move a port's underlying file descriptor to a given value.
  203. * Returns #f if fdes is already the given value.
  204. * #t if fdes moved.
  205. * MOVE->FDES is implemented in Scheme and calls this primitive.
  206. */
  207. SCM_DEFINE (scm_primitive_move_to_fdes, "primitive-move->fdes", 2, 0, 0,
  208. (SCM port, SCM fd),
  209. "Moves the underlying file descriptor for @var{port} to the integer\n"
  210. "value @var{fd} without changing the revealed count of @var{port}.\n"
  211. "Any other ports already using this descriptor will be automatically\n"
  212. "shifted to new descriptors and their revealed counts reset to zero.\n"
  213. "The return value is @code{#f} if the file descriptor already had the\n"
  214. "required value or @code{#t} if it was moved.")
  215. #define FUNC_NAME s_scm_primitive_move_to_fdes
  216. {
  217. scm_t_fport *stream;
  218. int old_fd;
  219. int new_fd;
  220. int rv;
  221. port = SCM_COERCE_OUTPORT (port);
  222. SCM_VALIDATE_OPFPORT (1, port);
  223. stream = SCM_FSTREAM (port);
  224. old_fd = stream->fdes;
  225. new_fd = scm_to_int (fd);
  226. if (old_fd == new_fd)
  227. {
  228. return SCM_BOOL_F;
  229. }
  230. scm_evict_ports (new_fd);
  231. rv = dup2 (old_fd, new_fd);
  232. if (rv == -1)
  233. SCM_SYSERROR;
  234. stream->fdes = new_fd;
  235. scm_run_fdes_finalizers (old_fd);
  236. SCM_SYSCALL (close (old_fd));
  237. return SCM_BOOL_T;
  238. }
  239. #undef FUNC_NAME
  240. static SCM
  241. get_matching_port (void *closure, SCM port, SCM result)
  242. {
  243. int fd = * (int *) closure;
  244. if (SCM_OPFPORTP (port) && SCM_FSTREAM (port)->fdes == fd)
  245. result = scm_cons (port, result);
  246. return result;
  247. }
  248. /* Return a list of ports using a given file descriptor. */
  249. SCM_DEFINE (scm_fdes_to_ports, "fdes->ports", 1, 0, 0,
  250. (SCM fd),
  251. "Return a list of existing ports which have @var{fd} as an\n"
  252. "underlying file descriptor, without changing their revealed\n"
  253. "counts.")
  254. #define FUNC_NAME s_scm_fdes_to_ports
  255. {
  256. SCM result = SCM_EOL;
  257. int int_fd = scm_to_int (fd);
  258. result = scm_c_weak_set_fold (get_matching_port,
  259. (void*) &int_fd, result,
  260. scm_i_port_weak_set);
  261. return result;
  262. }
  263. #undef FUNC_NAME
  264. static void
  265. scm_init_ice_9_ioext (void)
  266. {
  267. #include "ioext.x"
  268. }
  269. void
  270. scm_init_ioext ()
  271. {
  272. scm_add_feature ("i/o-extensions");
  273. scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION,
  274. "scm_init_ice_9_ioext",
  275. (scm_t_extension_init_func) scm_init_ice_9_ioext,
  276. NULL);
  277. }