3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
15 #include "BufferParams.h"
17 #include "ServerSocket.h"
19 #include "frontends/alert.h" //to be removed?
21 #include "support/debug.h"
22 #include "support/filetools.h"
23 #include "support/gettext.h"
24 #include "support/lstrings.h"
25 #include "support/mutex.h"
26 #include "support/docstream.h"
27 #include "support/os.h"
28 #include "support/PathChanger.h"
29 #include "support/Systemcall.h"
30 #include "support/textutils.h"
31 #include "support/Translator.h"
37 // FIXME: Q_OS_MAC is not available, it's in Qt
38 #ifdef USE_MACOSX_PACKAGING
39 #include "support/linkback/LinkBackProxy.h"
47 using namespace lyx::support;
51 namespace Alert = frontend::Alert;
52 namespace os = support::os;
56 string const token_from_format("$$i");
57 string const token_path_format("$$p");
58 string const token_socket_format("$$a");
61 class FormatNamesEqual : public unary_function<Format, bool> {
63 FormatNamesEqual(string const & name)
66 bool operator()(Format const & f) const
68 return f.name() == name_;
75 class FormatExtensionsEqual : public unary_function<Format, bool> {
77 FormatExtensionsEqual(string const & extension)
78 : extension_(extension)
80 bool operator()(Format const & f) const
82 return f.hasExtension(extension_);
89 class FormatMimeEqual : public unary_function<Format, bool> {
91 FormatMimeEqual(string const & mime)
94 bool operator()(Format const & f) const
96 // The test for empty mime strings is needed since we allow
97 // formats with empty mime types.
98 return f.mime() == mime_ && !mime_.empty();
107 bool Format::formatSorter(Format const * lhs, Format const * rhs)
109 return compare_locale(translateIfPossible(lhs->prettyname()),
110 translateIfPossible(rhs->prettyname())) < 0;
113 bool operator<(Format const & a, Format const & b)
115 return compare_locale(translateIfPossible(a.prettyname()),
116 translateIfPossible(b.prettyname())) < 0;
120 Format::Format(string const & n, string const & e, docstring const & p,
121 string const & s, string const & v, string const & ed,
122 string const & m, int flags)
123 : name_(n), prettyname_(p), shortcut_(s), viewer_(v),
124 editor_(ed), mime_(m), flags_(flags)
126 extension_list_ = getVectorFromString(e, ",");
127 LYXERR(Debug::GRAPHICS, "New Format: n=" << n << ", flags=" << flags);
131 bool Format::dummy() const
133 return extension().empty();
137 string const Format::extensions() const
139 return getStringFromVector(extension_list_, ", ");
143 bool Format::hasExtension(string const & e) const
145 return (find(extension_list_.begin(), extension_list_.end(), e)
146 != extension_list_.end());
150 bool Format::isChildFormat() const
154 return isDigitASCII(name_[name_.length() - 1]);
158 string const Format::parentFormat() const
160 return name_.substr(0, name_.length() - 1);
164 void Format::setExtensions(string const & e)
166 extension_list_ = getVectorFromString(e, ",");
170 // This method should return a reference, and throw an exception
171 // if the format named name cannot be found (Lgb)
172 Format const * Formats::getFormat(string const & name) const
174 FormatList::const_iterator cit =
175 find_if(formatlist_.begin(), formatlist_.end(),
176 FormatNamesEqual(name));
177 if (cit != formatlist_.end())
186 /** Guess the file format name (as in Format::name()) from contents.
187 * Normally you don't want to use this directly, but rather
188 * Formats::getFormatFromFile().
190 string guessFormatFromContents(FileName const & fn)
192 // the different filetypes and what they contain in one of the first lines
193 // (dots are any characters). (Herbert 20020131)
196 // EPS %!PS-Adobe-3.0 EPSF...
200 // JPG \377\330... (0xFFD8)
203 // PBM P1... or P4 (B/W)
204 // PGM P2... or P5 (Grayscale)
205 // PPM P3... or P6 (color)
206 // PS %!PS-Adobe-2.0 or 1.0, no "EPSF"!
207 // SGI \001\332... (decimal 474)
209 // TIFF II... or MM...
211 // XPM /* XPM */ sometimes missing (f.ex. tgif-export)
212 // ...static char *...
213 // XWD \000\000\000\151 (0x00006900) decimal 105
215 // GZIP \037\213 http://www.ietf.org/rfc/rfc1952.txt
216 // ZIP PK... http://www.halyava.ru/document/ind_arch.htm
217 // Z \037\235 UNIX compress
220 if (fn.empty() || !fn.isReadableFile())
223 ifstream ifs(fn.toFilesystemEncoding().c_str());
225 // Couldn't open file...
229 static string const gzipStamp = "\037\213";
232 static string const zipStamp = "PK";
234 // ZIP containers (koffice, openoffice.org etc).
235 static string const nonzipStamp = "\010\0\0\0mimetypeapplication/";
238 static string const compressStamp = "\037\235";
240 // DOS binary EPS according to Adobe TN-5002
241 static string const binEPSStamp = "\xC5\xD0\xD3\xC6";
244 // Maximum strings to read
245 int const max_count = 50;
250 bool firstLine = true;
251 bool backslash = false;
252 bool maybelatex = false;
254 while ((count++ < max_count) && format.empty() && !maybelatex) {
259 string const stamp = str.substr(0, 2);
260 if (firstLine && str.size() >= 2) {
261 // at first we check for a zipped file, because this
262 // information is saved in the first bytes of the file!
263 // also some graphic formats which save the information
264 // in the first line, too.
265 if (prefixIs(str, gzipStamp)) {
268 } else if (stamp == zipStamp &&
269 !contains(str, nonzipStamp)) {
272 } else if (stamp == compressStamp) {
276 } else if (stamp == "BM") {
279 } else if (stamp == "\377\330") {
282 } else if (stamp == "\001\332") {
284 } else if (prefixIs(str, binEPSStamp)) {
288 // Don't need to use str.at(0), str.at(1) because
289 // we already know that str.size() >= 2
290 } else if (str[0] == 'P') {
306 } else if ((stamp == "II") || (stamp == "MM")) {
309 } else if (prefixIs(str,"%TGIF")) {
312 } else if (prefixIs(str,"#FIG")) {
315 } else if (prefixIs(str,"GIF")) {
318 } else if (str.size() > 3) {
319 int const c = ((str[0] << 24) & (str[1] << 16) &
320 (str[2] << 8) & str[3]);
331 else if (contains(str,"EPSF"))
332 // dummy, if we have wrong file description like
333 // %!PS-Adobe-2.0EPSF"
336 else if (contains(str, "Grace"))
339 else if (contains(str, "%PDF"))
340 // autodetect pdf format for graphics inclusion
343 else if (contains(str, "PNG"))
346 else if (contains(str, "%!PS-Adobe")) {
349 if (contains(str,"EPSF"))
355 else if (contains(str, "_bits[]"))
358 else if (contains(str, "XPM") || contains(str, "static char *"))
361 else if (contains(str, "BITPIX"))
364 else if (contains(str, "\\documentclass") ||
365 contains(str, "\\chapter") ||
366 contains(str, "\\section") ||
367 contains(str, "\\begin") ||
368 contains(str, "\\end") ||
369 contains(str, "$$") ||
370 contains(str, "\\[") ||
371 contains(str, "\\]"))
374 if (contains(str, '\\'))
376 dollars += count_char(str, '$');
377 if (backslash && dollars > 1)
383 if (format.empty() && maybelatex && !isBinaryFile(fn))
386 if (format.empty()) {
388 LYXERR(Debug::GRAPHICS, "filetools(getFormatFromContents)\n"
389 "\tFile type not recognised before EOF!");
391 LYXERR(Debug::GRAPHICS, "Recognised Fileformat: " << format);
395 LYXERR(Debug::GRAPHICS, "filetools(getFormatFromContents)\n"
396 << "\tCouldn't find a known format!");
403 string Formats::getFormatFromFile(FileName const & filename) const
405 if (filename.empty())
411 if (filename.exists()) {
412 magic_t magic_cookie = magic_open(MAGIC_MIME);
414 if (magic_load(magic_cookie, NULL) != 0) {
415 LYXERR(Debug::GRAPHICS, "Formats::getFormatFromFile\n"
416 << "\tCouldn't load magic database - "
417 << magic_error(magic_cookie));
419 char const * result = magic_file(magic_cookie,
420 filename.toFilesystemEncoding().c_str());
423 mime = token(result, ';', 0);
425 LYXERR(Debug::GRAPHICS, "Formats::getFormatFromFile\n"
426 << "\tCouldn't query magic database - "
427 << magic_error(magic_cookie));
429 // our own detection is better for binary files (can be anything)
430 // and different plain text formats
431 if (!mime.empty() && mime != "application/octet-stream" &&
432 mime != "text/plain") {
433 Formats::const_iterator cit =
434 find_if(formatlist_.begin(), formatlist_.end(),
435 FormatMimeEqual(mime));
436 if (cit != formatlist_.end()) {
437 LYXERR(Debug::GRAPHICS, "\tgot format from MIME type: "
438 << mime << " -> " << cit->name());
439 // See special eps/ps handling below
440 if (mime == "application/postscript")
441 psformat = cit->name();
443 format = cit->name();
447 magic_close(magic_cookie);
448 // libmagic recognizes as latex also some formats of ours
449 // such as pstex and pdftex. Therefore we have to perform
450 // additional checks in this case (bug 9244).
451 if (!format.empty() && format != "latex")
457 string const ext = getExtension(filename.absFileName());
458 if (format.empty()) {
459 // libmagic does not distinguish eps and ps.
460 // Therefore we need to use our own detection here, but only if it
461 // recognizes either ps or eps. Otherwise the libmagic guess will
462 // be better (bug 9146).
463 format = guessFormatFromContents(filename);
464 if (!psformat.empty()) {
465 if (isPostScriptFileFormat(format))
471 if (isZippedFileFormat(format) && !ext.empty()) {
472 string const & fmt_name = getFormatFromExtension(ext);
473 if (!fmt_name.empty()) {
474 Format const * p_format = getFormat(fmt_name);
475 if (p_format && p_format->zippedNative())
476 return p_format->name();
479 // Don't simply return latex (bug 9244).
480 if (!format.empty() && format != "latex")
484 // Both libmagic and our guessing from contents may return as latex
485 // also lyx files and our pstex and pdftex formats. In this case we
486 // give precedence to the format determined by the extension.
487 if (format == "latex") {
488 format = getFormatFromExtension(ext);
489 return format.empty() ? "latex" : format;
492 // try to find a format from the file extension.
493 return getFormatFromExtension(ext);
497 string Formats::getFormatFromExtension(string const & ext) const
500 // this is ambigous if two formats have the same extension,
501 // but better than nothing
502 Formats::const_iterator cit =
503 find_if(formatlist_.begin(), formatlist_.end(),
504 FormatExtensionsEqual(ext));
505 if (cit != formatlist_.end()) {
506 LYXERR(Debug::GRAPHICS, "\twill guess format from file extension: "
507 << ext << " -> " << cit->name());
515 /// Used to store last timestamp of file and whether it is (was) zipped
518 std::time_t timestamp;
519 ZippedInfo(bool zipped, std::time_t timestamp)
520 : zipped(zipped), timestamp(timestamp) { }
524 /// Mapping absolute pathnames of files to their ZippedInfo metadata.
525 static std::map<std::string, ZippedInfo> zipped_;
526 static Mutex zipped_mutex;
529 bool Formats::isZippedFile(support::FileName const & filename) const {
530 string const & fname = filename.absFileName();
531 time_t timestamp = filename.lastModified();
532 Mutex::Locker lock(&zipped_mutex);
533 map<string, ZippedInfo>::iterator it = zipped_.find(fname);
534 if (it != zipped_.end() && it->second.timestamp == timestamp)
535 return it->second.zipped;
536 // FIXME perf: This very expensive function is called on startup on each
537 // file whic is going to be parsed, and also on svgz icons. Maybe there is a
538 // quicker way to check whether a file is zipped? I.e. for icons we
539 // probably just need to check the extension (svgz vs svg).
540 string const & format = getFormatFromFile(filename);
541 bool zipped = (format == "gzip" || format == "zip");
542 zipped_.insert(make_pair(fname, ZippedInfo(zipped, timestamp)));
547 bool Formats::isZippedFileFormat(string const & format)
549 return contains("gzip zip compress", format) && !format.empty();
553 bool Formats::isPostScriptFileFormat(string const & format)
555 return format == "ps" || format == "eps";
558 static string fixCommand(string const & cmd, string const & ext,
559 os::auto_open_mode mode)
561 // configure.py says we do not want a viewer/editor
565 // Does the OS manage this format?
566 if (os::canAutoOpenFile(ext, mode))
569 // if configure.py found nothing, clear the command
570 if (token(cmd, ' ', 0) == "auto")
573 // use the command found by configure.py
578 void Formats::setAutoOpen()
580 FormatList::iterator fit = formatlist_.begin();
581 FormatList::iterator const fend = formatlist_.end();
582 for ( ; fit != fend ; ++fit) {
583 fit->setViewer(fixCommand(fit->viewer(), fit->extension(), os::VIEW));
584 fit->setEditor(fixCommand(fit->editor(), fit->extension(), os::EDIT));
589 int Formats::getNumber(string const & name) const
591 FormatList::const_iterator cit =
592 find_if(formatlist_.begin(), formatlist_.end(),
593 FormatNamesEqual(name));
594 if (cit != formatlist_.end())
595 return distance(formatlist_.begin(), cit);
601 void Formats::add(string const & name)
603 if (!getFormat(name))
604 add(name, name, from_utf8(name), string(), string(), string(),
605 string(), Format::document);
609 void Formats::add(string const & name, string const & extensions,
610 docstring const & prettyname, string const & shortcut,
611 string const & viewer, string const & editor,
612 string const & mime, int flags)
614 FormatList::iterator it =
615 find_if(formatlist_.begin(), formatlist_.end(),
616 FormatNamesEqual(name));
617 if (it == formatlist_.end())
618 formatlist_.push_back(Format(name, extensions, prettyname,
619 shortcut, viewer, editor, mime, flags));
621 *it = Format(name, extensions, prettyname, shortcut, viewer,
622 editor, mime, flags);
626 void Formats::erase(string const & name)
628 FormatList::iterator it =
629 find_if(formatlist_.begin(), formatlist_.end(),
630 FormatNamesEqual(name));
631 if (it != formatlist_.end())
632 formatlist_.erase(it);
638 std::sort(formatlist_.begin(), formatlist_.end());
642 void Formats::setViewer(string const & name, string const & command)
645 FormatList::iterator it =
646 find_if(formatlist_.begin(), formatlist_.end(),
647 FormatNamesEqual(name));
648 if (it != formatlist_.end())
649 it->setViewer(command);
653 void Formats::setEditor(string const & name, string const & command)
656 FormatList::iterator it =
657 find_if(formatlist_.begin(), formatlist_.end(),
658 FormatNamesEqual(name));
659 if (it != formatlist_.end())
660 it->setEditor(command);
664 bool Formats::view(Buffer const & buffer, FileName const & filename,
665 string const & format_name) const
667 if (filename.empty() || !filename.exists()) {
668 Alert::error(_("Cannot view file"),
669 bformat(_("File does not exist: %1$s"),
670 from_utf8(filename.absFileName())));
674 Format const * format = getFormat(format_name);
675 if (format && format->viewer().empty() &&
676 format->isChildFormat())
677 format = getFormat(format->parentFormat());
678 if (!format || format->viewer().empty()) {
679 // FIXME: I believe this is the wrong place to show alerts, it should be done
680 // by the caller (this should be "utility" code)
681 Alert::error(_("Cannot view file"),
682 bformat(_("No information for viewing %1$s"),
683 translateIfPossible(prettyName(format_name))));
687 if (format->viewer() == "auto") {
688 if (os::autoOpenFile(filename.absFileName(), os::VIEW, buffer.filePath()))
691 Alert::error(_("Cannot view file"),
692 bformat(_("Auto-view file %1$s failed"),
693 from_utf8(filename.absFileName())));
698 string command = format->viewer();
700 // Escape backslashes if not already in double or single quotes.
701 // We cannot simply quote the whole command as there may be arguments.
702 if (contains(command, '\\')) {
703 bool inquote1 = false;
704 bool inquote2 = false;
705 string::iterator cit = command.begin();
706 for (; cit != command.end(); ++cit) {
709 inquote1 = !inquote1;
712 inquote2 = !inquote2;
715 if (!inquote1 && !inquote2)
716 cit = ++command.insert(cit, '\\');
722 if (format_name == "dvi" &&
723 !lyxrc.view_dvi_paper_option.empty()) {
724 string paper_size = buffer.params().paperSizeName(BufferParams::XDVI);
725 if (!paper_size.empty()) {
726 command += ' ' + lyxrc.view_dvi_paper_option;
727 command += ' ' + paper_size;
728 if (buffer.params().orientation == ORIENTATION_LANDSCAPE &&
729 buffer.params().papersize != PAPER_CUSTOM)
734 if (!contains(command, token_from_format))
735 command += ' ' + token_from_format;
737 command = subst(command, token_from_format,
738 quoteName(onlyFileName(filename.toFilesystemEncoding()), quote_shell_filename));
739 command = subst(command, token_path_format,
740 quoteName(onlyPath(filename.toFilesystemEncoding()), quote_shell_filename));
741 command = subst(command, token_socket_format, quoteName(theServerSocket().address()));
742 LYXERR(Debug::FILES, "Executing command: " << command);
743 // FIXME UNICODE utf8 can be wrong for files
744 buffer.message(_("Executing command: ") + from_utf8(command));
746 PathChanger p(filename.onlyPath());
748 one.startscript(Systemcall::DontWait, command,
749 buffer.filePath(), buffer.layoutPos());
751 // we can't report any sort of error, since we aren't waiting
756 bool Formats::edit(Buffer const & buffer, FileName const & filename,
757 string const & format_name) const
759 if (filename.empty() || !filename.exists()) {
760 Alert::error(_("Cannot edit file"),
761 bformat(_("File does not exist: %1$s"),
762 from_utf8(filename.absFileName())));
766 // LinkBack files look like PDF, but have the .linkback extension
767 string const ext = getExtension(filename.absFileName());
768 if (format_name == "pdf6" && ext == "linkback") {
769 #ifdef USE_MACOSX_PACKAGING
770 return editLinkBackFile(filename.absFileName().c_str());
772 Alert::error(_("Cannot edit file"),
773 _("LinkBack files can only be edited on Apple Mac OSX."));
775 #endif // USE_MACOSX_PACKAGING
778 Format const * format = getFormat(format_name);
779 if (format && format->editor().empty() &&
780 format->isChildFormat())
781 format = getFormat(format->parentFormat());
782 if (!format || format->editor().empty()) {
783 // FIXME: I believe this is the wrong place to show alerts, it should
784 // be done by the caller (this should be "utility" code)
785 Alert::error(_("Cannot edit file"),
786 bformat(_("No information for editing %1$s"),
787 translateIfPossible(prettyName(format_name))));
792 if (format->editor() == "auto") {
793 if (os::autoOpenFile(filename.absFileName(), os::EDIT, buffer.filePath()))
796 Alert::error(_("Cannot edit file"),
797 bformat(_("Auto-edit file %1$s failed"),
798 from_utf8(filename.absFileName())));
803 string command = format->editor();
805 if (!contains(command, token_from_format))
806 command += ' ' + token_from_format;
808 command = subst(command, token_from_format,
809 quoteName(filename.toFilesystemEncoding(), quote_shell_filename));
810 command = subst(command, token_path_format,
811 quoteName(onlyPath(filename.toFilesystemEncoding()), quote_shell_filename));
812 command = subst(command, token_socket_format, quoteName(theServerSocket().address()));
813 LYXERR(Debug::FILES, "Executing command: " << command);
814 // FIXME UNICODE utf8 can be wrong for files
815 buffer.message(_("Executing command: ") + from_utf8(command));
818 one.startscript(Systemcall::DontWait, command,
819 buffer.filePath(), buffer.layoutPos());
821 // we can't report any sort of error, since we aren't waiting
826 docstring const Formats::prettyName(string const & name) const
828 Format const * format = getFormat(name);
830 return format->prettyname();
832 return from_utf8(name);
836 string const Formats::extension(string const & name) const
838 Format const * format = getFormat(name);
840 return format->extension();
846 string const Formats::extensions(string const & name) const
848 Format const * format = getFormat(name);
850 return format->extensions();
858 typedef Translator<OutputParams::FLAVOR, string> FlavorTranslator;
861 FlavorTranslator initFlavorTranslator()
863 FlavorTranslator f(OutputParams::LATEX, "latex");
864 f.addPair(OutputParams::DVILUATEX, "dviluatex");
865 f.addPair(OutputParams::LUATEX, "luatex");
866 f.addPair(OutputParams::PDFLATEX, "pdflatex");
867 f.addPair(OutputParams::XETEX, "xetex");
868 f.addPair(OutputParams::XML, "docbook-xml");
869 f.addPair(OutputParams::HTML, "xhtml");
870 f.addPair(OutputParams::TEXT, "text");
871 f.addPair(OutputParams::LYX, "lyx");
876 FlavorTranslator const & flavorTranslator()
878 static FlavorTranslator const translator = initFlavorTranslator();
885 std::string flavor2format(OutputParams::FLAVOR flavor)
887 return flavorTranslator().find(flavor);
891 /* Not currently needed, but I'll leave the code in case it is.
892 OutputParams::FLAVOR format2flavor(std::string fmt)
894 return flavorTranslator().find(fmt);