io - format of BASIC file buffer


#include <basic/io.h>


All open files including standard input and output are managed through the biotab:
/*	Basmark BASIC Compiler
 * Copyright 1985, 1986, 1987, 1988, 1989, 1990, 1991, 1992
 *	by Basmark Corporation
 *	Cleveland, Ohio, USA	*/

#include <sys/types.h>
#include <sys/ioctl.h>

#ifdef SGTTY
#include <sgtty.h>
#ifdef TERMIO
#include <termio.h>

#define NFILES 100	/* maximum number of files */
#define BUFSIZ 512	/* size of an ordinary file buffer */
#define	IBMSECTOR 128	/* size of an IBM disk sector */
#define IBMNOWIDTH 255	/* infinite width magic no. */
#define NOWIDTH (-1)	/* magic no. to mean infinite width */
#define STDWIDTH 80	/* default width for standard output */

#define _NFILE 20

struct biotty {
	unsigned tty_len;
#ifdef SGTTY
	struct sgttyb tty_sgttyb;
	struct tchars tty_tchars;
#ifdef LTCHARS
	struct ltchars tty_ltchars;
#ifdef TERMIO
	struct termio tty_termio;

struct biofile {
	unsigned	bio_len;	/* length of buffer */
	char		*bio_buf;	/* buffer */
	char		*bio_ptr;	/* buffer pointer */
	unsigned	bio_cnt;	/* portion remaining */

struct biobuf {	/* BASIC i/o buffer */
	int	bio_fd;		/* file descriptor */
	int	bio_width;	/* width of line on device */
	int	bio_pos;	/* current column position */
	int	bio_line;	/* current row */
	int	bio_isrand;	/* flag: random i/o file? */
	int	bio_istty;	/* flag: raw tty file? */
	int	bio_isbuf;	/* flag: buffered file? */
	union {
		struct biofile	bio_file;	/* file info */
		struct biotty	bio_tty;	/* tty info */
	} bio_dev;		/* union depending on device type */

struct biotab { /* BASIC i/o `logical unit' */
	int	bio_isopen;	/* flag: is file open? */
	struct biobuf *bio_in;	/* input buffer */
	struct biobuf *bio_out;	/* output buffer */
	int	bio_pid;	/* process id for "OPEN PIPE:" */
	char	*bio_rbuf;	/* biggest random buffer */
	int	bio_rlen;	/* biggest random buffer length */
The SGTTY, LTCHARS and TERMIO #ifdefs account for differences in the terminal handling methods but the resulting biotty structure (and the code which uses it) handle these differences. 

A biotab structure is maintained for each file number (see initio(io)).  Within biotab, if bio_isopen is set, the file is open.  If bio_pid is non-zero, the file is open as a pipe and bio_pid is the process id of the process created (see bopen(io)).  Bio_rbuf and bio_rlen are the latest and largest random I/O buffer and its length used on this file number.  Bio_in and bio_out point to biobuf structures for input and output.  These pointers are the same for random I/O and ttyís open for two-way communication.  One of these pointers is zero if the file is open for input or output only. 

A biobuf structure is maintained for each distinct input or output channel.  Within biobuf, bio_fd is the UNIX file descriptor.  Bio_width is the width of the device or NOWIDTH (see width(io)).  Bio_pos is the current horizontal position.  Bio_line is the current vertical position although this value is allowed to grow until a cursor motion resets it.  These positions are numbered starting with one (see cls(misc)).  If bio_isrand is set, the file is a random I/O file.  If bio_istty is set, the file is a full-duplex tty with its state altered and the old state recorded in bio_dev.bio_tty (see initty(io)).  If bio_isbuf is set, the file is a buffered file with the buffer information in bio_dev.bio_file

A biofile structure is maintained for buffered files.  Within biofile, bio_len is the length of the buffer.  Bio_buf points to the buffer itself.  Bio_ptr points to the current position within the buffer.  Bio_cnt is the portion of the buffer remaining (either unread or yet to be written). 

A biotty structure is maintained for each tty.  Within biotty, tty_len is the number of seconds to wait for input before a time-out.  The other structures vary depending on the tty interface of the UNIX system (see tty(4) or termio(7)).  If bio_istty is set, these structures record the state of the tty in order to restore it when the file is closed.  If bio_istty is clear and yet bio_isbuf is set, the file is a half-duplex tty. 




bopen(io), bclose(io), cls(misc), initio(io), initty(io), width(io)
open(2), close(2), ioctl(2), tty(4) or termio(7) in the UNIX Programmerís Manual

from The Basmark QuickBASIC Programmerís Manual by Lawrence Leinweber