Annotation of gforth/kernel/paths.fs, revision 1.15
1.1 anton 1: \ paths.fs path file handling 03may97jaw
2:
1.6 anton 3: \ Copyright (C) 1995,1996,1997,1998 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
9: \ as published by the Free Software Foundation; either version 2
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, write to the Free Software
19: \ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20:
1.5 anton 21: \ -Changing the search-path:
1.12 crook 22: \ fpath+ <path> adds a directory to the searchpath
1.5 anton 23: \ fpath= <path>|<path> makes complete now searchpath
24: \ seperator is |
1.12 crook 25: \ .fpath displays the search path
1.5 anton 26: \ remark I:
27: \ a ./ in the beginning of filename is expanded to the directory the
28: \ current file comes from. ./ can also be included in the search-path!
29: \ ~+/ loads from the current working directory
30:
31: \ remark II:
32: \ if there is no sufficient space for the search path increase it!
33:
34:
35: \ -Creating custom paths:
36:
37: \ It is possible to use the search mechanism on yourself.
38:
39: \ Make a buffer for the path:
1.15 ! anton 40: \ create mypath 100 path,
1.5 anton 41: \ mypath path+
42: \ mypath path=
43: \ mypath .path
44:
45: \ do a open with the search path:
46: \ open-path-file ( adr len path -- fd adr len ior )
47: \ the file is opened read-only; if the file is not found an error is generated
1.1 anton 48:
49: \ questions to: wilke@jwdt.com
50:
1.15 ! anton 51: : path-allot ( umax -- )
! 52: \G @code{Allot} a path with @i{umax} characters capacity, initially empty.
! 53: chars dup , 0 , allot ;
! 54:
1.1 anton 55: [IFUNDEF] +place
56: : +place ( adr len adr )
57: 2dup >r >r
58: dup c@ char+ + swap move
59: r> r> dup c@ rot + swap c! ;
60: [THEN]
61:
62: [IFUNDEF] place
63: : place ( c-addr1 u c-addr2 )
64: 2dup c! char+ swap move ;
65: [THEN]
66:
1.8 anton 67: create sourcepath 1024 chars , 0 , 1024 chars allot \ !! make this dynamic
1.15 ! anton 68: sourcepath avalue fpath ( -- path-addr ) \ gforth
! 69: \ The path Gforth uses for @code{included} and friends.
1.1 anton 70:
1.15 ! anton 71: : also-path ( c-addr len path-addr -- ) \ gforth
! 72: \G add the directory @i{c-addr len} to @i{path-addr}.
1.1 anton 73: >r
74: \ len check
75: r@ cell+ @ over + r@ @ u> ABORT" path buffer too small!"
76: \ copy into
77: tuck r@ cell+ dup @ cell+ + swap cmove
1.12 crook 78: \ make delimiter
1.1 anton 79: 0 r@ cell+ dup @ cell+ + 2 pick + c! 1 + r> cell+ +!
1.15 ! anton 80: ;
! 81:
! 82: : clear-path ( path-addr -- ) \ gforth
! 83: \G Set the path @i{path-addr} to empty.
! 84: 0 swap cell+ ! ;
1.1 anton 85:
86: : only-path ( adr len path^ -- )
1.15 ! anton 87: dup clear-path also-path ;
1.1 anton 88:
1.2 jwilke 89: : path+ ( path-addr "dir" -- ) \ gforth
1.12 crook 90: \G Add the directory @var{dir} to the search path @var{path-addr}.
1.9 crook 91: name rot also-path ;
1.2 jwilke 92:
93: : fpath+ ( "dir" ) \ gforth
1.12 crook 94: \G Add directory @var{dir} to the Forth search path.
1.9 crook 95: fpath path+ ;
1.2 jwilke 96:
97: : path= ( path-addr "dir1|dir2|dir3" ) \ gforth
1.12 crook 98: \G Make a complete new search path; the path separator is |.
1.9 crook 99: name 2dup bounds ?DO i c@ '| = IF 0 i c! THEN LOOP
100: rot only-path ;
1.2 jwilke 101:
102: : fpath= ( "dir1|dir2|dir3" ) \ gforth
1.12 crook 103: \G Make a complete new Forth search path; the path separator is |.
1.9 crook 104: fpath path= ;
1.1 anton 105:
106: : path>counted cell+ dup cell+ swap @ ;
107:
108: : next-path ( adr len -- adr2 len2 )
109: 2dup 0 scan
110: dup 0= IF 2drop 0 -rot 0 -rot EXIT THEN
111: >r 1+ -rot r@ 1- -rot
112: r> - ;
113:
1.7 pazsan 114: : previous-path ( path^ -- )
1.15 ! anton 115: \ !! "fpath previous-path" doesn't work
1.1 anton 116: dup path>counted
117: BEGIN tuck dup WHILE repeat ;
118:
1.2 jwilke 119: : .path ( path-addr -- ) \ gforth
1.12 crook 120: \G Display the contents of the search path @var{path-addr}.
1.9 crook 121: path>counted
122: BEGIN next-path dup WHILE type space REPEAT 2drop 2drop ;
1.1 anton 123:
1.5 anton 124: : .fpath ( -- ) \ gforth
1.12 crook 125: \G Display the contents of the Forth search path.
1.9 crook 126: fpath .path ;
1.1 anton 127:
128: : absolut-path? ( addr u -- flag ) \ gforth
1.9 crook 129: \G A path is absolute if it starts with a / or a ~ (~ expansion),
1.5 anton 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!
1.1 anton 133: 2dup 2 u> swap 1+ c@ ': = and >r \ dos absoulte: c:/....
134: over c@ '/ = >r
135: over c@ '~ = >r
1.5 anton 136: \ 2dup 3 min S" ../" compare 0= r> or >r \ not catered for in expandtopic
137: 2 min S" ./" compare 0=
138: r> r> r> or or or ;
1.1 anton 139:
140: Create ofile 0 c, 255 chars allot
141: Create tfile 0 c, 255 chars allot
142:
143: : pathsep? dup [char] / = swap [char] \ = or ;
144:
145: : need/ ofile dup c@ + c@ pathsep? 0= IF s" /" ofile +place THEN ;
146:
1.2 jwilke 147: : extractpath ( adr len -- adr len2 )
148: BEGIN dup WHILE 1-
149: 2dup + c@ pathsep? IF EXIT THEN
150: REPEAT ;
1.1 anton 151:
1.4 pazsan 152: : remove~+ ( -- )
153: ofile count 3 min s" ~+/" compare 0=
154: IF
155: ofile count 3 /string ofile place
156: THEN ;
157:
1.5 anton 158: : expandtopic ( -- ) \ stack effect correct? - anton
159: \ expands "./" into an absolute name
160: ofile count 2 min s" ./" compare 0=
161: IF
162: ofile count 1 /string tfile place
1.13 jwilke 163: 0 ofile c! sourcefilename extractpath ofile place
164: \ care of / only if there is a directory
165: ofile c@ IF need/ THEN
1.3 pazsan 166: tfile count over c@ pathsep? IF 1 /string THEN
167: ofile +place
1.5 anton 168: THEN ;
1.3 pazsan 169:
170: : compact.. ( adr len -- adr2 len2 )
1.14 pazsan 171: \ deletes phrases like "xy/.." out of our directory name 2dec97jaw
172: over swap
173: BEGIN dup WHILE
174: dup >r '/ scan 2dup 4 min s" /../" compare 0=
175: IF
176: dup r> - >r 4 /string over r> + 4 -
177: swap 2dup + >r move dup r> over -
178: ELSE
179: rdrop dup 1 min /string
180: THEN
181: REPEAT drop over - ;
1.3 pazsan 182:
1.5 anton 183: : reworkdir ( -- )
1.4 pazsan 184: remove~+
1.10 anton 185: ofile count compact..
1.3 pazsan 186: nip ofile c! ;
187:
1.5 anton 188: : open-ofile ( -- fid ior )
1.7 pazsan 189: \G opens the file whose name is in ofile
1.5 anton 190: expandtopic reworkdir
191: ofile count r/o open-file ;
192:
1.2 jwilke 193: : check-path ( adr1 len1 adr2 len2 -- fd 0 | 0 <>0 )
194: 0 ofile ! >r >r ofile place need/
195: r> r> ofile +place
1.5 anton 196: open-ofile ;
1.1 anton 197:
1.15 ! anton 198: \ !! allow arbitrary FAMs, not just R/O
1.12 crook 199: : open-path-file ( addr1 u1 path-addr -- wfileid addr2 u2 0 | ior ) \ gforth
200: \G Look in path @var{path-addr} for the file specified by @var{addr1 u1}.
1.15 ! anton 201: \G If found, the resulting path and and (read-only) open file descriptor
1.12 crook 202: \G are returned. If the file is not found, @var{ior} is non-zero.
1.1 anton 203: >r
204: 2dup absolut-path?
205: IF rdrop
1.5 anton 206: ofile place open-ofile
1.3 pazsan 207: dup 0= IF >r ofile count r> THEN EXIT
1.1 anton 208: ELSE r> path>counted
209: BEGIN next-path dup
210: WHILE 5 pick 5 pick check-path
1.2 jwilke 211: 0= IF >r 2drop 2drop r> ofile count 0 EXIT ELSE drop THEN
1.1 anton 212: REPEAT
1.2 jwilke 213: 2drop 2drop 2drop -&38
1.1 anton 214: THEN ;
215:
1.12 crook 216: : open-fpath-file ( addr1 u1 -- wfileid addr2 u2 0 | ior ) \ gforth
217: \G Look in the Forth search path for the file specified by @var{addr1 u1}.
218: \G If found, the resulting path and an open file descriptor
219: \G are returned. If the file is not found, @var{ior} is non-zero.
1.9 crook 220: fpath open-path-file ;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>