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