C-------------------------------------------------------------------C
C  Directory Management        January 1983                         C
C  Written by John C. Hayre, Texas Instruments                      C
C  Send suggestions and/or complaints to:                           C
C                                                                   C
C               Veeco Integrated Automation			    C
C               10355 Brockwood Rd				    C
C               Dallas, Texas 75238                                 C
C               (Attention John Hayre)                              C
C                                                                   C
C  Phone: 214-349-8482                                              C
C-------------------------------------------------------------------C
C Utilities for Directory Managment:                              C
C-----------------------------------------------------------------C
C
C
C-----------------------------------------------------------------C
C Include a file for - Printing                                   C
C                    - Copying                                    C
C                    - Deleting                                   C
C Put the file that the cursor is beside into the 'INCLUD' array  C
C optionally put an asterik on the CRT.                           C
C-----------------------------------------------------------------C
       Subroutine Include(Cl, Showit)
       Implicit Integer*4 (A - Z)
       Character*128 LName
       Character*1 Debug
       Include 'Common.Dat'
C-----------------------------------------------------------------C
       If (Ld(Cl)(2:2) .NE. '*') then
         Call GetName(Ld(Cl), LName, NamType)  ! Get name 
         If (NamType .LT. 2) then      ! File or Dir?
           Ld(Cl)(2:2) = '*'           ! Insert asterik
           If (Showit .EQ. 1) then 
             If (Downlast .EQ. 1) then
               Call Dnarrow
             Else
               Call UpArrow
             End If
           End If
           IncluCount = IncluCount + 1 ! Bump include file count
           If (IncluCount .LE. 1) IncluCount = 1 ! Insurance
           IncluLines(IncluCount) = Cl
           Do I = IncluCount - 1, 1, -1
             If (IncluLines(I) .GT. Cl) then 
               IncluLines(I + 1) = IncluLines(I)
               IncluLines(I) = Cl
             End If
           End Do
         End If
       End If
       Return 
       End
C-------------------------------------------------------------------C
C Write the keypad help message.                                    C
C-------------------------------------------------------------------C
       Subroutine WritHelp
       Implicit Integer*4 (A - Z)
       Character*1 Com
       Character*60 Sym
       Character*132 OneLine
       Include 'Common.Dat'
       Character*3 Graph,ASCII
       Character*4 Nvid,Rvid
