File:  [gforth] / gforth / kernel / files.fs
Revision 1.28: download - view: text, annotated - select for diffs
Mon Dec 31 15:25:19 2012 UTC (8 years, 11 months ago) by anton
Branches: MAIN
CVS tags: HEAD
updated copyright year

    1: \ File specifiers                                       11jun93jaw
    2: 
    3: \ Copyright (C) 1995,1996,1997,1998,2000,2003,2006,2007,2012 Free Software Foundation, Inc.
    4: 
    5: \ This file is part of Gforth.
    6: 
    7: \ Gforth is free software; you can redistribute it and/or
    8: \ modify it under the terms of the GNU General Public License
    9: \ as published by the Free Software Foundation, either version 3
   10: \ of the License, or (at your option) any later version.
   11: 
   12: \ This program is distributed in the hope that it will be useful,
   13: \ but WITHOUT ANY WARRANTY; without even the implied warranty of
   14: \ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   15: \ GNU General Public License for more details.
   16: 
   17: \ You should have received a copy of the GNU General Public License
   18: \ along with this program. If not, see http://www.gnu.org/licenses/.
   19: 
   20: 4 Constant w/o ( -- fam ) \ file	w-o
   21: 2 Constant r/w ( -- fam ) \ file	r-w
   22: 0 Constant r/o ( -- fam ) \ file	r-o
   23: 
   24: : bin ( fam1 -- fam2 ) \ file
   25:     1 or ;
   26: 
   27: \ BIN WRITE-LINE                                        11jun93jaw
   28: 
   29: : write-line ( c-addr u wfileid -- ior ) \ file
   30:     dup >r write-file
   31:     ?dup IF
   32: 	r> drop EXIT
   33:     THEN
   34:     newline r> write-file ;
   35: 
   36: \ include-file                                         07apr93py
   37: 
   38: has? new-input 0= [IF]
   39: : loadfilename>r ( addr1 u1 -- R: addr2 u2 )
   40:     r> loadfilename 2@ 2>r >r
   41:     loadfilename 2! ;
   42: 
   43: : r>loadfilename ( R: addr u -- )
   44:     r> 2r> loadfilename 2! >r ;
   45: 
   46: : push-file  ( -- )  r>
   47:     #fill-bytes @ >r
   48:     loadline @    >r
   49:     loadfile @    >r
   50:     blk @         >r
   51:     tibstack @    >r
   52:     >tib @        >r
   53:     #tib @        >r
   54:     >in @         >r  >r
   55:     >tib @ tibstack @ = IF  #tib @ tibstack +!  THEN
   56:     tibstack @ >tib ! ;
   57: 
   58: : pop-file   ( throw-code -- throw-code )
   59:   dup IF
   60:       input-error-data >error
   61:   THEN
   62:   r>
   63:   r> >in         !
   64:   r> #tib        !
   65:   r> >tib        !
   66:   r> tibstack    !
   67:   r> blk         !
   68:   r> loadfile    !
   69:   r> loadline    !
   70:   r> #fill-bytes !  >r ;
   71: 
   72: : read-loop ( i*x -- j*x )
   73:   BEGIN  refill  WHILE  interpret  REPEAT ;
   74: 
   75: : include-file1 ( i*x wfileid -- j*x ior1 ior2 )
   76:     \G Interpret (process using the text interpreter) the contents of
   77:     \G the file @var{wfileid}.
   78:     push-file  loadfile !
   79:     0 loadline ! blk off  ['] read-loop catch
   80:     loadfile @ close-file swap 2dup or
   81:     pop-file  drop ;
   82: 
   83: : include-file2 ( i*x wfileid -- j*x )
   84:     \ like include-file, but does not update loadfile#
   85:     include-file1 throw throw ;
   86: 
   87: : include-file ( i*x wfileid -- j*x ) \ file
   88:     s" *a file*" loadfilename>r
   89:     include-file1
   90:     r>loadfilename
   91:     throw throw ;
   92: [THEN]
   93:     
   94: \ additional words only needed if there is file support
   95: 
   96: Redefinitions-start
   97: 
   98: : ( ( compilation 'ccc<close-paren>' -- ; run-time -- ) \ core,file	paren
   99:     loadfile @ 0= IF  postpone (  EXIT  THEN
  100:     BEGIN
  101: 	>in @
  102: 	[char] ) parse nip
  103: 	>in @ rot - = \ is there no delimter?
  104:     WHILE
  105: 	refill 0=
  106: 	IF
  107: 	    warnings @
  108: 	    IF
  109: 		." warning: ')' missing" cr
  110: 	    THEN
  111: 	    EXIT
  112: 	THEN
  113:     REPEAT ; immediate
  114: 
  115: Redefinitions-end

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>