home *** CD-ROM | disk | FTP | other *** search
/ CP/M / CPM_CDROM.iso / enterprs / cpm / terms / mite301.lbr / TEXTP.FQR / TEXTP.FOR
Text File  |  1993-05-08  |  6KB  |  269 lines

  1. C
  2. C    TEXTP - The Text Protocol
  3. C
  4. C       This program is designed to run on a mainframe or minicomputer
  5. C    and interact with the TEXT protocol in the MITE data communications
  6. C    package.  The protocol allows verified transmission of data into
  7. C    environments that ordinarily are unable to perform verified transfers
  8. C    of information.
  9. C
  10. C       Because of the extreme variety of systems on which this program
  11. C    may be implemented, this program is only a starting point.  The
  12. C    checksum routine will need extensive modification if the mainframe
  13. C    is not using ASCII representations. The input/output will need to be
  14. C    modified in most instances.  What this program is intended to be is
  15. C    complete documentation for the mainframe side of the protocol and an
  16. C    aid in implementing a version in a specific environment.
  17. C
  18. C       This version of TEXTP is written in Microsoft Fortran under CP/M-80.
  19. C
  20. C    Developed October 1983 by:
  21. C
  22. C       Donald Waldo
  23. C       Mycroft Labs, Inc.
  24. C       P.O. 6045
  25. C       Tallahassee, FL
  26. C       32314
  27. C
  28.     byte ifn(11)
  29.  5    write(1,10)
  30.  10    format(' Send (1) or Receive (2)?')
  31.     read(1,15)iopt
  32.  15    format(i1)
  33.     if((iopt.ne.1).and.(iopt.ne.2)) goto 5
  34.     write(1,20)
  35.  20    format(' Enter Filename')
  36.     read(1,25)ifn
  37.  25    format(11a1)
  38.     call open(5,ifn,0)
  39.     if(iopt.eq.1)call send
  40.     if(iopt.eq.2)call recv
  41.     endfile 5
  42.     goto 5
  43.     end
  44. C
  45. C
  46. C
  47. C
  48. C    Text file protocol - Receive
  49. C
  50. C    This subroutine receives a file using the TEXT
  51. C    file protocol.
  52. C
  53.     subroutine recv
  54.     integer*4 n,ick1,ick2,icksum,iconv,ick3
  55.     byte line(150)
  56.     data iE/69/  iA/65/   iN/78/    iX/88/
  57. C
  58. C    Initialize expected message number
  59. C
  60.     msgnum=0
  61. C
  62. C    Main loop - read lines and verify them.
  63. C    Each line transmitted by MITE starts with a 'D'.  This is followed
  64. C    by a 1 digit message number which helps improve error detection.
  65. C    Following the message number is a 3 digit count of the number of
  66. C    data characters on the line.  This is followed by a 5 digit (16 bit)
  67. C    checksum.  The checksum is a 16 bit sum of all characters on
  68. C    the line not including the checksum itself.  It is computed
  69. C    by summing the ASCII representation of the characters and
  70. C    ignoring overflow.  The checksum is followed by the data characters.
  71. C
  72.  30    read(1,12)line
  73.  12    format(150a1)
  74. C
  75. C    All done if 1st char = 'E'
  76. C
  77.     if(line(1).eq.iE) goto 900
  78. C
  79. C    Aborted if 1st char = 'X'
  80. C
  81.     if(line(1).eq.iX) goto 900
  82. C
  83. C    Get no. chars on line
  84. C
  85.     n=iconv(line,3,3)
  86.     if(n.lt.0) goto 40
  87. C
  88. C    compute checksum and verify against received value
  89. C
  90.     ick1=iconv(line,6,5)
  91.     if(ick1.lt.0) goto 40
  92.     ick2=icksum(line,1,5)
  93.     j=n
  94.     ick3=icksum(line,11,j)
  95.     ick2=ick2+ick3
  96.     if(ick2.ne.ick1) goto 40
  97. C
  98. C    Verify message number
  99. C
  100.     ick3=iconv(line,2,1)
  101.     msg=ick3
  102.     if(msg.eq.msgnum)goto 34
  103.     if(msg.eq.msgnum-1)goto 36
  104.     if((msgnum.eq.0).and.(msg.eq.9)) goto 36
  105.     goto 50
  106. C
  107. C    Line is good.  Write to disk and acknowlege.
  108. C
  109.  34    k=n+11
  110.     write(5,35)(line(i),i=11,k)
  111.  35    format(150a1)
  112.     msgnum=msgnum+1
  113.     if(msgnum.eq.10)msgnum=0
  114.  36    write(1,37)
  115.  37    format(3h RA/)
  116.     goto 30
  117. C
  118. C    Something went wrong.  Request that the line be retransmitted.
  119.  40    write(1,42)
  120.  42    format(3h RN/)
  121.     goto 30
  122. C
  123. C    Error - abort transmission
  124. C
  125.  50    write(1,52)
  126.  52    format(3h RX/)
  127. C
  128. C    Transmission complete.
  129. C
  130.  900    return
  131.     end
  132. C
  133. C
  134. C
  135. C
  136. C
  137.     subroutine send
  138. C
  139. C    Text file protocol send.
  140. C
  141. C    The data lines sent to MITE follow the same format described above.
  142. C
  143.     integer*4 icnt,ichk,iconv,icksum
  144.     byte line(80),line2(80)
  145.     data izero/48/  iblank/32/  iA/65/  iD/68/  iN/78/  iX/88/
  146. C
  147. C    Wait for OK from micro before sending first line.
  148. C
  149.     read(1,20)line
  150.     msgnum=-1
  151. C
  152. C    Get line from file
  153. C
  154.  15    read(5,20,end=50)line
  155.  20    format(80a1)
  156. C
  157. C    Increment message number, but keep in range 0-9
  158. C
  159.     msgnum=msgnum+1
  160.     if(msgnum.eq.10)msgnum=0
  161. C
  162. C    determine count
  163. C
  164.     i=80
  165.  24    if((line(i).ne.iblank).or.(i.eq.0)) goto 30
  166.     i=i-1
  167.     goto 24
  168.  30    icnt=i
  169. C
  170. C    Blank-fill output buffer
  171. C
  172.     do 32 i=1,80
  173.  32    line2(i)=iblank
  174. C
  175. C    compute checksum
  176. C
  177.     line2(1)=iD
  178.     line2(2)=msgnum+izero
  179.     call putnum(icnt,line2,3,3)
  180.     j=10+icnt
  181.     do 35 i=11,j
  182.  35    line2(i)=line(i-10)
  183.     j=icnt+5
  184.     ichk=icksum(line2,1,5)
  185.     ichk=ichk+icksum(line2,11,icnt)
  186.     call putnum(ichk,line2,6,5)
  187.  40    write(1,45)line2
  188.  45    format(1X,80a1/)
  189. C
  190. C    Get ack or nak
  191. C
  192.     read(1,20)line
  193.     if(line(2).eq.iN) goto 40
  194.     if(line(2).eq.iX) goto 60
  195.     if(line(2).ne.iA) goto 60
  196.     goto 15
  197.  50    write(1,55)
  198.  55    format(2h E)
  199.     return
  200.  60    write(1,65)
  201.  65    format(2h X)
  202.     return
  203.     end
  204. C
  205. C
  206. C
  207.     subroutine putnum(num,ibuf,ist,ilen)
  208.     integer*4 ipwr,num,itemp
  209.     byte ibuf(150)
  210.     data izero/48/
  211.     iptr=ist
  212.     itemp=num
  213.     ipwr=1
  214.     j=ilen-1
  215.     do 5 i=1,j
  216.  5    ipwr=ipwr*10
  217.  10    j=itemp/ipwr
  218.     ibuf(iptr)=j+izero
  219.     itemp=itemp-j*ipwr
  220.     ipwr=ipwr/10
  221.     iptr=iptr+1
  222.     if(iptr.lt.ist+ilen)goto 10
  223.     return
  224.     end
  225. C
  226. C
  227. C
  228.     integer*4 function icksum(ibuf,ist,len)
  229.     byte ibuf(150)
  230.     integer*4 k,maxint
  231. C
  232. C    This function computes a checksum as described above.
  233. C
  234.     maxint=2**16-1
  235.     k=0
  236.     j=ist+len-1
  237.     do 10 i=ist,j
  238.     k=k+ibuf(i)
  239.     if(k.gt.maxint)k=k-maxint-1
  240.  10    continue
  241.     icksum=k
  242.     return
  243.     end
  244. C
  245. C
  246. C
  247.     integer*4 function iconv(ibuf,ist,len)
  248.     byte ibuf(150)
  249.     data izero/48/  iblank/32/
  250.     ipw=10**(len-1)
  251.     k=0
  252.     j=ist+len-1
  253.     do 10 i=ist,j
  254.     ich=ibuf(i)
  255. C
  256. C    Convert blanks to zeros
  257. C
  258.     if(ich.eq.iblank)ich=izero
  259.     n=ich-izero
  260.     if((n.lt.0).or.(n.gt.9)) goto 99
  261.     k=k+ipw*n
  262.     ipw=ipw/10
  263.  10    continue
  264.     iconv=k
  265.     return
  266.  99    iconv=-1
  267.     return
  268.     end
  269.