* LaTeX.cpp (run):
[lyx.git] / src / LaTeX.cpp
blob8a74b61ea01a8b722fa724ada1134220131d4a8f
1 /**
2 * \file LaTeX.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Alfredo Braunstein
7 * \author Lars Gullik Bjønnes
8 * \author Jean-Marc Lasgouttes
9 * \author Angus Leeming
10 * \author Dekel Tsur
11 * \author Jürgen Spitzmüller
13 * Full author contact details are available in file CREDITS.
16 #include <config.h>
18 #include "BufferList.h"
19 #include "LaTeX.h"
20 #include "LyXRC.h"
21 #include "DepTable.h"
23 #include "support/debug.h"
24 #include "support/convert.h"
25 #include "support/FileName.h"
26 #include "support/filetools.h"
27 #include "support/gettext.h"
28 #include "support/lstrings.h"
29 #include "support/Systemcall.h"
30 #include "support/os.h"
32 #include <boost/regex.hpp>
34 #include <fstream>
36 using boost::regex;
37 using boost::smatch;
39 using namespace std;
40 using namespace lyx::support;
42 namespace lyx {
44 namespace os = support::os;
46 // TODO: in no particular order
47 // - get rid of the call to
48 // BufferList::updateIncludedTeXfiles, this should either
49 // be done before calling LaTeX::funcs or in a completely
50 // different way.
51 // - the makeindex style files should be taken care of with
52 // the dependency mechanism.
53 // - makeindex commandline options should be supported
54 // - somewhere support viewing of bibtex and makeindex log files.
55 // - we should perhaps also scan the bibtex log file
57 namespace {
59 docstring runMessage(unsigned int count)
61 return bformat(_("Waiting for LaTeX run number %1$d"), count);
64 } // anon namespace
67 * CLASS TEXERRORS
70 void TeXErrors::insertError(int line, docstring const & error_desc,
71 docstring const & error_text)
73 Error newerr(line, error_desc, error_text);
74 errors.push_back(newerr);
78 bool operator==(AuxInfo const & a, AuxInfo const & o)
80 return a.aux_file == o.aux_file
81 && a.citations == o.citations
82 && a.databases == o.databases
83 && a.styles == o.styles;
87 bool operator!=(AuxInfo const & a, AuxInfo const & o)
89 return !(a == o);
94 * CLASS LaTeX
97 LaTeX::LaTeX(string const & latex, OutputParams const & rp,
98 FileName const & f)
99 : cmd(latex), file(f), runparams(rp)
101 num_errors = 0;
102 if (prefixIs(cmd, "pdf")) { // Do we use pdflatex ?
103 depfile = FileName(file.absFilename() + ".dep-pdf");
104 output_file =
105 FileName(changeExtension(file.absFilename(), ".pdf"));
106 } else {
107 depfile = FileName(file.absFilename() + ".dep");
108 output_file =
109 FileName(changeExtension(file.absFilename(), ".dvi"));
114 void LaTeX::deleteFilesOnError() const
116 // currently just a dummy function.
118 // What files do we have to delete?
120 // This will at least make latex do all the runs
121 depfile.removeFile();
123 // but the reason for the error might be in a generated file...
125 // bibtex file
126 FileName const bbl(changeExtension(file.absFilename(), ".bbl"));
127 bbl.removeFile();
129 // makeindex file
130 FileName const ind(changeExtension(file.absFilename(), ".ind"));
131 ind.removeFile();
133 // nomencl file
134 FileName const nls(changeExtension(file.absFilename(), ".nls"));
135 nls.removeFile();
137 // nomencl file (old version of the package)
138 FileName const gls(changeExtension(file.absFilename(), ".gls"));
139 gls.removeFile();
141 // Also remove the aux file
142 FileName const aux(changeExtension(file.absFilename(), ".aux"));
143 aux.removeFile();
147 int LaTeX::run(TeXErrors & terr)
148 // We know that this function will only be run if the lyx buffer
149 // has been changed. We also know that a newly written .tex file
150 // is always different from the previous one because of the date
151 // in it. However it seems safe to run latex (at least) on time
152 // each time the .tex file changes.
154 int scanres = NO_ERRORS;
155 unsigned int count = 0; // number of times run
156 num_errors = 0; // just to make sure.
157 unsigned int const MAX_RUN = 6;
158 DepTable head; // empty head
159 bool rerun = false; // rerun requested
161 // The class LaTeX does not know the temp path.
162 theBufferList().updateIncludedTeXfiles(FileName::getcwd().absFilename(),
163 runparams);
165 // Never write the depfile if an error was encountered.
167 // 0
168 // first check if the file dependencies exist:
169 // ->If it does exist
170 // check if any of the files mentioned in it have
171 // changed (done using a checksum).
172 // -> if changed:
173 // run latex once and
174 // remake the dependency file
175 // -> if not changed:
176 // just return there is nothing to do for us.
177 // ->if it doesn't exist
178 // make it and
179 // run latex once (we need to run latex once anyway) and
180 // remake the dependency file.
183 bool had_depfile = depfile.exists();
184 bool run_bibtex = false;
185 FileName const aux_file(changeExtension(file.absFilename(), "aux"));
187 if (had_depfile) {
188 LYXERR(Debug::DEPEND, "Dependency file exists");
189 // Read the dep file:
190 had_depfile = head.read(depfile);
193 if (had_depfile) {
194 // Update the checksums
195 head.update();
196 // Can't just check if anything has changed because it might
197 // have aborted on error last time... in which cas we need
198 // to re-run latex and collect the error messages
199 // (even if they are the same).
200 if (!output_file.exists()) {
201 LYXERR(Debug::DEPEND,
202 "re-running LaTeX because output file doesn't exist.");
203 } else if (!head.sumchange()) {
204 LYXERR(Debug::DEPEND, "return no_change");
205 return NO_CHANGE;
206 } else {
207 LYXERR(Debug::DEPEND, "Dependency file has changed");
210 if (head.extchanged(".bib") || head.extchanged(".bst"))
211 run_bibtex = true;
212 } else
213 LYXERR(Debug::DEPEND,
214 "Dependency file does not exist, or has wrong format");
216 /// We scan the aux file even when had_depfile = false,
217 /// because we can run pdflatex on the file after running latex on it,
218 /// in which case we will not need to run bibtex again.
219 vector<AuxInfo> bibtex_info_old;
220 if (!run_bibtex)
221 bibtex_info_old = scanAuxFiles(aux_file);
223 ++count;
224 LYXERR(Debug::LATEX, "Run #" << count);
225 message(runMessage(count));
227 startscript();
228 scanres = scanLogFile(terr);
229 if (scanres & ERROR_RERUN) {
230 LYXERR(Debug::LATEX, "Rerunning LaTeX");
231 startscript();
232 scanres = scanLogFile(terr);
235 if (scanres & ERRORS) {
236 deleteFilesOnError();
237 return scanres; // return on error
240 vector<AuxInfo> const bibtex_info = scanAuxFiles(aux_file);
241 if (!run_bibtex && bibtex_info_old != bibtex_info)
242 run_bibtex = true;
244 // update the dependencies.
245 deplog(head); // reads the latex log
246 head.update();
248 // 1
249 // At this point we must run external programs if needed.
250 // makeindex will be run if a .idx file changed or was generated.
251 // And if there were undefined citations or changes in references
252 // the .aux file is checked for signs of bibtex. Bibtex is then run
253 // if needed.
255 // memoir (at least) writes an empty *idx file in the first place.
256 // A second latex run is needed.
257 FileName const idxfile(changeExtension(file.absFilename(), ".idx"));
258 rerun = idxfile.exists() && idxfile.isFileEmpty();
260 // run makeindex
261 if (head.haschanged(idxfile)) {
262 // no checks for now
263 LYXERR(Debug::LATEX, "Running MakeIndex.");
264 message(_("Running Index Processor."));
265 // onlyFilename() is needed for cygwin
266 rerun |= runMakeIndex(onlyFilename(idxfile.absFilename()),
267 runparams);
269 FileName const nlofile(changeExtension(file.absFilename(), ".nlo"));
270 if (head.haschanged(nlofile))
271 rerun |= runMakeIndexNomencl(file, ".nlo", ".nls");
272 FileName const glofile(changeExtension(file.absFilename(), ".glo"));
273 if (head.haschanged(glofile))
274 rerun |= runMakeIndexNomencl(file, ".glo", ".gls");
276 // run bibtex
277 // if (scanres & UNDEF_CIT || scanres & RERUN || run_bibtex)
278 if (scanres & UNDEF_CIT || run_bibtex) {
279 // Here we must scan the .aux file and look for
280 // "\bibdata" and/or "\bibstyle". If one of those
281 // tags is found -> run bibtex and set rerun = true;
282 // no checks for now
283 LYXERR(Debug::LATEX, "Running BibTeX.");
284 message(_("Running BibTeX."));
285 updateBibtexDependencies(head, bibtex_info);
286 rerun |= runBibTeX(bibtex_info);
287 } else if (!had_depfile) {
288 /// If we run pdflatex on the file after running latex on it,
289 /// then we do not need to run bibtex, but we do need to
290 /// insert the .bib and .bst files into the .dep-pdf file.
291 updateBibtexDependencies(head, bibtex_info);
294 // 2
295 // we know on this point that latex has been run once (or we just
296 // returned) and the question now is to decide if we need to run
297 // it any more. This is done by asking if any of the files in the
298 // dependency file has changed. (remember that the checksum for
299 // a given file is reported to have changed if it just was created)
300 // -> if changed or rerun == true:
301 // run latex once more and
302 // update the dependency structure
303 // -> if not changed:
304 // we do nothing at this point
306 if (rerun || head.sumchange()) {
307 rerun = false;
308 ++count;
309 LYXERR(Debug::DEPEND, "Dep. file has changed or rerun requested");
310 LYXERR(Debug::LATEX, "Run #" << count);
311 message(runMessage(count));
312 startscript();
313 scanres = scanLogFile(terr);
314 if (scanres & ERRORS) {
315 deleteFilesOnError();
316 return scanres; // return on error
319 // update the depedencies
320 deplog(head); // reads the latex log
321 head.update();
322 } else {
323 LYXERR(Debug::DEPEND, "Dep. file has NOT changed");
326 // 3
327 // rerun bibtex?
328 // Complex bibliography packages such as Biblatex require
329 // an additional bibtex cycle sometimes.
330 if (scanres & UNDEF_CIT) {
331 // Here we must scan the .aux file and look for
332 // "\bibdata" and/or "\bibstyle". If one of those
333 // tags is found -> run bibtex and set rerun = true;
334 // no checks for now
335 LYXERR(Debug::LATEX, "Running BibTeX.");
336 message(_("Running BibTeX."));
337 updateBibtexDependencies(head, bibtex_info);
338 rerun |= runBibTeX(bibtex_info);
341 // 4
342 // The inclusion of files generated by external programs such as
343 // makeindex or bibtex might have done changes to pagenumbering,
344 // etc. And because of this we must run the external programs
345 // again to make sure everything is redone correctly.
346 // Also there should be no need to run the external programs any
347 // more after this.
349 // run makeindex if the <file>.idx has changed or was generated.
350 if (head.haschanged(idxfile)) {
351 // no checks for now
352 LYXERR(Debug::LATEX, "Running MakeIndex.");
353 message(_("Running Index Processor."));
354 // onlyFilename() is needed for cygwin
355 rerun = runMakeIndex(onlyFilename(changeExtension(
356 file.absFilename(), ".idx")), runparams);
359 // I am not pretty sure if need this twice.
360 if (head.haschanged(nlofile))
361 rerun |= runMakeIndexNomencl(file, ".nlo", ".nls");
362 if (head.haschanged(glofile))
363 rerun |= runMakeIndexNomencl(file, ".glo", ".gls");
365 // 5
366 // we will only run latex more if the log file asks for it.
367 // or if the sumchange() is true.
368 // -> rerun asked for:
369 // run latex and
370 // remake the dependency file
371 // goto 2 or return if max runs are reached.
372 // -> rerun not asked for:
373 // just return (fall out of bottom of func)
375 while ((head.sumchange() || rerun || (scanres & RERUN))
376 && count < MAX_RUN) {
377 // Yes rerun until message goes away, or until
378 // MAX_RUNS are reached.
379 rerun = false;
380 ++count;
381 LYXERR(Debug::LATEX, "Run #" << count);
382 message(runMessage(count));
383 startscript();
384 scanres = scanLogFile(terr);
385 if (scanres & ERRORS) {
386 deleteFilesOnError();
387 return scanres; // return on error
390 // keep this updated
391 head.update();
394 // Write the dependencies to file.
395 head.write(depfile);
396 LYXERR(Debug::LATEX, "Done.");
397 return scanres;
401 int LaTeX::startscript()
403 // onlyFilename() is needed for cygwin
404 string tmp = cmd + ' '
405 + quoteName(onlyFilename(file.toFilesystemEncoding()))
406 + " > " + os::nulldev();
407 Systemcall one;
408 return one.startscript(Systemcall::Wait, tmp);
412 bool LaTeX::runMakeIndex(string const & f, OutputParams const & runparams,
413 string const & params)
415 LYXERR(Debug::LATEX,
416 "idx file has been made, running makeindex on file " << f);
417 string tmp = lyxrc.index_command + ' ';
419 tmp = subst(tmp, "$$lang", runparams.document_language);
420 tmp += quoteName(f);
421 tmp += params;
422 Systemcall one;
423 one.startscript(Systemcall::Wait, tmp);
424 return true;
428 bool LaTeX::runMakeIndexNomencl(FileName const & file,
429 string const & nlo, string const & nls)
431 LYXERR(Debug::LATEX, "Running MakeIndex for nomencl.");
432 message(_("Running MakeIndex for nomencl."));
433 string tmp = lyxrc.nomencl_command + ' ';
434 // onlyFilename() is needed for cygwin
435 tmp += quoteName(onlyFilename(changeExtension(file.absFilename(), nlo)));
436 tmp += " -o "
437 + onlyFilename(changeExtension(file.toFilesystemEncoding(), nls));
438 Systemcall one;
439 one.startscript(Systemcall::Wait, tmp);
440 return true;
444 vector<AuxInfo> const
445 LaTeX::scanAuxFiles(FileName const & file)
447 vector<AuxInfo> result;
449 result.push_back(scanAuxFile(file));
451 string const basename = removeExtension(file.absFilename());
452 for (int i = 1; i < 1000; ++i) {
453 FileName const file2(basename
454 + '.' + convert<string>(i)
455 + ".aux");
456 if (!file2.exists())
457 break;
458 result.push_back(scanAuxFile(file2));
460 return result;
464 AuxInfo const LaTeX::scanAuxFile(FileName const & file)
466 AuxInfo result;
467 result.aux_file = file;
468 scanAuxFile(file, result);
469 return result;
473 void LaTeX::scanAuxFile(FileName const & file, AuxInfo & aux_info)
475 LYXERR(Debug::LATEX, "Scanning aux file: " << file);
477 ifstream ifs(file.toFilesystemEncoding().c_str());
478 string token;
479 static regex const reg1("\\\\citation\\{([^}]+)\\}");
480 static regex const reg2("\\\\bibdata\\{([^}]+)\\}");
481 static regex const reg3("\\\\bibstyle\\{([^}]+)\\}");
482 static regex const reg4("\\\\@input\\{([^}]+)\\}");
484 while (getline(ifs, token)) {
485 token = rtrim(token, "\r");
486 smatch sub;
487 // FIXME UNICODE: We assume that citation keys and filenames
488 // in the aux file are in the file system encoding.
489 token = to_utf8(from_filesystem8bit(token));
490 if (regex_match(token, sub, reg1)) {
491 string data = sub.str(1);
492 while (!data.empty()) {
493 string citation;
494 data = split(data, citation, ',');
495 LYXERR(Debug::LATEX, "Citation: " << citation);
496 aux_info.citations.insert(citation);
498 } else if (regex_match(token, sub, reg2)) {
499 string data = sub.str(1);
500 // data is now all the bib files separated by ','
501 // get them one by one and pass them to the helper
502 while (!data.empty()) {
503 string database;
504 data = split(data, database, ',');
505 database = changeExtension(database, "bib");
506 LYXERR(Debug::LATEX, "BibTeX database: `" << database << '\'');
507 aux_info.databases.insert(database);
509 } else if (regex_match(token, sub, reg3)) {
510 string style = sub.str(1);
511 // token is now the style file
512 // pass it to the helper
513 style = changeExtension(style, "bst");
514 LYXERR(Debug::LATEX, "BibTeX style: `" << style << '\'');
515 aux_info.styles.insert(style);
516 } else if (regex_match(token, sub, reg4)) {
517 string const file2 = sub.str(1);
518 scanAuxFile(makeAbsPath(file2), aux_info);
524 void LaTeX::updateBibtexDependencies(DepTable & dep,
525 vector<AuxInfo> const & bibtex_info)
527 // Since a run of Bibtex mandates more latex runs it is ok to
528 // remove all ".bib" and ".bst" files.
529 dep.remove_files_with_extension(".bib");
530 dep.remove_files_with_extension(".bst");
531 //string aux = OnlyFilename(ChangeExtension(file, ".aux"));
533 for (vector<AuxInfo>::const_iterator it = bibtex_info.begin();
534 it != bibtex_info.end(); ++it) {
535 for (set<string>::const_iterator it2 = it->databases.begin();
536 it2 != it->databases.end(); ++it2) {
537 FileName const file = findtexfile(*it2, "bib");
538 if (!file.empty())
539 dep.insert(file, true);
542 for (set<string>::const_iterator it2 = it->styles.begin();
543 it2 != it->styles.end(); ++it2) {
544 FileName const file = findtexfile(*it2, "bst");
545 if (!file.empty())
546 dep.insert(file, true);
552 bool LaTeX::runBibTeX(vector<AuxInfo> const & bibtex_info)
554 bool result = false;
555 for (vector<AuxInfo>::const_iterator it = bibtex_info.begin();
556 it != bibtex_info.end(); ++it) {
557 if (it->databases.empty())
558 continue;
559 result = true;
561 string tmp = lyxrc.bibtex_command + " ";
562 // onlyFilename() is needed for cygwin
563 tmp += quoteName(onlyFilename(removeExtension(
564 it->aux_file.absFilename())));
565 Systemcall one;
566 one.startscript(Systemcall::Wait, tmp);
568 // Return whether bibtex was run
569 return result;
573 int LaTeX::scanLogFile(TeXErrors & terr)
575 int last_line = -1;
576 int line_count = 1;
577 int retval = NO_ERRORS;
578 string tmp =
579 onlyFilename(changeExtension(file.absFilename(), ".log"));
580 LYXERR(Debug::LATEX, "Log file: " << tmp);
581 FileName const fn = FileName(makeAbsPath(tmp));
582 ifstream ifs(fn.toFilesystemEncoding().c_str());
583 bool fle_style = false;
584 static regex file_line_error(".+\\.\\D+:[0-9]+: (.+)");
586 string token;
587 while (getline(ifs, token)) {
588 // MikTeX sometimes inserts \0 in the log file. They can't be
589 // removed directly with the existing string utility
590 // functions, so convert them first to \r, and remove all
591 // \r's afterwards, since we need to remove them anyway.
592 token = subst(token, '\0', '\r');
593 token = subst(token, "\r", "");
594 smatch sub;
596 LYXERR(Debug::LATEX, "Log line: " << token);
598 if (token.empty())
599 continue;
601 if (contains(token, "file:line:error style messages enabled"))
602 fle_style = true;
604 if (prefixIs(token, "LaTeX Warning:") ||
605 prefixIs(token, "! pdfTeX warning")) {
606 // Here shall we handle different
607 // types of warnings
608 retval |= LATEX_WARNING;
609 LYXERR(Debug::LATEX, "LaTeX Warning.");
610 if (contains(token, "Rerun to get cross-references")) {
611 retval |= RERUN;
612 LYXERR(Debug::LATEX, "We should rerun.");
613 // package clefval needs 2 latex runs before bibtex
614 } else if (contains(token, "Value of")
615 && contains(token, "on page")
616 && contains(token, "undefined")) {
617 retval |= ERROR_RERUN;
618 LYXERR(Debug::LATEX, "Force rerun.");
619 } else if (contains(token, "Citation")
620 && contains(token, "on page")
621 && contains(token, "undefined")) {
622 retval |= UNDEF_CIT;
624 } else if (prefixIs(token, "Package")) {
625 // Package warnings
626 retval |= PACKAGE_WARNING;
627 if (contains(token, "natbib Warning:")) {
628 // Natbib warnings
629 if (contains(token, "Citation")
630 && contains(token, "on page")
631 && contains(token, "undefined")) {
632 retval |= UNDEF_CIT;
634 } else if (contains(token, "run BibTeX")) {
635 retval |= UNDEF_CIT;
636 } else if (contains(token, "Rerun LaTeX") ||
637 contains(token, "Rerun to get")) {
638 // at least longtable.sty and bibtopic.sty
639 // might use this.
640 LYXERR(Debug::LATEX, "We should rerun.");
641 retval |= RERUN;
643 } else if (token[0] == '(') {
644 if (contains(token, "Rerun LaTeX") ||
645 contains(token, "Rerun to get")) {
646 // Used by natbib
647 LYXERR(Debug::LATEX, "We should rerun.");
648 retval |= RERUN;
650 } else if (prefixIs(token, "! ")
651 || (fle_style && regex_match(token, sub, file_line_error))) {
652 // Ok, we have something that looks like a TeX Error
653 // but what do we really have.
655 // Just get the error description:
656 string desc;
657 if (prefixIs(token, "! "))
658 desc = string(token, 2);
659 else if (fle_style)
660 desc = sub.str();
661 if (contains(token, "LaTeX Error:"))
662 retval |= LATEX_ERROR;
663 // get the next line
664 string tmp;
665 int count = 0;
666 do {
667 if (!getline(ifs, tmp))
668 break;
669 if (++count > 10)
670 break;
671 } while (!prefixIs(tmp, "l."));
672 if (prefixIs(tmp, "l.")) {
673 // we have a latex error
674 retval |= TEX_ERROR;
675 if (contains(desc,
676 "Package babel Error: You haven't defined the language") ||
677 contains(desc,
678 "Package babel Error: You haven't loaded the option"))
679 retval |= ERROR_RERUN;
680 // get the line number:
681 int line = 0;
682 sscanf(tmp.c_str(), "l.%d", &line);
683 // get the rest of the message:
684 string errstr(tmp, tmp.find(' '));
685 errstr += '\n';
686 getline(ifs, tmp);
687 while (!contains(errstr, "l.")
688 && !tmp.empty()
689 && !prefixIs(tmp, "! ")
690 && !contains(tmp, "(job aborted")) {
691 errstr += tmp;
692 errstr += "\n";
693 getline(ifs, tmp);
695 LYXERR(Debug::LATEX, "line: " << line << '\n'
696 << "Desc: " << desc << '\n' << "Text: " << errstr);
697 if (line == last_line)
698 ++line_count;
699 else {
700 line_count = 1;
701 last_line = line;
703 if (line_count <= 5) {
704 // FIXME UNICODE
705 // We have no idea what the encoding of
706 // the log file is.
707 // It seems that the output from the
708 // latex compiler itself is pure ASCII,
709 // but it can include bits from the
710 // document, so whatever encoding we
711 // assume here it can be wrong.
712 terr.insertError(line,
713 from_local8bit(desc),
714 from_local8bit(errstr));
715 ++num_errors;
718 } else {
719 // information messages, TeX warnings and other
720 // warnings we have not caught earlier.
721 if (prefixIs(token, "Overfull ")) {
722 retval |= TEX_WARNING;
723 } else if (prefixIs(token, "Underfull ")) {
724 retval |= TEX_WARNING;
725 } else if (contains(token, "Rerun to get citations")) {
726 // Natbib seems to use this.
727 retval |= UNDEF_CIT;
728 } else if (contains(token, "No pages of output")) {
729 // A dvi file was not created
730 retval |= NO_OUTPUT;
731 } else if (contains(token, "That makes 100 errors")) {
732 // More than 100 errors were reprted
733 retval |= TOO_MANY_ERRORS;
737 LYXERR(Debug::LATEX, "Log line: " << token);
738 return retval;
742 namespace {
744 bool insertIfExists(FileName const & absname, DepTable & head)
746 if (absname.exists() && !absname.isDirectory()) {
747 head.insert(absname, true);
748 return true;
750 return false;
754 bool handleFoundFile(string const & ff, DepTable & head)
756 // convert from native os path to unix path
757 string foundfile = os::internal_path(trim(ff));
759 LYXERR(Debug::DEPEND, "Found file: " << foundfile);
761 // Ok now we found a file.
762 // Now we should make sure that this is a file that we can
763 // access through the normal paths.
764 // We will not try any fancy search methods to
765 // find the file.
767 // (1) foundfile is an
768 // absolute path and should
769 // be inserted.
770 FileName absname(foundfile);
771 if (absname.isAbsolute()) {
772 LYXERR(Debug::DEPEND, "AbsolutePath file: " << foundfile);
773 // On initial insert we want to do the update at once
774 // since this file cannot be a file generated by
775 // the latex run.
776 if (!insertIfExists(absname, head)) {
777 // check for spaces
778 string strippedfile = foundfile;
779 while (contains(strippedfile, " ")) {
780 // files with spaces are often enclosed in quotation
781 // marks; those have to be removed
782 string unquoted = subst(strippedfile, "\"", "");
783 absname.set(unquoted);
784 if (insertIfExists(absname, head))
785 return true;
786 // strip off part after last space and try again
787 string tmp = strippedfile;
788 string const stripoff =
789 rsplit(tmp, strippedfile, ' ');
790 absname.set(strippedfile);
791 if (insertIfExists(absname, head))
792 return true;
797 string onlyfile = onlyFilename(foundfile);
798 absname = makeAbsPath(onlyfile);
800 // check for spaces
801 while (contains(foundfile, ' ')) {
802 if (absname.exists())
803 // everything o.k.
804 break;
805 else {
806 // files with spaces are often enclosed in quotation
807 // marks; those have to be removed
808 string unquoted = subst(foundfile, "\"", "");
809 absname = makeAbsPath(unquoted);
810 if (absname.exists())
811 break;
812 // strip off part after last space and try again
813 string strippedfile;
814 string const stripoff =
815 rsplit(foundfile, strippedfile, ' ');
816 foundfile = strippedfile;
817 onlyfile = onlyFilename(strippedfile);
818 absname = makeAbsPath(onlyfile);
822 // (2) foundfile is in the tmpdir
823 // insert it into head
824 if (absname.exists() && !absname.isDirectory()) {
825 // FIXME: This regex contained glo, but glo is used by the old
826 // version of nomencl.sty. Do we need to put it back?
827 static regex const unwanted("^.*\\.(aux|log|dvi|bbl|ind)$");
828 if (regex_match(onlyfile, unwanted)) {
829 LYXERR(Debug::DEPEND, "We don't want " << onlyfile
830 << " in the dep file");
831 } else if (suffixIs(onlyfile, ".tex")) {
832 // This is a tex file generated by LyX
833 // and latex is not likely to change this
834 // during its runs.
835 LYXERR(Debug::DEPEND, "Tmpdir TeX file: " << onlyfile);
836 head.insert(absname, true);
837 } else {
838 LYXERR(Debug::DEPEND, "In tmpdir file:" << onlyfile);
839 head.insert(absname);
841 return true;
842 } else {
843 LYXERR(Debug::DEPEND, "Not a file or we are unable to find it.");
844 return false;
849 bool checkLineBreak(string const & ff, DepTable & head)
851 if (!contains(ff, '.'))
852 return false;
854 // if we have a dot, we let handleFoundFile decide
855 return handleFoundFile(ff, head);
858 } // anon namespace
861 void LaTeX::deplog(DepTable & head)
863 // This function reads the LaTeX log file end extracts all the
864 // external files used by the LaTeX run. The files are then
865 // entered into the dependency file.
867 string const logfile =
868 onlyFilename(changeExtension(file.absFilename(), ".log"));
870 static regex const reg1("File: (.+).*");
871 static regex const reg2("No file (.+)(.).*");
872 static regex const reg3("\\\\openout[0-9]+.*=.*`(.+)(..).*");
873 // If an index should be created, MikTex does not write a line like
874 // \openout# = 'sample.idx'.
875 // but instead only a line like this into the log:
876 // Writing index file sample.idx
877 static regex const reg4("Writing index file (.+).*");
878 // files also can be enclosed in <...>
879 static regex const reg5("<([^>]+)(.).*");
880 static regex const regoldnomencl("Writing glossary file (.+).*");
881 static regex const regnomencl("Writing nomenclature file (.+).*");
882 // If a toc should be created, MikTex does not write a line like
883 // \openout# = `sample.toc'.
884 // but only a line like this into the log:
885 // \tf@toc=\write#
886 // This line is also written by tetex.
887 // This line is not present if no toc should be created.
888 static regex const miktexTocReg("\\\\tf@toc=\\\\write.*");
889 static regex const reg6(".*\\([^)]+.*");
891 FileName const fn = makeAbsPath(logfile);
892 ifstream ifs(fn.toFilesystemEncoding().c_str());
893 string lastline;
894 while (ifs) {
895 // Ok, the scanning of files here is not sufficient.
896 // Sometimes files are named by "File: xxx" only
897 // So I think we should use some regexps to find files instead.
898 // Note: all file names and paths might contains spaces.
899 bool found_file = false;
900 string token;
901 getline(ifs, token);
902 // MikTeX sometimes inserts \0 in the log file. They can't be
903 // removed directly with the existing string utility
904 // functions, so convert them first to \r, and remove all
905 // \r's afterwards, since we need to remove them anyway.
906 token = subst(token, '\0', '\r');
907 token = subst(token, "\r", "");
908 if (token.empty() || token == ")") {
909 lastline = string();
910 continue;
913 // Sometimes, filenames are broken across lines.
914 // We care for that and save suspicious lines.
915 // Here we exclude some cases where we are sure
916 // that there is no continued filename
917 if (!lastline.empty()) {
918 static regex const package_info("Package \\w+ Info: .*");
919 static regex const package_warning("Package \\w+ Warning: .*");
920 if (prefixIs(token, "File:") || prefixIs(token, "(Font)")
921 || prefixIs(token, "Package:")
922 || prefixIs(token, "Language:")
923 || prefixIs(token, "LaTeX Info:")
924 || prefixIs(token, "LaTeX Font Info:")
925 || prefixIs(token, "\\openout[")
926 || prefixIs(token, "))")
927 || regex_match(token, package_info)
928 || regex_match(token, package_warning))
929 lastline = string();
932 if (!lastline.empty())
933 // probably a continued filename from last line
934 token = lastline + token;
935 if (token.length() > 255) {
936 // string too long. Cut off.
937 token.erase(0, token.length() - 251);
940 smatch sub;
942 // FIXME UNICODE: We assume that the file names in the log
943 // file are in the file system encoding.
944 token = to_utf8(from_filesystem8bit(token));
946 // (1) "File: file.ext"
947 if (regex_match(token, sub, reg1)) {
948 // check for dot
949 found_file = checkLineBreak(sub.str(1), head);
950 // However, ...
951 if (suffixIs(token, ")"))
952 // no line break for sure
953 // pretend we've been succesfully searching
954 found_file = true;
955 // (2) "No file file.ext"
956 } else if (regex_match(token, sub, reg2)) {
957 // file names must contains a dot, line ends with dot
958 if (contains(sub.str(1), '.') && sub.str(2) == ".")
959 found_file = handleFoundFile(sub.str(1), head);
960 else
961 // we suspect a line break
962 found_file = false;
963 // (3) "\openout<nr> = `file.ext'."
964 } else if (regex_match(token, sub, reg3)) {
965 // search for closing '. at the end of the line
966 if (sub.str(2) == "\'.")
967 found_file = handleFoundFile(sub.str(1), head);
968 else
969 // probable line break
970 found_file = false;
971 // (4) "Writing index file file.ext"
972 } else if (regex_match(token, sub, reg4))
973 // check for dot
974 found_file = checkLineBreak(sub.str(1), head);
975 // (5) "<file.ext>"
976 else if (regex_match(token, sub, reg5)) {
977 // search for closing '>' and dot ('*.*>') at the eol
978 if (contains(sub.str(1), '.') && sub.str(2) == ">")
979 found_file = handleFoundFile(sub.str(1), head);
980 else
981 // probable line break
982 found_file = false;
983 // (6) "Writing nomenclature file file.ext"
984 } else if (regex_match(token, sub, regnomencl) ||
985 regex_match(token, sub, regoldnomencl))
986 // check for dot
987 found_file = checkLineBreak(sub.str(1), head);
988 // (7) "\tf@toc=\write<nr>" (for MikTeX)
989 else if (regex_match(token, sub, miktexTocReg))
990 found_file = handleFoundFile(onlyFilename(changeExtension(
991 file.absFilename(), ".toc")), head);
992 else
993 // not found, but we won't check further
994 // pretend we've been succesfully searching
995 found_file = true;
997 // (8) "(file.ext"
998 // note that we can have several of these on one line
999 // this must be queried separated, because of
1000 // cases such as "File: file.ext (type eps)"
1001 // where "File: file.ext" would be skipped
1002 if (regex_match(token, sub, reg6)) {
1003 // search for strings in (...)
1004 static regex reg6_1("\\(([^()]+)(.).*");
1005 smatch what;
1006 string::const_iterator first = token.begin();
1007 string::const_iterator end = token.end();
1009 while (regex_search(first, end, what, reg6_1)) {
1010 // if we have a dot, try to handle as file
1011 if (contains(what.str(1), '.')) {
1012 first = what[0].second;
1013 if (what.str(2) == ")") {
1014 handleFoundFile(what.str(1), head);
1015 // since we had a closing bracket,
1016 // do not investigate further
1017 found_file = true;
1018 } else
1019 // if we have no closing bracket,
1020 // try to handle as file nevertheless
1021 found_file = handleFoundFile(
1022 what.str(1) + what.str(2), head);
1024 // if we do not have a dot, check if the line has
1025 // a closing bracket (else, we suspect a line break)
1026 else if (what.str(2) != ")") {
1027 first = what[0].second;
1028 found_file = false;
1029 } else {
1030 // we have a closing bracket, so the content
1031 // is not a file name.
1032 // no need to investigate further
1033 // pretend we've been succesfully searching
1034 first = what[0].second;
1035 found_file = true;
1040 if (!found_file)
1041 // probable linebreak:
1042 // save this line
1043 lastline = token;
1044 else
1045 // no linebreak: reset
1046 lastline = string();
1049 // Make sure that the main .tex file is in the dependency file.
1050 head.insert(file, true);
1054 } // namespace lyx