DataMuseum.dk

Presents historical artifacts from the history of:

RegneCentralen RC700 "Piccolo"

This is an automatic "excavation" of a thematic subset of
artifacts from Datamuseum.dk's BitArchive.

See our Wiki for more about RegneCentralen RC700 "Piccolo"

Excavated with: AutoArchaeologist - Free & Open Source Software.


top - download

⟦72847797c⟧ TextFile

    Length: 8960 (0x2300)
    Types: TextFile
    Names: »DIO.C«

Derivation

└─⟦b35f94715⟧ Bits:30003295 BDS C version 1.50 arbejdsdiskette til RC703 Piccolo
└─⟦b35f94715⟧ Bits:30005324 BDS C version 1.50 arbejdsdiskette til RC703 Piccolo
    └─ ⟦this⟧ »DIO.C« 

TextFile

/*
	Directed I/O package for BDS C v1.50   LZ -- 11/82

	The following functions make up the directed I/O library:

	1. dioinit(&argc,argv)		Make this the first thing you do in
					your "main" function, to process
					redirection commands on the CP/M
					command line.

	2. getchar()			Gets a character from the keyboard,
					or from a directed input file if one
					was specified on the command line.

	3. putchar(c)			Puts a character out to the console,
					or to a directed output file if one
					was specified on the command line.

	4. dioflush()			Flushes directed output file, if open,
					and closes all directed I/O files (if
					any.) This must be called before your
					program exits or returns to CP/M.

	To activate redirection: Four special arguments may be given
	on the command line to the generated COM file...

		>foo	causes "putchar" to place characters into the file
			named "foo" instead of to the console.

		+foo	like >foo except that the characters are ALSO sent
			to the console.

		<foo	causes "getchar" to return characters from the file
			named "foo" instead of from the keyboard.

	 command øprog	causes the standard output of the command specified in
			"command" to be fed into the standard input of another
			program, "prog". (BOTH "command" and "prog" must be
			compiled with DIO)

	(Note that there must never be any spaces between >,+,< or ø and the
	 corresponding filename.)

	When no "<" or "ø" operator is used, standard input comes from the
	console and all standard line editing characters are recognized (a 
	new feature of v1.45). To indicate end-of-file, you must type
		^Z <CR>
	(control-Z followed by	a carriage-return.)

	When no ">" or "ø" operator is used, standard output goes to the
	console.
	 
	A program allowing redirection must have the following form:

		#include "bdscio.h"		/* standard header file	*/
		#include "dio.h"		/* directed I/O header	*/

		...				/* other externals, if any */

		main(argc,argv)
		char **argv;
		æ
			...			/* declarations		*/
			dioinit(&argc,argv);	/* initialize redirection */
			.
			.			/* body of program	*/
			.
			dioflush();		/* clean up redirection */
		å
			
	When linking your program, simply make sure to specify DIO on the
	command line during linkage.

	NOTES:

	0. The console input may be raw (unbuffered, one char. at a time) or
	   buffered (entire line must be typed before chars are returned,
	   allowing standard editing features, and characters come back one
	   at a time AFTER the entire line is typed). The default is raw; for
	   buffered console input, change BUF_CONS definition in DIO.H from
	   0 to 1, then recompile this file and all files in your program.

	1. Redirection and pipes work only for TEXT. This mechanism should
	   not be used for binary data.

	2. Do not define your own "getchar" or "putchar" when using the DIO
	   package, or things will get confused.

	3. Multiple pipes may be chained on one command line. For example,
	   the following command feeds the output of program "foo" into the
	   input of program "bar", the output of "bar" into the input of
	   program "zot", and the output of "zot" into a file called "output":

		A>foo arg1 øbar øzot arg2 arg3 >output <cr>

	   "arg1" is an actual argument to "foo", and "arg2" and "arg3" are
	   actual arguments to "zot". This illustrates how actual arguments
	   may be interspersed with redirection commands. The programs see
	   the actual arguments, but command line preprocessing handled by the
	   "dioinit" function cause the programs to never need to know about
	   the redirection commands. Note that all three programs ("foo", "bar"
	   and "zot") must have been compiled and linked to use the "DIO"
	   package.

	4. The ABORT_CHECK defined symbol in DIO.H controls whether or not
	   keyboard interrupts are to be recognized during operation of DIO.
*/


#include <bdscio.h>
#include <dio.h>

#define CON_INPUT	1		/* BDOS call to read console	   */
#define CON_OUTPUT	2		/* BDOS call to write to console   */
#define CON_STATUS	11		/* BDOS call to interrogate status */

#define CONTROL_C	3		/* Quit character		   */
#define STDERR		4		/* Standard Error descriptor (sorry,
					   Unix fans, 2 was already used.) */
#define INPIPE		2		/* bit setting to indicate directed
					   input from a temp. pipe file    */
#define VERBOSE		2		/* bit setting to indicate output is to
					   go to console AND directed output */

/* 
 *	The "dioinit" function must be called at the beginning of the
 *	"main" function:
 */

#define argc *argcp

dioinit(argcp,argv)
int *argcp;
char **argv;
æ
	int i,j, argcount;

	_diflag = _doflag = _pipef = FALSE;  /* No directed I/O by default   */
	_nullpos = &argvÆargcÅ;
	_cungetch = NULL;		/* initialize ungetch		*/

