/* common */
#include "check.h"
#include "coll.h"
+#include "fname.h"
#include "print.h"
#include "strbuf.h"
#include "xmalloc.h"
/* Struct that describes an active input file */
typedef struct AFile AFile;
struct AFile {
- unsigned Line; /* Line number for this file */
- FILE* F; /* Input file stream */
- IFile* Input; /* Points to corresponding IFile */
+ unsigned Line; /* Line number for this file */
+ FILE* F; /* Input file stream */
+ IFile* Input; /* Points to corresponding IFile */
+ int SearchPath; /* True if we've added a path for this file */
};
/* List of all input files */
static AFile* NewAFile (IFile* IF, FILE* F)
-/* Create and return a new AFile */
+/* Create a new AFile, push it onto the stack, add the path of the file to
+ * the path search list, and finally return a pointer to the new AFile struct.
+ */
{
+ StrBuf Path = AUTO_STRBUF_INITIALIZER;
+
/* Allocate a AFile structure */
AFile* AF = (AFile*) xmalloc (sizeof (AFile));
*/
if (IF->Usage++ == 0) {
- /* Get file size and modification time. There a race condition here,
+ /* Get file size and modification time. There a race condition here,
* since we cannot use fileno() (non standard identifier in standard
* header file), and therefore not fstat. When using stat with the
* file name, there's a risk that the file was deleted and recreated
* if a file has changed in the debugger, we will ignore this problem
* here.
*/
- struct stat Buf;
- if (stat (IF->Name, &Buf) != 0) {
- /* Error */
- Fatal ("Cannot stat `%s': %s", IF->Name, strerror (errno));
- }
+ struct stat Buf;
+ if (stat (IF->Name, &Buf) != 0) {
+ /* Error */
+ Fatal ("Cannot stat `%s': %s", IF->Name, strerror (errno));
+ }
IF->Size = (unsigned long) Buf.st_size;
- IF->MTime = (unsigned long) Buf.st_mtime;
+ IF->MTime = (unsigned long) Buf.st_mtime;
- /* Set the debug data */
- g_fileinfo (IF->Name, IF->Size, IF->MTime);
+ /* Set the debug data */
+ g_fileinfo (IF->Name, IF->Size, IF->MTime);
}
/* Insert the new structure into the AFile collection */
CollAppend (&AFiles, AF);
+ /* Get the path of this file and add it as an extra search path.
+ * To avoid file search overhead, we will add one path only once.
+ * This is checked by the PushSearchPath function.
+ */
+ SB_CopyBuf (&Path, IF->Name, FindName (IF->Name) - IF->Name);
+ SB_Terminate (&Path);
+ AF->SearchPath = PushSearchPath (UsrIncSearchPath, SB_GetConstBuf (&Path));
+ SB_Done (&Path);
+
/* Return the new struct */
return AF;
}
/* Delete the last active file from the active file collection */
CollDelete (&AFiles, AFileCount-1);
+ /* If we had added an extra search path for this AFile, remove it */
+ if (Input->SearchPath) {
+ PopSearchPath (UsrIncSearchPath);
+ }
+
/* Delete the active file structure */
FreeAFile (Input);
}
+static void WriteEscaped (FILE* F, const char* Name)
+/* Write a file name to a dependency file escaping spaces */
+{
+ while (*Name) {
+ if (*Name == ' ') {
+ /* Escape spaces */
+ fputc ('\\', F);
+ }
+ fputc (*Name, F);
+ ++Name;
+ }
+}
+
+
+
static void WriteDep (FILE* F, InputType Types)
/* Helper function. Writes all file names that match Types to the output */
{
fputc (' ', F);
}
- /* Print the dependency */
- fputs (IF->Name, F);
+ /* Print the dependency escaping spaces */
+ WriteEscaped (F, IF->Name);
}
}
* all files with the given types there.
*/
{
- const char* Target;
-
/* Open the file */
FILE* F = fopen (Name, "w");
if (F == 0) {
* file name as target, followed by a tab character.
*/
if (SB_IsEmpty (&DepTarget)) {
- Target = OutputFilename;
+ WriteEscaped (F, OutputFilename);
} else {
- Target = SB_GetConstBuf (&DepTarget);
+ WriteEscaped (F, SB_GetConstBuf (&DepTarget));
}
- fprintf (F, "%s:\t", Target);
+ fputs (":\t", F);
/* Write out the dependencies for the output file */
WriteDep (F, Types);