findcomp.cc 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* Find the correct compiler.
  2. Copyright (C) 2014 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free
  6. Software Foundation; either version 3, or (at your option) any later
  7. version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  11. for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GCC; see the file COPYING3. If not see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <config.h>
  16. #include <string>
  17. #include <dirent.h>
  18. #include <stdlib.h>
  19. #include "libiberty.h"
  20. #include "xregex.h"
  21. #include "findcomp.hh"
  22. class scanner
  23. {
  24. public:
  25. scanner (const std::string &dir)
  26. {
  27. m_dir = opendir (dir.c_str ());
  28. }
  29. ~scanner ()
  30. {
  31. if (m_dir != NULL)
  32. closedir (m_dir);
  33. }
  34. const char *next ()
  35. {
  36. if (m_dir == NULL)
  37. return NULL;
  38. struct dirent *entry = readdir (m_dir);
  39. if (entry == NULL)
  40. return NULL;
  41. return entry->d_name;
  42. }
  43. private:
  44. DIR *m_dir;
  45. };
  46. static bool
  47. search_dir (const regex_t &regexp, const std::string &dir, std::string *result)
  48. {
  49. scanner scan (dir);
  50. const char *filename;
  51. while ((filename = scan.next ()) != NULL)
  52. {
  53. if (regexec (&regexp, filename, 0, NULL, 0) == 0)
  54. {
  55. *result = filename;
  56. return true;
  57. }
  58. }
  59. return false;
  60. }
  61. class tokenizer
  62. {
  63. public:
  64. tokenizer (const char *str)
  65. : m_str (str),
  66. m_pos (0)
  67. {
  68. }
  69. bool done () const
  70. {
  71. return m_pos == std::string::npos;
  72. }
  73. std::string next ()
  74. {
  75. std::string::size_type last_pos = m_pos;
  76. std::string::size_type colon = m_str.find(':', last_pos);
  77. std::string result;
  78. if (colon == std::string::npos)
  79. {
  80. m_pos = colon;
  81. result = m_str.substr(last_pos, colon);
  82. }
  83. else
  84. {
  85. m_pos = colon + 1;
  86. result = m_str.substr(last_pos, colon - last_pos);
  87. }
  88. if (result == "")
  89. result = ".";
  90. return result;
  91. }
  92. private:
  93. std::string m_str;
  94. std::string::size_type m_pos;
  95. };
  96. bool
  97. find_compiler (const regex_t &regexp, std::string *result)
  98. {
  99. const char *cpath = getenv ("PATH");
  100. if (cpath == NULL)
  101. return false;
  102. tokenizer dirs (cpath);
  103. while (!dirs.done ())
  104. {
  105. std::string dir = dirs.next ();
  106. if (search_dir (regexp, dir, result))
  107. return true;
  108. }
  109. return false;
  110. }