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