; UNT.MAC - 5/11/87 - Untangle the mail file. ; When we get here, we assume that TNC has either opened an ; existing MAIL.DAT, or created a new one. Assume the file ; header has been read in. .z80 include TNC.LIB entry unt,mm13,mm14 external mfhs,mfhd,mlhd,mnext,mffree,mlfree,mfree external mcnt,mmhs,mhsize,mhnr,mhread,mhnext external mudt,mutm,mumnr,mhext,mhprev,mnr external mhfmsg,mhlmsg,mmts,mtnext,mhcur external @fill,@move,@prtx,bindec,numb,div8,getbuf external mfcb,mbfcb,fcb2,fcb2r,fcb3,fcb3r external memtop,$memry,date,time,curtime,opt2,inibak asciictl bdosdef tncdefs dseg t: ds 2 ms: ds 2 prev: ds 2 as: ds 2 bufptr: ds 2 bufcnt: ds 2 inbuf: ds 2 wtptr: ds 2 cseg mm13: ds 2 mm14: ds 2 ; Check if MAIL.DAT has any msgs. unt: prtx mm14 ld hl,(mm13) ; "no msgs" dtz mfhd ; Any messages? jp z,@prtx ; Tell no msgs ; Delete old .BAK, rename or copy .DAT to .BAK ; make new .BAK zmov fcb2,mfcb,fcbsize zmov fcb3,mbfcb,fcbsize call inibak ; Init for backup ret z ; Didn't work lxim fcb3r,0 dodosa setdma,mfhs dodosa rrec,fcb3 ; Read the file header movw mhcur,mfhd ; Save pointer to first hdr ; Initialize the file header. lxim mnext,1 lxim mfhd,1 lxim mffree,0 lxim mlfree,0 lxim mfree,0 lxim mcnt,0 call curtime move mudt,date,6 move mutm,time,4 movw mumnr,mnr lxim prev,0 ; No previous hdr call getbuf ld (as),hl ; Sectors of buffer available ; Copy the msg hdr record. domsg: inxm mcnt ; Count this msg movw fcb3r,mhcur dodosa setdma,mmhs dodosa rrec,fcb3 cmpm opt2,'R' ; Renumbering the messages? jr nz,domsga ; No movw mhnr,mcnt ; Renumber this one inc hl ; Next available number ld (mnr),hl ; into file header domsga: ld hl,(mhsize) ; Size in bytes ld de,125 add hl,de ; Round up ld e,126 call div8 ld h,0 ld (ms),hl ; Size in sectors call phd ; Print msg info movw mlhd,mnext ; This is current last hdr movw mhcur,mhnext ; Next hdr will be new current movw t,mhfmsg ; Save first text record # ld hl,(mnext) ; Next avail record inc hl ; plus 1 ld (mhfmsg),hl ; is new first text record dec hl ; and current record ld de,(ms) ; plus # records of text add hl,de ld (mhlmsg),hl ; is new last text record ld hl,0 ; There is no extension of hdr record ld (mhext),hl ; in the 820 version of system movw mhprev,prev ; Set previous hdr pointer dtz mhnext ; Is there a next msg? jr z,domsgc ; No ld hl,(mhlmsg) ; Last record in msg inc hl ; plus 1 ld (mhnext),hl ; is next header record domsgc: movw fcb2r,mnext ; Point I/O at hdr record dodosa wrec,fcb2 ; Write the hdr movw prev,mnext ; Current becomes previous inxm mnext ; Increment next avail record ld (wtptr),hl ; Where to begin writing domsgd: call rdbuf ; Read a bunch call wtbuf ; Write em dtz t ; More bunches? jr nz,domsgd ; Yes, do it dtz mhcur ; Another msg? jp nz,domsg ; Yes, do it lxim fcb2r,0 ; Point to file hdr dodosa setdma,mfhs ; Point I/O to file hdr buffer dodosa wrec,fcb2 ; Write file hdr dodosa close,fcb2 ; Close file dodosa open,mfcb ; Open it again, correct fcb ret ; Read a bunch of msg text records. rdbuf: movw bufptr,$memry ; Point to start of buffer space movw bufcnt,as ; Init avail buffer count lxim inbuf,0 ; Init count of records read rdbufa: movw fcb3r,t ; Point I/O to text record ld de,(bufptr) dodosa setdma ; and to text record buffer dodosa rrec,fcb3 ; Read text record inxm inbuf ; Count records in buffer inxm mnext ; and records used ld hl,(bufptr) ld e,(hl) inc hl ld d,(hl) ld (t),de ; Save next text record # ld a,e or d ; Is a next? ret z ; No ld de,(mnext) ; Next available record+1 ld hl,(bufptr) ld (hl),e inc hl ld (hl),d ; is new next text record dec hl ld de,128 add hl,de ld (bufptr),hl ; Point to next buffer dcxm bufcnt ; One less available ld a,l or h ; Any left jr nz,rdbufa ; Yes, read next ret ; Write the bunch of msg text records. wtbuf: movw bufptr,$memry wtrec: ld de,(bufptr) dodosa setdma movw fcb2r,wtptr dodosa wrec,fcb2 ; Write text record ld hl,(bufptr) ld de,128 add hl,de ld (bufptr),hl inxm wtptr dcxm inbuf ld a,l or h ; Done? jr nz,wtrec ; No ret dseg hdrm: dw $+2 db 'Hdr ' hdrm1: db ' ' db ' Bytes ' hdrm2: db ' ' db ' Sec ' hdrm3: db ' ' db ' # ' hdrm4: db ' ' db ' read ' hdrm5: db ' ',cr,0 cseg phd: ld hl,(mhcur) call bindec move hdrm1,numb,5 ld hl,(mhsize) call bindec move hdrm2,numb,5 ld hl,(ms) call bindec move hdrm3,numb,5 ld hl,(mhnr) call bindec move hdrm4,numb,5 ld hl,(mhread) call bindec move hdrm5,numb,5 prtx hdrm ret end