 Rem File: Cmd026.bas
 Rem Sub: Display.Catalogs
 Rem $Include: 'dndbbs.inc'
 On Error Goto Main.Error
 If Chained<>-2 Then
    Call Get.Node
    Call Get.Config(Var1$,Var2$,Var3$,Ext$)
    Local.Mode=True : Sysop=True
    User.Linelength=80
    Call Read.Rows(Var)
    User.Pagelength=Var
    Max.Row=Var
    Locate Max.Row-1,1,1
    Call Save.Screen2(0)
    Call Edit.Status.Line
    Color 15 ,0
    Print "DNDBBS Command 026 -- Display.Catalogs."
    Do While Lost.Carrier=False
       Color 15
       Print "Display (C)atalog/(S)ummary/(Q)uit? ";
       Do
          Out2=Inkey$
          If Out2=Chr$(13) Then Print "Q" : Out2="Q" : Exit Do
          If Len(Out2) Then Print Ucase$(Out2) : Exit Do
       Loop
       If Ucase$(Out2)="Q" Then
          Exit Do
       Endif
       If Ucase$(Out2)="C" Then
          TempX=False
          Call Display.Catalogs(Var1$,Var2$,Var3$,Ext$)
       Endif
       If Ucase$(Out2)="S" Then
          TempX=True
          Call Display.Catalogs(Var1$,Var2$,Var3$,Ext$)
       Endif
    Loop
    Call Clear.Status.Line
    Color 7,0
    Call Save.Screen2(-1)
    If TempX=0 Then TempX=1
    Locate TempX,1,1
    Print
    Color 15
    Print "Now exiting Display.Catalogs.."
    Color 7
    End
 Endif
 Var1$=Config3(52)
 Var2$=Config3(50)
 Var3$=Config3(65)

 TempA$=Config3(78)
 TempB$=Config3(9)
 TempC$=Config3(79)

 Ext$=File.Extension2
 Call Display.Catalogs(Var1$,Var2$,Var3$,Ext$)
Main.Resume:
 Chain DND.Path+"DNDBBS.EXE"
 End
Main.Error:
 Resume Main.Resume

Sub Display.Catalogs(Var1$,Var2$,Var3$,Ext$)
 On Local Error Goto Err.Trp24
 Var=TempX
 If Var Then
    FileName=Var1$+Var2$+Ext$
 Else
    FileName=Var1$+Var3$+Ext$
 Endif
 If Dir$(Filename)<>Nul Then
    Call Out.File(Filename,"-3") ' fixed 05/26/2021
    If Quit Then
       Exit Sub
    Endif
 Endif
 If Disable.Nodes=False Then
    If Var Then
       FileName=Var1$+Var2$+"M"+Ext$
    Else
       FileName=Var1$+Var3$+"M"+Ext$
    Endif
    If Dir$(Filename)<>Nul Then
       Call Out.File2(Filename,"/",TempA$)
       If Quit Then
          Exit Sub
       Endif
    Endif
 Endif
 If Disable.DM=False Then
    If Normal.User=False Then
       If Var Then
          FileName=Var1$+Var2$+"D"+Ext$
       Else
          FileName=Var1$+Var3$+"D"+Ext$
       Endif
       If Dir$(Filename)<>Nul Then
          Call Out.File2(Filename,"!",TempB$)
          If Quit Then
             Exit Sub
          Endif
       Endif
    Endif
 Endif
 If Disable.Sysop=False Then
    If Sysop Then
       If Var Then
          FileName=Var1$+Var2$+"S"+Ext$
       Else
          FileName=Var1$+Var3$+"S"+Ext$
       Endif
       If Dir$(Filename)<>Nul Then
          Call Out.File2(Filename,"|",TempC$)
       Endif
    Endif
 Endif
Err.Rsm24:
 Exit Sub
Err.Trp24:
 Resume Err.Rsm24
End Sub

