;*************************** AMUS Program Label ******************************
; Filename: UUDECO.M68                                      Date: 9/25/90
; Category: UTIL         Hash Code: 323-700-606-131      Version: 1.0(100)
; Initials: ROBB/AM      Name: Erik Petrich
; Company: Microlink Computing Systems, Inc.       Telephone #: 4053218333
; Related Files: UUENCO.M68
; Min. Op. Sys.:                               Expertise Level: ADV
; Special: Requires the 2.0 assembler to reassemble.
; Description: This is uudecode ported to AMOS/L. It converts ascii files
; built by uuencode or UUENCO back to the binary originals.
;*****************************************************************************
;
; UUDECO - Copyright 1990 Microlink Computing Systems, Inc.
;
; Usage:
;   UUDECO {DestinationFile=}SourceFile {SourceFile...}
;
; Notes:
;   The ascii source can be split across several files.
;   UUDECO will ignore any lines in the source file that are previous to
;     the line starting with the word "begin".
;   UUDECO will ignore any lines in the source file that are following
;     the line starting with the word "end".
;   UUDECO will ignore any blank lines (unlike most implementations of
;     uudecode). This make using multiple source files a bit easier.
;   UUDECO supports lines terminated with LF or CR or CR/LF.
;   If successfully decoded, UUDECO will display the decoded file size
;     and value that sum reports. These are often specified along with
;     the encoded file(s) to verify that you have received and decoded
;     the file without any errors.
;
;Edit History:
;[100] 08 September 1990 00:43  Edited by Erik Petrich
;

       SEARCH  SYS
       SEARCH  SYSSYM
       SEARCH  TRM

       VMAJOR = 1.
       VMINOR = 0.
       VEDIT  = 100.

       .OFINI
       .OFDEF  infil,D.DDB
       .OFDEF  inbuf,512.
       .OFDEF  outfil,D.DDB
       .OFDEF  outbuf,512.
       .OFDEF  linbuf,200.
       .OFDEF  linsiz,2.
       .OFDEF  FoundBegin,2.
       .OFDEF  FoundEnd,2.
       .OFDEF  FileEnded,2.
       .OFDEF  Aborted,2.
       .OFDEF  CheckSum,2.
       .OFDEF  FileSize,4.
       .OFDEF  LastChar,2.
       .OFDEF  HexState,2
       .OFSIZ  impsiz

       PHDR    -1,0,PH$REE!PH$REU
       GETIMP  impsiz,a5
       JOBIDX
       movw    JOBTYP(a6),d7
       andw    #J.HEX,d7
       movw    d7,HexState(a5)
       andw    #^C<J.HEX>,JOBTYP(a6)

       BYP
       LIN
       jeq     Usage
       mov     a2,a1
       orb     #D$ERC,D.FLG+outfil(a5)
       clr     d6
       FSPEC   outfil(a5)
       jne     Abort
       byp
       movb    (a2)+,d1
       cmpb    d1,#'=
       bne     20$
       call    OpenOutput
       br      30$
20$:    mov     a1,a2
30$:

StartFile:
       BYP
       LIN
       jeq     Done
       orb     #D$ERC,D.FLG+infil(a5)
       clr     d6
       FSPEC   infil(a5)
       jne     Abort
       lea     a6,inbuf(a5)
       mov     a6,D.BUF+infil(a5)
       orb     #D$INI,D.FLG+infil(a5)
       clr     D.DVR+infil(a5)
       OPENI   infil(a5)
       jne     Abort

ReadLine:
       CTRLC   Abort
       lea     a0,linbuf(a5)
       clrw    linsiz(a5)
10$:    FILINB  infil(a5)
       jne     Abort
       tst     D.SIZ+infil(a5)
       beq     20$
       cmpb    d1,#13.
       beq     20$
       cmpb    d1,#10.
       beq     20$
       movb    d1,(a0)+
       INCW    linsiz(a5)
       cmpw    linsiz(a5),#200.
       blo     10$
       typecr  <Line too long.>
       jmp     Abort
20$:

       tstw    linsiz(a5)
       jeq     GetNextLine

       tstw    FoundBegin(a5)
       jeq     CheckBegin

       tstw    FileEnded(a5)
       jeq     DecodeLine

       tstw    FoundEnd(a5)
       jeq     CheckEnd

       jmp     Done

CheckBegin:
       cmpw    linsiz(a5),#5
       jlo     GetNextLine
       lea     a0,linbuf(a5)
       lea     a6,BeginTxt
       movw    #4.,d7
