parrotcode: Generic IO | |
Contents | C |
src/io/io.c - Generic IO
The Parrot IO subsystem uses a per-interpreter stack to provide a layer-based approach to IO.
Each layer implements a subset of the ParrotIOLayerAPI
vtable.
To find an IO function,
Parrot searches the layer stack downwards until it finds a non-NULL
function pointer for that particular slot.
This file implements the generic functionality. Specific layers are in separate files: src/io/io_buf.c, src/io/io_stdio.c, src/io/io_unix.c, src/io/io_win32.c, and src/io/io_layers.c.
The ParrotIO
PMC provides the class-based interface that is used in Parrot ops.
The ParrotIO struct
is defined in src/io/io_private.h.
PARROT_API PARROT_WARN_UNUSED_RESULT PARROT_CANNOT_RETURN_NULL PMC *new_io_pmc(PARROT_INTERP, NULLOK(ParrotIO *io))
ParrotIO
PMC.PARROT_WARN_UNUSED_RESULT PARROT_CANNOT_RETURN_NULL STRING *PIO_make_io_string(PARROT_INTERP, NOTNULL(STRING **buf), size_t len)
buf
parameter can:strstart
tells this function to allocate the STRING memory.PARROT_API PARROT_WARN_UNUSED_RESULT PARROT_CANNOT_RETURN_NULL ParrotIO *PIO_new(PARROT_INTERP, SHIM(INTVAL iotype), INTVAL flags, INTVAL mode)
flags
and mode
are set in the returned ParrotIO
.iotype
is unused.PARROT_API void PIO_destroy(SHIM_INTERP, NOTNULL(PMC *pmc))
PARROT_API void PIO_init(PARROT_INTERP)
STD*
handles.PARROT_API void PIO_finish(PARROT_INTERP)
PARROT_API void PIO_internal_shutdown(SHIM_INTERP)
PARROT_API INTVAL PIO_init_stacks(PARROT_INTERP)
PARROT_API INTVAL PIO_base_init(SHIM_INTERP, SHIM(ParrotIOLayer *l))
INIT {}
block.0
.ParrotIO
interface PARROT_API PARROT_WARN_UNUSED_RESULT INTVAL PIO_parse_open_flags(NULLOK(const char *flagstr))
*flagstr
for Perl-style file open mode flags (<
,
>
,
>>
,
+<
,
+>
) and returns the combined generic bit flags.PARROT_API INTVAL PIO_peek(PARROT_INTERP, NOTNULL(PMC *pmc), NOTNULL(STRING **buffer))
PARROT_API INTVAL PIO_pioctl(PARROT_INTERP, NOTNULL(PMC *pmc), INTVAL cmd, INTVAL arg)
PIOCTL*
values in include/parrot/io.h.set
operations return 0
on success and a negative value on error.
get
operations use the return value as the value requested,
but should always be >= 0
.
A negative value indicates an error.
This may be too limited,
but we will see.
--MelvinPARROT_API INTVAL PIO_setbuf(PARROT_INTERP, NOTNULL(PMC *pmc), size_t bufsize)
*pmc
to bufsize
.
Returns 0
if the buffering was enabled.PARROT_API INTVAL PIO_setlinebuf(PARROT_INTERP, NOTNULL(PMC *pmc))
*pmc
.
Returns 0
if line buffering was successfully set,
or already enabled.PARROT_API PARROT_WARN_UNUSED_RESULT PARROT_CANNOT_RETURN_NULL PMC *PIO_open(PARROT_INTERP, NULLOK(ParrotIOLayer *layer), NOTNULL(const char *spath), NOTNULL(const char *sflags))
ParrotIO
PMC for *spath
.PARROT_API PARROT_WARN_UNUSED_RESULT PARROT_CANNOT_RETURN_NULL PMC *PIO_fdopen(PARROT_INTERP, NULLOK(ParrotIOLayer *layer), PIOHANDLE fd, NOTNULL(const char *sflags))
ParrotIO
PMC for *spath
on an existing,
open file descriptor.STD*
IO handles onto the OS IO handles (0,
1,
2).PARROT_API INTVAL PIO_close(PARROT_INTERP, NULLOK(PMC *pmc))
ParrotIO
PMC *pmc
.PARROT_API void PIO_flush(PARROT_INTERP, NOTNULL(PMC *pmc))
ParrotIO
PMC *pmc
.PARROT_API PARROT_WARN_UNUSED_RESULT PARROT_CANNOT_RETURN_NULL STRING *PIO_reads(PARROT_INTERP, NOTNULL(PMC *pmc), size_t len)
STRING*
holding up to len
bytes.PARROT_API PARROT_WARN_UNUSED_RESULT INTVAL PIO_read(PARROT_INTERP, NOTNULL(PMC *pmc), NOTNULL(char *buffer), size_t len)
len
bytes from *pmc
and copies them into *buffer
.PARROT_API PARROT_WARN_UNUSED_RESULT INTVAL PIO_write(PARROT_INTERP, NOTNULL(PMC *pmc), NOTNULL(const void *buffer), size_t len)
len
bytes from *buffer
to *pmc
.PARROT_API PARROT_WARN_UNUSED_RESULT PIOOFF_T PIO_seek(PARROT_INTERP, NOTNULL(PMC *pmc), PIOOFF_T offset, INTVAL w)
*pmc
to offset bytes
from the position indicated by w
.
Typically w
will be 0
for the start of the file,
1
for the current position,
and 2
for the end.PARROT_API PARROT_WARN_UNUSED_RESULT PIOOFF_T PIO_tell(PARROT_INTERP, NOTNULL(PMC *pmc))
*pmc
.PARROT_API PARROT_WARN_UNUSED_RESULT INTVAL PIO_eof(SHIM_INTERP, NOTNULL(PMC *pmc))
*pmc
's current read/write position is EOF
.PARROT_API INTVAL PIO_puts(PARROT_INTERP, NOTNULL(PMC *pmc), NOTNULL(const char *s))
*s
tp *pmc
.
C string version.PARROT_API INTVAL PIO_putps(PARROT_INTERP, NOTNULL(PMC *pmc), NULLOK(STRING *s))
*s
to *pmc
.
Parrot string version.PARROT_API INTVAL PIO_fprintf(PARROT_INTERP, NOTNULL(PMC *pmc), NOTNULL(const char *s), ...)
*pmc
.PARROT_API INTVAL PIO_printf(PARROT_INTERP, NOTNULL(const char *s), ...)
stdout
.PARROT_API INTVAL PIO_eprintf(NULLOK(PARROT_INTERP), NOTNULL(const char *s), ...)
stderr
.PARROT_API PARROT_WARN_UNUSED_RESULT PIOHANDLE PIO_getfd(SHIM_INTERP, NOTNULL(PMC *pmc))
*pmc
's file descriptor,
or 0
if it is not defined.PIO_STD*
Functions PARROT_API PARROT_WARN_UNUSED_RESULT PARROT_CANNOT_RETURN_NULL PMC *PIO_STDIN(PARROT_INTERP)
ParrotIO
PMC for stdin
.PARROT_API PARROT_WARN_UNUSED_RESULT PARROT_CANNOT_RETURN_NULL PMC *PIO_STDOUT(PARROT_INTERP)
ParrotIO
PMC for stdout
.PARROT_API PARROT_WARN_UNUSED_RESULT PARROT_CANNOT_RETURN_NULL PMC *PIO_STDERR(PARROT_INTERP)
ParrotIO
PMC for stderr
.PARROT_API void Parrot_IOData_mark(PARROT_INTERP, NOTNULL(ParrotIOData *piodata))
trace_active_PMCs()
to mark the IO data live.These are used to create offsets for the seek
op.
PIOOFF_T PIO_make_offset(INTVAL offset)
offset
.PIOOFF_T PIO_make_offset32(INTVAL hi, INTVAL lo)
hi
is shifted 32 bytes to the left and or
ed together with lo
.
This allows 64-bit seeks with only 32-bit INTVALS
.PIOOFF_T PIO_make_offset_pmc(PARROT_INTERP, NOTNULL(PMC *pmc))
get_integer
vtable method on *pmc
.PARROT_API INTVAL PIO_poll(PARROT_INTERP, NOTNULL(PMC *pmc), INTVAL which, INTVAL sec, INTVAL usec)
*pmc
for the events in which
every sec
seconds + usec
microseconds.PARROT_API PARROT_WARN_UNUSED_RESULT PARROT_CANNOT_RETURN_NULL PMC *PIO_socket(PARROT_INTERP, INTVAL fam, INTVAL type, INTVAL proto)
PARROT_API INTVAL PIO_recv(PARROT_INTERP, NOTNULL(PMC *pmc), NOTNULL(STRING **buf))
*pmc
in *buf
.
Returns -1
if it fails.PARROT_API PARROT_WARN_UNUSED_RESULT INTVAL PIO_send(PARROT_INTERP, NOTNULL(PMC *pmc), NOTNULL(STRING *buf))
*buf
to the connected socket *pmc
.
Returns -1
if it cannot send the message.PARROT_API INTVAL PIO_connect(PARROT_INTERP, NOTNULL(PMC *pmc), NOTNULL(STRING *address))
*pmc
to *address
.
Returns -1
on failure.PARROT_API INTVAL PIO_bind(PARROT_INTERP, NOTNULL(PMC *pmc), NOTNULL(STRING *address))
*pmc
's socket to the local address and port specified by *address
.
Returns -1
on failure.PARROT_API PARROT_WARN_UNUSED_RESULT INTVAL PIO_listen(PARROT_INTERP, NOTNULL(PMC *pmc), INTVAL backlog)
*pmc
.
Returns -1
on failure.PARROT_API PARROT_WARN_UNUSED_RESULT PARROT_CAN_RETURN_NULL PMC *PIO_accept(PARROT_INTERP, NOTNULL(PMC *pmc))
ParrotIO
socket.
Returns NULL
on failure.PARROT_API PARROT_WARN_UNUSED_RESULT INTVAL PIO_isatty(SHIM_INTERP, NOTNULL(PMC *pmc))
*pmc
is a console/tty.io/io_buf.c, io/io_passdown.c, io/io_stdio.c, io/io_unix.c, io/io_win32.c, io/io_private.h.
Initially written by Melvin Smith.
Some ideas and goals from Perl 5.7 and Nick Ing-Simmons' work.
Rework to use copy-on-write IO stacks rather than creating a new stack for each IO stream.
Add support for loadable layers in Parrot bytecode.
|