Annotation of gforth/kernel/paths.fs, revision 1.1
1.1 ! anton 1: \ paths.fs path file handling 03may97jaw
! 2:
! 3: \ Copyright (C) 1995-1997 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 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:
! 21: 0 [IF]
! 22:
! 23: -Changing the search-path:
! 24: fpath+ <path> adds a directory to the searchpath
! 25: fpath= <path>|<path> makes complete now searchpath
! 26: seperator is |
! 27: .fpath displays the search path
! 28: remark I:
! 29: a ~+ in the beginning of filename is expanded to the directory the
! 30: current file comes from. ~+ can also be included in the search-path!
! 31:
! 32: remark II:
! 33: if there is no sufficient space for the search path increase it!
! 34:
! 35:
! 36: -Creating custom paths:
! 37:
! 38: It is possible to use the search mechanism on yourself.
! 39:
! 40: Make a buffer for the path:
! 41: create mypath 100 chars , \ maximum length (is checked)
! 42: 0 , \ real len
! 43: 100 chars allot \ space for path
! 44: use the same functions as above with:
! 45: mypath path+
! 46: mypath path=
! 47: mypath .path
! 48:
! 49: do a open with the search path:
! 50: open-path-file ( adr len path -- fd adr len )
! 51: the file is opened read-only; if the file is not found an error is generated
! 52:
! 53: \ questions to: wilke@jwdt.com
! 54: [THEN]
! 55:
! 56: [IFUNDEF] +place
! 57: : +place ( adr len adr )
! 58: 2dup >r >r
! 59: dup c@ char+ + swap move
! 60: r> r> dup c@ rot + swap c! ;
! 61: [THEN]
! 62:
! 63: [IFUNDEF] place
! 64: : place ( c-addr1 u c-addr2 )
! 65: 2dup c! char+ swap move ;
! 66: [THEN]
! 67:
! 68: create sourcepath 256 chars , 0 , 256 chars allot
! 69: sourcepath avalue fpath
! 70:
! 71: : also-path ( adr len path^ -- )
! 72: >r
! 73: \ len check
! 74: r@ cell+ @ over + r@ @ u> ABORT" path buffer too small!"
! 75: \ copy into
! 76: tuck r@ cell+ dup @ cell+ + swap cmove
! 77: \ make delemiter
! 78: 0 r@ cell+ dup @ cell+ + 2 pick + c! 1 + r> cell+ +!
! 79: ;
! 80:
! 81: : only-path ( adr len path^ -- )
! 82: dup 0 swap cell+ ! also-path ;
! 83:
! 84: : path+ name rot also-path ;
! 85: : fpath+ fpath path+ ;
! 86:
! 87: : path= name 2dup bounds ?DO i c@ '| = IF 0 i c! THEN LOOP
! 88: rot only-path ;
! 89: : fpath= fpath path= ;
! 90:
! 91: : path>counted cell+ dup cell+ swap @ ;
! 92:
! 93: : next-path ( adr len -- adr2 len2 )
! 94: 2dup 0 scan
! 95: dup 0= IF 2drop 0 -rot 0 -rot EXIT THEN
! 96: >r 1+ -rot r@ 1- -rot
! 97: r> - ;
! 98:
! 99: : privous-path ( path^ -- )
! 100: dup path>counted
! 101: BEGIN tuck dup WHILE repeat ;
! 102:
! 103: : .path
! 104: path>counted
! 105: BEGIN next-path dup WHILE type space REPEAT 2drop 2drop ;
! 106:
! 107: : .fpath fpath .path ;
! 108:
! 109: : absolut-path? ( addr u -- flag ) \ gforth
! 110: \G a path is absolute, if it starts with a / or a ~ (~ expansion),
! 111: \G or if it is in the form ./* or ../*, extended regexp: ^[/~]|./|../
! 112: \G Pathes simply containing a / are not absolute!
! 113: 2dup 2 u> swap 1+ c@ ': = and >r \ dos absoulte: c:/....
! 114: over c@ '/ = >r
! 115: over c@ '~ = >r
! 116: 2dup 2 min S" ./" compare 0= >r
! 117: 3 min S" ../" compare 0=
! 118: r> r> r> r> or or or or ;
! 119:
! 120: Create ofile 0 c, 255 chars allot
! 121: Create tfile 0 c, 255 chars allot
! 122:
! 123: : pathsep? dup [char] / = swap [char] \ = or ;
! 124:
! 125: : need/ ofile dup c@ + c@ pathsep? 0= IF s" /" ofile +place THEN ;
! 126:
! 127: : check-path ( adr1 len1 adr2 len2 -- fd 0 | 0 <>0 )
! 128: 0 ofile ! >r >r ofile place need/
! 129: r> r> ofile +place
! 130: ofile count r/o open-file ;
! 131:
! 132: : expandtopic
! 133: ofile count 2 min s" ~+" compare 0=
! 134: IF ofile count 2 /string tfile place
! 135: 0 ofile c! sourcefilename onlypath ofile place need/
! 136: tfile count ofile +place
! 137: THEN ;
! 138:
! 139: : onlypath ( adr len -- adr len2 )
! 140: BEGIN dup WHILE 1-
! 141: 2dup + c@ pathsep? IF EXIT THEN
! 142: REPEAT ;
! 143:
! 144: : open-path-file ( adr len path -- fd adr1 len2 )
! 145: >r
! 146: 2dup absolut-path?
! 147: IF rdrop
! 148: ofile place expandtopic ofile count r/o open-file throw
! 149: ofile count EXIT
! 150: ELSE r> path>counted
! 151: BEGIN next-path dup
! 152: WHILE 5 pick 5 pick check-path
! 153: 0= IF >r 2drop 2drop r> ofile count EXIT ELSE drop THEN
! 154: REPEAT
! 155: 2drop 2drop 2drop -&38 throw
! 156: THEN ;
! 157:
! 158: : open-fpath-file fpath open-path-file ;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>