Debian Codegraph @

gcc/m2/gm2-compiler/DynamicPath.mod

raw
1 (* DynamicPath.mod implements a path for DynamicStrings.
2
3 Copyright (C) 2001-2025 Free Software Foundation, Inc.
4 Contributed by Gaius Mulley <[email protected]>.
5
6 This file is part of GNU Modula-2.
7
8 GNU Modula-2 is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GNU Modula-2 is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 Under Section 7 of GPL version 3, you are granted additional
19 permissions described in the GCC Runtime Library Exception, version
20 3.1, as published by the Free Software Foundation.
21
22 You should have received a copy of the GNU General Public License and
23 a copy of the GCC Runtime Library Exception along with this program;
24 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 <http://www.gnu.org/licenses/>. *)
26
27 IMPLEMENTATION MODULE DynamicPath ; (*!m2iso+gm2*)
28
29 FROM Storage IMPORT ALLOCATE, DEALLOCATE ;
30 FROM DynamicStrings IMPORT InitString, ConCat, ConCatChar, char, Dup,
31 KillString, Length, EqualArray ;
32 FROM SFIO IMPORT Exists ;
33 FROM FIO IMPORT StdErr ;
34 FROM M2Printf IMPORT fprintf0, fprintf1 ;
35
36 CONST
37 Directory = '/' ;
38 Debugging = FALSE ;
39
40 TYPE
41 PathList = POINTER TO RECORD
42 tail,
43 next : PathList ;
44 entry: String ;
45 END ;
46
47
48 VAR
49 FreeList: PathList ;
50
51
52 (*
53 KillPathList - places list pl onto the freelist.
54 Postcondition: pl will be NIL.
55 *)
56
57 PROCEDURE KillPathList (VAR pl: PathList) ;
58 BEGIN
59 IF pl # NIL
60 THEN
61 pl^.tail^.next := FreeList ;
62 FreeList := pl ;
63 pl := NIL
64 END
65 END KillPathList ;
66
67
68 (*
69 InitPathList - creates a new empty path list.
70 It takes a copy of the string.
71 *)
72
73 PROCEDURE InitPathList (str: String) : PathList ;
74 VAR
75 pl: PathList ;
76 BEGIN
77 NEW (pl) ;
78 WITH pl^ DO
79 tail := pl ;
80 next := NIL ;
81 entry := Dup (str)
82 END ;
83 RETURN pl
84 END InitPathList ;
85
86
87 (*
88 Cons - appends str to the end of a path list.
89 If pl is NIL a new list is created and returned
90 containing str.
91 *)
92
93 PROCEDURE Cons (pl: PathList; str: String) : PathList ;
94 BEGIN
95 IF pl = NIL
96 THEN
97 pl := InitPathList (str)
98 ELSE
99 pl := ConsList (pl, InitPathList (str))
100 END ;
101 RETURN pl
102 END Cons ;
103
104
105 (*
106 ConsList - concatenates path list left and right together.
107 *)
108
109 PROCEDURE ConsList (left, right: PathList) : PathList ;
110 BEGIN
111 IF right # NIL
112 THEN
113 left^.tail^.next := right ;
114 left^.tail := right^.tail
115 END ;
116 RETURN left
117 END ConsList ;
118
119
120 (*
121 Stash - returns pl before setting pl to NIL.
122 *)
123
124 PROCEDURE Stash (VAR pl: PathList) : PathList ;
125 VAR
126 old: PathList ;
127 BEGIN
128 old := pl ;
129 pl := NIL ;
130 RETURN old
131 END Stash ;
132
133
134 (*
135 AddDir - if str is not empty and does not end with / then add
136 a directory.
137 Postcondition: str is returned (with a '/' at the end)
138 or is empty.
139 *)
140
141 PROCEDURE AddDir (str: String) : String ;
142 BEGIN
143 IF Length (str) > 0
144 THEN
145 IF char (str, -1) # Directory
146 THEN
147 str := ConCatChar (str, Directory)
148 END
149 END ;
150 RETURN str
151 END AddDir ;
152
153
154 (*
155 FindFileName - returns NIL if a file cannot be found otherwise
156 it returns the path including the filename.
157 *)
158
159 PROCEDURE FindFileName (filename: String; pl: PathList) : String ;
160 VAR
161 s: String ;
162 BEGIN
163 WHILE pl # NIL DO
164 s := ConCat (AddDir (Dup (pl^.entry)), Dup (filename)) ;
165 IF Debugging
166 THEN
167 fprintf1 (StdErr, "testing for %s: ", s)
168 END ;
169 IF Exists (s)
170 THEN
171 IF Debugging
172 THEN
173 fprintf0 (StdErr, "yes\n")
174 END ;
175 RETURN s
176 END ;
177 IF Debugging
178 THEN
179 fprintf0 (StdErr, "no\n")
180 END ;
181 s := KillString (s) ;
182 pl := pl^.next
183 END ;
184 IF Debugging
185 THEN
186 fprintf1 (StdErr, "FindFileName did not find: %s in path\n", filename)
187 END ;
188 RETURN NIL
189 END FindFileName ;
190
191
192 (*
193 DumpPath - debugging dump of the pathlist.
194 *)
195
196 PROCEDURE DumpPath (name: String; pl: PathList) ;
197 BEGIN
198 fprintf1 (StdErr, "%s:", name) ;
199 WHILE pl # NIL DO
200 fprintf0 (StdErr, " {") ;
201 fprintf1 (StdErr, "%s", pl^.entry) ;
202 fprintf0 (StdErr, "}") ;
203 pl := pl^.next
204 END ;
205 fprintf0 (StdErr, "\n")
206 END DumpPath ;
207
208
209 BEGIN
210 FreeList := NIL
211 END DynamicPath.