home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_200 / 223_02 / fgetc.mac < prev    next >
Text File  |  1989-02-23  |  5KB  |  249 lines

  1. ;
  2. ;
  3. ;    fgetc(unit)    by F. A. Scacchitti    11 - 24 - 84
  4. ;
  5. ;    getc(unit)
  6. ;
  7. ;
  8. CBDOS    EQU    5    ;/* bdos entry point */
  9. CPMARG    EQU    128    ;/* CP/M command line */
  10. MAXARG    EQU    32    ;/* Maximum number of input args */
  11. STDIN    EQU    0
  12. STDOUT    EQU    1
  13. STDERR    EQU    2
  14. STDLST    EQU    4
  15. CTRLZ    EQU    26    ;control z
  16. NULL    EQU    0    ;pointer to nothing
  17. FCBSIZE    EQU    36    ;size, in bytes, of an FCB
  18. NEXTP    EQU    0    ;offset    to next-char. pointer in I/O struct.
  19. UNUSED    EQU    2    ;offset    to unused-pos.-count in I/O struct.
  20. BUFFER    EQU    6    ;offset    to disk    sector buf. in I/O struct.
  21. UNGOT    EQU    5    ;offset to char ungotten by ungetc()
  22. FLAG    EQU    33    ;file-type flag    byte (in unused    part of    FCB)
  23. FREEFLG    EQU    128    ;This I/O structure is available for the taking
  24. EOF    EQU    0FFH    ;# END OF FILE BYTE FOR UNGOTTEN CHAR ID
  25. EOFFLG    EQU    2    ;The end of this file has been hit
  26. WRTFLG    EQU    1    ;This file open    for writing
  27. BUFSIZ    EQU    1024    ;how long the sector buffer is
  28. NBUFS    EQU    8    ;number    of I/O buffers
  29. TBUFF    EQU    128    ;default cpm buffer
  30. LF    EQU    10
  31. EOL    EQU    13
  32. ;
  33. ; CP/M BDOS CALLS 
  34. ;
  35. CLOSE    EQU    16    ;close a file
  36. CPMSTR    EQU    9    ;print '$' delimited string on console
  37. CREATE    EQU    22    ;make a    file
  38. DMA    EQU    26    ;set DMA (I/O address)
  39. DELETE    EQU    19    ;delete    a file
  40. GETCH    EQU    1    ;read character    from console
  41. GETSTR    EQU    10    ;read string from console
  42. LSTOUT    EQU    5    ;write character to list device
  43. OPEN    EQU    15    ;open a    file
  44. PUTCH    EQU    2    ;write character to console
  45. QUERY    EQU    25    ;get logged-in drive id
  46. READ    EQU    20    ;read a    sector
  47. SELECT    EQU    14    ;log-in    a drive
  48. WRITE    EQU    21    ;write a sector
  49. ;
  50. ;    File i/o storage varibles found in ulink()
  51. ;
  52. EXTRN    ZZUNIT
  53. EXTRN    ZZIP
  54. EXTRN    ZZCHP
  55. EXTRN    ZZDP
  56. EXTRN    ZZFILE
  57. EXTRN    ZZMODE
  58. EXTRN    ZZCH
  59. EXTRN    ZZT
  60. EXTRN    ZZFN
  61. EXTRN    ZZNUBU
  62. EXTRN    ZZMXSC
  63. ;
  64. ;    Characteristics variable storage found in ulink()    
  65. ;
  66. EXTRN    ZZSVCH
  67. EXTRN    ZZDFLT
  68. EXTRN    ZZSTAK
  69. EXTRN    ZZMEM
  70. ;
  71. EXTRN    GETCHAR
  72. EXTRN    CPMIO
  73. ;
  74. FGETC::
  75. GETC::
  76.     POP    B
  77.     POP    H            ; get args
  78.     PUSH    H
  79.     PUSH    B
  80.                     ; c=cget(unit);
  81.     PUSH    H
  82.     CALL    CGET
  83.     POP    D
  84.     MOV    A,L            ; if(c=='\r')    
  85.     ANI    7FH            ; /* mask parity in compare */
  86.     CPI    EOL
  87.     JNZ    GETCRET
  88.     PUSH    H            ;    cget(unit);
  89.     PUSH    D            ;    /* to skip LF */
  90.     CALL    CGET
  91.     MOV    A,L
  92.     ANI    7FH
  93.     CPI    0AH            ; and only skip linefeeds
  94.     JZ    GETCNLF
  95.     LHLD    ZZUNIT
  96.     LXI    D,FCBSIZE+UNGOT
  97.     DAD    D
  98.     MOV    M,A            ; if not LF, next cgets it
  99. GETCNLF:
  100.     POP    H
  101.     POP    H
  102. GETCRET:
  103.     RET
  104. ;
  105. ;    cget(unit)
  106. ;
  107. CGET::
  108.     POP    D
  109.     POP    H
  110.     PUSH    H
  111.     PUSH    D
  112.     MOV    A,H
  113.     ORA    A            ; if(unit < 256) {
  114.     JNZ    CGET1            ;    /* assume stdin    */
  115.     CALL    GETCHAR            ;    getchar();
  116.     POP    D            ;/* return to caller of getc()
  117.     POP    D            ;    to bypass CR check */
  118.     RET                ;    return;    }
  119. CGET1:    SHLD    ZZUNIT
  120.     LXI    D,FLAG            ; if(unit[FLAG]    & EOF_FL)
  121.     DAD    D
  122.     MOV    A,M
  123.     ANI    EOFFLG
  124.     JZ    GTCIF1
  125.     LXI    H,-1            ;    return(-1);
  126.     RET
  127. GTCIF1:
  128.     LHLD    ZZUNIT            ; ip = unit + FCBSIZE;
  129.     LXI    D,FCBSIZE
  130.     DAD    D
  131.     SHLD    ZZIP
  132. ;
  133.     LXI    D,UNGOT            ;# CHECK FOR UNGOTTEN CHAR.
  134.     DAD    D
  135.     MOV    A,M
  136.     CPI    EOF            ;# IS IT EOF ?
  137.     JZ    GTCCON            ;# YES-CONTINUE WITH FGETC
  138.     MOV    B,A            ;# NO-LET'S GET THE UNGOTTEN
  139.     MVI    A,EOF
  140.     MOV    M,A            ;# PUT IN EOF TO MARK IT CLEAR
  141.     MOV    L,B
  142.     MOV    H,0
  143.     RET                ;# RETURN WITH CHAR IN HL
  144. ;
  145. GTCCON:    LHLD    ZZIP
  146.     LXI    D,NEXTP            ; cp = ip[NEXTP];
  147.     DAD    D
  148.     MOV    E,M
  149.     INX    H
  150.     MOV    D,M
  151.     XCHG
  152.     SHLD    ZZCHP
  153.     LHLD    ZZIP            ; if(ip[UNUSED]==0){
  154.     LXI    D,UNUSED
  155.     DAD    D
  156.     MOV    A,M
  157.     INX    H
  158.     ORA    M
  159.     JNZ    GTCIF2
  160. ;
  161. ; Mark beginning of each 128 byte segment of buffer with EOF to
  162. ; eliminate possible bogus read
  163. ;
  164.     LHLD    ZZUNIT
  165.     LXI    D,FCBSIZE + 4        ;    UNIT + FCBSIZE
  166.     DAD    D
  167.     LXI    D,128
  168.     MVI    B,8
  169.     MVI    A,1AH
  170. SETEOF:
  171.     MOV    M,A
  172.     DAD    D
  173.     DCR    B
  174.     JNZ    SETEOF
  175. ;
  176.     LXI    H,READ            ;if(cpmio(READ,unit)~=0)
  177.     PUSH    H
  178.     LHLD    ZZUNIT
  179.     PUSH    H
  180.     CALL    CPMIO
  181.     POP    D
  182.     POP    D
  183.     MOV    A,H
  184.     ORA    L
  185.     JZ    GTCIF3
  186.     LDA    ZZMXSC            ;# IS THIS THE FIRST READ
  187.     CPI    8            ;#
  188.     JNZ    GTCIF3            ;#NO,THERE'S CHARS IN THAT BUF.
  189.                     ;# YES, GETOUTAHERE
  190.     LXI    H,-1            ;        return(-1);
  191.     RET
  192. GTCIF3:
  193.     LHLD    ZZIP            ;else {    ip[UNUSED] = BUFSIZ;
  194.     LXI    D,UNUSED
  195.     DAD    D
  196.     LXI    D,BUFSIZ
  197.     MOV    M,E
  198.     INX    H
  199.     MOV    M,D
  200.     LHLD    ZZIP            ;    cp = &ip[BUFFER];
  201.     LXI    D,BUFFER
  202.     DAD    D
  203.     SHLD    ZZCHP
  204.                     ;    }
  205.                     ;}
  206. GTCIF2:
  207.     LHLD    ZZIP            ; ip[UNUSED]--;
  208.     LXI    D,UNUSED
  209.     DAD    D
  210.     MOV    E,M
  211.     INX    H
  212.     MOV    D,M
  213.     DCX    D
  214.     MOV    M,D
  215.     DCX    H
  216.     MOV    M,E
  217.     LHLD    ZZCHP            ; ip[NEXTP] = cp+1;
  218.     INX    H
  219.     XCHG
  220.     LHLD    ZZIP
  221.     LXI    B,NEXTP
  222.     DAD    B
  223.     MOV    M,E
  224.     INX    H
  225.     MOV    M,D
  226.     LHLD    ZZCHP            ; if(*cp==CTRL_Z){
  227.     MOV    A,M
  228.     ANI    7FH            ; /* mask parity  */
  229.     CPI    CTRLZ
  230.     JNZ    GTCIF4
  231.     LHLD    ZZUNIT            ;unit[FLAG] |= EOF_FL;
  232.     LXI    D,FLAG
  233.     DAD    D
  234.     MOV    A,M
  235.     ORI    EOFFLG
  236.     MOV    M,A
  237.     LXI    H,-1            ;    return(-1);
  238.     RET
  239.                     ;    }
  240. GTCIF4:
  241.     MOV    A,M
  242.     MOV    L,A            ; return(*cp & 0377);
  243.     MVI    H,0
  244.     RET
  245.  
  246.     END
  247.  
  248.  
  249.