10$:    cmpb    (a0)+,(a6)+
       dbne    d7,10$
       jne     GetNextLine
       SETW    FoundBegin(a5)
       save    a2
       mov     a0,a2

       byp
       lin
       jeq     20$
       gtoct                   ; parse the permission level
       bmi     BadPermission
       cmp     d1,#777
       bhi     BadPermission

       byp
       lin
       jeq     20$
       tstb    D.OPN+outfil(a5)
       jne     20$
       orb     #D$BYP,D.FLG+outfil(a5)
       clr     d6
       FSPEC   outfil(a5)      ; parse the output filename
       jne     BadFilename
       andb    #^C<D$BYP>,D.FLG+outfil(a5)
       call    OpenOutput
       type    <Decoding to >
       pfile   outfil(a5)
       crlf

20$:    rest    a2
       jmp     GetNextLine
BeginTxt:
       ascii   /begin/
       EVEN
BadPermission:
       typecr  <Bad permission level specified in file.>
       jmp     Abort
BadFilename:
       typecr  <Invalid AMOS filename specified in file.>
       typecr  <Please specify a destination file on the>
       typecr  <command line when invoking UUDECO.>
       jmp     Abort

CheckEnd:
       cmpw    linsiz(a5),#3
       blo     20$
       lea     a0,linbuf(a5)
       lea     a6,EndTxt
       movw    #2.,d7
10$:    cmpb    (a0)+,(a6)+
       dbne    d7,10$
       jne     GetNextLine
       SETW    FoundEnd(a5)
       CLOSE   infil(a5)
       jne     Abort
       jmp     Done
20$:    jmp     GetNextLine
EndTxt:
       ascii   /end/
       EVEN

GetNextLine:
       tst     D.SIZ+infil(a5)
       jne     ReadLine
       CLOSE   infil(a5)
       jne     Abort
       jmp     StartFile


DecodeLine:
       lea     a0,linbuf(a5)
       clr     d2
       movb    (a0)+,d2
       subb    #32.,d2
       andb    #^h3f,d2
       bne     10$
       SETW    FileEnded(a5)
       jmp     GetNextLine

10$:    movw    #3.,d7
20$:    movb    (a0)+,d1
       subb    #32.,d1
       andb    #^H3f,d1
       lsl     d0,#6
       orb     d1,d0
       dbf     d7,20$
       lsl     d0,#8.
       movw    #2.,d3
30$:    tstw    d2
       jeq     GetNextLine
       rol     d0,#8.
       movb    d0,d1
       call    ByteOut
       DECW    d2
       dbf     d3,30$
       br      10$


ByteOut:
       rorw    CheckSum(a5)
       clr     d7
       movb    d1,d7
       addw    d7,CheckSum(a5)
       INC     FileSize(a5)
       tstb    D.OPN+outfil(a5)
       bne     10$
       cmpb    LastChar(a5),#13.
       beq     5$
       cmpb    d1,#10.
       bne     5$
       save    d1
       movb    #13.,d1
       TTY
       rest    d1
5$:     TTY
       movb    d1,LastChar(a5)
       rtn
10$:    FILOTB  outfil(a5)
       jne     Abort
       rtn

Abort:  CRLF
       SETW    Aborted(a5)
Done:   jobidx
       andw    #^C<J.HEX>,JOBTYP(a6)
       movw    HexState(a6),d7
       orw     d7,JOBTYP(a6)

       tstb    D.OPN+outfil(a5)
       beq     10$
       CLOSE   outfil(a5)
10$:    tstb    D.OPN+infil(a5)
       beq     20$
       CLOSE   outfil(a5)
20$:    tstw    Aborted(a5)
       beq     25$
       EXIT
25$:    tstw    FoundBegin(a5)
       bne     30$
       TYPECR  <No begin found>
       EXIT
30$:    tstw    FileEnded(a5)
       bne     40$
       TYPECR  <File incomplete>
       EXIT
40$:    tstw    FoundEnd(a5)
       bne     50$
       TYPECR  <No end found>
       EXIT
50$:
       TYPE    <File size =>
       mov     FileSize(a5),d1
       DCVT    0,OT$TRM
       CRLF
       TYPE    <      Sum =>
       clr     d1
       movw    CheckSum(a5),d1
       DCVT    0,OT$TRM
       CRLF
       EXIT

       EXIT

OpenOutput:
       lea     a6,outbuf(a5)
       mov     a6,D.BUF+outfil(a5)
       orb     #D$INI,D.FLG+outfil(a5)
       clr     D.DVR+outfil(a5)
       LOOKUP  outfil(a5)
       bne     10$
       DSKDEL  outfil(a5)
       jne     Abort
10$:    OPENO   outfil(a5)
       jne     Abort
       rtn

Usage:
       TYPECR  <UUDECO - Copyright 1990 Microlink Computing Systems, Inc.>
       CRLF
       TYPECR  <Usage:>
       TYPECR  <  UUDECO {DestinationFile=}SourceFile {SourceFile...}>
       EXIT

       end