File:  [gforth] / gforth / kernel / paths.fs
Revision 1.23: download - view: text, annotated - select for diffs
Sun Mar 9 15:17:06 2003 UTC (21 years, 1 month ago) by anton
Branches: MAIN
CVS tags: v0-6-2, v0-6-1, v0-6-0, HEAD
updated copyright years

    1: \ paths.fs path file handling                                    03may97jaw
    2: 
    3: \ Copyright (C) 1995,1996,1997,1998,2000,2003 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., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
   20: 
   21: \ -Changing the search-path:
   22: \ fpath+ <path> 	adds a directory to the searchpath
   23: \ fpath= <path>|<path>	makes complete now searchpath
   24: \ 			seperator is |
   25: \ .fpath		displays the search path
   26: \ remark I: 
   27: \ a ./ in the beginning of filename is expanded to the directory the
   28: \ current file comes from. ./ can also be included in the search-path!
   29: \ ~+/ loads from the current working directory
   30: 
   31: \ remark II:
   32: \ if there is no sufficient space for the search path increase it!
   33: 
   34: 
   35: \ -Creating custom paths:
   36: 
   37: \ It is possible to use the search mechanism on yourself.
   38: 
   39: \ Make a buffer for the path:
   40: \ create mypath	100 path,
   41: \ mypath path+ 
   42: \ mypath path=
   43: \ mypath .path
   44: 
   45: \ do a open with the search path:
   46: \ open-path-file ( adr len path -- fd adr len ior )
   47: \ the file is opened read-only; if the file is not found an error is generated
   48: 
   49: \ questions to: wilke@jwdt.com
   50: 
   51: : path-allot ( umax -- )
   52:     \G @code{Allot} a path with @i{umax} characters capacity, initially empty.
   53:     chars dup , 0 , allot ;
   54: 
   55: [IFUNDEF] +place
   56: : +place ( adr len adr )
   57:         2dup >r >r
   58:         dup c@ char+ + swap move
   59:         r> r> dup c@ rot + swap c! ;
   60: [THEN]
   61: 
   62: [IFUNDEF] place
   63: : place ( c-addr1 u c-addr2 )
   64:         2dup c! char+ swap move ;
   65: [THEN]
   66: 
   67: \ create sourcepath 1024 chars , 0 , 1024 chars allot \ !! make this dynamic
   68: 0 avalue fpath ( -- path-addr ) \ gforth
   69: 
   70: : os-cold ( -- )
   71:     1024 chars dup 2 cells + allocate throw to fpath
   72:     0 swap fpath 2!
   73:     pathstring 2@ fpath only-path 
   74:     init-included-files ;
   75: 
   76: \ The path Gforth uses for @code{included} and friends.
   77: 
   78: : also-path ( c-addr len path-addr -- ) \ gforth
   79:     \G add the directory @i{c-addr len} to @i{path-addr}.
   80:   >r
   81:   \ len check
   82:   r@ cell+ @ over + r@ @ u> ABORT" path buffer too small!"
   83:   \ copy into
   84:   tuck r@ cell+ dup @ cell+ + swap cmove
   85:   \ make delimiter
   86:   0 r@ cell+ dup @ cell+ + 2 pick + c! 1 + r> cell+ +!
   87: ;
   88: 
   89: : clear-path ( path-addr -- ) \ gforth
   90:     \G Set the path @i{path-addr} to empty.
   91:     0 swap cell+ ! ;
   92: 
   93: : only-path ( adr len path^ -- )
   94:     dup clear-path also-path ;
   95: 
   96: : path+ ( path-addr  "dir" -- ) \ gforth
   97:     \G Add the directory @var{dir} to the search path @var{path-addr}.
   98:     name rot also-path ;
   99: 
  100: : fpath+ ( "dir" ) \ gforth
  101:     \G Add directory @var{dir} to the Forth search path.
  102:     fpath path+ ;
  103: 
  104: : path= ( path-addr "dir1|dir2|dir3" ) \ gforth
  105:     \G Make a complete new search path; the path separator is |.
  106:     name 2dup bounds ?DO i c@ '| = IF 0 i c! THEN LOOP
  107:     rot only-path ;
  108: 
  109: : fpath= ( "dir1|dir2|dir3" ) \ gforth
  110:     \G Make a complete new Forth search path; the path separator is |.
  111:     fpath path= ;
  112: 
  113: : path>counted  cell+ dup cell+ swap @ ;
  114: 
  115: : next-path ( adr len -- adr2 len2 )
  116:   2dup 0 scan
  117:   dup 0= IF     2drop 0 -rot 0 -rot EXIT THEN
  118:   >r 1+ -rot r@ 1- -rot
  119:   r> - ;
  120: 
  121: : previous-path ( path^ -- )
  122:     \ !! "fpath previous-path" doesn't work
  123:   dup path>counted
  124:   BEGIN tuck dup WHILE repeat ;
  125: 
  126: : .path ( path-addr -- ) \ gforth
  127:     \G Display the contents of the search path @var{path-addr}.
  128:     path>counted
  129:     BEGIN next-path dup WHILE type space REPEAT 2drop 2drop ;
  130: 
  131: : .fpath ( -- ) \ gforth
  132:     \G Display the contents of the Forth search path.
  133:     fpath .path ;
  134: 
  135: : absolut-path? ( addr u -- flag ) \ gforth
  136:     \G A path is absolute if it starts with a / or a ~ (~ expansion),
  137:     \G or if it is in the form ./*, extended regexp: ^[/~]|./, or if
  138:     \G it has a colon as second character ("C:...").  Paths simply
  139:     \G containing a / are not absolute!
  140:     2dup 2 u> swap 1+ c@ ': = and >r \ dos absoulte: c:/....
  141:     over c@ '/ = >r
  142:     over c@ '~ = >r
  143:     \ 2dup S" ../" string-prefix? r> or >r \ not catered for in expandtopic
  144:     S" ./" string-prefix?
  145:     r> r> r> or or or ;
  146: 
  147: Create ofile 0 c, 255 chars allot
  148: Create tfile 0 c, 255 chars allot
  149: 
  150: : pathsep? dup [char] / = swap [char] \ = or ;
  151: 
  152: : need/   ofile dup c@ + c@ pathsep? 0= IF s" /" ofile +place THEN ;
  153: 
  154: : extractpath ( adr len -- adr len2 )
  155:   BEGIN dup WHILE 1-
  156:         2dup + c@ pathsep? IF EXIT THEN
  157:   REPEAT ;
  158: 
  159: : remove~+ ( -- )
  160:     ofile count s" ~+/" string-prefix?
  161:     IF
  162: 	ofile count 3 /string ofile place
  163:     THEN ;
  164: 
  165: : expandtopic ( -- ) \ stack effect correct? - anton
  166:     \ expands "./" into an absolute name
  167:     ofile count s" ./" string-prefix?
  168:     IF
  169: 	ofile count 1 /string tfile place
  170: 	0 ofile c! sourcefilename extractpath ofile place
  171: 	\ care of / only if there is a directory
  172: 	ofile c@ IF need/ THEN
  173: 	tfile count over c@ pathsep? IF 1 /string THEN
  174: 	ofile +place
  175:     THEN ;
  176: 
  177: : compact.. ( adr len -- adr2 len2 )
  178:     \ deletes phrases like "xy/.." out of our directory name 2dec97jaw
  179:     over swap
  180:     BEGIN  dup  WHILE
  181:         dup >r '/ scan 2dup s" /../" string-prefix?
  182:         IF
  183:             dup r> - >r 4 /string over r> + 4 -
  184:             swap 2dup + >r move dup r> over -
  185:         ELSE
  186:             rdrop dup 1 min /string
  187:         THEN
  188:     REPEAT  drop over - ;
  189: 
  190: : reworkdir ( -- )
  191:   remove~+
  192:   ofile count compact..
  193:   nip ofile c! ;
  194: 
  195: : open-ofile ( -- fid ior )
  196:     \G opens the file whose name is in ofile
  197:     expandtopic reworkdir
  198:     ofile count r/o open-file ;
  199: 
  200: : check-path ( adr1 len1 adr2 len2 -- fd 0 | 0 <>0 )
  201:   0 ofile ! >r >r ofile place need/
  202:   r> r> ofile +place
  203:   open-ofile ;
  204: 
  205: \ !! allow arbitrary FAMs, not just R/O
  206: : open-path-file ( addr1 u1 path-addr -- wfileid addr2 u2 0 | ior ) \ gforth
  207:     \G Look in path @var{path-addr} for the file specified by @var{addr1 u1}.
  208:     \G If found, the resulting path and and (read-only) open file descriptor
  209:     \G are returned. If the file is not found, @var{ior} is non-zero.
  210:   >r
  211:   2dup absolut-path?
  212:   IF    rdrop
  213:         ofile place open-ofile
  214: 	dup 0= IF >r ofile count r> THEN EXIT
  215:   ELSE  r> path>counted
  216:         BEGIN  next-path dup
  217:         WHILE  5 pick 5 pick check-path
  218:         0= IF >r 2drop 2drop r> ofile count 0 EXIT ELSE drop THEN
  219:   REPEAT
  220:         2drop 2drop 2drop -&37
  221:   THEN ;
  222: 
  223: : open-fpath-file ( addr1 u1 -- wfileid addr2 u2 0 | ior ) \ gforth
  224:     \G Look in the Forth search path for the file specified by @var{addr1 u1}.
  225:     \G If found, the resulting path and an open file descriptor
  226:     \G are returned. If the file is not found, @var{ior} is non-zero.
  227:     fpath open-path-file ;

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