From b5589bf6cf82439dc8dcc6c9465ce3b32dc0f389 Mon Sep 17 00:00:00 2001 From: Angus Leeming Date: Mon, 10 Jan 2005 19:17:43 +0000 Subject: [PATCH] The package reworking. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@9463 a592a061-630c-0410-9148-cb99ea01b6c8 --- config/ChangeLog | 4 + config/lyxinclude.m4 | 31 + configure.ac | 7 + po/POTFILES.in | 1 - src/BufferView_pimpl.C | 8 +- src/ChangeLog | 13 + src/bufferlist.C | 6 +- src/client/ChangeLog | 5 + src/client/messages.C | 10 +- src/exporter.C | 5 +- src/frontends/controllers/ChangeLog | 8 + src/frontends/controllers/ControlAboutlyx.C | 11 +- src/frontends/controllers/ControlGraphics.C | 11 +- src/frontends/controllers/helper_funcs.C | 9 +- src/frontends/controllers/tex_helpers.C | 13 +- src/frontends/gnome/ChangeLog | 5 + src/frontends/gnome/lyx_gui.C | 3 +- src/frontends/gtk/ChangeLog | 6 + src/frontends/gtk/ghelpers.C | 4 +- src/frontends/gtk/lyx_gui.C | 11 +- src/frontends/qt2/ChangeLog | 5 + src/frontends/qt2/lyx_gui.C | 4 +- src/frontends/xforms/ChangeLog | 7 + src/frontends/xforms/FormFiledialog.C | 9 +- src/frontends/xforms/FormPreferences.C | 6 +- src/frontends/xforms/lyx_gui.C | 10 +- src/insets/ChangeLog | 5 + src/insets/ExternalSupport.C | 5 +- src/insets/ExternalTemplate.C | 4 +- src/lyx_cb.C | 19 +- src/lyx_main.C | 106 ++- src/lyxfunc.C | 16 +- src/messages.C | 8 +- src/support/ChangeLog | 23 + src/support/Makefile.am | 25 +- src/support/filetools.C | 56 +- src/support/filetools.h | 9 +- src/support/os.h | 13 +- src/support/os_os2.C | 71 +- src/support/os_unix.C | 80 +-- src/support/os_win32.C | 95 +-- src/support/package.C.in | 696 ++++++++++++++++++++ src/support/package.h | 183 +++++ src/support/path_defines.C.in | 383 ----------- src/support/path_defines.h | 51 -- src/support/tempname.C | 7 +- src/tex2lyx/ChangeLog | 7 +- src/tex2lyx/tex2lyx.C | 16 +- 48 files changed, 1261 insertions(+), 829 deletions(-) create mode 100644 src/support/package.C.in create mode 100644 src/support/package.h delete mode 100644 src/support/path_defines.C.in delete mode 100644 src/support/path_defines.h diff --git a/config/ChangeLog b/config/ChangeLog index 3de6b3923a..a13821f449 100644 --- a/config/ChangeLog +++ b/config/ChangeLog @@ -1,3 +1,7 @@ +2005-01-06 Jean-Marc Lasgouttes + + * lyxinclude.m4 (LYX_USE_PACKAGING): new macro. + 2005-01-05 Lars Gullik Bjonnes * lyxinclude.m4 (enable_stdlib_debug): new switch diff --git a/config/lyxinclude.m4 b/config/lyxinclude.m4 index d1b3b8488f..eb2aa64ecc 100644 --- a/config/lyxinclude.m4 +++ b/config/lyxinclude.m4 @@ -550,6 +550,37 @@ AC_SUBST(FRONTENDS_PROGS) ]) +## Check what kind of packaging should be used at install time. +## The default is autodetected. +AC_DEFUN([LYX_USE_PACKAGING], +[AC_MSG_CHECKING([what packaging should be used]) +AC_ARG_WITH(packaging, + [ --with-packaging=THIS Use THIS packaging for installation: + Possible values: posix, windows, macosx], + [lyx_use_packaging="$withval"], [ + case $host in + *-apple-darwin*) lyx_use_packaging=macosx ;; + *-pc-mingw32*) lyx_use_packaging=windows;; + *) lyx_use_packaging=posix;; + esac]) +AC_MSG_RESULT($lyx_use_packaging) +case $lyx_use_packaging in + macosx) AC_DEFINE(USE_MACOSX_PACKAGING, 1, [Define to 1 if LyX should use a MacOS X application bundle file layout]) + default_prefix="/Applications/LyX.app" + bindir='${prefix}/Contents/MacOS' + datadir='${prefix}/Contents/Resources' + mandir='${prefix}/Contents/Resources/man' ;; + windows) AC_DEFINE(USE_WINDOWS_PACKAGING, 1, [Define to 1 if LyX should use a Windows-style file layout]) + default_prefix="'C:Program Files/LyX'" + bindir='${prefix}/bin' + datadir='${prefix}/Resources' + mandir='${prefix}/Resources/man' ;; + posix) AC_DEFINE(USE_POSIX_PACKAGING, 1, [Define to 1 if LyX should use a POSIX-style file layout]) + default_prefix=$ac_default_prefix ;; + *) LYX_ERROR([Unknown packaging type $lyx_use_packaging]) ;; +esac +]) + ## ------------------------------------------------------------------------ ## Find a file (or one of more files in a list of dirs) diff --git a/configure.ac b/configure.ac index be36a8c8ef..a8a2ffafde 100644 --- a/configure.ac +++ b/configure.ac @@ -227,6 +227,12 @@ dnl qt build will fail without moc or uic esac done +### Check how the files should be packaged +LYX_USE_PACKAGING +# fix the value of the prefixes. +test "x$prefix" = xNONE && prefix=$default_prefix +test "x$exec_prefix" = xNONE && exec_prefix='${prefix}' + ### Setup GNU gettext dnl GNU gettext is written in C AC_LANG_PUSH(C) @@ -310,6 +316,7 @@ VERSION_INFO="Configuration\n\ C++ Compiler flags: ${CPPFLAGS} ${CXXFLAGS}\n\ Linker flags: ${LDFLAGS}\n\ ${FRONTEND_INFO}\ + Packaging: ${lyx_use_packaging}\n\ LyX binary dir: ${real_bindir}\n\ LyX files dir: ${real_datadir}\n" diff --git a/po/POTFILES.in b/po/POTFILES.in index 566e18cd4f..295efd9f15 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -205,7 +205,6 @@ src/output_plaintext.C src/paragraph.C src/rowpainter.C src/support/filefilterlist.C -src/support/path_defines.C.in src/text.C src/text2.C src/text3.C diff --git a/src/BufferView_pimpl.C b/src/BufferView_pimpl.C index 9bf0dadfc6..bc8b3ee148 100644 --- a/src/BufferView_pimpl.C +++ b/src/BufferView_pimpl.C @@ -65,11 +65,11 @@ #include "graphics/Previews.h" +#include "support/convert.h" #include "support/filefilterlist.h" #include "support/filetools.h" #include "support/forkedcontr.h" -#include "support/path_defines.h" -#include "support/convert.h" +#include "support/package.h" #include "support/types.h" #include @@ -86,8 +86,8 @@ using lyx::support::ForkedcallsController; using lyx::support::IsDirWriteable; using lyx::support::MakeDisplayPath; using lyx::support::MakeAbsPath; +using lyx::support::package; using lyx::support::strToUnsignedInt; -using lyx::support::system_lyxdir; using std::endl; using std::istringstream; @@ -779,7 +779,7 @@ void BufferView::Pimpl::MenuInsertLyXFile(string const & filenm) make_pair(string(_("Documents|#o#O")), string(lyxrc.document_path)), make_pair(string(_("Examples|#E#e")), - string(AddPath(system_lyxdir(), "examples")))); + string(AddPath(package().system_support(), "examples")))); FileDialog::Result result = fileDlg.open(initpath, diff --git a/src/ChangeLog b/src/ChangeLog index 51460ea0bc..fe2acaeca1 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,16 @@ +2005-01-10 Angus Leeming + + * Makefile.am: remove the lyx_main.C special casing. + + * BufferView_pimpl.C: + * bufferlist.C: + * exporter.C: + * lyx_cb.C: + * lyx_main.C: + * lyxfunc.C: + * messages.C: use support/package.h to provide the paths to the + various directories used by LyX. + 2005-01-09 Georg Baum * CutAndPaste.C (pasteSelectionHelper): fix bug 1332 (preserve the diff --git a/src/bufferlist.C b/src/bufferlist.C index d5a5e0252f..2ae3df6687 100644 --- a/src/bufferlist.C +++ b/src/bufferlist.C @@ -27,7 +27,7 @@ #include "frontends/Alert.h" #include "support/filetools.h" -#include "support/os.h" +#include "support/package.h" #include @@ -40,6 +40,7 @@ using lyx::support::MakeAbsPath; using lyx::support::MakeDisplayPath; using lyx::support::OnlyFilename; using lyx::support::removeAutosaveFile; +using lyx::support::package; using lyx::support::prefixIs; using boost::bind; @@ -55,7 +56,6 @@ using std::vector; using std::back_inserter; using std::transform; -namespace os = lyx::support::os; BufferList::BufferList() {} @@ -319,7 +319,7 @@ void BufferList::emergencyWrite(Buffer * buf) } // 2) In HOME directory. - string s = AddName(os::homepath(), buf->fileName()); + string s = AddName(package().home_dir(), buf->fileName()); s += ".emergency"; lyxerr << ' ' << s << endl; if (buf->writeFile(s)) { diff --git a/src/client/ChangeLog b/src/client/ChangeLog index c8fecd592b..8417fe70cf 100644 --- a/src/client/ChangeLog +++ b/src/client/ChangeLog @@ -1,3 +1,8 @@ +2005-01-10 Angus Leeming + + * messages.C: use support/package.h to provide the paths to the + various directories used by LyX. + 2004-12-16 Angus Leeming * messages.C: remove redundant "using lyx::support::GetEnvPath;" diff --git a/src/client/messages.C b/src/client/messages.C index a118c733b3..c1b0f8bae3 100644 --- a/src/client/messages.C +++ b/src/client/messages.C @@ -11,10 +11,9 @@ #include "messages.h" #include "support/filetools.h" -#include "support/path_defines.h" - -using lyx::support::lyx_localedir; +#include "support/package.h" +using lyx::support::package; using std::string; @@ -39,7 +38,8 @@ public: //lyxerr << "Messages: language(" << l // << ") in dir(" << dir << ")" << std::endl; - cat_gl = mssg_gl.open(PACKAGE, loc_gl, lyx_localedir().c_str()); + cat_gl = mssg_gl.open(PACKAGE, loc_gl, + package().locale_dir().c_str()); } @@ -94,7 +94,7 @@ public: char * old = strdup(setlocale(LC_ALL, 0)); char * n = setlocale(LC_ALL, lang_.c_str()); - bindtextdomain(PACKAGE, lyx_localedir().c_str()); + bindtextdomain(PACKAGE, package().locale_dir().c_str()); textdomain(PACKAGE); const char* msg = gettext(m.c_str()); setlocale(LC_ALL, old); diff --git a/src/exporter.C b/src/exporter.C index ba090ea8af..7576096d6b 100644 --- a/src/exporter.C +++ b/src/exporter.C @@ -33,7 +33,7 @@ #include "support/FileInfo.h" #include "support/filetools.h" #include "support/lyxlib.h" -#include "support/os.h" +#include "support/package.h" using lyx::support::AddName; using lyx::support::bformat; @@ -43,6 +43,7 @@ using lyx::support::MakeAbsPath; using lyx::support::MakeDisplayPath; using lyx::support::OnlyFilename; using lyx::support::OnlyPath; +using lyx::support::package; using lyx::support::prefixIs; using std::find; @@ -103,7 +104,7 @@ CopyStatus copyFile(string const & format, // overwrite themselves. This check could be changed to // boost::filesystem::equivalent(sourceFile, destFile) if export to // other directories than the document directory is desired. - if (!prefixIs(OnlyPath(sourceFile), lyx::support::os::getTmpDir())) + if (!prefixIs(OnlyPath(sourceFile), package().temp_dir())) return ret; if (!force) { diff --git a/src/frontends/controllers/ChangeLog b/src/frontends/controllers/ChangeLog index d9c4f73e20..f0eec8a4da 100644 --- a/src/frontends/controllers/ChangeLog +++ b/src/frontends/controllers/ChangeLog @@ -1,3 +1,11 @@ +2005-01-10 Angus Leeming + + * ControlAboutlyx.C: + * ControlGraphics.C: + * helper_funcs.C: + * tex_helpers.C: use support/package.h to provide the paths to the + various directories used by LyX. + 2005-01-06 Lars Gullik Bjonnes * ControlTabularCreate.C (dispatchParams): diff --git a/src/frontends/controllers/ControlAboutlyx.C b/src/frontends/controllers/ControlAboutlyx.C index 6591b6f748..b0b7ef1d34 100644 --- a/src/frontends/controllers/ControlAboutlyx.C +++ b/src/frontends/controllers/ControlAboutlyx.C @@ -16,7 +16,7 @@ #include "version.h" #include "support/filetools.h" // FileSearch -#include "support/path_defines.h" +#include "support/package.h" #include #include @@ -30,8 +30,7 @@ namespace lyx { using support::FileSearch; using support::MakeDisplayPath; -using support::system_lyxdir; -using support::user_lyxdir; +using support::package; namespace frontend { @@ -43,7 +42,7 @@ ControlAboutlyx::ControlAboutlyx(Dialog & parent) void ControlAboutlyx::getCredits(ostream & ss) const { - string const name = FileSearch(system_lyxdir(), "CREDITS"); + string const name = FileSearch(package().system_support(), "CREDITS"); bool found(!name.empty()); @@ -90,10 +89,10 @@ string const ControlAboutlyx::getVersion() const << lyx_release_date << "\n" << _("Library directory: ") - << MakeDisplayPath(system_lyxdir()) + << MakeDisplayPath(package().system_support()) << "\n" << _("User directory: ") - << MakeDisplayPath(user_lyxdir()); + << MakeDisplayPath(package().user_support()); return ss.str(); } diff --git a/src/frontends/controllers/ControlGraphics.C b/src/frontends/controllers/ControlGraphics.C index 8335e35713..611afc0910 100644 --- a/src/frontends/controllers/ControlGraphics.C +++ b/src/frontends/controllers/ControlGraphics.C @@ -25,11 +25,11 @@ #include "insets/insetgraphics.h" +#include "support/convert.h" #include "support/FileInfo.h" #include "support/filefilterlist.h" #include "support/filetools.h" -#include "support/path_defines.h" -#include "support/convert.h" +#include "support/package.h" #include "support/types.h" using std::make_pair; @@ -44,9 +44,8 @@ using support::FileFilterList; using support::FileInfo; using support::IsFileReadable; using support::MakeAbsPath; +using support::package; using support::readBB_from_PSFile; -using support::system_lyxdir; -using support::user_lyxdir; namespace frontend { @@ -85,11 +84,11 @@ string const ControlGraphics::browse(string const & in_name) const string const title = _("Select graphics file"); // Does user clipart directory exist? - string clipdir = AddName (user_lyxdir(), "clipart"); + string clipdir = AddName (package().user_support(), "clipart"); FileInfo fileInfo(clipdir); if (!(fileInfo.isOK() && fileInfo.isDir())) // No - bail out to system clipart directory - clipdir = AddName (system_lyxdir(), "clipart"); + clipdir = AddName (package().system_support(), "clipart"); pair dir1(_("Clipart|#C#c"), clipdir); pair dir2(_("Documents|#o#O"), string(lyxrc.document_path)); // Show the file browser dialog diff --git a/src/frontends/controllers/helper_funcs.C b/src/frontends/controllers/helper_funcs.C index 7d9fdb1614..aafd5f3537 100644 --- a/src/frontends/controllers/helper_funcs.C +++ b/src/frontends/controllers/helper_funcs.C @@ -20,7 +20,7 @@ #include "frontends/FileDialog.h" #include "support/filetools.h" -#include "support/path_defines.h" +#include "support/package.h" using std::pair; using std::vector; @@ -40,9 +40,8 @@ using support::MakeAbsPath; using support::MakeRelPath; using support::OnlyFilename; using support::OnlyPath; +using support::package; using support::prefixIs; -using support::system_lyxdir; -using support::user_lyxdir; namespace frontend { @@ -116,10 +115,10 @@ string const browseLibFile(string const & dir, FileFilterList const & filters) { pair const dir1(_("System files|#S#s"), - AddName(system_lyxdir(), dir)); + AddName(package().system_support(), dir)); pair const dir2(_("User files|#U#u"), - AddName(user_lyxdir(), dir)); + AddName(package().user_support(), dir)); string const result = browseFile(LibFileSearch(dir, name, ext), title, filters, false, dir1, dir2); diff --git a/src/frontends/controllers/tex_helpers.C b/src/frontends/controllers/tex_helpers.C index 383464b96b..66583a32b1 100644 --- a/src/frontends/controllers/tex_helpers.C +++ b/src/frontends/controllers/tex_helpers.C @@ -16,10 +16,10 @@ #include "support/filetools.h" #include "support/lstrings.h" -#include "support/systemcall.h" -#include "support/path.h" #include "support/lyxalgo.h" -#include "support/path_defines.h" +#include "support/package.h" +#include "support/path.h" +#include "support/systemcall.h" #include #include @@ -34,12 +34,11 @@ using support::GetFileContents; using support::getVectorFromString; using support::LibFileSearch; using support::OnlyFilename; -using support::Path; +using support::package; using support::Path; using support::split; using support::Systemcall; using support::token; -using support::user_lyxdir; namespace frontend { @@ -48,7 +47,7 @@ namespace frontend { void rescanTexStyles() { // Run rescan in user lyx directory - Path p(user_lyxdir()); + Path p(package().user_support()); Systemcall one; one.startscript(Systemcall::Wait, LibFileSearch("scripts", "TeXFiles.sh")); @@ -58,7 +57,7 @@ void rescanTexStyles() void texhash() { // Run texhash in user lyx directory - Path p(user_lyxdir()); + Path p(package().user_support()); //path to texhash through system Systemcall one; diff --git a/src/frontends/gnome/ChangeLog b/src/frontends/gnome/ChangeLog index 2bb1ab2900..6422099642 100644 --- a/src/frontends/gnome/ChangeLog +++ b/src/frontends/gnome/ChangeLog @@ -1,3 +1,8 @@ +2005-01-10 Angus Leeming + + * lyx_gui.C: use support/package.h to provide the paths to the + various directories used by LyX. + 2005-01-04 Jürgen Spitzmüller * lyx_gui.C: change getDVI() to int. diff --git a/src/frontends/gnome/lyx_gui.C b/src/frontends/gnome/lyx_gui.C index eac418cb20..4c9e3460ab 100644 --- a/src/frontends/gnome/lyx_gui.C +++ b/src/frontends/gnome/lyx_gui.C @@ -16,7 +16,7 @@ #include "support/lyxlib.h" #include "support/os.h" #include "support/filetools.h" -#include "support/path_defines.h" +#include "support/package.h" #include "debug.h" #include "gettext.h" @@ -160,7 +160,6 @@ void lyx_gui::parse_init(int & argc, char * argv[]) if (!display) { lyxerr << "LyX: unable to access X display, exiting" << endl; - os::warn("Unable to access X display, exiting"); ::exit(1); } diff --git a/src/frontends/gtk/ChangeLog b/src/frontends/gtk/ChangeLog index 4123144369..047230d818 100644 --- a/src/frontends/gtk/ChangeLog +++ b/src/frontends/gtk/ChangeLog @@ -1,3 +1,9 @@ +2005-01-10 Angus Leeming + + * ghelpers.C: + * lyx_gui.C: use support/package.h to provide the paths to the + various directories used by LyX. + 2005-01-06 Lars Gullik Bjonnes * GParagraph.C: drop unused include tostr.h diff --git a/src/frontends/gtk/ghelpers.C b/src/frontends/gtk/ghelpers.C index 2491e1721d..7d0d85c43e 100644 --- a/src/frontends/gtk/ghelpers.C +++ b/src/frontends/gtk/ghelpers.C @@ -23,7 +23,7 @@ #include "debug.h" #include "support/filetools.h" -#include "support/path_defines.h" +#include "support/package.h" using std::string; using std::vector; @@ -144,7 +144,7 @@ string const findGladeFile(string const & name) // Second, search in the src tree. string const dir = - lyx::support::AddPath(lyx::support::top_srcdir(), + lyx::support::AddPath(lyx::support::package().top_srcdir(), "src/frontends/gtk/glade"); filename = lyx::support::ChangeExtension(name, ".glade"); diff --git a/src/frontends/gtk/lyx_gui.C b/src/frontends/gtk/lyx_gui.C index 0d940337e0..64f4affea9 100644 --- a/src/frontends/gtk/lyx_gui.C +++ b/src/frontends/gtk/lyx_gui.C @@ -54,7 +54,7 @@ #include "support/lyxlib.h" #include "support/os.h" #include "support/filetools.h" -#include "support/path_defines.h" +#include "support/package.h" #include @@ -72,6 +72,8 @@ namespace os = lyx::support::os; using std::ostringstream; using std::string; +using lyx::support::package; + using lyx::frontend::colorCache; using lyx::frontend::GView; using lyx::frontend::XformsColor; @@ -181,7 +183,6 @@ void parse_init_xforms(int & argc, char * argv[]) if (!display) { lyxerr << "LyX: unable to access X display, exiting" << std::endl; - lyx::support::os::warn("Unable to access X display, exiting"); ::exit(1); } @@ -212,8 +213,8 @@ void lyx_gui::parse_init(int & argc, char * argv[]) void parse_lyxrc_xforms() { - XformsColor::read(lyx::support::AddName( - lyx::support::user_lyxdir(), "preferences.xform")); + XformsColor::read(lyx::support::AddName(package().user_support(), + "preferences.xform")); if (lyxrc.popup_font_encoding.empty()) lyxrc.popup_font_encoding = lyxrc.font_norm; @@ -345,7 +346,7 @@ void lyx_gui::start(string const & batch, std::vector const & files) lyxserver = new LyXServer(&view.getLyXFunc(), lyxrc.lyxpipes); lyxsocket = new LyXServerSocket(&view.getLyXFunc(), - os::internal_path(os::getTmpDir() + "/lyxsocket")); + os::internal_path(package().temp_dir() + "/lyxsocket")); for_each(files.begin(), files.end(), bind(&BufferView::loadLyXFile, view.view(), _1, true)); diff --git a/src/frontends/qt2/ChangeLog b/src/frontends/qt2/ChangeLog index d2f5825366..3e13694d47 100644 --- a/src/frontends/qt2/ChangeLog +++ b/src/frontends/qt2/ChangeLog @@ -1,3 +1,8 @@ +2005-01-10 Angus Leeming + + * lyx_gui.C: use support/package.h to provide the paths to the + various directories used by LyX. + 2005-01-07 Ruurd Reitsma * qfont_metrics.C (ascent, descent): correct the metrics returned diff --git a/src/frontends/qt2/lyx_gui.C b/src/frontends/qt2/lyx_gui.C index ad5a739347..f5230da557 100644 --- a/src/frontends/qt2/lyx_gui.C +++ b/src/frontends/qt2/lyx_gui.C @@ -29,6 +29,7 @@ #include "support/lstrings.h" #include "support/os.h" +#include "support/package.h" #include "debug.h" // Dear Lord, deliver us from Evil, aka the Qt headers @@ -55,6 +56,7 @@ #include using lyx::support::ltrim; +using lyx::support::package; using lyx::frontend::QtView; @@ -229,7 +231,7 @@ void start(string const & batch, vector const & files) lyxserver = new LyXServer(&view.getLyXFunc(), lyxrc.lyxpipes); lyxsocket = new LyXServerSocket(&view.getLyXFunc(), - os::internal_path(os::getTmpDir() + "/lyxsocket")); + os::internal_path(package().temp_dir() + "/lyxsocket")); for_each(files.begin(), files.end(), bind(&BufferView::loadLyXFile, view.view(), _1, true)); diff --git a/src/frontends/xforms/ChangeLog b/src/frontends/xforms/ChangeLog index ea6cc1f3d9..a8861acd86 100644 --- a/src/frontends/xforms/ChangeLog +++ b/src/frontends/xforms/ChangeLog @@ -1,3 +1,10 @@ +2005-01-10 Angus Leeming + + * FormFiledialog.C: + * FormPreferences.C: + * lyx_gui.C: use support/package.h to provide the paths to the + various directories used by LyX. + 2005-01-06 Lars Gullik Bjonnes * xforms_helpers.C: diff --git a/src/frontends/xforms/FormFiledialog.C b/src/frontends/xforms/FormFiledialog.C index 7cea65f8f4..4f90128219 100644 --- a/src/frontends/xforms/FormFiledialog.C +++ b/src/frontends/xforms/FormFiledialog.C @@ -19,14 +19,14 @@ #include "frontends/Dialogs.h" +#include "support/convert.h" #include "support/FileInfo.h" #include "support/filefilterlist.h" #include "support/filetools.h" #include "support/globbing.h" #include "support/lstrings.h" #include "support/lyxlib.h" -#include "support/os.h" -#include "support/convert.h" +#include "support/package.h" #include "lyx_forms.h" @@ -69,6 +69,7 @@ using lyx::support::getcwd; using lyx::support::LyXReadLink; using lyx::support::MakeAbsPath; using lyx::support::OnlyFilename; +using lyx::support::package; using lyx::support::split; using lyx::support::subst; using lyx::support::suffixIs; @@ -82,7 +83,7 @@ using std::map; using std::vector; using namespace lyx::frontend; -namespace os = lyx::support::os; + namespace { @@ -617,7 +618,7 @@ void FileDialog::Private::FileDlgCB(FL_OBJECT *, long arg) break; case 11: // home - current_dlg_->SetDirectory(os::homepath()); + current_dlg_->SetDirectory(package().home_dir()); current_dlg_->SetFilters(fl_get_input(file_dlg_form_->PatBox)); current_dlg_->Reread(); break; diff --git a/src/frontends/xforms/FormPreferences.C b/src/frontends/xforms/FormPreferences.C index 4593bddf5f..c79c643253 100644 --- a/src/frontends/xforms/FormPreferences.C +++ b/src/frontends/xforms/FormPreferences.C @@ -29,7 +29,7 @@ #include "lyxfont.h" #include "support/lstrings.h" -#include "support/path_defines.h" +#include "support/package.h" #include "support/filetools.h" #include "lyx_forms.h" @@ -50,10 +50,10 @@ namespace lyx { using support::AddName; using support::ChangeExtension; +using support::package; using support::rtrim; using support::strToDbl; using support::trim; -using support::user_lyxdir; namespace frontend { @@ -305,7 +305,7 @@ void FormPreferences::apply() // The "Save" button has been pressed. if (dialog().isClosing() && colors_.modifiedXformsPrefs) { string const filename = - AddName(user_lyxdir(), "preferences.xform"); + AddName(package().user_support(), "preferences.xform"); colors_.modifiedXformsPrefs = !XformsColor::write(filename); } } diff --git a/src/frontends/xforms/lyx_gui.C b/src/frontends/xforms/lyx_gui.C index fcf67db923..9b19a07309 100644 --- a/src/frontends/xforms/lyx_gui.C +++ b/src/frontends/xforms/lyx_gui.C @@ -36,7 +36,7 @@ #include "support/filetools.h" #include "support/lyxlib.h" #include "support/os.h" -#include "support/path_defines.h" +#include "support/package.h" #include "lyx_forms.h" @@ -48,7 +48,7 @@ #include using lyx::support::AddName; -using lyx::support::user_lyxdir; +using lyx::support::package; using lyx::frontend::fontloader; using lyx::frontend::getRGBColor; @@ -179,7 +179,6 @@ void parse_init(int & argc, char * argv[]) if (!display) { lyxerr << "LyX: unable to access X display, exiting" << endl; - os::warn("Unable to access X display, exiting"); ::exit(1); } @@ -204,7 +203,8 @@ void parse_init(int & argc, char * argv[]) void parse_lyxrc() { - XformsColor::read(AddName(user_lyxdir(), "preferences.xform")); + XformsColor::read(AddName(package().user_support(), + "preferences.xform")); if (lyxrc.popup_font_encoding.empty()) lyxrc.popup_font_encoding = lyxrc.font_norm; @@ -302,7 +302,7 @@ void start(string const & batch, vector const & files) lyxserver = new LyXServer(&view->getLyXFunc(), lyxrc.lyxpipes); lyxsocket = new LyXServerSocket(&view->getLyXFunc(), - os::internal_path(os::getTmpDir() + "/lyxsocket")); + os::internal_path(package().temp_dir() + "/lyxsocket")); for_each(files.begin(), files.end(), bind(&BufferView::loadLyXFile, view->view(), _1, true)); diff --git a/src/insets/ChangeLog b/src/insets/ChangeLog index a73f5cdf18..5a20dfe6cb 100644 --- a/src/insets/ChangeLog +++ b/src/insets/ChangeLog @@ -1,3 +1,8 @@ +2005-01-10 Angus Leeming + + * ExternalTemplate.C: use support/package.h to provide the paths to the + various directories used by LyX. + 2005-01-06 Lars Gullik Bjonnes * insetwrap.C: diff --git a/src/insets/ExternalSupport.C b/src/insets/ExternalSupport.C index 510b02c439..081520a24d 100644 --- a/src/insets/ExternalSupport.C +++ b/src/insets/ExternalSupport.C @@ -28,8 +28,8 @@ #include "support/lstrings.h" #include "support/lyxalgo.h" #include "support/lyxlib.h" +#include "support/package.h" #include "support/path.h" -#include "support/path_defines.h" #include "support/std_ostream.h" @@ -109,7 +109,8 @@ string const doSubstitution(InsetExternalParams const & params, relToParentPath); } result = support::subst(result, "$$Tempname", params.tempname()); - result = support::subst(result, "$$Sysdir", support::system_lyxdir()); + result = support::subst(result, "$$Sysdir", + support::package().system_support()); // Handle the $$Contents(filename) syntax if (support::contains(result, "$$Contents(\"")) { diff --git a/src/insets/ExternalTemplate.C b/src/insets/ExternalTemplate.C index 482a0663db..1976677594 100644 --- a/src/insets/ExternalTemplate.C +++ b/src/insets/ExternalTemplate.C @@ -18,8 +18,8 @@ #include "support/filetools.h" #include "support/lstrings.h" +#include "support/package.h" #include "support/path.h" -#include "support/path_defines.h" namespace support = lyx::support; @@ -54,7 +54,7 @@ Template::Format::Format() TemplateManager::TemplateManager() { - readTemplates(support::user_lyxdir()); + readTemplates(support::package().user_support()); if (lyxerr.debugging(Debug::EXTERNAL)) { dumpPreambleDefs(lyxerr); lyxerr << '\n'; diff --git a/src/lyx_cb.C b/src/lyx_cb.C index 211bae8df7..24dbdb8afc 100644 --- a/src/lyx_cb.C +++ b/src/lyx_cb.C @@ -39,9 +39,8 @@ #include "support/filetools.h" #include "support/forkedcall.h" #include "support/lyxlib.h" -#include "support/os.h" +#include "support/package.h" #include "support/path.h" -#include "support/path_defines.h" #include "support/systemcall.h" #include @@ -62,17 +61,14 @@ using lyx::support::MakeDisplayPath; using lyx::support::OnlyFilename; using lyx::support::OnlyPath; using lyx::support::Path; +using lyx::support::package; using lyx::support::QuoteName; using lyx::support::removeAutosaveFile; using lyx::support::rename; using lyx::support::split; -using lyx::support::system_lyxdir; using lyx::support::Systemcall; using lyx::support::tempName; using lyx::support::unlink; -using lyx::support::user_lyxdir; - -namespace os = lyx::support::os; using boost::shared_ptr; @@ -205,11 +201,11 @@ void QuitLyX() bufferlist.closeAll(); // do any other cleanup procedures now - lyxerr[Debug::INFO] << "Deleting tmp dir " << os::getTmpDir() << endl; + lyxerr[Debug::INFO] << "Deleting tmp dir " << package().temp_dir() << endl; - if (destroyDir(os::getTmpDir()) != 0) { + if (destroyDir(package().temp_dir()) != 0) { string msg = bformat(_("Could not remove the temporary directory %1$s"), - os::getTmpDir()); + package().temp_dir()); Alert::warning(_("Could not remove temporary directory"), msg); } @@ -444,8 +440,9 @@ void Reconfigure(BufferView * bv) bv->owner()->message(_("Running configure...")); // Run configure in user lyx directory - Path p(user_lyxdir()); - string const configure_script = AddName(system_lyxdir(), "configure"); + Path p(package().user_support()); + string const configure_script = + AddName(package().system_support(), "configure"); string const configure_command = "sh " + QuoteName(configure_script); Systemcall one; one.startscript(Systemcall::Wait, configure_command); diff --git a/src/lyx_main.C b/src/lyx_main.C index 15cbee94a5..4e09a29474 100644 --- a/src/lyx_main.C +++ b/src/lyx_main.C @@ -49,8 +49,8 @@ #include "support/filetools.h" #include "support/lyxlib.h" #include "support/os.h" +#include "support/package.h" #include "support/path.h" -#include "support/path_defines.h" #include @@ -67,15 +67,10 @@ using lyx::support::FileSearch; using lyx::support::GetEnv; using lyx::support::i18nLibFileSearch; using lyx::support::LibFileSearch; +using lyx::support::package; using lyx::support::Path; using lyx::support::QuoteName; using lyx::support::rtrim; -using lyx::support::setLyxPaths; -using lyx::support::system_lyxdir; -using lyx::support::user_lyxdir; - -using lyx::support::os::getTmpDir; -using lyx::support::os::setTmpDir; namespace os = lyx::support::os; @@ -102,6 +97,12 @@ boost::scoped_ptr toplevel_keymap; namespace { +// Filled with the command line arguments "foo" of "-sysdir foo" or +// "-userdir foo". +string cl_system_support; +string cl_user_support; + + void showFileError(string const & error) { Alert::warning(_("Could not read configuration file"), @@ -189,6 +190,8 @@ void LyX::priv_exec(int & argc, char * argv[]) // we need to parse for "-dbg" and "-help" bool const want_gui = easyParse(argc, argv); + lyx::support::init_package(argv[0], cl_system_support, cl_user_support); + if (want_gui) lyx_gui::parse_init(argc, argv); @@ -340,12 +343,36 @@ void LyX::init(bool gui) signal(SIGTERM, error_handler); // SIGPIPE can be safely ignored. - bool const explicit_userdir = setLyxPaths(); +#if defined (USE_MACOSX_PACKAGING) + // Set PATH for LyX/Mac + // + // LyX/Mac is a relocatable application bundle; here we add to + // the PATH so it can find binaries like reLyX inside its own + // application bundle, and also append PATH elements that it + // needs to run latex, previewers, etc. + string oldpath = GetEnv("PATH"); + string newpath = "PATH=" + oldpath + ":" + package().binary_dir() + ":"; + newpath += "/sw/bin:/usr/local/bin:" + "/usr/local/teTeX/bin/powerpc-apple-darwin-current"; + PutEnv(newpath); + lyxerr[Debug::INIT] << "Running from LyX/Mac bundle. " + "Setting PATH to: " << GetEnv("PATH") << endl; +#endif + + // Set the locale_dir. + string const & locale_dir = package().locale_dir(); + FileInfo fi(locale_dir); + if (fi.isOK() && fi.isDir()) { + lyxerr[Debug::INIT] + << "Setting locale directory to " + << locale_dir << endl; + //gettext_init(locale_dir); + } // Check that user LyX directory is ok. We don't do that if // running in batch mode. if (gui) { - queryUserLyXDir(explicit_userdir); + queryUserLyXDir(package().explicit_user_support()); } else { first_start = false; } @@ -353,12 +380,16 @@ void LyX::init(bool gui) // Disable gui when easyparse says so lyx_gui::use_gui = gui; + lyxrc.tempdir_path = package().temp_dir(); + lyxrc.document_path = package().document_dir(); + if (lyxrc.template_path.empty()) { - lyxrc.template_path = AddPath(system_lyxdir(), "templates"); + lyxrc.template_path = AddPath(package().system_support(), + "templates"); } if (lyxrc.lastfiles.empty()) { - lyxrc.lastfiles = AddName(user_lyxdir(), "lastfiles"); + lyxrc.lastfiles = AddName(package().user_support(), "lastfiles"); } if (lyxrc.roman_font_name.empty()) @@ -406,8 +437,10 @@ void LyX::init(bool gui) if (lyxerr.debugging(Debug::LYXRC)) lyxrc.print(); - setTmpDir(createLyXTmpDir(lyxrc.tempdir_path)); - if (getTmpDir().empty()) { + package().document_dir() = lyxrc.document_path; + + package().temp_dir() = createLyXTmpDir(lyxrc.tempdir_path); + if (package().temp_dir().empty()) { Alert::error(_("Could not create temporary directory"), bformat(_("Could not create a temporary directory in\n" "%1$s. Make sure that this\n" @@ -422,7 +455,7 @@ void LyX::init(bool gui) } if (lyxerr.debugging(Debug::INIT)) { - lyxerr << "LyX tmp dir: `" << getTmpDir() << '\'' << endl; + lyxerr << "LyX tmp dir: `" << package().temp_dir() << '\'' << endl; } lyxerr[Debug::INIT] << "Reading lastfiles `" @@ -528,20 +561,20 @@ void LyX::deadKeyBindings(kb_keymap * kbmap) void LyX::queryUserLyXDir(bool explicit_userdir) { - string const configure_script = AddName(system_lyxdir(), "configure"); + string const configure_script = AddName(package().system_support(), "configure"); string const configure_command = "sh " + QuoteName(configure_script); // Does user directory exist? - FileInfo fileInfo(user_lyxdir()); + FileInfo fileInfo(package().user_support()); if (fileInfo.isOK() && fileInfo.isDir()) { first_start = false; FileInfo script(configure_script); - FileInfo defaults(AddName(user_lyxdir(), "lyxrc.defaults")); + FileInfo defaults(AddName(package().user_support(), "lyxrc.defaults")); if (defaults.isOK() && script.isOK() && defaults.getModificationTime() < script.getModificationTime()) { lyxerr << _("LyX: reconfiguring user directory") << endl; - Path p(user_lyxdir()); + Path p(package().user_support()); ::system(configure_command.c_str()); lyxerr << "LyX: " << _("Done!") << endl; } @@ -550,19 +583,34 @@ void LyX::queryUserLyXDir(bool explicit_userdir) first_start = !explicit_userdir; - lyxerr << bformat(_("LyX: Creating directory %1$s" - " and running configure..."), user_lyxdir()) << endl; + // If the user specified explicitly a directory, ask whether + // to create it. If the user says "no", then exit. + if (explicit_userdir && + !Alert::prompt( + _("Missing LyX support directory"), + bformat(_("You have specified a non-existent user " + "LyX directory, %1$s.\n" + "It is needed to keep your own configuration."), + package().user_support()), + 1, 0, + _("&Create directory."), + _("&Exit LyX."))) { + lyxerr << _("No user LyX directory. Exiting.") << endl; + exit(1); + } - if (!createDirectory(user_lyxdir(), 0755)) { - // Failed, let's use $HOME instead. - user_lyxdir(os::homepath()); - lyxerr << bformat(_("Failed. Will use %1$s instead."), - user_lyxdir()) << endl; - return; + lyxerr << bformat(_("LyX: Creating directory %1$s" + " and running configure..."), package().user_support()) << endl; + + if (!createDirectory(package().user_support(), 0755)) { + // Failed, so let's exit. + lyxerr << _("Failed to create directory. Exiting.") + << endl; + exit(1); } // Run configure in user lyx directory - Path p(user_lyxdir()); + Path p(package().user_support()); ::system(configure_command.c_str()); lyxerr << "LyX: " << _("Done!") << endl; } @@ -763,7 +811,7 @@ int parse_sysdir(string const & arg, string const &) lyxerr << _("Missing directory for -sysdir switch") << endl; exit(1); } - system_lyxdir(arg); + cl_system_support = arg; return 1; } @@ -773,7 +821,7 @@ int parse_userdir(string const & arg, string const &) lyxerr << _("Missing directory for -userdir switch") << endl; exit(1); } - user_lyxdir(arg); + cl_user_support = arg; return 1; } diff --git a/src/lyxfunc.C b/src/lyxfunc.C index 7188aa5774..9753450d6f 100644 --- a/src/lyxfunc.C +++ b/src/lyxfunc.C @@ -84,7 +84,7 @@ #include "support/forkedcontr.h" #include "support/lstrings.h" #include "support/path.h" -#include "support/path_defines.h" +#include "support/package.h" #include "support/systemcall.h" #include "support/convert.h" #include "support/os.h" @@ -108,6 +108,7 @@ using lyx::support::IsFileReadable; using lyx::support::isStrInt; using lyx::support::MakeAbsPath; using lyx::support::MakeDisplayPath; +using lyx::support::package; using lyx::support::Path; using lyx::support::QuoteName; using lyx::support::rtrim; @@ -115,13 +116,10 @@ using lyx::support::split; using lyx::support::strToInt; using lyx::support::strToUnsignedInt; using lyx::support::subst; -using lyx::support::system_lyxdir; using lyx::support::Systemcall; using lyx::support::token; using lyx::support::trim; -using lyx::support::user_lyxdir; using lyx::support::prefixIs; -using lyx::support::os::getTmpDir; using std::endl; using std::make_pair; @@ -1069,7 +1067,7 @@ void LyXFunc::dispatch(FuncRequest const & cmd) int row; istringstream is(argument); is >> file_name >> row; - if (prefixIs(file_name, getTmpDir())) { + if (prefixIs(file_name, package().temp_dir())) { // Needed by inverse dvi search. If it is a file // in tmpdir, call the apropriated function view()->setBuffer(bufferlist.getBufferFromTmp(file_name)); @@ -1294,7 +1292,7 @@ void LyXFunc::dispatch(FuncRequest const & cmd) } case LFUN_SAVEPREFERENCES: { - Path p(user_lyxdir()); + Path p(package().user_support()); lyxrc.write("preferences", false); break; } @@ -1398,7 +1396,7 @@ void LyXFunc::dispatch(FuncRequest const & cmd) case LFUN_SAVE_AS_DEFAULT: { string const fname = - AddName(AddPath(user_lyxdir(), "templates/"), + AddName(AddPath(package().user_support(), "templates/"), "defaults.lyx"); Buffer defaults(fname); @@ -1661,7 +1659,7 @@ void LyXFunc::open(string const & fname) make_pair(string(_("Documents|#o#O")), string(lyxrc.document_path)), make_pair(string(_("Examples|#E#e")), - string(AddPath(system_lyxdir(), "examples")))); + string(AddPath(package().system_support(), "examples")))); FileDialog::Result result = fileDlg.open(initpath, @@ -1737,7 +1735,7 @@ void LyXFunc::doImport(string const & argument) make_pair(string(_("Documents|#o#O")), string(lyxrc.document_path)), make_pair(string(_("Examples|#E#e")), - string(AddPath(system_lyxdir(), "examples")))); + string(AddPath(package().system_support(), "examples")))); string const filter = formats.prettyName(format) + " (*." + formats.extension(format) + ')'; diff --git a/src/messages.C b/src/messages.C index f3ddd12eab..d523af7626 100644 --- a/src/messages.C +++ b/src/messages.C @@ -11,11 +11,11 @@ #include "messages.h" #include "support/filetools.h" -#include "support/path_defines.h" +#include "support/package.h" #include -using lyx::support::lyx_localedir; +using lyx::support::package; using std::string; @@ -41,7 +41,7 @@ public: //lyxerr << "Messages: language(" << l // << ") in dir(" << dir << ")" << std::endl; - cat_gl = mssg_gl.open(PACKAGE, loc_gl, lyx_localedir().c_str()); + cat_gl = mssg_gl.open(PACKAGE, loc_gl, package().locale_dir().c_str()); } @@ -96,7 +96,7 @@ public: char * old = strdup(setlocale(LC_ALL, 0)); char * n = setlocale(LC_ALL, lang_.c_str()); - bindtextdomain(PACKAGE, lyx_localedir().c_str()); + bindtextdomain(PACKAGE, package().locale_dir().c_str()); textdomain(PACKAGE); const char* msg = gettext(m.c_str()); // Some english words have different translations, depending diff --git a/src/support/ChangeLog b/src/support/ChangeLog index 59d37379e5..d54b7ba2f9 100644 --- a/src/support/ChangeLog +++ b/src/support/ChangeLog @@ -1,3 +1,26 @@ +2005-01-10 Angus Leeming + + * os.h: + * os_os2.C: + * os_unix.C: + * os_win32.C (binpath, binname, homepath, setTmpDir, getTmpDir, warn): + removed. Remove also all code to set the associated data variables. + + * package.{C.in,h}: new files to ascertain the paths to the + various directories used by LyX. Does nothing with these paths, + just determines the strings themselves. + + * path_defines.{C.in,h}: removed. + + * Makefile.am: remove path_defines.{C.in,h}. Add package.{C.in,h}. + + * filetools.C[Ch] (GetEnvPath): remove. + (getEnvPath): replacement. Returns a vector of paths. + + * filetools.C: + * tempname.C: use support/package.h to provide the paths to the + various directories used by LyX. + 2005-01-07 Angus Leeming * convert.C (string>): add specialization for long. diff --git a/src/support/Makefile.am b/src/support/Makefile.am index ad8291ee34..de6adecb9c 100644 --- a/src/support/Makefile.am +++ b/src/support/Makefile.am @@ -4,13 +4,13 @@ noinst_LTLIBRARIES = libsupport.la CLEANFILES += path_defines.C -EXTRA_DIST = path_defines.C.in os_unix.C os_win32.C os_os2.C +EXTRA_DIST = package.C.in os_unix.C os_win32.C os_os2.C if USE_COMPRESSION COMPRESSION = gzstream.C gzstream.h endif -BUILT_SOURCES = path_defines.C +BUILT_SOURCES = package.C AM_CPPFLAGS = $(PCH_FLAGS) -I$(srcdir)/.. $(BOOST_INCLUDES) @@ -60,8 +60,8 @@ libsupport_la_SOURCES = \ os2_errortable.h \ path.C \ path.h \ - path_defines.C \ - path_defines.h \ + package.C \ + package.h \ putenv.C \ rename.C \ rmdir.C \ @@ -80,20 +80,19 @@ libsupport_la_SOURCES = \ unlink.C -path_defines.C: build_path_defines +package.C: build_package -build_path_defines: path_defines.C.in - @rm -f tmp_path_defines ;\ +build_package: package.C.in + @rm -f tmp_package ;\ ABS_SRCDIR=`cd $(top_srcdir) && pwd` ;\ ABS_BUILDDIR=`cd ../.. && pwd` ;\ sed "s,%LYX_DIR%,$(pkgdatadir), ;\ s,%LOCALEDIR%,$(datadir)/locale, ;\ - s,%BUILDDIR%,$${ABS_BUILDDIR}, ;\ s,%TOP_SRCDIR%,$${ABS_SRCDIR}," \ - $(srcdir)/path_defines.C.in > tmp_path_defines ;\ - if cmp -s tmp_path_defines path_defines.C ; then \ - rm -f tmp_path_defines ;\ + $(srcdir)/package.C.in > tmp_package ;\ + if cmp -s tmp_package package.C ; then \ + rm -f tmp_package ;\ else \ - rm -f path_defines.C ;\ - mv tmp_path_defines path_defines.C ;\ + rm -f package.C ;\ + mv tmp_package package.C ;\ fi diff --git a/src/support/filetools.C b/src/support/filetools.C index 2f220470e2..5a354a6096 100644 --- a/src/support/filetools.C +++ b/src/support/filetools.C @@ -27,8 +27,8 @@ #include "support/lstrings.h" #include "support/FileInfo.h" #include "support/forkedcontr.h" +#include "support/package.h" #include "support/path.h" -#include "support/path_defines.h" #include "support/lyxlib.h" #include "support/os.h" @@ -38,6 +38,7 @@ #include #include +#include #include @@ -188,8 +189,10 @@ string const FileOpenSearch(string const & path, string const & name, path_element = os::internal_path(path_element); if (!suffixIs(path_element, '/')) path_element+= '/'; - path_element = subst(path_element, "$$LyX", system_lyxdir()); - path_element = subst(path_element, "$$User", user_lyxdir()); + path_element = subst(path_element, "$$LyX", + package().system_support()); + path_element = subst(path_element, "$$User", + package().user_support()); real_file = FileSearch(path_element, name, ext); @@ -294,16 +297,18 @@ string const FileSearch(string const & path, string const & name, string const LibFileSearch(string const & dir, string const & name, string const & ext) { - string fullname = FileSearch(AddPath(user_lyxdir(), dir), name, ext); + string fullname = FileSearch(AddPath(package().user_support(), dir), + name, ext); if (!fullname.empty()) return fullname; - if (!build_lyxdir().empty()) - fullname = FileSearch(AddPath(build_lyxdir(), dir), name, ext); + if (!package().build_support().empty()) + fullname = FileSearch(AddPath(package().build_support(), dir), + name, ext); if (!fullname.empty()) return fullname; - return FileSearch(AddPath(system_lyxdir(), dir), name, ext); + return FileSearch(AddPath(package().system_support(), dir), name, ext); } @@ -389,6 +394,30 @@ string const GetEnv(string const & envname) } +vector const getEnvPath(string const & name) +{ + typedef boost::char_separator Separator; + typedef boost::tokenizer Tokenizer; + +#if defined (__EMX__) || defined (_WIN32) + Separator const separator(";"); +#else + Separator const separator(":"); +#endif + + string const env_var = GetEnv(name); + Tokenizer const tokens(env_var, separator); + Tokenizer::const_iterator it = tokens.begin(); + Tokenizer::const_iterator const end = tokens.end(); + + std::vector vars; + for (; it != end; ++it) + vars.push_back(os::internal_path(*it)); + + return vars; +} + + string const GetEnvPath(string const & name) { #ifndef __EMX__ @@ -495,9 +524,12 @@ string const createBufferTmpDir() { static int count; // We are in our own directory. Why bother to mangle name? - // In fact I wrote this code to circumvent a problematic behaviour (bug?) - // of EMX mkstemp(). - string const tmpfl = os::getTmpDir() + "/lyx_tmpbuf" + convert(count++); + // In fact I wrote this code to circumvent a problematic behaviour + // (bug?) of EMX mkstemp(). + string const tmpfl = + package().temp_dir() + "/lyx_tmpbuf" + + convert(count++); + if (mkdir(tmpfl, 0777)) { lyxerr << "LyX could not create the temporary directory '" << tmpfl << "'" << endl; @@ -690,7 +722,7 @@ string const ExpandPath(string const & path) return getcwd() + '/' + RTemp; } if (Temp == "~") { - return os::homepath() + '/' + RTemp; + return package().home_dir() + '/' + RTemp; } if (Temp == "..") { return MakeAbsPath(copy); @@ -1111,7 +1143,7 @@ string const MakeDisplayPath(string const & path, unsigned int threshold) { string str = path; - string const home(os::homepath()); + string const home(package().home_dir()); // replace /home/blah with ~/ if (prefixIs(str, home)) diff --git a/src/support/filetools.h b/src/support/filetools.h index 6b4c4b1edf..29069f88b8 100644 --- a/src/support/filetools.h +++ b/src/support/filetools.h @@ -115,8 +115,13 @@ std::string const LibScriptSearch(std::string const & command); /// std::string const GetEnv(std::string const & envname); -/// A helper function. -std::string const GetEnvPath(std::string const & name); +/** Return the contents of the environment variable \c name, + * split using the OS-dependent token separating elements. + * Each element is then passed through os::internal_path to + * guarantee that it is in the form of a unix-stype path. + * If the environment variable is not set, then returns an empty vector. + */ +std::vector const getEnvPath(std::string const & name); /// Substitutes active latex characters with underscores in filename std::string const MakeLatexName(std::string const & file); diff --git a/src/support/os.h b/src/support/os.h index bb4a023139..2aae334d34 100644 --- a/src/support/os.h +++ b/src/support/os.h @@ -29,16 +29,7 @@ enum shell_type { // do some work just once void init(int argc, char * argv[]); -// returns path of LyX binary -std::string const & binpath(); -// returns name of LyX binary -std::string const & binname(); -// -void setTmpDir(std::string const & p); -// -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(); // @@ -56,8 +47,6 @@ bool is_absolute_path(std::string const & p); // returns a string suitable to be passed to popen when // same for popen(). char const * popen_read_mode(); -// -void warn(std::string const & mesg); } // namespace os } // namespace support diff --git a/src/support/os_os2.C b/src/support/os_os2.C index ac4b33e98f..9a42ebeecb 100644 --- a/src/support/os_os2.C +++ b/src/support/os_os2.C @@ -21,27 +21,19 @@ #define INCL_DOSERRORS #include -#include -using boost::scoped_array; +namespace lyx { +namespace support { +namespace os { namespace { -string binpath_; -string binname_; -string tmpdir_; -string homepath_; -string nulldev_; - -os::shell_type shell_ = os::UNIX; +shell_type shell_ = UNIX; unsigned long cp_ = 0; } -namespace os { - - void init(int argc, char * argv[]) { _wildcard(&argc, &argv); @@ -50,16 +42,6 @@ void init(int argc, char * argv[]) APIRET rc = DosGetInfoBlocks(&ptib, &ppib); if (rc != NO_ERROR) exit(rc); - scoped_array tmp(new char[256]); - // This is the only reliable way to retrieve the executable name. - rc = DosQueryModuleName(ppib->pib_hmte, 256L, tmp); - if (rc != NO_ERROR) - exit(rc); - string p = tmp.get(); - p = internal_path(p); - binname_ = OnlyFilename(p); - binname_.erase(binname_.length()-4, string::npos); - binpath_ = OnlyPath(p); // OS/2 cmd.exe has another use for '&' string sh = OnlyFilename(GetEnvPath("EMXSHELL")); @@ -89,16 +71,6 @@ 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"; -} - - -void warn(string const & /*mesg*/) -{ - return; } @@ -218,38 +190,9 @@ char const * popen_read_mode() } -string const & binpath() -{ - return binpath_; -} - - -string const & binname() -{ - return binname_; -} - - -void setTmpDir(string const & p) -{ - tmpdir_ = p; -} - - -string const & getTmpDir() -{ - return tmpdir_; -} - - -string const & homepath() -{ - return homepath_; -} - - string const & nulldev() { + static string const nulldev_ = "null"; return nulldev_; } @@ -259,4 +202,6 @@ shell_type shell() return shell_; } -} // end namespace os +} // namespace os +} // namespace support +} // namespace lyx diff --git a/src/support/os_unix.C b/src/support/os_unix.C index 53594714e3..b5d67f34e3 100644 --- a/src/support/os_unix.C +++ b/src/support/os_unix.C @@ -13,61 +13,16 @@ #include #include "support/os.h" -#include "support/filetools.h" -#include "support/lstrings.h" - using std::string; -namespace { - -string binpath_; -string binname_; -string tmpdir_; -string homepath_; -string nulldev_; - -} - namespace lyx { namespace support { namespace os { -void init(int /*argc*/, char * argv[]) -{ - static bool initialized = false; - if (initialized) - return; - initialized = true; - - string tmp = argv[0]; - binname_ = OnlyFilename(tmp); - tmp = ExpandPath(tmp); // This expands ./ and ~/ - if (!is_absolute_path(tmp)) { - string binsearchpath = GetEnvPath("PATH"); - // This will make "src/lyx" work always :-) - binsearchpath += ";."; - tmp = FileOpenSearch(binsearchpath, tmp); - } - - tmp = MakeAbsPath(OnlyPath(tmp)); - - // In case we are running in place and compiled with shared libraries - if (suffixIs(tmp, "/.libs/")) - tmp.erase(tmp.length() - 6, string::npos); - binpath_ = tmp; - - tmpdir_ = "/tmp"; - homepath_ = GetEnvPath("HOME"); - nulldev_ = "/dev/null"; -} - - -void warn(string const & /*mesg*/) -{ - return; -} +void init(int, char *[]) +{} string current_root() @@ -120,38 +75,9 @@ char const * popen_read_mode() } -string const & binpath() -{ - return binpath_; -} - - -string const & binname() -{ - return binname_; -} - - -void setTmpDir(string const & p) -{ - tmpdir_ = p; -} - - -string const & getTmpDir() -{ - return tmpdir_; -} - - -string const & homepath() -{ - return homepath_; -} - - string const & nulldev() { + static string const nulldev_ = "/dev/null"; return nulldev_; } diff --git a/src/support/os_win32.C b/src/support/os_win32.C index b7c48ae1a4..386246949e 100644 --- a/src/support/os_win32.C +++ b/src/support/os_win32.C @@ -13,8 +13,6 @@ #include #include "support/os.h" -#include "support/filetools.h" -#include "support/lstrings.h" #include "debug.h" @@ -28,69 +26,16 @@ # include // _getdrive #endif -using namespace lyx::support; using std::endl; using std::string; -namespace { - -string binpath_; -string binname_; -string tmpdir_; -string homepath_; -string nulldev_; - -} - - namespace lyx { namespace support { namespace os { -void init(int /* argc */, char * argv[]) -{ - static bool initialized = false; - if (initialized) - return; - initialized = true; - - string tmp = internal_path(argv[0]); - binname_ = OnlyFilename(tmp); - tmp = ExpandPath(tmp); // This expands ./ and ~/ - - if (!is_absolute_path(tmp)) { - string binsearchpath = GetEnvPath("PATH"); - // This will make "src/lyx" work always :-) - binsearchpath += ";."; - tmp = internal_path(argv[0]); - tmp = FileOpenSearch(binsearchpath, tmp); - } - - tmp = MakeAbsPath(OnlyPath(tmp)); - - // In case we are running in place and compiled with shared libraries - if (suffixIs(tmp, "/.libs/")) - tmp.erase(tmp.length()-6, string::npos); - binpath_ = tmp; - -#if defined(__CYGWIN__) || defined(__CYGWIN32__) - tmpdir_ = "/tmp"; - homepath_ = GetEnvPath("HOME"); - nulldev_ = "/dev/null"; -#else - tmpdir_ = string(); - homepath_ = GetEnvPath("HOMEDRIVE") + GetEnvPath("HOMEPATH"); - nulldev_ = "nul"; -#endif -} - - -void warn(string const & mesg) -{ - MessageBox(0, mesg.c_str(), "LyX error", - MB_OK|MB_ICONSTOP|MB_SYSTEMMODAL); -} +void init(int, char *[]) +{} string current_root() @@ -129,6 +74,7 @@ string::size_type common_path(string const & p1, string const & p2) string external_path(string const & p) { string dos_path; + #if defined(__CYGWIN__) || defined(__CYGWIN32__) // Translate from cygwin path syntax to dos path syntax if (is_absolute_path(p)) { @@ -196,38 +142,13 @@ char const * popen_read_mode() } -string const & binpath() -{ - return binpath_; -} - - -string const & binname() -{ - return binname_; -} - - -void setTmpDir(string const & p) -{ - tmpdir_ = p; -} - - -string const & getTmpDir() -{ - return tmpdir_; -} - - -string const & homepath() -{ - return homepath_; -} - - string const & nulldev() { +#if defined(__CYGWIN__) || defined(__CYGWIN32__) + static string const nulldev_ = "/dev/null"; +#else + static string const nulldev_ = "nul"; +#endif return nulldev_; } diff --git a/src/support/package.C.in b/src/support/package.C.in new file mode 100644 index 0000000000..9ad6bfe47e --- /dev/null +++ b/src/support/package.C.in @@ -0,0 +1,696 @@ +// -*- C++ -*- +/** + * \file package.C + * This file is part of LyX, the document processor. + * Licence details can be found in the file COPYING. + * + * \author Angus Leeming + * + * Full author contact details are available in file CREDITS. + * + * Warning! This file is autogenerated from package.C.in. + * All changes to this file will be lost. + */ + +#include + +#include "support/package.h" + +#include "debug.h" +#include "gettext.h" + +#include "support/FileInfo.h" +#include "support/filetools.h" +#include "support/lstrings.h" +#include "support/os.h" + +#include +#include + +#include +#include + +#if !defined (USE_WINDOWS_PACKAGING) && \ + !defined (USE_MACOSX_PACKAGING) && \ + !defined (USE_POSIX_PACKAGING) +#error USE_FOO_PACKAGING must be defined for FOO = WINDOWS, MACOSX or POSIX. +#endif + +#if defined (USE_WINDOWS_PACKAGING) +# include +# include // SHGetFolderPath + + // Needed for MinGW: +# ifndef SHGFP_TYPE_CURRENT +# define SHGFP_TYPE_CURRENT 0 +# endif + +#elif defined (USE_MACOSX_PACKAGING) +# include // FSFindFolder, FSRefMakePath +#endif + +using std::string; + + +namespace lyx { +namespace support { + +namespace { + +Package package_; +bool initialised_ = false; + +} // namespace anon + + +void init_package(string const & command_line_arg0, + string const & command_line_system_support_dir, + string const & command_line_user_support_dir) +{ + // Can do so only once. + if (initialised_) + return; + + package_ = Package(command_line_arg0, + command_line_system_support_dir, + command_line_user_support_dir); + initialised_ = true; +} + + +Package const & package() +{ + BOOST_ASSERT(initialised_); + return package_; +} + + +namespace { + +string const abs_path_from_binary_name(string const & exe); + +std::pair const get_build_dirs(string const & abs_binary); + +string const get_document_dir(string const & home_dir); + +string const get_home_dir(); + +string const get_locale_dir(string const & system_support_dir); + +string const get_system_support_dir(string const & abs_binary, + string const & command_line_system_support_dir); + +string const get_temp_dir(); + +string const get_default_user_support_dir(string const & home_dir); + +std::pair const +get_user_support_dir(string const & default_user_support_dir, + string const & command_line_user_support_dir); + +} // namespace anon + + +Package::Package(string const & command_line_arg0, + string const & command_line_system_support_dir, + string const & command_line_user_support_dir) + : explicit_user_support_dir_(false) +{ + home_dir_ = get_home_dir(); + temp_dir_ = get_temp_dir(); + document_dir_ = get_document_dir(home_dir_); + + string const abs_binary = abs_path_from_binary_name(command_line_arg0); + binary_dir_ = OnlyPath(abs_binary); + + // Is LyX being run in-place from the build tree? + boost::tie(build_support_dir_, system_support_dir_) = + get_build_dirs(abs_binary); + + if (build_support_dir_.empty()) + system_support_dir_ = + get_system_support_dir(abs_binary, + command_line_system_support_dir); + + locale_dir_ = get_locale_dir(system_support_dir_); + + string const default_user_support_dir = + get_default_user_support_dir(home_dir_); + boost::tie(user_support_dir_, explicit_user_support_dir_) = + get_user_support_dir(default_user_support_dir, + command_line_user_support_dir); + + lyxerr[Debug::INIT] + << "\n" + << "\tbinary_dir " << binary_dir() << '\n' + << "\tsystem_support " << system_support() << '\n' + << "\tbuild_support " << build_support() << '\n' + << "\tuser_support " << user_support() << '\n' + << "\tlocale_dir " << locale_dir() << '\n' + << "\tdocument_dir " << document_dir() << '\n' + << "\ttemp_dir " << temp_dir() << '\n' + << "\thome_dir " << home_dir() << '\n' + << "<\\package>\n" << std::endl; +} + + +namespace { + +// These next three functions contain the stuff that is substituted at +// configuration-time. +string const top_srcdir() +{ + static string const dir("%TOP_SRCDIR%"); + return dir; +} + + +string const hardcoded_localedir() +{ + return string("%LOCALEDIR%"); +} + + +string const hardcoded_system_support_dir() +{ + return string("%LYX_DIR%"); +} + +} // namespace anon + + +string const & Package::top_srcdir() const +{ + static string const dir("%TOP_SRCDIR%"); + return dir; +} + + +namespace { + +bool check_command_line_dir(string const & dir, + string const & file, + string const & command_line_switch); + +string const extract_env_var_dir(string const & env_var); + +bool check_env_var_dir(string const & dir, + string const & env_var); + +bool check_env_var_dir(string const & dir, + string const & file, + string const & env_var); + +string const relative_locale_dir(); + +string const relative_system_support_dir(); + + +#if defined (USE_WINDOWS_PACKAGING) +// Given a folder ID, returns the folder name (in unix-style format). +// Eg CSIDL_PERSONAL -> "C:/Documents and Settings/USERNAME/My Documents" +string const win32_folder_path(int folder_id) +{ + char folder_path[PATH_MAX + 1]; + if (SUCCEEDED(SHGetFolderPath(0, folder_id, 0, + SHGFP_TYPE_CURRENT, folder_path))) + return os::internal_path(folder_path); + return string(); +} +#endif + + +std::pair const get_build_dirs(string const & abs_binary) +{ + string const check_text = "Checking whether LyX is run in place..."; + + // We're looking for "lyxrc.defaults" in a directory + // binary_dir/../lib + // We're also looking for "chkconfig.ltx" in a directory + // binary_dir/top_srcdir()/lib + // If both are found, then we're running LyX in-place. + + // Note that the name of the lyx binary may be a symbolic link. + // If that is the case, then we follow the links too. + string binary = abs_binary; + while (true) { + // Try and find "lyxrc.defaults". + string const binary_dir = OnlyPath(binary); + string const build_support_dir = + NormalizePath(AddPath(binary_dir, "../lib")); + + if (!FileSearch(build_support_dir, "lyxrc.defaults").empty()) { + // Try and find "chkconfig.ltx". + string const system_support_dir = + MakeAbsPath(AddPath(top_srcdir(), "lib"), + AddPath(binary_dir, "support")); + + if (!FileSearch(system_support_dir, "chkconfig.ltx").empty()) { + lyxerr[Debug::INIT] << check_text << " yes" + << std::endl; + return std::make_pair(build_support_dir, system_support_dir); + } + } + + // Check whether binary is a symbolic link. + // If so, resolve it and repeat the exercise. + FileInfo const file(binary, true); + if (!file.isOK() || !file.isLink()) + break; + + string link; + if (LyXReadLink(binary, link, true)) { + binary = link; + } else { + // Unable to resolve the link. + break; + } + } + + lyxerr[Debug::INIT] << check_text << " no" << std::endl; + return std::make_pair(string(), string()); +} + + +// Specification of document_dir_ may be reset by LyXRC, +// but the default is fixed for a given OS. +string const get_document_dir(string const & home_dir) +{ +#if defined (USE_WINDOWS_PACKAGING) + (void)home_dir; // Silence warning about unused variable. + return win32_folder_path(CSIDL_PERSONAL); +#else // Posix-like. + return home_dir; +#endif +} + + +// The specification of home_dir_ is fixed for a given OS. +// A typical example on Windows: "C:/Documents and Settings/USERNAME" +// and on a Posix-like machine: "/home/USERNAME". +string const get_home_dir() +{ +#if defined (USE_WINDOWS_PACKAGING) + string const home_dir = GetEnv("USERPROFILE"); +#else // Posix-like. + string const home_dir = GetEnv("HOME"); +#endif + + return os::internal_path(home_dir); +} + + +// Several sources are probed to ascertain the locale directory. +// The only requirement is that the result is indeed a directory. +string const get_locale_dir(string const & system_support_dir) +{ + // 1. Use the "LYX_LOCALEDIR" environment variable. + string path = extract_env_var_dir("LYX_LOCALEDIR"); + if (!path.empty() && check_env_var_dir(path, "LYX_LOCALEDIR")) + return path; + + // 2. Search for system_support_dir / + // The is OS-dependent. (On Unix, it will + // be "../locale/".) + path = NormalizePath(AddPath(system_support_dir, relative_locale_dir())); + + FileInfo fi(path); + if (fi.isOK() && fi.isDir()) + return path; + + // 3. Fall back to the hard-coded LOCALEDIR. + path = hardcoded_localedir(); + FileInfo fi2(path); + if (fi2.isOK() && fi2.isDir()) + return path; + + return string(); +} + + +// Specification of temp_dir_ may be reset by LyXRC, +// but the default is fixed for a given OS. +string const get_temp_dir() +{ +#if defined (USE_WINDOWS_PACKAGING) + // Typical example: C:/TEMP/. + char path[PATH_MAX + 1]; + GetTempPath(PATH_MAX, path); + return os::internal_path(path); +#else // Posix-like. + return "/tmp"; +#endif +} + + +// If we use a 'lazy' lyxerr in the hope of setting the locale before +// printing any messages, then we should ensure that it is flushed first. +void bail_out() +{ +#ifndef CXX_GLOBAL_CSTD + using std::exit; +#endif + exit(1); +} + + +// Extracts the absolute path from the foo of "-sysdir foo" or "-userdir foo" +string const abs_path_from_command_line(string const & command_line) +{ + if (command_line.empty()) + return string(); + + string const path = os::internal_path(command_line); + return os::is_absolute_path(path) ? path : MakeAbsPath(path); +} + + +// Does the grunt work for abs_path_from_binary_name() +string const get_binary_path(string const & exe) +{ + string const exe_path = os::internal_path(exe); + if (os::is_absolute_path(exe_path)) + return exe_path; + + // Two possibilities present themselves. + // 1. The binary is relative to the CWD. + string const abs_exe_path = MakeAbsPath(exe_path); + if (FileInfo(abs_exe_path, true).isOK()) + return abs_exe_path; + + // 2. exe must be the name of the binary only and it + // can be found on the PATH. + string const exe_name = OnlyFilename(exe_path); + if (exe_name != exe_path) + return string(); + + std::vector const path = getEnvPath("PATH"); + std::vector::const_iterator it = path.begin(); + std::vector::const_iterator const end = path.end(); + for (; it != end; ++it) { + if (!os::is_absolute_path(*it)) + // Someone is playing silly buggers. + continue; + + string const exe_path = AddName(*it, exe_name); + if (FileInfo(exe_path, true).isOK()) + return exe_path; + } + + // Didn't find anything. + return string(); +} + + +// Extracts the absolute path to the binary name received as argv[0]. +string const abs_path_from_binary_name(string const & exe) +{ + string const abs_binary = get_binary_path(exe); + if (abs_binary.empty()) { + lyxerr << bformat(_("Unable to determine the path to the " + "LyX binary from the command line %1%"), + exe) + << std::endl; + bail_out(); + } + return abs_binary; +} + + +// A plethora of directories is searched to ascertain the system +// lyxdir which is defined as the first directory to contain +// "chkconfig.ltx". +string const +get_system_support_dir(string const & abs_binary, + string const & command_line_system_support_dir) +{ + string const chkconfig_ltx = "chkconfig.ltx"; + + // searched_dirs is used for diagnostic purposes only in the case + // that "chkconfig.ltx" is not found. + std::list searched_dirs; + + // 1. Use the -sysdir command line parameter. + string path = abs_path_from_command_line(command_line_system_support_dir); + if (!path.empty()) { + searched_dirs.push_back(path); + if (check_command_line_dir(path, chkconfig_ltx, "-sysdir")) + return path; + } + + // 2. Use the "LYX_DIR_13x" environment variable. + path = extract_env_var_dir("LYX_DIR_13x"); + if (!path.empty()) { + searched_dirs.push_back(path); + if (check_env_var_dir(path, chkconfig_ltx, "LYX_DIR_13x")) + return path; + } + + // 3. Search relative to the lyx binary. + // We're looking for "chkconfig.ltx" in a directory + // OnlyPath(abs_binary) / / PACKAGE / + // PACKAGE is hardcoded in config.h. Eg "lyx" or "lyx-1.3.6cvs". + // is OS-dependent; on Unix, it will be "../share/". + string const relative_lyxdir = relative_system_support_dir(); + + // One subtlety to be aware of. The name of the lyx binary may be + // a symbolic link. If that is the case, then we follow the links too. + string binary = abs_binary; + while (true) { + // Try and find "chkconfig.ltx". + string const binary_dir = OnlyPath(binary); + + string const lyxdir = + NormalizePath(AddPath(binary_dir, relative_lyxdir)); + searched_dirs.push_back(lyxdir); + + if (!FileSearch(lyxdir, chkconfig_ltx).empty()) { + // Success! "chkconfig.ltx" has been found. + return lyxdir; + } + + // Check whether binary is a symbolic link. + // If so, resolve it and repeat the exercise. + FileInfo const file(binary, true); + if (!file.isOK() || !file.isLink()) + break; + + string link; + if (LyXReadLink(binary, link, true)) { + binary = link; + } else { + // Unable to resolve the link. + break; + } + } + + // 4. Repeat the exercise on the directory itself. + string binary_dir = OnlyPath(abs_binary); + while (true) { + // This time test whether the directory is a symbolic link + // *before* looking for "chkconfig.ltx". + // (We've looked relative to the original already.) + FileInfo const file(binary_dir, true); + if (!file.isOK() || !file.isLink()) + break; + + string link; + if (LyXReadLink(binary_dir, link, true)) { + binary_dir = link; + } else { + // Unable to resolve the link. + break; + } + + // Try and find "chkconfig.ltx". + string const lyxdir = + NormalizePath(AddPath(binary_dir, relative_lyxdir)); + searched_dirs.push_back(lyxdir); + + if (!FileSearch(lyxdir, chkconfig_ltx).empty()) { + // Success! "chkconfig.ltx" has been found. + return lyxdir; + } + } + + // 5. In desparation, try the hard-coded system support dir. + path = hardcoded_system_support_dir(); + if (!FileSearch(path, chkconfig_ltx).empty()) + return path; + + // Everything has failed :-( + // So inform the user and exit. + string searched_dirs_str; + typedef std::list::const_iterator iterator; + iterator const begin = searched_dirs.begin(); + iterator const end = searched_dirs.end(); + for (iterator it = begin; it != end; ++it) { + if (it != begin) + searched_dirs_str += "\n\t"; + searched_dirs_str += *it; + } + + lyxerr << bformat(_("Unable to determine the system directory " + "having searched\n" + "\t%1%\n" + "Try the '-sysdir' command line parameter or " + "set the environment variable LYX_DIR_13x to " + "the LyX system directory containing the file " + "`chkconfig.ltx'."), + searched_dirs_str) + << std::endl; + + bail_out(); + // Keep the compiler happy. + return string(); +} + + +// Returns the absolute path to the user lyxdir, together with a flag +// indicating whether this directory was specified explicitly (as -userdir +// or through an environment variable) or whether it was deduced. +std::pair const +get_user_support_dir(string const & default_user_support_dir, + string const & command_line_user_support_dir) +{ + bool explicit_userdir = true; + + // 1. Use the -userdir command line parameter. + string path = + abs_path_from_command_line(command_line_user_support_dir); + if (!path.empty()) + return std::make_pair(path, explicit_userdir); + + // 2. Use the LYX_USERDIR_13x environment variable. + path = extract_env_var_dir("LYX_USERDIR_13x"); + if (!path.empty()) + return std::make_pair(path, explicit_userdir); + + // 3. Use the OS-dependent default_user_support_dir + explicit_userdir = false; + return std::make_pair(default_user_support_dir, explicit_userdir); +} + + +// $HOME/.lyx on all platforms but Win32 where it will be something like +// "C:/Documents and Settings/USERNAME/Application Data/lyx" +string const get_default_user_support_dir(string const & home_dir) +{ +#if defined (USE_WINDOWS_PACKAGING) + (void)home_dir; // Silence warning about unused variable. + + return AddPath(win32_folder_path(CSIDL_APPDATA), PACKAGE); + +#elif defined (USE_MACOSX_PACKAGING) + (void)home_dir; // Silence warning about unused variable. + + FSRef fsref; + OSErr const error_code = + FSFindFolder(kUserDomain, kApplicationSupportFolderType, + kDontCreateFolder, &fsref); + if (error_code != 0) + return string(); + + char store[PATH_MAX + 1]; + OSStatus const status_code = + FSRefMakePath(&fsref, + reinterpret_cast(store), PATH_MAX); + if (status_code != 0) + return string(); + + return AddPath(reinterpret_cast(store), PACKAGE); + +#else // USE_POSIX_PACKAGING + return AddPath(home_dir, string(".") + PACKAGE); +#endif +} + + +// Check that directory @c dir contains @c file. +// Else emit a warning about an invalid @c command_line_switch. +bool check_command_line_dir(string const & dir, + string const & file, + string const & command_line_switch) +{ + string const abs_path = FileSearch(dir, file); + if (abs_path.empty()) { + lyxerr << bformat(_("Invalid %1% switch.\n" + "Directory %2% does not contain %3%."), + command_line_switch, dir, file) + << std::endl; + } + + return !abs_path.empty(); +} + + +// The environment variable @c env_var expands to a (single) file path. +string const extract_env_var_dir(string const & env_var) +{ + string const dir = os::internal_path(GetEnv(env_var)); + return dir.empty() ? dir : MakeAbsPath(dir); +} + + +// Check that directory @c dir contains @c file. +// Else emit a warning about an invalid @c env_var. +bool check_env_var_dir(string const & dir, + string const & file, + string const & env_var) +{ + string const abs_path = FileSearch(dir, file); + if (abs_path.empty()) { + lyxerr << bformat(_("Invalid %1% environment variable.\n" + "Directory %2% does not contain %3%."), + env_var, dir, file) + << std::endl; + } + + return !abs_path.empty(); +} + + +// Check that directory @c dir is indeed a directory. +// Else emit a warning about an invalid @c env_var. +bool check_env_var_dir(string const & dir, + string const & env_var) +{ + FileInfo fi(dir); + bool const success = (fi.isOK() && fi.isDir()); + + if (!success) { + lyxerr << bformat(_("Invalid %1% environment variable.\n" + "%2% is not a directory."), + env_var, dir) + << std::endl; + } + + return success; +} + + +// The locale directory relative to the LyX system directory. +string const relative_locale_dir() +{ + return "../locale/"; +} + + +// The system lyxdir is relative to the directory containing the LyX binary. +string const relative_system_support_dir() +{ + string result; + +#if defined (USE_WINDOWS_PACKAGING) || defined (USE_MACOSX_PACKAGING) + result = AddPath("../Resources/", PACKAGE); +#else // Posix-like. + result = AddPath("../share/", PACKAGE); +#endif + + return result; +} + +} // namespace anon + +} // namespace support +} // namespace lyx diff --git a/src/support/package.h b/src/support/package.h new file mode 100644 index 0000000000..cfe1828c41 --- /dev/null +++ b/src/support/package.h @@ -0,0 +1,183 @@ +// -*- C++ -*- +/** + * \file package.h + * This file is part of LyX, the document processor. + * Licence details can be found in the file COPYING. + * + * \author Angus Leeming + * + * Full author contact details are available in file CREDITS. + * + * A store of the paths to the various different directoies used + * by LyX. These paths differ markedly from one OS to another, + * following the local Windows, MacOS X or Posix conventions. + */ +#ifndef LYX_PACHAGE_H +#define LYX_PACHAGE_H + +#include + +namespace lyx { +namespace support { + +class Package; + +/** Initialise package() with the command line data. + * This data is exactly as it was passed in the argv[] array. + * + * @param command_line_arg0 argv[0], the name of the LyX executable + * as passed on the command line. + * + * @param command_line_system_support_dir, the LyX system dir, + * as specified on the command line with "-sysdir ". + * + * @param command_line_user_support_dir, the LyX user dir, + * as specified on the command line with "-userdir ". + */ +void init_package(std::string const & command_line_arg0, + std::string const & command_line_system_support_dir, + std::string const & command_line_user_support_dir); + +/** Accessor to the global data. + * Asserts that init_package() has been called first. + */ +Package const & package(); + +class Package { +public: + /// Default constructor does not lead to the paths being set. + Package(); + + /** Called by init_package, above. + * All paths will be initialized. + */ + Package(std::string const & command_line_arg0, + std::string const & command_line_system_support_dir, + std::string const & command_line_user_support_dir); + + /** The directory containing the LyX executable. + */ + std::string const & binary_dir() const; + + /** The top of the LyX source code tree. + * Used by the GTK frontend when searching for .glade files. + */ + std::string const & top_srcdir() const; + + /** The path to the system-level support files + * we're actually going to use. + */ + std::string const & system_support() const; + + /** The path to the autogenerated support files + * when running in-place. + */ + std::string const & build_support() const; + + /** The path to the user-level support files. + */ + std::string const & user_support() const; + + /** The user_support directory was set explicitly using either + * the -userdir command line switch or + * the LYX_USERDIR_13x environment variable. + */ + bool explicit_user_support() const; + + /** The path to the locale directory. + */ + std::string const & locale_dir() const; + + /** The default document directory. + * Can be reset by LyXRC. + */ + std::string & document_dir() const; + + /** The path to the temporary directory. + * (Eg /tmp on *nix.) + * Can be reset by LyXRC. + */ + std::string & temp_dir() const; + + /** Used when setting the user_support directory. + * Used also when expanding "~/" or contracting to "~/". (filetools.C) + * Used by the XForms file dialog. + * Used in emergencyWrite (bufferlist.C) as one possible location + * for the dump. + */ + std::string const & home_dir() const; + +private: + std::string binary_dir_; + std::string system_support_dir_; + std::string build_support_dir_; + std::string user_support_dir_; + std::string locale_dir_; + mutable std::string document_dir_; + mutable std::string temp_dir_; + std::string home_dir_; + bool explicit_user_support_dir_; +}; + + +inline +Package::Package() {} + +inline +std::string const & Package::binary_dir() const +{ + return binary_dir_; +} + +inline +std::string const & Package::system_support() const +{ + return system_support_dir_; +} + +inline +std::string const & Package::build_support() const +{ + return build_support_dir_; +} + +inline +std::string const & Package::user_support() const +{ + return user_support_dir_; +} + +inline +bool Package::explicit_user_support() const +{ + return explicit_user_support_dir_; +} + +inline +std::string const & Package::locale_dir() const +{ + return locale_dir_; +} + +inline +std::string & Package::document_dir() const +{ + return document_dir_; +} + +inline +std::string & Package::temp_dir() const +{ + return temp_dir_; +} + +inline +std::string const & Package::home_dir() const +{ + return home_dir_; +} + +} // namespace support +} // namespace lyx + +#endif // LYX_PACHAGE_H diff --git a/src/support/path_defines.C.in b/src/support/path_defines.C.in deleted file mode 100644 index dfe7062efb..0000000000 --- a/src/support/path_defines.C.in +++ /dev/null @@ -1,383 +0,0 @@ -// -*- C++ -*- -/** - * \file path_defines.C - * This file is part of LyX, the document processor. - * Licence details can be found in the file COPYING. - * - * \author Angus Leeming - * - * Full author contact details are available in file CREDITS. - * - * Warning! This file is autogenerated from path_defines.C.in. - * All changes to this file will be lost. - */ - -#include - -#include "path_defines.h" - -#include "debug.h" -#include "gettext.h" - -#include "FileInfo.h" -#include "filetools.h" -#include "lstrings.h" -#include "os.h" -#include "lyxlib.h" - -using std::string; -using std::endl; - - -namespace { - -// Store for the path to the lyx support files we're actually going to use. -string system_lyxdir_; - -// Store for the path to the autogenerated files when running in place -string build_lyxdir_; - -// Store for the path to the user-level support files. -string user_lyxdir_; - -// Store for the path to the locale directory. -string localedir_; - -/* The absolute path to the system-level lyx support files. - * (Make-time value.) - */ -string const & lyx_dir() -{ - static string const ld = "%LYX_DIR%"; - return ld; -} - -} // namespace anon - - -namespace lyx { -namespace support { - - -/* The absolute path to the top of the lyx build tree. - * (Make-time value.) - */ -string const & top_srcdir() -{ - static string const lts = "%TOP_SRCDIR%"; - return lts; -} - - -/* The absolute path to the system-level lyx locale directory. - * (Make-time value.) - */ -string const & lyx_localedir() -{ - static string const ll = "%LOCALEDIR%"; - if (localedir_.empty()) - return ll; - else - return localedir_; -} - - -string const & build_dir() -{ - static string const bl = "%BUILDDIR%"; - return bl; -} - - -string const & build_lyxdir() -{ - return build_lyxdir_; -} - - -void build_lyxdir(string const & sld) -{ - build_lyxdir_ = sld; -} - - -string const & system_lyxdir() -{ - return system_lyxdir_; -} - - -void system_lyxdir(string const & sld) -{ - system_lyxdir_ = sld; -} - - -string const & user_lyxdir() -{ - return user_lyxdir_; -} - - -void user_lyxdir(string const & uld) -{ - user_lyxdir_ = uld; -} - - -bool setLyxPaths() -{ - // - // Determine path of binary - // - - string binpath = os::binpath(); - string binname = os::binname(); - string fullbinname = MakeAbsPath(binname, binpath); - - if (binpath.empty()) { - lyxerr << _("Warning: could not determine path of binary.") - << "\n" - << _("If you have problems, try starting LyX with an absolute path.") - << endl; - } - lyxerr[Debug::INIT] << "Name of binary: " << binname << endl; - lyxerr[Debug::INIT] << "Path of binary: " << binpath << endl; - - - string searchpath; - - // - // Determine whether we are running in place and set - // build_lyxdir accordingly - // - - string const buildlyxdir = MakeAbsPath("../lib", binpath); - if (!FileSearch(buildlyxdir, "lyxrc.defaults").empty()) { - searchpath += AddPath(top_srcdir(), "lib") + ';'; - build_lyxdir(buildlyxdir); - lyxerr[Debug::INIT] << "Checking whether LyX is run in " - "place... yes" << endl; - } else { - lyxerr[Debug::INIT] - << "Checking whether LyX is run in place... no" - << endl; - } - - // - // Determine system directory. - // - - // Directories are searched in this order: - // 1) -sysdir command line parameter - // 2) LYX_DIR_14x environment variable - // 3) Maybe /TOP_SRCDIR/lib - // 4) /../Resources// [for LyX/Mac] - // 5) /../share// - // 5a) repeat 4 after following the Symlink if is a symbolic link. - // 6) hardcoded lyx_dir - // The directory is checked for the presence of the file - // "chkconfig.ltx", and if that is present, the directory - // is accepted as the system directory. - // If no chkconfig.ltx file is found, a warning is given, - // and the hardcoded lyx_dir is used. - - // If we had a command line switch, system_lyxdir_ is already set - if (!system_lyxdir_.empty()) - searchpath = MakeAbsPath(system_lyxdir_) + ';'; - - string const lyxdir = GetEnvPath("LYX_DIR_14x"); - - if (!lyxdir.empty()) { - lyxerr[Debug::INIT] << "LYX_DIR_14x: " << lyxdir << endl; - searchpath += lyxdir + ';'; - } - - // Path of binary/../Resources/ - searchpath += NormalizePath(AddPath(binpath, "../Resources/") + - OnlyFilename(binname)) + ';'; - - string fullbinpath = binpath; - FileInfo file(fullbinname, true); - if (file.isLink()) { - lyxerr[Debug::INIT] << "binary is a link" << endl; - string link; - if (LyXReadLink(fullbinname, link, true)) { - // Path of binary/../share/name of binary/ - searchpath += NormalizePath(AddPath(binpath, - "../share/") - + OnlyFilename(binname)); - searchpath += ';'; - fullbinpath = link; - binpath = MakeAbsPath(OnlyPath(fullbinpath)); - } - } - - bool followlink; - do { - // Path of binary/../share/name of binary/ - string const exe_name = OnlyFilename(binname); -#ifdef _WIN32 - string const lyx_system_dir_name = - suffixIs(exe_name, ".exe") ? - ChangeExtension(exe_name, "") : - exe_name; -#else - string const lyx_system_dir_name = exe_name; -#endif - string const shared_dir_name = - NormalizePath(AddPath(binpath, "../share/")); - searchpath += shared_dir_name + lyx_system_dir_name + ';'; - - // Follow Symlinks - FileInfo file(fullbinpath, true); - followlink = file.isLink(); - if (followlink) { - lyxerr[Debug::INIT] << " directory " << fullbinpath - << " is a link" << endl; - string link; - if (LyXReadLink(fullbinpath, link, true)) { - fullbinpath = link; - binpath = MakeAbsPath(OnlyPath(fullbinpath)); - } - else { - followlink = false; - } - } - } while (followlink); - - // /lib - searchpath += AddPath(top_srcdir(), "lib") + ';'; - // Hardcoded dir - searchpath += lyx_dir(); - - lyxerr[Debug::INIT] << "System directory search path: " - << searchpath << endl; - - string const filename = "chkconfig.ltx"; - string const temp = FileOpenSearch(searchpath, filename, string()); - system_lyxdir_ = OnlyPath(temp); - - // Reduce "path/../path" stuff out of system directory - system_lyxdir_ = NormalizePath(system_lyxdir_); - - bool path_shown = false; - - // Warn if environment variable is set, but unusable - if (!lyxdir.empty()) { - if (system_lyxdir_ != NormalizePath(lyxdir)) { - lyxerr <<_("LYX_DIR_14x environment variable no good.") - << '\n' - << _("System directory set to: ") - << system_lyxdir_ << endl; - path_shown = true; - } - } - - // Warn the user if we couldn't find "chkconfig.ltx" - if (system_lyxdir_ == "./") { - lyxerr <<_("LyX Warning! Couldn't determine system directory. ") - <<_("Try the '-sysdir' command line parameter or ") - <<_("set the environment variable LYX_DIR_14x to the " - "LyX system directory ") - << _("containing the file `chkconfig.ltx'.") << endl; - if (!path_shown) { - FileInfo fi(lyx_dir()); - if (!fi.exist()) { - lyxerr << "Couldn't even find the default LYX_DIR." << endl - << "Giving up." << endl; - exit(1); - } - lyxerr << bformat(_("Using built-in default %1$s but expect problems."), - lyx_dir()) << endl; - } else { - lyxerr << _("Expect problems.") << endl; - } - system_lyxdir_ = lyx_dir(); - path_shown = true; - } - - if (!path_shown) - lyxerr[Debug::INIT] << "System directory: '" - << system_lyxdir_ << '\'' << endl; - - // This is true when we are running from a Mac OS X bundle - // (for LyX/Mac) - bool const inOSXBundle = - system_lyxdir_ == NormalizePath(AddPath(binpath, "../Resources/") - + OnlyFilename(binname)); - if (inOSXBundle) - lyxerr[Debug::INIT] << "Running from LyX/Mac bundle." - << endl; - - // - // Set PATH for LyX/Mac - // LyX/Mac is a relocatable application bundle; here we add to - // the PATH so it can find binaries like reLyX inside its own - // application bundle, and also append PATH elements that it - // needs to run latex, previewers, etc. - // - - if (inOSXBundle) { - const string newpath = GetEnv("PATH") + ":" + binpath - + ":/sw/bin:/usr/local/bin" - + ":/usr/local/teTeX/bin/powerpc-apple-darwin-current"; - lyx::support::putenv("PATH", newpath); - lyxerr[Debug::INIT] << "LyX/Mac: setting PATH to: " - << GetEnv("PATH") << endl; - } - - - // - // Determine locale directory - // - if (!GetEnv("LYX_LOCALEDIR").empty()) { - localedir_ = GetEnv("LYX_LOCALEDIR"); - lyxerr[Debug::INIT] << "LYX_LOCALEDIR=" << localedir_ << endl; - } else if (inOSXBundle) { - string const maybe_localedir = - NormalizePath(AddPath(system_lyxdir_, "../locale")); - FileInfo fi(maybe_localedir); - if (fi.isOK() && fi.isDir()) { - lyxerr[Debug::INIT] - << "LyX/Mac: setting locale directory to " - << maybe_localedir << endl; - localedir_ = maybe_localedir; - } - } - - // - // Determine user lyx-dir - // - - // Directories are searched in this order: - // 1) -userdir command line parameter - // 2) LYX_USERDIR_14x environment variable - // 3) $HOME/. - - // If we had a command line switch, user_lyxdir_ is already set - bool explicit_userdir = true; - if (user_lyxdir_.empty()) { - - // LYX_USERDIR_14x environment variable - user_lyxdir_ = GetEnvPath("LYX_USERDIR_14x"); - - // default behaviour - if (user_lyxdir_.empty()) - if (inOSXBundle) - user_lyxdir_ = AddPath(os::homepath(), - "Library/Preferences/LyX"); - else - user_lyxdir_ = AddPath(os::homepath(), - string(".") + PACKAGE); - explicit_userdir = false; - } - - lyxerr[Debug::INIT] << "User LyX directory: '" - << user_lyxdir_ << '\'' << endl; - return explicit_userdir; -} - -} // namespace support -} // namespace lyx diff --git a/src/support/path_defines.h b/src/support/path_defines.h deleted file mode 100644 index 97dfc3a8ca..0000000000 --- a/src/support/path_defines.h +++ /dev/null @@ -1,51 +0,0 @@ -// -*- C++ -*- -/** - * \file path_defines.h - * This file is part of LyX, the document processor. - * Licence details can be found in the file COPYING. - * - * \author Angus Leeming - * - * Full author contact details are available in file CREDITS. - */ - -#ifndef PATH_DEFINES_H -#define PATH_DEFINES_H - -#include - - -namespace lyx { -namespace support { - -std::string const & lyx_localedir(); - -/* The absolute path to the top of the lyx build tree. - * (Make-time value.) - */ -std::string const & top_srcdir(); - -/// The absolute path to the lyx support files we're actually going to use. -std::string const & system_lyxdir(); - -/// Set the absolute path to the lyx support files (from the command line). -void system_lyxdir(std::string const &); - -/// The absolute path to the lyx support files in the build directory -std::string const & build_lyxdir(); - -/// The absolute path to the user-level lyx support files. -std::string const & user_lyxdir(); - -/// Set the absolute path to the user-level lyx support files. -void user_lyxdir(std::string const &); - -/** \returns true if the user lyx dir existed already and did not need - * to be created afresh. - */ -bool setLyxPaths(); - -} // namespace support -} // namespace lyx - -#endif // NOT PATH_DEFINES_H diff --git a/src/support/tempname.C b/src/support/tempname.C index 18826367c2..9e78525b82 100644 --- a/src/support/tempname.C +++ b/src/support/tempname.C @@ -11,9 +11,10 @@ #include #include "support/lyxlib.h" -#include "support/filetools.h" + #include "support/convert.h" -#include "support/os.h" +#include "support/filetools.h" +#include "support/package.h" #include "debug.h" @@ -53,7 +54,7 @@ int make_tempfile(char * templ) string const lyx::support::tempName(string const & dir, string const & mask) { - string const tmpdir(dir.empty() ? os::getTmpDir() : dir); + string const tmpdir(dir.empty() ? package().temp_dir() : dir); string tmpfl(AddName(tmpdir, mask)); tmpfl += convert(getpid()); tmpfl += "XXXXXX"; diff --git a/src/tex2lyx/ChangeLog b/src/tex2lyx/ChangeLog index 45dbfea54f..8437f28f4f 100644 --- a/src/tex2lyx/ChangeLog +++ b/src/tex2lyx/ChangeLog @@ -1,3 +1,8 @@ +2005-01-10 Angus Leeming + + * tex2lyx.C: use support/package.h to provide the paths to the + various directories used by LyX. + 2005-01-06 Lars Gullik Bjonnes * text.C: tostr -> convert @@ -11,8 +16,6 @@ * preamble.C (parse_preamble): handle second optional arg of \newcommand etc. and add the command to the known commands (fixes bug 1750) - * texparser.[Ch] (getFullArg): new, like getArg but distinguish - between empty arguments and no argument found * texparser.[Ch] (getFullOpt): new, like getOpt but distinguish between empty arguments and no argument found * tex2lyx.[Ch]: (add_known_command): new diff --git a/src/tex2lyx/tex2lyx.C b/src/tex2lyx/tex2lyx.C index 62c99b4922..0c26d02d03 100644 --- a/src/tex2lyx/tex2lyx.C +++ b/src/tex2lyx/tex2lyx.C @@ -17,11 +17,11 @@ #include "debug.h" #include "lyxtextclass.h" -#include "support/path_defines.h" #include "support/filetools.h" #include "support/lstrings.h" #include "support/lyxlib.h" #include "support/os.h" +#include "support/package.h" #include @@ -51,8 +51,6 @@ using lyx::support::isStrUnsignedInt; using lyx::support::ltrim; using lyx::support::rtrim; using lyx::support::strToUnsignedInt; -using lyx::support::system_lyxdir; -using lyx::support::user_lyxdir; using lyx::support::IsFileReadable; using lyx::support::IsFileWriteable; @@ -249,13 +247,19 @@ int parse_syntaxfile(string const & arg, string const &) } +// Filled with the command line arguments "foo" of "-sysdir foo" or +// "-userdir foo". +string cl_system_support; +string cl_user_support; + + int parse_sysdir(string const & arg, string const &) { if (arg.empty()) { cerr << "Missing directory for -sysdir switch" << endl; exit(1); } - system_lyxdir(arg); + cl_system_support = arg; return 1; } @@ -266,7 +270,7 @@ int parse_userdir(string const & arg, string const &) cerr << "Missing directory for -userdir switch" << endl; exit(1); } - user_lyxdir(arg); + cl_user_support = arg; return 1; } @@ -381,7 +385,7 @@ int main(int argc, char * argv[]) } lyx::support::os::init(argc, argv); - lyx::support::setLyxPaths(); + lyx::support::init_package(argv[0], cl_system_support, cl_user_support); string const system_syntaxfile = lyx::support::LibFileSearch("reLyX", "syntax.default"); if (system_syntaxfile.empty()) {