' specialized file display function.
Sub Out.File2(Var$,Var1$,Var3$)
 On Local Error Goto ErrorTrap23x
 If Dir$(Var$)=Nul Then
    If Local.Mode And Debug.Mode Then
       Carriage.Return=False
       Strng="Filename: "+Var$+" <not found>"
       If Chained=-2 Then
          Call IO.O
       Else
          Print Strng
       Endif
    Endif
    Exit Sub
 Endif
 Var=1
 If Local.Mode And Debug.Mode Then
    Carriage.Return=False
    Var=Var+1
    Strng="Filename: "+Var$
    If Chained=-2 Then
       Call IO.O
    Else
       Print Strng
    Endif
 Endif
 Var=Var+1
 Graphics.Off=False
 If Chained=-2 Then
    Call IO.O
 Endif
 Strng="File list. "
 Var2$="Press <ctrl-break>/<ctrl-c> to interrupt."
 If Local.Mode=False Then
    Var2$="Press <control-c>/<control-k> to interrupt."
 Endif
 Strng=Strng+Var2$
 If Chained=-2 Then
    Call IO.O
 Else
    Print Strng
 Endif
 Allow.Break=True
 Break=False
 Continuous=False
 Graphics.Off=True
 VarQ=False
 VarR=False
 Close #TempFile1
 Open Var$ For Input Shared As #TempFile1
 Call Modem.ANSI(4,37,15)
 Call ScreenANSI(4,37,15)
 Do While Lost.Carrier=False
    Allow.Break=True
    If Eof(TempFile1) Then
       Exit Do
    Endif
    Line Input #TempFile1,Strng
    ' convert prefix string
    If Len(Strng) Then
       VarP=0
       Do
          VarP=Instr(VarP+1,Strng,Var1$)
          If VarP Then
             Strng=Left$(Strng,VarP-1)+Var3$+Mid$(Strng,VarP+1)
             VarP=VarP+Len(Var3$)
          Else
             Exit Do
          Endif
       Loop
    Endif
    Strng2=Strng
    Graphics.On=True
    If Chained=-2 Then
       Call IO.O
    Else
       Print Strng
    Endif
    Graphics.On=False
    If Break Then
       Exit Do
    Endif
    Var=Var+1
    If Var>=User.Pagelength-2 Then
       Var=False
       If Continuous=False Then
          If Chained=-2 Then
             Call More.Prompt
          Else
             Color 14
             Print More$;
             Color 15
             Do
                Out2=Inkey$
                If Out2=Chr$(13) Then Print : Exit Do
                If Ucase$(Out2)="N" Then Print : No=True : Exit Do
                If Ucase$(Out2)="Q" Then Print : Quit=True : Exit Do
                If Ucase$(Out2)="Y" Then Print : Exit Do
                If Ucase$(Out2)="C" Then Print : Continuous=-1: Exit Do
             Loop
          Endif
          If No Or Quit Then
             Exit Do
          Endif
       Endif
    Endif
 Loop
 If Var>1 Then
    If Break=False Then
       If Chained=-2 Then
          Call More.Prompt
       Else
          Color 14
          Print More$;
          Color 15
          Do
             Out2=Inkey$
             If Out2=Chr$(13) Then Print : Exit Do
             If Len(Out2) Then Print : Exit Do
          Loop
       Endif
    Endif
 Endif
 Allow.Break=False
 Break=False
 Graphics.Off=True
ErrorResume23x:
 Exit Sub
ErrorTrap23x:
 Resume ErrorResume23x
End Sub

