File:  [gforth] / gforth / kernel / require.fs
Revision 1.17: download - view: text, annotated - select for diffs
Sat Sep 23 15:06:10 2000 UTC (21 years, 1 month ago) by anton
Branches: MAIN
CVS tags: HEAD
updated copyright dates in many files (not in ec-related files)

    1: \ require.fs
    2: 
    3: \ Copyright (C) 1995,1996,1997,1998,2000 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 2
   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, write to the Free Software
   19: \ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
   20: 
   21: \ Now: Kernel Module, Reloadable
   22: 
   23: create included-files 0 , 0 , ( pointer to and count of included files )
   24: \ note: these names must not contain a "/" or "\"; otherwise a part of
   25: \ that name might be used when expanding "./" (see expandtopic).
   26: here ," *a file*" dup c@ swap 1 + swap
   27: here ," *a block*" dup c@ swap 1 + swap
   28: here ," *evaluated string*" dup c@ swap 1 + swap
   29: here ," *the terminal*" dup c@ swap 1 + swap
   30: , A, , A, , A, , A, here 8 cells -
   31: create image-included-files 4 , A, ( pointer to and count of included files )
   32: \ included-files points to ALLOCATEd space, while image-included-files
   33: \ points to ALLOTed objects, so it survives a save-system
   34: 
   35: : sourcefilename ( -- c-addr u ) \ gforth
   36:     \G The name of the source file which is currently the input
   37:     \G source.  The result is valid only while the file is being
   38:     \G loaded.  If the current input source is no (stream) file, the
   39:     \G result is undefined.  In Gforth, the result is valid during the
   40:     \G whole seesion (but not across @code{savesystem} etc.).
   41:     loadfilename# @ loadfilename#>str ;
   42: 
   43: : sourceline# ( -- u ) \ gforth		sourceline-number
   44:     \G The line number of the line that is currently being interpreted
   45:     \G from a (stream) file. The first line has the number 1. If the
   46:     \G current input source is not a (stream) file, the result is
   47:     \G undefined.
   48:     loadline @ ;
   49: 
   50: : init-included-files ( -- ) \ gforth-internal
   51:     image-included-files 2@ 2* cells save-mem drop ( addr )
   52:     image-included-files 2@ nip included-files 2! ;
   53: 
   54: : included? ( c-addr u -- f ) \ gforth
   55:     \G True only if the file @var{c-addr u} is in the list of earlier
   56:     \G included files. If the file has been loaded, it may have been
   57:     \G specified as, say, @file{foo.fs} and found somewhere on the
   58:     \G Forth search path. To return @code{true} from @code{included?},
   59:     \G you must specify the exact path to the file, even if that is
   60:     \G @file{./foo.fs}
   61:     included-files 2@ 0
   62:     ?do ( c-addr u addr )
   63: 	dup >r 2@ 2over compare 0=
   64: 	if
   65: 	    2drop rdrop unloop
   66: 	    true EXIT
   67: 	then
   68: 	r> cell+ cell+
   69:     loop
   70:     2drop drop false ;
   71: 
   72: : add-included-file ( c-addr u -- ) \ gforth
   73:     \G add name c-addr u to included-files
   74:     included-files 2@ 2* cells 2 cells extend-mem
   75:     2/ cell / included-files 2!
   76:     2! ;
   77: 
   78: : included1 ( i*x file-id c-addr u -- j*x ) \ gforth
   79:     \G Include the file file-id with the name given by @var{c-addr u}.
   80:     loadfilename# @ >r
   81:     save-mem add-included-file ( file-id )
   82:     included-files 2@ nip 1- loadfilename# !
   83:     ['] include-file2 catch
   84:     r> loadfilename# !
   85:     throw ;
   86:     
   87: : included ( i*x c-addr u -- j*x ) \ file
   88:     \G @code{include-file} the file whose name is given by the string
   89:     \G @var{c-addr u}.
   90:     open-fpath-file throw included1 ;
   91: 
   92: : required ( i*x addr u -- j*x ) \ gforth
   93:     \G @code{include-file} the file with the name given by @var{addr
   94:     \G u}, if it is not @code{included} (or @code{required})
   95:     \G already. Currently this works by comparing the name of the file
   96:     \G (with path) against the names of earlier included files.
   97:     \ however, it may be better to fstat the file,
   98:     \ and compare the device and inode. The advantages would be: no
   99:     \ problems with several paths to the same file (e.g., due to
  100:     \ links) and we would catch files included with include-file and
  101:     \ write a require-file.
  102:     open-fpath-file throw 2dup included?
  103:     if
  104: 	2drop close-file throw
  105:     else
  106: 	included1
  107:     then ;
  108: 
  109: \ INCLUDE                                               9may93jaw
  110: 
  111: : include  ( ... "file" -- ... ) \ gforth
  112:     \G @code{include-file} the file @var{file}.
  113:     name included ;
  114: 
  115: : require  ( ... "file" -- ... ) \ gforth
  116:     \G @code{include-file} @var{file} only if it is not included already.
  117:     name required ;
  118: 
  119: \ : \I
  120: \   here 
  121: \   0 word count
  122: \   string,
  123: \   needsrcs^ @ ! ;
  124: 
  125: \ : .included ( -- ) \ gforth
  126: \     \G list the names of the files that have been @code{included}
  127: \   cr
  128: \   needs^ @
  129: \   BEGIN		dup 
  130: \   WHILE		dup cell+ count type cr
  131: \ 		5 spaces
  132: \ 		dup cell+ count + aligned
  133: \ 		@ dup IF count type ELSE drop THEN cr
  134: \ 		@
  135: \   REPEAT
  136: \   drop ;
  137: 
  138: \ : loadfilename#>str ( n -- adr len )
  139: \ \ this converts the filenumber into the string
  140: \   loadfilenamecount @ swap -
  141: \   needs^ @
  142: \   swap 0 ?DO dup 0= IF LEAVE THEN @ LOOP 
  143: \   dup IF cell+ count ELSE drop s" NOT FOUND" THEN ;
  144: 
  145: : loadfilename#>str ( n -- adr len )
  146:     included-files 2@ drop swap 2* cells + 2@ ;
  147: 
  148: : .strings ( addr u -- ) \ gforth
  149:     \G list the strings from an array of string descriptors at addr
  150:     \G with u entries, one per line.
  151:     2* cells bounds ?DO
  152: 	cr I 2@ type 2 cells +LOOP ;
  153: 
  154: : .included ( -- ) \ gforth
  155:     \G list the names of the files that have been @code{included}
  156:     included-files 2@ .strings ;
  157:     
  158: \ contains tools/newrequire.fs
  159: \ \I $Id: require.fs,v 1.17 2000/09/23 15:06:10 anton Exp $
  160: 

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