ENH: try to clean up the search for programs

This commit is contained in:
Bill Hoffman 2006-03-11 11:52:57 -05:00
parent 6420337317
commit 08bb4d52ba
1 changed files with 81 additions and 77 deletions

View File

@ -1914,94 +1914,99 @@ kwsys_stl::string SystemTools
* found. Otherwise, the empty string is returned. * found. Otherwise, the empty string is returned.
*/ */
kwsys_stl::string SystemTools::FindProgram( kwsys_stl::string SystemTools::FindProgram(
const char* name, const char* nameIn,
const kwsys_stl::vector<kwsys_stl::string>& userPaths, const kwsys_stl::vector<kwsys_stl::string>& userPaths,
bool no_system_path) bool no_system_path)
{ {
if(!name) if(!nameIn)
{ {
return ""; return "";
} }
kwsys_stl::string ext = SystemTools::GetExecutableExtension(); kwsys_stl::string name = nameIn;
if(ext.size()) bool hasExtension = false;
// check to see if the name already has a .xxx at
// the end of it
if(name.size() > 3 && name[name.size()-4] == '.')
{ {
unsigned int len = strlen(name); hasExtension = true;
if(len > ext.size())
{
if(strcmp(name+(len-ext.size()), ext.c_str()) == 0)
{
ext = ""; // name already has Executable extension
}
}
} }
// See if the executable exists as written. kwsys_stl::vector<kwsys_stl::string> extensions;
if(SystemTools::FileExists(name) && #if defined (_WIN32) || defined(__CYGWIN__) | defined(__MINGW32__)
!SystemTools::FileIsDirectory(name)) // on windows try .com then .exe
if(!hasExtension)
{ {
return SystemTools::CollapseFullPath(name); extensions.push_back(".com");
extensions.push_back(".exe");
} }
if(ext.size())
{
kwsys_stl::string tryPath = name;
tryPath += ext;
if(SystemTools::FileExists(tryPath.c_str()) &&
!SystemTools::FileIsDirectory(tryPath.c_str()))
{
return SystemTools::CollapseFullPath(tryPath.c_str());
}
}
kwsys_stl::vector<kwsys_stl::string> path;
// Add the system search path to our path.
if (!no_system_path)
{
SystemTools::GetPath(path);
}
// now add the additional paths
for(kwsys_stl::vector<kwsys_stl::string>::const_iterator i = userPaths.begin();
i != userPaths.end(); ++i)
{
path.push_back(*i);
}
for(kwsys_stl::vector<kwsys_stl::string>::iterator p = path.begin();
p != path.end(); ++p)
{
#ifdef _WIN32
// Remove double quotes from the path on windows
SystemTools::ReplaceString(*p, "\"", "");
#endif #endif
kwsys_stl::string tryPath = *p; kwsys_stl::string tryPath;
tryPath += "/"; // first try the name as it was given (adding extensions
tryPath += name; // if needed.)
if(SystemTools::FileExists(tryPath.c_str()) && if(extensions.size())
!SystemTools::FileIsDirectory(tryPath.c_str())) {
for(kwsys_stl::vector<kwsys_stl::string>::iterator i =
extensions.begin(); i != extensions.end(); ++i)
{ {
return SystemTools::CollapseFullPath(tryPath.c_str()); tryPath = name;
} tryPath += *i;
#ifdef _WIN32 if(SystemTools::FileExists(tryPath.c_str()) &&
// on windows try .com before .exe !SystemTools::FileIsDirectory(tryPath.c_str()))
if(ext.size() == 0) {
{ return SystemTools::CollapseFullPath(tryPath.c_str());
SystemTools::ReplaceString(tryPath, ".exe", ".com"); }
SystemTools::ReplaceString(tryPath, ".EXE", ".com"); }
} }
else else
{ {
tryPath += ".com"; tryPath = name;
} if(SystemTools::FileExists(tryPath.c_str()) &&
if(SystemTools::FileExists(tryPath.c_str()) && !SystemTools::FileIsDirectory(tryPath.c_str()))
!SystemTools::FileIsDirectory(tryPath.c_str())) {
{ return SystemTools::CollapseFullPath(tryPath.c_str());
return SystemTools::CollapseFullPath(tryPath.c_str()); }
} }
#endif // now construct the path
// now try to add ext if it is different than name kwsys_stl::vector<kwsys_stl::string> path;
if(ext.size()) // Add the system search path to our path.
{ if (!no_system_path)
tryPath = *p; {
tryPath += "/"; SystemTools::GetPath(path);
tryPath += name; }
tryPath += ext; // now add the additional paths
for(kwsys_stl::vector<kwsys_stl::string>::const_iterator i =
userPaths.begin(); i != userPaths.end(); ++i)
{
path.push_back(*i);
}
// Try each path
for(kwsys_stl::vector<kwsys_stl::string>::iterator p = path.begin();
p != path.end(); ++p)
{
#ifdef _WIN32
// Remove double quotes from the path on windows
SystemTools::ReplaceString(*p, "\"", "");
#endif
if(extensions.size())
{
for(kwsys_stl::vector<kwsys_stl::string>::iterator ext
= extensions.begin(); ext != extensions.end(); ++ext)
{
tryPath = *p;
tryPath += "/";
tryPath += name;
tryPath += *ext;
if(SystemTools::FileExists(tryPath.c_str()) &&
!SystemTools::FileIsDirectory(tryPath.c_str()))
{
return SystemTools::CollapseFullPath(tryPath.c_str());
}
}
}
else
{
tryPath = *p;
tryPath += "/";
tryPath += name;
if(SystemTools::FileExists(tryPath.c_str()) && if(SystemTools::FileExists(tryPath.c_str()) &&
!SystemTools::FileIsDirectory(tryPath.c_str())) !SystemTools::FileIsDirectory(tryPath.c_str()))
{ {
@ -2009,7 +2014,6 @@ kwsys_stl::string SystemTools::FindProgram(
} }
} }
} }
// Couldn't find the program. // Couldn't find the program.
return ""; return "";
} }