Annotation of gforth/kernel/paths.fs, revision 1.30

1.1       anton       1: \ paths.fs path file handling                                    03may97jaw
                      2: 
1.30    ! anton       3: \ Copyright (C) 1995,1996,1997,1998,2000,2003,2004,2005,2006 Free Software Foundation, Inc.
1.1       anton       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
1.17      anton      19: \ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
1.1       anton      20: 
1.5       anton      21: \ -Changing the search-path:
1.12      crook      22: \ fpath+ <path>        adds a directory to the searchpath
1.5       anton      23: \ fpath= <path>|<path> makes complete now searchpath
                     24: \                      seperator is |
1.12      crook      25: \ .fpath               displays the search path
1.5       anton      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:
1.15      anton      40: \ create mypath        100 path,
1.5       anton      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
1.1       anton      48: 
                     49: \ questions to: wilke@jwdt.com
                     50: 
1.26      anton      51: : path-allot ( umax -- ) \ gforth
1.15      anton      52:     \G @code{Allot} a path with @i{umax} characters capacity, initially empty.
                     53:     chars dup , 0 , allot ;
                     54: 
1.1       anton      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: 
1.18      pazsan     67: \ create sourcepath 1024 chars , 0 , 1024 chars allot \ !! make this dynamic
                     68: 0 avalue fpath ( -- path-addr ) \ gforth
                     69: 
                     70: : os-cold ( -- )
1.24      anton      71:     $400 chars dup 2 cells + allocate throw to fpath
1.18      pazsan     72:     0 swap fpath 2!
                     73:     pathstring 2@ fpath only-path 
                     74:     init-included-files ;
                     75: 
1.15      anton      76: \ The path Gforth uses for @code{included} and friends.
1.1       anton      77: 
1.15      anton      78: : also-path ( c-addr len path-addr -- ) \ gforth
                     79:     \G add the directory @i{c-addr len} to @i{path-addr}.
1.1       anton      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
1.12      crook      85:   \ make delimiter
1.1       anton      86:   0 r@ cell+ dup @ cell+ + 2 pick + c! 1 + r> cell+ +!
1.15      anton      87: ;
                     88: 
                     89: : clear-path ( path-addr -- ) \ gforth
                     90:     \G Set the path @i{path-addr} to empty.
                     91:     0 swap cell+ ! ;
1.1       anton      92: 
                     93: : only-path ( adr len path^ -- )
1.15      anton      94:     dup clear-path also-path ;
1.1       anton      95: 
1.2       jwilke     96: : path+ ( path-addr  "dir" -- ) \ gforth
1.12      crook      97:     \G Add the directory @var{dir} to the search path @var{path-addr}.
1.9       crook      98:     name rot also-path ;
1.2       jwilke     99: 
                    100: : fpath+ ( "dir" ) \ gforth
1.12      crook     101:     \G Add directory @var{dir} to the Forth search path.
1.9       crook     102:     fpath path+ ;
1.2       jwilke    103: 
                    104: : path= ( path-addr "dir1|dir2|dir3" ) \ gforth
1.12      crook     105:     \G Make a complete new search path; the path separator is |.
1.9       crook     106:     name 2dup bounds ?DO i c@ '| = IF 0 i c! THEN LOOP
                    107:     rot only-path ;
1.2       jwilke    108: 
                    109: : fpath= ( "dir1|dir2|dir3" ) \ gforth
1.12      crook     110:     \G Make a complete new Forth search path; the path separator is |.
1.9       crook     111:     fpath path= ;
1.1       anton     112: 
1.24      anton     113: : path>string ( path -- c-addr u )
                    114:     \ string contains NULs to separate/terminate components
                    115:     cell+ dup cell+ swap @ ;
                    116: 
                    117: : next-path ( addr u -- addr1 u1 addr2 u2 )
                    118:     \ addr2 u2 is the first component of the path, addr1 u1 is the rest
                    119:     2dup 0 scan
                    120:     dup 0= IF     2drop 0 -rot 0 -rot EXIT THEN
                    121:     >r 1+ -rot r@ 1- -rot
                    122:     r> - ;
