      SUBROUTINE MAIN
      INTEGER OUTFIL
      INTEGER CREATE, OPEN, OPENDR
      INTEGER DEPTH, DESC, I, J, LEVEL, N, NXTCOL, PTR(5), STATUS
      INTEGER PAT(132)
      INTEGER GDRPRM, GETARG, GETLIN, GETPAT, LENGTH, MATCH
      LOGICAL*1 BUF(512), DEFPAT(2), FILE(40)
      LOGICAL*1 OBUF(512), PPATH(40)
      COMMON / CLS / VERBOS, DODATE, REVERS, SORTIT, DIRSW, DOHEAD, PACK
     *IT, SCRAT(40), FMTCTL(512)
      INTEGER VERBOS
      INTEGER DODATE
      INTEGER REVERS
      INTEGER SORTIT
      INTEGER DIRSW
      INTEGER DOHEAD
      INTEGER PACKIT
      LOGICAL*1 SCRAT
      LOGICAL*1 FMTCTL
      LOGICAL*1 DIRPAT(4)
      LOGICAL*1 HEAD1(55)
      LOGICAL*1 HEAD2(17)
      LOGICAL*1 HEAD3(55)
      LOGICAL*1 HEAD4(18)
      DATA DIRPAT(1)/63/,DIRPAT(2)/42/,DIRPAT(3)/47/,DIRPAT(4)/0/
      DATA HEAD1(1)/102/,HEAD1(2)/105/,HEAD1(3)/108/,HEAD1(4)/101/,HEAD1
     *(5)/110/,HEAD1(6)/97/,HEAD1(7)/109/,HEAD1(8)/101/,HEAD1(9)/32/,HEA
     *D1(10)/32/,HEAD1(11)/32/,HEAD1(12)/32/,HEAD1(13)/32/,HEAD1(14)/32/
     *,HEAD1(15)/32/,HEAD1(16)/32/,HEAD1(17)/32/,HEAD1(18)/32/,HEAD1(19)
     */32/,HEAD1(20)/32/,HEAD1(21)/112/,HEAD1(22)/114/,HEAD1(23)/111/,HE
     *AD1(24)/116/,HEAD1(25)/101/,HEAD1(26)/99/,HEAD1(27)/116/,HEAD1(28)
     */105/,HEAD1(29)/111/,HEAD1(30)/110/,HEAD1(31)/32/,HEAD1(32)/32/,HE
     *AD1(33)/32/,HEAD1(34)/32/,HEAD1(35)/32/,HEAD1(36)/32/,HEAD1(37)/32
     */,HEAD1(38)/108/,HEAD1(39)/97/,HEAD1(40)/115/,HEAD1(41)/116/,HEAD1
     *(42)/32/,HEAD1(43)/109/,HEAD1(44)/111/,HEAD1(45)/100/,HEAD1(46)/10
     *5/,HEAD1(47)/102/,HEAD1(48)/105/,HEAD1(49)/101/,HEAD1(50)/100/,HEA
     *D1(51)/32/,HEAD1(52)/32/,HEAD1(53)/32/,HEAD1(54)/32/,HEAD1(55)/0/
      DATA HEAD2(1)/32/,HEAD2(2)/32/,HEAD2(3)/32/,HEAD2(4)/115/,HEAD2(5)
     */105/,HEAD2(6)/122/,HEAD2(7)/101/,HEAD2(8)/32/,HEAD2(9)/32/,HEAD2(
     *10)/32/,HEAD2(11)/111/,HEAD2(12)/119/,HEAD2(13)/110/,HEAD2(14)/101
     */,HEAD2(15)/114/,HEAD2(16)/10/,HEAD2(17)/0/
      DATA HEAD3(1)/45/,HEAD3(2)/45/,HEAD3(3)/45/,HEAD3(4)/45/,HEAD3(5)/
     *45/,HEAD3(6)/45/,HEAD3(7)/45/,HEAD3(8)/45/,HEAD3(9)/32/,HEAD3(10)/
     *32/,HEAD3(11)/32/,HEAD3(12)/32/,HEAD3(13)/32/,HEAD3(14)/32/,HEAD3(
     *15)/32/,HEAD3(16)/32/,HEAD3(17)/32/,HEAD3(18)/32/,HEAD3(19)/45/,HE
     *AD3(20)/45/,HEAD3(21)/45/,HEAD3(22)/45/,HEAD3(23)/45/,HEAD3(24)/45
     */,HEAD3(25)/45/,HEAD3(26)/45/,HEAD3(27)/45/,HEAD3(28)/45/,HEAD3(29
     *)/45/,HEAD3(30)/45/,HEAD3(31)/45/,HEAD3(32)/45/,HEAD3(33)/32/,HEAD
     *3(34)/32/,HEAD3(35)/45/,HEAD3(36)/45/,HEAD3(37)/45/,HEAD3(38)/45/,
     *HEAD3(39)/45/,HEAD3(40)/45/,HEAD3(41)/45/,HEAD3(42)/45/,HEAD3(43)/
     *45/,HEAD3(44)/45/,HEAD3(45)/45/,HEAD3(46)/45/,HEAD3(47)/45/,HEAD3(
     *48)/45/,HEAD3(49)/45/,HEAD3(50)/45/,HEAD3(51)/45/,HEAD3(52)/45/,HE
     *AD3(53)/45/,HEAD3(54)/45/,HEAD3(55)/0/
      DATA HEAD4(1)/32/,HEAD4(2)/45/,HEAD4(3)/45/,HEAD4(4)/45/,HEAD4(5)/
     *45/,HEAD4(6)/45/,HEAD4(7)/45/,HEAD4(8)/45/,HEAD4(9)/32/,HEAD4(10)/
     *32/,HEAD4(11)/45/,HEAD4(12)/45/,HEAD4(13)/45/,HEAD4(14)/45/,HEAD4(
     *15)/45/,HEAD4(16)/10/,HEAD4(17)/10/,HEAD4(18)/0/
      DATA DEFPAT / 37, 0 /
      CALL INITLS(FILE)
      LEVEL = 0
      IF (.NOT.( DOHEAD .EQ. 1 ))GOTO 23000
      CALL PUTLIN( HEAD1, 2)
      CALL PUTLIN( HEAD2, 2)
      CALL PUTLIN( HEAD3, 2)
      CALL PUTLIN( HEAD4, 2)
