
typedef	struct	Biobuf	Biobuf;

enum
{
	Bsize		= 8*1024,
	Bungetsize	= 4,		/* space for ungetc */
	Bmagic		= 0x314159,
	Beof		= -1,
	Bbad		= -2,

	Binactive	= 0,		/* states */
	Bractive,
	Bwactive,
	Bracteof,

	Bend
};

struct	Biobuf
{
	int	icount;		/* neg num of bytes at eob */
	int	ocount;		/* num of bytes at bob */
	int	rdline;		/* num of bytes after rdline */
	int	runesize;	/* num of bytes of last getrune */
	int	state;		/* r/w/inactive */
	int	fid;		/* open file */
	int	flag;		/* magic if malloc'ed */
	long	offset;		/* offset of buffer in file */
	int	bsize;		/* size of buffer */
	uchar*	bbuf;		/* pointer to beginning of buffer */
	uchar*	ebuf;		/* pointer to end of buffer */
	uchar*	gbuf;		/* pointer to good data in buf */
	uchar	b[Bungetsize+Bsize];
};

#define	BGETC(bp)\
	((bp)->icount?(bp)->bbuf[(bp)->bsize+(bp)->icount++]:Bgetc((bp)))
#define	BPUTC(bp,c)\
	((bp)->ocount?(bp)->bbuf[(bp)->bsize+(bp)->ocount++]=(c),0:Bputc((bp),(c)))
#define	BOFFSET(bp)\
	(((bp)->state==Bractive)?\
		(bp)->offset + (bp)->icount:\
	(((bp)->state==Bwactive)?\
		(bp)->offset + ((bp)->bsize + (bp)->ocount):\
		-1))
#define	BLINELEN(bp)\
	(bp)->rdline
#define	BFILDES(bp)\
	(bp)->fid

int	pm_Bbuffered(Biobuf*);
int	ns_Bbuffered(Biobuf*);
int	pm_Bfildes(Biobuf*);
int	ns_Bfildes(Biobuf*);
int	pm_Bflush(Biobuf*);
int	ns_Bflush(Biobuf*);
int	pm_Bgetc(Biobuf*);
int	ns_Bgetc(Biobuf*);
int	pm_Bgetd(Biobuf*, double*);
int	ns_Bgetd(Biobuf*, double*);
long	pm_Bgetrune(Biobuf*);
long	ns_Bgetrune(Biobuf*);
int	pm_Binit(Biobuf*, int, int);
int	ns_Binit(Biobuf*, int, int);
int	pm_Binits(Biobuf*, int, int, uchar*, int);
int	ns_Binits(Biobuf*, int, int, uchar*, int);
int	pm_Blinelen(Biobuf*);
int	ns_Blinelen(Biobuf*);
long	pm_Boffset(Biobuf*);
long	ns_Boffset(Biobuf*);
Biobuf*	pm_Bopen(char*, int);
Biobuf*	ns_Bopen(char*, int);
int pm_Bprint(Biobuf*, char*, ...);
int	ns_Bprint(Biobuf*, char*, ...);
int	pm_Bputc(Biobuf*, int);
int	ns_Bputc(Biobuf*, int);
int	pm_Bputrune(Biobuf*, long);
int	ns_Bputrune(Biobuf*, long);
void*	pm_Brdline(Biobuf*, int);
void*	ns_Brdline(Biobuf*, int);
long	pm_Bread(Biobuf*, void*, long);
long	ns_Bread(Biobuf*, void*, long);
long	pm_Bseek(Biobuf*, long, int);
long	ns_Bseek(Biobuf*, long, int);
int	pm_Bterm(Biobuf*);
int	ns_Bterm(Biobuf*);
int	pm_Bungetc(Biobuf*);
int	ns_Bungetc(Biobuf*);
int	pm_Bungetrune(Biobuf*);
int	ns_Bungetrune(Biobuf*);
long	pm_Bwrite(Biobuf*, void*, long);
long	ns_Bwrite(Biobuf*, void*, long);

#ifndef NODEFINE
#undef Bbuffered
#define Bbuffered pm_Bbuffered
#undef Bfildes
#define Bfildes pm_Bfildes
#undef Bflush
#define Bflush pm_Bflush
#undef Bgetc
#define Bgetc pm_Bgetc
#undef Bgetd
#define Bgetd pm_Bgetd
#undef Bgetrune
#define Bgetrune pm_Bgetrune
#undef Binit
#define Binit pm_Binit
#undef Binits
#define Binits pm_Binits
#undef Blinelen
#define Blinelen pm_Blinelen
#undef Boffset
#define Boffset pm_Boffset
#undef Bopen
#define Bopen pm_Bopen
#undef Bprint
#define Bprint pm_Bprint
#undef Bputc
#define Bputc pm_Bputc
#undef Bputrune
#define Bputrune pm_Bputrune
#undef Brdline
#define Brdline pm_Brdline
#undef Bread
#define Bread pm_Bread
#undef Bseek
#define Bseek pm_Bseek
#undef Bterm
#define Bterm pm_Bterm
#undef Bungetc
#define Bungetc pm_Bungetc
#undef Bungetrune
#define Bungetrune pm_Bungetrune
#undef Bwrite
#define Bwrite pm_Bwrite
#ifdef USENS
#undef Bbuffered
#define Bbuffered ns_Bbuffered
#undef Bfildes
#define Bfildes ns_Bfildes
#undef Bflush
#define Bflush ns_Bflush
#undef Bgetc
#define Bgetc ns_Bgetc
#undef Bgetd
#define Bgetd ns_Bgetd
#undef Bgetrune
#define Bgetrune ns_Bgetrune
#undef Binit
#define Binit ns_Binit
#undef Binits
#define Binits ns_Binits
#undef Blinelen
#define Blinelen ns_Blinelen
#undef Boffset
#define Boffset ns_Boffset
#undef Bopen
#define Bopen ns_Bopen
#undef Bprint
#define Bprint ns_Bprint
#undef Bputc
#define Bputc ns_Bputc
#undef Bputrune
#define Bputrune ns_Bputrune
#undef Brdline
#define Brdline ns_Brdline
#undef Bread
#define Bread ns_Bread
#undef Bseek
#define Bseek ns_Bseek
#undef Bterm
#define Bterm ns_Bterm
#undef Bungetc
#define Bungetc ns_Bungetc
#undef Bungetrune
#define Bungetrune ns_Bungetrune
#undef Bwrite
#define Bwrite ns_Bwrite
#endif /* USENS */
#endif /* !NODEFINE */
