diff --git a/development/scons/scons_manifest.py b/development/scons/scons_manifest.py index 51f1ff2fb2..ca5317b55a 100644 --- a/development/scons/scons_manifest.py +++ b/development/scons/scons_manifest.py @@ -99,6 +99,7 @@ src_support_header_files = Split(''' docstream.h docstring.h environment.h + ExceptionMessage.h filefilterlist.h filename.h filetools.h diff --git a/src/lyx_main.C b/src/lyx_main.C index b0a6b1713a..97f71100ff 100644 --- a/src/lyx_main.C +++ b/src/lyx_main.C @@ -53,6 +53,7 @@ #include "support/filetools.h" #include "support/lyxlib.h" #include "support/convert.h" +#include "support/ExceptionMessage.h" #include "support/os.h" #include "support/package.h" #include "support/path.h" @@ -395,9 +396,17 @@ int LyX::exec(int & argc, char * argv[]) // we need to parse for "-dbg" and "-help" easyParse(argc, argv); - support::init_package(to_utf8(from_local8bit(argv[0])), + try { support::init_package(to_utf8(from_local8bit(argv[0])), cl_system_support, cl_user_support, support::top_build_dir_is_one_level_up); + } catch (support::ExceptionMessage const & message) { + if (message.type_ == support::ErrorException) { + Alert::error(message.title_, message.details_); + exit(1); + } else if (message.type_ == support::WarningException) { + Alert::warning(message.title_, message.details_); + } + } if (!use_gui) { // FIXME: create a ConsoleApplication diff --git a/src/support/ExceptionMessage.h b/src/support/ExceptionMessage.h new file mode 100644 index 0000000000..dc597c6547 --- /dev/null +++ b/src/support/ExceptionMessage.h @@ -0,0 +1,49 @@ +// -*- C++ -*- +/** + * \file ExceptionMessage.h + * This file is part of LyX, the document processor. + * Licence details can be found in the file COPYING. + * + * \author Abdelrazak Younes + * + * 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_MESSAGE_H +#define LYX_MESSAGE_H + +#include "support/docstring.h" + +#include + +namespace lyx { +namespace support { + + +enum ExceptionType { + ErrorException, + WarningException +}; + + +class ExceptionMessage: public std::exception { +public: + ExceptionMessage(ExceptionType type, docstring const & title, + docstring const & details) + : exception((to_utf8(title) + "\n" + to_utf8(details)).c_str()), + type_(type), title_(title), details_(details) {} + + virtual ~ExceptionMessage() {} + + ExceptionType type_; + docstring title_; + docstring details_; +}; + +} // namespace support +} // namespace lyx + +#endif // LYX_MESSAGE_H diff --git a/src/support/Makefile.am b/src/support/Makefile.am index 3ad9d9fc5f..bb8e7a45c1 100644 --- a/src/support/Makefile.am +++ b/src/support/Makefile.am @@ -35,6 +35,7 @@ libsupport_la_SOURCES = \ docstring.h \ environment.h \ environment.C \ + ExceptionMessage.h \ filefilterlist.C \ filefilterlist.h \ filename.C \ diff --git a/src/support/os_win32.C b/src/support/os_win32.C index daa977b73f..b5545b85e6 100644 --- a/src/support/os_win32.C +++ b/src/support/os_win32.C @@ -18,10 +18,12 @@ #include "support/os_win32.h" #include "support/lstrings.h" #include "support/filetools.h" +#include "support/ExceptionMessage.h" #include "support/package.h" #include "support/path.h" #include "debug.h" +#include "gettext.h" #include @@ -324,36 +326,21 @@ void windows_style_tex_paths(bool use_windows_paths) } -namespace { - -void bail_out() -{ -#ifndef CXX_GLOBAL_CSTD - using std::exit; -#endif - exit(1); -} - -} // namespace anon - - GetFolderPath::GetFolderPath() : folder_module_(0), folder_path_func_(0) { folder_module_ = LoadLibrary("shfolder.dll"); if (!folder_module_) { - lyxerr << "Unable to load shfolder.dll\nPlease install." - << std::endl; - bail_out(); + throw ExceptionMessage(ErrorException, _("System file not found"), + _("Unable to load shfolder.dll\nPlease install.")); } folder_path_func_ = reinterpret_cast(::GetProcAddress(folder_module_, "SHGetFolderPathA")); if (folder_path_func_ == 0) { - lyxerr << "Unable to find SHGetFolderPathA in shfolder.dll\n" - "Don't know how to proceed. Sorry." - << std::endl; - bail_out(); + throw ExceptionMessage(ErrorException, _("System function not found"), + _("Unable to find SHGetFolderPathA in shfolder.dll\n" + "Don't know how to proceed. Sorry.")); } } diff --git a/src/support/package.C.in b/src/support/package.C.in index 6edd7abde4..efe19c6272 100644 --- a/src/support/package.C.in +++ b/src/support/package.C.in @@ -22,6 +22,7 @@ #include "support/environment.h" #include "support/filetools.h" #include "support/lstrings.h" +#include "support/ExceptionMessage.h" #include "support/os.h" #if defined (USE_WINDOWS_PACKAGING) @@ -369,15 +370,6 @@ string const get_temp_dir() } -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) { @@ -440,11 +432,10 @@ string const abs_path_from_binary_name(string const & exe) string const abs_binary = get_binary_path(exe); if (abs_binary.empty()) { // FIXME UNICODE - lyxerr << lyx::to_utf8(bformat(_("Unable to determine the path to the " - "LyX binary from the command line %1$s"), - lyx::from_utf8(exe))) - << std::endl; - bail_out(); + throw ExceptionMessage(ErrorException, + _("LyX binary not found"), + bformat(_("Unable to determine the path to the LyX binary from the command line %1$s"), + lyx::from_utf8(exe))); } return abs_binary; } @@ -562,17 +553,16 @@ get_system_support_dir(string const & abs_binary, } // FIXME UNICODE - lyxerr << lyx::to_utf8(bformat(_("Unable to determine the system directory " + throw ExceptionMessage(ErrorException, _("No system directory"), + bformat(_("Unable to determine the system directory " "having searched\n" "\t%1$s\n" "Use the '-sysdir' command line parameter or " "set the environment variable LYX_DIR_15x to " "the LyX system directory containing the file " "`chkconfig.ltx'."), - lyx::from_utf8(searched_dirs_str))) - << std::endl; + lyx::from_utf8(searched_dirs_str))); - bail_out(); // Keep the compiler happy. return string(); } @@ -648,11 +638,10 @@ bool check_command_line_dir(string const & dir, FileName const abs_path = fileSearch(dir, file); if (abs_path.empty()) { // FIXME UNICODE - lyxerr << lyx::to_utf8(bformat(_("Invalid %1$s switch.\n" - "Directory %2$s does not contain %3$s."), - lyx::from_utf8(command_line_switch), lyx::from_utf8(dir), - lyx::from_utf8(file))) - << std::endl; + throw ExceptionMessage(WarningException, _("File not found"), bformat( + _("Invalid %1$s switch.\nDirectory %2$s does not contain %3$s."), + lyx::from_utf8(command_line_switch), lyx::from_utf8(dir), + lyx::from_utf8(file))); } return !abs_path.empty(); @@ -676,10 +665,11 @@ bool check_env_var_dir(string const & dir, FileName const abs_path = fileSearch(dir, file); if (abs_path.empty()) { // FIXME UNICODE - lyxerr << lyx::to_utf8(bformat(_("Invalid %1$s environment variable.\n" - "Directory %2$s does not contain %3$s."), - lyx::from_utf8(env_var), lyx::from_utf8(dir), lyx::from_utf8(file))) - << std::endl; + throw ExceptionMessage(WarningException, _("File not found"), bformat( + _("Invalid %1$s environment variable.\n" + "Directory %2$s does not contain %3$s."), + lyx::from_utf8(env_var), lyx::from_utf8(dir), + lyx::from_utf8(file))); } return !abs_path.empty(); @@ -703,8 +693,8 @@ bool check_env_var_dir(string const & dir, docstring const fmt = _("Invalid %1$s environment variable.\n%2$s is not a directory."); - lyxerr << lyx::to_utf8(bformat(fmt, lyx::from_utf8(env_var), lyx::from_utf8(dir))) - << std::endl; + throw ExceptionMessage(WarningException, _("Directory not found"), bformat( + fmt, lyx::from_utf8(env_var), lyx::from_utf8(dir))); } return success; diff --git a/src/tex2lyx/tex2lyx.C b/src/tex2lyx/tex2lyx.C index ac24bc5aa5..2baf0e8550 100644 --- a/src/tex2lyx/tex2lyx.C +++ b/src/tex2lyx/tex2lyx.C @@ -23,6 +23,7 @@ #include "support/fs_extras.h" #include "support/lstrings.h" #include "support/lyxlib.h" +#include "support/ExceptionMessage.h" #include "support/os.h" #include "support/package.h" #include "support/unicode.h" @@ -518,9 +519,16 @@ int main(int argc, char * argv[]) } lyx::support::os::init(argc, argv); - support::init_package(to_utf8(from_local8bit(argv[0])), + + try { support::init_package(to_utf8(from_local8bit(argv[0])), cl_system_support, cl_user_support, support::top_build_dir_is_two_levels_up); + } catch (support::ExceptionMessage const & message) { + cerr << to_utf8(message.title_) << ':\n' + << to_utf8(message.details_) << endl; + if (message.type_ == support::ErrorException) + exit(1); + } // Now every known option is parsed. Look for input and output // file name (the latter is optional).