Annotation of gforth/kernel/paths.fs, revision 1.8

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:
                     22: \ fpath+ <path>                adds a directory to the searchpath
                     23: \ fpath= <path>|<path> makes complete now searchpath
                     24: \                      seperator is |
                     25: \ .fpath                       displays the search path
                     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:
                     40: \ create mypath        100 chars ,     \ maximum length (is checked)
                     41: \              0 ,             \ real len
                     42: \              100 chars allot \ space for path
                     43: \ use the same functions as above with:
                     44: \ mypath path+ 
                     45: \ mypath path=
                     46: \ mypath .path
                     47: 
                     48: \ do a open with the search path:
                     49: \ open-path-file ( adr len path -- fd adr len ior )
                     50: \ the file is opened read-only; if the file is not found an error is generated
1.1       anton      51: 
                     52: \ questions to: wilke@jwdt.com
                     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: 
1.8     ! anton      66: create sourcepath 1024 chars , 0 , 1024 chars allot \ !! make this dynamic
1.1       anton      67: sourcepath avalue fpath
                     68: 
                     69: : also-path ( adr len path^ -- )
                     70:   >r
                     71:   \ len check
                     72:   r@ cell+ @ over + r@ @ u> ABORT" path buffer too small!"
                     73:   \ copy into
                     74:   tuck r@ cell+ dup @ cell+ + swap cmove
                     75:   \ make delemiter
                     76:   0 r@ cell+ dup @ cell+ + 2 pick + c! 1 + r> cell+ +!
                     77:   ;
                     78: 
                     79: : only-path ( adr len path^ -- )
                     80:   dup 0 swap cell+ ! also-path ;
                     81: 
1.2       jwilke     82: : path+ ( path-addr  "dir" -- ) \ gforth
                     83: \G adds a directory to the search path path-addr
                     84:   name rot also-path ;
                     85: 
                     86: : fpath+ ( "dir" ) \ gforth
                     87: \G adds a directory to the forth search path
                     88:   fpath path+ ;
                     89: 
                     90: : path= ( path-addr "dir1|dir2|dir3" ) \ gforth
                     91: \G makes complete new searchpath, seperator is |
                     92:   name 2dup bounds ?DO i c@ '| = IF 0 i c! THEN LOOP
                     93:   rot only-path ;
                     94: 
                     95: : fpath= ( "dir1|dir2|dir3" ) \ gforth
                     96: \G makes complete new searchpath, serpeator is |
                     97:   fpath path= ;
1.1       anton      98: 
                     99: : path>counted  cell+ dup cell+ swap @ ;
                    100: 
                    101: : next-path ( adr len -- adr2 len2 )
                    102:   2dup 0 scan
                    103:   dup 0= IF     2drop 0 -rot 0 -rot EXIT THEN
                    104:   >r 1+ -rot r@ 1- -rot
                    105:   r> - ;
                    106: 
1.7       pazsan    107: : previous-path ( path^ -- )
1.1       anton     108:   dup path>counted
                    109:   BEGIN tuck dup WHILE repeat ;
                    110: 
1.2       jwilke    111: : .path ( path-addr -- ) \ gforth
                    112: \G displays the contents of the search path path-addr
1.1       anton     113:   path>counted
                    114:   BEGIN next-path dup WHILE type space REPEAT 2drop 2drop ;
                    115: 
1.5       anton     116: : .fpath ( -- ) \ gforth
1.2       jwilke    117: \G displays the contents of the forth search patch
                    118:   fpath .path ;
1.1       anton     119: 
                    120: : absolut-path? ( addr u -- flag ) \ gforth
                    121:     \G a path is absolute, if it starts with a / or a ~ (~ expansion),
