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.
21 #include "LayoutFile.h"
22 #include "LayoutModuleList.h"
23 #include "ModuleList.h"
25 #include "TextClass.h"
27 #include "support/convert.h"
28 #include "support/debug.h"
29 #include "support/ExceptionMessage.h"
30 #include "support/filetools.h"
31 #include "support/lassert.h"
32 #include "support/lstrings.h"
33 #include "support/Messages.h"
34 #include "support/os.h"
35 #include "support/Package.h"
36 #include "support/Systemcall.h"
47 using namespace lyx::support;
48 using namespace lyx::support::os;
54 void warning(docstring const & title, docstring const & message,
64 // Dummy texrow support
65 void TexRow::newline()
69 void TexRow::newlines(int)
73 // Dummy LyXRC support
80 // Dummy translation support
82 Messages const & getMessages(std::string const &)
88 Messages const & getGuiMessages()
94 // Keep the linker happy on Windows
99 string const trimSpaceAndEol(string const & a)
101 return trim(a, " \t\n\r");
105 void split(string const & s, vector<string> & result, char delim)
107 //cerr << "split 1: '" << s << "'\n";
110 while (getline(is, t, delim))
112 //cerr << "split 2\n";
116 string join(vector<string> const & input, char const * delim)
119 for (size_t i = 0; i != input.size(); ++i) {
128 char const * const * is_known(string const & str, char const * const * what)
130 for ( ; *what; ++what)
138 // current stack of nested environments
139 vector<string> active_environments;
142 string active_environment()
144 return active_environments.empty() ? string() : active_environments.back();
148 TeX2LyXDocClass textclass;
149 CommandMap known_commands;
150 CommandMap known_environments;
151 CommandMap known_math_environments;
152 FullCommandMap possible_textclass_commands;
153 FullEnvironmentMap possible_textclass_environments;
154 FullCommandMap possible_textclass_theorems;
155 int const LYX_FORMAT = LYX_FORMAT_TEX2LYX;
158 LayoutModuleList used_modules;
159 vector<string> preloaded_modules;
162 void convertArgs(string const & o1, bool o2, vector<ArgumentType> & arguments)
164 // We have to handle the following cases:
165 // definition o1 o2 invocation result
166 // \newcommand{\foo}{bar} "" false \foo bar
167 // \newcommand{\foo}[1]{bar #1} "[1]" false \foo{x} bar x
168 // \newcommand{\foo}[1][]{bar #1} "[1]" true \foo bar
169 // \newcommand{\foo}[1][]{bar #1} "[1]" true \foo[x] bar x
170 // \newcommand{\foo}[1][x]{bar #1} "[1]" true \foo[x] bar x
171 unsigned int nargs = 0;
172 string const opt1 = rtrim(ltrim(o1, "["), "]");
173 if (isStrUnsignedInt(opt1)) {
174 // The command has arguments
175 nargs = convert<unsigned int>(opt1);
176 if (nargs > 0 && o2) {
177 // The first argument is optional
178 arguments.push_back(optional);
182 for (unsigned int i = 0; i < nargs; ++i)
183 arguments.push_back(required);
187 void add_known_command(string const & command, string const & o1,
188 bool o2, docstring const & definition)
190 vector<ArgumentType> arguments;
191 convertArgs(o1, o2, arguments);
192 known_commands[command] = arguments;
193 if (!definition.empty())
194 possible_textclass_commands[command] =
195 FullCommand(arguments, definition);
199 void add_known_environment(string const & environment, string const & o1,
200 bool o2, docstring const & beg, docstring const &end)
202 vector<ArgumentType> arguments;
203 convertArgs(o1, o2, arguments);
204 known_environments[environment] = arguments;
205 if (!beg.empty() || ! end.empty())
206 possible_textclass_environments[environment] =
207 FullEnvironment(arguments, beg, end);
211 void add_known_theorem(string const & theorem, string const & o1,
212 bool o2, docstring const & definition)
214 vector<ArgumentType> arguments;
215 convertArgs(o1, o2, arguments);
216 if (!definition.empty())
217 possible_textclass_theorems[theorem] =
218 FullCommand(arguments, definition);
222 Layout const * findLayoutWithoutModule(TextClass const & textclass,
223 string const & name, bool command)
225 DocumentClass::const_iterator it = textclass.begin();
226 DocumentClass::const_iterator en = textclass.end();
227 for (; it != en; ++it) {
228 if (it->latexname() == name &&
229 ((command && it->isCommand()) || (!command && it->isEnvironment())))
236 InsetLayout const * findInsetLayoutWithoutModule(TextClass const & textclass,
237 string const & name, bool command)
239 DocumentClass::InsetLayouts::const_iterator it = textclass.insetLayouts().begin();
240 DocumentClass::InsetLayouts::const_iterator en = textclass.insetLayouts().end();
241 for (; it != en; ++it) {
242 if (it->second.latexname() == name &&
243 ((command && it->second.latextype() == InsetLayout::COMMAND) ||
244 (!command && it->second.latextype() == InsetLayout::ENVIRONMENT)))
245 return &(it->second);
253 typedef map<string, DocumentClassPtr> ModuleMap;
257 bool addModule(string const module, LayoutFile const & baseClass, LayoutModuleList & m, vector<string> & visited)
259 // avoid endless loop for circular dependency
260 vector<string>::const_iterator const vb = visited.begin();
261 vector<string>::const_iterator const ve = visited.end();
262 if (find(vb, ve, module) != ve) {
263 cerr << "Circular dependency detected for module " << module << '\n';
266 LyXModule const * const lm = theModuleList[module];
268 cerr << "Could not find module " << module << " in module list.\n";
271 bool foundone = false;
272 LayoutModuleList::const_iterator const exclmodstart = baseClass.excludedModules().begin();
273 LayoutModuleList::const_iterator const exclmodend = baseClass.excludedModules().end();
274 LayoutModuleList::const_iterator const provmodstart = baseClass.providedModules().begin();
275 LayoutModuleList::const_iterator const provmodend = baseClass.providedModules().end();
276 vector<string> const reqs = lm->getRequiredModules();
280 LayoutModuleList::const_iterator mit = m.begin();
281 LayoutModuleList::const_iterator men = m.end();
282 vector<string>::const_iterator rit = reqs.begin();
283 vector<string>::const_iterator ren = reqs.end();
284 for (; rit != ren; ++rit) {
285 if (find(mit, men, *rit) != men) {
289 if (find(provmodstart, provmodend, *rit) != provmodend) {
295 visited.push_back(module);
296 for (rit = reqs.begin(); rit != ren; ++rit) {
297 if (find(exclmodstart, exclmodend, *rit) == exclmodend) {
298 if (addModule(*rit, baseClass, m, visited)) {
308 cerr << "Could not add required modules for " << module << ".\n";
311 if (!m.moduleCanBeAdded(module, &baseClass))
320 // Create list of dummy document classes if not already done.
321 // This is needed since a module cannot be read on its own, only as
322 // part of a document class.
323 LayoutFile const & baseClass = LayoutFileList::get()[textclass.name()];
324 static bool init = true;
327 LyXModuleList::const_iterator const end = theModuleList.end();
328 LyXModuleList::const_iterator it = theModuleList.begin();
329 for (; it != end; ++it) {
330 string const module = it->getID();
333 if (!addModule(module, baseClass, m, v))
335 modules[module] = getDocumentClass(baseClass, m);
342 bool addModule(string const & module)
345 LayoutFile const & baseClass = LayoutFileList::get()[textclass.name()];
346 if (!used_modules.moduleCanBeAdded(module, &baseClass))
348 FileName layout_file = libFileSearch("layouts", module, "module");
349 if (textclass.read(layout_file, TextClass::MODULE)) {
350 used_modules.push_back(module);
351 // speed up further searches:
352 // the module does not need to be checked anymore.
353 ModuleMap::iterator const it = modules.find(module);
354 if (it != modules.end())
364 bool checkModule(string const & name, bool command)
366 // Cache to avoid slowdown by repated searches
367 static set<string> failed[2];
369 // Only add the module if the command was actually defined in the LyX preamble
370 bool theorem = false;
372 if (possible_textclass_commands.find('\\' + name) == possible_textclass_commands.end())
375 if (possible_textclass_environments.find(name) == possible_textclass_environments.end()) {
376 if (possible_textclass_theorems.find(name) != possible_textclass_theorems.end())
382 if (failed[command].find(name) != failed[command].end())
386 LayoutFile const & baseClass = LayoutFileList::get()[textclass.name()];
388 // Try to find a module that defines the command.
389 // Only add it if the definition can be found in the preamble of the
390 // style that corresponds to the command. This is a heuristic and
391 // different from the way how we parse the builtin commands of the
392 // text class (in that case we only compare the name), but it is
393 // needed since it is not unlikely that two different modules define a
394 // command with the same name.
395 ModuleMap::iterator const end = modules.end();
396 for (ModuleMap::iterator it = modules.begin(); it != end; ++it) {
397 string const module = it->first;
398 if (used_modules.moduleConflicts(module, &baseClass))
400 if (findLayoutWithoutModule(textclass, name, command))
402 if (findInsetLayoutWithoutModule(textclass, name, command))
404 DocumentClassConstPtr c = it->second;
405 Layout const * layout = findLayoutWithoutModule(*c, name, command);
406 InsetLayout const * insetlayout = layout ? 0 :
407 findInsetLayoutWithoutModule(*c, name, command);
410 preamble = layout->preamble();
411 else if (insetlayout)
412 preamble = insetlayout->preamble();
413 if (preamble.empty())
417 FullCommand const & cmd =
418 possible_textclass_commands['\\' + name];
419 if (preamble.find(cmd.def) != docstring::npos)
421 } else if (theorem) {
422 FullCommand const & thm =
423 possible_textclass_theorems[name];
424 if (preamble.find(thm.def) != docstring::npos)
427 FullEnvironment const & env =
428 possible_textclass_environments[name];
429 if (preamble.find(env.beg) != docstring::npos &&
430 preamble.find(env.end) != docstring::npos)
434 FileName layout_file = libFileSearch("layouts", module, "module");
435 if (textclass.read(layout_file, TextClass::MODULE)) {
436 used_modules.push_back(module);
437 // speed up further searches:
438 // the module does not need to be checked anymore.
444 failed[command].insert(name);
449 bool noweb_mode = false;
450 bool pdflatex = false;
452 bool is_nonCJKJapanese = false;
453 bool roundtrip = false;
460 * Read one command definition from the syntax file
462 void read_command(Parser & p, string command, CommandMap & commands)
464 if (p.next_token().asInput() == "*") {
468 vector<ArgumentType> arguments;
469 while (p.next_token().cat() == catBegin ||
470 p.next_token().asInput() == "[") {
471 if (p.next_token().cat() == catBegin) {
472 string const arg = p.getArg('{', '}');
473 if (arg == "translate")
474 arguments.push_back(required);
475 else if (arg == "group")
476 arguments.push_back(req_group);
477 else if (arg == "item")
478 arguments.push_back(item);
479 else if (arg == "displaymath")
480 arguments.push_back(displaymath);
482 arguments.push_back(verbatim);
484 string const arg = p.getArg('[', ']');
486 arguments.push_back(opt_group);
488 arguments.push_back(optional);
491 commands[command] = arguments;
496 * Read a class of environments from the syntax file
498 void read_environment(Parser & p, string const & begin,
499 CommandMap & environments)
503 Token const & t = p.get_token();
504 if (t.cat() == catLetter)
505 environment += t.asInput();
506 else if (!environment.empty()) {
508 read_command(p, environment, environments);
511 if (t.cat() == catEscape && t.asInput() == "\\end") {
512 string const end = p.getArg('{', '}');
521 * Read a list of TeX commands from a reLyX compatible syntax file.
522 * Since this list is used after all commands that have a LyX counterpart
523 * are handled, it does not matter that the "syntax.default" file
524 * has almost all of them listed. For the same reason the reLyX-specific
525 * reLyXre environment is ignored.
527 void read_syntaxfile(FileName const & file_name)
529 ifdocstream is(file_name.toFilesystemEncoding().c_str());
531 cerr << "Could not open syntax file \"" << file_name
532 << "\" for reading." << endl;
535 // We can use our TeX parser, since the syntax of the layout file is
536 // modeled after TeX.
537 // Unknown tokens are just silently ignored, this helps us to skip some
538 // reLyX specific things.
541 Token const & t = p.get_token();
542 if (t.cat() == catEscape) {
543 string const command = t.asInput();
544 if (command == "\\begin") {
545 string const name = p.getArg('{', '}');
546 if (name == "environments" || name == "reLyXre")
547 // We understand "reLyXre", but it is
548 // not as powerful as "environments".
549 read_environment(p, name,
551 else if (name == "mathenvironments")
552 read_environment(p, name,
553 known_math_environments);
555 read_command(p, command, known_commands);
562 string documentclass;
563 string default_encoding;
565 bool copy_files = false;
566 bool overwrite_files = false;
567 bool skip_children = false;
570 /// return the number of arguments consumed
571 typedef int (*cmd_helper)(string const &, string const &);
574 int parse_help(string const &, string const &)
576 cerr << "Usage: tex2lyx [options] infile.tex [outfile.lyx]\n"
578 "\t-c textclass Declare the textclass.\n"
579 "\t-m mod1[,mod2...] Load the given modules.\n"
580 "\t-copyfiles Copy all included files to the directory of outfile.lyx.\n"
581 "\t-e encoding Set the default encoding (latex name).\n"
582 "\t-f Force overwrite of .lyx files.\n"
583 "\t-help Print this message and quit.\n"
584 "\t-n translate a noweb (aka literate programming) file.\n"
585 "\t-skipchildren Do not translate included child documents.\n"
586 "\t-roundtrip re-export created .lyx file infile.lyx.lyx to infile.lyx.tex.\n"
587 "\t-s syntaxfile read additional syntax file.\n"
588 "\t-sysdir SYSDIR Set system directory to SYSDIR.\n"
589 "\t Default: " << package().system_support() << "\n"
590 "\t-userdir USERDIR Set user directory to USERDIR.\n"
591 "\t Default: " << package().user_support() << "\n"
592 "\t-version Summarize version and build info.\n"
594 "\tThe program searches for the files \"encodings\", \"lyxmodules.lst\",\n"
595 "\t\"textclass.lst\", \"syntax.default\", and \"unicodesymbols\", first in\n"
596 "\t\"USERDIR\", then in \"SYSDIR\". The subdirectories \"USERDIR/layouts\"\n"
597 "\tand \"SYSDIR/layouts\" are searched for layout and module files.\n"
598 "Check the tex2lyx man page for more details."
604 int parse_version(string const &, string const &)
606 lyxerr << "tex2lyx " << lyx_version
607 << " (" << lyx_release_date << ")" << endl;
608 lyxerr << "Built on " << __DATE__ << ", " << __TIME__ << endl;
610 lyxerr << lyx_version_info << endl;
615 void error_message(string const & message)
617 cerr << "tex2lyx: " << message << "\n\n";
619 parse_help(string(), string());
623 int parse_class(string const & arg, string const &)
626 error_message("Missing textclass string after -c switch");
632 int parse_module(string const & arg, string const &)
635 error_message("Missing modules string after -m switch");
636 split(arg, preloaded_modules, ',');
641 int parse_encoding(string const & arg, string const &)
644 error_message("Missing encoding string after -e switch");
645 default_encoding = arg;
650 int parse_syntaxfile(string const & arg, string const &)
653 error_message("Missing syntaxfile string after -s switch");
654 syntaxfile = internal_path(arg);
659 // Filled with the command line arguments "foo" of "-sysdir foo" or
661 string cl_system_support;
662 string cl_user_support;
665 int parse_sysdir(string const & arg, string const &)
668 error_message("Missing directory for -sysdir switch");
669 cl_system_support = internal_path(arg);
674 int parse_userdir(string const & arg, string const &)
677 error_message("Missing directory for -userdir switch");
678 cl_user_support = internal_path(arg);
683 int parse_force(string const &, string const &)
685 overwrite_files = true;
690 int parse_noweb(string const &, string const &)
697 int parse_skipchildren(string const &, string const &)
699 skip_children = true;
704 int parse_roundtrip(string const &, string const &)
711 int parse_copyfiles(string const &, string const &)
718 void easyParse(int & argc, char * argv[])
720 map<string, cmd_helper> cmdmap;
722 cmdmap["-h"] = parse_help;
723 cmdmap["-help"] = parse_help;
724 cmdmap["--help"] = parse_help;
725 cmdmap["-v"] = parse_version;
726 cmdmap["-version"] = parse_version;
727 cmdmap["--version"] = parse_version;
728 cmdmap["-c"] = parse_class;
729 cmdmap["-m"] = parse_module;
730 cmdmap["-e"] = parse_encoding;
731 cmdmap["-f"] = parse_force;
732 cmdmap["-s"] = parse_syntaxfile;
733 cmdmap["-n"] = parse_noweb;
734 cmdmap["-skipchildren"] = parse_skipchildren;
735 cmdmap["-sysdir"] = parse_sysdir;
736 cmdmap["-userdir"] = parse_userdir;
737 cmdmap["-roundtrip"] = parse_roundtrip;
738 cmdmap["-copyfiles"] = parse_copyfiles;
740 for (int i = 1; i < argc; ++i) {
741 map<string, cmd_helper>::const_iterator it
742 = cmdmap.find(argv[i]);
744 // don't complain if not found - may be parsed later
745 if (it == cmdmap.end()) {
746 if (argv[i][0] == '-')
747 error_message(string("Unknown option `") + argv[i] + "'.");
752 string arg = (i + 1 < argc) ? os::utf8_argv(i + 1) : string();
753 string arg2 = (i + 2 < argc) ? os::utf8_argv(i + 2) : string();
755 int const remove = 1 + it->second(arg, arg2);
757 // Now, remove used arguments by shifting
758 // the following ones remove places down.
759 os::remove_internal_args(i, remove);
761 for (int j = i; j < argc; ++j)
762 argv[j] = argv[j + remove];
768 // path of the first parsed file
769 string masterFilePathLyX;
770 string masterFilePathTeX;
771 // path of the currently parsed file
772 string parentFilePathTeX;
774 } // anonymous namespace
777 string getMasterFilePath(bool input)
779 return input ? masterFilePathTeX : masterFilePathLyX;
782 string getParentFilePath(bool input)
785 return parentFilePathTeX;
786 string const rel = to_utf8(makeRelPath(from_utf8(masterFilePathTeX),
787 from_utf8(parentFilePathTeX)));
788 if (rel.substr(0, 3) == "../") {
789 // The parent is not below the master - keep the path
790 return parentFilePathTeX;
792 return makeAbsPath(rel, masterFilePathLyX).absFileName();
802 bool overwriteFiles()
804 return overwrite_files;
810 return skip_children;
817 * Reads tex input from \a is and writes lyx output to \a os.
818 * Uses some common settings for the preamble, so this should only
819 * be used more than once for included documents.
820 * Caution: Overwrites the existing preamble settings if the new document
821 * contains a preamble.
822 * You must ensure that \p parentFilePathTeX is properly set before calling
825 bool tex2lyx(idocstream & is, ostream & os, string encoding)
827 // Set a sensible default encoding.
828 // This is used until an encoding command is found.
829 // For child documents use the encoding of the master, else latin1,
830 // since latin1 does not cause an iconv error if the actual encoding
831 // is different (bug 7509).
832 if (encoding.empty()) {
833 if (preamble.inputencoding() == "auto")
836 encoding = preamble.inputencoding();
840 p.setEncoding(encoding);
843 preamble.parse(p, documentclass, textclass);
844 list<string> removed_modules;
845 LayoutFile const & baseClass = LayoutFileList::get()[textclass.name()];
846 if (!used_modules.adaptToBaseClass(&baseClass, removed_modules)) {
847 cerr << "Could not load default modules for text class." << endl;
851 // Load preloaded modules.
852 // This needs to be done after the preamble is parsed, since the text
853 // class may not be known before. It neds to be done before parsing
854 // body, since otherwise the commands/environments provided by the
855 // modules would be parsed as ERT.
856 for (size_t i = 0; i < preloaded_modules.size(); ++i) {
857 if (!addModule(preloaded_modules[i])) {
858 cerr << "Error: Could not load module \""
859 << preloaded_modules[i] << "\"." << endl;
863 // Ensure that the modules are not loaded again for included files
864 preloaded_modules.clear();
866 active_environments.push_back("document");
867 Context context(true, textclass);
869 // store the document language in the context to be able to handle the
870 // commands like \foreignlanguage and \textenglish etc.
871 context.font.language = preamble.defaultLanguage();
872 // parse the main text
873 parse_text(p, ss, FLAG_END, true, context);
875 // Empty document body. LyX needs at least one paragraph.
876 context.check_layout(ss);
877 context.check_end_layout(ss);
878 ss << "\n\\end_body\n\\end_document\n";
879 active_environments.pop_back();
881 // We know the used modules only after parsing the full text
882 if (!used_modules.empty()) {
883 LayoutModuleList::const_iterator const end = used_modules.end();
884 LayoutModuleList::const_iterator it = used_modules.begin();
885 for (; it != end; ++it)
886 preamble.addModule(*it);
888 if (!preamble.writeLyXHeader(os, !active_environments.empty())) {
889 cerr << "Could not write LyX file header." << endl;
897 ofdocstream parsertest("parsertest.tex");
899 parsertest << p.get_token().asInput();
900 // <origfile> and parsertest.tex should now have identical content
906 /// convert TeX from \p infilename to LyX and write it to \p os
907 bool tex2lyx(FileName const & infilename, ostream & os, string const & encoding)
910 // forbid buffering on this stream
911 is.rdbuf()->pubsetbuf(0,0);
912 is.open(infilename.toFilesystemEncoding().c_str());
914 cerr << "Could not open input file \"" << infilename
915 << "\" for reading." << endl;
918 string const oldParentFilePath = parentFilePathTeX;
919 parentFilePathTeX = onlyPath(infilename.absFileName());
920 bool retval = tex2lyx(is, os, encoding);
921 parentFilePathTeX = oldParentFilePath;
925 } // anonymous namespace
928 bool tex2lyx(string const & infilename, FileName const & outfilename,
929 string const & encoding)
931 if (outfilename.isReadableFile()) {
932 if (overwrite_files) {
933 cerr << "Overwriting existing file "
934 << outfilename << endl;
936 cerr << "Not overwriting existing file "
937 << outfilename << endl;
941 cerr << "Creating file " << outfilename << endl;
943 ofstream os(outfilename.toFilesystemEncoding().c_str());
945 cerr << "Could not open output file \"" << outfilename
946 << "\" for writing." << endl;
950 cerr << "Input file: " << infilename << "\n";
951 cerr << "Output file: " << outfilename << "\n";
953 return tex2lyx(FileName(infilename), os, encoding);
957 bool tex2tex(string const & infilename, FileName const & outfilename,
958 string const & encoding)
960 if (!tex2lyx(infilename, outfilename, encoding))
962 string command = quoteName(package().lyx_binary().toFilesystemEncoding());
964 command += " -f main";
966 command += " -f none";
968 command += " -e pdflatex ";
970 command += " -e xetex ";
972 command += " -e latex ";
973 command += quoteName(outfilename.toFilesystemEncoding());
975 if (one.startscript(Systemcall::Wait, command) == 0)
977 cerr << "Error: Running '" << command << "' failed." << endl;
984 int main(int argc, char * argv[])
988 //setlocale(LC_CTYPE, "");
990 lyxerr.setStream(cerr);
992 os::init(argc, argv);
995 init_package(internal_path(os::utf8_argv(0)), string(), string());
996 } catch (ExceptionMessage const & message) {
997 cerr << to_utf8(message.title_) << ":\n"
998 << to_utf8(message.details_) << endl;
999 if (message.type_ == ErrorException)
1000 return EXIT_FAILURE;
1003 easyParse(argc, argv);
1006 error_message("Not enough arguments.");
1009 init_package(internal_path(os::utf8_argv(0)),
1010 cl_system_support, cl_user_support);
1011 } catch (ExceptionMessage const & message) {
1012 cerr << to_utf8(message.title_) << ":\n"
1013 << to_utf8(message.details_) << endl;
1014 if (message.type_ == ErrorException)
1015 return EXIT_FAILURE;
1018 // Now every known option is parsed. Look for input and output
1019 // file name (the latter is optional).
1020 string infilename = internal_path(os::utf8_argv(1));
1021 infilename = makeAbsPath(infilename).absFileName();
1025 outfilename = internal_path(os::utf8_argv(2));
1026 if (outfilename != "-")
1027 outfilename = makeAbsPath(outfilename).absFileName();
1029 if (outfilename == "-") {
1030 cerr << "Error: Writing to standard output is "
1031 "not supported in roundtrip mode."
1033 return EXIT_FAILURE;
1035 string texfilename = changeExtension(outfilename, ".tex");
1036 if (equivalent(FileName(infilename), FileName(texfilename))) {
1037 cerr << "Error: The input file `" << infilename
1038 << "´ would be overwritten by the TeX file exported from `"
1039 << outfilename << "´ in roundtrip mode." << endl;
1040 return EXIT_FAILURE;
1043 } else if (roundtrip) {
1044 // avoid overwriting the input file
1045 outfilename = changeExtension(infilename, ".lyx.lyx");
1047 outfilename = changeExtension(infilename, ".lyx");
1049 // Read the syntax tables
1050 FileName const system_syntaxfile = libFileSearch("", "syntax.default");
1051 if (system_syntaxfile.empty()) {
1052 cerr << "Error: Could not find syntax file \"syntax.default\"." << endl;
1053 return EXIT_FAILURE;
1055 read_syntaxfile(system_syntaxfile);
1056 if (!syntaxfile.empty())
1057 read_syntaxfile(makeAbsPath(syntaxfile));
1059 // Read the encodings table.
1060 FileName const symbols_path = libFileSearch(string(), "unicodesymbols");
1061 if (symbols_path.empty()) {
1062 cerr << "Error: Could not find file \"unicodesymbols\"."
1064 return EXIT_FAILURE;
1066 FileName const enc_path = libFileSearch(string(), "encodings");
1067 if (enc_path.empty()) {
1068 cerr << "Error: Could not find file \"encodings\"."
1070 return EXIT_FAILURE;
1072 encodings.read(enc_path, symbols_path);
1073 if (!default_encoding.empty() && !encodings.fromLaTeXName(default_encoding))
1074 error_message("Unknown LaTeX encoding `" + default_encoding + "'");
1077 LayoutFileList::get().read();
1078 //...and the modules
1079 theModuleList.read();
1081 // The real work now.
1082 masterFilePathTeX = onlyPath(infilename);
1083 parentFilePathTeX = masterFilePathTeX;
1084 if (outfilename == "-") {
1085 // assume same directory as input file
1086 masterFilePathLyX = masterFilePathTeX;
1087 if (tex2lyx(FileName(infilename), cout, default_encoding))
1088 return EXIT_SUCCESS;
1090 masterFilePathLyX = onlyPath(outfilename);
1092 FileName const path(masterFilePathLyX);
1093 if (!path.isDirectory()) {
1094 if (!path.createPath()) {
1095 cerr << "Warning: Could not create directory for file `"
1096 << masterFilePathLyX << "´." << endl;
1097 return EXIT_FAILURE;
1102 if (tex2tex(infilename, FileName(outfilename), default_encoding))
1103 return EXIT_SUCCESS;
1105 if (tex2lyx(infilename, FileName(outfilename), default_encoding))
1106 return EXIT_SUCCESS;
1109 return EXIT_FAILURE;