From 4b7891d873ee690d7db3cc2d64476ac3eef7f263 Mon Sep 17 00:00:00 2001 From: Angus Leeming Date: Wed, 15 Dec 2004 19:35:43 +0000 Subject: [PATCH] OS Abstraction of "HOME" and the null device. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@9376 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/ChangeLog | 8 ++++++++ src/LaTeX.C | 6 +----- src/bufferlist.C | 4 +++- src/frontends/xforms/ChangeLog | 5 +++++ src/frontends/xforms/FormFiledialog.C | 4 +++- src/lyx_main.C | 4 +++- src/support/ChangeLog | 12 ++++++++++++ src/support/filetools.C | 4 ++-- src/support/os.h | 10 +++++++--- src/support/os_os2.C | 25 +++++++++++++++++++++--- src/support/os_unix.C | 24 ++++++++++++++++++++--- src/support/os_win32.C | 28 +++++++++++++++++++++++++++ src/support/path_defines.C.in | 4 ++-- 13 files changed, 117 insertions(+), 21 deletions(-) diff --git a/src/ChangeLog b/src/ChangeLog index c9fbafc9b2..2dba8c2908 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,11 @@ +2004-12-14 Angus Leeming + + * LaTeX.C: (startscript): use os::nulldev() rather than "/dev/null". + + * bufferlist.C (emergencyWrite): + * lyx_main.C (queryUserLyXDir): use os::homepath(), not + GetEnvPath("HOME"). + 2004-12-14 Angus Leeming * main.C: (main): no longer pass pointers to os::init. diff --git a/src/LaTeX.C b/src/LaTeX.C index d63ce35b99..8609c3ed53 100644 --- a/src/LaTeX.C +++ b/src/LaTeX.C @@ -384,11 +384,7 @@ int LaTeX::run(TeXErrors & terr) int LaTeX::startscript() { -#ifndef __EMX__ - string tmp = cmd + ' ' + QuoteName(file) + " > /dev/null"; -#else // cmd.exe (OS/2) causes SYS0003 error at "/dev/null" - string tmp = cmd + ' ' + file + " > nul"; -#endif + string tmp = cmd + ' ' + QuoteName(file) + " > " + os::nulldev(); Systemcall one; return one.startscript(Systemcall::Wait, tmp); } diff --git a/src/bufferlist.C b/src/bufferlist.C index 9eedebeffe..47c4502877 100644 --- a/src/bufferlist.C +++ b/src/bufferlist.C @@ -27,6 +27,7 @@ #include "frontends/Alert.h" #include "support/filetools.h" +#include "support/os.h" #include @@ -55,6 +56,7 @@ using std::vector; using std::back_inserter; using std::transform; +namespace os = lyx::support::os; BufferList::BufferList() {} @@ -309,7 +311,7 @@ void BufferList::emergencyWrite(Buffer * buf) } // 2) In HOME directory. - string s = AddName(GetEnvPath("HOME"), buf->fileName()); + string s = AddName(os::homepath(), buf->fileName()); s += ".emergency"; lyxerr << ' ' << s << endl; if (buf->writeFile(s)) { diff --git a/src/frontends/xforms/ChangeLog b/src/frontends/xforms/ChangeLog index 150bbb222b..a57c27380b 100644 --- a/src/frontends/xforms/ChangeLog +++ b/src/frontends/xforms/ChangeLog @@ -1,3 +1,8 @@ +2004-12-14 Angus Leeming + + * FormFiledialog.C: (FileDlgCB): use os::homepath(), not + GetEnvPath("HOME"). + 2004-12-05 Angus Leeming * pch.h: s@@"lyx_forms.h"@ diff --git a/src/frontends/xforms/FormFiledialog.C b/src/frontends/xforms/FormFiledialog.C index 1c8aec4024..8a67e7192f 100644 --- a/src/frontends/xforms/FormFiledialog.C +++ b/src/frontends/xforms/FormFiledialog.C @@ -25,6 +25,7 @@ #include "support/globbing.h" #include "support/lstrings.h" #include "support/lyxlib.h" +#include "support/os.h" #include "support/tostr.h" #include "lyx_forms.h" @@ -82,6 +83,7 @@ using std::map; using std::vector; using namespace lyx::frontend; +namespace os = lyx::support::os; namespace { @@ -617,7 +619,7 @@ void FileDialog::Private::FileDlgCB(FL_OBJECT *, long arg) break; case 11: // home - current_dlg_->SetDirectory(GetEnvPath("HOME")); + current_dlg_->SetDirectory(os::homepath()); current_dlg_->SetFilters(fl_get_input(file_dlg_form_->PatBox)); current_dlg_->Reread(); break; diff --git a/src/lyx_main.C b/src/lyx_main.C index 879e9c2df6..a4113088c3 100644 --- a/src/lyx_main.C +++ b/src/lyx_main.C @@ -77,6 +77,8 @@ using lyx::support::user_lyxdir; using lyx::support::os::getTmpDir; using lyx::support::os::setTmpDir; +namespace os = lyx::support::os; + using std::endl; using std::string; using std::vector; @@ -552,7 +554,7 @@ void LyX::queryUserLyXDir(bool explicit_userdir) if (!createDirectory(user_lyxdir(), 0755)) { // Failed, let's use $HOME instead. - user_lyxdir(GetEnvPath("HOME")); + user_lyxdir(os::homepath()); lyxerr << bformat(_("Failed. Will use %1$s instead."), user_lyxdir()) << endl; return; diff --git a/src/support/ChangeLog b/src/support/ChangeLog index a3a774dd59..842d35d4ca 100644 --- a/src/support/ChangeLog +++ b/src/support/ChangeLog @@ -1,3 +1,15 @@ +2004-12-14 Angus Leeming + + * os.h, os_os2.C, os_unix.C, os_win32.C: + (binpath, binname, getTmpDir): return a const reference rather than + a copy of the data. + (homepath, nulldev): new functions returning the name of "HOME" and + the null device, respectively. + + * filetools.C: (ExpandPath, MakeDisplayPath): + * path_defines.C.in (setLyxPaths): use os::homepath(), not + GetEnvPath("HOME"). + 2004-12-14 Angus Leeming * os.h, os_{os2,unix,win32}.C (init): change interface to no longer diff --git a/src/support/filetools.C b/src/support/filetools.C index 776f89c113..78149fa4c5 100644 --- a/src/support/filetools.C +++ b/src/support/filetools.C @@ -690,7 +690,7 @@ string const ExpandPath(string const & path) return getcwd() + '/' + RTemp; } if (Temp == "~") { - return GetEnvPath("HOME") + '/' + RTemp; + return os::homepath() + '/' + RTemp; } if (Temp == "..") { return MakeAbsPath(copy); @@ -1111,7 +1111,7 @@ string const MakeDisplayPath(string const & path, unsigned int threshold) { string str = path; - string const home(GetEnvPath("HOME")); + string const home(os::homepath()); // replace /home/blah with ~/ if (prefixIs(str, home)) diff --git a/src/support/os.h b/src/support/os.h index 331d61e9e8..86b44e1bb7 100644 --- a/src/support/os.h +++ b/src/support/os.h @@ -30,13 +30,17 @@ enum shell_type { // do some work just once void init(int argc, char * argv[]); // returns path of LyX binary -std::string binpath(); +std::string const & binpath(); // returns name of LyX binary -std::string binname(); +std::string const & binname(); // void setTmpDir(std::string const & p); // -std::string getTmpDir(); +std::string const & getTmpDir(); +// Returns the user's home directory ($HOME in the unix world). +std::string const & homepath(); +// Returns the name of the NULL device (/dev/null, null). +std::string const & nulldev(); // std::string current_root(); // diff --git a/src/support/os_os2.C b/src/support/os_os2.C index 495a19e5ea..1b7c1465cc 100644 --- a/src/support/os_os2.C +++ b/src/support/os_os2.C @@ -30,6 +30,9 @@ namespace { string binpath_; string binname_; string tmpdir_; +string homepath_; +string nulldev_; + os::shell_type shell_ = os::UNIX; unsigned long cp_ = 0; @@ -86,6 +89,10 @@ void init(int argc, char * argv[]) // CPList[1] == system default codepage, the rest are auxilary. // Once cp_ is correctly set, you can call other routines. cp_ = CPList[1]; + + tmpdir_ = "/tmp"; + homepath_ = GetEnvPath("HOME"); + nulldev_ = "null"; } @@ -217,13 +224,13 @@ char const * popen_read_mode() } -string binpath() +string const & binpath() { return binpath_; } -string binname() +string const & binname() { return binname_; } @@ -235,12 +242,24 @@ void setTmpDir(string const & p) } -string getTmpDir() +string const & getTmpDir() { return tmpdir_; } +string const & homepath() +{ + return homepath_; +} + + +string const & nulldev() +{ + return nulldev_; +} + + shell_type shell() { return shell_; diff --git a/src/support/os_unix.C b/src/support/os_unix.C index 758ac1bfde..1d13cd5086 100644 --- a/src/support/os_unix.C +++ b/src/support/os_unix.C @@ -25,6 +25,8 @@ namespace { string binpath_; string binname_; string tmpdir_; +string homepath_; +string nulldev_; } @@ -55,6 +57,10 @@ void init(int /*argc*/, char * argv[]) if (suffixIs(tmp, "/.libs/")) tmp.erase(tmp.length() - 6, string::npos); binpath_ = tmp; + + tmpdir_ = "/tmp"; + homepath_ = GetEnvPath("HOME"); + nulldev_ = "/dev/null"; } @@ -120,13 +126,13 @@ char const * popen_read_mode() } -string binpath() +string const & binpath() { return binpath_; } -string binname() +string const & binname() { return binname_; } @@ -138,12 +144,24 @@ void setTmpDir(string const & p) } -string getTmpDir() +string const & getTmpDir() { return tmpdir_; } +string const & homepath() +{ + return homepath_; +} + + +string const & nulldev() +{ + return nulldev_; +} + + shell_type shell() { return UNIX; diff --git a/src/support/os_win32.C b/src/support/os_win32.C index 109e5825be..862d743164 100644 --- a/src/support/os_win32.C +++ b/src/support/os_win32.C @@ -32,6 +32,8 @@ namespace { string binpath_; string binname_; string tmpdir_; +string homepath_; +string nulldev_; } @@ -65,6 +67,16 @@ void init(int /* argc */, char * argv[]) if (suffixIs(tmp, "/.libs/")) tmp.erase(tmp.length()-6, string::npos); binpath_ = tmp; + +#ifdef __CYGWIN__ + tmpdir_ = "/tmp"; + homepath_ = GetEnvPath("HOME"); + nulldev_ = "/dev/null"; +#else + tmpdir_ = string(); + homepath_ = GetEnvPath("HOMEDRIVE") + GetEnvPath("HOMEPATH"); + nulldev_ = "nul"; +#endif } @@ -190,9 +202,25 @@ string getTmpDir() } +string const & homepath() +{ + return homepath_; +} + + +string const & nulldev() +{ + return nulldev_; +} + + shell_type shell() { +#ifdef __CYGWIN__ return UNIX; +#else + return CMD_EXE; +#endif } } // namespace os diff --git a/src/support/path_defines.C.in b/src/support/path_defines.C.in index 873773b179..e6b6e55875 100644 --- a/src/support/path_defines.C.in +++ b/src/support/path_defines.C.in @@ -356,10 +356,10 @@ bool setLyxPaths() // default behaviour if (user_lyxdir_.empty()) if (inOSXBundle) - user_lyxdir_ = AddPath(GetEnvPath("HOME"), + user_lyxdir_ = AddPath(os::homepath(), "Library/Preferences/LyX"); else - user_lyxdir_ = AddPath(GetEnvPath("HOME"), + user_lyxdir_ = AddPath(os::homepath(), string(".") + PACKAGE); explicit_userdir = false; } -- 2.39.2