Next: Reading and Printing Up: Ports Previous: File-Stream Ports

## Custom Ports

The `make-input-port` and `make-output-port` procedures create ports with arbitrary control procedures:

- `(make-input-port read-char-proc char-ready?-proc close-proc [peek-char-proc])` creates an input port.

  The read-char-proc argument is a procedure that takes no arguments and returns the next input character from the port. When no more characters are available from the port, read-char-proc returns `eof`. (If a non-character and non-`eof` value is returned, the `exn:i/o:port:user` exception is raised.)

  The char-ready?-proc argument is a procedure that takes no arguments and returns a true value if a character (or `eof`) is ready to be read, `#f` otherwise.

  The close-proc argument is a procedure of no arguments to be called when the port is closed.

  The peek-char-proc argument is a procedure that takes no arguments and returns the next input character from the port, but also saves the character for the next read-char-proc or peek-char-proc call. If peek-char-proc is not provided, the default procedure uses read-char-proc. If peek-char-proc is provided, MzScheme does not check that calling read-char-proc obtains the value returned by a previous peek-char-proc call.

- `(make-output-port write-proc close-proc)` creates an output port.

  The write-proc argument is a procedure that takes a string and writes it. (The write-proc procedure can safely store or mutate this string.)

  The close-proc argument is a procedure of no arguments; it is called when the port is closed.

Ports created by `make-input-port` and `make-output-port` are immediately open for reading or writing. If the close procedure does not have any side-effects, then the custom port does not need to be explicitly closed.