Sub Get.Config(Var1$,Var2$,Var3$,Ext$)
 On Local Error Goto Config.Error
 DND.Path=Environ$("DNDBBS")
 If DND.Path<>Nul Then
    If Right$(DND.Path,1)<>"\" Then
       DND.Path=DND.Path+"\"
    Endif
 Endif
 FileName=DND.Path+"DND"+NodeX+".CFG"
 Close #TempFile1
 Open FileName For Input Shared As #TempFile1
 Line Input #TempFile1,Var$
 For Temp1=1 To MaxConfigArray1
    Input #TempFile1,Var!
 Next
 For Temp1=1 To MaxConfigArray2
    Input #TempFile1,Var%
 Next
 For Temp1=1 To MaxConfigArray3
    Line Input #TempFile1,Var$
    Select Case Temp1
    Case 52
       Var1$=Var$ ' \dnddoc
    Case 50
       Var2$=Var$ ' summary
    Case 65
       Var3$=Var$ ' catalog
    Case 62
       Ext$=Var$ ' .doc
    Case 78
       TempA$=Var$ ' /
    Case 9
       TempB$=Var$ ' !
    Case 79
       TempC$=Var$ ' |
    Case 85
       ANSIPrefixChar=Var$ ' {
    End Select
 Next
 Exit Sub
Config.Error:
 Color 14,0
 Out2="CMD026 Module - Display.Catalogs"+Chr$(13)+Chr$(10)
 Out3="Error reading "+Lcase$(FileName)+" (error:"+Str$(Err)+"). Run Dndcnfg."
 Call Terminate.ProgramX
End Sub

Sub Get.Node
 On Local Error Goto Node.Error
 Var$=Command$
 Var$=Ltrim$(Rtrim$(Var$))
 If Instr(Var$,"/D") Then
    Debug.Mode=True
    Var$=Left$(Var$,Instr(Var$,"/D")-1)+Mid$(Var$,Instr(Var$,"/D")+1)
 Endif
 Var$=Ltrim$(Rtrim$(Var$))
 If Left$(Var$,3)="/N:" Then
    Var$=Mid$(Var$,4)
    Select Case Len(Var$)
    Case 0 ' /N:
       Node=0
       NodeX=Nul
       Var$=Nul
    Case 1 ' 0-9,A-Z
       Select Case Var$
       Case "0" To "9" 
          Node=Val(Var$)+1
          NodeX=Var$
          Var$=Nul
       Case "A" To "Z"
          Node=Asc(Var$)-54
          NodeX=Var$
          Var$=Nul
       Case Else
          Goto Node.Error
       End Select
    Case 2 ' 10-99
       VarX=Int(Val(Var$))
       If VarX>=10 And VarX<=99 Then
          Node=VarX+27 ' 37-126
          NodeX=Mid$(Str$(VarX),2) ' 10-99
          Var$=Nul
       Else
          Goto Node.Error
       Endif
    Case Else
       Goto Node.Error
    End Select
 Endif
 Var$=Ltrim$(Rtrim$(Var$))
 If Var$<>Nul Then Goto Node.Error
 Exit Sub
Node.Error:
 Color 14,0
 Out2="CMD026 Module - Display.Catalogs"+Chr$(13)+Chr$(10)
 Out3="Error reading node. Node must be <nul>,0-9,A-Z,10-99."+Chr$(13)+Chr$(10)
 Call Terminate.ProgramX
End Sub

Sub Terminate.ProgramX
 On Local Error Goto Error.Trap1
 Call ScreenANSI(4,37,15)
 Call ScreenANSI(2,Max.Row,1)
 If Len(Out2) Then
    Strng=Out2
    Call Scroll.Row(1)
 Endif
 If Len(Out3) Then
    Strng=Out3
    Call Scroll.Row(1)
 Endif
 Call Exit.To.DOSX
Error.Resume1:
 End
Error.Trap1:
 Resume Error.Resume1
End Sub

Sub Exit.To.DOSX
 On Local Error Goto Error.Trap2
 Call Extended.Driver(29)
 Call Modem.ANSI(1,37,7)
 Call ScreenANSI(4,33,7)
 Call ScreenANSI(2,Max.Row,1)
 If BBS.Mode Then
    Strng="Returning you to: "
    If Rtrim$(BBS.Name)=Nul Then
       Strng=Strng+"<n/a>"
    Else
       Strng=Strng+BBS.Name
    Endif
 Else
    If Windows.Detected Then
       Strng="Returning you to Windows Dos."
    Else
       Strng="Returning you to Dos."
    Endif
 Endif
 Call Scroll.Row(1)
 Call ScreenANSI(1,37,7)
 Color 7,0
