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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#include "CubitProcess.hpp"<--- Skipping configuration 'DBL_MAX' since the value of 'DBL_MAX' is unknown. Use -D if you want to check it. You can use -U to skip it explicitly.<--- Skipping configuration 'DBL_MIN' since the value of 'DBL_MIN' is unknown. Use -D if you want to check it. You can use -U to skip it explicitly.<--- Skipping configuration 'M_PI' since the value of 'M_PI' is unknown. Use -D if you want to check it. You can use -U to skip it explicitly.
#include "CubitString.hpp"
#include "CubitFileUtil.hpp"
#include "CubitUtil.hpp"
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <sstream>

#ifdef _WIN32
#include <windows.h>
#include <io.h>
#include <direct.h>

const char path_separator = '\\';
const char* path_separator_str = "\\";
#else
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>
#include <limits.h>
const char path_separator = '/';
const char* path_separator_str = "/";
#endif

#ifdef _WIN32
static CubitString quote_string(const CubitString& str)
{
  CubitString ret = "\"";  
  ret += str;
  ret += "\"";
  return ret;
}

static CubitString join_args(const std::vector<CubitString>& strings)
{
  CubitString joined_strings;
  for(unsigned int i=0; i<strings.size(); i++)
  {
    if(i!=0)
      joined_strings += " ";
    
    bool need_quote = strings[i].find(" ") != CubitString::npos;
    if(need_quote)
      joined_strings += "\"";
    joined_strings += strings[i];
    if(need_quote)
      joined_strings += "\"";
  }
  return joined_strings;
}
#endif

// given a relative or absolute path, make it absolute
static CubitString make_path_absolute(const CubitString& p)
{
  CubitString ret;
  if(!p.is_empty())
  {
    if ( CubitFileUtil::is_absolute(p.c_str()) )
    {
      ret = p;
    }
    else
    {
      // if any '/' character is in it, its relative to current directory
      CubitString wd;
      CubitFileUtil::get_current_working_directory(wd);
      ret = wd.c_str();
      ret += path_separator_str;
      ret += p;
    }
  }
  return ret;
}

#ifndef _WIN32
static sigset_t oldsig;
#endif

PidType CubitProcess::start(const CubitString& app, const std::vector<CubitString>& args, bool hide)
{
#ifndef _WIN32
  (void)hide;

  std::vector<const char*> c_args(args.size()+2);
  int idx = 0;
  c_args[idx++] = app.c_str();
  for(unsigned int i=0; i<args.size(); i++)
  {
    c_args[idx++] = args[i].c_str();
  }
  c_args[idx++] = NULL;
  
  CubitString app_real = find_executable(app);

  // temporarily block delivery of child signals
  // TODO: does this overwrite currently set signals for the process?
  // ANSWER: Yes it does, which is why we comment this out because it
  // can prevent the reaping of completed child processes.
  /*
  sigset_t newsig;
  sigemptyset(&newsig);
  sigaddset(&newsig, SIGCHLD);
  sigprocmask(SIG_BLOCK, &newsig, &oldsig);
  */

  pid_t pid = fork();
  if(pid < 0)
    return 0;

  if(pid == 0)
  {
    execv(app_real.c_str(), const_cast<char**>(&c_args[0]));
    perror(app_real.c_str());
    exit(EXIT_FAILURE);
  }

  return pid;
#else

  STARTUPINFOW si;
  PROCESS_INFORMATION pi;
  
  ZeroMemory( &si, sizeof(si) );
  si.cb = sizeof(si);
  ZeroMemory( &pi, sizeof(pi) );
  
  // hide child window
  if(hide)
  {
    si.dwFlags |= STARTF_USESHOWWINDOW;
    si.wShowWindow = SW_HIDE;
  }

  CubitString real_app = find_executable(app);
  real_app.replace("/", "\\");
  
  CubitString call = quote_string(app);
  CubitString joined_args = join_args(args);
  call += " ";
  call += joined_args;

    // Start the child process. 
  if( CreateProcessW( CubitString::toUtf16(real_app).c_str(),  // path to cubit executable 
                      const_cast<wchar_t*>(CubitString::toUtf16(call).c_str()), // Command line. 
                      NULL,      // Process handle not inheritable. 
                      NULL,      // Thread handle not inheritable. 
                      TRUE,     // Set handle inheritance to TRUE.
                      0,         // No creation flags. 
                      NULL,      // Use parent's environment block. 
                      NULL,      // Use parent's starting directory. 
                      &si,       // Pointer to STARTUPINFO structure.
                      &pi )      // Pointer to PROCESS_INFORMATION structure.
      )
  {
    return pi;
  }
  pi.hProcess = 0;
  return pi;
#endif
}

int CubitProcess::wait(PidType pid)
{
#ifndef _WIN32
  int status;
  int result = -1;
  
  while(1)
  {
    int change_pid = waitpid(-1, &status, 0);
    if(change_pid == pid)
    {
      if(WIFEXITED(status))
      {
        result = WEXITSTATUS(status);
        break;
      }
      else if (WIFSIGNALED(status))
      {
        result = 128 + WTERMSIG(status);
        break;
      }
    }
  }
  
  sigprocmask(SIG_SETMASK, &oldsig, NULL);

  return result;
#else
  
  // Wait until child process exits.
  WaitForSingleObject( pid.hProcess, INFINITE );
    
  // Get its exit code
  DWORD exit_code = -1;
  GetExitCodeProcess(pid.hProcess, &exit_code);
    
  // Close process and thread handles. 
  CloseHandle( pid.hProcess );
  CloseHandle( pid.hThread );
  return exit_code;

#endif
}


int CubitProcess::execute(const CubitString& app, const std::vector<CubitString>& args, bool hide)
{
  PidType pid = CubitProcess::start(app, args, hide);
#if _WIN32
  if(pid.hProcess == 0)
    return -1;
#else
  if(pid == 0)
    return -1;
#endif
  return CubitProcess::wait(pid);
}
  
CubitString CubitProcess::find_executable(const CubitString& app)
{
  CubitString app_real = app;

#ifdef _WIN32
  if(!app_real.ends_with(".exe"))
	  app_real += ".exe";
  const char path_delimiter = ';';
#else
  const char path_delimiter = ':';
#endif

#ifdef _WIN32
  if(app_real.get_at(0) != path_separator && 
	  (app_real.length() > 1 && app_real.get_at(1) != ':'))
#else
  if(app_real.get_at(0) != path_separator)
#endif
  {
    if(app_real.find(path_separator_str) != CubitString::npos)
    {
      // if any '/' character is in it, its relative to current directory
      app_real = make_path_absolute(app_real);
    }
    else
    {
      // search PATH env. for this executable (note PATH may have relative directories)
      std::vector<CubitString> paths;
      std::stringstream ss(CubitUtil::getenv("PATH").c_str());
      std::string item;
      while(std::getline(ss, item, path_delimiter))
      {
        paths.push_back(item.c_str());
      }

      for(size_t i=0; i<paths.size(); i++)
      {
        CubitString p = paths[i];
        p += CubitFileUtil::separator();
        p += app_real;
        CubitString abs_p = make_path_absolute(p);
        if(CubitFileUtil::path_exists(abs_p.c_str()))
        {
          app_real = abs_p;
          break;
        }
      }
    }
  }
  return app_real;
}