| 1 |
(* Copyright (C) 2015-2025 Free Software Foundation, Inc. *) |
| 2 |
(* This file is part of GNU Modula-2. |
| 3 |
|
| 4 |
GNU Modula-2 is free software; you can redistribute it and/or modify it under |
| 5 |
the terms of the GNU General Public License as published by the Free |
| 6 |
Software Foundation; either version 3, or (at your option) any later |
| 7 |
version. |
| 8 |
|
| 9 |
GNU Modula-2 is distributed in the hope that it will be useful, but WITHOUT ANY |
| 10 |
WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 |
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 |
for more details. |
| 13 |
|
| 14 |
You should have received a copy of the GNU General Public License |
| 15 |
along with GCC; see the file COPYING3. If not see |
| 16 |
<http://www.gnu.org/licenses/>. *) |
| 17 |
|
| 18 |
IMPLEMENTATION MODULE mcSearch ; |
| 19 |
|
| 20 |
|
| 21 |
FROM SFIO IMPORT Exists ; |
| 22 |
FROM mcFileName IMPORT calculateFileName ; |
| 23 |
|
| 24 |
FROM DynamicStrings IMPORT InitString, InitStringChar, |
| 25 |
KillString, ConCat, ConCatChar, Index, Slice, |
| 26 |
Add, EqualArray, Dup, Mark, |
| 27 |
PushAllocation, PopAllocationExemption, |
| 28 |
InitStringDB, InitStringCharStarDB, |
| 29 |
InitStringCharDB, MultDB, DupDB, SliceDB ; |
| 30 |
|
| 31 |
|
| 32 |
CONST |
| 33 |
Directory = '/' ; |
| 34 |
|
| 35 |
VAR |
| 36 |
Def, Mod, |
| 37 |
UserPath, |
| 38 |
InitialPath: String ; |
| 39 |
|
| 40 |
(* |
| 41 |
#define InitString(X) InitStringDB(X, __FILE__, __LINE__) |
| 42 |
#define InitStringCharStar(X) InitStringCharStarDB(X, __FILE__, __LINE__) |
| 43 |
#define InitStringChar(X) InitStringCharDB(X, __FILE__, __LINE__) |
| 44 |
#define Mult(X,Y) MultDB(X, Y, __FILE__, __LINE__) |
| 45 |
#define Dup(X) DupDB(X, __FILE__, __LINE__) |
| 46 |
#define Slice(X,Y,Z) SliceDB(X, Y, Z, __FILE__, __LINE__) |
| 47 |
*) |
| 48 |
|
| 49 |
|
| 50 |
(* |
| 51 |
doDSdbEnter - |
| 52 |
*) |
| 53 |
|
| 54 |
PROCEDURE doDSdbEnter ; |
| 55 |
BEGIN |
| 56 |
PushAllocation |
| 57 |
END doDSdbEnter ; |
| 58 |
|
| 59 |
|
| 60 |
(* |
| 61 |
doDSdbExit - |
| 62 |
*) |
| 63 |
|
| 64 |
PROCEDURE doDSdbExit (s: String) ; |
| 65 |
BEGIN |
| 66 |
s := PopAllocationExemption (TRUE, s) |
| 67 |
END doDSdbExit ; |
| 68 |
|
| 69 |
|
| 70 |
(* |
| 71 |
DSdbEnter - |
| 72 |
*) |
| 73 |
|
| 74 |
PROCEDURE DSdbEnter ; |
| 75 |
BEGIN |
| 76 |
END DSdbEnter ; |
| 77 |
|
| 78 |
|
| 79 |
(* |
| 80 |
DSdbExit - |
| 81 |
*) |
| 82 |
|
| 83 |
PROCEDURE DSdbExit (s: String) ; |
| 84 |
BEGIN |
| 85 |
END DSdbExit ; |
| 86 |
|
| 87 |
|
| 88 |
(* |
| 89 |
#define DSdbEnter doDSdbEnter |
| 90 |
#define DSdbExit doDSdbExit |
| 91 |
*) |
| 92 |
|
| 93 |
|
| 94 |
(* |
| 95 |
prependSearchPath - prepends a new path to the initial search path. |
| 96 |
*) |
| 97 |
|
| 98 |
PROCEDURE prependSearchPath (path: String) ; |
| 99 |
BEGIN |
| 100 |
DSdbEnter ; |
| 101 |
IF EqualArray (UserPath, '') |
| 102 |
THEN |
| 103 |
UserPath := KillString (UserPath) ; |
| 104 |
UserPath := Dup (path) |
| 105 |
ELSE |
| 106 |
UserPath := ConCat (ConCatChar (UserPath, ':'), path) |
| 107 |
END ; |
| 108 |
DSdbExit (UserPath) |
| 109 |
END prependSearchPath ; |
| 110 |
|
| 111 |
|
| 112 |
(* |
| 113 |
findSourceFile - attempts to locate the source file FileName. |
| 114 |
If a file is found then TRUE is returned otherwise |
| 115 |
FALSE is returned. |
| 116 |
The parameter fullPath is set indicating the |
| 117 |
absolute location of source FileName. |
| 118 |
fullPath will be totally overwritten and should |
| 119 |
not be initialized by InitString before this function |
| 120 |
is called. |
| 121 |
fullPath is set to NIL if this function returns FALSE. |
| 122 |
findSourceFile sets fullPath to a new string if successful. |
| 123 |
The string, FileName, is not altered. |
| 124 |
*) |
| 125 |
|
| 126 |
PROCEDURE findSourceFile (FileName: String; |
| 127 |
VAR fullPath: String) : BOOLEAN ; |
| 128 |
VAR |
| 129 |
completeSearchPath: String ; |
| 130 |
start, end : INTEGER ; |
| 131 |
newpath : String ; |
| 132 |
BEGIN |
| 133 |
IF EqualArray (UserPath, '') |
| 134 |
THEN |
| 135 |
IF EqualArray (InitialPath, '') |
| 136 |
THEN |
| 137 |
completeSearchPath := InitString ('.') |
| 138 |
ELSE |
| 139 |
completeSearchPath := Dup (InitialPath) |
| 140 |
END |
| 141 |
ELSE |
| 142 |
completeSearchPath := ConCat (ConCatChar (Dup (UserPath), ':'), InitialPath) |
| 143 |
END ; |
| 144 |
start := 0 ; |
| 145 |
end := Index (completeSearchPath, ':', CARDINAL (start)) ; |
| 146 |
REPEAT |
| 147 |
IF end=-1 |
| 148 |
THEN |
| 149 |
end := 0 |
| 150 |
END ; |
| 151 |
newpath := Slice (completeSearchPath, start, end) ; |
| 152 |
IF EqualArray (newpath, '.') |
| 153 |
THEN |
| 154 |
newpath := KillString (newpath) ; |
| 155 |
newpath := Dup (FileName) |
| 156 |
ELSE |
| 157 |
newpath := ConCat (ConCatChar (newpath, Directory), FileName) |
| 158 |
END ; |
| 159 |
IF Exists (newpath) |
| 160 |
THEN |
| 161 |
fullPath := newpath ; |
| 162 |
completeSearchPath := KillString (completeSearchPath) ; |
| 163 |
RETURN TRUE |
| 164 |
END ; |
| 165 |
newpath := KillString (newpath) ; |
| 166 |
IF end#0 |
| 167 |
THEN |
| 168 |
start := end+1 ; |
| 169 |
end := Index (completeSearchPath, ':', CARDINAL (start)) |
| 170 |
END |
| 171 |
UNTIL end=0 ; |
| 172 |
|
| 173 |
fullPath := NIL ; |
| 174 |
newpath := KillString (newpath) ; |
| 175 |
completeSearchPath := KillString (completeSearchPath) ; |
| 176 |
RETURN FALSE |
| 177 |
END findSourceFile ; |
| 178 |
|
| 179 |
|
| 180 |
(* |
| 181 |
findSourceDefFile - attempts to find the definition module for |
| 182 |
a module, stem. If successful it returns |
| 183 |
the full path and returns TRUE. If unsuccessful |
| 184 |
then FALSE is returned and fullPath is set to NIL. |
| 185 |
*) |
| 186 |
|
| 187 |
PROCEDURE findSourceDefFile (stem: String; VAR fullPath: String) : BOOLEAN ; |
| 188 |
VAR |
| 189 |
f: String ; |
| 190 |
BEGIN |
| 191 |
IF Def#NIL |
| 192 |
THEN |
| 193 |
f := calculateFileName (stem, Def) ; |
| 194 |
IF findSourceFile (f, fullPath) |
| 195 |
THEN |
| 196 |
RETURN TRUE |
| 197 |
END ; |
| 198 |
f := KillString (f) |
| 199 |
END ; |
| 200 |
(* and try the GNU Modula-2 default extension *) |
| 201 |
f := calculateFileName (stem, Mark (InitString ('def'))) ; |
| 202 |
RETURN findSourceFile (f, fullPath) |
| 203 |
END findSourceDefFile ; |
| 204 |
|
| 205 |
|
| 206 |
(* |
| 207 |
findSourceModFile - attempts to find the implementation module for |
| 208 |
a module, stem. If successful it returns |
| 209 |
the full path and returns TRUE. If unsuccessful |
| 210 |
then FALSE is returned and fullPath is set to NIL. |
| 211 |
*) |
| 212 |
|
| 213 |
PROCEDURE findSourceModFile (stem: String; VAR fullPath: String) : BOOLEAN ; |
| 214 |
VAR |
| 215 |
f: String ; |
| 216 |
BEGIN |
| 217 |
IF Mod#NIL |
| 218 |
THEN |
| 219 |
f := calculateFileName (stem, Mod) ; |
| 220 |
IF findSourceFile (f, fullPath) |
| 221 |
THEN |
| 222 |
RETURN TRUE |
| 223 |
END ; |
| 224 |
f := KillString (f) |
| 225 |
END ; |
| 226 |
(* and try the GNU Modula-2 default extension *) |
| 227 |
f := calculateFileName (stem, Mark (InitString ('mod'))) ; |
| 228 |
RETURN findSourceFile (f, fullPath) |
| 229 |
END findSourceModFile ; |
| 230 |
|
| 231 |
|
| 232 |
(* |
| 233 |
setDefExtension - sets the default extension for definition modules to, ext. |
| 234 |
The string, ext, should be deallocated by the caller at |
| 235 |
an appropriate time. |
| 236 |
*) |
| 237 |
|
| 238 |
PROCEDURE setDefExtension (ext: String) ; |
| 239 |
BEGIN |
| 240 |
Def := KillString (Def) ; |
| 241 |
Def := Dup (ext) |
| 242 |
END setDefExtension ; |
| 243 |
|
| 244 |
|
| 245 |
(* |
| 246 |
setModExtension - sets the default extension for implementation and program |
| 247 |
modules to, ext. The string, ext, should be deallocated |
| 248 |
by the caller at an appropriate time. |
| 249 |
*) |
| 250 |
|
| 251 |
PROCEDURE setModExtension (ext: String) ; |
| 252 |
BEGIN |
| 253 |
Mod := KillString (Mod) ; |
| 254 |
Mod := Dup (ext) |
| 255 |
END setModExtension ; |
| 256 |
|
| 257 |
|
| 258 |
(* |
| 259 |
initSearchPath - assigns the search path to Path. |
| 260 |
The string Path may take the form: |
| 261 |
|
| 262 |
Path ::= IndividualPath { ":" IndividualPath } |
| 263 |
IndividualPath ::= "." | DirectoryPath |
| 264 |
DirectoryPath ::= [ "/" ] Name { "/" Name } |
| 265 |
Name ::= Letter { (Letter | Number) } |
| 266 |
Letter ::= A..Z | a..z |
| 267 |
Number ::= 0..9 |
| 268 |
*) |
| 269 |
|
| 270 |
PROCEDURE initSearchPath (path: String) ; |
| 271 |
BEGIN |
| 272 |
IF InitialPath#NIL |
| 273 |
THEN |
| 274 |
InitialPath := KillString (InitialPath) |
| 275 |
END ; |
| 276 |
InitialPath := path |
| 277 |
END initSearchPath ; |
| 278 |
|
| 279 |
|
| 280 |
(* |
| 281 |
Init - initializes the search path. |
| 282 |
*) |
| 283 |
|
| 284 |
PROCEDURE Init ; |
| 285 |
BEGIN |
| 286 |
UserPath := InitString ('') ; |
| 287 |
InitialPath := InitStringChar ('.') ; |
| 288 |
Def := NIL ; |
| 289 |
Mod := NIL |
| 290 |
END Init ; |
| 291 |
|
| 292 |
|
| 293 |
BEGIN |
| 294 |
Init |
| 295 |
END mcSearch. |