Error.Resume2:
 Exit Sub
Error.Trap2:
 Resume Error.Resume2
End Sub

Sub Read.Rows(Var)
 On Local Error Goto Error.Trap62x
 Temp1=50
 Locate 50,1,0
 Var=50
 Exit Sub
Next.Trap1:
 Temp1=43
 Locate 43,1,0
 Var=43
 Exit Sub
Next.Trap2:
 Temp1=25
 Locate 25,1,0
 Var=25
 Exit Sub
Next.Trap3:
 Var=12
Error.Resume62x:
 Exit Sub
Error.Trap62x:
 If Temp1=50 Then
    Resume Next.Trap1
 Endif
 If Temp1=43 Then
    Resume Next.Trap2
 Endif
 If Temp1=25 Then
    Resume Next.Trap3
 Endif
 Resume Error.Resume62x
End Sub

Sub Edit.Status.Line
 On Local Error Goto Error.Trap58
 Var$="DND Display <timeon: "+Fclock$+">"
 Var$=Left$(Var$,79)
 Var$=Var$+Space$(79-Len(Var$))
 Call Display.Status.Line2(True,Var$)
Error.Resume58:
 Exit Sub
Error.Trap58:
 Resume Error.Resume58
End Sub

Sub Clear.Status.Line
 On Local Error Goto Error.Trap58a
 Var$=Space$(79)
 Call Display.Status.Line2(False,Var$)
Error.Resume58a:
 Exit Sub
Error.Trap58a:
 Resume Error.Resume58a
End Sub

Sub Display.Status.Line2(Var,Var$)
 On Local Error Goto Error.Trap29
 Call Cursor(False)
 If Var Then
    Color 14,1
 Else
    Color 15,0
 Endif
 Locate Max.Row,1,0
 Print Var$;
 If Var Then
    Color 0, 1
 Else
    Color 7, 0
 Endif
 Locate Max.Row,80
 Print " ";
 Locate Max.Row-1,1,1
 Graphics.Off=False
 Call Restore.Color
 Call Cursor(True)
Error.Resume29:
 Exit Sub
Error.Trap29:
 Resume Error.Resume29
End Sub

Sub Save.Screen2(Var)
 On Local Error Goto Error.Trap62
 If Var=False Then
    VarX3=(Max.Row-1)*80+80
    Redim Temp.ArrayY(VarX3) As Long
    Redim Temp.ArrayZ(VarX3) As Double
    For Var1=1 To Max.Row
       For Var2=1 To 80
          TempZ1=Screen(Var1,Var2)
          TempZ2=Screen(Var1,Var2,1)
          VarX2=(Var1-1)*80+Var2
          Temp.ArrayY(VarX2)=TempZ1
          Temp.ArrayZ(VarX2)=TempZ2
       Next
    Next
 Else
    Cls
    For VarX=Max.Row To 1 Step -1
       For VarY=1 To 80
          VarX2=(VarX-1)*80+VarY
          If Temp.ArrayY(VarX2)<>32 Then
             Goto Next.Line
          Endif
       Next
    Next
Next.Line:
    TempX=VarX
    For Var1=1 To VarX ' Max.Row
       For Var2=1 To 80
          VarX2=(Var1-1)*80+Var2
          VarB=Int(Temp.ArrayZ(VarX2)/16)
          VarF=Temp.ArrayZ(VarX2) Mod 16
          TempZ1=Temp.ArrayY(VarX2)
          Locate Var1,Var2,0
          Color VarF,VarB
          Print Chr$(TempZ1);
       Next
    Next
 Endif
Error.Resume62:
 Exit Sub
Error.Trap62:
 Resume Error.Resume62
End Sub
