]> git.lyx.org Git - lyx.git/blob - src/tex2lyx/tex2lyx.cpp
Typos
[lyx.git] / src / tex2lyx / tex2lyx.cpp
1 /**
2  * \file tex2lyx.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author André Pönitz
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 // {[(
12
13 #include <config.h>
14
15 #include "tex2lyx.h"
16
17 #include "Context.h"
18 #include "Encoding.h"
19 #include "Layout.h"
20 #include "LayoutFile.h"
21 #include "LayoutModuleList.h"
22 #include "ModuleList.h"
23 #include "TextClass.h"
24
25 #include "support/convert.h"
26 #include "support/debug.h"
27 #include "support/ExceptionMessage.h"
28 #include "support/filetools.h"
29 #include "support/lassert.h"
30 #include "support/lstrings.h"
31 #include "support/Messages.h"
32 #include "support/os.h"
33 #include "support/Package.h"
34 #include "support/Systemcall.h"
35
36 #include <cstdlib>
37 #include <iostream>
38 #include <string>
39 #include <sstream>
40 #include <vector>
41 #include <map>
42
43 using namespace std;
44 using namespace lyx::support;
45 using namespace lyx::support::os;
46
47 namespace lyx {
48
49 namespace frontend {
50 namespace Alert {
51         void warning(docstring const & title, docstring const & message,
52                                  bool const &)
53         {
54                 LYXERR0(title);
55                 LYXERR0(message);
56         }
57 }
58 }
59
60
61 // Dummy texrow support
62 void TexRow::newline()
63 {}
64
65
66 void TexRow::newlines(int)
67 {}
68
69
70 // Dummy LyXRC support
71 class LyXRC {
72 public:
73         string icon_set;
74 } lyxrc;
75
76
77 // Dummy translation support
78 Messages messages_;
79 Messages const & getMessages(std::string const &)
80 {
81         return messages_;
82 }
83
84
85 Messages const & getGuiMessages()
86 {
87         return messages_;
88 }
89
90
91 // Keep the linker happy on Windows
92 void lyx_exit(int)
93 {}
94
95
96 string const trim(string const & a, char const * p)
97 {
98         // LASSERT(p, /**/);
99
100         if (a.empty() || !*p)
101                 return a;
102
103         size_t r = a.find_last_not_of(p);
104         size_t l = a.find_first_not_of(p);
105
106         // Is this the minimal test? (lgb)
107         if (r == string::npos && l == string::npos)
108                 return string();
109
110         return a.substr(l, r - l + 1);
111 }
112
113
114 void split(string const & s, vector<string> & result, char delim)
115 {
116         //cerr << "split 1: '" << s << "'\n";
117         istringstream is(s);
118         string t;
119         while (getline(is, t, delim))
120                 result.push_back(t);
121         //cerr << "split 2\n";
122 }
123
124
125 string join(vector<string> const & input, char const * delim)
126 {
127         ostringstream os;
128         for (size_t i = 0; i != input.size(); ++i) {
129                 if (i)
130                         os << delim;
131                 os << input[i];
132         }
133         return os.str();
134 }
135
136
137 char const * const * is_known(string const & str, char const * const * what)
138 {
139         for ( ; *what; ++what)
140                 if (str == *what)
141                         return what;
142         return 0;
143 }
144
145
146
147 // current stack of nested environments
148 vector<string> active_environments;
149
150
151 string active_environment()
152 {
153         return active_environments.empty() ? string() : active_environments.back();
154 }
155
156
157 TeX2LyXDocClass textclass;
158 CommandMap known_commands;
159 CommandMap known_environments;
160 CommandMap known_math_environments;
161 FullCommandMap possible_textclass_commands;
162 FullEnvironmentMap possible_textclass_environments;
163
164 /// used modules
165 LayoutModuleList used_modules;
166
167
168 void convertArgs(string const & o1, bool o2, vector<ArgumentType> & arguments)
169 {
170         // We have to handle the following cases:
171         // definition                      o1    o2    invocation result
172         // \newcommand{\foo}{bar}          ""    false \foo       bar
173         // \newcommand{\foo}[1]{bar #1}    "[1]" false \foo{x}    bar x
174         // \newcommand{\foo}[1][]{bar #1}  "[1]" true  \foo       bar
175         // \newcommand{\foo}[1][]{bar #1}  "[1]" true  \foo[x]    bar x
176         // \newcommand{\foo}[1][x]{bar #1} "[1]" true  \foo[x]    bar x
177         unsigned int nargs = 0;
178         string const opt1 = rtrim(ltrim(o1, "["), "]");
179         if (isStrUnsignedInt(opt1)) {
180                 // The command has arguments
181                 nargs = convert<unsigned int>(opt1);
182                 if (nargs > 0 && o2) {
183                         // The first argument is optional
184                         arguments.push_back(optional);
185                         --nargs;
186                 }
187         }
188         for (unsigned int i = 0; i < nargs; ++i)
189                 arguments.push_back(required);
190 }
191
192
193 void add_known_command(string const & command, string const & o1,
194                        bool o2, docstring const & definition)
195 {
196         vector<ArgumentType> arguments;
197         convertArgs(o1, o2, arguments);
198         known_commands[command] = arguments;
199         if (!definition.empty())
200                 possible_textclass_commands[command] =
201                         FullCommand(arguments, definition);
202 }
203
204
205 void add_known_environment(string const & environment, string const & o1,
206                            bool o2, docstring const & beg, docstring const &end)
207 {
208         vector<ArgumentType> arguments;
209         convertArgs(o1, o2, arguments);
210         known_environments[environment] = arguments;
211         if (!beg.empty() || ! end.empty())
212                 possible_textclass_environments[environment] =
213                         FullEnvironment(arguments, beg, end);
214 }
215
216
217 Layout const * findLayoutWithoutModule(TextClass const & textclass,
218                                        string const & name, bool command)
219 {
220         DocumentClass::const_iterator it = textclass.begin();
221         DocumentClass::const_iterator en = textclass.end();
222         for (; it != en; ++it) {
223                 if (it->latexname() == name &&
224                     ((command && it->isCommand()) || (!command && it->isEnvironment())))
225                         return &*it;
226         }
227         return 0;
228 }
229
230
231 InsetLayout const * findInsetLayoutWithoutModule(TextClass const & textclass,
232                                                  string const & name, bool command)
233 {
234         DocumentClass::InsetLayouts::const_iterator it = textclass.insetLayouts().begin();
235         DocumentClass::InsetLayouts::const_iterator en = textclass.insetLayouts().end();
236         for (; it != en; ++it) {
237                 if (it->second.latexname() == name &&
238                     ((command && it->second.latextype() == InsetLayout::COMMAND) ||
239                      (!command && it->second.latextype() == InsetLayout::ENVIRONMENT)))
240                         return &(it->second);
241         }
242         return 0;
243 }
244
245
246 bool checkModule(string const & name, bool command)
247 {
248         // Cache to avoid slowdown by repated searches
249         static set<string> failed[2];
250
251         // Only add the module if the command was actually defined in the LyX preamble
252         if (command) {
253                 if (possible_textclass_commands.find('\\' + name) == possible_textclass_commands.end())
254                         return false;
255         } else {
256                 if (possible_textclass_environments.find(name) == possible_textclass_environments.end())
257                         return false;
258         }
259         if (failed[command].find(name) != failed[command].end())
260                 return false;
261
262         // Create list of dummy document classes if not already done.
263         // This is needed since a module cannot be read on its own, only as
264         // part of a document class.
265         LayoutFile const & baseClass = LayoutFileList::get()[textclass.name()];
266         typedef map<string, DocumentClass *> ModuleMap;
267         static ModuleMap modules;
268         static bool init = true;
269         if (init) {
270                 baseClass.load();
271                 DocumentClassBundle & bundle = DocumentClassBundle::get();
272                 LyXModuleList::const_iterator const end = theModuleList.end();
273                 LyXModuleList::const_iterator it = theModuleList.begin();
274                 for (; it != end; it++) {
275                         string const module = it->getID();
276                         LayoutModuleList m;
277                         // FIXME this excludes all modules that depend on another one
278                         if (!m.moduleCanBeAdded(module, &baseClass))
279                                 continue;
280                         m.push_back(module);
281                         modules[module] = &bundle.makeDocumentClass(baseClass, m);
282                 }
283                 init = false;
284         }
285
286         // Try to find a module that defines the command.
287         // Only add it if the definition can be found in the preamble of the
288         // style that corresponds to the command. This is a heuristic and
289         // different from the way how we parse the builtin commands of the
290         // text class (in that case we only compare the name), but it is
291         // needed since it is not unlikely that two different modules define a
292         // command with the same name.
293         ModuleMap::iterator const end = modules.end();
294         for (ModuleMap::iterator it = modules.begin(); it != end; it++) {
295                 string const module = it->first;
296                 if (!used_modules.moduleCanBeAdded(module, &baseClass))
297                         continue;
298                 if (findLayoutWithoutModule(textclass, name, command))
299                         continue;
300                 if (findInsetLayoutWithoutModule(textclass, name, command))
301                         continue;
302                 DocumentClass const * c = it->second;
303                 Layout const * layout = findLayoutWithoutModule(*c, name, command);
304                 InsetLayout const * insetlayout = layout ? 0 :
305                         findInsetLayoutWithoutModule(*c, name, command);
306                 docstring preamble;
307                 if (layout)
308                         preamble = layout->preamble();
309                 else if (insetlayout)
310                         preamble = insetlayout->preamble();
311                 if (preamble.empty())
312                         continue;
313                 bool add = false;
314                 if (command) {
315                         FullCommand const & cmd =
316                                 possible_textclass_commands['\\' + name];
317                         if (preamble.find(cmd.def) != docstring::npos)
318                                 add = true;
319                 } else {
320                         FullEnvironment const & env =
321                                 possible_textclass_environments[name];
322                         if (preamble.find(env.beg) != docstring::npos &&
323                             preamble.find(env.end) != docstring::npos)
324                                 add = true;
325                 }
326                 if (add) {
327                         FileName layout_file = libFileSearch("layouts", module, "module");
328                         if (textclass.read(layout_file, TextClass::MODULE)) {
329                                 used_modules.push_back(module);
330                                 // speed up further searches:
331                                 // the module does not need to be checked anymore.
332                                 modules.erase(it);
333                                 return true;
334                         }
335                 }
336         }
337         failed[command].insert(name);
338         return false;
339 }
340
341
342 bool noweb_mode = false;
343 bool pdflatex = false;
344 bool roundtrip = false;
345
346
347 namespace {
348
349
350 /*!
351  * Read one command definition from the syntax file
352  */
353 void read_command(Parser & p, string command, CommandMap & commands)
354 {
355         if (p.next_token().asInput() == "*") {
356                 p.get_token();
357                 command += '*';
358         }
359         vector<ArgumentType> arguments;
360         while (p.next_token().cat() == catBegin ||
361                p.next_token().asInput() == "[") {
362                 if (p.next_token().cat() == catBegin) {
363                         string const arg = p.getArg('{', '}');
364                         if (arg == "translate")
365                                 arguments.push_back(required);
366                         else if (arg == "item")
367                                 arguments.push_back(item);
368                         else
369                                 arguments.push_back(verbatim);
370                 } else {
371                         p.getArg('[', ']');
372                         arguments.push_back(optional);
373                 }
374         }
375         commands[command] = arguments;
376 }
377
378
379 /*!
380  * Read a class of environments from the syntax file
381  */
382 void read_environment(Parser & p, string const & begin,
383                       CommandMap & environments)
384 {
385         string environment;
386         while (p.good()) {
387                 Token const & t = p.get_token();
388                 if (t.cat() == catLetter)
389                         environment += t.asInput();
390                 else if (!environment.empty()) {
391                         p.putback();
392                         read_command(p, environment, environments);
393                         environment.erase();
394                 }
395                 if (t.cat() == catEscape && t.asInput() == "\\end") {
396                         string const end = p.getArg('{', '}');
397                         if (end == begin)
398                                 return;
399                 }
400         }
401 }
402
403
404 /*!
405  * Read a list of TeX commands from a reLyX compatible syntax file.
406  * Since this list is used after all commands that have a LyX counterpart
407  * are handled, it does not matter that the "syntax.default" file
408  * has almost all of them listed. For the same reason the reLyX-specific
409  * reLyXre environment is ignored.
410  */
411 void read_syntaxfile(FileName const & file_name)
412 {
413         ifdocstream is(file_name.toFilesystemEncoding().c_str());
414         if (!is.good()) {
415                 cerr << "Could not open syntax file \"" << file_name
416                      << "\" for reading." << endl;
417                 exit(2);
418         }
419         // We can use our TeX parser, since the syntax of the layout file is
420         // modeled after TeX.
421         // Unknown tokens are just silently ignored, this helps us to skip some
422         // reLyX specific things.
423         Parser p(is);
424         while (p.good()) {
425                 Token const & t = p.get_token();
426                 if (t.cat() == catEscape) {
427                         string const command = t.asInput();
428                         if (command == "\\begin") {
429                                 string const name = p.getArg('{', '}');
430                                 if (name == "environments" || name == "reLyXre")
431                                         // We understand "reLyXre", but it is
432                                         // not as powerful as "environments".
433                                         read_environment(p, name,
434                                                 known_environments);
435                                 else if (name == "mathenvironments")
436                                         read_environment(p, name,
437                                                 known_math_environments);
438                         } else {
439                                 read_command(p, command, known_commands);
440                         }
441                 }
442         }
443 }
444
445
446 string documentclass;
447 string default_encoding;
448 string syntaxfile;
449 bool overwrite_files = false;
450 int error_code = 0;
451
452 /// return the number of arguments consumed
453 typedef int (*cmd_helper)(string const &, string const &);
454
455
456 int parse_help(string const &, string const &)
457 {
458         cerr << "Usage: tex2lyx [options] infile.tex [outfile.lyx]\n"
459                 "Options:\n"
460                 "\t-c textclass       Declare the textclass.\n"
461                 "\t-e encoding        Set the default encoding (latex name).\n"
462                 "\t-f                 Force overwrite of .lyx files.\n"
463                 "\t-help              Print this message and quit.\n"
464                 "\t-n                 translate a noweb (aka literate programming) file.\n"
465                 "\t-roundtrip         re-export created .lyx file infile.lyx.lyx to infile.lyx.tex.\n"
466                 "\t-s syntaxfile      read additional syntax file.\n"
467                 "\t-sysdir dir        Set system directory to DIR.\n"
468                 "\t-userdir DIR       Set user directory to DIR."
469              << endl;
470         exit(error_code);
471 }
472
473
474 void error_message(string const & message)
475 {
476         cerr << "tex2lyx: " << message << "\n\n";
477         error_code = 1;
478         parse_help(string(), string());
479 }
480
481
482 int parse_class(string const & arg, string const &)
483 {
484         if (arg.empty())
485                 error_message("Missing textclass string after -c switch");
486         documentclass = arg;
487         return 1;
488 }
489
490
491 int parse_encoding(string const & arg, string const &)
492 {
493         if (arg.empty())
494                 error_message("Missing encoding string after -e switch");
495         default_encoding = arg;
496         return 1;
497 }
498
499
500 int parse_syntaxfile(string const & arg, string const &)
501 {
502         if (arg.empty())
503                 error_message("Missing syntaxfile string after -s switch");
504         syntaxfile = internal_path(arg);
505         return 1;
506 }
507
508
509 // Filled with the command line arguments "foo" of "-sysdir foo" or
510 // "-userdir foo".
511 string cl_system_support;
512 string cl_user_support;
513
514
515 int parse_sysdir(string const & arg, string const &)
516 {
517         if (arg.empty())
518                 error_message("Missing directory for -sysdir switch");
519         cl_system_support = internal_path(arg);
520         return 1;
521 }
522
523
524 int parse_userdir(string const & arg, string const &)
525 {
526         if (arg.empty())
527                 error_message("Missing directory for -userdir switch");
528         cl_user_support = internal_path(arg);
529         return 1;
530 }
531
532
533 int parse_force(string const &, string const &)
534 {
535         overwrite_files = true;
536         return 0;
537 }
538
539
540 int parse_noweb(string const &, string const &)
541 {
542         noweb_mode = true;
543         return 0;
544 }
545
546
547 int parse_roundtrip(string const &, string const &)
548 {
549         roundtrip = true;
550         return 0;
551 }
552
553
554 void easyParse(int & argc, char * argv[])
555 {
556         map<string, cmd_helper> cmdmap;
557
558         cmdmap["-c"] = parse_class;
559         cmdmap["-e"] = parse_encoding;
560         cmdmap["-f"] = parse_force;
561         cmdmap["-s"] = parse_syntaxfile;
562         cmdmap["-help"] = parse_help;
563         cmdmap["--help"] = parse_help;
564         cmdmap["-n"] = parse_noweb;
565         cmdmap["-sysdir"] = parse_sysdir;
566         cmdmap["-userdir"] = parse_userdir;
567         cmdmap["-roundtrip"] = parse_roundtrip;
568
569         for (int i = 1; i < argc; ++i) {
570                 map<string, cmd_helper>::const_iterator it
571                         = cmdmap.find(argv[i]);
572
573                 // don't complain if not found - may be parsed later
574                 if (it == cmdmap.end()) {
575                         if (argv[i][0] == '-')
576                                 error_message(string("Unknown option `") + argv[i] + "'.");
577                         else
578                                 continue;
579                 }
580
581                 string arg = (i + 1 < argc) ? os::utf8_argv(i + 1) : string();
582                 string arg2 = (i + 2 < argc) ? os::utf8_argv(i + 2) : string();
583
584                 int const remove = 1 + it->second(arg, arg2);
585
586                 // Now, remove used arguments by shifting
587                 // the following ones remove places down.
588                 os::remove_internal_args(i, remove);
589                 argc -= remove;
590                 for (int j = i; j < argc; ++j)
591                         argv[j] = argv[j + remove];
592                 --i;
593         }
594 }
595
596
597 // path of the first parsed file
598 string masterFilePath;
599 // path of the currently parsed file
600 string parentFilePath;
601
602 } // anonymous namespace
603
604
605 string getMasterFilePath()
606 {
607         return masterFilePath;
608 }
609
610 string getParentFilePath()
611 {
612         return parentFilePath;
613 }
614
615
616 namespace {
617
618 /*!
619  *  Reads tex input from \a is and writes lyx output to \a os.
620  *  Uses some common settings for the preamble, so this should only
621  *  be used more than once for included documents.
622  *  Caution: Overwrites the existing preamble settings if the new document
623  *  contains a preamble.
624  *  You must ensure that \p parentFilePath is properly set before calling
625  *  this function!
626  */
627 void tex2lyx(idocstream & is, ostream & os, string const & encoding)
628 {
629         Parser p(is);
630         if (!encoding.empty())
631                 p.setEncoding(encoding);
632         //p.dump();
633
634         ostringstream ps;
635         parse_preamble(p, ps, documentclass, textclass);
636
637         active_environments.push_back("document");
638         Context context(true, textclass);
639         stringstream ss;
640         parse_text(p, ss, FLAG_END, true, context);
641         if (Context::empty)
642                 // Empty document body. LyX needs at least one paragraph.
643                 context.check_layout(ss);
644         context.check_end_layout(ss);
645         ss << "\n\\end_body\n\\end_document\n";
646         active_environments.pop_back();
647
648         // We know the used modules only after parsing the full text
649         ostringstream ms;
650         if (!used_modules.empty()) {
651                 ms << "\\begin_modules\n";
652                 LayoutModuleList::const_iterator const end = used_modules.end();
653                 LayoutModuleList::const_iterator it = used_modules.begin();
654                 for (; it != end; it++)
655                         ms << *it << '\n';
656                 ms << "\\end_modules\n";
657         }
658         os << subst(ps.str(), modules_placeholder, ms.str());
659
660         ss.seekg(0);
661         os << ss.str();
662 #ifdef TEST_PARSER
663         p.reset();
664         ofdocstream parsertest("parsertest.tex");
665         while (p.good())
666                 parsertest << p.get_token().asInput();
667         // <origfile> and parsertest.tex should now have identical content
668 #endif
669 }
670
671
672 /// convert TeX from \p infilename to LyX and write it to \p os
673 bool tex2lyx(FileName const & infilename, ostream & os, string const & encoding)
674 {
675         ifdocstream is;
676         // forbid buffering on this stream
677         is.rdbuf()->pubsetbuf(0,0);
678         is.open(infilename.toFilesystemEncoding().c_str());
679         if (!is.good()) {
680                 cerr << "Could not open input file \"" << infilename
681                      << "\" for reading." << endl;
682                 return false;
683         }
684         string const oldParentFilePath = parentFilePath;
685         parentFilePath = onlyPath(infilename.absFileName());
686         tex2lyx(is, os, encoding);
687         parentFilePath = oldParentFilePath;
688         return true;
689 }
690
691 } // anonymous namespace
692
693
694 bool tex2lyx(string const & infilename, FileName const & outfilename, 
695              string const & encoding)
696 {
697         if (outfilename.isReadableFile()) {
698                 if (overwrite_files) {
699                         cerr << "Overwriting existing file "
700                              << outfilename << endl;
701                 } else {
702                         cerr << "Not overwriting existing file "
703                              << outfilename << endl;
704                         return false;
705                 }
706         } else {
707                 cerr << "Creating file " << outfilename << endl;
708         }
709         ofstream os(outfilename.toFilesystemEncoding().c_str());
710         if (!os.good()) {
711                 cerr << "Could not open output file \"" << outfilename
712                      << "\" for writing." << endl;
713                 return false;
714         }
715 #ifdef FILEDEBUG
716         cerr << "Input file: " << infilename << "\n";
717         cerr << "Output file: " << outfilename << "\n";
718 #endif
719         return tex2lyx(FileName(infilename), os, encoding);
720 }
721
722
723 bool tex2tex(string const & infilename, FileName const & outfilename,
724              string const & encoding)
725 {
726         if (!tex2lyx(infilename, outfilename, encoding))
727                 return false;
728         string command = quoteName(package().lyx_binary().toFilesystemEncoding());
729         if (overwrite_files)
730                 command += " -f main";
731         else
732                 command += " -f none";
733         if (pdflatex)
734                 command += " -e pdflatex ";
735         else
736                 command += " -e latex ";
737         command += quoteName(outfilename.toFilesystemEncoding());
738         Systemcall one;
739         if (one.startscript(Systemcall::Wait, command) == 0)
740                 return true;
741         cerr << "Error: Running '" << command << "' failed." << endl;
742         return false;
743 }
744
745 } // namespace lyx
746
747
748 int main(int argc, char * argv[])
749 {
750         using namespace lyx;
751
752         //setlocale(LC_CTYPE, "");
753
754         lyxerr.setStream(cerr);
755
756         os::init(argc, argv);
757
758         easyParse(argc, argv);
759
760         if (argc <= 1) 
761                 error_message("Not enough arguments.");
762
763         try {
764                 init_package(internal_path(os::utf8_argv(0)),
765                              cl_system_support, cl_user_support,
766                              top_build_dir_is_two_levels_up);
767         } catch (ExceptionMessage const & message) {
768                 cerr << to_utf8(message.title_) << ":\n"
769                      << to_utf8(message.details_) << endl;
770                 if (message.type_ == ErrorException)
771                         return EXIT_FAILURE;
772         }
773
774         // Now every known option is parsed. Look for input and output
775         // file name (the latter is optional).
776         string infilename = internal_path(os::utf8_argv(1));
777         infilename = makeAbsPath(infilename).absFileName();
778
779         string outfilename;
780         if (roundtrip) {
781                 if (argc > 2) {
782                         // Do not allow a user supplied output filename
783                         // (otherwise it could easily happen that LyX would
784                         // overwrite the original .tex file)
785                         cerr << "Error: output filename must not be given in roundtrip mode."
786                              << endl;
787                         return EXIT_FAILURE;
788                 }
789                 outfilename = changeExtension(infilename, ".lyx.lyx");
790         } else if (argc > 2) {
791                 outfilename = internal_path(os::utf8_argv(2));
792                 if (outfilename != "-")
793                         outfilename = makeAbsPath(outfilename).absFileName();
794         } else
795                 outfilename = changeExtension(infilename, ".lyx");
796
797         // Read the syntax tables
798         FileName const system_syntaxfile = libFileSearch("", "syntax.default");
799         if (system_syntaxfile.empty()) {
800                 cerr << "Error: Could not find syntax file \"syntax.default\"." << endl;
801                 return EXIT_FAILURE;
802         }
803         read_syntaxfile(system_syntaxfile);
804         if (!syntaxfile.empty())
805                 read_syntaxfile(makeAbsPath(syntaxfile));
806
807         // Read the encodings table.
808         FileName const symbols_path = libFileSearch(string(), "unicodesymbols");
809         if (symbols_path.empty()) {
810                 cerr << "Error: Could not find file \"unicodesymbols\"." 
811                      << endl;
812                 return EXIT_FAILURE;
813         }
814         FileName const enc_path = libFileSearch(string(), "encodings");
815         if (enc_path.empty()) {
816                 cerr << "Error: Could not find file \"encodings\"." 
817                      << endl;
818                 return EXIT_FAILURE;
819         }
820         encodings.read(enc_path, symbols_path);
821         if (!default_encoding.empty() && !encodings.fromLaTeXName(default_encoding))
822                 error_message("Unknown LaTeX encoding `" + default_encoding + "'");
823
824         // Load the layouts
825         LayoutFileList::get().read();
826         //...and the modules
827         theModuleList.read();
828
829         // The real work now.
830         masterFilePath = onlyPath(infilename);
831         parentFilePath = masterFilePath;
832         if (outfilename == "-") {
833                 if (tex2lyx(FileName(infilename), cout, default_encoding))
834                         return EXIT_SUCCESS;
835         } else if (roundtrip) {
836                 if (tex2tex(infilename, FileName(outfilename), default_encoding))
837                         return EXIT_SUCCESS;
838         } else {
839                 if (tex2lyx(infilename, FileName(outfilename), default_encoding))
840                         return EXIT_SUCCESS;
841         }
842         return EXIT_FAILURE;
843 }
844
845 // }])