From 97303baac25799aad893c37fa4158d4a188522b2 Mon Sep 17 00:00:00 2001 From: Enrico Forestieri Date: Wed, 13 May 2009 09:48:11 +0000 Subject: [PATCH] Avoid #ifdef's and use OS specific code. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@29652 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/support/Systemcall.cpp | 22 ++++++++++++---------- src/support/os.h | 10 ++++++++++ src/support/os_cygwin.cpp | 20 ++++++++++++++++++++ src/support/os_unix.cpp | 35 ++++++++++++++++++++++++++++++++++- src/support/os_win32.cpp | 25 +++++++++++++++++++++++++ 5 files changed, 101 insertions(+), 11 deletions(-) diff --git a/src/support/Systemcall.cpp b/src/support/Systemcall.cpp index 281d6d871f..d7b50b0541 100644 --- a/src/support/Systemcall.cpp +++ b/src/support/Systemcall.cpp @@ -60,12 +60,12 @@ int Systemcall::startscript(Starttype how, string const & what) #else QString cmd = QString::fromLocal8Bit(what.c_str()); QProcess * process = new QProcess; -#ifndef _WIN32 - if (isatty(1)) - process->setStandardOutputFile(toqstr("/dev/stdout")); - if (isatty(2)) - process->setStandardErrorFile(toqstr("/dev/stderr")); -#endif + if (os::terminal_output()) { + // Qt won't start the process if we redirect stdout and + // stderr this way, without running in a terminal. + process->setStandardOutputFile(toqstr(os::stdoutdev())); + process->setStandardErrorFile(toqstr(os::stderrdev())); + } process->start(cmd); if (!process->waitForStarted(3000)) { LYXERR0("Qprocess " << cmd << " did not start!"); @@ -92,10 +92,12 @@ int Systemcall::startscript(Starttype how, string const & what) LYXERR0("state " << process->state()); LYXERR0("status " << process->exitStatus()); } -#ifdef _WIN32 - cout << fromqstr(QString::fromLocal8Bit(process->readAllStandardOutput().data())) << endl; - cerr << fromqstr(QString::fromLocal8Bit(process->readAllStandardError().data())) << endl; -#endif + if (!os::terminal_output()) { + // Even if we are not running in a terminal, the output could + // go to some log file, for example ~/.xsession-errors on *nix. + cout << fromqstr(QString::fromLocal8Bit(process->readAllStandardOutput().data())) << endl; + cerr << fromqstr(QString::fromLocal8Bit(process->readAllStandardError().data())) << endl; + } killProcess(process); return exit_code; #endif diff --git a/src/support/os.h b/src/support/os.h index f96fa64384..b5d1fecd80 100644 --- a/src/support/os.h +++ b/src/support/os.h @@ -33,6 +33,16 @@ void init(int argc, char * argv[]); /// Returns the name of the NULL device (/dev/null, null). std::string const & nulldev(); +/// Returns the name of the stdout device (/dev/stdout, /dev/tty, conout$). +std::string const & stdoutdev(); + +/// Returns the name of the stderr device (/dev/stderr, /dev/tty, conout$). +std::string const & stderrdev(); + +/// Tells whether LyX is being run from a terminal and stdout/stderr are +/// not redirected. +bool terminal_output(); + /// Returns "/" on *nix, "C:/", etc on Windows. std::string current_root(); diff --git a/src/support/os_cygwin.cpp b/src/support/os_cygwin.cpp index ea97a29e47..840a11ccc1 100644 --- a/src/support/os_cygwin.cpp +++ b/src/support/os_cygwin.cpp @@ -240,6 +240,26 @@ string const & nulldev() } +string const & stdoutdev() +{ + static string const stdoutdev_ = "/dev/stdout"; + return stdoutdev_; +} + + +string const & stderrdev() +{ + static string const stderrdev_ = "/dev/stderr"; + return stderrdev_; +} + + +bool terminal_output() +{ + return isatty(1) && isatty(2); +} + + shell_type shell() { return UNIX; diff --git a/src/support/os_unix.cpp b/src/support/os_unix.cpp index 4bbd76fe1c..ccc04b69e2 100644 --- a/src/support/os_unix.cpp +++ b/src/support/os_unix.cpp @@ -30,8 +30,23 @@ namespace lyx { namespace support { namespace os { +namespace { + +string stdoutdev_ = "/dev/stdout"; +string stderrdev_ = "/dev/stderr"; + +} // namespace anon + void init(int, char *[]) -{} +{ + // Check whether /dev/stdout and /dev/stderr are available, + // otherwise default to /dev/tty. + if (access(stdoutdev_.c_str(), W_OK) != 0 + || access(stderrdev_.c_str(), W_OK) != 0) { + stdoutdev_ = "/dev/tty"; + stderrdev_ = "/dev/tty"; + } +} string current_root() @@ -130,6 +145,24 @@ string const & nulldev() } +string const & stdoutdev() +{ + return stdoutdev_; +} + + +string const & stderrdev() +{ + return stderrdev_; +} + + +bool terminal_output() +{ + return isatty(1) && isatty(2); +} + + shell_type shell() { return UNIX; diff --git a/src/support/os_win32.cpp b/src/support/os_win32.cpp index b99d21c13b..f17f06de5f 100644 --- a/src/support/os_win32.cpp +++ b/src/support/os_win32.cpp @@ -296,6 +296,31 @@ string const & nulldev() } +string const & stdoutdev() +{ + static string const stdoutdev_ = "conout$"; + return stdoutdev_; +} + + +string const & stderrdev() +{ + static string const stderrdev_ = "conout$"; + return stderrdev_; +} + + +bool terminal_output() +{ + // FIXME: Passing conout$ to Qt fails, most probably for the + // reason explained here: + // http://support.microsoft.com/?scid=kb%3Ben-us%3B90088&x=15&y=15 + // How to convince Qt to open conout$ in FILE_SHARE_WRITE mode? + // For the time being, we assume we are not running in a terminal. + return false; +} + + shell_type shell() { return CMD_EXE;