Page MenuHomePhabricator (Chris)

No OneTemporary

Authored By
Unknown
Size
16 KB
Referenced Files
None
Subscribers
None
diff --git a/src/main.cpp b/src/main.cpp
index 76f84b8..3e56c44 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -1,142 +1,152 @@
#include "darktheme/DarkStyle.h"
#include "ApplicationGlobal.h"
#include "MainWindow.h"
#include "MySettings.h"
#include "common/joinpath.h"
#include "common/misc.h"
#include "main.h"
#include "platform.h"
#include "webclient.h"
#include <QApplication>
#include <QDebug>
#include <QDir>
#include <QMessageBox>
#include <QProxyStyle>
#include <QStandardPaths>
#include <QTranslator>
#include <string>
+#ifdef Q_OS_WIN
+#include "win32/win32.h"
+#endif
+
ApplicationGlobal *global = nullptr;
ApplicationSettings ApplicationSettings::defaultSettings()
{
ApplicationSettings s;
s.proxy_server = "http://squid:3128/";
return s;
}
static bool isHighDpiScalingEnabled()
{
MySettings s;
s.beginGroup("UI");
QVariant v = s.value("EnableHighDpiScaling");
return v.isNull() || v.toBool();
}
+void setEnvironmentVariable(QString const &name, QString const &value);
+
int main(int argc, char *argv[])
{
- putenv((char *)"UNICODEMAP_JP=cp932");
+#ifdef Q_OS_WIN
+ setEnvironmentVariable("UNICODEMAP_JP", "cp932");
+#else
+ setenv("UNICODEMAP_JP", "cp932", 1);
+#endif
ApplicationGlobal g;
global = &g;
global->organization_name = ORGANIZATION_NAME;
global->application_name = APPLICATION_NAME;
global->generic_config_dir = QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation);
global->app_config_dir = global->generic_config_dir / global->organization_name / global->application_name;
global->config_file_path = joinpath(global->app_config_dir, global->application_name + ".ini");
if (!QFileInfo(global->app_config_dir).isDir()) {
QDir().mkpath(global->app_config_dir);
}
if (isHighDpiScalingEnabled()){
#if (QT_VERSION < QT_VERSION_CHECK(5, 6, 0))
qDebug() << "High DPI scaling is not supported";
#else
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif
}
QApplication a(argc, argv);
QApplication::setOrganizationName(global->organization_name);
QApplication::setApplicationName(global->application_name);
qRegisterMetaType<RepositoryItem>("RepositoryItem");
{
MySettings s;
s.beginGroup("UI");
global->language_id = s.value("Language").toString();
global->theme_id = s.value("Theme").toString();
if (global->theme_id.compare("dark", Qt::CaseInsensitive) == 0) {
global->theme = createDarkTheme();
} else {
global->theme = createStandardTheme();
}
s.endGroup();
}
QApplication::setStyle(global->theme->newStyle());
if (QApplication::queryKeyboardModifiers() & Qt::ShiftModifier) {
global->start_with_shift_key = true;
}
WebClient::initialize();
bool f_open_here = false;
QStringList args;
for (int i = 1; i < argc; i++) {
std::string arg = argv[i];
if (arg[0] == '-') {
if (arg == "--open-here") {
f_open_here = true;
}
} else {
args.push_back(QString::fromStdString(arg));
}
}
if (global->app_config_dir.isEmpty()) {
QMessageBox::warning(nullptr, qApp->applicationName(), "Preparation of data storage folder failed.");
return 1;
}
QTranslator translator;
{
if (global->language_id.isEmpty() || global->language_id == "en") {
// thru
} else {
QString path = ":/translations/Guitar_" + global->language_id;
translator.load(path, QApplication::applicationDirPath());
QApplication::installTranslator(&translator);
}
}
MainWindow w;
global->panel_bg_color = w.palette().color(QPalette::Background);
w.setWindowIcon(QIcon(":/image/guitar.png"));
w.show();
w.shown();
if (f_open_here) {
QString dir = QDir::current().absolutePath();
w.autoOpenRepository(dir);
} else if (args.size() == 1) {
QString dir = args[0] / QString();
if (dir.startsWith("./") || dir.startsWith(".\\")) {
dir = QDir::current().absolutePath() / dir.mid(2);
}
QFileInfo fi(dir);
if (fi.isDir()) {
dir = fi.absolutePath();
w.autoOpenRepository(dir);
}
}
return QApplication::exec();
}
diff --git a/src/unix/UnixProcess.cpp b/src/unix/UnixProcess.cpp
index 40d583d..d4fa994 100644
--- a/src/unix/UnixProcess.cpp
+++ b/src/unix/UnixProcess.cpp
@@ -1,337 +1,337 @@
#include "UnixProcess.h"
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <QDebug>
#include <QMutex>
#include <QThread>
#include <deque>
class OutputReaderThread : public QThread {
private:
int fd;
QMutex *mutex;
std::deque<char> *buffer;
protected:
void run() override
{
while (1) {
char buf[1024];
int n = read(fd, buf, sizeof(buf));
if (n < 1) break;
if (buffer) {
QMutexLocker lock(mutex);
buffer->insert(buffer->end(), buf, buf + n);
}
}
}
public:
OutputReaderThread(int fd, QMutex *mutex, std::deque<char> *out)
: fd(fd)
, mutex(mutex)
, buffer(out)
{
}
};
class UnixProcessThread : public QThread {
public:
QMutex *mutex = nullptr;
std::vector<std::string> argvec;
std::vector<char *> args;
std::deque<char> inq;
std::deque<char> outq;
std::deque<char> errq;
bool use_input = false;
int fd_in_read = -1;
int pid = 0;
int exit_code = -1;
bool close_input_later = false;
protected:
public:
void init(QMutex *mutex, bool use_input)
{
this->mutex = mutex;
this->use_input = use_input;
}
void reset()
{
argvec.clear();
args.clear();
inq.clear();
outq.clear();
errq.clear();
use_input = false;
fd_in_read = -1;
pid = 0;
exit_code = -1;
close_input_later = false;
}
protected:
void run() override
{
exit_code = -1;
const int R = 0;
const int W = 1;
const int E = 2;
int stdin_pipe[3] = { -1, -1, -1 };
int stdout_pipe[3] = { -1, -1, -1 };
int stderr_pipe[3] = { -1, -1, -1 };
try {
int fd_out_write;
int fd_err_write;
int pid;
if (pipe(stdin_pipe) < 0) {
throw std::string("failed: pipe");
}
if (pipe(stdout_pipe) < 0) {
throw std::string("failed: pipe");
}
if (pipe(stderr_pipe) < 0) {
throw std::string("failed: pipe");
}
pid = fork();
if (pid < 0) {
throw std::string("failed: fork");
}
if (pid == 0) { // child
- putenv(const_cast<char *>("LANG=C"));
+ setenv("LANG", "C", 1);
close(stdin_pipe[W]);
close(stdout_pipe[R]);
close(stderr_pipe[R]);
dup2(stdin_pipe[R], R);
dup2(stdout_pipe[W], W);
dup2(stderr_pipe[W], E);
close(stdin_pipe[R]);
close(stdout_pipe[W]);
close(stderr_pipe[E]);
if (execvp(args[0], &args[0]) < 0) {
close(stdin_pipe[R]);
close(stdout_pipe[W]);
close(stderr_pipe[E]);
fprintf(stderr, "failed: exec\n");
exit(1);
}
}
close(stdin_pipe[R]);
close(stdout_pipe[W]);
close(stderr_pipe[W]);
fd_in_read = stdin_pipe[W];
fd_out_write = stdout_pipe[R];
fd_err_write = stderr_pipe[R];
//
if (!use_input) {
closeInput();
}
// WriteThread t0(fd_in_read, mutex, &input, &close_input_later);
OutputReaderThread t1(fd_out_write, mutex, &outq);
OutputReaderThread t2(fd_err_write, mutex, &errq);
// if (use_input) t0.start();
t1.start();
t2.start();
while (1) {
QThread::currentThread()->msleep(1);
int status = 0;
if (waitpid(pid, &status, WNOHANG) == pid) {
if (WIFEXITED(status)) {
exit_code = WEXITSTATUS(status);
break;
}
if (WIFSIGNALED(status)) {
exit_code = -1;
break;
}
}
{
QMutexLocker lock(mutex);
int n = inq.size();
if (n > 0) {
while (n > 0) {
char tmp[1024];
int l = n;
if (l > (int)sizeof(tmp)) {
l = sizeof(tmp);
}
std::copy(inq.begin(), inq.begin() + l, tmp);
inq.erase(inq.begin(), inq.begin() + l);
if (fd_in_read != -1) {
int r = write(fd_in_read, tmp, l);
(void)r;
}
n -= l;
}
} else if (close_input_later) {
closeInput();
}
}
}
// if (use_input) {
// t0.requestInterruption();
// t0.wait();
// }
t1.wait();
t2.wait();
close(fd_out_write);
} catch (std::string const &e) {
close(stdin_pipe[R]);
close(stdin_pipe[W]);
close(stdout_pipe[R]);
close(stdout_pipe[W]);
close(stderr_pipe[R]);
close(stderr_pipe[W]);
fprintf(stderr, "%s\n", e.c_str());
exit(1);
}
}
public:
void writeInput(char const *ptr, int len)
{
QMutexLocker lock(mutex);
inq.insert(inq.end(), ptr, ptr + len);
}
void closeInput()
{
if (fd_in_read >= 0) {
close(fd_in_read);
fd_in_read = -1;
}
}
};
struct UnixProcess::Private {
QMutex mutex;
UnixProcessThread th;
};
UnixProcess::UnixProcess()
: m(new Private)
{
}
UnixProcess::~UnixProcess()
{
delete m;
}
void UnixProcess::parseArgs(std::string const &cmd, std::vector<std::string> *out)
{
out->clear();
char const *begin = cmd.c_str();
char const *end = begin + cmd.size();
std::vector<char> tmp;
char const *ptr = begin;
int quote = 0;
while (1) {
int c = 0;
if (ptr < end) {
c = *ptr & 0xff;
}
if (c == '\"' && ptr + 2 < end && ptr[1] == '\"' && ptr[2] == '\"') {
tmp.push_back(c);
ptr += 3;
} else {
if (quote != 0 && c != 0) {
if (c == quote) {
quote = 0;
} else {
tmp.push_back(c);
}
} else if (c == '\"') {
quote = c;
} else if (isspace(c) || c == 0) {
if (!tmp.empty()) {
std::string s(&tmp[0], tmp.size());
out->push_back(s);
}
if (c == 0) break;
tmp.clear();
} else {
tmp.push_back(c);
}
ptr++;
}
}
}
void UnixProcess::start(QString const &command, bool use_input)
{
std::string cmd = command.toStdString();
parseArgs(cmd, &m->th.argvec);
if (!m->th.argvec.empty()) {
for (std::string const &s : m->th.argvec) {
m->th.args.push_back(const_cast<char *>(s.c_str()));
}
m->th.args.push_back(nullptr);
m->th.init(&m->mutex, use_input);
m->th.start();
}
}
int UnixProcess::wait()
{
m->th.wait();
outbytes.clear();
errbytes.clear();
if (!m->th.outq.empty()) outbytes.insert(outbytes.end(), m->th.outq.begin(), m->th.outq.end());
if (!m->th.errq.empty()) errbytes.insert(errbytes.end(), m->th.errq.begin(), m->th.errq.end());
int exit_code = m->th.exit_code;
m->th.reset();
return exit_code;
}
void UnixProcess::writeInput(char const *ptr, int len)
{
m->th.writeInput(ptr, len);
}
void UnixProcess::closeInput(bool justnow)
{
if (justnow) {
m->th.closeInput();
} else {
m->th.close_input_later = true;
}
}
QString UnixProcess::outstring()
{
if (outbytes.empty()) return QString();
std::vector<char> v;
v.insert(v.end(), outbytes.begin(), outbytes.end());
return QString::fromUtf8(&v[0], v.size());
}
QString UnixProcess::errstring()
{
if (errbytes.empty()) return QString();
std::vector<char> v;
v.insert(v.end(), errbytes.begin(), errbytes.end());
return QString::fromUtf8(&v[0], v.size());
}
diff --git a/src/unix/UnixPtyProcess.cpp b/src/unix/UnixPtyProcess.cpp
index 4a187c9..c7e10bb 100644
--- a/src/unix/UnixPtyProcess.cpp
+++ b/src/unix/UnixPtyProcess.cpp
@@ -1,243 +1,242 @@
#include "UnixPtyProcess.h"
#include <unistd.h>
#include <cstdlib>
#include <csignal>
#include <fcntl.h>
#include <termios.h>
#include <sys/ioctl.h>
#include <sys/wait.h>
#include <deque>
#include <QMutex>
#include <QDir>
namespace {
void make_argv(char *command, std::vector<char *> *out)
{
char *dst = command;
char *src = command;
char *arg = command;
bool quote = false;
bool accept = false;
while (1) {
char c = *src;
if (c == '\"') {
if (src[1] == '\"' && src[2] == '\"') {
*dst++ = c;
src += 3;
} else {
quote = !quote;
accept = true;
src++;
}
} else if (quote && c != 0) {
*dst++ = *src++;
} else if (c == 0 || isspace(c & 0xff)) {
*dst++ = 0;
if (accept || *arg) {
out->push_back(arg);
}
if (c == 0) break;
accept = false;
arg = dst;
src++;
} else {
*dst++ = *src++;
}
}
}
class OutputReaderThread : public QThread {
private:
QMutex *mutex;
int pty_master;
std::deque<char> *output_queue;
std::vector<char> *output_vector;
protected:
void run()
{
while (1) {
if (isInterruptionRequested()) break;
char buf[1024];
int len = read(pty_master, buf, sizeof(buf));
if (len < 1) break;
{
QMutexLocker lock(mutex);
output_queue->insert(output_queue->end(), buf, buf + len);
output_vector->insert(output_vector->end(), buf, buf + len);
}
}
}
public:
void start(QMutex *mutex, int pty_master, std::deque<char> *outq, std::vector<char> *outv)
{
this->mutex = mutex;
this->pty_master = pty_master;
this->output_queue = outq;
this->output_vector = outv;
QThread::start();
}
};
} // namespace
// UnixPtyProcess
struct UnixPtyProcess::Private {
QMutex mutex;
std::string command;
int pty_master;
std::deque<char> output_queue;
std::vector<char> output_vector;
OutputReaderThread th_output_reader;
int exit_code = -1;
};
UnixPtyProcess::UnixPtyProcess()
: m(new Private)
{
}
UnixPtyProcess::~UnixPtyProcess()
{
stop();
delete m;
}
bool UnixPtyProcess::isRunning() const
{
return QThread::isRunning();
}
void UnixPtyProcess::writeInput(char const *ptr, int len)
{
int r = write(m->pty_master, ptr, len);
(void)r;
}
int UnixPtyProcess::readOutput(char *ptr, int len)
{
QMutexLocker lock(&m->mutex);
int n = m->output_queue.size();
if (n > len) {
n = len;
}
if (n > 0) {
auto it = m->output_queue.begin();
std::copy(it, it + n, ptr);
m->output_queue.erase(it, it + n);
}
return n;
}
void UnixPtyProcess::start(QString const &cmd, QVariant const &userdata)
{
if (isRunning()) return;
m->command = cmd.toStdString();
user_data = userdata;
QThread::start();
}
bool UnixPtyProcess::wait(unsigned long time)
{
return QThread::wait(time);
}
void UnixPtyProcess::run()
{
struct termios orig_termios;
struct winsize orig_winsize;
tcgetattr(STDIN_FILENO, &orig_termios);
ioctl(STDIN_FILENO, TIOCGWINSZ, (char *)&orig_winsize);
m->pty_master = posix_openpt(O_RDWR);
grantpt(m->pty_master);
unlockpt(m->pty_master);
-
pid_t pid = fork();
if (pid == 0) {
setsid();
- putenv(const_cast<char *>("LANG=C"));
+ setenv("LANG", "C", 1);
char *pts_name = ptsname(m->pty_master);
int pty_slave = open(pts_name, O_RDWR);
close(m->pty_master);
struct termios tio;
memset(&tio, 0, sizeof(tio));
cfmakeraw(&tio);
tio.c_cc[VMIN] = 1;
tio.c_cc[VTIME] = 0;
tio.c_lflag |= ECHO;
tcsetattr(pty_slave, TCSANOW, &tio);
ioctl(pty_slave, TIOCSWINSZ, &orig_winsize);
dup2(pty_slave, STDIN_FILENO);
dup2(pty_slave, STDOUT_FILENO);
dup2(pty_slave, STDERR_FILENO);
close(pty_slave);
QDir::setCurrent(change_dir);
char *command = (char *)alloca(m->command.size() + 1);
strcpy(command, m->command.c_str());
std::vector<char *> argv;
make_argv(command, &argv);
argv.push_back(nullptr);
execvp(argv[0], &argv[0]);
} else {
bool ok = false;
m->th_output_reader.start(&m->mutex, m->pty_master, &m->output_queue, &m->output_vector);
while (1) {
if (isInterruptionRequested()) break;
int status = 0;
int r = waitpid(pid, &status, WNOHANG);
if (r < 0) break;
QThread::currentThread()->msleep(1);
if (r > 0) {
if (WIFEXITED(status)) {
m->exit_code = WEXITSTATUS(status);
ok = true;
break;
}
if (WIFSIGNALED(status)) {
break;
}
}
}
kill(pid, SIGTERM);
m->th_output_reader.requestInterruption();
m->th_output_reader.wait();
close(m->pty_master);
m->pty_master = -1;
emit completed(ok, user_data);
}
}
void UnixPtyProcess::stop()
{
requestInterruption();
wait();
}
int UnixPtyProcess::getExitCode() const
{
return m->exit_code;
}
QString UnixPtyProcess::getMessage() const
{
QString s;
if (!m->output_vector.empty()) {
s = QString::fromUtf8(&m->output_vector[0], m->output_vector.size());
}
return s;
}

File Metadata

Mime Type
text/x-diff
Expires
Tue, Jun 16, 12:24 AM (2 w, 1 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
70463
Default Alt Text
(16 KB)

Event Timeline