| 1 |
(* M2Search.mod provides a mechanism to search selected directories. |
| 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 |
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 |
IMPLEMENTATION MODULE M2Search ; |
| 23 |
|
| 24 |
|
| 25 |
FROM M2FileName IMPORT CalculateFileName ; |
| 26 |
FROM Assertion IMPORT Assert ; |
| 27 |
FROM PathName IMPORT FindNamedPathFile ; |
| 28 |
|
| 29 |
FROM DynamicStrings IMPORT InitString, InitStringChar, |
| 30 |
KillString, ConCat, ConCatChar, Index, Slice, |
| 31 |
Add, EqualArray, Dup, Mark, |
| 32 |
PushAllocation, PopAllocationExemption, |
| 33 |
InitStringDB, InitStringCharStarDB, |
| 34 |
InitStringCharDB, MultDB, DupDB, SliceDB ; |
| 35 |
|
| 36 |
|
| 37 |
CONST |
| 38 |
GarbageDebugging = FALSE ; |
| 39 |
DefaultDefExt = '.def' ; |
| 40 |
DefaultModExt = '.mod' ; |
| 41 |
|
| 42 |
VAR |
| 43 |
Def, Mod: String ; |
| 44 |
|
| 45 |
(* Internal garbage collection debugging routines. *) |
| 46 |
|
| 47 |
(* |
| 48 |
#define InitString(X) InitStringDB(X, __FILE__, __LINE__) |
| 49 |
#define InitStringCharStar(X) InitStringCharStarDB(X, __FILE__, __LINE__) |
| 50 |
#define InitStringChar(X) InitStringCharDB(X, __FILE__, __LINE__) |
| 51 |
#define Mult(X,Y) MultDB(X, Y, __FILE__, __LINE__) |
| 52 |
#define Dup(X) DupDB(X, __FILE__, __LINE__) |
| 53 |
#define Slice(X,Y,Z) SliceDB(X, Y, Z, __FILE__, __LINE__) |
| 54 |
*) |
| 55 |
|
| 56 |
|
| 57 |
(* |
| 58 |
doDSdbEnter - called when compiled with -fcpp to enable runtime garbage |
| 59 |
collection debugging. |
| 60 |
*) |
| 61 |
|
| 62 |
(* |
| 63 |
PROCEDURE doDSdbEnter ; |
| 64 |
BEGIN |
| 65 |
PushAllocation |
| 66 |
END doDSdbEnter ; |
| 67 |
*) |
| 68 |
|
| 69 |
|
| 70 |
(* |
| 71 |
doDSdbExit - called when compiled with -fcpp to enable runtime garbage |
| 72 |
collection debugging. The parameter string s is exempt from |
| 73 |
garbage collection analysis. |
| 74 |
*) |
| 75 |
|
| 76 |
(* |
| 77 |
PROCEDURE doDSdbExit (s: String) ; |
| 78 |
BEGIN |
| 79 |
(* Check to see whether no strings have been lost since the PushAllocation. *) |
| 80 |
Assert (PopAllocationExemption (TRUE, s) = s) |
| 81 |
END doDSdbExit ; |
| 82 |
*) |
| 83 |
|
| 84 |
|
| 85 |
(* |
| 86 |
DSdbEnter - dummy nop entry code which the preprocessor replaces by |
| 87 |
doDSsbEnter when debugging garbage collection at runtime. |
| 88 |
*) |
| 89 |
|
| 90 |
(* |
| 91 |
PROCEDURE DSdbEnter ; |
| 92 |
BEGIN |
| 93 |
END DSdbEnter ; |
| 94 |
*) |
| 95 |
|
| 96 |
(* |
| 97 |
DSdbExit - dummy nop exit code which the preprocessor replaces by |
| 98 |
doDSsbExit when debugging garbage collection at runtime. |
| 99 |
*) |
| 100 |
|
| 101 |
(* |
| 102 |
PROCEDURE DSdbExit (s: String) ; |
| 103 |
BEGIN |
| 104 |
IF GarbageDebugging |
| 105 |
THEN |
| 106 |
Assert (s # NIL) |
| 107 |
END |
| 108 |
END DSdbExit ; |
| 109 |
*) |
| 110 |
|
| 111 |
|
| 112 |
(* |
| 113 |
#define DSdbEnter doDSdbEnter |
| 114 |
#define DSdbExit doDSdbExit |
| 115 |
*) |
| 116 |
|
| 117 |
|
| 118 |
(* |
| 119 |
FindSourceFile - attempts to locate the source file FileName. |
| 120 |
If a file is found then TRUE is returned otherwise |
| 121 |
FALSE is returned. |
| 122 |
The parameter FullPath is set indicating the |
| 123 |
absolute location of source FileName. |
| 124 |
FullPath will be totally overwritten and should |
| 125 |
not be initialized by InitString before this function |
| 126 |
is called. |
| 127 |
FullPath is set to NIL if this function returns FALSE. |
| 128 |
FindSourceFile sets FullPath to a new string if successful. |
| 129 |
The string FileName is not altered. |
| 130 |
*) |
| 131 |
|
| 132 |
PROCEDURE FindSourceFile (FileName: String; |
| 133 |
VAR FullPath, named: String) : BOOLEAN ; |
| 134 |
BEGIN |
| 135 |
FullPath := FindNamedPathFile (FileName, named) ; |
| 136 |
RETURN FullPath # NIL |
| 137 |
END FindSourceFile ; |
| 138 |
|
| 139 |
|
| 140 |
(* |
| 141 |
FindSourceDefFile - attempts to find the definition module for |
| 142 |
a module, Stem. If successful it returns |
| 143 |
the full path and returns TRUE. If unsuccessful |
| 144 |
then FALSE is returned and FullPath is set to NIL. |
| 145 |
*) |
| 146 |
|
| 147 |
PROCEDURE FindSourceDefFile (Stem: String; VAR FullPath, named: String) : BOOLEAN ; |
| 148 |
VAR |
| 149 |
f: String ; |
| 150 |
BEGIN |
| 151 |
IF Def # NIL |
| 152 |
THEN |
| 153 |
f := CalculateFileName (Stem, Def) ; |
| 154 |
IF FindSourceFile (f, FullPath, named) |
| 155 |
THEN |
| 156 |
RETURN TRUE |
| 157 |
END ; |
| 158 |
f := KillString (f) |
| 159 |
END ; |
| 160 |
(* Try the GNU Modula-2 default extension. *) |
| 161 |
f := CalculateFileName (Stem, Mark (InitString (DefaultDefExt))) ; |
| 162 |
RETURN FindSourceFile (f, FullPath, named) |
| 163 |
END FindSourceDefFile ; |
| 164 |
|
| 165 |
|
| 166 |
(* |
| 167 |
FindSourceModFile - attempts to find the implementation module for |
| 168 |
a module, Stem. If successful it returns |
| 169 |
the full path and returns TRUE. If unsuccessful |
| 170 |
then FALSE is returned and FullPath is set to NIL. |
| 171 |
*) |
| 172 |
|
| 173 |
PROCEDURE FindSourceModFile (Stem: String; VAR FullPath, named: String) : BOOLEAN ; |
| 174 |
VAR |
| 175 |
f: String ; |
| 176 |
BEGIN |
| 177 |
IF Mod#NIL |
| 178 |
THEN |
| 179 |
f := CalculateFileName (Stem, Mod) ; |
| 180 |
IF FindSourceFile (f, FullPath, named) |
| 181 |
THEN |
| 182 |
RETURN TRUE |
| 183 |
END ; |
| 184 |
f := KillString (f) |
| 185 |
END ; |
| 186 |
(* Try the GNU Modula-2 default extension. *) |
| 187 |
f := CalculateFileName (Stem, Mark (InitString (DefaultModExt))) ; |
| 188 |
RETURN FindSourceFile (f, FullPath, named) |
| 189 |
END FindSourceModFile ; |
| 190 |
|
| 191 |
|
| 192 |
(* |
| 193 |
SetDefExtension - sets the default extension for definition modules to, ext. |
| 194 |
The string, ext, should be deallocated by the caller at |
| 195 |
an appropriate time. |
| 196 |
*) |
| 197 |
|
| 198 |
PROCEDURE SetDefExtension (ext: String) ; |
| 199 |
BEGIN |
| 200 |
Def := KillString (Def) ; |
| 201 |
Def := Dup (ext) |
| 202 |
END SetDefExtension ; |
| 203 |
|
| 204 |
|
| 205 |
(* |
| 206 |
SetModExtension - sets the default extension for implementation and program |
| 207 |
modules to, ext. The string, ext, should be deallocated |
| 208 |
by the caller at an appropriate time. |
| 209 |
*) |
| 210 |
|
| 211 |
PROCEDURE SetModExtension (ext: String) ; |
| 212 |
BEGIN |
| 213 |
Mod := KillString (Mod) ; |
| 214 |
Mod := Dup (ext) |
| 215 |
END SetModExtension ; |
| 216 |
|
| 217 |
|
| 218 |
(* |
| 219 |
Init - initializes the def and mod default string names to NIL. |
| 220 |
*) |
| 221 |
|
| 222 |
PROCEDURE Init ; |
| 223 |
BEGIN |
| 224 |
Def := NIL ; |
| 225 |
Mod := NIL |
| 226 |
END Init ; |
| 227 |
|
| 228 |
|
| 229 |
BEGIN |
| 230 |
Init |
| 231 |
END M2Search. |