- Added feature to ensure include files come from the right place. The

order in which Scyther searches for files is now.
   1. From the prefix of the previously found file.
   2. Current directory.
   3. Anything in SPDLDIRS
  Here, 1 is new.
- When using -E (--expert), scyther shows any files it reads on stderr.
This commit is contained in:
ccremers 2006-06-11 15:22:20 +00:00
parent 6ac5e2a428
commit 780ca9880f

View File

@ -23,6 +23,8 @@ extern struct tacnode *spdltac;
const char *progname = "scyther"; const char *progname = "scyther";
const char *releasetag = SVNVERSION; const char *releasetag = SVNVERSION;
char *lastfoundprefix = NULL;
// Forward declarations // Forward declarations
void process_environment (void); void process_environment (void);
void process_switches (int commandline); void process_switches (int commandline);
@ -93,12 +95,19 @@ switchesInit (int argc, char **argv)
void void
switchesDone (void) switchesDone (void)
{ {
if (lastfoundprefix != NULL)
free (lastfoundprefix);
} }
//! Open a (protocol) file. //! Open a (protocol) file.
/** /**
* Uses the environment variable SCYTHERDIR to also search for files * Uses the environment variable SCYTHERDIR to also search for files
* *
* If a file was opened before, this is stored in the static char* (initially
* NULL) lastfoundprefix. This prefix can override any other: the point of it is that
* if you find a file in a non-standard location, which then does an include,
* you want this to refer to the directory where you found that last file.
*
* If reopener == NULL then we open a new file pointer, otherwise reopen this one. * If reopener == NULL then we open a new file pointer, otherwise reopen this one.
*/ */
FILE * FILE *
@ -108,6 +117,9 @@ openFileSearch (char *filename, FILE * reopener)
char *dirs; char *dirs;
//! try a filename and a prefix. //! try a filename and a prefix.
/**
* Prefixes don't have to end with "/"; this will be added automatically.
*/
int try (char *prefix) int try (char *prefix)
{ {
char *buffer = NULL; char *buffer = NULL;
@ -125,7 +137,7 @@ openFileSearch (char *filename, FILE * reopener)
buflen = prefixlen + namelen + 1; buflen = prefixlen + namelen + 1;
// Does the prefix end with a slash? (it should) // Does the prefix end with a slash? (it should)
if (nameindex > 0 && prefix[nameindex - 1] != '/') if (prefixlen > 0 && prefix[prefixlen - 1] != '/')
{ {
addslash = true; addslash = true;
buflen++; buflen++;
@ -139,38 +151,65 @@ openFileSearch (char *filename, FILE * reopener)
// Add the slash in the center // Add the slash in the center
if (addslash) if (addslash)
{ buffer[nameindex - 1] = '/';
buffer[nameindex - 1] = '/';
}
// Now try to open it // Now try to open it
if (reopener != NULL) if (reopener != NULL)
{ {
if (freopen (buffer, "r", reopener) != NULL) if (freopen (buffer, "r", reopener) != NULL)
{ result = true;
result = true;
}
} }
else else
{ {
reopener = fopen (buffer, "r"); reopener = fopen (buffer, "r");
if (reopener != NULL) if (reopener != NULL)
{ result = true;
result = true;
}
} }
if (result)
{
// There is a result. Does it have a prefix?
char *ls;
// Non-standard location, maybe we should warn for that
if (switches.expert)
{
globalError++;
eprintf ("Reading file %s.\n", buffer);
globalError--;
}
// Compute the prefix (simply scan for the last slash, if any)
ls = strrchr (buffer, '/');
if (ls != NULL)
{
// Store it for any next includes or something like that
// Clear the old one
if (lastfoundprefix != NULL)
free (lastfoundprefix);
ls[0] = '\0';
lastfoundprefix = buffer;
return true;
}
}
free (buffer); free (buffer);
return result; return result;
} }
// main code. // main code.
if (try ("")) // Try last file prefix (if it exists!)
if (lastfoundprefix != NULL)
{ {
return reopener; if (try (lastfoundprefix))
return reopener;
} }
// Try current directory
if (try (""))
return reopener;
// Now try the environment variable // Now try the environment variable
dirs = getenv ("SCYTHERDIR"); dirs = getenv ("SCYTHERDIR");
while (dirs != NULL) while (dirs != NULL)
@ -179,9 +218,7 @@ openFileSearch (char *filename, FILE * reopener)
{ {
// try this one // try this one
if (try (dirs)) if (try (dirs))
{ return reopener;
return reopener;
}
// skip to next // skip to next
dirs = strpbrk (dirs, separators); dirs = strpbrk (dirs, separators);
if (dirs != NULL) if (dirs != NULL)