1: \ paths.fs path file handling 03may97jaw
2:
3: \ Copyright (C) 1995,1996,1997,1998,2000,2003,2004,2005,2006 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 3
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, see http://www.gnu.org/licenses/.
19:
20: \ -Changing the search-path:
21: \ fpath+ <path> adds a directory to the searchpath
22: \ fpath= <path>|<path> makes complete now searchpath
23: \ seperator is |
24: \ .fpath displays the search path
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:
39: \ create mypath 100 path,
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
47:
48: \ questions to: wilke@jwdt.com
49:
50: : path-allot ( umax -- ) \ gforth
51: \G @code{Allot} a path with @i{umax} characters capacity, initially empty.
52: chars dup , 0 , allot ;
53:
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:
66: \ create sourcepath 1024 chars , 0 , 1024 chars allot \ !! make this dynamic
67: 0 avalue fpath ( -- path-addr ) \ gforth
68:
69: : os-cold ( -- )
70: $400 chars dup 2 cells + allocate throw to fpath
71: 0 swap fpath 2!
72: pathstring 2@ fpath only-path
73: init-included-files ;
74:
75: \ The path Gforth uses for @code{included} and friends.
76:
77: : also-path ( c-addr len path-addr -- ) \ gforth
78: \G add the directory @i{c-addr len} to @i{path-addr}.
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
84: \ make delimiter
85: 0 r@ cell+ dup @ cell+ + 2 pick + c! 1 + r> cell+ +!
86: ;
87:
88: : clear-path ( path-addr -- ) \ gforth
89: \G Set the path @i{path-addr} to empty.
90: 0 swap cell+ ! ;
91:
92: : only-path ( adr len path^ -- )
93: dup clear-path also-path ;
94:
95: : path+ ( path-addr "dir" -- ) \ gforth
96: \G Add the directory @var{dir} to the search path @var{path-addr}.
97: name rot also-path ;
98:
99: : fpath+ ( "dir" ) \ gforth
100: \G Add directory @var{dir} to the Forth search path.
101: fpath path+ ;
102:
103: : path= ( path-addr "dir1|dir2|dir3" ) \ gforth
104: \G Make a complete new search path; the path separator is |.
105: name 2dup bounds ?DO i c@ '| = IF 0 i c! THEN LOOP
106: rot only-path ;
107:
108: : fpath= ( "dir1|dir2|dir3" ) \ gforth
109: \G Make a complete new Forth search path; the path separator is |.
110: fpath path= ;
111:
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> - ;
122:
123: : previous-path ( path^ -- )
124: \ !! "fpath previous-path" doesn't work
125: dup path>string
126: BEGIN tuck dup WHILE repeat ;
127:
128: : .path ( path-addr -- ) \ gforth
129: \G Display the contents of the search path @var{path-addr}.
130: path>string
131: BEGIN next-path dup WHILE type space REPEAT 2drop 2drop ;
132:
133: : .fpath ( -- ) \ gforth
134: \G Display the contents of the Forth search path.
135: fpath .path ;
136:
137: : absolut-path? ( addr u -- flag ) \ gforth
138: \G A path is absolute if it starts with a / or a ~ (~ expansion),
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!
142: 2dup 2 u> swap 1+ c@ ': = and >r \ dos absoulte: c:/....
143: over c@ '/ = >r
144: over c@ '~ = >r
145: \ 2dup S" ../" string-prefix? r> or >r \ not catered for in expandtopic
146: S" ./" string-prefix?
147: r> r> r> or or or ;
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:
156: : extractpath ( adr len -- adr len2 )
157: BEGIN dup WHILE 1-
158: 2dup + c@ pathsep? IF EXIT THEN
159: REPEAT ;
160:
161: : remove~+ ( -- )
162: ofile count s" ~+/" string-prefix?
163: IF
164: ofile count 3 /string ofile place
165: THEN ;
166:
167: : expandtopic ( -- ) \ stack effect correct? - anton
168: \ expands "./" into an absolute name
169: ofile count s" ./" string-prefix?
170: IF
171: ofile count 1 /string tfile place
172: 0 ofile c! includefilename 2@ extractpath ofile place
173: \ care of / only if there is a directory
174: ofile c@ IF need/ THEN
175: tfile count over c@ pathsep? IF 1 /string THEN
176: ofile +place
177: THEN ;
178:
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
224:
225: : reworkdir ( -- )
226: remove~+
227: ofile count compact-filename
228: nip ofile c! ;
229:
230: : open-ofile ( -- fid ior )
231: \G opens the file whose name is in ofile
232: expandtopic reworkdir
233: ofile count r/o open-file ;
234:
235: : check-path ( adr1 len1 adr2 len2 -- fid 0 | 0 ior )
236: 0 ofile ! >r >r ofile place need/
237: r> r> ofile +place
238: open-ofile ;
239:
240: \ !! allow arbitrary FAMs, not just R/O
241: : open-path-file ( addr1 u1 path-addr -- wfileid addr2 u2 0 | ior ) \ gforth
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
250: ofile place open-ofile
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>
265: THEN ;
266:
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.
271: fpath open-path-file ;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>