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