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/convert.h"
27 #include "support/debug.h"
28 #include "support/environment.h"
29 #include "support/gettext.h"
30 #include "support/lstrings.h"
31 #include "support/lyxlib.h"
32 #include "support/os.h"
33 #include "support/Package.h"
34 #include "support/Path.h"
35 #include "support/Systemcall.h"
37 #include <boost/assert.hpp>
38 #include <boost/regex.hpp>
55 using std::ostringstream;
62 bool isLyXFilename(string const & filename)
64 return suffixIs(ascii_lowercase(filename), ".lyx");
68 bool isSGMLFilename(string const & filename)
70 return suffixIs(ascii_lowercase(filename), ".sgml");
74 bool isValidLaTeXFilename(string const & filename)
76 string const invalid_chars("#$%{}()[]\"^");
77 return filename.find_first_of(invalid_chars) == string::npos;
81 string const latex_path(string const & original_path,
82 latex_path_extension extension,
85 // On cygwin, we may need windows or posix style paths.
86 string path = os::latex_path(original_path);
87 path = subst(path, "~", "\\string~");
88 if (path.find(' ') != string::npos) {
89 // We can't use '"' because " is sometimes active (e.g. if
90 // babel is loaded with the "german" option)
91 if (extension == EXCLUDE_EXTENSION) {
92 // ChangeExtension calls os::internal_path internally
93 // so don't use it to remove the extension.
94 string const ext = getExtension(path);
95 string const base = ext.empty() ?
97 path.substr(0, path.length() - ext.length() - 1);
98 // ChangeExtension calls os::internal_path internally
99 // so don't use it to re-add the extension.
100 path = "\\string\"" + base + "\\string\"." + ext;
102 path = "\\string\"" + path + "\\string\"";
106 return dots == ESCAPE_DOTS ? subst(path, ".", "\\lyxdot ") : path;
110 // Substitutes spaces with underscores in filename (and path)
111 FileName const makeLatexName(FileName const & file)
113 string name = file.onlyFileName();
114 string const path = file.onlyPath().absFilename() + "/";
116 // ok so we scan through the string twice, but who cares.
117 // FIXME: in Unicode time this will break for sure! There is
118 // a non-latin world out there...
119 string const keep = "abcdefghijklmnopqrstuvwxyz"
120 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
121 "@!'()*+,-./0123456789:;<=>?[]`|";
123 string::size_type pos = 0;
124 while ((pos = name.find_first_not_of(keep, pos)) != string::npos)
127 FileName latex_name(path + name);
128 latex_name.changeExtension(".tex");
133 string const quoteName(string const & name, quote_style style)
137 // This does not work for filenames containing " (windows)
138 // or ' (all other OSes). This can't be changed easily, since
139 // we would need to adapt the command line parser in
140 // Forkedcall::generateChild. Therefore we don't pass user
141 // filenames to child processes if possible. We store them in
142 // a python script instead, where we don't have these
144 return (os::shell() == os::UNIX) ?
148 return "\"" + subst(subst(name, "\\", "\\\\"), "\"", "\\\"")
151 // shut up stupid compiler
157 // Uses a string of paths separated by ";"s to find a file to open.
158 // Can't cope with pathnames with a ';' in them. Returns full path to file.
159 // If path entry begins with $$LyX/, use system_lyxdir
160 // If path entry begins with $$User/, use user_lyxdir
161 // Example: "$$User/doc;$$LyX/doc"
162 FileName const fileOpenSearch(string const & path, string const & name,
167 bool notfound = true;
168 string tmppath = split(path, path_element, ';');
170 while (notfound && !path_element.empty()) {
171 path_element = os::internal_path(path_element);
172 if (!suffixIs(path_element, '/'))
174 path_element = subst(path_element, "$$LyX",
175 package().system_support().absFilename());
176 path_element = subst(path_element, "$$User",
177 package().user_support().absFilename());
179 real_file = fileSearch(path_element, name, ext);
181 if (real_file.empty()) {
183 tmppath = split(tmppath, path_element, ';');
184 } while (!tmppath.empty() && path_element.empty());
194 // Returns the real name of file name in directory path, with optional
196 FileName const fileSearch(string const & path, string const & name,
197 string const & ext, search_mode mode)
199 // if `name' is an absolute path, we ignore the setting of `path'
200 // Expand Environmentvariables in 'name'
201 string const tmpname = replaceEnvironmentPath(name);
202 FileName fullname(makeAbsPath(tmpname, path));
203 // search first without extension, then with it.
204 if (fullname.isReadableFile())
208 return mode == allow_unreadable ? fullname : FileName();
209 // Only add the extension if it is not already the extension of
211 if (getExtension(fullname.absFilename()) != ext)
212 fullname = FileName(addExtension(fullname.absFilename(), ext));
213 if (fullname.isReadableFile() || mode == allow_unreadable)
219 // Search the file name.ext in the subdirectory dir of
221 // 2) build_lyxdir (if not empty)
223 FileName const libFileSearch(string const & dir, string const & name,
226 FileName fullname = fileSearch(addPath(package().user_support().absFilename(), dir),
228 if (!fullname.empty())
231 if (!package().build_support().empty())
232 fullname = fileSearch(addPath(package().build_support().absFilename(), dir),
234 if (!fullname.empty())
237 return fileSearch(addPath(package().system_support().absFilename(), dir), name, ext);
241 FileName const i18nLibFileSearch(string const & dir, string const & name,
244 /* The highest priority value is the `LANGUAGE' environment
245 variable. But we don't use the value if the currently
246 selected locale is the C locale. This is a GNU extension.
248 Otherwise, w use a trick to guess what support/gettext.has done:
249 each po file is able to tell us its name. (JMarc)
252 string lang = to_ascii(_("[[Replace with the code of your language]]"));
253 string const language = getEnv("LANGUAGE");
254 if (!lang.empty() && !language.empty())
258 lang = split(lang, l, ':');
261 // First try with the full name
262 tmp = libFileSearch(addPath(dir, l), name, ext);
266 // Then the name without country code
267 string const shortl = token(l, '_', 0);
269 tmp = libFileSearch(addPath(dir, shortl), name, ext);
275 // For compatibility, to be removed later (JMarc)
276 tmp = libFileSearch(dir, token(l, '_', 0) + '_' + name,
279 lyxerr << "i18nLibFileSearch: File `" << tmp
280 << "' has been found by the old method" <<endl;
284 lang = split(lang, l, ':');
287 return libFileSearch(dir, name, ext);
291 string const libScriptSearch(string const & command_in, quote_style style)
293 static string const token_scriptpath = "$$s/";
295 string command = command_in;
296 // Find the starting position of "$$s/"
297 string::size_type const pos1 = command.find(token_scriptpath);
298 if (pos1 == string::npos)
300 // Find the end of the "$$s/some_subdir/some_script" word within
301 // command. Assumes that the script name does not contain spaces.
302 string::size_type const start_script = pos1 + 4;
303 string::size_type const pos2 = command.find(' ', start_script);
304 string::size_type const size_script = pos2 == string::npos?
305 (command.size() - start_script) : pos2 - start_script;
307 // Does this script file exist?
308 string const script =
309 libFileSearch(".", command.substr(start_script, size_script)).absFilename();
311 if (script.empty()) {
312 // Replace "$$s/" with ""
313 command.erase(pos1, 4);
315 // Replace "$$s/foo/some_script" with "<path to>/some_script".
316 string::size_type const size_replace = size_script + 4;
317 command.replace(pos1, size_replace, quoteName(script, style));
324 static FileName createTmpDir(FileName const & tempdir, string const & mask)
326 LYXERR(Debug::FILES, "createTmpDir: tempdir=`" << tempdir << "'\n"
327 << "createTmpDir: mask=`" << mask << '\'');
329 FileName const tmpfl(tempName(tempdir, mask));
330 // lyx::tempName actually creates a file to make sure that it
331 // stays unique. So we have to delete it before we can create
332 // a dir with the same name. Note also that we are not thread
333 // safe because of the gap between unlink and mkdir. (Lgb)
336 if (tmpfl.empty() || mkdir(tmpfl, 0700)) {
337 lyxerr << "LyX could not create the temporary directory '"
338 << tmpfl << "'" << endl;
345 string const createBufferTmpDir()
348 // We are in our own directory. Why bother to mangle name?
349 // In fact I wrote this code to circumvent a problematic behaviour
350 // (bug?) of EMX mkstemp().
352 package().temp_dir().absFilename() + "/lyx_tmpbuf" +
353 convert<string>(count++);
355 if (mkdir(FileName(tmpfl), 0777)) {
356 lyxerr << "LyX could not create the temporary directory '"
357 << tmpfl << "'" << endl;
364 FileName const createLyXTmpDir(FileName const & deflt)
366 if (deflt.empty() || deflt.absFilename() == "/tmp")
367 return createTmpDir(FileName("/tmp"), "lyx_tmpdir");
369 if (!mkdir(deflt, 0777))
372 if (deflt.isDirWritable()) {
373 // deflt could not be created because it
374 // did exist already, so let's create our own
376 return createTmpDir(deflt, "lyx_tmpdir");
378 // some other error occured.
379 return createTmpDir(FileName("/tmp"), "lyx_tmpdir");
384 // Strip filename from path name
385 string const onlyPath(string const & filename)
387 // If empty filename, return empty
388 if (filename.empty())
391 // Find last / or start of filename
392 size_t j = filename.rfind('/');
393 return j == string::npos ? "./" : filename.substr(0, j + 1);
397 // Convert relative path into absolute path based on a basepath.
398 // If relpath is absolute, just use that.
399 // If basepath is empty, use CWD as base.
400 FileName const makeAbsPath(string const & relPath, string const & basePath)
402 // checks for already absolute path
403 if (os::is_absolute_path(relPath))
404 return FileName(relPath);
406 // Copies given paths
407 string tempRel = os::internal_path(relPath);
408 // Since TempRel is NOT absolute, we can safely replace "//" with "/"
409 tempRel = subst(tempRel, "//", "/");
413 if (os::is_absolute_path(basePath))
416 tempBase = addPath(getcwd().absFilename(), basePath);
418 // Handle /./ at the end of the path
419 while (suffixIs(tempBase, "/./"))
420 tempBase.erase(tempBase.length() - 2);
422 // processes relative path
423 string rTemp = tempRel;
426 while (!rTemp.empty()) {
428 rTemp = split(rTemp, temp, '/');
430 if (temp == ".") continue;
432 // Remove one level of TempBase
433 string::difference_type i = tempBase.length() - 2;
436 while (i > 0 && tempBase[i] != '/')
439 tempBase.erase(i, string::npos);
442 } else if (temp.empty() && !rTemp.empty()) {
443 tempBase = os::current_root() + rTemp;
446 // Add this piece to TempBase
447 if (!suffixIs(tempBase, '/'))
453 // returns absolute path
454 return FileName(tempBase);
458 // Correctly append filename to the pathname.
459 // If pathname is '.', then don't use pathname.
460 // Chops any path of filename.
461 string const addName(string const & path, string const & fname)
463 string const basename = onlyFilename(fname);
466 if (path != "." && path != "./" && !path.empty()) {
467 buf = os::internal_path(path);
468 if (!suffixIs(path, '/'))
472 return buf + basename;
476 // Strips path from filename
477 string const onlyFilename(string const & fname)
482 string::size_type j = fname.rfind('/');
483 if (j == string::npos) // no '/' in fname
487 return fname.substr(j + 1);
491 /// Returns true is path is absolute
492 bool absolutePath(string const & path)
494 return os::is_absolute_path(path);
498 // Create absolute path. If impossible, don't do anything
499 // Supports ./ and ~/. Later we can add support for ~logname/. (Asger)
500 string const expandPath(string const & path)
502 // checks for already absolute path
503 string rTemp = replaceEnvironmentPath(path);
504 if (os::is_absolute_path(rTemp))
508 string const copy = rTemp;
511 rTemp = split(rTemp, temp, '/');
514 return getcwd().absFilename() + '/' + rTemp;
517 return package().home_dir().absFilename() + '/' + rTemp;
520 return makeAbsPath(copy).absFilename();
522 // Don't know how to handle this
527 // Search the string for ${VAR} and $VAR and replace VAR using getenv.
528 string const replaceEnvironmentPath(string const & path)
530 // ${VAR} is defined as
531 // $\{[A-Za-z_][A-Za-z_0-9]*\}
532 static string const envvar_br = "[$]\\{([A-Za-z_][A-Za-z_0-9]*)\\}";
534 // $VAR is defined as:
535 // $\{[A-Za-z_][A-Za-z_0-9]*\}
536 static string const envvar = "[$]([A-Za-z_][A-Za-z_0-9]*)";
538 static boost::regex envvar_br_re("(.*)" + envvar_br + "(.*)");
539 static boost::regex envvar_re("(.*)" + envvar + "(.*)");
542 string result = path;
544 regex_match(result, what, envvar_br_re);
545 if (!what[0].matched) {
546 regex_match(result, what, envvar_re);
547 if (!what[0].matched)
550 result = what.str(1) + getEnv(what.str(2)) + what.str(3);
556 // Make relative path out of two absolute paths
557 docstring const makeRelPath(docstring const & abspath, docstring const & basepath)
558 // Makes relative path out of absolute path. If it is deeper than basepath,
559 // it's easy. If basepath and abspath share something (they are all deeper
560 // than some directory), it'll be rendered using ..'s. If they are completely
561 // different, then the absolute path will be used as relative path.
563 docstring::size_type const abslen = abspath.length();
564 docstring::size_type const baselen = basepath.length();
566 docstring::size_type i = os::common_path(abspath, basepath);
569 // actually no match - cannot make it relative
573 // Count how many dirs there are in basepath above match
574 // and append as many '..''s into relpath
576 docstring::size_type j = i;
577 while (j < baselen) {
578 if (basepath[j] == '/') {
579 if (j + 1 == baselen)
586 // Append relative stuff from common directory to abspath
587 if (abspath[i] == '/')
589 for (; i < abslen; ++i)
592 if (suffixIs(buf, '/'))
593 buf.erase(buf.length() - 1);
594 // Substitute empty with .
601 // Append sub-directory(ies) to a path in an intelligent way
602 string const addPath(string const & path, string const & path_2)
605 string const path2 = os::internal_path(path_2);
607 if (!path.empty() && path != "." && path != "./") {
608 buf = os::internal_path(path);
609 if (path[path.length() - 1] != '/')
613 if (!path2.empty()) {
614 string::size_type const p2start = path2.find_first_not_of('/');
615 string::size_type const p2end = path2.find_last_not_of('/');
616 string const tmp = path2.substr(p2start, p2end - p2start + 1);
623 string const changeExtension(string const & oldname, string const & extension)
625 string::size_type const last_slash = oldname.rfind('/');
626 string::size_type last_dot = oldname.rfind('.');
627 if (last_dot < last_slash && last_slash != string::npos)
628 last_dot = string::npos;
631 // Make sure the extension starts with a dot
632 if (!extension.empty() && extension[0] != '.')
633 ext= '.' + extension;
637 return os::internal_path(oldname.substr(0, last_dot) + ext);
641 string const removeExtension(string const & name)
643 return changeExtension(name, string());
647 string const addExtension(string const & name, string const & extension)
649 if (!extension.empty() && extension[0] != '.')
650 return name + '.' + extension;
651 return name + extension;
655 /// Return the extension of the file (not including the .)
656 string const getExtension(string const & name)
658 string::size_type const last_slash = name.rfind('/');
659 string::size_type const last_dot = name.rfind('.');
660 if (last_dot != string::npos &&
661 (last_slash == string::npos || last_dot > last_slash))
662 return name.substr(last_dot + 1,
663 name.length() - (last_dot + 1));
669 string const unzippedFileName(string const & zipped_file)
671 string const ext = getExtension(zipped_file);
672 if (ext == "gz" || ext == "z" || ext == "Z")
673 return changeExtension(zipped_file, string());
674 return "unzipped_" + zipped_file;
678 FileName const unzipFile(FileName const & zipped_file, string const & unzipped_file)
680 FileName const tempfile = FileName(unzipped_file.empty() ?
681 unzippedFileName(zipped_file.toFilesystemEncoding()) :
684 string const command = "gunzip -c " +
685 zipped_file.toFilesystemEncoding() + " > " +
686 tempfile.toFilesystemEncoding();
688 one.startscript(Systemcall::Wait, command);
689 // test that command was executed successfully (anon)
690 // yes, please do. (Lgb)
695 docstring const makeDisplayPath(string const & path, unsigned int threshold)
699 // If file is from LyXDir, display it as if it were relative.
700 string const system = package().system_support().absFilename();
701 if (prefixIs(str, system) && str != system)
702 return from_utf8("[" + str.erase(0, system.length()) + "]");
704 // replace /home/blah with ~/
705 string const home = package().home_dir().absFilename();
706 if (!home.empty() && prefixIs(str, home))
707 str = subst(str, home, "~");
709 if (str.length() <= threshold)
710 return from_utf8(os::external_path(str));
712 string const prefix = ".../";
715 while (str.length() > threshold)
716 str = split(str, temp, '/');
718 // Did we shorten everything away?
720 // Yes, filename itself is too long.
721 // Pick the start and the end of the filename.
722 str = onlyFilename(path);
723 string const head = str.substr(0, threshold / 2 - 3);
725 string::size_type len = str.length();
727 str.substr(len - threshold / 2 - 2, len - 1);
728 str = head + "..." + tail;
731 return from_utf8(os::external_path(prefix + str));
735 bool readLink(FileName const & file, FileName & link)
738 char linkbuffer[512];
739 // Should be PATH_MAX but that needs autconf support
740 string const encoded = file.toFilesystemEncoding();
741 int const nRead = ::readlink(encoded.c_str(),
742 linkbuffer, sizeof(linkbuffer) - 1);
745 linkbuffer[nRead] = '\0'; // terminator
746 link = makeAbsPath(linkbuffer, onlyPath(file.absFilename()));
754 cmd_ret const runCommand(string const & cmd)
756 // FIXME: replace all calls to RunCommand with ForkedCall
757 // (if the output is not needed) or the code in ISpell.cpp
758 // (if the output is needed).
760 // One question is if we should use popen or
761 // create our own popen based on fork, exec, pipe
762 // of course the best would be to have a
763 // pstream (process stream), with the
764 // variants ipstream, opstream
766 #if defined (HAVE_POPEN)
767 FILE * inf = ::popen(cmd.c_str(), os::popen_read_mode());
768 #elif defined (HAVE__POPEN)
769 FILE * inf = ::_popen(cmd.c_str(), os::popen_read_mode());
771 #error No popen() function.
774 // (Claus Hentschel) Check if popen was succesful ;-)
776 lyxerr << "RunCommand:: could not start child process" << endl;
777 return make_pair(-1, string());
783 ret += static_cast<char>(c);
787 #if defined (HAVE_PCLOSE)
788 int const pret = pclose(inf);
789 #elif defined (HAVE__PCLOSE)
790 int const pret = _pclose(inf);
792 #error No pclose() function.
796 perror("RunCommand:: could not terminate child process");
798 return make_pair(pret, ret);
802 FileName const findtexfile(string const & fil, string const & /*format*/)
804 /* There is no problem to extend this function too use other
805 methods to look for files. It could be setup to look
806 in environment paths and also if wanted as a last resort
807 to a recursive find. One of the easier extensions would
808 perhaps be to use the LyX file lookup methods. But! I am
809 going to implement this until I see some demand for it.
813 // If the file can be found directly, we just return a
814 // absolute path version of it.
815 FileName const absfile(makeAbsPath(fil));
816 if (absfile.exists())
819 // No we try to find it using kpsewhich.
820 // It seems from the kpsewhich manual page that it is safe to use
821 // kpsewhich without --format: "When the --format option is not
822 // given, the search path used when looking for a file is inferred
823 // from the name given, by looking for a known extension. If no
824 // known extension is found, the search path for TeX source files
826 // However, we want to take advantage of the format sine almost all
827 // the different formats has environment variables that can be used
828 // to controll which paths to search. f.ex. bib looks in
829 // BIBINPUTS and TEXBIB. Small list follows:
830 // bib - BIBINPUTS, TEXBIB
832 // graphic/figure - TEXPICTS, TEXINPUTS
833 // ist - TEXINDEXSTYLE, INDEXSTYLE
834 // pk - PROGRAMFONTS, PKFONTS, TEXPKS, GLYPHFONTS, TEXFONTS
836 // tfm - TFMFONTS, TEXFONTS
837 // This means that to use kpsewhich in the best possible way we
838 // should help it by setting additional path in the approp. envir.var.
839 string const kpsecmd = "kpsewhich " + fil;
841 cmd_ret const c = runCommand(kpsecmd);
843 LYXERR(Debug::LATEX, "kpse status = " << c.first << '\n'
844 << "kpse result = `" << rtrim(c.second, "\n\r") << '\'');
846 return FileName(rtrim(to_utf8(from_filesystem8bit(c.second)), "\n\r"));
852 void removeAutosaveFile(string const & filename)
854 string a = onlyPath(filename);
856 a += onlyFilename(filename);
858 FileName const autosave(a);
859 if (autosave.exists())
860 autosave.removeFile();
864 void readBB_lyxerrMessage(FileName const & file, bool & zipped,
865 string const & message)
867 LYXERR(Debug::GRAPHICS, "[readBB_from_PSFile] " << message);
868 // FIXME: Why is this func deleting a file? (Lgb)
874 string const readBB_from_PSFile(FileName const & file)
876 // in a (e)ps-file it's an entry like %%BoundingBox:23 45 321 345
877 // It seems that every command in the header has an own line,
878 // getline() should work for all files.
879 // On the other hand some plot programs write the bb at the
880 // end of the file. Than we have in the header:
881 // %%BoundingBox: (atend)
882 // In this case we must check the end.
883 bool zipped = file.isZippedFile();
884 FileName const file_ = zipped ? unzipFile(file) : file;
885 string const format = file_.guessFormatFromContents();
887 if (format != "eps" && format != "ps") {
888 readBB_lyxerrMessage(file_, zipped,"no(e)ps-format");
892 static boost::regex bbox_re(
893 "^%%BoundingBox:\\s*([[:digit:]]+)\\s+([[:digit:]]+)\\s+([[:digit:]]+)\\s+([[:digit:]]+)");
894 std::ifstream is(file_.toFilesystemEncoding().c_str());
899 if (regex_match(s, what, bbox_re)) {
900 // Our callers expect the tokens in the string
901 // separated by single spaces.
902 // FIXME: change return type from string to something
905 os << what.str(1) << ' ' << what.str(2) << ' '
906 << what.str(3) << ' ' << what.str(4);
907 string const bb = os.str();
908 readBB_lyxerrMessage(file_, zipped, bb);
912 readBB_lyxerrMessage(file_, zipped, "no bb found");
917 int compare_timestamps(FileName const & file1, FileName const & file2)
919 // If the original is newer than the copy, then copy the original
920 // to the new directory.
923 if (file1.exists() && file2.exists()) {
924 double const tmp = difftime(file1.lastModified(), file2.lastModified());
926 cmp = tmp > 0 ? 1 : -1;
928 } else if (file1.exists()) {
930 } else if (file2.exists()) {
937 } //namespace support