3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * parts Copyright 1985, 1990, 1993 Free Software Foundation, Inc.
8 * \author Ivan Schreter
9 * \author Dirk Niggemann
10 * \author Asger Alstrup
11 * \author Lars Gullik Bjønnes
12 * \author Jean-Marc Lasgouttes
13 * \author Angus Leeming
17 * Full author contact details are available in file CREDITS.
19 * General path-mangling functions
27 #include "support/filetools.h"
29 #include "support/convert.h"
30 #include "support/debug.h"
31 #include "support/environment.h"
32 #include "support/gettext.h"
33 #include "support/lstrings.h"
34 #include "support/os.h"
35 #include "support/Messages.h"
36 #include "support/Package.h"
37 #include "support/PathChanger.h"
38 #include "support/Systemcall.h"
39 #include "support/qstring_helpers.h"
40 #include "support/TempFile.h"
44 #include "support/lassert.h"
45 #include "support/regex.h"
77 bool isLyXFileName(string const & filename)
79 return suffixIs(ascii_lowercase(filename), ".lyx");
83 bool isSGMLFileName(string const & filename)
85 return suffixIs(ascii_lowercase(filename), ".sgml");
89 bool isValidLaTeXFileName(string const & filename)
91 string const invalid_chars("#%\"");
92 return filename.find_first_of(invalid_chars) == string::npos;
96 bool isValidDVIFileName(string const & filename)
98 string const invalid_chars("${}()[]^");
99 return filename.find_first_of(invalid_chars) == string::npos;
103 bool isBinaryFile(FileName const & filename)
105 bool isbinary = false;
106 if (filename.empty() || !filename.exists())
110 magic_t magic_cookie = magic_open(MAGIC_MIME_ENCODING);
112 bool detected = true;
113 if (magic_load(magic_cookie, NULL) != 0) {
114 LYXERR(Debug::FILES, "isBinaryFile: "
115 "Could not load magic database - "
116 << magic_error(magic_cookie));
119 char const *charset = magic_file(magic_cookie,
120 filename.toFilesystemEncoding().c_str());
121 isbinary = contains(charset, "binary");
123 magic_close(magic_cookie);
128 // Try by looking for binary chars at the beginning of the file.
129 // Note that this is formally not correct, since count_bin_chars
130 // expects utf8, and the passed string can be anything: plain text
131 // in any encoding, or really binary data. In practice it works,
132 // since QString::fromUtf8() drops invalid utf8 sequences, and
133 // while the exact number may not be correct, we still get a high
134 // number for truly binary files.
136 ifstream ifs(filename.toFilesystemEncoding().c_str());
140 // Maximum strings to read
141 int const max_count = 50;
143 // Maximum number of binary chars allowed
144 int const max_bin = 5;
149 while (count++ < max_count && !ifs.eof()) {
151 binchars += count_bin_chars(str);
153 return binchars > max_bin;
157 string const latex_path(string const & original_path,
158 latex_path_extension extension,
159 latex_path_dots dots)
161 // On cygwin, we may need windows or posix style paths.
162 string path = os::latex_path(original_path);
163 path = subst(path, "~", "\\string~");
164 if (path.find(' ') != string::npos) {
165 // We can't use '"' because " is sometimes active (e.g. if
166 // babel is loaded with the "german" option)
167 if (extension == EXCLUDE_EXTENSION) {
168 // changeExtension calls os::internal_path internally
169 // so don't use it to remove the extension.
170 string const ext = getExtension(path);
171 string const base = ext.empty() ?
173 path.substr(0, path.length() - ext.length() - 1);
174 // changeExtension calls os::internal_path internally
175 // so don't use it to re-add the extension.
176 path = "\\string\"" + base + "\\string\"." + ext;
178 path = "\\string\"" + path + "\\string\"";
182 if (dots != ESCAPE_DOTS)
185 // Replace dots with the lyxdot macro, but only in the file name,
186 // not the directory part.
187 // addName etc call os::internal_path internally
188 // so don't use them for path manipulation
189 // The directory separator is always '/' for LaTeX.
190 string::size_type pos = path.rfind('/');
191 if (pos == string::npos)
192 return subst(path, ".", "\\lyxdot ");
193 return path.substr(0, pos) + subst(path.substr(pos), ".", "\\lyxdot ");
197 // Substitutes spaces with underscores in filename (and path)
198 FileName const makeLatexName(FileName const & file)
200 string name = file.onlyFileName();
201 string const path = file.onlyPath().absFileName() + "/";
203 // ok so we scan through the string twice, but who cares.
204 // FIXME: in Unicode time this will break for sure! There is
205 // a non-latin world out there...
206 string const keep = "abcdefghijklmnopqrstuvwxyz"
207 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
208 "@!'()*+,-./0123456789:;<=>?[]`|";
210 string::size_type pos = 0;
211 while ((pos = name.find_first_not_of(keep, pos)) != string::npos)
214 FileName latex_name(path + name);
215 latex_name.changeExtension(".tex");
220 string const quoteName(string const & name, quote_style style)
224 // This does not work on native Windows for filenames
225 // containing the following characters < > : " / \ | ? *
226 // Moreover, it can't be made to work, as, according to
227 // http://msdn.microsoft.com/en-us/library/aa365247(VS.85).aspx
228 // those are reserved characters, and thus are forbidden.
229 // Please, also note that the command-line parser in
230 // ForkedCall::generateChild cannot deal with filenames
231 // containing " or ', therefore we don't pass user filenames
232 // to child processes if possible. We store them in a python
233 // script instead, where we don't have these limitations.
235 return (os::shell() == os::UNIX) ?
236 '\'' + subst(name, "'", "\'\\\'\'") + '\'' :
239 // According to the QProcess parser, a single double
240 // quote is represented by three consecutive ones.
241 // Here we simply escape the double quote and let our
242 // simple parser in Systemcall.cpp do the substitution.
243 return '"' + subst(name, "\"", "\\\"") + '"';
245 case quote_shell_filename:
246 return quoteName(os::external_path(name), quote_shell);
248 return "\"" + subst(subst(name, "\\", "\\\\"), "\"", "\\\"")
251 // shut up stupid compiler
257 // Uses a string of paths separated by ";"s to find a file to open.
258 // Can't cope with pathnames with a ';' in them. Returns full path to file.
259 // If path entry begins with $$LyX/, use system_lyxdir
260 // If path entry begins with $$User/, use user_lyxdir
261 // Example: "$$User/doc;$$LyX/doc"
262 FileName const fileOpenSearch(string const & path, string const & name,
267 bool notfound = true;
268 string tmppath = split(path, path_element, ';');
270 while (notfound && !path_element.empty()) {
271 path_element = os::internal_path(path_element);
272 if (!suffixIs(path_element, '/'))
274 path_element = subst(path_element, "$$LyX",
275 package().system_support().absFileName());
276 path_element = subst(path_element, "$$User",
277 package().user_support().absFileName());
279 real_file = fileSearch(path_element, name, ext);
281 if (real_file.empty()) {
283 tmppath = split(tmppath, path_element, ';');
284 } while (!tmppath.empty() && path_element.empty());
294 // Returns the real name of file name in directory path, with optional
296 FileName const fileSearch(string const & path, string const & name,
297 string const & exts, search_mode mode)
299 // if `name' is an absolute path, we ignore the setting of `path'
300 // Expand Environmentvariables in 'name'
301 string const tmpname = replaceEnvironmentPath(name);
302 FileName fullname(makeAbsPath(tmpname, path));
303 // search first without extension, then with it.
304 if (fullname.isReadableFile())
308 return mode == may_not_exist ? fullname : FileName();
310 string ext = token(exts, ',', n);
311 while (!ext.empty()) {
312 // Only add the extension if it is not already the extension of
314 bool addext = getExtension(fullname.absFileName()) != ext;
316 if (mode == check_hidpi) {
317 FileName fullname2x = FileName(addExtension(fullname.absFileName() + "@2x", ext));
318 if (fullname2x.isReadableFile())
321 fullname = FileName(addExtension(fullname.absFileName(), ext));
323 if (fullname.isReadableFile() || mode == may_not_exist)
326 fullname.changeExtension("");
327 ext = token(exts, ',', ++n);
333 // Search the file name.ext in the subdirectory dir of
335 // 2) build_lyxdir (if not empty)
337 FileName const libFileSearch(string const & dir, string const & name,
338 string const & ext, search_mode mode)
340 FileName fullname = fileSearch(addPath(package().user_support().absFileName(), dir),
342 if (!fullname.empty())
345 if (!package().build_support().empty())
346 fullname = fileSearch(addPath(package().build_support().absFileName(), dir),
348 if (!fullname.empty())
351 return fileSearch(addPath(package().system_support().absFileName(), dir),
356 FileName const i18nLibFileSearch(string const & dir, string const & name,
359 // if the LANGUAGE variable is set, use it as a fallback for searching for files.
360 string lang = getGuiMessages().language();
361 string const language = getEnv("LANGUAGE");
362 if (!language.empty())
363 lang += ":" + language;
365 for (auto const & l : getVectorFromString(lang, ":")) {
367 // First try with the full name
368 // `en' files are not in a subdirectory
370 tmp = libFileSearch(dir, name, ext);
372 tmp = libFileSearch(addPath(dir, l), name, ext);
376 // Then the name without country code
377 string const shortl = token(l, '_', 0);
379 tmp = libFileSearch(addPath(dir, shortl), name, ext);
385 return libFileSearch(dir, name, ext);
389 FileName const imageLibFileSearch(string & dir, string const & name,
390 string const & ext, search_mode mode)
392 if (!lyx::lyxrc.icon_set.empty()) {
393 string const imagedir = addPath(dir, lyx::lyxrc.icon_set);
394 FileName const fn = libFileSearch(imagedir, name, ext, mode);
400 return libFileSearch(dir, name, ext, mode);
404 string const commandPrep(string const & command_in)
406 static string const token_scriptpath = "$$s/";
407 string const python_call = "python -tt";
409 string command = command_in;
410 if (prefixIs(command_in, python_call))
411 command = os::python() + command_in.substr(python_call.length());
413 // Find the starting position of "$$s/"
414 string::size_type const pos1 = command.find(token_scriptpath);
415 if (pos1 == string::npos)
417 // Find the end of the "$$s/some_subdir/some_script" word within
418 // command. Assumes that the script name does not contain spaces.
419 string::size_type const start_script = pos1 + 4;
420 string::size_type const pos2 = command.find(' ', start_script);
421 string::size_type const size_script = pos2 == string::npos?
422 (command.size() - start_script) : pos2 - start_script;
424 // Does this script file exist?
425 string const script =
426 libFileSearch(".", command.substr(start_script, size_script)).absFileName();
428 if (script.empty()) {
429 // Replace "$$s/" with ""
430 command.erase(pos1, 4);
432 quote_style style = quote_shell;
433 if (prefixIs(command, os::python()))
434 style = quote_python;
436 // Replace "$$s/foo/some_script" with "<path to>/some_script".
437 string::size_type const size_replace = size_script + 4;
438 command.replace(pos1, size_replace, quoteName(script, style));
445 FileName const tempFileName(string const & mask, bool const dir)
447 FileName tempfile = TempFile(mask).name();
448 // Since the QTemporaryFile object is destroyed at function return
449 // (which is what is intended here), the next call to this function
450 // may return the same file name again.
451 // Thus, in order to prevent race conditions, we track returned names
452 // and create our own unique names if QTemporaryFile returns a name again.
453 if (tmp_names_.find(tempfile.absFileName()) == tmp_names_.end()) {
454 tmp_names_.insert(tempfile.absFileName());
458 // OK, we need another name. Simply append digits.
459 FileName tmp = tempfile;
462 // Store and remove extensions
463 ext = "." + tempfile.extension();
464 tmp.changeExtension("");
466 for (int i = 1; i < INT_MAX ;++i) {
467 // Append digit to filename and re-add extension
468 string const new_fn =
469 tmp.absFileName() + convert<string>(i) + ext;
470 if (tmp_names_.find(new_fn) == tmp_names_.end()) {
471 tmp_names_.insert(new_fn);
472 tempfile.set(new_fn);
477 // This should not happen!
478 LYXERR0("tempFileName(): Could not create unique temp file name!");
483 void removeTempFile(FileName const & fn)
488 string const abs = fn.absFileName();
489 if (tmp_names_.find(abs) != tmp_names_.end())
490 tmp_names_.erase(abs);
495 static FileName createTmpDir(FileName const & tempdir, string const & mask)
497 LYXERR(Debug::FILES, "createTmpDir: tempdir=`" << tempdir << "'\n"
498 << "createTmpDir: mask=`" << mask << '\'');
500 QFileInfo tmp_fi(QDir(toqstr(tempdir.absFileName())), toqstr(mask));
501 FileName const tmpfl =
502 tempFileName(fromqstr(tmp_fi.absoluteFilePath()) + ".XXXXXXXXXXXX", true);
504 if (tmpfl.empty() || !tmpfl.createDirectory(0700)) {
505 LYXERR0("LyX could not create temporary directory in " << tempdir
514 FileName const createLyXTmpDir(FileName const & deflt)
516 if (deflt.empty() || deflt == package().system_temp_dir())
517 return createTmpDir(package().system_temp_dir(), "lyx_tmpdir");
519 if (deflt.createDirectory(0777))
522 if (deflt.isDirWritable()) {
523 // deflt could not be created because it
524 // did exist already, so let's create our own
526 return createTmpDir(deflt, "lyx_tmpdir");
528 // some other error occurred.
529 return createTmpDir(package().system_temp_dir(), "lyx_tmpdir");
534 // Strip filename from path name
535 string const onlyPath(string const & filename)
537 // If empty filename, return empty
538 if (filename.empty())
541 // Find last / or start of filename
542 size_t j = filename.rfind('/');
543 return j == string::npos ? "./" : filename.substr(0, j + 1);
547 // Convert relative path into absolute path based on a basepath.
548 // If relpath is absolute, just use that.
549 // If basepath is empty, use CWD as base.
550 // Note that basePath can be a relative path, in the sense that it may
551 // not begin with "/" (e.g.), but it should NOT contain such constructs
553 // FIXME It might be nice if the code didn't simply assume that.
554 FileName const makeAbsPath(string const & relPath, string const & basePath)
556 // checks for already absolute path
557 if (FileName::isAbsolute(relPath))
558 return FileName(relPath);
560 // Copies given paths
561 string tempRel = os::internal_path(relPath);
562 // Since TempRel is NOT absolute, we can safely replace "//" with "/"
563 tempRel = subst(tempRel, "//", "/");
567 if (FileName::isAbsolute(basePath))
570 tempBase = addPath(FileName::getcwd().absFileName(), basePath);
572 // Handle /./ at the end of the path
573 while (suffixIs(tempBase, "/./"))
574 tempBase.erase(tempBase.length() - 2);
576 // processes relative path
577 string rTemp = tempRel;
580 // Check for a leading "~"
582 rTemp = split(rTemp, temp, '/');
584 tempBase = Package::get_home_dir().absFileName();
589 while (!rTemp.empty()) {
591 rTemp = split(rTemp, temp, '/');
593 if (temp == ".") continue;
595 // Remove one level of TempBase
596 if (tempBase.length() <= 1) {
597 //this is supposed to be an absolute path, so...
601 //erase a trailing slash if there is one
602 if (suffixIs(tempBase, "/"))
603 tempBase.erase(tempBase.length() - 1, string::npos);
605 string::size_type i = tempBase.length() - 1;
606 while (i > 0 && tempBase[i] != '/')
609 tempBase.erase(i, string::npos);
612 } else if (temp.empty() && !rTemp.empty()) {
613 tempBase = os::current_root() + rTemp;
616 // Add this piece to TempBase
617 if (!suffixIs(tempBase, '/'))
623 // returns absolute path
624 return FileName(tempBase);
628 // Correctly append filename to the pathname.
629 // If pathname is '.', then don't use pathname.
630 // Chops any path of filename.
631 string const addName(string const & path, string const & fname)
633 string const basename = onlyFileName(fname);
636 if (path != "." && path != "./" && !path.empty()) {
637 buf = os::internal_path(path);
638 if (!suffixIs(path, '/'))
642 return buf + basename;
646 // Strips path from filename
647 string const onlyFileName(string const & fname)
652 string::size_type j = fname.rfind('/');
653 if (j == string::npos) // no '/' in fname
657 return fname.substr(j + 1);
661 // Search the string for ${VAR} and $VAR and replace VAR using getenv.
662 string const replaceEnvironmentPath(string const & path)
664 // ${VAR} is defined as
665 // $\{[A-Za-z_][A-Za-z_0-9]*\}
666 static string const envvar_br = "[$]\\{([A-Za-z_][A-Za-z_0-9]*)\\}";
668 // $VAR is defined as:
669 // $[A-Za-z_][A-Za-z_0-9]*
670 static string const envvar = "[$]([A-Za-z_][A-Za-z_0-9]*)";
672 // Coverity thinks that the regex constructor can return an
673 // exception. We know that it is not true since our regex are
674 // hardcoded, but we have to protect against that nevertheless.
676 static regex const envvar_br_re("(.*)" + envvar_br + "(.*)");
677 static regex const envvar_re("(.*)" + envvar + "(.*)");
678 string result = path;
681 if (!regex_match(result, what, envvar_br_re)) {
682 if (!regex_match(result, what, envvar_re))
685 string env_var = getEnv(what.str(2));
686 result = what.str(1) + env_var + what.str(3);
689 } catch (exception const & e) {
690 LYXERR0("Something is very wrong: " << e.what());
696 // Return a command prefix for setting the environment of the TeX engine.
697 string latexEnvCmdPrefix(string const & path, string const & lpath)
699 bool use_lpath = !(lpath.empty() || lpath == "." || lpath == "./");
701 if (path.empty() || (lyxrc.texinputs_prefix.empty() && !use_lpath))
704 string texinputs_prefix = lyxrc.texinputs_prefix.empty() ? string()
705 : os::latex_path_list(
706 replaceCurdirPath(path, lyxrc.texinputs_prefix));
707 string const allother_prefix = os::latex_path_list(path);
708 string const sep = string(1, os::path_separator(os::TEXENGINE));
709 string const texinputs = getEnv("TEXINPUTS");
710 string const bibinputs = getEnv("BIBINPUTS");
711 string const bstinputs = getEnv("BSTINPUTS");
712 string const texfonts = getEnv("TEXFONTS");
715 string const abslpath = FileName::isAbsolute(lpath)
716 ? os::latex_path(lpath)
717 : os::latex_path(FileName(path + "/" + lpath).realPath());
718 if (texinputs_prefix.empty())
719 texinputs_prefix = abslpath;
720 else if (suffixIs(texinputs_prefix, sep))
721 texinputs_prefix.append(abslpath + sep);
723 texinputs_prefix.append(sep + abslpath);
726 if (os::shell() == os::UNIX)
727 return "env TEXINPUTS=\"." + sep + texinputs_prefix
728 + sep + texinputs + "\" "
729 + "BIBINPUTS=\"." + sep + allother_prefix
730 + sep + bibinputs + "\" "
731 + "BSTINPUTS=\"." + sep + allother_prefix
732 + sep + bstinputs + "\" "
733 + "TEXFONTS=\"." + sep + allother_prefix
734 + sep + texfonts + "\" ";
736 // NOTE: the dummy blank dirs are necessary to force the
737 // QProcess parser to quote the argument (see bug 9453)
738 return "cmd /d /c set \"TEXINPUTS=." + sep + " "
739 + sep + texinputs_prefix
740 + sep + texinputs + "\" & "
741 + "set \"BIBINPUTS=." + sep + " "
742 + sep + allother_prefix
743 + sep + bibinputs + "\" & "
744 + "set \"BSTINPUTS=." + sep + " "
745 + sep + allother_prefix
746 + sep + bstinputs + "\" & "
747 + "set \"TEXFONTS=." + sep + " "
748 + sep + allother_prefix
749 + sep + texfonts + "\" & ";
753 // Replace current directory in all elements of a path list with a given path.
754 string const replaceCurdirPath(string const & path, string const & pathlist)
756 string const oldpathlist = replaceEnvironmentPath(pathlist);
757 char const sep = os::path_separator();
760 for (size_t i = 0, k = 0; i != string::npos; k = i) {
761 i = oldpathlist.find(sep, i);
762 string p = oldpathlist.substr(k, i - k);
763 if (FileName::isAbsolute(p)) {
769 } else if (prefixIs(p, "./")) {
771 while (p[offset] == '/')
774 newpathlist += addPath(path, p.substr(offset));
775 if (suffixIs(p, "//"))
778 if (i != string::npos) {
780 // Stop here if the last element is empty
781 if (++i == oldpathlist.length())
789 // Make relative path out of two absolute paths
790 docstring const makeRelPath(docstring const & abspath, docstring const & basepath)
791 // Makes relative path out of absolute path. If it is deeper than basepath,
792 // it's easy. If basepath and abspath share something (they are all deeper
793 // than some directory), it'll be rendered using ..'s. If they are completely
794 // different, then the absolute path will be used as relative path.
796 docstring::size_type const abslen = abspath.length();
797 docstring::size_type const baselen = basepath.length();
799 docstring::size_type i = os::common_path(abspath, basepath);
802 // actually no match - cannot make it relative
806 // Count how many dirs there are in basepath above match
807 // and append as many '..''s into relpath
809 docstring::size_type j = i;
810 while (j < baselen) {
811 if (basepath[j] == '/') {
812 if (j + 1 == baselen)
819 // Append relative stuff from common directory to abspath
820 if (abspath[i] == '/')
822 for (; i < abslen; ++i)
825 if (suffixIs(buf, '/'))
826 buf.erase(buf.length() - 1);
827 // Substitute empty with .
834 // Append sub-directory(ies) to a path in an intelligent way
835 string const addPath(string const & path, string const & path_2)
838 string const path2 = os::internal_path(path_2);
840 if (!path.empty() && path != "." && path != "./") {
841 buf = os::internal_path(path);
842 if (path[path.length() - 1] != '/')
846 if (!path2.empty()) {
847 string::size_type const p2start = path2.find_first_not_of('/');
848 string::size_type const p2end = path2.find_last_not_of('/');
849 string const tmp = path2.substr(p2start, p2end - p2start + 1);
856 string const changeExtension(string const & oldname, string const & extension)
858 string::size_type const last_slash = oldname.rfind('/');
859 string::size_type last_dot = oldname.rfind('.');
860 if (last_dot < last_slash && last_slash != string::npos)
861 last_dot = string::npos;
864 // Make sure the extension starts with a dot
865 if (!extension.empty() && extension[0] != '.')
866 ext= '.' + extension;
870 return os::internal_path(oldname.substr(0, last_dot) + ext);
874 string const removeExtension(string const & name)
876 return changeExtension(name, string());
880 string const addExtension(string const & name, string const & extension)
882 if (!extension.empty() && extension[0] != '.')
883 return name + '.' + extension;
884 return name + extension;
888 /// Return the extension of the file (not including the .)
889 string const getExtension(string const & name)
891 string::size_type const last_slash = name.rfind('/');
892 string::size_type const last_dot = name.rfind('.');
893 if (last_dot != string::npos &&
894 (last_slash == string::npos || last_dot > last_slash))
895 return name.substr(last_dot + 1,
896 name.length() - (last_dot + 1));
902 string const unzippedFileName(string const & zipped_file)
904 string const ext = getExtension(zipped_file);
905 if (ext == "gz" || ext == "z" || ext == "Z")
906 return changeExtension(zipped_file, string());
907 else if (ext == "svgz")
908 return changeExtension(zipped_file, "svg");
909 return onlyPath(zipped_file) + "unzipped_" + onlyFileName(zipped_file);
913 FileName const unzipFile(FileName const & zipped_file, string const & unzipped_file)
915 FileName const tempfile = FileName(unzipped_file.empty() ?
916 unzippedFileName(zipped_file.toFilesystemEncoding()) :
919 string const command = "gunzip -c \"" +
920 zipped_file.toFilesystemEncoding() + "\" > \"" +
921 tempfile.toFilesystemEncoding() + "\"";
923 one.startscript(Systemcall::Wait, command);
924 // test that command was executed successfully (anon)
925 // yes, please do. (Lgb)
930 docstring const makeDisplayPath(string const & path, unsigned int threshold)
934 // If file is from LyXDir, display it as if it were relative.
935 string const system = package().system_support().absFileName();
936 if (prefixIs(str, system) && str != system)
937 return from_utf8("[" + str.erase(0, system.length()) + "]");
939 // replace /home/blah with ~/
940 string const home = Package::get_home_dir().absFileName();
941 if (!home.empty() && prefixIs(str, home))
942 str = subst(str, home, "~");
944 if (str.length() <= threshold)
945 return from_utf8(os::external_path(str));
947 string const prefix = ".../";
948 docstring dstr = from_utf8(str);
951 while (dstr.length() > threshold)
952 dstr = split(dstr, temp, '/');
954 // Did we shorten everything away?
956 // Yes, filename itself is too long.
957 // Pick the start and the end of the filename.
958 docstring fstr = from_utf8(onlyFileName(path));
960 if (support::truncateWithEllipsis(dstr, threshold / 2))
961 dstr += fstr.substr(fstr.length() - threshold / 2 - 2,
965 return from_utf8(os::external_path(prefix + to_utf8(dstr)));
970 bool readLink(FileName const & file, FileName & link)
972 string const encoded = file.toFilesystemEncoding();
973 #ifdef HAVE_DEF_PATH_MAX
974 char linkbuffer[PATH_MAX + 1];
975 ssize_t const nRead = ::readlink(encoded.c_str(),
976 linkbuffer, sizeof(linkbuffer) - 1);
979 linkbuffer[nRead] = '\0'; // terminator
981 vector<char> buf(1024);
985 nRead = ::readlink(encoded.c_str(), &buf[0], buf.size() - 1);
989 if (static_cast<size_t>(nRead) < buf.size() - 1) {
992 buf.resize(buf.size() * 2);
994 buf[nRead] = '\0'; // terminator
995 const char * linkbuffer = &buf[0];
997 link = makeAbsPath(linkbuffer, onlyPath(file.absFileName()));
1001 bool readLink(FileName const &, FileName &)
1008 cmd_ret const runCommand(string const & cmd)
1010 // FIXME: replace all calls to RunCommand with ForkedCall
1011 // (if the output is not needed) or the code in ISpell.cpp
1012 // (if the output is needed).
1014 // One question is if we should use popen or
1015 // create our own popen based on fork, exec, pipe
1016 // of course the best would be to have a
1017 // pstream (process stream), with the
1018 // variants ipstream, opstream
1021 lyxerr << "\nRunning: " << cmd << endl;
1023 LYXERR(Debug::INFO,"Running: " << cmd);
1025 #if defined (_WIN32)
1026 STARTUPINFO startup;
1027 PROCESS_INFORMATION process;
1028 SECURITY_ATTRIBUTES security;
1031 bool err2out = false;
1033 string const infile = trim(split(cmd, command, '<'), " \"");
1034 command = rtrim(command);
1035 if (suffixIs(command, "2>&1")) {
1036 command = rtrim(command, "2>&1");
1039 string const cmdarg = "/d /c " + command;
1040 string const comspec = getEnv("COMSPEC");
1042 security.nLength = sizeof(SECURITY_ATTRIBUTES);
1043 security.bInheritHandle = TRUE;
1044 security.lpSecurityDescriptor = NULL;
1046 if (CreatePipe(&in, &out, &security, 0)) {
1047 memset(&startup, 0, sizeof(STARTUPINFO));
1048 memset(&process, 0, sizeof(PROCESS_INFORMATION));
1050 startup.cb = sizeof(STARTUPINFO);
1051 startup.dwFlags = STARTF_USESTDHANDLES;
1053 startup.hStdError = err2out ? out : GetStdHandle(STD_ERROR_HANDLE);
1054 startup.hStdInput = infile.empty()
1055 ? GetStdHandle(STD_INPUT_HANDLE)
1056 : CreateFile(infile.c_str(), GENERIC_READ,
1057 FILE_SHARE_READ, &security, OPEN_EXISTING,
1058 FILE_ATTRIBUTE_NORMAL, NULL);
1059 startup.hStdOutput = out;
1061 if (startup.hStdInput != INVALID_HANDLE_VALUE &&
1062 CreateProcess(comspec.c_str(), (LPTSTR)cmdarg.c_str(),
1063 &security, &security, TRUE, CREATE_NO_WINDOW,
1064 0, 0, &startup, &process)) {
1066 CloseHandle(process.hThread);
1067 int fno = _open_osfhandle((intptr_t)in, _O_RDONLY);
1069 inf = _fdopen(fno, "r");
1072 #elif defined (HAVE_POPEN)
1073 FILE * inf = ::popen(cmd.c_str(), os::popen_read_mode());
1074 #elif defined (HAVE__POPEN)
1075 FILE * inf = ::_popen(cmd.c_str(), os::popen_read_mode());
1077 #error No popen() function.
1080 // (Claus Hentschel) Check if popen was successful ;-)
1082 lyxerr << "RunCommand:: could not start child process" << endl;
1083 return make_pair(-1, string());
1089 ret += static_cast<char>(c);
1093 #if defined (_WIN32)
1094 WaitForSingleObject(process.hProcess, INFINITE);
1096 if (!GetExitCodeProcess(process.hProcess, &pret))
1098 if (!infile.empty())
1099 CloseHandle(startup.hStdInput);
1100 CloseHandle(process.hProcess);
1101 if (fclose(inf) != 0)
1103 #elif defined (HAVE_PCLOSE)
1104 int const pret = pclose(inf);
1105 #elif defined (HAVE__PCLOSE)
1106 int const pret = _pclose(inf);
1108 #error No pclose() function.
1112 perror("RunCommand:: could not terminate child process");
1114 return make_pair(pret, ret);
1118 FileName const findtexfile(string const & fil, string const & /*format*/,
1119 bool const onlykpse)
1121 /* There is no problem to extend this function too use other
1122 methods to look for files. It could be setup to look
1123 in environment paths and also if wanted as a last resort
1124 to a recursive find. One of the easier extensions would
1125 perhaps be to use the LyX file lookup methods. But! I am
1126 going to implement this until I see some demand for it.
1130 // If the file can be found directly, we just return a
1131 // absolute path version of it.
1133 FileName const absfile(makeAbsPath(fil));
1134 if (absfile.exists())
1138 // Now we try to find it using kpsewhich.
1139 // It seems from the kpsewhich manual page that it is safe to use
1140 // kpsewhich without --format: "When the --format option is not
1141 // given, the search path used when looking for a file is inferred
1142 // from the name given, by looking for a known extension. If no
1143 // known extension is found, the search path for TeX source files
1145 // However, we want to take advantage of the format sine almost all
1146 // the different formats has environment variables that can be used
1147 // to controll which paths to search. f.ex. bib looks in
1148 // BIBINPUTS and TEXBIB. Small list follows:
1149 // bib - BIBINPUTS, TEXBIB
1151 // graphic/figure - TEXPICTS, TEXINPUTS
1152 // ist - TEXINDEXSTYLE, INDEXSTYLE
1153 // pk - PROGRAMFONTS, PKFONTS, TEXPKS, GLYPHFONTS, TEXFONTS
1155 // tfm - TFMFONTS, TEXFONTS
1156 // This means that to use kpsewhich in the best possible way we
1157 // should help it by setting additional path in the approp. envir.var.
1158 string const kpsecmd = "kpsewhich " + fil;
1160 cmd_ret const c = runCommand(kpsecmd);
1162 LYXERR(Debug::LATEX, "kpse status = " << c.first << '\n'
1163 << "kpse result = `" << rtrim(c.second, "\n\r") << '\'');
1165 return FileName(rtrim(to_utf8(from_filesystem8bit(c.second)), "\n\r"));
1171 int compare_timestamps(FileName const & file1, FileName const & file2)
1173 // If the original is newer than the copy, then copy the original
1174 // to the new directory.
1177 if (file1.exists() && file2.exists()) {
1178 double const tmp = difftime(file1.lastModified(), file2.lastModified());
1180 cmp = tmp > 0 ? 1 : -1;
1182 } else if (file1.exists()) {
1184 } else if (file2.exists()) {
1192 bool prefs2prefs(FileName const & filename, FileName const & tempfile, bool lfuns)
1194 FileName const script = libFileSearch("scripts", "prefs2prefs.py");
1195 if (script.empty()) {
1196 LYXERR0("Could not find bind file conversion "
1197 "script prefs2prefs.py.");
1201 ostringstream command;
1202 command << os::python() << ' ' << quoteName(script.toFilesystemEncoding())
1203 << ' ' << (lfuns ? "-l" : "-p") << ' '
1204 << quoteName(filename.toFilesystemEncoding())
1205 << ' ' << quoteName(tempfile.toFilesystemEncoding());
1206 string const command_str = command.str();
1208 LYXERR(Debug::FILES, "Running `" << command_str << '\'');
1210 cmd_ret const ret = runCommand(command_str);
1211 if (ret.first != 0) {
1212 LYXERR0("Could not run file conversion script prefs2prefs.py.");
1219 bool configFileNeedsUpdate(string const & file)
1221 // We cannot initialize configure_script directly because the package
1222 // is not initialized yet when static objects are constructed.
1223 static FileName configure_script;
1224 static bool firstrun = true;
1227 FileName(addName(package().system_support().absFileName(),
1233 FileName(addName(package().user_support().absFileName(), file));
1234 return !absfile.exists()
1235 || configure_script.lastModified() > absfile.lastModified();
1239 int fileLock(const char * lock_file)
1242 #if defined(HAVE_LOCKF)
1243 fd = open(lock_file, O_CREAT|O_APPEND|O_SYNC|O_RDWR, 0666);
1246 if (lockf(fd, F_LOCK, 0) != 0) {
1255 void fileUnlock(int fd, const char * /* lock_file*/)
1257 #if defined(HAVE_LOCKF)
1259 if (lockf(fd, F_ULOCK, 0))
1260 LYXERR0("Can't unlock the file.");
1266 } //namespace support