C-------------------------------------------------------------------C
100      Graph = Char(27)//'(0'  ! graphics mode
         ASCII = Char(27)//'(B'  ! ASCII character mode
         Nvid  = Char(27)//'[0m' ! normal video
         Rvid  = Char(27)//'[7m' ! reverse video
         Call Lib$Erase_Page(1,1)
         Call Writel(LargeScreen, 1, 1)
         OneLine = Graph//'lqqqqqqqwqqqqqqqwqqqqqqqwqqqqqqqk   '//ASCII/
     -/Rvid//' GOLD  '//Nvid//' - 2nd functions '
         Call Writel(OneLine, 1, 1)
         OneLine = Graph//'x'//Rvid//'       '//Nvid//'x       xFIND NXx
     -       x   '//ASCII//'HELP    - Display this Screen'
         Call WriteL(OneLine, 2, 1)
         OneLine =  Graph//'x'//Rvid//' GOLD  '//Nvid//'x HELP  x       
     -x       x   '//ASCII//'FIND NX - Find next occurence of string'
         Call WriteL(OneLine, 3, 1)
         OneLine = Graph//'x'//Rvid//'       '//Nvid//'x       x'//Rvid/
     -/' FIND  '//Nvid//'x       x   '//Rvid//' FIND  '//Nvid//ASCII//' 
     -- Find string'
         Call WriteL(OneLine, 4, 1)
         OneLine = Graph//'tqqqqqqqnqqqqqqqnqqqqqqqnqqqqqqqu   '
         Call WriteL(OneLine, 5, 1)
         OneLine = Graph//'x       x PAGE  x       x       x   '
     -//ASCII//'PAGE    - Page in current direction'
         Call WriteL(OneLine, 6, 1)
         Oneline = Graph//'x       x   8   x       x       x   '
         Call WriteL(OneLine, 7, 1)
         Oneline = Graph//'x'//Rvid//'  DCL  '//Nvid//'x       x       x
     -       x   '//ASCII//Rvid//'  DCL  '//Nvid//' - One DCL Command'
         Call WriteL(OneLine, 8, 1)
         Oneline = Graph//'tqqqqqqqnqqqqqqqnqqqqqqqnqqqqqqqu   '
         Call WriteL(OneLine, 9, 1)
         Oneline = Graph//'xADVANCEx BACKUPx       x       x   '//ASCII/
     -/'ADVANCE - Advance thru file'
         Call WriteL(OneLine, 10, 1)
         Oneline = Graph//'x   4   x   5   x       x       x   '//ASCII/
     -/Rvid//' BOTTOM'//Nvid//' - Go to bottom of file'
         Call WriteL(OneLine, 11, 1)
         Oneline = Graph//'x'//Rvid//' BOTTOM'//Nvid//'x'//Rvid//'  TOP 
     - '//Nvid//'x       x       x   '//ASCII//'BACKUP  - Backup '
         Call WriteL(OneLine, 12, 1)
         Oneline = Graph//'tqqqqqqqnqqqqqqqnqqqqqqqnqqqqqqqu   '//ASCII/
     -/Rvid//'  TOP  '//Nvid//' - Go to top of file'
         Call WriteL(OneLine, 13, 1)
         Oneline =  Graph//'x       x       x       x       x   '
         Call WriteL(OneLine, 14, 1)
         Oneline = Graph//'x       x SHOW  x  POP  x       x   '//ASCII/
     -/'POP     - go up one dir'
         Call WriteL(OneLine, 15, 1)
         Oneline = Graph//'x       x   2   x   3   x       x   '//ASCII/
     -/'SHOW    - file or dir '
         Call WriteL(OneLine, 16, 1)
         Oneline = Graph//'tqqqqqqqvqqqqqqqnqqqqqqqu       x   '
         Call WriteL(OneLine, 17, 1)
         Oneline = Graph//'x    1 LINE     x       x       x   '
         Call WriteL(OneLine, 18, 1)
         Oneline = Graph//'x               x       x       x   '//ASCII/
     -/'1 LINE  - Move 1 line up or down '
         Call WriteL(OneLine, 19, 1)
         Oneline = Graph//'x               x       x       x   '
         Call WriteL(OneLine, 20, 1)
         Oneline = Graph//'mqqqqqqqqqqqqqqqvqqqqqqqvqqqqqqqj'//ASCII
         Call WriteL(OneLine, 21, 1)
         Oneline =  '   '
         Call WriteL(OneLine, 22, 1)
         Oneline = '    Enter ''M'' for more help, ' 
     -//'any other key to continue.'
         Call WriteL(OneLine, 23, 1)
         Call Get1Char(Com, 0)
         If ((Com .EQ. 'M') .OR. (Com .EQ. 'm')) then
           Call Help
           Call Get1Char(Com, 0)
         End If
         If ((Com .EQ. 'M') .OR. (Com .Eq. 'm')) Goto 100
         Call Writel(SmallScreen,24,10)
         Call Write24
         CMD = ' '
       Return
       End
C-----------------------------------------------------------------C
C Exclude Utility:                                                C
C Undo the exclude.  Take the file name out of the 'INCLU' array  C
C and take out the asterik from the 'LD' array.                   C
C-----------------------------------------------------------------C
       Subroutine Exclude
       Implicit Integer*4 (A - Z)
       Include 'Common.Dat'
C-------------------------------------------------------------------C
       Cl = TopLine + CursorLine - 1
       If (Ld(Cl)(2:2) .EQ. '*') then
         Tx = 0  !index of target name
         Do I = 1, IncluCount       !ensure target is in array
           If (IncluLines(I) .EQ. Cl) Tx = I
         End Do
         If (Tx .NE. 0) then            ! take target out of array
           Do I = Tx, IncluCount - 1
             IncluLines(I) = IncluLines(I + 1)
           End Do
           IncluLines(IncluCount) = 0   ! for debug purposes
           IncluCount = IncluCount - 1  ! decrement array count
           SvDLast = DownLast
           Call DnArrow                 ! show asterik removed
           Call UpArrow                 !
           DownLast = SvDLast
           If (IncluCount .LT. 0) IncluCount = 0 ! Insurance
         End If
       End If
       Ld(Cl)(2:2) = ' '            ! remove asterik
       If (Downlast .EQ. 1) then
         Call Dnarrow
       Else
         Call UpArrow
       End If
       Return 
       End
