]> git.lyx.org Git - lyx.git/blob - src/Format.cpp
remove most traces of boost::regex
[lyx.git] / src / Format.cpp
1 /**
2  * \file Format.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Dekel Tsur
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "Format.h"
14 #include "Buffer.h"
15 #include "BufferParams.h"
16 #include "LyXRC.h"
17 #include "ServerSocket.h"
18
19 #include "frontends/alert.h" //to be removed?
20
21 #include "support/debug.h"
22 #include "support/docstream.h"
23 #include "support/filetools.h"
24 #include "support/gettext.h"
25 #include "support/lstrings.h"
26 #include "support/lyxmagic.h"
27 #include "support/mutex.h"
28 #include "support/os.h"
29 #include "support/PathChanger.h"
30 #include "support/Systemcall.h"
31 #include "support/textutils.h"
32 #include "support/Translator.h"
33
34 #include <algorithm>
35 #include <functional>
36 #include <map>
37 #include <ctime>
38
39 // FIXME: Q_OS_MAC is not available, it's in Qt
40 #ifdef USE_MACOSX_PACKAGING
41 #include "support/linkback/LinkBackProxy.h"
42 #endif
43
44 using namespace std;
45 using namespace lyx::support;
46
47 namespace lyx {
48
49 namespace Alert = frontend::Alert;
50 namespace os = support::os;
51
52 namespace {
53
54 string const token_from_format("$$i");
55 string const token_path_format("$$p");
56 string const token_socket_format("$$a");
57
58 } // namespace
59
60
61 bool Format::formatSorter(Format const * lhs, Format const * rhs)
62 {
63         return compare_locale(translateIfPossible(lhs->prettyname()),
64                               translateIfPossible(rhs->prettyname())) < 0;
65 }
66
67 bool operator<(Format const & a, Format const & b)
68 {
69         return compare_locale(translateIfPossible(a.prettyname()),
70                               translateIfPossible(b.prettyname())) < 0;
71 }
72
73
74 Format::Format(string const & n, string const & e, docstring const & p,
75                string const & s, string const & v, string const & ed,
76                string const & m, int flags)
77         : name_(n), prettyname_(p), shortcut_(s), viewer_(v),
78           editor_(ed), mime_(m), flags_(flags)
79 {
80         extension_list_ = getVectorFromString(e, ",");
81         LYXERR(Debug::GRAPHICS, "New Format: n=" << n << ", flags=" << flags);
82 }
83
84
85 bool Format::dummy() const
86 {
87         return extension().empty();
88 }
89
90
91 string const Format::extensions() const
92 {
93         return getStringFromVector(extension_list_, ", ");
94 }
95
96
97 bool Format::hasExtension(string const & ext) const
98 {
99         return (find(extension_list_.begin(), extension_list_.end(), ext)
100                 != extension_list_.end());
101 }
102
103
104 bool Format::isChildFormat() const
105 {
106         if (name_.empty())
107                 return false;
108         return isDigitASCII(name_[name_.length() - 1]);
109 }
110
111
112 string const Format::parentFormat() const
113 {
114         return name_.substr(0, name_.length() - 1);
115 }
116
117
118 void Format::setExtensions(string const & e)
119 {
120         extension_list_ = getVectorFromString(e, ",");
121 }
122
123
124 namespace {
125
126 std::function<bool (Format const &)> FormatNameIs(string const & name)
127 {
128         return [name](Format const & f){ return f.name() == name; };
129 }
130
131 }
132
133 // This method should return a reference, and throw an exception
134 // if the format named name cannot be found (Lgb)
135 Format const * Formats::getFormat(string const & name) const
136 {
137         FormatList::const_iterator cit =
138                 find_if(formatlist_.begin(), formatlist_.end(),
139                         FormatNameIs(name));
140         if (cit != formatlist_.end())
141                 return &(*cit);
142         else
143                 return nullptr;
144 }
145
146
147 Format * Formats::getFormat(string const & name)
148 {
149         FormatList::iterator it =
150                 find_if(formatlist_.begin(), formatlist_.end(),
151                                 FormatNameIs(name));
152
153         if (it != formatlist_.end())
154                 return &(*it);
155
156         return nullptr;
157 }
158
159
160 namespace {
161
162 /** Guess the file format name (as in Format::name()) from contents.
163  *  Normally you don't want to use this directly, but rather
164  *  Formats::getFormatFromFile().
165  */
166 string guessFormatFromContents(FileName const & fn)
167 {
168         // the different filetypes and what they contain in one of the first lines
169         // (dots are any characters).           (Herbert 20020131)
170         // AGR  Grace...
171         // BMP  BM...
172         // EPS  %!PS-Adobe-3.0 EPSF...
173         // FIG  #FIG...
174         // FITS ...BITPIX...
175         // GIF  GIF...
176         // JPG  \377\330...     (0xFFD8)
177         // PDF  %PDF-...
178         // PNG  .PNG...
179         // PBM  P1... or P4     (B/W)
180         // PGM  P2... or P5     (Grayscale)
181         // PPM  P3... or P6     (color)
182         // PS   %!PS-Adobe-2.0 or 1.0,  no "EPSF"!
183         // SGI  \001\332...     (decimal 474)
184         // TGIF %TGIF...
185         // TIFF II... or MM...
186         // XBM  ..._bits[]...
187         // XPM  /* XPM */    sometimes missing (f.ex. tgif-export)
188         //      ...static char *...
189         // XWD  \000\000\000\151        (0x00006900) decimal 105
190         //
191         // GZIP \037\213        http://www.ietf.org/rfc/rfc1952.txt
192         // ZIP  PK...                   http://www.halyava.ru/document/ind_arch.htm
193         // Z    \037\235                UNIX compress
194
195         // paranoia check
196         if (fn.empty() || !fn.isReadableFile())
197                 return string();
198
199         ifstream ifs(fn.toFilesystemEncoding().c_str());
200         if (!ifs)
201                 // Couldn't open file...
202                 return string();
203
204         // gnuzip
205         static string const gzipStamp = "\037\213";
206
207         // PKZIP
208         static string const zipStamp = "PK";
209
210         // ZIP containers (koffice, openoffice.org etc).
211         static string const nonzipStamp = "\010\0\0\0mimetypeapplication/";
212
213         // compress
214         static string const compressStamp = "\037\235";
215
216         // DOS binary EPS according to Adobe TN-5002
217         static string const binEPSStamp = "\xC5\xD0\xD3\xC6";
218
219
220         // Maximum strings to read
221         int const max_count = 50;
222         int count = 0;
223
224         string str;
225         string format;
226         bool firstLine = true;
227         bool backslash = false;
228         bool maybelatex = false;
229         int dollars = 0;
230         while ((count++ < max_count) && format.empty() && !maybelatex) {
231                 if (ifs.eof())
232                         break;
233
234                 getline(ifs, str);
235                 string const stamp = str.substr(0, 2);
236                 if (firstLine && str.size() >= 2) {
237                         // at first we check for a zipped file, because this
238                         // information is saved in the first bytes of the file!
239                         // also some graphic formats which save the information
240                         // in the first line, too.
241                         if (prefixIs(str, gzipStamp)) {
242                                 format =  "gzip";
243
244                         } else if (stamp == zipStamp &&
245                                    !contains(str, nonzipStamp)) {
246                                 format =  "zip";
247
248                         } else if (stamp == compressStamp) {
249                                 format =  "compress";
250
251                         // the graphics part
252                         } else if (stamp == "BM") {
253                                 format =  "bmp";
254
255                         } else if (stamp == "\377\330") {
256                                 format =  "jpg";
257
258                         } else if (prefixIs(str, "\x89PNG")) {
259                                 format =  "png";
260
261                         } else if (stamp == "\001\332") {
262                                 format =  "sgi";
263
264                         } else if (prefixIs(str, binEPSStamp)) {
265                                 format =  "eps";
266
267                         // PBM family
268                         // Don't need to use str.at(0), str.at(1) because
269                         // we already know that str.size() >= 2
270                         } else if (str[0] == 'P') {
271                                 switch (str[1]) {
272                                 case '1':
273                                 case '4':
274                                         format =  "pbm";
275                                     break;
276                                 case '2':
277                                 case '5':
278                                         format =  "pgm";
279                                     break;
280                                 case '3':
281                                 case '6':
282                                         format =  "ppm";
283                                 }
284                                 break;
285
286                         } else if ((stamp == "II") || (stamp == "MM")) {
287                                 format =  "tiff";
288
289                         } else if (prefixIs(str,"%TGIF")) {
290                                 format =  "tgif";
291
292                         } else if (prefixIs(str,"#FIG")) {
293                                 format =  "fig";
294
295                         } else if (prefixIs(str,"GIF")) {
296                                 format =  "gif";
297
298                         } else if (str.size() > 3) {
299                                 int const c = ((str[0] << 24) & (str[1] << 16) &
300                                                (str[2] << 8)  & str[3]);
301                                 if (c == 105) {
302                                         format =  "xwd";
303                                 }
304                         }
305
306                         firstLine = false;
307                 }
308
309                 if (!format.empty())
310                     break;
311                 else if (contains(str,"EPSF"))
312                         // dummy, if we have wrong file description like
313                         // %!PS-Adobe-2.0EPSF"
314                         format = "eps";
315
316                 else if (contains(str, "Grace"))
317                         format = "agr";
318
319                 else if (contains(str, "%PDF"))
320                         // autodetect pdf format for graphics inclusion
321                         format = "pdf6";
322
323                 else if (contains(str, " EMF"))
324                         format = "emf";
325
326                 else if (contains(str, "%!PS-Adobe")) {
327                         // eps or ps
328                         ifs >> str;
329                         if (contains(str,"EPSF"))
330                                 format = "eps";
331                         else
332                             format = "ps";
333                 }
334
335                 else if (contains(str, "_bits[]"))
336                         format = "xbm";
337
338                 else if (contains(str, "XPM") || contains(str, "static char *"))
339                         format = "xpm";
340
341                 else if (contains(str, "BITPIX"))
342                         format = "fits";
343
344                 else if (contains(str, "\\documentclass") ||
345                          contains(str, "\\chapter") ||
346                          contains(str, "\\section") ||
347                          contains(str, "\\begin") ||
348                          contains(str, "\\end") ||
349                          contains(str, "$$") ||
350                          contains(str, "\\[") ||
351                          contains(str, "\\]"))
352                         maybelatex = true;
353                 else {
354                         if (contains(str, '\\'))
355                                 backslash = true;
356                         dollars += count_char(str, '$');
357                         if (backslash && dollars > 1)
358                                 // inline equation
359                                 maybelatex = true;
360                 }
361         }
362
363         if (format.empty() && maybelatex && !isBinaryFile(fn))
364                 format = "latex";
365
366         if (format.empty()) {
367                 if (ifs.eof())
368                         LYXERR(Debug::GRAPHICS, "filetools(getFormatFromContents)\n"
369                                "\tFile type not recognised before EOF!");
370         } else {
371                 LYXERR(Debug::GRAPHICS, "Recognised Fileformat: " << format);
372                 return format;
373         }
374
375         LYXERR(Debug::GRAPHICS, "filetools(getFormatFromContents)\n"
376                 << "\tCouldn't find a known format!");
377         return string();
378 }
379
380 } // namespace
381
382
383 string Formats::getFormatFromFile(FileName const & filename) const
384 {
385         if (filename.empty())
386                 return string();
387
388         string psformat;
389         string format;
390         if (filename.exists()) {
391                 // one instance of Magic that will be reused for next calls
392                 // This avoids to read the magic file everytime
393                 // If libmagic is not available, Magic::file returns an empty string.
394                 static Magic magic;
395                 string const result = magic.file(filename.toFilesystemEncoding());
396                 string const mime = token(result, ';', 0);
397                 // our own detection is better for binary files (can be anything)
398                 // and different plain text formats
399                 if (!mime.empty() && mime != "application/octet-stream" &&
400                         mime != "text/plain") {
401                         Formats::const_iterator cit =
402                                 find_if(formatlist_.begin(), formatlist_.end(),
403                                                 [mime](Format const & f){ return f.mime() == mime; });
404                         if (cit != formatlist_.end()) {
405                                 LYXERR(Debug::GRAPHICS, "\tgot format from MIME type: "
406                                            << mime << " -> " << cit->name());
407                                 // See special eps/ps handling below
408                                 if (mime == "application/postscript")
409                                         psformat = cit->name();
410                                 else
411                                         format = cit->name();
412                         }
413                 }
414
415                 // libmagic recognizes as latex also some formats of ours
416                 // such as pstex and pdftex. Therefore we have to perform
417                 // additional checks in this case (bug 9244).
418                 if (!format.empty() && format != "latex")
419                         return format;
420         }
421
422         string const ext = getExtension(filename.absFileName());
423         if (format.empty()) {
424                 // libmagic does not distinguish eps and ps.
425                 // Therefore we need to use our own detection here, but only if it
426                 // recognizes either ps or eps. Otherwise the libmagic guess will
427                 // be better (bug 9146).
428                 format = guessFormatFromContents(filename);
429                 if (!psformat.empty()) {
430                         if (isPostScriptFileFormat(format))
431                                 return format;
432                         else
433                                 return psformat;
434                 }
435
436                 if (isZippedFileFormat(format) && !ext.empty()) {
437                         string const & fmt_name = getFormatFromExtension(ext);
438                         if (!fmt_name.empty()) {
439                                 Format const * p_format = getFormat(fmt_name);
440                                 if (p_format && p_format->zippedNative())
441                                         return p_format->name();
442                         }
443                 }
444                 // Don't simply return latex (bug 9244).
445                 if (!format.empty() && format != "latex")
446                         return format;
447         }
448
449         // Both libmagic and our guessing from contents may return as latex
450         // also lyx files and our pstex and pdftex formats. In this case we
451         // give precedence to the format determined by the extension.
452         if (format == "latex") {
453                 format = getFormatFromExtension(ext);
454                 return format.empty() ? "latex" : format;
455         }
456
457         // try to find a format from the file extension.
458         return getFormatFromExtension(ext);
459 }
460
461
462 string Formats::getFormatFromExtension(string const & ext) const
463 {
464         if (!ext.empty()) {
465                 // this is ambigous if two formats have the same extension,
466                 // but better than nothing
467                 Formats::const_iterator cit =
468                         find_if(formatlist_.begin(), formatlist_.end(),
469                                 [ext](Format const & f){ return f.hasExtension(ext); });
470                 if (cit != formatlist_.end()) {
471                         LYXERR(Debug::GRAPHICS, "\twill guess format from file extension: "
472                                 << ext << " -> " << cit->name());
473                         return cit->name();
474                 }
475         }
476         return string();
477 }
478
479
480 /// Used to store last timestamp of file and whether it is (was) zipped
481 struct ZippedInfo {
482         bool zipped;
483         std::time_t timestamp;
484         ZippedInfo(bool zipped, std::time_t timestamp)
485         : zipped(zipped), timestamp(timestamp) { }
486 };
487
488
489 /// Mapping absolute pathnames of files to their ZippedInfo metadata.
490 static std::map<std::string, ZippedInfo> zipped_;
491 static Mutex zipped_mutex;
492
493
494 bool Formats::isZippedFile(support::FileName const & filename) const {
495         string const & fname = filename.absFileName();
496         time_t timestamp = filename.lastModified();
497         Mutex::Locker lock(&zipped_mutex);
498         map<string, ZippedInfo>::iterator it = zipped_.find(fname);
499         if (it != zipped_.end() && it->second.timestamp == timestamp)
500                 return it->second.zipped;
501         // FIXME perf: This very expensive function is called on startup on each
502         // file whic is going to be parsed, and also on svgz icons. Maybe there is a
503         // quicker way to check whether a file is zipped?  I.e. for icons we
504         // probably just need to check the extension (svgz vs svg).
505         string const & format = getFormatFromFile(filename);
506         bool zipped = (format == "gzip" || format == "zip");
507         zipped_.insert(make_pair(fname, ZippedInfo(zipped, timestamp)));
508         return zipped;
509 }
510
511
512 bool Formats::isZippedFileFormat(string const & format)
513 {
514         return contains("gzip zip compress", format) && !format.empty();
515 }
516
517
518 bool Formats::isPostScriptFileFormat(string const & format)
519 {
520         return format == "ps" || format == "eps";
521 }
522
523 static string fixCommand(string const & cmd, string const & ext,
524                   os::auto_open_mode mode)
525 {
526         // configure.py says we do not want a viewer/editor
527         if (cmd.empty())
528                 return cmd;
529
530         // Does the OS manage this format?
531         if (os::canAutoOpenFile(ext, mode))
532                 return "auto";
533
534         // if configure.py found nothing, clear the command
535         if (token(cmd, ' ', 0) == "auto")
536                 return string();
537
538         // use the command found by configure.py
539         return cmd;
540 }
541
542
543 void Formats::setAutoOpen()
544 {
545         FormatList::iterator fit = formatlist_.begin();
546         FormatList::iterator const fend = formatlist_.end();
547         for ( ; fit != fend ; ++fit) {
548                 fit->setViewer(fixCommand(fit->viewer(), fit->extension(), os::VIEW));
549                 fit->setEditor(fixCommand(fit->editor(), fit->extension(), os::EDIT));
550         }
551 }
552
553
554 int Formats::getNumber(string const & name) const
555 {
556         FormatList::const_iterator cit =
557                 find_if(formatlist_.begin(), formatlist_.end(),
558                         FormatNameIs(name));
559         if (cit == formatlist_.end())
560                 return -1;
561
562         return distance(formatlist_.begin(), cit);
563 }
564
565
566 void Formats::add(string const & name)
567 {
568         if (!getFormat(name))
569                 add(name, name, from_utf8(name), string(), string(), string(),
570                     string(), Format::document);
571 }
572
573
574 void Formats::add(string const & name, string const & extensions,
575                   docstring const & prettyname, string const & shortcut,
576                   string const & viewer, string const & editor,
577                   string const & mime, int flags)
578 {
579         Format * format = getFormat(name);
580         if (format)
581                 *format = Format(name, extensions, prettyname, shortcut, viewer,
582                                  editor, mime, flags);
583         else
584                 formatlist_.push_back(Format(name, extensions, prettyname,
585                                                 shortcut, viewer, editor, mime, flags));
586 }
587
588
589 void Formats::erase(string const & name)
590 {
591         FormatList::iterator it =
592                 find_if(formatlist_.begin(), formatlist_.end(),
593                         FormatNameIs(name));
594         if (it != formatlist_.end())
595                 formatlist_.erase(it);
596 }
597
598
599 void Formats::sort()
600 {
601         std::sort(formatlist_.begin(), formatlist_.end());
602 }
603
604
605 void Formats::setViewer(string const & name, string const & command)
606 {
607         add(name);
608         Format * format = getFormat(name);
609         if (format)
610                 format->setViewer(command);
611         else
612                 LYXERR0("Unable to set viewer for non-existent format: " << name);
613 }
614
615
616 void Formats::setEditor(string const & name, string const & command)
617 {
618         add(name);
619         Format * format = getFormat(name);
620         if (format)
621                 format->setEditor(command);
622         else
623                 LYXERR0("Unable to set editor for non-existent format: " << name);
624 }
625
626
627 bool Formats::view(Buffer const & buffer, FileName const & filename,
628                    string const & format_name) const
629 {
630         if (filename.empty() || !filename.exists()) {
631                 Alert::error(_("Cannot view file"),
632                         bformat(_("File does not exist: %1$s"),
633                                 from_utf8(filename.absFileName())));
634                 return false;
635         }
636
637         Format const * format = getFormat(format_name);
638         if (format && format->viewer().empty() &&
639             format->isChildFormat())
640                 format = getFormat(format->parentFormat());
641         if (!format || format->viewer().empty()) {
642 // FIXME: I believe this is the wrong place to show alerts, it should be done
643 // by the caller (this should be "utility" code)
644                 Alert::error(_("Cannot view file"),
645                         bformat(_("No information for viewing %1$s"),
646                                 translateIfPossible(prettyName(format_name))));
647                 return false;
648         }
649         // viewer is 'auto'
650         if (format->viewer() == "auto") {
651                 if (os::autoOpenFile(filename.absFileName(), os::VIEW, buffer.filePath()))
652                         return true;
653                 else {
654                         Alert::error(_("Cannot view file"),
655                                 bformat(_("Auto-view file %1$s failed"),
656                                         from_utf8(filename.absFileName())));
657                         return false;
658                 }
659         }
660
661         string command = format->viewer();
662
663         // Escape backslashes if not already in double or single quotes.
664         // We cannot simply quote the whole command as there may be arguments.
665         if (contains(command, '\\')) {
666                 bool inquote1 = false;
667                 bool inquote2 = false;
668                 string::iterator cit = command.begin();
669                 for (; cit != command.end(); ++cit) {
670                         switch (*cit) {
671                         case '"':
672                                 inquote1 = !inquote1;
673                                 break;
674                         case '\'':
675                                 inquote2 = !inquote2;
676                                 break;
677                         case '\\':
678                                 if (!inquote1 && !inquote2)
679                                         cit = ++command.insert(cit, '\\');
680                                 break;
681                         }
682                 }
683         }
684
685         if (format_name == "dvi" &&
686             !lyxrc.view_dvi_paper_option.empty()) {
687                 string paper_size = buffer.params().paperSizeName(BufferParams::XDVI);
688                 if (!paper_size.empty()) {
689                         command += ' ' + lyxrc.view_dvi_paper_option;
690                         command += ' ' + paper_size;
691                         if (buffer.params().orientation == ORIENTATION_LANDSCAPE &&
692                             buffer.params().papersize != PAPER_CUSTOM)
693                                 command += 'r';
694                 }
695         }
696
697         if (!contains(command, token_from_format))
698                 command += ' ' + token_from_format;
699
700         command = subst(command, token_from_format,
701                         quoteName(onlyFileName(filename.toFilesystemEncoding()), quote_shell_filename));
702         command = subst(command, token_path_format,
703                         quoteName(onlyPath(filename.toFilesystemEncoding()), quote_shell_filename));
704         command = subst(command, token_socket_format, quoteName(theServerSocket().address()));
705         LYXERR(Debug::FILES, "Executing command: " << command);
706         // FIXME UNICODE utf8 can be wrong for files
707         buffer.message(_("Executing command: ") + from_utf8(command));
708
709         PathChanger p(filename.onlyPath());
710         Systemcall one;
711         one.startscript(Systemcall::DontWait, command,
712                         buffer.filePath(), buffer.layoutPos());
713
714         // we can't report any sort of error, since we aren't waiting
715         return true;
716 }
717
718
719 bool Formats::edit(Buffer const & buffer, FileName const & filename,
720                          string const & format_name) const
721 {
722         if (filename.empty() || !filename.exists()) {
723                 Alert::error(_("Cannot edit file"),
724                         bformat(_("File does not exist: %1$s"),
725                                 from_utf8(filename.absFileName())));
726                 return false;
727         }
728
729         // LinkBack files look like PDF, but have the .linkback extension
730         string const ext = getExtension(filename.absFileName());
731         if (format_name == "pdf6" && ext == "linkback") {
732 #ifdef USE_MACOSX_PACKAGING
733                 return editLinkBackFile(filename.absFileName().c_str());
734 #else
735                 Alert::error(_("Cannot edit file"),
736                              _("LinkBack files can only be edited on Apple Mac OSX."));
737                 return false;
738 #endif // USE_MACOSX_PACKAGING
739         }
740
741         Format const * format = getFormat(format_name);
742         if (format && format->editor().empty() &&
743             format->isChildFormat())
744                 format = getFormat(format->parentFormat());
745         if (!format || format->editor().empty()) {
746 // FIXME: I believe this is the wrong place to show alerts, it should
747 // be done by the caller (this should be "utility" code)
748                 Alert::error(_("Cannot edit file"),
749                         bformat(_("No information for editing %1$s"),
750                                 translateIfPossible(prettyName(format_name))));
751                 return false;
752         }
753
754         // editor is 'auto'
755         if (format->editor() == "auto") {
756                 if (os::autoOpenFile(filename.absFileName(), os::EDIT, buffer.filePath()))
757                         return true;
758                 else {
759                         Alert::error(_("Cannot edit file"),
760                                 bformat(_("Auto-edit file %1$s failed"),
761                                         from_utf8(filename.absFileName())));
762                         return false;
763                 }
764         }
765
766         string command = format->editor();
767
768         if (!contains(command, token_from_format))
769                 command += ' ' + token_from_format;
770
771         command = subst(command, token_from_format,
772                         quoteName(filename.toFilesystemEncoding(), quote_shell_filename));
773         command = subst(command, token_path_format,
774                         quoteName(onlyPath(filename.toFilesystemEncoding()), quote_shell_filename));
775         command = subst(command, token_socket_format, quoteName(theServerSocket().address()));
776         LYXERR(Debug::FILES, "Executing command: " << command);
777         // FIXME UNICODE utf8 can be wrong for files
778         buffer.message(_("Executing command: ") + from_utf8(command));
779
780         Systemcall one;
781         one.startscript(Systemcall::DontWait, command,
782                         buffer.filePath(), buffer.layoutPos());
783
784         // we can't report any sort of error, since we aren't waiting
785         return true;
786 }
787
788
789 docstring const Formats::prettyName(string const & name) const
790 {
791         Format const * format = getFormat(name);
792         if (format)
793                 return format->prettyname();
794         else
795                 return from_utf8(name);
796 }
797
798
799 string const Formats::extension(string const & name) const
800 {
801         Format const * format = getFormat(name);
802         if (format)
803                 return format->extension();
804         else
805                 return name;
806 }
807
808
809 string const Formats::extensions(string const & name) const
810 {
811         Format const * format = getFormat(name);
812         if (format)
813                 return format->extensions();
814         else
815                 return name;
816 }
817
818
819 namespace {
820
821 typedef Translator<OutputParams::FLAVOR, string> FlavorTranslator;
822
823
824 FlavorTranslator initFlavorTranslator()
825 {
826         FlavorTranslator f(OutputParams::LATEX, "latex");
827         f.addPair(OutputParams::DVILUATEX, "dviluatex");
828         f.addPair(OutputParams::LUATEX, "luatex");
829         f.addPair(OutputParams::PDFLATEX, "pdflatex");
830         f.addPair(OutputParams::XETEX, "xetex");
831         f.addPair(OutputParams::DOCBOOK5, "docbook-xml");
832         f.addPair(OutputParams::HTML, "xhtml");
833         f.addPair(OutputParams::TEXT, "text");
834         f.addPair(OutputParams::LYX, "lyx");
835         return f;
836 }
837
838
839 FlavorTranslator const & flavorTranslator()
840 {
841         static FlavorTranslator const translator = initFlavorTranslator();
842         return translator;
843 }
844
845 } // namespace
846
847
848 std::string flavor2format(OutputParams::FLAVOR flavor)
849 {
850         return flavorTranslator().find(flavor);
851 }
852
853
854 /* Not currently needed, but I'll leave the code in case it is.
855 OutputParams::FLAVOR format2flavor(std::string fmt)
856 {
857         return flavorTranslator().find(fmt);
858 } */
859
860 } // namespace lyx