[Ada] Do not use exponentiation for common bases in floating-point Value
[official-gcc.git] / libcody / resolver.cc
blobb83880f341ee853bf3f97eb86c910ba83e75b438
1 // CODYlib -*- mode:c++ -*-
2 // Copyright (C) 2020 Nathan Sidwell, nathan@acm.org
3 // License: Apache v2.0
5 // Cody
6 #include "internal.hh"
7 // OS
8 #include <fcntl.h>
9 #include <unistd.h>
10 #include <sys/stat.h>
11 #include <sys/types.h>
13 #if (defined (__unix__) \
14 || (defined (__Apple__) \
15 && defined (__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) \
16 && __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 101000))
17 // Autoconf test?
18 #define HAVE_FSTATAT 1
19 #else
20 #define HAVE_FSTATAT 0
21 #endif
23 // Resolver code
25 #if __windows__
26 inline bool IsDirSep (char c)
28 return c == '/' || c == '\\';
30 inline bool IsAbsPath (char const *str)
32 // IIRC windows has the concept of per-drive current directories,
33 // which make drive-using paths confusing. Let's not get into that.
34 return IsDirSep (str)
35 || (((str[0] >= 'A' && str[1] <= 'Z')
36 || (str[0] >= 'a' && str[1] <= 'z'))&& str[1] == ':');
38 #else
39 inline bool IsDirSep (char c)
41 return c == '/';
43 inline bool IsAbsPath (char const *str)
45 return IsDirSep (str[0]);
47 #endif
49 constexpr char DIR_SEPARATOR = '/';
51 constexpr char DOT_REPLACE = ','; // Replace . directories
52 constexpr char COLON_REPLACE = '-'; // Replace : (partition char)
53 constexpr char const REPO_DIR[] = "cmi.cache";
55 namespace Cody {
57 Resolver::~Resolver ()
61 char const *Resolver::GetCMISuffix ()
63 return "cmi";
66 std::string Resolver::GetCMIName (std::string const &module)
68 std::string result;
70 result.reserve (module.size () + 8);
71 bool is_header = false;
72 bool is_abs = false;
74 if (IsAbsPath (module.c_str ()))
75 is_header = is_abs = true;
76 else if (module.front () == '.' && IsDirSep (module.c_str ()[1]))
77 is_header = true;
79 if (is_abs)
81 result.push_back ('.');
82 result.append (module);
84 else
85 result = std::move (module);
87 if (is_header)
89 if (!is_abs)
90 result[0] = DOT_REPLACE;
92 /* Map .. to DOT_REPLACE, DOT_REPLACE. */
93 for (size_t ix = 1; ; ix++)
95 ix = result.find ('.', ix);
96 if (ix == result.npos)
97 break;
98 if (ix + 2 > result.size ())
99 break;
100 if (result[ix + 1] != '.')
101 continue;
102 if (!IsDirSep (result[ix - 1]))
103 continue;
104 if (!IsDirSep (result[ix + 2]))
105 continue;
106 result[ix] = DOT_REPLACE;
107 result[ix + 1] = DOT_REPLACE;
110 else if (COLON_REPLACE != ':')
112 // There can only be one colon in a module name
113 auto colon = result.find (':');
114 if (colon != result.npos)
115 result[colon] = COLON_REPLACE;
118 if (char const *suffix = GetCMISuffix ())
120 result.push_back ('.');
121 result.append (suffix);
124 return result;
127 void Resolver::WaitUntilReady (Server *)
131 Resolver *Resolver::ConnectRequest (Server *s, unsigned version,
132 std::string &, std::string &)
134 if (version > Version)
135 s->ErrorResponse ("version mismatch");
136 else
137 s->ConnectResponse ("default");
139 return this;
142 int Resolver::ModuleRepoRequest (Server *s)
144 s->PathnameResponse (REPO_DIR);
145 return 0;
148 // Deprecated resolver functions
149 int Resolver::ModuleExportRequest (Server *s, Flags, std::string &module)
151 auto cmi = GetCMIName (module);
152 s->PathnameResponse (cmi);
153 return 0;
156 int Resolver::ModuleImportRequest (Server *s, Flags, std::string &module)
158 auto cmi = GetCMIName (module);
159 s->PathnameResponse (cmi);
160 return 0;
163 int Resolver::ModuleCompiledRequest (Server *s, Flags, std::string &)
165 s->OKResponse ();
166 return 0;
169 int Resolver::IncludeTranslateRequest (Server *s, Flags, std::string &include)
171 bool xlate = false;
173 // This is not the most efficient
174 auto cmi = GetCMIName (include);
175 struct stat statbuf;
177 #if HAVE_FSTATAT
178 int fd_dir = open (REPO_DIR, O_RDONLY | O_CLOEXEC | O_DIRECTORY);
179 if (fd_dir >= 0
180 && fstatat (fd_dir, cmi.c_str (), &statbuf, 0) == 0
181 && S_ISREG (statbuf.st_mode))
182 // Sadly can't easily check if this process has read access,
183 // except by trying to open it.
184 xlate = true;
185 if (fd_dir >= 0)
186 close (fd_dir);
187 #else
188 std::string append = REPO_DIR;
189 append.push_back (DIR_SEPARATOR);
190 append.append (cmi);
191 if (stat (append.c_str (), &statbuf) == 0
192 || S_ISREG (statbuf.st_mode))
193 xlate = true;
194 #endif
196 if (xlate)
197 s->PathnameResponse (cmi);
198 else
199 s->BoolResponse (false);
201 return 0;
204 void Resolver::ErrorResponse (Server *server, std::string &&msg)
206 server->ErrorResponse (msg);