C-------------------------------------------------------------------C
C Output a message to line 23 of the CRT.                           C
C-------------------------------------------------------------------C
       Subroutine Mess(Strng)
       Implicit Integer*4 (A - Z)
       Character*(*) Strng
       Character*8 Temp8
       Include 'Common.Dat'
C-------------------------------------------------------------------C
       Clean23 = 1
       Call Lib$Erase_Line(23, 1)
       Call Writel(ReVideo, 23, 1)
       L = Len(Strng)
       Call Writel(Strng(1:L), 23, 1)
       Call Writel(Dull, 23, L + 2)
       Call Lib$Set_Cursor(23, L + 2)
       Return
       End
C-----------------------------------------------------------------C
C Find String utility.  Look for a string in Ld.  If the string isC
C found show the line, otherwise, put a message on the bottom of  C
C the CRT.                                                        C
C-----------------------------------------------------------------C
       Subroutine ShowSRCH
       Implicit Integer*4 (A - Z)
       Character*1 chom
       Include 'Common.Dat'
C-----------------------------------------------------------------C
       If (SrchSize .LT. 1) Return
       Element = 0
       If (Forward .EQ. 1) then 
         Do 100 I = TopLine + CursorLine, LastLine
           If (Ld(I)(1:26)
     1     .EQ. '                          ') goto 100
           If (Ld(I)(1:32)
     1     .EQ. '         Directories:  Total of ') goto 100
           If (Ld(I)(1:32)
     1     .EQ. '               Files:  Total of ') goto 100
           If (Ld(I)(1:26)
     1     .EQ. '          This Directory: ') goto 100
           If (Ld(I)(1:26)
     1     .EQ. '      Previous Directory: ') Goto 100
           G = Lib$Matchc(Search(1:SrchSize), Ld(I))
           If (G .NE. 0) then 
             Element = I
             Goto 300
           End If
100        Continue 
       Else
         Do 200 I = TopLine + CursorLine - 2, 1, -1
           If (Ld(I)(1:26)
     1     .EQ. '                          ') goto 200
           If (Ld(I)(1:32)
     1     .EQ. '         Directories:  Total of ') goto 200
           If (Ld(I)(1:32)
     1     .EQ. '               Files:  Total of ') goto 200
           If (Ld(I)(1:26)
     1     .EQ. '          This Directory: ') goto 200
           If (Ld(I)(1:26)
     1     .EQ. '      Previous Directory: ') Goto 200
           G = Lib$Matchc(Search(1:SrchSize), Ld(I))
           If (G .NE. 0) then 
             Element = I
             Goto 300
           End If
200      Continue
       End If
C-------------------------------------------------------------------C
C String not found.  Show information message on CRT line 23.       C
C-------------------------------------------------------------------C
       Call Mess(' String not found   ')
       Goto 9999
C-------------------------------------------------------------------C
C String was found.  Show line that has string.                     C
C-------------------------------------------------------------------C
300    Continue
       If (Clean23 .EQ. 1) then 
         Call Fix23
         Clean23 = 0
       End IF
       Cl = TopLine + Cursorline - 1
       Call Writel(Dull, Cursorline, 1)
       Call Writel(Ld(Cl), Cursorline, 1)
       If ((Element .LT. TopLine + 22)  !already at right page
     - .AND. (Element .GE. Topline)) then
         CursorLine = Element - TopLine + 1
       Else
         TopLine = Element
         Cursorline = 1
         Call Write24
         tttt = Topline + Cursorline
         Call UpArrow
         If (TTTT .NE. Topline + Cursorline) then 
           Call DnArrow
         End If
       End If
9999   Return
       End
