home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / forth / compiler / fpc / source / tstdio.seq < prev    next >
Text File  |  1990-02-12  |  5KB  |  128 lines

  1. \\ TSTDIO.SEQ           Target Compiler Standard I/O    by Tom Zimmer
  2.  
  3.   Redirectable I/O      * STDIO *
  4.  
  5.   GETCHAR can be told to get its input from a pipe, or redirected input
  6.   with either the "|" or "<" symbols at the DOS command line. These two
  7.   symbols are used to redirect the STDIN handle to a file from the normal
  8.   keyboard. STDIN is normally buffered, that is it reads a line at a time,
  9.   then returns one character at a time till all characters of the line
  10.   have been passed out. If you are using GETCHAR for its redirectability,
  11.   and you want to use keyboard input as well, then you will probably want
  12.   to tell DOS to use "raw" or "uncooked" I/O processing. This can be done
  13.   with the RAW_I/O word at the beginning of your program to tell DOS to
  14.   read characters one at a time, rather than a line at a time.
  15.  
  16.  
  17. {
  18.  
  19. FORTH DECIMAL TARGET >LIBRARY       \ A library file
  20.  
  21. ICODE RAW_STDIN ( -- )          \ make DOS read STDIN one char    * STDIO *
  22.                                 \ at a time rather than a line at a time.
  23.                 MOV AX, # $4400
  24.                 MOV BX, # $00
  25.                 INT $21         \ get device information for STDIN
  26.                 MOV DH, # 0
  27.                 OR  DL, # $20   \ set RAW bit in control byte
  28.                 MOV AX, # $4401 \ set device information for STDIN
  29.                 INT $21
  30.                 RET             END-ICODE
  31.  
  32. ICODE GETCHAR   ( -- c1 )       \ c1 = char read from stdin       * STDIO *
  33.                                 \ return -1 if at end of file
  34.                 [ASSEMBLER]
  35.                 DEC SI
  36.                 DEC SI
  37.                 MOV 0 [SI], BX
  38.                 MOV CX, # 1             \ read one character
  39.                 MOV AH, # $3F           \ with a function $3F
  40.                 DEC SI
  41.                 DEC SI
  42.                 MOV DX, SI              \ room for char to be read
  43.                 MOV BX, # 0             \ from handle 0, stdin
  44.                 INT $21
  45.              U< IF      LODSW                   \ if CF true
  46.                         MOV BX, # -1            \ then return -1
  47.                 ELSE    CMP AX, # 0
  48.                      0= IF      LODSW           \ if read no characters
  49.                                 MOV BX, # -1    \ then return -1
  50.                         ELSE    LODSW           \ otherwise
  51.                                 MOV BX, AX      \ return AL on stack, in BX
  52.                                 SUB BH, BH
  53.                         THEN
  54.                 THEN
  55.                 RET             END-ICODE
  56.  
  57. : STDIN_INIT    ( -- )          \ initialize input to use STDIN   * STDIO *
  58.                 RAW_STDIN
  59.                 ['] GETCHAR !> KEY ( !> KEY-F ) ;
  60.  
  61. \ ***************************************************************************
  62. \ un-buffered character output to STDIO.
  63.  
  64. ICODE PUTCHAR   ( c1 -- )       \ put one char to STDOUT          * STDIO *
  65.                 MOV DX, BX
  66.                 MOV AH, # 06
  67.                 INT $21
  68.                 LOAD_BX
  69.                 RET             END-ICODE
  70.  
  71. \ ***************************************************************************
  72. \ Buffered character input. Use BUFIO_INIT before using GETCHAR_B OR
  73. \ PUTCHAR_B, it initializes values, and allocated needed buffer space.
  74.  
  75. \ Performance is significantly affected by the size of the read and write
  76. \ buffers. The default size works well, but a considerable improvement
  77. \ in throughput can be obtained by increasing GCH.MAX and or PCH.MAX to
  78. \ around 2000.  This will of course use more DS: memory.
  79.  
  80. \ If you are using PUTCHAR_B, be sure to use FLUSH_B before leaving to
  81. \ return to DOS, it will flush any extra characters in the write buffer
  82. \ to disk.
  83.  
  84. 32 VALUE GCH.MAX       32 VALUE PCH.MAX
  85.  0 VALUE GCH.OFF        0 VALUE GCH.LEN         0 VALUE GCH.BUF
  86.  0 VALUE PCH.OFF                                0 VALUE PCH.BUF
  87.   HANDLE GCH.HNDL        HANDLE PCH.HNDL
  88.  
  89. : BUFIO_INIT    ( -- )          \ Init buffered character I/O
  90.                 0 GCH.HNDL >HNDLE !
  91.                 1 PCH.HNDL >HNDLE !
  92.                 OFF> GCH.LEN    OFF> GCH.OFF
  93.                                 OFF> PCH.OFF
  94.                 GCH.MAX 1+ DS:ALLOC =: GCH.BUF   \ allocate at runtime
  95.                 PCH.MAX 1+ DS:ALLOC =: PCH.BUF ; \ allocate at runtime
  96.  
  97. : GETCHAR_B     ( -- c1 )       \ c1 = char read from stdin buffered   STDIO
  98.                                 \ return -1 if at end of file
  99.                 GCH.OFF GCH.LEN =
  100.                 IF      GCH.BUF GCH.MAX GCH.HNDL HREAD =: GCH.LEN
  101.                         OFF> GCH.OFF
  102.                 THEN    GCH.LEN
  103.                 IF      GCH.BUF GCH.OFF + C@    \ return a character
  104.                         INCR> GCH.OFF
  105.                 ELSE    -1                      \ return -1 if no characters
  106.                 THEN    ;
  107.  
  108.  
  109.  
  110.  
  111. : FLUSH_B       ( -- )
  112.                 PCH.OFF ?DUP
  113.                 IF      PCH.BUF SWAP PCH.HNDL HWRITE DROP
  114.                         OFF> PCH.OFF
  115.                 THEN    ;
  116.  
  117. : PUTCHAR_B     ( c1 -- )       \ buffered write of a character to STDIO
  118.                 PCH.BUF PCH.OFF + C!
  119.                 INCR> PCH.OFF
  120.                 PCH.OFF PCH.MAX >=
  121.                 IF      FLUSH_B
  122.                 THEN    ;
  123.  
  124.  
  125. FORTH DECIMAL TARGET >TARGET
  126.  
  127. }
  128.