From bfebf647eb635f29dc095a14ccbc9fbf25704af5 Mon Sep 17 00:00:00 2001 From: Curle Date: Wed, 6 Dec 2023 18:21:31 +0000 Subject: [PATCH] Optionally search relative to the current source file to find headers. --- src/Importer.c | 27 +++++++++++++++++++++++++-- tests/sieve.er | 2 +- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/Importer.c b/src/Importer.c index 40b7c4f..13f4636 100644 --- a/src/Importer.c +++ b/src/Importer.c @@ -12,6 +12,10 @@ #include #include +#if defined(__GNUC__) || defined(APPLE) +#include +#endif + /** * The function of the importer is to read in definitions from a file, and store * them into the symbol tables. @@ -50,6 +54,10 @@ // Read in the string that we know must be there. char* Module = strdup(CurrentIdentifier); + // Two strategies for finding the module; either it's relative to cwd, or it's relative to source. + bool FoundImport = false; + // Check the cwd first. + // Figure out the working directory char CWD[PATH_MAX]; @@ -65,8 +73,23 @@ // Stat the file to see if it exists struct stat FileInfo; - if (stat(ModulePath, &FileInfo) != 0) - DieMessage("Unable to access the imported module", ModulePath); + if (stat(ModulePath, &FileInfo) != 0) { + free(ModulePath); + char SourcePath[PATH_MAX + 1]; + realpath(CurrentFile->SourceName, SourcePath); + char* SourceFolderLength = strrchr(SourcePath, '/'); + *(SourceFolderLength + 1) = '\0'; + size_t SourcePathLength = strlen(SourcePath); + + ModulePath = malloc(SourcePathLength + sizeof(Module) + 1); + strcpy(ModulePath, SourcePath); + strcpy(ModulePath + SourcePathLength, Module); + + printf("Scanning %s for module definitions.\n", ModulePath); + + if (stat(ModulePath, &FileInfo) != 0) + DieMessage("Unable to access the imported module", ModulePath); + } // At this point, the file exists and we have the path. // Save the current file, so that we can restore it later diff --git a/tests/sieve.er b/tests/sieve.er index 65ec885..792cd70 100644 --- a/tests/sieve.er +++ b/tests/sieve.er @@ -1,4 +1,4 @@ -import "tests/import/defs.eh" +import "import/defs.eh" long num[100]; int :: main() {