]> git.lyx.org Git - features.git/blob - src/support/filetools.cpp
b28d81210c48283395c566dca52bd898994848cc
[features.git] / src / support / filetools.cpp
1 /**
2  * \file filetools.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * parts Copyright 1985, 1990, 1993 Free Software Foundation, Inc.
7  *
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
14  * \author John Levon
15  * \author Herbert Voß
16  *
17  * Full author contact details are available in file CREDITS.
18  *
19  * General path-mangling functions
20  */
21
22 #include <config.h>
23
24 #include "LyXRC.h"
25
26 #include "support/filetools.h"
27
28 #include "support/debug.h"
29 #include "support/environment.h"
30 #include "support/gettext.h"
31 #include "support/lstrings.h"
32 #include "support/os.h"
33 #include "support/Messages.h"
34 #include "support/Package.h"
35 #include "support/PathChanger.h"
36 #include "support/Systemcall.h"
37 #include "support/qstring_helpers.h"
38
39 #include <QDir>
40 #include <QTemporaryFile>
41
42 #include "support/lassert.h"
43 #include "support/regex.h"
44
45 #include <fcntl.h>
46 #ifdef HAVE_MAGIC_H
47 #include <magic.h>
48 #endif
49 #ifdef HAVE_UNISTD_H
50 #include <unistd.h>
51 #endif
52
53 #include <cerrno>
54 #include <climits>
55 #include <cstdlib>
56 #include <cstdio>
57
58 #include <utility>
59 #include <fstream>
60 #include <sstream>
61 #include <vector>
62
63 #if defined (_WIN32)
64 #include <io.h>
65 #include <windows.h>
66 #endif
67
68 using namespace std;
69
70 #define USE_QPROCESS
71
72 namespace lyx {
73 namespace support {
74
75 bool isLyXFileName(string const & filename)
76 {
77         return suffixIs(ascii_lowercase(filename), ".lyx");
78 }
79
80
81 bool isSGMLFileName(string const & filename)
82 {
83         return suffixIs(ascii_lowercase(filename), ".sgml");
84 }
85
86
87 bool isValidLaTeXFileName(string const & filename)
88 {
89         string const invalid_chars("#%\"");
90         return filename.find_first_of(invalid_chars) == string::npos;
91 }
92
93
94 bool isValidDVIFileName(string const & filename)
95 {
96         string const invalid_chars("${}()[]^");
97         return filename.find_first_of(invalid_chars) == string::npos;
98 }
99
100
101 bool isBinaryFile(FileName const & filename)
102 {
103         bool isbinary = false;
104         if (filename.empty() || !filename.exists())
105                 return isbinary;
106
107 #ifdef HAVE_MAGIC_H
108         magic_t magic_cookie = magic_open(MAGIC_MIME_ENCODING);
109         if (magic_cookie) {
110                 bool detected = true;
111                 if (magic_load(magic_cookie, NULL) != 0) {
112                         LYXERR(Debug::FILES, "isBinaryFile: "
113                                 "Could not load magic database - "
114                                 << magic_error(magic_cookie));
115                         detected = false;
116                 } else {
117                         char const *charset = magic_file(magic_cookie,
118                                         filename.toFilesystemEncoding().c_str());
119                         isbinary = contains(charset, "binary");
120                 }
121                 magic_close(magic_cookie);
122                 if (detected)
123                         return isbinary;
124         }
125 #endif
126         // Try by looking for binary chars at the beginning of the file.
127         // Note that this is formally not correct, since count_bin_chars
128         // expects utf8, and the passed string can be anything: plain text
129         // in any encoding, or really binary data. In practice it works,
130         // since QString::fromUtf8() drops invalid utf8 sequences, and
131         // while the exact number may not be correct, we still get a high
132         // number for truly binary files.
133
134         ifstream ifs(filename.toFilesystemEncoding().c_str());
135         if (!ifs)
136                 return isbinary;
137
138         // Maximum strings to read
139         int const max_count = 50;
140
141         // Maximum number of binary chars allowed
142         int const max_bin = 5;
143
144         int count = 0;
145         int binchars = 0;
146         string str;
147         while (count++ < max_count && !ifs.eof()) {
148                 getline(ifs, str);
149                 binchars += count_bin_chars(str);
150         }
151         return binchars > max_bin;
152 }
153
154
155 string const latex_path(string const & original_path,
156                 latex_path_extension extension,
157                 latex_path_dots dots)
158 {
159         // On cygwin, we may need windows or posix style paths.
160         string path = os::latex_path(original_path);
161         path = subst(path, "~", "\\string~");
162         if (path.find(' ') != string::npos) {
163                 // We can't use '"' because " is sometimes active (e.g. if
164                 // babel is loaded with the "german" option)
165                 if (extension == EXCLUDE_EXTENSION) {
166                         // changeExtension calls os::internal_path internally
167                         // so don't use it to remove the extension.
168                         string const ext = getExtension(path);
169                         string const base = ext.empty() ?
170                                 path :
171                                 path.substr(0, path.length() - ext.length() - 1);
172                         // changeExtension calls os::internal_path internally
173                         // so don't use it to re-add the extension.
174                         path = "\\string\"" + base + "\\string\"." + ext;
175                 } else {
176                         path = "\\string\"" + path + "\\string\"";
177                 }
178         }
179
180         if (dots != ESCAPE_DOTS)
181                 return path;
182
183         // Replace dots with the lyxdot macro, but only in the file name,
184         // not the directory part.
185         // addName etc call os::internal_path internally
186         // so don't use them for path manipulation
187         // The directory separator is always '/' for LaTeX.
188         string::size_type pos = path.rfind('/');
189         if (pos == string::npos)
190                 return subst(path, ".", "\\lyxdot ");
191         return path.substr(0, pos) + subst(path.substr(pos), ".", "\\lyxdot ");
192 }
193
194
195 // Substitutes spaces with underscores in filename (and path)
196 FileName const makeLatexName(FileName const & file)
197 {
198         string name = file.onlyFileName();
199         string const path = file.onlyPath().absFileName() + "/";
200
201         // ok so we scan through the string twice, but who cares.
202         // FIXME: in Unicode time this will break for sure! There is
203         // a non-latin world out there...
204         string const keep = "abcdefghijklmnopqrstuvwxyz"
205                 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
206                 "@!'()*+,-./0123456789:;<=>?[]`|";
207
208         string::size_type pos = 0;
209         while ((pos = name.find_first_not_of(keep, pos)) != string::npos)
210                 name[pos++] = '_';
211
212         FileName latex_name(path + name);
213         latex_name.changeExtension(".tex");
214         return latex_name;
215 }
216
217
218 string const quoteName(string const & name, quote_style style)
219 {
220         switch(style) {
221         case quote_shell:
222                 // This does not work on native Windows for filenames
223                 // containing the following characters < > : " / \ | ? *
224                 // Moreover, it can't be made to work, as, according to
225                 // http://msdn.microsoft.com/en-us/library/aa365247(VS.85).aspx
226                 // those are reserved characters, and thus are forbidden.
227                 // Please, also note that the command-line parser in
228                 // ForkedCall::generateChild cannot deal with filenames
229                 // containing " or ', therefore we don't pass user filenames
230                 // to child processes if possible. We store them in a python
231                 // script instead, where we don't have these limitations.
232 #ifndef USE_QPROCESS
233                 return (os::shell() == os::UNIX) ?
234                         '\'' + subst(name, "'", "\'\\\'\'") + '\'' :
235                         '"' + name + '"';
236 #else
237                 // According to the QProcess parser, a single double
238                 // quote is represented by three consecutive ones.
239                 // Here we simply escape the double quote and let our
240                 // simple parser in Systemcall.cpp do the substitution.
241                 return '"' + subst(name, "\"", "\\\"") + '"';
242 #endif
243         case quote_shell_filename:
244                 return quoteName(os::external_path(name), quote_shell);
245         case quote_python:
246                 return "\"" + subst(subst(name, "\\", "\\\\"), "\"", "\\\"")
247                      + "\"";
248         }
249         // shut up stupid compiler
250         return string();
251 }
252
253
254 #if 0
255 // Uses a string of paths separated by ";"s to find a file to open.
256 // Can't cope with pathnames with a ';' in them. Returns full path to file.
257 // If path entry begins with $$LyX/, use system_lyxdir
258 // If path entry begins with $$User/, use user_lyxdir
259 // Example: "$$User/doc;$$LyX/doc"
260 FileName const fileOpenSearch(string const & path, string const & name,
261                              string const & ext)
262 {
263         FileName real_file;
264         string path_element;
265         bool notfound = true;
266         string tmppath = split(path, path_element, ';');
267
268         while (notfound && !path_element.empty()) {
269                 path_element = os::internal_path(path_element);
270                 if (!suffixIs(path_element, '/'))
271                         path_element += '/';
272                 path_element = subst(path_element, "$$LyX",
273                                      package().system_support().absFileName());
274                 path_element = subst(path_element, "$$User",
275                                      package().user_support().absFileName());
276
277                 real_file = fileSearch(path_element, name, ext);
278
279                 if (real_file.empty()) {
280                         do {
281                                 tmppath = split(tmppath, path_element, ';');
282                         } while (!tmppath.empty() && path_element.empty());
283                 } else {
284                         notfound = false;
285                 }
286         }
287         return real_file;
288 }
289 #endif
290
291
292 // Returns the real name of file name in directory path, with optional
293 // extension ext.
294 FileName const fileSearch(string const & path, string const & name,
295                           string const & exts, search_mode mode)
296 {
297         // if `name' is an absolute path, we ignore the setting of `path'
298         // Expand Environmentvariables in 'name'
299         string const tmpname = replaceEnvironmentPath(name);
300         FileName fullname(makeAbsPath(tmpname, path));
301         // search first without extension, then with it.
302         if (fullname.isReadableFile())
303                 return fullname;
304         if (exts.empty())
305                 // We are done.
306                 return mode == may_not_exist ? fullname : FileName();
307         int n = 0;
308         string ext = token(exts, ',', n);
309         while (!ext.empty()) {
310                 // Only add the extension if it is not already the extension of
311                 // fullname.
312                 bool addext = getExtension(fullname.absFileName()) != ext;
313                 if (addext) {
314                         if (mode == check_hidpi) {
315                                 FileName fullname2x = FileName(addExtension(fullname.absFileName() + "@2x", ext));
316                                 if (fullname2x.isReadableFile())
317                                         return fullname2x;
318                         }
319                         fullname = FileName(addExtension(fullname.absFileName(), ext));
320                 }
321                 if (fullname.isReadableFile() || mode == may_not_exist)
322                         return fullname;
323                 if (addext)
324                         fullname.changeExtension("");
325                 ext = token(exts, ',', ++n);
326         }
327         return FileName();
328 }
329
330
331 // Search the file name.ext in the subdirectory dir of
332 //   1) user_lyxdir
333 //   2) build_lyxdir (if not empty)
334 //   3) system_lyxdir
335 FileName const libFileSearch(string const & dir, string const & name,
336                            string const & ext, search_mode mode)
337 {
338         FileName fullname = fileSearch(addPath(package().user_support().absFileName(), dir),
339                                      name, ext, mode);
340         if (!fullname.empty())
341                 return fullname;
342
343         if (!package().build_support().empty())
344                 fullname = fileSearch(addPath(package().build_support().absFileName(), dir),
345                                       name, ext, mode);
346         if (!fullname.empty())
347                 return fullname;
348
349         return fileSearch(addPath(package().system_support().absFileName(), dir),
350                                       name, ext, mode);
351 }
352
353
354 FileName const i18nLibFileSearch(string const & dir, string const & name,
355                   string const & ext)
356 {
357         /* The highest priority value is the `LANGUAGE' environment
358            variable. But we don't use the value if the currently
359            selected locale is the C locale. This is a GNU extension.
360
361            Otherwise, w use a trick to guess what support/gettext.has done:
362            each po file is able to tell us its name. (JMarc)
363         */
364
365         string lang = getGuiMessages().language();
366         string const language = getEnv("LANGUAGE");
367         if (!lang.empty() && !language.empty())
368                 lang = language;
369
370         string l;
371         lang = split(lang, l, ':');
372         while (!l.empty()) {
373                 FileName tmp;
374                 // First try with the full name
375                 tmp = libFileSearch(addPath(dir, l), name, ext);
376                 if (!tmp.empty())
377                         return tmp;
378
379                 // Then the name without country code
380                 string const shortl = token(l, '_', 0);
381                 if (shortl != l) {
382                         tmp = libFileSearch(addPath(dir, shortl), name, ext);
383                         if (!tmp.empty())
384                                 return tmp;
385                 }
386
387 #if 1
388                 // For compatibility, to be removed later (JMarc)
389                 tmp = libFileSearch(dir, token(l, '_', 0) + '_' + name,
390                                     ext);
391                 if (!tmp.empty()) {
392                         lyxerr << "i18nLibFileSearch: File `" << tmp
393                                << "' has been found by the old method" <<endl;
394                         return tmp;
395                 }
396 #endif
397                 lang = split(lang, l, ':');
398         }
399
400         return libFileSearch(dir, name, ext);
401 }
402
403
404 FileName const imageLibFileSearch(string & dir, string const & name,
405                   string const & ext, search_mode mode)
406 {
407         if (!lyx::lyxrc.icon_set.empty()) {
408                 string const imagedir = addPath(dir, lyx::lyxrc.icon_set);
409                 FileName const fn = libFileSearch(imagedir, name, ext, mode);
410                 if (fn.exists()) {
411                         dir = imagedir;
412                         return fn;
413                 }
414         }
415         return libFileSearch(dir, name, ext, mode);
416 }
417
418
419 string const commandPrep(string const & command_in)
420 {
421         static string const token_scriptpath = "$$s/";
422         string const python_call = "python -tt";
423
424         string command = command_in;
425         if (prefixIs(command_in, python_call))
426                 command = os::python() + command_in.substr(python_call.length());
427
428         // Find the starting position of "$$s/"
429         string::size_type const pos1 = command.find(token_scriptpath);
430         if (pos1 == string::npos)
431                 return command;
432         // Find the end of the "$$s/some_subdir/some_script" word within
433         // command. Assumes that the script name does not contain spaces.
434         string::size_type const start_script = pos1 + 4;
435         string::size_type const pos2 = command.find(' ', start_script);
436         string::size_type const size_script = pos2 == string::npos?
437                 (command.size() - start_script) : pos2 - start_script;
438
439         // Does this script file exist?
440         string const script =
441                 libFileSearch(".", command.substr(start_script, size_script)).absFileName();
442
443         if (script.empty()) {
444                 // Replace "$$s/" with ""
445                 command.erase(pos1, 4);
446         } else {
447                 quote_style style = quote_shell;
448                 if (prefixIs(command, os::python()))
449                         style = quote_python;
450
451                 // Replace "$$s/foo/some_script" with "<path to>/some_script".
452                 string::size_type const size_replace = size_script + 4;
453                 command.replace(pos1, size_replace, quoteName(script, style));
454         }
455
456         return command;
457 }
458
459
460 static string createTempFile(QString const & mask)
461 {
462         // FIXME: This is not safe. QTemporaryFile creates a file in open(),
463         //        but the file is deleted when qt_tmp goes out of scope.
464         //        Therefore the next call to createTempFile() may create the
465         //        same file again. To make this safe the QTemporaryFile object
466         //        needs to be kept for the whole life time of the temp file name.
467         //        This could be achieved by creating a class TempDir (like
468         //        TempFile, but using a currentlky non-existing
469         //        QTemporaryDirectory object).
470         QTemporaryFile qt_tmp(mask + ".XXXXXXXXXXXX");
471         if (qt_tmp.open()) {
472                 string const temp_file = fromqstr(qt_tmp.fileName());
473                 LYXERR(Debug::FILES, "Temporary file `" << temp_file << "' created.");
474                 return temp_file;
475         }
476         LYXERR(Debug::FILES, "Unable to create temporary file with following template: "
477                         << qt_tmp.fileTemplate());
478         return string();
479 }
480
481
482 static FileName createTmpDir(FileName const & tempdir, string const & mask)
483 {
484         LYXERR(Debug::FILES, "createTmpDir: tempdir=`" << tempdir << "'\n"
485                 << "createTmpDir:    mask=`" << mask << '\'');
486
487         QFileInfo tmp_fi(QDir(toqstr(tempdir.absFileName())), toqstr(mask));
488         FileName const tmpfl(createTempFile(tmp_fi.absoluteFilePath()));
489
490         if (tmpfl.empty() || !tmpfl.createDirectory(0700)) {
491                 LYXERR0("LyX could not create temporary directory in " << tempdir
492                         << "'");
493                 return FileName();
494         }
495
496         return tmpfl;
497 }
498
499
500 FileName const createLyXTmpDir(FileName const & deflt)
501 {
502         if (deflt.empty() || deflt == package().system_temp_dir())
503                 return createTmpDir(package().system_temp_dir(), "lyx_tmpdir");
504
505         if (deflt.createDirectory(0777))
506                 return deflt;
507
508         if (deflt.isDirWritable()) {
509                 // deflt could not be created because it
510                 // did exist already, so let's create our own
511                 // dir inside deflt.
512                 return createTmpDir(deflt, "lyx_tmpdir");
513         } else {
514                 // some other error occurred.
515                 return createTmpDir(package().system_temp_dir(), "lyx_tmpdir");
516         }
517 }
518
519
520 // Strip filename from path name
521 string const onlyPath(string const & filename)
522 {
523         // If empty filename, return empty
524         if (filename.empty())
525                 return filename;
526
527         // Find last / or start of filename
528         size_t j = filename.rfind('/');
529         return j == string::npos ? "./" : filename.substr(0, j + 1);
530 }
531
532
533 // Convert relative path into absolute path based on a basepath.
534 // If relpath is absolute, just use that.
535 // If basepath is empty, use CWD as base.
536 // Note that basePath can be a relative path, in the sense that it may
537 // not begin with "/" (e.g.), but it should NOT contain such constructs
538 // as "/../".
539 // FIXME It might be nice if the code didn't simply assume that.
540 FileName const makeAbsPath(string const & relPath, string const & basePath)
541 {
542         // checks for already absolute path
543         if (FileName::isAbsolute(relPath))
544                 return FileName(relPath);
545
546         // Copies given paths
547         string tempRel = os::internal_path(relPath);
548         // Since TempRel is NOT absolute, we can safely replace "//" with "/"
549         tempRel = subst(tempRel, "//", "/");
550
551         string tempBase;
552
553         if (FileName::isAbsolute(basePath))
554                 tempBase = basePath;
555         else
556                 tempBase = addPath(FileName::getcwd().absFileName(), basePath);
557
558         // Handle /./ at the end of the path
559         while (suffixIs(tempBase, "/./"))
560                 tempBase.erase(tempBase.length() - 2);
561
562         // processes relative path
563         string rTemp = tempRel;
564         string temp;
565
566         // Check for a leading "~"
567         // Split by first /
568         rTemp = split(rTemp, temp, '/');
569         if (temp == "~") {
570                 tempBase = Package::get_home_dir().absFileName();
571                 tempRel = rTemp;
572         }
573
574         rTemp = tempRel;
575         while (!rTemp.empty()) {
576                 // Split by next /
577                 rTemp = split(rTemp, temp, '/');
578
579                 if (temp == ".") continue;
580                 if (temp == "..") {
581                         // Remove one level of TempBase
582                         if (tempBase.length() <= 1) {
583                                 //this is supposed to be an absolute path, so...
584                                 tempBase = "/";
585                                 continue;
586                         }
587                         //erase a trailing slash if there is one
588                         if (suffixIs(tempBase, "/"))
589                                 tempBase.erase(tempBase.length() - 1, string::npos);
590
591                         string::size_type i = tempBase.length() - 1;
592                         while (i > 0 && tempBase[i] != '/')
593                                 --i;
594                         if (i > 0)
595                                 tempBase.erase(i, string::npos);
596                         else
597                                 tempBase = '/';
598                 } else if (temp.empty() && !rTemp.empty()) {
599                                 tempBase = os::current_root() + rTemp;
600                                 rTemp.erase();
601                 } else {
602                         // Add this piece to TempBase
603                         if (!suffixIs(tempBase, '/'))
604                                 tempBase += '/';
605                         tempBase += temp;
606                 }
607         }
608
609         // returns absolute path
610         return FileName(tempBase);
611 }
612
613
614 // Correctly append filename to the pathname.
615 // If pathname is '.', then don't use pathname.
616 // Chops any path of filename.
617 string const addName(string const & path, string const & fname)
618 {
619         string const basename = onlyFileName(fname);
620         string buf;
621
622         if (path != "." && path != "./" && !path.empty()) {
623                 buf = os::internal_path(path);
624                 if (!suffixIs(path, '/'))
625                         buf += '/';
626         }
627
628         return buf + basename;
629 }
630
631
632 // Strips path from filename
633 string const onlyFileName(string const & fname)
634 {
635         if (fname.empty())
636                 return fname;
637
638         string::size_type j = fname.rfind('/');
639         if (j == string::npos) // no '/' in fname
640                 return fname;
641
642         // Strip to basename
643         return fname.substr(j + 1);
644 }
645
646
647 // Search the string for ${VAR} and $VAR and replace VAR using getenv.
648 string const replaceEnvironmentPath(string const & path)
649 {
650         // ${VAR} is defined as
651         // $\{[A-Za-z_][A-Za-z_0-9]*\}
652         static string const envvar_br = "[$]\\{([A-Za-z_][A-Za-z_0-9]*)\\}";
653
654         // $VAR is defined as:
655         // $[A-Za-z_][A-Za-z_0-9]*
656         static string const envvar = "[$]([A-Za-z_][A-Za-z_0-9]*)";
657
658         static regex const envvar_br_re("(.*)" + envvar_br + "(.*)");
659         static regex const envvar_re("(.*)" + envvar + "(.*)");
660         string result = path;
661         while (1) {
662                 smatch what;
663                 if (!regex_match(result, what, envvar_br_re)) {
664                         if (!regex_match(result, what, envvar_re))
665                                 break;
666                 }
667                 string env_var = getEnv(what.str(2));
668                 result = what.str(1) + env_var + what.str(3);
669         }
670         return result;
671 }
672
673
674 // Return a command prefix for setting the environment of the TeX engine.
675 string latexEnvCmdPrefix(string const & path, string const & lpath)
676 {
677         bool use_lpath = !(lpath.empty() || lpath == "." || lpath == "./");
678
679         if (path.empty() || (lyxrc.texinputs_prefix.empty() && !use_lpath))
680                 return string();
681
682         string texinputs_prefix = lyxrc.texinputs_prefix.empty() ? string()
683                 : os::latex_path_list(
684                         replaceCurdirPath(path, lyxrc.texinputs_prefix));
685         string const allother_prefix = os::latex_path_list(path);
686         string const sep = string(1, os::path_separator(os::TEXENGINE));
687         string const texinputs = getEnv("TEXINPUTS");
688         string const bibinputs = getEnv("BIBINPUTS");
689         string const bstinputs = getEnv("BSTINPUTS");
690         string const texfonts = getEnv("TEXFONTS");
691
692         if (use_lpath) {
693                 string const abslpath = FileName::isAbsolute(lpath)
694                         ? os::latex_path(lpath)
695                         : os::latex_path(FileName(path + "/" + lpath).realPath());
696                 if (texinputs_prefix.empty())
697                         texinputs_prefix = abslpath;
698                 else if (suffixIs(texinputs_prefix, sep))
699                         texinputs_prefix.append(abslpath + sep);
700                 else
701                         texinputs_prefix.append(sep + abslpath);
702         }
703
704         if (os::shell() == os::UNIX)
705                 return "env TEXINPUTS=\"." + sep + texinputs_prefix
706                                            + sep + texinputs + "\" "
707                          + "BIBINPUTS=\"." + sep + allother_prefix
708                                            + sep + bibinputs + "\" "
709                          + "BSTINPUTS=\"." + sep + allother_prefix
710                                            + sep + bstinputs + "\" "
711                          + "TEXFONTS=\"."  + sep + allother_prefix
712                                            + sep + texfonts + "\" ";
713         else
714                 // NOTE: the dummy blank dirs are necessary to force the
715                 //       QProcess parser to quote the argument (see bug 9453)
716                 return "cmd /d /c set \"TEXINPUTS=." + sep + " "
717                                                 + sep + texinputs_prefix
718                                                 + sep + texinputs + "\" & "
719                                + "set \"BIBINPUTS=." + sep + " "
720                                                 + sep + allother_prefix
721                                                 + sep + bibinputs + "\" & "
722                                + "set \"BSTINPUTS=." + sep + " "
723                                                 + sep + allother_prefix
724                                                 + sep + bstinputs + "\" & "
725                                + "set \"TEXFONTS=."  + sep + " "
726                                                 + sep + allother_prefix
727                                                 + sep + texfonts + "\" & ";
728 }
729
730
731 // Replace current directory in all elements of a path list with a given path.
732 string const replaceCurdirPath(string const & path, string const & pathlist)
733 {
734         string const oldpathlist = replaceEnvironmentPath(pathlist);
735         char const sep = os::path_separator();
736         string newpathlist;
737
738         for (size_t i = 0, k = 0; i != string::npos; k = i) {
739                 i = oldpathlist.find(sep, i);
740                 string p = oldpathlist.substr(k, i - k);
741                 if (FileName::isAbsolute(p)) {
742                         newpathlist += p;
743                 } else if (i > k) {
744                         size_t offset = 0;
745                         if (p == ".") {
746                                 offset = 1;
747                         } else if (prefixIs(p, "./")) {
748                                 offset = 2;
749                                 while (p[offset] == '/')
750                                         ++offset;
751                         }
752                         newpathlist += addPath(path, p.substr(offset));
753                         if (suffixIs(p, "//"))
754                                 newpathlist += '/';
755                 }
756                 if (i != string::npos) {
757                         newpathlist += sep;
758                         // Stop here if the last element is empty
759                         if (++i == oldpathlist.length())
760                                 break;
761                 }
762         }
763         return newpathlist;
764 }
765
766
767 // Make relative path out of two absolute paths
768 docstring const makeRelPath(docstring const & abspath, docstring const & basepath)
769 // Makes relative path out of absolute path. If it is deeper than basepath,
770 // it's easy. If basepath and abspath share something (they are all deeper
771 // than some directory), it'll be rendered using ..'s. If they are completely
772 // different, then the absolute path will be used as relative path.
773 {
774         docstring::size_type const abslen = abspath.length();
775         docstring::size_type const baselen = basepath.length();
776
777         docstring::size_type i = os::common_path(abspath, basepath);
778
779         if (i == 0) {
780                 // actually no match - cannot make it relative
781                 return abspath;
782         }
783
784         // Count how many dirs there are in basepath above match
785         // and append as many '..''s into relpath
786         docstring buf;
787         docstring::size_type j = i;
788         while (j < baselen) {
789                 if (basepath[j] == '/') {
790                         if (j + 1 == baselen)
791                                 break;
792                         buf += "../";
793                 }
794                 ++j;
795         }
796
797         // Append relative stuff from common directory to abspath
798         if (abspath[i] == '/')
799                 ++i;
800         for (; i < abslen; ++i)
801                 buf += abspath[i];
802         // Remove trailing /
803         if (suffixIs(buf, '/'))
804                 buf.erase(buf.length() - 1);
805         // Substitute empty with .
806         if (buf.empty())
807                 buf = '.';
808         return buf;
809 }
810
811
812 // Append sub-directory(ies) to a path in an intelligent way
813 string const addPath(string const & path, string const & path_2)
814 {
815         string buf;
816         string const path2 = os::internal_path(path_2);
817
818         if (!path.empty() && path != "." && path != "./") {
819                 buf = os::internal_path(path);
820                 if (path[path.length() - 1] != '/')
821                         buf += '/';
822         }
823
824         if (!path2.empty()) {
825                 string::size_type const p2start = path2.find_first_not_of('/');
826                 string::size_type const p2end = path2.find_last_not_of('/');
827                 string const tmp = path2.substr(p2start, p2end - p2start + 1);
828                 buf += tmp + '/';
829         }
830         return buf;
831 }
832
833
834 string const changeExtension(string const & oldname, string const & extension)
835 {
836         string::size_type const last_slash = oldname.rfind('/');
837         string::size_type last_dot = oldname.rfind('.');
838         if (last_dot < last_slash && last_slash != string::npos)
839                 last_dot = string::npos;
840
841         string ext;
842         // Make sure the extension starts with a dot
843         if (!extension.empty() && extension[0] != '.')
844                 ext= '.' + extension;
845         else
846                 ext = extension;
847
848         return os::internal_path(oldname.substr(0, last_dot) + ext);
849 }
850
851
852 string const removeExtension(string const & name)
853 {
854         return changeExtension(name, string());
855 }
856
857
858 string const addExtension(string const & name, string const & extension)
859 {
860         if (!extension.empty() && extension[0] != '.')
861                 return name + '.' + extension;
862         return name + extension;
863 }
864
865
866 /// Return the extension of the file (not including the .)
867 string const getExtension(string const & name)
868 {
869         string::size_type const last_slash = name.rfind('/');
870         string::size_type const last_dot = name.rfind('.');
871         if (last_dot != string::npos &&
872             (last_slash == string::npos || last_dot > last_slash))
873                 return name.substr(last_dot + 1,
874                                    name.length() - (last_dot + 1));
875         else
876                 return string();
877 }
878
879
880 string const unzippedFileName(string const & zipped_file)
881 {
882         string const ext = getExtension(zipped_file);
883         if (ext == "gz" || ext == "z" || ext == "Z")
884                 return changeExtension(zipped_file, string());
885         else if (ext == "svgz")
886                 return changeExtension(zipped_file, "svg");
887         return onlyPath(zipped_file) + "unzipped_" + onlyFileName(zipped_file);
888 }
889
890
891 FileName const unzipFile(FileName const & zipped_file, string const & unzipped_file)
892 {
893         FileName const tempfile = FileName(unzipped_file.empty() ?
894                 unzippedFileName(zipped_file.toFilesystemEncoding()) :
895                 unzipped_file);
896         // Run gunzip
897         string const command = "gunzip -c " +
898                 zipped_file.toFilesystemEncoding() + " > " +
899                 tempfile.toFilesystemEncoding();
900         Systemcall one;
901         one.startscript(Systemcall::Wait, command);
902         // test that command was executed successfully (anon)
903         // yes, please do. (Lgb)
904         return tempfile;
905 }
906
907
908 docstring const makeDisplayPath(string const & path, unsigned int threshold)
909 {
910         string str = path;
911
912         // If file is from LyXDir, display it as if it were relative.
913         string const system = package().system_support().absFileName();
914         if (prefixIs(str, system) && str != system)
915                 return from_utf8("[" + str.erase(0, system.length()) + "]");
916
917         // replace /home/blah with ~/
918         string const home = Package::get_home_dir().absFileName();
919         if (!home.empty() && prefixIs(str, home))
920                 str = subst(str, home, "~");
921
922         if (str.length() <= threshold)
923                 return from_utf8(os::external_path(str));
924
925         string const prefix = ".../";
926         docstring dstr = from_utf8(str);
927         docstring temp;
928
929         while (dstr.length() > threshold)
930                 dstr = split(dstr, temp, '/');
931
932         // Did we shorten everything away?
933         if (dstr.empty()) {
934                 // Yes, filename itself is too long.
935                 // Pick the start and the end of the filename.
936                 docstring fstr = from_utf8(onlyFileName(path));
937                 dstr = fstr;
938                 if (support::truncateWithEllipsis(dstr, threshold / 2))
939                         dstr += fstr.substr(fstr.length() - threshold / 2 - 2,
940                                                                 docstring::npos);
941         }
942
943         return from_utf8(os::external_path(prefix + to_utf8(dstr)));
944 }
945
946
947 #ifdef HAVE_READLINK
948 bool readLink(FileName const & file, FileName & link)
949 {
950         string const encoded = file.toFilesystemEncoding();
951 #ifdef HAVE_DEF_PATH_MAX
952         char linkbuffer[PATH_MAX + 1];
953         ssize_t const nRead = ::readlink(encoded.c_str(),
954                                      linkbuffer, sizeof(linkbuffer) - 1);
955         if (nRead <= 0)
956                 return false;
957         linkbuffer[nRead] = '\0'; // terminator
958 #else
959         vector<char> buf(1024);
960         int nRead = -1;
961
962         while (true) {
963                 nRead = ::readlink(encoded.c_str(), &buf[0], buf.size() - 1);
964                 if (nRead < 0) {
965                         return false;
966                 }
967                 if (static_cast<size_t>(nRead) < buf.size() - 1) {
968                         break;
969                 }
970                 buf.resize(buf.size() * 2);
971         }
972         buf[nRead] = '\0'; // terminator
973         const char * linkbuffer = &buf[0];
974 #endif
975         link = makeAbsPath(linkbuffer, onlyPath(file.absFileName()));
976         return true;
977 }
978 #else
979 bool readLink(FileName const &, FileName &)
980 {
981         return false;
982 }
983 #endif
984
985
986 cmd_ret const runCommand(string const & cmd)
987 {
988         // FIXME: replace all calls to RunCommand with ForkedCall
989         // (if the output is not needed) or the code in ISpell.cpp
990         // (if the output is needed).
991
992         // One question is if we should use popen or
993         // create our own popen based on fork, exec, pipe
994         // of course the best would be to have a
995         // pstream (process stream), with the
996         // variants ipstream, opstream
997
998 #if defined (_WIN32)
999         STARTUPINFO startup;
1000         PROCESS_INFORMATION process;
1001         SECURITY_ATTRIBUTES security;
1002         HANDLE in, out;
1003         FILE * inf = 0;
1004         bool err2out = false;
1005         string command;
1006         string const infile = trim(split(cmd, command, '<'), " \"");
1007         command = rtrim(command);
1008         if (suffixIs(command, "2>&1")) {
1009                 command = rtrim(command, "2>&1");
1010                 err2out = true;
1011         }
1012         string const cmdarg = "/d /c " + command;
1013         string const comspec = getEnv("COMSPEC");
1014
1015         security.nLength = sizeof(SECURITY_ATTRIBUTES);
1016         security.bInheritHandle = TRUE;
1017         security.lpSecurityDescriptor = NULL;
1018
1019         if (CreatePipe(&in, &out, &security, 0)) {
1020                 memset(&startup, 0, sizeof(STARTUPINFO));
1021                 memset(&process, 0, sizeof(PROCESS_INFORMATION));
1022
1023                 startup.cb = sizeof(STARTUPINFO);
1024                 startup.dwFlags = STARTF_USESTDHANDLES;
1025
1026                 startup.hStdError = err2out ? out : GetStdHandle(STD_ERROR_HANDLE);
1027                 startup.hStdInput = infile.empty()
1028                         ? GetStdHandle(STD_INPUT_HANDLE)
1029                         : CreateFile(infile.c_str(), GENERIC_READ,
1030                                 FILE_SHARE_READ, &security, OPEN_EXISTING,
1031                                 FILE_ATTRIBUTE_NORMAL, NULL);
1032                 startup.hStdOutput = out;
1033
1034                 if (startup.hStdInput != INVALID_HANDLE_VALUE &&
1035                         CreateProcess(comspec.c_str(), (LPTSTR)cmdarg.c_str(),
1036                                 &security, &security, TRUE, CREATE_NO_WINDOW,
1037                                 0, 0, &startup, &process)) {
1038
1039                         CloseHandle(process.hThread);
1040                         int fno = _open_osfhandle((intptr_t)in, _O_RDONLY);
1041                         CloseHandle(out);
1042                         inf = _fdopen(fno, "r");
1043                 }
1044         }
1045 #elif defined (HAVE_POPEN)
1046         FILE * inf = ::popen(cmd.c_str(), os::popen_read_mode());
1047 #elif defined (HAVE__POPEN)
1048         FILE * inf = ::_popen(cmd.c_str(), os::popen_read_mode());
1049 #else
1050 #error No popen() function.
1051 #endif
1052
1053         // (Claus Hentschel) Check if popen was successful ;-)
1054         if (!inf) {
1055                 lyxerr << "RunCommand:: could not start child process" << endl;
1056                 return make_pair(-1, string());
1057         }
1058
1059         string ret;
1060         int c = fgetc(inf);
1061         while (c != EOF) {
1062                 ret += static_cast<char>(c);
1063                 c = fgetc(inf);
1064         }
1065
1066 #if defined (_WIN32)
1067         WaitForSingleObject(process.hProcess, INFINITE);
1068         if (!infile.empty())
1069                 CloseHandle(startup.hStdInput);
1070         CloseHandle(process.hProcess);
1071         int const pret = fclose(inf);
1072 #elif defined (HAVE_PCLOSE)
1073         int const pret = pclose(inf);
1074 #elif defined (HAVE__PCLOSE)
1075         int const pret = _pclose(inf);
1076 #else
1077 #error No pclose() function.
1078 #endif
1079
1080         if (pret == -1)
1081                 perror("RunCommand:: could not terminate child process");
1082
1083         return make_pair(pret, ret);
1084 }
1085
1086
1087 FileName const findtexfile(string const & fil, string const & /*format*/)
1088 {
1089         /* There is no problem to extend this function too use other
1090            methods to look for files. It could be setup to look
1091            in environment paths and also if wanted as a last resort
1092            to a recursive find. One of the easier extensions would
1093            perhaps be to use the LyX file lookup methods. But! I am
1094            going to implement this until I see some demand for it.
1095            Lgb
1096         */
1097
1098         // If the file can be found directly, we just return a
1099         // absolute path version of it.
1100         FileName const absfile(makeAbsPath(fil));
1101         if (absfile.exists())
1102                 return absfile;
1103
1104         // Now we try to find it using kpsewhich.
1105         // It seems from the kpsewhich manual page that it is safe to use
1106         // kpsewhich without --format: "When the --format option is not
1107         // given, the search path used when looking for a file is inferred
1108         // from the name given, by looking for a known extension. If no
1109         // known extension is found, the search path for TeX source files
1110         // is used."
1111         // However, we want to take advantage of the format sine almost all
1112         // the different formats has environment variables that can be used
1113         // to controll which paths to search. f.ex. bib looks in
1114         // BIBINPUTS and TEXBIB. Small list follows:
1115         // bib - BIBINPUTS, TEXBIB
1116         // bst - BSTINPUTS
1117         // graphic/figure - TEXPICTS, TEXINPUTS
1118         // ist - TEXINDEXSTYLE, INDEXSTYLE
1119         // pk - PROGRAMFONTS, PKFONTS, TEXPKS, GLYPHFONTS, TEXFONTS
1120         // tex - TEXINPUTS
1121         // tfm - TFMFONTS, TEXFONTS
1122         // This means that to use kpsewhich in the best possible way we
1123         // should help it by setting additional path in the approp. envir.var.
1124         string const kpsecmd = "kpsewhich " + fil;
1125
1126         cmd_ret const c = runCommand(kpsecmd);
1127
1128         LYXERR(Debug::LATEX, "kpse status = " << c.first << '\n'
1129                  << "kpse result = `" << rtrim(c.second, "\n\r") << '\'');
1130         if (c.first != -1)
1131                 return FileName(rtrim(to_utf8(from_filesystem8bit(c.second)), "\n\r"));
1132         else
1133                 return FileName();
1134 }
1135
1136
1137 int compare_timestamps(FileName const & file1, FileName const & file2)
1138 {
1139         // If the original is newer than the copy, then copy the original
1140         // to the new directory.
1141
1142         int cmp = 0;
1143         if (file1.exists() && file2.exists()) {
1144                 double const tmp = difftime(file1.lastModified(), file2.lastModified());
1145                 if (tmp != 0)
1146                         cmp = tmp > 0 ? 1 : -1;
1147
1148         } else if (file1.exists()) {
1149                 cmp = 1;
1150         } else if (file2.exists()) {
1151                 cmp = -1;
1152         }
1153
1154         return cmp;
1155 }
1156
1157
1158 bool prefs2prefs(FileName const & filename, FileName const & tempfile, bool lfuns)
1159 {
1160         FileName const script = libFileSearch("scripts", "prefs2prefs.py");
1161         if (script.empty()) {
1162                 LYXERR0("Could not find bind file conversion "
1163                                 "script prefs2prefs.py.");
1164                 return false;
1165         }
1166
1167         ostringstream command;
1168         command << os::python() << ' ' << quoteName(script.toFilesystemEncoding())
1169           << ' ' << (lfuns ? "-l" : "-p") << ' '
1170                 << quoteName(filename.toFilesystemEncoding())
1171                 << ' ' << quoteName(tempfile.toFilesystemEncoding());
1172         string const command_str = command.str();
1173
1174         LYXERR(Debug::FILES, "Running `" << command_str << '\'');
1175
1176         cmd_ret const ret = runCommand(command_str);
1177         if (ret.first != 0) {
1178                 LYXERR0("Could not run file conversion script prefs2prefs.py.");
1179                 return false;
1180         }
1181         return true;
1182 }
1183
1184
1185 bool configFileNeedsUpdate(string const & file)
1186 {
1187         // We cannot initialize configure_script directly because the package
1188         // is not initialized yet when static objects are constructed.
1189         static FileName configure_script;
1190         static bool firstrun = true;
1191         if (firstrun) {
1192                 configure_script =
1193                         FileName(addName(package().system_support().absFileName(),
1194                                 "configure.py"));
1195                 firstrun = false;
1196         }
1197
1198         FileName absfile =
1199                 FileName(addName(package().user_support().absFileName(), file));
1200         return !absfile.exists()
1201                 || configure_script.lastModified() > absfile.lastModified();
1202 }
1203
1204
1205 int fileLock(const char * lock_file)
1206 {
1207         int fd = -1;
1208 #if defined(HAVE_LOCKF)
1209         fd = open(lock_file, O_CREAT|O_APPEND|O_SYNC|O_RDWR, 0666);
1210         if (fd == -1)
1211                 return -1;
1212         if (lockf(fd, F_LOCK, 0) != 0) {
1213                 close(fd);
1214                 return -1;
1215         }
1216 #endif
1217         return fd;
1218 }
1219
1220
1221 void fileUnlock(int fd, const char * /* lock_file*/)
1222 {
1223 #if defined(HAVE_LOCKF)
1224         if (fd >= 0) {
1225                 if (lockf(fd, F_ULOCK, 0))
1226                         LYXERR0("Can't unlock the file.");
1227                 close(fd);
1228         }
1229 #endif
1230 }
1231
1232 } //namespace support
1233 } // namespace lyx