perm filename IOMODS.SAI[NEW,AIL] blob sn#248610 filedate 1979-01-08 generic text, type T, neo UTF8
ENTRY INITIN,!INTTY,ENDIN,INITOUT,OUTTTY,ENDOUT;
BEGIN "IOMODS"

"
THE FOLLOWING ROUTINES ALLOW THE USER TO DO INPUT/OUTPUT WITH DISK
FILES, ALMOST AS EASILY AS WITH A TELETYPE. TO USE THESE ROUTINES 
DO THE FOLLOWING:

1). INCLUDE THE FOLLOWING DECLARATIONS IN YOUR SOURCE PROGRAM.

	EXTERNAL PROCEDURE INITIN(STRING FILIN;BOOLEAN NOCHK(FALSE));
	EXTERNAL STRING PROCEDURE !INTTY;
	EXTERNAL PROCEDURE ENDIN;

	EXTERNAL PROCEDURE INITOUT(STRING FILOUT;BOOLEAN VERIFY(TRUE));
	EXTERNAL PROCEDURE OUTTTY(STRING S);
	EXTERNAL PROCEDURE ENDOUT;

    THE ABOVE MAY ALSO BE ACCOMPLISHED BY:
	    REQUIRE IOMODS.HDR[LEP,JRL] SOURCE!FILE;
    (WHERE IOMODS.HDR[LEP,JRL] IS CONTAINED WITHIN STRING QUOTES). THIS 
    SOURCE!FILE ALSO DEFINES MACROS FOR CR,CRLF,TAB,VT,FF,LF.

2). REQUIRE THE LOAD!MODULE IOMODS.REL.(RESULT OF COMPILING THIS FILE)
    EITHER IN YOUR SOURCE PROGRAM OR WHEN YOU LOAD THE PROGRAM.

3). TO OPEN THE DISK FILES CALL INITIN (IF AN INPUT FILE), INITOUT(IF AN
    AN OUTPUT FILE). THE INITIALIZATION ROUTINE WILL THEN USE THE STRING
    PARAMETER AS A FILE NAME. IF THE STRING IS TTY THE INPUT (OR OUTPUT)
    WILL BE DONE WITH THE TELETYPE, ANY OTHER RESPONSE WILL BE TAKEN AS A DISK
    FILE WHICH WILL BE LOOKUP'ED OR ENTER'ED.(THUS YOU ARE RESTRICTED
    AND ARE NOT ALLOWED TO HAVE A DISK FILE NAMED TTY).

    NOTE THAT IF YOU DO NOT CALL INITIN, OR INITOUT YOU WILL BE PROMPTED
    FOR THE FILE NAME WHEN !INTTY OR OUTTTY IS CALLED THE FIRST TIME.

4). WITHIN THE PROGRAM USE !INTTY JUST AS YOU WOULD INCHWL AND OUTTTY
    AS YOU WOULD OUTSTR.
   
5). BEFORE EXITING YOUR PROGRAM BE SURE TO EXECUTE AN ENDOUT TO CLOSE
    YOUR OUTPUT FILES. AT ANY TIME DURING THE EXECUTION YOU MAY EXECUTE
    AN ENDOUT OR ENDIN TO CLOSE YOUR FILES SO THAT YOU MAY OPEN OTHER
    FILES FOR INPUT OR OUTPUT.
"
COMMENT INPUT;
DEFINE BRKTAB = 1;
DEFINE CRLF = "('15&'12)";

INTEGER CHANIN;
INTERNAL INTEGER !INEOF;
OWN INTEGER EOFCHK;

INTERNAL PROCEDURE INITIN(STRING FNAME;BOOLEAN NOCHK(FALSE));
BEGIN 
	INTEGER COUNT,IOFLG,BRCHAR,GOODNAME;
	IF NOCHK THEN EOFCHK←FALSE ELSE EOFCHK←TRUE;
	IF CHANIN = 0 THEN 
	 BEGIN
	    IOFLG ← TRUE;
	    GOODNAME ← LENGTH(FNAME);
	    WHILE IOFLG DO
	    BEGIN
		WHILE NOT GOODNAME DO
		    BEGIN OUTSTR("INPUT FILE NAME?");
			  FNAME ← INCHWL;
			  GOODNAME ← LENGTH(FNAME);
			  IF CHANIN THEN
			     BEGIN RELEASE(CHANIN);
				   CHANIN ← 0;
			     END;
		    END;
		OPEN(CHANIN←GETCHAN,IF EQU(FNAME,"TTY") OR EQU(FNAME,"tty") 
		    THEN "TTY" ELSE "DSK",0,2,0,COUNT,BRCHAR,!INEOF);
		COUNT ← 300;
		LOOKUP(CHANIN,FNAME,IOFLG);
		GOODNAME ← FALSE;
	    END;
	    SETBREAK(BRKTAB,'12,'15,"INS");
	 END ELSE USERERR(0,1,"TWO INITINS PREFORMED WITHOUT ENDIN");
END;

