/* UNIX Unified Stream Protocol Header File

   Copyright 1986 by the Massachusetts Institute of Technology
   See permission and disclaimer notice in file "notice.h"
 */

#ifndef USP_H_INCLUDED
#define USP_H_INCLUDED 1

/*
   The following are filled in by the configure script.
   usp.h should not include "config.h" because usp.h gets installed
   and config.h does not.
*/
/* The number of bytes in a int.  */
#undef SIZEOF_INT

/* The number of bytes in a long.  */
#undef SIZEOF_LONG

/* useful constants */

#define CLOSED		       99       /* connection ahs been closed */
#define EOB_FLAG               0x8000   /* end of block flag */
#define MAX_SUB_BLOCK_LENGTH   508	/* max sub-block length less hdr len */

#define CONNECTION_OPEN        10       /* USP connection-open block type */
#define CONNECTION_ERROR       20       /* USP connection-error block type */
#define CONNECTION_END         254      /* USP connection-end block type */
#define CONNECTION_END_REPLY   255      /* USP connection-end-reply block type */


#define FOREIGN_DETECT         1        /* error detected by foreign USP */
#define TRANSPORT_DETECT       2        /* error detected by transport layer */
#define INTERMEDIATE_DETECT    3        /* error detected by protocol converter */

/* USP internal error codes transmitted in CONNECTION_ERROR block */

#define CE_UNKNOWN             1

/* during connection establishment */

#define CE_NAME_UNKNOWN        10
#define CE_SERVICE_UNKNOWN     11
#define CE_HOST_DOWN           12
#define CE_SERVICE_UNSUPPORTED 13
#define CE_SERVICE_UNAVAILABLE 14
#define CE_CONVERSION_UNAVAILABLE 15

/* after connection establishment */


#define CE_TRANSPORT_FAILURE   20
#define CE_TRANSPORT_TIMEOUT   21
#define CE_CLIENT_FAILURE      22
#define CE_PATH_TRANSLATION    23

/* internal choice numbers for USP host names */

#define GLOBAL_NAME            0
#define PATH_NAME              1


/* USP-to-client error codes */

#include <usp/usp_et.h>

/* USP data types */

typedef unsigned char Byte;

typedef unsigned short USPBoolean;
typedef short USPInteger;
typedef unsigned short USPCardinal;
#if (SIZEOF_LONG == 4)
typedef long USPLong_integer;
typedef unsigned long USPLong_cardinal;
#elif (SIZEOF_INT == 4)
typedef int USPLong_integer;
typedef unsigned int USPLong_cardinal;
#endif
typedef char *USPString;

typedef struct {
    FILE	*us_read;
    FILE        *us_write;
    int	    	us_in_receiving_p;	    /* input: receiving block? */
    unsigned    us_last_sub_block_p;	    /* last sub-block flag */
    unsigned    us_nunread_sub_block_bytes; /* #bytes remaining in subblock */
    int         us_out_sending_p;	    /* output: sending block? */
    unsigned    us_nsent_sub_block_bytes;   /* buffer pointer */
    char    	us_outbuf[MAX_SUB_BLOCK_LENGTH];    /* output sub-block buf */
} USPStream;

/* block.c */
int USP_rcv_blk (USPStream *, USPCardinal *);
int USP_begin_block (USPStream *, USPCardinal);
int USP_end_block (USPStream *);
int USP_flush_block (USPStream *);
int USP_end_of_block_p (USPStream *);

/* get.c */
int USP_get_boolean (USPStream *, USPBoolean *);
int USP_get_integer (USPStream *, USPInteger *);
int USP_get_cardinal (USPStream *, USPCardinal *);
int USP_get_long_integer (USPStream *, USPLong_integer *);
int USP_get_long_cardinal (USPStream *, USPLong_cardinal *);
int USP_get_string (USPStream *, USPString *);
int USP_get_byte_block (USPStream *, Byte *, unsigned, unsigned *);

/* main.c */
USPStream *USP_make_connection (char *, char *);
USPStream *USP_associate (int);
int USP_close_connection (USPStream *);
int USP_shutdown (USPStream *);

/* put.c */
int USP_put_boolean (USPStream *, USPBoolean);
int USP_put_integer (USPStream *, USPInteger);
int USP_put_cardinal (USPStream *, USPCardinal);
int USP_put_long_integer (USPStream *, USPLong_integer);
int USP_put_long_cardinal (USPStream *, USPLong_cardinal);
int USP_put_string (USPStream *, USPString);
int USP_put_byte_block (USPStream *, Byte *, unsigned);

#endif
