ioext.c 8.8 KB

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