2005-10-08 11:20:51 +00:00
|
|
|
/** \file io.c
|
|
|
|
|
|
|
|
Utilities for io redirection.
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-10-08 11:20:51 +00:00
|
|
|
*/
|
|
|
|
#include "config.h"
|
|
|
|
|
2006-02-28 13:17:16 +00:00
|
|
|
|
2005-10-08 11:20:51 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <wchar.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/types.h>
|
2006-08-09 22:53:38 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_SYS_TERMIOS_H
|
|
|
|
#include <sys/termios.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_SYS_IOCTL_H
|
2005-10-08 11:20:51 +00:00
|
|
|
#include <sys/ioctl.h>
|
2006-08-09 22:53:38 +00:00
|
|
|
#endif
|
|
|
|
|
2005-10-08 11:20:51 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
#if HAVE_NCURSES_H
|
|
|
|
#include <ncurses.h>
|
|
|
|
#else
|
|
|
|
#include <curses.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if HAVE_TERMIO_H
|
|
|
|
#include <termio.h>
|
|
|
|
#endif
|
|
|
|
|
2006-01-19 12:22:07 +00:00
|
|
|
#if HAVE_TERM_H
|
2005-10-08 11:20:51 +00:00
|
|
|
#include <term.h>
|
2006-01-19 12:22:07 +00:00
|
|
|
#elif HAVE_NCURSES_TERM_H
|
|
|
|
#include <ncurses/term.h>
|
|
|
|
#endif
|
2005-10-08 11:20:51 +00:00
|
|
|
|
2006-02-28 13:17:16 +00:00
|
|
|
#include "fallback.h"
|
2005-10-08 11:20:51 +00:00
|
|
|
#include "util.h"
|
2006-02-28 13:17:16 +00:00
|
|
|
|
2005-10-08 11:20:51 +00:00
|
|
|
#include "wutil.h"
|
|
|
|
#include "exec.h"
|
|
|
|
#include "common.h"
|
|
|
|
#include "io.h"
|
2006-07-19 22:55:49 +00:00
|
|
|
|
2005-10-08 11:20:51 +00:00
|
|
|
|
|
|
|
void io_buffer_read( io_data_t *d )
|
|
|
|
{
|
2005-10-11 19:31:16 +00:00
|
|
|
exec_close(d->param1.pipe_fd[1] );
|
2006-11-12 12:16:13 +00:00
|
|
|
|
2005-10-08 11:20:51 +00:00
|
|
|
if( d->io_mode == IO_BUFFER )
|
2011-12-27 03:18:46 +00:00
|
|
|
{
|
2005-12-11 22:27:30 +00:00
|
|
|
/* if( fcntl( d->param1.pipe_fd[0], F_SETFL, 0 ) )
|
2005-10-08 11:20:51 +00:00
|
|
|
{
|
|
|
|
wperror( L"fcntl" );
|
|
|
|
return;
|
2005-12-11 22:27:30 +00:00
|
|
|
} */
|
|
|
|
debug( 4, L"io_buffer_read: blocking read on fd %d", d->param1.pipe_fd[0] );
|
2005-10-08 11:20:51 +00:00
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
char b[4096];
|
2012-08-04 22:11:43 +00:00
|
|
|
long l;
|
2005-10-11 19:31:16 +00:00
|
|
|
l=read_blocked( d->param1.pipe_fd[0], b, 4096 );
|
2005-10-08 11:20:51 +00:00
|
|
|
if( l==0 )
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if( l<0 )
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
exec_read_io_buffer is only called on jobs that have
|
|
|
|
exited, and will therefore never block. But a broken
|
|
|
|
pipe seems to cause some flags to reset, causing the
|
|
|
|
EOF flag to not be set. Therefore, EAGAIN is ignored
|
|
|
|
and we exit anyway.
|
|
|
|
*/
|
|
|
|
if( errno != EAGAIN )
|
|
|
|
{
|
2011-12-27 03:18:46 +00:00
|
|
|
debug( 1,
|
|
|
|
_(L"An error occured while reading output from code block on file descriptor %d"),
|
2005-10-11 19:31:16 +00:00
|
|
|
d->param1.pipe_fd[0] );
|
2011-12-27 03:18:46 +00:00
|
|
|
wperror( L"io_buffer_read" );
|
2005-10-08 11:20:51 +00:00
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
|
|
|
break;
|
2005-10-08 11:20:51 +00:00
|
|
|
}
|
|
|
|
else
|
2011-12-27 03:18:46 +00:00
|
|
|
{
|
2012-03-04 10:35:30 +00:00
|
|
|
d->out_buffer_append( b, l );
|
2005-10-08 11:20:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-08-13 01:38:03 +00:00
|
|
|
io_data_t *io_buffer_create( int is_input )
|
2005-10-08 11:20:51 +00:00
|
|
|
{
|
2012-03-04 10:35:30 +00:00
|
|
|
std::auto_ptr<io_data_t> buffer_redirect(new io_data_t);
|
|
|
|
buffer_redirect->out_buffer_create();
|
2005-10-08 11:20:51 +00:00
|
|
|
buffer_redirect->io_mode=IO_BUFFER;
|
|
|
|
buffer_redirect->next=0;
|
2006-11-12 12:16:13 +00:00
|
|
|
buffer_redirect->is_input = is_input;
|
2006-08-13 01:38:03 +00:00
|
|
|
buffer_redirect->fd=is_input?0:1;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-10-11 19:31:16 +00:00
|
|
|
if( exec_pipe( buffer_redirect->param1.pipe_fd ) == -1 )
|
2005-10-08 11:20:51 +00:00
|
|
|
{
|
|
|
|
debug( 1, PIPE_ERROR );
|
|
|
|
wperror (L"pipe");
|
2012-03-04 10:35:30 +00:00
|
|
|
return NULL;
|
2005-10-08 11:20:51 +00:00
|
|
|
}
|
2005-10-11 19:31:16 +00:00
|
|
|
else if( fcntl( buffer_redirect->param1.pipe_fd[0],
|
2005-10-08 11:20:51 +00:00
|
|
|
F_SETFL,
|
|
|
|
O_NONBLOCK ) )
|
|
|
|
{
|
|
|
|
debug( 1, PIPE_ERROR );
|
|
|
|
wperror( L"fcntl" );
|
2012-03-04 10:35:30 +00:00
|
|
|
return NULL;
|
2005-10-08 11:20:51 +00:00
|
|
|
}
|
2012-03-04 07:56:35 +00:00
|
|
|
return buffer_redirect.release();
|
2005-10-08 11:20:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void io_buffer_destroy( io_data_t *io_buffer )
|
|
|
|
{
|
2006-11-12 12:16:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
If this is an input buffer, then io_read_buffer will not have
|
|
|
|
been called, and we need to close the output fd as well.
|
|
|
|
*/
|
|
|
|
if( io_buffer->is_input )
|
|
|
|
{
|
|
|
|
exec_close(io_buffer->param1.pipe_fd[1] );
|
|
|
|
}
|
|
|
|
|
2005-10-11 19:31:16 +00:00
|
|
|
exec_close( io_buffer->param1.pipe_fd[0] );
|
2005-10-08 11:20:51 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
Dont free fd for writing. This should already be free'd before
|
|
|
|
calling exec_read_io_buffer on the buffer
|
|
|
|
*/
|
2012-02-23 17:40:51 +00:00
|
|
|
delete io_buffer;
|
2005-10-08 11:20:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
io_data_t *io_add( io_data_t *list, io_data_t *element )
|
|
|
|
{
|
|
|
|
io_data_t *curr = list;
|
|
|
|
if( curr == 0 )
|
|
|
|
return element;
|
|
|
|
while( curr->next != 0 )
|
|
|
|
curr = curr->next;
|
|
|
|
curr->next = element;
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
io_data_t *io_remove( io_data_t *list, io_data_t *element )
|
|
|
|
{
|
|
|
|
io_data_t *curr, *prev=0;
|
|
|
|
for( curr=list; curr; curr = curr->next )
|
2011-12-27 03:18:46 +00:00
|
|
|
{
|
2005-10-08 11:20:51 +00:00
|
|
|
if( element == curr )
|
|
|
|
{
|
|
|
|
if( prev == 0 )
|
|
|
|
{
|
|
|
|
io_data_t *tmp = element->next;
|
|
|
|
element->next = 0;
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
prev->next = element->next;
|
|
|
|
element->next = 0;
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
prev = curr;
|
|
|
|
}
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2012-02-10 02:43:36 +00:00
|
|
|
io_data_t *io_duplicate( io_data_t *l )
|
2005-10-08 11:20:51 +00:00
|
|
|
{
|
|
|
|
io_data_t *res;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-10-08 11:20:51 +00:00
|
|
|
if( l == 0 )
|
|
|
|
return 0;
|
|
|
|
|
2012-02-10 02:43:36 +00:00
|
|
|
res = new io_data_t(*l);
|
|
|
|
res->next=io_duplicate(l->next );
|
2011-12-27 03:18:46 +00:00
|
|
|
return res;
|
2005-10-08 11:20:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
io_data_t *io_get( io_data_t *io, int fd )
|
|
|
|
{
|
2012-03-09 07:21:07 +00:00
|
|
|
if( io == NULL )
|
2005-10-08 11:20:51 +00:00
|
|
|
return 0;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-10-08 11:20:51 +00:00
|
|
|
io_data_t *res = io_get( io->next, fd );
|
|
|
|
if( res )
|
|
|
|
return res;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-10-08 11:20:51 +00:00
|
|
|
if( io->fd == fd )
|
|
|
|
return io;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-05-14 22:29:05 +00:00
|
|
|
|
|
|
|
void io_print( io_data_t *io )
|
2005-10-09 11:48:16 +00:00
|
|
|
{
|
|
|
|
if( !io )
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-08-13 01:38:03 +00:00
|
|
|
debug( 1, L"IO fd %d, type ", io->fd );
|
2005-10-09 11:48:16 +00:00
|
|
|
switch( io->io_mode )
|
|
|
|
{
|
|
|
|
case IO_PIPE:
|
2006-05-14 22:29:05 +00:00
|
|
|
debug( 1, L"PIPE, data %d", io->param1.pipe_fd[io->fd?1:0] );
|
2005-10-09 11:48:16 +00:00
|
|
|
break;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-10-09 11:48:16 +00:00
|
|
|
case IO_FD:
|
2006-05-14 22:29:05 +00:00
|
|
|
debug( 1, L"FD, copy %d", io->param1.old_fd );
|
2005-10-09 11:48:16 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case IO_BUFFER:
|
2006-05-14 22:29:05 +00:00
|
|
|
debug( 1, L"BUFFER" );
|
2005-10-09 11:48:16 +00:00
|
|
|
break;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-10-09 11:48:16 +00:00
|
|
|
default:
|
2006-05-14 22:29:05 +00:00
|
|
|
debug( 1, L"OTHER" );
|
2005-10-09 11:48:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
io_print( io->next );
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-10-09 11:48:16 +00:00
|
|
|
}
|