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