Debian Codegraph @

gcc/m2/gm2-compiler/PathName.mod

raw
1 IMPLEMENTATION MODULE PathName ;
2
3 FROM Storage IMPORT ALLOCATE, DEALLOCATE ;
4 FROM DynamicStrings IMPORT InitString, ConCat, ConCatChar, char, Dup,
5 KillString, Length, EqualArray, Equal, Mark ;
6 FROM SFIO IMPORT Exists ;
7 FROM FIO IMPORT StdErr ;
8 FROM M2Printf IMPORT fprintf0, fprintf1, fprintf2 ;
9 FROM FormatStrings IMPORT Sprintf1 ;
10
11 FROM DynamicPath IMPORT InitPathList, FindFileName ;
12
13 IMPORT DynamicPath ;
14
15
16 CONST
17 Debugging = FALSE ;
18
19 TYPE
20 NamedPath = POINTER TO RECORD
21 pathList: PathList ;
22 name : String ;
23 tail,
24 next : NamedPath ;
25 END ;
26
27
28 VAR
29 FreeList,
30 NamedPathHead: NamedPath ;
31
32
33
34 (*
35 AddSystem -
36 *)
37
38 PROCEDURE AddSystem (named, directory: String) ;
39 BEGIN
40 IF NamedPathHead = NIL
41 THEN
42 (* Empty dictionary add single entry. *)
43 SetNamedPath (InitNamedPath (named, InitPathList (directory)))
44 ELSIF Equal (NamedPathHead^.tail^.name, named)
45 THEN
46 NamedPathHead^.tail^.pathList := DynamicPath.Cons (NamedPathHead^.tail^.pathList,
47 directory)
48 ELSE
49 SetNamedPath (ConsList (NamedPathHead,
50 InitNamedPath (named, InitPathList (directory))))
51 END
52 END AddSystem ;
53
54
55 (*
56 AddUser -
57 *)
58
59 PROCEDURE AddUser (named, directory: String) ;
60 BEGIN
61 IF NamedPathHead = NIL
62 THEN
63 (* Empty dictionary add single entry. *)
64 SetNamedPath (InitNamedPath (named, InitPathList (directory)))
65 ELSIF EqualArray (NamedPathHead^.name, '')
66 THEN
67 (* Found user node. *)
68 NamedPathHead^.pathList := DynamicPath.Cons (NamedPathHead^.pathList,
69 directory)
70 ELSE
71 (* No user node yet, so we will create one. *)
72 NamedPathHead := ConsList (InitNamedPath (named, InitPathList (directory)),
73 NamedPathHead) ;
74 SetNamedPath (NamedPathHead)
75 END
76 END AddUser ;
77
78
79 (*
80 AddInclude - adds include path to the named path. If named path
81 is the same as the previous call then the include path
82 is appended to the named path PathList otherwise a new
83 named path is created and placed at the end of the
84 named path list.
85 *)
86
87 PROCEDURE AddInclude (named, directory: String) ;
88 BEGIN
89 IF Debugging
90 THEN
91 fprintf2 (StdErr, "named = %s, directory =%s\n",
92 named, directory)
93 END ;
94 IF (named = NIL) OR EqualArray (named, '')
95 THEN
96 AddUser (named, directory) ;
97 IF Debugging
98 THEN
99 DumpPathName ('User pathname')
100 END
101 ELSE
102 AddSystem (named, directory) ;
103 IF Debugging
104 THEN
105 DumpPathName ('System pathname')
106 END
107 END
108 END AddInclude ;
109
110
111 (*
112 SetNamedPath - assigns the named path to the default path.
113 *)
114
115 PROCEDURE SetNamedPath (named: NamedPath) ;
116 BEGIN
117 NamedPathHead := named
118 END SetNamedPath ;
119
120
121 (*
122 GetNamedPath - returns the default named path.
123 *)
124
125 PROCEDURE GetNamedPath () : NamedPath ;
126 BEGIN
127 RETURN NamedPathHead
128 END GetNamedPath ;
129
130
131 (*
132 KillNamedPath - places list np onto the freelist.
133 Postcondition: np will be NIL.
134 *)
135
136 PROCEDURE KillNamedPath (VAR np: NamedPath) ;
137 BEGIN
138 IF np # NIL
139 THEN
140 np^.tail^.next := FreeList ;
141 FreeList := np ;
142 np := NIL
143 END
144 END KillNamedPath ;
145
146
147 (*
148 ConsList - concatenates named path left and right together.
149 *)
150
151 PROCEDURE ConsList (left, right: NamedPath) : NamedPath ;
152 BEGIN
153 IF right # NIL
154 THEN
155 left^.tail^.next := right ;
156 left^.tail := right^.tail
157 END ;
158 RETURN left
159 END ConsList ;
160
161
162 (*
163 Cons - appends pl to the end of a named path.
164 If np is NIL a new list is created and returned
165 containing named and pl.
166 *)
167
168 PROCEDURE Cons (np: NamedPath; named: String; pl: PathList) : NamedPath ;
169 BEGIN
170 IF np = NIL
171 THEN
172 np := InitNamedPath (named, pl)
173 ELSE
174 np := ConsList (np, InitNamedPath (named, pl))
175 END ;
176 RETURN np
177 END Cons ;
178
179
180 (*
181 Stash - returns np before setting np to NIL.
182 *)
183
184 PROCEDURE Stash (VAR np: NamedPath) : NamedPath ;
185 VAR
186 old: NamedPath ;
187 BEGIN
188 old := np ;
189 np := NIL ;
190 RETURN old
191 END Stash ;
192
193
194 (*
195 InitNamedPath - creates a new path name with an associated pathlist.
196 *)
197
198 PROCEDURE InitNamedPath (name: String; pl: PathList) : NamedPath ;
199 VAR
200 np: NamedPath ;
201 BEGIN
202 NEW (np) ;
203 IF np = NIL
204 THEN
205 HALT
206 ELSE
207 np^.pathList := pl ;
208 np^.name := Dup (name) ;
209 np^.next := NIL ;
210 np^.tail := np
211 END ;
212 RETURN np
213 END InitNamedPath ;
214
215
216 (*
217 FindNamedPathFile - Post-condition: returns NIL if a file cannot be found otherwise
218 it returns the path including the filename.
219 It also returns a new string the name of the path.
220 Pre-condition: if name = NIL then it searches
221 user path first, followed by any
222 named path.
223 elsif name = ''
224 then
225 search user path
226 else
227 search named path
228 fi
229 *)
230
231 PROCEDURE FindNamedPathFile (filename: String; VAR name: String) : String ;
232 VAR
233 foundFile: String ;
234 np : NamedPath ;
235 BEGIN
236 np := NamedPathHead ;
237 WHILE np # NIL DO
238 IF (name = NIL) OR Equal (np^.name, name)
239 THEN
240 foundFile := FindFileName (filename, np^.pathList) ;
241 IF foundFile # NIL
242 THEN
243 name := Dup (np^.name) ;
244 RETURN foundFile
245 END
246 END ;
247 np := np^.next
248 END ;
249 name := NIL ;
250 RETURN NIL
251 END FindNamedPathFile ;
252
253
254 (*
255 DumpPathName - display the dictionary of names and all path entries.
256 *)
257
258 PROCEDURE DumpPathName (name: ARRAY OF CHAR) ;
259 VAR
260 np : NamedPath ;
261 leader: String ;
262 BEGIN
263 fprintf0 (StdErr, name) ;
264 fprintf0 (StdErr, " = {\n") ;
265 np := NamedPathHead ;
266 WHILE np # NIL DO
267 leader := Sprintf1 (Mark (InitString (" %s")), np^.name) ;
268 DynamicPath.DumpPath (leader, np^.pathList) ;
269 leader := KillString (leader) ;
270 np := np^.next
271 END ;
272 fprintf0 (StdErr, "}\n")
273 END DumpPathName ;
274
275
276 BEGIN
277 NamedPathHead := NIL ;
278 FreeList := NIL
279 END PathName.