23000 CONTINUE
      IF (.NOT.( PACKIT .EQ. 1 ))GOTO 23002
      CALL INPACK( NXTCOL, 80, OBUF, 2)
23002 CONTINUE
23004 CONTINUE
      LEVEL = LEVEL + 1
      STATUS = GETARG( LEVEL, PPATH, 40)
      IF (.NOT.( STATUS .EQ. -1 .AND. LEVEL .GT. 1 ))GOTO 23007
      GOTO 23006
23007 CONTINUE
      IF (.NOT.( STATUS .EQ. -1 ))GOTO 23009
      CALL GWDIR( PPATH, 5)
23009 CONTINUE
23008 CONTINUE
      CALL STRCPY( DEFPAT, FILE)
      IF (.NOT.( OPENDR( PPATH, DESC) .EQ. -3 ))GOTO 23011
      CALL STRCPY( PPATH, BUF)
      CALL MKPATH( BUF, PPATH)
      IF (.NOT.( PPATH(1) .EQ. 0 ))GOTO 23013
      CALL CANT(BUF)
23013 CONTINUE
      CALL EXPPTH( PPATH, DEPTH, PTR, BUF)
      J = PTR(DEPTH)
      JUNK = GTFTOK( PPATH, J, FILE)
      J = PTR(DEPTH)
      PPATH(J) = 0
      IF (.NOT.( OPENDR( PPATH, DESC) .EQ. -3 ))GOTO 23015
      CALL CANT(PPATH)
