      SUBROUTINE MAIN
      INTEGER I, INT, PAGE, LCNT, J, JUNK, SCRAT
      INTEGER NXTFIL, OPEN, GETLIN, GETWRD, EQUAL, IMPUT, IMGET
      LOGICAL*1 FILE(40), PATH(40), LIN(512), WRD(512)
      INTEGER MEM(2500)
      LOGICAL*1 CMEM(5000)
      EQUIVALENCE (CMEM(1),MEM(1))
      COMMON/CDSMEM/MEM
      INTEGER INMEMT
      INTEGER IMINIT
      LOGICAL*1 HDRSTR(5)
      LOGICAL*1 LIPSIS(10)
      LOGICAL*1 BIGLIP(10)
      LOGICAL*1 TOC(18)
      LOGICAL*1 SINDEX(17)
      DATA HDRSTR(1)/35/,HDRSTR(2)/45/,HDRSTR(3)/104/,HDRSTR(4)/45/,HDRS
     *TR(5)/0/
      DATA LIPSIS(1)/32/,LIPSIS(2)/32/,LIPSIS(3)/32/,LIPSIS(4)/32/,LIPSI
     *S(5)/46/,LIPSIS(6)/46/,LIPSIS(7)/46/,LIPSIS(8)/46/,LIPSIS(9)/46/,L
     *IPSIS(10)/0/
      DATA BIGLIP(1)/32/,BIGLIP(2)/32/,BIGLIP(3)/32/,BIGLIP(4)/32/,BIGLI
     *P(5)/32/,BIGLIP(6)/80/,BIGLIP(7)/97/,BIGLIP(8)/103/,BIGLIP(9)/101/
     *,BIGLIP(10)/0/
      DATA TOC(1)/84/,TOC(2)/97/,TOC(3)/98/,TOC(4)/108/,TOC(5)/101/,TOC(
     *6)/32/,TOC(7)/111/,TOC(8)/102/,TOC(9)/32/,TOC(10)/67/,TOC(11)/111/
     *,TOC(12)/110/,TOC(13)/116/,TOC(14)/101/,TOC(15)/110/,TOC(16)/116/,
     *TOC(17)/115/,TOC(18)/0/
      DATA SINDEX(1)/73/,SINDEX(2)/110/,SINDEX(3)/100/,SINDEX(4)/101/,SI
     *NDEX(5)/120/,SINDEX(6)/32/,SINDEX(7)/111/,SINDEX(8)/102/,SINDEX(9)
     */32/,SINDEX(10)/69/,SINDEX(11)/110/,SINDEX(12)/116/,SINDEX(13)/114
     */,SINDEX(14)/105/,SINDEX(15)/101/,SINDEX(16)/115/,SINDEX(17)/0/
      CALL QUERY(24Husage:  alist [file] ...)
      I=1
23000 IF (.NOT.(NXTFIL(I, FILE, PATH, SCRAT) .NE. -1))GOTO 23002
      INT = OPEN(FILE, 1)
      IF (.NOT.(INT .EQ. -3))GOTO 23003
      CALL CANT(FILE)
23003 CONTINUE
      INMEMT = IMINIT(2500, 25)
      IF (.NOT.(INMEMT .EQ. 0))GOTO 23005
      CALL ERROR(39HError initializing in-memory sort area.)
23005 CONTINUE
      IF (.NOT.(I .GT. 1))GOTO 23007
      CALL PUTCH(12, 2)
23007 CONTINUE
      CALL FMTHDR(PATH, TOC, LIN)
      CALL PUTLIN(LIN, 2)
      CALL PUTCH(10, 2)
      PAGE = 0
      LCNT = 0
