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

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

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