cmSiteNameCommand.cxx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmSiteNameCommand.h"
  14. #include <cmsys/RegularExpression.hxx>
  15. // cmSiteNameCommand
  16. bool cmSiteNameCommand::InitialPass(std::vector<std::string> const& args)
  17. {
  18. if(args.size() != 1 )
  19. {
  20. this->SetError("called with incorrect number of arguments");
  21. return false;
  22. }
  23. std::vector<std::string> paths;
  24. paths.push_back("/usr/bsd");
  25. paths.push_back("/usr/sbin");
  26. paths.push_back("/usr/bin");
  27. paths.push_back("/bin");
  28. paths.push_back("/sbin");
  29. paths.push_back("/usr/local/bin");
  30. const char* cacheValue
  31. = m_Makefile->GetDefinition(args[0].c_str());
  32. if(cacheValue)
  33. {
  34. return true;
  35. }
  36. const char *temp = m_Makefile->GetDefinition("HOSTNAME");
  37. std::string hostname_cmd;
  38. if(temp)
  39. {
  40. hostname_cmd = temp;
  41. }
  42. else
  43. {
  44. hostname_cmd = cmSystemTools::FindProgram("hostname", paths);
  45. }
  46. std::string siteName = "unknown";
  47. #if defined(_WIN32) && !defined(__CYGWIN__)
  48. std::string host;
  49. if(cmSystemTools::ReadRegistryValue(
  50. "HKEY_LOCAL_MACHINE\\System\\CurrentControlSet\\Control\\ComputerName\\ComputerName;ComputerName",
  51. host))
  52. {
  53. siteName = host;
  54. }
  55. #else
  56. // try to find the hostname for this computer
  57. if (!cmSystemTools::IsOff(hostname_cmd.c_str()))
  58. {
  59. std::string host;
  60. cmSystemTools::RunSingleCommand(hostname_cmd.c_str(),
  61. &host);
  62. // got the hostname
  63. if (host.length())
  64. {
  65. // remove any white space from the host name
  66. std::string hostRegExp = "[ \t\n\r]*([^\t\n\r ]*)[ \t\n\r]*";
  67. cmsys::RegularExpression hostReg (hostRegExp.c_str());
  68. if (hostReg.find(host.c_str()))
  69. {
  70. // strip whitespace
  71. host = hostReg.match(1);
  72. }
  73. if(host.length())
  74. {
  75. siteName = host;
  76. temp = m_Makefile->GetDefinition("NSLOOKUP");
  77. std::string nslookup_cmd;
  78. if(temp)
  79. {
  80. nslookup_cmd = temp;
  81. }
  82. else
  83. {
  84. nslookup_cmd = cmSystemTools::FindProgram("nslookup", paths);
  85. }
  86. // try to find the domain name for this computer
  87. if (!cmSystemTools::IsOff(nslookup_cmd.c_str()))
  88. {
  89. nslookup_cmd += " ";
  90. nslookup_cmd += host;
  91. std::string nsOutput;
  92. cmSystemTools::RunSingleCommand(nslookup_cmd.c_str(),
  93. &nsOutput);
  94. // got the domain name
  95. if (nsOutput.length())
  96. {
  97. std::string RegExp = ".*Name:[ \t\n]*";
  98. RegExp += host;
  99. RegExp += "\\.([^ \t\n\r]*)[ \t\n\r]*Address:";
  100. cmsys::RegularExpression reg( RegExp.c_str() );
  101. if(reg.find(nsOutput.c_str()))
  102. {
  103. siteName += '.' + cmSystemTools::LowerCase(reg.match(1));
  104. }
  105. }
  106. }
  107. }
  108. }
  109. }
  110. #endif
  111. m_Makefile->
  112. AddCacheDefinition(args[0].c_str(),
  113. siteName.c_str(),
  114. "Name of the computer/site where compile is being run",
  115. cmCacheManager::STRING);
  116. return true;
  117. }