io.texi 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. @node POSIX I/O utilities
  2. @section I/O utilities
  3. @stindex posix-i/o
  4. These procedures for manipulating pipes and ports built on file
  5. descriptors are provided by the structures @code{posix-i/o} &
  6. @code{posix}.
  7. @cindex POSIX pipe I/O
  8. @cindex pipe I/O
  9. @deffn procedure open-pipe @returns{} [input-port output-port]
  10. Creates a pipe and returns the two ends of the pipe as an input port &
  11. an output port.
  12. @end deffn
  13. @cindex file descriptor ports
  14. @cindex fd-ports
  15. A @dfn{file descriptor port} (or @dfn{fd-port}) is a port or a
  16. @embedref{Channels, channel} that reads from or writes to an OS file
  17. descriptor. File descriptor ports are returned by the standard Scheme
  18. procedures @code{open-input-file} & @code{open-output-file} as well as
  19. the procedures @code{open-file} & @code{open-pipe} from this POSIX
  20. interface.
  21. @deffn procedure fd-port? port @returns{} boolean
  22. @deffnx procedure port->fd port @returns{} integer or @code{#f}
  23. @code{Fd-port?} returns true if @var{port} is a port that reads from or
  24. writes to a file descriptor, or false if not. @code{Port->fd} returns
  25. the file descriptor that @var{port} reads from or writes to, if it is a
  26. file descriptor port, or @code{#f} if it is not. It is an error to
  27. pass a value that is not a port to either of these procedures.
  28. @strong{Note:} Channels may @emph{not} be passed to these procedures.
  29. To access a channel's file descriptor, use @code{channel-os-index};
  30. @pxref{Channels} for more details.
  31. @end deffn
  32. @cindex file descriptor reassignment
  33. @deffn procedure remap-file-descriptors! fd-spec @dots{} @returns{} unspecified
  34. Reassigns file descriptors to ports. Each @var{fd-spec} specifies what
  35. port is to be mapped to what file descriptor: the first port gets file
  36. descriptor @code{0}; the second, @code{1}; and so on. An @var{fd-spec}
  37. is either a port that reads from or writes to a file descriptor or
  38. @code{#f}; in the latter case, the corresponding file descriptor is not
  39. used. Any open ports not listed are marked @emph{close-on-exec}. The
  40. same port may be moved to multiple new file descriptors.
  41. For example,
  42. @lisp
  43. (remap-file-descriptors! (current-output-port)
  44. #f
  45. (current-input-port))@end lisp
  46. @noindent
  47. moves the current output port to file descriptor @code{0} (@ie{}
  48. @code{stdin}) and the current input port to file descriptor @code{2}
  49. (@ie{} @code{stderr}). File descriptor @code{1} (@code{stdout}) is not
  50. mapped to anything, and all other open ports (including anything that
  51. had the file descriptor @code{1}) are marked @emph{close-on-exec}.
  52. @end deffn
  53. @cindex fd-port dup'ing
  54. @deffn procedure dup fd-port @returns{} fd-port
  55. @deffnx procedure dup-switching-mode fd-port @returns{} fd-port
  56. @deffnx procedure dup2 fd-port fdes @returns{} fd-port
  57. These change @var{fd-port}'s file descriptor and return new ports that
  58. have the ports' old file descriptors. @code{Dup} uses the lowest
  59. unused file descriptor; @code{dup2} uses the one provided.
  60. @code{Dup-switching-mode} is the same as @code{dup} except that the
  61. returned port is an input port if the argument was an output port and
  62. vice versa. If any existing port uses the file descriptor passed to
  63. @code{dup2}, that port is closed.
  64. @end deffn
  65. @deffn procedure close-all-port port-or-channel @dots{} @returns unspecified
  66. Closes all ports or channels not listed as arguments.
  67. @end deffn
  68. @deffn procedure close-on-exec? channel @returns{} boolean
  69. @deffnx procedure set-close-on-exec?! channel boolean @returns{} unspecified
  70. These access the boolean flag that specifies whether @var{channel} will
  71. be closed when a new program is @code{exec}'d.
  72. @end deffn
  73. @cindex @code{fcntl}
  74. @cindex POSIX @code{fcntl}
  75. @cindex fd-port I/O flags
  76. @cindex I/O flags
  77. @deffn procedure i/o-flags fd-port @returns{} file-options
  78. @deffnx procedure set-i/o-flags! fd-port file-options @returns{} unspecified
  79. These access various @embedref{POSIX file system access, file options}
  80. for @var{fd-port}. The options that may be read are @code{append},
  81. @code{nonblocking}, @code{read-only}, @code{read-write}, and
  82. @code{write-only}; only the @code{append} and @code{nonblocking}
  83. options can be written.
  84. @end deffn
  85. @cindex terminal ports
  86. @cindex POSIX terminal ports
  87. @deffn procedure port-is-a-terminal? port @returns{} boolean
  88. @deffnx procedure port-terminal-name port @returns{} string or @code{#f}
  89. @code{Port-is-a-terminal?} returns true of @var{port} is a port that
  90. has an underlying file descriptor associated with a terminal. For such
  91. ports, @code{port-terminal-name} returns the name of the terminal; for
  92. all others, it returns @code{#f}.
  93. @strong{Note:} These procedures accept only ports, not channels.
  94. @end deffn