23009 IF (.NOT.(GETLIN(LIN, INT) .NE. -1))GOTO 23010
      J = 1
      JUNK = GETWRD(LIN, J, WRD)
      IF (.NOT.(EQUAL(WRD, HDRSTR) .EQ. 1))GOTO 23011
      PAGE = PAGE + 1
      CALL SKIPBL(LIN, J)
      CALL PUTHDR(LIN(J), LIPSIS, PAGE)
      JUNK = GETWRD(LIN, J, WRD)
      CALL FOLD(WRD)
      CALL FMTNDX(WRD, PAGE, LIN)
      JUNK = IMPUT(INMEMT, LIN)
      LCNT = 1
      GOTO 23012
23011 CONTINUE
      IF (.NOT.(LCNT .GE. 60))GOTO 23013
      PAGE = PAGE + 1
      LCNT = 1
      GOTO 23014
23013 CONTINUE
      LCNT = LCNT + 1
23014 CONTINUE
23012 CONTINUE
      GOTO 23009
23010 CONTINUE
      CALL CLOSE(INT)
      CALL IMSORT(INMEMT)
      CALL PUTCH(12, 2)
      CALL FMTHDR(PATH, SINDEX, LIN)
      CALL PUTLIN(LIN, 2)
      CALL PUTCH(10, 2)
23015 IF (.NOT.(IMGET(INMEMT, LIN) .NE. -1))GOTO 23016
      J=1
23017 IF (.NOT.(J .LE. 23))GOTO 23019
      CALL PUTCH(32, 2)
23018 J=J+1
      GOTO 23017
23019 CONTINUE
      CALL PUTLIN(LIN, 2)
      CALL PUTCH(10, 2)
      GOTO 23015
23016 CONTINUE
      INT = OPEN(FILE, 1)
      PAGE = 0
      LCNT = 0
23020 IF (.NOT.(GETLIN(LIN, INT) .NE. -1))GOTO 23021
      J = 1
      JUNK = GETWRD(LIN, J, WRD)
      IF (.NOT.(EQUAL(WRD, HDRSTR) .EQ. 1))GOTO 23022
      PAGE = PAGE + 1
      CALL PUTCH(12, 2)
      CALL PUTHDR(LIN, BIGLIP, PAGE)
      LCNT = 1
      GOTO 23023
23022 CONTINUE
      IF (.NOT.(LCNT .GE. 60))GOTO 23024
      PAGE = PAGE + 1
      CALL PUTCH(12, 2)
      LCNT = 1
      CALL PUTLIN(LIN, 2)
      GOTO 23025
23024 CONTINUE
      LCNT = LCNT + 1
      CALL PUTLIN(LIN, 2)
23025 CONTINUE
23023 CONTINUE
      GOTO 23020
23021 CONTINUE
      CALL CLOSE(INT)
      IF (.NOT.(SCRAT .EQ. 1))GOTO 23026
      CALL REMOVE(FILE)
23026 CONTINUE
23001 I=I+1
      GOTO 23000
23002 CONTINUE
      RETURN
      END
      SUBROUTINE FMTHDR(PATH, MIDDLE, BUF)
      LOGICAL*1 PATH(40), MIDDLE(100), BUF(100), DATE(10), TIME(10)
      INTEGER J, LIMIT, INIT, NOW(7)
      INTEGER LENGTH
      DATA INIT /1/
      IF (.NOT.(INIT .EQ. 1))GOTO 23028
      INIT = 0
      CALL GETNOW(NOW)
      CALL FMTDAT(DATE, TIME, NOW, 1)
23028 CONTINUE
      J = 1
      CALL STCOPY(PATH, 1, BUF, J)
      LIMIT = (72 - LENGTH(MIDDLE)) / 2 + 1
23030 IF (.NOT.(J .LE. LIMIT))GOTO 23031
      CALL CHCOPY(32, BUF, J)
      GOTO 23030
23031 CONTINUE
      CALL STCOPY(MIDDLE, 1, BUF, J)
      LIMIT = 72 - LENGTH(DATE) - LENGTH(TIME) - 1
23032 IF (.NOT.(J .LE. LIMIT))GOTO 23033
      CALL CHCOPY(32, BUF, J)
      GOTO 23032
