cmSiteNameCommand.cxx 3.6 KB

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