INTERNAL STRING PROCEDURE !INTTY;
BEGIN STRING INSTRING;
	IF CHANIN = 0 THEN
		INITIN(NULL);
	INSTRING ← INPUT(CHANIN,BRKTAB);
	IF EOFCHK AND !INEOF THEN USERERR(0,1,"READ PAST END OF INPUT FILE");
	RETURN(INSTRING);
END;

INTERNAL PROCEDURE ENDIN;
BEGIN IF CHANIN THEN
	BEGIN CLOSE(CHANIN);
		RELEASE(CHANIN);
		CHANIN←0;
	END;
END;
COMMENT OUTPUT;
INTEGER CHANOUT;

INTERNAL PROCEDURE INITOUT(STRING FNAME;BOOLEAN VERIFY(TRUE));
BEGIN "INITOUT"
     STRING DEV; INTEGER COUNT,BRCHAR,EOF,IOFLG;
     INTEGER GOODNAME;
     IF CHANOUT = 0 THEN
       BEGIN 
	  IOFLG ← TRUE;
	  GOODNAME ← LENGTH(FNAME);
	  WHILE IOFLG DO
	     BEGIN "WHILE IOFLG"
		WHILE NOT GOODNAME DO
		   BEGIN
		      OUTSTR("OUTPUT FILE NAME?");
		      FNAME ← INCHWL;
		      GOODNAME ← LENGTH(FNAME);
		      IF CHANOUT THEN
			 BEGIN
			     RELEASE(CHANOUT);
			     CHANOUT←0;
			 END;
		   END;
		DEV←IF EQU(FNAME,"TTY")
				 THEN "TTY" ELSE "DSK";
		OPEN(CHANOUT←GETCHAN,DEV,'10000,0,2,COUNT,BRCHAR,EOF);
		IF VERIFY AND NOT EQU("TTY",DEV) THEN
		    BEGIN
			COMMENT HERE WE VERIFY THAT WE ARE NOT DESTROYING
				SOME EXISTENT FILE;
			LOOKUP(CHANOUT,FNAME,IOFLG);
			IF IOFLG < 0 THEN
			CASE IOFLG LAND '777777 OF
			BEGIN 
			    [0] ;
			    [1] BEGIN   GOODNAME ← FALSE;
					CONTINUE "WHILE IOFLG";
				END;
			    [2] BEGIN
				   OUTSTR(CRLF&"ATTEMPTED PROTECTION VIOLATION."&
						" FILE "&FNAME&"   ");
				   GOODNAME ← FALSE;
				   CONTINUE "WHILE IOFLG";
				END;
			    [3] BEGIN
				   OUTSTR(CRLF&"FILE IN USE. FILE "&FNAME&"   ");
				   GOODNAME ← FALSE;
				   CONTINUE "WHILE IOFLG";
				END;
			    [4] BEGIN
				   GOODNAME ← FALSE;
				   CONTINUE "WHILE IOFLG";
				END;
			    [5] BEGIN
				   GOODNAME ← FALSE;
				   CONTINUE "WHILE IOFLG";
				END;
			    [6] BEGIN
				   GOODNAME ← FALSE;
				   CONTINUE "WHILE IOFLG";
			        END;
			    [7] BEGIN
				   GOODNAME ← FALSE;
				   CONTINUE "WHILE IOFLG";
				END;
			    [10]
				BEGIN
				   GOODNAME ← FALSE;
				   CONTINUE "WHILE IOFLG";
				END;
			    [11]BEGIN
				   GOODNAME ← FALSE;
				   CONTINUE "WHILE IOFLG";
				END;
			    [12] BEGIN
				   GOODNAME ← FALSE;
				   CONTINUE "WHILE IOFLG";
				END
			END ELSE
			BEGIN
				INTEGER TINT;
				GOODNAME ← FALSE;
				IOFLG ← TRUE;
				OUTSTR(CRLF&"OUTPUT FILE:"&FNAME&" ALREADY"&
					" EXISTS. OVERWRITE?");
				IF (TINT ← INCHWL) NEQ "Y" AND TINT NEQ "y" THEN
					CONTINUE "WHILE IOFLG";
			END;
			CLOSIN(CHANOUT);
		   END;
		ENTER(CHANOUT,FNAME,IOFLG);
		IF IOFLG THEN
		   BEGIN
			   GOODNAME ← FALSE;
			   OUTSTR(CRLF&"UNABLE TO OPEN OUTPUT FILE "&FNAME&CRLF);
		   END;
	     END "WHILE IOFLG";
       END ELSE USERERR(0,1,"TWO INITOUTS WITHOUT ENDOUT");
END;

INTERNAL PROCEDURE OUTTTY(STRING X);
BEGIN 
	IF CHANOUT = 0 THEN
		INITOUT(NULL);
	OUT(CHANOUT,X);
END;

INTERNAL PROCEDURE ENDOUT;
BEGIN IF CHANOUT THEN
	BEGIN 	CLOSE(CHANOUT);
		RELEASE(CHANOUT);
		CHANOUT←0;
	END;
END;

SIMPLE PROCEDURE INITIT;
BEGIN CHANIN←CHANOUT←0 END;

REQUIRE INITIT INITIALIZATION;
END "IOMODS"