Imported Upstream version 21
[debian/pforth] / filefind.fth
1 \ @(#) filefind.fth 98/01/26 1.2
2 \ FILE?  ( <name> -- , report which file this Forth word was defined in )
3 \
4 \ FILE? looks for ::::Filename and ;;;; in the dictionary
5 \ that have been left by INCLUDE.  It figures out nested
6 \ includes and reports each file that defines the word.
7 \
8 \ Author: Phil Burk
9 \ Copyright 1992 Phil Burk
10 \
11 \ 00001 PLB 2/21/92 Handle words from kernel or keyboard.
12 \               Support EACH.FILE?
13 \ 961213 PLB Port to pForth.
14
15 ANEW TASK-FILEFIND.FTH
16
17 : ODD@ { addr | val -- val , fetch from odd aligned address, IBM PCs??? }
18         4 0
19         DO
20                 addr i + c@
21                 val 8 lshift or -> val
22         LOOP
23         val
24 ;
25
26 \ scan dictionary from NFA for filename
27 : F?.SEARCH.NFA { nfa | dpth stoploop keyb nfa0 -- addr count }
28         0 -> dpth
29         0 -> stoploop
30         0 -> keyb
31         nfa -> nfa0
32         BEGIN
33                 nfa prevname -> nfa
34                 nfa 0>
35                 IF
36                         nfa 1+ odd@
37                         CASE
38                                 $ 3a3a3a3a ( :::: )
39                                 OF
40                                         dpth 0=
41                                         IF
42                                                 nfa count 31 and
43                                                 4 - swap 4 + swap
44                                                 true -> stoploop
45                                         ELSE
46                                                 -1 dpth + -> dpth
47                                         THEN
48                                 ENDOF
49                                 $ 3b3b3b3b ( ;;;; )
50                                 OF
51                                                 1 dpth + -> dpth
52                                                 true -> keyb     \ maybe from keyboard
53                                 ENDOF
54                         ENDCASE
55                 ELSE
56                         true -> stoploop
57                         keyb
58                         IF
59                                 " keyboard"
60                         ELSE
61                                 " 'C' kernel"
62                         THEN
63                         count
64                 THEN
65                 stoploop
66         UNTIL
67 ;
68
69 : FINDNFA.FROM { $name start_nfa -- nfa true | $word false }
70         context @ >r
71         start_nfa context !
72         $name findnfa
73         r> context !
74 ;
75
76 \ Search entire dictionary for all occurences of named word.
77 : FILE? {  | $word nfa done? -- , take name from input }
78         0 -> done?
79         bl word -> $word
80         $word findnfa
81         IF  ( -- nfa )
82                 $word count type ."  from:" cr
83                 -> nfa
84                 BEGIN
85                         nfa f?.search.nfa ( addr cnt )
86                         nfa name> 12 .r   \ print xt
87                         4 spaces type cr
88                         nfa prevname dup -> nfa
89                         0>
90                         IF
91                                 $word nfa findnfa.from  \ search from one behind found nfa
92                                 swap -> nfa
93                                 not
94                         ELSE
95                                 true
96                         THEN
97                 UNTIL
98         ELSE ( -- $word )
99                 count type ."  not found!" cr
100         THEN
101 ;
102