1.1       anton     123: 
1.7       pazsan    124: : previous-path ( path^ -- )
1.15      anton     125:     \ !! "fpath previous-path" doesn't work
1.24      anton     126:   dup path>string
1.1       anton     127:   BEGIN tuck dup WHILE repeat ;
                    128: 
1.2       jwilke    129: : .path ( path-addr -- ) \ gforth
1.12      crook     130:     \G Display the contents of the search path @var{path-addr}.
1.24      anton     131:     path>string
1.9       crook     132:     BEGIN next-path dup WHILE type space REPEAT 2drop 2drop ;
1.1       anton     133: 
1.5       anton     134: : .fpath ( -- ) \ gforth
1.12      crook     135:     \G Display the contents of the Forth search path.
1.9       crook     136:     fpath .path ;
1.1       anton     137: 
                    138: : absolut-path? ( addr u -- flag ) \ gforth
1.9       crook     139:     \G A path is absolute if it starts with a / or a ~ (~ expansion),
1.5       anton     140:     \G or if it is in the form ./*, extended regexp: ^[/~]|./, or if
                    141:     \G it has a colon as second character ("C:...").  Paths simply
                    142:     \G containing a / are not absolute!
1.1       anton     143:     2dup 2 u> swap 1+ c@ ': = and >r \ dos absoulte: c:/....
                    144:     over c@ '/ = >r
                    145:     over c@ '~ = >r
1.19      anton     146:     \ 2dup S" ../" string-prefix? r> or >r \ not catered for in expandtopic
                    147:     S" ./" string-prefix?
1.5       anton     148:     r> r> r> or or or ;
1.1       anton     149: 
                    150: Create ofile 0 c, 255 chars allot
                    151: Create tfile 0 c, 255 chars allot
                    152: 
                    153: : pathsep? dup [char] / = swap [char] \ = or ;
                    154: 
                    155: : need/   ofile dup c@ + c@ pathsep? 0= IF s" /" ofile +place THEN ;
                    156: 
1.2       jwilke    157: : extractpath ( adr len -- adr len2 )
                    158:   BEGIN dup WHILE 1-
                    159:         2dup + c@ pathsep? IF EXIT THEN
                    160:   REPEAT ;
1.1       anton     161: 
1.4       pazsan    162: : remove~+ ( -- )
1.19      anton     163:     ofile count s" ~+/" string-prefix?
1.4       pazsan    164:     IF
                    165:        ofile count 3 /string ofile place
                    166:     THEN ;
                    167: 
1.5       anton     168: : expandtopic ( -- ) \ stack effect correct? - anton
                    169:     \ expands "./" into an absolute name
1.19      anton     170:     ofile count s" ./" string-prefix?
1.5       anton     171:     IF
                    172:        ofile count 1 /string tfile place
1.29      anton     173:        0 ofile c! includefilename 2@ extractpath ofile place
1.13      jwilke    174:        \ care of / only if there is a directory
                    175:        ofile c@ IF need/ THEN
1.3       pazsan    176:        tfile count over c@ pathsep? IF 1 /string THEN
                    177:        ofile +place
1.5       anton     178:     THEN ;
1.3       pazsan    179: 
1.24      anton     180: : del-string ( addr u u1 -- addr u2 )
                    181:     \ delete u1 characters from string by moving stuff from further up
                    182:     2 pick >r /string r@ over >r swap cmove 2r> ;
                    183: 
                    184: : del-./s ( addr u -- addr u2 )
                    185:     \ deletes (/*./)* at the start of the string
                    186:     BEGIN ( current-addr u )
                    187:        BEGIN ( current-addr u )
                    188:            over c@ '/ = WHILE
                    189:                1 del-string
                    190:        REPEAT
                    191:        2dup s" ./" string-prefix? WHILE
                    192:            2 del-string
                    193:     REPEAT ;
                    194: 
                    195: : preserve-root ( addr1 u1 -- addr2 u2 )
                    196:     over c@ '/ = if \ preserve / at start
                    197:        1 /string
                    198:     endif ;
                    199: 
                    200: 
                    201: : skip-..-prefixes ( addr1 u1 -- addr2 u2 )
                    202:     \ deal with ../ at start
                    203:     begin ( current-addr u )
                    204:        del-./s 2dup s" ../" string-prefix? while
                    205:            3 /string
                    206:     repeat ;
                    207:     
                    208: : compact-filename ( addr u1 -- addr u2 )
                    209:     \ rewrite filename in place, eliminating multiple slashes, "./", and "x/.."
                    210:     over swap preserve-root skip-..-prefixes
                    211:     ( start current-addr u )
                    212:     over swap '/ scan dup if ( start addr3 addr4 u4 )
                    213:        1 /string del-./s recurse
                    214:        2dup s" ../" string-prefix? if ( start addr3 addr4 u4 )
                    215:            3 /string ( start to from count )
                    216:            >r swap 2dup r@ cmove r>
                    217:        endif
                    218:     endif
                    219:     + nip over - ;
                    220: 
                    221: \ test cases:
                    222: \ s" z/../../../a" compact-filename type cr
                    223: \ s" ../z/../../../a/c" compact-filename type cr
                    224: \ s" /././//./../..///x/y/../z/.././..//..//a//b/../c" compact-filename type cr
1.3       pazsan    225: 
1.5       anton     226: : reworkdir ( -- )
1.4       pazsan    227:   remove~+
1.24      anton     228:   ofile count compact-filename
1.3       pazsan    229:   nip ofile c! ;
                    230: 
1.5       anton     231: : open-ofile ( -- fid ior )
1.7       pazsan    232:     \G opens the file whose name is in ofile
1.5       anton     233:     expandtopic reworkdir
                    234:     ofile count r/o open-file ;
                    235: 
1.28      anton     236: : check-path ( adr1 len1 adr2 len2 -- fid 0 | 0 ior )
1.2       jwilke    237:   0 ofile ! >r >r ofile place need/
                    238:   r> r> ofile +place
1.5       anton     239:   open-ofile ;
1.1       anton     240: 
1.15      anton     241: \ !! allow arbitrary FAMs, not just R/O
1.12      crook     242: : open-path-file ( addr1 u1 path-addr -- wfileid addr2 u2 0 | ior ) \ gforth
1.28      anton     243: \G Look in path @var{path-addr} for the file specified by @var{addr1
                    244: \G u1}.  If found, the resulting path and and (read-only) open file
                    245: \G descriptor are returned. If the file is not found, @var{ior} is
                    246: \G what came back from the last attempt at opening the file (in the
                    247: \G current implementation).
                    248:     >r
                    249:     2dup absolut-path? IF
                    250:         rdrop
1.5       anton     251:         ofile place open-ofile
1.28      anton     252:         dup 0= IF
                    253:             >r ofile count r> THEN
                    254:         EXIT
                    255:     ELSE
                    256:         r> -&37 >r path>string BEGIN
                    257:             next-path dup WHILE
                    258:                 r> drop
                    259:                 5 pick 5 pick check-path dup 0= IF
                    260:                     drop >r 2drop 2drop r> ofile count 0 EXIT
                    261:                 ELSE
                    262:                     >r drop
                    263:                 THEN
                    264:         REPEAT
                    265:         2drop 2drop 2drop r>
1.1       anton     266:   THEN ;
                    267: 
1.12      crook     268: : open-fpath-file ( addr1 u1 -- wfileid addr2 u2 0 | ior ) \ gforth
                    269:     \G Look in the Forth search path for the file specified by @var{addr1 u1}.
                    270:     \G If found, the resulting path and an open file descriptor
                    271:     \G are returned. If the file is not found, @var{ior} is non-zero.
1.9       crook     272:     fpath open-path-file ;

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