1.5       anton     122:     \G or if it is in the form ./*, extended regexp: ^[/~]|./, or if
                    123:     \G it has a colon as second character ("C:...").  Paths simply
                    124:     \G containing a / are not absolute!
1.1       anton     125:     2dup 2 u> swap 1+ c@ ': = and >r \ dos absoulte: c:/....
                    126:     over c@ '/ = >r
                    127:     over c@ '~ = >r
1.5       anton     128:     \ 2dup 3 min S" ../" compare 0= r> or >r \ not catered for in expandtopic
                    129:     2 min S" ./" compare 0=
                    130:     r> r> r> or or or ;
1.1       anton     131: 
                    132: Create ofile 0 c, 255 chars allot
                    133: Create tfile 0 c, 255 chars allot
                    134: 
                    135: : pathsep? dup [char] / = swap [char] \ = or ;
                    136: 
                    137: : need/   ofile dup c@ + c@ pathsep? 0= IF s" /" ofile +place THEN ;
                    138: 
1.2       jwilke    139: : extractpath ( adr len -- adr len2 )
                    140:   BEGIN dup WHILE 1-
                    141:         2dup + c@ pathsep? IF EXIT THEN
                    142:   REPEAT ;
1.1       anton     143: 
1.4       pazsan    144: : remove~+ ( -- )
                    145:     ofile count 3 min s" ~+/" compare 0=
                    146:     IF
                    147:        ofile count 3 /string ofile place
                    148:     THEN ;
                    149: 
1.5       anton     150: : expandtopic ( -- ) \ stack effect correct? - anton
                    151:     \ expands "./" into an absolute name
                    152:     ofile count 2 min s" ./" compare 0=
                    153:     IF
                    154:        ofile count 1 /string tfile place
1.2       jwilke    155:        0 ofile c! sourcefilename extractpath ofile place need/
1.3       pazsan    156:        tfile count over c@ pathsep? IF 1 /string THEN
                    157:        ofile +place
1.5       anton     158:     THEN ;
1.1       anton     159:        
1.3       pazsan    160: : compact// ( adr len -- adr2 len2 )
                    161: \ deletes phrases like "//" out of our directory name 2dec97jaw
                    162:   over >r
                    163:   BEGIN        dup WHILE
                    164:        over c@ pathsep? over 1- 0<> and
                    165:        IF over 1+ c@ pathsep?
                    166:           IF   1- over 1+ swap move
                    167:           THEN
                    168:        THEN
                    169:        1 /string
                    170:    REPEAT 
                    171:    drop r> tuck - ;
                    172: 
                    173: : compact.. ( adr len -- adr2 len2 )
                    174: \ deletes phrases like "xy/.." out of our directory name 2dec97jaw
                    175:   over >r -1 >r
                    176:   BEGIN dup WHILE
                    177:        over c@ pathsep? 
                    178:        IF      r@ -1 =
                    179:                IF      r> drop dup >r
                    180:                ELSE    2dup 1 /string 
                    181:                        3 min s" ../" compare
                    182:                        0=
                    183:                        IF      r@ over - ( diff )
                    184:                                2 pick swap - ( dest-adr )
                    185:                                >r 3 /string r> swap 2dup >r >r
                    186:                                move r> r>
                    187:                        ELSE    r> drop dup >r
                    188:                        THEN
                    189:                THEN
                    190:        THEN
                    191:        1 /string
                    192:   REPEAT 
                    193:   r> drop 
                    194:   drop r> tuck - ;
                    195: 
1.5       anton     196: : reworkdir ( -- )
1.4       pazsan    197:   remove~+
1.3       pazsan    198:   ofile count compact// compact..
                    199:   nip ofile c! ;
                    200: 
1.5       anton     201: : open-ofile ( -- fid ior )
1.7       pazsan    202:     \G opens the file whose name is in ofile
1.5       anton     203:     expandtopic reworkdir
                    204:     ofile count r/o open-file ;
                    205: 
1.2       jwilke    206: : check-path ( adr1 len1 adr2 len2 -- fd 0 | 0 <>0 )
                    207:   0 ofile ! >r >r ofile place need/
                    208:   r> r> ofile +place
1.5       anton     209:   open-ofile ;
1.1       anton     210: 
1.2       jwilke    211: : open-path-file ( adr len path-addr -- fd adr1 len2 0 | ior ) \ gforth
                    212: \G looks in path path-addr for the file specified by adr len
                    213: \G when found the resulting path and an open file descriptor
                    214: \G is returned. If the file is not found ior is non zero
1.1       anton     215:   >r
                    216:   2dup absolut-path?
                    217:   IF    rdrop
1.5       anton     218:         ofile place open-ofile
1.3       pazsan    219:        dup 0= IF >r ofile count r> THEN EXIT
1.1       anton     220:   ELSE  r> path>counted
                    221:         BEGIN  next-path dup
                    222:         WHILE  5 pick 5 pick check-path
1.2       jwilke    223:         0= IF >r 2drop 2drop r> ofile count 0 EXIT ELSE drop THEN
1.1       anton     224:   REPEAT
1.2       jwilke    225:         2drop 2drop 2drop -&38
1.1       anton     226:   THEN ;
                    227: 
1.2       jwilke    228: : open-fpath-file ( adr len -- fd adr1 len2 0 | ior ) \ gforth
                    229: \G looks in the forth search path for the file specified by adr len
                    230: \G when found the resulting path and an open file descriptor
                    231: \G is returned. If the file is not found ior is non zero
                    232:   fpath open-path-file ;

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>