23015 CONTINUE
23011 CONTINUE
      IF (.NOT.( DIRSW .EQ. 1 ))GOTO 23017
      CALL CONCAT( FILE, DIRPAT, FILE)
23017 CONTINUE
      CALL FOLD(FILE)
      IF (.NOT.( GETPAT( FILE, PAT) .EQ. -3 ))GOTO 23019
      CALL ERROR( 18H? Illegal pattern. )
23019 CONTINUE
      IF (.NOT.( SORTIT .EQ. 1 ))GOTO 23021
      OUTFIL = CREATE( SCRAT, 2)
      IF (.NOT.( OUTFIL .EQ. -3 ))GOTO 23023
      CALL ERROR( 32H? Error creating temporary file. )
23023 CONTINUE
      GOTO 23022
23021 CONTINUE
      OUTFIL = 2
23022 CONTINUE
23025 IF (.NOT.( GDRPRM( DESC, FILE) .NE. -1 ))GOTO 23026
      I = LENGTH(FILE) + 1
      FILE(I) = 10
      FILE( I + 1 ) = 0
      CALL FOLD(FILE)
      IF (.NOT.( MATCH( FILE, PAT) .EQ. 1 ))GOTO 23027
      I = LENGTH(FILE)
      FILE(I) = 0
      IF (.NOT.( PACKIT .EQ. 1 .AND. OUTFIL .EQ. 2 ))GOTO 23029
      CALL DOPACK( FILE, NXTCOL, 80, OBUF, 2)
      GOTO 23030
23029 CONTINUE
      IF (.NOT.( VERBOS .EQ. 1 ))GOTO 23031
      CALL GDRAUX( DESC, FILE, BUF, PPATH, FMTCTL)
      IF (.NOT.( DODATE .EQ. 1 ))GOTO 23033
      CALL PUTLIN( PPATH, OUTFIL)
23033 CONTINUE
      CALL PUTLIN( BUF, OUTFIL)
      GOTO 23032
23031 CONTINUE
      CALL PUTLIN( FILE, OUTFIL)
23032 CONTINUE
      CALL PUTCH( 10, OUTFIL)
23030 CONTINUE
23027 CONTINUE
      GOTO 23025
23026 CONTINUE
      CALL CLOSDR(DESC)
      IF (.NOT.( SORTIT .EQ. 1 ))GOTO 23035
      CALL CLOSE(OUTFIL)
      CALL SORTLS( SCRAT, REVERS, BUF)
      OUTFIL = OPEN( SCRAT, 1)
      IF (.NOT.( OUTFIL .EQ. -3 ))GOTO 23037
      CALL ERROR( 37HError in opening sorted scratch file. )
23037 CONTINUE
23039 IF (.NOT.( GETLIN( BUF, OUTFIL) .NE. -1 ))GOTO 23040
      IF (.NOT.( DODATE .EQ. 1 ))GOTO 23041
      I = 11 + 1
      GOTO 23042
23041 CONTINUE
      I = 1
23042 CONTINUE
      IF (.NOT.( PACKIT .EQ. 1 ))GOTO 23043
      I = 1
      JUNK = GETWRD( BUF, I, FILE)
      CALL DOPACK( FILE, NXTCOL, 80, OBUF, 2)
      GOTO 23044
23043 CONTINUE
      CALL PUTLIN( BUF(I), 2)
23044 CONTINUE
      GOTO 23039
23040 CONTINUE
      CALL CLOSE(OUTFIL)
23035 CONTINUE
      IF (.NOT.( PACKIT .EQ. 1 ))GOTO 23045
      CALL FLPACK( NXTCOL, 80, OBUF, 2)
