[comp.os.rsts] Tail for RSTS written in BASIC PLUS

nash@ucselx.sdsu.edu (Ron Nash) (04/11/90)

I have recieved several requests for the tail program I wrote here
at SDSU.  Here it is in all its groans and glory.  It is written to
be a CCL command, but can be simply run.  This has run under RSTS/E
versions 8, 9.0.  I release it into the public domain.

#	This is a shell archive.
#	Remove everything above and including the cut line.
#	Then run the rest of the file through sh.
#----cut here-----cut here-----cut here-----cut here----#
#!/bin/sh
# shar:   Shell Archiver
#	Run the following text with /bin/sh to create:
#	tail.bas
# This archive created: Tue Apr 10 10:14:35 1990
cat << \SHAR_EOF > tail.bas
10      EXTEND
11      &
	!	TAIL - Print the last part of a file		&
	!	       By Ron Nash, San Diego State University	&
	&
	!	UT CCL TAIL-=[1,2]TAIL.BAC &
	&
	!       This program will print the last N lines of a file.     &
	!	If N is not specified, the last 20 lines are displayed.	&
	!	Tail can be installed as a CCL command or run.		&
	&
	!	Example to print the last 40 lines of STATE.LST type:	&
	!	TAIL STATE.LST 40					&

19	ON ERROR GOTO 900

20      LF$ = CHR$(10)  &
\       N% = 20%        ! Default number of lines To print
30      CORE.COMMON$ = SYS(CHR$(7%))
40      FIRST.SPACE% = INSTR(1%,CORE.COMMON$," ") &
\	GOTO 800 IF FIRST.SPACE% = 0%
50      SECOND.SPACE% = INSTR(FIRST.SPACE%+1%,CORE.COMMON$," ")
70      IF SECOND.SPACE% = 0% THEN P1$ = RIGHT(CORE.COMMON$,FIRST.SPACE%) &
\          GOTO 100
80      P1$ = MID(CORE.COMMON$,FIRST.SPACE%+1%,SECOND.SPACE%-FIRST.SPACE%)
90      P2$ = RIGHT(CORE.COMMON$,SECOND.SPACE%+1%) &
\	N% = VAL(P2$)

100     DIM M%(30%)
120     F$ = SYS(CHR$(6%)+CHR$(-10%)+P1$)
124     PPN$ = MID(F$,5%,2%)
130     RADIX50.NAME$=MID(F$,7%,6%)
140     DISK.NAME$=MID(F$,23%,4%)
150     F$ = SYS(CHR$(6%)+CHR$(17%)+STRING$(2,255%)+PPN$+       &
                 RADIX50.NAME$+STRING$(10%,0%)+DISK.NAME$+STRING$(3%,0%))
160     CHANGE F$ TO M%
170     L = M%(13%)+SWAP%(M%(14%))
180     L = 65536.+L IF L<0.
190     L = 65536.*M%(16%)+L
200     !       FILE IS L BLOCKS LONG - READ BACKWARDS UNTIL N LINES    &
        !       READ.  PRINT N LINES READING FORWARDS                   &

210     OPEN P1$ FOR INPUT AS FILE #1%
220     FIELD #1%, 512% AS R$
225     R = L 
230     GET #1%, RECORD R
300     FOR K% = 1% TO 512%
310     K% = INSTR(K%,R$,lf$)
320     IF K% = 0% THEN K% = 512% \ GOTO 350
330     LINES.READ% = LINES.READ% + 1%
340     NEXT K%
350     IF LINES.READ% >= N% THEN 400
360     R = R - 1 &
\       IF R < 1 THEN 400
365     GET #1%, RECORD R
370     GOTO 300
400     LINES.TOSKIP% = LINES.READ% - N%
410     LINES.TOSKIP% = 1% IF LINES.TOSKIP% < 0%
420     IF R <= 1 THEN CLOSE #1% &
\          OPEN P1$ FOR INPUT AS FILE #1% &
\          GOTO 500
430     GET #1%, RECORD R-1
500     INPUT LINE #1%, A$  FOR I% = 1% TO LINES.TOSKIP%
510     INPUT LINE #1%, A$
520     PRINT A$;
530     GOTO 510

800	PRINT "_FILE: "; &
\	OPEN 'KB:' AS FILE #1% &
\	INPUT LINE #1%, CORE.COMMON$ &
\	CORE.COMMON$ = CVT$$(CORE.COMMON$,4%+16%+32%) &
\	FIRST.SPACE% = 0% &
\	CLOSE #1% &
\	GOTO 50

900     IF ERL = 510 THEN RESUME 999

910	IF ERR = 2% THEN RESUME 800 
912	IF ERL = 800 THEN RESUME 999

920	IF ERR = 5% THEN &
	PRINT "%TAIL-W-SEARCHFAIL, error searching for ";P1$	&
\	PRINT "-TAIL-E-FNF, file not found" &
\	RESUME 999
930	IF ERR = 6% THEN &
	PRINT "%TAIL-W-SEARCHFAIL, error searching for ";P1$	&
\	PRINT "-TAIL-F-DEV, error in device name or inappropriate device type for operation" &
\	RESUME 999
940	IF ERL = 120 THEN &
	PRINT "%TAIL-W-SEARCHFAIL, error searching for ";P1$	&
\	PRINT "-TAIL-F-FNM, error in file name" &
\	RESUME 999
950	IF ERL = 90 THEN &
	PRINT "%TAIL-F-INVQUALVAL, value '";P2$;"' invalid for number of lines to print" &
\	RESUME 999
990     ON ERROR GOTO 0
999     END
SHAR_EOF
#	End of shell archive
exit 0

-- 
Ron Nash
San Diego State University
Internet:  nash@ucselx.sdsu.edu
UUCP:      ucsd!sdsu!ucselx!nash