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
24 #include "support/filetools.h"
26 #include "support/debug.h"
27 #include "support/environment.h"
28 #include "support/gettext.h"
29 #include "support/lstrings.h"
30 #include "support/os.h"
31 #include "support/Package.h"
32 #include "support/Path.h"
33 #include "support/Systemcall.h"
34 #include "support/qstring_helpers.h"
38 #include "support/lassert.h"
39 #include <boost/regex.hpp>
58 bool isLyXFileName(string const & filename)
60 return suffixIs(ascii_lowercase(filename), ".lyx");
64 bool isSGMLFileName(string const & filename)
66 return suffixIs(ascii_lowercase(filename), ".sgml");
70 bool isValidLaTeXFileName(string const & filename)
72 string const invalid_chars("#$%{}()[]\"^");
73 return filename.find_first_of(invalid_chars) == string::npos;
77 string const latex_path(string const & original_path,
78 latex_path_extension extension,
81 // On cygwin, we may need windows or posix style paths.
82 string path = os::latex_path(original_path);
83 path = subst(path, "~", "\\string~");
84 if (path.find(' ') != string::npos) {
85 // We can't use '"' because " is sometimes active (e.g. if
86 // babel is loaded with the "german" option)
87 if (extension == EXCLUDE_EXTENSION) {
88 // ChangeExtension calls os::internal_path internally
89 // so don't use it to remove the extension.
90 string const ext = getExtension(path);
91 string const base = ext.empty() ?
93 path.substr(0, path.length() - ext.length() - 1);
94 // ChangeExtension calls os::internal_path internally
95 // so don't use it to re-add the extension.
96 path = "\\string\"" + base + "\\string\"." + ext;
98 path = "\\string\"" + path + "\\string\"";
102 return dots == ESCAPE_DOTS ? subst(path, ".", "\\lyxdot ") : path;
106 // Substitutes spaces with underscores in filename (and path)
107 FileName const makeLatexName(FileName const & file)
109 string name = file.onlyFileName();
110 string const path = file.onlyPath().absFileName() + "/";
112 // ok so we scan through the string twice, but who cares.
113 // FIXME: in Unicode time this will break for sure! There is
114 // a non-latin world out there...
115 string const keep = "abcdefghijklmnopqrstuvwxyz"
116 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
117 "@!'()*+,-./0123456789:;<=>?[]`|";
119 string::size_type pos = 0;
120 while ((pos = name.find_first_not_of(keep, pos)) != string::npos)
123 FileName latex_name(path + name);
124 latex_name.changeExtension(".tex");
129 string const quoteName(string const & name, quote_style style)
133 // This does not work on native Windows for filenames
134 // containing the following characters < > : " / \ | ? *
135 // Moreover, it can't be made to work, as, according to
136 // http://msdn.microsoft.com/en-us/library/aa365247(VS.85).aspx
137 // those are reserved characters, and thus are forbidden.
138 // Please, also note that the command-line parser in
139 // ForkedCall::generateChild cannot deal with filenames
140 // containing " or ', therefore we don't pass user filenames
141 // to child processes if possible. We store them in a python
142 // script instead, where we don't have these limitations.
144 return (os::shell() == os::UNIX) ?
145 '\'' + subst(name, "'", "\'\\\'\'") + '\'' :
148 // According to the QProcess parser, a single double
149 // quote is represented by three consecutive ones.
150 // Here we simply escape the double quote and let our
151 // simple parser in Systemcall.cpp do the substitution.
152 return '"' + subst(name, "\"", "\\\"") + '"';
155 return "\"" + subst(subst(name, "\\", "\\\\"), "\"", "\\\"")
158 // shut up stupid compiler
164 // Uses a string of paths separated by ";"s to find a file to open.
165 // Can't cope with pathnames with a ';' in them. Returns full path to file.
166 // If path entry begins with $$LyX/, use system_lyxdir
167 // If path entry begins with $$User/, use user_lyxdir
168 // Example: "$$User/doc;$$LyX/doc"
169 FileName const fileOpenSearch(string const & path, string const & name,
174 bool notfound = true;
175 string tmppath = split(path, path_element, ';');
177 while (notfound && !path_element.empty()) {
178 path_element = os::internal_path(path_element);
179 if (!suffixIs(path_element, '/'))
181 path_element = subst(path_element, "$$LyX",
182 package().system_support().absFileName());
183 path_element = subst(path_element, "$$User",
184 package().user_support().absFileName());
186 real_file = fileSearch(path_element, name, ext);
188 if (real_file.empty()) {
190 tmppath = split(tmppath, path_element, ';');
191 } while (!tmppath.empty() && path_element.empty());
201 // Returns the real name of file name in directory path, with optional
203 FileName const fileSearch(string const & path, string const & name,
204 string const & ext, search_mode mode)
206 // if `name' is an absolute path, we ignore the setting of `path'
207 // Expand Environmentvariables in 'name'
208 string const tmpname = replaceEnvironmentPath(name);
209 FileName fullname(makeAbsPath(tmpname, path));
210 // search first without extension, then with it.
211 if (fullname.isReadableFile())
215 return mode == may_not_exist ? fullname : FileName();
216 // Only add the extension if it is not already the extension of
218 if (getExtension(fullname.absFileName()) != ext)
219 fullname = FileName(addExtension(fullname.absFileName(), ext));
220 if (fullname.isReadableFile() || mode == may_not_exist)
226 // Search the file name.ext in the subdirectory dir of
228 // 2) build_lyxdir (if not empty)
230 FileName const libFileSearch(string const & dir, string const & name,
233 FileName fullname = fileSearch(addPath(package().user_support().absFileName(), dir),
235 if (!fullname.empty())
238 if (!package().build_support().empty())
239 fullname = fileSearch(addPath(package().build_support().absFileName(), dir),
241 if (!fullname.empty())
244 return fileSearch(addPath(package().system_support().absFileName(), dir), name, ext);
248 FileName const i18nLibFileSearch(string const & dir, string const & name,
251 /* The highest priority value is the `LANGUAGE' environment
252 variable. But we don't use the value if the currently
253 selected locale is the C locale. This is a GNU extension.
255 Otherwise, w use a trick to guess what support/gettext.has done:
256 each po file is able to tell us its name. (JMarc)
259 string lang = to_ascii(_("[[Replace with the code of your language]]"));
260 string const language = getEnv("LANGUAGE");
261 if (!lang.empty() && !language.empty())
265 lang = split(lang, l, ':');
268 // First try with the full name
269 tmp = libFileSearch(addPath(dir, l), name, ext);
273 // Then the name without country code
274 string const shortl = token(l, '_', 0);
276 tmp = libFileSearch(addPath(dir, shortl), name, ext);
282 // For compatibility, to be removed later (JMarc)
283 tmp = libFileSearch(dir, token(l, '_', 0) + '_' + name,
286 lyxerr << "i18nLibFileSearch: File `" << tmp
287 << "' has been found by the old method" <<endl;
291 lang = split(lang, l, ':');
294 return libFileSearch(dir, name, ext);
298 string const libScriptSearch(string const & command_in, quote_style style)
300 static string const token_scriptpath = "$$s/";
302 string command = command_in;
303 // Find the starting position of "$$s/"
304 string::size_type const pos1 = command.find(token_scriptpath);
305 if (pos1 == string::npos)
307 // Find the end of the "$$s/some_subdir/some_script" word within
308 // command. Assumes that the script name does not contain spaces.
309 string::size_type const start_script = pos1 + 4;
310 string::size_type const pos2 = command.find(' ', start_script);
311 string::size_type const size_script = pos2 == string::npos?
312 (command.size() - start_script) : pos2 - start_script;
314 // Does this script file exist?
315 string const script =
316 libFileSearch(".", command.substr(start_script, size_script)).absFileName();
318 if (script.empty()) {
319 // Replace "$$s/" with ""
320 command.erase(pos1, 4);
322 // Replace "$$s/foo/some_script" with "<path to>/some_script".
323 string::size_type const size_replace = size_script + 4;
324 command.replace(pos1, size_replace, quoteName(script, style));
331 static FileName createTmpDir(FileName const & tempdir, string const & mask)
333 LYXERR(Debug::FILES, "createTmpDir: tempdir=`" << tempdir << "'\n"
334 << "createTmpDir: mask=`" << mask << '\'');
336 FileName const tmpfl = FileName::tempName(tempdir, mask);
338 if (tmpfl.empty() || !tmpfl.createDirectory(0700)) {
339 LYXERR0("LyX could not create temporary directory in " << tempdir
348 FileName const createLyXTmpDir(FileName const & deflt)
350 if (deflt.empty() || deflt == package().system_temp_dir())
351 return createTmpDir(package().system_temp_dir(), "lyx_tmpdir");
353 if (deflt.createDirectory(0777))
356 if (deflt.isDirWritable()) {
357 // deflt could not be created because it
358 // did exist already, so let's create our own
360 return createTmpDir(deflt, "lyx_tmpdir");
362 // some other error occured.
363 return createTmpDir(package().system_temp_dir(), "lyx_tmpdir");
368 // Strip filename from path name
369 string const onlyPath(string const & filename)
371 // If empty filename, return empty
372 if (filename.empty())
375 // Find last / or start of filename
376 size_t j = filename.rfind('/');
377 return j == string::npos ? "./" : filename.substr(0, j + 1);
381 // Convert relative path into absolute path based on a basepath.
382 // If relpath is absolute, just use that.
383 // If basepath is empty, use CWD as base.
384 // Note that basePath can be a relative path, in the sense that it may
385 // not begin with "/" (e.g.), but it should NOT contain such constructs
387 // FIXME It might be nice if the code didn't simply assume that.
388 FileName const makeAbsPath(string const & relPath, string const & basePath)
390 // checks for already absolute path
391 if (FileName::isAbsolute(relPath))
392 return FileName(relPath);
394 // Copies given paths
395 string tempRel = os::internal_path(relPath);
396 // Since TempRel is NOT absolute, we can safely replace "//" with "/"
397 tempRel = subst(tempRel, "//", "/");
401 if (FileName::isAbsolute(basePath))
404 tempBase = addPath(FileName::getcwd().absFileName(), basePath);
406 // Handle /./ at the end of the path
407 while (suffixIs(tempBase, "/./"))
408 tempBase.erase(tempBase.length() - 2);
410 // processes relative path
411 string rTemp = tempRel;
414 // Check for a leading "~"
416 rTemp = split(rTemp, temp, '/');
418 tempBase = package().home_dir().absFileName();
423 while (!rTemp.empty()) {
425 rTemp = split(rTemp, temp, '/');
427 if (temp == ".") continue;
429 // Remove one level of TempBase
430 if (tempBase.length() <= 1) {
431 //this is supposed to be an absolute path, so...
435 //erase a trailing slash if there is one
436 if (suffixIs(tempBase, "/"))
437 tempBase.erase(tempBase.length() - 1, string::npos);
439 string::size_type i = tempBase.length() - 1;
440 while (i > 0 && tempBase[i] != '/')
443 tempBase.erase(i, string::npos);
446 } else if (temp.empty() && !rTemp.empty()) {
447 tempBase = os::current_root() + rTemp;
450 // Add this piece to TempBase
451 if (!suffixIs(tempBase, '/'))
457 // returns absolute path
458 return FileName(tempBase);
462 // Correctly append filename to the pathname.
463 // If pathname is '.', then don't use pathname.
464 // Chops any path of filename.
465 string const addName(string const & path, string const & fname)
467 string const basename = onlyFileName(fname);
470 if (path != "." && path != "./" && !path.empty()) {
471 buf = os::internal_path(path);
472 if (!suffixIs(path, '/'))
476 return buf + basename;
480 // Strips path from filename
481 string const onlyFileName(string const & fname)
486 string::size_type j = fname.rfind('/');
487 if (j == string::npos) // no '/' in fname
491 return fname.substr(j + 1);
495 // Create absolute path. If impossible, don't do anything
496 // Supports ./ and ~/. Later we can add support for ~logname/. (Asger)
497 string const expandPath(string const & path)
499 // checks for already absolute path
500 string rTemp = replaceEnvironmentPath(path);
501 if (FileName::isAbsolute(rTemp))
505 string const copy = rTemp;
508 rTemp = split(rTemp, temp, '/');
511 return FileName::getcwd().absFileName() + '/' + rTemp;
514 return package().home_dir().absFileName() + '/' + rTemp;
517 return makeAbsPath(copy).absFileName();
519 // Don't know how to handle this
524 // Search the string for ${VAR} and $VAR and replace VAR using getenv.
525 string const replaceEnvironmentPath(string const & path)
527 // ${VAR} is defined as
528 // $\{[A-Za-z_][A-Za-z_0-9]*\}
529 static string const envvar_br = "[$]\\{([A-Za-z_][A-Za-z_0-9]*)\\}";
531 // $VAR is defined as:
532 // $\{[A-Za-z_][A-Za-z_0-9]*\}
533 static string const envvar = "[$]([A-Za-z_][A-Za-z_0-9]*)";
535 static boost::regex envvar_br_re("(.*)" + envvar_br + "(.*)");
536 static boost::regex envvar_re("(.*)" + envvar + "(.*)");
539 string remaining = path;
541 regex_match(remaining, what, envvar_br_re);
542 if (!what[0].matched) {
543 regex_match(remaining, what, envvar_re);
544 if (!what[0].matched) {
549 string env_var = getEnv(what.str(2));
550 if (!env_var.empty())
551 result += what.str(1) + env_var;
553 result += what.str(1) + "$" + what.str(2);
554 remaining = what.str(3);
560 // Make relative path out of two absolute paths
561 docstring const makeRelPath(docstring const & abspath, docstring const & basepath)
562 // Makes relative path out of absolute path. If it is deeper than basepath,
563 // it's easy. If basepath and abspath share something (they are all deeper
564 // than some directory), it'll be rendered using ..'s. If they are completely
565 // different, then the absolute path will be used as relative path.
567 docstring::size_type const abslen = abspath.length();
568 docstring::size_type const baselen = basepath.length();
570 docstring::size_type i = os::common_path(abspath, basepath);
573 // actually no match - cannot make it relative
577 // Count how many dirs there are in basepath above match
578 // and append as many '..''s into relpath
580 docstring::size_type j = i;
581 while (j < baselen) {
582 if (basepath[j] == '/') {
583 if (j + 1 == baselen)
590 // Append relative stuff from common directory to abspath
591 if (abspath[i] == '/')
593 for (; i < abslen; ++i)
596 if (suffixIs(buf, '/'))
597 buf.erase(buf.length() - 1);
598 // Substitute empty with .
605 // Append sub-directory(ies) to a path in an intelligent way
606 string const addPath(string const & path, string const & path_2)
609 string const path2 = os::internal_path(path_2);
611 if (!path.empty() && path != "." && path != "./") {
612 buf = os::internal_path(path);
613 if (path[path.length() - 1] != '/')
617 if (!path2.empty()) {
618 string::size_type const p2start = path2.find_first_not_of('/');
619 string::size_type const p2end = path2.find_last_not_of('/');
620 string const tmp = path2.substr(p2start, p2end - p2start + 1);
627 string const changeExtension(string const & oldname, string const & extension)
629 string::size_type const last_slash = oldname.rfind('/');
630 string::size_type last_dot = oldname.rfind('.');
631 if (last_dot < last_slash && last_slash != string::npos)
632 last_dot = string::npos;
635 // Make sure the extension starts with a dot
636 if (!extension.empty() && extension[0] != '.')
637 ext= '.' + extension;
641 return os::internal_path(oldname.substr(0, last_dot) + ext);
645 string const removeExtension(string const & name)
647 return changeExtension(name, string());
651 string const addExtension(string const & name, string const & extension)
653 if (!extension.empty() && extension[0] != '.')
654 return name + '.' + extension;
655 return name + extension;
659 /// Return the extension of the file (not including the .)
660 string const getExtension(string const & name)
662 string::size_type const last_slash = name.rfind('/');
663 string::size_type const last_dot = name.rfind('.');
664 if (last_dot != string::npos &&
665 (last_slash == string::npos || last_dot > last_slash))
666 return name.substr(last_dot + 1,
667 name.length() - (last_dot + 1));
673 string const unzippedFileName(string const & zipped_file)
675 string const ext = getExtension(zipped_file);
676 if (ext == "gz" || ext == "z" || ext == "Z")
677 return changeExtension(zipped_file, string());
678 return onlyPath(zipped_file) + "unzipped_" + onlyFileName(zipped_file);
682 FileName const unzipFile(FileName const & zipped_file, string const & unzipped_file)
684 FileName const tempfile = FileName(unzipped_file.empty() ?
685 unzippedFileName(zipped_file.toFilesystemEncoding()) :
688 string const command = "gunzip -c " +
689 zipped_file.toFilesystemEncoding() + " > " +
690 tempfile.toFilesystemEncoding();
692 one.startscript(Systemcall::Wait, command);
693 // test that command was executed successfully (anon)
694 // yes, please do. (Lgb)
699 docstring const makeDisplayPath(string const & path, unsigned int threshold)
703 // If file is from LyXDir, display it as if it were relative.
704 string const system = package().system_support().absFileName();
705 if (prefixIs(str, system) && str != system)
706 return from_utf8("[" + str.erase(0, system.length()) + "]");
708 // replace /home/blah with ~/
709 string const home = package().home_dir().absFileName();
710 if (!home.empty() && prefixIs(str, home))
711 str = subst(str, home, "~");
713 if (str.length() <= threshold)
714 return from_utf8(os::external_path(str));
716 string const prefix = ".../";
719 while (str.length() > threshold)
720 str = split(str, temp, '/');
722 // Did we shorten everything away?
724 // Yes, filename itself is too long.
725 // Pick the start and the end of the filename.
726 str = onlyFileName(path);
727 string const head = str.substr(0, threshold / 2 - 3);
729 string::size_type len = str.length();
731 str.substr(len - threshold / 2 - 2, len - 1);
732 str = head + "..." + tail;
735 return from_utf8(os::external_path(prefix + str));
739 bool readLink(FileName const & file, FileName & link)
742 char linkbuffer[PATH_MAX + 1];
743 string const encoded = file.toFilesystemEncoding();
744 int const nRead = ::readlink(encoded.c_str(),
745 linkbuffer, sizeof(linkbuffer) - 1);
748 linkbuffer[nRead] = '\0'; // terminator
749 link = makeAbsPath(linkbuffer, onlyPath(file.absFileName()));
757 cmd_ret const runCommand(string const & cmd)
759 // FIXME: replace all calls to RunCommand with ForkedCall
760 // (if the output is not needed) or the code in ISpell.cpp
761 // (if the output is needed).
763 // One question is if we should use popen or
764 // create our own popen based on fork, exec, pipe
765 // of course the best would be to have a
766 // pstream (process stream), with the
767 // variants ipstream, opstream
769 #if defined (HAVE_POPEN)
770 FILE * inf = ::popen(cmd.c_str(), os::popen_read_mode());
771 #elif defined (HAVE__POPEN)
772 FILE * inf = ::_popen(cmd.c_str(), os::popen_read_mode());
774 #error No popen() function.
777 // (Claus Hentschel) Check if popen was succesful ;-)
779 lyxerr << "RunCommand:: could not start child process" << endl;
780 return make_pair(-1, string());
786 ret += static_cast<char>(c);
790 #if defined (HAVE_PCLOSE)
791 int const pret = pclose(inf);
792 #elif defined (HAVE__PCLOSE)
793 int const pret = _pclose(inf);
795 #error No pclose() function.
799 perror("RunCommand:: could not terminate child process");
801 return make_pair(pret, ret);
805 FileName const findtexfile(string const & fil, string const & /*format*/)
807 /* There is no problem to extend this function too use other
808 methods to look for files. It could be setup to look
809 in environment paths and also if wanted as a last resort
810 to a recursive find. One of the easier extensions would
811 perhaps be to use the LyX file lookup methods. But! I am
812 going to implement this until I see some demand for it.
816 // If the file can be found directly, we just return a
817 // absolute path version of it.
818 FileName const absfile(makeAbsPath(fil));
819 if (absfile.exists())
822 // Now we try to find it using kpsewhich.
823 // It seems from the kpsewhich manual page that it is safe to use
824 // kpsewhich without --format: "When the --format option is not
825 // given, the search path used when looking for a file is inferred
826 // from the name given, by looking for a known extension. If no
827 // known extension is found, the search path for TeX source files
829 // However, we want to take advantage of the format sine almost all
830 // the different formats has environment variables that can be used
831 // to controll which paths to search. f.ex. bib looks in
832 // BIBINPUTS and TEXBIB. Small list follows:
833 // bib - BIBINPUTS, TEXBIB
835 // graphic/figure - TEXPICTS, TEXINPUTS
836 // ist - TEXINDEXSTYLE, INDEXSTYLE
837 // pk - PROGRAMFONTS, PKFONTS, TEXPKS, GLYPHFONTS, TEXFONTS
839 // tfm - TFMFONTS, TEXFONTS
840 // This means that to use kpsewhich in the best possible way we
841 // should help it by setting additional path in the approp. envir.var.
842 string const kpsecmd = "kpsewhich " + fil;
844 cmd_ret const c = runCommand(kpsecmd);
846 LYXERR(Debug::LATEX, "kpse status = " << c.first << '\n'
847 << "kpse result = `" << rtrim(c.second, "\n\r") << '\'');
849 return FileName(rtrim(to_utf8(from_filesystem8bit(c.second)), "\n\r"));
855 void readBB_lyxerrMessage(FileName const & file, bool & zipped,
856 string const & message)
858 LYXERR(Debug::GRAPHICS, "[readBB_from_PSFile] " << message);
859 // FIXME: Why is this func deleting a file? (Lgb)
865 string const readBB_from_PSFile(FileName const & file)
867 // in a (e)ps-file it's an entry like %%BoundingBox:23 45 321 345
868 // It seems that every command in the header has an own line,
869 // getline() should work for all files.
870 // On the other hand some plot programs write the bb at the
871 // end of the file. Than we have in the header:
872 // %%BoundingBox: (atend)
873 // In this case we must check the end.
874 bool zipped = file.isZippedFile();
875 FileName const file_ = zipped ? unzipFile(file) : file;
876 string const format = file_.guessFormatFromContents();
878 if (format != "eps" && format != "ps") {
879 readBB_lyxerrMessage(file_, zipped,"no(e)ps-format");
883 static boost::regex bbox_re(
884 "^%%BoundingBox:\\s*([[:digit:]]+)\\s+([[:digit:]]+)\\s+([[:digit:]]+)\\s+([[:digit:]]+)");
885 ifstream is(file_.toFilesystemEncoding().c_str());
890 if (regex_match(s, what, bbox_re)) {
891 // Our callers expect the tokens in the string
892 // separated by single spaces.
893 // FIXME: change return type from string to something
896 os << what.str(1) << ' ' << what.str(2) << ' '
897 << what.str(3) << ' ' << what.str(4);
898 string const bb = os.str();
899 readBB_lyxerrMessage(file_, zipped, bb);
903 readBB_lyxerrMessage(file_, zipped, "no bb found");
908 int compare_timestamps(FileName const & file1, FileName const & file2)
910 // If the original is newer than the copy, then copy the original
911 // to the new directory.
914 if (file1.exists() && file2.exists()) {
915 double const tmp = difftime(file1.lastModified(), file2.lastModified());
917 cmp = tmp > 0 ? 1 : -1;
919 } else if (file1.exists()) {
921 } else if (file2.exists()) {
929 } //namespace support