ioext.c 8.6 KB

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