23045 CONTINUE
23005 GOTO 23004
23006 CONTINUE
      CALL ENDLS
      RETURN
      END
      SUBROUTINE ENDLS
      INTEGER JUNK
      INTEGER REMOVE
      COMMON / CLS / VERBOS, DODATE, REVERS, SORTIT, DIRSW, DOHEAD, PACK
     *IT, SCRAT(40), FMTCTL(512)
      INTEGER VERBOS
      INTEGER DODATE
      INTEGER REVERS
      INTEGER SORTIT
      INTEGER DIRSW
      INTEGER DOHEAD
      INTEGER PACKIT
      LOGICAL*1 SCRAT
      LOGICAL*1 FMTCTL
      JUNK = REMOVE(SCRAT)
      RETURN
      END
      SUBROUTINE INITLS(ARG)
      INTEGER I, GETARG, INDEXC, ISATTY
      LOGICAL*1 ARG(40)
      COMMON / CLS / VERBOS, DODATE, REVERS, SORTIT, DIRSW, DOHEAD, PACK
     *IT, SCRAT(40), FMTCTL(512)
      INTEGER VERBOS
      INTEGER DODATE
      INTEGER REVERS
      INTEGER SORTIT
      INTEGER DIRSW
      INTEGER DOHEAD
      INTEGER PACKIT
      LOGICAL*1 SCRAT
      LOGICAL*1 FMTCTL
      LOGICAL*1 DFLFMT(16)
      LOGICAL*1 USESTR(47)
      DATA DFLFMT(1)/49/,DFLFMT(2)/55/,DFLFMT(3)/110/,DFLFMT(4)/32/,DFLF
     *MT(5)/112/,DFLFMT(6)/32/,DFLFMT(7)/32/,DFLFMT(8)/109/,DFLFMT(9)/32
     */,DFLFMT(10)/32/,DFLFMT(11)/54/,DFLFMT(12)/98/,DFLFMT(13)/32/,DFLF
     *MT(14)/32/,DFLFMT(15)/111/,DFLFMT(16)/0/
      DATA USESTR(1)/117/,USESTR(2)/115/,USESTR(3)/97/,USESTR(4)/103/,US
     *ESTR(5)/101/,USESTR(6)/58/,USESTR(7)/32/,USESTR(8)/32/,USESTR(9)/1
     *08/,USESTR(10)/115/,USESTR(11)/32/,USESTR(12)/91/,USESTR(13)/45/,U
     *SESTR(14)/49/,USESTR(15)/100/,USESTR(16)/104/,USESTR(17)/110/,USES
     *TR(18)/114/,USESTR(19)/116/,USESTR(20)/118/,USESTR(21)/93/,USESTR(
     *22)/32/,USESTR(23)/91/,USESTR(24)/45/,USESTR(25)/102/,USESTR(26)/1
     *15/,USESTR(27)/116/,USESTR(28)/114/,USESTR(29)/105/,USESTR(30)/110
     */,USESTR(31)/103/,USESTR(32)/93/,USESTR(33)/32/,USESTR(34)/91/,USE
     *STR(35)/112/,USESTR(36)/97/,USESTR(37)/116/,USESTR(38)/104/,USESTR
     *(39)/110/,USESTR(40)/97/,USESTR(41)/109/,USESTR(42)/101/,USESTR(43
     *)/93/,USESTR(44)/46/,USESTR(45)/46/,USESTR(46)/46/,USESTR(47)/0/
      CALL QUERY(USESTR)
      VERBOS = 0
      DOHEAD = 0
      DODATE = 0
      REVERS = 0
      DIRSW = 0
      PACKIT = ISATTY(2)
      SORTIT = 0
      CALL STRCPY( DFLFMT, FMTCTL)
      I = 1 
23047 IF (.NOT.(GETARG( I, ARG, 40) .NE. -1 ))GOTO 23049
      CALL FOLD(ARG)
      IF (.NOT.( ARG(1) .EQ. 45 ))GOTO 23050
      IF (.NOT.( INDEXC( ARG, 102) .GT. 0 ))GOTO 23052
      CALL SCOPY( ARG, 3, FMTCTL, 1)
      VERBOS = 1
      GOTO 23053
23052 CONTINUE
      IF (.NOT.( INDEXC( ARG, 118) .GT. 0 .OR. INDEXC( ARG, 108) .GT. 0)
     *)GOTO 23054
      VERBOS = 1