23033 CONTINUE
      CALL STCOPY(DATE, 1, BUF, J)
      CALL CHCOPY(32, BUF, J)
      CALL STCOPY(TIME, 1, BUF, J)
      CALL CHCOPY(10, BUF, J)
      RETURN
      END
      SUBROUTINE FMTNDX(FILE, PAGE, BUF)
      LOGICAL*1 FILE(40), BUF(100), NUM(6)
      INTEGER PAGE, NBLANK, I, J
      INTEGER LENGTH, ITOC
      NBLANK = 25 - LENGTH(FILE) - ITOC(PAGE, NUM, 6)
      J = 1
      CALL STCOPY(FILE, 1, BUF, J)
      I=1
23034 IF (.NOT.(I .LE. NBLANK))GOTO 23036
      CALL CHCOPY(32, BUF, J)
23035 I=I+1
      GOTO 23034
23036 CONTINUE
      CALL SCOPY(NUM, 1, BUF, J)
      RETURN
      END
      INTEGER FUNCTION NXTFIL(I, FILE, PATH, SCRAT)
      INTEGER I, SCRAT, INT
      INTEGER GETARG, CREATE
      LOGICAL*1 FILE(40), PATH(40)
      LOGICAL*1 SEED(4)
      LOGICAL*1 STDPTH(15)
      DATA SEED(1)/97/,SEED(2)/108/,SEED(3)/115/,SEED(4)/0/
      DATA STDPTH(1)/83/,STDPTH(2)/116/,STDPTH(3)/97/,STDPTH(4)/110/,STD
     *PTH(5)/100/,STDPTH(6)/97/,STDPTH(7)/114/,STDPTH(8)/100/,STDPTH(9)/
     *32/,STDPTH(10)/105/,STDPTH(11)/110/,STDPTH(12)/112/,STDPTH(13)/117
     */,STDPTH(14)/116/,STDPTH(15)/0/
      IF (.NOT.(GETARG(I, FILE, 40) .NE. -1))GOTO 23037
      CALL MKPATH(FILE, PATH)
      SCRAT = 0
      NXTFIL = 0
      GOTO 23038
23037 CONTINUE
      IF (.NOT.(I .GT. 1))GOTO 23039
      NXTFIL = -1
      GOTO 23040
23039 CONTINUE
      CALL SCRATF(SEED, FILE)
      INT = CREATE(FILE, 2)
      IF (.NOT.(INT .NE. -3))GOTO 23041
      SCRAT = 1
      CALL FCOPY(1, INT)
      CALL CLOSE(INT)
      CALL SCOPY(STDPTH, 1, PATH, 1)
      NXTFIL = 0
      GOTO 23042
23041 CONTINUE
      CALL REMARK(47HCannot create scratch file  for standard input.)
      NXTFIL = -1
23042 CONTINUE
23040 CONTINUE
23038 CONTINUE
      RETURN
      END
      SUBROUTINE PUTHDR(FIRST, MIDDLE, PAGE)
      LOGICAL*1 FIRST(100), MIDDLE(100)
      INTEGER PAGE, I, LIMIT
      INTEGER LENGTH
      LIMIT = 67 - LENGTH(MIDDLE)
      I=1
23043 IF (.NOT.(I .LE. LIMIT))GOTO 23045
      IF (.NOT.(FIRST(I) .EQ. 10 .OR. FIRST(I) .EQ. 0))GOTO 23046
      GOTO 23045
23046 CONTINUE
      CALL PUTCH(FIRST(I), 2)
23047 CONTINUE
23044 I=I+1
      GOTO 23043
23045 CONTINUE
23048 IF (.NOT.(I .LE. LIMIT))GOTO 23050
      CALL PUTCH(32, 2)
23049 I=I+1
      GOTO 23048
23050 CONTINUE
      CALL PUTLIN(MIDDLE, 2)
      CALL PUTINT(PAGE, 5,2)
      CALL PUTCH(10, 2)
      RETURN
      END
