summaryrefslogtreecommitdiffstats
path: root/src/Utils.cxx
blob: 160e13c92761e87527e70ffa441b851260282d1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
  Copyright Kitware, Inc.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/

#include "Utils.h"
#include "Version.h"

#include <cxsys/Process.h>
#include <fstream>
#include <llvm/ADT/SmallString.h>
#include <llvm/Support/FileSystem.h>
#include <llvm/Support/Path.h>
#include <vector>

static std::string castxmlResourceDir;
static std::string castxmlClangResourceDir;

static std::string GetMainExecutable(const char* argv0)
{
  return llvm::sys::fs::getMainExecutable(argv0,
                                          (void*)(intptr_t)GetMainExecutable);
}

static bool tryBuildDir(std::string const& dir)
{
  // Build tree has
  //   <build>/CMakeFiles/castxmlSourceDir.txt
  //   <build>/CMakeFiles/castxmlClangResourceDir.txt
  std::string src_dir_txt = dir + "/CMakeFiles/castxmlSourceDir.txt";
  std::string cl_dir_txt = dir + "/CMakeFiles/castxmlClangResourceDir.txt";
  std::ifstream src_fin(src_dir_txt.c_str());
  std::ifstream cl_fin(cl_dir_txt.c_str());
  std::string src_dir;
  std::string cl_dir;
  if (std::getline(src_fin, src_dir) && llvm::sys::fs::is_directory(src_dir) &&
      std::getline(cl_fin, cl_dir) && llvm::sys::fs::is_directory(cl_dir)) {
    castxmlResourceDir = src_dir + "/share/castxml";
    castxmlClangResourceDir = cl_dir;
    return true;
  }
  return false;
}

bool findResourceDir(const char* argv0, std::ostream& error)
{
  std::string exe = GetMainExecutable(argv0);
  if (!llvm::sys::path::is_absolute(exe)) {
    error << "error: unable to locate " << argv0 << "\n";
    return false;
  }

  // Install tree has
  //   <prefix>/bin/castxml
  //   <prefix>/<CASTXML_INSTALL_DATA_DIR>
  //   <prefix>/<CASTXML_INSTALL_DATA_DIR>/clang
  llvm::SmallString<16> dir(exe);
  llvm::sys::path::remove_filename(dir);
  llvm::sys::path::remove_filename(dir);
  castxmlResourceDir = std::string(dir.str()) + "/" + CASTXML_INSTALL_DATA_DIR;
  castxmlClangResourceDir = castxmlResourceDir + "/clang";
  if (!llvm::sys::fs::is_directory(castxmlResourceDir) ||
      !llvm::sys::fs::is_directory(castxmlClangResourceDir)) {
    llvm::SmallString<16> dir2(dir);
    llvm::sys::path::remove_filename(dir2);
    // Build tree has
    //   <build>/bin[/<config>]/castxml
    if (!tryBuildDir(dir.str()) && !tryBuildDir(dir2.str())) {
      error << "Unable to locate resources for " << exe << "\n";
      return false;
    }
  }

  return true;
}

std::string getResourceDir()
{
  return castxmlResourceDir;
}

std::string getClangResourceDir()
{
  return castxmlClangResourceDir;
}

std::string getVersionString()
{
  return CASTXML_VERSION_STRING;
}

unsigned int getVersionValue()
{
  return (CASTXML_VERSION_MAJOR * 1000000 + CASTXML_VERSION_MINOR * 1000 +
          CASTXML_VERSION_PATCH * 1);
}

bool runCommand(int argc, const char* const* argv, int& ret, std::string& out,
                std::string& err, std::string& msg)
{
  std::vector<const char*> cmd(argv, argv + argc);
  cmd.push_back(0);
  ret = 1;
  out = "";
  err = "";
  std::vector<char> outBuf;
  std::vector<char> errBuf;

  cxsysProcess* cp = cxsysProcess_New();
  cxsysProcess_SetCommand(cp, &*cmd.begin());
  cxsysProcess_SetOption(cp, cxsysProcess_Option_HideWindow, 1);
#ifdef _WIN32
  cxsysProcess_SetPipeFile(cp, cxsysProcess_Pipe_STDIN, "//./nul");
#else
  cxsysProcess_SetPipeFile(cp, cxsysProcess_Pipe_STDIN, "/dev/null");
#endif
  cxsysProcess_Execute(cp);

  char* data;
  int length;
  int pipe;
  while ((pipe = cxsysProcess_WaitForData(cp, &data, &length, 0)) > 0) {
    if (pipe == cxsysProcess_Pipe_STDOUT) {
      outBuf.insert(outBuf.end(), data, data + length);
    } else if (pipe == cxsysProcess_Pipe_STDERR) {
      errBuf.insert(errBuf.end(), data, data + length);
    }
  }

  cxsysProcess_WaitForExit(cp, 0);
  if (!outBuf.empty()) {
    out.append(&*outBuf.begin(), outBuf.size());
  }
  if (!errBuf.empty()) {
    err.append(&*errBuf.begin(), errBuf.size());
  }

  bool result = true;
  switch (cxsysProcess_GetState(cp)) {
    case cxsysProcess_State_Exited:
      ret = cxsysProcess_GetExitValue(cp);
      break;
    case cxsysProcess_State_Exception:
      msg = cxsysProcess_GetExceptionString(cp);
      result = false;
      break;
    case cxsysProcess_State_Error:
      msg = cxsysProcess_GetErrorString(cp);
      result = false;
      break;
    default:
      msg = "Process terminated in unexpected state.\n";
      result = false;
      break;
  }

  cxsysProcess_Delete(cp);
  return result;
}

std::string encodeXML(std::string const& in, bool cdata)
{
  std::string xml;
  const char* last = in.c_str();
  for (const char* c = last; *c; ++c) {
    switch (*c) {
#define XML(OUT)                                                              \
  xml.append(last, c - last);                                                 \
  last = c + 1;                                                               \
  xml.append(OUT)
      case '&':
        XML("&amp;");
        break;
      case '<':
        XML("&lt;");
        break;
      case '>':
        XML("&gt;");
        break;
      case '\'':
        if (!cdata) {
          XML("&apos;");
        }
        break;
      case '"':
        if (!cdata) {
          XML("&quot;");
        }
        break;
      default:
        break;
#undef XML
    }
  }
  xml.append(last);
  return xml;
}

std::string stringReplace(std::string str, std::string const& in,
                          std::string const& out)
{
  std::string::size_type p = 0;
  while ((p = str.find(in, p)) != std::string::npos) {
    str.replace(p, in.size(), out);
    p += out.length();
  }
  return str;
}

#if defined(_WIN32)
#include <windows.h>
#endif

void suppressInteractiveErrors()
{
#if defined(_WIN32)
  SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX);
#endif
}