23054 CONTINUE
      IF (.NOT.( INDEXC( ARG, 104) .GT. 0 ))GOTO 23056
      DOHEAD = 1
23056 CONTINUE
      IF (.NOT.( INDEXC( ARG, 116) .GT. 0 ))GOTO 23058
      DODATE = 1
23058 CONTINUE
      IF (.NOT.( INDEXC( ARG, 114) .GT. 0 ))GOTO 23060
      REVERS = 1
23060 CONTINUE
      IF (.NOT.( INDEXC( ARG, 110) .GT. 0 .AND. 0 .EQ. 0 ))GOTO 23062
      SORTIT = 1
23062 CONTINUE
      IF (.NOT.( INDEXC( ARG, 100) .GT. 0 ))GOTO 23064
      DIRSW = 1
23064 CONTINUE
      IF (.NOT.( INDEXC( ARG, 49) .GT. 0 ))GOTO 23066
      PACKIT = 0
23066 CONTINUE
23053 CONTINUE
      CALL DELARG(I)
      I = I - 1
23050 CONTINUE
23048 I = I + 1 
      GOTO 23047
23049 CONTINUE
      IF (.NOT.( DODATE .EQ. 1 ))GOTO 23068
      VERBOS = 1
23068 CONTINUE
      IF (.NOT.( DODATE .EQ. 1 .OR. REVERS .EQ. 1 ))GOTO 23070
      SORTIT = 1
23070 CONTINUE
      IF (.NOT.( VERBOS .EQ. 0 ))GOTO 23072
      DOHEAD = 0
      GOTO 23073
23072 CONTINUE
      PACKIT = 0
23073 CONTINUE
      IF (.NOT.( SORTIT .EQ. 1 ))GOTO 23074
      CALL SCRATF( LS0, SCRAT)
23074 CONTINUE
      RETURN
      END
      SUBROUTINE SORTLS( FILE, REVERS, BUF)
      LOGICAL*1 BUF(100), DESCR(7), FILE(100), PROC(40)
      LOGICAL*1 SPATH(120)
      INTEGER I, REVERS
      INTEGER LOCCOM, SSPAWN
      LOGICAL*1 SUFFIX(7)
      LOGICAL*1 PLUSO(4)
      LOGICAL*1 MR(4)
      LOGICAL*1 SORT(5)
      DATA SUFFIX(1)/46/,SUFFIX(2)/116/,SUFFIX(3)/115/,SUFFIX(4)/107/,SU
     *FFIX(5)/0/,SUFFIX(6)/10/,SUFFIX(7)/0/
      DATA PLUSO(1)/32/,PLUSO(2)/43/,PLUSO(3)/111/,PLUSO(4)/0/
      DATA MR(1)/32/,MR(2)/45/,MR(3)/114/,MR(4)/0/
      DATA SORT(1)/115/,SORT(2)/111/,SORT(3)/114/,SORT(4)/116/,SORT(5)/0
     */
      I = 1
      CALL STCOPY( SORT, 1, BUF, I)
      CALL CHCOPY( 32, BUF, I)
      CALL STCOPY( FILE, 1, BUF, I)
      CALL STCOPY( PLUSO, 1, BUF, I)
      CALL STCOPY( FILE, 1, BUF, I)
      IF (.NOT.( REVERS .EQ. 1 ))GOTO 23076
      CALL STCOPY( MR, 1, BUF, I)
23076 CONTINUE
      BUF(I) = 0
      CALL IMPATH(SPATH)
      IF (.NOT.( LOCCOM( SORT, SPATH, SUFFIX, PROC) .NE. 60 ))GOTO 23078
      CALL ERROR( 31H? Can't locate SORT image file. )
23078 CONTINUE
      IF (.NOT.( SSPAWN( PROC, BUF, DESCR, 119) .EQ. -3 ))GOTO 23080
      CALL ERROR( 24H? Error in spawning SORT )
23080 CONTINUE
      RETURN
      END
