| 1 |
/* do not edit automatically generated by mc from mcSearch. */ |
| 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 |
#include "config.h" |
| 19 |
#include "system.h" |
| 20 |
#include <stdbool.h> |
| 21 |
# if !defined (PROC_D) |
| 22 |
# define PROC_D |
| 23 |
typedef void (*PROC_t) (void); |
| 24 |
typedef struct { PROC_t proc; } PROC; |
| 25 |
# endif |
| 26 |
|
| 27 |
# if !defined (TRUE) |
| 28 |
# define TRUE (1==1) |
| 29 |
# endif |
| 30 |
|
| 31 |
# if !defined (FALSE) |
| 32 |
# define FALSE (1==0) |
| 33 |
# endif |
| 34 |
|
| 35 |
#if defined(__cplusplus) |
| 36 |
# undef NULL |
| 37 |
# define NULL 0 |
| 38 |
#endif |
| 39 |
#define _mcSearch_C |
| 40 |
|
| 41 |
#include "GmcSearch.h" |
| 42 |
# include "GSFIO.h" |
| 43 |
# include "GmcFileName.h" |
| 44 |
# include "GDynamicStrings.h" |
| 45 |
|
| 46 |
# define Directory '/' |
| 47 |
static DynamicStrings_String Def; |
| 48 |
static DynamicStrings_String Mod; |
| 49 |
static DynamicStrings_String UserPath; |
| 50 |
static DynamicStrings_String InitialPath; |
| 51 |
|
| 52 |
/* |
| 53 |
initSearchPath - assigns the search path to Path. |
| 54 |
The string Path may take the form: |
| 55 |
|
| 56 |
Path ::= IndividualPath { ":" IndividualPath } |
| 57 |
IndividualPath ::= "." | DirectoryPath |
| 58 |
DirectoryPath ::= [ "/" ] Name { "/" Name } |
| 59 |
Name ::= Letter { (Letter | Number) } |
| 60 |
Letter ::= A..Z | a..z |
| 61 |
Number ::= 0..9 |
| 62 |
*/ |
| 63 |
|
| 64 |
extern "C" void mcSearch_initSearchPath (DynamicStrings_String path); |
| 65 |
|
| 66 |
/* |
| 67 |
prependSearchPath - prepends a new path to the initial search path. |
| 68 |
*/ |
| 69 |
|
| 70 |
extern "C" void mcSearch_prependSearchPath (DynamicStrings_String path); |
| 71 |
|
| 72 |
/* |
| 73 |
findSourceFile - attempts to locate the source file FileName. |
| 74 |
If a file is found then TRUE is returned otherwise |
| 75 |
FALSE is returned. |
| 76 |
The parameter fullPath is set indicating the |
| 77 |
absolute location of source FileName. |
| 78 |
fullPath will be totally overwritten and should |
| 79 |
not be initialized by InitString before this function |
| 80 |
is called. |
| 81 |
fullPath is set to NIL if this function returns FALSE. |
| 82 |
findSourceFile sets fullPath to a new string if successful. |
| 83 |
The string, FileName, is not altered. |
| 84 |
*/ |
| 85 |
|
| 86 |
extern "C" bool mcSearch_findSourceFile (DynamicStrings_String FileName, DynamicStrings_String *fullPath); |
| 87 |
|
| 88 |
/* |
| 89 |
findSourceDefFile - attempts to find the definition module for |
| 90 |
a module, stem. If successful it returns |
| 91 |
the full path and returns TRUE. If unsuccessful |
| 92 |
then FALSE is returned and fullPath is set to NIL. |
| 93 |
*/ |
| 94 |
|
| 95 |
extern "C" bool mcSearch_findSourceDefFile (DynamicStrings_String stem, DynamicStrings_String *fullPath); |
| 96 |
|
| 97 |
/* |
| 98 |
findSourceModFile - attempts to find the implementation module for |
| 99 |
a module, stem. If successful it returns |
| 100 |
the full path and returns TRUE. If unsuccessful |
| 101 |
then FALSE is returned and fullPath is set to NIL. |
| 102 |
*/ |
| 103 |
|
| 104 |
extern "C" bool mcSearch_findSourceModFile (DynamicStrings_String stem, DynamicStrings_String *fullPath); |
| 105 |
|
| 106 |
/* |
| 107 |
setDefExtension - sets the default extension for definition modules to, ext. |
| 108 |
The string, ext, should be deallocated by the caller at |
| 109 |
an appropriate time. |
| 110 |
*/ |
| 111 |
|
| 112 |
extern "C" void mcSearch_setDefExtension (DynamicStrings_String ext); |
| 113 |
|
| 114 |
/* |
| 115 |
setModExtension - sets the default extension for implementation and program |
| 116 |
modules to, ext. The string, ext, should be deallocated |
| 117 |
by the caller at an appropriate time. |
| 118 |
*/ |
| 119 |
|
| 120 |
extern "C" void mcSearch_setModExtension (DynamicStrings_String ext); |
| 121 |
|
| 122 |
/* |
| 123 |
doDSdbEnter - |
| 124 |
*/ |
| 125 |
|
| 126 |
static void doDSdbEnter (void); |
| 127 |
|
| 128 |
/* |
| 129 |
doDSdbExit - |
| 130 |
*/ |
| 131 |
|
| 132 |
static void doDSdbExit (DynamicStrings_String s); |
| 133 |
|
| 134 |
/* |
| 135 |
DSdbEnter - |
| 136 |
*/ |
| 137 |
|
| 138 |
static void DSdbEnter (void); |
| 139 |
|
| 140 |
/* |
| 141 |
DSdbExit - |
| 142 |
*/ |
| 143 |
|
| 144 |
static void DSdbExit (DynamicStrings_String s); |
| 145 |
|
| 146 |
/* |
| 147 |
Init - initializes the search path. |
| 148 |
*/ |
| 149 |
|
| 150 |
static void Init (void); |
| 151 |
|
| 152 |
|
| 153 |
/* |
| 154 |
doDSdbEnter - |
| 155 |
*/ |
| 156 |
|
| 157 |
static void doDSdbEnter (void) |
| 158 |
{ |
| 159 |
DynamicStrings_PushAllocation (); |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
/* |
| 164 |
doDSdbExit - |
| 165 |
*/ |
| 166 |
|
| 167 |
static void doDSdbExit (DynamicStrings_String s) |
| 168 |
{ |
| 169 |
s = DynamicStrings_PopAllocationExemption (true, s); |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
/* |
| 174 |
DSdbEnter - |
| 175 |
*/ |
| 176 |
|
| 177 |
static void DSdbEnter (void) |
| 178 |
{ |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
/* |
| 183 |
DSdbExit - |
| 184 |
*/ |
| 185 |
|
| 186 |
static void DSdbExit (DynamicStrings_String s) |
| 187 |
{ |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
/* |
| 192 |
Init - initializes the search path. |
| 193 |
*/ |
| 194 |
|
| 195 |
static void Init (void) |
| 196 |
{ |
| 197 |
UserPath = DynamicStrings_InitString ((const char *) "", 0); |
| 198 |
InitialPath = DynamicStrings_InitStringChar ('.'); |
| 199 |
Def = static_cast<DynamicStrings_String> (NULL); |
| 200 |
Mod = static_cast<DynamicStrings_String> (NULL); |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
/* |
| 205 |
initSearchPath - assigns the search path to Path. |
| 206 |
The string Path may take the form: |
| 207 |
|
| 208 |
Path ::= IndividualPath { ":" IndividualPath } |
| 209 |
IndividualPath ::= "." | DirectoryPath |
| 210 |
DirectoryPath ::= [ "/" ] Name { "/" Name } |
| 211 |
Name ::= Letter { (Letter | Number) } |
| 212 |
Letter ::= A..Z | a..z |
| 213 |
Number ::= 0..9 |
| 214 |
*/ |
| 215 |
|
| 216 |
extern "C" void mcSearch_initSearchPath (DynamicStrings_String path) |
| 217 |
{ |
| 218 |
if (InitialPath != NULL) |
| 219 |
{ |
| 220 |
InitialPath = DynamicStrings_KillString (InitialPath); |
| 221 |
} |
| 222 |
InitialPath = path; |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
/* |
| 227 |
prependSearchPath - prepends a new path to the initial search path. |
| 228 |
*/ |
| 229 |
|
| 230 |
extern "C" void mcSearch_prependSearchPath (DynamicStrings_String path) |
| 231 |
{ |
| 232 |
DSdbEnter (); |
| 233 |
if (DynamicStrings_EqualArray (UserPath, (const char *) "", 0)) |
| 234 |
{ |
| 235 |
UserPath = DynamicStrings_KillString (UserPath); |
| 236 |
UserPath = DynamicStrings_Dup (path); |
| 237 |
} |
| 238 |
else |
| 239 |
{ |
| 240 |
UserPath = DynamicStrings_ConCat (DynamicStrings_ConCatChar (UserPath, ':'), path); |
| 241 |
} |
| 242 |
DSdbExit (UserPath); |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
/* |
| 247 |
findSourceFile - attempts to locate the source file FileName. |
| 248 |
If a file is found then TRUE is returned otherwise |
| 249 |
FALSE is returned. |
| 250 |
The parameter fullPath is set indicating the |
| 251 |
absolute location of source FileName. |
| 252 |
fullPath will be totally overwritten and should |
| 253 |
not be initialized by InitString before this function |
| 254 |
is called. |
| 255 |
fullPath is set to NIL if this function returns FALSE. |
| 256 |
findSourceFile sets fullPath to a new string if successful. |
| 257 |
The string, FileName, is not altered. |
| 258 |
*/ |
| 259 |
|
| 260 |
extern "C" bool mcSearch_findSourceFile (DynamicStrings_String FileName, DynamicStrings_String *fullPath) |
| 261 |
{ |
| 262 |
DynamicStrings_String completeSearchPath; |
| 263 |
int start; |
| 264 |
int end; |
| 265 |
DynamicStrings_String newpath; |
| 266 |
|
| 267 |
if (DynamicStrings_EqualArray (UserPath, (const char *) "", 0)) |
| 268 |
{ |
| 269 |
if (DynamicStrings_EqualArray (InitialPath, (const char *) "", 0)) |
| 270 |
{ |
| 271 |
completeSearchPath = DynamicStrings_InitString ((const char *) ".", 1); |
| 272 |
} |
| 273 |
else |
| 274 |
{ |
| 275 |
completeSearchPath = DynamicStrings_Dup (InitialPath); |
| 276 |
} |
| 277 |
} |
| 278 |
else |
| 279 |
{ |
| 280 |
completeSearchPath = DynamicStrings_ConCat (DynamicStrings_ConCatChar (DynamicStrings_Dup (UserPath), ':'), InitialPath); |
| 281 |
} |
| 282 |
start = 0; |
| 283 |
end = DynamicStrings_Index (completeSearchPath, ':', (unsigned int ) (start)); |
| 284 |
do { |
| 285 |
if (end == -1) |
| 286 |
{ |
| 287 |
end = 0; |
| 288 |
} |
| 289 |
newpath = DynamicStrings_Slice (completeSearchPath, start, end); |
| 290 |
if (DynamicStrings_EqualArray (newpath, (const char *) ".", 1)) |
| 291 |
{ |
| 292 |
newpath = DynamicStrings_KillString (newpath); |
| 293 |
newpath = DynamicStrings_Dup (FileName); |
| 294 |
} |
| 295 |
else |
| 296 |
{ |
| 297 |
newpath = DynamicStrings_ConCat (DynamicStrings_ConCatChar (newpath, Directory), FileName); |
| 298 |
} |
| 299 |
if (SFIO_Exists (newpath)) |
| 300 |
{ |
| 301 |
(*fullPath) = newpath; |
| 302 |
completeSearchPath = DynamicStrings_KillString (completeSearchPath); |
| 303 |
return true; |
| 304 |
} |
| 305 |
newpath = DynamicStrings_KillString (newpath); |
| 306 |
if (end != 0) |
| 307 |
{ |
| 308 |
start = end+1; |
| 309 |
end = DynamicStrings_Index (completeSearchPath, ':', (unsigned int ) (start)); |
| 310 |
} |
| 311 |
} while (! (end == 0)); |
| 312 |
(*fullPath) = static_cast<DynamicStrings_String> (NULL); |
| 313 |
newpath = DynamicStrings_KillString (newpath); |
| 314 |
completeSearchPath = DynamicStrings_KillString (completeSearchPath); |
| 315 |
return false; |
| 316 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 317 |
__builtin_unreachable (); |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
/* |
| 322 |
findSourceDefFile - attempts to find the definition module for |
| 323 |
a module, stem. If successful it returns |
| 324 |
the full path and returns TRUE. If unsuccessful |
| 325 |
then FALSE is returned and fullPath is set to NIL. |
| 326 |
*/ |
| 327 |
|
| 328 |
extern "C" bool mcSearch_findSourceDefFile (DynamicStrings_String stem, DynamicStrings_String *fullPath) |
| 329 |
{ |
| 330 |
DynamicStrings_String f; |
| 331 |
|
| 332 |
if (Def != NULL) |
| 333 |
{ |
| 334 |
f = mcFileName_calculateFileName (stem, Def); |
| 335 |
if (mcSearch_findSourceFile (f, fullPath)) |
| 336 |
{ |
| 337 |
return true; |
| 338 |
} |
| 339 |
f = DynamicStrings_KillString (f); |
| 340 |
} |
| 341 |
/* and try the GNU Modula-2 default extension */ |
| 342 |
f = mcFileName_calculateFileName (stem, DynamicStrings_Mark (DynamicStrings_InitString ((const char *) "def", 3))); |
| 343 |
return mcSearch_findSourceFile (f, fullPath); |
| 344 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 345 |
__builtin_unreachable (); |
| 346 |
} |
| 347 |
|
| 348 |
|
| 349 |
/* |
| 350 |
findSourceModFile - attempts to find the implementation module for |
| 351 |
a module, stem. If successful it returns |
| 352 |
the full path and returns TRUE. If unsuccessful |
| 353 |
then FALSE is returned and fullPath is set to NIL. |
| 354 |
*/ |
| 355 |
|
| 356 |
extern "C" bool mcSearch_findSourceModFile (DynamicStrings_String stem, DynamicStrings_String *fullPath) |
| 357 |
{ |
| 358 |
DynamicStrings_String f; |
| 359 |
|
| 360 |
if (Mod != NULL) |
| 361 |
{ |
| 362 |
f = mcFileName_calculateFileName (stem, Mod); |
| 363 |
if (mcSearch_findSourceFile (f, fullPath)) |
| 364 |
{ |
| 365 |
return true; |
| 366 |
} |
| 367 |
f = DynamicStrings_KillString (f); |
| 368 |
} |
| 369 |
/* and try the GNU Modula-2 default extension */ |
| 370 |
f = mcFileName_calculateFileName (stem, DynamicStrings_Mark (DynamicStrings_InitString ((const char *) "mod", 3))); |
| 371 |
return mcSearch_findSourceFile (f, fullPath); |
| 372 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 373 |
__builtin_unreachable (); |
| 374 |
} |
| 375 |
|
| 376 |
|
| 377 |
/* |
| 378 |
setDefExtension - sets the default extension for definition modules to, ext. |
| 379 |
The string, ext, should be deallocated by the caller at |
| 380 |
an appropriate time. |
| 381 |
*/ |
| 382 |
|
| 383 |
extern "C" void mcSearch_setDefExtension (DynamicStrings_String ext) |
| 384 |
{ |
| 385 |
Def = DynamicStrings_KillString (Def); |
| 386 |
Def = DynamicStrings_Dup (ext); |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
/* |
| 391 |
setModExtension - sets the default extension for implementation and program |
| 392 |
modules to, ext. The string, ext, should be deallocated |
| 393 |
by the caller at an appropriate time. |
| 394 |
*/ |
| 395 |
|
| 396 |
extern "C" void mcSearch_setModExtension (DynamicStrings_String ext) |
| 397 |
{ |
| 398 |
Mod = DynamicStrings_KillString (Mod); |
| 399 |
Mod = DynamicStrings_Dup (ext); |
| 400 |
} |
| 401 |
|
| 402 |
extern "C" void _M2_mcSearch_init (__attribute__((unused)) int argc, __attribute__((unused)) char *argv[], __attribute__((unused)) char *envp[]) |
| 403 |
{ |
| 404 |
Init (); |
| 405 |
} |
| 406 |
|
| 407 |
extern "C" void _M2_mcSearch_fini (__attribute__((unused)) int argc, __attribute__((unused)) char *argv[], __attribute__((unused)) char *envp[]) |
| 408 |
{ |
| 409 |
} |