]> git.lyx.org Git - lyx.git/blob - src/support/filetools.cpp
Revert qprocess code. Revisions reverted: 22026, 22030, 22044, 22048,
[lyx.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 "support/filetools.h"
25
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"
36 #include "support/qstring_helpers.h"
37
38 #include <QDir>
39
40 #include <boost/assert.hpp>
41 #include <boost/regex.hpp>
42
43 #include <fcntl.h>
44
45 #include <cerrno>
46 #include <cstdlib>
47 #include <cstdio>
48
49 #include <utility>
50 #include <fstream>
51 #include <sstream>
52
53 using namespace std;
54
55 namespace lyx {
56 namespace support {
57
58 bool isLyXFilename(string const & filename)
59 {
60         return suffixIs(ascii_lowercase(filename), ".lyx");
61 }
62
63
64 bool isSGMLFilename(string const & filename)
65 {
66         return suffixIs(ascii_lowercase(filename), ".sgml");
67 }
68
69
70 bool isValidLaTeXFilename(string const & filename)
71 {
72         string const invalid_chars("#$%{}()[]\"^");
73         return filename.find_first_of(invalid_chars) == string::npos;
74 }
75
76
77 string const latex_path(string const & original_path,
78                 latex_path_extension extension,
79                 latex_path_dots dots)
80 {
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() ?
92                                 path :
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;
97                 } else {
98                         path = "\\string\"" + path + "\\string\"";
99                 }
100         }
101
102         return dots == ESCAPE_DOTS ? subst(path, ".", "\\lyxdot ") : path;
103 }
104
105
106 // Substitutes spaces with underscores in filename (and path)
107 FileName const makeLatexName(FileName const & file)
108 {
109         string name = file.onlyFileName();
110         string const path = file.onlyPath().absFilename() + "/";
111
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:;<=>?[]`|";
118
119         string::size_type pos = 0;
120         while ((pos = name.find_first_not_of(keep, pos)) != string::npos)
121                 name[pos++] = '_';
122
123         FileName latex_name(path + name);
124         latex_name.changeExtension(".tex");
125         return latex_name;
126 }
127
128
129 string const quoteName(string const & name, quote_style style)
130 {
131         switch(style) {
132         case quote_shell:
133                 // This does not work for filenames containing " (windows)
134                 // or ' (all other OSes). This can't be changed easily, since
135                 // we would need to adapt the command line parser in
136                 // Forkedcall::generateChild. Therefore we don't pass user
137                 // filenames to child processes if possible. We store them in
138                 // a python script instead, where we don't have these
139                 // limitations.
140                 return (os::shell() == os::UNIX) ?
141                         '\'' + name + '\'':
142                         '"' + name + '"';
143         case quote_python:
144                 return "\"" + subst(subst(name, "\\", "\\\\"), "\"", "\\\"")
145                      + "\"";
146         }
147         // shut up stupid compiler
148         return string();
149 }
150
151
152 #if 0
153 // Uses a string of paths separated by ";"s to find a file to open.
154 // Can't cope with pathnames with a ';' in them. Returns full path to file.
155 // If path entry begins with $$LyX/, use system_lyxdir
156 // If path entry begins with $$User/, use user_lyxdir
157 // Example: "$$User/doc;$$LyX/doc"
158 FileName const fileOpenSearch(string const & path, string const & name,
159                              string const & ext)
160 {
161         FileName real_file;
162         string path_element;
163         bool notfound = true;
164         string tmppath = split(path, path_element, ';');
165
166         while (notfound && !path_element.empty()) {
167                 path_element = os::internal_path(path_element);
168                 if (!suffixIs(path_element, '/'))
169                         path_element += '/';
170                 path_element = subst(path_element, "$$LyX",
171                                      package().system_support().absFilename());
172                 path_element = subst(path_element, "$$User",
173                                      package().user_support().absFilename());
174
175                 real_file = fileSearch(path_element, name, ext);
176
177                 if (real_file.empty()) {
178                         do {
179                                 tmppath = split(tmppath, path_element, ';');
180                         } while (!tmppath.empty() && path_element.empty());
181                 } else {
182                         notfound = false;
183                 }
184         }
185         return real_file;
186 }
187 #endif
188
189
190 // Returns the real name of file name in directory path, with optional
191 // extension ext.
192 FileName const fileSearch(string const & path, string const & name,
193                           string const & ext, search_mode mode)
194 {
195         // if `name' is an absolute path, we ignore the setting of `path'
196         // Expand Environmentvariables in 'name'
197         string const tmpname = replaceEnvironmentPath(name);
198         FileName fullname(makeAbsPath(tmpname, path));
199         // search first without extension, then with it.
200         if (fullname.isReadableFile())
201                 return fullname;
202         if (ext.empty())
203                 // We are done.
204                 return mode == allow_unreadable ? fullname : FileName();
205         // Only add the extension if it is not already the extension of
206         // fullname.
207         if (getExtension(fullname.absFilename()) != ext)
208                 fullname = FileName(addExtension(fullname.absFilename(), ext));
209         if (fullname.isReadableFile() || mode == allow_unreadable)
210                 return fullname;
211         return FileName();
212 }
213
214
215 // Search the file name.ext in the subdirectory dir of
216 //   1) user_lyxdir
217 //   2) build_lyxdir (if not empty)
218 //   3) system_lyxdir
219 FileName const libFileSearch(string const & dir, string const & name,
220                            string const & ext)
221 {
222         FileName fullname = fileSearch(addPath(package().user_support().absFilename(), dir),
223                                      name, ext);
224         if (!fullname.empty())
225                 return fullname;
226
227         if (!package().build_support().empty())
228                 fullname = fileSearch(addPath(package().build_support().absFilename(), dir),
229                                       name, ext);
230         if (!fullname.empty())
231                 return fullname;
232
233         return fileSearch(addPath(package().system_support().absFilename(), dir), name, ext);
234 }
235
236
237 FileName const i18nLibFileSearch(string const & dir, string const & name,
238                   string const & ext)
239 {
240         /* The highest priority value is the `LANGUAGE' environment
241            variable. But we don't use the value if the currently
242            selected locale is the C locale. This is a GNU extension.
243
244            Otherwise, w use a trick to guess what support/gettext.has done:
245            each po file is able to tell us its name. (JMarc)
246         */
247
248         string lang = to_ascii(_("[[Replace with the code of your language]]"));
249         string const language = getEnv("LANGUAGE");
250         if (!lang.empty() && !language.empty())
251                 lang = language;
252
253         string l;
254         lang = split(lang, l, ':');
255         while (!l.empty()) {
256                 FileName tmp;
257                 // First try with the full name
258                 tmp = libFileSearch(addPath(dir, l), name, ext);
259                 if (!tmp.empty())
260                         return tmp;
261
262                 // Then the name without country code
263                 string const shortl = token(l, '_', 0);
264                 if (shortl != l) {
265                         tmp = libFileSearch(addPath(dir, shortl), name, ext);
266                         if (!tmp.empty())
267                                 return tmp;
268                 }
269
270 #if 1
271                 // For compatibility, to be removed later (JMarc)
272                 tmp = libFileSearch(dir, token(l, '_', 0) + '_' + name,
273                                     ext);
274                 if (!tmp.empty()) {
275                         lyxerr << "i18nLibFileSearch: File `" << tmp
276                                << "' has been found by the old method" <<endl;
277                         return tmp;
278                 }
279 #endif
280                 lang = split(lang, l, ':');
281         }
282
283         return libFileSearch(dir, name, ext);
284 }
285
286
287 string const libScriptSearch(string const & command_in, quote_style style)
288 {
289         static string const token_scriptpath = "$$s/";
290
291         string command = command_in;
292         // Find the starting position of "$$s/"
293         string::size_type const pos1 = command.find(token_scriptpath);
294         if (pos1 == string::npos)
295                 return command;
296         // Find the end of the "$$s/some_subdir/some_script" word within
297         // command. Assumes that the script name does not contain spaces.
298         string::size_type const start_script = pos1 + 4;
299         string::size_type const pos2 = command.find(' ', start_script);
300         string::size_type const size_script = pos2 == string::npos?
301                 (command.size() - start_script) : pos2 - start_script;
302
303         // Does this script file exist?
304         string const script =
305                 libFileSearch(".", command.substr(start_script, size_script)).absFilename();
306
307         if (script.empty()) {
308                 // Replace "$$s/" with ""
309                 command.erase(pos1, 4);
310         } else {
311                 // Replace "$$s/foo/some_script" with "<path to>/some_script".
312                 string::size_type const size_replace = size_script + 4;
313                 command.replace(pos1, size_replace, quoteName(script, style));
314         }
315
316         return command;
317 }
318
319
320 static FileName createTmpDir(FileName const & tempdir, string const & mask)
321 {
322         LYXERR(Debug::FILES, "createTmpDir: tempdir=`" << tempdir << "'\n"
323                 << "createTmpDir:    mask=`" << mask << '\'');
324
325         FileName const tmpfl(tempName(tempdir, mask));
326         // lyx::tempName actually creates a file to make sure that it
327         // stays unique. So we have to delete it before we can create
328         // a dir with the same name. Note also that we are not thread
329         // safe because of the gap between unlink and mkdir. (Lgb)
330         tmpfl.removeFile();
331
332         if (tmpfl.empty() || mkdir(tmpfl, 0700)) {
333                 lyxerr << "LyX could not create the temporary directory '"
334                        << tmpfl << "'" << endl;
335                 return FileName();
336         }
337
338         return tmpfl;
339 }
340
341 string const createBufferTmpDir()
342 {
343         static int count;
344         // We are in our own directory.  Why bother to mangle name?
345         // In fact I wrote this code to circumvent a problematic behaviour
346         // (bug?) of EMX mkstemp().
347         string const tmpfl =
348                 package().temp_dir().absFilename() + "/lyx_tmpbuf" +
349                 convert<string>(count++);
350
351         if (mkdir(FileName(tmpfl), 0777)) {
352                 lyxerr << "LyX could not create the temporary directory '"
353                        << tmpfl << "'" << endl;
354                 return string();
355         }
356         return tmpfl;
357 }
358
359
360 FileName const createLyXTmpDir(FileName const & deflt)
361 {
362         if (deflt.empty() || deflt.absFilename() == "/tmp")
363                 return createTmpDir(FileName("/tmp"), "lyx_tmpdir");
364
365         if (!mkdir(deflt, 0777)) 
366                 return deflt;
367
368         if (deflt.isDirWritable()) {
369                 // deflt could not be created because it
370                 // did exist already, so let's create our own
371                 // dir inside deflt.
372                 return createTmpDir(deflt, "lyx_tmpdir");
373         } else {
374                 // some other error occured.
375                 return createTmpDir(FileName("/tmp"), "lyx_tmpdir");
376         }
377 }
378
379
380 // Strip filename from path name
381 string const onlyPath(string const & filename)
382 {
383         // If empty filename, return empty
384         if (filename.empty())
385                 return filename;
386
387         // Find last / or start of filename
388         size_t j = filename.rfind('/');
389         return j == string::npos ? "./" : filename.substr(0, j + 1);
390 }
391
392
393 // Convert relative path into absolute path based on a basepath.
394 // If relpath is absolute, just use that.
395 // If basepath is empty, use CWD as base.
396 FileName const makeAbsPath(string const & relPath, string const & basePath)
397 {
398         // checks for already absolute path
399         if (os::is_absolute_path(relPath))
400                 return FileName(relPath);
401
402         // Copies given paths
403         string tempRel = os::internal_path(relPath);
404         // Since TempRel is NOT absolute, we can safely replace "//" with "/"
405         tempRel = subst(tempRel, "//", "/");
406
407         string tempBase;
408
409         if (os::is_absolute_path(basePath))
410                 tempBase = basePath;
411         else
412                 tempBase = addPath(getcwd().absFilename(), basePath);
413
414         // Handle /./ at the end of the path
415         while (suffixIs(tempBase, "/./"))
416                 tempBase.erase(tempBase.length() - 2);
417
418         // processes relative path
419         string rTemp = tempRel;
420         string temp;
421
422         while (!rTemp.empty()) {
423                 // Split by next /
424                 rTemp = split(rTemp, temp, '/');
425
426                 if (temp == ".") continue;
427                 if (temp == "..") {
428                         // Remove one level of TempBase
429                         string::difference_type i = tempBase.length() - 2;
430                         if (i < 0)
431                                 i = 0;
432                         while (i > 0 && tempBase[i] != '/')
433                                 --i;
434                         if (i > 0)
435                                 tempBase.erase(i, string::npos);
436                         else
437                                 tempBase += '/';
438                 } else if (temp.empty() && !rTemp.empty()) {
439                                 tempBase = os::current_root() + rTemp;
440                                 rTemp.erase();
441                 } else {
442                         // Add this piece to TempBase
443                         if (!suffixIs(tempBase, '/'))
444                                 tempBase += '/';
445                         tempBase += temp;
446                 }
447         }
448
449         // returns absolute path
450         return FileName(tempBase);
451 }
452
453
454 // Correctly append filename to the pathname.
455 // If pathname is '.', then don't use pathname.
456 // Chops any path of filename.
457 string const addName(string const & path, string const & fname)
458 {
459         string const basename = onlyFilename(fname);
460         string buf;
461
462         if (path != "." && path != "./" && !path.empty()) {
463                 buf = os::internal_path(path);
464                 if (!suffixIs(path, '/'))
465                         buf += '/';
466         }
467
468         return buf + basename;
469 }
470
471
472 // Strips path from filename
473 string const onlyFilename(string const & fname)
474 {
475         if (fname.empty())
476                 return fname;
477
478         string::size_type j = fname.rfind('/');
479         if (j == string::npos) // no '/' in fname
480                 return fname;
481
482         // Strip to basename
483         return fname.substr(j + 1);
484 }
485
486
487 /// Returns true is path is absolute
488 bool absolutePath(string const & path)
489 {
490         return os::is_absolute_path(path);
491 }
492
493
494 // Create absolute path. If impossible, don't do anything
495 // Supports ./ and ~/. Later we can add support for ~logname/. (Asger)
496 string const expandPath(string const & path)
497 {
498         // checks for already absolute path
499         string rTemp = replaceEnvironmentPath(path);
500         if (os::is_absolute_path(rTemp))
501                 return rTemp;
502
503         string temp;
504         string const copy = rTemp;
505
506         // Split by next /
507         rTemp = split(rTemp, temp, '/');
508
509         if (temp == ".")
510                 return getcwd().absFilename() + '/' + rTemp;
511
512         if (temp == "~")
513                 return package().home_dir().absFilename() + '/' + rTemp;
514
515         if (temp == "..")
516                 return makeAbsPath(copy).absFilename();
517
518         // Don't know how to handle this
519         return copy;
520 }
521
522
523 // Search the string for ${VAR} and $VAR and replace VAR using getenv.
524 string const replaceEnvironmentPath(string const & path)
525 {
526         // ${VAR} is defined as
527         // $\{[A-Za-z_][A-Za-z_0-9]*\}
528         static string const envvar_br = "[$]\\{([A-Za-z_][A-Za-z_0-9]*)\\}";
529
530         // $VAR is defined as:
531         // $\{[A-Za-z_][A-Za-z_0-9]*\}
532         static string const envvar = "[$]([A-Za-z_][A-Za-z_0-9]*)";
533
534         static boost::regex envvar_br_re("(.*)" + envvar_br + "(.*)");
535         static boost::regex envvar_re("(.*)" + envvar + "(.*)");
536         boost::smatch what;
537
538         string result = path;
539         while (1) {
540                 regex_match(result, what, envvar_br_re);
541                 if (!what[0].matched) {
542                         regex_match(result, what, envvar_re);
543                         if (!what[0].matched)
544                                 break;
545                 }
546                 result = what.str(1) + getEnv(what.str(2)) + what.str(3);
547         }
548         return result;
549 }
550
551
552 // Make relative path out of two absolute paths
553 docstring const makeRelPath(docstring const & abspath, docstring const & basepath)
554 // Makes relative path out of absolute path. If it is deeper than basepath,
555 // it's easy. If basepath and abspath share something (they are all deeper
556 // than some directory), it'll be rendered using ..'s. If they are completely
557 // different, then the absolute path will be used as relative path.
558 {
559         docstring::size_type const abslen = abspath.length();
560         docstring::size_type const baselen = basepath.length();
561
562         docstring::size_type i = os::common_path(abspath, basepath);
563
564         if (i == 0) {
565                 // actually no match - cannot make it relative
566                 return abspath;
567         }
568
569         // Count how many dirs there are in basepath above match
570         // and append as many '..''s into relpath
571         docstring buf;
572         docstring::size_type j = i;
573         while (j < baselen) {
574                 if (basepath[j] == '/') {
575                         if (j + 1 == baselen)
576                                 break;
577                         buf += "../";
578                 }
579                 ++j;
580         }
581
582         // Append relative stuff from common directory to abspath
583         if (abspath[i] == '/')
584                 ++i;
585         for (; i < abslen; ++i)
586                 buf += abspath[i];
587         // Remove trailing /
588         if (suffixIs(buf, '/'))
589                 buf.erase(buf.length() - 1);
590         // Substitute empty with .
591         if (buf.empty())
592                 buf = '.';
593         return buf;
594 }
595
596
597 // Append sub-directory(ies) to a path in an intelligent way
598 string const addPath(string const & path, string const & path_2)
599 {
600         string buf;
601         string const path2 = os::internal_path(path_2);
602
603         if (!path.empty() && path != "." && path != "./") {
604                 buf = os::internal_path(path);
605                 if (path[path.length() - 1] != '/')
606                         buf += '/';
607         }
608
609         if (!path2.empty()) {
610                 string::size_type const p2start = path2.find_first_not_of('/');
611                 string::size_type const p2end = path2.find_last_not_of('/');
612                 string const tmp = path2.substr(p2start, p2end - p2start + 1);
613                 buf += tmp + '/';
614         }
615         return buf;
616 }
617
618
619 string const changeExtension(string const & oldname, string const & extension)
620 {
621         string::size_type const last_slash = oldname.rfind('/');
622         string::size_type last_dot = oldname.rfind('.');
623         if (last_dot < last_slash && last_slash != string::npos)
624                 last_dot = string::npos;
625
626         string ext;
627         // Make sure the extension starts with a dot
628         if (!extension.empty() && extension[0] != '.')
629                 ext= '.' + extension;
630         else
631                 ext = extension;
632
633         return os::internal_path(oldname.substr(0, last_dot) + ext);
634 }
635
636
637 string const removeExtension(string const & name)
638 {
639         return changeExtension(name, string());
640 }
641
642
643 string const addExtension(string const & name, string const & extension)
644 {
645         if (!extension.empty() && extension[0] != '.')
646                 return name + '.' + extension;
647         return name + extension;
648 }
649
650
651 /// Return the extension of the file (not including the .)
652 string const getExtension(string const & name)
653 {
654         string::size_type const last_slash = name.rfind('/');
655         string::size_type const last_dot = name.rfind('.');
656         if (last_dot != string::npos &&
657             (last_slash == string::npos || last_dot > last_slash))
658                 return name.substr(last_dot + 1,
659                                    name.length() - (last_dot + 1));
660         else
661                 return string();
662 }
663
664
665 string const unzippedFileName(string const & zipped_file)
666 {
667         string const ext = getExtension(zipped_file);
668         if (ext == "gz" || ext == "z" || ext == "Z")
669                 return changeExtension(zipped_file, string());
670         return "unzipped_" + zipped_file;
671 }
672
673
674 FileName const unzipFile(FileName const & zipped_file, string const & unzipped_file)
675 {
676         FileName const tempfile = FileName(unzipped_file.empty() ?
677                 unzippedFileName(zipped_file.toFilesystemEncoding()) :
678                 unzipped_file);
679         // Run gunzip
680         string const command = "gunzip -c " +
681                 zipped_file.toFilesystemEncoding() + " > " +
682                 tempfile.toFilesystemEncoding();
683         Systemcall one;
684         one.startscript(Systemcall::Wait, command);
685         // test that command was executed successfully (anon)
686         // yes, please do. (Lgb)
687         return tempfile;
688 }
689
690
691 docstring const makeDisplayPath(string const & path, unsigned int threshold)
692 {
693         string str = path;
694
695         // If file is from LyXDir, display it as if it were relative.
696         string const system = package().system_support().absFilename();
697         if (prefixIs(str, system) && str != system)
698                 return from_utf8("[" + str.erase(0, system.length()) + "]");
699
700         // replace /home/blah with ~/
701         string const home = package().home_dir().absFilename();
702         if (!home.empty() && prefixIs(str, home))
703                 str = subst(str, home, "~");
704
705         if (str.length() <= threshold)
706                 return from_utf8(os::external_path(str));
707
708         string const prefix = ".../";
709         string temp;
710
711         while (str.length() > threshold)
712                 str = split(str, temp, '/');
713
714         // Did we shorten everything away?
715         if (str.empty()) {
716                 // Yes, filename itself is too long.
717                 // Pick the start and the end of the filename.
718                 str = onlyFilename(path);
719                 string const head = str.substr(0, threshold / 2 - 3);
720
721                 string::size_type len = str.length();
722                 string const tail =
723                         str.substr(len - threshold / 2 - 2, len - 1);
724                 str = head + "..." + tail;
725         }
726
727         return from_utf8(os::external_path(prefix + str));
728 }
729
730
731 bool readLink(FileName const & file, FileName & link)
732 {
733 #ifdef HAVE_READLINK
734         char linkbuffer[512];
735         // Should be PATH_MAX but that needs autconf support
736         string const encoded = file.toFilesystemEncoding();
737         int const nRead = ::readlink(encoded.c_str(),
738                                      linkbuffer, sizeof(linkbuffer) - 1);
739         if (nRead <= 0)
740                 return false;
741         linkbuffer[nRead] = '\0'; // terminator
742         link = makeAbsPath(linkbuffer, onlyPath(file.absFilename()));
743         return true;
744 #else
745         return false;
746 #endif
747 }
748
749
750 cmd_ret const runCommand(string const & cmd)
751 {
752         // FIXME: replace all calls to RunCommand with ForkedCall
753         // (if the output is not needed) or the code in ISpell.cpp
754         // (if the output is needed).
755
756         // One question is if we should use popen or
757         // create our own popen based on fork, exec, pipe
758         // of course the best would be to have a
759         // pstream (process stream), with the
760         // variants ipstream, opstream
761
762 #if defined (HAVE_POPEN)
763         FILE * inf = ::popen(cmd.c_str(), os::popen_read_mode());
764 #elif defined (HAVE__POPEN)
765         FILE * inf = ::_popen(cmd.c_str(), os::popen_read_mode());
766 #else
767 #error No popen() function.
768 #endif
769
770         // (Claus Hentschel) Check if popen was succesful ;-)
771         if (!inf) {
772                 lyxerr << "RunCommand:: could not start child process" << endl;
773                 return make_pair(-1, string());
774         }
775
776         string ret;
777         int c = fgetc(inf);
778         while (c != EOF) {
779                 ret += static_cast<char>(c);
780                 c = fgetc(inf);
781         }
782
783 #if defined (HAVE_PCLOSE)
784         int const pret = pclose(inf);
785 #elif defined (HAVE__PCLOSE)
786         int const pret = _pclose(inf);
787 #else
788 #error No pclose() function.
789 #endif
790
791         if (pret == -1)
792                 perror("RunCommand:: could not terminate child process");
793
794         return make_pair(pret, ret);
795 }
796
797
798 FileName const findtexfile(string const & fil, string const & /*format*/)
799 {
800         /* There is no problem to extend this function too use other
801            methods to look for files. It could be setup to look
802            in environment paths and also if wanted as a last resort
803            to a recursive find. One of the easier extensions would
804            perhaps be to use the LyX file lookup methods. But! I am
805            going to implement this until I see some demand for it.
806            Lgb
807         */
808
809         // If the file can be found directly, we just return a
810         // absolute path version of it.
811         FileName const absfile(makeAbsPath(fil));
812         if (absfile.exists())
813                 return absfile;
814
815         // No we try to find it using kpsewhich.
816         // It seems from the kpsewhich manual page that it is safe to use
817         // kpsewhich without --format: "When the --format option is not
818         // given, the search path used when looking for a file is inferred
819         // from the name given, by looking for a known extension. If no
820         // known extension is found, the search path for TeX source files
821         // is used."
822         // However, we want to take advantage of the format sine almost all
823         // the different formats has environment variables that can be used
824         // to controll which paths to search. f.ex. bib looks in
825         // BIBINPUTS and TEXBIB. Small list follows:
826         // bib - BIBINPUTS, TEXBIB
827         // bst - BSTINPUTS
828         // graphic/figure - TEXPICTS, TEXINPUTS
829         // ist - TEXINDEXSTYLE, INDEXSTYLE
830         // pk - PROGRAMFONTS, PKFONTS, TEXPKS, GLYPHFONTS, TEXFONTS
831         // tex - TEXINPUTS
832         // tfm - TFMFONTS, TEXFONTS
833         // This means that to use kpsewhich in the best possible way we
834         // should help it by setting additional path in the approp. envir.var.
835         string const kpsecmd = "kpsewhich " + fil;
836
837         cmd_ret const c = runCommand(kpsecmd);
838
839         LYXERR(Debug::LATEX, "kpse status = " << c.first << '\n'
840                  << "kpse result = `" << rtrim(c.second, "\n\r") << '\'');
841         if (c.first != -1)
842                 return FileName(rtrim(to_utf8(from_filesystem8bit(c.second)), "\n\r"));
843         else
844                 return FileName();
845 }
846
847
848 void removeAutosaveFile(string const & filename)
849 {
850         string a = onlyPath(filename);
851         a += '#';
852         a += onlyFilename(filename);
853         a += '#';
854         FileName const autosave(a);
855         if (autosave.exists())
856                 autosave.removeFile();
857 }
858
859
860 void readBB_lyxerrMessage(FileName const & file, bool & zipped,
861         string const & message)
862 {
863         LYXERR(Debug::GRAPHICS, "[readBB_from_PSFile] " << message);
864         // FIXME: Why is this func deleting a file? (Lgb)
865         if (zipped)
866                 file.removeFile();
867 }
868
869
870 string const readBB_from_PSFile(FileName const & file)
871 {
872         // in a (e)ps-file it's an entry like %%BoundingBox:23 45 321 345
873         // It seems that every command in the header has an own line,
874         // getline() should work for all files.
875         // On the other hand some plot programs write the bb at the
876         // end of the file. Than we have in the header:
877         // %%BoundingBox: (atend)
878         // In this case we must check the end.
879         bool zipped = file.isZippedFile();
880         FileName const file_ = zipped ? unzipFile(file) : file;
881         string const format = file_.guessFormatFromContents();
882
883         if (format != "eps" && format != "ps") {
884                 readBB_lyxerrMessage(file_, zipped,"no(e)ps-format");
885                 return string();
886         }
887
888         static boost::regex bbox_re(
889                 "^%%BoundingBox:\\s*([[:digit:]]+)\\s+([[:digit:]]+)\\s+([[:digit:]]+)\\s+([[:digit:]]+)");
890         std::ifstream is(file_.toFilesystemEncoding().c_str());
891         while (is) {
892                 string s;
893                 getline(is,s);
894                 boost::smatch what;
895                 if (regex_match(s, what, bbox_re)) {
896                         // Our callers expect the tokens in the string
897                         // separated by single spaces.
898                         // FIXME: change return type from string to something
899                         // sensible
900                         ostringstream os;
901                         os << what.str(1) << ' ' << what.str(2) << ' '
902                            << what.str(3) << ' ' << what.str(4);
903                         string const bb = os.str();
904                         readBB_lyxerrMessage(file_, zipped, bb);
905                         return bb;
906                 }
907         }
908         readBB_lyxerrMessage(file_, zipped, "no bb found");
909         return string();
910 }
911
912
913 int compare_timestamps(FileName const & file1, FileName const & file2)
914 {
915         // If the original is newer than the copy, then copy the original
916         // to the new directory.
917
918         int cmp = 0;
919         if (file1.exists() && file2.exists()) {
920                 double const tmp = difftime(file1.lastModified(), file2.lastModified());
921                 if (tmp != 0)
922                         cmp = tmp > 0 ? 1 : -1;
923
924         } else if (file1.exists()) {
925                 cmp = 1;
926         } else if (file2.exists()) {
927                 cmp = -1;
928         }
929
930         return cmp;
931 }
932
933
934 } //namespace support
935 } // namespace lyx