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,55 +1914,71 @@ 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
} }
} kwsys_stl::vector<kwsys_stl::string> extensions;
} #if defined (_WIN32) || defined(__CYGWIN__) | defined(__MINGW32__)
// See if the executable exists as written. // on windows try .com then .exe
if(SystemTools::FileExists(name) && if(!hasExtension)
!SystemTools::FileIsDirectory(name))
{ {
return SystemTools::CollapseFullPath(name); extensions.push_back(".com");
extensions.push_back(".exe");
} }
if(ext.size()) #endif
kwsys_stl::string tryPath;
// first try the name as it was given (adding extensions
// if needed.)
if(extensions.size())
{ {
kwsys_stl::string tryPath = name; for(kwsys_stl::vector<kwsys_stl::string>::iterator i =
tryPath += ext; extensions.begin(); i != extensions.end(); ++i)
{
tryPath = name;
tryPath += *i;
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());
} }
} }
}
else
{
tryPath = name;
if(SystemTools::FileExists(tryPath.c_str()) &&
!SystemTools::FileIsDirectory(tryPath.c_str()))
{
return SystemTools::CollapseFullPath(tryPath.c_str());
}
}
// now construct the path
kwsys_stl::vector<kwsys_stl::string> path; kwsys_stl::vector<kwsys_stl::string> path;
// Add the system search path to our path. // Add the system search path to our path.
if (!no_system_path) if (!no_system_path)
{ {
SystemTools::GetPath(path); SystemTools::GetPath(path);
} }
// now add the additional paths // now add the additional paths
for(kwsys_stl::vector<kwsys_stl::string>::const_iterator i = userPaths.begin(); for(kwsys_stl::vector<kwsys_stl::string>::const_iterator i =
i != userPaths.end(); ++i) userPaths.begin(); i != userPaths.end(); ++i)
{ {
path.push_back(*i); path.push_back(*i);
} }
// Try each path
for(kwsys_stl::vector<kwsys_stl::string>::iterator p = path.begin(); for(kwsys_stl::vector<kwsys_stl::string>::iterator p = path.begin();
p != path.end(); ++p) p != path.end(); ++p)
{ {
@ -1970,38 +1986,27 @@ kwsys_stl::string SystemTools::FindProgram(
// Remove double quotes from the path on windows // Remove double quotes from the path on windows
SystemTools::ReplaceString(*p, "\"", ""); SystemTools::ReplaceString(*p, "\"", "");
#endif #endif
kwsys_stl::string tryPath = *p; if(extensions.size())
tryPath += "/";
tryPath += name;
if(SystemTools::FileExists(tryPath.c_str()) &&
!SystemTools::FileIsDirectory(tryPath.c_str()))
{ {
return SystemTools::CollapseFullPath(tryPath.c_str()); for(kwsys_stl::vector<kwsys_stl::string>::iterator ext
} = extensions.begin(); ext != extensions.end(); ++ext)
#ifdef _WIN32 {
// on windows try .com before .exe tryPath = *p;
if(ext.size() == 0) tryPath += "/";
{ tryPath += name;
SystemTools::ReplaceString(tryPath, ".exe", ".com"); tryPath += *ext;
SystemTools::ReplaceString(tryPath, ".EXE", ".com"); if(SystemTools::FileExists(tryPath.c_str()) &&
} !SystemTools::FileIsDirectory(tryPath.c_str()))
else {
{ return SystemTools::CollapseFullPath(tryPath.c_str());
tryPath += ".com"; }
} }
if(SystemTools::FileExists(tryPath.c_str()) && }
!SystemTools::FileIsDirectory(tryPath.c_str())) else
{
return SystemTools::CollapseFullPath(tryPath.c_str());
}
#endif
// now try to add ext if it is different than name
if(ext.size())
{ {
tryPath = *p; tryPath = *p;
tryPath += "/"; tryPath += "/";
tryPath += name; tryPath += name;
tryPath += ext;
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 "";
} }