2002-09-25 10:03:41 +00:00
|
|
|
/**
|
Rename files in src/support, step one.
src/support/package.h src/support/Package.h Package
src/support/package.C.in src/support/Package.C.in Package
src/support/path.h src/support/Path.h Path
src/support/fs_extras.h src/support/fs_extras.h NOCLASSES
src/support/RandomAccessList.h src/support/RandomAccessList.h RandomAccessList
src/support/lyxmanip.h src/support/lyxmanip.h NOCLASSES
src/support/rename.C src/support/rename.cpp NOCLASSES
src/support/abort.C src/support/abort.cpp NOCLASSES
src/support/lyxlib.h src/support/lyxlib.h NOCLASSES
src/support/ExceptionMessage.h src/support/ExceptionMessage.h ExceptionMessage
src/support/copy.C src/support/copy.cpp NOCLASSES
src/support/limited_stack.h src/support/limited_stack.h limited_stack
src/support/filefilterlist.C src/support/FileFilterList.cpp ['FileFilterList', 'Filter']
src/support/cow_ptr.h src/support/cow_ptr.h cow_ptr
src/support/os_unix.C src/support/os_unix.cpp NOCLASSES
src/support/socktools.h src/support/socktools.h NOCLASSES
src/support/forkedcontr.h src/support/ForkedcallsController.h ForkedcallsController
src/support/os.h src/support/os.h NOCLASSES
src/support/FileMonitor.h src/support/FileMonitor.h FileMonitor
src/support/copied_ptr.h src/support/copied_ptr.h copied_ptr
src/support/translator.h src/support/Translator.h Translator
src/support/filetools.C src/support/filetools.cpp NOCLASSES
src/support/unlink.C src/support/unlink.cpp NOCLASSES
src/support/os_win32.C src/support/os_win32.cpp GetFolderPath
src/support/lstrings.C src/support/lstrings.cpp NOCLASSES
src/support/qstring_helpers.C src/support/qstring_helpers.cpp NOCLASSES
src/support/getcwd.C src/support/getcwd.cpp NOCLASSES
src/support/systemcall.C src/support/Systemcall.cpp Systemcall
src/support/lyxalgo.h src/support/lyxalgo.h NOCLASSES
src/support/filefilterlist.h src/support/FileFilterList.h ['FileFilterList', 'Filter']
src/support/unicode.C src/support/unicode.cpp IconvProcessor
src/support/userinfo.C src/support/userinfo.cpp NOCLASSES
src/support/lyxtime.C src/support/lyxtime.cpp NOCLASSES
src/support/kill.C src/support/kill.cpp NOCLASSES
src/support/docstring.C src/support/docstring.cpp to_local8bit_failure
src/support/os_cygwin.C src/support/os_cygwin.cpp NOCLASSES
src/support/lyxsum.C src/support/lyxsum.cpp NOCLASSES
src/support/environment.C src/support/environment.cpp NOCLASSES
src/support/filetools.h src/support/filetools.h NOCLASSES
src/support/textutils.C src/support/textutils.cpp NOCLASSES
src/support/mkdir.C src/support/mkdir.cpp NOCLASSES
src/support/forkedcall.C src/support/Forkedcall.cpp ['ForkedProcess', 'Forkedcall']
src/support/tempname.C src/support/tempname.cpp NOCLASSES
src/support/os_win32.h src/support/os_win32.h GetFolderPath
src/support/types.h src/support/types.h NOCLASSES
src/support/lstrings.h src/support/lstrings.h NOCLASSES
src/support/forkedcallqueue.C src/support/ForkedCallQueue.cpp ForkedCallQueue
src/support/qstring_helpers.h src/support/qstring_helpers.h NOCLASSES
src/support/convert.C src/support/convert.cpp NOCLASSES
src/support/filename.C src/support/FileName.cpp ['FileName', 'DocFileName']
src/support/tests/convert.C src/support/tests/convert.cpp NOCLASSES
src/support/tests/filetools.C src/support/tests/filetools.cpp NOCLASSES
src/support/tests/lstrings.C src/support/tests/lstrings.cpp NOCLASSES
src/support/tests/boost.C src/support/tests/boost.cpp NOCLASSES
src/support/docstream.C src/support/docstream.cpp ['iconv_codecvt_facet_exception', 'idocfstream', 'odocfstream']
src/support/std_istream.h src/support/std_istream.h NOCLASSES
src/support/systemcall.h src/support/Systemcall.h Systemcall
src/support/chdir.C src/support/chdir.cpp NOCLASSES
src/support/std_ostream.h src/support/std_ostream.h NOCLASSES
src/support/unicode.h src/support/unicode.h IconvProcessor
src/support/path.C src/support/Path.cpp Path
src/support/fs_extras.C src/support/fs_extras.cpp NOCLASSES
src/support/userinfo.h src/support/userinfo.h NOCLASSES
src/support/lyxtime.h src/support/lyxtime.h NOCLASSES
src/support/docstring.h src/support/docstring.h to_local8bit_failure
src/support/debugstream.h src/support/debugstream.h basic_debugstream
src/support/environment.h src/support/environment.h NOCLASSES
src/support/textutils.h src/support/textutils.h NOCLASSES
src/support/forkedcall.h src/support/Forkedcall.h ['ForkedProcess', 'Forkedcall']
src/support/socktools.C src/support/socktools.cpp NOCLASSES
src/support/forkedcallqueue.h src/support/ForkedCallQueue.h ForkedCallQueue
src/support/forkedcontr.C src/support/ForkedcallsController.cpp ForkedcallsController
src/support/os.C src/support/os.cpp NOCLASSES
src/support/convert.h src/support/convert.h NOCLASSES
src/support/filename.h src/support/FileName.h ['FileName', 'DocFileName']
src/support/docstream.h src/support/docstream.h ['iconv_codecvt_facet_exception', 'idocfstream', 'odocfstream']
src/support/FileMonitor.C src/support/FileMonitor.cpp FileMonitor
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@18024 a592a061-630c-0410-9148-cb99ea01b6c8
2007-04-26 05:12:52 +00:00
|
|
|
* \file filetools.cpp
|
2002-09-25 10:03:41 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
|
|
|
*
|
|
|
|
* parts Copyright 1985, 1990, 1993 Free Software Foundation, Inc.
|
|
|
|
*
|
|
|
|
* \author Ivan Schreter
|
|
|
|
* \author Dirk Niggemann
|
|
|
|
* \author Asger Alstrup
|
2008-11-14 15:58:50 +00:00
|
|
|
* \author Lars Gullik Bjønnes
|
2002-09-25 10:03:41 +00:00
|
|
|
* \author Jean-Marc Lasgouttes
|
2003-06-18 09:56:10 +00:00
|
|
|
* \author Angus Leeming
|
|
|
|
* \author John Levon
|
2008-11-14 15:58:50 +00:00
|
|
|
* \author Herbert Voß
|
2002-09-25 10:03:41 +00:00
|
|
|
*
|
2003-09-08 09:51:40 +00:00
|
|
|
* Full author contact details are available in file CREDITS.
|
2002-09-25 10:03:41 +00:00
|
|
|
*
|
|
|
|
* General path-mangling functions
|
|
|
|
*/
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2016-08-17 20:32:14 +00:00
|
|
|
#include "LyX.h"
|
2011-03-25 01:59:34 +00:00
|
|
|
#include "LyXRC.h"
|
|
|
|
|
2007-11-29 07:04:28 +00:00
|
|
|
#include "support/filetools.h"
|
|
|
|
|
|
|
|
#include "support/debug.h"
|
2005-02-15 13:45:41 +00:00
|
|
|
#include "support/environment.h"
|
2007-11-29 07:04:28 +00:00
|
|
|
#include "support/gettext.h"
|
2005-02-15 13:45:41 +00:00
|
|
|
#include "support/lstrings.h"
|
2004-11-07 13:22:51 +00:00
|
|
|
#include "support/os.h"
|
2012-07-15 20:22:10 +00:00
|
|
|
#include "support/Messages.h"
|
Rename files in src/support, step one.
src/support/package.h src/support/Package.h Package
src/support/package.C.in src/support/Package.C.in Package
src/support/path.h src/support/Path.h Path
src/support/fs_extras.h src/support/fs_extras.h NOCLASSES
src/support/RandomAccessList.h src/support/RandomAccessList.h RandomAccessList
src/support/lyxmanip.h src/support/lyxmanip.h NOCLASSES
src/support/rename.C src/support/rename.cpp NOCLASSES
src/support/abort.C src/support/abort.cpp NOCLASSES
src/support/lyxlib.h src/support/lyxlib.h NOCLASSES
src/support/ExceptionMessage.h src/support/ExceptionMessage.h ExceptionMessage
src/support/copy.C src/support/copy.cpp NOCLASSES
src/support/limited_stack.h src/support/limited_stack.h limited_stack
src/support/filefilterlist.C src/support/FileFilterList.cpp ['FileFilterList', 'Filter']
src/support/cow_ptr.h src/support/cow_ptr.h cow_ptr
src/support/os_unix.C src/support/os_unix.cpp NOCLASSES
src/support/socktools.h src/support/socktools.h NOCLASSES
src/support/forkedcontr.h src/support/ForkedcallsController.h ForkedcallsController
src/support/os.h src/support/os.h NOCLASSES
src/support/FileMonitor.h src/support/FileMonitor.h FileMonitor
src/support/copied_ptr.h src/support/copied_ptr.h copied_ptr
src/support/translator.h src/support/Translator.h Translator
src/support/filetools.C src/support/filetools.cpp NOCLASSES
src/support/unlink.C src/support/unlink.cpp NOCLASSES
src/support/os_win32.C src/support/os_win32.cpp GetFolderPath
src/support/lstrings.C src/support/lstrings.cpp NOCLASSES
src/support/qstring_helpers.C src/support/qstring_helpers.cpp NOCLASSES
src/support/getcwd.C src/support/getcwd.cpp NOCLASSES
src/support/systemcall.C src/support/Systemcall.cpp Systemcall
src/support/lyxalgo.h src/support/lyxalgo.h NOCLASSES
src/support/filefilterlist.h src/support/FileFilterList.h ['FileFilterList', 'Filter']
src/support/unicode.C src/support/unicode.cpp IconvProcessor
src/support/userinfo.C src/support/userinfo.cpp NOCLASSES
src/support/lyxtime.C src/support/lyxtime.cpp NOCLASSES
src/support/kill.C src/support/kill.cpp NOCLASSES
src/support/docstring.C src/support/docstring.cpp to_local8bit_failure
src/support/os_cygwin.C src/support/os_cygwin.cpp NOCLASSES
src/support/lyxsum.C src/support/lyxsum.cpp NOCLASSES
src/support/environment.C src/support/environment.cpp NOCLASSES
src/support/filetools.h src/support/filetools.h NOCLASSES
src/support/textutils.C src/support/textutils.cpp NOCLASSES
src/support/mkdir.C src/support/mkdir.cpp NOCLASSES
src/support/forkedcall.C src/support/Forkedcall.cpp ['ForkedProcess', 'Forkedcall']
src/support/tempname.C src/support/tempname.cpp NOCLASSES
src/support/os_win32.h src/support/os_win32.h GetFolderPath
src/support/types.h src/support/types.h NOCLASSES
src/support/lstrings.h src/support/lstrings.h NOCLASSES
src/support/forkedcallqueue.C src/support/ForkedCallQueue.cpp ForkedCallQueue
src/support/qstring_helpers.h src/support/qstring_helpers.h NOCLASSES
src/support/convert.C src/support/convert.cpp NOCLASSES
src/support/filename.C src/support/FileName.cpp ['FileName', 'DocFileName']
src/support/tests/convert.C src/support/tests/convert.cpp NOCLASSES
src/support/tests/filetools.C src/support/tests/filetools.cpp NOCLASSES
src/support/tests/lstrings.C src/support/tests/lstrings.cpp NOCLASSES
src/support/tests/boost.C src/support/tests/boost.cpp NOCLASSES
src/support/docstream.C src/support/docstream.cpp ['iconv_codecvt_facet_exception', 'idocfstream', 'odocfstream']
src/support/std_istream.h src/support/std_istream.h NOCLASSES
src/support/systemcall.h src/support/Systemcall.h Systemcall
src/support/chdir.C src/support/chdir.cpp NOCLASSES
src/support/std_ostream.h src/support/std_ostream.h NOCLASSES
src/support/unicode.h src/support/unicode.h IconvProcessor
src/support/path.C src/support/Path.cpp Path
src/support/fs_extras.C src/support/fs_extras.cpp NOCLASSES
src/support/userinfo.h src/support/userinfo.h NOCLASSES
src/support/lyxtime.h src/support/lyxtime.h NOCLASSES
src/support/docstring.h src/support/docstring.h to_local8bit_failure
src/support/debugstream.h src/support/debugstream.h basic_debugstream
src/support/environment.h src/support/environment.h NOCLASSES
src/support/textutils.h src/support/textutils.h NOCLASSES
src/support/forkedcall.h src/support/Forkedcall.h ['ForkedProcess', 'Forkedcall']
src/support/socktools.C src/support/socktools.cpp NOCLASSES
src/support/forkedcallqueue.h src/support/ForkedCallQueue.h ForkedCallQueue
src/support/forkedcontr.C src/support/ForkedcallsController.cpp ForkedcallsController
src/support/os.C src/support/os.cpp NOCLASSES
src/support/convert.h src/support/convert.h NOCLASSES
src/support/filename.h src/support/FileName.h ['FileName', 'DocFileName']
src/support/docstream.h src/support/docstream.h ['iconv_codecvt_facet_exception', 'idocfstream', 'odocfstream']
src/support/FileMonitor.C src/support/FileMonitor.cpp FileMonitor
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@18024 a592a061-630c-0410-9148-cb99ea01b6c8
2007-04-26 05:12:52 +00:00
|
|
|
#include "support/Package.h"
|
2013-02-06 10:36:58 +00:00
|
|
|
#include "support/PathChanger.h"
|
Rename files in src/support, step one.
src/support/package.h src/support/Package.h Package
src/support/package.C.in src/support/Package.C.in Package
src/support/path.h src/support/Path.h Path
src/support/fs_extras.h src/support/fs_extras.h NOCLASSES
src/support/RandomAccessList.h src/support/RandomAccessList.h RandomAccessList
src/support/lyxmanip.h src/support/lyxmanip.h NOCLASSES
src/support/rename.C src/support/rename.cpp NOCLASSES
src/support/abort.C src/support/abort.cpp NOCLASSES
src/support/lyxlib.h src/support/lyxlib.h NOCLASSES
src/support/ExceptionMessage.h src/support/ExceptionMessage.h ExceptionMessage
src/support/copy.C src/support/copy.cpp NOCLASSES
src/support/limited_stack.h src/support/limited_stack.h limited_stack
src/support/filefilterlist.C src/support/FileFilterList.cpp ['FileFilterList', 'Filter']
src/support/cow_ptr.h src/support/cow_ptr.h cow_ptr
src/support/os_unix.C src/support/os_unix.cpp NOCLASSES
src/support/socktools.h src/support/socktools.h NOCLASSES
src/support/forkedcontr.h src/support/ForkedcallsController.h ForkedcallsController
src/support/os.h src/support/os.h NOCLASSES
src/support/FileMonitor.h src/support/FileMonitor.h FileMonitor
src/support/copied_ptr.h src/support/copied_ptr.h copied_ptr
src/support/translator.h src/support/Translator.h Translator
src/support/filetools.C src/support/filetools.cpp NOCLASSES
src/support/unlink.C src/support/unlink.cpp NOCLASSES
src/support/os_win32.C src/support/os_win32.cpp GetFolderPath
src/support/lstrings.C src/support/lstrings.cpp NOCLASSES
src/support/qstring_helpers.C src/support/qstring_helpers.cpp NOCLASSES
src/support/getcwd.C src/support/getcwd.cpp NOCLASSES
src/support/systemcall.C src/support/Systemcall.cpp Systemcall
src/support/lyxalgo.h src/support/lyxalgo.h NOCLASSES
src/support/filefilterlist.h src/support/FileFilterList.h ['FileFilterList', 'Filter']
src/support/unicode.C src/support/unicode.cpp IconvProcessor
src/support/userinfo.C src/support/userinfo.cpp NOCLASSES
src/support/lyxtime.C src/support/lyxtime.cpp NOCLASSES
src/support/kill.C src/support/kill.cpp NOCLASSES
src/support/docstring.C src/support/docstring.cpp to_local8bit_failure
src/support/os_cygwin.C src/support/os_cygwin.cpp NOCLASSES
src/support/lyxsum.C src/support/lyxsum.cpp NOCLASSES
src/support/environment.C src/support/environment.cpp NOCLASSES
src/support/filetools.h src/support/filetools.h NOCLASSES
src/support/textutils.C src/support/textutils.cpp NOCLASSES
src/support/mkdir.C src/support/mkdir.cpp NOCLASSES
src/support/forkedcall.C src/support/Forkedcall.cpp ['ForkedProcess', 'Forkedcall']
src/support/tempname.C src/support/tempname.cpp NOCLASSES
src/support/os_win32.h src/support/os_win32.h GetFolderPath
src/support/types.h src/support/types.h NOCLASSES
src/support/lstrings.h src/support/lstrings.h NOCLASSES
src/support/forkedcallqueue.C src/support/ForkedCallQueue.cpp ForkedCallQueue
src/support/qstring_helpers.h src/support/qstring_helpers.h NOCLASSES
src/support/convert.C src/support/convert.cpp NOCLASSES
src/support/filename.C src/support/FileName.cpp ['FileName', 'DocFileName']
src/support/tests/convert.C src/support/tests/convert.cpp NOCLASSES
src/support/tests/filetools.C src/support/tests/filetools.cpp NOCLASSES
src/support/tests/lstrings.C src/support/tests/lstrings.cpp NOCLASSES
src/support/tests/boost.C src/support/tests/boost.cpp NOCLASSES
src/support/docstream.C src/support/docstream.cpp ['iconv_codecvt_facet_exception', 'idocfstream', 'odocfstream']
src/support/std_istream.h src/support/std_istream.h NOCLASSES
src/support/systemcall.h src/support/Systemcall.h Systemcall
src/support/chdir.C src/support/chdir.cpp NOCLASSES
src/support/std_ostream.h src/support/std_ostream.h NOCLASSES
src/support/unicode.h src/support/unicode.h IconvProcessor
src/support/path.C src/support/Path.cpp Path
src/support/fs_extras.C src/support/fs_extras.cpp NOCLASSES
src/support/userinfo.h src/support/userinfo.h NOCLASSES
src/support/lyxtime.h src/support/lyxtime.h NOCLASSES
src/support/docstring.h src/support/docstring.h to_local8bit_failure
src/support/debugstream.h src/support/debugstream.h basic_debugstream
src/support/environment.h src/support/environment.h NOCLASSES
src/support/textutils.h src/support/textutils.h NOCLASSES
src/support/forkedcall.h src/support/Forkedcall.h ['ForkedProcess', 'Forkedcall']
src/support/socktools.C src/support/socktools.cpp NOCLASSES
src/support/forkedcallqueue.h src/support/ForkedCallQueue.h ForkedCallQueue
src/support/forkedcontr.C src/support/ForkedcallsController.cpp ForkedcallsController
src/support/os.C src/support/os.cpp NOCLASSES
src/support/convert.h src/support/convert.h NOCLASSES
src/support/filename.h src/support/FileName.h ['FileName', 'DocFileName']
src/support/docstream.h src/support/docstream.h ['iconv_codecvt_facet_exception', 'idocfstream', 'odocfstream']
src/support/FileMonitor.C src/support/FileMonitor.cpp FileMonitor
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@18024 a592a061-630c-0410-9148-cb99ea01b6c8
2007-04-26 05:12:52 +00:00
|
|
|
#include "support/Systemcall.h"
|
2007-12-02 09:59:26 +00:00
|
|
|
#include "support/qstring_helpers.h"
|
|
|
|
|
|
|
|
#include <QDir>
|
2014-06-09 11:05:50 +00:00
|
|
|
#include <QTemporaryFile>
|
2001-07-29 15:34:18 +00:00
|
|
|
|
2008-04-30 08:26:40 +00:00
|
|
|
#include "support/lassert.h"
|
2010-06-29 17:09:40 +00:00
|
|
|
#include "support/regex.h"
|
2003-09-09 17:25:35 +00:00
|
|
|
|
2004-07-24 10:55:30 +00:00
|
|
|
#include <fcntl.h>
|
2014-06-11 21:04:39 +00:00
|
|
|
#ifdef HAVE_MAGIC_H
|
|
|
|
#include <magic.h>
|
|
|
|
#endif
|
2014-12-21 17:13:33 +00:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
2004-07-24 10:55:30 +00:00
|
|
|
|
2005-04-21 14:03:30 +00:00
|
|
|
#include <cerrno>
|
2016-06-11 11:13:00 +00:00
|
|
|
#include <climits>
|
2002-06-10 07:57:39 +00:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstdio>
|
|
|
|
|
|
|
|
#include <utility>
|
|
|
|
#include <fstream>
|
2004-07-24 10:55:30 +00:00
|
|
|
#include <sstream>
|
2011-07-17 12:32:00 +00:00
|
|
|
#include <vector>
|
2002-06-10 07:57:39 +00:00
|
|
|
|
2010-06-28 17:45:04 +00:00
|
|
|
#if defined (_WIN32)
|
2010-06-29 13:23:33 +00:00
|
|
|
#include <io.h>
|
2010-06-28 17:45:04 +00:00
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
2007-12-12 10:16:00 +00:00
|
|
|
using namespace std;
|
2000-03-28 02:18:55 +00:00
|
|
|
|
2009-05-11 22:03:52 +00:00
|
|
|
#define USE_QPROCESS
|
|
|
|
|
2003-06-30 23:56:22 +00:00
|
|
|
namespace lyx {
|
|
|
|
namespace support {
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2010-04-21 01:19:31 +00:00
|
|
|
bool isLyXFileName(string const & filename)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2002-07-16 21:17:10 +00:00
|
|
|
return suffixIs(ascii_lowercase(filename), ".lyx");
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-21 01:19:31 +00:00
|
|
|
bool isSGMLFileName(string const & filename)
|
2000-11-13 15:43:36 +00:00
|
|
|
{
|
2002-07-16 21:17:10 +00:00
|
|
|
return suffixIs(ascii_lowercase(filename), ".sgml");
|
2000-11-13 15:43:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-21 01:19:31 +00:00
|
|
|
bool isValidLaTeXFileName(string const & filename)
|
2007-06-26 16:55:42 +00:00
|
|
|
{
|
2010-09-27 02:39:22 +00:00
|
|
|
string const invalid_chars("#%\"");
|
|
|
|
return filename.find_first_of(invalid_chars) == string::npos;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool isValidDVIFileName(string const & filename)
|
|
|
|
{
|
|
|
|
string const invalid_chars("${}()[]^");
|
2007-10-18 23:03:51 +00:00
|
|
|
return filename.find_first_of(invalid_chars) == string::npos;
|
2007-06-26 16:55:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-11 21:04:39 +00:00
|
|
|
bool isBinaryFile(FileName const & filename)
|
|
|
|
{
|
|
|
|
bool isbinary = false;
|
|
|
|
if (filename.empty() || !filename.exists())
|
|
|
|
return isbinary;
|
|
|
|
|
|
|
|
#ifdef HAVE_MAGIC_H
|
|
|
|
magic_t magic_cookie = magic_open(MAGIC_MIME_ENCODING);
|
|
|
|
if (magic_cookie) {
|
|
|
|
bool detected = true;
|
|
|
|
if (magic_load(magic_cookie, NULL) != 0) {
|
|
|
|
LYXERR(Debug::FILES, "isBinaryFile: "
|
|
|
|
"Could not load magic database - "
|
|
|
|
<< magic_error(magic_cookie));
|
|
|
|
detected = false;
|
|
|
|
} else {
|
|
|
|
char const *charset = magic_file(magic_cookie,
|
|
|
|
filename.toFilesystemEncoding().c_str());
|
|
|
|
isbinary = contains(charset, "binary");
|
|
|
|
}
|
|
|
|
magic_close(magic_cookie);
|
|
|
|
if (detected)
|
|
|
|
return isbinary;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
// Try by looking for binary chars at the beginning of the file.
|
|
|
|
// Note that this is formally not correct, since count_bin_chars
|
|
|
|
// expects utf8, and the passed string can be anything: plain text
|
|
|
|
// in any encoding, or really binary data. In practice it works,
|
|
|
|
// since QString::fromUtf8() drops invalid utf8 sequences, and
|
|
|
|
// while the exact number may not be correct, we still get a high
|
|
|
|
// number for truly binary files.
|
|
|
|
|
|
|
|
ifstream ifs(filename.toFilesystemEncoding().c_str());
|
|
|
|
if (!ifs)
|
|
|
|
return isbinary;
|
|
|
|
|
|
|
|
// Maximum strings to read
|
|
|
|
int const max_count = 50;
|
|
|
|
|
|
|
|
// Maximum number of binary chars allowed
|
|
|
|
int const max_bin = 5;
|
|
|
|
|
|
|
|
int count = 0;
|
|
|
|
int binchars = 0;
|
|
|
|
string str;
|
|
|
|
while (count++ < max_count && !ifs.eof()) {
|
|
|
|
getline(ifs, str);
|
|
|
|
binchars += count_bin_chars(str);
|
|
|
|
}
|
|
|
|
return binchars > max_bin;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-10 09:31:27 +00:00
|
|
|
string const latex_path(string const & original_path,
|
|
|
|
latex_path_extension extension,
|
|
|
|
latex_path_dots dots)
|
2005-04-18 17:43:11 +00:00
|
|
|
{
|
2006-03-24 12:48:37 +00:00
|
|
|
// On cygwin, we may need windows or posix style paths.
|
2006-04-05 19:26:08 +00:00
|
|
|
string path = os::latex_path(original_path);
|
2005-04-18 17:43:11 +00:00
|
|
|
path = subst(path, "~", "\\string~");
|
2005-07-14 22:09:22 +00:00
|
|
|
if (path.find(' ') != string::npos) {
|
2005-06-21 09:01:41 +00:00
|
|
|
// We can't use '"' because " is sometimes active (e.g. if
|
|
|
|
// babel is loaded with the "german" option)
|
2005-07-10 09:31:27 +00:00
|
|
|
if (extension == EXCLUDE_EXTENSION) {
|
2011-10-31 20:27:21 +00:00
|
|
|
// changeExtension calls os::internal_path internally
|
2006-04-05 19:26:08 +00:00
|
|
|
// so don't use it to remove the extension.
|
2006-04-08 22:31:11 +00:00
|
|
|
string const ext = getExtension(path);
|
2006-04-05 19:26:08 +00:00
|
|
|
string const base = ext.empty() ?
|
|
|
|
path :
|
|
|
|
path.substr(0, path.length() - ext.length() - 1);
|
2011-10-31 20:27:21 +00:00
|
|
|
// changeExtension calls os::internal_path internally
|
2005-07-08 15:43:46 +00:00
|
|
|
// so don't use it to re-add the extension.
|
|
|
|
path = "\\string\"" + base + "\\string\"." + ext;
|
2005-07-14 22:09:22 +00:00
|
|
|
} else {
|
2005-07-08 15:43:46 +00:00
|
|
|
path = "\\string\"" + path + "\\string\"";
|
2005-07-14 22:09:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-31 20:27:21 +00:00
|
|
|
if (dots != ESCAPE_DOTS)
|
|
|
|
return path;
|
|
|
|
|
|
|
|
// Replace dots with the lyxdot macro, but only in the file name,
|
|
|
|
// not the directory part.
|
|
|
|
// addName etc call os::internal_path internally
|
|
|
|
// so don't use them for path manipulation
|
|
|
|
// The directory separator is always '/' for LaTeX.
|
|
|
|
string::size_type pos = path.rfind('/');
|
|
|
|
if (pos == string::npos)
|
|
|
|
return subst(path, ".", "\\lyxdot ");
|
|
|
|
return path.substr(0, pos) + subst(path.substr(pos), ".", "\\lyxdot ");
|
2005-04-18 17:43:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
// Substitutes spaces with underscores in filename (and path)
|
2007-11-28 18:07:09 +00:00
|
|
|
FileName const makeLatexName(FileName const & file)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2007-11-28 18:07:09 +00:00
|
|
|
string name = file.onlyFileName();
|
2010-04-21 01:19:09 +00:00
|
|
|
string const path = file.onlyPath().absFileName() + "/";
|
2002-03-21 17:09:55 +00:00
|
|
|
|
1999-11-02 21:19:58 +00:00
|
|
|
// ok so we scan through the string twice, but who cares.
|
2007-11-28 18:07:09 +00:00
|
|
|
// FIXME: in Unicode time this will break for sure! There is
|
|
|
|
// a non-latin world out there...
|
2005-07-14 22:09:22 +00:00
|
|
|
string const keep = "abcdefghijklmnopqrstuvwxyz"
|
1999-11-03 12:24:23 +00:00
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
2007-02-25 09:00:28 +00:00
|
|
|
"@!'()*+,-./0123456789:;<=>?[]`|";
|
2002-03-21 17:09:55 +00:00
|
|
|
|
1999-11-02 21:19:58 +00:00
|
|
|
string::size_type pos = 0;
|
2005-07-14 22:09:22 +00:00
|
|
|
while ((pos = name.find_first_not_of(keep, pos)) != string::npos)
|
1999-11-03 12:45:11 +00:00
|
|
|
name[pos++] = '_';
|
2005-07-14 22:09:22 +00:00
|
|
|
|
2007-11-28 18:07:09 +00:00
|
|
|
FileName latex_name(path + name);
|
|
|
|
latex_name.changeExtension(".tex");
|
|
|
|
return latex_name;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
2000-08-05 05:17:18 +00:00
|
|
|
|
2006-09-11 16:07:49 +00:00
|
|
|
string const quoteName(string const & name, quote_style style)
|
1999-12-03 13:51:01 +00:00
|
|
|
{
|
2006-09-11 16:07:49 +00:00
|
|
|
switch(style) {
|
|
|
|
case quote_shell:
|
2009-12-19 19:29:53 +00:00
|
|
|
// This does not work on native Windows for filenames
|
|
|
|
// containing the following characters < > : " / \ | ? *
|
|
|
|
// Moreover, it can't be made to work, as, according to
|
|
|
|
// http://msdn.microsoft.com/en-us/library/aa365247(VS.85).aspx
|
|
|
|
// those are reserved characters, and thus are forbidden.
|
|
|
|
// Please, also note that the command-line parser in
|
|
|
|
// ForkedCall::generateChild cannot deal with filenames
|
|
|
|
// containing " or ', therefore we don't pass user filenames
|
|
|
|
// to child processes if possible. We store them in a python
|
|
|
|
// script instead, where we don't have these limitations.
|
2009-05-11 22:03:52 +00:00
|
|
|
#ifndef USE_QPROCESS
|
2006-09-11 16:07:49 +00:00
|
|
|
return (os::shell() == os::UNIX) ?
|
2009-12-19 19:29:53 +00:00
|
|
|
'\'' + subst(name, "'", "\'\\\'\'") + '\'' :
|
2006-09-11 16:07:49 +00:00
|
|
|
'"' + name + '"';
|
2009-05-11 22:03:52 +00:00
|
|
|
#else
|
2009-12-18 22:15:46 +00:00
|
|
|
// According to the QProcess parser, a single double
|
|
|
|
// quote is represented by three consecutive ones.
|
2009-12-19 17:26:09 +00:00
|
|
|
// Here we simply escape the double quote and let our
|
|
|
|
// simple parser in Systemcall.cpp do the substitution.
|
|
|
|
return '"' + subst(name, "\"", "\\\"") + '"';
|
2009-05-11 22:03:52 +00:00
|
|
|
#endif
|
2014-11-19 21:47:36 +00:00
|
|
|
case quote_shell_filename:
|
|
|
|
return quoteName(os::external_path(name), quote_shell);
|
2006-09-11 16:07:49 +00:00
|
|
|
case quote_python:
|
|
|
|
return "\"" + subst(subst(name, "\\", "\\\\"), "\"", "\\\"")
|
|
|
|
+ "\"";
|
|
|
|
}
|
|
|
|
// shut up stupid compiler
|
|
|
|
return string();
|
1999-12-03 13:51:01 +00:00
|
|
|
}
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2006-12-21 12:04:01 +00:00
|
|
|
#if 0
|
1999-09-27 18:44:28 +00:00
|
|
|
// Uses a string of paths separated by ";"s to find a file to open.
|
|
|
|
// Can't cope with pathnames with a ';' in them. Returns full path to file.
|
|
|
|
// If path entry begins with $$LyX/, use system_lyxdir
|
|
|
|
// If path entry begins with $$User/, use user_lyxdir
|
|
|
|
// Example: "$$User/doc;$$LyX/doc"
|
2006-11-26 21:30:39 +00:00
|
|
|
FileName const fileOpenSearch(string const & path, string const & name,
|
2000-08-05 05:17:18 +00:00
|
|
|
string const & ext)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2006-11-26 21:30:39 +00:00
|
|
|
FileName real_file;
|
2002-06-26 19:07:34 +00:00
|
|
|
string path_element;
|
1999-09-27 18:44:28 +00:00
|
|
|
bool notfound = true;
|
1999-11-02 06:42:25 +00:00
|
|
|
string tmppath = split(path, path_element, ';');
|
2002-03-21 17:09:55 +00:00
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
while (notfound && !path_element.empty()) {
|
2004-12-20 16:59:33 +00:00
|
|
|
path_element = os::internal_path(path_element);
|
1999-10-02 16:21:10 +00:00
|
|
|
if (!suffixIs(path_element, '/'))
|
2005-07-14 22:09:22 +00:00
|
|
|
path_element += '/';
|
2005-01-10 19:17:43 +00:00
|
|
|
path_element = subst(path_element, "$$LyX",
|
2010-04-21 01:19:09 +00:00
|
|
|
package().system_support().absFileName());
|
2005-01-10 19:17:43 +00:00
|
|
|
path_element = subst(path_element, "$$User",
|
2010-04-21 01:19:09 +00:00
|
|
|
package().user_support().absFileName());
|
2002-03-21 17:09:55 +00:00
|
|
|
|
2006-04-08 22:31:11 +00:00
|
|
|
real_file = fileSearch(path_element, name, ext);
|
2002-03-21 17:09:55 +00:00
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
if (real_file.empty()) {
|
2000-01-20 01:41:55 +00:00
|
|
|
do {
|
|
|
|
tmppath = split(tmppath, path_element, ';');
|
2001-12-05 08:04:20 +00:00
|
|
|
} while (!tmppath.empty() && path_element.empty());
|
1999-09-27 18:44:28 +00:00
|
|
|
} else {
|
2000-01-20 01:41:55 +00:00
|
|
|
notfound = false;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return real_file;
|
|
|
|
}
|
2006-12-21 12:04:01 +00:00
|
|
|
#endif
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
// Returns the real name of file name in directory path, with optional
|
2002-03-21 17:09:55 +00:00
|
|
|
// extension ext.
|
2006-11-26 21:30:39 +00:00
|
|
|
FileName const fileSearch(string const & path, string const & name,
|
2015-03-12 23:47:21 +00:00
|
|
|
string const & exts, search_mode mode)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
|
|
|
// if `name' is an absolute path, we ignore the setting of `path'
|
|
|
|
// Expand Environmentvariables in 'name'
|
2006-04-08 22:31:11 +00:00
|
|
|
string const tmpname = replaceEnvironmentPath(name);
|
2006-11-26 21:30:39 +00:00
|
|
|
FileName fullname(makeAbsPath(tmpname, path));
|
1999-09-27 18:44:28 +00:00
|
|
|
// search first without extension, then with it.
|
2007-11-25 11:18:16 +00:00
|
|
|
if (fullname.isReadableFile())
|
1999-09-27 18:44:28 +00:00
|
|
|
return fullname;
|
2015-03-12 23:47:21 +00:00
|
|
|
if (exts.empty())
|
2006-12-21 12:04:01 +00:00
|
|
|
// We are done.
|
2008-01-26 20:25:25 +00:00
|
|
|
return mode == may_not_exist ? fullname : FileName();
|
2015-03-12 23:47:21 +00:00
|
|
|
int n = 0;
|
|
|
|
string ext = token(exts, ',', n);
|
|
|
|
while (!ext.empty()) {
|
|
|
|
// Only add the extension if it is not already the extension of
|
|
|
|
// fullname.
|
|
|
|
bool addext = getExtension(fullname.absFileName()) != ext;
|
|
|
|
if (addext) {
|
|
|
|
if (mode == check_hidpi) {
|
|
|
|
FileName fullname2x = FileName(addExtension(fullname.absFileName() + "@2x", ext));
|
|
|
|
if (fullname2x.isReadableFile())
|
|
|
|
return fullname2x;
|
|
|
|
}
|
|
|
|
fullname = FileName(addExtension(fullname.absFileName(), ext));
|
2014-10-18 13:19:47 +00:00
|
|
|
}
|
2015-03-12 23:47:21 +00:00
|
|
|
if (fullname.isReadableFile() || mode == may_not_exist)
|
|
|
|
return fullname;
|
|
|
|
if (addext)
|
|
|
|
fullname.changeExtension("");
|
|
|
|
ext = token(exts, ',', ++n);
|
2014-10-18 13:19:47 +00:00
|
|
|
}
|
2007-01-29 18:37:13 +00:00
|
|
|
return FileName();
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Search the file name.ext in the subdirectory dir of
|
|
|
|
// 1) user_lyxdir
|
|
|
|
// 2) build_lyxdir (if not empty)
|
|
|
|
// 3) system_lyxdir
|
2006-11-26 21:30:39 +00:00
|
|
|
FileName const libFileSearch(string const & dir, string const & name,
|
2014-10-18 13:19:47 +00:00
|
|
|
string const & ext, search_mode mode)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2010-04-21 01:19:09 +00:00
|
|
|
FileName fullname = fileSearch(addPath(package().user_support().absFileName(), dir),
|
2014-10-18 13:19:47 +00:00
|
|
|
name, ext, mode);
|
1999-09-27 18:44:28 +00:00
|
|
|
if (!fullname.empty())
|
|
|
|
return fullname;
|
2002-03-21 17:09:55 +00:00
|
|
|
|
2005-01-10 19:17:43 +00:00
|
|
|
if (!package().build_support().empty())
|
2010-04-21 01:19:09 +00:00
|
|
|
fullname = fileSearch(addPath(package().build_support().absFileName(), dir),
|
2014-10-18 13:19:47 +00:00
|
|
|
name, ext, mode);
|
1999-09-27 18:44:28 +00:00
|
|
|
if (!fullname.empty())
|
|
|
|
return fullname;
|
2002-03-21 17:09:55 +00:00
|
|
|
|
2014-10-18 13:19:47 +00:00
|
|
|
return fileSearch(addPath(package().system_support().absFileName(), dir),
|
|
|
|
name, ext, mode);
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
|
2006-11-26 21:30:39 +00:00
|
|
|
FileName const i18nLibFileSearch(string const & dir, string const & name,
|
2000-08-05 05:17:18 +00:00
|
|
|
string const & ext)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2001-10-12 16:23:26 +00:00
|
|
|
/* The highest priority value is the `LANGUAGE' environment
|
2001-10-24 13:21:20 +00:00
|
|
|
variable. But we don't use the value if the currently
|
2007-08-07 20:39:55 +00:00
|
|
|
selected locale is the C locale. This is a GNU extension.
|
2004-03-24 12:05:57 +00:00
|
|
|
|
2007-11-29 07:04:28 +00:00
|
|
|
Otherwise, w use a trick to guess what support/gettext.has done:
|
2007-08-07 20:39:55 +00:00
|
|
|
each po file is able to tell us its name. (JMarc)
|
|
|
|
*/
|
|
|
|
|
2012-07-15 20:22:10 +00:00
|
|
|
string lang = getGuiMessages().language();
|
2005-02-15 13:45:41 +00:00
|
|
|
string const language = getEnv("LANGUAGE");
|
2007-08-07 20:39:55 +00:00
|
|
|
if (!lang.empty() && !language.empty())
|
2004-03-24 12:05:57 +00:00
|
|
|
lang = language;
|
2002-03-21 17:09:55 +00:00
|
|
|
|
2004-06-29 12:55:09 +00:00
|
|
|
string l;
|
|
|
|
lang = split(lang, l, ':');
|
2007-08-07 20:39:55 +00:00
|
|
|
while (!l.empty()) {
|
|
|
|
FileName tmp;
|
|
|
|
// First try with the full name
|
|
|
|
tmp = libFileSearch(addPath(dir, l), name, ext);
|
1999-09-27 18:44:28 +00:00
|
|
|
if (!tmp.empty())
|
|
|
|
return tmp;
|
2007-08-07 20:39:55 +00:00
|
|
|
|
|
|
|
// Then the name without country code
|
|
|
|
string const shortl = token(l, '_', 0);
|
|
|
|
if (shortl != l) {
|
|
|
|
tmp = libFileSearch(addPath(dir, shortl), name, ext);
|
|
|
|
if (!tmp.empty())
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
2007-02-25 18:12:03 +00:00
|
|
|
#if 1
|
2007-08-07 20:39:55 +00:00
|
|
|
// For compatibility, to be removed later (JMarc)
|
|
|
|
tmp = libFileSearch(dir, token(l, '_', 0) + '_' + name,
|
|
|
|
ext);
|
|
|
|
if (!tmp.empty()) {
|
|
|
|
lyxerr << "i18nLibFileSearch: File `" << tmp
|
2007-02-25 18:12:03 +00:00
|
|
|
<< "' has been found by the old method" <<endl;
|
2007-08-07 20:39:55 +00:00
|
|
|
return tmp;
|
2007-02-25 18:12:03 +00:00
|
|
|
}
|
|
|
|
#endif
|
2004-06-29 12:55:09 +00:00
|
|
|
lang = split(lang, l, ':');
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
2004-06-29 12:55:09 +00:00
|
|
|
|
2006-04-08 22:31:11 +00:00
|
|
|
return libFileSearch(dir, name, ext);
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-25 01:59:34 +00:00
|
|
|
FileName const imageLibFileSearch(string & dir, string const & name,
|
2014-10-18 13:19:47 +00:00
|
|
|
string const & ext, search_mode mode)
|
2011-03-25 01:59:34 +00:00
|
|
|
{
|
|
|
|
if (!lyx::lyxrc.icon_set.empty()) {
|
|
|
|
string const imagedir = addPath(dir, lyx::lyxrc.icon_set);
|
2014-10-18 13:19:47 +00:00
|
|
|
FileName const fn = libFileSearch(imagedir, name, ext, mode);
|
2011-03-25 01:59:34 +00:00
|
|
|
if (fn.exists()) {
|
|
|
|
dir = imagedir;
|
|
|
|
return fn;
|
|
|
|
}
|
|
|
|
}
|
2014-10-18 13:19:47 +00:00
|
|
|
return libFileSearch(dir, name, ext, mode);
|
2011-03-25 01:59:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-03 15:57:52 +00:00
|
|
|
string const commandPrep(string const & command_in)
|
2002-03-27 00:05:28 +00:00
|
|
|
{
|
2005-07-14 22:09:22 +00:00
|
|
|
static string const token_scriptpath = "$$s/";
|
2014-02-03 15:57:52 +00:00
|
|
|
string const python_call = "python -tt";
|
2003-05-31 09:24:46 +00:00
|
|
|
|
|
|
|
string command = command_in;
|
2014-02-03 15:57:52 +00:00
|
|
|
if (prefixIs(command_in, python_call))
|
|
|
|
command = os::python() + command_in.substr(python_call.length());
|
|
|
|
|
2003-05-31 09:24:46 +00:00
|
|
|
// Find the starting position of "$$s/"
|
|
|
|
string::size_type const pos1 = command.find(token_scriptpath);
|
|
|
|
if (pos1 == string::npos)
|
2002-03-27 00:05:28 +00:00
|
|
|
return command;
|
2005-02-01 16:41:24 +00:00
|
|
|
// Find the end of the "$$s/some_subdir/some_script" word within
|
|
|
|
// command. Assumes that the script name does not contain spaces.
|
2003-05-31 09:24:46 +00:00
|
|
|
string::size_type const start_script = pos1 + 4;
|
|
|
|
string::size_type const pos2 = command.find(' ', start_script);
|
|
|
|
string::size_type const size_script = pos2 == string::npos?
|
|
|
|
(command.size() - start_script) : pos2 - start_script;
|
|
|
|
|
|
|
|
// Does this script file exist?
|
2007-05-28 22:27:45 +00:00
|
|
|
string const script =
|
2010-04-21 01:19:09 +00:00
|
|
|
libFileSearch(".", command.substr(start_script, size_script)).absFileName();
|
2003-05-31 09:24:46 +00:00
|
|
|
|
|
|
|
if (script.empty()) {
|
|
|
|
// Replace "$$s/" with ""
|
|
|
|
command.erase(pos1, 4);
|
|
|
|
} else {
|
2014-01-31 03:59:33 +00:00
|
|
|
quote_style style = quote_shell;
|
2014-02-03 15:57:52 +00:00
|
|
|
if (prefixIs(command, os::python()))
|
2014-01-31 03:59:33 +00:00
|
|
|
style = quote_python;
|
|
|
|
|
2005-01-20 15:02:16 +00:00
|
|
|
// Replace "$$s/foo/some_script" with "<path to>/some_script".
|
2003-05-31 09:24:46 +00:00
|
|
|
string::size_type const size_replace = size_script + 4;
|
2006-09-11 16:07:49 +00:00
|
|
|
command.replace(pos1, size_replace, quoteName(script, style));
|
2003-05-31 09:24:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return command;
|
2002-03-27 00:05:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-09 11:05:50 +00:00
|
|
|
static string createTempFile(QString const & mask)
|
|
|
|
{
|
|
|
|
// FIXME: This is not safe. QTemporaryFile creates a file in open(),
|
|
|
|
// but the file is deleted when qt_tmp goes out of scope.
|
|
|
|
// Therefore the next call to createTempFile() may create the
|
|
|
|
// same file again. To make this safe the QTemporaryFile object
|
|
|
|
// needs to be kept for the whole life time of the temp file name.
|
|
|
|
// This could be achieved by creating a class TempDir (like
|
|
|
|
// TempFile, but using a currentlky non-existing
|
|
|
|
// QTemporaryDirectory object).
|
|
|
|
QTemporaryFile qt_tmp(mask + ".XXXXXXXXXXXX");
|
|
|
|
if (qt_tmp.open()) {
|
|
|
|
string const temp_file = fromqstr(qt_tmp.fileName());
|
|
|
|
LYXERR(Debug::FILES, "Temporary file `" << temp_file << "' created.");
|
|
|
|
return temp_file;
|
|
|
|
}
|
|
|
|
LYXERR(Debug::FILES, "Unable to create temporary file with following template: "
|
|
|
|
<< qt_tmp.fileTemplate());
|
|
|
|
return string();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-10-20 10:03:45 +00:00
|
|
|
static FileName createTmpDir(FileName const & tempdir, string const & mask)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2007-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::FILES, "createTmpDir: tempdir=`" << tempdir << "'\n"
|
|
|
|
<< "createTmpDir: mask=`" << mask << '\'');
|
2002-03-21 17:09:55 +00:00
|
|
|
|
2014-06-09 11:05:50 +00:00
|
|
|
QFileInfo tmp_fi(QDir(toqstr(tempdir.absFileName())), toqstr(mask));
|
|
|
|
FileName const tmpfl(createTempFile(tmp_fi.absoluteFilePath()));
|
2002-03-21 17:09:55 +00:00
|
|
|
|
2007-12-14 22:33:15 +00:00
|
|
|
if (tmpfl.empty() || !tmpfl.createDirectory(0700)) {
|
2008-07-23 09:27:33 +00:00
|
|
|
LYXERR0("LyX could not create temporary directory in " << tempdir
|
2008-06-09 09:57:10 +00:00
|
|
|
<< "'");
|
2006-11-26 21:30:39 +00:00
|
|
|
return FileName();
|
2004-02-25 12:00:53 +00:00
|
|
|
}
|
2003-03-29 09:02:08 +00:00
|
|
|
|
2006-12-27 10:56:11 +00:00
|
|
|
return tmpfl;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-26 21:30:39 +00:00
|
|
|
FileName const createLyXTmpDir(FileName const & deflt)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2008-06-09 14:33:31 +00:00
|
|
|
if (deflt.empty() || deflt == package().system_temp_dir())
|
|
|
|
return createTmpDir(package().system_temp_dir(), "lyx_tmpdir");
|
2007-11-07 19:52:11 +00:00
|
|
|
|
2010-09-06 07:10:35 +00:00
|
|
|
if (deflt.createDirectory(0777))
|
2007-11-07 19:52:11 +00:00
|
|
|
return deflt;
|
|
|
|
|
|
|
|
if (deflt.isDirWritable()) {
|
|
|
|
// deflt could not be created because it
|
|
|
|
// did exist already, so let's create our own
|
|
|
|
// dir inside deflt.
|
|
|
|
return createTmpDir(deflt, "lyx_tmpdir");
|
1999-09-27 18:44:28 +00:00
|
|
|
} else {
|
2015-02-09 23:42:35 +00:00
|
|
|
// some other error occurred.
|
2008-06-09 14:33:31 +00:00
|
|
|
return createTmpDir(package().system_temp_dir(), "lyx_tmpdir");
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Strip filename from path name
|
2006-04-08 22:31:11 +00:00
|
|
|
string const onlyPath(string const & filename)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
|
|
|
// If empty filename, return empty
|
2005-07-14 22:09:22 +00:00
|
|
|
if (filename.empty())
|
|
|
|
return filename;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
// Find last / or start of filename
|
2007-11-07 19:52:11 +00:00
|
|
|
size_t j = filename.rfind('/');
|
2005-07-14 22:09:22 +00:00
|
|
|
return j == string::npos ? "./" : filename.substr(0, j + 1);
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Convert relative path into absolute path based on a basepath.
|
|
|
|
// If relpath is absolute, just use that.
|
|
|
|
// If basepath is empty, use CWD as base.
|
2008-01-26 20:25:25 +00:00
|
|
|
// Note that basePath can be a relative path, in the sense that it may
|
|
|
|
// not begin with "/" (e.g.), but it should NOT contain such constructs
|
|
|
|
// as "/../".
|
|
|
|
// FIXME It might be nice if the code didn't simply assume that.
|
2006-12-27 10:56:11 +00:00
|
|
|
FileName const makeAbsPath(string const & relPath, string const & basePath)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
|
|
|
// checks for already absolute path
|
2009-05-02 17:12:31 +00:00
|
|
|
if (FileName::isAbsolute(relPath))
|
|
|
|
return FileName(relPath);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
// Copies given paths
|
2006-04-08 22:31:11 +00:00
|
|
|
string tempRel = os::internal_path(relPath);
|
2001-05-17 15:11:01 +00:00
|
|
|
// Since TempRel is NOT absolute, we can safely replace "//" with "/"
|
2006-04-08 22:31:11 +00:00
|
|
|
tempRel = subst(tempRel, "//", "/");
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2006-04-08 22:31:11 +00:00
|
|
|
string tempBase;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2009-05-02 17:12:31 +00:00
|
|
|
if (FileName::isAbsolute(basePath))
|
2006-04-08 22:31:11 +00:00
|
|
|
tempBase = basePath;
|
2001-05-17 15:11:01 +00:00
|
|
|
else
|
2010-04-21 01:19:09 +00:00
|
|
|
tempBase = addPath(FileName::getcwd().absFileName(), basePath);
|
2002-03-21 17:09:55 +00:00
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
// Handle /./ at the end of the path
|
2006-04-08 22:31:11 +00:00
|
|
|
while (suffixIs(tempBase, "/./"))
|
|
|
|
tempBase.erase(tempBase.length() - 2);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
// processes relative path
|
2006-04-08 22:31:11 +00:00
|
|
|
string rTemp = tempRel;
|
|
|
|
string temp;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2008-01-26 20:41:19 +00:00
|
|
|
// Check for a leading "~"
|
|
|
|
// Split by first /
|
|
|
|
rTemp = split(rTemp, temp, '/');
|
|
|
|
if (temp == "~") {
|
2011-04-13 19:11:55 +00:00
|
|
|
tempBase = Package::get_home_dir().absFileName();
|
2008-01-26 20:41:19 +00:00
|
|
|
tempRel = rTemp;
|
|
|
|
}
|
|
|
|
|
|
|
|
rTemp = tempRel;
|
2006-04-08 22:31:11 +00:00
|
|
|
while (!rTemp.empty()) {
|
1999-09-27 18:44:28 +00:00
|
|
|
// Split by next /
|
2006-04-08 22:31:11 +00:00
|
|
|
rTemp = split(rTemp, temp, '/');
|
2002-03-21 17:09:55 +00:00
|
|
|
|
2006-04-08 22:31:11 +00:00
|
|
|
if (temp == ".") continue;
|
|
|
|
if (temp == "..") {
|
1999-09-27 18:44:28 +00:00
|
|
|
// Remove one level of TempBase
|
2008-01-26 20:41:19 +00:00
|
|
|
if (tempBase.length() <= 1) {
|
|
|
|
//this is supposed to be an absolute path, so...
|
|
|
|
tempBase = "/";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
//erase a trailing slash if there is one
|
|
|
|
if (suffixIs(tempBase, "/"))
|
|
|
|
tempBase.erase(tempBase.length() - 1, string::npos);
|
|
|
|
|
|
|
|
string::size_type i = tempBase.length() - 1;
|
2006-04-08 22:31:11 +00:00
|
|
|
while (i > 0 && tempBase[i] != '/')
|
2005-07-14 22:09:22 +00:00
|
|
|
--i;
|
1999-11-01 04:22:28 +00:00
|
|
|
if (i > 0)
|
2006-04-08 22:31:11 +00:00
|
|
|
tempBase.erase(i, string::npos);
|
1999-09-27 18:44:28 +00:00
|
|
|
else
|
2008-01-26 20:41:19 +00:00
|
|
|
tempBase = '/';
|
2006-04-08 22:31:11 +00:00
|
|
|
} else if (temp.empty() && !rTemp.empty()) {
|
|
|
|
tempBase = os::current_root() + rTemp;
|
|
|
|
rTemp.erase();
|
1999-09-27 18:44:28 +00:00
|
|
|
} else {
|
|
|
|
// Add this piece to TempBase
|
2006-04-08 22:31:11 +00:00
|
|
|
if (!suffixIs(tempBase, '/'))
|
|
|
|
tempBase += '/';
|
|
|
|
tempBase += temp;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns absolute path
|
2007-11-28 15:25:07 +00:00
|
|
|
return FileName(tempBase);
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Correctly append filename to the pathname.
|
|
|
|
// If pathname is '.', then don't use pathname.
|
|
|
|
// Chops any path of filename.
|
2006-04-08 22:31:11 +00:00
|
|
|
string const addName(string const & path, string const & fname)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2010-04-21 01:19:28 +00:00
|
|
|
string const basename = onlyFileName(fname);
|
1999-10-02 16:21:10 +00:00
|
|
|
string buf;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
if (path != "." && path != "./" && !path.empty()) {
|
2004-12-20 16:59:33 +00:00
|
|
|
buf = os::internal_path(path);
|
1999-10-02 16:21:10 +00:00
|
|
|
if (!suffixIs(path, '/'))
|
1999-09-27 18:44:28 +00:00
|
|
|
buf += '/';
|
|
|
|
}
|
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
return buf + basename;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Strips path from filename
|
2010-04-21 01:19:28 +00:00
|
|
|
string const onlyFileName(string const & fname)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
1999-10-20 14:35:05 +00:00
|
|
|
if (fname.empty())
|
|
|
|
return fname;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
string::size_type j = fname.rfind('/');
|
|
|
|
if (j == string::npos) // no '/' in fname
|
|
|
|
return fname;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
// Strip to basename
|
1999-10-02 16:21:10 +00:00
|
|
|
return fname.substr(j + 1);
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-09-29 21:44:34 +00:00
|
|
|
// Search the string for ${VAR} and $VAR and replace VAR using getenv.
|
2006-04-08 22:31:11 +00:00
|
|
|
string const replaceEnvironmentPath(string const & path)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2003-09-29 21:44:34 +00:00
|
|
|
// ${VAR} is defined as
|
2003-09-25 23:01:43 +00:00
|
|
|
// $\{[A-Za-z_][A-Za-z_0-9]*\}
|
2003-10-16 16:41:32 +00:00
|
|
|
static string const envvar_br = "[$]\\{([A-Za-z_][A-Za-z_0-9]*)\\}";
|
2003-09-25 23:01:43 +00:00
|
|
|
|
2003-09-29 21:44:34 +00:00
|
|
|
// $VAR is defined as:
|
2010-12-16 17:51:30 +00:00
|
|
|
// $[A-Za-z_][A-Za-z_0-9]*
|
2003-10-16 16:41:32 +00:00
|
|
|
static string const envvar = "[$]([A-Za-z_][A-Za-z_0-9]*)";
|
2003-09-29 21:44:34 +00:00
|
|
|
|
2013-10-07 22:59:05 +00:00
|
|
|
static regex const envvar_br_re("(.*)" + envvar_br + "(.*)");
|
|
|
|
static regex const envvar_re("(.*)" + envvar + "(.*)");
|
2011-01-25 21:10:49 +00:00
|
|
|
string result = path;
|
2003-09-25 23:01:43 +00:00
|
|
|
while (1) {
|
2011-02-16 18:59:19 +00:00
|
|
|
smatch what;
|
2011-02-17 19:39:07 +00:00
|
|
|
if (!regex_match(result, what, envvar_br_re)) {
|
|
|
|
if (!regex_match(result, what, envvar_re))
|
2003-09-29 21:44:34 +00:00
|
|
|
break;
|
|
|
|
}
|
2009-05-09 17:06:54 +00:00
|
|
|
string env_var = getEnv(what.str(2));
|
2011-01-25 21:10:49 +00:00
|
|
|
result = what.str(1) + env_var + what.str(3);
|
2002-03-21 17:09:55 +00:00
|
|
|
}
|
2003-09-25 23:01:43 +00:00
|
|
|
return result;
|
2002-11-06 11:15:24 +00:00
|
|
|
}
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
|
2011-09-25 13:35:42 +00:00
|
|
|
// Return a command prefix for setting the environment of the TeX engine.
|
Fix bug #4812 (Layout in local directory lost on Save As, Copying)
The "save-as" part of the bug is fixed by extending the \textclass tag
such that, if a local layout file is used, its path relative to the
document directory is now stored together with the name. If a relative
path cannot be used, an absolute one is used but, in this case, the
document is not usable on a different platform.
The "copy" part is fixed by introducing a new \origin tag, which is
written when the file is saved. This tag stores the absolute path of
the document directory. If the document is manually copied to a
different location, the local layout file is retrivied by using
\origin (which is only updated on save).
This new tag may prove useful also for locating other files when the
document is manually moved to a different directory.
As in the original implementation the files needed for the layout
(for example, a latex class) had to be in the same directory as the
layout file, this directory has also to be added to TEXINPUTS.
2015-05-13 19:40:51 +00:00
|
|
|
string latexEnvCmdPrefix(string const & path, string const & lpath)
|
2011-09-25 13:35:42 +00:00
|
|
|
{
|
Fix bug #4812 (Layout in local directory lost on Save As, Copying)
The "save-as" part of the bug is fixed by extending the \textclass tag
such that, if a local layout file is used, its path relative to the
document directory is now stored together with the name. If a relative
path cannot be used, an absolute one is used but, in this case, the
document is not usable on a different platform.
The "copy" part is fixed by introducing a new \origin tag, which is
written when the file is saved. This tag stores the absolute path of
the document directory. If the document is manually copied to a
different location, the local layout file is retrivied by using
\origin (which is only updated on save).
This new tag may prove useful also for locating other files when the
document is manually moved to a different directory.
As in the original implementation the files needed for the layout
(for example, a latex class) had to be in the same directory as the
layout file, this directory has also to be added to TEXINPUTS.
2015-05-13 19:40:51 +00:00
|
|
|
bool use_lpath = !(lpath.empty() || lpath == "." || lpath == "./");
|
|
|
|
|
|
|
|
if (path.empty() || (lyxrc.texinputs_prefix.empty() && !use_lpath))
|
2011-09-25 13:35:42 +00:00
|
|
|
return string();
|
|
|
|
|
Fix bug #4812 (Layout in local directory lost on Save As, Copying)
The "save-as" part of the bug is fixed by extending the \textclass tag
such that, if a local layout file is used, its path relative to the
document directory is now stored together with the name. If a relative
path cannot be used, an absolute one is used but, in this case, the
document is not usable on a different platform.
The "copy" part is fixed by introducing a new \origin tag, which is
written when the file is saved. This tag stores the absolute path of
the document directory. If the document is manually copied to a
different location, the local layout file is retrivied by using
\origin (which is only updated on save).
This new tag may prove useful also for locating other files when the
document is manually moved to a different directory.
As in the original implementation the files needed for the layout
(for example, a latex class) had to be in the same directory as the
layout file, this directory has also to be added to TEXINPUTS.
2015-05-13 19:40:51 +00:00
|
|
|
string texinputs_prefix = lyxrc.texinputs_prefix.empty() ? string()
|
|
|
|
: os::latex_path_list(
|
2011-09-25 13:35:42 +00:00
|
|
|
replaceCurdirPath(path, lyxrc.texinputs_prefix));
|
2015-11-03 22:28:36 +00:00
|
|
|
string const allother_prefix = os::latex_path_list(path);
|
2011-09-25 13:35:42 +00:00
|
|
|
string const sep = string(1, os::path_separator(os::TEXENGINE));
|
|
|
|
string const texinputs = getEnv("TEXINPUTS");
|
2015-11-03 22:28:36 +00:00
|
|
|
string const bibinputs = getEnv("BIBINPUTS");
|
|
|
|
string const bstinputs = getEnv("BSTINPUTS");
|
|
|
|
string const texfonts = getEnv("TEXFONTS");
|
2011-09-25 13:35:42 +00:00
|
|
|
|
Fix bug #4812 (Layout in local directory lost on Save As, Copying)
The "save-as" part of the bug is fixed by extending the \textclass tag
such that, if a local layout file is used, its path relative to the
document directory is now stored together with the name. If a relative
path cannot be used, an absolute one is used but, in this case, the
document is not usable on a different platform.
The "copy" part is fixed by introducing a new \origin tag, which is
written when the file is saved. This tag stores the absolute path of
the document directory. If the document is manually copied to a
different location, the local layout file is retrivied by using
\origin (which is only updated on save).
This new tag may prove useful also for locating other files when the
document is manually moved to a different directory.
As in the original implementation the files needed for the layout
(for example, a latex class) had to be in the same directory as the
layout file, this directory has also to be added to TEXINPUTS.
2015-05-13 19:40:51 +00:00
|
|
|
if (use_lpath) {
|
|
|
|
string const abslpath = FileName::isAbsolute(lpath)
|
|
|
|
? os::latex_path(lpath)
|
|
|
|
: os::latex_path(FileName(path + "/" + lpath).realPath());
|
|
|
|
if (texinputs_prefix.empty())
|
|
|
|
texinputs_prefix = abslpath;
|
|
|
|
else if (suffixIs(texinputs_prefix, sep))
|
|
|
|
texinputs_prefix.append(abslpath + sep);
|
|
|
|
else
|
|
|
|
texinputs_prefix.append(sep + abslpath);
|
|
|
|
}
|
|
|
|
|
2011-09-25 13:35:42 +00:00
|
|
|
if (os::shell() == os::UNIX)
|
2011-09-25 20:33:06 +00:00
|
|
|
return "env TEXINPUTS=\"." + sep + texinputs_prefix
|
2015-11-03 22:28:36 +00:00
|
|
|
+ sep + texinputs + "\" "
|
|
|
|
+ "BIBINPUTS=\"." + sep + allother_prefix
|
|
|
|
+ sep + bibinputs + "\" "
|
|
|
|
+ "BSTINPUTS=\"." + sep + allother_prefix
|
|
|
|
+ sep + bstinputs + "\" "
|
|
|
|
+ "TEXFONTS=\"." + sep + allother_prefix
|
|
|
|
+ sep + texfonts + "\" ";
|
2011-09-25 13:35:42 +00:00
|
|
|
else
|
2015-11-03 22:28:36 +00:00
|
|
|
// NOTE: the dummy blank dirs are necessary to force the
|
2015-03-16 09:50:02 +00:00
|
|
|
// QProcess parser to quote the argument (see bug 9453)
|
|
|
|
return "cmd /d /c set \"TEXINPUTS=." + sep + " "
|
2015-11-03 22:28:36 +00:00
|
|
|
+ sep + texinputs_prefix
|
|
|
|
+ sep + texinputs + "\" & "
|
|
|
|
+ "set \"BIBINPUTS=." + sep + " "
|
|
|
|
+ sep + allother_prefix
|
|
|
|
+ sep + bibinputs + "\" & "
|
|
|
|
+ "set \"BSTINPUTS=." + sep + " "
|
|
|
|
+ sep + allother_prefix
|
|
|
|
+ sep + bstinputs + "\" & "
|
|
|
|
+ "set \"TEXFONTS=." + sep + " "
|
|
|
|
+ sep + allother_prefix
|
|
|
|
+ sep + texfonts + "\" & ";
|
2011-09-25 13:35:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
Introduce the possibility of setting a prefix for the TEXINPUTS environment
variable. This is done in the preferences, much like as the PATH prefix.
A single '.' in the paths will get replaced with the current document dir
and also non-absolute paths will be prefixed with that dir.
The default semantics of TEXINPUTS apply, such that, for example, if a
path is terminated with a double slash, all subdirectories will be also
searched by both the TeX engine and ancillary programs such as dvi
previewers or dvips. As an example, if the prefix is set to ".:figs", the
TEXINPUTS variable will be set as ".:<docdir>:<docdir>/figs:$ORIGTEXINPUTS",
where <docdir> is the document directory.
The initial '.' is necessary to address the actual current dir (this will
be the temp dir at preview time), while if TEXINPUTS was initially unset,
such that $ORIGTEXINPUTS is empty, a colon (or semicolon on Windows) will
end the path list. This is very important, because we don't want to replace
the system directories but to complement them and, in order to do that, an
empty element has to be present in the list. Indeed, according to the
TEXINPUTS semantics, an empty element means the standard search path.
This works whether TEXINPUTS is originally set or not, because if the
original TEXINPUTS starts with a colon (meaning that the standard search
path is wanted there) we will have an empty element at that point,
otherwise the final colon will simply serve as a path separator.
Of course, on Windows a ';' has to be used as a path separator. LyX will
take care of transforming the platform path list into one understandable
by the TeX engine. For example, this will be the case for a Cygwin version
of LyX using a native Windows TeX engine or viceversa. I tested all of
this and it works for me.
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@38681 a592a061-630c-0410-9148-cb99ea01b6c8
2011-05-09 23:25:51 +00:00
|
|
|
// Replace current directory in all elements of a path list with a given path.
|
|
|
|
string const replaceCurdirPath(string const & path, string const & pathlist)
|
|
|
|
{
|
|
|
|
string const oldpathlist = replaceEnvironmentPath(pathlist);
|
|
|
|
char const sep = os::path_separator();
|
|
|
|
string newpathlist;
|
|
|
|
|
|
|
|
for (size_t i = 0, k = 0; i != string::npos; k = i) {
|
|
|
|
i = oldpathlist.find(sep, i);
|
|
|
|
string p = oldpathlist.substr(k, i - k);
|
|
|
|
if (FileName::isAbsolute(p)) {
|
|
|
|
newpathlist += p;
|
|
|
|
} else if (i > k) {
|
|
|
|
size_t offset = 0;
|
|
|
|
if (p == ".") {
|
|
|
|
offset = 1;
|
|
|
|
} else if (prefixIs(p, "./")) {
|
|
|
|
offset = 2;
|
|
|
|
while (p[offset] == '/')
|
|
|
|
++offset;
|
|
|
|
}
|
|
|
|
newpathlist += addPath(path, p.substr(offset));
|
|
|
|
if (suffixIs(p, "//"))
|
|
|
|
newpathlist += '/';
|
|
|
|
}
|
|
|
|
if (i != string::npos) {
|
|
|
|
newpathlist += sep;
|
2014-12-29 22:46:25 +00:00
|
|
|
// Stop here if the last element is empty
|
Introduce the possibility of setting a prefix for the TEXINPUTS environment
variable. This is done in the preferences, much like as the PATH prefix.
A single '.' in the paths will get replaced with the current document dir
and also non-absolute paths will be prefixed with that dir.
The default semantics of TEXINPUTS apply, such that, for example, if a
path is terminated with a double slash, all subdirectories will be also
searched by both the TeX engine and ancillary programs such as dvi
previewers or dvips. As an example, if the prefix is set to ".:figs", the
TEXINPUTS variable will be set as ".:<docdir>:<docdir>/figs:$ORIGTEXINPUTS",
where <docdir> is the document directory.
The initial '.' is necessary to address the actual current dir (this will
be the temp dir at preview time), while if TEXINPUTS was initially unset,
such that $ORIGTEXINPUTS is empty, a colon (or semicolon on Windows) will
end the path list. This is very important, because we don't want to replace
the system directories but to complement them and, in order to do that, an
empty element has to be present in the list. Indeed, according to the
TEXINPUTS semantics, an empty element means the standard search path.
This works whether TEXINPUTS is originally set or not, because if the
original TEXINPUTS starts with a colon (meaning that the standard search
path is wanted there) we will have an empty element at that point,
otherwise the final colon will simply serve as a path separator.
Of course, on Windows a ';' has to be used as a path separator. LyX will
take care of transforming the platform path list into one understandable
by the TeX engine. For example, this will be the case for a Cygwin version
of LyX using a native Windows TeX engine or viceversa. I tested all of
this and it works for me.
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@38681 a592a061-630c-0410-9148-cb99ea01b6c8
2011-05-09 23:25:51 +00:00
|
|
|
if (++i == oldpathlist.length())
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return newpathlist;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
// Make relative path out of two absolute paths
|
2007-03-27 07:24:16 +00:00
|
|
|
docstring const makeRelPath(docstring const & abspath, docstring const & basepath)
|
1999-09-27 18:44:28 +00:00
|
|
|
// Makes relative path out of absolute path. If it is deeper than basepath,
|
|
|
|
// it's easy. If basepath and abspath share something (they are all deeper
|
|
|
|
// than some directory), it'll be rendered using ..'s. If they are completely
|
|
|
|
// different, then the absolute path will be used as relative path.
|
|
|
|
{
|
2007-03-27 07:24:16 +00:00
|
|
|
docstring::size_type const abslen = abspath.length();
|
|
|
|
docstring::size_type const baselen = basepath.length();
|
2001-05-17 15:11:01 +00:00
|
|
|
|
2007-03-27 07:24:16 +00:00
|
|
|
docstring::size_type i = os::common_path(abspath, basepath);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
if (i == 0) {
|
|
|
|
// actually no match - cannot make it relative
|
|
|
|
return abspath;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Count how many dirs there are in basepath above match
|
|
|
|
// and append as many '..''s into relpath
|
2007-03-27 07:24:16 +00:00
|
|
|
docstring buf;
|
|
|
|
docstring::size_type j = i;
|
1999-09-27 18:44:28 +00:00
|
|
|
while (j < baselen) {
|
|
|
|
if (basepath[j] == '/') {
|
2001-10-24 13:21:20 +00:00
|
|
|
if (j + 1 == baselen)
|
|
|
|
break;
|
1999-09-27 18:44:28 +00:00
|
|
|
buf += "../";
|
|
|
|
}
|
|
|
|
++j;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append relative stuff from common directory to abspath
|
2001-10-24 13:21:20 +00:00
|
|
|
if (abspath[i] == '/')
|
|
|
|
++i;
|
1999-12-07 00:44:53 +00:00
|
|
|
for (; i < abslen; ++i)
|
1999-09-27 18:44:28 +00:00
|
|
|
buf += abspath[i];
|
|
|
|
// Remove trailing /
|
1999-10-02 16:21:10 +00:00
|
|
|
if (suffixIs(buf, '/'))
|
|
|
|
buf.erase(buf.length() - 1);
|
1999-09-27 18:44:28 +00:00
|
|
|
// Substitute empty with .
|
|
|
|
if (buf.empty())
|
|
|
|
buf = '.';
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Append sub-directory(ies) to a path in an intelligent way
|
2006-04-08 22:31:11 +00:00
|
|
|
string const addPath(string const & path, string const & path_2)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
1999-10-02 16:21:10 +00:00
|
|
|
string buf;
|
2004-12-20 16:59:33 +00:00
|
|
|
string const path2 = os::internal_path(path_2);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
if (!path.empty() && path != "." && path != "./") {
|
2004-12-20 16:59:33 +00:00
|
|
|
buf = os::internal_path(path);
|
1999-10-02 16:21:10 +00:00
|
|
|
if (path[path.length() - 1] != '/')
|
1999-09-27 18:44:28 +00:00
|
|
|
buf += '/';
|
|
|
|
}
|
|
|
|
|
2001-05-17 15:11:01 +00:00
|
|
|
if (!path2.empty()) {
|
2002-03-21 17:09:55 +00:00
|
|
|
string::size_type const p2start = path2.find_first_not_of('/');
|
2001-05-17 15:11:01 +00:00
|
|
|
string::size_type const p2end = path2.find_last_not_of('/');
|
|
|
|
string const tmp = path2.substr(p2start, p2end - p2start + 1);
|
|
|
|
buf += tmp + '/';
|
|
|
|
}
|
1999-09-27 18:44:28 +00:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-04-08 22:31:11 +00:00
|
|
|
string const changeExtension(string const & oldname, string const & extension)
|
1999-10-02 16:21:10 +00:00
|
|
|
{
|
2000-09-26 13:54:57 +00:00
|
|
|
string::size_type const last_slash = oldname.rfind('/');
|
2000-02-07 20:17:03 +00:00
|
|
|
string::size_type last_dot = oldname.rfind('.');
|
|
|
|
if (last_dot < last_slash && last_slash != string::npos)
|
|
|
|
last_dot = string::npos;
|
2002-03-21 17:09:55 +00:00
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
string ext;
|
|
|
|
// Make sure the extension starts with a dot
|
1999-09-27 18:44:28 +00:00
|
|
|
if (!extension.empty() && extension[0] != '.')
|
2002-11-27 10:30:28 +00:00
|
|
|
ext= '.' + extension;
|
1999-09-27 18:44:28 +00:00
|
|
|
else
|
|
|
|
ext = extension;
|
2000-05-11 16:12:46 +00:00
|
|
|
|
2004-12-20 16:59:33 +00:00
|
|
|
return os::internal_path(oldname.substr(0, last_dot) + ext);
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
2000-09-14 17:53:12 +00:00
|
|
|
|
2006-04-01 10:49:58 +00:00
|
|
|
string const removeExtension(string const & name)
|
|
|
|
{
|
2006-04-08 22:31:11 +00:00
|
|
|
return changeExtension(name, string());
|
2006-04-01 10:49:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-12-21 12:04:01 +00:00
|
|
|
string const addExtension(string const & name, string const & extension)
|
|
|
|
{
|
|
|
|
if (!extension.empty() && extension[0] != '.')
|
|
|
|
return name + '.' + extension;
|
|
|
|
return name + extension;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-08-30 03:40:51 +00:00
|
|
|
/// Return the extension of the file (not including the .)
|
2006-04-08 22:31:11 +00:00
|
|
|
string const getExtension(string const & name)
|
2000-08-30 03:40:51 +00:00
|
|
|
{
|
2000-09-26 13:54:57 +00:00
|
|
|
string::size_type const last_slash = name.rfind('/');
|
|
|
|
string::size_type const last_dot = name.rfind('.');
|
2000-08-30 03:40:51 +00:00
|
|
|
if (last_dot != string::npos &&
|
|
|
|
(last_slash == string::npos || last_dot > last_slash))
|
|
|
|
return name.substr(last_dot + 1,
|
|
|
|
name.length() - (last_dot + 1));
|
|
|
|
else
|
|
|
|
return string();
|
|
|
|
}
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2004-10-29 15:47:55 +00:00
|
|
|
|
2003-09-03 17:23:38 +00:00
|
|
|
string const unzippedFileName(string const & zipped_file)
|
|
|
|
{
|
2006-04-08 22:31:11 +00:00
|
|
|
string const ext = getExtension(zipped_file);
|
2003-09-03 17:23:38 +00:00
|
|
|
if (ext == "gz" || ext == "z" || ext == "Z")
|
2006-04-08 22:31:11 +00:00
|
|
|
return changeExtension(zipped_file, string());
|
2015-03-12 23:34:53 +00:00
|
|
|
else if (ext == "svgz")
|
|
|
|
return changeExtension(zipped_file, "svg");
|
2010-04-21 01:19:28 +00:00
|
|
|
return onlyPath(zipped_file) + "unzipped_" + onlyFileName(zipped_file);
|
2003-09-03 17:23:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-26 21:30:39 +00:00
|
|
|
FileName const unzipFile(FileName const & zipped_file, string const & unzipped_file)
|
2002-02-07 19:37:34 +00:00
|
|
|
{
|
2006-11-26 21:30:39 +00:00
|
|
|
FileName const tempfile = FileName(unzipped_file.empty() ?
|
|
|
|
unzippedFileName(zipped_file.toFilesystemEncoding()) :
|
|
|
|
unzipped_file);
|
2002-02-16 18:34:30 +00:00
|
|
|
// Run gunzip
|
2006-11-26 21:30:39 +00:00
|
|
|
string const command = "gunzip -c " +
|
|
|
|
zipped_file.toFilesystemEncoding() + " > " +
|
|
|
|
tempfile.toFilesystemEncoding();
|
2002-02-18 19:13:48 +00:00
|
|
|
Systemcall one;
|
|
|
|
one.startscript(Systemcall::Wait, command);
|
2002-02-16 18:34:30 +00:00
|
|
|
// test that command was executed successfully (anon)
|
|
|
|
// yes, please do. (Lgb)
|
|
|
|
return tempfile;
|
2002-02-07 19:37:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-11 08:54:10 +00:00
|
|
|
docstring const makeDisplayPath(string const & path, unsigned int threshold)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2003-04-08 00:06:52 +00:00
|
|
|
string str = path;
|
2007-02-28 21:48:04 +00:00
|
|
|
|
|
|
|
// If file is from LyXDir, display it as if it were relative.
|
2010-04-21 01:19:09 +00:00
|
|
|
string const system = package().system_support().absFileName();
|
2007-03-14 00:36:25 +00:00
|
|
|
if (prefixIs(str, system) && str != system)
|
2007-05-28 22:27:45 +00:00
|
|
|
return from_utf8("[" + str.erase(0, system.length()) + "]");
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2003-04-08 00:06:52 +00:00
|
|
|
// replace /home/blah with ~/
|
2011-04-13 19:11:55 +00:00
|
|
|
string const home = Package::get_home_dir().absFileName();
|
2005-09-07 09:44:58 +00:00
|
|
|
if (!home.empty() && prefixIs(str, home))
|
2003-04-08 00:06:52 +00:00
|
|
|
str = subst(str, home, "~");
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2003-04-08 00:06:52 +00:00
|
|
|
if (str.length() <= threshold)
|
2007-01-18 20:47:27 +00:00
|
|
|
return from_utf8(os::external_path(str));
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2003-04-08 00:06:52 +00:00
|
|
|
string const prefix = ".../";
|
2011-11-06 22:36:14 +00:00
|
|
|
docstring dstr = from_utf8(str);
|
|
|
|
docstring temp;
|
2002-03-21 17:09:55 +00:00
|
|
|
|
2011-11-06 22:36:14 +00:00
|
|
|
while (dstr.length() > threshold)
|
|
|
|
dstr = split(dstr, temp, '/');
|
2002-03-21 17:09:55 +00:00
|
|
|
|
2003-04-08 00:06:52 +00:00
|
|
|
// Did we shorten everything away?
|
2011-11-06 22:36:14 +00:00
|
|
|
if (dstr.empty()) {
|
2003-04-08 00:06:52 +00:00
|
|
|
// Yes, filename itself is too long.
|
|
|
|
// Pick the start and the end of the filename.
|
2015-10-21 14:02:12 +00:00
|
|
|
docstring fstr = from_utf8(onlyFileName(path));
|
|
|
|
dstr = fstr;
|
|
|
|
if (support::truncateWithEllipsis(dstr, threshold / 2))
|
|
|
|
dstr += fstr.substr(fstr.length() - threshold / 2 - 2,
|
|
|
|
docstring::npos);
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
2003-04-08 00:06:52 +00:00
|
|
|
|
2011-11-06 22:36:14 +00:00
|
|
|
return from_utf8(os::external_path(prefix + to_utf8(dstr)));
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
1999-11-02 06:42:25 +00:00
|
|
|
|
2010-09-06 07:11:27 +00:00
|
|
|
#ifdef HAVE_READLINK
|
2007-04-06 13:09:54 +00:00
|
|
|
bool readLink(FileName const & file, FileName & link)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2007-04-06 13:09:54 +00:00
|
|
|
string const encoded = file.toFilesystemEncoding();
|
2011-07-17 12:32:00 +00:00
|
|
|
#ifdef HAVE_DEF_PATH_MAX
|
|
|
|
char linkbuffer[PATH_MAX + 1];
|
2014-05-20 11:46:22 +00:00
|
|
|
ssize_t const nRead = ::readlink(encoded.c_str(),
|
2002-07-17 21:43:06 +00:00
|
|
|
linkbuffer, sizeof(linkbuffer) - 1);
|
1999-09-27 18:44:28 +00:00
|
|
|
if (nRead <= 0)
|
|
|
|
return false;
|
2002-07-17 21:43:06 +00:00
|
|
|
linkbuffer[nRead] = '\0'; // terminator
|
2011-07-17 12:32:00 +00:00
|
|
|
#else
|
|
|
|
vector<char> buf(1024);
|
|
|
|
int nRead = -1;
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
nRead = ::readlink(encoded.c_str(), &buf[0], buf.size() - 1);
|
|
|
|
if (nRead < 0) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-05-20 11:46:22 +00:00
|
|
|
if (static_cast<size_t>(nRead) < buf.size() - 1) {
|
2011-07-17 12:32:00 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
buf.resize(buf.size() * 2);
|
|
|
|
}
|
|
|
|
buf[nRead] = '\0'; // terminator
|
|
|
|
const char * linkbuffer = &buf[0];
|
|
|
|
#endif
|
2010-04-21 01:19:09 +00:00
|
|
|
link = makeAbsPath(linkbuffer, onlyPath(file.absFileName()));
|
1999-09-27 18:44:28 +00:00
|
|
|
return true;
|
2010-09-06 07:11:27 +00:00
|
|
|
}
|
2005-01-04 13:24:56 +00:00
|
|
|
#else
|
2010-09-06 07:11:27 +00:00
|
|
|
bool readLink(FileName const &, FileName &)
|
|
|
|
{
|
2005-01-04 13:24:56 +00:00
|
|
|
return false;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
2010-09-06 07:11:27 +00:00
|
|
|
#endif
|
1999-11-24 22:14:46 +00:00
|
|
|
|
|
|
|
|
2006-04-08 22:31:11 +00:00
|
|
|
cmd_ret const runCommand(string const & cmd)
|
1999-11-24 22:14:46 +00:00
|
|
|
{
|
2004-04-05 08:52:45 +00:00
|
|
|
// FIXME: replace all calls to RunCommand with ForkedCall
|
2007-04-26 04:41:58 +00:00
|
|
|
// (if the output is not needed) or the code in ISpell.cpp
|
2004-04-05 06:46:22 +00:00
|
|
|
// (if the output is needed).
|
2004-04-20 08:51:15 +00:00
|
|
|
|
1999-11-24 22:14:46 +00:00
|
|
|
// One question is if we should use popen or
|
|
|
|
// create our own popen based on fork, exec, pipe
|
|
|
|
// of course the best would be to have a
|
|
|
|
// pstream (process stream), with the
|
2000-01-06 02:44:26 +00:00
|
|
|
// variants ipstream, opstream
|
2002-02-08 11:23:58 +00:00
|
|
|
|
2016-08-17 20:32:14 +00:00
|
|
|
if (verbose)
|
|
|
|
lyxerr << "\nRunning: " << cmd << endl;
|
|
|
|
else
|
|
|
|
LYXERR(Debug::INFO,"Running: " << cmd);
|
|
|
|
|
2010-06-28 17:45:04 +00:00
|
|
|
#if defined (_WIN32)
|
2010-09-06 07:10:35 +00:00
|
|
|
STARTUPINFO startup;
|
|
|
|
PROCESS_INFORMATION process;
|
|
|
|
SECURITY_ATTRIBUTES security;
|
2010-06-28 17:45:04 +00:00
|
|
|
HANDLE in, out;
|
|
|
|
FILE * inf = 0;
|
2011-08-23 18:45:46 +00:00
|
|
|
bool err2out = false;
|
2011-08-22 01:39:49 +00:00
|
|
|
string command;
|
|
|
|
string const infile = trim(split(cmd, command, '<'), " \"");
|
2011-08-23 18:45:46 +00:00
|
|
|
command = rtrim(command);
|
|
|
|
if (suffixIs(command, "2>&1")) {
|
|
|
|
command = rtrim(command, "2>&1");
|
|
|
|
err2out = true;
|
|
|
|
}
|
2011-08-28 16:53:38 +00:00
|
|
|
string const cmdarg = "/d /c " + command;
|
2011-08-22 01:39:49 +00:00
|
|
|
string const comspec = getEnv("COMSPEC");
|
2010-06-28 17:45:04 +00:00
|
|
|
|
2010-09-06 07:10:35 +00:00
|
|
|
security.nLength = sizeof(SECURITY_ATTRIBUTES);
|
|
|
|
security.bInheritHandle = TRUE;
|
|
|
|
security.lpSecurityDescriptor = NULL;
|
|
|
|
|
|
|
|
if (CreatePipe(&in, &out, &security, 0)) {
|
2010-06-28 17:45:04 +00:00
|
|
|
memset(&startup, 0, sizeof(STARTUPINFO));
|
|
|
|
memset(&process, 0, sizeof(PROCESS_INFORMATION));
|
2010-09-06 07:10:35 +00:00
|
|
|
|
2010-06-28 17:45:04 +00:00
|
|
|
startup.cb = sizeof(STARTUPINFO);
|
|
|
|
startup.dwFlags = STARTF_USESTDHANDLES;
|
2010-09-06 07:10:35 +00:00
|
|
|
|
2011-08-23 18:45:46 +00:00
|
|
|
startup.hStdError = err2out ? out : GetStdHandle(STD_ERROR_HANDLE);
|
2011-08-22 01:39:49 +00:00
|
|
|
startup.hStdInput = infile.empty()
|
|
|
|
? GetStdHandle(STD_INPUT_HANDLE)
|
|
|
|
: CreateFile(infile.c_str(), GENERIC_READ,
|
|
|
|
FILE_SHARE_READ, &security, OPEN_EXISTING,
|
|
|
|
FILE_ATTRIBUTE_NORMAL, NULL);
|
2010-06-28 17:45:04 +00:00
|
|
|
startup.hStdOutput = out;
|
|
|
|
|
2011-08-22 01:39:49 +00:00
|
|
|
if (startup.hStdInput != INVALID_HANDLE_VALUE &&
|
|
|
|
CreateProcess(comspec.c_str(), (LPTSTR)cmdarg.c_str(),
|
|
|
|
&security, &security, TRUE, CREATE_NO_WINDOW,
|
|
|
|
0, 0, &startup, &process)) {
|
2010-06-28 17:45:04 +00:00
|
|
|
|
|
|
|
CloseHandle(process.hThread);
|
2016-05-05 17:28:11 +00:00
|
|
|
int fno = _open_osfhandle((intptr_t)in, _O_RDONLY);
|
2010-06-28 17:45:04 +00:00
|
|
|
CloseHandle(out);
|
|
|
|
inf = _fdopen(fno, "r");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#elif defined (HAVE_POPEN)
|
2002-09-23 16:03:11 +00:00
|
|
|
FILE * inf = ::popen(cmd.c_str(), os::popen_read_mode());
|
2005-05-03 20:32:55 +00:00
|
|
|
#elif defined (HAVE__POPEN)
|
|
|
|
FILE * inf = ::_popen(cmd.c_str(), os::popen_read_mode());
|
|
|
|
#else
|
|
|
|
#error No popen() function.
|
|
|
|
#endif
|
2002-02-08 11:23:58 +00:00
|
|
|
|
2010-12-29 16:57:04 +00:00
|
|
|
// (Claus Hentschel) Check if popen was successful ;-)
|
2004-04-05 06:46:22 +00:00
|
|
|
if (!inf) {
|
|
|
|
lyxerr << "RunCommand:: could not start child process" << endl;
|
2005-07-14 22:09:22 +00:00
|
|
|
return make_pair(-1, string());
|
|
|
|
}
|
2002-03-21 17:09:55 +00:00
|
|
|
|
1999-11-24 22:14:46 +00:00
|
|
|
string ret;
|
|
|
|
int c = fgetc(inf);
|
|
|
|
while (c != EOF) {
|
|
|
|
ret += static_cast<char>(c);
|
|
|
|
c = fgetc(inf);
|
|
|
|
}
|
2005-05-03 20:32:55 +00:00
|
|
|
|
2010-06-28 17:45:04 +00:00
|
|
|
#if defined (_WIN32)
|
|
|
|
WaitForSingleObject(process.hProcess, INFINITE);
|
2016-08-17 20:58:13 +00:00
|
|
|
DWORD pret;
|
|
|
|
if (!GetExitCodeProcess(process.hProcess, &pret))
|
|
|
|
pret = -1;
|
2011-08-22 01:39:49 +00:00
|
|
|
if (!infile.empty())
|
|
|
|
CloseHandle(startup.hStdInput);
|
2010-06-28 17:45:04 +00:00
|
|
|
CloseHandle(process.hProcess);
|
2016-08-17 20:58:13 +00:00
|
|
|
if (fclose(inf) != 0)
|
|
|
|
pret = -1;
|
2010-06-28 17:45:04 +00:00
|
|
|
#elif defined (HAVE_PCLOSE)
|
2000-11-14 02:01:57 +00:00
|
|
|
int const pret = pclose(inf);
|
2005-05-03 20:32:55 +00:00
|
|
|
#elif defined (HAVE__PCLOSE)
|
|
|
|
int const pret = _pclose(inf);
|
|
|
|
#else
|
|
|
|
#error No pclose() function.
|
|
|
|
#endif
|
|
|
|
|
2004-04-05 06:46:22 +00:00
|
|
|
if (pret == -1)
|
|
|
|
perror("RunCommand:: could not terminate child process");
|
|
|
|
|
1999-11-24 22:14:46 +00:00
|
|
|
return make_pair(pret, ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-12-01 20:09:08 +00:00
|
|
|
FileName const findtexfile(string const & fil, string const & /*format*/)
|
1999-11-24 22:14:46 +00:00
|
|
|
{
|
|
|
|
/* There is no problem to extend this function too use other
|
|
|
|
methods to look for files. It could be setup to look
|
|
|
|
in environment paths and also if wanted as a last resort
|
|
|
|
to a recursive find. One of the easier extensions would
|
|
|
|
perhaps be to use the LyX file lookup methods. But! I am
|
|
|
|
going to implement this until I see some demand for it.
|
|
|
|
Lgb
|
|
|
|
*/
|
2002-03-21 17:09:55 +00:00
|
|
|
|
2000-03-01 18:15:39 +00:00
|
|
|
// If the file can be found directly, we just return a
|
2002-03-21 17:09:55 +00:00
|
|
|
// absolute path version of it.
|
2006-12-01 20:09:08 +00:00
|
|
|
FileName const absfile(makeAbsPath(fil));
|
2007-10-20 10:03:45 +00:00
|
|
|
if (absfile.exists())
|
2006-12-01 20:09:08 +00:00
|
|
|
return absfile;
|
2000-03-01 18:15:39 +00:00
|
|
|
|
2008-06-05 05:39:19 +00:00
|
|
|
// Now we try to find it using kpsewhich.
|
2000-03-02 02:19:43 +00:00
|
|
|
// It seems from the kpsewhich manual page that it is safe to use
|
|
|
|
// kpsewhich without --format: "When the --format option is not
|
|
|
|
// given, the search path used when looking for a file is inferred
|
|
|
|
// from the name given, by looking for a known extension. If no
|
|
|
|
// known extension is found, the search path for TeX source files
|
|
|
|
// is used."
|
|
|
|
// However, we want to take advantage of the format sine almost all
|
|
|
|
// the different formats has environment variables that can be used
|
|
|
|
// to controll which paths to search. f.ex. bib looks in
|
|
|
|
// BIBINPUTS and TEXBIB. Small list follows:
|
|
|
|
// bib - BIBINPUTS, TEXBIB
|
|
|
|
// bst - BSTINPUTS
|
|
|
|
// graphic/figure - TEXPICTS, TEXINPUTS
|
|
|
|
// ist - TEXINDEXSTYLE, INDEXSTYLE
|
|
|
|
// pk - PROGRAMFONTS, PKFONTS, TEXPKS, GLYPHFONTS, TEXFONTS
|
|
|
|
// tex - TEXINPUTS
|
|
|
|
// tfm - TFMFONTS, TEXFONTS
|
|
|
|
// This means that to use kpsewhich in the best possible way we
|
|
|
|
// should help it by setting additional path in the approp. envir.var.
|
2001-10-24 13:21:20 +00:00
|
|
|
string const kpsecmd = "kpsewhich " + fil;
|
2000-01-12 11:16:01 +00:00
|
|
|
|
2006-04-08 22:31:11 +00:00
|
|
|
cmd_ret const c = runCommand(kpsecmd);
|
2001-10-24 13:21:20 +00:00
|
|
|
|
2007-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::LATEX, "kpse status = " << c.first << '\n'
|
|
|
|
<< "kpse result = `" << rtrim(c.second, "\n\r") << '\'');
|
2002-03-21 17:09:55 +00:00
|
|
|
if (c.first != -1)
|
2007-11-28 15:25:07 +00:00
|
|
|
return FileName(rtrim(to_utf8(from_filesystem8bit(c.second)), "\n\r"));
|
2001-06-21 10:55:48 +00:00
|
|
|
else
|
2006-12-01 20:09:08 +00:00
|
|
|
return FileName();
|
1999-11-24 22:14:46 +00:00
|
|
|
}
|
2000-08-08 15:36:25 +00:00
|
|
|
|
2000-09-14 17:53:12 +00:00
|
|
|
|
2007-10-18 21:10:35 +00:00
|
|
|
int compare_timestamps(FileName const & file1, FileName const & file2)
|
2003-06-03 10:14:52 +00:00
|
|
|
{
|
|
|
|
// If the original is newer than the copy, then copy the original
|
|
|
|
// to the new directory.
|
2003-09-03 17:23:38 +00:00
|
|
|
|
|
|
|
int cmp = 0;
|
2007-10-18 21:10:35 +00:00
|
|
|
if (file1.exists() && file2.exists()) {
|
|
|
|
double const tmp = difftime(file1.lastModified(), file2.lastModified());
|
2003-09-03 17:23:38 +00:00
|
|
|
if (tmp != 0)
|
|
|
|
cmp = tmp > 0 ? 1 : -1;
|
|
|
|
|
2007-10-18 21:10:35 +00:00
|
|
|
} else if (file1.exists()) {
|
2003-09-03 17:23:38 +00:00
|
|
|
cmp = 1;
|
2007-10-18 21:10:35 +00:00
|
|
|
} else if (file2.exists()) {
|
2003-09-03 17:23:38 +00:00
|
|
|
cmp = -1;
|
2003-06-03 10:14:52 +00:00
|
|
|
}
|
|
|
|
|
2003-09-03 17:23:38 +00:00
|
|
|
return cmp;
|
2003-06-03 10:14:52 +00:00
|
|
|
}
|
2003-06-30 23:56:22 +00:00
|
|
|
|
2007-12-02 09:19:43 +00:00
|
|
|
|
2011-01-16 19:51:02 +00:00
|
|
|
bool prefs2prefs(FileName const & filename, FileName const & tempfile, bool lfuns)
|
|
|
|
{
|
|
|
|
FileName const script = libFileSearch("scripts", "prefs2prefs.py");
|
|
|
|
if (script.empty()) {
|
|
|
|
LYXERR0("Could not find bind file conversion "
|
|
|
|
"script prefs2prefs.py.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ostringstream command;
|
|
|
|
command << os::python() << ' ' << quoteName(script.toFilesystemEncoding())
|
|
|
|
<< ' ' << (lfuns ? "-l" : "-p") << ' '
|
|
|
|
<< quoteName(filename.toFilesystemEncoding())
|
|
|
|
<< ' ' << quoteName(tempfile.toFilesystemEncoding());
|
|
|
|
string const command_str = command.str();
|
|
|
|
|
|
|
|
LYXERR(Debug::FILES, "Running `" << command_str << '\'');
|
|
|
|
|
|
|
|
cmd_ret const ret = runCommand(command_str);
|
|
|
|
if (ret.first != 0) {
|
2011-01-17 21:49:28 +00:00
|
|
|
LYXERR0("Could not run file conversion script prefs2prefs.py.");
|
2011-01-16 19:51:02 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-12-29 22:46:25 +00:00
|
|
|
|
|
|
|
bool configFileNeedsUpdate(string const & file)
|
|
|
|
{
|
|
|
|
// We cannot initialize configure_script directly because the package
|
|
|
|
// is not initialized yet when static objects are constructed.
|
|
|
|
static FileName configure_script;
|
|
|
|
static bool firstrun = true;
|
|
|
|
if (firstrun) {
|
|
|
|
configure_script =
|
|
|
|
FileName(addName(package().system_support().absFileName(),
|
|
|
|
"configure.py"));
|
|
|
|
firstrun = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileName absfile =
|
|
|
|
FileName(addName(package().user_support().absFileName(), file));
|
|
|
|
return !absfile.exists()
|
|
|
|
|| configure_script.lastModified() > absfile.lastModified();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-11-20 18:40:32 +00:00
|
|
|
int fileLock(const char * lock_file)
|
|
|
|
{
|
|
|
|
int fd = -1;
|
|
|
|
#if defined(HAVE_LOCKF)
|
|
|
|
fd = open(lock_file, O_CREAT|O_APPEND|O_SYNC|O_RDWR, 0666);
|
2015-09-12 21:49:32 +00:00
|
|
|
if (fd == -1)
|
|
|
|
return -1;
|
2013-11-20 18:40:32 +00:00
|
|
|
if (lockf(fd, F_LOCK, 0) != 0) {
|
|
|
|
close(fd);
|
2015-09-12 21:49:32 +00:00
|
|
|
return -1;
|
2013-11-20 18:40:32 +00:00
|
|
|
}
|
|
|
|
#endif
|
2015-09-12 21:49:32 +00:00
|
|
|
return fd;
|
2013-11-20 18:40:32 +00:00
|
|
|
}
|
|
|
|
|
2015-09-12 21:49:32 +00:00
|
|
|
|
2014-01-05 09:04:23 +00:00
|
|
|
void fileUnlock(int fd, const char * /* lock_file*/)
|
2013-11-20 18:40:32 +00:00
|
|
|
{
|
|
|
|
#if defined(HAVE_LOCKF)
|
2014-03-27 08:10:59 +00:00
|
|
|
if (fd >= 0) {
|
|
|
|
if (lockf(fd, F_ULOCK, 0))
|
|
|
|
LYXERR0("Can't unlock the file.");
|
2013-11-20 18:40:32 +00:00
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2011-01-16 19:51:02 +00:00
|
|
|
|
2003-06-30 23:56:22 +00:00
|
|
|
} //namespace support
|
|
|
|
} // namespace lyx
|