/* libsizzle/fdport.h - file descriptor port handling
 
  Copyright (C) 1999, 2000 Martin Grabmueller <mgrabmue@cs.tu-berlin.de>
 
  This is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2, or (at your option)
  any later version.
  
  This software is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  
  You should have received a copy of the GNU General Public License
  along with this package; see the file COPYING.  If not, write to the
  Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
  MA 02111-1307, USA.  */

#ifndef __SIZZLE_FDPORT_H__
#define __SIZZLE_FDPORT_H__

#if HAVE_CONFIG_H
# include <config.h>
#endif 

#if __SIZZLE_IMPORT__
# define SIZZLE_API __declspec (dllimport)
#elif __SIZZLE_EXPORT__ 
# define SIZZLE_API __declspec (dllexport)
#else
# define SIZZLE_API
#endif

#include "node.h"

/* Type descriminator and accessor macros for string ports.  */
#define fdport_p(c)        (port_p(c) && port_ptype(c) == zzz_fdport_port_type)
#define fdport_fd(c)       (((fdport_info_t) port_info(c))->fd)
#define fdport_rptr(c)     (((fdport_info_t) port_info(c))->rbuf_ptr)
#define fdport_rend(c)     (((fdport_info_t) port_info(c))->rbuf_end)
#define fdport_rbuf(c)     (((fdport_info_t) port_info(c))->rbuffer)
#define fdport_rbufsize(c) (((fdport_info_t) port_info(c))->rbuf_size)
#define fdport_wptr(c)     (((fdport_info_t) port_info(c))->wbuf_ptr)
#define fdport_wend(c)     (((fdport_info_t) port_info(c))->wbuf_end)
#define fdport_wbuf(c)     (((fdport_info_t) port_info(c))->wbuffer)
#define fdport_wbufsize(c) (((fdport_info_t) port_info(c))->wbuf_size)

/* Info structure for file descriptor ports.  */
typedef struct fdport_info * fdport_info_t;
struct fdport_info
{
  int fd;
  unsigned rbuf_size;
  char * rbuffer;
  char * rbuf_ptr;
  char * rbuf_end;
  unsigned wbuf_size;
  char * wbuffer;
  char * wbuf_ptr;
  char * wbuf_end;
};

/* Port type identifier for file descriptor objects.  */
SIZZLE_API extern zzz_object_t zzz_fdport_port_type;

/* These file descriptor objects are connected to the standard
   input/output/error descriptors.  */
SIZZLE_API extern zzz_scm_t zzz_stdin_fdport;
SIZZLE_API extern zzz_scm_t zzz_stdout_fdport;
SIZZLE_API extern zzz_scm_t zzz_stderr_fdport;

/* Create a file descriptor port for file descriptor @var{fd}.  The
   port is readable if @var{read} is true and writable if @var{write}
   is true.  */
SIZZLE_API zzz_scm_t zzz_make_fdport (int fd, int read, int write);

/* Initialize the file descriptor port module.  Register the file
   descriptor port type and create the standard ports.  */
SIZZLE_API result_t zzz_init_fdport (void);

#endif /* not __SIZZLE_FDPORT_H__ */
