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