git-svn-id: svn://db.shs.com.ru/pip@239 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5

This commit is contained in:
2016-08-24 13:12:49 +00:00
parent 3b6a746644
commit 77a48eb270
133 changed files with 304 additions and 225 deletions

View File

@@ -1,7 +1,7 @@
/*
PIP - Platform Independent Primitives
Process
Copyright (C) 2016 Ivan Pelipenko peri4ko@gmail.com
Copyright (C) 2016 Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@ -19,7 +19,7 @@
#include "piprocess.h"
#ifdef WINDOWS
//# include <.h>
# include <winbase.h>
#else
# include <sys/wait.h>
# include <csignal>
@@ -60,62 +60,27 @@ void PIProcess::exec_() {
}
void PIProcess::terminate() {
#ifdef WINDOWS
if (is_exec) if (!TerminateProcess(pi.hProcess, 0)) return; pi.dwProcessId = 0;
#else
if (is_exec) kill(pid, SIGKILL); pid = 0;
#endif
}
int PIProcess::pID() const {
#ifdef WINDOWS
return pi.dwProcessId;
#else
return pid;
#endif
}
int PIProcess::currentPID() {
#ifdef WINDOWS
return GetCurrentProcessId();
#else
return getpid();
#endif
}
PIStringList PIProcess::currentEnvironment() {
PIStringList l;
int i = 0;
while (environ[i] != 0) {
l << environ[i];
++i;
}
return l;
}
void PIProcess::run() {
void PIProcess::startProc(bool detached) {
//cout << "run" << endl;
PIString str;
/// arguments convertion
as = 0;
int as = 0;
#ifdef WINDOWS
//args.pop_front();
piForeachC (PIString & i, args)
as += i.lengthAscii() + 1;
as += i.lengthAscii() + 3;
char * a = new char[as];
memset(a, ' ', as - 1);
as = 0;
for (int i = 0; i < args.size_s(); ++i) {
str = args[i];
memcpy(&a[as], str.data(), str.lengthAscii());
as += str.length() + 1;
a[as] = '"';
memcpy(&a[as + 1], str.data(), str.lengthAscii());
a[as + str.length() + 1] = '"';
as += str.length() + 3;
}
a[as - 1] = 0;
//piCout << a;
#else
char * a[args.size_s() + 1];
for (int i = 0; i < args.size_s(); ++i) {
@@ -157,10 +122,11 @@ void PIProcess::run() {
str = args.front();
is_exec = true;
execStarted(str);
if (!detached) execStarted(str);
#ifndef WINDOWS
pid = fork();
if (pid == 0) {
int pid_ = fork();
if (!detached) pid = pid_;
if (pid_ == 0) {
#endif
tf_in = tf_out = tf_err = 0;
//cout << "exec " << tf_in << ", " << tf_out << ", " << tf_err << endl;
@@ -179,13 +145,14 @@ void PIProcess::run() {
0, // Process handle not inheritable
0, // Thread handle not inheritable
false, // Set handle inheritance to FALSE
0, // No creation flags
detached ? DETACHED_PROCESS : 0, // Creation flags
0,//e, // Use environment
wd.isEmpty() ? 0 : wd.data(), // Use working directory
&si, // Pointer to STARTUPINFO structure
&pi)) // Pointer to PROCESS_INFORMATION structure
{
WaitForSingleObject(pi.hProcess, INFINITE);
if (!detached) WaitForSingleObject(pi.hProcess, INFINITE);
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
} else
piCoutObj << "\"CreateProcess\" error, " << errorString();
@@ -197,15 +164,18 @@ void PIProcess::run() {
} else {
msleep(1);
//cout << "wait" << endl;
wait(&exit_code);
/*if (tf_in != 0) fclose(tf_in);
if (tf_out != 0) fclose(tf_out);
if (tf_err != 0) fclose(tf_err);*/
pid = 0;
//cout << "wait done" << endl;
if (!detached) {
wait(&exit_code);
/*if (tf_in != 0) fclose(tf_in);
if (tf_out != 0) fclose(tf_out);
if (tf_err != 0) fclose(tf_err);*/
pid_ = 0;
if (!detached) pid = pid_;
//cout << "wait done" << endl;
}
}
#endif
execFinished(str, exit_code);
if (!detached) execFinished(str, exit_code);
is_exec = false;
for (int i = 0; i < env.size_s(); ++i)
delete e[i];
@@ -216,7 +186,56 @@ void PIProcess::run() {
for (int i = 0; i < args.size_s(); ++i)
delete a[i];
#endif
//cout << "end " << tf_in << ", " << tf_out << ", " << tf_err << endl;
}
void PIProcess::terminate() {
#ifdef WINDOWS
if (is_exec) if (!TerminateProcess(pi.hProcess, 0)) return; pi.dwProcessId = 0;
#else
if (is_exec) kill(pid, SIGKILL); pid = 0;
#endif
}
void PIProcess::execIndependent(const PIString & program, const PIStringList & args_) {
PIProcess p;
p.args << program << args_;
p.startProc(true);
}
int PIProcess::pID() const {
#ifdef WINDOWS
return pi.dwProcessId;
#else
return pid;
#endif
}
int PIProcess::currentPID() {
#ifdef WINDOWS
return GetCurrentProcessId();
#else
return getpid();
#endif
}
PIStringList PIProcess::currentEnvironment() {
PIStringList l;
int i = 0;
while (environ[i] != 0) {
l << environ[i];
++i;
}
return l;
}
void PIProcess::run() {
startProc(false);
}