C-----------------------------------------------------------------C
C Fix CRT line 23 back after putting a message on it.             C
C-----------------------------------------------------------------C
       Subroutine Fix23
       Implicit Integer*4 (A - Z)
       Character*1 G
       Character*8 Temp8
       Character*3 Temp3
       Character*80 Temp80
       Include 'Common.Dat'
C-----------------------------------------------------------------C
       Call Writel(Dull, 23, 1)
       Call Writel(UnderLine, 23, 1)
       Call Lib$Put_Screen(Blanks, 23, 1)
       Call Writel(Dull, 23, 76)
       Do I = 11, 63
         X = I
        If ((DisName(I:I) .EQ. ' ') .OR. (DisName(I:I) .EQ. Char(0)))
     -   Goto 200
       End Do
200    Continue             
         Encode(3, 300, Temp3) Version
300      Format(I3)
         Call Writel(ReVideo, 23, 65)
         Call Writel('DM Version '//Temp3, 23, 3)
         Call Writel(Dull, 23, 80)
       Y = 64
       If (Fast .EQ. 1) Y = Y + 14
       Call Writel(Revideo, 23, Y-X)
       Call Writel(DisName(1:X), 23, Y-X)
       Call Writel(Dull, 23, 80)
       If (Fast .EQ. 0) then
         Encode(8, 400, Temp8) Total_Blks
400      Format(I8)
         Call Writel(ReVideo, 23, 65)
         Call Writel(Temp8//' Blocks', 23, 65)
         Call Writel(Dull, 23, 80)
       End If
       Clean23 = 0  ! Tell them that it is clean.
       Return
       End
C-----------------------------------------------------------------C
C Find String utility.  Get the characters to look for from the   C
C user into Search.  Also set srchsize to the string size.        C
C Then call FindStrg.                                             C
C-----------------------------------------------------------------C
       Subroutine FindStrg
       Implicit Integer*4 (A - Z)
       Integer*2 NBytes
       Character*80 Stg
       Include 'Common.Dat'
C-----------------------------------------------------------------C
       Call Mess('Enter search string: ')
       Call KBDName(SEARCH, SRCHSIZE)
       Call Fix23
       Call ShowSrch
       Return
       End
C-----------------------------------------------------------------C
C Get Ptotect Utility:                                            C
C Get the protection of a file passed in X.  Convert it to an     C
C ASCII string of form '(X,X,X,X)' where X is the RWED protectionsC
C of the file.                                                    C
C-----------------------------------------------------------------C
       Subroutine GetProt(X, Stg)
       Implicit Integer*4 (A - Z)
       Character*21 Stg
       Include 'Common.Dat'
C-----------------------------------------------------------------C
       In = 2  !Index into stg
       Stg = '(                    '
       Do I = 1, 4
         Do J = 1, 4
           Call GetPro2(Bitx, X)
           If (Bitx .EQ. 1) then
             If (J .EQ. 1) Stg(In : In) = 'R'
             If (J .EQ. 2) Stg(In : In) = 'W'
             If (J .EQ. 3) Stg(In : In) = 'E'
             If (J .EQ. 4) Stg(In : In) = 'D'
             In = In + 1
           End If
         End Do
         Stg(In:In) = ','
         In = In + 1
       End Do
       Stg(In - 1 : In - 1) = ')'  ! put on top of last ','
       Return 
       End
C-----------------------------------------------------------------C
C Get Ptotect Utility2                                            C
C  Shift bits around.  Very awkard in fortran!                    C
C-----------------------------------------------------------------C
       Subroutine GetPro2(Bitx, Wordx)
       Implicit Integer*4 (A - Z)
       Include 'Common.Dat'
C-------------------------------------------------------------------C
       Y = Wordx / 2
       Y = Y * 2
       If (Y .EQ. Wordx) then 
         Bitx = 1
       Else
         Bitx = 0
       End If
       Wordx = Wordx / 2
       Return 
       End
C-----------------------------------------------------------------C
C Help Utility:                                                   C
C Print the help message on the CRT.                              C
C-----------------------------------------------------------------C
       Subroutine Help
       Implicit Integer*4 (A - Z)
       Include 'Common.Dat'
C-------------------------------------------------------------------C
       Call Lib$Erase_Page(1,1)
       Print *, BRIGHT//'  Directory Management Single Letter Commands:'
       Print *, dull//'A/Z - Go to top/bottom of screen'
       print *, 'W  -  Select a wildcard for file display'
       print *, 'T  -  Tag a file - Used in D, C and P Commands'
       Print *, 'U  -  Untag'
       Print *, 'Q  -  Quit - Exit to starting place'
       Print *, 'X  -  Quit to last directory'
       Print *, 'R  -  Rename - Prompts for new file name'
       Print *, 'V  -  View either a file or a directory'
       Print *, 'E  -  Edit - Edt editor called.'
       Print *, 'F  -  FMS commands accepted.'
       Print *, '$  -  Spawn DCL command (more efficient than %).'
       Print *, '%  -  Leave DMM and run DCL command, then reenter DMM.'
       Print *, '@  -  Envoke COM file.'

       If (Fast .EQ. 1) then 
         Print *, 'M  -  Show all file information '
         Print *, 'I  -  Show file information for this file'
       Else
         Print *, 'L  -  Show Less File information '
       End If
       Print *, 'G  -  Purge - Purge "This Directory"'
       Print *, 'D  -  Delete - File(s) or Dir(s)'
       Print *, 'P  -  Print - File(s) to default printer'
       Print *, 'C  -  Copy - Prompts for file to copy into'
       print *
       Print *, 'Enter ''M'' for more help, another key to continue.'
       Return
       End
C-------------------------------------------------------------------C
C Conversion routine for ASCII directory names.                     C
C Used to get the name to print on the top line of the CRT.         C
C InName - Name of a directory - [ALL.IS.WELL.THAT.ENDS]WELL.DIR;1  C
C OutName - Returned previous dir - [ALL.IS.WELL.THAT.ENDS.WELL]    C
C-------------------------------------------------------------------C
       Subroutine PreDIR(InName, OutName)
       Implicit Integer*4 (A - Z)
       Character*128 InName,OutName
       Include 'Common.Dat'
C-------------------------------------------------------------------C
       I = Lib$Matchc(']',InName)
       OutName = InName(1:I)
       Do J = I + 1, 128
         OutName(J:J) = ' '
       End Do
       Return
       End
C-------------------------------------------------------------------C
C ASCII conversion routine.                                         C
C Given the directory name, return the file name of that directory. C
C InName - Name of a directory of form [ALL.IS.WELL.THAT.ENDS.WELL] C
C OutName - Returned name of form [ALL.IS.WELL.THAT.ENDS]WELL.DIR;1 C
C-------------------------------------------------------------------C
       Subroutine RealDIR(InName, OutName)
       Implicit Integer*4 (A - Z)
       Character*128 InName,OutName,XName
       Character*1 cccx
       Include 'Common.Dat'
C-------------------------------------------------------------------C
       If (InName(1:8) .EQ. '[000000]') then
         OutName(1:18) = '[000000]000000.DIR'
         OutName(19:) = ' '
       Else
         I = Lib$Matchc(']',InName)
         Do J = 1, 128 
           If (J .LT. I) then
             OutName(J:J) = InName(J:J)
           Else
             OutName(J:J) = ' '
           End If
         End Do
         Period = Lib$Matchc('.',InName)   
         If (Period .EQ. 0) then           ! add [000000] directory
           Outname(1 : 8) = '[000000]'
           Do K = 2, I - 1
             OutName(7 + K : 7 + K) = InName(K:K)
           End Do
         Else                      ! Has '.''s no need for [000000]
110         J = Lib$Matchc('.',Outname(Period + 1 : 128))
           Period = Period + J               ! I = the last '.'
           If (J .NE. 0) goto 110
           Outname(Period : Period) = ']'
         End If
         I = Lib$Matchc('  ',Outname)
         If (I .EQ. 0) stop
         Outname(I  : I + 3) = '.DIR'
       End If
       I = Lib$Matchc('[000000.000000', OutName) 
       If (I .NE. 0) then 
         G = Lib$Matchc('.000000', OutName(1:14))
         OutName(G:G) = '['
         Do J = 1, (128 - G) + 1
           OutName(J:J) = OutName(J+G-1:J+G-1)
         End Do
       End If
       Return
       End
C-------------------------------------------------------------------C
C SetChoices :  The variable "CHOICES" holds the ASCII that will beC
C  displayed on the bottom line of the CRT.  This is the only place C
C  that CHOICES gets changed.                                       C
C-------------------------------------------------------------------C
       Subroutine SetChoices(Speed)
       Implicit Integer*4 (A - Z)
       Include 'Common.Dat'
C-------------------------------------------------------------------C
      If (Speed .LE. 0) then  !all info is displayed
           Choices = 
     -Bright  //'$ % '//
     -          '@ A Z W '//
     -          'T'//Dull//'ag '
     -//Bright//'U'//Dull//'ntag '
     -//Bright//'Q'//Dull//'uit e'
     -//Bright//'X'//Dull//'it '
     -//Bright//'R'//Dull//'en '
     -//Bright//'V'//Dull//'iew '
     -//Bright//'E'//Dull//'DT '
     -//Bright//'F'//Dull//'MS '
     -//Bright//'L'//Dull//'ess '
     -//Bright//'D'//Dull//'el '
     -//Bright//'C'//Dull//'opy '
     -//'pur'//
     -  Bright//'G'//Dull//'e '
     -//Bright//'P'//Dull//'rint'
      Else
           Choices = 
     -Bright//  '$ % '//
     -          '@ A Z W '//
     -          'T'//Dull//'ag '
     -//Bright//'U'//Dull//'ntag '
     -//Bright//'Q'//Dull//'uit e'
     -//Bright//'X'//Dull//'it '
     -//Bright//'R'//Dull//'en '
     -//Bright//'V'//Dull//'iew '
     -//Bright//'E'//Dull//'DT '
     -//Bright//'F'//Dull//'MS '
     -//Bright//'M'//Dull//'ore '
     -//Bright//'I'//Dull//'nfo '
     -//Bright//'D'//Dull//'el '
     -//Bright//'C'//Dull//'opy '
     -//'pur'//Bright//'G'//Dull//'e '
     -//Bright//'P'//Dull//'rint'
      End If
      Return
      End
C-------------------------------------------------------------------C
C InName = One of the lines of the CRT.                             C
C OUTName = Returned name of file.                                  C
C Type = 0 for file type name                                       C
C      = 1 for directory type name                                  C
C      = 2 for 'Previous Directory' type name                       C
C-------------------------------------------------------------------C
       Subroutine GetName(InpName, OutpName, Type)
       Implicit Integer*4 (A - Z)
       Character*128 InpName,OutpName,NameXX
       Include 'Common.Dat'
C-------------------------------------------------------------------C
       Type = 0                 ! Assume a file type
       OutpName = '                                        '
       J = 1
       X = Lib$Matchc('Previous ',InpName) 
       If (X .NE. 0) then 
         J = 27 - 5
         Type = 2               ! 'Previous Directory' type
       End If
       K = 1
100    OutpName(K:K) = InpName(J+5:J+5)
       K = K + 1
       J = J + 1
       If (InpName(J+5:J+5) .NE. ' ') goto 100
       I = Lib$Matchc('.DIR',OutpName)
       If (I .NE. 0) then
         Type = 1   ! It is a 'DIR' not a file type
         NameXX = OutpName    ! Get current directory name
         OutpName = DName
         L = Lib$Matchc(']', OutpName)
         If (L .EQ. 0) Stop
         OutpName(L:L) = '.'
         Do M = 1, I - 1
           L = L + 1
           OutpName(L:L) = NameXX(M:M)
           OutpName(L + 1 : L + 3) = ']  ' ! ensure ends in "[  "
         End Do
       End If
       G = Lib$Matchc('[000000.', OutpName)
       If (G .NE. 0) then 
         G = Lib$Matchc('.', OutpName)
         Do I = G + 1, 128
           OutpName(I-7:I-7) = OutpName(I:I)
         End Do
       End If
       Return
       End
