Line data Source code
1 : /* do not edit automatically generated by mc from PathName. */
2 : /* M2PathName.mod maintain a dictionary of named paths.
3 : Copyright (C) 2023-2026 Free Software Foundation, Inc.
4 : Contributed by Gaius Mulley <gaius.mulley@southwales.ac.uk>.
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 : You should have received a copy of the GNU General Public License
19 : along with GNU Modula-2; see the file COPYING3. If not see
20 : <http://www.gnu.org/licenses/>. */
21 :
22 : #include "config.h"
23 : #include "system.h"
24 : #include <stdbool.h>
25 : # if !defined (PROC_D)
26 : # define PROC_D
27 : typedef void (*PROC_t) (void);
28 : typedef struct { PROC_t proc; } PROC;
29 : # endif
30 :
31 : # if !defined (FALSE)
32 : # define FALSE (1==0)
33 : # endif
34 :
35 : # include "GStorage.h"
36 : #if defined(__cplusplus)
37 : # undef NULL
38 : # define NULL 0
39 : #endif
40 : #define _PathName_C
41 :
42 : #include "GPathName.h"
43 : # include "GStorage.h"
44 : # include "GDynamicStrings.h"
45 : # include "GSFIO.h"
46 : # include "GFIO.h"
47 : # include "GM2Printf.h"
48 : # include "GFormatStrings.h"
49 : # include "GDynamicPath.h"
50 : # include "GM2RTS.h"
51 :
52 : # define Debugging false
53 : typedef struct PathName__T1_r PathName__T1;
54 :
55 : typedef PathName__T1 *PathName_NamedPath__opaque;
56 :
57 : struct PathName__T1_r {
58 : DynamicPath_PathList pathList;
59 : DynamicStrings_String name;
60 : PathName_NamedPath__opaque tail;
61 : PathName_NamedPath__opaque next;
62 : };
63 :
64 : static PathName_NamedPath__opaque FreeList;
65 : static PathName_NamedPath__opaque NamedPathHead;
66 :
67 : /*
68 : FindNamedPathFile - Post-condition: returns NIL if a file cannot be found otherwise
69 : it returns the path including the filename.
70 : It also returns a new string the name of the path.
71 : Pre-condition: if name = NIL then it searches
72 : user path first, followed by any
73 : named path.
74 : elsif name = ''
75 : then
76 : search user path
77 : else
78 : search named path
79 : fi
80 : */
81 :
82 : extern "C" DynamicStrings_String PathName_FindNamedPathFile (DynamicStrings_String filename, DynamicStrings_String *name);
83 :
84 : /*
85 : AddInclude - adds include path to the named path. If named path
86 : is the same as the previous call then the include path
87 : is appended to the named path PathList otherwise a new
88 : named path is created and placed at the end of the
89 : named path list.
90 : */
91 :
92 : extern "C" void PathName_AddInclude (DynamicStrings_String named, DynamicStrings_String directory);
93 :
94 : /*
95 : InitNamedPath - creates a new path name with an associated pathlist.
96 : */
97 :
98 : extern "C" PathName_NamedPath PathName_InitNamedPath (DynamicStrings_String name, DynamicPath_PathList pl);
99 :
100 : /*
101 : KillNamedPath - places list np onto the freelist.
102 : Postcondition: np will be NIL.
103 : */
104 :
105 : extern "C" void PathName_KillNamedPath (PathName_NamedPath *np);
106 :
107 : /*
108 : Cons - appends pl to the end of a named path.
109 : If np is NIL a new list is created and returned
110 : containing named and pl.
111 : */
112 :
113 : extern "C" PathName_NamedPath PathName_Cons (PathName_NamedPath np, DynamicStrings_String named, DynamicPath_PathList pl);
114 :
115 : /*
116 : ConsList - concatenates named path left and right together.
117 : */
118 :
119 : extern "C" PathName_NamedPath PathName_ConsList (PathName_NamedPath left, PathName_NamedPath right);
120 :
121 : /*
122 : Stash - returns np before setting np to NIL.
123 : */
124 :
125 : extern "C" PathName_NamedPath PathName_Stash (PathName_NamedPath *np);
126 :
127 : /*
128 : SetNamedPath - assigns the named path to the default path.
129 : */
130 :
131 : extern "C" void PathName_SetNamedPath (PathName_NamedPath named);
132 :
133 : /*
134 : GetNamedPath - returns the default named path.
135 : */
136 :
137 : extern "C" PathName_NamedPath PathName_GetNamedPath (void);
138 :
139 : /*
140 : DumpPathName - display the dictionary of names and all path entries.
141 : */
142 :
143 : extern "C" void PathName_DumpPathName (const char *name_, unsigned int _name_high);
144 :
145 : /*
146 : AddSystem -
147 : */
148 :
149 : static void AddSystem (DynamicStrings_String named, DynamicStrings_String directory);
150 :
151 : /*
152 : AddUser -
153 : */
154 :
155 : static void AddUser (DynamicStrings_String named, DynamicStrings_String directory);
156 :
157 :
158 : /*
159 : AddSystem -
160 : */
161 :
162 176150 : static void AddSystem (DynamicStrings_String named, DynamicStrings_String directory)
163 : {
164 176150 : if (NamedPathHead == NULL)
165 : {
166 : /* Empty dictionary add single entry. */
167 14930 : PathName_SetNamedPath (PathName_InitNamedPath (named, DynamicPath_InitPathList (directory)));
168 : }
169 161220 : else if (DynamicStrings_Equal (NamedPathHead->tail->name, named))
170 : {
171 : /* avoid dangling else. */
172 54598 : NamedPathHead->tail->pathList = DynamicPath_Cons (NamedPathHead->tail->pathList, directory);
173 : }
174 : else
175 : {
176 : /* avoid dangling else. */
177 106622 : PathName_SetNamedPath (PathName_ConsList (static_cast<PathName_NamedPath> (NamedPathHead), PathName_InitNamedPath (named, DynamicPath_InitPathList (directory))));
178 : }
179 176150 : }
180 :
181 :
182 : /*
183 : AddUser -
184 : */
185 :
186 69630 : static void AddUser (DynamicStrings_String named, DynamicStrings_String directory)
187 : {
188 69630 : if (NamedPathHead == NULL)
189 : {
190 : /* Empty dictionary add single entry. */
191 22 : PathName_SetNamedPath (PathName_InitNamedPath (named, DynamicPath_InitPathList (directory)));
192 : }
193 69608 : else if (DynamicStrings_EqualArray (NamedPathHead->name, (const char *) "", 0))
194 : {
195 : /* avoid dangling else. */
196 : /* Found user node. */
197 55249 : NamedPathHead->pathList = DynamicPath_Cons (NamedPathHead->pathList, directory);
198 : }
199 : else
200 : {
201 : /* avoid dangling else. */
202 : /* No user node yet, so we will create one. */
203 14359 : NamedPathHead = static_cast<PathName_NamedPath__opaque> (PathName_ConsList (PathName_InitNamedPath (named, DynamicPath_InitPathList (directory)), static_cast<PathName_NamedPath> (NamedPathHead)));
204 14359 : PathName_SetNamedPath (static_cast<PathName_NamedPath> (NamedPathHead));
205 : }
206 69630 : }
207 :
208 :
209 : /*
210 : FindNamedPathFile - Post-condition: returns NIL if a file cannot be found otherwise
211 : it returns the path including the filename.
212 : It also returns a new string the name of the path.
213 : Pre-condition: if name = NIL then it searches
214 : user path first, followed by any
215 : named path.
216 : elsif name = ''
217 : then
218 : search user path
219 : else
220 : search named path
221 : fi
222 : */
223 :
224 249313 : extern "C" DynamicStrings_String PathName_FindNamedPathFile (DynamicStrings_String filename, DynamicStrings_String *name)
225 : {
226 249313 : DynamicStrings_String foundFile;
227 249313 : PathName_NamedPath__opaque np;
228 :
229 249313 : np = NamedPathHead;
230 1045671 : while (np != NULL)
231 : {
232 1026326 : if (((*name) == NULL) || (DynamicStrings_Equal (np->name, (*name))))
233 : {
234 1026326 : foundFile = DynamicPath_FindFileName (filename, np->pathList);
235 1026326 : if (foundFile != NULL)
236 : {
237 229968 : (*name) = DynamicStrings_Dup (np->name);
238 229968 : return foundFile;
239 : }
240 : }
241 796358 : np = np->next;
242 : }
243 19345 : (*name) = static_cast<DynamicStrings_String> (NULL);
244 19345 : return static_cast<DynamicStrings_String> (NULL);
245 : /* static analysis guarentees a RETURN statement will be used before here. */
246 : __builtin_unreachable ();
247 : }
248 :
249 :
250 : /*
251 : AddInclude - adds include path to the named path. If named path
252 : is the same as the previous call then the include path
253 : is appended to the named path PathList otherwise a new
254 : named path is created and placed at the end of the
255 : named path list.
256 : */
257 :
258 245780 : extern "C" void PathName_AddInclude (DynamicStrings_String named, DynamicStrings_String directory)
259 : {
260 245780 : if (Debugging)
261 : {
262 : M2Printf_fprintf2 (FIO_StdErr, (const char *) "named = %s, directory =%s\\n", 27, (const unsigned char *) &named, (sizeof (named)-1), (const unsigned char *) &directory, (sizeof (directory)-1));
263 : }
264 245780 : if ((named == NULL) || (DynamicStrings_EqualArray (named, (const char *) "", 0)))
265 : {
266 : /* avoid dangling else. */
267 69630 : AddUser (named, directory);
268 69630 : if (Debugging)
269 : {
270 : PathName_DumpPathName ((const char *) "User pathname", 13);
271 : }
272 : }
273 : else
274 : {
275 176150 : AddSystem (named, directory);
276 176150 : if (Debugging)
277 : {
278 : PathName_DumpPathName ((const char *) "System pathname", 15);
279 : }
280 : }
281 245780 : }
282 :
283 :
284 : /*
285 : InitNamedPath - creates a new path name with an associated pathlist.
286 : */
287 :
288 135933 : extern "C" PathName_NamedPath PathName_InitNamedPath (DynamicStrings_String name, DynamicPath_PathList pl)
289 : {
290 135933 : PathName_NamedPath__opaque np;
291 :
292 135933 : Storage_ALLOCATE ((void **) &np, sizeof (PathName__T1));
293 135933 : if (np == NULL)
294 : {
295 0 : M2RTS_HALT (-1);
296 : __builtin_unreachable ();
297 : }
298 : else
299 : {
300 135933 : np->pathList = pl;
301 135933 : np->name = DynamicStrings_Dup (name);
302 135933 : np->next = static_cast<PathName_NamedPath__opaque> (NULL);
303 135933 : np->tail = np;
304 : }
305 135933 : return static_cast<PathName_NamedPath> (np);
306 : /* static analysis guarentees a RETURN statement will be used before here. */
307 : __builtin_unreachable ();
308 : }
309 :
310 :
311 : /*
312 : KillNamedPath - places list np onto the freelist.
313 : Postcondition: np will be NIL.
314 : */
315 :
316 0 : extern "C" void PathName_KillNamedPath (PathName_NamedPath *np)
317 : {
318 0 : if ((*np) != NULL)
319 : {
320 0 : static_cast<PathName_NamedPath__opaque> ((*np))->tail->next = FreeList;
321 0 : FreeList = static_cast<PathName_NamedPath__opaque> ((*np));
322 0 : (*np) = static_cast<PathName_NamedPath> (NULL);
323 : }
324 0 : }
325 :
326 :
327 : /*
328 : Cons - appends pl to the end of a named path.
329 : If np is NIL a new list is created and returned
330 : containing named and pl.
331 : */
332 :
333 0 : extern "C" PathName_NamedPath PathName_Cons (PathName_NamedPath np, DynamicStrings_String named, DynamicPath_PathList pl)
334 : {
335 0 : if (np == NULL)
336 : {
337 0 : np = PathName_InitNamedPath (named, pl);
338 : }
339 : else
340 : {
341 0 : np = PathName_ConsList (np, PathName_InitNamedPath (named, pl));
342 : }
343 0 : return np;
344 : /* static analysis guarentees a RETURN statement will be used before here. */
345 : __builtin_unreachable ();
346 : }
347 :
348 :
349 : /*
350 : ConsList - concatenates named path left and right together.
351 : */
352 :
353 120981 : extern "C" PathName_NamedPath PathName_ConsList (PathName_NamedPath left, PathName_NamedPath right)
354 : {
355 120981 : if (right != NULL)
356 : {
357 120981 : static_cast<PathName_NamedPath__opaque> (left)->tail->next = static_cast<PathName_NamedPath__opaque> (right);
358 120981 : static_cast<PathName_NamedPath__opaque> (left)->tail = static_cast<PathName_NamedPath__opaque> (right)->tail;
359 : }
360 120981 : return left;
361 : /* static analysis guarentees a RETURN statement will be used before here. */
362 : __builtin_unreachable ();
363 : }
364 :
365 :
366 : /*
367 : Stash - returns np before setting np to NIL.
368 : */
369 :
370 0 : extern "C" PathName_NamedPath PathName_Stash (PathName_NamedPath *np)
371 : {
372 0 : PathName_NamedPath__opaque old;
373 :
374 0 : old = static_cast<PathName_NamedPath__opaque> ((*np));
375 0 : (*np) = static_cast<PathName_NamedPath> (NULL);
376 0 : return static_cast<PathName_NamedPath> (old);
377 : /* static analysis guarentees a RETURN statement will be used before here. */
378 : __builtin_unreachable ();
379 : }
380 :
381 :
382 : /*
383 : SetNamedPath - assigns the named path to the default path.
384 : */
385 :
386 135933 : extern "C" void PathName_SetNamedPath (PathName_NamedPath named)
387 : {
388 135933 : NamedPathHead = static_cast<PathName_NamedPath__opaque> (named);
389 135933 : }
390 :
391 :
392 : /*
393 : GetNamedPath - returns the default named path.
394 : */
395 :
396 0 : extern "C" PathName_NamedPath PathName_GetNamedPath (void)
397 : {
398 0 : return static_cast<PathName_NamedPath> (NamedPathHead);
399 : /* static analysis guarentees a RETURN statement will be used before here. */
400 : __builtin_unreachable ();
401 : }
402 :
403 :
404 : /*
405 : DumpPathName - display the dictionary of names and all path entries.
406 : */
407 :
408 0 : extern "C" void PathName_DumpPathName (const char *name_, unsigned int _name_high)
409 : {
410 0 : PathName_NamedPath__opaque np;
411 0 : DynamicStrings_String leader;
412 0 : char name[_name_high+1];
413 :
414 : /* make a local copy of each unbounded array. */
415 0 : memcpy (name, name_, _name_high+1);
416 :
417 0 : M2Printf_fprintf0 (FIO_StdErr, (const char *) name, _name_high);
418 0 : M2Printf_fprintf0 (FIO_StdErr, (const char *) " = {\\n", 6);
419 0 : np = NamedPathHead;
420 0 : while (np != NULL)
421 : {
422 0 : leader = FormatStrings_Sprintf1 (DynamicStrings_Mark (DynamicStrings_InitString ((const char *) " %s", 4)), (const unsigned char *) &np->name, (sizeof (np->name)-1));
423 0 : DynamicPath_DumpPath (leader, np->pathList);
424 0 : leader = DynamicStrings_KillString (leader);
425 0 : np = np->next;
426 : }
427 0 : M2Printf_fprintf0 (FIO_StdErr, (const char *) "}\\n", 3);
428 0 : }
429 :
430 14952 : extern "C" void _M2_PathName_init (__attribute__((unused)) int argc, __attribute__((unused)) char *argv[], __attribute__((unused)) char *envp[])
431 : {
432 14952 : NamedPathHead = static_cast<PathName_NamedPath__opaque> (NULL);
433 14952 : FreeList = static_cast<PathName_NamedPath__opaque> (NULL);
434 14952 : }
435 :
436 0 : extern "C" void _M2_PathName_fini (__attribute__((unused)) int argc, __attribute__((unused)) char *argv[], __attribute__((unused)) char *envp[])
437 : {
438 0 : }
|