Annotation of gforth/kernel/paths.fs, revision 1.37
1.1 anton 1: \ paths.fs path file handling 03may97jaw
2:
1.37 ! anton 3: \ Copyright (C) 1995,1996,1997,1998,2000,2003,2004,2005,2006,2007,2008,2010 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.35 pazsan 50: include string.fs
1.15 anton 51:
1.1 anton 52: [IFUNDEF] +place
53: : +place ( adr len adr )
54: 2dup >r >r
55: dup c@ char+ + swap move
56: r> r> dup c@ rot + swap c! ;
57: [THEN]
58:
59: [IFUNDEF] place
60: : place ( c-addr1 u c-addr2 )
61: 2dup c! char+ swap move ;
62: [THEN]
63:
1.35 pazsan 64: Variable fpath ( -- path-addr ) \ gforth
65: Variable ofile
66: Variable tfile
1.18 pazsan 67:
68: : os-cold ( -- )
1.36 pazsan 69: fpath $init
70: ofile $init
71: tfile $init
1.18 pazsan 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.35 pazsan 79: >r
80: r@ $@len IF \ add separator if necessary
81: s" |" r@ $+! 0 r@ $@ + 1- c!
82: THEN
83: r> $+! ;
1.15 anton 84:
85: : clear-path ( path-addr -- ) \ gforth
86: \G Set the path @i{path-addr} to empty.
1.35 pazsan 87: s" " rot $! ;
1.1 anton 88:
1.35 pazsan 89: : only-path ( adr len path -- )
1.15 anton 90: dup clear-path also-path ;
1.1 anton 91:
1.2 jwilke 92: : path+ ( path-addr "dir" -- ) \ gforth
1.12 crook 93: \G Add the directory @var{dir} to the search path @var{path-addr}.
1.9 crook 94: name rot also-path ;
1.2 jwilke 95:
96: : fpath+ ( "dir" ) \ gforth
1.12 crook 97: \G Add directory @var{dir} to the Forth search path.
1.9 crook 98: fpath path+ ;
1.2 jwilke 99:
100: : path= ( path-addr "dir1|dir2|dir3" ) \ gforth
1.12 crook 101: \G Make a complete new search path; the path separator is |.
1.9 crook 102: name 2dup bounds ?DO i c@ '| = IF 0 i c! THEN LOOP
103: rot only-path ;
1.2 jwilke 104:
105: : fpath= ( "dir1|dir2|dir3" ) \ gforth
1.12 crook 106: \G Make a complete new Forth search path; the path separator is |.
1.9 crook 107: fpath path= ;
1.1 anton 108:
1.24 anton 109: : path>string ( path -- c-addr u )
110: \ string contains NULs to separate/terminate components
1.35 pazsan 111: $@ ;
1.24 anton 112:
113: : next-path ( addr u -- addr1 u1 addr2 u2 )
114: \ addr2 u2 is the first component of the path, addr1 u1 is the rest
1.35 pazsan 115: 0 $split 2swap ;
1.1 anton 116:
1.2 jwilke 117: : .path ( path-addr -- ) \ gforth
1.12 crook 118: \G Display the contents of the search path @var{path-addr}.
1.24 anton 119: path>string
1.9 crook 120: BEGIN next-path dup WHILE type space REPEAT 2drop 2drop ;
1.1 anton 121:
1.5 anton 122: : .fpath ( -- ) \ gforth
1.12 crook 123: \G Display the contents of the Forth search path.
1.9 crook 124: fpath .path ;
1.1 anton 125:
126: : absolut-path? ( addr u -- flag ) \ gforth
1.9 crook 127: \G A path is absolute if it starts with a / or a ~ (~ expansion),
1.5 anton 128: \G or if it is in the form ./*, extended regexp: ^[/~]|./, or if
129: \G it has a colon as second character ("C:..."). Paths simply
130: \G containing a / are not absolute!
1.1 anton 131: 2dup 2 u> swap 1+ c@ ': = and >r \ dos absoulte: c:/....
132: over c@ '/ = >r
133: over c@ '~ = >r
1.19 anton 134: \ 2dup S" ../" string-prefix? r> or >r \ not catered for in expandtopic
135: S" ./" string-prefix?
1.5 anton 136: r> r> r> or or or ;
1.1 anton 137:
138: : pathsep? dup [char] / = swap [char] \ = or ;
139:
1.35 pazsan 140: : need/ ofile $@ 1- + c@ pathsep? 0= IF s" /" ofile $+! THEN ;
1.1 anton 141:
1.2 jwilke 142: : extractpath ( adr len -- adr len2 )
143: BEGIN dup WHILE 1-
144: 2dup + c@ pathsep? IF EXIT THEN
145: REPEAT ;
1.1 anton 146:
1.4 pazsan 147: : remove~+ ( -- )
1.35 pazsan 148: ofile $@ s" ~+/" string-prefix?
1.4 pazsan 149: IF
1.35 pazsan 150: ofile 0 3 $del
1.4 pazsan 151: THEN ;
152:
1.5 anton 153: : expandtopic ( -- ) \ stack effect correct? - anton
154: \ expands "./" into an absolute name
1.35 pazsan 155: ofile $@ s" ./" string-prefix?
1.5 anton 156: IF
1.35 pazsan 157: ofile $@ 1 /string tfile $!
158: includefilename 2@ extractpath ofile $!
1.13 jwilke 159: \ care of / only if there is a directory
1.35 pazsan 160: ofile $@len IF need/ THEN
161: tfile $@ over c@ pathsep? IF 1 /string THEN
162: ofile $+!
1.5 anton 163: THEN ;
1.3 pazsan 164:
1.24 anton 165: : del-string ( addr u u1 -- addr u2 )
166: \ delete u1 characters from string by moving stuff from further up
167: 2 pick >r /string r@ over >r swap cmove 2r> ;
168:
169: : del-./s ( addr u -- addr u2 )
170: \ deletes (/*./)* at the start of the string
171: BEGIN ( current-addr u )
172: BEGIN ( current-addr u )
173: over c@ '/ = WHILE
174: 1 del-string
175: REPEAT
176: 2dup s" ./" string-prefix? WHILE
177: 2 del-string
178: REPEAT ;
179:
180: : preserve-root ( addr1 u1 -- addr2 u2 )
181: over c@ '/ = if \ preserve / at start
182: 1 /string
183: endif ;
184:
185:
186: : skip-..-prefixes ( addr1 u1 -- addr2 u2 )
187: \ deal with ../ at start
188: begin ( current-addr u )
189: del-./s 2dup s" ../" string-prefix? while
190: 3 /string
191: repeat ;
192:
193: : compact-filename ( addr u1 -- addr u2 )
194: \ rewrite filename in place, eliminating multiple slashes, "./", and "x/.."
195: over swap preserve-root skip-..-prefixes
196: ( start current-addr u )
197: over swap '/ scan dup if ( start addr3 addr4 u4 )
198: 1 /string del-./s recurse
199: 2dup s" ../" string-prefix? if ( start addr3 addr4 u4 )
200: 3 /string ( start to from count )
1.35 pazsan 201: >r swap 2dup r@ move r>
1.24 anton 202: endif
203: endif
204: + nip over - ;
205:
206: \ test cases:
207: \ s" z/../../../a" compact-filename type cr
208: \ s" ../z/../../../a/c" compact-filename type cr
209: \ s" /././//./../..///x/y/../z/.././..//..//a//b/../c" compact-filename type cr
1.3 pazsan 210:
1.5 anton 211: : reworkdir ( -- )
1.4 pazsan 212: remove~+
1.35 pazsan 213: ofile $@ compact-filename
214: nip ofile $!len ;
1.3 pazsan 215:
1.5 anton 216: : open-ofile ( -- fid ior )
1.7 pazsan 217: \G opens the file whose name is in ofile
1.5 anton 218: expandtopic reworkdir
1.35 pazsan 219: ofile $@ r/o open-file ;
1.5 anton 220:
1.28 anton 221: : check-path ( adr1 len1 adr2 len2 -- fid 0 | 0 ior )
1.35 pazsan 222: >r >r ofile $! need/
223: r> r> ofile $+!
1.5 anton 224: open-ofile ;
1.1 anton 225:
1.15 anton 226: \ !! allow arbitrary FAMs, not just R/O
1.12 crook 227: : open-path-file ( addr1 u1 path-addr -- wfileid addr2 u2 0 | ior ) \ gforth
1.28 anton 228: \G Look in path @var{path-addr} for the file specified by @var{addr1
229: \G u1}. If found, the resulting path and and (read-only) open file
230: \G descriptor are returned. If the file is not found, @var{ior} is
231: \G what came back from the last attempt at opening the file (in the
232: \G current implementation).
233: >r
234: 2dup absolut-path? IF
235: rdrop
1.35 pazsan 236: ofile $! open-ofile
1.28 anton 237: dup 0= IF
1.35 pazsan 238: >r ofile $@ r> THEN
1.28 anton 239: EXIT
240: ELSE
241: r> -&37 >r path>string BEGIN
242: next-path dup WHILE
243: r> drop
244: 5 pick 5 pick check-path dup 0= IF
1.35 pazsan 245: drop >r 2drop 2drop r> ofile $@ 0 EXIT
1.28 anton 246: ELSE
247: >r drop
248: THEN
249: REPEAT
250: 2drop 2drop 2drop r>
1.1 anton 251: THEN ;
252:
1.12 crook 253: : open-fpath-file ( addr1 u1 -- wfileid addr2 u2 0 | ior ) \ gforth
254: \G Look in the Forth search path for the file specified by @var{addr1 u1}.
255: \G If found, the resulting path and an open file descriptor
256: \G are returned. If the file is not found, @var{ior} is non-zero.
1.9 crook 257: fpath open-path-file ;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>