ioext.c 9.0 KB

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