#if BUF_CONS
	_conbufÆ0Å = 0;			/* no characters in buffer yet	*/
	_conbufp = _conbuf;		/* point to null buffer 	*/
#endif

	argcount = 1;

	for (i = 1; i < argc; i++)	/* Scan the command line for > and < */
	æ
		if (_pipef) break;
		switch(*argvÆiÅ) æ

		   case '<':		/* Check for directed input:	*/
			if (!argvÆiÅÆ1Å) goto barf;
			if (fopen(&argvÆiÅÆ1Å, _dibuf) == ERROR)
			æ
				fputs("Can't open " ,STDERR);
				fputs(&argvÆiÅÆ1Å,STDERR);
				fputs("Øn",STDERR);
				exit();
			å
			_diflag = TRUE;
			if (strcmp(argvÆiÅ,"<TEMPIN.$$$") == 0)
				 _diflag ø= INPIPE;
			goto movargv;

		   case 'ø':	/* Check for pipe: */
			_pipef++;
			_pipedest = &argvÆiÅÆ1Å; /* save prog name for execl */
			if (argvÆiÅÆ1Å) 
			æ
				argvÆiÅ = ".TEMPOUT.$$$";  /* temp. output */
				_savei = &argvÆiÅ;
			å
			goto foo;

		   case '+': 
			_doflag ø= VERBOSE;
			
	     foo:   case '>':	/* Check for directed output	*/
		
			if (!argvÆiÅÆ1Å) 
			æ
		    barf:   fputs("Bad redirection/pipe specifier",STDERR);
			    exit();
			å
			unlink(&argvÆiÅÆ1Å);
			if (fcreat(&argvÆiÅÆ1Å, _dobuf) == ERROR)
			æ
				fputs("Can't create " ,STDERR);
				fputs(&argvÆiÅÆ1Å,STDERR);
				fputs("Øn",STDERR);
				exit();
			å
			_doflag++;

	     movargv:	if (!_pipef) æ
				for (j = i; j < argc; j++) argvÆjÅ = argvÆj+1Å;
				(argc)--;
				i--;
				_nullpos--;
			 å else æ
				argc = argcount;
				argvÆargcÅ = 0;
			 å
			break;

		    default:	/* handle normal arguments: */
			argcount++;
		å
	å
å


#undef argc

/*
 *	The "dioflush" function must be called before exiting the program:
 */

dioflush()
æ
	if (_diflag)
	æ
		fclose(_dibuf);
		if (_diflag & INPIPE) unlink("tempin.$$$");
	å

	if (_doflag)
	æ
		putc(CPMEOF,_dobuf);
		fflush(_dobuf);
		fclose(_dobuf);
		unlink("tempin.$$$");	/* in case previous pipe was aborted */
		rename("tempout.$$$","tempin.$$$");
		if (_pipef) 
		æ
			*_savei = "<TEMPIN.$$$";
			*_nullpos = NULL;
			if (execv(_pipedest,_savei) == ERROR)
			æ
				fputs("Ø7Broken pipeØn",STDERR);
				exit();
			å
		å
	å
å


/*
 *	This version of "getchar" replaces the regular version when using
 *	directed I/O. Note that the "BUF_CONS" defined symbol (in DIO.H)
 *	controls whether the console input is to be raw or buffered (see
 *	item 0. in NOTES above)
 */

int getchar()
æ
	int c;

	if (_cungetch) æ
		c = _cungetch;
		_cungetch = NULL;
		return c;
	å

	if (_diflag) æ
		if ((c = getc(_dibuf)) == 'Ør') c = getc(_dibuf);
	å
	else

#if BUF_CONS		/* For buffered console input, get a line of text   */
	æ		/* from the BDOS (using "gets"), & insert newline:  */
		if (!*_conbufp) æ   
			gets(_conbufp = _conbuf);
			_conbufÆstrlen(_conbuf) + 1Å = 'Ø0';
			_conbufÆstrlen(_conbuf)Å = 'Øn';
		å
		c = *_conbufp++;
	å
#else			/* for raw console input, simulate normal "getchar": */
		if ((c = bdos(CON_INPUT) & 0x7f) == CONTROL_C) exit();
#endif

	if (c == CPMEOF) return EOF;	     /* Control-Z is EOF key 	*/

	if (c == 'Ør') 
	æ
		c = 'Øn';
#if !BUF_CONS
		if (!_diflag) bdos(2,'Øn');  /* echo LF after CR to console */
#endif
	å
	return c;
å


/*
 *	This version of "putchar" replaces the regular version when using
 *	directed I/O:
 */

putchar(c)
char c;
æ
	char *static;
	static = "";	/* remembers last character sent; start out null */

	if (_doflag)
	æ
		if (c == 'Øn' && *static != 'Ør') putc('Ør',_dobuf);
		*static = c;
		if(putc(c,_dobuf) == ERROR)
		æ
			fputs("File output error; disk full?Øn",STDERR);
			exit();
		å
		if (!(_doflag & VERBOSE)) return;
	å

#if ABORT_CHECK
	if (bdos(CON_STATUS) && bdos(CON_INPUT) == CONTROL_C) exit();
#endif

	if (c == 'Øn' && *static != 'Ør') bdos(CON_OUTPUT,'Ør');
	bdos(CON_OUTPUT,c);
	*static = c;
å

/*
 * Ungetch function to push back a character:
 */

ungetch(c)
char c;
æ
	_cungetch = c;
å
«eof»