cmSiteNameCommand.cxx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. const char* cacheValue
  23. = m_Makefile->GetDefinition(args[0].c_str());
  24. if(cacheValue)
  25. {
  26. return true;
  27. }
  28. const char *temp = m_Makefile->GetDefinition("HOSTNAME");
  29. std::string hostname_cmd;
  30. if(temp)
  31. {
  32. hostname_cmd = temp;
  33. }
  34. else
  35. {
  36. hostname_cmd = cmSystemTools::FindProgram("hostname");
  37. }
  38. std::string siteName = "unknown";
  39. // try to find the hostname for this computer
  40. if (hostname_cmd.length())
  41. {
  42. std::string host;
  43. cmSystemTools::RunCommand(hostname_cmd.c_str(),
  44. host);
  45. // got the hostname
  46. if (host.length())
  47. {
  48. // remove any white space from the host name
  49. std::string hostRegExp = "[ \t\n\r]*([^\t\n\r ]*)[ \t\n\r]*";
  50. cmRegularExpression hostReg (hostRegExp.c_str());
  51. if (hostReg.find(host.c_str()))
  52. {
  53. // strip whitespace
  54. host = hostReg.match(1);
  55. }
  56. if(host.length())
  57. {
  58. siteName = host;
  59. temp = m_Makefile->GetDefinition("NSLOOKUP");
  60. std::string nslookup_cmd;
  61. if(temp)
  62. {
  63. nslookup_cmd = temp;
  64. }
  65. else
  66. {
  67. nslookup_cmd = cmSystemTools::FindProgram("nslookup");
  68. }
  69. // try to find the domain name for this computer
  70. if (nslookup_cmd.length())
  71. {
  72. nslookup_cmd += " ";
  73. nslookup_cmd += host;
  74. std::string nsOutput;
  75. cmSystemTools::RunCommand(nslookup_cmd.c_str(),
  76. nsOutput);
  77. // got the domain name
  78. if (nsOutput.length())
  79. {
  80. std::string RegExp = ".*Name:[ \t\n]*";
  81. RegExp += host;
  82. RegExp += "\\.([^ \t\n\r]*)[ \t\n\r]*Address:";
  83. cmRegularExpression reg( RegExp.c_str() );
  84. if(reg.find(nsOutput.c_str()))
  85. {
  86. siteName += '.' + cmSystemTools::LowerCase(reg.match(1));
  87. }
  88. }
  89. }
  90. }
  91. }
  92. }
  93. m_Makefile->
  94. AddCacheDefinition(args[0].c_str(),
  95. siteName.c_str(),
  96. "Name of the computer/site where compile is being run",
  97. cmCacheManager::STRING);
  98. return true;
  99. }