C   07/06/96 606071831  MEMBER NAME  BRDS     (S4)          FORTRAN
      SUBROUTINE BRDS(IUN,NBUFL,INIR)
C     BOS SUBPROGRAM =3.4=
      EXTERNAL BINP
#include "acs.for"
      COMMON/BCS/IW(1)
      INTEGER HIDD/'HIDD'/
C
C     BUFFER FOR INPUT = BANK ('+BUF',IUN), LENGTH = 10 + NRECL
C
C     IBF+ 1   =1 (INPUT)
C          2   BUFFER SIZE USED
C          3   NR OF IO-ERRORS
C          4   NR OF RECORDS
C          5   NR OF EVENTS ACCEPTED
C          6   NR OF EVENTS, WHICH ARE TOO LARGE
C          7   SUM OF NR OF WORDS IN ALL EVENTS
C          8   MAX NR OF WORDS IN AN EVENT
C          9   LIMIT FOR NR OF ERRORS
C         10   POINTER WITHIN RECORD TO NEXT EVENT
C         ----------------
C         11   NTOT (= NR OF WORDS IN THE RECORD FOLOWING)
C         12   RECORD
C          .   . . .
C          .   . . .
C
C     LOCATE BUFFER BANK
   10 IF(IBFI.EQ.0) IBFI=IBLN('+BUF')+1
      IBF=IBFI
    2 IBF=IW(IBF-1)
      IF(IBF.EQ.0)        GOTO    94
      IF(IW(IBF-2)-IUN) 2,4,94
    4 IF(IW(IBF+1).EQ.1) GOTO 12
      ICOND=23
      CALL BDMP
   12 IF(IW(IBF+10)) 20,14,30
C     CHECK FREE SPACE
   14 IF(NFRE.EQ.0) GOTO 90
      CALL BGAR(IGA)
      GOTO 10
C     NEW RECORD IN BUFFER
   20 IF(IW(IBF+11).GT.0) GOTO 22
C     READ ERROR
      IW(IBF+3)=IW(IBF+3)+1
      ITREC=1
      IF(IW(IBF+3).LE.IW(IBF+9)) GOTO 90
      ICOND=21
      CALL BDMP
C     NO READ ERROR
   22 IPREC=ITREC
      ITREC=0
      IW(IBF+4)=IW(IBF+4)+1
      IF(IW(IBF).EQ.11) GOTO 23
C     BUFFER USED, CHECK LENGTH
      IF(IW(IBF+11)+11.GT.IW(IBF)) GOTO 24
      GOTO 25
C     NO BUFFER USED
   23 IF(NEXT+IW(IBF+11).LT.NLST) GOTO 25
C     RECORD TOO LONG
   24 ICOND=22
      CALL BDMP
C     RESET POINTER
   25 IW(IBF+10)=12
      INDH=IBF+IW(IBF+10)
      IF(IW(INDH).EQ.HIDD) GOTO 29
C     FULL RECORD (NO LSB-RECORD)
      LENG=IW(IBF+11)
      IF(LENG.LT.4) GOTO 80
      IC=0
      IW(IBF+10)=0
      IF(LIC.EQ.0) GOTO 28
C     ERROR, HIDD WAS EXPECTED
      NER(7)=NER(7)+1
   26 IF(NEXT+IW(IBF+11).LT.NLST) GOTO 27
      IW(IBF+6)=IW(IBF+6)+1
      LIC=0
      GOTO 12
   27 CALL UCOPY2(IW(IBF+12),IW(NEXT),IW(IBF+11))
   28 NEXTA=NEXT
      CALL BFRR(LENG,AR)
      GOTO 71
C     LSB-RECORD
   29 IW(IBF+2)=MAX0(IW(IBF+2),IW(IBF+11)+1)
      IF(IW(IBF).NE.11) GOTO 32
      CALL BCHM(IBF,IW(IBF+2)+9,IER)
      IF(IER.EQ.0) GOTO 32
      GOTO 96
C     OLD RECORD OF LSB TYPE
   30 INDH=IBF+IW(IBF+10)
      IF(IW(INDH).NE.HIDD) GOTO 80
      LIC=0
C
   32 LENG=IW(INDH+3)-1
      IF(LENG.LT.4) GOTO 80
      IC=IW(INDH+4)
      IF(IC.LT.0.OR.IC.GT.3) GOTO 80
C     CHECK LENGTH OF RECORD
      IF(IW(IBF+10)+LENG-7.NE.IW(IBF+11)) GOTO 34
      IW(IBF+10)=0
      GOTO 40
   34 IW(IBF+10)=IW(IBF+10)+LENG+5
      IF(IW(IBF+10).GT.IW(IBF+11)+8) GOTO 80
C     CHECK NEW IC / LAST IC
   40 IF(IC-1) 42,44,46
   42 IF(LIC) 54,60,54
   44 IF(LIC) 50,52,50
   46 IF(LIC) 56,56,60
   50 NER(6)=NER(6)+1
   52 IF(IW(IBF+10).NE.0) GOTO 80
      GOTO 60
   54 NER(7)=NER(7)+1
      GOTO 60
   56 NER(8)=NER(8)+1
      GOTO 12
C
   60 IF(IC.EQ.0.OR.IC.EQ.1) NEXTI=NEXT
      IF(NEXTI+LENG.LT.NLST) GOTO 62
      IW(IBF+6)=IW(IBF+6)+1
      LIC=0
      GOTO 12
   62 CALL UCOPY2(IW(INDH+5),IW(NEXTI),LENG)
      NEXTI=NEXTI+LENG
      IF(IC.EQ.1.OR.IC.EQ.2) GOTO 92
C     NEW EVENT COMPLETE
   70 NEXTA=NEXT
      IF(IC.GE.2.AND.IPREC.NE.0) GOTO 56
      LEV=NEXTI-NEXT
      CALL BFRR(LEV,AR)
   71 NEXTB=NEXT
      LEV=NEXTB-NEXTA
      IF(LEV.GT.0) GOTO 72
      GOTO 12
   72 IW(IBF+5)=IW(IBF+5)+1
      IW(IBF+7)=IW(IBF+7)+LEV
      IW(IBF+8)=MAX0(IW(IBF+8),LEV)
      INIR=0
      GOTO 100
C     WRONG RECORD
   80 NER(9)=NER(9)+1
C     NEW INPUT REQUIRED
   90 IC=0
      IF(IW(IBF).EQ.11) GOTO 91
      CALL BCHM(IBF,11-IW(IBF),IER)
      IF(IER.NE.0) GOTO 96
      CALL BCHM(IBF,0,IER)
      IF(IER.NE.0) GOTO 96
      CALL BGAR(IGA)
      CALL BLOC(IBF,'+BUF',IUN,&96)
      GOTO 92
   91 CALL BCHM(IBF,0,IER)
      IF(IER.NE.0) GOTO 96
   92 IW(IBF+10)=-1
      LIC=IC
      INIR=IBF+11
      NBUFL=IW(IBF)-10
      IF(NBUFL.GT.1) GOTO 100
      NBUFL=NLST-NEXT
      GOTO 100
   94 CALL BCRE(IBF,'+BUF',IUN,11,&96,IER)
      IW(IBF+1)=1
      IW(IBF+2)=0
      IW(IBF+9)=10
      ITREC=0
      GOTO 12
   96 ICOND=12
      CALL BDMP
  100 RETURN
      END
