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 "OutputParams.h"
18 #include "ServerSocket.h"
20 #include "frontends/alert.h" //to be removed?
22 #include "support/debug.h"
23 #include "support/docstream.h"
24 #include "support/filetools.h"
25 #include "support/gettext.h"
26 #include "support/lstrings.h"
27 #include "support/lyxmagic.h"
28 #include "support/mutex.h"
29 #include "support/os.h"
30 #include "support/PathChanger.h"
31 #include "support/Systemcall.h"
32 #include "support/textutils.h"
33 #include "support/Translator.h"
40 // FIXME: Q_OS_MAC is not available, it's in Qt
41 #ifdef USE_MACOSX_PACKAGING
42 #include "support/linkback/LinkBackProxy.h"
46 using namespace lyx::support;
50 namespace Alert = frontend::Alert;
51 namespace os = support::os;
55 string const token_from_format("$$i");
56 string const token_path_format("$$p");
57 string const token_socket_format("$$a");
62 bool Format::formatSorter(Format const * lhs, Format const * rhs)
64 return compare_locale(translateIfPossible(lhs->prettyname()),
65 translateIfPossible(rhs->prettyname())) < 0;
68 bool operator<(Format const & a, Format const & b)
70 return compare_locale(translateIfPossible(a.prettyname()),
71 translateIfPossible(b.prettyname())) < 0;
75 Format::Format(string const & n, string const & e, docstring const & p,
76 string const & s, string const & v, string const & ed,
77 string const & m, int flags)
78 : name_(n), prettyname_(p), shortcut_(s), viewer_(v),
79 editor_(ed), mime_(m), flags_(flags)
81 extension_list_ = getVectorFromString(e, ",");
82 LYXERR(Debug::GRAPHICS, "New Format: n=" << n << ", flags=" << flags);
86 bool Format::dummy() const
88 return extension().empty();
92 string const Format::extensions() const
94 return getStringFromVector(extension_list_, ", ");
98 bool Format::hasExtension(string const & ext) const
100 return (find(extension_list_.begin(), extension_list_.end(), ext)
101 != extension_list_.end());
105 bool Format::isChildFormat() const
109 return isDigitASCII(name_[name_.length() - 1]);
113 string const Format::parentFormat() const
115 return name_.substr(0, name_.length() - 1);
119 void Format::setExtensions(string const & e)
121 extension_list_ = getVectorFromString(e, ",");
127 std::function<bool (Format const &)> FormatNameIs(string const & name)
129 return [name](Format const & f){ return f.name() == name; };
134 // This method should return a reference, and throw an exception
135 // if the format named name cannot be found (Lgb)
136 Format const * Formats::getFormat(string const & name) const
138 FormatList::const_iterator cit =
139 find_if(formatlist_.begin(), formatlist_.end(),
141 if (cit != formatlist_.end())
148 Format * Formats::getFormat(string const & name)
150 FormatList::iterator it =
151 find_if(formatlist_.begin(), formatlist_.end(),
154 if (it != formatlist_.end())
163 /** Guess the file format name (as in Format::name()) from contents.
164 * Normally you don't want to use this directly, but rather
165 * Formats::getFormatFromFile().
167 string guessFormatFromContents(FileName const & fn)
169 // the different filetypes and what they contain in one of the first lines
170 // (dots are any characters). (Herbert 20020131)
173 // EPS %!PS-Adobe-3.0 EPSF...
177 // JPG \377\330... (0xFFD8)
180 // PBM P1... or P4 (B/W)
181 // PGM P2... or P5 (Grayscale)
182 // PPM P3... or P6 (color)
183 // PS %!PS-Adobe-2.0 or 1.0, no "EPSF"!
184 // SGI \001\332... (decimal 474)
186 // TIFF II... or MM...
188 // XPM /* XPM */ sometimes missing (f.ex. tgif-export)
189 // ...static char *...
190 // XWD \000\000\000\151 (0x00006900) decimal 105
192 // GZIP \037\213 http://www.ietf.org/rfc/rfc1952.txt
193 // ZIP PK... http://www.halyava.ru/document/ind_arch.htm
194 // Z \037\235 UNIX compress
197 if (fn.empty() || !fn.isReadableFile())
200 ifstream ifs(fn.toFilesystemEncoding().c_str());
202 // Couldn't open file...
206 static string const gzipStamp = "\037\213";
209 static string const zipStamp = "PK";
211 // ZIP containers (koffice, openoffice.org etc).
212 static string const nonzipStamp = "\010\0\0\0mimetypeapplication/";
215 static string const compressStamp = "\037\235";
217 // DOS binary EPS according to Adobe TN-5002
218 static string const binEPSStamp = "\xC5\xD0\xD3\xC6";
221 // Maximum strings to read
222 int const max_count = 50;
227 bool firstLine = true;
228 bool backslash = false;
229 bool maybelatex = false;
231 while ((count++ < max_count) && format.empty() && !maybelatex) {
236 string const stamp = str.substr(0, 2);
237 if (firstLine && str.size() >= 2) {
238 // at first we check for a zipped file, because this
239 // information is saved in the first bytes of the file!
240 // also some graphic formats which save the information
241 // in the first line, too.
242 if (prefixIs(str, gzipStamp)) {
245 } else if (stamp == zipStamp &&
246 !contains(str, nonzipStamp)) {
249 } else if (stamp == compressStamp) {
253 } else if (stamp == "BM") {
256 } else if (stamp == "\377\330") {
259 } else if (prefixIs(str, "\x89PNG")) {
262 } else if (stamp == "\001\332") {
265 } else if (prefixIs(str, binEPSStamp)) {
269 // Don't need to use str.at(0), str.at(1) because
270 // we already know that str.size() >= 2
271 } else if (str[0] == 'P') {
287 } else if ((stamp == "II") || (stamp == "MM")) {
290 } else if (prefixIs(str,"%TGIF")) {
293 } else if (prefixIs(str,"#FIG")) {
296 } else if (prefixIs(str,"GIF")) {
299 } else if (str.size() > 3) {
300 int const c = ((str[0] << 24) & (str[1] << 16) &
301 (str[2] << 8) & str[3]);
312 else if (contains(str,"EPSF"))
313 // dummy, if we have wrong file description like
314 // %!PS-Adobe-2.0EPSF"
317 else if (contains(str, "Grace"))
320 else if (contains(str, "%PDF"))
321 // autodetect pdf format for graphics inclusion
324 else if (contains(str, " EMF"))
327 else if (contains(str, "%!PS-Adobe")) {
330 if (contains(str,"EPSF"))
336 else if (contains(str, "_bits[]"))
339 else if (contains(str, "XPM") || contains(str, "static char *"))
342 else if (contains(str, "BITPIX"))
345 else if (contains(str, "\\documentclass") ||
346 contains(str, "\\chapter") ||
347 contains(str, "\\section") ||
348 contains(str, "\\begin") ||
349 contains(str, "\\end") ||
350 contains(str, "$$") ||
351 contains(str, "\\[") ||
352 contains(str, "\\]"))
355 if (contains(str, '\\'))
357 dollars += count_char(str, '$');
358 if (backslash && dollars > 1)
364 if (format.empty() && maybelatex && !isBinaryFile(fn))
367 if (format.empty()) {
369 LYXERR(Debug::GRAPHICS, "filetools(getFormatFromContents)\n"
370 "\tFile type not recognised before EOF!");
372 LYXERR(Debug::GRAPHICS, "Recognised Fileformat: " << format);
376 LYXERR(Debug::GRAPHICS, "filetools(getFormatFromContents)\n"
377 << "\tCouldn't find a known format!");
384 string Formats::getFormatFromFile(FileName const & filename) const
386 if (filename.empty())
391 if (filename.exists()) {
392 // one instance of Magic that will be reused for next calls
393 // This avoids to read the magic file everytime
394 // If libmagic is not available, Magic::file returns an empty string.
396 string const result = magic.file(filename.toFilesystemEncoding());
397 string const mime = token(result, ';', 0);
398 // our own detection is better for binary files (can be anything)
399 // and different plain text formats
400 if (!mime.empty() && mime != "application/octet-stream" &&
401 mime != "text/plain") {
402 Formats::const_iterator cit =
403 find_if(formatlist_.begin(), formatlist_.end(),
404 [mime](Format const & f){ return f.mime() == mime; });
405 if (cit != formatlist_.end()) {
406 LYXERR(Debug::GRAPHICS, "\tgot format from MIME type: "
407 << mime << " -> " << cit->name());
408 // See special eps/ps handling below
409 if (mime == "application/postscript")
410 psformat = cit->name();
412 format = cit->name();
416 // libmagic recognizes as latex also some formats of ours
417 // such as pstex and pdftex. Therefore we have to perform
418 // additional checks in this case (bug 9244).
419 if (!format.empty() && format != "latex")
423 string const ext = getExtension(filename.absFileName());
424 if (format.empty()) {
425 // libmagic does not distinguish eps and ps.
426 // Therefore we need to use our own detection here, but only if it
427 // recognizes either ps or eps. Otherwise the libmagic guess will
428 // be better (bug 9146).
429 format = guessFormatFromContents(filename);
430 if (!psformat.empty()) {
431 if (isPostScriptFileFormat(format))
437 if (isZippedFileFormat(format) && !ext.empty()) {
438 string const & fmt_name = getFormatFromExtension(ext);
439 if (!fmt_name.empty()) {
440 Format const * p_format = getFormat(fmt_name);
441 if (p_format && p_format->zippedNative())
442 return p_format->name();
445 // Don't simply return latex (bug 9244).
446 if (!format.empty() && format != "latex")
450 // Both libmagic and our guessing from contents may return as latex
451 // also lyx files and our pstex and pdftex formats. In this case we
452 // give precedence to the format determined by the extension.
453 if (format == "latex") {
454 format = getFormatFromExtension(ext);
455 return format.empty() ? "latex" : format;
458 // try to find a format from the file extension.
459 return getFormatFromExtension(ext);
463 string Formats::getFormatFromExtension(string const & ext) const
466 // this is ambigous if two formats have the same extension,
467 // but better than nothing
468 Formats::const_iterator cit =
469 find_if(formatlist_.begin(), formatlist_.end(),
470 [ext](Format const & f){ return f.hasExtension(ext); });
471 if (cit != formatlist_.end()) {
472 LYXERR(Debug::GRAPHICS, "\twill guess format from file extension: "
473 << ext << " -> " << cit->name());
481 /// Used to store last timestamp of file and whether it is (was) zipped
484 std::time_t timestamp;
485 ZippedInfo(bool zipped, std::time_t timestamp)
486 : zipped(zipped), timestamp(timestamp) { }
490 /// Mapping absolute pathnames of files to their ZippedInfo metadata.
491 static std::map<std::string, ZippedInfo> zipped_;
492 static Mutex zipped_mutex;
495 bool Formats::isZippedFile(support::FileName const & filename) const {
496 string const & fname = filename.absFileName();
497 time_t timestamp = filename.lastModified();
498 Mutex::Locker lock(&zipped_mutex);
499 map<string, ZippedInfo>::iterator it = zipped_.find(fname);
500 if (it != zipped_.end() && it->second.timestamp == timestamp)
501 return it->second.zipped;
502 // FIXME perf: This very expensive function is called on startup on each
503 // file whic is going to be parsed, and also on svgz icons. Maybe there is a
504 // quicker way to check whether a file is zipped? I.e. for icons we
505 // probably just need to check the extension (svgz vs svg).
506 string const & format = getFormatFromFile(filename);
507 bool zipped = (format == "gzip" || format == "zip");
508 zipped_.insert(make_pair(fname, ZippedInfo(zipped, timestamp)));
513 bool Formats::isZippedFileFormat(string const & format)
515 return contains("gzip zip compress", format) && !format.empty();
519 bool Formats::isPostScriptFileFormat(string const & format)
521 return format == "ps" || format == "eps";
524 static string fixCommand(string const & cmd, string const & ext,
525 os::auto_open_mode mode)
527 // configure.py says we do not want a viewer/editor
531 // Does the OS manage this format?
532 if (os::canAutoOpenFile(ext, mode))
535 // if configure.py found nothing, clear the command
536 if (token(cmd, ' ', 0) == "auto")
539 // use the command found by configure.py
544 void Formats::setAutoOpen()
546 FormatList::iterator fit = formatlist_.begin();
547 FormatList::iterator const fend = formatlist_.end();
548 for ( ; fit != fend ; ++fit) {
549 fit->setViewer(fixCommand(fit->viewer(), fit->extension(), os::VIEW));
550 fit->setEditor(fixCommand(fit->editor(), fit->extension(), os::EDIT));
555 int Formats::getNumber(string const & name) const
557 FormatList::const_iterator cit =
558 find_if(formatlist_.begin(), formatlist_.end(),
560 if (cit == formatlist_.end())
563 return distance(formatlist_.begin(), cit);
567 void Formats::add(string const & name)
569 if (!getFormat(name))
570 add(name, name, from_utf8(name), string(), string(), string(),
571 string(), Format::document);
575 void Formats::add(string const & name, string const & extensions,
576 docstring const & prettyname, string const & shortcut,
577 string const & viewer, string const & editor,
578 string const & mime, int flags)
580 Format * format = getFormat(name);
582 *format = Format(name, extensions, prettyname, shortcut, viewer,
583 editor, mime, flags);
585 formatlist_.push_back(Format(name, extensions, prettyname,
586 shortcut, viewer, editor, mime, flags));
590 void Formats::erase(string const & name)
592 FormatList::iterator it =
593 find_if(formatlist_.begin(), formatlist_.end(),
595 if (it != formatlist_.end())
596 formatlist_.erase(it);
602 std::sort(formatlist_.begin(), formatlist_.end());
606 void Formats::setViewer(string const & name, string const & command)
609 Format * format = getFormat(name);
611 format->setViewer(command);
613 LYXERR0("Unable to set viewer for non-existent format: " << name);
617 void Formats::setEditor(string const & name, string const & command)
620 Format * format = getFormat(name);
622 format->setEditor(command);
624 LYXERR0("Unable to set editor for non-existent format: " << name);
628 bool Formats::view(Buffer const & buffer, FileName const & filename,
629 string const & format_name) const
631 if (filename.empty() || !filename.exists()) {
632 Alert::error(_("Cannot view file"),
633 bformat(_("File does not exist: %1$s"),
634 from_utf8(filename.absFileName())));
638 Format const * format = getFormat(format_name);
639 if (format && format->viewer().empty() &&
640 format->isChildFormat())
641 format = getFormat(format->parentFormat());
642 if (!format || format->viewer().empty()) {
643 // FIXME: I believe this is the wrong place to show alerts, it should be done
644 // by the caller (this should be "utility" code)
645 Alert::error(_("Cannot view file"),
646 bformat(_("No information for viewing %1$s"),
647 translateIfPossible(prettyName(format_name))));
651 if (format->viewer() == "auto") {
652 if (os::autoOpenFile(filename.absFileName(), os::VIEW, buffer.filePath()))
655 Alert::error(_("Cannot view file"),
656 bformat(_("Auto-view file %1$s failed"),
657 from_utf8(filename.absFileName())));
662 string command = format->viewer();
664 // Escape backslashes if not already in double or single quotes.
665 // We cannot simply quote the whole command as there may be arguments.
666 if (contains(command, '\\')) {
667 bool inquote1 = false;
668 bool inquote2 = false;
669 string::iterator cit = command.begin();
670 for (; cit != command.end(); ++cit) {
673 inquote1 = !inquote1;
676 inquote2 = !inquote2;
679 if (!inquote1 && !inquote2)
680 cit = ++command.insert(cit, '\\');
686 if (format_name == "dvi" &&
687 !lyxrc.view_dvi_paper_option.empty()) {
688 string paper_size = buffer.params().paperSizeName(BufferParams::XDVI);
689 if (!paper_size.empty()) {
690 command += ' ' + lyxrc.view_dvi_paper_option;
691 command += ' ' + paper_size;
692 if (buffer.params().orientation == ORIENTATION_LANDSCAPE &&
693 buffer.params().papersize != PAPER_CUSTOM)
698 if (!contains(command, token_from_format))
699 command += ' ' + token_from_format;
701 command = subst(command, token_from_format,
702 quoteName(onlyFileName(filename.toFilesystemEncoding()), quote_shell_filename));
703 command = subst(command, token_path_format,
704 quoteName(onlyPath(filename.toFilesystemEncoding()), quote_shell_filename));
705 command = subst(command, token_socket_format, quoteName(theServerSocket().address()));
706 LYXERR(Debug::FILES, "Executing command: " << command);
707 // FIXME UNICODE utf8 can be wrong for files
708 buffer.message(_("Executing command: ") + from_utf8(command));
710 PathChanger p(filename.onlyPath());
712 one.startscript(Systemcall::DontWait, command,
713 buffer.filePath(), buffer.layoutPos());
715 // we can't report any sort of error, since we aren't waiting
720 bool Formats::edit(Buffer const & buffer, FileName const & filename,
721 string const & format_name) const
723 if (filename.empty() || !filename.exists()) {
724 Alert::error(_("Cannot edit file"),
725 bformat(_("File does not exist: %1$s"),
726 from_utf8(filename.absFileName())));
730 // LinkBack files look like PDF, but have the .linkback extension
731 string const ext = getExtension(filename.absFileName());
732 if (format_name == "pdf6" && ext == "linkback") {
733 #ifdef USE_MACOSX_PACKAGING
734 return editLinkBackFile(filename.absFileName().c_str());
736 Alert::error(_("Cannot edit file"),
737 _("LinkBack files can only be edited on Apple Mac OSX."));
739 #endif // USE_MACOSX_PACKAGING
742 Format const * format = getFormat(format_name);
743 if (format && format->editor().empty() &&
744 format->isChildFormat())
745 format = getFormat(format->parentFormat());
746 if (!format || format->editor().empty()) {
747 // FIXME: I believe this is the wrong place to show alerts, it should
748 // be done by the caller (this should be "utility" code)
749 Alert::error(_("Cannot edit file"),
750 bformat(_("No information for editing %1$s"),
751 translateIfPossible(prettyName(format_name))));
756 if (format->editor() == "auto") {
757 if (os::autoOpenFile(filename.absFileName(), os::EDIT, buffer.filePath()))
760 Alert::error(_("Cannot edit file"),
761 bformat(_("Auto-edit file %1$s failed"),
762 from_utf8(filename.absFileName())));
767 string command = format->editor();
769 if (!contains(command, token_from_format))
770 command += ' ' + token_from_format;
772 command = subst(command, token_from_format,
773 quoteName(filename.toFilesystemEncoding(), quote_shell_filename));
774 command = subst(command, token_path_format,
775 quoteName(onlyPath(filename.toFilesystemEncoding()), quote_shell_filename));
776 command = subst(command, token_socket_format, quoteName(theServerSocket().address()));
777 LYXERR(Debug::FILES, "Executing command: " << command);
778 // FIXME UNICODE utf8 can be wrong for files
779 buffer.message(_("Executing command: ") + from_utf8(command));
782 one.startscript(Systemcall::DontWait, command,
783 buffer.filePath(), buffer.layoutPos());
785 // we can't report any sort of error, since we aren't waiting
790 docstring const Formats::prettyName(string const & name) const
792 Format const * format = getFormat(name);
794 return format->prettyname();
796 return from_utf8(name);
800 string const Formats::extension(string const & name) const
802 Format const * format = getFormat(name);
804 return format->extension();
810 string const Formats::extensions(string const & name) const
812 Format const * format = getFormat(name);
814 return format->extensions();
822 typedef Translator<FLAVOR, string> FlavorTranslator;
825 FlavorTranslator initFlavorTranslator()
827 FlavorTranslator f(FLAVOR::LATEX, "latex");
828 f.addPair(FLAVOR::DVILUATEX, "dviluatex");
829 f.addPair(FLAVOR::LUATEX, "luatex");
830 f.addPair(FLAVOR::PDFLATEX, "pdflatex");
831 f.addPair(FLAVOR::XETEX, "xetex");
832 f.addPair(FLAVOR::DOCBOOK5, "docbook-xml");
833 f.addPair(FLAVOR::HTML, "xhtml");
834 f.addPair(FLAVOR::TEXT, "text");
835 f.addPair(FLAVOR::LYX, "lyx");
840 FlavorTranslator const & flavorTranslator()
842 static FlavorTranslator const translator = initFlavorTranslator();
849 std::string flavor2format(FLAVOR flavor)
851 return flavorTranslator().find(flavor);
855 /* Not currently needed, but I'll leave the code in case it is.
856 FLAVOR format2flavor(std::string fmt)
858 return flavorTranslator().find(fmt);