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
This commit is contained in:
Enrico Forestieri 2009-05-13 09:48:11 +00:00
parent 50ddeeb77e
commit 97303baac2
5 changed files with 101 additions and 11 deletions

View File

@ -60,12 +60,12 @@ int Systemcall::startscript(Starttype how, string const & what)
#else #else
QString cmd = QString::fromLocal8Bit(what.c_str()); QString cmd = QString::fromLocal8Bit(what.c_str());
QProcess * process = new QProcess; QProcess * process = new QProcess;
#ifndef _WIN32 if (os::terminal_output()) {
if (isatty(1)) // Qt won't start the process if we redirect stdout and
process->setStandardOutputFile(toqstr("/dev/stdout")); // stderr this way, without running in a terminal.
if (isatty(2)) process->setStandardOutputFile(toqstr(os::stdoutdev()));
process->setStandardErrorFile(toqstr("/dev/stderr")); process->setStandardErrorFile(toqstr(os::stderrdev()));
#endif }
process->start(cmd); process->start(cmd);
if (!process->waitForStarted(3000)) { if (!process->waitForStarted(3000)) {
LYXERR0("Qprocess " << cmd << " did not start!"); LYXERR0("Qprocess " << cmd << " did not start!");
@ -92,10 +92,12 @@ int Systemcall::startscript(Starttype how, string const & what)
LYXERR0("state " << process->state()); LYXERR0("state " << process->state());
LYXERR0("status " << process->exitStatus()); LYXERR0("status " << process->exitStatus());
} }
#ifdef _WIN32 if (!os::terminal_output()) {
cout << fromqstr(QString::fromLocal8Bit(process->readAllStandardOutput().data())) << endl; // Even if we are not running in a terminal, the output could
cerr << fromqstr(QString::fromLocal8Bit(process->readAllStandardError().data())) << endl; // go to some log file, for example ~/.xsession-errors on *nix.
#endif cout << fromqstr(QString::fromLocal8Bit(process->readAllStandardOutput().data())) << endl;
cerr << fromqstr(QString::fromLocal8Bit(process->readAllStandardError().data())) << endl;
}
killProcess(process); killProcess(process);
return exit_code; return exit_code;
#endif #endif

View File

@ -33,6 +33,16 @@ void init(int argc, char * argv[]);
/// Returns the name of the NULL device (/dev/null, null). /// Returns the name of the NULL device (/dev/null, null).
std::string const & nulldev(); 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. /// Returns "/" on *nix, "C:/", etc on Windows.
std::string current_root(); std::string current_root();

View File

@ -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() shell_type shell()
{ {
return UNIX; return UNIX;

View File

@ -30,8 +30,23 @@ namespace lyx {
namespace support { namespace support {
namespace os { namespace os {
namespace {
string stdoutdev_ = "/dev/stdout";
string stderrdev_ = "/dev/stderr";
} // namespace anon
void init(int, char *[]) 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() 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() shell_type shell()
{ {
return UNIX; return UNIX;

View File

@ -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() shell_type shell()
{ {
return CMD_EXE; return CMD_EXE;