1: \ paths.fs path file handling 03may97jaw
2:
3: \ Copyright (C) 1995,1996,1997,1998,2000,2003,2004,2005,2006,2007,2008,2010 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: : os-cold ( -- )
69: fpath $init
70: ofile $init
71: tfile $init
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: r@ $@len IF \ add separator if necessary
81: s" |" r@ $+! 0 r@ $@ + 1- c!
82: THEN
83: r> $+! ;
84:
85: : clear-path ( path-addr -- ) \ gforth
86: \G Set the path @i{path-addr} to empty.
87: s" " rot $! ;
88:
89: : only-path ( adr len path -- )
90: dup clear-path also-path ;
91:
92: : path+ ( path-addr "dir" -- ) \ gforth
93: \G Add the directory @var{dir} to the search path @var{path-addr}.
94: name rot also-path ;
95:
96: : fpath+ ( "dir" ) \ gforth
97: \G Add directory @var{dir} to the Forth search path.
98: fpath path+ ;
99:
100: : path= ( path-addr "dir1|dir2|dir3" ) \ gforth
101: \G Make a complete new search path; the path separator is |.
102: name 2dup bounds ?DO i c@ '| = IF 0 i c! THEN LOOP
103: rot only-path ;
104:
105: : fpath= ( "dir1|dir2|dir3" ) \ gforth
106: \G Make a complete new Forth search path; the path separator is |.
107: fpath path= ;
108:
109: : path>string ( path -- c-addr u )
110: \ string contains NULs to separate/terminate components
111: $@ ;
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
115: 0 $split 2swap ;
116:
117: : .path ( path-addr -- ) \ gforth
118: \G Display the contents of the search path @var{path-addr}.
119: path>string
120: BEGIN next-path dup WHILE type space REPEAT 2drop 2drop ;
121:
122: : .fpath ( -- ) \ gforth
123: \G Display the contents of the Forth search path.
124: fpath .path ;
125:
126: : absolut-path? ( addr u -- flag ) \ gforth
127: \G A path is absolute if it starts with a / or a ~ (~ expansion),
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!
131: 2dup 2 u> swap 1+ c@ ': = and >r \ dos absoulte: c:/....
132: over c@ '/ = >r
133: over c@ '~ = >r
134: \ 2dup S" ../" string-prefix? r> or >r \ not catered for in expandtopic
135: S" ./" string-prefix?
136: r> r> r> or or or ;
137:
138: : pathsep? dup [char] / = swap [char] \ = or ;
139:
140: : need/ ofile $@ 1- + c@ pathsep? 0= IF s" /" ofile $+! THEN ;
141:
142: : extractpath ( adr len -- adr len2 )
143: BEGIN dup WHILE 1-
144: 2dup + c@ pathsep? IF EXIT THEN
145: REPEAT ;
146:
147: : remove~+ ( -- )
148: ofile $@ s" ~+/" string-prefix?
149: IF
150: ofile 0 3 $del
151: THEN ;
152:
153: : expandtopic ( -- ) \ stack effect correct? - anton
154: \ expands "./" into an absolute name
155: ofile $@ s" ./" string-prefix?
156: IF
157: ofile $@ 1 /string tfile $!
158: includefilename 2@ extractpath ofile $!
159: \ care of / only if there is a directory
160: ofile $@len IF need/ THEN
161: tfile $@ over c@ pathsep? IF 1 /string THEN
162: ofile $+!
163: THEN ;
164:
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 )
201: >r swap 2dup r@ move r>
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
210:
211: : reworkdir ( -- )
212: remove~+
213: ofile $@ compact-filename
214: nip ofile $!len ;
215:
216: : open-ofile ( -- fid ior )
217: \G opens the file whose name is in ofile
218: expandtopic reworkdir
219: ofile $@ r/o open-file ;
220:
221: : check-path ( adr1 len1 adr2 len2 -- fid 0 | 0 ior )
222: >r >r ofile $! need/
223: r> r> ofile $+!
224: open-ofile ;
225:
226: \ !! allow arbitrary FAMs, not just R/O
227: : open-path-file ( addr1 u1 path-addr -- wfileid addr2 u2 0 | ior ) \ gforth
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
236: ofile $! open-ofile
237: dup 0= IF
238: >r ofile $@ r> THEN
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
245: drop >r 2drop 2drop r> ofile $@ 0 EXIT
246: ELSE
247: >r drop
248: THEN
249: REPEAT
250: 2drop 2drop 2drop r>
251: THEN ;
252:
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.
257: fpath open-path-file ;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>